• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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
14using skwindow::DisplayParams;
15using skwindow::IOSWindowInfo;
16using 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
27namespace {
28
29class MetalWindowContext_ios : public MetalWindowContext {
30public:
31    MetalWindowContext_ios(const IOSWindowInfo&, 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
40private:
41    sk_app::Window_ios*  fWindow;
42    UIViewController*    fViewController;
43    MetalView*           fMetalView;
44};
45
46MetalWindowContext_ios::MetalWindowContext_ios(const IOSWindowInfo& info,
47                                               const DisplayParams& params)
48    : MetalWindowContext(params)
49    , fWindow(info.fWindow)
50    , fViewController(info.fViewController) {
51
52    // iOS test apps currently ignore MSAA settings.
53
54    this->initializeContext();
55}
56
57MetalWindowContext_ios::~MetalWindowContext_ios() {
58    this->destroyContext();
59    [fMetalView removeFromSuperview];
60    [fMetalView release];
61}
62
63bool MetalWindowContext_ios::onInitializeContext() {
64    SkASSERT(fWindow != nil);
65    SkASSERT(fViewController != nil);
66
67    CGRect frameRect = [fViewController.view frame];
68    fMetalView = [[[MetalView alloc] initWithFrame:frameRect] initWithWindow:fWindow];
69    [fViewController.view addSubview:fMetalView];
70
71    fMetalLayer = (CAMetalLayer*)fMetalView.layer;
72    fMetalLayer.device = fDevice.get();
73    fMetalLayer.pixelFormat = MTLPixelFormatBGRA8Unorm;
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
85void MetalWindowContext_ios::onDestroyContext() {}
86
87void 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
96namespace skwindow {
97
98std::unique_ptr<WindowContext> MakeMetalForIOS(const IOSWindowInfo& info,
99                                               const DisplayParams& params) {
100    std::unique_ptr<WindowContext> ctx(new MetalWindowContext_ios(info, params));
101    if (!ctx->isValid()) {
102        return nullptr;
103    }
104    return ctx;
105}
106
107}  // namespace skwindow
108