blob: 03bddf5f1b4527620b5da7f5cc1ffca356ed232b [file] [log] [blame]
Jim Van Verthbe39f712019-02-08 15:36:14 -05001/*
2 * Copyright 2019 Google Inc.
3 *
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
6 */
7
Mike Kleinc0bd9f92019-04-23 12:05:21 -05008#include "include/core/SkCanvas.h"
9#include "include/core/SkSurface.h"
10#include "include/gpu/GrBackendSurface.h"
11#include "include/gpu/GrContext.h"
12#include "include/gpu/mtl/GrMtlTypes.h"
13#include "src/core/SkMathPriv.h"
14#include "src/gpu/GrCaps.h"
15#include "src/gpu/GrContextPriv.h"
16#include "src/image/SkImage_Base.h"
17#include "tools/sk_app/MetalWindowContext.h"
Jim Van Verthbe39f712019-02-08 15:36:14 -050018
Jim Van Verthd063e8b2019-05-16 10:31:56 -040019using sk_app::DisplayParams;
20using sk_app::MetalWindowContext;
Jim Van Verthbe39f712019-02-08 15:36:14 -050021
Jim Van Verthd063e8b2019-05-16 10:31:56 -040022namespace sk_app {
Jim Van Verthbe39f712019-02-08 15:36:14 -050023
24MetalWindowContext::MetalWindowContext(const DisplayParams& params)
25 : WindowContext(params)
Jim Van Verthd063e8b2019-05-16 10:31:56 -040026 , fValid(false) {
27
Jim Van Verthbe39f712019-02-08 15:36:14 -050028 fDisplayParams.fMSAASampleCount = GrNextPow2(fDisplayParams.fMSAASampleCount);
29}
30
31void MetalWindowContext::initializeContext() {
32 SkASSERT(!fContext);
33
Jim Van Verthbe39f712019-02-08 15:36:14 -050034 fDevice = MTLCreateSystemDefaultDevice();
35 fQueue = [fDevice newCommandQueue];
36
Jim Van Verthd063e8b2019-05-16 10:31:56 -040037 if (fDisplayParams.fMSAASampleCount > 1) {
Jim Van Verth598903f2019-10-07 15:52:47 -040038 if (@available(macOS 10.11, iOS 9.0, *)) {
39 if (![fDevice supportsTextureSampleCount:fDisplayParams.fMSAASampleCount]) {
40 return;
41 }
42 } else {
Jim Van Verthd063e8b2019-05-16 10:31:56 -040043 return;
44 }
45 }
Jim Van Verthd361e642019-07-11 15:40:53 -040046 fSampleCount = fDisplayParams.fMSAASampleCount;
Jim Van Verthd063e8b2019-05-16 10:31:56 -040047 fStencilBits = 8;
48
Jim Van Verthbe39f712019-02-08 15:36:14 -050049 fValid = this->onInitializeContext();
Jim Van Verthd063e8b2019-05-16 10:31:56 -040050
Jim Van Verth6e4fee82019-06-06 12:33:53 -040051 fContext = GrContext::MakeMetal((__bridge void*)fDevice, (__bridge void*)fQueue,
52 fDisplayParams.fGrContextOptions);
Jim Van Verthbe39f712019-02-08 15:36:14 -050053 if (!fContext && fDisplayParams.fMSAASampleCount > 1) {
54 fDisplayParams.fMSAASampleCount /= 2;
55 this->initializeContext();
56 return;
57 }
58}
59
60void MetalWindowContext::destroyContext() {
Jim Van Verthbe39f712019-02-08 15:36:14 -050061 if (fContext) {
62 // in case we have outstanding refs to this guy (lua?)
63 fContext->abandonContext();
64 fContext.reset();
65 }
66
Jim Van Verthbe39f712019-02-08 15:36:14 -050067 this->onDestroyContext();
Jim Van Verthd063e8b2019-05-16 10:31:56 -040068
69 fMetalLayer = nil;
70 fValid = false;
71
Jim Van Verth6e4fee82019-06-06 12:33:53 -040072 [fQueue release];
73 [fDevice release];
Jim Van Verthbe39f712019-02-08 15:36:14 -050074}
75
76sk_sp<SkSurface> MetalWindowContext::getBackbufferSurface() {
77 sk_sp<SkSurface> surface;
78 if (fContext) {
Jim Van Verthb2f55532019-09-19 13:03:09 -040079 surface = SkSurface::MakeFromCAMetalLayer(fContext.get(), (__bridge GrMTLHandle)fMetalLayer,
80 kTopLeft_GrSurfaceOrigin, fSampleCount,
81 kBGRA_8888_SkColorType,
82 fDisplayParams.fColorSpace,
83 &fDisplayParams.fSurfaceProps,
Jim Van Verthcbf59e02019-10-17 14:58:37 -040084 &fDrawableHandle);
Jim Van Verthbe39f712019-02-08 15:36:14 -050085 }
86
87 return surface;
88}
89
90void MetalWindowContext::swapBuffers() {
Jim Van Verthcbf59e02019-10-17 14:58:37 -040091 // ARC is off in sk_app, so we need to release the CF ref manually
92 id<CAMetalDrawable> currentDrawable = (id<CAMetalDrawable>)fDrawableHandle;
93 CFRelease(fDrawableHandle);
94
Jim Van Verthbe39f712019-02-08 15:36:14 -050095 id<MTLCommandBuffer> commandBuffer = [fQueue commandBuffer];
96 commandBuffer.label = @"Present";
97
Jim Van Verthcbf59e02019-10-17 14:58:37 -040098 [commandBuffer presentDrawable:currentDrawable];
Jim Van Verthbe39f712019-02-08 15:36:14 -050099 [commandBuffer commit];
Jim Van Verthbe39f712019-02-08 15:36:14 -0500100}
101
102void MetalWindowContext::setDisplayParams(const DisplayParams& params) {
103 this->destroyContext();
104 fDisplayParams = params;
105 this->initializeContext();
106}
107
108} //namespace sk_app