1cb93a386Sopenharmony_ci/* 2cb93a386Sopenharmony_ci * Copyright 2019 Google Inc. 3cb93a386Sopenharmony_ci * 4cb93a386Sopenharmony_ci * Use of this source code is governed by a BSD-style license that can be 5cb93a386Sopenharmony_ci * found in the LICENSE file. 6cb93a386Sopenharmony_ci */ 7cb93a386Sopenharmony_ci 8cb93a386Sopenharmony_ci#include "tools/sk_app/MetalWindowContext.h" 9cb93a386Sopenharmony_ci#include "tools/sk_app/ios/WindowContextFactory_ios.h" 10cb93a386Sopenharmony_ci 11cb93a386Sopenharmony_ci#import <Metal/Metal.h> 12cb93a386Sopenharmony_ci#import <UIKit/UIKit.h> 13cb93a386Sopenharmony_ci 14cb93a386Sopenharmony_ciusing sk_app::DisplayParams; 15cb93a386Sopenharmony_ciusing sk_app::window_context_factory::IOSWindowInfo; 16cb93a386Sopenharmony_ciusing sk_app::MetalWindowContext; 17cb93a386Sopenharmony_ci 18cb93a386Sopenharmony_ci@interface MetalView : MainView 19cb93a386Sopenharmony_ci@end 20cb93a386Sopenharmony_ci 21cb93a386Sopenharmony_ci@implementation MetalView 22cb93a386Sopenharmony_ci+ (Class) layerClass { 23cb93a386Sopenharmony_ci return [CAMetalLayer class]; 24cb93a386Sopenharmony_ci} 25cb93a386Sopenharmony_ci@end 26cb93a386Sopenharmony_ci 27cb93a386Sopenharmony_cinamespace { 28cb93a386Sopenharmony_ci 29cb93a386Sopenharmony_ciclass MetalWindowContext_ios : public MetalWindowContext { 30cb93a386Sopenharmony_cipublic: 31cb93a386Sopenharmony_ci MetalWindowContext_ios(const IOSWindowInfo&, const DisplayParams&); 32cb93a386Sopenharmony_ci 33cb93a386Sopenharmony_ci ~MetalWindowContext_ios() override; 34cb93a386Sopenharmony_ci 35cb93a386Sopenharmony_ci bool onInitializeContext() override; 36cb93a386Sopenharmony_ci void onDestroyContext() override; 37cb93a386Sopenharmony_ci 38cb93a386Sopenharmony_ci void resize(int w, int h) override; 39cb93a386Sopenharmony_ci 40cb93a386Sopenharmony_ciprivate: 41cb93a386Sopenharmony_ci sk_app::Window_ios* fWindow; 42cb93a386Sopenharmony_ci UIViewController* fViewController; 43cb93a386Sopenharmony_ci MetalView* fMetalView; 44cb93a386Sopenharmony_ci 45cb93a386Sopenharmony_ci using INHERITED = MetalWindowContext; 46cb93a386Sopenharmony_ci}; 47cb93a386Sopenharmony_ci 48cb93a386Sopenharmony_ciMetalWindowContext_ios::MetalWindowContext_ios(const IOSWindowInfo& info, 49cb93a386Sopenharmony_ci const DisplayParams& params) 50cb93a386Sopenharmony_ci : INHERITED(params) 51cb93a386Sopenharmony_ci , fWindow(info.fWindow) 52cb93a386Sopenharmony_ci , fViewController(info.fViewController) { 53cb93a386Sopenharmony_ci 54cb93a386Sopenharmony_ci // any config code here (particularly for msaa)? 55cb93a386Sopenharmony_ci 56cb93a386Sopenharmony_ci this->initializeContext(); 57cb93a386Sopenharmony_ci} 58cb93a386Sopenharmony_ci 59cb93a386Sopenharmony_ciMetalWindowContext_ios::~MetalWindowContext_ios() { 60cb93a386Sopenharmony_ci this->destroyContext(); 61cb93a386Sopenharmony_ci [fMetalView removeFromSuperview]; 62cb93a386Sopenharmony_ci [fMetalView release]; 63cb93a386Sopenharmony_ci} 64cb93a386Sopenharmony_ci 65cb93a386Sopenharmony_cibool MetalWindowContext_ios::onInitializeContext() { 66cb93a386Sopenharmony_ci SkASSERT(nil != fWindow); 67cb93a386Sopenharmony_ci SkASSERT(nil != fViewController); 68cb93a386Sopenharmony_ci 69cb93a386Sopenharmony_ci CGRect frameRect = [fViewController.view frame]; 70cb93a386Sopenharmony_ci fMetalView = [[[MetalView alloc] initWithFrame:frameRect] initWithWindow:fWindow]; 71cb93a386Sopenharmony_ci [fViewController.view addSubview:fMetalView]; 72cb93a386Sopenharmony_ci 73cb93a386Sopenharmony_ci fMetalLayer = (CAMetalLayer*)fMetalView.layer; 74cb93a386Sopenharmony_ci fMetalLayer.device = fDevice.get(); 75cb93a386Sopenharmony_ci fMetalLayer.pixelFormat = MTLPixelFormatBGRA8Unorm; 76cb93a386Sopenharmony_ci fMetalLayer.drawableSize = frameRect.size; 77cb93a386Sopenharmony_ci fMetalLayer.frame = frameRect; 78cb93a386Sopenharmony_ci 79cb93a386Sopenharmony_ci // TODO: need solution for iOS 80cb93a386Sopenharmony_ci // BOOL useVsync = fDisplayParams.fDisableVsync ? NO : YES; 81cb93a386Sopenharmony_ci // fMetalLayer.displaySyncEnabled = useVsync; 82cb93a386Sopenharmony_ci fMetalLayer.contentsGravity = kCAGravityTopLeft; 83cb93a386Sopenharmony_ci 84cb93a386Sopenharmony_ci fWidth = frameRect.size.width; 85cb93a386Sopenharmony_ci fHeight = frameRect.size.height; 86cb93a386Sopenharmony_ci 87cb93a386Sopenharmony_ci return true; 88cb93a386Sopenharmony_ci} 89cb93a386Sopenharmony_ci 90cb93a386Sopenharmony_civoid MetalWindowContext_ios::onDestroyContext() {} 91cb93a386Sopenharmony_ci 92cb93a386Sopenharmony_civoid MetalWindowContext_ios::resize(int w, int h) { 93cb93a386Sopenharmony_ci // TODO: handle rotation 94cb93a386Sopenharmony_ci fMetalLayer.drawableSize = fMetalView.frame.size; 95cb93a386Sopenharmony_ci fMetalLayer.frame = fMetalView.frame; 96cb93a386Sopenharmony_ci fWidth = w; 97cb93a386Sopenharmony_ci fHeight = h; 98cb93a386Sopenharmony_ci} 99cb93a386Sopenharmony_ci 100cb93a386Sopenharmony_ci} // anonymous namespace 101cb93a386Sopenharmony_ci 102cb93a386Sopenharmony_cinamespace sk_app { 103cb93a386Sopenharmony_cinamespace window_context_factory { 104cb93a386Sopenharmony_ci 105cb93a386Sopenharmony_cistd::unique_ptr<WindowContext> MakeMetalForIOS(const IOSWindowInfo& info, 106cb93a386Sopenharmony_ci const DisplayParams& params) { 107cb93a386Sopenharmony_ci std::unique_ptr<WindowContext> ctx(new MetalWindowContext_ios(info, params)); 108cb93a386Sopenharmony_ci if (!ctx->isValid()) { 109cb93a386Sopenharmony_ci return nullptr; 110cb93a386Sopenharmony_ci } 111cb93a386Sopenharmony_ci return ctx; 112cb93a386Sopenharmony_ci} 113cb93a386Sopenharmony_ci 114cb93a386Sopenharmony_ci} // namespace window_context_factory 115cb93a386Sopenharmony_ci} // namespace sk_app 116