Skip to content

Commit a3f81eb

Browse files
committed
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); }
1 parent 2efd3e0 commit a3f81eb

File tree

1 file changed

+28
-2
lines changed

1 file changed

+28
-2
lines changed

modules/juce_opengl/opengl/juce_OpenGLGraphicsContext.cpp

Lines changed: 28 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -978,6 +978,20 @@ struct StateHelpers
978978
{
979979
BlendingMode() noexcept {}
980980

981+
~BlendingMode()
982+
{
983+
glBlendFuncSeparate (prevSrcRGB, prevDstRGB, prevSrcAlpha, prevDstAlpha);
984+
985+
if ((bool) glIsEnabled (GL_BLEND) == prevBlendEnabled)
986+
return;
987+
988+
if (prevBlendEnabled)
989+
glEnable (GL_BLEND);
990+
else
991+
glDisable (GL_BLEND);
992+
993+
}
994+
981995
void resync() noexcept
982996
{
983997
glDisable (GL_BLEND);
@@ -1030,8 +1044,20 @@ struct StateHelpers
10301044
}
10311045

10321046
private:
1033-
bool blendingEnabled = false;
1047+
static GLenum getBlendEnum (GLenum kind)
1048+
{
1049+
GLint result{};
1050+
glGetIntegerv (kind, &result);
1051+
return static_cast<GLenum> (result);
1052+
}
1053+
10341054
GLenum srcFunction = 0, dstFunction = 0;
1055+
GLenum prevSrcAlpha = getBlendEnum (GL_BLEND_SRC_ALPHA);
1056+
GLenum prevSrcRGB = getBlendEnum (GL_BLEND_SRC_RGB);
1057+
GLenum prevDstAlpha = getBlendEnum (GL_BLEND_DST_ALPHA);
1058+
GLenum prevDstRGB = getBlendEnum (GL_BLEND_DST_RGB);
1059+
bool blendingEnabled = false;
1060+
bool prevBlendEnabled = glIsEnabled (GL_BLEND);
10351061
};
10361062

10371063
//==============================================================================
@@ -1786,7 +1812,7 @@ struct GLState
17861812
//==============================================================================
17871813
struct SavedState final : public RenderingHelpers::SavedStateBase<SavedState>
17881814
{
1789-
using BaseClass = RenderingHelpers::SavedStateBase<SavedState>;
1815+
using BaseClass = SavedStateBase;
17901816

17911817
SavedState (GLState* s) : BaseClass (s->target.bounds), state (s)
17921818
{}

0 commit comments

Comments
 (0)