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/SkColorSpace.h" 7#include "include/core/SkColorType.h" 8#include "include/core/SkSurface.h" 9#include "include/gpu/ganesh/GrBackendSurface.h" 10#include "include/gpu/ganesh/GrContextOptions.h" 11#include "include/gpu/ganesh/GrDirectContext.h" 12#include "include/gpu/ganesh/mtl/GrMtlBackendContext.h" 13#include "include/gpu/ganesh/mtl/GrMtlDirectContext.h" 14#include "include/gpu/ganesh/mtl/GrMtlTypes.h" 15#include "include/gpu/ganesh/mtl/SkSurfaceMetal.h" 16 17#import <Metal/Metal.h> 18#import <MetalKit/MetalKit.h> 19 20sk_sp<SkSurface> SkMtkViewToSurface(MTKView* mtkView, GrRecordingContext* rContext) { 21 if (!rContext || 22 MTLPixelFormatDepth32Float_Stencil8 != [mtkView depthStencilPixelFormat] || 23 MTLPixelFormatBGRA8Unorm != [mtkView colorPixelFormat]) { 24 return nullptr; 25 } 26 27 const SkColorType colorType = kBGRA_8888_SkColorType; // MTLPixelFormatBGRA8Unorm 28 sk_sp<SkColorSpace> colorSpace = nullptr; // MTLPixelFormatBGRA8Unorm 29 const GrSurfaceOrigin origin = kTopLeft_GrSurfaceOrigin; 30 const SkSurfaceProps surfaceProps; 31 int sampleCount = (int)[mtkView sampleCount]; 32 33 return SkSurfaces::WrapMTKView(rContext, 34 (__bridge GrMTLHandle)mtkView, 35 origin, 36 sampleCount, 37 colorType, 38 colorSpace, 39 &surfaceProps); 40} 41 42GrContextHolder SkMetalDeviceToGrContext(id<MTLDevice> device, id<MTLCommandQueue> queue) { 43 GrMtlBackendContext backendContext = {}; 44 backendContext.fDevice.reset((__bridge void*)device); 45 backendContext.fQueue.reset((__bridge void*)queue); 46 GrContextOptions grContextOptions; // set different options here. 47 return GrContextHolder(GrDirectContexts::MakeMetal(backendContext, grContextOptions).release()); 48} 49 50void SkMtkViewConfigForSkia(MTKView* mtkView) { 51 [mtkView setDepthStencilPixelFormat:MTLPixelFormatDepth32Float_Stencil8]; 52 [mtkView setColorPixelFormat:MTLPixelFormatBGRA8Unorm]; 53 [mtkView setSampleCount:1]; 54} 55