KiCad PCB EDA Suite
cairo_compositor.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 (C) 2013 CERN
5  * Copyright (C) 2019 KiCad Developers, see AUTHORS.txt for contributors.
6  * @author Maciej Suminski <maciej.suminski@cern.ch>
7  *
8  * This program is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU General Public License
10  * as published by the Free Software Foundation; either version 2
11  * of the License, or (at your option) any later version.
12  *
13  * This program is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16  * GNU General Public License for more details.
17  *
18  * You should have received a copy of the GNU General Public License
19  * along with this program; if not, you may find one here:
20  * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
21  * or you may search the http:O//www.gnu.org website for the version 2 license,
22  * or you may write to the Free Software Foundation, Inc.,
23  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
24  */
25 
33 #include <wx/log.h>
34 
35 using namespace KIGFX;
36 
37 CAIRO_COMPOSITOR::CAIRO_COMPOSITOR( cairo_t** aMainContext ) :
38  m_current( 0 ), m_currentContext( aMainContext ), m_mainContext( *aMainContext ),
39  m_currentAntialiasingMode( CAIRO_ANTIALIAS_DEFAULT )
40 {
41  // Do not have uninitialized members:
42  cairo_matrix_init_identity( &m_matrix );
43  m_stride = 0;
44  m_bufferSize = 0;
45 }
46 
47 
49 {
50  clean();
51 }
52 
53 
55 {
56  // Nothing has to be done
57 }
58 
59 
61 {
62 
63  switch( aMode )
64  {
66  m_currentAntialiasingMode = CAIRO_ANTIALIAS_FAST;
67  break;
69  m_currentAntialiasingMode = CAIRO_ANTIALIAS_GOOD;
70  break;
72  m_currentAntialiasingMode = CAIRO_ANTIALIAS_BEST;
73  break;
74  default:
75  m_currentAntialiasingMode = CAIRO_ANTIALIAS_NONE;
76  }
77 
78  clean();
79 }
80 
81 
82 void CAIRO_COMPOSITOR::Resize( unsigned int aWidth, unsigned int aHeight )
83 {
84  clean();
85 
86  m_width = aWidth;
87  m_height = aHeight;
88 
89  m_stride = cairo_format_stride_for_width( CAIRO_FORMAT_ARGB32, m_width );
91 }
92 
93 
95 {
96  // Pixel storage
97  BitmapPtr bitmap = new uint32_t[m_bufferSize]();
98 
99  // Create the Cairo surface
100  cairo_surface_t* surface = cairo_image_surface_create_for_data(
101  (unsigned char*) bitmap,
102  CAIRO_FORMAT_ARGB32, m_width,
103  m_height, m_stride );
104  cairo_t* context = cairo_create( surface );
105 #ifdef DEBUG
106  cairo_status_t status = cairo_status( context );
107  wxASSERT_MSG( status == CAIRO_STATUS_SUCCESS, wxT( "Cairo context creation error" ) );
108 #endif /* DEBUG */
109 
110  // Set default settings for the buffer
111  cairo_set_antialias( context, m_currentAntialiasingMode );
112 
113  // Use the same transformation matrix as the main context
114  cairo_get_matrix( m_mainContext, &m_matrix );
115  cairo_set_matrix( context, &m_matrix );
116 
117  // Store the new buffer
118  CAIRO_BUFFER buffer = { context, surface, bitmap };
119  m_buffers.push_back( buffer );
120 
121  return usedBuffers();
122 }
123 
124 
125 void CAIRO_COMPOSITOR::SetBuffer( unsigned int aBufferHandle )
126 {
127  wxASSERT_MSG( aBufferHandle <= usedBuffers(), wxT( "Tried to use a not existing buffer" ) );
128 
129  // Get currently used transformation matrix, so it can be applied to the new buffer
130  cairo_get_matrix( *m_currentContext, &m_matrix );
131 
132  m_current = aBufferHandle - 1;
134 
135  // Apply the current transformation matrix
136  cairo_set_matrix( *m_currentContext, &m_matrix );
137 }
138 
140 {
141 }
142 
144 {
145  // Clear the pixel storage
146  memset( m_buffers[m_current].bitmap, 0x00, m_bufferSize * sizeof(int) );
147 }
148 
149 
150 void CAIRO_COMPOSITOR::DrawBuffer( unsigned int aBufferHandle )
151 {
152  wxASSERT_MSG( aBufferHandle <= usedBuffers(), wxT( "Tried to use a not existing buffer" ) );
153 
154  // Reset the transformation matrix, so it is possible to composite images using
155  // screen coordinates instead of world coordinates
156  cairo_get_matrix( m_mainContext, &m_matrix );
157  cairo_identity_matrix( m_mainContext );
158 
159  // Draw the selected buffer contents
160  cairo_set_source_surface( m_mainContext, m_buffers[aBufferHandle - 1].surface, 0.0, 0.0 );
161  cairo_paint( m_mainContext );
162 
163  // Restore the transformation matrix
164  cairo_set_matrix( m_mainContext, &m_matrix );
165 }
166 
168 {
169 }
170 
172 {
173  CAIRO_BUFFERS::const_iterator it;
174 
175  for( it = m_buffers.begin(); it != m_buffers.end(); ++it )
176  {
177  cairo_destroy( it->context );
178  cairo_surface_destroy( it->surface );
179  delete[] it->bitmap;
180  }
181 
182  m_buffers.clear();
183 }
void clean()
Function clean() performs freeing of resources.
unsigned int m_stride
Stride to use given the desired format and width.
Class CAIRO_GAL is the cairo implementation of the graphics abstraction layer.
Definition: color4d.cpp:131
unsigned int m_width
Width of the buffer (in pixels)
Definition: compositor.h:119
virtual void Begin() override
Function Begin() Call this at the beginning of each frame.
virtual void Present() override
Function Present() Call this to present the output buffer to the screen.
unsigned int usedBuffers()
Returns number of currently used buffers.
virtual void ClearBuffer(const COLOR4D &aColor) override
Function ClearBuffer() clears the selected buffer (set by the SetBuffer() function).
virtual void SetBuffer(unsigned int aBufferHandle) override
Function SetBuffer() sets the selected buffer as the rendering target.
unsigned int m_current
Currently used buffer handle.
CAIRO_BUFFERS m_buffers
Stores information about initialized buffers.
Class that handles multitarget rendering (ie.
cairo_antialias_t m_currentAntialiasingMode
cairo_t ** m_currentContext
Pointer to the current context, so it can be changed.
virtual void DrawBuffer(unsigned int aBufferHandle) override
Function DrawBuffer() draws the selected buffer to the output buffer.
void SetAntialiasingMode(CAIRO_ANTIALIASING_MODE aMode)
unsigned int m_bufferSize
Amount of memory needed to store a buffer.
virtual void Initialize() override
Function Reset() performs primary initialiation, necessary to use the object.
CAIRO_COMPOSITOR(cairo_t **aMainContext)
virtual void Resize(unsigned int aWidth, unsigned int aHeight) override
Function Resize() clears the state of COMPOSITOR, so it has to be reinitialized again with the new di...
cairo_matrix_t m_matrix
Transformation matrix.
cairo_t * m_mainContext
Rendering target used for compositing (the main display)
unsigned int m_height
Height of the buffer (in pixels)
Definition: compositor.h:120
virtual unsigned int CreateBuffer() override
Function CreateBuffer() prepares a new buffer that may be used as a rendering target.
COLOR4D is the color representation with 4 components: red, green, blue, alpha.
Definition: color4d.h:40