From b27af5def995947d4561edf8a4edcdabe90c8610 Mon Sep 17 00:00:00 2001 From: reuk Date: Wed, 3 Aug 2022 19:50:16 +0100 Subject: [PATCH] OpenGL: Tidying --- .../juce_opengl/native/juce_OpenGL_win32.h | 66 +++++++++---------- .../juce_opengl/opengl/juce_OpenGLContext.cpp | 16 ++--- 2 files changed, 40 insertions(+), 42 deletions(-) diff --git a/modules/juce_opengl/native/juce_OpenGL_win32.h b/modules/juce_opengl/native/juce_OpenGL_win32.h index f5956df86b..b38a745734 100644 --- a/modules/juce_opengl/native/juce_OpenGL_win32.h +++ b/modules/juce_opengl/native/juce_OpenGL_win32.h @@ -44,13 +44,13 @@ public: PIXELFORMATDESCRIPTOR pfd; initialisePixelFormatDescriptor (pfd, pixelFormat); - auto pixFormat = ChoosePixelFormat (dc, &pfd); + auto pixFormat = ChoosePixelFormat (dc.get(), &pfd); if (pixFormat != 0) - SetPixelFormat (dc, pixFormat, &pfd); + SetPixelFormat (dc.get(), pixFormat, &pfd); - initialiseWGLExtensions (dc); - renderContext = createRenderContext (version, dc); + initialiseWGLExtensions (dc.get()); + renderContext.reset (createRenderContext (version, dc.get())); if (renderContext != nullptr) { @@ -62,20 +62,20 @@ public: if (wglFormat != pixFormat && wglFormat != 0) { // can't change the pixel format of a window, so need to delete the - // old one and create a new one.. - releaseDC(); + // old one and create a new one. + dc.reset(); nativeWindow = nullptr; createNativeWindow (component); - if (SetPixelFormat (dc, wglFormat, &pfd)) + if (SetPixelFormat (dc.get(), wglFormat, &pfd)) { - deleteRenderContext(); - renderContext = createRenderContext (version, dc); + renderContext.reset(); + renderContext.reset (createRenderContext (version, dc.get())); } } if (contextToShareWithIn != nullptr) - wglShareLists ((HGLRC) contextToShareWithIn, renderContext); + wglShareLists ((HGLRC) contextToShareWithIn, renderContext.get()); component.getTopLevelComponent()->repaint(); component.repaint(); @@ -84,8 +84,8 @@ public: ~NativeContext() override { - deleteRenderContext(); - releaseDC(); + renderContext.reset(); + dc.reset(); if (safeComponent != nullptr) if (auto* peer = safeComponent->getTopLevelComponent()->getPeer()) @@ -107,9 +107,9 @@ public: } static void deactivateCurrentContext() { wglMakeCurrent (nullptr, nullptr); } - bool makeActive() const noexcept { return isActive() || wglMakeCurrent (dc, renderContext) != FALSE; } - bool isActive() const noexcept { return wglGetCurrentContext() == renderContext; } - void swapBuffers() const noexcept { SwapBuffers (dc); } + bool makeActive() const noexcept { return isActive() || wglMakeCurrent (dc.get(), renderContext.get()) != FALSE; } + bool isActive() const noexcept { return wglGetCurrentContext() == renderContext.get(); } + void swapBuffers() const noexcept { SwapBuffers (dc.get()); } bool setSwapInterval (int numFramesPerSwap) { @@ -137,7 +137,7 @@ public: } bool createdOk() const noexcept { return getRawContext() != nullptr; } - void* getRawContext() const noexcept { return renderContext; } + void* getRawContext() const noexcept { return renderContext.get(); } unsigned int getFrameBufferID() const noexcept { return 0; } void triggerRepaint() @@ -292,21 +292,8 @@ private: } nativeWindow->setVisible (true); - dc = GetDC ((HWND) nativeWindow->getNativeHandle()); - } - - void deleteRenderContext() - { - if (renderContext != nullptr) - { - wglDeleteContext (renderContext); - renderContext = nullptr; - } - } - - void releaseDC() - { - ReleaseDC ((HWND) nativeWindow->getNativeHandle(), dc); + dc = std::unique_ptr, DeviceContextDeleter> { GetDC ((HWND) nativeWindow->getNativeHandle()), + DeviceContextDeleter { (HWND) nativeWindow->getNativeHandle() } }; } int wglChoosePixelFormatExtension (const OpenGLPixelFormat& pixelFormat) const @@ -351,7 +338,7 @@ private: jassert (n <= numElementsInArray (atts)); UINT formatsCount = 0; - wglChoosePixelFormatARB (dc, atts, nullptr, 1, &format, &formatsCount); + wglChoosePixelFormatARB (dc.get(), atts, nullptr, 1, &format, &formatsCount); } return format; @@ -368,12 +355,23 @@ private: #undef JUCE_DECLARE_WGL_EXTENSION_FUNCTION //============================================================================== + struct RenderContextDeleter + { + void operator() (HGLRC ptr) const { wglDeleteContext (ptr); } + }; + + struct DeviceContextDeleter + { + void operator() (HDC ptr) const { ReleaseDC (hwnd, ptr); } + HWND hwnd; + }; + std::unique_ptr dummyComponent; std::unique_ptr nativeWindow; std::unique_ptr threadAwarenessSetter; Component::SafePointer safeComponent; - HGLRC renderContext; - HDC dc; + std::unique_ptr, RenderContextDeleter> renderContext; + std::unique_ptr, DeviceContextDeleter> dc; OpenGLContext* context = nullptr; double nativeScaleFactor = 1.0; diff --git a/modules/juce_opengl/opengl/juce_OpenGLContext.cpp b/modules/juce_opengl/opengl/juce_OpenGLContext.cpp index 7cb2658c27..79bf388080 100644 --- a/modules/juce_opengl/opengl/juce_OpenGLContext.cpp +++ b/modules/juce_opengl/opengl/juce_OpenGLContext.cpp @@ -1364,7 +1364,7 @@ void OpenGLContext::copyTexture (const Rectangle& targetClipArea, struct OverlayShaderProgram : public ReferenceCountedObject { OverlayShaderProgram (OpenGLContext& context) - : program (context), builder (program), params (program) + : program (context), params (program) {} static const OverlayShaderProgram& select (OpenGLContext& context) @@ -1382,11 +1382,12 @@ void OpenGLContext::copyTexture (const Rectangle& targetClipArea, return *program; } - struct ProgramBuilder + struct BuiltProgram : public OpenGLShaderProgram { - ProgramBuilder (OpenGLShaderProgram& prog) + explicit BuiltProgram (OpenGLContext& context) + : OpenGLShaderProgram (context) { - prog.addVertexShader (OpenGLHelpers::translateVertexShaderToV3 ( + addVertexShader (OpenGLHelpers::translateVertexShaderToV3 ( "attribute " JUCE_HIGHP " vec2 position;" "uniform " JUCE_HIGHP " vec2 screenSize;" "uniform " JUCE_HIGHP " float textureBounds[4];" @@ -1400,7 +1401,7 @@ void OpenGLContext::copyTexture (const Rectangle& targetClipArea, "texturePos = vec2 (texturePos.x, vOffsetAndScale.x + vOffsetAndScale.y * texturePos.y);" "}")); - prog.addFragmentShader (OpenGLHelpers::translateFragmentShaderToV3 ( + addFragmentShader (OpenGLHelpers::translateFragmentShaderToV3 ( "uniform sampler2D imageTexture;" "varying " JUCE_HIGHP " vec2 texturePos;" "void main()" @@ -1408,7 +1409,7 @@ void OpenGLContext::copyTexture (const Rectangle& targetClipArea, "gl_FragColor = texture2D (imageTexture, texturePos);" "}")); - prog.link(); + link(); } }; @@ -1437,8 +1438,7 @@ void OpenGLContext::copyTexture (const Rectangle& targetClipArea, OpenGLShaderProgram::Uniform screenSize, imageTexture, textureBounds, vOffsetAndScale; }; - OpenGLShaderProgram program; - ProgramBuilder builder; + BuiltProgram program; Params params; };