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"
|
|
|
|
#include "include/gpu/GrContext.h"
|
|
|
|
#include "include/gpu/mtl/GrMtlTypes.h"
|
|
|
|
#include "src/core/SkMathPriv.h"
|
|
|
|
#include "src/gpu/GrCaps.h"
|
|
|
|
#include "src/gpu/GrContextPriv.h"
|
|
|
|
#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)
|
|
|
|
: WindowContext(params)
|
2019-05-16 14:31:56 +00:00
|
|
|
, fValid(false) {
|
|
|
|
|
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) {
|
|
|
|
if (![fDevice supportsTextureSampleCount:fDisplayParams.fMSAASampleCount]) {
|
|
|
|
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
|
|
|
|
2019-06-06 16:33:53 +00:00
|
|
|
fContext = GrContext::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) {
|
|
|
|
// in case we have outstanding refs to this guy (lua?)
|
|
|
|
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) {
|
2019-09-19 17:03:09 +00:00
|
|
|
GrMTLHandle drawable;
|
|
|
|
surface = SkSurface::MakeFromCAMetalLayer(fContext.get(), (__bridge GrMTLHandle)fMetalLayer,
|
|
|
|
kTopLeft_GrSurfaceOrigin, fSampleCount,
|
|
|
|
kBGRA_8888_SkColorType,
|
|
|
|
fDisplayParams.fColorSpace,
|
|
|
|
&fDisplayParams.fSurfaceProps,
|
|
|
|
&drawable);
|
|
|
|
// ARC is off in sk_app, so we need to release the CF ref manually
|
|
|
|
fCurrentDrawable = (id<CAMetalDrawable>)drawable;
|
|
|
|
CFRelease(drawable);
|
2019-02-08 20:36:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return surface;
|
|
|
|
}
|
|
|
|
|
|
|
|
void MetalWindowContext::swapBuffers() {
|
|
|
|
id<MTLCommandBuffer> commandBuffer = [fQueue commandBuffer];
|
|
|
|
commandBuffer.label = @"Present";
|
|
|
|
|
2019-05-16 14:31:56 +00:00
|
|
|
[commandBuffer presentDrawable:fCurrentDrawable];
|
2019-02-08 20:36:14 +00:00
|
|
|
[commandBuffer commit];
|
2019-05-16 14:31:56 +00:00
|
|
|
fCurrentDrawable = nil;
|
2019-02-08 20:36:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void MetalWindowContext::setDisplayParams(const DisplayParams& params) {
|
|
|
|
this->destroyContext();
|
|
|
|
fDisplayParams = params;
|
|
|
|
this->initializeContext();
|
|
|
|
}
|
|
|
|
|
|
|
|
} //namespace sk_app
|