1// Copyright 2019 Google LLC. 2// Use of this source code is governed by a BSD-style license that can be found in the LICENSE file. 3 4#include "tools/skottie_ios_app/SkMetalViewBridge.h" 5 6#include "include/core/SkSurface.h" 7#include "include/gpu/GrBackendSurface.h" 8#include "include/gpu/GrContextOptions.h" 9#include "include/gpu/GrDirectContext.h" 10#include "include/gpu/mtl/GrMtlTypes.h" 11 12#import <Metal/Metal.h> 13#import <MetalKit/MetalKit.h> 14 15sk_sp<SkSurface> SkMtkViewToSurface(MTKView* mtkView, GrRecordingContext* rContext) { 16 if (!rContext || 17 MTLPixelFormatDepth32Float_Stencil8 != [mtkView depthStencilPixelFormat] || 18 MTLPixelFormatBGRA8Unorm != [mtkView colorPixelFormat]) { 19 return nullptr; 20 } 21 22 const SkColorType colorType = kBGRA_8888_SkColorType; // MTLPixelFormatBGRA8Unorm 23 sk_sp<SkColorSpace> colorSpace = nullptr; // MTLPixelFormatBGRA8Unorm 24 const GrSurfaceOrigin origin = kTopLeft_GrSurfaceOrigin; 25 const SkSurfaceProps surfaceProps; 26 int sampleCount = (int)[mtkView sampleCount]; 27 28 return SkSurface::MakeFromMTKView(rContext, (__bridge GrMTLHandle)mtkView, origin, sampleCount, 29 colorType, colorSpace, &surfaceProps); 30} 31 32GrContextHolder SkMetalDeviceToGrContext(id<MTLDevice> device, id<MTLCommandQueue> queue) { 33 GrContextOptions grContextOptions; // set different options here. 34 return GrContextHolder(GrDirectContext::MakeMetal((__bridge void*)device, 35 (__bridge void*)queue, 36 grContextOptions).release()); 37} 38 39void SkMtkViewConfigForSkia(MTKView* mtkView) { 40 [mtkView setDepthStencilPixelFormat:MTLPixelFormatDepth32Float_Stencil8]; 41 [mtkView setColorPixelFormat:MTLPixelFormatBGRA8Unorm]; 42 [mtkView setSampleCount:1]; 43} 44