Skip to content

Commit

Permalink
Optimize VU Meter Draw
Browse files Browse the repository at this point in the history
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 surge-synthesizer#639 - Re-Implement VU Meter
Addresses parts of surge-synthesizer#446 and surge-synthesizer#556; performance and pixely bits
  • Loading branch information
baconpaul committed Feb 21, 2019
1 parent de2e7bd commit 5168836
Show file tree
Hide file tree
Showing 4 changed files with 64 additions and 11 deletions.
26 changes: 26 additions & 0 deletions src/common/gui/CScalableBitmap.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
28 changes: 20 additions & 8 deletions src/common/gui/CSurgeVuMeter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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);
}
4 changes: 4 additions & 0 deletions src/common/gui/CSurgeVuMeter.h
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
17 changes: 14 additions & 3 deletions src/common/gui/SurgeGUIEditor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down

0 comments on commit 5168836

Please sign in to comment.