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

Constructor & Destructor Documentation

◆ GL_BITMAP_CACHE()

KIGFX::GL_BITMAP_CACHE::GL_BITMAP_CACHE ( )
inline

Definition at line 87 of file opengl_gal.cpp.

88  {
89  }

◆ ~GL_BITMAP_CACHE()

GL_BITMAP_CACHE::~GL_BITMAP_CACHE ( )

Definition at line 111 of file opengl_gal.cpp.

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

Member Function Documentation

◆ cacheBitmap()

GLuint GL_BITMAP_CACHE::cacheBitmap ( const BITMAP_BASE aBitmap)
private

Definition at line 136 of file opengl_gal.cpp.

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

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

Member Data Documentation

◆ m_bitmaps

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

Definition at line 105 of file opengl_gal.cpp.


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