1*c8dee2aaSAndroid Build Coastguard Worker/* 2*c8dee2aaSAndroid Build Coastguard Worker * Copyright 2019 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/MetalWindowContext.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::MetalWindowContext; 17*c8dee2aaSAndroid Build Coastguard Worker 18*c8dee2aaSAndroid Build Coastguard Worker@interface MetalView : MainView 19*c8dee2aaSAndroid Build Coastguard Worker@end 20*c8dee2aaSAndroid Build Coastguard Worker 21*c8dee2aaSAndroid Build Coastguard Worker@implementation MetalView 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 MetalWindowContext_ios : public MetalWindowContext { 30*c8dee2aaSAndroid Build Coastguard Workerpublic: 31*c8dee2aaSAndroid Build Coastguard Worker MetalWindowContext_ios(const IOSWindowInfo&, std::unique_ptr<const DisplayParams>); 32*c8dee2aaSAndroid Build Coastguard Worker 33*c8dee2aaSAndroid Build Coastguard Worker ~MetalWindowContext_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 MetalView* fMetalView; 44*c8dee2aaSAndroid Build Coastguard Worker}; 45*c8dee2aaSAndroid Build Coastguard Worker 46*c8dee2aaSAndroid Build Coastguard WorkerMetalWindowContext_ios::MetalWindowContext_ios(const IOSWindowInfo& info, 47*c8dee2aaSAndroid Build Coastguard Worker std::unique_ptr<const DisplayParams> params) 48*c8dee2aaSAndroid Build Coastguard Worker : MetalWindowContext(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 WorkerMetalWindowContext_ios::~MetalWindowContext_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 MetalWindowContext_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 = [[[MetalView 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 76*c8dee2aaSAndroid Build Coastguard Worker fMetalLayer.contentsGravity = kCAGravityTopLeft; 77*c8dee2aaSAndroid Build Coastguard Worker 78*c8dee2aaSAndroid Build Coastguard Worker fWidth = frameRect.size.width; 79*c8dee2aaSAndroid Build Coastguard Worker fHeight = frameRect.size.height; 80*c8dee2aaSAndroid Build Coastguard Worker 81*c8dee2aaSAndroid Build Coastguard Worker return true; 82*c8dee2aaSAndroid Build Coastguard Worker} 83*c8dee2aaSAndroid Build Coastguard Worker 84*c8dee2aaSAndroid Build Coastguard Workervoid MetalWindowContext_ios::onDestroyContext() {} 85*c8dee2aaSAndroid Build Coastguard Worker 86*c8dee2aaSAndroid Build Coastguard Workervoid MetalWindowContext_ios::resize(int w, int h) { 87*c8dee2aaSAndroid Build Coastguard Worker fMetalLayer.drawableSize = fMetalView.frame.size; 88*c8dee2aaSAndroid Build Coastguard Worker fMetalLayer.frame = fMetalView.frame; 89*c8dee2aaSAndroid Build Coastguard Worker fWidth = w; 90*c8dee2aaSAndroid Build Coastguard Worker fHeight = h; 91*c8dee2aaSAndroid Build Coastguard Worker} 92*c8dee2aaSAndroid Build Coastguard Worker 93*c8dee2aaSAndroid Build Coastguard Worker} // anonymous namespace 94*c8dee2aaSAndroid Build Coastguard Worker 95*c8dee2aaSAndroid Build Coastguard Workernamespace skwindow { 96*c8dee2aaSAndroid Build Coastguard Worker 97*c8dee2aaSAndroid Build Coastguard Workerstd::unique_ptr<WindowContext> MakeMetalForIOS(const IOSWindowInfo& info, 98*c8dee2aaSAndroid Build Coastguard Worker std::unique_ptr<const DisplayParams> params) { 99*c8dee2aaSAndroid Build Coastguard Worker std::unique_ptr<WindowContext> ctx(new MetalWindowContext_ios(info, std::move(params))); 100*c8dee2aaSAndroid Build Coastguard Worker if (!ctx->isValid()) { 101*c8dee2aaSAndroid Build Coastguard Worker return nullptr; 102*c8dee2aaSAndroid Build Coastguard Worker } 103*c8dee2aaSAndroid Build Coastguard Worker return ctx; 104*c8dee2aaSAndroid Build Coastguard Worker} 105*c8dee2aaSAndroid Build Coastguard Worker 106*c8dee2aaSAndroid Build Coastguard Worker} // namespace skwindow 107