Flutter Engine
The Flutter Engine
Loading...
Searching...
No Matches
GraphiteDawnMetalWindowContext_mac.mm
Go to the documentation of this file.
1/*
2 * Copyright 2022 Google LLC
3 *
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
6 */
7
10
11#import <Cocoa/Cocoa.h>
12#import <QuartzCore/CAConstraintLayoutManager.h>
13#import <QuartzCore/CAMetalLayer.h>
14
18
19namespace {
20
21class GraphiteDawnMetalWindowContext_mac : public GraphiteDawnWindowContext {
22public:
23 GraphiteDawnMetalWindowContext_mac(const MacWindowInfo&, const DisplayParams&);
24
25 ~GraphiteDawnMetalWindowContext_mac() override;
26
27 bool onInitializeContext() override;
28 void onDestroyContext() override;
29 void resize(int w, int h) override;
30
31private:
32 bool resizeInternal();
33
34 NSView* fMainView;
35 CAMetalLayer* fMetalLayer;
36};
37
38GraphiteDawnMetalWindowContext_mac::GraphiteDawnMetalWindowContext_mac(const MacWindowInfo& info,
39 const DisplayParams& params)
40 : GraphiteDawnWindowContext(params, wgpu::TextureFormat::BGRA8Unorm)
41 , fMainView(info.fMainView) {
42
43 CGFloat backingScaleFactor = skwindow::GetBackingScaleFactor(fMainView);
44 CGSize backingSize = fMainView.bounds.size;
45 this->initializeContext(backingSize.width * backingScaleFactor,
46 backingSize.height * backingScaleFactor);
47}
48
49GraphiteDawnMetalWindowContext_mac::~GraphiteDawnMetalWindowContext_mac() {
50 this->destroyContext();
51}
52
53bool GraphiteDawnMetalWindowContext_mac::onInitializeContext() {
54 SkASSERT(nil != fMainView);
55
56 auto device = createDevice(wgpu::BackendType::Metal);
57 if (!device) {
59 return false;
60 }
61
62 // Create a CAMetalLayer that covers the whole window that will be passed to
63 // CreateSurface.
64 fMetalLayer = [CAMetalLayer layer];
65 BOOL useVsync = fDisplayParams.fDisableVsync ? NO : YES;
66 fMetalLayer.displaySyncEnabled = useVsync;
67 fMainView.wantsLayer = YES;
68 fMainView.layer = fMetalLayer;
69
70 // Adjust fMetalLayer size based on window size.
71 this->resizeInternal();
72
73 wgpu::SurfaceDescriptorFromMetalLayer surfaceChainedDesc;
74 surfaceChainedDesc.layer = fMetalLayer;
75 wgpu::SurfaceDescriptor surfaceDesc;
76 surfaceDesc.nextInChain = &surfaceChainedDesc;
77
78 auto surface = wgpu::Instance(fInstance->Get()).CreateSurface(&surfaceDesc);
79 if (!surface) {
80 SkASSERT(false);
81 return false;
82 }
83
84
85 fDevice = std::move(device);
86 fSurface = std::move(surface);
87 fSwapChain = this->createSwapChain();
88
89 return true;
90}
91
92void GraphiteDawnMetalWindowContext_mac::onDestroyContext() {
93 fMetalLayer = nil;
94 fMainView.layer = nil;
95 fMainView.wantsLayer = NO;
96}
97
98void GraphiteDawnMetalWindowContext_mac::resize(int w, int h) {
99 if (!this->resizeInternal()) {
100 return;
101 }
102 fSwapChain = this->createSwapChain();
103}
104
105bool GraphiteDawnMetalWindowContext_mac::resizeInternal() {
106 CGFloat backingScaleFactor = skwindow::GetBackingScaleFactor(fMainView);
107 CGSize backingSize = fMainView.bounds.size;
108 backingSize.width *= backingScaleFactor;
109 backingSize.height *= backingScaleFactor;
110
111 fMetalLayer.drawableSize = backingSize;
112 fMetalLayer.contentsScale = backingScaleFactor;
113
114 if (fWidth == backingSize.width && fHeight == backingSize.height) {
115 return false;
116 }
117
118 fWidth = backingSize.width;
119 fHeight = backingSize.height;
120 return true;
121}
122
123} // anonymous namespace
124
125namespace skwindow {
126
127std::unique_ptr<WindowContext> MakeGraphiteDawnMetalForMac(const MacWindowInfo& info,
128 const DisplayParams& params) {
129 std::unique_ptr<WindowContext> ctx(new GraphiteDawnMetalWindowContext_mac(info, params));
130 if (!ctx->isValid()) {
131 return nullptr;
132 }
133 return ctx;
134}
135
136} // namespace skwindow
static void info(const char *fmt,...) SK_PRINTF_LIKE(1
Definition DM.cpp:213
#define SkASSERT(cond)
Definition SkAssert.h:116
virtual void resize(int w, int h)=0
const EmbeddedViewParams * params
VkDevice device
Definition main.cc:53
VkSurfaceKHR surface
Definition main.cc:49
static CGFloat GetBackingScaleFactor(NSView *view)
SkScalar w
SkScalar h
int BOOL