Skip to content

Commit

Permalink
OpenGL: Add back CVDisplayLink-driven drawing
Browse files Browse the repository at this point in the history
  • Loading branch information
reuk committed Dec 14, 2022
1 parent 3b2f716 commit f2d0d9c
Showing 1 changed file with 49 additions and 8 deletions.
57 changes: 49 additions & 8 deletions modules/juce_opengl/opengl/juce_OpenGLContext.cpp
Expand Up @@ -148,6 +148,8 @@ class OpenGLContext::CachedImage : public CachedComponentImage
context.nativeContext = nativeContext.get();
else
nativeContext.reset();

refreshDisplayLinkConnection();
}

~CachedImage() override
Expand Down Expand Up @@ -332,7 +334,15 @@ class OpenGLContext::CachedImage : public CachedComponentImage

const auto stateToUse = state.fetch_and (StateFlags::persistent);

if (! isFlagSet (stateToUse, StateFlags::pendingRender) && ! context.continuousRepaint)
#if JUCE_MAC
// On macOS, we use a display link callback to trigger repaints, rather than
// letting them run at full throttle
const auto noAutomaticRepaint = true;
#else
const auto noAutomaticRepaint = ! context.continuousRepaint;
#endif

if (! isFlagSet (stateToUse, StateFlags::pendingRender) && noAutomaticRepaint)
return RenderStatus::noWork;

const auto isUpdating = isFlagSet (stateToUse, StateFlags::paintComponents);
Expand Down Expand Up @@ -916,6 +926,30 @@ class OpenGLContext::CachedImage : public CachedComponentImage
} };
};

void refreshDisplayLinkConnection()
{
#if JUCE_MAC
if (context.continuousRepaint)
{
connection.emplace (sharedDisplayLinks->registerFactory ([this] (CGDirectDisplayID display)
{
return [this, display]
{
if (auto* view = nativeContext->getNSView())
if (auto* window = [view window])
if (auto* screen = [window screen])
if (display == ScopedDisplayLink::getDisplayIdForScreen (screen))
triggerRepaint();
};
}));
}
else
{
connection.reset();
}
#endif
}

//==============================================================================
friend class NativeContext;
std::unique_ptr<NativeContext> nativeContext;
Expand Down Expand Up @@ -1008,6 +1042,10 @@ class OpenGLContext::CachedImage : public CachedComponentImage
// Note: the NSViewComponentPeer also has a SharedResourcePointer<PerScreenDisplayLinks> to
// avoid unnecessarily duplicating display-link threads.
SharedResourcePointer<PerScreenDisplayLinks> sharedDisplayLinks;

// On macOS, rather than letting swapBuffers block as appropriate, we use a display link
// callback to mark the view as needing to repaint.
std::optional<PerScreenDisplayLinks::Connection> connection;
#endif

enum StateFlags
Expand Down Expand Up @@ -1217,13 +1255,16 @@ void OpenGLContext::setContinuousRepainting (bool shouldContinuouslyRepaint) noe
{
continuousRepaint = shouldContinuouslyRepaint;

#if JUCE_MAC
if (auto* component = getTargetComponent())
{
detach();
attachment.reset (new Attachment (*this, *component));
}
#endif
#if JUCE_MAC
if (auto* component = getTargetComponent())
{
detach();
attachment.reset (new Attachment (*this, *component));
}

if (auto* cachedImage = getCachedImage())
cachedImage->refreshDisplayLinkConnection();
#endif

triggerRepaint();
}
Expand Down

0 comments on commit f2d0d9c

Please sign in to comment.