KiCad PCB EDA Suite
KIGFX::NONCACHED_CONTAINER Class Reference

#include <noncached_container.h>

Inheritance diagram for KIGFX::NONCACHED_CONTAINER:
KIGFX::VERTEX_CONTAINER

Public Member Functions

 NONCACHED_CONTAINER (unsigned int aSize=DEFAULT_SIZE)
 Construct a non-cached container object. More...
 
virtual ~NONCACHED_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
 Set the item for the further actions. 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...
 
void Delete (VERTEX_ITEM *aItem) override
 Erase the data related to an item. More...
 
virtual void Clear () override
 Remove all data stored in the container and restores its original state. More...
 
virtual unsigned int GetSize () const override
 Return amount of vertices currently stored in the container. More...
 
virtual void Map ()
 Prepare the container for vertices updates. More...
 
virtual void Unmap ()
 Finish the vertices updates stage. More...
 
virtual void FinishItem ()
 Clean up after adding an item. More...
 
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...
 
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 Member Functions

unsigned int usedSpace () const
 Return size of the used memory space. More...
 

Protected Attributes

unsigned int m_freePtr
 < Index of the free first space where a vertex can be stored More...
 
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
 

Detailed Description

Definition at line 41 of file noncached_container.h.

Constructor & Destructor Documentation

◆ NONCACHED_CONTAINER()

NONCACHED_CONTAINER::NONCACHED_CONTAINER ( unsigned int  aSize = DEFAULT_SIZE)

Construct a non-cached container object.

Parameters
aSizeis the size of the cache.
Exceptions
bad_allocexception if memory allocation fails.

Definition at line 39 of file noncached_container.cpp.

39  :
40  VERTEX_CONTAINER( aSize ),
41  m_freePtr( 0 )
42 {
43  m_vertices = static_cast<VERTEX*>( malloc( aSize * sizeof( VERTEX ) ) );
44 
45  // Unfortunately we cannot remove the use of malloc here because realloc is used in
46  // the Allocate method below. The new operator behavior is mimicked here so that a
47  // malloc failure can be caught in the OpenGL initialization code further up the stack.
48  if( !m_vertices )
49  throw std::bad_alloc();
50 
51  memset( m_vertices, 0x00, aSize * sizeof( VERTEX ) );
52 }
unsigned int m_freePtr
< Index of the free first space where a vertex can be stored
VERTEX_CONTAINER(unsigned int aSize=DEFAULT_SIZE)

References KIGFX::VERTEX_CONTAINER::m_vertices.

◆ ~NONCACHED_CONTAINER()

NONCACHED_CONTAINER::~NONCACHED_CONTAINER ( )
virtual

Definition at line 55 of file noncached_container.cpp.

56 {
57  free( m_vertices );
58 }

References KIGFX::VERTEX_CONTAINER::m_vertices.

Member Function Documentation

◆ Allocate()

VERTEX * NONCACHED_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 or NULL in case of failure.

Implements KIGFX::VERTEX_CONTAINER.

Definition at line 68 of file noncached_container.cpp.

69 {
70  if( m_freeSpace < aSize )
71  {
72  // Double the space
73  VERTEX* newVertices =
74  static_cast<VERTEX*>( realloc( m_vertices, m_currentSize * 2 * sizeof( VERTEX ) ) );
75 
76  if( newVertices != nullptr )
77  {
78  m_vertices = newVertices;
80  m_currentSize *= 2;
81  }
82  else
83  {
84  throw std::bad_alloc();
85  }
86  }
87 
88  VERTEX* freeVertex = &m_vertices[m_freePtr];
89 
90  // Move to the next free chunk
91  m_freePtr += aSize;
92  m_freeSpace -= aSize;
93 
94  return freeVertex;
95 }
unsigned int m_freePtr
< Index of the free first space where a vertex can be stored
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.

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

◆ Clear()

void NONCACHED_CONTAINER::Clear ( )
overridevirtual

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

Implements KIGFX::VERTEX_CONTAINER.

Definition at line 98 of file noncached_container.cpp.

99 {
100  m_freePtr = 0;
102 }
unsigned int m_freePtr
< Index of the free first space where a vertex can be stored
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.

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

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

◆ Delete()

void KIGFX::NONCACHED_CONTAINER::Delete ( VERTEX_ITEM aItem)
inlineoverridevirtual

Erase the data related to an item.

Parameters
aItemis the item to be erased.

Implements KIGFX::VERTEX_CONTAINER.

Definition at line 65 of file noncached_container.h.

65 {}

◆ FinishItem()

virtual void KIGFX::VERTEX_CONTAINER::FinishItem ( )
inlinevirtualinherited

Clean up after adding an item.

Reimplemented in KIGFX::CACHED_CONTAINER.

Definition at line 78 of file vertex_container.h.

78 {};

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

◆ GetSize()

virtual unsigned int KIGFX::NONCACHED_CONTAINER::GetSize ( ) const
inlineoverridevirtual

Return amount of vertices currently stored in the container.

Reimplemented from KIGFX::VERTEX_CONTAINER.

Definition at line 71 of file noncached_container.h.

72  {
73  // As the m_freePtr points to the first free space, we can safely assume
74  // that this is the number of vertices stored inside
75  return m_freePtr;
76  }
unsigned int m_freePtr
< Index of the free first space where a vertex can be stored

References m_freePtr.

◆ 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::NONCACHED_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 53 of file noncached_container.h.

54  {
55  return false;
56  }

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

◆ 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::VERTEX_CONTAINER::Map ( )
inlinevirtualinherited

Prepare the container for vertices updates.

Reimplemented in KIGFX::CACHED_CONTAINER, KIGFX::CACHED_CONTAINER_GPU, and KIGFX::CACHED_CONTAINER_RAM.

Definition at line 61 of file vertex_container.h.

61 {}

◆ 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 NONCACHED_CONTAINER::SetItem ( VERTEX_ITEM aItem)
overridevirtual

Set the item for the further actions.

Parameters
aItemis the item or NULL in case of finishing the item.

Implements KIGFX::VERTEX_CONTAINER.

Definition at line 61 of file noncached_container.cpp.

62 {
63  // Nothing has to be done, as the noncached container
64  // does not care about VERTEX_ITEMs ownership
65 }

◆ Unmap()

virtual void KIGFX::VERTEX_CONTAINER::Unmap ( )
inlinevirtualinherited

Finish the vertices updates stage.

Reimplemented in KIGFX::CACHED_CONTAINER, KIGFX::CACHED_CONTAINER_GPU, and KIGFX::CACHED_CONTAINER_RAM.

Definition at line 66 of file vertex_container.h.

66 {}

◆ 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 KIGFX::CACHED_CONTAINER::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_currentSize

◆ m_dirty

bool KIGFX::VERTEX_CONTAINER::m_dirty
protectedinherited

◆ m_failed

bool KIGFX::VERTEX_CONTAINER::m_failed
protectedinherited

◆ m_freePtr

unsigned int KIGFX::NONCACHED_CONTAINER::m_freePtr
protected

< Index of the free first space where a vertex can be stored

Definition at line 80 of file noncached_container.h.

Referenced by Allocate(), Clear(), and GetSize().

◆ 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 KIGFX::CACHED_CONTAINER::Delete().

◆ m_vertices


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