Skip to content

Commit

Permalink
Merge pull request #2338 from BOINC/mac_OS10_13_Screensaver_Fix_3
Browse files Browse the repository at this point in the history
[WIP] Mac GFX Library: fix bugs introduced in commit baac677 when rendering…
  • Loading branch information
SETIguy authored Feb 4, 2018
2 parents 39fe2b2 + 32dfd64 commit 9cc3df8
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 34 deletions.
7 changes: 1 addition & 6 deletions api/graphics2.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -88,12 +88,7 @@ bool throttled_app_render(int x, int y, double t) {
boinc_calling_thread_cpu_time(t0);
}

#ifdef __APPLE__
if (UseSharedOffscreenBuffer()) {
MacPrepareOffscreenBuffer();
}
#endif
app_graphics_render(x, y, t);
app_graphics_render(x, y, t);

#ifdef __APPLE__
if (UseSharedOffscreenBuffer()) {
Expand Down
8 changes: 4 additions & 4 deletions api/graphics2_unix.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -115,12 +115,12 @@ static void maybe_render() {
new_ypos = glutGet(GLUT_WINDOW_Y);
new_width = glutGet(GLUT_WINDOW_WIDTH);
new_height = glutGet(GLUT_WINDOW_HEIGHT);


if (throttled_app_render(new_width, new_height, dtime())) {
#ifdef __APPLE__
if (UseSharedOffscreenBuffer()) {
return; // Don't try to send garbage to screen
return; // Don't waste cycles drawing to hidden window on screen
}
#endif
glutSwapBuffers();
Expand Down Expand Up @@ -179,7 +179,7 @@ static void make_window(const char* title) {
#ifdef __APPLE__
glutWMCloseFunc(boinc_close_window_and_quit_aux); // Enable the window's close box
BringAppToFront();
// Show window only after a successful call to throttled_app_render();
// Show window only after a successful call to throttled_app_render();
// this avoids momentary display of old image when screensaver restarts
// which made image appear to "jump."
need_show = true;
Expand Down
58 changes: 35 additions & 23 deletions api/macglutfix.m
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,6 @@ - (void)sendIOSurfaceMachPortToClients: (uint32_t)index withMachPort:(mach_port_

static ServerController *myserverController;

static uint32_t nextFrameIndex;
static uint32_t currentFrameIndex;

static IOSurfaceRef ioSurfaceBuffers[NUM_IOSURFACE_BUFFERS];
Expand Down Expand Up @@ -294,8 +293,13 @@ - (void)sendIOSurfaceMachPortToClients:(uint32_t)index withMachPort:(mach_port_t
@end


void MacPrepareOffscreenBuffer() {
GLuint name, namef;
void MacPassOffscreenBufferToScreenSaver() {
NSOpenGLContext * myContext = [ NSOpenGLContext currentContext ];
NSView *myView = [ myContext view ];
GLsizei w = myView.bounds.size.width;
GLsizei h = myView.bounds.size.height;

GLuint name, namef;

if (!myserverController) {
myserverController = [[[ServerController alloc] init] retain];
Expand All @@ -318,25 +322,20 @@ void MacPrepareOffscreenBuffer() {
}
}

if(!textureNames[nextFrameIndex])
{
NSOpenGLContext * myContext = [ NSOpenGLContext currentContext ];
NSView *myView = [ myContext view ];
GLsizei w = myView.bounds.size.width;
GLsizei h = myView.bounds.size.height;

if(!textureNames[currentFrameIndex])
{
CGLContextObj cgl_ctx = (CGLContextObj)[myContext CGLContextObj];

glGenTextures(1, &name);

glBindTexture(GL_TEXTURE_RECTANGLE, name);
// At the moment, CGLTexImageIOSurface2D requires the GL_TEXTURE_RECTANGLE target
CGLTexImageIOSurface2D(cgl_ctx, GL_TEXTURE_RECTANGLE, GL_RGBA, w, h, GL_BGRA, GL_UNSIGNED_INT_8_8_8_8_REV,
ioSurfaceBuffers[nextFrameIndex], 0);
ioSurfaceBuffers[currentFrameIndex], 0);
glTexParameteri(GL_TEXTURE_RECTANGLE, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_RECTANGLE, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_RECTANGLE, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_RECTANGLE, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_RECTANGLE, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);

// Generate an FBO and bind the texture to it as a render target.
glBindTexture(GL_TEXTURE_RECTANGLE, 0);
Expand All @@ -352,23 +351,36 @@ void MacPrepareOffscreenBuffer() {
}
glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_TEXTURE_RECTANGLE, depthBufferName);

glBindFramebuffer(GL_FRAMEBUFFER, 0);

fboNames[nextFrameIndex] = namef;
textureNames[nextFrameIndex] = name;
}
currentFrameIndex = nextFrameIndex;
nextFrameIndex = (nextFrameIndex + 1) % NUM_IOSURFACE_BUFFERS;
fboNames[currentFrameIndex] = namef;
textureNames[currentFrameIndex] = name;
}

glBindFramebuffer(GL_FRAMEBUFFER, fboNames[currentFrameIndex]);
}
glBindFramebuffer(GL_FRAMEBUFFER, 0); // First, draw to default FBO (screen FBO)

void MacPassOffscreenBufferToScreenSaver() {
glutSwapBuffers();
// To see the original rendering in the graphics app's full-screen window
// for debugging, temporarily enable this "glutSwapBuffers" statement
// glutSwapBuffers(); // FOR DEBUGGING ONLY

// Copy the default FBO to the IOSurface texture's FBO
glBindFramebuffer(GL_DRAW_FRAMEBUFFER, fboNames[currentFrameIndex]);
glBindFramebuffer(GL_READ_FRAMEBUFFER, 0);
glBlitFramebuffer(0,0,w,h, 0,0,w,h, GL_COLOR_BUFFER_BIT, GL_NEAREST);

// To see the contents of the IOSurface in the graphics app's full-screen window
// for debugging, temporarily change "#if 0" to #if 1" bin the next line:
#if 0 // FOR DEBUGGING ONLY
glBindFramebuffer(GL_READ_FRAMEBUFFER, fboNames[currentFrameIndex]);
glBindFramebuffer(GL_DRAW_FRAMEBUFFER, 0);
glBlitFramebuffer(0,0,w,h, 0,0,w,h, GL_COLOR_BUFFER_BIT, GL_NEAREST);
#endif

glutSwapBuffers();
[myserverController sendIOSurfaceMachPortToClients: currentFrameIndex
withMachPort:ioSurfaceMachPorts[currentFrameIndex]];
glFlush();
glBindFramebuffer(GL_FRAMEBUFFER, 0);

currentFrameIndex = (currentFrameIndex + 1) % NUM_IOSURFACE_BUFFERS;
}

// Code for debugging:
Expand Down
1 change: 0 additions & 1 deletion api/x_opengl.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ extern int xwin_glut_is_initialized();

#ifdef __APPLE__
extern void MacGLUTFix(bool isScreenSaver);
extern void MacPrepareOffscreenBuffer(void);
extern void MacPassOffscreenBufferToScreenSaver(void);
extern void BringAppToFront(void);
extern void HideThisApp(void);
Expand Down

0 comments on commit 9cc3df8

Please sign in to comment.