| 1 |
/* |
| 2 |
* Copyright (C) 2015 The Android Open Source Project |
| 3 |
* |
| 4 |
* Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 |
* you may not use this file except in compliance with the License. |
| 6 |
* You may obtain a copy of the License at |
| 7 |
* |
| 8 |
* http://www.apache.org/licenses/LICENSE-2.0 |
| 9 |
* |
| 10 |
* Unless required by applicable law or agreed to in writing, software |
| 11 |
* distributed under the License is distributed on an "AS IS" BASIS, |
| 12 |
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 |
* See the License for the specific language governing permissions and |
| 14 |
* limitations under the License. |
| 15 |
*/ |
| 16 |
|
| 17 |
#include <filamentapp/NativeWindowHelper.h> |
| 18 |
|
| 19 |
#include <utils/Panic.h> |
| 20 |
|
| 21 |
#include <Cocoa/Cocoa.h> |
| 22 |
#include <QuartzCore/QuartzCore.h> |
| 23 |
|
| 24 |
#include <SDL_syswm.h> |
| 25 |
|
| 26 |
void* getNativeWindow(SDL_Window* sdlWindow) { |
| 27 |
SDL_SysWMinfo wmi; |
| 28 |
SDL_VERSION(&wmi.version); |
| 29 |
ASSERT_POSTCONDITION(SDL_GetWindowWMInfo(sdlWindow, &wmi), "SDL version unsupported!"); |
| 30 |
NSWindow* win = wmi.info.cocoa.window; |
| 31 |
NSView* view = [win contentView]; |
| 32 |
return view; |
| 33 |
} |
| 34 |
|
| 35 |
void* setUpMetalLayer(void* nativeView) { |
| 36 |
NSView* view = (NSView*) nativeView; |
| 37 |
[view setWantsLayer:YES]; |
| 38 |
CAMetalLayer* metalLayer = [CAMetalLayer layer]; |
| 39 |
metalLayer.bounds = view.bounds; |
| 40 |
|
| 41 |
// It's important to set the drawableSize to the actual backing pixels. When rendering |
| 42 |
// full-screen, we can skip the macOS compositor if the size matches the display size. |
| 43 |
metalLayer.drawableSize = [view convertSizeToBacking:view.bounds.size]; |
| 44 |
|
| 45 |
// In its implementation of vkGetPhysicalDeviceSurfaceCapabilitiesKHR, MoltenVK takes into |
| 46 |
// consideration both the size (in points) of the bounds, and the contentsScale of the |
| 47 |
// CAMetalLayer from which the Vulkan surface was created. |
| 48 |
// See also https://github.com/KhronosGroup/MoltenVK/issues/428 |
| 49 |
metalLayer.contentsScale = view.window.backingScaleFactor; |
| 50 |
|
| 51 |
// This is set to NO by default, but is also important to ensure we can bypass the compositor |
| 52 |
// in full-screen mode |
| 53 |
// See "Direct to Display" http://metalkit.org/2017/06/30/introducing-metal-2.html. |
| 54 |
metalLayer.opaque = YES; |
| 55 |
|
| 56 |
[view setLayer:metalLayer]; |
| 57 |
|
| 58 |
return metalLayer; |
| 59 |
} |
| 60 |
|
| 61 |
void* resizeMetalLayer(void* nativeView) { |
| 62 |
NSView* view = (NSView*) nativeView; |
| 63 |
CAMetalLayer* metalLayer = (CAMetalLayer*) view.layer; |
| 64 |
CGSize viewSize = view.bounds.size; |
| 65 |
NSSize newDrawableSize = [view convertSizeToBacking:view.bounds.size]; |
| 66 |
metalLayer.drawableSize = newDrawableSize; |
| 67 |
metalLayer.contentsScale = view.window.backingScaleFactor; |
| 68 |
return metalLayer; |
| 69 |
} |