blob: 5b623811ed4c164af3a660f34ab539b0f88b263f [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"
Robert Phillipsed653392020-07-10 13:55:21 -040011#include "include/gpu/GrDirectContext.h"
Jim Van Verth351c9b52020-11-12 15:21:11 -050012#include "include/gpu/mtl/GrMtlBackendContext.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050013#include "include/gpu/mtl/GrMtlTypes.h"
14#include "src/core/SkMathPriv.h"
15#include "src/gpu/GrCaps.h"
Adlai Hollera0693042020-10-14 11:23:11 -040016#include "src/gpu/GrDirectContextPriv.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050017#include "src/image/SkImage_Base.h"
18#include "tools/sk_app/MetalWindowContext.h"
Jim Van Verthbe39f712019-02-08 15:36:14 -050019
Jim Van Verthd063e8b2019-05-16 10:31:56 -040020using sk_app::DisplayParams;
21using sk_app::MetalWindowContext;
Jim Van Verthbe39f712019-02-08 15:36:14 -050022
Jim Van Verthd063e8b2019-05-16 10:31:56 -040023namespace sk_app {
Jim Van Verthbe39f712019-02-08 15:36:14 -050024
25MetalWindowContext::MetalWindowContext(const DisplayParams& params)
Robert Phillipsed653392020-07-10 13:55:21 -040026 : WindowContext(params)
Jim Van Verth2db4ba12021-02-22 09:41:27 -050027 , fValid(false)
28 , fDrawableHandle(nil) {
Jim Van Verthbe39f712019-02-08 15:36:14 -050029 fDisplayParams.fMSAASampleCount = GrNextPow2(fDisplayParams.fMSAASampleCount);
30}
31
Jim Van Verth9e640472020-11-19 12:59:06 -050032NSURL* MetalWindowContext::CacheURL() {
33 NSArray *paths = [[NSFileManager defaultManager] URLsForDirectory:NSCachesDirectory
34 inDomains:NSUserDomainMask];
35 NSURL* cachePath = [paths objectAtIndex:0];
36 return [cachePath URLByAppendingPathComponent:@"binaryArchive.metallib"];
37}
38
Jim Van Verthbe39f712019-02-08 15:36:14 -050039void MetalWindowContext::initializeContext() {
40 SkASSERT(!fContext);
41
Jim Van Verth2db4ba12021-02-22 09:41:27 -050042 fDevice.reset(MTLCreateSystemDefaultDevice());
43 fQueue.reset([*fDevice newCommandQueue]);
Jim Van Verthbe39f712019-02-08 15:36:14 -050044
Jim Van Verthd063e8b2019-05-16 10:31:56 -040045 if (fDisplayParams.fMSAASampleCount > 1) {
Jim Van Verth598903f2019-10-07 15:52:47 -040046 if (@available(macOS 10.11, iOS 9.0, *)) {
Jim Van Verth2db4ba12021-02-22 09:41:27 -050047 if (![*fDevice supportsTextureSampleCount:fDisplayParams.fMSAASampleCount]) {
Jim Van Verth598903f2019-10-07 15:52:47 -040048 return;
49 }
50 } else {
Jim Van Verthd063e8b2019-05-16 10:31:56 -040051 return;
52 }
53 }
Jim Van Verthd361e642019-07-11 15:40:53 -040054 fSampleCount = fDisplayParams.fMSAASampleCount;
Jim Van Verthd063e8b2019-05-16 10:31:56 -040055 fStencilBits = 8;
56
Jim Van Verthbe39f712019-02-08 15:36:14 -050057 fValid = this->onInitializeContext();
Jim Van Verthd063e8b2019-05-16 10:31:56 -040058
Jim Van Verth9e640472020-11-19 12:59:06 -050059#if GR_METAL_SDK_VERSION >= 230
Jim Van Verthecc91082020-11-20 15:30:25 -050060 if (fDisplayParams.fEnableBinaryArchive) {
61 if (@available(macOS 11.0, iOS 14.0, *)) {
Jim Van Verth2db4ba12021-02-22 09:41:27 -050062 sk_cfp<MTLBinaryArchiveDescriptor*> desc([MTLBinaryArchiveDescriptor new]);
63 (*desc).url = CacheURL(); // try to load
Jim Van Verth9e640472020-11-19 12:59:06 -050064 NSError* error;
Jim Van Verth2db4ba12021-02-22 09:41:27 -050065 fPipelineArchive = [*fDevice newBinaryArchiveWithDescriptor:*desc error:&error];
Jim Van Verth9e640472020-11-19 12:59:06 -050066 if (!fPipelineArchive) {
Jim Van Verth2db4ba12021-02-22 09:41:27 -050067 (*desc).url = nil; // create new
Jim Van Verthecc91082020-11-20 15:30:25 -050068 NSError* error;
Jim Van Verth2db4ba12021-02-22 09:41:27 -050069 fPipelineArchive = [*fDevice newBinaryArchiveWithDescriptor:*desc error:&error];
Jim Van Verthecc91082020-11-20 15:30:25 -050070 if (!fPipelineArchive) {
71 SkDebugf("Error creating MTLBinaryArchive:\n%s\n",
72 error.debugDescription.UTF8String);
73 }
Jim Van Verth9e640472020-11-19 12:59:06 -050074 }
75 }
Jim Van Verthecc91082020-11-20 15:30:25 -050076 } else {
Jim Van Verth95fb5782020-12-03 15:11:36 -050077 if (@available(macOS 11.0, iOS 14.0, *)) {
78 fPipelineArchive = nil;
79 }
Jim Van Verth9e640472020-11-19 12:59:06 -050080 }
81#endif
82
Jim Van Verth351c9b52020-11-12 15:21:11 -050083 GrMtlBackendContext backendContext = {};
Jim Van Verth2db4ba12021-02-22 09:41:27 -050084 backendContext.fDevice.retain((GrMTLHandle)fDevice.get());
85 backendContext.fQueue.retain((GrMTLHandle)fQueue.get());
Jim Van Verth9e640472020-11-19 12:59:06 -050086#if GR_METAL_SDK_VERSION >= 230
87 if (@available(macOS 11.0, iOS 14.0, *)) {
88 backendContext.fBinaryArchive.retain((__bridge GrMTLHandle)fPipelineArchive);
89 }
90#endif
Jim Van Verth351c9b52020-11-12 15:21:11 -050091 fContext = GrDirectContext::MakeMetal(backendContext, fDisplayParams.fGrContextOptions);
Jim Van Verthbe39f712019-02-08 15:36:14 -050092 if (!fContext && fDisplayParams.fMSAASampleCount > 1) {
93 fDisplayParams.fMSAASampleCount /= 2;
94 this->initializeContext();
95 return;
96 }
97}
98
99void MetalWindowContext::destroyContext() {
Jim Van Verthbe39f712019-02-08 15:36:14 -0500100 if (fContext) {
Leon Scroggins IIIa4c80982020-07-28 10:20:58 -0400101 // in case we have outstanding refs to this (lua?)
Jim Van Verthbe39f712019-02-08 15:36:14 -0500102 fContext->abandonContext();
103 fContext.reset();
104 }
105
Jim Van Verthbe39f712019-02-08 15:36:14 -0500106 this->onDestroyContext();
Jim Van Verthd063e8b2019-05-16 10:31:56 -0400107
108 fMetalLayer = nil;
109 fValid = false;
110
Jim Van Verth9e640472020-11-19 12:59:06 -0500111#if GR_METAL_SDK_VERSION >= 230
112 if (@available(macOS 11.0, iOS 14.0, *)) {
113 [fPipelineArchive release];
114 }
115#endif
Jim Van Verth2db4ba12021-02-22 09:41:27 -0500116 fQueue.reset();
117 fDevice.reset();
Jim Van Verthbe39f712019-02-08 15:36:14 -0500118}
119
120sk_sp<SkSurface> MetalWindowContext::getBackbufferSurface() {
121 sk_sp<SkSurface> surface;
122 if (fContext) {
Jim Van Verth7c647982020-10-23 12:47:57 -0400123 if (fDisplayParams.fDelayDrawableAcquisition) {
124 surface = SkSurface::MakeFromCAMetalLayer(fContext.get(),
125 (__bridge GrMTLHandle)fMetalLayer,
126 kTopLeft_GrSurfaceOrigin, fSampleCount,
127 kBGRA_8888_SkColorType,
128 fDisplayParams.fColorSpace,
129 &fDisplayParams.fSurfaceProps,
130 &fDrawableHandle);
131 } else {
132 id<CAMetalDrawable> currentDrawable = [fMetalLayer nextDrawable];
133
134 GrMtlTextureInfo fbInfo;
135 fbInfo.fTexture.retain(currentDrawable.texture);
136
137 GrBackendRenderTarget backendRT(fWidth,
138 fHeight,
139 fSampleCount,
140 fbInfo);
141
142 surface = SkSurface::MakeFromBackendRenderTarget(fContext.get(), backendRT,
143 kTopLeft_GrSurfaceOrigin,
144 kBGRA_8888_SkColorType,
145 fDisplayParams.fColorSpace,
146 &fDisplayParams.fSurfaceProps);
147
148 fDrawableHandle = CFRetain((GrMTLHandle) currentDrawable);
149 }
Jim Van Verthbe39f712019-02-08 15:36:14 -0500150 }
151
152 return surface;
153}
154
155void MetalWindowContext::swapBuffers() {
Jim Van Verthcbf59e02019-10-17 14:58:37 -0400156 id<CAMetalDrawable> currentDrawable = (id<CAMetalDrawable>)fDrawableHandle;
Jim Van Verthcbf59e02019-10-17 14:58:37 -0400157
Jim Van Verth2db4ba12021-02-22 09:41:27 -0500158 id<MTLCommandBuffer> commandBuffer([*fQueue commandBuffer]);
Jim Van Verthbe39f712019-02-08 15:36:14 -0500159 commandBuffer.label = @"Present";
160
Jim Van Verthcbf59e02019-10-17 14:58:37 -0400161 [commandBuffer presentDrawable:currentDrawable];
Jim Van Verthbe39f712019-02-08 15:36:14 -0500162 [commandBuffer commit];
Jim Van Vertha496e3f2020-08-14 11:42:35 -0400163 // ARC is off in sk_app, so we need to release the CF ref manually
164 CFRelease(fDrawableHandle);
165 fDrawableHandle = nil;
Jim Van Verthbe39f712019-02-08 15:36:14 -0500166}
167
168void MetalWindowContext::setDisplayParams(const DisplayParams& params) {
169 this->destroyContext();
170 fDisplayParams = params;
171 this->initializeContext();
172}
173
Jim Van Verth5a89ed52020-11-16 11:06:58 -0500174void MetalWindowContext::activate(bool isActive) {
Jim Van Verth9e640472020-11-19 12:59:06 -0500175 // serialize pipeline archive
176 if (!isActive) {
177#if GR_METAL_SDK_VERSION >= 230
178 if (@available(macOS 11.0, iOS 14.0, *)) {
Jim Van Verthecc91082020-11-20 15:30:25 -0500179 if (fPipelineArchive) {
180 NSError* error;
181 [fPipelineArchive serializeToURL:CacheURL() error:&error];
182 if (error) {
183 SkDebugf("Error storing MTLBinaryArchive:\n%s\n",
184 error.debugDescription.UTF8String);
185 }
Jim Van Verth9e640472020-11-19 12:59:06 -0500186 }
187 }
188#endif
189 }
Jim Van Verth5a89ed52020-11-16 11:06:58 -0500190}
191
Jim Van Verthbe39f712019-02-08 15:36:14 -0500192} //namespace sk_app