From 51688362e01bf1992c40a82d92f2324063c79a8e Mon Sep 17 00:00:00 2001 From: Paul Date: Thu, 21 Feb 2019 08:57:44 -0500 Subject: [PATCH] Optimize VU Meter Draw The VU Meter was a cause of substantial idle load. In a 30 second timer sample, a full half second was spent drawing background images - as much time as was spent filling up the audio buffer with no sound. There were a couple of key reasons for this: 1. The VU Meter invalidates every idle even if value hasn't changed 2. When the VU meter invalidates even legitimately it forces a full repaint of the background image into a clip rect because that's what VSTGUI::CViewContainer does So - somewhat unsatisfactorily in the second case - this diff fixes those by only invalidating when value changes (which is great for idle) and supressing the background image repaint using knowledge of the placement of the VU meter (which is fragile and unsatisfying but the only option we have given the protection of VSTGUI::CFrame's final status and so on). While at it, replace the pixel painted edges so the VU meeter has a better boundary when zoomed. Closes #639 - Re-Implement VU Meter Addresses parts of #446 and #556; performance and pixely bits --- src/common/gui/CScalableBitmap.cpp | 26 ++++++++++++++++++++++++++ src/common/gui/CSurgeVuMeter.cpp | 28 ++++++++++++++++++++-------- src/common/gui/CSurgeVuMeter.h | 4 ++++ src/common/gui/SurgeGUIEditor.cpp | 17 ++++++++++++++--- 4 files changed, 64 insertions(+), 11 deletions(-) diff --git a/src/common/gui/CScalableBitmap.cpp b/src/common/gui/CScalableBitmap.cpp index 6730e2aa3cf..0f940877344 100644 --- a/src/common/gui/CScalableBitmap.cpp +++ b/src/common/gui/CScalableBitmap.cpp @@ -115,6 +115,32 @@ CScalableBitmap::CScalableBitmap(CResourceDescription desc) void CScalableBitmap::draw (CDrawContext* context, const CRect& rect, const CPoint& offset, float alpha ) { + /* + ** CViewContainer, in the ::drawRect method, no matter what invalidates, calls a drawBackground + ** on the entire background with a clip rectangle applied. This is not normally a problem when + ** you invalidate infrequently or just have a constant color background. But in Surge we have + ** a drawn background on our frame and we invalidate part of our UI every frame because of the + ** vu meter. So every cycle we redraw our entire background into a vu meter sized clip rectangle + ** which is immediately overwritten by a vu meter which fills its entire space. + ** + ** There's no good way out of this. We can't subclass CFrame since it is final (yuch). + ** We cant have the vu meter not invalidate. So we make the bitmap smart. + ** + ** Well, smart. We make it so that if we are redrawing a rectangle which is positioned relative + ** to the background at the same point as the VU Meter. That is, a draw at 763+14 on a background + ** of size 904x542 (ratioed for scaling) gets background supressed. + ** + ** Am I particularly proud of this? No. But it does supress all those draws. + */ + VSTGUI::CRect cl; + context->getClipRect(cl); + float p1 = cl.getTopLeft().x / rect.getWidth(); + float p2 = cl.getTopLeft().y / rect.getHeight(); + if (fabs(p1 - 763.0 / 904.4) < 0.01 && fabs(p2 - 14.0 / 542.0) < 0.01) + { + return; + } + if (lastSeenZoom != currentPhysicalZoomFactor) { int ns = -1; diff --git a/src/common/gui/CSurgeVuMeter.cpp b/src/common/gui/CSurgeVuMeter.cpp index 93b5c5ae23a..42959ae8153 100644 --- a/src/common/gui/CSurgeVuMeter.cpp +++ b/src/common/gui/CSurgeVuMeter.cpp @@ -53,15 +53,19 @@ void CSurgeVuMeter::draw(CDrawContext* dc) CRect size = getViewSize(); CRect lbox(size); - dc->setFrameColor(kBlackCColor); - CRect f1(lbox), f2(lbox); - f1.inset(1, 1); - f2.inset(0, 2); - dc->drawRect(f1); - dc->drawRect(f2); + VSTGUI::CDrawMode origMode = dc->getDrawMode(); + VSTGUI::CDrawMode newMode(VSTGUI::kAntiAliasing); + dc->setDrawMode(newMode); - lbox.right--; - lbox.bottom--; + dc->setFillColor(VSTGUI::CColor(0xCD, 0xCE, 0xD4)); // The light gray from origina-vector skin + dc->drawRect(size, VSTGUI::kDrawFilled); + + CRect rectBox = lbox; + rectBox.inset(1, 1); + VSTGUI::CGraphicsPath* path = dc->createRoundRectGraphicsPath(rectBox, 2); + + dc->setFillColor(kBlackCColor); + dc->drawGraphicsPath(path, VSTGUI::CDrawContext::kPathFilled); CRect bar(lbox); bar.inset(2, 2); @@ -128,5 +132,13 @@ void CSurgeVuMeter::draw(CDrawContext* dc) dc->drawRect(midline, kDrawFilled); } } + + dc->setFrameColor(VSTGUI::CColor(0xA1, 0xA4, 0xB7)); // the dark gray from original vector skin + dc->setLineWidth(1); + dc->drawGraphicsPath(path, VSTGUI::CDrawContext::kPathStroked); + + path->forget(); + dc->setDrawMode(origMode); + setDirty(false); } diff --git a/src/common/gui/CSurgeVuMeter.h b/src/common/gui/CSurgeVuMeter.h index 89de9a18f06..dcde257bf44 100644 --- a/src/common/gui/CSurgeVuMeter.h +++ b/src/common/gui/CSurgeVuMeter.h @@ -21,6 +21,10 @@ class CSurgeVuMeter : public VSTGUI::CControl void setType(int vutype); // void setSecondaryValue(float v); void setValueR(float f); + float getValueR() + { + return valueR; + } bool stereo; private: diff --git a/src/common/gui/SurgeGUIEditor.cpp b/src/common/gui/SurgeGUIEditor.cpp index fa3326cfbb4..728f69a279a 100644 --- a/src/common/gui/SurgeGUIEditor.cpp +++ b/src/common/gui/SurgeGUIEditor.cpp @@ -320,9 +320,20 @@ void SurgeGUIEditor::idle() } } - vu[0]->setValue(synth->vu_peak[0]); - ((CSurgeVuMeter*)vu[0])->setValueR(synth->vu_peak[1]); - vu[0]->invalid(); + bool vuInvalid = false; + if (synth->vu_peak[0] != vu[0]->getValue()) + { + vuInvalid = true; + vu[0]->setValue(synth->vu_peak[0]); + } + if (synth->vu_peak[1] != ((CSurgeVuMeter*)vu[0])->getValueR()) + { + ((CSurgeVuMeter*)vu[0])->setValueR(synth->vu_peak[1]); + vuInvalid = true; + } + if (vuInvalid) + vu[0]->invalid(); + for (int i = 0; i < 8; i++) { assert(i + 1 < Effect::KNumVuSlots);