Jim Van Verth | be39f71 | 2019-02-08 15:36:14 -0500 | [diff] [blame] | 1 | /* |
| 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 Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 8 | #include "include/core/SkCanvas.h" |
| 9 | #include "include/core/SkSurface.h" |
| 10 | #include "include/gpu/GrBackendSurface.h" |
Robert Phillips | ed65339 | 2020-07-10 13:55:21 -0400 | [diff] [blame] | 11 | #include "include/gpu/GrDirectContext.h" |
Jim Van Verth | 351c9b5 | 2020-11-12 15:21:11 -0500 | [diff] [blame] | 12 | #include "include/gpu/mtl/GrMtlBackendContext.h" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 13 | #include "include/gpu/mtl/GrMtlTypes.h" |
| 14 | #include "src/core/SkMathPriv.h" |
| 15 | #include "src/gpu/GrCaps.h" |
Adlai Holler | a069304 | 2020-10-14 11:23:11 -0400 | [diff] [blame] | 16 | #include "src/gpu/GrDirectContextPriv.h" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 17 | #include "src/image/SkImage_Base.h" |
| 18 | #include "tools/sk_app/MetalWindowContext.h" |
Jim Van Verth | be39f71 | 2019-02-08 15:36:14 -0500 | [diff] [blame] | 19 | |
Jim Van Verth | d063e8b | 2019-05-16 10:31:56 -0400 | [diff] [blame] | 20 | using sk_app::DisplayParams; |
| 21 | using sk_app::MetalWindowContext; |
Jim Van Verth | be39f71 | 2019-02-08 15:36:14 -0500 | [diff] [blame] | 22 | |
Jim Van Verth | d063e8b | 2019-05-16 10:31:56 -0400 | [diff] [blame] | 23 | namespace sk_app { |
Jim Van Verth | be39f71 | 2019-02-08 15:36:14 -0500 | [diff] [blame] | 24 | |
| 25 | MetalWindowContext::MetalWindowContext(const DisplayParams& params) |
Robert Phillips | ed65339 | 2020-07-10 13:55:21 -0400 | [diff] [blame] | 26 | : WindowContext(params) |
Jim Van Verth | 2db4ba1 | 2021-02-22 09:41:27 -0500 | [diff] [blame] | 27 | , fValid(false) |
| 28 | , fDrawableHandle(nil) { |
Jim Van Verth | be39f71 | 2019-02-08 15:36:14 -0500 | [diff] [blame] | 29 | fDisplayParams.fMSAASampleCount = GrNextPow2(fDisplayParams.fMSAASampleCount); |
| 30 | } |
| 31 | |
Jim Van Verth | 9e64047 | 2020-11-19 12:59:06 -0500 | [diff] [blame] | 32 | NSURL* 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 Verth | be39f71 | 2019-02-08 15:36:14 -0500 | [diff] [blame] | 39 | void MetalWindowContext::initializeContext() { |
| 40 | SkASSERT(!fContext); |
| 41 | |
Jim Van Verth | 2db4ba1 | 2021-02-22 09:41:27 -0500 | [diff] [blame] | 42 | fDevice.reset(MTLCreateSystemDefaultDevice()); |
| 43 | fQueue.reset([*fDevice newCommandQueue]); |
Jim Van Verth | be39f71 | 2019-02-08 15:36:14 -0500 | [diff] [blame] | 44 | |
Jim Van Verth | d063e8b | 2019-05-16 10:31:56 -0400 | [diff] [blame] | 45 | if (fDisplayParams.fMSAASampleCount > 1) { |
Jim Van Verth | 598903f | 2019-10-07 15:52:47 -0400 | [diff] [blame] | 46 | if (@available(macOS 10.11, iOS 9.0, *)) { |
Jim Van Verth | 2db4ba1 | 2021-02-22 09:41:27 -0500 | [diff] [blame] | 47 | if (![*fDevice supportsTextureSampleCount:fDisplayParams.fMSAASampleCount]) { |
Jim Van Verth | 598903f | 2019-10-07 15:52:47 -0400 | [diff] [blame] | 48 | return; |
| 49 | } |
| 50 | } else { |
Jim Van Verth | d063e8b | 2019-05-16 10:31:56 -0400 | [diff] [blame] | 51 | return; |
| 52 | } |
| 53 | } |
Jim Van Verth | d361e64 | 2019-07-11 15:40:53 -0400 | [diff] [blame] | 54 | fSampleCount = fDisplayParams.fMSAASampleCount; |
Jim Van Verth | d063e8b | 2019-05-16 10:31:56 -0400 | [diff] [blame] | 55 | fStencilBits = 8; |
| 56 | |
Jim Van Verth | be39f71 | 2019-02-08 15:36:14 -0500 | [diff] [blame] | 57 | fValid = this->onInitializeContext(); |
Jim Van Verth | d063e8b | 2019-05-16 10:31:56 -0400 | [diff] [blame] | 58 | |
Jim Van Verth | 9e64047 | 2020-11-19 12:59:06 -0500 | [diff] [blame] | 59 | #if GR_METAL_SDK_VERSION >= 230 |
Jim Van Verth | ecc9108 | 2020-11-20 15:30:25 -0500 | [diff] [blame] | 60 | if (fDisplayParams.fEnableBinaryArchive) { |
| 61 | if (@available(macOS 11.0, iOS 14.0, *)) { |
Jim Van Verth | 2db4ba1 | 2021-02-22 09:41:27 -0500 | [diff] [blame] | 62 | sk_cfp<MTLBinaryArchiveDescriptor*> desc([MTLBinaryArchiveDescriptor new]); |
| 63 | (*desc).url = CacheURL(); // try to load |
Jim Van Verth | 9e64047 | 2020-11-19 12:59:06 -0500 | [diff] [blame] | 64 | NSError* error; |
Jim Van Verth | 2db4ba1 | 2021-02-22 09:41:27 -0500 | [diff] [blame] | 65 | fPipelineArchive = [*fDevice newBinaryArchiveWithDescriptor:*desc error:&error]; |
Jim Van Verth | 9e64047 | 2020-11-19 12:59:06 -0500 | [diff] [blame] | 66 | if (!fPipelineArchive) { |
Jim Van Verth | 2db4ba1 | 2021-02-22 09:41:27 -0500 | [diff] [blame] | 67 | (*desc).url = nil; // create new |
Jim Van Verth | 2db4ba1 | 2021-02-22 09:41:27 -0500 | [diff] [blame] | 68 | fPipelineArchive = [*fDevice newBinaryArchiveWithDescriptor:*desc error:&error]; |
Jim Van Verth | ecc9108 | 2020-11-20 15:30:25 -0500 | [diff] [blame] | 69 | if (!fPipelineArchive) { |
| 70 | SkDebugf("Error creating MTLBinaryArchive:\n%s\n", |
| 71 | error.debugDescription.UTF8String); |
| 72 | } |
Jim Van Verth | 9e64047 | 2020-11-19 12:59:06 -0500 | [diff] [blame] | 73 | } |
| 74 | } |
Jim Van Verth | ecc9108 | 2020-11-20 15:30:25 -0500 | [diff] [blame] | 75 | } else { |
Jim Van Verth | 95fb578 | 2020-12-03 15:11:36 -0500 | [diff] [blame] | 76 | if (@available(macOS 11.0, iOS 14.0, *)) { |
| 77 | fPipelineArchive = nil; |
| 78 | } |
Jim Van Verth | 9e64047 | 2020-11-19 12:59:06 -0500 | [diff] [blame] | 79 | } |
| 80 | #endif |
| 81 | |
Jim Van Verth | 351c9b5 | 2020-11-12 15:21:11 -0500 | [diff] [blame] | 82 | GrMtlBackendContext backendContext = {}; |
Jim Van Verth | 2db4ba1 | 2021-02-22 09:41:27 -0500 | [diff] [blame] | 83 | backendContext.fDevice.retain((GrMTLHandle)fDevice.get()); |
| 84 | backendContext.fQueue.retain((GrMTLHandle)fQueue.get()); |
Jim Van Verth | 9e64047 | 2020-11-19 12:59:06 -0500 | [diff] [blame] | 85 | #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 Verth | 351c9b5 | 2020-11-12 15:21:11 -0500 | [diff] [blame] | 90 | fContext = GrDirectContext::MakeMetal(backendContext, fDisplayParams.fGrContextOptions); |
Jim Van Verth | be39f71 | 2019-02-08 15:36:14 -0500 | [diff] [blame] | 91 | if (!fContext && fDisplayParams.fMSAASampleCount > 1) { |
| 92 | fDisplayParams.fMSAASampleCount /= 2; |
| 93 | this->initializeContext(); |
| 94 | return; |
| 95 | } |
| 96 | } |
| 97 | |
| 98 | void MetalWindowContext::destroyContext() { |
Jim Van Verth | be39f71 | 2019-02-08 15:36:14 -0500 | [diff] [blame] | 99 | if (fContext) { |
Leon Scroggins III | a4c8098 | 2020-07-28 10:20:58 -0400 | [diff] [blame] | 100 | // in case we have outstanding refs to this (lua?) |
Jim Van Verth | be39f71 | 2019-02-08 15:36:14 -0500 | [diff] [blame] | 101 | fContext->abandonContext(); |
| 102 | fContext.reset(); |
| 103 | } |
| 104 | |
Jim Van Verth | be39f71 | 2019-02-08 15:36:14 -0500 | [diff] [blame] | 105 | this->onDestroyContext(); |
Jim Van Verth | d063e8b | 2019-05-16 10:31:56 -0400 | [diff] [blame] | 106 | |
| 107 | fMetalLayer = nil; |
| 108 | fValid = false; |
| 109 | |
Jim Van Verth | 9e64047 | 2020-11-19 12:59:06 -0500 | [diff] [blame] | 110 | #if GR_METAL_SDK_VERSION >= 230 |
| 111 | if (@available(macOS 11.0, iOS 14.0, *)) { |
| 112 | [fPipelineArchive release]; |
| 113 | } |
| 114 | #endif |
Jim Van Verth | 2db4ba1 | 2021-02-22 09:41:27 -0500 | [diff] [blame] | 115 | fQueue.reset(); |
| 116 | fDevice.reset(); |
Jim Van Verth | be39f71 | 2019-02-08 15:36:14 -0500 | [diff] [blame] | 117 | } |
| 118 | |
| 119 | sk_sp<SkSurface> MetalWindowContext::getBackbufferSurface() { |
| 120 | sk_sp<SkSurface> surface; |
| 121 | if (fContext) { |
Jim Van Verth | 7c64798 | 2020-10-23 12:47:57 -0400 | [diff] [blame] | 122 | 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 Verth | be39f71 | 2019-02-08 15:36:14 -0500 | [diff] [blame] | 149 | } |
| 150 | |
| 151 | return surface; |
| 152 | } |
| 153 | |
| 154 | void MetalWindowContext::swapBuffers() { |
Jim Van Verth | cbf59e0 | 2019-10-17 14:58:37 -0400 | [diff] [blame] | 155 | id<CAMetalDrawable> currentDrawable = (id<CAMetalDrawable>)fDrawableHandle; |
Jim Van Verth | cbf59e0 | 2019-10-17 14:58:37 -0400 | [diff] [blame] | 156 | |
Jim Van Verth | 2db4ba1 | 2021-02-22 09:41:27 -0500 | [diff] [blame] | 157 | id<MTLCommandBuffer> commandBuffer([*fQueue commandBuffer]); |
Jim Van Verth | be39f71 | 2019-02-08 15:36:14 -0500 | [diff] [blame] | 158 | commandBuffer.label = @"Present"; |
| 159 | |
Jim Van Verth | cbf59e0 | 2019-10-17 14:58:37 -0400 | [diff] [blame] | 160 | [commandBuffer presentDrawable:currentDrawable]; |
Jim Van Verth | be39f71 | 2019-02-08 15:36:14 -0500 | [diff] [blame] | 161 | [commandBuffer commit]; |
Jim Van Verth | a496e3f | 2020-08-14 11:42:35 -0400 | [diff] [blame] | 162 | // ARC is off in sk_app, so we need to release the CF ref manually |
| 163 | CFRelease(fDrawableHandle); |
| 164 | fDrawableHandle = nil; |
Jim Van Verth | be39f71 | 2019-02-08 15:36:14 -0500 | [diff] [blame] | 165 | } |
| 166 | |
| 167 | void MetalWindowContext::setDisplayParams(const DisplayParams& params) { |
| 168 | this->destroyContext(); |
| 169 | fDisplayParams = params; |
| 170 | this->initializeContext(); |
| 171 | } |
| 172 | |
Jim Van Verth | 5a89ed5 | 2020-11-16 11:06:58 -0500 | [diff] [blame] | 173 | void MetalWindowContext::activate(bool isActive) { |
Jim Van Verth | 9e64047 | 2020-11-19 12:59:06 -0500 | [diff] [blame] | 174 | // serialize pipeline archive |
| 175 | if (!isActive) { |
| 176 | #if GR_METAL_SDK_VERSION >= 230 |
| 177 | if (@available(macOS 11.0, iOS 14.0, *)) { |
Jim Van Verth | ecc9108 | 2020-11-20 15:30:25 -0500 | [diff] [blame] | 178 | 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 Verth | 9e64047 | 2020-11-19 12:59:06 -0500 | [diff] [blame] | 185 | } |
| 186 | } |
| 187 | #endif |
| 188 | } |
Jim Van Verth | 5a89ed5 | 2020-11-16 11:06:58 -0500 | [diff] [blame] | 189 | } |
| 190 | |
Jim Van Verth | be39f71 | 2019-02-08 15:36:14 -0500 | [diff] [blame] | 191 | } //namespace sk_app |