From 3f5220511c1c738293e22557031bae18dca95cde Mon Sep 17 00:00:00 2001 From: qiuguohua Date: Thu, 14 Sep 2023 08:47:44 +0800 Subject: [PATCH] Fixed multi-threading causing view read failure --- native/cocos/platform/mac/modules/Screen.mm | 6 +++--- native/cocos/platform/mac/modules/SystemWindow.mm | 2 +- native/cocos/renderer/gfx-metal/MTLSwapchain.mm | 7 ------- 3 files changed, 4 insertions(+), 11 deletions(-) diff --git a/native/cocos/platform/mac/modules/Screen.mm b/native/cocos/platform/mac/modules/Screen.mm index e2049c8f135..10a3de9a762 100644 --- a/native/cocos/platform/mac/modules/Screen.mm +++ b/native/cocos/platform/mac/modules/Screen.mm @@ -32,7 +32,7 @@ of this software and associated engine source code (the "Software"), a limited, #include "base/Macros.h" #include "cocos/bindings/jswrapper/SeApi.h" #include "platform/interfaces/modules/ISystemWindowManager.h" -#include "platform/interfaces/modules/ISystemWindow.h" +#include "platform/mac/modules/SystemWindow.h" #include "application/ApplicationManager.h" namespace cc { @@ -52,8 +52,8 @@ of this software and associated engine source code (the "Software"), a limited, global->getProperty("devicePixelRatio", &devicePixelRatioVal); return devicePixelRatioVal.isNumber() ? devicePixelRatioVal.toFloat() : 1.F; #else - auto* window = CC_GET_MAIN_SYSTEM_WINDOW(); - NSWindow* nsWindow = reinterpret_cast(window->getWindowHandle()) ; + auto* systemWindow = static_cast(CC_GET_MAIN_SYSTEM_WINDOW()); + auto* nsWindow = systemWindow->getNSWindow(); return [nsWindow backingScaleFactor]; #endif } diff --git a/native/cocos/platform/mac/modules/SystemWindow.mm b/native/cocos/platform/mac/modules/SystemWindow.mm index 310082a1bfd..4575cf39e41 100644 --- a/native/cocos/platform/mac/modules/SystemWindow.mm +++ b/native/cocos/platform/mac/modules/SystemWindow.mm @@ -66,7 +66,7 @@ of this software and associated documentation files (the "Software"), to deal [view addSubview:newView]; [nsWindow.contentView setWantsBestResolutionOpenGLSurface:YES]; [nsWindow makeKeyAndOrderFront:nil]; - _windowHandle = reinterpret_cast(nsWindow.contentView) ; + _windowHandle = reinterpret_cast(newView); auto dpr = [nsWindow backingScaleFactor]; _width = w * dpr; diff --git a/native/cocos/renderer/gfx-metal/MTLSwapchain.mm b/native/cocos/renderer/gfx-metal/MTLSwapchain.mm index 313f0c33edb..0fbbce7202a 100644 --- a/native/cocos/renderer/gfx-metal/MTLSwapchain.mm +++ b/native/cocos/renderer/gfx-metal/MTLSwapchain.mm @@ -61,13 +61,6 @@ of this software and associated engine source code (the "Software"), a limited, if (!layer.device) { layer.device = MTLCreateSystemDefaultDevice(); } -#elif CC_PLATFORM == CC_PLATFORM_MACOS - CAMetalLayer *layer = nullptr; - auto *view = (CCView *)info.windowHandle; - if(view && view.subviews.count > 0) { - NSView* subView = view.subviews[0]; - layer = static_cast(subView.layer); - } #else auto *view = (CCView *)info.windowHandle; CAMetalLayer *layer = static_cast(view.layer);