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
34namespace KIGFX
35{
36class VERTEX_ITEM;
37class SHADER;
38
47{
48public:
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
99protected:
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
183private:
185 void showFreeChunks();
186 void showUsedChunks();
187 void test();
188};
189} // namespace KIGFX
190
191#endif /* CACHED_CONTAINER_H_ */
Class to store VERTEX instances with caching.
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.
static constexpr unsigned int DEFAULT_SIZE
The Cairo implementation of the graphics abstraction layer.
Definition: color4d.cpp:243
Class to store vertices and handle transfers between system memory and GPU memory.