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
 Perform primary initialization, necessary to use the object. More...
 
virtual void Resize (unsigned int aWidth, unsigned int aHeight) override
 Clear the state of COMPOSITOR, so it has to be reinitialized again with the new dimensions. More...
 
virtual unsigned int CreateBuffer () override
 Prepare a new buffer that may be used as a rendering target. More...
 
virtual unsigned int GetBuffer () const override
 Return currently used buffer handle. More...
 
virtual void SetBuffer (unsigned int aBufferHandle) override
 Set the selected buffer as the rendering target. More...
 
virtual void Begin () override
 Call this at the beginning of each frame. More...
 
virtual void ClearBuffer (const COLOR4D &aColor) override
 Clear the selected buffer (set by the SetBuffer() function). More...
 
void DrawBuffer (unsigned int aSourceHandle, unsigned int aDestHandle, cairo_operator_t op)
 Paints source to destination using the cairo operator. More...
 
virtual void DrawBuffer (unsigned int aBufferHandle) override
 Draw the selected buffer to the output buffer. More...
 
virtual void Present () override
 Call this to present the output buffer to the screen. More...
 
void SetAntialiasingMode (CAIRO_ANTIALIASING_MODE aMode)
 
CAIRO_ANTIALIASING_MODE GetAntialiasingMode () const
 
virtual void SetMainContext (cairo_t *aMainContext)
 Set a context to be treated as the main context (ie. More...
 

Protected Types

typedef uint32_t * BitmapPtr
 
typedef std::deque< CAIRO_BUFFERCAIRO_BUFFERS
 

Protected Member Functions

void clean ()
 Perform freeing of resources. More...
 
unsigned int usedBuffers ()
 Return 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...
 
cairo_antialias_t m_currentAntialiasingMode
 
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 44 of file cairo_compositor.h.

Member Typedef Documentation

◆ BitmapPtr

typedef uint32_t* KIGFX::CAIRO_COMPOSITOR::BitmapPtr
protected

Definition at line 129 of file cairo_compositor.h.

◆ CAIRO_BUFFERS

Definition at line 138 of file cairo_compositor.h.

Constructor & Destructor Documentation

◆ CAIRO_COMPOSITOR()

CAIRO_COMPOSITOR::CAIRO_COMPOSITOR ( cairo_t **  aMainContext)

Definition at line 38 of file cairo_compositor.cpp.

38  :
39  m_current( 0 ),
40  m_currentContext( aMainContext ),
41  m_mainContext( *aMainContext ),
42  m_currentAntialiasingMode( CAIRO_ANTIALIAS_DEFAULT )
43 {
44  // Do not have uninitialized members:
45  cairo_matrix_init_identity( &m_matrix );
46  m_stride = 0;
47  m_bufferSize = 0;
48 }
unsigned int m_stride
Stride to use given the desired format and width.
unsigned int m_current
Currently used buffer handle.
cairo_antialias_t m_currentAntialiasingMode
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)

References m_bufferSize, m_matrix, and m_stride.

◆ ~CAIRO_COMPOSITOR()

CAIRO_COMPOSITOR::~CAIRO_COMPOSITOR ( )
virtual

Definition at line 51 of file cairo_compositor.cpp.

52 {
53  clean();
54 }
void clean()
Perform freeing of resources.

References clean().

Member Function Documentation

◆ Begin()

void CAIRO_COMPOSITOR::Begin ( )
overridevirtual

Call this at the beginning of each frame.

Implements KIGFX::COMPOSITOR.

Definition at line 133 of file cairo_compositor.cpp.

134 {
135 }

◆ clean()

void CAIRO_COMPOSITOR::clean ( )
protected

Perform freeing of resources.

Definition at line 191 of file cairo_compositor.cpp.

192 {
193  CAIRO_BUFFERS::const_iterator it;
194 
195  for( it = m_buffers.begin(); it != m_buffers.end(); ++it )
196  {
197  cairo_destroy( it->context );
198  cairo_surface_destroy( it->surface );
199  delete[] it->bitmap;
200  }
201 
202  m_buffers.clear();
203 }
CAIRO_BUFFERS m_buffers
Stores information about initialized buffers.

References m_buffers.

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

◆ ClearBuffer()

void CAIRO_COMPOSITOR::ClearBuffer ( const COLOR4D aColor)
overridevirtual

Clear the selected buffer (set by the SetBuffer() function).

Implements KIGFX::COMPOSITOR.

Definition at line 138 of file cairo_compositor.cpp.

139 {
140  // Clear the pixel storage
141  memset( m_buffers[m_current].bitmap, 0x00, m_bufferSize * sizeof( int ) );
142 }
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.

References m_buffers, m_bufferSize, and m_current.

◆ CreateBuffer()

unsigned int CAIRO_COMPOSITOR::CreateBuffer ( )
overridevirtual

Prepare 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 88 of file cairo_compositor.cpp.

89 {
90  // Pixel storage
91  BitmapPtr bitmap = new uint32_t[m_bufferSize]();
92 
93  // Create the Cairo surface
94  cairo_surface_t* surface = cairo_image_surface_create_for_data(
95  (unsigned char*) bitmap, CAIRO_FORMAT_ARGB32, m_width, m_height, m_stride );
96  cairo_t* context = cairo_create( surface );
97 
98 #ifdef DEBUG
99  cairo_status_t status = cairo_status( context );
100  wxASSERT_MSG( status == CAIRO_STATUS_SUCCESS, wxT( "Cairo context creation error" ) );
101 #endif /* DEBUG */
102 
103  // Set default settings for the buffer
104  cairo_set_antialias( context, m_currentAntialiasingMode );
105 
106  // Use the same transformation matrix as the main context
107  cairo_get_matrix( m_mainContext, &m_matrix );
108  cairo_set_matrix( context, &m_matrix );
109 
110  // Store the new buffer
111  CAIRO_BUFFER buffer = { context, surface, bitmap };
112  m_buffers.push_back( buffer );
113 
114  return usedBuffers();
115 }
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:112
unsigned int usedBuffers()
Return number of currently used buffers.
CAIRO_BUFFERS m_buffers
Stores information about initialized buffers.
cairo_antialias_t m_currentAntialiasingMode
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)
unsigned int m_height
Height of the buffer (in pixels)
Definition: compositor.h:113

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

◆ DrawBuffer() [1/2]

void CAIRO_COMPOSITOR::DrawBuffer ( unsigned int  aSourceHandle,
unsigned int  aDestHandle,
cairo_operator_t  op 
)

Paints source to destination using the cairo operator.

Useful for differential mode.

Parameters
aSourceHandleSource buffer to paint
aDestHandleDestination buffer to paint on to
opPainting operation

Definition at line 145 of file cairo_compositor.cpp.

147 {
148  wxASSERT_MSG( aSourceHandle <= usedBuffers() && aDestHandle <= usedBuffers(),
149  wxT( "Tried to use a not existing buffer" ) );
150 
151  // Reset the transformation matrix, so it is possible to composite images using
152  // screen coordinates instead of world coordinates
153  cairo_get_matrix( m_mainContext, &m_matrix );
154  cairo_identity_matrix( m_mainContext );
155 
156  // Draw the selected buffer contents
157  cairo_t* ct = cairo_create( m_buffers[aDestHandle - 1].surface );
158  cairo_set_operator( ct, op );
159  cairo_set_source_surface( ct, m_buffers[aSourceHandle - 1].surface, 0.0, 0.0 );
160  cairo_paint( ct );
161  cairo_destroy( ct );
162 
163  // Restore the transformation matrix
164  cairo_set_matrix( m_mainContext, &m_matrix );
165 }
unsigned int usedBuffers()
Return 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)

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

◆ DrawBuffer() [2/2]

void CAIRO_COMPOSITOR::DrawBuffer ( unsigned int  aBufferHandle)
overridevirtual

Draw the selected buffer to the output buffer.

Parameters
aBufferHandleis the handle of the buffer to be drawn.

Implements KIGFX::COMPOSITOR.

Definition at line 168 of file cairo_compositor.cpp.

169 {
170  wxASSERT_MSG( aBufferHandle <= usedBuffers(), wxT( "Tried to use a not existing buffer" ) );
171 
172  // Reset the transformation matrix, so it is possible to composite images using
173  // screen coordinates instead of world coordinates
174  cairo_get_matrix( m_mainContext, &m_matrix );
175  cairo_identity_matrix( m_mainContext );
176 
177  // Draw the selected buffer contents
178  cairo_set_source_surface( m_mainContext, m_buffers[aBufferHandle - 1].surface, 0.0, 0.0 );
179  cairo_paint( m_mainContext );
180 
181  // Restore the transformation matrix
182  cairo_set_matrix( m_mainContext, &m_matrix );
183 }
unsigned int usedBuffers()
Return 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)

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

◆ GetAntialiasingMode()

CAIRO_ANTIALIASING_MODE KIGFX::CAIRO_COMPOSITOR::GetAntialiasingMode ( ) const
inline

Definition at line 90 of file cairo_compositor.h.

91  {
93  {
94  case CAIRO_ANTIALIAS_FAST:
96  case CAIRO_ANTIALIAS_GOOD:
98  default:
100  }
101  }
cairo_antialias_t m_currentAntialiasingMode

References KIGFX::FAST, KIGFX::GOOD, m_currentAntialiasingMode, and KIGFX::NONE.

◆ GetBuffer()

virtual unsigned int KIGFX::CAIRO_COMPOSITOR::GetBuffer ( ) const
inlineoverridevirtual

Return currently used buffer handle.

Returns
Currently used buffer handle.

Implements KIGFX::COMPOSITOR.

Definition at line 60 of file cairo_compositor.h.

61  {
62  return m_current + 1;
63  }
unsigned int m_current
Currently used buffer handle.

References m_current.

◆ Initialize()

void CAIRO_COMPOSITOR::Initialize ( )
overridevirtual

Perform primary initialization, necessary to use the object.

Implements KIGFX::COMPOSITOR.

Definition at line 57 of file cairo_compositor.cpp.

58 {
59  // Nothing has to be done
60 }

◆ Present()

void CAIRO_COMPOSITOR::Present ( )
overridevirtual

Call this to present the output buffer to the screen.

Implements KIGFX::COMPOSITOR.

Definition at line 186 of file cairo_compositor.cpp.

187 {
188 }

◆ Resize()

void CAIRO_COMPOSITOR::Resize ( unsigned int  aWidth,
unsigned int  aHeight 
)
overridevirtual

Clear 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 76 of file cairo_compositor.cpp.

77 {
78  clean();
79 
80  m_width = aWidth;
81  m_height = aHeight;
82 
83  m_stride = cairo_format_stride_for_width( CAIRO_FORMAT_ARGB32, m_width );
85 }
void clean()
Perform 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:112
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:113

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

◆ SetAntialiasingMode()

void CAIRO_COMPOSITOR::SetAntialiasingMode ( CAIRO_ANTIALIASING_MODE  aMode)

Definition at line 63 of file cairo_compositor.cpp.

64 {
65  switch( aMode )
66  {
67  case CAIRO_ANTIALIASING_MODE::FAST: m_currentAntialiasingMode = CAIRO_ANTIALIAS_FAST; break;
68  case CAIRO_ANTIALIASING_MODE::GOOD: m_currentAntialiasingMode = CAIRO_ANTIALIAS_GOOD; break;
69  default: m_currentAntialiasingMode = CAIRO_ANTIALIAS_NONE;
70  }
71 
72  clean();
73 }
void clean()
Perform freeing of resources.
cairo_antialias_t m_currentAntialiasingMode

References clean(), KIGFX::FAST, KIGFX::GOOD, and m_currentAntialiasingMode.

◆ SetBuffer()

void CAIRO_COMPOSITOR::SetBuffer ( unsigned int  aBufferHandle)
overridevirtual

Set 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 118 of file cairo_compositor.cpp.

119 {
120  wxASSERT_MSG( aBufferHandle <= usedBuffers(), wxT( "Tried to use a not existing buffer" ) );
121 
122  // Get currently used transformation matrix, so it can be applied to the new buffer
123  cairo_get_matrix( *m_currentContext, &m_matrix );
124 
125  m_current = aBufferHandle - 1;
127 
128  // Apply the current transformation matrix
129  cairo_set_matrix( *m_currentContext, &m_matrix );
130 }
unsigned int usedBuffers()
Return 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.

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

◆ SetMainContext()

virtual void KIGFX::CAIRO_COMPOSITOR::SetMainContext ( cairo_t *  aMainContext)
inlinevirtual

Set 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 109 of file cairo_compositor.h.

110  {
111  m_mainContext = aMainContext;
112 
113  // Use the context's transformation matrix
114  cairo_get_matrix( m_mainContext, &m_matrix );
115  }
cairo_matrix_t m_matrix
Transformation matrix.
cairo_t * m_mainContext
Rendering target used for compositing (the main display)

References m_mainContext, and m_matrix.

◆ usedBuffers()

unsigned int KIGFX::CAIRO_COMPOSITOR::usedBuffers ( )
inlineprotected

Return number of currently used buffers.

Definition at line 124 of file cairo_compositor.h.

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

References m_buffers.

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

Member Data Documentation

◆ m_buffers

CAIRO_BUFFERS KIGFX::CAIRO_COMPOSITOR::m_buffers
protected

Stores information about initialized buffers.

Definition at line 150 of file cairo_compositor.h.

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

◆ m_bufferSize

unsigned int KIGFX::CAIRO_COMPOSITOR::m_bufferSize
protected

Amount of memory needed to store a buffer.

Definition at line 153 of file cairo_compositor.h.

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

◆ m_current

unsigned int KIGFX::CAIRO_COMPOSITOR::m_current
protected

Currently used buffer handle.

Definition at line 137 of file cairo_compositor.h.

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

◆ m_currentAntialiasingMode

cairo_antialias_t KIGFX::CAIRO_COMPOSITOR::m_currentAntialiasingMode
protected

Definition at line 155 of file cairo_compositor.h.

Referenced by CreateBuffer(), GetAntialiasingMode(), and SetAntialiasingMode().

◆ m_currentContext

cairo_t** KIGFX::CAIRO_COMPOSITOR::m_currentContext
protected

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

Definition at line 141 of file cairo_compositor.h.

Referenced by SetBuffer().

◆ m_height

unsigned int KIGFX::COMPOSITOR::m_height
protectedinherited

Height of the buffer (in pixels)

Definition at line 113 of file compositor.h.

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

◆ m_mainContext

cairo_t* KIGFX::CAIRO_COMPOSITOR::m_mainContext
protected

Rendering target used for compositing (the main display)

Definition at line 144 of file cairo_compositor.h.

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

◆ m_matrix

cairo_matrix_t KIGFX::CAIRO_COMPOSITOR::m_matrix
protected

Transformation matrix.

Definition at line 147 of file cairo_compositor.h.

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

◆ m_stride

unsigned int KIGFX::CAIRO_COMPOSITOR::m_stride
protected

Stride to use given the desired format and width.

Definition at line 152 of file cairo_compositor.h.

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

◆ m_width

unsigned int KIGFX::COMPOSITOR::m_width
protectedinherited

Width of the buffer (in pixels)

Definition at line 112 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: