From a3f81eb2fbf1ba4bb823edd2c90e3986f423b846 Mon Sep 17 00:00:00 2001 From: reuk Date: Thu, 13 Nov 2025 17:28:15 +0000 Subject: [PATCH] OpenGL: Correctly restore blend mode when nested context goes out-of-scope Fixes an issue where primitives such as text could end up with sharp edges when creating temporary contexts: void paint (Graphics& g) { g.fillAll (Colours::white); const auto preferredType = g.getInternalContext().getPreferredImageTypeForTemporaryImages(); Image img (Image::ARGB, getWidth(), getHeight(), false, *preferredType); { Graphics g2 (img); } g.setColour (Colours::black); g.setFont (32); g.drawText ("test", getLocalBounds(), Justification::centred); } --- .../opengl/juce_OpenGLGraphicsContext.cpp | 30 +++++++++++++++++-- 1 file changed, 28 insertions(+), 2 deletions(-) diff --git a/modules/juce_opengl/opengl/juce_OpenGLGraphicsContext.cpp b/modules/juce_opengl/opengl/juce_OpenGLGraphicsContext.cpp index 9289f8acdc..043816f6ab 100644 --- a/modules/juce_opengl/opengl/juce_OpenGLGraphicsContext.cpp +++ b/modules/juce_opengl/opengl/juce_OpenGLGraphicsContext.cpp @@ -978,6 +978,20 @@ struct StateHelpers { BlendingMode() noexcept {} + ~BlendingMode() + { + glBlendFuncSeparate (prevSrcRGB, prevDstRGB, prevSrcAlpha, prevDstAlpha); + + if ((bool) glIsEnabled (GL_BLEND) == prevBlendEnabled) + return; + + if (prevBlendEnabled) + glEnable (GL_BLEND); + else + glDisable (GL_BLEND); + + } + void resync() noexcept { glDisable (GL_BLEND); @@ -1030,8 +1044,20 @@ struct StateHelpers } private: - bool blendingEnabled = false; + static GLenum getBlendEnum (GLenum kind) + { + GLint result{}; + glGetIntegerv (kind, &result); + return static_cast (result); + } + GLenum srcFunction = 0, dstFunction = 0; + GLenum prevSrcAlpha = getBlendEnum (GL_BLEND_SRC_ALPHA); + GLenum prevSrcRGB = getBlendEnum (GL_BLEND_SRC_RGB); + GLenum prevDstAlpha = getBlendEnum (GL_BLEND_DST_ALPHA); + GLenum prevDstRGB = getBlendEnum (GL_BLEND_DST_RGB); + bool blendingEnabled = false; + bool prevBlendEnabled = glIsEnabled (GL_BLEND); }; //============================================================================== @@ -1786,7 +1812,7 @@ private: //============================================================================== struct SavedState final : public RenderingHelpers::SavedStateBase { - using BaseClass = RenderingHelpers::SavedStateBase; + using BaseClass = SavedStateBase; SavedState (GLState* s) : BaseClass (s->target.bounds), state (s) {}