Hi all,
I’ve created the following very simple test case simulating what would happen if I draw a meter. It is basically drawing one part of the window with one colour and the other part with another colour. The timerCallback() is updating the “level” and repainting the window.
#pragma once
#include <JuceHeader.h>
class MainComponent : public juce::Component, private juce::Timer
{
public:
//==============================================================================
MainComponent();
~MainComponent() override;
//==============================================================================
void paint (juce::Graphics&) override;
void resized() override;
void timerCallback() override;
private:
// optional
// juce::OpenGLContext openGLContext;
juce::Image onImage, offImage;
int level {0};
JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (MainComponent)
};
//==============================================================================
MainComponent::MainComponent()
{
// optional
// openGLContext.attachTo(*this);
setOpaque(true);
setSize (600, 400);
startTimerHz(60);
}
MainComponent::~MainComponent()
{
// optional
// openGLContext.detach();
stopTimer();
}
//==============================================================================
void MainComponent::paint (juce::Graphics& g)
{
auto w = getWidth();
auto onImageHeight = getHeight() - level;
// VERSION 1:
// g.drawImage(offImage, 0, 0, w, level, 0, 0, w, level);
// g.drawImage(onImage, 0, level, w, onImageHeight, 0, level, w, onImageHeight);
// =======================================================
// VERSION 2:
g.setColour(juce::Colours::sandybrown);
g.fillRect(0, 0, w, level);
g.setColour(juce::Colours::darkgrey);
g.fillRect(0, level, w, onImageHeight);
}
void MainComponent::resized()
{
level = 0;
onImage = juce::Image(juce::Image::ARGB, getWidth(), getHeight(), true, juce::OpenGLImageType());
juce::Graphics gOn(onImage);
gOn.fillAll(juce::Colours::sandybrown);
offImage = juce::Image(juce::Image::ARGB, getWidth(), getHeight(), true, juce::OpenGLImageType());
juce::Graphics gOff(offImage);
gOff.fillAll(juce::Colours::darkgrey);
}
void MainComponent::timerCallback() {
if (++level > getHeight())
level = 0;
repaint();
}
-
Why is Version1 (drawing an
Image
created inresized()
) significantly slower than Version2 (directly drawing inpaint()
). Sure, in that example, it doesn’t make any difference which solution to use. But what if I have a more complex Image to draw which would be expensive to render in everypaint()
call? -
using
openGLContext.attachTo(*this)
brings CPU usage down by a lot in Version2 but does nothing in Version1. Is there something special to consider when using anOpenGLContext
with anImage
?
Thanks
Stefan