Skip to content

Commit

Permalink
Optimize VU Meter Draw (surge-synthesizer#640)
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

Former-commit-id: 0e650308bda2a949e7c32265c3c534c9252835ec [formerly b51bdd2]
Former-commit-id: 075d28ec6fb51087fe96dcac3aa76def95574427
Former-commit-id: ce8d1c8ea41323ee2fce9dab210c644cbc9de14a
  • Loading branch information
baconpaul authored Feb 23, 2019
1 parent b897218 commit 6511f7f
Show file tree
Hide file tree
Showing 4 changed files with 68 additions and 16 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
37 changes: 24 additions & 13 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 @@ -104,15 +108,15 @@ void CSurgeVuMeter::draw(CDrawContext* dc)

barblack.left = bar.right - 1;
barblack.right++;
dc->setFillColor(kBlackCColor);
dc->drawRect(barblack, kDrawFilled);

if (stereo)
{
CRect midline(lbox);
midline.inset(0, 4);
midline.left++;
midline.top++;
midline.left+=2;
midline.right-=2;
midline.top+=2;
midline.bottom --;
// midline.bottom = midline.top+2;
bar.offset(0, 3);
barblack.offset(0, 3);
Expand All @@ -124,9 +128,16 @@ void CSurgeVuMeter::draw(CDrawContext* dc)

barblack.left = bar.right - 1;
dc->setFillColor(kBlackCColor);
dc->drawRect(barblack, kDrawFilled);
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 @@ -321,9 +321,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 6511f7f

Please sign in to comment.