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  {
93  return IsShownOnScreen() && !GetClientRect().IsEmpty();
94  }
95 
96  // ---------------
97  // Drawing methods
98  // ---------------
99 
101  virtual void BeginDrawing() override;
102 
104  virtual void EndDrawing() override;
105 
107  virtual void DrawLine( const VECTOR2D& aStartPoint, const VECTOR2D& aEndPoint ) override;
108 
110  virtual void DrawSegment( const VECTOR2D& aStartPoint, const VECTOR2D& aEndPoint, double aWidth ) override;
111 
113  virtual void DrawCircle( const VECTOR2D& aCenterPoint, double aRadius ) override;
114 
116  virtual void DrawArc( const VECTOR2D& aCenterPoint, double aRadius,
117  double aStartAngle, double aEndAngle ) override;
118 
120  virtual void DrawArcSegment( const VECTOR2D& aCenterPoint, double aRadius,
121  double aStartAngle, double aEndAngle, double aWidth ) override;
122 
124  virtual void DrawRectangle( const VECTOR2D& aStartPoint, const VECTOR2D& aEndPoint ) override;
125 
127  virtual void DrawPolyline( const std::deque<VECTOR2D>& aPointList ) override { drawPoly( aPointList ); }
128  virtual void DrawPolyline( const VECTOR2D aPointList[], int aListSize ) override { drawPoly( aPointList, aListSize ); }
129  virtual void DrawPolyline( const SHAPE_LINE_CHAIN& aLineChain ) override { drawPoly( aLineChain ); }
130 
132  virtual void DrawPolygon( const std::deque<VECTOR2D>& aPointList ) override { drawPoly( aPointList ); }
133  virtual void DrawPolygon( const VECTOR2D aPointList[], int aListSize ) override { drawPoly( aPointList, aListSize ); }
134  virtual void DrawPolygon( const SHAPE_POLY_SET& aPolySet ) override;
135 
137  virtual void DrawCurve( const VECTOR2D& startPoint, const VECTOR2D& controlPointA,
138  const VECTOR2D& controlPointB, const VECTOR2D& endPoint ) override;
139 
141  virtual void DrawBitmap( const BITMAP_BASE& aBitmap ) override;
142 
143  // --------------
144  // Screen methods
145  // --------------
146 
148  virtual void ResizeScreen( int aWidth, int aHeight ) override;
149 
151  virtual bool Show( bool aShow ) override;
152 
154  virtual void Flush() override;
155 
157  virtual void ClearScreen( ) override;
158 
159  // -----------------
160  // Attribute setting
161  // -----------------
162 
164  virtual void SetIsFill( bool aIsFillEnabled ) override;
165 
167  virtual void SetIsStroke( bool aIsStrokeEnabled ) override;
168 
170  virtual void SetStrokeColor( const COLOR4D& aColor ) override;
171 
173  virtual void SetFillColor( const COLOR4D& aColor ) override;
174 
176  virtual void SetLineWidth( double aLineWidth ) override;
177 
179  virtual void SetLayerDepth( double aLayerDepth ) override;
180 
181  // --------------
182  // Transformation
183  // --------------
184 
186  virtual void Transform( const MATRIX3x3D& aTransformation ) override;
187 
189  virtual void Rotate( double aAngle ) override;
190 
192  virtual void Translate( const VECTOR2D& aTranslation ) override;
193 
195  virtual void Scale( const VECTOR2D& aScale ) override;
196 
198  virtual void Save() override;
199 
201  virtual void Restore() override;
202 
203  // --------------------------------------------
204  // Group methods
205  // ---------------------------------------------
206 
208  virtual int BeginGroup() override;
209 
211  virtual void EndGroup() override;
212 
214  virtual void DrawGroup( int aGroupNumber ) override;
215 
217  virtual void ChangeGroupColor( int aGroupNumber, const COLOR4D& aNewColor ) override;
218 
220  virtual void ChangeGroupDepth( int aGroupNumber, int aDepth ) override;
221 
223  virtual void DeleteGroup( int aGroupNumber ) override;
224 
226  virtual void ClearCache() override;
227 
228  // --------------------------------------------------------
229  // Handling the world <-> screen transformation
230  // --------------------------------------------------------
231 
233  virtual void SaveScreen() override;
234 
236  virtual void RestoreScreen() override;
237 
239  virtual void SetTarget( RENDER_TARGET aTarget ) override;
240 
242  virtual RENDER_TARGET GetTarget() const override;
243 
245  virtual void ClearTarget( RENDER_TARGET aTarget ) override;
246 
248  virtual void SetNegativeDrawMode( bool aSetting ) override;
249 
250  // -------
251  // Cursor
252  // -------
253 
255  virtual void DrawCursor( const VECTOR2D& aCursorPosition ) override;
256 
262  void PostPaint()
263  {
264  if( paintListener )
265  {
266  wxPaintEvent redrawEvent;
267  wxPostEvent( paintListener, redrawEvent );
268  }
269  }
270 
271  void SetMouseListener( wxEvtHandler* aMouseListener )
272  {
273  mouseListener = aMouseListener;
274  }
275 
276  void SetPaintListener( wxEvtHandler* aPaintListener )
277  {
278  paintListener = aPaintListener;
279  }
280 
281  virtual void EnableDepthTest( bool aEnabled = false ) override;
282 
283 protected:
284  virtual void drawGridLine( const VECTOR2D& aStartPoint, const VECTOR2D& aEndPoint ) override;
285 
286 private:
288  typedef GAL super;
289 
290  // Compositing variables
291  std::shared_ptr<CAIRO_COMPOSITOR> compositor;
292  unsigned int mainBuffer;
293  unsigned int overlayBuffer;
296 
297  // Variables related to wxWidgets
298  wxWindow* parentWindow;
299  wxEvtHandler* mouseListener;
300  wxEvtHandler* paintListener;
301  unsigned int bufferSize;
302  unsigned char* wxOutput;
303 
305  static const int MAX_CAIRO_ARGUMENTS = 4;
306 
309  {
317  //CMD_TRANSFORM, ///< Transform the actual context
324  };
325 
327  typedef struct
328  {
330  union {
331  double dblArg[MAX_CAIRO_ARGUMENTS];
332  bool boolArg;
333  int intArg;
334  } argument;
335  cairo_path_t* cairoPath;
336  } GROUP_ELEMENT;
337 
338  // Variables for the grouping function
339  bool isGrouping;
341  typedef std::deque<GROUP_ELEMENT> GROUP;
342  std::map<int, GROUP> groups;
343  unsigned int groupCounter;
344  GROUP* currentGroup;
345 
346  // Variables related to Cairo <-> wxWidgets
347  cairo_matrix_t cairoWorldScreenMatrix;
348  cairo_t* currentContext;
349  cairo_t* context;
350  cairo_surface_t* surface;
351  unsigned int* bitmapBuffer;
352  unsigned int* bitmapBufferBackup;
353  int stride;
356 
358 
360  bool updatedGalDisplayOptions( const GAL_DISPLAY_OPTIONS& aOptions ) override;
361 
362  void flushPath();
363  // Methods
364  void storePath();
365 
366  // Event handlers
372  void onPaint( wxPaintEvent& aEvent );
373 
379  void skipMouseEvent( wxMouseEvent& aEvent );
380 
384  virtual void blitCursor( wxMemoryDC& clientDC );
385 
387  void initSurface();
388 
390  void deinitSurface();
391 
393  void allocateBitmaps();
394 
396  void deleteBitmaps();
397 
399  void setCompositor();
400 
402  void drawPoly( const std::deque<VECTOR2D>& aPointList );
403  void drawPoly( const VECTOR2D aPointList[], int aListSize );
404  void drawPoly( const SHAPE_LINE_CHAIN& aLineChain );
405 
411  unsigned int getNewGroupNumber();
412 
414  static const cairo_format_t GAL_FORMAT = CAIRO_FORMAT_RGB24;
415 
417  static const float LAYER_ALPHA;
418 };
419 } // namespace KIGFX
420 
421 #endif // CAIROGAL_H_
unsigned int groupCounter
Counter used for generating keys for groups.
Definition: cairo_gal.h:343
virtual void DeleteGroup(int aGroupNumber) override
Delete the group from the memory.
Definition: cairo_gal.cpp:809
void initSurface()
Prepare Cairo surfaces for drawing.
Definition: cairo_gal.cpp:1075
virtual void SetLayerDepth(double aLayerDepth) override
Set the depth of the layer (position on the z-axis)
Definition: cairo_gal.cpp:552
int intArg
An int argument.
Definition: cairo_gal.h:333
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:419
void deinitSurface()
Destroy Cairo surfaces when are not needed anymore.
Definition: cairo_gal.cpp:1119
Scale the context.
Definition: cairo_gal.h:320
void onPaint(wxPaintEvent &aEvent)
Paint event handler.
Definition: cairo_gal.cpp:1020
cairo_path_t * cairoPath
Pointer to a Cairo path.
Definition: cairo_gal.h:335
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:1146
static const float LAYER_ALPHA
Opacity of a single layer
Definition: cairo_gal.h:417
virtual ~CAIRO_GAL()
Definition: cairo_gal.cpp:106
bool boolArg
A bool argument.
Definition: cairo_gal.h:332
virtual void SetIsFill(bool aIsFillEnabled) override
Enable/disable fill.
Definition: cairo_gal.cpp:462
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:1132
virtual void DrawRectangle(const VECTOR2D &aStartPoint, const VECTOR2D &aEndPoint) override
Draw a rectangle.
Definition: cairo_gal.cpp:322
virtual void SetStrokeColor(const COLOR4D &aColor) override
Set the stroke color.
Definition: cairo_gal.cpp:492
static const int MAX_CAIRO_ARGUMENTS
Maximum number of arguments for one command.
Definition: cairo_gal.h:305
void PostPaint()
Function PostPaint posts an event to m_paint_listener.
Definition: cairo_gal.h:262
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:315
virtual void Flush() override
Force all remaining objects to be drawn.
Definition: cairo_gal.cpp:447
bool isInitialized
Are Cairo image & surface ready to use.
Definition: cairo_gal.h:354
virtual void Restore() override
Restore the context.
Definition: cairo_gal.cpp:650
virtual void RestoreScreen() override
Restore the screen contents.
Definition: cairo_gal.cpp:854
unsigned int overlayBuffer
Handle to the overlay buffer.
Definition: cairo_gal.h:293
bool IsVisible() const override
>
Definition: cairo_gal.h:91
virtual void Translate(const VECTOR2D &aTranslation) override
Translate the context.
Definition: cairo_gal.cpp:595
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:128
GRAPHICS_COMMAND
Definitions for the command recorder.
Definition: cairo_gal.h:308
bool validCompositor
Compositor initialization flag.
Definition: cairo_gal.h:295
GROUP * currentGroup
Currently used group.
Definition: cairo_gal.h:344
virtual void SetFillColor(const COLOR4D &aColor) override
Set the fill color.
Definition: cairo_gal.cpp:510
This class handle bitmap images in KiCad.
Definition: bitmap_base.h:46
Translate the context.
Definition: cairo_gal.h:319
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:294
unsigned char * wxOutput
wxImage comaptible buffer
Definition: cairo_gal.h:302
unsigned int getNewGroupNumber()
Returns a valid key that can be used as a new group number.
Definition: cairo_gal.cpp:1205
virtual void ClearScreen() override
Clear the screen.
Definition: cairo_gal.cpp:453
virtual void ClearTarget(RENDER_TARGET aTarget) override
Clears the target for rendering.
Definition: cairo_gal.cpp:903
bool isGrouping
Is grouping enabled ?
Definition: cairo_gal.h:339
Class SHAPE_POLY_SET.
std::shared_ptr< CAIRO_COMPOSITOR > compositor
Object for layers compositing.
Definition: cairo_gal.h:291
virtual void DrawBitmap(const BITMAP_BASE &aBitmap) override
Draw a bitmap image.
Definition: cairo_gal.cpp:360
void SetPaintListener(wxEvtHandler *aPaintListener)
Definition: cairo_gal.h:276
std::map< int, GROUP > groups
List of graphic groups.
Definition: cairo_gal.h:342
Enable/disable stroking.
Definition: cairo_gal.h:311
virtual void DrawArcSegment(const VECTOR2D &aCenterPoint, double aRadius, double aStartAngle, double aEndAngle, double aWidth) override
Draw an arc segment.
Definition: cairo_gal.cpp:275
GAL super
Super class definition.
Definition: cairo_gal.h:288
void storePath()
Store the actual path.
Definition: cairo_gal.cpp:972
virtual void Transform(const MATRIX3x3D &aTransformation) override
Transform the context.
Definition: cairo_gal.cpp:561
virtual void Scale(const VECTOR2D &aScale) override
Scale the context.
Definition: cairo_gal.cpp:614
virtual void ChangeGroupColor(int aGroupNumber, const COLOR4D &aNewColor) override
Changes the color used to draw the group.
Definition: cairo_gal.cpp:784
virtual void Rotate(double aAngle) override
Rotate the context.
Definition: cairo_gal.cpp:577
Type definition for an graphics group element.
Definition: cairo_gal.h:327
GRAPHICS_COMMAND command
Command to execute.
Definition: cairo_gal.h:329
Restore the transformation matrix.
Definition: cairo_gal.h:322
virtual void ClearCache() override
Delete all data created during caching of graphic items.
Definition: cairo_gal.cpp:829
cairo_surface_t * surface
Cairo surface.
Definition: cairo_gal.h:350
virtual void SetIsStroke(bool aIsStrokeEnabled) override
Enable/disable stroked outlines.
Definition: cairo_gal.cpp:477
COLOR4D backgroundColor
Background color.
Definition: cairo_gal.h:355
cairo_t * context
Cairo image.
Definition: cairo_gal.h:349
bool isElementAdded
Was an graphic element added ?
Definition: cairo_gal.h:340
virtual void SetLineWidth(double aLineWidth) override
Set the line width.
Definition: cairo_gal.cpp:528
wxEvtHandler * paintListener
Paint listener.
Definition: cairo_gal.h:300
virtual void DrawPolygon(const VECTOR2D aPointList[], int aListSize) override
Definition: cairo_gal.h:133
virtual void SetTarget(RENDER_TARGET aTarget) override
Sets the target for rendering.
Definition: cairo_gal.cpp:869
unsigned int * bitmapBuffer
Storage of the cairo image.
Definition: cairo_gal.h:351
void skipMouseEvent(wxMouseEvent &aEvent)
Mouse event handler, forwards the event to the child.
Definition: cairo_gal.cpp:1026
static const cairo_format_t GAL_FORMAT
Format used to store pixels.
Definition: cairo_gal.h:414
Rotate the context.
Definition: cairo_gal.h:318
virtual void DrawCursor(const VECTOR2D &aCursorPosition) override
Draw the cursor.
Definition: cairo_gal.cpp:935
int stride
Stride value for Cairo.
Definition: cairo_gal.h:353
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:347
cairo_matrix_t cairoWorldScreenMatrix
Cairo world to screen transformation matrix.
Definition: cairo_gal.h:347
virtual bool Show(bool aShow) override
Shows/hides the GAL canvas.
Definition: cairo_gal.cpp:436
unsigned int * bitmapBufferBackup
Backup storage of the cairo image.
Definition: cairo_gal.h:352
Enable/disable filling.
Definition: cairo_gal.h:310
virtual RENDER_TARGET GetTarget() const override
Gets the currently used target for rendering.
Definition: cairo_gal.cpp:897
virtual void SaveScreen() override
Save the screen contents.
Definition: cairo_gal.cpp:838
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:802
virtual void SetNegativeDrawMode(bool aSetting) override
Sets negative draw mode in the renderer.
Definition: cairo_gal.cpp:929
virtual void DrawPolyline(const std::deque< VECTOR2D > &aPointList) override
Draw a polyline.
Definition: cairo_gal.h:127
void SetMouseListener(wxEvtHandler *aMouseListener)
Definition: cairo_gal.h:271
virtual void DrawGroup(int aGroupNumber) override
Draw the stored group.
Definition: cairo_gal.cpp:694
cairo_t * currentContext
Currently used Cairo context for drawing.
Definition: cairo_gal.h:348
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:941
void deleteBitmaps()
Allocate the bitmaps for drawing.
Definition: cairo_gal.cpp:1067
unsigned int bufferSize
Size of buffers cairoOutput, bitmapBuffers.
Definition: cairo_gal.h:301
virtual void DrawPolygon(const std::deque< VECTOR2D > &aPointList) override
Draw a polygon.
Definition: cairo_gal.h:132
virtual int BeginGroup() override
Begin a group.
Definition: cairo_gal.cpp:667
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:341
Set the fill path.
Definition: cairo_gal.h:316
wxEvtHandler * mouseListener
Mouse listener.
Definition: cairo_gal.h:299
void allocateBitmaps()
Allocate the bitmaps for drawing.
Definition: cairo_gal.cpp:1052
virtual void blitCursor(wxMemoryDC &clientDC)
Blits cursor into the current screen.
Definition: cairo_gal.cpp:1034
unsigned int mainBuffer
Handle to the main buffer.
Definition: cairo_gal.h:292
virtual void DrawPolyline(const SHAPE_LINE_CHAIN &aLineChain) override
Definition: cairo_gal.h:129
wxWindow * parentWindow
Parent window.
Definition: cairo_gal.h:298
virtual void EndGroup() override
End the group.
Definition: cairo_gal.cpp:685
Save the transformation matrix.
Definition: cairo_gal.h:321
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 EnableDepthTest(bool aEnabled=false) override
Definition: cairo_gal.cpp:1216
virtual void Save() override
Save the context.
Definition: cairo_gal.cpp:633