KiCad PCB EDA Suite
Loading...
Searching...
No Matches
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.
 
virtual void SetItem (VERTEX_ITEM *aItem) override
 Clean up after adding an item.
 
virtual void FinishItem () override
 Clean up after adding an item.
 
virtual VERTEXAllocate (unsigned int aSize) override
 Return allocated space for the requested number of vertices associated with the current item (set with SetItem()).
 
virtual void Delete (VERTEX_ITEM *aItem) override
 Remove all data stored in the container and restores its original state.
 
virtual void Clear () override
 Remove all data stored in the container and restores its original state.
 
virtual unsigned int GetBufferHandle () const =0
 Return handle to the vertex buffer.
 
virtual bool IsMapped () const =0
 Return true if vertex buffer is currently mapped.
 
virtual void Map () override=0
 Finish the vertices updates stage.
 
virtual void Unmap () override=0
 Finish the vertices updates stage.
 
virtual unsigned int AllItemsSize () const
 
VERTEXGetAllVertices () const
 Return pointer to the vertices stored in the container.
 
virtual VERTEXGetVertices (unsigned int aOffset) const
 Return vertices stored at the specific offset.
 
virtual unsigned int GetSize () const
 Return amount of vertices currently stored in the container.
 
bool IsDirty () const
 Return information about the container cache state.
 
void SetDirty ()
 Set the dirty flag, so vertices in the container are going to be reuploaded to the GPU on the next frame.
 
void ClearDirty ()
 Clear the dirty flag to prevent reuploading vertices to the GPU memory.
 

Static Public Member Functions

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

Protected Types

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

Protected Member Functions

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

Protected Attributes

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

Static Protected Attributes

static constexpr unsigned int DEFAULT_SIZE = 1048576
 

Private Member Functions

void showFreeChunks ()
 Debug & test functions.
 
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 54 of file cached_container.cpp.

References m_freeChunks.

◆ ~CACHED_CONTAINER()

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

Definition at line 50 of file cached_container.h.

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 391 of file cached_container.cpp.

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.

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 111 of file cached_container.cpp.

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 193 of file cached_container.cpp.

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.

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 276 of file cached_container.cpp.

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_GPU, and KIGFX::CACHED_CONTAINER_RAM.

Referenced by Delete(), and reallocate().

◆ Delete()

void CACHED_CONTAINER::Delete ( VERTEX_ITEM aItem)
overridevirtual

◆ FinishItem()

void CACHED_CONTAINER::FinishItem ( )
overridevirtual

Clean up after adding an item.

Reimplemented from KIGFX::VERTEX_CONTAINER.

Definition at line 79 of file cached_container.cpp.

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.

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_GPU, and KIGFX::CACHED_CONTAINER_RAM.

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.

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.

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.

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.

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.

◆ 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.

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_GPU, and KIGFX::CACHED_CONTAINER_RAM.

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.

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 337 of file cached_container.cpp.

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 212 of file cached_container.cpp.

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.

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 66 of file cached_container.cpp.

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 401 of file cached_container.cpp.

Referenced by Allocate().

◆ showUsedChunks()

void CACHED_CONTAINER::showUsedChunks ( )
private

Definition at line 406 of file cached_container.cpp.

Referenced by Allocate().

◆ test()

◆ 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
staticconstexprprotectedinherited

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: