Browse Source

OpenGL Demos: Fix Thread Sanitizer warnings

v6.1.6
reuk 3 years ago
parent
commit
a87efde9ba
No known key found for this signature in database GPG Key ID: 9ADCD339CFC98A11
2 changed files with 55 additions and 21 deletions
  1. +15
    -3
      examples/GUI/OpenGLAppDemo.h
  2. +40
    -18
      examples/GUI/OpenGLDemo.h

+ 15
- 3
examples/GUI/OpenGLAppDemo.h View File

@@ -83,8 +83,10 @@ public:
Matrix3D<float> 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<float>::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<int> bounds;
CriticalSection mutex;
JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (OpenGLAppDemo)
};

+ 40
- 18
examples/GUI/OpenGLDemo.h View File

@@ -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<float> 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<float>::fromFrustum (-w, w, -h, h, 4.0f, 30.0f);
}
Matrix3D<float> getViewMatrix() const
{
auto viewMatrix = draggableOrientation.getRotationMatrix()
* Vector3D<float> (0.0f, 1.0f, -10.0f);
const ScopedLock lock (mutex);
auto viewMatrix = draggableOrientation.getRotationMatrix() * Vector3D<float> (0.0f, 1.0f, -10.0f);
auto rotationMatrix = Matrix3D<float>::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<int> 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<LowLevelGraphicsContext> 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<FileChooser> textureFileChooser;
std::atomic<bool> buttonDown { false };
JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (DemoControlsOverlay)
};


Loading…
Cancel
Save