From ceefcca7ad4cf8dee0dd1344b22b90d6e2344973 Mon Sep 17 00:00:00 2001 From: Zhiyi Zhang Date: Thu, 11 Apr 2024 10:00:47 +0800 Subject: [PATCH] winemac.drv: Update OpenGL context immediately after the window content view is visible. Fix errors such as GL_FRAMEBUFFER_UNDEFINED and GL_INVALID_FRAMEBUFFER_OPERATION for OpenGL functions when the default framebuffer 0 is bound on macOS. These errors happen because the NSOpenGLContext doesn't have a view bound at the time of an OpenGL call. The view could not be set for the NSOpenGLContext because the window or view can still be invisible. Setting an invisible view for the NSOpenGLContext can generate invalid drawable messages as 682ed910 has shown. Thus setting the view could be deferred because the NSOpenGLContext needs a visible view. However, right after the window becomes visible, an OpenGL function that involves the default framebuffer can be called. And when the view is still not set at the time of the OpenGL call, errors such as GL_FRAMEBUFFER_UNDEFINED and GL_INVALID_FRAMEBUFFER_OPERATION could happen and result in rendering errors such as black screen. So we need to set the view for the NSOpenGLContext as soon as the view becomes visible. It's possible that the window and view are still invisible when OpenGL functions involving the default framebuffer get called. In such cases, I think errors like GL_FRAMEBUFFER_UNDEFINED are justified on macOS. Fix Active Trader Pro black screen at launch on macOS. The application creates a d3d9 device with an invisible window. And then it shows the window before calling d3d9_swapchain_Present(). So all the [NSOpenGLContext setView] opportunities are missed and no view is set. Then when glBlitFramebuffer() gets called, GL_FRAMEBUFFER_UNDEFINED happens and so nothing is rendered. The application only renders one frame before login so the window remains black. --- dlls/opengl32/tests/opengl.c | 1 - dlls/winemac.drv/cocoa_opengl.m | 7 ++++++- dlls/winemac.drv/cocoa_window.m | 1 + 3 files changed, 7 insertions(+), 2 deletions(-) diff --git a/dlls/opengl32/tests/opengl.c b/dlls/opengl32/tests/opengl.c index 7647ae6aa11..ebeefffb72c 100644 --- a/dlls/opengl32/tests/opengl.c +++ b/dlls/opengl32/tests/opengl.c @@ -1415,7 +1415,6 @@ static void test_framebuffer(void) pglBindFramebuffer(GL_FRAMEBUFFER, 0); status = pglCheckFramebufferStatus(GL_FRAMEBUFFER); - todo_wine_if(status == GL_FRAMEBUFFER_UNDEFINED) /* macOS */ ok(status == GL_FRAMEBUFFER_COMPLETE, "Expected %#x, got %#x.\n", GL_FRAMEBUFFER_COMPLETE, status); ret = wglMakeCurrent(NULL, NULL); diff --git a/dlls/winemac.drv/cocoa_opengl.m b/dlls/winemac.drv/cocoa_opengl.m index 31a1f28970b..fa69be7d927 100644 --- a/dlls/winemac.drv/cocoa_opengl.m +++ b/dlls/winemac.drv/cocoa_opengl.m @@ -83,7 +83,12 @@ macdrv_set_view_backing_size((macdrv_view)self.view, view_backing); NSView* save = self.view; - OnMainThread(^{ + if ([NSThread isMainThread]) + { + [super clearDrawable]; + [super setView:save]; + } + else OnMainThread(^{ [super clearDrawable]; [super setView:save]; }); diff --git a/dlls/winemac.drv/cocoa_window.m b/dlls/winemac.drv/cocoa_window.m index 1655ea98ef7..9d2a53c60ae 100644 --- a/dlls/winemac.drv/cocoa_window.m +++ b/dlls/winemac.drv/cocoa_window.m @@ -570,6 +570,7 @@ static CVReturn WineDisplayLinkCallback(CVDisplayLinkRef displayLink, const CVTi clearedGlSurface = TRUE; } context.needsUpdate = TRUE; + macdrv_update_opengl_context(context); } [glContexts addObjectsFromArray:pendingGlContexts]; [pendingGlContexts removeAllObjects]; -- 2.11.4.GIT