KiCad PCB EDA Suite
KIGFX::CAIRO_COMPOSITOR Class Reference

#include <cairo_compositor.h>

Inheritance diagram for KIGFX::CAIRO_COMPOSITOR:
KIGFX::COMPOSITOR

Classes

struct  CAIRO_BUFFER
 

Public Member Functions

 CAIRO_COMPOSITOR (cairo_t **aMainContext)
 
virtual ~CAIRO_COMPOSITOR ()
 
virtual void Initialize () override
 Function Reset() performs primary initialiation, necessary to use the object. More...
 
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 dimensions. More...
 
virtual unsigned int CreateBuffer () override
 Function CreateBuffer() prepares a new buffer that may be used as a rendering target. More...
 
virtual unsigned int GetBuffer () const override
 Function GetBuffer() returns currently used buffer handle. More...
 
virtual void SetBuffer (unsigned int aBufferHandle) override
 Function SetBuffer() sets the selected buffer as the rendering target. More...
 
virtual void Begin () override
 Function Begin() Call this at the beginning of each frame. More...
 
virtual void ClearBuffer (const COLOR4D &aColor) override
 Function ClearBuffer() clears the selected buffer (set by the SetBuffer() function). More...
 
virtual void DrawBuffer (unsigned int aBufferHandle) override
 Function DrawBuffer() draws the selected buffer to the output buffer. More...
 
virtual void Present () override
 Function Present() Call this to present the output buffer to the screen. More...
 
virtual void SetMainContext (cairo_t *aMainContext)
 Function SetMainContext() Sets a context to be treated as the main context (ie. More...
 

Protected Types

typedef boost::shared_array< unsigned int > BitmapPtr
 
typedef std::deque< CAIRO_BUFFERCAIRO_BUFFERS
 

Protected Member Functions

void clean ()
 Function clean() performs freeing of resources. More...
 
unsigned int usedBuffers ()
 Returns number of currently used buffers. More...
 

Protected Attributes

unsigned int m_current
 Currently used buffer handle. More...
 
cairo_t ** m_currentContext
 Pointer to the current context, so it can be changed. More...
 
cairo_t * m_mainContext
 Rendering target used for compositing (the main display) More...
 
cairo_matrix_t m_matrix
 Transformation matrix. More...
 
CAIRO_BUFFERS m_buffers
 Stores information about initialized buffers. More...
 
unsigned int m_stride
 Stride to use given the desired format and width. More...
 
unsigned int m_bufferSize
 Amount of memory needed to store a buffer. More...
 
unsigned int m_width
 Width of the buffer (in pixels) More...
 
unsigned int m_height
 Height of the buffer (in pixels) More...
 

Detailed Description

Definition at line 41 of file cairo_compositor.h.

Member Typedef Documentation

typedef boost::shared_array<unsigned int> KIGFX::CAIRO_COMPOSITOR::BitmapPtr
protected

Definition at line 93 of file cairo_compositor.h.

Definition at line 102 of file cairo_compositor.h.

Constructor & Destructor Documentation

CAIRO_COMPOSITOR::CAIRO_COMPOSITOR ( cairo_t **  aMainContext)

Definition at line 36 of file cairo_compositor.cpp.

References m_bufferSize, m_matrix, and m_stride.

36  :
37  m_current( 0 ), m_currentContext( aMainContext ), m_mainContext( *aMainContext )
38 {
39  // Do not have uninitialized members:
40  cairo_matrix_init_identity( &m_matrix );
41  m_stride = 0;
42  m_bufferSize = 0;
43 }
unsigned int m_stride
Stride to use given the desired format and width.
unsigned int m_current
Currently used buffer handle.
cairo_t ** m_currentContext
Pointer to the current context, so it can be changed.
unsigned int m_bufferSize
Amount of memory needed to store a buffer.
cairo_matrix_t m_matrix
Transformation matrix.
cairo_t * m_mainContext
Rendering target used for compositing (the main display)
CAIRO_COMPOSITOR::~CAIRO_COMPOSITOR ( )
virtual

Definition at line 46 of file cairo_compositor.cpp.

References clean().

47 {
48  clean();
49 }
void clean()
Function clean() performs freeing of resources.

Member Function Documentation

void CAIRO_COMPOSITOR::Begin ( )
overridevirtual

Function Begin() Call this at the beginning of each frame.

Implements KIGFX::COMPOSITOR.

Definition at line 119 of file cairo_compositor.cpp.

Referenced by GetBuffer().

120 {
121 }
void CAIRO_COMPOSITOR::clean ( )
protected

Function clean() performs freeing of resources.

Definition at line 151 of file cairo_compositor.cpp.

References m_buffers.

Referenced by Resize(), and ~CAIRO_COMPOSITOR().

152 {
153  CAIRO_BUFFERS::const_iterator it;
154 
155  for( it = m_buffers.begin(); it != m_buffers.end(); ++it )
156  {
157  cairo_destroy( it->context );
158  cairo_surface_destroy( it->surface );
159  }
160 
161  m_buffers.clear();
162 }
CAIRO_BUFFERS m_buffers
Stores information about initialized buffers.
void CAIRO_COMPOSITOR::ClearBuffer ( const COLOR4D aColor)
overridevirtual

Function ClearBuffer() clears the selected buffer (set by the SetBuffer() function).

Implements KIGFX::COMPOSITOR.

Definition at line 123 of file cairo_compositor.cpp.

References m_buffers, m_bufferSize, and m_current.

Referenced by GetBuffer().

124 {
125  // Clear the pixel storage
126  memset( m_buffers[m_current].bitmap.get(), 0x00, m_bufferSize * sizeof(int) );
127 }
unsigned int m_current
Currently used buffer handle.
CAIRO_BUFFERS m_buffers
Stores information about initialized buffers.
unsigned int m_bufferSize
Amount of memory needed to store a buffer.
unsigned int CAIRO_COMPOSITOR::CreateBuffer ( )
overridevirtual

Function CreateBuffer() prepares a new buffer that may be used as a rendering target.

Returns
is the handle of the buffer. In case of failure 0 (zero) is returned as the handle.

Implements KIGFX::COMPOSITOR.

Definition at line 70 of file cairo_compositor.cpp.

References m_buffers, m_bufferSize, KIGFX::COMPOSITOR::m_height, m_mainContext, m_matrix, m_stride, KIGFX::COMPOSITOR::m_width, and usedBuffers().

71 {
72  // Pixel storage
73  BitmapPtr bitmap( new unsigned int[m_bufferSize] );
74 
75  memset( bitmap.get(), 0x00, m_bufferSize * sizeof(int) );
76 
77  // Create the Cairo surface
78  cairo_surface_t* surface = cairo_image_surface_create_for_data(
79  (unsigned char*) bitmap.get(),
80  CAIRO_FORMAT_ARGB32, m_width,
81  m_height, m_stride );
82  cairo_t* context = cairo_create( surface );
83 #ifdef __WXDEBUG__
84  cairo_status_t status = cairo_status( context );
85  wxASSERT_MSG( status == CAIRO_STATUS_SUCCESS, wxT( "Cairo context creation error" ) );
86 #endif /* __WXDEBUG__ */
87 
88  // Set default settings for the buffer
89  cairo_set_antialias( context, CAIRO_ANTIALIAS_NONE );
90  cairo_set_line_join( context, CAIRO_LINE_JOIN_ROUND );
91  cairo_set_line_cap( context, CAIRO_LINE_CAP_ROUND );
92 
93  // Use the same transformation matrix as the main context
94  cairo_get_matrix( m_mainContext, &m_matrix );
95  cairo_set_matrix( context, &m_matrix );
96 
97  // Store the new buffer
98  CAIRO_BUFFER buffer = { context, surface, bitmap };
99  m_buffers.push_back( buffer );
100 
101  return usedBuffers();
102 }
unsigned int m_stride
Stride to use given the desired format and width.
unsigned int m_width
Width of the buffer (in pixels)
Definition: compositor.h:119
unsigned int usedBuffers()
Returns number of currently used buffers.
CAIRO_BUFFERS m_buffers
Stores information about initialized buffers.
unsigned int m_bufferSize
Amount of memory needed to store a buffer.
boost::shared_array< unsigned int > BitmapPtr
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
void CAIRO_COMPOSITOR::DrawBuffer ( unsigned int  aBufferHandle)
overridevirtual

Function DrawBuffer() draws the selected buffer to the output buffer.

Parameters
aBufferHandleis the handle of the buffer to be drawn.

Implements KIGFX::COMPOSITOR.

Definition at line 130 of file cairo_compositor.cpp.

References m_buffers, m_mainContext, m_matrix, and usedBuffers().

Referenced by GetBuffer().

131 {
132  wxASSERT_MSG( aBufferHandle <= usedBuffers(), wxT( "Tried to use a not existing buffer" ) );
133 
134  // Reset the transformation matrix, so it is possible to composite images using
135  // screen coordinates instead of world coordinates
136  cairo_get_matrix( m_mainContext, &m_matrix );
137  cairo_identity_matrix( m_mainContext );
138 
139  // Draw the selected buffer contents
140  cairo_set_source_surface( m_mainContext, m_buffers[aBufferHandle - 1].surface, 0.0, 0.0 );
141  cairo_paint( m_mainContext );
142 
143  // Restore the transformation matrix
144  cairo_set_matrix( m_mainContext, &m_matrix );
145 }
unsigned int usedBuffers()
Returns number of currently used buffers.
CAIRO_BUFFERS m_buffers
Stores information about initialized buffers.
cairo_matrix_t m_matrix
Transformation matrix.
cairo_t * m_mainContext
Rendering target used for compositing (the main display)
virtual unsigned int KIGFX::CAIRO_COMPOSITOR::GetBuffer ( ) const
inlineoverridevirtual

Function GetBuffer() returns currently used buffer handle.

Returns
Currently used buffer handle.

Implements KIGFX::COMPOSITOR.

Definition at line 57 of file cairo_compositor.h.

References Begin(), ClearBuffer(), DrawBuffer(), m_current, Present(), and SetBuffer().

58  {
59  return m_current + 1;
60  }
unsigned int m_current
Currently used buffer handle.
void CAIRO_COMPOSITOR::Initialize ( )
overridevirtual

Function Reset() performs primary initialiation, necessary to use the object.

Implements KIGFX::COMPOSITOR.

Definition at line 52 of file cairo_compositor.cpp.

53 {
54  // Nothing has to be done
55 }
void CAIRO_COMPOSITOR::Present ( )
overridevirtual

Function Present() Call this to present the output buffer to the screen.

Implements KIGFX::COMPOSITOR.

Definition at line 147 of file cairo_compositor.cpp.

Referenced by GetBuffer().

148 {
149 }
void CAIRO_COMPOSITOR::Resize ( unsigned int  aWidth,
unsigned int  aHeight 
)
overridevirtual

Function Resize() clears the state of COMPOSITOR, so it has to be reinitialized again with the new dimensions.

Parameters
aWidthis the framebuffer width (in pixels).
aHeightis the framebuffer height (in pixels).

Implements KIGFX::COMPOSITOR.

Definition at line 58 of file cairo_compositor.cpp.

References clean(), m_bufferSize, KIGFX::COMPOSITOR::m_height, m_stride, and KIGFX::COMPOSITOR::m_width.

59 {
60  clean();
61 
62  m_width = aWidth;
63  m_height = aHeight;
64 
65  m_stride = cairo_format_stride_for_width( CAIRO_FORMAT_ARGB32, m_width );
67 }
void clean()
Function clean() performs freeing of resources.
unsigned int m_stride
Stride to use given the desired format and width.
unsigned int m_width
Width of the buffer (in pixels)
Definition: compositor.h:119
unsigned int m_bufferSize
Amount of memory needed to store a buffer.
unsigned int m_height
Height of the buffer (in pixels)
Definition: compositor.h:120
void CAIRO_COMPOSITOR::SetBuffer ( unsigned int  aBufferHandle)
overridevirtual

Function SetBuffer() sets the selected buffer as the rendering target.

All the following drawing functions are going to be rendered in the selected buffer.

Parameters
aBufferHandleis the handle of the buffer or 0 in case of rendering directly to the display.

Implements KIGFX::COMPOSITOR.

Definition at line 105 of file cairo_compositor.cpp.

References m_buffers, m_current, m_currentContext, m_matrix, and usedBuffers().

Referenced by GetBuffer().

106 {
107  wxASSERT_MSG( aBufferHandle <= usedBuffers(), wxT( "Tried to use a not existing buffer" ) );
108 
109  // Get currently used transformation matrix, so it can be applied to the new buffer
110  cairo_get_matrix( *m_currentContext, &m_matrix );
111 
112  m_current = aBufferHandle - 1;
114 
115  // Apply the current transformation matrix
116  cairo_set_matrix( *m_currentContext, &m_matrix );
117 }
unsigned int usedBuffers()
Returns number of currently used buffers.
unsigned int m_current
Currently used buffer handle.
CAIRO_BUFFERS m_buffers
Stores information about initialized buffers.
cairo_t ** m_currentContext
Pointer to the current context, so it can be changed.
cairo_matrix_t m_matrix
Transformation matrix.
virtual void KIGFX::CAIRO_COMPOSITOR::SetMainContext ( cairo_t *  aMainContext)
inlinevirtual

Function SetMainContext() Sets a context to be treated as the main context (ie.

as a target of buffers rendering and as a source of settings for newly created buffers).

Parameters
aMainContextis the context that should be treated as the main one.

Definition at line 84 of file cairo_compositor.h.

References m_mainContext, and m_matrix.

85  {
86  m_mainContext = aMainContext;
87 
88  // Use the context's transformation matrix
89  cairo_get_matrix( m_mainContext, &m_matrix );
90  }
cairo_matrix_t m_matrix
Transformation matrix.
cairo_t * m_mainContext
Rendering target used for compositing (the main display)
unsigned int KIGFX::CAIRO_COMPOSITOR::usedBuffers ( )
inlineprotected

Returns number of currently used buffers.

Definition at line 126 of file cairo_compositor.h.

Referenced by CreateBuffer(), DrawBuffer(), and SetBuffer().

127  {
128  return m_buffers.size();
129  }
CAIRO_BUFFERS m_buffers
Stores information about initialized buffers.

Member Data Documentation

CAIRO_BUFFERS KIGFX::CAIRO_COMPOSITOR::m_buffers
protected

Stores information about initialized buffers.

Definition at line 114 of file cairo_compositor.h.

Referenced by clean(), ClearBuffer(), CreateBuffer(), DrawBuffer(), and SetBuffer().

unsigned int KIGFX::CAIRO_COMPOSITOR::m_bufferSize
protected

Amount of memory needed to store a buffer.

Definition at line 117 of file cairo_compositor.h.

Referenced by CAIRO_COMPOSITOR(), ClearBuffer(), CreateBuffer(), and Resize().

unsigned int KIGFX::CAIRO_COMPOSITOR::m_current
protected

Currently used buffer handle.

Definition at line 101 of file cairo_compositor.h.

Referenced by ClearBuffer(), GetBuffer(), and SetBuffer().

cairo_t** KIGFX::CAIRO_COMPOSITOR::m_currentContext
protected

Pointer to the current context, so it can be changed.

Definition at line 105 of file cairo_compositor.h.

Referenced by SetBuffer().

unsigned int KIGFX::COMPOSITOR::m_height
protectedinherited

Height of the buffer (in pixels)

Definition at line 120 of file compositor.h.

Referenced by CreateBuffer(), KIGFX::OPENGL_COMPOSITOR::GetScreenSize(), Resize(), and KIGFX::OPENGL_COMPOSITOR::Resize().

cairo_t* KIGFX::CAIRO_COMPOSITOR::m_mainContext
protected

Rendering target used for compositing (the main display)

Definition at line 108 of file cairo_compositor.h.

Referenced by CreateBuffer(), DrawBuffer(), and SetMainContext().

cairo_matrix_t KIGFX::CAIRO_COMPOSITOR::m_matrix
protected

Transformation matrix.

Definition at line 111 of file cairo_compositor.h.

Referenced by CAIRO_COMPOSITOR(), CreateBuffer(), DrawBuffer(), SetBuffer(), and SetMainContext().

unsigned int KIGFX::CAIRO_COMPOSITOR::m_stride
protected

Stride to use given the desired format and width.

Definition at line 116 of file cairo_compositor.h.

Referenced by CAIRO_COMPOSITOR(), CreateBuffer(), and Resize().

unsigned int KIGFX::COMPOSITOR::m_width
protectedinherited

Width of the buffer (in pixels)

Definition at line 119 of file compositor.h.

Referenced by CreateBuffer(), KIGFX::OPENGL_COMPOSITOR::GetScreenSize(), Resize(), and KIGFX::OPENGL_COMPOSITOR::Resize().


The documentation for this class was generated from the following files: