2019-02-08 20:36:14 +00:00
|
|
|
/*
|
|
|
|
* Copyright 2019 Google Inc.
|
|
|
|
*
|
|
|
|
* Use of this source code is governed by a BSD-style license that can be
|
|
|
|
* found in the LICENSE file.
|
|
|
|
*/
|
|
|
|
|
2019-04-23 17:05:21 +00:00
|
|
|
#include "include/core/SkCanvas.h"
|
|
|
|
#include "include/core/SkSurface.h"
|
|
|
|
#include "include/gpu/GrBackendSurface.h"
|
2020-07-10 17:55:21 +00:00
|
|
|
#include "include/gpu/GrDirectContext.h"
|
2019-04-23 17:05:21 +00:00
|
|
|
#include "include/gpu/mtl/GrMtlTypes.h"
|
|
|
|
#include "src/core/SkMathPriv.h"
|
|
|
|
#include "src/gpu/GrCaps.h"
|
2020-10-14 15:23:11 +00:00
|
|
|
#include "src/gpu/GrDirectContextPriv.h"
|
2019-04-23 17:05:21 +00:00
|
|
|
#include "src/image/SkImage_Base.h"
|
|
|
|
#include "tools/sk_app/MetalWindowContext.h"
|
2019-02-08 20:36:14 +00:00
|
|
|
|
2019-05-16 14:31:56 +00:00
|
|
|
using sk_app::DisplayParams;
|
|
|
|
using sk_app::MetalWindowContext;
|
|
|
|
|
|
|
|
namespace sk_app {
|
2019-02-08 20:36:14 +00:00
|
|
|
|
|
|
|
MetalWindowContext::MetalWindowContext(const DisplayParams& params)
|
2020-07-10 17:55:21 +00:00
|
|
|
: WindowContext(params)
|
|
|
|
, fValid(false) {
|
2019-05-16 14:31:56 +00:00
|
|
|
|
2019-02-08 20:36:14 +00:00
|
|
|
fDisplayParams.fMSAASampleCount = GrNextPow2(fDisplayParams.fMSAASampleCount);
|
|
|
|
}
|
|
|
|
|
|
|
|
void MetalWindowContext::initializeContext() {
|
|
|
|
SkASSERT(!fContext);
|
|
|
|
|
|
|
|
fDevice = MTLCreateSystemDefaultDevice();
|
|
|
|
fQueue = [fDevice newCommandQueue];
|
|
|
|
|
2019-05-16 14:31:56 +00:00
|
|
|
if (fDisplayParams.fMSAASampleCount > 1) {
|
2019-10-07 19:52:47 +00:00
|
|
|
if (@available(macOS 10.11, iOS 9.0, *)) {
|
|
|
|
if (![fDevice supportsTextureSampleCount:fDisplayParams.fMSAASampleCount]) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
} else {
|
2019-05-16 14:31:56 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
2019-07-11 19:40:53 +00:00
|
|
|
fSampleCount = fDisplayParams.fMSAASampleCount;
|
2019-05-16 14:31:56 +00:00
|
|
|
fStencilBits = 8;
|
|
|
|
|
2019-02-08 20:36:14 +00:00
|
|
|
fValid = this->onInitializeContext();
|
2019-05-16 14:31:56 +00:00
|
|
|
|
2020-07-13 20:13:31 +00:00
|
|
|
fContext = GrDirectContext::MakeMetal((__bridge void*)fDevice, (__bridge void*)fQueue,
|
|
|
|
fDisplayParams.fGrContextOptions);
|
2019-02-08 20:36:14 +00:00
|
|
|
if (!fContext && fDisplayParams.fMSAASampleCount > 1) {
|
|
|
|
fDisplayParams.fMSAASampleCount /= 2;
|
|
|
|
this->initializeContext();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void MetalWindowContext::destroyContext() {
|
|
|
|
if (fContext) {
|
2020-07-28 14:20:58 +00:00
|
|
|
// in case we have outstanding refs to this (lua?)
|
2019-02-08 20:36:14 +00:00
|
|
|
fContext->abandonContext();
|
|
|
|
fContext.reset();
|
|
|
|
}
|
|
|
|
|
|
|
|
this->onDestroyContext();
|
2019-05-16 14:31:56 +00:00
|
|
|
|
|
|
|
fMetalLayer = nil;
|
|
|
|
fValid = false;
|
|
|
|
|
2019-06-06 16:33:53 +00:00
|
|
|
[fQueue release];
|
|
|
|
[fDevice release];
|
2019-02-08 20:36:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
sk_sp<SkSurface> MetalWindowContext::getBackbufferSurface() {
|
|
|
|
sk_sp<SkSurface> surface;
|
|
|
|
if (fContext) {
|
2020-10-23 16:47:57 +00:00
|
|
|
if (fDisplayParams.fDelayDrawableAcquisition) {
|
|
|
|
surface = SkSurface::MakeFromCAMetalLayer(fContext.get(),
|
|
|
|
(__bridge GrMTLHandle)fMetalLayer,
|
|
|
|
kTopLeft_GrSurfaceOrigin, fSampleCount,
|
|
|
|
kBGRA_8888_SkColorType,
|
|
|
|
fDisplayParams.fColorSpace,
|
|
|
|
&fDisplayParams.fSurfaceProps,
|
|
|
|
&fDrawableHandle);
|
|
|
|
} else {
|
|
|
|
id<CAMetalDrawable> currentDrawable = [fMetalLayer nextDrawable];
|
|
|
|
|
|
|
|
GrMtlTextureInfo fbInfo;
|
|
|
|
fbInfo.fTexture.retain(currentDrawable.texture);
|
|
|
|
|
|
|
|
GrBackendRenderTarget backendRT(fWidth,
|
|
|
|
fHeight,
|
|
|
|
fSampleCount,
|
|
|
|
fbInfo);
|
|
|
|
|
|
|
|
surface = SkSurface::MakeFromBackendRenderTarget(fContext.get(), backendRT,
|
|
|
|
kTopLeft_GrSurfaceOrigin,
|
|
|
|
kBGRA_8888_SkColorType,
|
|
|
|
fDisplayParams.fColorSpace,
|
|
|
|
&fDisplayParams.fSurfaceProps);
|
|
|
|
|
|
|
|
fDrawableHandle = CFRetain((GrMTLHandle) currentDrawable);
|
|
|
|
}
|
2019-02-08 20:36:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return surface;
|
|
|
|
}
|
|
|
|
|
|
|
|
void MetalWindowContext::swapBuffers() {
|
2019-10-17 18:58:37 +00:00
|
|
|
id<CAMetalDrawable> currentDrawable = (id<CAMetalDrawable>)fDrawableHandle;
|
|
|
|
|
2019-02-08 20:36:14 +00:00
|
|
|
id<MTLCommandBuffer> commandBuffer = [fQueue commandBuffer];
|
|
|
|
commandBuffer.label = @"Present";
|
|
|
|
|
2019-10-17 18:58:37 +00:00
|
|
|
[commandBuffer presentDrawable:currentDrawable];
|
2019-02-08 20:36:14 +00:00
|
|
|
[commandBuffer commit];
|
2020-08-14 15:42:35 +00:00
|
|
|
// ARC is off in sk_app, so we need to release the CF ref manually
|
|
|
|
CFRelease(fDrawableHandle);
|
|
|
|
fDrawableHandle = nil;
|
2019-02-08 20:36:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void MetalWindowContext::setDisplayParams(const DisplayParams& params) {
|
|
|
|
this->destroyContext();
|
|
|
|
fDisplayParams = params;
|
|
|
|
this->initializeContext();
|
|
|
|
}
|
|
|
|
|
|
|
|
} //namespace sk_app
|