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( const COLOR4D& aColor ) 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 
243  // -------
244  // Cursor
245  // -------
246 
248  virtual void DrawCursor( const VECTOR2D& aCursorPosition ) override;
249 
255  void PostPaint()
256  {
257  if( paintListener )
258  {
259  wxPaintEvent redrawEvent;
260  wxPostEvent( paintListener, redrawEvent );
261  }
262  }
263 
264  void SetMouseListener( wxEvtHandler* aMouseListener )
265  {
266  mouseListener = aMouseListener;
267  }
268 
269  void SetPaintListener( wxEvtHandler* aPaintListener )
270  {
271  paintListener = aPaintListener;
272  }
273 
274 protected:
275  virtual void drawGridLine( const VECTOR2D& aStartPoint, const VECTOR2D& aEndPoint ) override;
276 
277 private:
279  typedef GAL super;
280 
281  // Compositing variables
282  std::shared_ptr<CAIRO_COMPOSITOR> compositor;
283  unsigned int mainBuffer;
284  unsigned int overlayBuffer;
287 
288  // Variables related to wxWidgets
289  wxWindow* parentWindow;
290  wxEvtHandler* mouseListener;
291  wxEvtHandler* paintListener;
292  unsigned int bufferSize;
293  unsigned char* wxOutput;
294 
296  static const int MAX_CAIRO_ARGUMENTS = 4;
297 
300  {
308  //CMD_TRANSFORM, ///< Transform the actual context
315  };
316 
318  typedef struct
319  {
321  union {
322  double dblArg[MAX_CAIRO_ARGUMENTS];
323  bool boolArg;
324  int intArg;
325  } argument;
326  cairo_path_t* cairoPath;
327  } GROUP_ELEMENT;
328 
329  // Variables for the grouping function
330  bool isGrouping;
332  typedef std::deque<GROUP_ELEMENT> GROUP;
333  std::map<int, GROUP> groups;
334  unsigned int groupCounter;
335  GROUP* currentGroup;
336 
337  // Variables related to Cairo <-> wxWidgets
338  cairo_matrix_t cairoWorldScreenMatrix;
339  cairo_t* currentContext;
340  cairo_t* context;
341  cairo_surface_t* surface;
342  unsigned int* bitmapBuffer;
343  unsigned int* bitmapBufferBackup;
344  int stride;
347 
349 
351  bool updatedGalDisplayOptions( const GAL_DISPLAY_OPTIONS& aOptions ) override;
352 
353  void flushPath();
354  // Methods
355  void storePath();
356 
357  // Event handlers
363  void onPaint( wxPaintEvent& aEvent );
364 
370  void skipMouseEvent( wxMouseEvent& aEvent );
371 
375  virtual void blitCursor( wxMemoryDC& clientDC );
376 
378  void initSurface();
379 
381  void deinitSurface();
382 
384  void allocateBitmaps();
385 
387  void deleteBitmaps();
388 
390  void setCompositor();
391 
393  void drawPoly( const std::deque<VECTOR2D>& aPointList );
394  void drawPoly( const VECTOR2D aPointList[], int aListSize );
395  void drawPoly( const SHAPE_LINE_CHAIN& aLineChain );
396 
402  unsigned int getNewGroupNumber();
403 
405  static const cairo_format_t GAL_FORMAT = CAIRO_FORMAT_RGB24;
406 
408  static const float LAYER_ALPHA;
409 };
410 } // namespace KIGFX
411 
412 #endif // CAIROGAL_H_
virtual void ClearScreen(const COLOR4D &aColor) override
Clear the screen.
Definition: cairo_gal.cpp:369
unsigned int groupCounter
Counter used for generating keys for groups.
Definition: cairo_gal.h:334
virtual void DeleteGroup(int aGroupNumber) override
Delete the group from the memory.
Definition: cairo_gal.cpp:725
void initSurface()
Prepare Cairo surfaces for drawing.
Definition: cairo_gal.cpp:985
virtual void SetLayerDepth(double aLayerDepth) override
Set the depth of the layer (position on the z-axis)
Definition: cairo_gal.cpp:468
int intArg
An int argument.
Definition: cairo_gal.h:324
virtual void EndDrawing() override
End the drawing, needs to be called for every new frame.
Definition: cairo_gal.cpp:127
bool updatedGalDisplayOptions(const GAL_DISPLAY_OPTIONS &aOptions) override
Cairo-specific update handlers
Definition: cairo_gal.cpp:101
Class CAIRO_GAL is the cairo implementation of the graphics abstraction layer.
Definition: class_module.h:56
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:335
void deinitSurface()
Destroy Cairo surfaces when are not needed anymore.
Definition: cairo_gal.cpp:1028
Scale the context.
Definition: cairo_gal.h:311
void onPaint(wxPaintEvent &aEvent)
Paint event handler.
Definition: cairo_gal.cpp:930
cairo_path_t * cairoPath
Pointer to a Cairo path.
Definition: cairo_gal.h:326
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:1055
static const float LAYER_ALPHA
Opacity of a single layer
Definition: cairo_gal.h:408
virtual ~CAIRO_GAL()
Definition: cairo_gal.cpp:92
bool boolArg
A bool argument.
Definition: cairo_gal.h:323
virtual void SetIsFill(bool aIsFillEnabled) override
Enable/disable fill.
Definition: cairo_gal.cpp:378
virtual void DrawArc(const VECTOR2D &aCenterPoint, double aRadius, double aStartAngle, double aEndAngle) override
Draw an arc.
Definition: cairo_gal.cpp:225
void setCompositor()
Prepare the compositor.
Definition: cairo_gal.cpp:1041
virtual void DrawRectangle(const VECTOR2D &aStartPoint, const VECTOR2D &aEndPoint) override
Draw a rectangle.
Definition: cairo_gal.cpp:297
virtual void SetStrokeColor(const COLOR4D &aColor) override
Set the stroke color.
Definition: cairo_gal.cpp:408
static const int MAX_CAIRO_ARGUMENTS
Maximum number of arguments for one command.
Definition: cairo_gal.h:296
void PostPaint()
Function PostPaint posts an event to m_paint_listener.
Definition: cairo_gal.h:255
virtual void BeginDrawing() override
Begin the drawing, needs to be called for every new frame.
Definition: cairo_gal.cpp:115
Set the stroke path.
Definition: cairo_gal.h:306
virtual void Flush() override
Force all remaining objects to be drawn.
Definition: cairo_gal.cpp:363
bool isInitialized
Are Cairo image & surface ready to use.
Definition: cairo_gal.h:345
virtual void Restore() override
Restore the context.
Definition: cairo_gal.cpp:566
virtual void RestoreScreen() override
Restore the screen contents.
Definition: cairo_gal.cpp:770
unsigned int overlayBuffer
Handle to the overlay buffer.
Definition: cairo_gal.h:284
bool IsVisible() const override
>
Definition: cairo_gal.h:91
virtual void Translate(const VECTOR2D &aTranslation) override
Translate the context.
Definition: cairo_gal.cpp:511
virtual void DrawCircle(const VECTOR2D &aCenterPoint, double aRadius) override
Draw a circle using world coordinates.
Definition: cairo_gal.cpp:216
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:299
bool validCompositor
Compositor initialization flag.
Definition: cairo_gal.h:286
GROUP * currentGroup
Currently used group.
Definition: cairo_gal.h:335
virtual void SetFillColor(const COLOR4D &aColor) override
Set the fill color.
Definition: cairo_gal.cpp:426
Translate the context.
Definition: cairo_gal.h:310
virtual void DrawSegment(const VECTOR2D &aStartPoint, const VECTOR2D &aEndPoint, double aWidth) override
Draw a rounded segment.
Definition: cairo_gal.cpp:172
RENDER_TARGET currentTarget
Current rendering target.
Definition: cairo_gal.h:285
unsigned char * wxOutput
wxImage comaptible buffer
Definition: cairo_gal.h:293
unsigned int getNewGroupNumber()
Returns a valid key that can be used as a new group number.
Definition: cairo_gal.cpp:1114
virtual void ClearTarget(RENDER_TARGET aTarget) override
Clears the target for rendering.
Definition: cairo_gal.cpp:819
bool isGrouping
Is grouping enabled ?
Definition: cairo_gal.h:330
Class SHAPE_POLY_SET.
std::shared_ptr< CAIRO_COMPOSITOR > compositor
Object for layers compositing.
Definition: cairo_gal.h:282
void SetPaintListener(wxEvtHandler *aPaintListener)
Definition: cairo_gal.h:269
std::map< int, GROUP > groups
List of graphic groups.
Definition: cairo_gal.h:333
Enable/disable stroking.
Definition: cairo_gal.h:302
virtual void DrawArcSegment(const VECTOR2D &aCenterPoint, double aRadius, double aStartAngle, double aEndAngle, double aWidth) override
Draw an arc segment.
Definition: cairo_gal.cpp:252
GAL super
Super class definition.
Definition: cairo_gal.h:279
void storePath()
Store the actual path.
Definition: cairo_gal.cpp:882
virtual void Transform(const MATRIX3x3D &aTransformation) override
Transform the context.
Definition: cairo_gal.cpp:477
virtual void Scale(const VECTOR2D &aScale) override
Scale the context.
Definition: cairo_gal.cpp:530
virtual void ChangeGroupColor(int aGroupNumber, const COLOR4D &aNewColor) override
Changes the color used to draw the group.
Definition: cairo_gal.cpp:700
virtual void Rotate(double aAngle) override
Rotate the context.
Definition: cairo_gal.cpp:493
Type definition for an graphics group element.
Definition: cairo_gal.h:318
GRAPHICS_COMMAND command
Command to execute.
Definition: cairo_gal.h:320
Restore the transformation matrix.
Definition: cairo_gal.h:313
virtual void ClearCache() override
Delete all data created during caching of graphic items.
Definition: cairo_gal.cpp:745
cairo_surface_t * surface
Cairo surface.
Definition: cairo_gal.h:341
virtual void SetIsStroke(bool aIsStrokeEnabled) override
Enable/disable stroked outlines.
Definition: cairo_gal.cpp:393
COLOR4D backgroundColor
Background color.
Definition: cairo_gal.h:346
cairo_t * context
Cairo image.
Definition: cairo_gal.h:340
bool isElementAdded
Was an graphic element added ?
Definition: cairo_gal.h:331
virtual void SetLineWidth(double aLineWidth) override
Set the line width.
Definition: cairo_gal.cpp:444
wxEvtHandler * paintListener
Paint listener.
Definition: cairo_gal.h:291
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:785
unsigned int * bitmapBuffer
Storage of the cairo image.
Definition: cairo_gal.h:342
void skipMouseEvent(wxMouseEvent &aEvent)
Mouse event handler, forwards the event to the child.
Definition: cairo_gal.cpp:936
static const cairo_format_t GAL_FORMAT
Format used to store pixels.
Definition: cairo_gal.h:405
Rotate the context.
Definition: cairo_gal.h:309
virtual void DrawCursor(const VECTOR2D &aCursorPosition) override
Draw the cursor.
Definition: cairo_gal.cpp:845
int stride
Stride value for Cairo.
Definition: cairo_gal.h:344
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:322
cairo_matrix_t cairoWorldScreenMatrix
Cairo world to screen transformation matrix.
Definition: cairo_gal.h:338
virtual bool Show(bool aShow) override
Shows/hides the GAL canvas.
Definition: cairo_gal.cpp:352
unsigned int * bitmapBufferBackup
Backup storage of the cairo image.
Definition: cairo_gal.h:343
Enable/disable filling.
Definition: cairo_gal.h:301
virtual RENDER_TARGET GetTarget() const override
Gets the currently used target for rendering.
Definition: cairo_gal.cpp:813
virtual void SaveScreen() override
Save the screen contents.
Definition: cairo_gal.cpp:754
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:718
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:264
virtual void DrawGroup(int aGroupNumber) override
Draw the stored group.
Definition: cairo_gal.cpp:610
cairo_t * currentContext
Currently used Cairo context for drawing.
Definition: cairo_gal.h:339
virtual void DrawLine(const VECTOR2D &aStartPoint, const VECTOR2D &aEndPoint) override
Draw a line.
Definition: cairo_gal.cpp:163
virtual void drawGridLine(const VECTOR2D &aStartPoint, const VECTOR2D &aEndPoint) override
Draw a grid line (usually a simplified line function).
Definition: cairo_gal.cpp:851
void deleteBitmaps()
Allocate the bitmaps for drawing.
Definition: cairo_gal.cpp:977
unsigned int bufferSize
Size of buffers cairoOutput, bitmapBuffers.
Definition: cairo_gal.h:292
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:583
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:332
Set the fill path.
Definition: cairo_gal.h:307
wxEvtHandler * mouseListener
Mouse listener.
Definition: cairo_gal.h:290
void allocateBitmaps()
Allocate the bitmaps for drawing.
Definition: cairo_gal.cpp:962
virtual void blitCursor(wxMemoryDC &clientDC)
Blits cursor into the current screen.
Definition: cairo_gal.cpp:944
unsigned int mainBuffer
Handle to the main buffer.
Definition: cairo_gal.h:283
virtual void DrawPolyline(const SHAPE_LINE_CHAIN &aLineChain) override
Definition: cairo_gal.h:128
wxWindow * parentWindow
Parent window.
Definition: cairo_gal.h:289
virtual void EndGroup() override
End the group.
Definition: cairo_gal.cpp:601
Save the transformation matrix.
Definition: cairo_gal.h:312
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:549