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

Constructor & Destructor Documentation

◆ GL_BITMAP_CACHE()

KIGFX::GL_BITMAP_CACHE::GL_BITMAP_CACHE ( )
inline

Definition at line 85 of file opengl_gal.cpp.

86  {
87  }

◆ ~GL_BITMAP_CACHE()

GL_BITMAP_CACHE::~GL_BITMAP_CACHE ( )

Definition at line 109 of file opengl_gal.cpp.

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

Member Function Documentation

◆ cacheBitmap()

GLuint GL_BITMAP_CACHE::cacheBitmap ( const BITMAP_BASE aBitmap)
private

Definition at line 134 of file opengl_gal.cpp.

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

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

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

Member Data Documentation

◆ m_bitmaps

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

Definition at line 103 of file opengl_gal.cpp.


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