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 90 of file opengl_gal.cpp.

Constructor & Destructor Documentation

◆ GL_BITMAP_CACHE()

KIGFX::GL_BITMAP_CACHE::GL_BITMAP_CACHE ( )
inline

Definition at line 93 of file opengl_gal.cpp.

93 {}

◆ ~GL_BITMAP_CACHE()

GL_BITMAP_CACHE::~GL_BITMAP_CACHE ( )

Definition at line 114 of file opengl_gal.cpp.

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

Member Function Documentation

◆ cacheBitmap()

GLuint GL_BITMAP_CACHE::cacheBitmap ( const BITMAP_BASE aBitmap)
private

Definition at line 139 of file opengl_gal.cpp.

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

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

Member Data Documentation

◆ m_bitmaps

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

Definition at line 108 of file opengl_gal.cpp.


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