KiCad PCB EDA Suite
KIGFX::CACHED_CONTAINER Class Referenceabstract

Class to store VERTEX instances with caching. More...

#include <cached_container.h>

Inheritance diagram for KIGFX::CACHED_CONTAINER:
KIGFX::VERTEX_CONTAINER KIGFX::CACHED_CONTAINER_GPU KIGFX::CACHED_CONTAINER_RAM

Public Member Functions

 CACHED_CONTAINER (unsigned int aSize=DEFAULT_SIZE)
 
virtual ~CACHED_CONTAINER ()
 
bool IsCached () const override
 Return true if the container caches vertex data in RAM or video memory. More...
 
virtual void SetItem (VERTEX_ITEM *aItem) override
 Clean up after adding an item. More...
 
virtual void FinishItem () override
 Clean up after adding an item. More...
 
virtual VERTEXAllocate (unsigned int aSize) override
 Return allocated space for the requested number of vertices associated with the current item (set with SetItem()). More...
 
virtual void Delete (VERTEX_ITEM *aItem) override
 Remove all data stored in the container and restores its original state. More...
 
virtual void Clear () override
 Remove all data stored in the container and restores its original state. More...
 
virtual unsigned int GetBufferHandle () const =0
 Return handle to the vertex buffer. More...
 
virtual bool IsMapped () const =0
 Return true if vertex buffer is currently mapped. More...
 
virtual void Map () override=0
 Finish the vertices updates stage. More...
 
virtual void Unmap () override=0
 Finish the vertices updates stage. More...
 
virtual unsigned int AllItemsSize () const
 
VERTEXGetAllVertices () const
 Return pointer to the vertices stored in the container. More...
 
virtual VERTEXGetVertices (unsigned int aOffset) const
 Return vertices stored at the specific offset. More...
 
virtual unsigned int GetSize () const
 Return amount of vertices currently stored in the container. More...
 
bool IsDirty () const
 Return information about the container cache state. More...
 
void SetDirty ()
 Set the dirty flag, so vertices in the container are going to be reuploaded to the GPU on the next frame. More...
 
void ClearDirty ()
 Clear the dirty flag to prevent reuploading vertices to the GPU memory. More...
 

Static Public Member Functions

static VERTEX_CONTAINERMakeContainer (bool aCached)
 Return a pointer to a new container of an appropriate type. More...
 

Protected Types

typedef std::pair< unsigned int, unsigned int > CHUNK
 < Maps size of free memory chunks to their offsets More...
 
typedef std::multimap< unsigned int, unsigned int > FREE_CHUNK_MAP
 
typedef std::set< VERTEX_ITEM * > ITEMS
 List of all the stored items. More...
 

Protected Member Functions

bool reallocate (unsigned int aSize)
 Resize the chunk that stores the current item to the given size. More...
 
virtual bool defragmentResize (unsigned int aNewSize)=0
 Remove empty spaces between chunks and optionally resizes the container. More...
 
void defragment (VERTEX *aTarget)
 Transfer all stored data to a new buffer, removing empty spaces between the data chunks in the container. More...
 
void mergeFreeChunks ()
 Look for consecutive free memory chunks and merges them, decreasing fragmentation of memory. More...
 
int getChunkSize (const CHUNK &aChunk) const
 Return the size of a chunk. More...
 
unsigned int getChunkOffset (const CHUNK &aChunk) const
 Return the offset of a chunk. More...
 
void addFreeChunk (unsigned int aOffset, unsigned int aSize)
 Add a chunk marked as a free space. More...
 
unsigned int usedSpace () const
 Return size of the used memory space. More...
 

Protected Attributes

FREE_CHUNK_MAP m_freeChunks
 Stored VERTEX_ITEMs. More...
 
ITEMS m_items
 Currently modified item. More...
 
VERTEX_ITEMm_item
 Properties of currently modified chunk & item. More...
 
unsigned int m_chunkSize
 
unsigned int m_chunkOffset
 Maximal vertex index number stored in the container. More...
 
unsigned int m_maxIndex
 
unsigned int m_freeSpace
 Current container size, expressed in vertices. More...
 
unsigned int m_currentSize
 Store the initial size, so it can be resized to this on Clear() More...
 
unsigned int m_initialSize
 Actual storage memory. More...
 
VERTEXm_vertices
 
bool m_failed
 
bool m_dirty
 Default initial size of a container (expressed in vertices) More...
 

Static Protected Attributes

static constexpr unsigned int DEFAULT_SIZE = 1048576
 

Private Member Functions

void showFreeChunks ()
 Debug & test functions. More...
 
void showUsedChunks ()
 
void test ()
 

Detailed Description

Class to store VERTEX instances with caching.

It associates VERTEX objects and with VERTEX_ITEMs. Caching vertices data in the memory and a enables fast reuse of that data.

Definition at line 46 of file cached_container.h.

Member Typedef Documentation

◆ CHUNK

typedef std::pair<unsigned int, unsigned int> KIGFX::CACHED_CONTAINER::CHUNK
protected

< Maps size of free memory chunks to their offsets

Definition at line 101 of file cached_container.h.

◆ FREE_CHUNK_MAP

typedef std::multimap<unsigned int, unsigned int> KIGFX::CACHED_CONTAINER::FREE_CHUNK_MAP
protected

Definition at line 102 of file cached_container.h.

◆ ITEMS

typedef std::set<VERTEX_ITEM*> KIGFX::CACHED_CONTAINER::ITEMS
protected

List of all the stored items.

Definition at line 105 of file cached_container.h.

Constructor & Destructor Documentation

◆ CACHED_CONTAINER()

CACHED_CONTAINER::CACHED_CONTAINER ( unsigned int  aSize = DEFAULT_SIZE)

Definition at line 50 of file cached_container.cpp.

50  :
51  VERTEX_CONTAINER( aSize ),
52  m_item( nullptr ),
53  m_chunkSize( 0 ),
54  m_chunkOffset( 0 ),
55  m_maxIndex( 0 )
56 {
57  // In the beginning there is only free space
58  m_freeChunks.insert( std::make_pair( aSize, 0 ) );
59 }
VERTEX_CONTAINER(unsigned int aSize=DEFAULT_SIZE)
VERTEX_ITEM * m_item
Properties of currently modified chunk & item.
FREE_CHUNK_MAP m_freeChunks
Stored VERTEX_ITEMs.
unsigned int m_chunkOffset
Maximal vertex index number stored in the container.

References m_freeChunks.

◆ ~CACHED_CONTAINER()

virtual KIGFX::CACHED_CONTAINER::~CACHED_CONTAINER ( )
inlinevirtual

Definition at line 50 of file cached_container.h.

50 {}

Member Function Documentation

◆ addFreeChunk()

void CACHED_CONTAINER::addFreeChunk ( unsigned int  aOffset,
unsigned int  aSize 
)
protected

Add a chunk marked as a free space.

Store size & offset of free chunks.

Definition at line 360 of file cached_container.cpp.

361 {
362  assert( aOffset + aSize <= m_currentSize );
363  assert( aSize > 0 );
364 
365  m_freeChunks.insert( std::make_pair( aSize, aOffset ) );
366  m_freeSpace += aSize;
367 }
unsigned int m_currentSize
Store the initial size, so it can be resized to this on Clear()
unsigned int m_freeSpace
Current container size, expressed in vertices.
FREE_CHUNK_MAP m_freeChunks
Stored VERTEX_ITEMs.

References KIGFX::VERTEX_CONTAINER::m_currentSize, m_freeChunks, and KIGFX::VERTEX_CONTAINER::m_freeSpace.

Referenced by Delete(), FinishItem(), and reallocate().

◆ AllItemsSize()

virtual unsigned int KIGFX::CACHED_CONTAINER::AllItemsSize ( ) const
inlinevirtual

Reimplemented in KIGFX::CACHED_CONTAINER_GPU.

Definition at line 97 of file cached_container.h.

97 { return 0; }

Referenced by KIGFX::GPU_CACHED_MANAGER::EndDrawing().

◆ Allocate()

VERTEX * CACHED_CONTAINER::Allocate ( unsigned int  aSize)
overridevirtual

Return allocated space for the requested number of vertices associated with the current item (set with SetItem()).

The allocated space is added at the end of the chunk used by the current item and may serve to store new vertices.

Parameters
aSizeis the number of vertices to be allocated.
Returns
Pointer to the allocated space.
Exceptions
bad_allocexception if allocation fails.Erase the data related to an item.
Parameters
aItemis the item to be erased.

Implements KIGFX::VERTEX_CONTAINER.

Definition at line 107 of file cached_container.cpp.

108 {
109  assert( m_item != nullptr );
110  assert( IsMapped() );
111 
112  if( m_failed )
113  return nullptr;
114 
115  unsigned int itemSize = m_item->GetSize();
116  unsigned int newSize = itemSize + aSize;
117 
118  if( newSize > m_chunkSize )
119  {
120  // There is not enough space in the currently reserved chunk, so we have to resize it
121  if( !reallocate( newSize ) )
122  {
123  m_failed = true;
124  return nullptr;
125  }
126  }
127 
128  VERTEX* reserved = &m_vertices[m_chunkOffset + itemSize];
129 
130  // Now the item officially possesses the memory chunk
131  m_item->setSize( newSize );
132 
133  // The content has to be updated
134  m_dirty = true;
135 
136 #if CACHED_CONTAINER_TEST > 0
137  test();
138 #endif
139 #if CACHED_CONTAINER_TEST > 2
140  showFreeChunks();
141  showUsedChunks();
142 #endif
143 
144  return reserved;
145 }
void showFreeChunks()
Debug & test functions.
unsigned int GetSize() const
Return information about number of vertices stored.
Definition: vertex_item.h:58
void setSize(unsigned int aSize)
Set data size in the container.
Definition: vertex_item.h:94
VERTEX_ITEM * m_item
Properties of currently modified chunk & item.
bool reallocate(unsigned int aSize)
Resize the chunk that stores the current item to the given size.
bool m_dirty
Default initial size of a container (expressed in vertices)
virtual bool IsMapped() const =0
Return true if vertex buffer is currently mapped.
unsigned int m_chunkOffset
Maximal vertex index number stored in the container.

References KIGFX::VERTEX_ITEM::GetSize(), IsMapped(), m_chunkOffset, m_chunkSize, KIGFX::VERTEX_CONTAINER::m_dirty, KIGFX::VERTEX_CONTAINER::m_failed, m_item, KIGFX::VERTEX_CONTAINER::m_vertices, reallocate(), KIGFX::VERTEX_ITEM::setSize(), showFreeChunks(), showUsedChunks(), and test().

◆ Clear()

void CACHED_CONTAINER::Clear ( )
overridevirtual

Remove all data stored in the container and restores its original state.

Implements KIGFX::VERTEX_CONTAINER.

Definition at line 189 of file cached_container.cpp.

190 {
192  m_maxIndex = 0;
193  m_failed = false;
194 
195  // Set the size of all the stored VERTEX_ITEMs to 0, so it is clear that they are not held
196  // in the container anymore
197  for( ITEMS::iterator it = m_items.begin(); it != m_items.end(); ++it )
198  ( *it )->setSize( 0 );
199 
200  m_items.clear();
201 
202  // Now there is only free space left
203  m_freeChunks.clear();
204  m_freeChunks.insert( std::make_pair( m_freeSpace, 0 ) );
205 }
unsigned int m_currentSize
Store the initial size, so it can be resized to this on Clear()
ITEMS m_items
Currently modified item.
unsigned int m_freeSpace
Current container size, expressed in vertices.
FREE_CHUNK_MAP m_freeChunks
Stored VERTEX_ITEMs.

References KIGFX::VERTEX_CONTAINER::m_currentSize, KIGFX::VERTEX_CONTAINER::m_failed, m_freeChunks, KIGFX::VERTEX_CONTAINER::m_freeSpace, m_items, and m_maxIndex.

◆ ClearDirty()

void KIGFX::VERTEX_CONTAINER::ClearDirty ( )
inlineinherited

Clear the dirty flag to prevent reuploading vertices to the GPU memory.

Definition at line 152 of file vertex_container.h.

153  {
154  m_dirty = false;
155  }
bool m_dirty
Default initial size of a container (expressed in vertices)

References KIGFX::VERTEX_CONTAINER::m_dirty.

Referenced by KIGFX::GPU_CACHED_MANAGER::EndDrawing().

◆ defragment()

void CACHED_CONTAINER::defragment ( VERTEX aTarget)
protected

Transfer all stored data to a new buffer, removing empty spaces between the data chunks in the container.

Parameters
aTargetis the destination for the defragmented data.

Definition at line 272 of file cached_container.cpp.

273 {
274  // Defragmentation
275  ITEMS::iterator it, it_end;
276  int newOffset = 0;
277 
278  for( VERTEX_ITEM* item : m_items )
279  {
280  int itemOffset = item->GetOffset();
281  int itemSize = item->GetSize();
282 
283  // Move an item to the new container
284  memcpy( &aTarget[newOffset], &m_vertices[itemOffset], itemSize * VERTEX_SIZE );
285 
286  // Update new offset
287  item->setOffset( newOffset );
288 
289  // Move to the next free space
290  newOffset += itemSize;
291  }
292 
293  // Move the current item and place it at the end
294  if( m_item->GetSize() > 0 )
295  {
296  memcpy( &aTarget[newOffset], &m_vertices[m_item->GetOffset()],
297  m_item->GetSize() * VERTEX_SIZE );
298  m_item->setOffset( newOffset );
299  m_chunkOffset = newOffset;
300  }
301 
302  m_maxIndex = usedSpace();
303 }
unsigned int GetSize() const
Return information about number of vertices stored.
Definition: vertex_item.h:58
ITEMS m_items
Currently modified item.
VERTEX_ITEM * m_item
Properties of currently modified chunk & item.
void setOffset(unsigned int aOffset)
Set data offset in the container.
Definition: vertex_item.h:84
unsigned int usedSpace() const
Return size of the used memory space.
static constexpr size_t VERTEX_SIZE
Definition: vertex_common.h:67
unsigned int GetOffset() const
Return data offset in the container.
Definition: vertex_item.h:68
unsigned int m_chunkOffset
Maximal vertex index number stored in the container.

References KIGFX::VERTEX_ITEM::GetOffset(), KIGFX::VERTEX_ITEM::GetSize(), m_chunkOffset, m_item, m_items, m_maxIndex, KIGFX::VERTEX_CONTAINER::m_vertices, KIGFX::VERTEX_ITEM::setOffset(), KIGFX::VERTEX_CONTAINER::usedSpace(), and KIGFX::VERTEX_SIZE.

Referenced by KIGFX::CACHED_CONTAINER_RAM::defragmentResize(), and KIGFX::CACHED_CONTAINER_GPU::defragmentResizeMemcpy().

◆ defragmentResize()

virtual bool KIGFX::CACHED_CONTAINER::defragmentResize ( unsigned int  aNewSize)
protectedpure virtual

Remove empty spaces between chunks and optionally resizes the container.

After the operation there is continuous space for storing vertices at the end of the container.

Parameters
aNewSizeis the new size of container, expressed in number of vertices.
Returns
false in case of failure (e.g. memory shortage).

Implemented in KIGFX::CACHED_CONTAINER_RAM, and KIGFX::CACHED_CONTAINER_GPU.

Referenced by Delete(), and reallocate().

◆ Delete()

void CACHED_CONTAINER::Delete ( VERTEX_ITEM aItem)
overridevirtual

Remove all data stored in the container and restores its original state.

Implements KIGFX::VERTEX_CONTAINER.

Definition at line 148 of file cached_container.cpp.

149 {
150  assert( aItem != nullptr );
151  assert( m_items.find( aItem ) != m_items.end() || aItem->GetSize() == 0 );
152 
153  int size = aItem->GetSize();
154 
155  if( size == 0 )
156  return; // Item is not stored here
157 
158  int offset = aItem->GetOffset();
159 
160  // Insert a free memory chunk entry in the place where item was stored
161  addFreeChunk( offset, size );
162 
163  // Indicate that the item is not stored in the container anymore
164  aItem->setSize( 0 );
165 
166  m_items.erase( aItem );
167 
168 #if CACHED_CONTAINER_TEST > 0
169  test();
170 #endif
171 
172  // This dynamic memory freeing optimize memory usage, but in fact can create
173  // out of memory issues because freeing and reallocation large chunks of memory
174  // can create memory fragmentation and no room to reallocate large chunks
175  // after many free/reallocate cycles during a session using the same complex board
176  // So it can be disable.
177  // Currently: it is disable to avoid "out of memory" issues
178 #if 0
179  // Dynamic memory freeing, there is no point in holding
180  // a large amount of memory when there is no use for it
181  if( m_freeSpace > ( 0.75 * m_currentSize ) && m_currentSize > m_initialSize )
182  {
184  }
185 #endif
186 }
unsigned int m_initialSize
Actual storage memory.
void addFreeChunk(unsigned int aOffset, unsigned int aSize)
Add a chunk marked as a free space.
unsigned int GetSize() const
Return information about number of vertices stored.
Definition: vertex_item.h:58
unsigned int m_currentSize
Store the initial size, so it can be resized to this on Clear()
void setSize(unsigned int aSize)
Set data size in the container.
Definition: vertex_item.h:94
ITEMS m_items
Currently modified item.
unsigned int m_freeSpace
Current container size, expressed in vertices.
unsigned int GetOffset() const
Return data offset in the container.
Definition: vertex_item.h:68
virtual bool defragmentResize(unsigned int aNewSize)=0
Remove empty spaces between chunks and optionally resizes the container.

References addFreeChunk(), defragmentResize(), KIGFX::VERTEX_ITEM::GetOffset(), KIGFX::VERTEX_ITEM::GetSize(), KIGFX::VERTEX_CONTAINER::m_currentSize, KIGFX::VERTEX_CONTAINER::m_freeSpace, KIGFX::VERTEX_CONTAINER::m_initialSize, m_items, KIGFX::VERTEX_ITEM::setSize(), and test().

◆ FinishItem()

void CACHED_CONTAINER::FinishItem ( )
overridevirtual

Clean up after adding an item.

Reimplemented from KIGFX::VERTEX_CONTAINER.

Definition at line 75 of file cached_container.cpp.

76 {
77  assert( m_item != nullptr );
78 
79  unsigned int itemSize = m_item->GetSize();
80 
81  // Finishing the previously edited item
82  if( itemSize < m_chunkSize )
83  {
84  // There is some not used but reserved memory left, so we should return it to the pool
85  int itemOffset = m_item->GetOffset();
86 
87  // Add the not used memory back to the pool
88  addFreeChunk( itemOffset + itemSize, m_chunkSize - itemSize );
89  // mergeFreeChunks(); // veery slow and buggy
90 
91  m_maxIndex = std::max( itemOffset + itemSize, m_maxIndex );
92  }
93 
94  if( itemSize > 0 )
95  m_items.insert( m_item );
96 
97  m_item = nullptr;
98  m_chunkSize = 0;
99  m_chunkOffset = 0;
100 
101 #if CACHED_CONTAINER_TEST > 1
102  test();
103 #endif
104 }
void addFreeChunk(unsigned int aOffset, unsigned int aSize)
Add a chunk marked as a free space.
unsigned int GetSize() const
Return information about number of vertices stored.
Definition: vertex_item.h:58
ITEMS m_items
Currently modified item.
VERTEX_ITEM * m_item
Properties of currently modified chunk & item.
unsigned int GetOffset() const
Return data offset in the container.
Definition: vertex_item.h:68
unsigned int m_chunkOffset
Maximal vertex index number stored in the container.

References addFreeChunk(), KIGFX::VERTEX_ITEM::GetOffset(), KIGFX::VERTEX_ITEM::GetSize(), m_chunkOffset, m_chunkSize, m_item, m_items, m_maxIndex, and test().

◆ GetAllVertices()

VERTEX* KIGFX::VERTEX_CONTAINER::GetAllVertices ( ) const
inlineinherited

Return pointer to the vertices stored in the container.

Definition at line 107 of file vertex_container.h.

108  {
109  return m_vertices;
110  }

References KIGFX::VERTEX_CONTAINER::m_vertices.

Referenced by KIGFX::GPU_NONCACHED_MANAGER::EndDrawing().

◆ GetBufferHandle()

virtual unsigned int KIGFX::CACHED_CONTAINER::GetBufferHandle ( ) const
pure virtual

Return handle to the vertex buffer.

It might be negative if the buffer is not initialized.

Implemented in KIGFX::CACHED_CONTAINER_RAM, and KIGFX::CACHED_CONTAINER_GPU.

Referenced by KIGFX::GPU_CACHED_MANAGER::EndDrawing().

◆ getChunkOffset()

unsigned int KIGFX::CACHED_CONTAINER::getChunkOffset ( const CHUNK aChunk) const
inlineprotected

Return the offset of a chunk.

Parameters
aChunkis the chunk.

Definition at line 157 of file cached_container.h.

158  {
159  return aChunk.second;
160  }

Referenced by reallocate().

◆ getChunkSize()

int KIGFX::CACHED_CONTAINER::getChunkSize ( const CHUNK aChunk) const
inlineprotected

Return the size of a chunk.

Parameters
aChunkis the chunk.

Definition at line 147 of file cached_container.h.

148  {
149  return aChunk.first;
150  }

Referenced by reallocate(), and test().

◆ GetSize()

virtual unsigned int KIGFX::VERTEX_CONTAINER::GetSize ( ) const
inlinevirtualinherited

Return amount of vertices currently stored in the container.

Reimplemented in KIGFX::NONCACHED_CONTAINER.

Definition at line 125 of file vertex_container.h.

126  {
127  return m_currentSize;
128  }
unsigned int m_currentSize
Store the initial size, so it can be resized to this on Clear()

References KIGFX::VERTEX_CONTAINER::m_currentSize.

Referenced by KIGFX::GPU_NONCACHED_MANAGER::EndDrawing().

◆ GetVertices()

virtual VERTEX* KIGFX::VERTEX_CONTAINER::GetVertices ( unsigned int  aOffset) const
inlinevirtualinherited

Return vertices stored at the specific offset.

Parameters
aOffsetis the offset.

Definition at line 117 of file vertex_container.h.

118  {
119  return &m_vertices[aOffset];
120  }

References KIGFX::VERTEX_CONTAINER::m_vertices.

◆ IsCached()

bool KIGFX::CACHED_CONTAINER::IsCached ( ) const
inlineoverridevirtual

Return true if the container caches vertex data in RAM or video memory.

Otherwise it is a single batch draw which is later discarded.

Implements KIGFX::VERTEX_CONTAINER.

Definition at line 52 of file cached_container.h.

53  {
54  return true;
55  }

◆ IsDirty()

bool KIGFX::VERTEX_CONTAINER::IsDirty ( ) const
inlineinherited

Return information about the container cache state.

Returns
True in case the vertices have to be reuploaded.

Definition at line 135 of file vertex_container.h.

136  {
137  return m_dirty;
138  }
bool m_dirty
Default initial size of a container (expressed in vertices)

References KIGFX::VERTEX_CONTAINER::m_dirty.

◆ IsMapped()

virtual bool KIGFX::CACHED_CONTAINER::IsMapped ( ) const
pure virtual

Return true if vertex buffer is currently mapped.

Prepare the container for vertices updates.

Implemented in KIGFX::CACHED_CONTAINER_RAM, and KIGFX::CACHED_CONTAINER_GPU.

Referenced by Allocate(), KIGFX::GPU_CACHED_MANAGER::EndDrawing(), and reallocate().

◆ MakeContainer()

VERTEX_CONTAINER * VERTEX_CONTAINER::MakeContainer ( bool  aCached)
staticinherited

Return a pointer to a new container of an appropriate type.

Definition at line 42 of file vertex_container.cpp.

43 {
44  if( aCached )
45  {
46  const char* vendor = (const char*) glGetString( GL_VENDOR );
47 
48  // Open source drivers do not cope well with GPU memory mapping,
49  // so the vertex data has to be kept in RAM
50  if( strstr( vendor, "X.Org" ) || strstr( vendor, "nouveau" ) )
51  return new CACHED_CONTAINER_RAM;
52  else
53  return new CACHED_CONTAINER_GPU;
54  }
55 
56  return new NONCACHED_CONTAINER;
57 }
Specialization of CACHED_CONTAINER that stores data in RAM.
Specialization of CACHED_CONTAINER that stores data in video memory via memory mapping.

Referenced by KIGFX::VERTEX_MANAGER::VERTEX_MANAGER().

◆ Map()

virtual void KIGFX::CACHED_CONTAINER::Map ( )
overridepure virtual

Finish the vertices updates stage.

Reimplemented from KIGFX::VERTEX_CONTAINER.

Implemented in KIGFX::CACHED_CONTAINER_GPU, and KIGFX::CACHED_CONTAINER_RAM.

◆ mergeFreeChunks()

void CACHED_CONTAINER::mergeFreeChunks ( )
protected

Look for consecutive free memory chunks and merges them, decreasing fragmentation of memory.

Definition at line 306 of file cached_container.cpp.

307 {
308  if( m_freeChunks.size() <= 1 ) // There are no chunks that can be merged
309  return;
310 
311 #ifdef KICAD_GAL_PROFILE
312  PROF_COUNTER totalTime;
313 #endif /* KICAD_GAL_PROFILE */
314 
315  // Reversed free chunks map - this one stores chunk size with its offset as the key
316  std::list<CHUNK> freeChunks;
317 
318  FREE_CHUNK_MAP::const_iterator it, it_end;
319 
320  for( it = m_freeChunks.begin(), it_end = m_freeChunks.end(); it != it_end; ++it )
321  {
322  freeChunks.emplace_back( it->second, it->first );
323  }
324 
325  m_freeChunks.clear();
326  freeChunks.sort();
327 
328  std::list<CHUNK>::const_iterator itf, itf_end;
329  unsigned int offset = freeChunks.front().first;
330  unsigned int size = freeChunks.front().second;
331  freeChunks.pop_front();
332 
333  for( itf = freeChunks.begin(), itf_end = freeChunks.end(); itf != itf_end; ++itf )
334  {
335  if( itf->first == offset + size )
336  {
337  // These chunks can be merged, so just increase the current chunk size and go on
338  size += itf->second;
339  }
340  else
341  {
342  // These chunks cannot be merged
343  // So store the previous one
344  m_freeChunks.insert( std::make_pair( size, offset ) );
345  // and let's check the next chunk
346  offset = itf->first;
347  size = itf->second;
348  }
349  }
350 
351  // Add the last one
352  m_freeChunks.insert( std::make_pair( size, offset ) );
353 
354 #if CACHED_CONTAINER_TEST > 0
355  test();
356 #endif
357 }
A small class to help profiling.
Definition: profile.h:45
FREE_CHUNK_MAP m_freeChunks
Stored VERTEX_ITEMs.

References m_freeChunks, and test().

◆ reallocate()

bool CACHED_CONTAINER::reallocate ( unsigned int  aSize)
protected

Resize the chunk that stores the current item to the given size.

The current item has its offset adjusted after the call, and the new chunk parameters are stored in m_chunkOffset and m_chunkSize.

Parameters
aSizeis the requested chunk size.
Returns
true in case of success, false otherwise.

Definition at line 208 of file cached_container.cpp.

209 {
210  assert( aSize > 0 );
211  assert( IsMapped() );
212 
213  unsigned int itemSize = m_item->GetSize();
214 
215  // Find a free space chunk >= aSize
216  FREE_CHUNK_MAP::iterator newChunk = m_freeChunks.lower_bound( aSize );
217 
218  // Is there enough space to store vertices?
219  if( newChunk == m_freeChunks.end() )
220  {
221  bool result;
222 
223  // Would it be enough to double the current space?
224  if( aSize < m_freeSpace + m_currentSize )
225  {
226  // Yes: exponential growing
227  result = defragmentResize( m_currentSize * 2 );
228  }
229  else
230  {
231  // No: grow to the nearest greater power of 2
232  result = defragmentResize( pow( 2, ceil( log2( m_currentSize * 2 + aSize ) ) ) );
233  }
234 
235  if( !result )
236  return false;
237 
238  newChunk = m_freeChunks.lower_bound( aSize );
239  assert( newChunk != m_freeChunks.end() );
240  }
241 
242  // Parameters of the allocated chunk
243  unsigned int newChunkSize = getChunkSize( *newChunk );
244  unsigned int newChunkOffset = getChunkOffset( *newChunk );
245 
246  assert( newChunkSize >= aSize );
247  assert( newChunkOffset < m_currentSize );
248 
249  // Check if the item was previously stored in the container
250  if( itemSize > 0 )
251  {
252  // The item was reallocated, so we have to copy all the old data to the new place
253  memcpy( &m_vertices[newChunkOffset], &m_vertices[m_chunkOffset], itemSize * VERTEX_SIZE );
254 
255  // Free the space used by the previous chunk
257  }
258 
259  // Remove the new allocated chunk from the free space pool
260  m_freeChunks.erase( newChunk );
261  m_freeSpace -= newChunkSize;
262 
263  m_chunkSize = newChunkSize;
264  m_chunkOffset = newChunkOffset;
265 
267 
268  return true;
269 }
void addFreeChunk(unsigned int aOffset, unsigned int aSize)
Add a chunk marked as a free space.
unsigned int GetSize() const
Return information about number of vertices stored.
Definition: vertex_item.h:58
unsigned int getChunkOffset(const CHUNK &aChunk) const
Return the offset of a chunk.
unsigned int m_currentSize
Store the initial size, so it can be resized to this on Clear()
VERTEX_ITEM * m_item
Properties of currently modified chunk & item.
unsigned int m_freeSpace
Current container size, expressed in vertices.
int getChunkSize(const CHUNK &aChunk) const
Return the size of a chunk.
void setOffset(unsigned int aOffset)
Set data offset in the container.
Definition: vertex_item.h:84
FREE_CHUNK_MAP m_freeChunks
Stored VERTEX_ITEMs.
static constexpr size_t VERTEX_SIZE
Definition: vertex_common.h:67
virtual bool IsMapped() const =0
Return true if vertex buffer is currently mapped.
unsigned int m_chunkOffset
Maximal vertex index number stored in the container.
virtual bool defragmentResize(unsigned int aNewSize)=0
Remove empty spaces between chunks and optionally resizes the container.

References addFreeChunk(), defragmentResize(), getChunkOffset(), getChunkSize(), KIGFX::VERTEX_ITEM::GetSize(), IsMapped(), m_chunkOffset, m_chunkSize, KIGFX::VERTEX_CONTAINER::m_currentSize, m_freeChunks, KIGFX::VERTEX_CONTAINER::m_freeSpace, m_item, KIGFX::VERTEX_CONTAINER::m_vertices, KIGFX::VERTEX_ITEM::setOffset(), and KIGFX::VERTEX_SIZE.

Referenced by Allocate().

◆ SetDirty()

void KIGFX::VERTEX_CONTAINER::SetDirty ( )
inlineinherited

Set the dirty flag, so vertices in the container are going to be reuploaded to the GPU on the next frame.

Definition at line 144 of file vertex_container.h.

145  {
146  m_dirty = true;
147  }
bool m_dirty
Default initial size of a container (expressed in vertices)

References KIGFX::VERTEX_CONTAINER::m_dirty.

◆ SetItem()

void CACHED_CONTAINER::SetItem ( VERTEX_ITEM aItem)
overridevirtual

Clean up after adding an item.

Implements KIGFX::VERTEX_CONTAINER.

Definition at line 62 of file cached_container.cpp.

63 {
64  assert( aItem != nullptr );
65 
66  unsigned int itemSize = aItem->GetSize();
67  m_item = aItem;
68  m_chunkSize = itemSize;
69 
70  // Get the previously set offset if the item was stored previously
71  m_chunkOffset = itemSize > 0 ? aItem->GetOffset() : -1;
72 }
unsigned int GetSize() const
Return information about number of vertices stored.
Definition: vertex_item.h:58
VERTEX_ITEM * m_item
Properties of currently modified chunk & item.
unsigned int GetOffset() const
Return data offset in the container.
Definition: vertex_item.h:68
unsigned int m_chunkOffset
Maximal vertex index number stored in the container.

References KIGFX::VERTEX_ITEM::GetOffset(), KIGFX::VERTEX_ITEM::GetSize(), m_chunkOffset, m_chunkSize, and m_item.

◆ showFreeChunks()

void CACHED_CONTAINER::showFreeChunks ( )
private

Debug & test functions.

Definition at line 370 of file cached_container.cpp.

371 {
372 }

Referenced by Allocate().

◆ showUsedChunks()

void CACHED_CONTAINER::showUsedChunks ( )
private

Definition at line 375 of file cached_container.cpp.

376 {
377 }

Referenced by Allocate().

◆ test()

void CACHED_CONTAINER::test ( )
private

Definition at line 380 of file cached_container.cpp.

381 {
382 #ifdef KICAD_GAL_PROFILE
383  // Free space check
384  unsigned int freeSpace = 0;
385  FREE_CHUNK_MAP::iterator itf;
386 
387  for( itf = m_freeChunks.begin(); itf != m_freeChunks.end(); ++itf )
388  freeSpace += getChunkSize( *itf );
389 
390  assert( freeSpace == m_freeSpace );
391 
392  // Used space check
393  unsigned int used_space = 0;
394  ITEMS::iterator itr;
395 
396  for( itr = m_items.begin(); itr != m_items.end(); ++itr )
397  used_space += ( *itr )->GetSize();
398 
399  // If we have a chunk assigned, then there must be an item edited
400  assert( m_chunkSize == 0 || m_item );
401 
402  // Currently reserved chunk is also counted as used
403  used_space += m_chunkSize;
404 
405  assert( ( m_freeSpace + used_space ) == m_currentSize );
406 
407  // Overlapping check TODO
408 #endif /* KICAD_GAL_PROFILE */
409 }
unsigned int m_currentSize
Store the initial size, so it can be resized to this on Clear()
ITEMS m_items
Currently modified item.
VERTEX_ITEM * m_item
Properties of currently modified chunk & item.
unsigned int m_freeSpace
Current container size, expressed in vertices.
int getChunkSize(const CHUNK &aChunk) const
Return the size of a chunk.
FREE_CHUNK_MAP m_freeChunks
Stored VERTEX_ITEMs.

References getChunkSize(), m_chunkSize, KIGFX::VERTEX_CONTAINER::m_currentSize, m_freeChunks, KIGFX::VERTEX_CONTAINER::m_freeSpace, m_item, and m_items.

Referenced by Allocate(), Delete(), FinishItem(), and mergeFreeChunks().

◆ Unmap()

virtual void KIGFX::CACHED_CONTAINER::Unmap ( )
overridepure virtual

Finish the vertices updates stage.

Reimplemented from KIGFX::VERTEX_CONTAINER.

Implemented in KIGFX::CACHED_CONTAINER_GPU, and KIGFX::CACHED_CONTAINER_RAM.

Referenced by KIGFX::GPU_CACHED_MANAGER::EndDrawing().

◆ usedSpace()

unsigned int KIGFX::VERTEX_CONTAINER::usedSpace ( ) const
inlineprotectedinherited

Return size of the used memory space.

Returns
Size of the used memory space (expressed as a number of vertices).Free space left in the container, expressed in vertices

Definition at line 165 of file vertex_container.h.

References KIGFX::VERTEX_CONTAINER::m_currentSize, and KIGFX::VERTEX_CONTAINER::m_freeSpace.

Referenced by defragment(), KIGFX::CACHED_CONTAINER_GPU::defragmentResize(), KIGFX::CACHED_CONTAINER_RAM::defragmentResize(), and KIGFX::CACHED_CONTAINER_GPU::defragmentResizeMemcpy().

Member Data Documentation

◆ DEFAULT_SIZE

constexpr unsigned int KIGFX::VERTEX_CONTAINER::DEFAULT_SIZE = 1048576
staticprotectedinherited

Definition at line 187 of file vertex_container.h.

◆ m_chunkOffset

unsigned int KIGFX::CACHED_CONTAINER::m_chunkOffset
protected

Maximal vertex index number stored in the container.

Definition at line 178 of file cached_container.h.

Referenced by Allocate(), defragment(), KIGFX::CACHED_CONTAINER_GPU::defragmentResize(), FinishItem(), reallocate(), and SetItem().

◆ m_chunkSize

unsigned int KIGFX::CACHED_CONTAINER::m_chunkSize
protected

Definition at line 177 of file cached_container.h.

Referenced by Allocate(), FinishItem(), reallocate(), SetItem(), and test().

◆ m_currentSize

◆ m_dirty

bool KIGFX::VERTEX_CONTAINER::m_dirty
protectedinherited

◆ m_failed

bool KIGFX::VERTEX_CONTAINER::m_failed
protectedinherited

Definition at line 183 of file vertex_container.h.

Referenced by Allocate(), and Clear().

◆ m_freeChunks

◆ m_freeSpace

◆ m_initialSize

unsigned int KIGFX::VERTEX_CONTAINER::m_initialSize
protectedinherited

Actual storage memory.

Definition at line 177 of file vertex_container.h.

Referenced by Delete().

◆ m_item

VERTEX_ITEM* KIGFX::CACHED_CONTAINER::m_item
protected

Properties of currently modified chunk & item.

Definition at line 174 of file cached_container.h.

Referenced by Allocate(), defragment(), KIGFX::CACHED_CONTAINER_GPU::defragmentResize(), FinishItem(), reallocate(), SetItem(), and test().

◆ m_items

ITEMS KIGFX::CACHED_CONTAINER::m_items
protected

◆ m_maxIndex

unsigned int KIGFX::CACHED_CONTAINER::m_maxIndex
protected

◆ m_vertices


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