iPlug2 - C++ Audio Plug-in Framework
Loading...
Searching...
No Matches
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
24#if defined OS_MAC || defined OS_IOS
25#if defined IGRAPHICS_GLES2 || defined IGRAPHICS_GLES3
26 #include <libGLESv2/angle_gl.h>
27#endif
28#endif
29
30 #define NANOVG_FBO_VALID 1
31 #include "nanovg_gl_utils.h"
32#elif defined IGRAPHICS_METAL
33 #include "nanovg_mtl.h"
34#else
35 #error you must define either IGRAPHICS_GL2, IGRAPHICS_GLES2 etc or IGRAPHICS_METAL when using IGRAPHICS_NANOVG
36#endif
37
38#if defined IGRAPHICS_GL2
39 #define NANOVG_GL2 1
40 #define nvgCreateContext(flags) nvgCreateGL2(flags)
41 #define nvgDeleteContext(context) nvgDeleteGL2(context)
42#elif defined IGRAPHICS_GLES2
43 #define NANOVG_GLES2 1
44 #define nvgCreateContext(flags) nvgCreateGLES2(flags)
45 #define nvgDeleteContext(context) nvgDeleteGLES2(context)
46#elif defined IGRAPHICS_GL3
47 #define NANOVG_GL3 1
48 #define nvgCreateContext(flags) nvgCreateGL3(flags)
49 #define nvgDeleteContext(context) nvgDeleteGL3(context)
50#elif defined IGRAPHICS_GLES3
51 #define NANOVG_GLES3 1
52 #define nvgCreateContext(flags) nvgCreateGLES3(flags)
53 #define nvgDeleteContext(context) nvgDeleteGLES3(context)
54#elif defined IGRAPHICS_METAL
55 #define nvgCreateContext(layer, flags) nvgCreateMTL(layer, flags)
56 #define nvgDeleteContext(context) nvgDeleteMTL(context)
57 #define nvgBindFramebuffer(fb) mnvgBindFramebuffer(fb)
58 #define nvgCreateFramebuffer(ctx, w, h, flags) mnvgCreateFramebuffer(ctx, w, h, flags)
59 #define nvgDeleteFramebuffer(fb) mnvgDeleteFramebuffer(fb)
60#endif
61
62#if defined IGRAPHICS_GL
63 #define nvgBindFramebuffer(fb) nvgluBindFramebuffer(fb)
64 #define nvgCreateFramebuffer(ctx, w, h, flags) nvgluCreateFramebuffer(ctx, w, h, flags)
65 #define nvgDeleteFramebuffer(fb) nvgluDeleteFramebuffer(fb)
66 using NVGframebuffer = NVGLUframebuffer;
67#elif defined IGRAPHICS_METAL
68 using NVGframebuffer = MNVGframebuffer;
69#endif
70
71BEGIN_IPLUG_NAMESPACE
72BEGIN_IGRAPHICS_NAMESPACE
73
75NVGcolor NanoVGColor(const IColor& color, const IBlend* pBlend = 0);
76
78void NanoVGRect(NVGcontext* pContext, const IRECT& r);
79
81void NanoVGSetBlendMode(NVGcontext* pContext, const IBlend* pBlend);
82
84NVGpaint NanoVGPaint(NVGcontext* pContext, const IPattern& pattern, const IBlend* pBlend = 0);
85
89{
90private:
91 class Bitmap;
92
93public:
94 IGraphicsNanoVG(IGEditorDelegate& dlg, int w, int h, int fps, float scale);
96
97 const char* GetDrawingAPIStr() override;
98
99 void BeginFrame() override;
100 void EndFrame() override;
101 void OnViewInitialized(void* pContext) override;
102 void OnViewDestroyed() override;
103 void DrawResize() override;
104
105 void DrawBitmap(const IBitmap& bitmap, const IRECT& dest, int srcX, int srcY, const IBlend* pBlend) override;
106
107 void DrawDottedLine(const IColor& color, float x1, float y1, float x2, float y2, const IBlend* pBlend, float thickness, float dashLen) override;
108 void DrawDottedRect(const IColor& color, const IRECT& bounds, const IBlend* pBlend, float thickness, float dashLen) override;
109
110 void DrawFastDropShadow(const IRECT& innerBounds, const IRECT& outerBounds, float xyDrop = 5.f, float roundness = 0.f, float blur = 10.f, IBlend* pBlend = nullptr) override;
111
112 void DrawMultiLineText(const IText& text, const char* str, const IRECT& bounds, const IBlend* pBlend) override;
113
114 void PathClear() override;
115 void PathClose() override;
116 void PathArc(float cx, float cy, float r, float a1, float a2, EWinding winding) override;
117 void PathMoveTo(float x, float y) override;
118 void PathLineTo(float x, float y) override;
119 void PathCubicBezierTo(float c1x, float c1y, float c2x, float c2y, float x2, float y2) override;
120 void PathQuadraticBezierTo(float cx, float cy, float x2, float y2) override;
121 void PathSetWinding(bool clockwise) override;
122 void PathStroke(const IPattern& pattern, float thickness, const IStrokeOptions& options, const IBlend* pBlend) override;
123 void PathFill(const IPattern& pattern, const IFillOptions& options, const IBlend* pBlend) override;
124
125 IColor GetPoint(int x, int y) override;
126 void* GetDrawContext() override { return (void*) mVG; }
127
128 IBitmap LoadBitmap(const char* name, int nStates, bool framesAreHorizontal, int targetScale) override;
129 void ReleaseBitmap(const IBitmap& bitmap) override { }; // NO-OP
130 void RetainBitmap(const IBitmap& bitmap, const char * cacheName) override { }; // NO-OP
131 bool BitmapExtSupported(const char* ext) override;
132
133 void DeleteFBO(NVGframebuffer* pBuffer);
134
135protected:
136 APIBitmap* LoadAPIBitmap(const char* fileNameOrResID, int scale, EResourceLocation location, const char* ext) override;
137 APIBitmap* LoadAPIBitmap(const char* name, const void* pData, int dataSize, int scale) override;
138 APIBitmap* CreateAPIBitmap(int width, int height, float scale, double drawScale, bool cacheable = false) override;
139
140 bool LoadAPIFont(const char* fontID, const PlatformFontPtr& font) override;
141
142 int AlphaChannel() const override { return 3; }
143
144 bool FlippedBitmap() const override
145 {
146#if defined(IGRAPHICS_GL)
147 return true;
148#else
149 return false;
150#endif
151 }
152
153 void GetLayerBitmapData(const ILayerPtr& layer, RawBitmapData& data) override;
154 void ApplyShadowMask(ILayerPtr& layer, RawBitmapData& mask, const IShadow& shadow) override;
155
156 float DoMeasureText(const IText& text, const char* str, IRECT& bounds) const override;
157 void DoDrawText(const IText& text, const char* str, const IRECT& bounds, const IBlend* pBlend) override;
158
159private:
160 void PrepareAndMeasureText(const IText& text, const char* str, IRECT& r, double& x, double & y) const;
161 void PathTransformSetMatrix(const IMatrix& m) override;
162 void SetClipRegion(const IRECT& r) override;
163 void UpdateLayer() override;
164 void ClearFBOStack();
165
166 bool mInDraw = false;
167 WDL_Mutex mFBOMutex;
168 std::stack<NVGframebuffer*> mFBOStack; // A stack of FBOs that requires freeing at the end of the frame
169 StaticStorage<APIBitmap> mBitmapCache; //not actually static (doesn't require retaining or releasing)
170 NVGcontext* mVG = nullptr;
171 NVGframebuffer* mMainFrameBuffer = nullptr;
172 int mInitialFBO = 0;
173};
174
175END_IGRAPHICS_NAMESPACE
176END_IPLUG_NAMESPACE
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
IGraphics draw class using NanoVG
const char * GetDrawingAPIStr() 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...
void DrawResize() override
Called to update the drawing surface after a resize.
void PathClear() override
Clear the stack of path drawing commands.
IBitmap LoadBitmap(const char *name, int nStates, bool framesAreHorizontal, int targetScale) override
Load a bitmap image from disk or from windows resource.
void PathFill(const IPattern &pattern, const IFillOptions &options, const IBlend *pBlend) override
Fill the current current path.
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 DrawDottedRect(const IColor &color, const IRECT &bounds, const IBlend *pBlend, float thickness, float dashLen) override
Draw a dotted rectangle to the graphics context.
void PathSetWinding(bool clockwise) override
NanoVG only.
APIBitmap * LoadAPIBitmap(const char *fileNameOrResID, int scale, EResourceLocation location, const char *ext) override
Drawing API method to load a bitmap, called internally.
void ReleaseBitmap(const IBitmap &bitmap) override
Releases an IBitmap from the cache/static storage.
void PathStroke(const IPattern &pattern, float thickness, const IStrokeOptions &options, const IBlend *pBlend) override
Stroke the current current path.
void PathArc(float cx, float cy, float r, float a1, float a2, EWinding winding) override
Add an arc to the current path.
void PathLineTo(float x, float y) override
Add a line to the current path from the current point to the specified location.
void OnViewDestroyed() override
Called after a platform view is destroyed, so that drawing classes can e.g.
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 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.
void * GetDrawContext() override
Gets a void pointer to underlying drawing context, for the IGraphics backend See draw class implement...
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.
IColor GetPoint(int x, int y) override
Get the color at an X, Y location in the graphics context.
bool LoadAPIFont(const char *fontID, const PlatformFontPtr &font) override
Drawing API method to load a font from a PlatformFontPtr, called internally.
void PathClose() override
Close the path that is being specified.
void DoDrawText(const IText &text, const char *str, const IRECT &bounds, const IBlend *pBlend) override
Internal method to draw text.
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.
void PathMoveTo(float x, float y) override
Move the current point in the current path.
void OnViewInitialized(void *pContext) override
Called after platform view initialization, so that drawing classes can e.g.
void BeginFrame() override
Called at the beginning of drawing.
void EndFrame() override
Called by some drawing API classes to finally blit the draw bitmap onto the screen or perform other c...
int AlphaChannel() const 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 RetainBitmap(const IBitmap &bitmap, const char *cacheName) override
Adds an IBitmap to the cache/static storage.
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.
bool BitmapExtSupported(const char *ext) override
Checks a file extension and reports whether this drawing API supports loading that extension.
float DoMeasureText(const IText &text, const char *str, IRECT &bounds) const override
Internal method to measure text dimensions.
bool FlippedBitmap() const override
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.
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,...