KiCad PCB EDA Suite
cairo_gal.h
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) 2012 Torsten Hueter, torstenhtr <at> gmx.de
5  * Copyright (C) 2012 Kicad Developers, see change_log.txt for contributors.
6  * Copyright (C) 2017-2018 CERN
7  * @author Maciej Suminski <maciej.suminski@cern.ch>
8  *
9  * CairoGal - Graphics Abstraction Layer for Cairo
10  *
11  * This program is free software; you can redistribute it and/or
12  * modify it under the terms of the GNU General Public License
13  * as published by the Free Software Foundation; either version 2
14  * of the License, or (at your option) any later version.
15  *
16  * This program is distributed in the hope that it will be useful,
17  * but WITHOUT ANY WARRANTY; without even the implied warranty of
18  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19  * GNU General Public License for more details.
20  *
21  * You should have received a copy of the GNU General Public License
22  * along with this program; if not, you may find one here:
23  * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
24  * or you may search the http://www.gnu.org website for the version 2 license,
25  * or you may write to the Free Software Foundation, Inc.,
26  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
27  */
28 
29 #ifndef CAIROGAL_H_
30 #define CAIROGAL_H_
31 
32 #include <map>
33 #include <iterator>
34 
35 #include <cairo.h>
36 
38 #include <wx/dcbuffer.h>
39 
40 #include <memory>
41 
42 #if defined(__WXMSW__)
43 #define SCREEN_DEPTH 24
44 #else
45 #if wxCHECK_VERSION( 2, 9, 0 )
46 #define SCREEN_DEPTH wxBITMAP_SCREEN_DEPTH
47 #else
48 #define SCREEN_DEPTH 32
49 #endif
50 #endif
51 
64 namespace KIGFX
65 {
66 class CAIRO_COMPOSITOR;
67 
68 class CAIRO_GAL_BASE : public GAL
69 {
70 public:
71  CAIRO_GAL_BASE( GAL_DISPLAY_OPTIONS& aDisplayOptions );
72 
73  virtual ~CAIRO_GAL_BASE();
74 
75  // ---------------
76  // Drawing methods
77  // ---------------
78 
80  virtual void DrawLine( const VECTOR2D& aStartPoint, const VECTOR2D& aEndPoint ) override;
81 
83  virtual void DrawSegment( const VECTOR2D& aStartPoint, const VECTOR2D& aEndPoint, double aWidth ) override;
84 
86  virtual void DrawCircle( const VECTOR2D& aCenterPoint, double aRadius ) override;
87 
89  virtual void DrawArc( const VECTOR2D& aCenterPoint, double aRadius,
90  double aStartAngle, double aEndAngle ) override;
91 
93  virtual void DrawArcSegment( const VECTOR2D& aCenterPoint, double aRadius,
94  double aStartAngle, double aEndAngle, double aWidth ) override;
95 
97  virtual void DrawRectangle( const VECTOR2D& aStartPoint, const VECTOR2D& aEndPoint ) override;
98 
100  virtual void DrawPolyline( const std::deque<VECTOR2D>& aPointList ) override { drawPoly( aPointList ); }
101  virtual void DrawPolyline( const VECTOR2D aPointList[], int aListSize ) override { drawPoly( aPointList, aListSize ); }
102  virtual void DrawPolyline( const SHAPE_LINE_CHAIN& aLineChain ) override { drawPoly( aLineChain ); }
103 
105  virtual void DrawPolygon( const std::deque<VECTOR2D>& aPointList ) override { drawPoly( aPointList ); }
106  virtual void DrawPolygon( const VECTOR2D aPointList[], int aListSize ) override { drawPoly( aPointList, aListSize ); }
107  virtual void DrawPolygon( const SHAPE_POLY_SET& aPolySet ) override;
108 
110  virtual void DrawCurve( const VECTOR2D& startPoint, const VECTOR2D& controlPointA,
111  const VECTOR2D& controlPointB, const VECTOR2D& endPoint ) override;
112 
114  virtual void DrawBitmap( const BITMAP_BASE& aBitmap ) override;
115 
116  // --------------
117  // Screen methods
118  // --------------
119 
121  virtual void ResizeScreen( int aWidth, int aHeight ) override;
122 
124  virtual void Flush() override;
125 
127  virtual void ClearScreen() override;
128 
129  // -----------------
130  // Attribute setting
131  // -----------------
132 
134  virtual void SetIsFill( bool aIsFillEnabled ) override;
135 
137  virtual void SetIsStroke( bool aIsStrokeEnabled ) override;
138 
140  virtual void SetStrokeColor( const COLOR4D& aColor ) override;
141 
143  virtual void SetFillColor( const COLOR4D& aColor ) override;
144 
146  virtual void SetLineWidth( float aLineWidth ) override;
147 
149  virtual void SetLayerDepth( double aLayerDepth ) override;
150 
151  // --------------
152  // Transformation
153  // --------------
154 
156  virtual void Transform( const MATRIX3x3D& aTransformation ) override;
157 
159  virtual void Rotate( double aAngle ) override;
160 
162  virtual void Translate( const VECTOR2D& aTranslation ) override;
163 
165  virtual void Scale( const VECTOR2D& aScale ) override;
166 
168  virtual void Save() override;
169 
171  virtual void Restore() override;
172 
173  // --------------------------------------------
174  // Group methods
175  // ---------------------------------------------
176 
178  virtual int BeginGroup() override;
179 
181  virtual void EndGroup() override;
182 
184  virtual void DrawGroup( int aGroupNumber ) override;
185 
187  virtual void ChangeGroupColor( int aGroupNumber, const COLOR4D& aNewColor ) override;
188 
190  virtual void ChangeGroupDepth( int aGroupNumber, int aDepth ) override;
191 
193  virtual void DeleteGroup( int aGroupNumber ) override;
194 
196  virtual void ClearCache() override;
197 
198  // --------------------------------------------------------
199  // Handling the world <-> screen transformation
200  // --------------------------------------------------------
201 
203  virtual void SetNegativeDrawMode( bool aSetting ) override;
204 
205  // -------
206  // Cursor
207  // -------
208 
210  virtual void DrawCursor( const VECTOR2D& aCursorPosition ) override;
211 
212  virtual void EnableDepthTest( bool aEnabled = false ) override;
213 
214 protected:
216  virtual void beginDrawing() override;
217 
219  virtual void endDrawing() override;
220 
221  void resetContext();
222 
223  virtual void drawGridLine( const VECTOR2D& aStartPoint, const VECTOR2D& aEndPoint ) override;
224 
226  typedef GAL super;
227 
229  static const int MAX_CAIRO_ARGUMENTS = 4;
230 
233  {
241  //CMD_TRANSFORM, ///< Transform the actual context
248  };
249 
251  typedef struct
252  {
254  union {
255  double dblArg[MAX_CAIRO_ARGUMENTS];
256  bool boolArg;
257  int intArg;
258  } argument;
259  cairo_path_t* cairoPath;
260  } GROUP_ELEMENT;
261 
262  // Variables for the grouping function
263  bool isGrouping;
265  typedef std::deque<GROUP_ELEMENT> GROUP;
266  std::map<int, GROUP> groups;
267  unsigned int groupCounter;
268  GROUP* currentGroup;
269 
270  cairo_matrix_t cairoWorldScreenMatrix;
271  cairo_t* currentContext;
272  cairo_t* context;
273  cairo_surface_t* surface;
274 
275  void flushPath();
276  void storePath();
277 
281  virtual void blitCursor( wxMemoryDC& clientDC );
282 
284  void drawPoly( const std::deque<VECTOR2D>& aPointList );
285  void drawPoly( const VECTOR2D aPointList[], int aListSize );
286  void drawPoly( const SHAPE_LINE_CHAIN& aLineChain );
287 
293  unsigned int getNewGroupNumber();
294 
296  static constexpr cairo_format_t GAL_FORMAT = CAIRO_FORMAT_RGB24;
297 };
298 
299 
300 class CAIRO_GAL : public CAIRO_GAL_BASE, public wxWindow
301 {
302 public:
318  CAIRO_GAL( GAL_DISPLAY_OPTIONS& aDisplayOptions,
319  wxWindow* aParent, wxEvtHandler* aMouseListener = NULL,
320  wxEvtHandler* aPaintListener = NULL, const wxString& aName = wxT( "CairoCanvas" ) );
321 
322  virtual ~CAIRO_GAL();
323 
325  bool IsVisible() const override
326  {
327  return IsShownOnScreen() && !GetClientRect().IsEmpty();
328  }
329 
330  virtual void ResizeScreen( int aWidth, int aHeight ) override;
331 
332  virtual bool Show( bool aShow ) override;
333 
334  virtual void SaveScreen() override;
335 
336  virtual void RestoreScreen() override;
337 
338  virtual int BeginGroup() override;
339 
340  virtual void EndGroup() override;
341 
342  virtual void SetTarget( RENDER_TARGET aTarget ) override;
343 
344  virtual RENDER_TARGET GetTarget() const override;
345 
346  virtual void ClearTarget( RENDER_TARGET aTarget ) override;
347 
353  void PostPaint()
354  {
355  if( paintListener )
356  {
357  wxPaintEvent redrawEvent;
358  wxPostEvent( paintListener, redrawEvent );
359  }
360  }
361 
362  void SetMouseListener( wxEvtHandler* aMouseListener )
363  {
364  mouseListener = aMouseListener;
365  }
366 
367  void SetPaintListener( wxEvtHandler* aPaintListener )
368  {
369  paintListener = aPaintListener;
370  }
371 
372 protected:
373  // Compositor related variables
374  std::shared_ptr<CAIRO_COMPOSITOR> compositor;
375  unsigned int mainBuffer;
376  unsigned int overlayBuffer;
379 
380  // Variables related to wxWidgets
381  wxWindow* parentWindow;
382  wxEvtHandler* mouseListener;
383  wxEvtHandler* paintListener;
384  unsigned int bufferSize;
385  unsigned char* wxOutput;
386 
387  // Variables related to Cairo <-> wxWidgets
388  unsigned int* bitmapBuffer;
389  unsigned int* bitmapBufferBackup;
390  int stride;
394 
396  virtual void beginDrawing() override;
397 
399  virtual void endDrawing() override;
400 
402  void initSurface();
403 
405  void deinitSurface();
406 
408  void allocateBitmaps();
409 
411  void deleteBitmaps();
412 
414  void setCompositor();
415 
416  // Event handlers
422  void onPaint( wxPaintEvent& aEvent );
423 
429  void skipMouseEvent( wxMouseEvent& aEvent );
430 
432  bool updatedGalDisplayOptions( const GAL_DISPLAY_OPTIONS& aOptions ) override;
433 };
434 
435 } // namespace KIGFX
436 
437 #endif // CAIROGAL_H_
virtual void SetNegativeDrawMode(bool aSetting) override
Sets negative draw mode in the renderer.
Definition: cairo_gal.cpp:717
virtual void DrawPolyline(const SHAPE_LINE_CHAIN &aLineChain) override
Definition: cairo_gal.h:102
virtual void DrawBitmap(const BITMAP_BASE &aBitmap) override
Draw a bitmap image.
Definition: cairo_gal.cpp:275
virtual void ResizeScreen(int aWidth, int aHeight) override
Resizes the canvas.
Definition: cairo_gal.cpp:328
unsigned int groupCounter
Counter used for generating keys for groups.
Definition: cairo_gal.h:267
virtual void DrawPolyline(const std::deque< VECTOR2D > &aPointList) override
Draw a polyline.
Definition: cairo_gal.h:100
virtual void EndGroup() override
End the group.
Definition: cairo_gal.cpp:566
virtual void ChangeGroupColor(int aGroupNumber, const COLOR4D &aNewColor) override
Changes the color used to draw the group.
Definition: cairo_gal.cpp:663
bool boolArg
A bool argument.
Definition: cairo_gal.h:256
Class CAIRO_GAL is the cairo implementation of the graphics abstraction layer.
Definition: class_module.h:57
virtual void SetIsStroke(bool aIsStrokeEnabled) override
Enable/disable stroked outlines.
Definition: cairo_gal.cpp:363
virtual void EnableDepthTest(bool aEnabled=false) override
Definition: cairo_gal.cpp:729
virtual void ChangeGroupDepth(int aGroupNumber, int aDepth) override
Changes the depth (Z-axis position) of the group.
Definition: cairo_gal.cpp:681
cairo_surface_t * surface
Cairo surface.
Definition: cairo_gal.h:273
std::deque< GROUP_ELEMENT > GROUP
A graphic group type definition.
Definition: cairo_gal.h:265
virtual void Translate(const VECTOR2D &aTranslation) override
Translate the context.
Definition: cairo_gal.cpp:478
CAIRO_GAL_BASE(GAL_DISPLAY_OPTIONS &aDisplayOptions)
Definition: cairo_gal.cpp:46
void storePath()
Store the actual path.
Definition: cairo_gal.cpp:792
Type definition for an graphics group element.
Definition: cairo_gal.h:251
Rotate the context.
Definition: cairo_gal.h:242
void PostPaint()
Function PostPaint posts an event to m_paint_listener.
Definition: cairo_gal.h:353
virtual void DeleteGroup(int aGroupNumber) override
Delete the group from the memory.
Definition: cairo_gal.cpp:688
static const int MAX_CAIRO_ARGUMENTS
Maximum number of arguments for one command.
Definition: cairo_gal.h:229
bool isInitialized
Are Cairo image & surface ready to use.
Definition: cairo_gal.h:392
unsigned int overlayBuffer
Handle to the overlay buffer.
Definition: cairo_gal.h:376
bool IsVisible() const override
>
Definition: cairo_gal.h:325
Enable/disable filling.
Definition: cairo_gal.h:234
virtual void SetLayerDepth(double aLayerDepth) override
Set the depth of the layer (position on the z-axis)
Definition: cairo_gal.cpp:437
GAL super
Super class definition.
Definition: cairo_gal.h:226
bool validCompositor
Compositor initialization flag.
Definition: cairo_gal.h:378
GRAPHICS_COMMAND
Definitions for the command recorder.
Definition: cairo_gal.h:232
This class handle bitmap images in KiCad.
Definition: bitmap_base.h:46
virtual void ClearCache() override
Delete all data created during caching of graphic items.
Definition: cairo_gal.cpp:708
virtual void DrawCircle(const VECTOR2D &aCenterPoint, double aRadius) override
Draw a circle using world coordinates.
Definition: cairo_gal.cpp:145
RENDER_TARGET currentTarget
Current rendering target.
Definition: cairo_gal.h:377
unsigned char * wxOutput
wxImage comaptible buffer
Definition: cairo_gal.h:385
virtual void DrawSegment(const VECTOR2D &aStartPoint, const VECTOR2D &aEndPoint, double aWidth) override
Draw a rounded segment.
Definition: cairo_gal.cpp:101
virtual void ClearScreen() override
Clear the screen.
Definition: cairo_gal.cpp:340
virtual void SaveScreen()
Save the screen contents.
virtual void drawGridLine(const VECTOR2D &aStartPoint, const VECTOR2D &aEndPoint) override
Draw a grid line (usually a simplified line function).
Definition: cairo_gal.cpp:761
virtual void DrawLine(const VECTOR2D &aStartPoint, const VECTOR2D &aEndPoint) override
Draw a line.
Definition: cairo_gal.cpp:92
GROUP * currentGroup
Currently used group.
Definition: cairo_gal.h:268
Class SHAPE_POLY_SET.
std::shared_ptr< CAIRO_COMPOSITOR > compositor
Object for layers compositing.
Definition: cairo_gal.h:374
virtual void endDrawing() override
Definition: cairo_gal.cpp:85
void SetPaintListener(wxEvtHandler *aPaintListener)
Definition: cairo_gal.h:367
virtual void ClearTarget(RENDER_TARGET aTarget)
Clears the target for rendering.
std::map< int, GROUP > groups
List of graphic groups.
Definition: cairo_gal.h:266
virtual void DrawCurve(const VECTOR2D &startPoint, const VECTOR2D &controlPointA, const VECTOR2D &controlPointB, const VECTOR2D &endPoint) override
Draw a cubic bezier spline.
Definition: cairo_gal.cpp:262
virtual void DrawArcSegment(const VECTOR2D &aCenterPoint, double aRadius, double aStartAngle, double aEndAngle, double aWidth) override
Draw an arc segment.
Definition: cairo_gal.cpp:190
cairo_t * context
Cairo image.
Definition: cairo_gal.h:272
bool isGrouping
Is grouping enabled ?
Definition: cairo_gal.h:263
COLOR4D backgroundColor
Background color.
Definition: cairo_gal.h:393
virtual void DrawCursor(const VECTOR2D &aCursorPosition) override
Draw the cursor.
Definition: cairo_gal.cpp:723
wxEvtHandler * paintListener
Paint listener.
Definition: cairo_gal.h:383
cairo_path_t * cairoPath
Pointer to a Cairo path.
Definition: cairo_gal.h:259
unsigned int * bitmapBuffer
Storage of the cairo image.
Definition: cairo_gal.h:388
virtual ~CAIRO_GAL_BASE()
Definition: cairo_gal.cpp:67
Save the transformation matrix.
Definition: cairo_gal.h:245
virtual void SetIsFill(bool aIsFillEnabled) override
Enable/disable fill.
Definition: cairo_gal.cpp:348
virtual bool updatedGalDisplayOptions(const GAL_DISPLAY_OPTIONS &aOptions)
Function updatedGalDisplayOptions.
virtual bool Show(bool aShow)
Shows/hides the GAL canvas.
virtual void DrawArc(const VECTOR2D &aCenterPoint, double aRadius, double aStartAngle, double aEndAngle) override
Draw an arc.
Definition: cairo_gal.cpp:154
int stride
Stride value for Cairo.
Definition: cairo_gal.h:390
virtual void SetStrokeColor(const COLOR4D &aColor) override
Set the stroke color.
Definition: cairo_gal.cpp:378
virtual void DrawRectangle(const VECTOR2D &aStartPoint, const VECTOR2D &aEndPoint) override
Draw a rectangle.
Definition: cairo_gal.cpp:237
virtual void Restore() override
Restore the context.
Definition: cairo_gal.cpp:533
unsigned int * bitmapBufferBackup
Backup storage of the cairo image.
Definition: cairo_gal.h:389
virtual int BeginGroup() override
Begin a group.
Definition: cairo_gal.cpp:550
virtual void RestoreScreen()
Restore the screen contents.
Class SHAPE_LINE_CHAIN.
GRAPHICS_COMMAND command
Command to execute.
Definition: cairo_gal.h:253
void drawPoly(const std::deque< VECTOR2D > &aPointList)
Drawing polygons & polylines is the same in cairo, so here is the common code.
Definition: cairo_gal.cpp:858
cairo_matrix_t cairoWorldScreenMatrix
Cairo world to screen transformation matrix.
Definition: cairo_gal.h:270
virtual void DrawPolyline(const VECTOR2D aPointList[], int aListSize) override
Definition: cairo_gal.h:101
Translate the context.
Definition: cairo_gal.h:243
virtual void Save() override
Save the context.
Definition: cairo_gal.cpp:516
virtual void beginDrawing() override
Definition: cairo_gal.cpp:79
void SetMouseListener(wxEvtHandler *aMouseListener)
Definition: cairo_gal.h:362
virtual void SetLineWidth(float aLineWidth) override
Set the line width.
Definition: cairo_gal.cpp:414
virtual void Scale(const VECTOR2D &aScale) override
Scale the context.
Definition: cairo_gal.cpp:497
cairo_t * currentContext
Currently used Cairo context for drawing.
Definition: cairo_gal.h:271
virtual void DrawGroup(int aGroupNumber) override
Draw the stored group.
Definition: cairo_gal.cpp:573
unsigned int bufferSize
Size of buffers cairoOutput, bitmapBuffers.
Definition: cairo_gal.h:384
RENDER_TARGET
RENDER_TARGET: Possible rendering targets.
Definition: definitions.h:39
bool isElementAdded
Was an graphic element added ?
Definition: cairo_gal.h:264
virtual void DrawPolygon(const std::deque< VECTOR2D > &aPointList) override
Draw a polygon.
Definition: cairo_gal.h:105
virtual RENDER_TARGET GetTarget() const
Gets the currently used target for rendering.
wxEvtHandler * mouseListener
Mouse listener.
Definition: cairo_gal.h:382
virtual void Transform(const MATRIX3x3D &aTransformation) override
Transform the context.
Definition: cairo_gal.cpp:444
unsigned int mainBuffer
Handle to the main buffer.
Definition: cairo_gal.h:375
wxWindow * parentWindow
Parent window.
Definition: cairo_gal.h:381
virtual void SetTarget(RENDER_TARGET aTarget)
Sets the target for rendering.
virtual void Flush() override
Force all remaining objects to be drawn.
Definition: cairo_gal.cpp:334
virtual void DrawPolygon(const VECTOR2D aPointList[], int aListSize) override
Definition: cairo_gal.h:106
Enable/disable stroking.
Definition: cairo_gal.h:235
unsigned int getNewGroupNumber()
Returns a valid key that can be used as a new group number.
Definition: cairo_gal.cpp:917
virtual void blitCursor(wxMemoryDC &clientDC)
Blits cursor into the current screen.
Definition: cairo_gal.cpp:840
Restore the transformation matrix.
Definition: cairo_gal.h:246
virtual void SetFillColor(const COLOR4D &aColor) override
Set the fill color.
Definition: cairo_gal.cpp:396
Class GAL is the abstract interface for drawing on a 2D-surface.
virtual void Rotate(double aAngle) override
Rotate the context.
Definition: cairo_gal.cpp:460
static constexpr cairo_format_t GAL_FORMAT
Format used to store pixels.
Definition: cairo_gal.h:296
Class COLOR4D is the color representation with 4 components: red, green, blue, alpha.
Definition: color4d.h:39