Skip to content

Commit 84eed04

Browse files
committed
Component: Add function to clear all cached image resources
In the DemoRunner, switching to and fro between the Settings tab and the Demo tab displaying the OpenGL demo could lead to GL_INVALID_OPERATION errors. This is because closing the demo shuts down the GL context, destroying resources such as framebuffers. If any Image objects backed by framebuffers outlive the context, they will be invalidated. Component effect images are especially likely to hold onto invalid framebuffer references. With this change in place, images cached by Components will be invalidated when the attached GL context goes out of scope, and will be recreated when the new context is created.
1 parent bf54fb1 commit 84eed04

File tree

3 files changed

+18
-2
lines changed

3 files changed

+18
-2
lines changed

modules/juce_gui_basics/components/juce_Component.cpp

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -247,6 +247,11 @@ class Component::EffectState
247247
effect->applyEffect (effectImage, g, scale, ignoreAlphaLevel ? 1.0f : c.getAlpha());
248248
}
249249

250+
void releaseResources()
251+
{
252+
effectImage = {};
253+
}
254+
250255
private:
251256
Image effectImage;
252257
ImageEffectFilter* effect;
@@ -613,6 +618,15 @@ void Component::setBufferedToImage (bool shouldBeBuffered)
613618
}
614619
}
615620

621+
void Component::invalidateCachedImageResources()
622+
{
623+
if (cachedImage != nullptr)
624+
cachedImage->releaseResources();
625+
626+
if (effectState != nullptr)
627+
effectState->releaseResources();
628+
}
629+
616630
//==============================================================================
617631
void Component::reorderChildInternal (int sourceIndex, int destIndex)
618632
{

modules/juce_gui_basics/components/juce_Component.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2504,6 +2504,9 @@ class JUCE_API Component : public MouseListener
25042504
*/
25052505
CachedComponentImage* getCachedComponentImage() const noexcept { return cachedImage.get(); }
25062506

2507+
/** Invalidates cached images, both in the CachedComponentImage (if any) and the image effect state. */
2508+
void invalidateCachedImageResources();
2509+
25072510
/** Sets a flag to indicate whether mouse drag events on this Component should be ignored when it is inside a
25082511
Viewport with drag-to-scroll functionality enabled. This is useful for Components such as sliders that
25092512
should not move when their parent Viewport when dragged.

modules/juce_gui_basics/detail/juce_ComponentHelpers.h

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -237,8 +237,7 @@ struct ComponentHelpers
237237

238238
static void releaseAllCachedImageResources (Component& c)
239239
{
240-
if (auto* cached = c.getCachedComponentImage())
241-
cached->releaseResources();
240+
c.invalidateCachedImageResources();
242241

243242
for (auto* child : c.childComponentList)
244243
releaseAllCachedImageResources (*child);

0 commit comments

Comments
 (0)