blob: c464c3904a97f3214782d9e534596435b449d4fc [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 Verth2db4ba12021-02-22 09:41:27 -050068 fPipelineArchive = [*fDevice newBinaryArchiveWithDescriptor:*desc error:&error];
Jim Van Verthecc91082020-11-20 15:30:25 -050069 if (!fPipelineArchive) {
70 SkDebugf("Error creating MTLBinaryArchive:\n%s\n",
71 error.debugDescription.UTF8String);
72 }
Jim Van Verth9e640472020-11-19 12:59:06 -050073 }
74 }
Jim Van Verthecc91082020-11-20 15:30:25 -050075 } else {
Jim Van Verth95fb5782020-12-03 15:11:36 -050076 if (@available(macOS 11.0, iOS 14.0, *)) {
77 fPipelineArchive = nil;
78 }
Jim Van Verth9e640472020-11-19 12:59:06 -050079 }
80#endif
81
Jim Van Verth351c9b52020-11-12 15:21:11 -050082 GrMtlBackendContext backendContext = {};
Jim Van Verth2db4ba12021-02-22 09:41:27 -050083 backendContext.fDevice.retain((GrMTLHandle)fDevice.get());
84 backendContext.fQueue.retain((GrMTLHandle)fQueue.get());
Jim Van Verth9e640472020-11-19 12:59:06 -050085#if GR_METAL_SDK_VERSION >= 230
86 if (@available(macOS 11.0, iOS 14.0, *)) {
87 backendContext.fBinaryArchive.retain((__bridge GrMTLHandle)fPipelineArchive);
88 }
89#endif
Jim Van Verth351c9b52020-11-12 15:21:11 -050090 fContext = GrDirectContext::MakeMetal(backendContext, fDisplayParams.fGrContextOptions);
Jim Van Verthbe39f712019-02-08 15:36:14 -050091 if (!fContext && fDisplayParams.fMSAASampleCount > 1) {
92 fDisplayParams.fMSAASampleCount /= 2;
93 this->initializeContext();
94 return;
95 }
96}
97
98void MetalWindowContext::destroyContext() {
Jim Van Verthbe39f712019-02-08 15:36:14 -050099 if (fContext) {
Leon Scroggins IIIa4c80982020-07-28 10:20:58 -0400100 // in case we have outstanding refs to this (lua?)
Jim Van Verthbe39f712019-02-08 15:36:14 -0500101 fContext->abandonContext();
102 fContext.reset();
103 }
104
Jim Van Verthbe39f712019-02-08 15:36:14 -0500105 this->onDestroyContext();
Jim Van Verthd063e8b2019-05-16 10:31:56 -0400106
107 fMetalLayer = nil;
108 fValid = false;
109
Jim Van Verth9e640472020-11-19 12:59:06 -0500110#if GR_METAL_SDK_VERSION >= 230
111 if (@available(macOS 11.0, iOS 14.0, *)) {
112 [fPipelineArchive release];
113 }
114#endif
Jim Van Verth2db4ba12021-02-22 09:41:27 -0500115 fQueue.reset();
116 fDevice.reset();
Jim Van Verthbe39f712019-02-08 15:36:14 -0500117}
118
119sk_sp<SkSurface> MetalWindowContext::getBackbufferSurface() {
120 sk_sp<SkSurface> surface;
121 if (fContext) {
Jim Van Verth7c647982020-10-23 12:47:57 -0400122 if (fDisplayParams.fDelayDrawableAcquisition) {
123 surface = SkSurface::MakeFromCAMetalLayer(fContext.get(),
124 (__bridge GrMTLHandle)fMetalLayer,
125 kTopLeft_GrSurfaceOrigin, fSampleCount,
126 kBGRA_8888_SkColorType,
127 fDisplayParams.fColorSpace,
128 &fDisplayParams.fSurfaceProps,
129 &fDrawableHandle);
130 } else {
131 id<CAMetalDrawable> currentDrawable = [fMetalLayer nextDrawable];
132
133 GrMtlTextureInfo fbInfo;
134 fbInfo.fTexture.retain(currentDrawable.texture);
135
136 GrBackendRenderTarget backendRT(fWidth,
137 fHeight,
138 fSampleCount,
139 fbInfo);
140
141 surface = SkSurface::MakeFromBackendRenderTarget(fContext.get(), backendRT,
142 kTopLeft_GrSurfaceOrigin,
143 kBGRA_8888_SkColorType,
144 fDisplayParams.fColorSpace,
145 &fDisplayParams.fSurfaceProps);
146
147 fDrawableHandle = CFRetain((GrMTLHandle) currentDrawable);
148 }
Jim Van Verthbe39f712019-02-08 15:36:14 -0500149 }
150
151 return surface;
152}
153
154void MetalWindowContext::swapBuffers() {
Jim Van Verthcbf59e02019-10-17 14:58:37 -0400155 id<CAMetalDrawable> currentDrawable = (id<CAMetalDrawable>)fDrawableHandle;
Jim Van Verthcbf59e02019-10-17 14:58:37 -0400156
Jim Van Verth2db4ba12021-02-22 09:41:27 -0500157 id<MTLCommandBuffer> commandBuffer([*fQueue commandBuffer]);
Jim Van Verthbe39f712019-02-08 15:36:14 -0500158 commandBuffer.label = @"Present";
159
Jim Van Verthcbf59e02019-10-17 14:58:37 -0400160 [commandBuffer presentDrawable:currentDrawable];
Jim Van Verthbe39f712019-02-08 15:36:14 -0500161 [commandBuffer commit];
Jim Van Vertha496e3f2020-08-14 11:42:35 -0400162 // ARC is off in sk_app, so we need to release the CF ref manually
163 CFRelease(fDrawableHandle);
164 fDrawableHandle = nil;
Jim Van Verthbe39f712019-02-08 15:36:14 -0500165}
166
167void MetalWindowContext::setDisplayParams(const DisplayParams& params) {
168 this->destroyContext();
169 fDisplayParams = params;
170 this->initializeContext();
171}
172
Jim Van Verth5a89ed52020-11-16 11:06:58 -0500173void MetalWindowContext::activate(bool isActive) {
Jim Van Verth9e640472020-11-19 12:59:06 -0500174 // serialize pipeline archive
175 if (!isActive) {
176#if GR_METAL_SDK_VERSION >= 230
177 if (@available(macOS 11.0, iOS 14.0, *)) {
Jim Van Verthecc91082020-11-20 15:30:25 -0500178 if (fPipelineArchive) {
179 NSError* error;
180 [fPipelineArchive serializeToURL:CacheURL() error:&error];
181 if (error) {
182 SkDebugf("Error storing MTLBinaryArchive:\n%s\n",
183 error.debugDescription.UTF8String);
184 }
Jim Van Verth9e640472020-11-19 12:59:06 -0500185 }
186 }
187#endif
188 }
Jim Van Verth5a89ed52020-11-16 11:06:58 -0500189}
190
Jim Van Verthbe39f712019-02-08 15:36:14 -0500191} //namespace sk_app