KiCad PCB EDA Suite
cached_container.h
Go to the documentation of this file.
1 /*
2  * This program source code file is part of KiCad, a free EDA CAD application.
3  *
4  * Copyright (C) 2013 CERN
5  * @author Maciej Suminski <maciej.suminski@cern.ch>
6  *
7  * This program is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU General Public License
9  * as published by the Free Software Foundation; either version 2
10  * of the License, or (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program; if not, you may find one here:
19  * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
20  * or you may search the http://www.gnu.org website for the version 2 license,
21  * or you may write to the Free Software Foundation, Inc.,
22  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
23  */
24 
32 #ifndef CACHED_CONTAINER_H_
33 #define CACHED_CONTAINER_H_
34 
36 #include <map>
37 #include <set>
38 
39 namespace KIGFX
40 {
41 class VERTEX_ITEM;
42 class SHADER;
43 
45 {
46 public:
47  CACHED_CONTAINER( unsigned int aSize = defaultInitSize );
49 
51  virtual void SetItem( VERTEX_ITEM* aItem ) override;
52 
54  virtual void FinishItem() override;
55 
57  virtual VERTEX* Allocate( unsigned int aSize ) override;
58 
60  virtual void Delete( VERTEX_ITEM* aItem ) override;
61 
63  virtual void Clear() override;
64 
69  inline unsigned int GetBufferHandle() const
70  {
71  return m_glBufferHandle;
72  }
73 
78  inline bool IsMapped() const
79  {
80  return m_isMapped;
81  }
82 
84  void Map() override;
85 
87  void Unmap() override;
88 
89 protected:
91  typedef std::pair<unsigned int, unsigned int> CHUNK;
92  typedef std::multimap<unsigned int, unsigned int> FREE_CHUNK_MAP;
93 
95  typedef std::set<VERTEX_ITEM*> ITEMS;
96 
98  FREE_CHUNK_MAP m_freeChunks;
99 
101  ITEMS m_items;
102 
105 
107  unsigned int m_chunkSize;
108  unsigned int m_chunkOffset;
109 
112 
115 
117  unsigned int m_glBufferHandle;
118 
121 
127  void init();
128 
138  bool reallocate( unsigned int aSize );
139 
148  bool defragmentResize( unsigned int aNewSize );
149  bool defragmentResizeMemcpy( unsigned int aNewSize );
150 
156  void mergeFreeChunks();
157 
164  unsigned int getPowerOf2( unsigned int aNumber ) const;
165 
166 private:
173  inline int getChunkSize( const CHUNK& aChunk ) const
174  {
175  return aChunk.first;
176  }
177 
184  inline unsigned int getChunkOffset( const CHUNK& aChunk ) const
185  {
186  return aChunk.second;
187  }
188 
193  void addFreeChunk( unsigned int aOffset, unsigned int aSize );
194 
196  void showFreeChunks();
197  void showUsedChunks();
198  void test();
199 };
200 } // namespace KIGFX
201 
202 #endif /* CACHED_CONTAINER_H_ */
void showFreeChunks()
Debug & test functions.
int getChunkSize(const CHUNK &aChunk) const
Function getChunkSize() returns size of the given chunk.
void addFreeChunk(unsigned int aOffset, unsigned int aSize)
Function addFreeChunk Adds a chunk marked as free.
Class CAIRO_GAL is the cairo implementation of the graphics abstraction layer.
Definition: class_module.h:58
bool m_isInitialized
Flag saying if the vertex buffer is initialized
unsigned int getPowerOf2(unsigned int aNumber) const
Function getPowerOf2() returns the nearest power of 2, bigger than aNumber.
bool defragmentResizeMemcpy(unsigned int aNewSize)
unsigned int GetBufferHandle() const
Function GetBufferHandle() returns handle to the vertex buffer.
static const unsigned int defaultInitSize
void init()
Function init() performs the GL vertex buffer initialization.
unsigned int m_chunkSize
Properties of currently modified chunk & item
std::set< VERTEX_ITEM * > ITEMS
List of all the stored items.
virtual void SetItem(VERTEX_ITEM *aItem) override
>
std::multimap< unsigned int, unsigned int > FREE_CHUNK_MAP
bool m_useCopyBuffer
Flag saying whether it is safe to use glCopyBufferSubData
ITEMS m_items
Stored VERTEX_ITEMs
VERTEX_ITEM * m_item
Currently modified item
virtual void FinishItem() override
>
CACHED_CONTAINER(unsigned int aSize=defaultInitSize)
std::pair< unsigned int, unsigned int > CHUNK
Maps size of free memory chunks to their offsets
unsigned int getChunkOffset(const CHUNK &aChunk) const
Function getChunkOffset() returns offset of the chunk.
void mergeFreeChunks()
Function mergeFreeChunks() looks for consecutive free memory chunks and merges them, decreasing fragmentation of memory.
virtual void Clear() override
>
bool defragmentResize(unsigned int aNewSize)
Function defragmentResize() removes empty spaces between chunks and optionally resizes the container...
Class to store vertices and handle transfers between system memory and GPU memory.
virtual VERTEX * Allocate(unsigned int aSize) override
>
FREE_CHUNK_MAP m_freeChunks
Stores size & offset of free chunks.
unsigned int m_glBufferHandle
Vertex buffer handle
bool reallocate(unsigned int aSize)
Function reallocate() resizes the chunk that stores the current item to the given size...
bool m_isMapped
Flag saying if vertex buffer is currently mapped
bool IsMapped() const
Function IsMapped() returns true if vertex buffer is currently mapped.
virtual void Delete(VERTEX_ITEM *aItem) override
>