1
0
Fork 0
mirror of https://github.com/juce-framework/JUCE.git synced 2026-01-25 02:04:23 +00:00

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

This commit is contained in:
jules 2013-07-23 15:57:50 +01:00
parent 4d7aaace23
commit 8d05e58bc7

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>