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 
140  virtual void DrawBitmap( const BITMAP_BASE& aBitmap ) override;
141 
142  // --------------
143  // Screen methods
144  // --------------
145 
147  virtual void ResizeScreen( int aWidth, int aHeight ) override;
148 
150  virtual bool Show( bool aShow ) override;
151 
153  virtual void Flush() override;
154 
156  virtual void ClearScreen( ) override;
157 
158  // -----------------
159  // Attribute setting
160  // -----------------
161 
163  virtual void SetIsFill( bool aIsFillEnabled ) override;
164 
166  virtual void SetIsStroke( bool aIsStrokeEnabled ) override;
167 
169  virtual void SetStrokeColor( const COLOR4D& aColor ) override;
170 
172  virtual void SetFillColor( const COLOR4D& aColor ) override;
173 
175  virtual void SetLineWidth( double aLineWidth ) override;
176 
178  virtual void SetLayerDepth( double aLayerDepth ) override;
179 
180  // --------------
181  // Transformation
182  // --------------
183 
185  virtual void Transform( const MATRIX3x3D& aTransformation ) override;
186 
188  virtual void Rotate( double aAngle ) override;
189 
191  virtual void Translate( const VECTOR2D& aTranslation ) override;
192 
194  virtual void Scale( const VECTOR2D& aScale ) override;
195 
197  virtual void Save() override;
198 
200  virtual void Restore() override;
201 
202  // --------------------------------------------
203  // Group methods
204  // ---------------------------------------------
205 
207  virtual int BeginGroup() override;
208 
210  virtual void EndGroup() override;
211 
213  virtual void DrawGroup( int aGroupNumber ) override;
214 
216  virtual void ChangeGroupColor( int aGroupNumber, const COLOR4D& aNewColor ) override;
217 
219  virtual void ChangeGroupDepth( int aGroupNumber, int aDepth ) override;
220 
222  virtual void DeleteGroup( int aGroupNumber ) override;
223 
225  virtual void ClearCache() override;
226 
227  // --------------------------------------------------------
228  // Handling the world <-> screen transformation
229  // --------------------------------------------------------
230 
232  virtual void SaveScreen() override;
233 
235  virtual void RestoreScreen() override;
236 
238  virtual void SetTarget( RENDER_TARGET aTarget ) override;
239 
241  virtual RENDER_TARGET GetTarget() const override;
242 
244  virtual void ClearTarget( RENDER_TARGET aTarget ) override;
245 
247  virtual void SetNegativeDrawMode( bool aSetting ) override;
248 
249  // -------
250  // Cursor
251  // -------
252 
254  virtual void DrawCursor( const VECTOR2D& aCursorPosition ) override;
255 
261  void PostPaint()
262  {
263  if( paintListener )
264  {
265  wxPaintEvent redrawEvent;
266  wxPostEvent( paintListener, redrawEvent );
267  }
268  }
269 
270  void SetMouseListener( wxEvtHandler* aMouseListener )
271  {
272  mouseListener = aMouseListener;
273  }
274 
275  void SetPaintListener( wxEvtHandler* aPaintListener )
276  {
277  paintListener = aPaintListener;
278  }
279 
280 protected:
281  virtual void drawGridLine( const VECTOR2D& aStartPoint, const VECTOR2D& aEndPoint ) override;
282 
283 private:
285  typedef GAL super;
286 
287  // Compositing variables
288  std::shared_ptr<CAIRO_COMPOSITOR> compositor;
289  unsigned int mainBuffer;
290  unsigned int overlayBuffer;
293 
294  // Variables related to wxWidgets
295  wxWindow* parentWindow;
296  wxEvtHandler* mouseListener;
297  wxEvtHandler* paintListener;
298  unsigned int bufferSize;
299  unsigned char* wxOutput;
300 
302  static const int MAX_CAIRO_ARGUMENTS = 4;
303 
306  {
314  //CMD_TRANSFORM, ///< Transform the actual context
321  };
322 
324  typedef struct
325  {
327  union {
328  double dblArg[MAX_CAIRO_ARGUMENTS];
329  bool boolArg;
330  int intArg;
331  } argument;
332  cairo_path_t* cairoPath;
333  } GROUP_ELEMENT;
334 
335  // Variables for the grouping function
336  bool isGrouping;
338  typedef std::deque<GROUP_ELEMENT> GROUP;
339  std::map<int, GROUP> groups;
340  unsigned int groupCounter;
341  GROUP* currentGroup;
342 
343  // Variables related to Cairo <-> wxWidgets
344  cairo_matrix_t cairoWorldScreenMatrix;
345  cairo_t* currentContext;
346  cairo_t* context;
347  cairo_surface_t* surface;
348  unsigned int* bitmapBuffer;
349  unsigned int* bitmapBufferBackup;
350  int stride;
353 
355 
357  bool updatedGalDisplayOptions( const GAL_DISPLAY_OPTIONS& aOptions ) override;
358 
359  void flushPath();
360  // Methods
361  void storePath();
362 
363  // Event handlers
369  void onPaint( wxPaintEvent& aEvent );
370 
376  void skipMouseEvent( wxMouseEvent& aEvent );
377 
381  virtual void blitCursor( wxMemoryDC& clientDC );
382 
384  void initSurface();
385 
387  void deinitSurface();
388 
390  void allocateBitmaps();
391 
393  void deleteBitmaps();
394 
396  void setCompositor();
397 
399  void drawPoly( const std::deque<VECTOR2D>& aPointList );
400  void drawPoly( const VECTOR2D aPointList[], int aListSize );
401  void drawPoly( const SHAPE_LINE_CHAIN& aLineChain );
402 
408  unsigned int getNewGroupNumber();
409 
411  static const cairo_format_t GAL_FORMAT = CAIRO_FORMAT_RGB24;
412 
414  static const float LAYER_ALPHA;
415 };
416 } // namespace KIGFX
417 
418 #endif // CAIROGAL_H_
unsigned int groupCounter
Counter used for generating keys for groups.
Definition: cairo_gal.h:340
virtual void DeleteGroup(int aGroupNumber) override
Delete the group from the memory.
Definition: cairo_gal.cpp:796
void initSurface()
Prepare Cairo surfaces for drawing.
Definition: cairo_gal.cpp:1062
virtual void SetLayerDepth(double aLayerDepth) override
Set the depth of the layer (position on the z-axis)
Definition: cairo_gal.cpp:539
int intArg
An int argument.
Definition: cairo_gal.h:330
virtual void EndDrawing() override
End the drawing, needs to be called for every new frame.
Definition: cairo_gal.cpp:141
bool updatedGalDisplayOptions(const GAL_DISPLAY_OPTIONS &aOptions) override
Cairo-specific update handlers
Definition: cairo_gal.cpp:115
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:46
virtual void ResizeScreen(int aWidth, int aHeight) override
Resizes the canvas.
Definition: cairo_gal.cpp:406
void deinitSurface()
Destroy Cairo surfaces when are not needed anymore.
Definition: cairo_gal.cpp:1106
Scale the context.
Definition: cairo_gal.h:317
void onPaint(wxPaintEvent &aEvent)
Paint event handler.
Definition: cairo_gal.cpp:1007
cairo_path_t * cairoPath
Pointer to a Cairo path.
Definition: cairo_gal.h:332
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:1133
static const float LAYER_ALPHA
Opacity of a single layer
Definition: cairo_gal.h:414
virtual ~CAIRO_GAL()
Definition: cairo_gal.cpp:106
bool boolArg
A bool argument.
Definition: cairo_gal.h:329
virtual void SetIsFill(bool aIsFillEnabled) override
Enable/disable fill.
Definition: cairo_gal.cpp:449
virtual void DrawArc(const VECTOR2D &aCenterPoint, double aRadius, double aStartAngle, double aEndAngle) override
Draw an arc.
Definition: cairo_gal.cpp:239
void setCompositor()
Prepare the compositor.
Definition: cairo_gal.cpp:1119
virtual void DrawRectangle(const VECTOR2D &aStartPoint, const VECTOR2D &aEndPoint) override
Draw a rectangle.
Definition: cairo_gal.cpp:313
virtual void SetStrokeColor(const COLOR4D &aColor) override
Set the stroke color.
Definition: cairo_gal.cpp:479
static const int MAX_CAIRO_ARGUMENTS
Maximum number of arguments for one command.
Definition: cairo_gal.h:302
void PostPaint()
Function PostPaint posts an event to m_paint_listener.
Definition: cairo_gal.h:261
virtual void BeginDrawing() override
Begin the drawing, needs to be called for every new frame.
Definition: cairo_gal.cpp:129
Set the stroke path.
Definition: cairo_gal.h:312
virtual void Flush() override
Force all remaining objects to be drawn.
Definition: cairo_gal.cpp:434
bool isInitialized
Are Cairo image & surface ready to use.
Definition: cairo_gal.h:351
virtual void Restore() override
Restore the context.
Definition: cairo_gal.cpp:637
virtual void RestoreScreen() override
Restore the screen contents.
Definition: cairo_gal.cpp:841
unsigned int overlayBuffer
Handle to the overlay buffer.
Definition: cairo_gal.h:290
bool IsVisible() const override
>
Definition: cairo_gal.h:91
virtual void Translate(const VECTOR2D &aTranslation) override
Translate the context.
Definition: cairo_gal.cpp:582
virtual void DrawCircle(const VECTOR2D &aCenterPoint, double aRadius) override
Draw a circle using world coordinates.
Definition: cairo_gal.cpp:230
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:305
bool validCompositor
Compositor initialization flag.
Definition: cairo_gal.h:292
GROUP * currentGroup
Currently used group.
Definition: cairo_gal.h:341
virtual void SetFillColor(const COLOR4D &aColor) override
Set the fill color.
Definition: cairo_gal.cpp:497
This class handle bitmap images in KiCad.
Definition: bitmap_base.h:44
Translate the context.
Definition: cairo_gal.h:316
virtual void DrawSegment(const VECTOR2D &aStartPoint, const VECTOR2D &aEndPoint, double aWidth) override
Draw a rounded segment.
Definition: cairo_gal.cpp:186
RENDER_TARGET currentTarget
Current rendering target.
Definition: cairo_gal.h:291
unsigned char * wxOutput
wxImage comaptible buffer
Definition: cairo_gal.h:299
unsigned int getNewGroupNumber()
Returns a valid key that can be used as a new group number.
Definition: cairo_gal.cpp:1192
virtual void ClearScreen() override
Clear the screen.
Definition: cairo_gal.cpp:440
virtual void ClearTarget(RENDER_TARGET aTarget) override
Clears the target for rendering.
Definition: cairo_gal.cpp:890
bool isGrouping
Is grouping enabled ?
Definition: cairo_gal.h:336
Class SHAPE_POLY_SET.
std::shared_ptr< CAIRO_COMPOSITOR > compositor
Object for layers compositing.
Definition: cairo_gal.h:288
virtual void DrawBitmap(const BITMAP_BASE &aBitmap) override
Draw a bitmap image.
Definition: cairo_gal.cpp:351
void SetPaintListener(wxEvtHandler *aPaintListener)
Definition: cairo_gal.h:275
std::map< int, GROUP > groups
List of graphic groups.
Definition: cairo_gal.h:339
Enable/disable stroking.
Definition: cairo_gal.h:308
virtual void DrawArcSegment(const VECTOR2D &aCenterPoint, double aRadius, double aStartAngle, double aEndAngle, double aWidth) override
Draw an arc segment.
Definition: cairo_gal.cpp:266
GAL super
Super class definition.
Definition: cairo_gal.h:285
void storePath()
Store the actual path.
Definition: cairo_gal.cpp:959
virtual void Transform(const MATRIX3x3D &aTransformation) override
Transform the context.
Definition: cairo_gal.cpp:548
virtual void Scale(const VECTOR2D &aScale) override
Scale the context.
Definition: cairo_gal.cpp:601
virtual void ChangeGroupColor(int aGroupNumber, const COLOR4D &aNewColor) override
Changes the color used to draw the group.
Definition: cairo_gal.cpp:771
virtual void Rotate(double aAngle) override
Rotate the context.
Definition: cairo_gal.cpp:564
Type definition for an graphics group element.
Definition: cairo_gal.h:324
GRAPHICS_COMMAND command
Command to execute.
Definition: cairo_gal.h:326
Restore the transformation matrix.
Definition: cairo_gal.h:319
virtual void ClearCache() override
Delete all data created during caching of graphic items.
Definition: cairo_gal.cpp:816
cairo_surface_t * surface
Cairo surface.
Definition: cairo_gal.h:347
virtual void SetIsStroke(bool aIsStrokeEnabled) override
Enable/disable stroked outlines.
Definition: cairo_gal.cpp:464
COLOR4D backgroundColor
Background color.
Definition: cairo_gal.h:352
cairo_t * context
Cairo image.
Definition: cairo_gal.h:346
bool isElementAdded
Was an graphic element added ?
Definition: cairo_gal.h:337
virtual void SetLineWidth(double aLineWidth) override
Set the line width.
Definition: cairo_gal.cpp:515
wxEvtHandler * paintListener
Paint listener.
Definition: cairo_gal.h:297
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:856
unsigned int * bitmapBuffer
Storage of the cairo image.
Definition: cairo_gal.h:348
void skipMouseEvent(wxMouseEvent &aEvent)
Mouse event handler, forwards the event to the child.
Definition: cairo_gal.cpp:1013
static const cairo_format_t GAL_FORMAT
Format used to store pixels.
Definition: cairo_gal.h:411
Rotate the context.
Definition: cairo_gal.h:315
virtual void DrawCursor(const VECTOR2D &aCursorPosition) override
Draw the cursor.
Definition: cairo_gal.cpp:922
int stride
Stride value for Cairo.
Definition: cairo_gal.h:350
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:338
cairo_matrix_t cairoWorldScreenMatrix
Cairo world to screen transformation matrix.
Definition: cairo_gal.h:344
virtual bool Show(bool aShow) override
Shows/hides the GAL canvas.
Definition: cairo_gal.cpp:423
unsigned int * bitmapBufferBackup
Backup storage of the cairo image.
Definition: cairo_gal.h:349
Enable/disable filling.
Definition: cairo_gal.h:307
virtual RENDER_TARGET GetTarget() const override
Gets the currently used target for rendering.
Definition: cairo_gal.cpp:884
virtual void SaveScreen() override
Save the screen contents.
Definition: cairo_gal.cpp:825
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:789
virtual void SetNegativeDrawMode(bool aSetting) override
Sets negative draw mode in the renderer.
Definition: cairo_gal.cpp:916
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:270
virtual void DrawGroup(int aGroupNumber) override
Draw the stored group.
Definition: cairo_gal.cpp:681
cairo_t * currentContext
Currently used Cairo context for drawing.
Definition: cairo_gal.h:345
virtual void DrawLine(const VECTOR2D &aStartPoint, const VECTOR2D &aEndPoint) override
Draw a line.
Definition: cairo_gal.cpp:177
virtual void drawGridLine(const VECTOR2D &aStartPoint, const VECTOR2D &aEndPoint) override
Draw a grid line (usually a simplified line function).
Definition: cairo_gal.cpp:928
void deleteBitmaps()
Allocate the bitmaps for drawing.
Definition: cairo_gal.cpp:1054
unsigned int bufferSize
Size of buffers cairoOutput, bitmapBuffers.
Definition: cairo_gal.h:298
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:654
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:338
Set the fill path.
Definition: cairo_gal.h:313
wxEvtHandler * mouseListener
Mouse listener.
Definition: cairo_gal.h:296
void allocateBitmaps()
Allocate the bitmaps for drawing.
Definition: cairo_gal.cpp:1039
virtual void blitCursor(wxMemoryDC &clientDC)
Blits cursor into the current screen.
Definition: cairo_gal.cpp:1021
unsigned int mainBuffer
Handle to the main buffer.
Definition: cairo_gal.h:289
virtual void DrawPolyline(const SHAPE_LINE_CHAIN &aLineChain) override
Definition: cairo_gal.h:128
wxWindow * parentWindow
Parent window.
Definition: cairo_gal.h:295
virtual void EndGroup() override
End the group.
Definition: cairo_gal.cpp:672
Save the transformation matrix.
Definition: cairo_gal.h:318
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:620