From a87efde9ba196eff6e606411789d5875c16c315c Mon Sep 17 00:00:00 2001 From: reuk Date: Tue, 10 Aug 2021 18:59:15 +0100 Subject: [PATCH] OpenGL Demos: Fix Thread Sanitizer warnings --- examples/GUI/OpenGLAppDemo.h | 18 +++++++++-- examples/GUI/OpenGLDemo.h | 58 +++++++++++++++++++++++++----------- 2 files changed, 55 insertions(+), 21 deletions(-) diff --git a/examples/GUI/OpenGLAppDemo.h b/examples/GUI/OpenGLAppDemo.h index 4828dd4301..c2cf967074 100644 --- a/examples/GUI/OpenGLAppDemo.h +++ b/examples/GUI/OpenGLAppDemo.h @@ -83,8 +83,10 @@ public: Matrix3D getProjectionMatrix() const { + const ScopedLock lock (mutex); + auto w = 1.0f / (0.5f + 0.1f); - auto h = w * getLocalBounds().toFloat().getAspectRatio (false); + auto h = w * bounds.toFloat().getAspectRatio (false); return Matrix3D::fromFrustum (-w, w, -h, h, 4.0f, 30.0f); } @@ -109,7 +111,12 @@ public: glEnable (GL_BLEND); glBlendFunc (GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); - glViewport (0, 0, roundToInt (desktopScale * (float) getWidth()), roundToInt (desktopScale * (float) getHeight())); + { + const ScopedLock lock (mutex); + glViewport (0, 0, + roundToInt (desktopScale * (float) bounds.getWidth()), + roundToInt (desktopScale * (float) bounds.getHeight())); + } shader->use(); @@ -124,7 +131,6 @@ public: // Reset the element buffers so child Components draw correctly glBindBuffer (GL_ARRAY_BUFFER, 0); glBindBuffer (GL_ELEMENT_ARRAY_BUFFER, 0); - } void paint (Graphics& g) override @@ -144,6 +150,9 @@ public: // This is called when this component is resized. // If you add any child components, this is where you should // update their positions. + + const ScopedLock lock (mutex); + bounds = getLocalBounds(); } void createShaders() @@ -421,5 +430,8 @@ private: String newVertexShader, newFragmentShader; + Rectangle bounds; + CriticalSection mutex; + JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (OpenGLAppDemo) }; diff --git a/examples/GUI/OpenGLDemo.h b/examples/GUI/OpenGLDemo.h index 6c33b46b46..a962d119ee 100644 --- a/examples/GUI/OpenGLDemo.h +++ b/examples/GUI/OpenGLDemo.h @@ -676,10 +676,6 @@ struct OpenGLUtils g.setColour (Colours::black); g.setFont (40); - const MessageManagerLock mml (ThreadPoolJob::getCurrentThreadPoolJob()); - if (! mml.lockWasGained()) - return false; - g.drawFittedText (String (Time::getCurrentTime().getMilliseconds()), image.getBounds(), Justification::centred, 1); } @@ -816,6 +812,8 @@ public: { using namespace ::juce::gl; + const ScopedLock lock (mutex); + jassert (OpenGLHelpers::isContextActive()); auto desktopScale = (float) openGLContext.getRenderingScale(); @@ -845,7 +843,9 @@ public: glActiveTexture (GL_TEXTURE0); glEnable (GL_TEXTURE_2D); - glViewport (0, 0, roundToInt (desktopScale * (float) getWidth()), roundToInt (desktopScale * (float) getHeight())); + glViewport (0, 0, + roundToInt (desktopScale * (float) bounds.getWidth()), + roundToInt (desktopScale * (float) bounds.getHeight())); texture.bind(); @@ -875,23 +875,25 @@ public: glBindBuffer (GL_ARRAY_BUFFER, 0); glBindBuffer (GL_ELEMENT_ARRAY_BUFFER, 0); - if (! controlsOverlay->isMouseButtonDown()) + if (! controlsOverlay->isMouseButtonDownThreadsafe()) rotation += (float) rotationSpeed; } Matrix3D getProjectionMatrix() const { + const ScopedLock lock (mutex); + auto w = 1.0f / (scale + 0.1f); - auto h = w * getLocalBounds().toFloat().getAspectRatio (false); + auto h = w * bounds.toFloat().getAspectRatio (false); return Matrix3D::fromFrustum (-w, w, -h, h, 4.0f, 30.0f); } Matrix3D getViewMatrix() const { - auto viewMatrix = draggableOrientation.getRotationMatrix() - * Vector3D (0.0f, 1.0f, -10.0f); + const ScopedLock lock (mutex); + auto viewMatrix = draggableOrientation.getRotationMatrix() * Vector3D (0.0f, 1.0f, -10.0f); auto rotationMatrix = Matrix3D::rotation ({ rotation, rotation, -0.3f }); return rotationMatrix * viewMatrix; @@ -912,14 +914,19 @@ public: void resized() override { - controlsOverlay->setBounds (getLocalBounds()); - draggableOrientation.setViewport (getLocalBounds()); + const ScopedLock lock (mutex); + + bounds = getLocalBounds(); + controlsOverlay->setBounds (bounds); + draggableOrientation.setViewport (bounds); } + Rectangle bounds; Draggable3DOrientation draggableOrientation; bool doBackgroundDrawing = false; float scale = 0.5f, rotationSpeed = 0.0f; BouncingNumber bouncingNumber; + CriticalSection mutex; private: void handleAsyncUpdate() override @@ -931,8 +938,8 @@ private: { // Create an OpenGLGraphicsContext that will draw into this GL window.. std::unique_ptr glRenderer (createOpenGLGraphicsContext (openGLContext, - roundToInt (desktopScale * (float) getWidth()), - roundToInt (desktopScale * (float) getHeight()))); + roundToInt (desktopScale * (float) bounds.getWidth()), + roundToInt (desktopScale * (float) bounds.getHeight()))); if (glRenderer.get() != nullptr) { @@ -945,11 +952,11 @@ private: // This stuff just creates a spinning star shape and fills it.. Path p; - p.addStar ({ (float) getWidth() * s.x.getValue(), - (float) getHeight() * s.y.getValue() }, + p.addStar ({ (float) bounds.getWidth() * s.x.getValue(), + (float) bounds.getHeight() * s.y.getValue() }, 7, - (float) getHeight() * size * 0.5f, - (float) getHeight() * size, + (float) bounds.getHeight() * size * 0.5f, + (float) bounds.getHeight() * size, s.angle.getValue()); auto hue = s.hue.getValue(); @@ -957,7 +964,7 @@ private: g.setGradientFill (ColourGradient (Colours::green.withRotatedHue (hue).withAlpha (0.8f), 0, 0, Colours::red.withRotatedHue (hue).withAlpha (0.5f), - 0, (float) getHeight(), false)); + 0, (float) bounds.getHeight(), false)); g.fillPath (p); } } @@ -1071,16 +1078,27 @@ private: tabbedComp.setBounds (shaderArea); } + bool isMouseButtonDownThreadsafe() const { return buttonDown; } + void mouseDown (const MouseEvent& e) override { + const ScopedLock lock (demo.mutex); demo.draggableOrientation.mouseDown (e.getPosition()); + + buttonDown = true; } void mouseDrag (const MouseEvent& e) override { + const ScopedLock lock (demo.mutex); demo.draggableOrientation.mouseDrag (e.getPosition()); } + void mouseUp (const MouseEvent&) override + { + buttonDown = false; + } + void mouseWheelMove (const MouseEvent&, const MouseWheelDetails& d) override { sizeSlider.setValue (sizeSlider.getValue() + d.deltaY); @@ -1149,6 +1167,8 @@ private: private: void sliderValueChanged (Slider*) override { + const ScopedLock lock (demo.mutex); + demo.scale = (float) sizeSlider .getValue(); demo.rotationSpeed = (float) speedSlider.getValue(); } @@ -1208,6 +1228,8 @@ private: std::unique_ptr textureFileChooser; + std::atomic buttonDown { false }; + JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (DemoControlsOverlay) };