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