iPlug2 - C++ Audio Plug-in Framework
IGraphicsNanoVG.h
1 /*
2  ==============================================================================
3 
4  This file is part of the iPlug 2 library. Copyright (C) the iPlug 2 developers.
5 
6  See LICENSE.txt for more info.
7 
8  ==============================================================================
9 */
10 
11 #pragma once
12 
13 #include "IPlugPlatform.h"
14 #include "IGraphics.h"
15 
16 #include "nanovg.h"
17 #include "mutex.h"
18 #include <stack>
19 
20 // Thanks to Olli Wang/MOUI for much of this macro magic https://github.com/ollix/moui
21 
22 #if defined IGRAPHICS_GL
23  #define NANOVG_FBO_VALID 1
24  #include "nanovg_gl_utils.h"
25 #elif defined IGRAPHICS_METAL
26  #include "nanovg_mtl.h"
27 #else
28  #error you must define either IGRAPHICS_GL2, IGRAPHICS_GLES2 etc or IGRAPHICS_METAL when using IGRAPHICS_NANOVG
29 #endif
30 
31 #if defined IGRAPHICS_GL2
32  #define NANOVG_GL2 1
33  #define nvgCreateContext(flags) nvgCreateGL2(flags)
34  #define nvgDeleteContext(context) nvgDeleteGL2(context)
35 #elif defined IGRAPHICS_GLES2
36  #define NANOVG_GLES2 1
37  #define nvgCreateContext(flags) nvgCreateGLES2(flags)
38  #define nvgDeleteContext(context) nvgDeleteGLES2(context)
39 #elif defined IGRAPHICS_GL3
40  #define NANOVG_GL3 1
41  #define nvgCreateContext(flags) nvgCreateGL3(flags)
42  #define nvgDeleteContext(context) nvgDeleteGL3(context)
43 #elif defined IGRAPHICS_GLES3
44  #define NANOVG_GLES3 1
45  #define nvgCreateContext(flags) nvgCreateGLES3(flags)
46  #define nvgDeleteContext(context) nvgDeleteGLES3(context)
47 #elif defined IGRAPHICS_METAL
48  #define nvgCreateContext(layer, flags) nvgCreateMTL(layer, flags)
49  #define nvgDeleteContext(context) nvgDeleteMTL(context)
50  #define nvgBindFramebuffer(fb) mnvgBindFramebuffer(fb)
51  #define nvgCreateFramebuffer(ctx, w, h, flags) mnvgCreateFramebuffer(ctx, w, h, flags)
52  #define nvgDeleteFramebuffer(fb) mnvgDeleteFramebuffer(fb)
53 #endif
54 
55 #if defined IGRAPHICS_GL
56  #define nvgBindFramebuffer(fb) nvgluBindFramebuffer(fb)
57  #define nvgCreateFramebuffer(ctx, w, h, flags) nvgluCreateFramebuffer(ctx, w, h, flags)
58  #define nvgDeleteFramebuffer(fb) nvgluDeleteFramebuffer(fb)
59  using NVGframebuffer = NVGLUframebuffer;
60 #elif defined IGRAPHICS_METAL
61  using NVGframebuffer = MNVGframebuffer;
62 #endif
63 
64 BEGIN_IPLUG_NAMESPACE
65 BEGIN_IGRAPHICS_NAMESPACE
66 
68 NVGcolor NanoVGColor(const IColor& color, const IBlend* pBlend = 0);
69 
71 void NanoVGRect(NVGcontext* pContext, const IRECT& r);
72 
74 void NanoVGSetBlendMode(NVGcontext* pContext, const IBlend* pBlend);
75 
77 NVGpaint NanoVGPaint(NVGcontext* pContext, const IPattern& pattern, const IBlend* pBlend = 0);
78 
81 class IGraphicsNanoVG : public IGraphics
82 {
83 private:
84  class Bitmap;
85 
86 public:
87  IGraphicsNanoVG(IGEditorDelegate& dlg, int w, int h, int fps, float scale);
88  ~IGraphicsNanoVG();
89 
90  const char* GetDrawingAPIStr() override;
91 
92  void BeginFrame() override;
93  void EndFrame() override;
94  void OnViewInitialized(void* pContext) override;
95  void OnViewDestroyed() override;
96  void DrawResize() override;
97 
98  void DrawBitmap(const IBitmap& bitmap, const IRECT& dest, int srcX, int srcY, const IBlend* pBlend) override;
99 
100  void DrawDottedLine(const IColor& color, float x1, float y1, float x2, float y2, const IBlend* pBlend, float thickness, float dashLen) override;
101  void DrawDottedRect(const IColor& color, const IRECT& bounds, const IBlend* pBlend, float thickness, float dashLen) override;
102 
103  void DrawFastDropShadow(const IRECT& innerBounds, const IRECT& outerBounds, float xyDrop = 5.f, float roundness = 0.f, float blur = 10.f, IBlend* pBlend = nullptr) override;
104 
105  void PathClear() override;
106  void PathClose() override;
107  void PathArc(float cx, float cy, float r, float a1, float a2, EWinding winding) override;
108  void PathMoveTo(float x, float y) override;
109  void PathLineTo(float x, float y) override;
110  void PathCubicBezierTo(float c1x, float c1y, float c2x, float c2y, float x2, float y2) override;
111  void PathQuadraticBezierTo(float cx, float cy, float x2, float y2) override;
112  void PathSetWinding(bool clockwise) override;
113  void PathStroke(const IPattern& pattern, float thickness, const IStrokeOptions& options, const IBlend* pBlend) override;
114  void PathFill(const IPattern& pattern, const IFillOptions& options, const IBlend* pBlend) override;
115 
116  IColor GetPoint(int x, int y) override;
117  void* GetDrawContext() override { return (void*) mVG; }
118 
119  IBitmap LoadBitmap(const char* name, int nStates, bool framesAreHorizontal, int targetScale) override;
120  void ReleaseBitmap(const IBitmap& bitmap) override { }; // NO-OP
121  void RetainBitmap(const IBitmap& bitmap, const char * cacheName) override { }; // NO-OP
122  bool BitmapExtSupported(const char* ext) override;
123 
124  void DeleteFBO(NVGframebuffer* pBuffer);
125 
126 protected:
127  APIBitmap* LoadAPIBitmap(const char* fileNameOrResID, int scale, EResourceLocation location, const char* ext) override;
128  APIBitmap* LoadAPIBitmap(const char* name, const void* pData, int dataSize, int scale) override;
129  APIBitmap* CreateAPIBitmap(int width, int height, int scale, double drawScale, bool cacheable = false) override;
130 
131  bool LoadAPIFont(const char* fontID, const PlatformFontPtr& font) override;
132 
133  int AlphaChannel() const override { return 3; }
134 
135  bool FlippedBitmap() const override
136  {
137 #if defined(IGRAPHICS_GL)
138  return true;
139 #else
140  return false;
141 #endif
142  }
143 
144  void GetLayerBitmapData(const ILayerPtr& layer, RawBitmapData& data) override;
145  void ApplyShadowMask(ILayerPtr& layer, RawBitmapData& mask, const IShadow& shadow) override;
146 
147  float DoMeasureText(const IText& text, const char* str, IRECT& bounds) const override;
148  void DoDrawText(const IText& text, const char* str, const IRECT& bounds, const IBlend* pBlend) override;
149 
150 private:
151  void PrepareAndMeasureText(const IText& text, const char* str, IRECT& r, double& x, double & y) const;
152  void PathTransformSetMatrix(const IMatrix& m) override;
153  void SetClipRegion(const IRECT& r) override;
154  void UpdateLayer() override;
155  void ClearFBOStack();
156 
157  bool mInDraw = false;
158  WDL_Mutex mFBOMutex;
159  std::stack<NVGframebuffer*> mFBOStack; // A stack of FBOs that requires freeing at the end of the frame
160  StaticStorage<APIBitmap> mBitmapCache; //not actually static (doesn't require retaining or releasing)
161  NVGcontext* mVG = nullptr;
162  NVGframebuffer* mMainFrameBuffer = nullptr;
163  int mInitialFBO = 0;
164 };
165 
166 END_IGRAPHICS_NAMESPACE
167 END_IPLUG_NAMESPACE
void DoDrawText(const IText &text, const char *str, const IRECT &bounds, const IBlend *pBlend) override
IColor GetPoint(int x, int y) override
Get the color at an X, Y location in the graphics context.
void DrawDottedLine(const IColor &color, float x1, float y1, float x2, float y2, const IBlend *pBlend, float thickness, float dashLen) override
Draw a dotted line to the graphics context.
Used to manage a rectangular area, independent of draw class/platform.
void RetainBitmap(const IBitmap &bitmap, const char *cacheName) override
Adds an IBitmap to the cache/static storage.
void PathCubicBezierTo(float c1x, float c1y, float c2x, float c2y, float x2, float y2) override
Add a cubic bezier to the current path from the current point to the specified location.
void PathFill(const IPattern &pattern, const IFillOptions &options, const IBlend *pBlend) override
Fill the current current path.
void DrawResize() override
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.
const char * GetDrawingAPIStr() override
Include to get consistently named preprocessor macros for different platforms and logging functionali...
void DrawDottedRect(const IColor &color, const IRECT &bounds, const IBlend *pBlend, float thickness, float dashLen) override
Draw a dotted rectangle to the graphics context.
void EndFrame() override
Called by some drawing API classes to finally blit the draw bitmap onto the screen or perform other c...
Used to manage color data, independent of draw class/platform.
Used to manage stroke behaviour for path based drawing back ends.
int AlphaChannel() const override
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...
void PathArc(float cx, float cy, float r, float a1, float a2, EWinding winding) override
Add an arc to the current path.
float DoMeasureText(const IText &text, const char *str, IRECT &bounds) const override
void PathLineTo(float x, float y) override
Add a line to the current path from the current point to the specified location.
void * GetDrawContext() override
Gets a void pointer to underlying drawing context, for the IGraphics backend See draw class implement...
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 OnViewInitialized(void *pContext) override
Called after platform view initialization, so that drawing classes can e.g.
IText is used to manage font and text/text entry style for a piece of text on the UI...
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...
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.
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 PathClose() override
Close the path that is being specified.
void OnViewDestroyed() override
Called after a platform view is destroyed, so that drawing classes can e.g.
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.
IBitmap LoadBitmap(const char *name, int nStates, bool framesAreHorizontal, int targetScale) override
Load a bitmap image from disk or from windows resource.
A base class interface for a bitmap abstraction around the different drawing back end bitmap represen...
void PathSetWinding(bool clockwise) override
NanoVG only.
void GetLayerBitmapData(const ILayerPtr &layer, RawBitmapData &data) override
Get the contents of a layers pixels as bitmap data.
APIBitmap * LoadAPIBitmap(const char *fileNameOrResID, int scale, EResourceLocation location, const char *ext) override
Drawing API method to load a bitmap, called internally.
void PathStroke(const IPattern &pattern, float thickness, const IStrokeOptions &options, const IBlend *pBlend) override
Stroke the current current path.
void PathMoveTo(float x, float y) override
Move the current point in the current path.
bool FlippedBitmap() const override
void DrawFastDropShadow(const IRECT &innerBounds, const IRECT &outerBounds, float xyDrop=5.f, float roundness=0.f, float blur=10.f, IBlend *pBlend=nullptr) override
NanoVG only.
IGraphics draw class using NanoVG.
bool BitmapExtSupported(const char *ext) override
Checks a file extension and reports whether this drawing API supports loading that extension...
void PathClear() override
Clear the stack of path drawing commands.
std::unique_ptr< ILayer > ILayerPtr
ILayerPtr is a managed pointer for transferring the ownership of layers.
Used to store pattern information for gradients.
bool LoadAPIFont(const char *fontID, const PlatformFontPtr &font) override
Drawing API method to load a font from a PlatformFontPtr, called internally.
Used to store transformation matrices.
void BeginFrame() override
Called at the beginning of drawing.
void ReleaseBitmap(const IBitmap &bitmap) override
Releases an IBitmap from the cache/static storage.