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 | ecc9108 | 2020-11-20 15:30:25 -0500 | [diff] [blame] | 68 | NSError* error; |
Jim Van Verth | 2db4ba1 | 2021-02-22 09:41:27 -0500 | [diff] [blame] | 69 | fPipelineArchive = [*fDevice newBinaryArchiveWithDescriptor:*desc error:&error]; |
Jim Van Verth | ecc9108 | 2020-11-20 15:30:25 -0500 | [diff] [blame] | 70 | if (!fPipelineArchive) { |
| 71 | SkDebugf("Error creating MTLBinaryArchive:\n%s\n", |
| 72 | error.debugDescription.UTF8String); |
| 73 | } |
Jim Van Verth | 9e64047 | 2020-11-19 12:59:06 -0500 | [diff] [blame] | 74 | } |
| 75 | } |
Jim Van Verth | ecc9108 | 2020-11-20 15:30:25 -0500 | [diff] [blame] | 76 | } else { |
Jim Van Verth | 95fb578 | 2020-12-03 15:11:36 -0500 | [diff] [blame] | 77 | if (@available(macOS 11.0, iOS 14.0, *)) { |
| 78 | fPipelineArchive = nil; |
| 79 | } |
Jim Van Verth | 9e64047 | 2020-11-19 12:59:06 -0500 | [diff] [blame] | 80 | } |
| 81 | #endif |
| 82 | |
Jim Van Verth | 351c9b5 | 2020-11-12 15:21:11 -0500 | [diff] [blame] | 83 | GrMtlBackendContext backendContext = {}; |
Jim Van Verth | 2db4ba1 | 2021-02-22 09:41:27 -0500 | [diff] [blame] | 84 | backendContext.fDevice.retain((GrMTLHandle)fDevice.get()); |
| 85 | backendContext.fQueue.retain((GrMTLHandle)fQueue.get()); |
Jim Van Verth | 9e64047 | 2020-11-19 12:59:06 -0500 | [diff] [blame] | 86 | #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 Verth | 351c9b5 | 2020-11-12 15:21:11 -0500 | [diff] [blame] | 91 | fContext = GrDirectContext::MakeMetal(backendContext, fDisplayParams.fGrContextOptions); |
Jim Van Verth | be39f71 | 2019-02-08 15:36:14 -0500 | [diff] [blame] | 92 | if (!fContext && fDisplayParams.fMSAASampleCount > 1) { |
| 93 | fDisplayParams.fMSAASampleCount /= 2; |
| 94 | this->initializeContext(); |
| 95 | return; |
| 96 | } |
| 97 | } |
| 98 | |
| 99 | void MetalWindowContext::destroyContext() { |
Jim Van Verth | be39f71 | 2019-02-08 15:36:14 -0500 | [diff] [blame] | 100 | if (fContext) { |
Leon Scroggins III | a4c8098 | 2020-07-28 10:20:58 -0400 | [diff] [blame] | 101 | // in case we have outstanding refs to this (lua?) |
Jim Van Verth | be39f71 | 2019-02-08 15:36:14 -0500 | [diff] [blame] | 102 | fContext->abandonContext(); |
| 103 | fContext.reset(); |
| 104 | } |
| 105 | |
Jim Van Verth | be39f71 | 2019-02-08 15:36:14 -0500 | [diff] [blame] | 106 | this->onDestroyContext(); |
Jim Van Verth | d063e8b | 2019-05-16 10:31:56 -0400 | [diff] [blame] | 107 | |
| 108 | fMetalLayer = nil; |
| 109 | fValid = false; |
| 110 | |
Jim Van Verth | 9e64047 | 2020-11-19 12:59:06 -0500 | [diff] [blame] | 111 | #if GR_METAL_SDK_VERSION >= 230 |
| 112 | if (@available(macOS 11.0, iOS 14.0, *)) { |
| 113 | [fPipelineArchive release]; |
| 114 | } |
| 115 | #endif |
Jim Van Verth | 2db4ba1 | 2021-02-22 09:41:27 -0500 | [diff] [blame] | 116 | fQueue.reset(); |
| 117 | fDevice.reset(); |
Jim Van Verth | be39f71 | 2019-02-08 15:36:14 -0500 | [diff] [blame] | 118 | } |
| 119 | |
| 120 | sk_sp<SkSurface> MetalWindowContext::getBackbufferSurface() { |
| 121 | sk_sp<SkSurface> surface; |
| 122 | if (fContext) { |
Jim Van Verth | 7c64798 | 2020-10-23 12:47:57 -0400 | [diff] [blame] | 123 | 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 Verth | be39f71 | 2019-02-08 15:36:14 -0500 | [diff] [blame] | 150 | } |
| 151 | |
| 152 | return surface; |
| 153 | } |
| 154 | |
| 155 | void MetalWindowContext::swapBuffers() { |
Jim Van Verth | cbf59e0 | 2019-10-17 14:58:37 -0400 | [diff] [blame] | 156 | id<CAMetalDrawable> currentDrawable = (id<CAMetalDrawable>)fDrawableHandle; |
Jim Van Verth | cbf59e0 | 2019-10-17 14:58:37 -0400 | [diff] [blame] | 157 | |
Jim Van Verth | 2db4ba1 | 2021-02-22 09:41:27 -0500 | [diff] [blame] | 158 | id<MTLCommandBuffer> commandBuffer([*fQueue commandBuffer]); |
Jim Van Verth | be39f71 | 2019-02-08 15:36:14 -0500 | [diff] [blame] | 159 | commandBuffer.label = @"Present"; |
| 160 | |
Jim Van Verth | cbf59e0 | 2019-10-17 14:58:37 -0400 | [diff] [blame] | 161 | [commandBuffer presentDrawable:currentDrawable]; |
Jim Van Verth | be39f71 | 2019-02-08 15:36:14 -0500 | [diff] [blame] | 162 | [commandBuffer commit]; |
Jim Van Verth | a496e3f | 2020-08-14 11:42:35 -0400 | [diff] [blame] | 163 | // ARC is off in sk_app, so we need to release the CF ref manually |
| 164 | CFRelease(fDrawableHandle); |
| 165 | fDrawableHandle = nil; |
Jim Van Verth | be39f71 | 2019-02-08 15:36:14 -0500 | [diff] [blame] | 166 | } |
| 167 | |
| 168 | void MetalWindowContext::setDisplayParams(const DisplayParams& params) { |
| 169 | this->destroyContext(); |
| 170 | fDisplayParams = params; |
| 171 | this->initializeContext(); |
| 172 | } |
| 173 | |
Jim Van Verth | 5a89ed5 | 2020-11-16 11:06:58 -0500 | [diff] [blame] | 174 | void MetalWindowContext::activate(bool isActive) { |
Jim Van Verth | 9e64047 | 2020-11-19 12:59:06 -0500 | [diff] [blame] | 175 | // serialize pipeline archive |
| 176 | if (!isActive) { |
| 177 | #if GR_METAL_SDK_VERSION >= 230 |
| 178 | if (@available(macOS 11.0, iOS 14.0, *)) { |
Jim Van Verth | ecc9108 | 2020-11-20 15:30:25 -0500 | [diff] [blame] | 179 | 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 Verth | 9e64047 | 2020-11-19 12:59:06 -0500 | [diff] [blame] | 186 | } |
| 187 | } |
| 188 | #endif |
| 189 | } |
Jim Van Verth | 5a89ed5 | 2020-11-16 11:06:58 -0500 | [diff] [blame] | 190 | } |
| 191 | |
Jim Van Verth | be39f71 | 2019-02-08 15:36:14 -0500 | [diff] [blame] | 192 | } //namespace sk_app |