-
-
Notifications
You must be signed in to change notification settings - Fork 99
Expand file tree
/
Copy pathNVGSurface.cpp
More file actions
527 lines (442 loc) · 15.2 KB
/
NVGSurface.cpp
File metadata and controls
527 lines (442 loc) · 15.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
/*
// Copyright (c) 2021-2025 Timothy Schoen and Alex Mitchell
// For information on usage and redistribution, and for a DISCLAIMER OF ALL
// WARRANTIES, see the file, "LICENSE.txt," in this distribution.
*/
#include <juce_gui_basics/juce_gui_basics.h>
#include <juce_opengl/juce_opengl.h>
#include <BinaryData.h>
using namespace juce::gl;
#include <nanovg.h>
#ifdef NANOVG_GL_IMPLEMENTATION
# include <nanovg_gl.h>
# include <nanovg_gl_utils.h>
#if JUCE_LINUX || JUCE_BSD
void nvgluSetCornerRadius(float radius, bool left, bool right);
#endif
#endif
#include "NVGSurface.h"
#include "Utility/NVGGraphicsContext.h"
#include "PluginEditor.h"
#include "PluginProcessor.h"
#define ENABLE_FPS_COUNT 0
class FrameTimer {
public:
FrameTimer()
{
startTime = getNow();
prevTime = startTime;
}
void render(NVGcontext* nvg, int const width, int const height, float const scale)
{
nvgBeginFrame(nvg, width, height, scale);
nvgFillColor(nvg, nvgRGBA(40, 40, 40, 255));
nvgFillRect(nvg, 0, 0, 40, 22);
nvgFontSize(nvg, 20.0f);
nvgTextAlign(nvg, NVG_ALIGN_LEFT | NVG_ALIGN_TOP);
nvgFillColor(nvg, nvgRGBA(240, 240, 240, 255));
StackArray<char, 16> fpsBuf;
snprintf(fpsBuf.data(), 16, "%d", static_cast<int>(round(1.0f / getAverageFrameTime())));
nvgText(nvg, 7, 2, fpsBuf.data(), nullptr);
nvgGlobalScissor(nvg, 0, 0, 40 * scale, 22 * scale);
nvgEndFrame(nvg);
}
void addFrameTime()
{
auto const timeSeconds = getTime();
auto const dt = timeSeconds - prevTime;
perf_head = (perf_head + 1) % 32;
frame_times[perf_head] = dt;
prevTime = timeSeconds;
}
double getTime() const { return getNow() - startTime; }
private:
static double getNow()
{
auto const ticks = Time::getHighResolutionTicks();
return Time::highResolutionTicksToSeconds(ticks);
}
float getAverageFrameTime() const
{
float avg = 0;
for (int i = 0; i < 32; i++) {
avg += frame_times[i];
}
return avg / static_cast<float>(32);
}
float frame_times[32] = { };
int perf_head = 0;
double startTime = 0, prevTime = 0;
};
NVGSurface::NVGSurface(PluginEditor* e)
: editor(e)
{
#ifdef NANOVG_GL_IMPLEMENTATION
glContext = std::make_unique<OpenGLContext>();
auto pixelFormat = OpenGLPixelFormat(8, 8, 16, 8);
glContext->setPixelFormat(pixelFormat);
# ifdef NANOVG_GL3_IMPLEMENTATION
glContext->setOpenGLVersionRequired(OpenGLContext::OpenGLVersion::openGL3_2);
# endif
glContext->setSwapInterval(0);
#endif
#if ENABLE_FPS_COUNT
frameTimer = std::make_unique<FrameTimer>();
#endif
setInterceptsMouseClicks(false, false);
setWantsKeyboardFocus(false);
setSize(1, 1);
backupImageComponent.setInterceptsMouseClicks(false, false);
editor->addChildComponent(backupImageComponent);
// Start rendering asynchronously, so we are sure the window has been added to the desktop
// kind of a hack, but works well enough
MessageManager::callAsync([_this = SafePointer(this)] {
if (_this) {
_this->vBlankAttachment = std::make_unique<VBlankAttachment>(_this.getComponent(), [_this]() {
_this->render();
});
}
});
}
NVGSurface::~NVGSurface()
{
detachContext();
}
void NVGSurface::initialise()
{
#ifdef NANOVG_METAL_IMPLEMENTATION
auto* peer = getPeer()->getNativeHandle();
auto* view = OSUtils::MTLCreateView(peer, 0, 0, getWidth(), getHeight());
setView(view);
setVisible(true);
lastRenderScale = calculateRenderScale();
nvg = nvgCreateContext(view, 0, getWidth() * lastRenderScale, getHeight() * lastRenderScale);
#else
setVisible(true);
glContext->attachTo(*this);
glContext->initialiseOnThread();
glContext->makeActive();
lastRenderScale = calculateRenderScale();
nvg = nvgCreateContext(0);
#endif
if (!nvg) {
static bool isShowingMessageBox = false;
if (!isShowingMessageBox) {
isShowingMessageBox = true;
std::cerr << "could not initialise nvg" << std::endl;
AlertWindow::showMessageBoxAsync(MessageBoxIconType::WarningIcon,
"Could not initialize plugdata",
"Please check that you have up-to-date graphics drivers installed. At least OpenGL 3.0 support is required to run plugdata.",
"OK",
nullptr,
nullptr);
}
return;
}
#if JUCE_LINUX || JUCE_BSD
nvgluSetCornerRadius(12.0f * lastRenderScale, roundedRight, roundedRight);
#endif
surfaces[nvg] = this;
nvgAtlasTextThreshold(nvg, 32.0f);
nvgCreateFontMem(nvg, "Inter-Regular", BinaryData::getResourceCopy(BinaryData::InterRegular_ttf), BinaryData::getResourceSize(BinaryData::InterRegular_ttf), 0);
nvgCreateFontMem(nvg, "Inter-Bold", BinaryData::getResourceCopy(BinaryData::InterBold_ttf), BinaryData::getResourceSize(BinaryData::InterBold_ttf), 0);
nvgCreateFontMem(nvg, "Inter-Tabular", BinaryData::getResourceCopy(BinaryData::InterTabular_ttf), BinaryData::getResourceSize(BinaryData::InterTabular_ttf), 0);
nvgCreateFontMem(nvg, "icon_font-Regular", BinaryData::getResourceCopy(BinaryData::IconFont_ttf), BinaryData::getResourceSize(BinaryData::IconFont_ttf), 0);
updateWindowContextVisibility();
}
void NVGSurface::updateWindowContextVisibility()
{
if (renderThroughImage == isRenderingThroughImage)
return;
isRenderingThroughImage = renderThroughImage;
// Render a frame to the new target before showing/hiding GPU context
renderAll();
#ifdef NANOVG_GL_IMPLEMENTATION
if (glContext)
glContext->setVisible(!renderThroughImage);
#else
if (auto* view = getView()) {
OSUtils::MTLSetVisible(view, !renderThroughImage);
}
#endif
invalidateAll();
}
void NVGSurface::detachContext()
{
if (makeContextActive()) {
editor->getNanoLLGC()->removeCachedImages();
NVGFramebuffer::clearAll(nvg);
NVGImage::clearAll(nvg);
NVGCachedPath::clearAll(nvg);
if (invalidFBO) {
nvgDeleteFramebuffer(invalidFBO);
invalidFBO = nullptr;
}
if (nvg) {
nvgDeleteContext(nvg);
nvg = nullptr;
surfaces.erase(nvg);
}
#ifdef NANOVG_METAL_IMPLEMENTATION
if (auto* view = getView()) {
OSUtils::MTLDeleteView(view);
setView(nullptr);
}
#else
glContext->detach();
#endif
isRenderingThroughImage = false;
}
}
void NVGSurface::updateBufferSize()
{
float const pixelScale = getRenderScale();
int const scaledWidth = std::max<int>(getWidth() * pixelScale, 1);
int const scaledHeight = std::max<int>(getHeight() * pixelScale, 1);
if (fbWidth != scaledWidth || fbHeight != scaledHeight || !invalidFBO) {
if (invalidFBO)
nvgDeleteFramebuffer(invalidFBO);
invalidFBO = nvgCreateFramebuffer(nvg, scaledWidth, scaledHeight, 0);
fbWidth = scaledWidth;
fbHeight = scaledHeight;
invalidArea = getLocalBounds();
}
}
void NVGSurface::lookAndFeelChanged()
{
if (makeContextActive()) {
NVGFramebuffer::clearAll(nvg);
NVGImage::clearAll(nvg);
invalidateAll();
}
}
bool NVGSurface::makeContextActive()
{
#ifdef NANOVG_METAL_IMPLEMENTATION
// No need to make context active with Metal, so just check if we have initialised and return that
return getView() != nullptr && nvg != nullptr && mnvgDevice(nvg) != nullptr;
#else
if (glContext && glContext->makeActive()) {
if (renderThroughImage)
updateWindowContextVisibility();
return true;
}
return false;
#endif
}
float NVGSurface::calculateRenderScale() const
{
#ifdef NANOVG_METAL_IMPLEMENTATION
return OSUtils::MTLGetPixelScale(getView()) * Desktop::getInstance().getGlobalScaleFactor();
#else
return glContext->getRenderingScale();
#endif
}
float NVGSurface::getRenderScale() const
{
return lastRenderScale;
}
void NVGSurface::updateBounds(Rectangle<int> const bounds)
{
currentBounds = bounds;
#if JUCE_LINUX || JUCE_BSD
// Directly setting bounds gives the best result on Linux
setBounds(bounds);
#endif
#ifdef NANOVG_GL_IMPLEMENTATION
updateWindowContextVisibility();
#endif
invalidateAll();
}
void NVGSurface::resized()
{
#if JUCE_LINUX || JUCE_BSD
Path clipPath;
clipPath.addRoundedRectangle(0, 0, getWidth(), getHeight(), Corners::windowCornerRadius, Corners::windowCornerRadius, roundedLeft, roundedRight, roundedLeft, roundedRight);
backupImageComponent.setClipPath(clipPath);
#endif
backupImageComponent.setBounds(editor->getLocalArea(this, getLocalBounds()));
invalidateAll();
}
void NVGSurface::invalidateAll()
{
invalidArea = invalidArea.getUnion(getLocalBounds());
}
void NVGSurface::invalidateArea(Rectangle<int> const area)
{
invalidArea = invalidArea.getUnion(area);
}
void NVGSurface::renderAll()
{
invalidateAll();
render();
}
void NVGSurface::render()
{
if (renderThroughImage) {
auto const startTime = Time::getMillisecondCounter();
if (startTime - lastRenderTime < 32) {
return; // When rendering through juce::image, limit framerate to 30 fps
}
lastRenderTime = startTime;
}
if (!getPeer()) {
return;
}
// Do this right before rendering, so that it doesn't show a frame with the last rendered content skewed to the new view size
if (getBounds() != currentBounds) {
setBounds(currentBounds);
}
#if JUCE_LINUX
if (skipFrame) {
// On Linux, there is a strange bug where repaints will be executed immediately if the last repaint took too long
// This will then completely occupy the message thread, leaving no space for handling interaction...
// So if our rendering took too long, we need to manually skip a frame
skipFrame = false;
return;
}
auto start = Time::getMillisecondCounter();
#endif
if (!nvg) {
initialise();
if (!nvg) {
return;
}
}
if (!makeContextActive()) {
return;
}
auto pixelScale = calculateRenderScale();
auto const desktopScale = Desktop::getInstance().getGlobalScaleFactor();
auto const devicePixelScale = pixelScale / desktopScale;
if (std::abs(lastRenderScale - pixelScale) > 0.1f) {
detachContext();
initialise();
return; // Render on next frame
}
#if NANOVG_METAL_IMPLEMENTATION
if (pixelScale == 0) // This happens sometimes when an AUv3 plugin is hidden behind the parameter control view
return;
#else
auto viewWidth = getWidth() * pixelScale;
auto viewHeight = getHeight() * pixelScale;
// In case we apply a global scale factor, calculate the necessary fractional offset for glViewport size
if (!approximatelyEqual(desktopScale, 1.0f)) {
auto desktopScaleOffsetX = getWidth() * desktopScale;
auto desktopScaleOffsetY = getHeight() * desktopScale;
viewWidth += (std::ceil(desktopScaleOffsetX) - desktopScaleOffsetX) * devicePixelScale;
viewHeight += (std::ceil(desktopScaleOffsetY) - desktopScaleOffsetY) * devicePixelScale;
}
#endif
#if ENABLE_FPS_COUNT
frameTimer->addFrameTime();
#endif
updateBufferSize();
invalidArea = invalidArea.getIntersection(getLocalBounds());
for (auto bufferedObject : bufferedObjects) {
if (bufferedObject)
bufferedObject->updateFramebuffers(nvg);
}
if (!invalidArea.isEmpty()) {
// Draw only the invalidated region on top of framebuffer
nvgBindFramebuffer(invalidFBO);
#ifdef NANOVG_GL_IMPLEMENTATION
nvgViewport(0, 0, viewWidth, viewHeight);
#endif
nvgBeginFrame(nvg, getWidth() * desktopScale, getHeight() * desktopScale, devicePixelScale);
nvgScale(nvg, desktopScale, desktopScale);
editor->renderArea(nvg, invalidArea);
nvgGlobalScissor(nvg, invalidArea.getX() * pixelScale, invalidArea.getY() * pixelScale, invalidArea.getWidth() * pixelScale, invalidArea.getHeight() * pixelScale);
nvgEndFrame(nvg);
#if ENABLE_FPS_COUNT
frameTimer->render(nvg, getWidth(), getHeight(), pixelScale);
#endif
if (renderThroughImage) {
renderFrameToImage(backupRenderImage, invalidArea);
} else {
needsBufferSwap = true;
}
invalidArea = Rectangle<int>(0, 0, 0, 0);
}
if (needsBufferSwap) {
blitToScreen();
needsBufferSwap = false;
}
#if JUCE_LINUX
auto* display = Desktop::getInstance().getDisplays().getPrimaryDisplay();
auto refreshRate = display ? display->verticalFrequencyHz.value_or(60.0) : 60.0;
if (Time::getMillisecondCounter() - start > (1000 / refreshRate)) {
skipFrame = true;
}
#endif
}
void NVGSurface::blitToScreen()
{
if (!makeContextActive() || !invalidFBO) {
return;
}
auto pixelScale = calculateRenderScale();
#if NANOVG_METAL_IMPLEMENTATION
auto const devicePixelScale = pixelScale / Desktop::getInstance().getGlobalScaleFactor();
auto viewWidth = getWidth() * devicePixelScale;
auto viewHeight = getHeight() * devicePixelScale;
if (auto* view = getView()) {
mnvgSetViewBounds(view, getWidth() * pixelScale, getHeight() * pixelScale);
}
#else
auto viewWidth = getWidth() * pixelScale;
auto viewHeight = getHeight() * pixelScale;
#endif
nvgBindFramebuffer(nullptr);
nvgBlitFramebuffer(nvg, invalidFBO, 0, 0, viewWidth, viewHeight);
#ifdef NANOVG_GL_IMPLEMENTATION
glContext->swapBuffers();
#endif
}
#if JUCE_LINUX || JUCE_BSD
void NVGSurface::setRoundedBottomCorners(bool left, bool right)
{
roundedLeft = left;
roundedRight = right;
nvgluSetCornerRadius(12.0f * lastRenderScale, roundedLeft, roundedRight);
}
#endif
void NVGSurface::renderFrameToImage(Image& image, Rectangle<int> const area)
{
nvgBindFramebuffer(nullptr);
if (!image.isValid() || image.getWidth() != fbWidth || image.getHeight() != fbHeight) {
image = Image(Image::PixelFormat::ARGB, fbWidth, fbHeight, true);
}
Image::BitmapData imageData(image, Image::BitmapData::writeOnly);
auto region = (area.getIntersection(getLocalBounds()).toFloat() * getRenderScale()).getSmallestIntegerContainer().getIntersection({ fbWidth, fbHeight });
nvgReadPixels(nvg, invalidFBO, 0, region.getY(), fbWidth, region.getHeight(), fbHeight, imageData.getLinePointer(region.getY()));
backupImageComponent.setImage(image);
backupImageComponent.repaint(area);
}
void NVGSurface::setRenderThroughImage(bool const shouldRenderThroughImage)
{
renderThroughImage = shouldRenderThroughImage;
backupImageComponent.setVisible(shouldRenderThroughImage);
if (!shouldRenderThroughImage)
backupRenderImage = Image();
}
NVGSurface* NVGSurface::getSurfaceForContext(NVGcontext* nvg)
{
auto const nvgIter = surfaces.find(nvg);
if (nvgIter != surfaces.end()) {
return nvgIter->second;
}
return nullptr;
}
void NVGSurface::addBufferedObject(NVGComponent* component)
{
bufferedObjects.insert(component);
}
void NVGSurface::removeBufferedObject(NVGComponent* component)
{
bufferedObjects.erase(component);
}
void NVGSurface::handleCommandMessage(int commandID)
{
needsBufferSwap = true;
}