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" |
| 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 Verth | be39f71 | 2019-02-08 15:36:14 -0500 | [diff] [blame] | 18 | |
Jim Van Verth | d063e8b | 2019-05-16 10:31:56 -0400 | [diff] [blame] | 19 | using sk_app::DisplayParams; |
| 20 | using sk_app::MetalWindowContext; |
Jim Van Verth | be39f71 | 2019-02-08 15:36:14 -0500 | [diff] [blame] | 21 | |
Jim Van Verth | d063e8b | 2019-05-16 10:31:56 -0400 | [diff] [blame] | 22 | namespace sk_app { |
Jim Van Verth | be39f71 | 2019-02-08 15:36:14 -0500 | [diff] [blame] | 23 | |
| 24 | MetalWindowContext::MetalWindowContext(const DisplayParams& params) |
| 25 | : WindowContext(params) |
Jim Van Verth | d063e8b | 2019-05-16 10:31:56 -0400 | [diff] [blame] | 26 | , fValid(false) { |
| 27 | |
Jim Van Verth | be39f71 | 2019-02-08 15:36:14 -0500 | [diff] [blame] | 28 | fDisplayParams.fMSAASampleCount = GrNextPow2(fDisplayParams.fMSAASampleCount); |
| 29 | } |
| 30 | |
| 31 | void MetalWindowContext::initializeContext() { |
| 32 | SkASSERT(!fContext); |
| 33 | |
Jim Van Verth | be39f71 | 2019-02-08 15:36:14 -0500 | [diff] [blame] | 34 | fDevice = MTLCreateSystemDefaultDevice(); |
| 35 | fQueue = [fDevice newCommandQueue]; |
| 36 | |
Jim Van Verth | d063e8b | 2019-05-16 10:31:56 -0400 | [diff] [blame] | 37 | if (fDisplayParams.fMSAASampleCount > 1) { |
Jim Van Verth | 598903f | 2019-10-07 15:52:47 -0400 | [diff] [blame] | 38 | if (@available(macOS 10.11, iOS 9.0, *)) { |
| 39 | if (![fDevice supportsTextureSampleCount:fDisplayParams.fMSAASampleCount]) { |
| 40 | return; |
| 41 | } |
| 42 | } else { |
Jim Van Verth | d063e8b | 2019-05-16 10:31:56 -0400 | [diff] [blame] | 43 | return; |
| 44 | } |
| 45 | } |
Jim Van Verth | d361e64 | 2019-07-11 15:40:53 -0400 | [diff] [blame] | 46 | fSampleCount = fDisplayParams.fMSAASampleCount; |
Jim Van Verth | d063e8b | 2019-05-16 10:31:56 -0400 | [diff] [blame] | 47 | fStencilBits = 8; |
| 48 | |
Jim Van Verth | be39f71 | 2019-02-08 15:36:14 -0500 | [diff] [blame] | 49 | fValid = this->onInitializeContext(); |
Jim Van Verth | d063e8b | 2019-05-16 10:31:56 -0400 | [diff] [blame] | 50 | |
Jim Van Verth | 6e4fee8 | 2019-06-06 12:33:53 -0400 | [diff] [blame] | 51 | fContext = GrContext::MakeMetal((__bridge void*)fDevice, (__bridge void*)fQueue, |
| 52 | fDisplayParams.fGrContextOptions); |
Jim Van Verth | be39f71 | 2019-02-08 15:36:14 -0500 | [diff] [blame] | 53 | if (!fContext && fDisplayParams.fMSAASampleCount > 1) { |
| 54 | fDisplayParams.fMSAASampleCount /= 2; |
| 55 | this->initializeContext(); |
| 56 | return; |
| 57 | } |
| 58 | } |
| 59 | |
| 60 | void MetalWindowContext::destroyContext() { |
Jim Van Verth | be39f71 | 2019-02-08 15:36:14 -0500 | [diff] [blame] | 61 | if (fContext) { |
| 62 | // in case we have outstanding refs to this guy (lua?) |
| 63 | fContext->abandonContext(); |
| 64 | fContext.reset(); |
| 65 | } |
| 66 | |
Jim Van Verth | be39f71 | 2019-02-08 15:36:14 -0500 | [diff] [blame] | 67 | this->onDestroyContext(); |
Jim Van Verth | d063e8b | 2019-05-16 10:31:56 -0400 | [diff] [blame] | 68 | |
| 69 | fMetalLayer = nil; |
| 70 | fValid = false; |
| 71 | |
Jim Van Verth | 6e4fee8 | 2019-06-06 12:33:53 -0400 | [diff] [blame] | 72 | [fQueue release]; |
| 73 | [fDevice release]; |
Jim Van Verth | be39f71 | 2019-02-08 15:36:14 -0500 | [diff] [blame] | 74 | } |
| 75 | |
| 76 | sk_sp<SkSurface> MetalWindowContext::getBackbufferSurface() { |
| 77 | sk_sp<SkSurface> surface; |
| 78 | if (fContext) { |
Jim Van Verth | b2f5553 | 2019-09-19 13:03:09 -0400 | [diff] [blame] | 79 | surface = SkSurface::MakeFromCAMetalLayer(fContext.get(), (__bridge GrMTLHandle)fMetalLayer, |
| 80 | kTopLeft_GrSurfaceOrigin, fSampleCount, |
| 81 | kBGRA_8888_SkColorType, |
| 82 | fDisplayParams.fColorSpace, |
| 83 | &fDisplayParams.fSurfaceProps, |
Jim Van Verth | cbf59e0 | 2019-10-17 14:58:37 -0400 | [diff] [blame] | 84 | &fDrawableHandle); |
Jim Van Verth | be39f71 | 2019-02-08 15:36:14 -0500 | [diff] [blame] | 85 | } |
| 86 | |
| 87 | return surface; |
| 88 | } |
| 89 | |
| 90 | void MetalWindowContext::swapBuffers() { |
Jim Van Verth | cbf59e0 | 2019-10-17 14:58:37 -0400 | [diff] [blame] | 91 | // 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 Verth | be39f71 | 2019-02-08 15:36:14 -0500 | [diff] [blame] | 95 | id<MTLCommandBuffer> commandBuffer = [fQueue commandBuffer]; |
| 96 | commandBuffer.label = @"Present"; |
| 97 | |
Jim Van Verth | cbf59e0 | 2019-10-17 14:58:37 -0400 | [diff] [blame] | 98 | [commandBuffer presentDrawable:currentDrawable]; |
Jim Van Verth | be39f71 | 2019-02-08 15:36:14 -0500 | [diff] [blame] | 99 | [commandBuffer commit]; |
Jim Van Verth | be39f71 | 2019-02-08 15:36:14 -0500 | [diff] [blame] | 100 | } |
| 101 | |
| 102 | void MetalWindowContext::setDisplayParams(const DisplayParams& params) { |
| 103 | this->destroyContext(); |
| 104 | fDisplayParams = params; |
| 105 | this->initializeContext(); |
| 106 | } |
| 107 | |
| 108 | } //namespace sk_app |