2019-09-09 21:03:38 +00:00
|
|
|
// Copyright 2019 Google LLC.
|
|
|
|
// Use of this source code is governed by a BSD-style license that can be found in the LICENSE file.
|
|
|
|
|
2019-12-11 15:32:59 +00:00
|
|
|
#include "tools/skottie_ios_app/SkMetalViewBridge.h"
|
2019-09-09 21:03:38 +00:00
|
|
|
|
|
|
|
#include "include/core/SkSurface.h"
|
|
|
|
#include "include/gpu/GrBackendSurface.h"
|
|
|
|
#include "include/gpu/GrContext.h"
|
|
|
|
#include "include/gpu/GrContextOptions.h"
|
|
|
|
#include "include/gpu/mtl/GrMtlTypes.h"
|
|
|
|
|
|
|
|
#import <Metal/Metal.h>
|
|
|
|
#import <MetalKit/MetalKit.h>
|
|
|
|
|
|
|
|
sk_sp<SkSurface> SkMtkViewToSurface(MTKView* mtkView, GrContext* grContext) {
|
2020-01-08 17:15:44 +00:00
|
|
|
if (!grContext ||
|
2019-09-09 21:03:38 +00:00
|
|
|
MTLPixelFormatDepth32Float_Stencil8 != [mtkView depthStencilPixelFormat] ||
|
|
|
|
MTLPixelFormatBGRA8Unorm != [mtkView colorPixelFormat]) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
2020-01-08 17:15:44 +00:00
|
|
|
|
2019-09-09 21:03:38 +00:00
|
|
|
const SkColorType colorType = kBGRA_8888_SkColorType; // MTLPixelFormatBGRA8Unorm
|
|
|
|
sk_sp<SkColorSpace> colorSpace = nullptr; // MTLPixelFormatBGRA8Unorm
|
|
|
|
const GrSurfaceOrigin origin = kTopLeft_GrSurfaceOrigin;
|
|
|
|
const SkSurfaceProps surfaceProps(SkSurfaceProps::kLegacyFontHost_InitType);
|
|
|
|
int sampleCount = (int)[mtkView sampleCount];
|
2020-01-08 17:15:44 +00:00
|
|
|
|
|
|
|
return SkSurface::MakeFromMTKView(grContext, (__bridge GrMTLHandle)mtkView, origin, sampleCount,
|
|
|
|
colorType, colorSpace, &surfaceProps);
|
2019-09-09 21:03:38 +00:00
|
|
|
}
|
|
|
|
|
2019-12-11 15:32:59 +00:00
|
|
|
GrContextHolder SkMetalDeviceToGrContext(id<MTLDevice> device, id<MTLCommandQueue> queue) {
|
|
|
|
GrContextOptions grContextOptions; // set different options here.
|
|
|
|
return GrContextHolder(GrContext::MakeMetal((__bridge void*)device,
|
|
|
|
(__bridge void*)queue,
|
|
|
|
grContextOptions).release());
|
2019-09-09 21:03:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void SkMtkViewConfigForSkia(MTKView* mtkView) {
|
|
|
|
[mtkView setDepthStencilPixelFormat:MTLPixelFormatDepth32Float_Stencil8];
|
|
|
|
[mtkView setColorPixelFormat:MTLPixelFormatBGRA8Unorm];
|
|
|
|
[mtkView setSampleCount:1];
|
|
|
|
}
|