pcsx2/common/CocoaTools.mm
2022-06-04 14:50:33 +01:00

67 lines
2.0 KiB
Plaintext

/* PCSX2 - PS2 Emulator for PCs
* Copyright (C) 2002-2022 PCSX2 Dev Team
*
* PCSX2 is free software: you can redistribute it and/or modify it under the terms
* of the GNU Lesser General Public License as published by the Free Software Found-
* ation, either version 3 of the License, or (at your option) any later version.
*
* PCSX2 is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
* without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
* PURPOSE. See the GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along with PCSX2.
* If not, see <http://www.gnu.org/licenses/>.
*/
#if ! __has_feature(objc_arc)
#error "Compile this with -fobjc-arc"
#endif
#include "CocoaTools.h"
#include "Console.h"
#include "WindowInfo.h"
#include <Cocoa/Cocoa.h>
#include <QuartzCore/QuartzCore.h>
bool CocoaTools::CreateMetalLayer(WindowInfo* wi)
{
if (![NSThread isMainThread])
{
bool ret;
dispatch_sync(dispatch_get_main_queue(), [&ret, wi]{ ret = CreateMetalLayer(wi); });
return ret;
}
CAMetalLayer* layer = [CAMetalLayer layer];
if (!layer)
{
Console.Error("Failed to create Metal layer.");
return false;
}
NSView* view = (__bridge NSView*)wi->window_handle;
[view setWantsLayer:YES];
[view setLayer:layer];
[layer setContentsScale:[[[view window] screen] backingScaleFactor]];
// Store the layer pointer, that way MoltenVK doesn't call [NSView layer] outside the main thread.
wi->surface_handle = (__bridge_retained void*)layer;
return true;
}
void CocoaTools::DestroyMetalLayer(WindowInfo* wi)
{
if (![NSThread isMainThread])
{
dispatch_sync_f(dispatch_get_main_queue(), wi, [](void* ctx){ DestroyMetalLayer(static_cast<WindowInfo*>(ctx)); });
return;
}
NSView* view = (__bridge NSView*)wi->window_handle;
CAMetalLayer* layer = (__bridge_transfer CAMetalLayer*)wi->surface_handle;
if (!layer)
return;
wi->surface_handle = nullptr;
[view setLayer:nil];
[view setWantsLayer:NO];
}