Browse Source

Horrible hack to work around OSX openGL swap intervals not working correctly when the window is occluded.

tags/2021-05-28
jules 12 years ago
parent
commit
8d05e58bc7
1 changed files with 34 additions and 0 deletions
  1. +34
    -0
      modules/juce_opengl/native/juce_OpenGL_osx.h

+ 34
- 0
modules/juce_opengl/native/juce_OpenGL_osx.h View File

@@ -28,6 +28,7 @@ public:
NativeContext (Component& component,
const OpenGLPixelFormat& pixFormat,
void* contextToShare)
: lastSwapTime (0), minSwapTimeMs (0), underrunCounter (0)
{
NSOpenGLPixelFormatAttribute attribs[] =
{
@@ -132,12 +133,16 @@ public:
void swapBuffers()
{
[renderContext flushBuffer];
sleepIfRenderingTooFast();
}
void updateWindowPosition (const Rectangle<int>&) {}
bool setSwapInterval (int numFramesPerSwap)
{
minSwapTimeMs = (numFramesPerSwap * 1000) / 60;
[renderContext setValues: (const GLint*) &numFramesPerSwap
forParameter: NSOpenGLCPSwapInterval];
return true;
@@ -152,9 +157,38 @@ public:
return numFrames;
}
void sleepIfRenderingTooFast()
{
// When our window is entirely occluded by other windows, the system
// fails to correctly implement the swap interval time, so the render
// loop spins at full speed, burning CPU. This hack detects when things
// are going too fast and slows things down if necessary.
if (minSwapTimeMs > 0)
{
const double now = Time::getMillisecondCounterHiRes();
const int elapsed = (int) (now - lastSwapTime);
lastSwapTime = now;
if (isPositiveAndBelow (elapsed, minSwapTimeMs - 3))
{
if (underrunCounter > 3)
Thread::sleep (minSwapTimeMs - elapsed);
else
++underrunCounter;
}
else
{
underrunCounter = 0;
}
}
}
NSOpenGLContext* renderContext;
NSOpenGLView* view;
ReferenceCountedObjectPtr<ReferenceCountedObject> viewAttachment;
double lastSwapTime;
int minSwapTimeMs, underrunCounter;
//==============================================================================
struct MouseForwardingNSOpenGLViewClass : public ObjCClass <NSOpenGLView>


Loading…
Cancel
Save