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

Constructor & Destructor Documentation

◆ GL_BITMAP_CACHE()

KIGFX::GL_BITMAP_CACHE::GL_BITMAP_CACHE ( )
inline

Definition at line 78 of file opengl_gal.cpp.

79  {
80  }

◆ ~GL_BITMAP_CACHE()

GL_BITMAP_CACHE::~GL_BITMAP_CACHE ( )

Definition at line 102 of file opengl_gal.cpp.

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

Member Function Documentation

◆ cacheBitmap()

GLuint GL_BITMAP_CACHE::cacheBitmap ( const BITMAP_BASE aBitmap)
private

Definition at line 127 of file opengl_gal.cpp.

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.

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

◆ RequestBitmap()

GLuint GL_BITMAP_CACHE::RequestBitmap ( const BITMAP_BASE aBitmap)

Definition at line 109 of file opengl_gal.cpp.

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

Member Data Documentation

◆ m_bitmaps

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

Definition at line 96 of file opengl_gal.cpp.


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