From 32dfd641567fd0e2e0d658dc8f4e5a68bef7dedf Mon Sep 17 00:00:00 2001 From: Charlie Fenton Date: Sun, 4 Feb 2018 03:21:54 -0800 Subject: [PATCH] Mac GFX Library: fix bugs introduced in commit baac677 when rendering some graphics apps. Project graphics apps for Macintosh should be relinked with libboinc_graphics2.a built using these updated source files. --- api/graphics2.cpp | 7 +---- api/graphics2_unix.cpp | 8 +++--- api/macglutfix.m | 58 +++++++++++++++++++++++++----------------- api/x_opengl.h | 1 - 4 files changed, 40 insertions(+), 34 deletions(-) diff --git a/api/graphics2.cpp b/api/graphics2.cpp index d72d7bd3ac5..1dba6255787 100644 --- a/api/graphics2.cpp +++ b/api/graphics2.cpp @@ -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()) { diff --git a/api/graphics2_unix.cpp b/api/graphics2_unix.cpp index 4659d44155b..eb5fe32ed49 100644 --- a/api/graphics2_unix.cpp +++ b/api/graphics2_unix.cpp @@ -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(); @@ -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; diff --git a/api/macglutfix.m b/api/macglutfix.m index 08070ef6758..36158e6a564 100644 --- a/api/macglutfix.m +++ b/api/macglutfix.m @@ -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]; @@ -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]; @@ -318,13 +322,8 @@ 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); @@ -332,11 +331,11 @@ void MacPrepareOffscreenBuffer() { 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); @@ -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: diff --git a/api/x_opengl.h b/api/x_opengl.h index bb38cee47fe..d82a51c0993 100644 --- a/api/x_opengl.h +++ b/api/x_opengl.h @@ -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);