xref: /aosp_15_r20/external/skia/tools/window/ios/GraphiteMetalWindowContext_ios.mm (revision c8dee2aa9b3f27cf6c858bd81872bdeb2c07ed17)
1*c8dee2aaSAndroid Build Coastguard Worker/*
2*c8dee2aaSAndroid Build Coastguard Worker * Copyright 2024 Google Inc.
3*c8dee2aaSAndroid Build Coastguard Worker *
4*c8dee2aaSAndroid Build Coastguard Worker * Use of this source code is governed by a BSD-style license that can be
5*c8dee2aaSAndroid Build Coastguard Worker * found in the LICENSE file.
6*c8dee2aaSAndroid Build Coastguard Worker */
7*c8dee2aaSAndroid Build Coastguard Worker
8*c8dee2aaSAndroid Build Coastguard Worker#include "tools/window/GraphiteNativeMetalWindowContext.h"
9*c8dee2aaSAndroid Build Coastguard Worker#include "tools/window/ios/WindowContextFactory_ios.h"
10*c8dee2aaSAndroid Build Coastguard Worker
11*c8dee2aaSAndroid Build Coastguard Worker#import <Metal/Metal.h>
12*c8dee2aaSAndroid Build Coastguard Worker#import <UIKit/UIKit.h>
13*c8dee2aaSAndroid Build Coastguard Worker
14*c8dee2aaSAndroid Build Coastguard Workerusing skwindow::DisplayParams;
15*c8dee2aaSAndroid Build Coastguard Workerusing skwindow::IOSWindowInfo;
16*c8dee2aaSAndroid Build Coastguard Workerusing skwindow::internal::GraphiteMetalWindowContext;
17*c8dee2aaSAndroid Build Coastguard Worker
18*c8dee2aaSAndroid Build Coastguard Worker@interface GraphiteMetalView : MainView
19*c8dee2aaSAndroid Build Coastguard Worker@end
20*c8dee2aaSAndroid Build Coastguard Worker
21*c8dee2aaSAndroid Build Coastguard Worker@implementation GraphiteMetalView
22*c8dee2aaSAndroid Build Coastguard Worker+ (Class) layerClass {
23*c8dee2aaSAndroid Build Coastguard Worker    return [CAMetalLayer class];
24*c8dee2aaSAndroid Build Coastguard Worker}
25*c8dee2aaSAndroid Build Coastguard Worker@end
26*c8dee2aaSAndroid Build Coastguard Worker
27*c8dee2aaSAndroid Build Coastguard Workernamespace {
28*c8dee2aaSAndroid Build Coastguard Worker
29*c8dee2aaSAndroid Build Coastguard Workerclass GraphiteMetalWindowContext_ios : public GraphiteMetalWindowContext {
30*c8dee2aaSAndroid Build Coastguard Workerpublic:
31*c8dee2aaSAndroid Build Coastguard Worker    GraphiteMetalWindowContext_ios(const IOSWindowInfo&, std::unique_ptr<const DisplayParams>);
32*c8dee2aaSAndroid Build Coastguard Worker
33*c8dee2aaSAndroid Build Coastguard Worker    ~GraphiteMetalWindowContext_ios() override;
34*c8dee2aaSAndroid Build Coastguard Worker
35*c8dee2aaSAndroid Build Coastguard Worker    bool onInitializeContext() override;
36*c8dee2aaSAndroid Build Coastguard Worker    void onDestroyContext() override;
37*c8dee2aaSAndroid Build Coastguard Worker
38*c8dee2aaSAndroid Build Coastguard Worker    void resize(int w, int h) override;
39*c8dee2aaSAndroid Build Coastguard Worker
40*c8dee2aaSAndroid Build Coastguard Workerprivate:
41*c8dee2aaSAndroid Build Coastguard Worker    sk_app::Window_ios*  fWindow;
42*c8dee2aaSAndroid Build Coastguard Worker    UIViewController*    fViewController;
43*c8dee2aaSAndroid Build Coastguard Worker    GraphiteMetalView*   fMetalView;
44*c8dee2aaSAndroid Build Coastguard Worker};
45*c8dee2aaSAndroid Build Coastguard Worker
46*c8dee2aaSAndroid Build Coastguard WorkerGraphiteMetalWindowContext_ios::GraphiteMetalWindowContext_ios(
47*c8dee2aaSAndroid Build Coastguard Worker        const IOSWindowInfo& info, std::unique_ptr<const DisplayParams> params)
48*c8dee2aaSAndroid Build Coastguard Worker        : GraphiteMetalWindowContext(std::move(params))
49*c8dee2aaSAndroid Build Coastguard Worker        , fWindow(info.fWindow)
50*c8dee2aaSAndroid Build Coastguard Worker        , fViewController(info.fViewController) {
51*c8dee2aaSAndroid Build Coastguard Worker    // iOS test apps currently ignore MSAA settings.
52*c8dee2aaSAndroid Build Coastguard Worker
53*c8dee2aaSAndroid Build Coastguard Worker    this->initializeContext();
54*c8dee2aaSAndroid Build Coastguard Worker}
55*c8dee2aaSAndroid Build Coastguard Worker
56*c8dee2aaSAndroid Build Coastguard WorkerGraphiteMetalWindowContext_ios::~GraphiteMetalWindowContext_ios() {
57*c8dee2aaSAndroid Build Coastguard Worker    this->destroyContext();
58*c8dee2aaSAndroid Build Coastguard Worker    [fMetalView removeFromSuperview];
59*c8dee2aaSAndroid Build Coastguard Worker    [fMetalView release];
60*c8dee2aaSAndroid Build Coastguard Worker}
61*c8dee2aaSAndroid Build Coastguard Worker
62*c8dee2aaSAndroid Build Coastguard Workerbool GraphiteMetalWindowContext_ios::onInitializeContext() {
63*c8dee2aaSAndroid Build Coastguard Worker    SkASSERT(fWindow != nil);
64*c8dee2aaSAndroid Build Coastguard Worker    SkASSERT(fViewController != nil);
65*c8dee2aaSAndroid Build Coastguard Worker
66*c8dee2aaSAndroid Build Coastguard Worker    CGRect frameRect = [fViewController.view frame];
67*c8dee2aaSAndroid Build Coastguard Worker    fMetalView = [[[GraphiteMetalView alloc] initWithFrame:frameRect] initWithWindow:fWindow];
68*c8dee2aaSAndroid Build Coastguard Worker    [fViewController.view addSubview:fMetalView];
69*c8dee2aaSAndroid Build Coastguard Worker
70*c8dee2aaSAndroid Build Coastguard Worker    fMetalLayer = (CAMetalLayer*)fMetalView.layer;
71*c8dee2aaSAndroid Build Coastguard Worker    fMetalLayer.device = fDevice.get();
72*c8dee2aaSAndroid Build Coastguard Worker    fMetalLayer.pixelFormat = MTLPixelFormatBGRA8Unorm;
73*c8dee2aaSAndroid Build Coastguard Worker    fMetalLayer.drawableSize = frameRect.size;
74*c8dee2aaSAndroid Build Coastguard Worker    fMetalLayer.frame = frameRect;
75*c8dee2aaSAndroid Build Coastguard Worker    fMetalLayer.framebufferOnly = false;
76*c8dee2aaSAndroid Build Coastguard Worker
77*c8dee2aaSAndroid Build Coastguard Worker    fMetalLayer.contentsGravity = kCAGravityTopLeft;
78*c8dee2aaSAndroid Build Coastguard Worker
79*c8dee2aaSAndroid Build Coastguard Worker    fWidth = frameRect.size.width;
80*c8dee2aaSAndroid Build Coastguard Worker    fHeight = frameRect.size.height;
81*c8dee2aaSAndroid Build Coastguard Worker
82*c8dee2aaSAndroid Build Coastguard Worker    return true;
83*c8dee2aaSAndroid Build Coastguard Worker}
84*c8dee2aaSAndroid Build Coastguard Worker
85*c8dee2aaSAndroid Build Coastguard Workervoid GraphiteMetalWindowContext_ios::onDestroyContext() {}
86*c8dee2aaSAndroid Build Coastguard Worker
87*c8dee2aaSAndroid Build Coastguard Workervoid GraphiteMetalWindowContext_ios::resize(int w, int h) {
88*c8dee2aaSAndroid Build Coastguard Worker    fMetalLayer.drawableSize = fMetalView.frame.size;
89*c8dee2aaSAndroid Build Coastguard Worker    fMetalLayer.frame = fMetalView.frame;
90*c8dee2aaSAndroid Build Coastguard Worker    fWidth = w;
91*c8dee2aaSAndroid Build Coastguard Worker    fHeight = h;
92*c8dee2aaSAndroid Build Coastguard Worker}
93*c8dee2aaSAndroid Build Coastguard Worker
94*c8dee2aaSAndroid Build Coastguard Worker}  // anonymous namespace
95*c8dee2aaSAndroid Build Coastguard Worker
96*c8dee2aaSAndroid Build Coastguard Workernamespace skwindow {
97*c8dee2aaSAndroid Build Coastguard Worker
98*c8dee2aaSAndroid Build Coastguard Workerstd::unique_ptr<WindowContext> MakeGraphiteMetalForIOS(
99*c8dee2aaSAndroid Build Coastguard Worker        const IOSWindowInfo& info, std::unique_ptr<const DisplayParams> params) {
100*c8dee2aaSAndroid Build Coastguard Worker    std::unique_ptr<WindowContext> ctx(new GraphiteMetalWindowContext_ios(info, std::move(params)));
101*c8dee2aaSAndroid Build Coastguard Worker    if (!ctx->isValid()) {
102*c8dee2aaSAndroid Build Coastguard Worker        return nullptr;
103*c8dee2aaSAndroid Build Coastguard Worker    }
104*c8dee2aaSAndroid Build Coastguard Worker    return ctx;
105*c8dee2aaSAndroid Build Coastguard Worker}
106*c8dee2aaSAndroid Build Coastguard Worker
107*c8dee2aaSAndroid Build Coastguard Worker}  // namespace skwindow
108