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 #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
64BEGIN_IPLUG_NAMESPACE
65BEGIN_IGRAPHICS_NAMESPACE
66
68NVGcolor NanoVGColor(const IColor& color, const IBlend* pBlend = 0);
69
71void NanoVGRect(NVGcontext* pContext, const IRECT& r);
72
74void NanoVGSetBlendMode(NVGcontext* pContext, const IBlend* pBlend);
75
77NVGpaint NanoVGPaint(NVGcontext* pContext, const IPattern& pattern, const IBlend* pBlend = 0);
78
82{
83private:
84 class Bitmap;
85
86public:
87 IGraphicsNanoVG(IGEditorDelegate& dlg, int w, int h, int fps, float scale);
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 DrawMultiLineText(const IText& text, const char* str, IRECT& bounds, const IBlend* pBlend) override;
106
107 void PathClear() override;
108 void PathClose() override;
109 void PathArc(float cx, float cy, float r, float a1, float a2, EWinding winding) override;
110 void PathMoveTo(float x, float y) override;
111 void PathLineTo(float x, float y) override;
112 void PathCubicBezierTo(float c1x, float c1y, float c2x, float c2y, float x2, float y2) override;
113 void PathQuadraticBezierTo(float cx, float cy, float x2, float y2) override;
114 void PathSetWinding(bool clockwise) override;
115 void PathStroke(const IPattern& pattern, float thickness, const IStrokeOptions& options, const IBlend* pBlend) override;
116 void PathFill(const IPattern& pattern, const IFillOptions& options, const IBlend* pBlend) override;
117
118 IColor GetPoint(int x, int y) override;
119 void* GetDrawContext() override { return (void*) mVG; }
120
121 IBitmap LoadBitmap(const char* name, int nStates, bool framesAreHorizontal, int targetScale) override;
122 void ReleaseBitmap(const IBitmap& bitmap) override { }; // NO-OP
123 void RetainBitmap(const IBitmap& bitmap, const char * cacheName) override { }; // NO-OP
124 bool BitmapExtSupported(const char* ext) override;
125
126 void DeleteFBO(NVGframebuffer* pBuffer);
127
128protected:
129 APIBitmap* LoadAPIBitmap(const char* fileNameOrResID, int scale, EResourceLocation location, const char* ext) override;
130 APIBitmap* LoadAPIBitmap(const char* name, const void* pData, int dataSize, int scale) override;
131 APIBitmap* CreateAPIBitmap(int width, int height, float scale, double drawScale, bool cacheable = false) override;
132
133 bool LoadAPIFont(const char* fontID, const PlatformFontPtr& font) override;
134
135 int AlphaChannel() const override { return 3; }
136
137 bool FlippedBitmap() const override
138 {
139#if defined(IGRAPHICS_GL)
140 return true;
141#else
142 return false;
143#endif
144 }
145
146 void GetLayerBitmapData(const ILayerPtr& layer, RawBitmapData& data) override;
147 void ApplyShadowMask(ILayerPtr& layer, RawBitmapData& mask, const IShadow& shadow) override;
148
149 float DoMeasureText(const IText& text, const char* str, IRECT& bounds) const override;
150 void DoDrawText(const IText& text, const char* str, const IRECT& bounds, const IBlend* pBlend) override;
151
152private:
153 void PrepareAndMeasureText(const IText& text, const char* str, IRECT& r, double& x, double & y) const;
154 void PathTransformSetMatrix(const IMatrix& m) override;
155 void SetClipRegion(const IRECT& r) override;
156 void UpdateLayer() override;
157 void ClearFBOStack();
158
159 bool mInDraw = false;
160 WDL_Mutex mFBOMutex;
161 std::stack<NVGframebuffer*> mFBOStack; // A stack of FBOs that requires freeing at the end of the frame
162 StaticStorage<APIBitmap> mBitmapCache; //not actually static (doesn't require retaining or releasing)
163 NVGcontext* mVG = nullptr;
164 NVGframebuffer* mMainFrameBuffer = nullptr;
165 int mInitialFBO = 0;
166};
167
168END_IGRAPHICS_NAMESPACE
169END_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 for a SOMETHING that uses IGraphics for it's 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
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 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.
void DrawMultiLineText(const IText &text, const char *str, IRECT &bounds, const IBlend *pBlend) override
Draw some multi-line text to the graphics context in a specific rectangle (NanoVG only)
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
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
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,...