Debian Codegraph @

tools/window/GraphiteNativeMetalWindowContext.mm

raw
1 /*
2 * Copyright 2021 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
8 #include "tools/window/GraphiteNativeMetalWindowContext.h"
9
10 #include "include/core/SkSurface.h"
11 #include "include/gpu/graphite/BackendTexture.h"
12 #include "include/gpu/graphite/Context.h"
13 #include "include/gpu/graphite/ContextOptions.h"
14 #include "include/gpu/graphite/Recorder.h"
15 #include "include/gpu/graphite/Recording.h"
16 #include "include/gpu/graphite/Surface.h"
17 #include "include/gpu/graphite/mtl/MtlBackendContext.h"
18 #include "include/gpu/graphite/mtl/MtlGraphiteTypes.h"
19 #include "src/base/SkMathPriv.h"
20 #include "src/gpu/graphite/ContextOptionsPriv.h"
21 #include "tools/graphite/GraphiteToolUtils.h"
22 #include "tools/graphite/TestOptions.h"
23 #include "tools/window/GraphiteDisplayParams.h"
24
25 using skwindow::DisplayParams;
26 using skwindow::internal::GraphiteMetalWindowContext;
27
28 namespace skwindow::internal {
29
30 GraphiteMetalWindowContext::GraphiteMetalWindowContext(std::unique_ptr<const DisplayParams> params)
31 : WindowContext(DisplayParamsBuilder(params.get()).roundUpMSAA().detach())
32 , fValid(false)
33 , fDrawableHandle(nil) {}
34
35 void GraphiteMetalWindowContext::initializeContext() {
36 #if defined(SK_GANESH)
37 SkASSERT(!fContext);
38 #endif
39 SkASSERT(!fGraphiteContext);
40
41 fDevice.reset(MTLCreateSystemDefaultDevice());
42 fQueue.reset([*fDevice newCommandQueue]);
43
44 if (fDisplayParams->msaaSampleCount() > 1) {
45 if (@available(macOS 10.11, iOS 9.0, tvOS 9.0, *)) {
46 if (![*fDevice supportsTextureSampleCount:fDisplayParams->msaaSampleCount()]) {
47 return;
48 }
49 } else {
50 return;
51 }
52 }
53 fSampleCount = fDisplayParams->msaaSampleCount();
54 fStencilBits = 8;
55
56 fValid = this->onInitializeContext();
57
58 skgpu::graphite::MtlBackendContext backendContext = {};
59 backendContext.fDevice.retain((CFTypeRef)fDevice.get());
60 backendContext.fQueue.retain((CFTypeRef)fQueue.get());
61
62 SkASSERT(fDisplayParams->graphiteTestOptions());
63 skwindow::GraphiteTestOptions opts = *fDisplayParams->graphiteTestOptions();
64
65 opts.fTestOptions.fContextOptions.fRequireOrderedRecordings = true;
66 // Needed to make synchronous readPixels work:
67 opts.fPriv.fStoreContextRefInRecorder = true;
68 fDisplayParams =
69 GraphiteDisplayParamsBuilder(fDisplayParams.get()).graphiteTestOptions(opts).detach();
70 fGraphiteContext = skgpu::graphite::ContextFactory::MakeMetal(
71 backendContext, fDisplayParams->graphiteTestOptions()->fTestOptions.fContextOptions);
72 fGraphiteRecorder = fGraphiteContext->makeRecorder(ToolUtils::CreateTestingRecorderOptions());
73 // TODO
74 // if (!fGraphiteContext && fDisplayParams->msaaSampleCount() > 1) {
75 // fDisplayParams->msaaSampleCount() /= 2;
76 // this->initializeContext();
77 // return;
78 // }
79 }
80
81 void GraphiteMetalWindowContext::destroyContext() {
82 if (fGraphiteContext) {
83 fGraphiteRecorder.reset();
84 fGraphiteContext.reset();
85 }
86
87 this->onDestroyContext();
88
89 fMetalLayer = nil;
90 fValid = false;
91
92 fQueue.reset();
93 fDevice.reset();
94 }
95
96 sk_sp<SkSurface> GraphiteMetalWindowContext::getBackbufferSurface() {
97 sk_sp<SkSurface> surface;
98 id<CAMetalDrawable> currentDrawable = [fMetalLayer nextDrawable];
99 if (currentDrawable == nil) {
100 return nullptr;
101 }
102
103 auto backendTex = skgpu::graphite::BackendTextures::MakeMetal(
104 this->dimensions(), (CFTypeRef)currentDrawable.texture);
105
106 surface = SkSurfaces::WrapBackendTexture(this->graphiteRecorder(),
107 backendTex,
108 fDisplayParams->colorSpace(),
109 &fDisplayParams->surfaceProps());
110 fDrawableHandle = CFRetain((CFTypeRef)currentDrawable);
111
112 return surface;
113 }
114
115 void GraphiteMetalWindowContext::onSwapBuffers() {
116 this->submitToGpu();
117
118 id<CAMetalDrawable> currentDrawable = (id<CAMetalDrawable>)fDrawableHandle;
119
120 id<MTLCommandBuffer> commandBuffer([*fQueue commandBuffer]);
121 commandBuffer.label = @"Present";
122
123 [commandBuffer presentDrawable:currentDrawable];
124 [commandBuffer commit];
125 // ARC is off in sk_app, so we need to release the CF ref manually
126 CFRelease(fDrawableHandle);
127 fDrawableHandle = nil;
128 }
129
130 void GraphiteMetalWindowContext::setDisplayParams(std::unique_ptr<const DisplayParams> params) {
131 this->destroyContext();
132 fDisplayParams = std::move(params);
133 this->initializeContext();
134 }
135
136 void GraphiteMetalWindowContext::activate(bool isActive) {}
137
138 } // namespace skwindow::internal