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 2013-2017 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 
25 #ifndef CACHED_CONTAINER_H_
26 #define CACHED_CONTAINER_H_
27 
29 #include <map>
30 #include <set>
31 
32 namespace KIGFX
33 {
34 class VERTEX_ITEM;
35 class SHADER;
36 
44 {
45 public:
46  CACHED_CONTAINER( unsigned int aSize = DEFAULT_SIZE );
47  virtual ~CACHED_CONTAINER() {}
48 
49  bool IsCached() const override
50  {
51  return true;
52  }
53 
55  virtual void SetItem( VERTEX_ITEM* aItem ) override;
56 
58  virtual void FinishItem() override;
59 
61  virtual VERTEX* Allocate( unsigned int aSize ) override;
62 
64  virtual void Delete( VERTEX_ITEM* aItem ) override;
65 
67  virtual void Clear() override;
68 
72  virtual unsigned int GetBufferHandle() const = 0;
73 
77  virtual bool IsMapped() const = 0;
78 
80  virtual void Map() override = 0;
81 
83  virtual void Unmap() override = 0;
84 
85 protected:
87  typedef std::pair<unsigned int, unsigned int> CHUNK;
88  typedef std::multimap<unsigned int, unsigned int> FREE_CHUNK_MAP;
89 
91  typedef std::set<VERTEX_ITEM*> ITEMS;
92 
94  FREE_CHUNK_MAP m_freeChunks;
95 
97  ITEMS m_items;
98 
101 
103  unsigned int m_chunkSize;
104  unsigned int m_chunkOffset;
105 
107  unsigned int m_maxIndex;
108 
117  bool reallocate( unsigned int aSize );
118 
126  virtual bool defragmentResize( unsigned int aNewSize ) = 0;
127 
133  void defragment( VERTEX* aTarget );
134 
139  void mergeFreeChunks();
140 
146  inline int getChunkSize( const CHUNK& aChunk ) const
147  {
148  return aChunk.first;
149  }
150 
156  inline unsigned int getChunkOffset( const CHUNK& aChunk ) const
157  {
158  return aChunk.second;
159  }
160 
164  void addFreeChunk( unsigned int aOffset, unsigned int aSize );
165 
166 private:
168  void showFreeChunks();
169  void showUsedChunks();
170  void test();
171 };
172 } // namespace KIGFX
173 
174 #endif /* CACHED_CONTAINER_H_ */
void showFreeChunks()
Debug & test functions.
int getChunkSize(const CHUNK &aChunk) const
Returns the size of a chunk.
void addFreeChunk(unsigned int aOffset, unsigned int aSize)
Adds a chunk marked as a free space.
Class CAIRO_GAL is the cairo implementation of the graphics abstraction layer.
Definition: class_module.h:58
Data structure for vertices {X,Y,Z,R,G,B,A,shader¶m}
Definition: vertex_common.h:50
void defragment(VERTEX *aTarget)
Transfers all stored data to a new buffer, removing empty spaces between the data chunks in the conta...
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
virtual unsigned int GetBufferHandle() const =0
Returns handle to the vertex buffer.
ITEMS m_items
Stored VERTEX_ITEMs
VERTEX_ITEM * m_item
Currently modified item
virtual void FinishItem() override
>
Class to store VERTEX instances with caching.
std::pair< unsigned int, unsigned int > CHUNK
Maps size of free memory chunks to their offsets
unsigned int getChunkOffset(const CHUNK &aChunk) const
Returns the offset of a chunk.
bool IsCached() const override
Returns true if the container caches vertex data in RAM or video memory.
static constexpr unsigned int DEFAULT_SIZE
void mergeFreeChunks()
Looks for consecutive free memory chunks and merges them, decreasing fragmentation of memory...
virtual void Clear() override
>
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.
CACHED_CONTAINER(unsigned int aSize=DEFAULT_SIZE)
bool reallocate(unsigned int aSize)
Resizes the chunk that stores the current item to the given size.
virtual void Map() override=0
>
unsigned int m_maxIndex
Maximal vertex index number stored in the container
virtual bool IsMapped() const =0
Returns true if vertex buffer is currently mapped.
virtual void Delete(VERTEX_ITEM *aItem) override
>
virtual void Unmap() override=0
>
virtual bool defragmentResize(unsigned int aNewSize)=0
Removes empty spaces between chunks and optionally resizes the container.