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  *
7  * CairoGal - Graphics Abstraction Layer for Cairo
8  *
9  * This program is free software; you can redistribute it and/or
10  * modify it under the terms of the GNU General Public License
11  * as published by the Free Software Foundation; either version 2
12  * of the License, or (at your option) any later version.
13  *
14  * This program is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17  * GNU General Public License for more details.
18  *
19  * You should have received a copy of the GNU General Public License
20  * along with this program; if not, you may find one here:
21  * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
22  * or you may search the http://www.gnu.org website for the version 2 license,
23  * or you may write to the Free Software Foundation, Inc.,
24  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
25  */
26 
27 #ifndef CAIROGAL_H_
28 #define CAIROGAL_H_
29 
30 #include <map>
31 #include <iterator>
32 
33 #include <cairo.h>
34 
36 #include <wx/dcbuffer.h>
37 
38 #include <memory>
39 
40 #if defined(__WXMSW__)
41 #define SCREEN_DEPTH 24
42 #else
43 #if wxCHECK_VERSION( 2, 9, 0 )
44 #define SCREEN_DEPTH wxBITMAP_SCREEN_DEPTH
45 #else
46 #define SCREEN_DEPTH 32
47 #endif
48 #endif
49 
62 namespace KIGFX
63 {
64 class CAIRO_COMPOSITOR;
65 
66 class CAIRO_GAL : public GAL, public wxWindow
67 {
68 public:
84  CAIRO_GAL( GAL_DISPLAY_OPTIONS& aDisplayOptions,
85  wxWindow* aParent, wxEvtHandler* aMouseListener = NULL,
86  wxEvtHandler* aPaintListener = NULL, const wxString& aName = wxT( "CairoCanvas" ) );
87 
88  virtual ~CAIRO_GAL();
89 
91  bool IsVisible() const override {
92  return IsShownOnScreen();
93  }
94 
95  // ---------------
96  // Drawing methods
97  // ---------------
98 
100  virtual void BeginDrawing() override;
101 
103  virtual void EndDrawing() override;
104 
106  virtual void DrawLine( const VECTOR2D& aStartPoint, const VECTOR2D& aEndPoint ) override;
107 
109  virtual void DrawSegment( const VECTOR2D& aStartPoint, const VECTOR2D& aEndPoint, double aWidth ) override;
110 
112  virtual void DrawCircle( const VECTOR2D& aCenterPoint, double aRadius ) override;
113 
115  virtual void DrawArc( const VECTOR2D& aCenterPoint, double aRadius,
116  double aStartAngle, double aEndAngle ) override;
117 
119  virtual void DrawArcSegment( const VECTOR2D& aCenterPoint, double aRadius,
120  double aStartAngle, double aEndAngle, double aWidth ) override;
121 
123  virtual void DrawRectangle( const VECTOR2D& aStartPoint, const VECTOR2D& aEndPoint ) override;
124 
126  virtual void DrawPolyline( const std::deque<VECTOR2D>& aPointList ) override { drawPoly( aPointList ); }
127  virtual void DrawPolyline( const VECTOR2D aPointList[], int aListSize ) override { drawPoly( aPointList, aListSize ); }
128  virtual void DrawPolyline( const SHAPE_LINE_CHAIN& aLineChain ) override { drawPoly( aLineChain ); }
129 
131  virtual void DrawPolygon( const std::deque<VECTOR2D>& aPointList ) override { drawPoly( aPointList ); }
132  virtual void DrawPolygon( const VECTOR2D aPointList[], int aListSize ) override { drawPoly( aPointList, aListSize ); }
133  virtual void DrawPolygon( const SHAPE_POLY_SET& aPolySet ) override;
134 
136  virtual void DrawCurve( const VECTOR2D& startPoint, const VECTOR2D& controlPointA,
137  const VECTOR2D& controlPointB, const VECTOR2D& endPoint ) override;
138 
139  // --------------
140  // Screen methods
141  // --------------
142 
144  virtual void ResizeScreen( int aWidth, int aHeight ) override;
145 
147  virtual bool Show( bool aShow ) override;
148 
150  virtual void Flush() override;
151 
153  virtual void ClearScreen( ) override;
154 
155  // -----------------
156  // Attribute setting
157  // -----------------
158 
160  virtual void SetIsFill( bool aIsFillEnabled ) override;
161 
163  virtual void SetIsStroke( bool aIsStrokeEnabled ) override;
164 
166  virtual void SetStrokeColor( const COLOR4D& aColor ) override;
167 
169  virtual void SetFillColor( const COLOR4D& aColor ) override;
170 
172  virtual void SetLineWidth( double aLineWidth ) override;
173 
175  virtual void SetLayerDepth( double aLayerDepth ) override;
176 
177  // --------------
178  // Transformation
179  // --------------
180 
182  virtual void Transform( const MATRIX3x3D& aTransformation ) override;
183 
185  virtual void Rotate( double aAngle ) override;
186 
188  virtual void Translate( const VECTOR2D& aTranslation ) override;
189 
191  virtual void Scale( const VECTOR2D& aScale ) override;
192 
194  virtual void Save() override;
195 
197  virtual void Restore() override;
198 
199  // --------------------------------------------
200  // Group methods
201  // ---------------------------------------------
202 
204  virtual int BeginGroup() override;
205 
207  virtual void EndGroup() override;
208 
210  virtual void DrawGroup( int aGroupNumber ) override;
211 
213  virtual void ChangeGroupColor( int aGroupNumber, const COLOR4D& aNewColor ) override;
214 
216  virtual void ChangeGroupDepth( int aGroupNumber, int aDepth ) override;
217 
219  virtual void DeleteGroup( int aGroupNumber ) override;
220 
222  virtual void ClearCache() override;
223 
224  // --------------------------------------------------------
225  // Handling the world <-> screen transformation
226  // --------------------------------------------------------
227 
229  virtual void SaveScreen() override;
230 
232  virtual void RestoreScreen() override;
233 
235  virtual void SetTarget( RENDER_TARGET aTarget ) override;
236 
238  virtual RENDER_TARGET GetTarget() const override;
239 
241  virtual void ClearTarget( RENDER_TARGET aTarget ) override;
242 
244  virtual void SetNegativeDrawMode( bool aSetting ) override;
245 
246  // -------
247  // Cursor
248  // -------
249 
251  virtual void DrawCursor( const VECTOR2D& aCursorPosition ) override;
252 
258  void PostPaint()
259  {
260  if( paintListener )
261  {
262  wxPaintEvent redrawEvent;
263  wxPostEvent( paintListener, redrawEvent );
264  }
265  }
266 
267  void SetMouseListener( wxEvtHandler* aMouseListener )
268  {
269  mouseListener = aMouseListener;
270  }
271 
272  void SetPaintListener( wxEvtHandler* aPaintListener )
273  {
274  paintListener = aPaintListener;
275  }
276 
277 protected:
278  virtual void drawGridLine( const VECTOR2D& aStartPoint, const VECTOR2D& aEndPoint ) override;
279 
280 private:
282  typedef GAL super;
283 
284  // Compositing variables
285  std::shared_ptr<CAIRO_COMPOSITOR> compositor;
286  unsigned int mainBuffer;
287  unsigned int overlayBuffer;
290 
291  // Variables related to wxWidgets
292  wxWindow* parentWindow;
293  wxEvtHandler* mouseListener;
294  wxEvtHandler* paintListener;
295  unsigned int bufferSize;
296  unsigned char* wxOutput;
297 
299  static const int MAX_CAIRO_ARGUMENTS = 4;
300 
303  {
311  //CMD_TRANSFORM, ///< Transform the actual context
318  };
319 
321  typedef struct
322  {
324  union {
325  double dblArg[MAX_CAIRO_ARGUMENTS];
326  bool boolArg;
327  int intArg;
328  } argument;
329  cairo_path_t* cairoPath;
330  } GROUP_ELEMENT;
331 
332  // Variables for the grouping function
333  bool isGrouping;
335  typedef std::deque<GROUP_ELEMENT> GROUP;
336  std::map<int, GROUP> groups;
337  unsigned int groupCounter;
338  GROUP* currentGroup;
339 
340  // Variables related to Cairo <-> wxWidgets
341  cairo_matrix_t cairoWorldScreenMatrix;
342  cairo_t* currentContext;
343  cairo_t* context;
344  cairo_surface_t* surface;
345  unsigned int* bitmapBuffer;
346  unsigned int* bitmapBufferBackup;
347  int stride;
350 
352 
354  bool updatedGalDisplayOptions( const GAL_DISPLAY_OPTIONS& aOptions ) override;
355 
356  void flushPath();
357  // Methods
358  void storePath();
359 
360  // Event handlers
366  void onPaint( wxPaintEvent& aEvent );
367 
373  void skipMouseEvent( wxMouseEvent& aEvent );
374 
378  virtual void blitCursor( wxMemoryDC& clientDC );
379 
381  void initSurface();
382 
384  void deinitSurface();
385 
387  void allocateBitmaps();
388 
390  void deleteBitmaps();
391 
393  void setCompositor();
394 
396  void drawPoly( const std::deque<VECTOR2D>& aPointList );
397  void drawPoly( const VECTOR2D aPointList[], int aListSize );
398  void drawPoly( const SHAPE_LINE_CHAIN& aLineChain );
399 
405  unsigned int getNewGroupNumber();
406 
408  static const cairo_format_t GAL_FORMAT = CAIRO_FORMAT_RGB24;
409 
411  static const float LAYER_ALPHA;
412 };
413 } // namespace KIGFX
414 
415 #endif // CAIROGAL_H_
unsigned int groupCounter
Counter used for generating keys for groups.
Definition: cairo_gal.h:337
virtual void DeleteGroup(int aGroupNumber) override
Delete the group from the memory.
Definition: cairo_gal.cpp:740
void initSurface()
Prepare Cairo surfaces for drawing.
Definition: cairo_gal.cpp:1006
virtual void SetLayerDepth(double aLayerDepth) override
Set the depth of the layer (position on the z-axis)
Definition: cairo_gal.cpp:483
int intArg
An int argument.
Definition: cairo_gal.h:327
virtual void EndDrawing() override
End the drawing, needs to be called for every new frame.
Definition: cairo_gal.cpp:140
bool updatedGalDisplayOptions(const GAL_DISPLAY_OPTIONS &aOptions) override
Cairo-specific update handlers
Definition: cairo_gal.cpp:114
Class CAIRO_GAL is the cairo implementation of the graphics abstraction layer.
Definition: class_module.h:58
CAIRO_GAL(GAL_DISPLAY_OPTIONS &aDisplayOptions, wxWindow *aParent, wxEvtHandler *aMouseListener=NULL, wxEvtHandler *aPaintListener=NULL, const wxString &aName=wxT("CairoCanvas"))
Constructor CAIRO_GAL.
Definition: cairo_gal.cpp:45
virtual void ResizeScreen(int aWidth, int aHeight) override
Resizes the canvas.
Definition: cairo_gal.cpp:350
void deinitSurface()
Destroy Cairo surfaces when are not needed anymore.
Definition: cairo_gal.cpp:1050
Scale the context.
Definition: cairo_gal.h:314
void onPaint(wxPaintEvent &aEvent)
Paint event handler.
Definition: cairo_gal.cpp:951
cairo_path_t * cairoPath
Pointer to a Cairo path.
Definition: cairo_gal.h:329
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:1077
static const float LAYER_ALPHA
Opacity of a single layer
Definition: cairo_gal.h:411
virtual ~CAIRO_GAL()
Definition: cairo_gal.cpp:105
bool boolArg
A bool argument.
Definition: cairo_gal.h:326
virtual void SetIsFill(bool aIsFillEnabled) override
Enable/disable fill.
Definition: cairo_gal.cpp:393
virtual void DrawArc(const VECTOR2D &aCenterPoint, double aRadius, double aStartAngle, double aEndAngle) override
Draw an arc.
Definition: cairo_gal.cpp:238
void setCompositor()
Prepare the compositor.
Definition: cairo_gal.cpp:1063
virtual void DrawRectangle(const VECTOR2D &aStartPoint, const VECTOR2D &aEndPoint) override
Draw a rectangle.
Definition: cairo_gal.cpp:312
virtual void SetStrokeColor(const COLOR4D &aColor) override
Set the stroke color.
Definition: cairo_gal.cpp:423
static const int MAX_CAIRO_ARGUMENTS
Maximum number of arguments for one command.
Definition: cairo_gal.h:299
void PostPaint()
Function PostPaint posts an event to m_paint_listener.
Definition: cairo_gal.h:258
virtual void BeginDrawing() override
Begin the drawing, needs to be called for every new frame.
Definition: cairo_gal.cpp:128
Set the stroke path.
Definition: cairo_gal.h:309
virtual void Flush() override
Force all remaining objects to be drawn.
Definition: cairo_gal.cpp:378
bool isInitialized
Are Cairo image & surface ready to use.
Definition: cairo_gal.h:348
virtual void Restore() override
Restore the context.
Definition: cairo_gal.cpp:581
virtual void RestoreScreen() override
Restore the screen contents.
Definition: cairo_gal.cpp:785
unsigned int overlayBuffer
Handle to the overlay buffer.
Definition: cairo_gal.h:287
bool IsVisible() const override
>
Definition: cairo_gal.h:91
virtual void Translate(const VECTOR2D &aTranslation) override
Translate the context.
Definition: cairo_gal.cpp:526
virtual void DrawCircle(const VECTOR2D &aCenterPoint, double aRadius) override
Draw a circle using world coordinates.
Definition: cairo_gal.cpp:229
virtual void DrawPolyline(const VECTOR2D aPointList[], int aListSize) override
Definition: cairo_gal.h:127
GRAPHICS_COMMAND
Definitions for the command recorder.
Definition: cairo_gal.h:302
bool validCompositor
Compositor initialization flag.
Definition: cairo_gal.h:289
GROUP * currentGroup
Currently used group.
Definition: cairo_gal.h:338
virtual void SetFillColor(const COLOR4D &aColor) override
Set the fill color.
Definition: cairo_gal.cpp:441
Translate the context.
Definition: cairo_gal.h:313
virtual void DrawSegment(const VECTOR2D &aStartPoint, const VECTOR2D &aEndPoint, double aWidth) override
Draw a rounded segment.
Definition: cairo_gal.cpp:185
RENDER_TARGET currentTarget
Current rendering target.
Definition: cairo_gal.h:288
unsigned char * wxOutput
wxImage comaptible buffer
Definition: cairo_gal.h:296
unsigned int getNewGroupNumber()
Returns a valid key that can be used as a new group number.
Definition: cairo_gal.cpp:1136
virtual void ClearScreen() override
Clear the screen.
Definition: cairo_gal.cpp:384
virtual void ClearTarget(RENDER_TARGET aTarget) override
Clears the target for rendering.
Definition: cairo_gal.cpp:834
bool isGrouping
Is grouping enabled ?
Definition: cairo_gal.h:333
Class SHAPE_POLY_SET.
std::shared_ptr< CAIRO_COMPOSITOR > compositor
Object for layers compositing.
Definition: cairo_gal.h:285
void SetPaintListener(wxEvtHandler *aPaintListener)
Definition: cairo_gal.h:272
std::map< int, GROUP > groups
List of graphic groups.
Definition: cairo_gal.h:336
Enable/disable stroking.
Definition: cairo_gal.h:305
virtual void DrawArcSegment(const VECTOR2D &aCenterPoint, double aRadius, double aStartAngle, double aEndAngle, double aWidth) override
Draw an arc segment.
Definition: cairo_gal.cpp:265
GAL super
Super class definition.
Definition: cairo_gal.h:282
void storePath()
Store the actual path.
Definition: cairo_gal.cpp:903
virtual void Transform(const MATRIX3x3D &aTransformation) override
Transform the context.
Definition: cairo_gal.cpp:492
virtual void Scale(const VECTOR2D &aScale) override
Scale the context.
Definition: cairo_gal.cpp:545
virtual void ChangeGroupColor(int aGroupNumber, const COLOR4D &aNewColor) override
Changes the color used to draw the group.
Definition: cairo_gal.cpp:715
virtual void Rotate(double aAngle) override
Rotate the context.
Definition: cairo_gal.cpp:508
Type definition for an graphics group element.
Definition: cairo_gal.h:321
GRAPHICS_COMMAND command
Command to execute.
Definition: cairo_gal.h:323
Restore the transformation matrix.
Definition: cairo_gal.h:316
virtual void ClearCache() override
Delete all data created during caching of graphic items.
Definition: cairo_gal.cpp:760
cairo_surface_t * surface
Cairo surface.
Definition: cairo_gal.h:344
virtual void SetIsStroke(bool aIsStrokeEnabled) override
Enable/disable stroked outlines.
Definition: cairo_gal.cpp:408
COLOR4D backgroundColor
Background color.
Definition: cairo_gal.h:349
cairo_t * context
Cairo image.
Definition: cairo_gal.h:343
bool isElementAdded
Was an graphic element added ?
Definition: cairo_gal.h:334
virtual void SetLineWidth(double aLineWidth) override
Set the line width.
Definition: cairo_gal.cpp:459
wxEvtHandler * paintListener
Paint listener.
Definition: cairo_gal.h:294
virtual void DrawPolygon(const VECTOR2D aPointList[], int aListSize) override
Definition: cairo_gal.h:132
virtual void SetTarget(RENDER_TARGET aTarget) override
Sets the target for rendering.
Definition: cairo_gal.cpp:800
unsigned int * bitmapBuffer
Storage of the cairo image.
Definition: cairo_gal.h:345
void skipMouseEvent(wxMouseEvent &aEvent)
Mouse event handler, forwards the event to the child.
Definition: cairo_gal.cpp:957
static const cairo_format_t GAL_FORMAT
Format used to store pixels.
Definition: cairo_gal.h:408
Rotate the context.
Definition: cairo_gal.h:312
virtual void DrawCursor(const VECTOR2D &aCursorPosition) override
Draw the cursor.
Definition: cairo_gal.cpp:866
int stride
Stride value for Cairo.
Definition: cairo_gal.h:347
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:337
cairo_matrix_t cairoWorldScreenMatrix
Cairo world to screen transformation matrix.
Definition: cairo_gal.h:341
virtual bool Show(bool aShow) override
Shows/hides the GAL canvas.
Definition: cairo_gal.cpp:367
unsigned int * bitmapBufferBackup
Backup storage of the cairo image.
Definition: cairo_gal.h:346
Enable/disable filling.
Definition: cairo_gal.h:304
virtual RENDER_TARGET GetTarget() const override
Gets the currently used target for rendering.
Definition: cairo_gal.cpp:828
virtual void SaveScreen() override
Save the screen contents.
Definition: cairo_gal.cpp:769
Class SHAPE_LINE_CHAIN.
virtual void ChangeGroupDepth(int aGroupNumber, int aDepth) override
Changes the depth (Z-axis position) of the group.
Definition: cairo_gal.cpp:733
virtual void SetNegativeDrawMode(bool aSetting) override
Sets negative draw mode in the renderer.
Definition: cairo_gal.cpp:860
virtual void DrawPolyline(const std::deque< VECTOR2D > &aPointList) override
Draw a polyline.
Definition: cairo_gal.h:126
void SetMouseListener(wxEvtHandler *aMouseListener)
Definition: cairo_gal.h:267
virtual void DrawGroup(int aGroupNumber) override
Draw the stored group.
Definition: cairo_gal.cpp:625
cairo_t * currentContext
Currently used Cairo context for drawing.
Definition: cairo_gal.h:342
virtual void DrawLine(const VECTOR2D &aStartPoint, const VECTOR2D &aEndPoint) override
Draw a line.
Definition: cairo_gal.cpp:176
virtual void drawGridLine(const VECTOR2D &aStartPoint, const VECTOR2D &aEndPoint) override
Draw a grid line (usually a simplified line function).
Definition: cairo_gal.cpp:872
void deleteBitmaps()
Allocate the bitmaps for drawing.
Definition: cairo_gal.cpp:998
unsigned int bufferSize
Size of buffers cairoOutput, bitmapBuffers.
Definition: cairo_gal.h:295
virtual void DrawPolygon(const std::deque< VECTOR2D > &aPointList) override
Draw a polygon.
Definition: cairo_gal.h:131
virtual int BeginGroup() override
Begin a group.
Definition: cairo_gal.cpp:598
RENDER_TARGET
RENDER_TARGET: Possible rendering targets.
Definition: definitions.h:39
std::deque< GROUP_ELEMENT > GROUP
A graphic group type definition.
Definition: cairo_gal.h:335
Set the fill path.
Definition: cairo_gal.h:310
wxEvtHandler * mouseListener
Mouse listener.
Definition: cairo_gal.h:293
void allocateBitmaps()
Allocate the bitmaps for drawing.
Definition: cairo_gal.cpp:983
virtual void blitCursor(wxMemoryDC &clientDC)
Blits cursor into the current screen.
Definition: cairo_gal.cpp:965
unsigned int mainBuffer
Handle to the main buffer.
Definition: cairo_gal.h:286
virtual void DrawPolyline(const SHAPE_LINE_CHAIN &aLineChain) override
Definition: cairo_gal.h:128
wxWindow * parentWindow
Parent window.
Definition: cairo_gal.h:292
virtual void EndGroup() override
End the group.
Definition: cairo_gal.cpp:616
Save the transformation matrix.
Definition: cairo_gal.h:315
Class GAL is the abstract interface for drawing on a 2D-surface.
Class COLOR4D is the color representation with 4 components: red, green, blue, alpha.
Definition: color4d.h:39
virtual void Save() override
Save the context.
Definition: cairo_gal.cpp:564