Debian Codegraph @

tools/window/ios/MetalWindowContext_ios.mm

raw
1 /*
2 * Copyright 2019 Google Inc.
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
8 #include "tools/window/MetalWindowContext.h"
9 #include "tools/window/ios/WindowContextFactory_ios.h"
10
11 #import <Metal/Metal.h>
12 #import <UIKit/UIKit.h>
13
14 using skwindow::DisplayParams;
15 using skwindow::IOSWindowInfo;
16 using skwindow::internal::MetalWindowContext;
17
18 @interface MetalView : MainView
19 @end
20
21 @implementation MetalView
22 + (Class) layerClass {
23 return [CAMetalLayer class];
24 }
25 @end
26
27 namespace {
28
29 class MetalWindowContext_ios : public MetalWindowContext {
30 public:
31 MetalWindowContext_ios(const IOSWindowInfo&, std::unique_ptr<const DisplayParams>);
32
33 ~MetalWindowContext_ios() override;
34
35 bool onInitializeContext() override;
36 void onDestroyContext() override;
37
38 void resize(int w, int h) override;
39
40 private:
41 sk_app::Window_ios* fWindow;
42 UIViewController* fViewController;
43 MetalView* fMetalView;
44 };
45
46 MetalWindowContext_ios::MetalWindowContext_ios(const IOSWindowInfo& info,
47 std::unique_ptr<const DisplayParams> params)
48 : MetalWindowContext(std::move(params))
49 , fWindow(info.fWindow)
50 , fViewController(info.fViewController) {
51 // iOS test apps currently ignore MSAA settings.
52
53 this->initializeContext();
54 }
55
56 MetalWindowContext_ios::~MetalWindowContext_ios() {
57 this->destroyContext();
58 [fMetalView removeFromSuperview];
59 [fMetalView release];
60 }
61
62 bool MetalWindowContext_ios::onInitializeContext() {
63 SkASSERT(fWindow != nil);
64 SkASSERT(fViewController != nil);
65
66 CGRect frameRect = [fViewController.view frame];
67 fMetalView = [[[MetalView alloc] initWithFrame:frameRect] initWithWindow:fWindow];
68 [fViewController.view addSubview:fMetalView];
69
70 fMetalLayer = (CAMetalLayer*)fMetalView.layer;
71 fMetalLayer.device = fDevice.get();
72 fMetalLayer.pixelFormat = MTLPixelFormatBGRA8Unorm;
73 fMetalLayer.framebufferOnly = false;
74 fMetalLayer.drawableSize = frameRect.size;
75 fMetalLayer.frame = frameRect;
76
77 fMetalLayer.contentsGravity = kCAGravityTopLeft;
78
79 fWidth = frameRect.size.width;
80 fHeight = frameRect.size.height;
81
82 return true;
83 }
84
85 void MetalWindowContext_ios::onDestroyContext() {}
86
87 void MetalWindowContext_ios::resize(int w, int h) {
88 fMetalLayer.drawableSize = fMetalView.frame.size;
89 fMetalLayer.frame = fMetalView.frame;
90 fWidth = w;
91 fHeight = h;
92 }
93
94 } // anonymous namespace
95
96 namespace skwindow {
97
98 std::unique_ptr<WindowContext> MakeMetalForIOS(const IOSWindowInfo& info,
99 std::unique_ptr<const DisplayParams> params) {
100 std::unique_ptr<WindowContext> ctx(new MetalWindowContext_ios(info, std::move(params)));
101 if (!ctx->isValid()) {
102 return nullptr;
103 }
104 return ctx;
105 }
106
107 } // namespace skwindow