MeterHistogram: Small performance improvement on Draw().

This commit is contained in:
spx 2010-11-26 20:22:13 +00:00
parent 78cb8b2543
commit 04208a97c3
2 changed files with 187 additions and 214 deletions

View File

@ -61,11 +61,57 @@ CMeterHistogram::CMeterHistogram(CMeterWindow* meterWindow) : CMeter(meterWindow
*/ */
CMeterHistogram::~CMeterHistogram() CMeterHistogram::~CMeterHistogram()
{ {
if (m_PrimaryBitmap) delete m_PrimaryBitmap; DisposeImage();
if (m_SecondaryBitmap) delete m_SecondaryBitmap; DisposeBuffer();
if (m_BothBitmap) delete m_BothBitmap; }
if (m_PrimaryValues) delete [] m_PrimaryValues;
if (m_SecondaryValues) delete [] m_SecondaryValues; /*
** DisposeImage
**
** Disposes the image buffers.
**
*/
void CMeterHistogram::DisposeImage()
{
if (m_PrimaryBitmap)
{
delete m_PrimaryBitmap;
m_PrimaryBitmap = NULL;
}
if (m_SecondaryBitmap)
{
delete m_SecondaryBitmap;
m_SecondaryBitmap = NULL;
}
if (m_BothBitmap)
{
delete m_BothBitmap;
m_BothBitmap = NULL;
}
}
/*
** DisposeBuffer
**
** Disposes the buffers.
**
*/
void CMeterHistogram::DisposeBuffer()
{
// Reset current position
m_MeterPos = 0;
// Delete buffers
if (m_PrimaryValues)
{
delete [] m_PrimaryValues;
m_PrimaryValues = NULL;
}
if (m_SecondaryValues)
{
delete [] m_SecondaryValues;
m_SecondaryValues = NULL;
}
} }
/* /*
@ -79,6 +125,15 @@ void CMeterHistogram::Initialize()
{ {
CMeter::Initialize(); CMeter::Initialize();
// A sanity check
if (m_SecondaryMeasure && !m_PrimaryImageName.empty() && (m_BothImageName.empty() || m_SecondaryImageName.empty()))
{
LSLog(LOG_DEBUG, APPNAME, L"You need to define SecondaryImage and BothImage also!");
DisposeImage();
}
else
{
// Load the bitmaps if defined // Load the bitmaps if defined
if(!m_PrimaryImageName.empty()) if(!m_PrimaryImageName.empty())
{ {
@ -157,66 +212,19 @@ void CMeterHistogram::Initialize()
m_BothBitmap = NULL; m_BothBitmap = NULL;
} }
} }
// A sanity check
if (m_SecondaryMeasure && !m_PrimaryImageName.empty() && (m_BothImageName.empty() || m_SecondaryImageName.empty()))
{
LSLog(LOG_DEBUG, APPNAME, L"You need to define SecondaryImage and BothImage also!");
if (m_PrimaryBitmap)
{
delete m_PrimaryBitmap;
m_PrimaryBitmap = NULL;
}
if (m_SecondaryBitmap)
{
delete m_SecondaryBitmap;
m_SecondaryBitmap = NULL;
}
if (m_BothBitmap)
{
delete m_BothBitmap;
m_BothBitmap = NULL;
}
} }
if ((!m_PrimaryImageName.empty() && !m_PrimaryBitmap) || if ((!m_PrimaryImageName.empty() && !m_PrimaryBitmap) ||
(!m_SecondaryImageName.empty() && !m_SecondaryBitmap) || (!m_SecondaryImageName.empty() && !m_SecondaryBitmap) ||
(!m_BothImageName.empty() && !m_BothBitmap)) (!m_BothImageName.empty() && !m_BothBitmap))
{ {
// Reset current position DisposeBuffer();
m_MeterPos = 0;
// Delete buffers
if (m_PrimaryValues)
{
delete [] m_PrimaryValues;
m_PrimaryValues = NULL;
}
if (m_SecondaryValues)
{
delete [] m_SecondaryValues;
m_SecondaryValues = NULL;
}
m_WidthChanged = false; m_WidthChanged = false;
} }
else if (m_WidthChanged) else if (m_WidthChanged)
{ {
// Reset current position DisposeBuffer();
m_MeterPos = 0;
// Delete buffers
if (m_PrimaryValues)
{
delete [] m_PrimaryValues;
m_PrimaryValues = NULL;
}
if (m_SecondaryValues)
{
delete [] m_SecondaryValues;
m_SecondaryValues = NULL;
}
// Create buffers for values // Create buffers for values
if (m_W > 0) if (m_W > 0)
@ -350,7 +358,7 @@ bool CMeterHistogram::Update()
{ {
// Go through all values and find the max // Go through all values and find the max
double newValue = 0; double newValue = 0.0;
for (int i = 0; i < m_W; ++i) for (int i = 0; i < m_W; ++i)
{ {
newValue = max(newValue, m_PrimaryValues[i]); newValue = max(newValue, m_PrimaryValues[i]);
@ -409,25 +417,18 @@ bool CMeterHistogram::Draw(Graphics& graphics)
(m_Measure && !m_PrimaryValues) || (m_Measure && !m_PrimaryValues) ||
(m_SecondaryMeasure && !m_SecondaryValues)) return false; (m_SecondaryMeasure && !m_SecondaryValues)) return false;
Pen primaryPen(m_PrimaryColor); GraphicsPath primaryPath;
Pen secondaryPen(m_SecondaryColor); GraphicsPath secondaryPath;
Pen bothPen(m_BothColor); GraphicsPath bothPath;
int x = GetX(); int x = GetX();
int y = GetY(); int y = GetY();
for (int i = 0; i < m_W; ++i) for (int i = 0; i < m_W; ++i)
{ {
double value; double value = (m_MaxPrimaryValue == 0.0) ?
0.0
if (m_MaxPrimaryValue == 0.0) : m_PrimaryValues[(i + m_MeterPos) % m_W] / m_MaxPrimaryValue;
{
value = 0;
}
else
{
value = m_PrimaryValues[(i + m_MeterPos) % m_W] / m_MaxPrimaryValue;
}
value -= m_MinPrimaryValue; value -= m_MinPrimaryValue;
int primaryBarHeight = (int)(m_H * value); int primaryBarHeight = (int)(m_H * value);
primaryBarHeight = min(m_H, primaryBarHeight); primaryBarHeight = min(m_H, primaryBarHeight);
@ -435,14 +436,9 @@ bool CMeterHistogram::Draw(Graphics& graphics)
if (m_SecondaryMeasure != NULL) if (m_SecondaryMeasure != NULL)
{ {
if (m_MaxSecondaryValue == 0.0) value = (m_MaxSecondaryValue == 0.0) ?
{ 0.0
value = 0; : m_SecondaryValues[(i + m_MeterPos) % m_W] / m_MaxSecondaryValue;
}
else
{
value = m_SecondaryValues[(i + m_MeterPos) % m_W] / m_MaxSecondaryValue;
}
value -= m_MinSecondaryValue; value -= m_MinSecondaryValue;
int secondaryBarHeight = (int)(m_H * value); int secondaryBarHeight = (int)(m_H * value);
secondaryBarHeight = min(m_H, secondaryBarHeight); secondaryBarHeight = min(m_H, secondaryBarHeight);
@ -452,114 +448,88 @@ bool CMeterHistogram::Draw(Graphics& graphics)
int bothBarHeight = min(primaryBarHeight, secondaryBarHeight); int bothBarHeight = min(primaryBarHeight, secondaryBarHeight);
// Draw image/color for the both lines // Draw image/color for the both lines
if (m_PrimaryBitmap)
{ {
if (m_Flip) Rect& r = (m_Flip) ?
Rect(x + i, y + bothBarHeight, 1, -bothBarHeight)
: Rect(x + i, y + m_H - bothBarHeight, 1, bothBarHeight);
if (m_BothBitmap)
{ {
Rect r(x + i, y + bothBarHeight, 1, -bothBarHeight);
graphics.DrawImage(m_BothBitmap, r, i, m_H - bothBarHeight, 1, bothBarHeight, UnitPixel); graphics.DrawImage(m_BothBitmap, r, i, m_H - bothBarHeight, 1, bothBarHeight, UnitPixel);
} }
else else
{ {
Rect r(x + i, y + m_H - bothBarHeight, 1, bothBarHeight); bothPath.AddRectangle(r); // cache
graphics.DrawImage(m_BothBitmap, r, i, m_H - bothBarHeight, 1, bothBarHeight, UnitPixel);
}
}
else
{
if (m_Flip)
{
graphics.DrawLine(&bothPen, x + i, y, x + i, y + bothBarHeight);
}
else
{
graphics.DrawLine(&bothPen, x + i, y + m_H, x + i, y + m_H - bothBarHeight);
} }
} }
// Draw the image/color for the rest // Draw the image/color for the rest
if (secondaryBarHeight > primaryBarHeight) if (secondaryBarHeight > primaryBarHeight)
{ {
Rect& r = (m_Flip) ?
Rect(x + i, y + secondaryBarHeight, 1, -(secondaryBarHeight - bothBarHeight))
: Rect(x + i, y + m_H - secondaryBarHeight, 1, secondaryBarHeight - bothBarHeight);
if (m_SecondaryBitmap) if (m_SecondaryBitmap)
{ {
if (m_Flip)
{
Rect r(x + i, y + secondaryBarHeight, 1, -(secondaryBarHeight - bothBarHeight));
graphics.DrawImage(m_SecondaryBitmap, r, i, m_H - secondaryBarHeight, 1, secondaryBarHeight - bothBarHeight, UnitPixel); graphics.DrawImage(m_SecondaryBitmap, r, i, m_H - secondaryBarHeight, 1, secondaryBarHeight - bothBarHeight, UnitPixel);
} }
else else
{ {
Rect r(x + i, y + m_H - secondaryBarHeight, 1, secondaryBarHeight - bothBarHeight); secondaryPath.AddRectangle(r); // cache
graphics.DrawImage(m_SecondaryBitmap, r, i, m_H - secondaryBarHeight, 1, secondaryBarHeight - bothBarHeight, UnitPixel);
}
}
else
{
if (m_Flip)
{
graphics.DrawLine(&secondaryPen, x + i, y + bothBarHeight, x + i, y + secondaryBarHeight);
}
else
{
graphics.DrawLine(&secondaryPen, x + i, y + m_H - bothBarHeight, x + i, y + m_H - secondaryBarHeight);
}
} }
} }
else else
{ {
Rect& r = (m_Flip) ?
Rect(x + i, y + primaryBarHeight, 1, -(primaryBarHeight - bothBarHeight))
: Rect(x + i, y + m_H - primaryBarHeight, 1, primaryBarHeight - bothBarHeight);
if (m_PrimaryBitmap) if (m_PrimaryBitmap)
{ {
if (m_Flip)
{
Rect r(x + i, y + primaryBarHeight, 1, -(primaryBarHeight - bothBarHeight));
graphics.DrawImage(m_PrimaryBitmap, r, i, m_H - primaryBarHeight, 1, primaryBarHeight - bothBarHeight, UnitPixel); graphics.DrawImage(m_PrimaryBitmap, r, i, m_H - primaryBarHeight, 1, primaryBarHeight - bothBarHeight, UnitPixel);
} }
else else
{ {
Rect r(x + i, y + m_H - primaryBarHeight, 1, primaryBarHeight - bothBarHeight); primaryPath.AddRectangle(r); // cache
graphics.DrawImage(m_PrimaryBitmap, r, i, m_H - primaryBarHeight, 1, primaryBarHeight - bothBarHeight, UnitPixel);
}
}
else
{
if (m_Flip)
{
graphics.DrawLine(&primaryPen, x + i, y, x + i, y + primaryBarHeight);
}
else
{
graphics.DrawLine(&primaryPen, x + i, y + m_H - bothBarHeight, x + i, y + m_H - primaryBarHeight);
}
} }
} }
} }
else else
{ {
Rect& r = (m_Flip) ?
Rect(x + i, y + primaryBarHeight, 1, -primaryBarHeight)
: Rect(x + i, y + m_H - primaryBarHeight, 1, primaryBarHeight);
if (m_PrimaryBitmap) if (m_PrimaryBitmap)
{ {
if (m_Flip)
{
Rect r(x + i, y + primaryBarHeight, 1, -primaryBarHeight);
graphics.DrawImage(m_PrimaryBitmap, r, i, m_H - primaryBarHeight, 1, primaryBarHeight, UnitPixel); graphics.DrawImage(m_PrimaryBitmap, r, i, m_H - primaryBarHeight, 1, primaryBarHeight, UnitPixel);
} }
else else
{ {
Rect r(x + i, y + m_H - primaryBarHeight, 1, primaryBarHeight); primaryPath.AddRectangle(r); // cache
graphics.DrawImage(m_PrimaryBitmap, r, i, m_H - primaryBarHeight, 1, primaryBarHeight, UnitPixel);
} }
} }
else }
// Draw cached rectangles
if (m_SecondaryMeasure != NULL)
{ {
if (m_Flip) if (!m_BothBitmap)
{ {
graphics.DrawLine(&primaryPen, x + i, y, x + i, y + primaryBarHeight); SolidBrush brush(m_BothColor);
graphics.FillPath(&brush, &bothPath);
} }
else if (!m_SecondaryBitmap)
{ {
graphics.DrawLine(&primaryPen, x + i, y + m_H, x + i, y + m_H - primaryBarHeight); SolidBrush brush(m_SecondaryColor);
} graphics.FillPath(&brush, &secondaryPath);
} }
} }
if (!m_PrimaryBitmap)
{
SolidBrush brush(m_PrimaryColor);
graphics.FillPath(&brush, &primaryPath);
} }
return true; return true;

View File

@ -35,6 +35,9 @@ public:
virtual void BindMeasure(const std::list<CMeasure*>& measures); virtual void BindMeasure(const std::list<CMeasure*>& measures);
private: private:
void DisposeImage();
void DisposeBuffer();
std::wstring m_SecondaryMeasureName; // Name of the secondary measure std::wstring m_SecondaryMeasureName; // Name of the secondary measure
CMeasure* m_SecondaryMeasure; // Pointer ot the secondary measure CMeasure* m_SecondaryMeasure; // Pointer ot the secondary measure
Gdiplus::Color m_PrimaryColor; // Color of the primary histogram Gdiplus::Color m_PrimaryColor; // Color of the primary histogram