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

Constructor & Destructor Documentation

◆ GL_BITMAP_CACHE()

KIGFX::GL_BITMAP_CACHE::GL_BITMAP_CACHE ( )
inline

Definition at line 77 of file opengl_gal.cpp.

78  {
79  }

◆ ~GL_BITMAP_CACHE()

GL_BITMAP_CACHE::~GL_BITMAP_CACHE ( )

Definition at line 101 of file opengl_gal.cpp.

102 {
103  for ( auto b = m_bitmaps.begin(); b != m_bitmaps.end(); ++b )
104  glDeleteTextures( 1, &b->second.id );
105 }
std::map< const BITMAP_BASE *, CACHED_BITMAP > m_bitmaps
Definition: opengl_gal.cpp:95

Member Function Documentation

◆ cacheBitmap()

GLuint GL_BITMAP_CACHE::cacheBitmap ( const BITMAP_BASE aBitmap)
private

Definition at line 126 of file opengl_gal.cpp.

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

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

109 {
110  auto it = m_bitmaps.find( aBitmap) ;
111 
112  if ( it != m_bitmaps.end() )
113  {
114  // A bitmap is found in cache bitmap.
115  // Ensure the associated texture is still valide (can be destoyed somewhere)
116  if( glIsTexture( it->second.id ) )
117  return it->second.id;
118 
119  // else if not valid, it will be recreated.
120  }
121 
122  return cacheBitmap( aBitmap );
123 }
GLuint cacheBitmap(const BITMAP_BASE *aBitmap)
Definition: opengl_gal.cpp:126
std::map< const BITMAP_BASE *, CACHED_BITMAP > m_bitmaps
Definition: opengl_gal.cpp:95

Member Data Documentation

◆ m_bitmaps

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

Definition at line 95 of file opengl_gal.cpp.


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