KiCad PCB EDA Suite
Loading...
Searching...
No Matches
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 The 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, see <https://www.gnu.org/licenses/>.
21 */
22
23#ifndef CACHED_CONTAINER_H_
24#define CACHED_CONTAINER_H_
25
27#include <map>
28#include <set>
29
30namespace KIGFX
31{
32class VERTEX_ITEM;
33class SHADER;
34
41
43{
44public:
45 CACHED_CONTAINER( unsigned int aSize = DEFAULT_SIZE );
46 virtual ~CACHED_CONTAINER() {}
47
48 bool IsCached() const override
49 {
50 return true;
51 }
52
53 virtual void SetItem( VERTEX_ITEM* aItem ) override;
54
56 virtual void FinishItem() override;
57
69 virtual VERTEX* Allocate( unsigned int aSize ) override;
70
72 virtual void Delete( VERTEX_ITEM* aItem ) override;
73
75 virtual void Clear() override;
76
80 virtual unsigned int GetBufferHandle() const = 0;
81
85 virtual bool IsMapped() const = 0;
86
88 virtual void Map() override = 0;
89
91 virtual void Unmap() override = 0;
92
93 virtual unsigned int AllItemsSize() const { return 0; }
94
95protected:
97 typedef std::pair<unsigned int, unsigned int> CHUNK;
98 typedef std::multimap<unsigned int, unsigned int> FREE_CHUNK_MAP;
99
101 typedef std::set<VERTEX_ITEM*> ITEMS;
102
111 bool reallocate( unsigned int aSize );
112
122 virtual bool defragmentResize( unsigned int aNewSize ) = 0;
123
130 void defragment( VERTEX* aTarget );
131
136 void mergeFreeChunks();
137
143 inline int getChunkSize( const CHUNK& aChunk ) const
144 {
145 return aChunk.first;
146 }
147
153 inline unsigned int getChunkOffset( const CHUNK& aChunk ) const
154 {
155 return aChunk.second;
156 }
157
161 void addFreeChunk( unsigned int aOffset, unsigned int aSize );
162
165
168
171
173 unsigned int m_chunkSize;
174 unsigned int m_chunkOffset;
175
177 unsigned int m_maxIndex;
178
179private:
181 void showFreeChunks();
182 void showUsedChunks();
183 void test();
184};
185} // namespace KIGFX
186
187#endif /* CACHED_CONTAINER_H_ */
unsigned int m_chunkOffset
Maximal vertex index number stored in the container.
void addFreeChunk(unsigned int aOffset, unsigned int aSize)
Add a chunk marked as a free space.
int getChunkSize(const CHUNK &aChunk) const
Return the size of a chunk.
void mergeFreeChunks()
Look for consecutive free memory chunks and merges them, decreasing fragmentation of memory.
void showFreeChunks()
Debug & test functions.
VERTEX_ITEM * m_item
Properties of currently modified chunk & item.
FREE_CHUNK_MAP m_freeChunks
Stored VERTEX_ITEMs.
std::multimap< unsigned int, unsigned int > FREE_CHUNK_MAP
void defragment(VERTEX *aTarget)
Transfer all stored data to a new buffer, removing empty spaces between the data chunks in the contai...
virtual unsigned int GetBufferHandle() const =0
Return handle to the vertex buffer.
virtual void FinishItem() override
Clean up after adding an item.
std::pair< unsigned int, unsigned int > CHUNK
< Maps size of free memory chunks to their offsets
virtual void Delete(VERTEX_ITEM *aItem) override
Remove all data stored in the container and restores its original state.
unsigned int getChunkOffset(const CHUNK &aChunk) const
Return the offset of a chunk.
virtual VERTEX * Allocate(unsigned int aSize) override
Return allocated space for the requested number of vertices associated with the current item (set wit...
virtual unsigned int AllItemsSize() const
virtual void Clear() override
Remove all data stored in the container and restores its original state.
virtual bool defragmentResize(unsigned int aNewSize)=0
Remove empty spaces between chunks and optionally resizes the container.
virtual void SetItem(VERTEX_ITEM *aItem) override
Clean up after adding an item.
bool IsCached() const override
Return true if the container caches vertex data in RAM or video memory.
CACHED_CONTAINER(unsigned int aSize=DEFAULT_SIZE)
virtual bool IsMapped() const =0
Return true if vertex buffer is currently mapped.
ITEMS m_items
Currently modified item.
virtual void Unmap() override=0
Finish the vertices updates stage.
virtual void Map() override=0
Finish the vertices updates stage.
bool reallocate(unsigned int aSize)
Resize the chunk that stores the current item to the given size.
std::set< VERTEX_ITEM * > ITEMS
List of all the stored items.
Provide the access to the OpenGL shaders.
Definition shader.h:73
static constexpr unsigned int DEFAULT_SIZE
VERTEX_CONTAINER(unsigned int aSize=DEFAULT_SIZE)
The Cairo implementation of the graphics abstraction layer.
Definition eda_group.h:29
Class to store vertices and handle transfers between system memory and GPU memory.