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  * Copyright (C) 2020-2021 KiCad Developers, see AUTHORS.txt for contributors.
6  *
7  * @author Maciej Suminski <[email protected]>
8  *
9  * This program is free software; you can redistribute it and/or
10  * modify it under the terms of the GNU General Public License
11  * as published by the Free Software Foundation; either version 2
12  * of the License, or (at your option) any later version.
13  *
14  * This program is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17  * GNU General Public License for more details.
18  *
19  * You should have received a copy of the GNU General Public License
20  * along with this program; if not, you may find one here:
21  * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
22  * or you may search the http://www.gnu.org website for the version 2 license,
23  * or you may write to the Free Software Foundation, Inc.,
24  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
25  */
26 
27 #ifndef CACHED_CONTAINER_H_
28 #define CACHED_CONTAINER_H_
29 
31 #include <map>
32 #include <set>
33 
34 namespace KIGFX
35 {
36 class VERTEX_ITEM;
37 class SHADER;
38 
47 {
48 public:
49  CACHED_CONTAINER( unsigned int aSize = DEFAULT_SIZE );
50  virtual ~CACHED_CONTAINER() {}
51 
52  bool IsCached() const override
53  {
54  return true;
55  }
56 
57  virtual void SetItem( VERTEX_ITEM* aItem ) override;
58 
60  virtual void FinishItem() override;
61 
73  virtual VERTEX* Allocate( unsigned int aSize ) override;
74 
76  virtual void Delete( VERTEX_ITEM* aItem ) override;
77 
79  virtual void Clear() override;
80 
84  virtual unsigned int GetBufferHandle() const = 0;
85 
89  virtual bool IsMapped() const = 0;
90 
92  virtual void Map() override = 0;
93 
95  virtual void Unmap() override = 0;
96 
97  virtual unsigned int AllItemsSize() const { return 0; }
98 
99 protected:
101  typedef std::pair<unsigned int, unsigned int> CHUNK;
102  typedef std::multimap<unsigned int, unsigned int> FREE_CHUNK_MAP;
103 
105  typedef std::set<VERTEX_ITEM*> ITEMS;
106 
115  bool reallocate( unsigned int aSize );
116 
126  virtual bool defragmentResize( unsigned int aNewSize ) = 0;
127 
134  void defragment( VERTEX* aTarget );
135 
140  void mergeFreeChunks();
141 
147  inline int getChunkSize( const CHUNK& aChunk ) const
148  {
149  return aChunk.first;
150  }
151 
157  inline unsigned int getChunkOffset( const CHUNK& aChunk ) const
158  {
159  return aChunk.second;
160  }
161 
165  void addFreeChunk( unsigned int aOffset, unsigned int aSize );
166 
169 
172 
175 
177  unsigned int m_chunkSize;
178  unsigned int m_chunkOffset;
179 
181  unsigned int m_maxIndex;
182 
183 private:
185  void showFreeChunks();
186  void showUsedChunks();
187  void test();
188 };
189 } // namespace KIGFX
190 
191 #endif /* CACHED_CONTAINER_H_ */
void showFreeChunks()
Debug & test functions.
void addFreeChunk(unsigned int aOffset, unsigned int aSize)
Add a chunk marked as a free space.
The Cairo implementation of the graphics abstraction layer.
Definition: color4d.cpp:236
unsigned int getChunkOffset(const CHUNK &aChunk) const
Return the offset of a chunk.
void defragment(VERTEX *aTarget)
Transfer all stored data to a new buffer, removing empty spaces between the data chunks in the contai...
std::set< VERTEX_ITEM * > ITEMS
List of all the stored items.
virtual void SetItem(VERTEX_ITEM *aItem) override
Clean up after adding an item.
virtual unsigned int AllItemsSize() const
std::multimap< unsigned int, unsigned int > FREE_CHUNK_MAP
virtual unsigned int GetBufferHandle() const =0
Return handle to the vertex buffer.
ITEMS m_items
Currently modified item.
VERTEX_ITEM * m_item
Properties of currently modified chunk & item.
virtual void FinishItem() override
Clean up after adding an item.
Class to store VERTEX instances with caching.
std::pair< unsigned int, unsigned int > CHUNK
< Maps size of free memory chunks to their offsets
bool IsCached() const override
Return true if the container caches vertex data in RAM or video memory.
int getChunkSize(const CHUNK &aChunk) const
Return the size of a chunk.
static constexpr unsigned int DEFAULT_SIZE
void mergeFreeChunks()
Look for consecutive free memory chunks and merges them, decreasing fragmentation of memory.
virtual void Clear() override
Remove all data stored in the container and restores its original state.
Class to store vertices and handle transfers between system memory and GPU memory.
virtual VERTEX * Allocate(unsigned int aSize) override
Return allocated space for the requested number of vertices associated with the current item (set wit...
FREE_CHUNK_MAP m_freeChunks
Stored VERTEX_ITEMs.
CACHED_CONTAINER(unsigned int aSize=DEFAULT_SIZE)
bool reallocate(unsigned int aSize)
Resize the chunk that stores the current item to the given size.
virtual void Map() override=0
Finish the vertices updates stage.
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 void Delete(VERTEX_ITEM *aItem) override
Remove all data stored in the container and restores its original state.
virtual void Unmap() override=0
Finish the vertices updates stage.
virtual bool defragmentResize(unsigned int aNewSize)=0
Remove empty spaces between chunks and optionally resizes the container.