KiCad PCB EDA Suite
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  * @author Maciej Suminski <maciej.suminski@cern.ch>
6  *
7  * This program is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU General Public License
9  * as published by the Free Software Foundation; either version 2
10  * of the License, or (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program; if not, you may find one here:
19  * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
20  * or you may search the http://www.gnu.org website for the version 2 license,
21  * or you may write to the Free Software Foundation, Inc.,
22  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
23  */
24 
27 #include <gal/opengl/vertex_item.h>
28 #include <gal/opengl/shader.h>
29 #include <gal/opengl/utils.h>
30 
31 #include <confirm.h>
32 #include <list>
33 #include <cassert>
34 
35 #ifdef __WXDEBUG__
36 #include <wx/log.h>
37 #include <profile.h>
38 #endif /* __WXDEBUG__ */
39 
40 using namespace KIGFX;
41 
43  CACHED_CONTAINER( aSize ), m_verticesBuffer( 0 )
44 {
45  glGenBuffers( 1, &m_verticesBuffer );
46  checkGlError( "generating vertices buffer" );
47 
48  m_vertices = static_cast<VERTEX*>( malloc( aSize * VERTEX_SIZE ) );
49 }
50 
51 
53 {
54  glDeleteBuffers( 1, &m_verticesBuffer );
55  free( m_vertices );
56 }
57 
58 
60 {
61  if( !m_dirty )
62  return;
63 
64  // Upload vertices coordinates and shader types to GPU memory
65  glBindBuffer( GL_ARRAY_BUFFER, m_verticesBuffer );
66  checkGlError( "binding vertices buffer" );
67  glBufferData( GL_ARRAY_BUFFER, m_maxIndex * VERTEX_SIZE, m_vertices, GL_STREAM_DRAW );
68  checkGlError( "transferring vertices" );
69  glBindBuffer( GL_ARRAY_BUFFER, 0 );
70  checkGlError( "unbinding vertices buffer" );
71 }
72 
73 
74 bool CACHED_CONTAINER_RAM::defragmentResize( unsigned int aNewSize )
75 {
76  wxLogTrace( "GAL_CACHED_CONTAINER",
77  wxT( "Resizing & defragmenting container (memcpy) from %d to %d" ),
78  m_currentSize, aNewSize );
79 
80  // No shrinking if we cannot fit all the data
81  if( usedSpace() > aNewSize )
82  return false;
83 
84 #ifdef __WXDEBUG__
85  PROF_COUNTER totalTime;
86 #endif /* __WXDEBUG__ */
87 
88  VERTEX* newBufferMem = static_cast<VERTEX*>( malloc( aNewSize * VERTEX_SIZE ) );
89 
90  if( !newBufferMem )
91  return false;
92 
93  defragment( newBufferMem );
94 
95  // Switch to the new vertex buffer
96  free( m_vertices );
97  m_vertices = newBufferMem;
98 
99 #ifdef __WXDEBUG__
100  totalTime.Stop();
101 
102  wxLogTrace( "GAL_CACHED_CONTAINER",
103  "Defragmented container storing %d vertices / %.1f ms",
104  m_currentSize - m_freeSpace, totalTime.msecs() );
105 #endif /* __WXDEBUG__ */
106 
107  m_freeSpace += ( aNewSize - m_currentSize );
108  m_currentSize = aNewSize;
109 
110  // Now there is only one big chunk of free memory
111  m_freeChunks.clear();
112  m_freeChunks.insert( std::make_pair( m_freeSpace, m_currentSize - m_freeSpace ) );
113  m_dirty = true;
114 
115  return true;
116 }
void Stop()
save the time when this function was called, and set the counter stane to stop
Definition: profile.h:82
unsigned int usedSpace() const
Function usedSpace() returns size of the used memory space.
Class CAIRO_GAL is the cairo implementation of the graphics abstraction layer.
Definition: class_module.h:58
Data structure for vertices {X,Y,Z,R,G,B,A,shader&param}
Definition: vertex_common.h:56
This file is part of the common library.
Class to control vertex container and GPU with possibility of emulating old-style OpenGL 1...
void defragment(VERTEX *aTarget)
Transfers all stored data to a new buffer, removing empty spaces between the data chunks in the conta...
unsigned int m_currentSize
Current container size, expressed in vertices
int checkGlError(const std::string &aInfo, bool aThrow)
Checks if one of recent OpenGL operations has failed.
Definition: utils.cpp:30
The class PROF_COUNTER is a small class to help profiling.
Definition: profile.h:45
VERTEX * m_vertices
Actual storage memory
unsigned int m_freeSpace
Free space left in the container, expressed in vertices
Class to store VERTEX instances with caching.
CACHED_CONTAINER_RAM(unsigned int aSize=DEFAULT_SIZE)
bool defragmentResize(unsigned int aNewSize) override
Defragments the currently stored data and resizes the buffer.
FREE_CHUNK_MAP m_freeChunks
Stores size & offset of free chunks.
double msecs() const
Definition: profile.h:124
Class to handle an item held in a container.
GLuint m_verticesBuffer
Handle to vertices buffer
static constexpr size_t VERTEX_SIZE
Definition: vertex_common.h:63
unsigned int m_maxIndex
Maximal vertex index number stored in the container