KiCad PCB EDA Suite
KIGFX::GL_BITMAP_CACHE Class Reference

Classes

struct  CACHED_BITMAP
 

Public Member Functions

 GL_BITMAP_CACHE ()
 
 ~GL_BITMAP_CACHE ()
 
GLuint RequestBitmap (const BITMAP_BASE *aBitmap)
 

Private Member Functions

GLuint cacheBitmap (const BITMAP_BASE *aBitmap)
 

Private Attributes

std::map< const BITMAP_BASE *, CACHED_BITMAPm_bitmaps
 

Detailed Description

Definition at line 88 of file opengl_gal.cpp.

Constructor & Destructor Documentation

◆ GL_BITMAP_CACHE()

KIGFX::GL_BITMAP_CACHE::GL_BITMAP_CACHE ( )
inline

Definition at line 91 of file opengl_gal.cpp.

91 {}

◆ ~GL_BITMAP_CACHE()

GL_BITMAP_CACHE::~GL_BITMAP_CACHE ( )

Definition at line 112 of file opengl_gal.cpp.

113 {
114  for( auto& bitmap : m_bitmaps )
115  glDeleteTextures( 1, &bitmap.second.id );
116 }
std::map< const BITMAP_BASE *, CACHED_BITMAP > m_bitmaps
Definition: opengl_gal.cpp:106

Member Function Documentation

◆ cacheBitmap()

GLuint GL_BITMAP_CACHE::cacheBitmap ( const BITMAP_BASE aBitmap)
private

Definition at line 137 of file opengl_gal.cpp.

138 {
139  CACHED_BITMAP bmp;
140 
141  bmp.w = aBitmap->GetSizePixels().x;
142  bmp.h = aBitmap->GetSizePixels().y;
143 
144  // The bitmap size needs to be a multiple of 4.
145  // This is easiest to achieve by ensuring that each row
146  // has a multiple of 4 pixels
147  int extra_w = bmp.w % 4;
148 
149  if( extra_w )
150  extra_w = 4 - extra_w;
151 
152  GLuint textureID;
153  glGenTextures( 1, &textureID );
154 
155  // make_unique initializes this to 0, so extra pixels are transparent
156  auto buf = std::make_unique<uint8_t[]>( ( bmp.w + extra_w ) * bmp.h * 4 );
157  const wxImage& imgData = *aBitmap->GetImageData();
158 
159  for( int y = 0; y < bmp.h; y++ )
160  {
161  for( int x = 0; x < bmp.w; x++ )
162  {
163  uint8_t* p = buf.get() + ( ( bmp.w + extra_w ) * y + x ) * 4;
164 
165  p[0] = imgData.GetRed( x, y );
166  p[1] = imgData.GetGreen( x, y );
167  p[2] = imgData.GetBlue( x, y );
168 
169  if( imgData.HasAlpha() )
170  p[3] = imgData.GetAlpha( x, y );
171  else if( imgData.HasMask() && p[0] == imgData.GetMaskRed()
172  && p[1] == imgData.GetMaskGreen() && p[2] == imgData.GetMaskBlue() )
173  p[3] = wxALPHA_TRANSPARENT;
174  else
175  p[3] = wxALPHA_OPAQUE;
176  }
177  }
178 
179  glBindTexture( GL_TEXTURE_2D, textureID );
180  glTexImage2D( GL_TEXTURE_2D, 0, GL_RGBA8, bmp.w + extra_w, bmp.h, 0, GL_RGBA, GL_UNSIGNED_BYTE,
181  buf.get() );
182 
183  glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST );
184  glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST );
185 
186  bmp.id = textureID;
187 
188 #ifndef DISABLE_BITMAP_CACHE
189  m_bitmaps[aBitmap] = bmp;
190 #endif
191 
192  return textureID;
193 }
wxImage * GetImageData()
Definition: bitmap_base.h:70
std::map< const BITMAP_BASE *, CACHED_BITMAP > m_bitmaps
Definition: opengl_gal.cpp:106
wxSize GetSizePixels() const
Definition: bitmap_base.h:124

References BITMAP_BASE::GetImageData(), BITMAP_BASE::GetSizePixels(), KIGFX::GL_BITMAP_CACHE::CACHED_BITMAP::h, KIGFX::GL_BITMAP_CACHE::CACHED_BITMAP::id, and KIGFX::GL_BITMAP_CACHE::CACHED_BITMAP::w.

◆ RequestBitmap()

GLuint GL_BITMAP_CACHE::RequestBitmap ( const BITMAP_BASE aBitmap)

Definition at line 119 of file opengl_gal.cpp.

120 {
121  auto it = m_bitmaps.find( aBitmap );
122 
123  if( it != m_bitmaps.end() )
124  {
125  // A bitmap is found in cache bitmap.
126  // Ensure the associated texture is still valid (can be destroyed somewhere)
127  if( glIsTexture( it->second.id ) )
128  return it->second.id;
129 
130  // else if not valid, it will be recreated.
131  }
132 
133  return cacheBitmap( aBitmap );
134 }
GLuint cacheBitmap(const BITMAP_BASE *aBitmap)
Definition: opengl_gal.cpp:137
std::map< const BITMAP_BASE *, CACHED_BITMAP > m_bitmaps
Definition: opengl_gal.cpp:106

Member Data Documentation

◆ m_bitmaps

std::map<const BITMAP_BASE*, CACHED_BITMAP> KIGFX::GL_BITMAP_CACHE::m_bitmaps
private

Definition at line 106 of file opengl_gal.cpp.


The documentation for this class was generated from the following file: