iPlug2 - C++ Audio Plug-in Framework
IGraphicsSkia.h
1 #pragma once
2 
3 #include "IPlugPlatform.h"
4 #include "IGraphics.h"
5 
6 // N.B. - this must be defined according to the skia build, not the iPlug build
7 #if (defined OS_MAC || defined OS_IOS) && !defined IGRAPHICS_SKIA_NO_METAL
8 #define SK_METAL
9 #endif
10 
11 #if defined IGRAPHICS_GL
12 #define SK_GL
13 #endif
14 
15 #pragma warning( push )
16 #pragma warning( disable : 4244 )
17 #include "SkSurface.h"
18 #include "SkPath.h"
19 #include "SkCanvas.h"
20 #include "SkImage.h"
21 #include "GrDirectContext.h"
22 #pragma warning( pop )
23 
24 BEGIN_IPLUG_NAMESPACE
25 BEGIN_IGRAPHICS_NAMESPACE
26 
28 SkRect SkiaRect(const IRECT& r);
29 
31 SkBlendMode SkiaBlendMode(const IBlend* pBlend);
32 
34 SkColor SkiaColor(const IColor& color, const IBlend* pBlend);
35 
37 SkTileMode SkiaTileMode(const IPattern& pattern);
38 
40 SkPaint SkiaPaint(const IPattern& pattern, const IBlend* pBlend);
41 
44 class IGraphicsSkia : public IGraphics
45 {
46 private:
47  class Bitmap;
48  struct Font;
49 public:
50  IGraphicsSkia(IGEditorDelegate& dlg, int w, int h, int fps, float scale);
51  ~IGraphicsSkia();
52 
53  const char* GetDrawingAPIStr() override ;
54 
55  void BeginFrame() override;
56  void EndFrame() override;
57  void OnViewInitialized(void* pContext) override;
58  void OnViewDestroyed() override;
59  void DrawResize() override;
60 
61  void DrawBitmap(const IBitmap& bitmap, const IRECT& dest, int srcX, int srcY, const IBlend* pBlend) override;
62 
63  void PathClear() override { mMainPath.reset(); }
64  void PathClose() override { mMainPath.close(); }
65  void PathArc(float cx, float cy, float r, float a1, float a2, EWinding winding) override;
66 
67  void PathMoveTo(float x, float y) override { mMainPath.moveTo(mMatrix.mapXY(x, y)); }
68  void PathLineTo(float x, float y) override { mMainPath.lineTo(mMatrix.mapXY(x, y)); }
69 
70  void PathCubicBezierTo(float x1, float y1, float x2, float y2, float x3, float y3) override
71  {
72  mMainPath.cubicTo(mMatrix.mapXY(x1, y1), mMatrix.mapXY(x2, y2), mMatrix.mapXY(x3, y3));
73  }
74 
75  void PathQuadraticBezierTo(float cx, float cy, float x2, float y2) override
76  {
77  mMainPath.quadTo(mMatrix.mapXY(cx, cy), mMatrix.mapXY(x2, y2));
78  }
79 
80  void PathStroke(const IPattern& pattern, float thickness, const IStrokeOptions& options, const IBlend* pBlend) override;
81  void PathFill(const IPattern& pattern, const IFillOptions& options, const IBlend* pBlend) override;
82 
83 #ifdef IGRAPHICS_DRAWFILL_DIRECT
84  //void DrawPoint(const IColor& color, float x, float y, const IBlend* pBlend) override;
85  //void DrawLine(const IColor& color, float x1, float y1, float x2, float y2, const IBlend* pBlend, float thickness) override;
86  //void DrawGrid(const IColor& color, const IRECT& bounds, float gridSizeH, float gridSizeV, const IBlend* pBlend, float thickness) override;
87  //void DrawData(const IColor& color, const IRECT& bounds, float* normYPoints, int nPoints, float* normXPoints, const IBlend* pBlend, float thickness) override;
88  //void DrawDottedLine(const IColor& color, float x1, float y1, float x2, float y2, const IBlend* pBlend, float thickness, float dashLen) override;
89  //void DrawTriangle(const IColor& color, float x1, float y1, float x2, float y2, float x3, float y3, const IBlend* pBlend, float thickness) override;
90  void DrawRect(const IColor& color, const IRECT& bounds, const IBlend* pBlend, float thickness) override;
91  void DrawRoundRect(const IColor& color, const IRECT& bounds, float cornerRadius, const IBlend* pBlend, float thickness) override;
92  //void DrawRoundRect(const IColor& color, const IRECT& bounds, float cRTL, float cRTR, float cRBR, float cRBL, const IBlend* pBlend, float thickness) override;
93  //void DrawConvexPolygon(const IColor& color, float* x, float* y, int nPoints, const IBlend* pBlend, float thickness) override;
94  void DrawArc(const IColor& color, float cx, float cy, float r, float a1, float a2, const IBlend* pBlend, float thickness) override;
95  void DrawCircle(const IColor& color, float cx, float cy, float r, const IBlend* pBlend, float thickness) override;
96  //void DrawDottedRect(const IColor& color, const IRECT& bounds, const IBlend* pBlend, float thickness, float dashLen) override;
97  void DrawEllipse(const IColor& color, const IRECT& bounds, const IBlend* pBlend, float thickness) override;
98  //void DrawEllipse(const IColor& color, float x, float y, float r1, float r2, float angle, const IBlend* pBlend, float thickness) override;
99 
100  //void FillTriangle(const IColor& color, float x1, float y1, float x2, float y2, float x3, float y3, const IBlend* pBlend) override;
101  void FillRect(const IColor& color, const IRECT& bounds, const IBlend* pBlend) override;
102  void FillRoundRect(const IColor& color, const IRECT& bounds, float cornerRadius, const IBlend* pBlend) override;
103  //void FillRoundRect(const IColor& color, const IRECT& bounds, float cRTL, float cRTR, float cRBR, float cRBL, const IBlend* pBlend) override;
104  //void FillConvexPolygon(const IColor& color, float* x, float* y, int nPoints, const IBlend* pBlend) override;
105  void FillArc(const IColor& color, float cx, float cy, float r, float a1, float a2, const IBlend* pBlend) override;
106  void FillCircle(const IColor& color, float cx, float cy, float r, const IBlend* pBlend) override;
107  void FillEllipse(const IColor& color, const IRECT& bounds, const IBlend* pBlend) override;
108  //void FillEllipse(const IColor& color, float x, float y, float r1, float r2, float angle, const IBlend* pBlend) override;
109 #endif
110  void DrawFastDropShadow(const IRECT& innerBounds, const IRECT& outerBounds, float xyDrop, float roundness, float blur, IBlend* pBlend) override;
111 
112  IColor GetPoint(int x, int y) override;
113  void* GetDrawContext() override { return (void*) mCanvas; }
114 
115  bool BitmapExtSupported(const char* ext) override;
116  int AlphaChannel() const override { return 3; }
117  bool FlippedBitmap() const override { return false; }
118 
119  APIBitmap* CreateAPIBitmap(int width, int height, int scale, double drawScale, bool cacheable = false) override;
120 
121  void GetLayerBitmapData(const ILayerPtr& layer, RawBitmapData& data) override;
122  void ApplyShadowMask(ILayerPtr& layer, RawBitmapData& mask, const IShadow& shadow) override;
123 
124  void UpdateLayer() override;
125 
126 protected:
127 
128  float DoMeasureText(const IText& text, const char* str, IRECT& bounds) const override;
129  void DoDrawText(const IText& text, const char* str, const IRECT& bounds, const IBlend* pBlend) override;
130 
131  bool LoadAPIFont(const char* fontID, const PlatformFontPtr& font) override;
132 
133  APIBitmap* LoadAPIBitmap(const char* fileNameOrResID, int scale, EResourceLocation location, const char* ext) override;
134  APIBitmap* LoadAPIBitmap(const char* name, const void* pData, int dataSize, int scale) override;
135 private:
136  void DrawImGui(SkSurface* surface);
137 
138  void PrepareAndMeasureText(const IText& text, const char* str, IRECT& r, double& x, double & y, SkFont& font) const;
139 
140  void PathTransformSetMatrix(const IMatrix& m) override;
141  void SetClipRegion(const IRECT& r) override;
142 
143  void RenderPath(SkPaint& paint);
144 
145  sk_sp<SkSurface> mSurface;
146  SkCanvas* mCanvas = nullptr;
147  SkPath mMainPath;
148  SkMatrix mMatrix;
149  SkMatrix mClipMatrix;
150  SkMatrix mFinalMatrix;
151 
152 #if defined OS_WIN && defined IGRAPHICS_CPU
153  WDL_TypedBuf<uint8_t> mSurfaceMemory;
154 #endif
155 
156 #ifndef IGRAPHICS_CPU
157  sk_sp<GrDirectContext> mGrContext;
158  sk_sp<SkSurface> mScreenSurface;
159 #endif
160 
161 #ifdef IGRAPHICS_METAL
162  void* mMTLDevice;
163  void* mMTLCommandQueue;
164  void* mMTLDrawable;
165  void* mMTLLayer;
166 #endif
167 
168  static StaticStorage<Font> sFontCache;
169 };
170 
171 END_IGRAPHICS_NAMESPACE
172 END_IPLUG_NAMESPACE
173 
const char * GetDrawingAPIStr() override
void UpdateLayer() override
Implemented by a graphics backend to prepare for drawing to the layer at the top of the stack...
void PathLineTo(float x, float y) override
Add a line to the current path from the current point to the specified location.
Definition: IGraphicsSkia.h:68
void PathQuadraticBezierTo(float cx, float cy, float x2, float y2) override
Add a quadratic bezier to the current path from the current point to the specified location...
Definition: IGraphicsSkia.h:75
Used to manage a rectangular area, independent of draw class/platform.
virtual void DrawRect(const IColor &color, const IRECT &bounds, const IBlend *pBlend=0, float thickness=1.f)
Draw a rectangle to the graphics context.
Definition: IGraphics.cpp:2475
Used to manage composite/blend operations, independent of draw class/platform.
User-facing bitmap abstraction that you use to manage bitmap data, independant of draw class/platform...
Used to manage fill behaviour for path based drawing back ends.
bool BitmapExtSupported(const char *ext) override
Checks a file extension and reports whether this drawing API supports loading that extension...
APIBitmap * CreateAPIBitmap(int width, int height, int scale, double drawScale, bool cacheable=false) override
Creates a new API bitmap, either in memory or as a GPU texture.
virtual void FillCircle(const IColor &color, float cx, float cy, float r, const IBlend *pBlend=0)
Fill a circle with a color.
Definition: IGraphics.cpp:2584
Include to get consistently named preprocessor macros for different platforms and logging functionali...
void GetLayerBitmapData(const ILayerPtr &layer, RawBitmapData &data) override
Get the contents of a layers pixels as bitmap data.
void PathArc(float cx, float cy, float r, float a1, float a2, EWinding winding) override
Add an arc to the current path.
virtual void DrawRoundRect(const IColor &color, const IRECT &bounds, float cornerRadius=5.f, const IBlend *pBlend=0, float thickness=1.f)
Draw a rounded rectangle to the graphics context.
Definition: IGraphics.cpp:2482
Used to manage color data, independent of draw class/platform.
void DrawBitmap(const IBitmap &bitmap, const IRECT &dest, int srcX, int srcY, const IBlend *pBlend) override
Draw a bitmap (raster) image to the graphics context.
Used to manage stroke behaviour for path based drawing back ends.
void EndFrame() override
Called by some drawing API classes to finally blit the draw bitmap onto the screen or perform other c...
void DrawResize() override
void PathClose() override
Close the path that is being specified.
Definition: IGraphicsSkia.h:64
The lowest level base class of an IGraphics context.
An editor delegate base class for a SOMETHING that uses IGraphics for it&#39;s UI.
void PathMoveTo(float x, float y) override
Move the current point in the current path.
Definition: IGraphicsSkia.h:67
IText is used to manage font and text/text entry style for a piece of text on the UI...
virtual void DrawCircle(const IColor &color, float cx, float cy, float r, const IBlend *pBlend=0, float thickness=1.f)
Draw a circle to the graphics context.
Definition: IGraphics.cpp:2510
int AlphaChannel() const override
float DoMeasureText(const IText &text, const char *str, IRECT &bounds) const override
void PathClear() override
Clear the stack of path drawing commands.
Definition: IGraphicsSkia.h:63
virtual void FillArc(const IColor &color, float cx, float cy, float r, float a1, float a2, const IBlend *pBlend=0)
Fill an arc segment with a color.
Definition: IGraphics.cpp:2575
void OnViewDestroyed() override
Called after a platform view is destroyed, so that drawing classes can e.g.
The lowest level base class of an IGraphics context.
Definition: IGraphics.h:86
Used to specify properties of a drop-shadow to a layer.
void ApplyShadowMask(ILayerPtr &layer, RawBitmapData &mask, const IShadow &shadow) override
Implemented by a graphics backend to apply a calculated shadow mask to a layer, according to the shad...
void OnViewInitialized(void *pContext) override
Called after platform view initialization, so that drawing classes can e.g.
virtual void FillRoundRect(const IColor &color, const IRECT &bounds, float cornerRadius=5.f, const IBlend *pBlend=0)
Fill a rounded rectangle with a color.
Definition: IGraphics.cpp:2554
virtual void FillRect(const IColor &color, const IRECT &bounds, const IBlend *pBlend=0)
Fill a rectangular region of the graphics context with a color.
Definition: IGraphics.cpp:2547
virtual void FillEllipse(const IColor &color, const IRECT &bounds, const IBlend *pBlend=0)
Fill an ellipse within a rectangular region of the graphics context.
Definition: IGraphics.cpp:2591
virtual void DrawEllipse(const IColor &color, const IRECT &bounds, const IBlend *pBlend=0, float thickness=1.f)
Draw an ellipse within a rectangular region of the graphics context.
Definition: IGraphics.cpp:2526
IGraphics draw class using Skia.
Definition: IGraphicsSkia.h:44
void PathCubicBezierTo(float x1, float y1, float x2, float y2, float x3, float y3) override
Add a cubic bezier to the current path from the current point to the specified location.
Definition: IGraphicsSkia.h:70
bool FlippedBitmap() const override
IColor GetPoint(int x, int y) override
Get the color at an X, Y location in the graphics context.
void * GetDrawContext() override
Gets a void pointer to underlying drawing context, for the IGraphics backend See draw class implement...
void DoDrawText(const IText &text, const char *str, const IRECT &bounds, const IBlend *pBlend) override
A base class interface for a bitmap abstraction around the different drawing back end bitmap represen...
void PathStroke(const IPattern &pattern, float thickness, const IStrokeOptions &options, const IBlend *pBlend) override
Stroke the current current path.
bool LoadAPIFont(const char *fontID, const PlatformFontPtr &font) override
Drawing API method to load a font from a PlatformFontPtr, called internally.
void BeginFrame() override
Called at the beginning of drawing.
void DrawFastDropShadow(const IRECT &innerBounds, const IRECT &outerBounds, float xyDrop, float roundness, float blur, IBlend *pBlend) override
NanoVG only.
virtual void DrawArc(const IColor &color, float cx, float cy, float r, float a1, float a2, const IBlend *pBlend=0, float thickness=1.f)
Draw an arc to the graphics context.
Definition: IGraphics.cpp:2503
void PathFill(const IPattern &pattern, const IFillOptions &options, const IBlend *pBlend) override
Fill the current current path.
std::unique_ptr< ILayer > ILayerPtr
ILayerPtr is a managed pointer for transferring the ownership of layers.
Used to store pattern information for gradients.
APIBitmap * LoadAPIBitmap(const char *fileNameOrResID, int scale, EResourceLocation location, const char *ext) override
Drawing API method to load a bitmap, called internally.
Used to store transformation matrices.