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
26#include <gal/gal.h>
28#include <map>
29#include <set>
30
31namespace KIGFX
32{
33class VERTEX_ITEM;
34class SHADER;
35
42
44{
45public:
46 CACHED_CONTAINER( unsigned int aSize = DEFAULT_SIZE );
47 virtual ~CACHED_CONTAINER() {}
48
49 bool IsCached() const override
50 {
51 return true;
52 }
53
54 virtual void SetItem( VERTEX_ITEM* aItem ) override;
55
57 virtual void FinishItem() override;
58
70 virtual VERTEX* Allocate( unsigned int aSize ) override;
71
73 virtual void Delete( VERTEX_ITEM* aItem ) override;
74
76 virtual void Clear() override;
77
81 virtual unsigned int GetBufferHandle() const = 0;
82
86 virtual bool IsMapped() const = 0;
87
89 virtual void Map() override = 0;
90
92 virtual void Unmap() override = 0;
93
94 virtual unsigned int AllItemsSize() const { return 0; }
95
96protected:
98 typedef std::pair<unsigned int, unsigned int> CHUNK;
99 typedef std::multimap<unsigned int, unsigned int> FREE_CHUNK_MAP;
100
109 bool reallocate( unsigned int aSize );
110
120 virtual bool defragmentResize( unsigned int aNewSize ) = 0;
121
128 void defragment( VERTEX* aTarget );
129
134 void mergeFreeChunks();
135
141 int getChunkSize( const CHUNK& aChunk ) const
142 {
143 return aChunk.first;
144 }
145
151 unsigned int getChunkOffset( const CHUNK& aChunk ) const
152 {
153 return aChunk.second;
154 }
155
159 void addFreeChunk( unsigned int aOffset, unsigned int aSize );
160
161protected:
163 std::set<VERTEX_ITEM*> m_items;
165
167 unsigned int m_chunkSize;
168 unsigned int m_chunkOffset;
169
171 unsigned int m_maxIndex;
172
173private:
175 void showFreeChunks();
176 void showUsedChunks();
177 void test();
178};
179} // namespace KIGFX
180
181#endif /* CACHED_CONTAINER_H_ */
int getChunkSize(const CHUNK &aChunk) const
Return the size of a chunk.
std::set< VERTEX_ITEM * > m_items
Stored VERTEX_ITEMs.
void mergeFreeChunks()
Look for consecutive free memory chunks and merges them, decreasing fragmentation of memory.
void showFreeChunks()
Debug & test functions.
VERTEX_ITEM * m_item
Currently modified item.
FREE_CHUNK_MAP m_freeChunks
Store size & offset of free chunks.
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...
unsigned int m_maxIndex
Maximal vertex index number stored in the container.
virtual unsigned int GetBufferHandle() const =0
Return handle to the vertex buffer.
std::pair< unsigned int, unsigned int > CHUNK
Maps size of free memory chunks to their offsets.
unsigned int getChunkOffset(const CHUNK &aChunk) const
Return the offset of a chunk.
virtual unsigned int AllItemsSize() const
unsigned int m_chunkSize
Properties of currently modified chunk & item.
virtual bool defragmentResize(unsigned int aNewSize)=0
Remove empty spaces between chunks and optionally resizes the container.
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.
virtual void Unmap() override=0
Finish the vertices updates stage.
virtual void Map() override=0
Prepare the container for vertices updates.
bool reallocate(unsigned int aSize)
Resize the chunk that stores the current item to the given size.
Provide the access to the OpenGL shaders.
Definition shader.h:73
static constexpr unsigned int DEFAULT_SIZE
Default initial size of a container (expressed in vertices)
VERTEX_CONTAINER(unsigned int aSize=DEFAULT_SIZE)
#define GAL_API
Definition gal.h:27
The Cairo implementation of the graphics abstraction layer.
Definition eda_group.h:30
Class to store vertices and handle transfers between system memory and GPU memory.