KiCad PCB EDA Suite
Loading...
Searching...
No Matches
cached_container_ram.cpp
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
26#include <gal/opengl/shader.h>
27#include <gal/opengl/utils.h>
28
29#include <confirm.h>
30#include <list>
31#include <cassert>
32
33#include <wx/log.h>
34#ifdef KICAD_GAL_PROFILE
35#include <core/profile.h>
36#endif /* KICAD_GAL_PROFILE */
37
38using namespace KIGFX;
39
40
48static const wxChar* const traceGalCachedContainer = wxT( "KICAD_GAL_CACHED_CONTAINER" );
49
50
52 CACHED_CONTAINER( aSize ),
54{
55 glGenBuffers( 1, &m_verticesBuffer );
56 checkGlError( "generating vertices buffer", __FILE__, __LINE__ );
57
58 m_vertices = static_cast<VERTEX*>( malloc( aSize * VERTEX_SIZE ) );
59
60 if( !m_vertices )
61 throw std::bad_alloc();
62}
63
64
66{
67 if( glDeleteBuffers )
68 glDeleteBuffers( 1, &m_verticesBuffer );
69
70 free( m_vertices );
71}
72
73
75{
76 if( !m_dirty )
77 return;
78
79 // Upload vertices coordinates and shader types to GPU memory
80 glBindBuffer( GL_ARRAY_BUFFER, m_verticesBuffer );
81 checkGlError( "binding vertices buffer", __FILE__, __LINE__ );
82 glBufferData( GL_ARRAY_BUFFER, m_maxIndex * VERTEX_SIZE, m_vertices, GL_STREAM_DRAW );
83 checkGlError( "transferring vertices", __FILE__, __LINE__ );
84 glBindBuffer( GL_ARRAY_BUFFER, 0 );
85 checkGlError( "unbinding vertices buffer", __FILE__, __LINE__ );
86}
87
88
89bool CACHED_CONTAINER_RAM::defragmentResize( unsigned int aNewSize )
90{
91 wxLogTrace( traceGalCachedContainer,
92 wxT( "Resizing & defragmenting container (memcpy) from %d to %d" ), m_currentSize,
93 aNewSize );
94
95 // No shrinking if we cannot fit all the data
96 if( usedSpace() > aNewSize )
97 return false;
98
99#ifdef KICAD_GAL_PROFILE
100 PROF_TIMER totalTime;
101#endif /* KICAD_GAL_PROFILE */
102
103 VERTEX* newBufferMem = static_cast<VERTEX*>( malloc( aNewSize * VERTEX_SIZE ) );
104
105 if( !newBufferMem )
106 throw std::bad_alloc();
107
108 defragment( newBufferMem );
109
110 // Switch to the new vertex buffer
111 free( m_vertices );
112 m_vertices = newBufferMem;
113
114#ifdef KICAD_GAL_PROFILE
115 totalTime.Stop();
116
117 wxLogTrace( traceGalCachedContainer, "Defragmented container storing %d vertices / %.1f ms",
118 m_currentSize - m_freeSpace, totalTime.msecs() );
119#endif /* KICAD_GAL_PROFILE */
120
121 m_freeSpace += ( aNewSize - m_currentSize );
122 m_currentSize = aNewSize;
123
124 // Now there is only one big chunk of free memory
125 m_freeChunks.clear();
126 m_freeChunks.insert( std::make_pair( m_freeSpace, m_currentSize - m_freeSpace ) );
127 m_dirty = true;
128
129 return true;
130}
~CACHED_CONTAINER_RAM()
Finish the vertices updates stage.
void Unmap() override
Finish the vertices updates stage.
bool defragmentResize(unsigned int aNewSize) override
Defragment the currently stored data and resizes the buffer.
CACHED_CONTAINER_RAM(unsigned int aSize=DEFAULT_SIZE)
FREE_CHUNK_MAP m_freeChunks
Stored VERTEX_ITEMs.
void defragment(VERTEX *aTarget)
Transfer all stored data to a new buffer, removing empty spaces between the data chunks in the contai...
CACHED_CONTAINER(unsigned int aSize=DEFAULT_SIZE)
unsigned int m_currentSize
Store the initial size, so it can be resized to this on Clear()
unsigned int m_freeSpace
Current container size, expressed in vertices.
unsigned int usedSpace() const
Return size of the used memory space.
bool m_dirty
Default initial size of a container (expressed in vertices)
A small class to help profiling.
Definition profile.h:46
void Stop()
Save the time when this function was called, and set the counter stane to stop.
Definition profile.h:86
double msecs(bool aSinceLast=false)
Definition profile.h:147
This file is part of the common library.
static const wxChar *const traceGalCachedContainer
Flag to enable debug output of the GAL OpenGL cached container.
The Cairo implementation of the graphics abstraction layer.
Definition eda_group.h:29
static constexpr size_t VERTEX_SIZE
int checkGlError(const std::string &aInfo, const char *aFile, int aLine, bool aThrow)
Check if a recent OpenGL operation has failed.
Definition utils.cpp:44
Class to handle an item held in a container.