Flutter Engine
The Flutter Engine
GraphiteMetalWindowContext.mm
Go to the documentation of this file.
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
19#include "src/base/SkMathPriv.h"
20#include "tools/GpuToolUtils.h"
22
25
26namespace skwindow::internal {
27
30 , fValid(false)
31 , fDrawableHandle(nil) {
33}
34
37 SkASSERT(!fGraphiteContext);
38
39 fDevice.reset(MTLCreateSystemDefaultDevice());
40 fQueue.reset([*fDevice newCommandQueue]);
41
43 if (@available(macOS 10.11, iOS 9.0, tvOS 9.0, *)) {
44 if (![*fDevice supportsTextureSampleCount:fDisplayParams.fMSAASampleCount]) {
45 return;
46 }
47 } else {
48 return;
49 }
50 }
52 fStencilBits = 8;
53
55
56 skgpu::graphite::MtlBackendContext backendContext = {};
57 backendContext.fDevice.retain((CFTypeRef)fDevice.get());
58 backendContext.fQueue.retain((CFTypeRef)fQueue.get());
59
60 fDisplayParams.fGraphiteContextOptions.fOptions.fDisableCachedGlyphUploads = true;
61 // Needed to make synchronous readPixels work:
62 fDisplayParams.fGraphiteContextOptions.fPriv.fStoreContextRefInRecorder = true;
64 backendContext, fDisplayParams.fGraphiteContextOptions.fOptions);
65 fGraphiteRecorder = fGraphiteContext->makeRecorder(ToolUtils::CreateTestingRecorderOptions());
66 // TODO
67// if (!fGraphiteContext && fDisplayParams.fMSAASampleCount > 1) {
68// fDisplayParams.fMSAASampleCount /= 2;
69// this->initializeContext();
70// return;
71// }
72}
73
75 if (fGraphiteContext) {
76 fGraphiteRecorder.reset();
77 fGraphiteContext.reset();
78 }
79
80 this->onDestroyContext();
81
82 fMetalLayer = nil;
83 fValid = false;
84
85 fQueue.reset();
86 fDevice.reset();
87}
88
91 id<CAMetalDrawable> currentDrawable = [fMetalLayer nextDrawable];
92 if (currentDrawable == nil) {
93 return nullptr;
94 }
95
97 (CFTypeRef)currentDrawable.texture);
98
99 surface = SkSurfaces::WrapBackendTexture(this->graphiteRecorder(),
100 backendTex,
104 fDrawableHandle = CFRetain((CFTypeRef) currentDrawable);
105
106 return surface;
107}
108
110 this->snapRecordingAndSubmit();
111
112 id<CAMetalDrawable> currentDrawable = (id<CAMetalDrawable>)fDrawableHandle;
113
114 id<MTLCommandBuffer> commandBuffer([*fQueue commandBuffer]);
115 commandBuffer.label = @"Present";
116
117 [commandBuffer presentDrawable:currentDrawable];
118 [commandBuffer commit];
119 // ARC is off in sk_app, so we need to release the CF ref manually
120 CFRelease(fDrawableHandle);
121 fDrawableHandle = nil;
122}
123
125 this->destroyContext();
127 this->initializeContext();
128}
129
131
132} //namespace skwindow::internal
#define SkASSERT(cond)
Definition: SkAssert.h:116
@ kBGRA_8888_SkColorType
pixel with 8 bits for blue, green, red, alpha; in 32-bit word
Definition: SkColorType.h:26
static uint32_t GrNextPow2(uint32_t n)
Definition: SkMathPriv.h:302
sk_sp< GrDirectContext > fContext
Definition: WindowContext.h:63
DisplayParams fDisplayParams
Definition: WindowContext.h:71
SkISize dimensions() const
Definition: WindowContext.h:54
void setDisplayParams(const DisplayParams &params) override
const EmbeddedViewParams * params
VkSurfaceKHR surface
Definition: main.cc:49
SK_API sk_sp< SkSurface > WrapBackendTexture(GrRecordingContext *context, const GrBackendTexture &backendTexture, GrSurfaceOrigin origin, int sampleCnt, SkColorType colorType, sk_sp< SkColorSpace > colorSpace, const SkSurfaceProps *surfaceProps, TextureReleaseProc textureReleaseProc=nullptr, ReleaseContext releaseContext=nullptr)
SK_API std::unique_ptr< Context > MakeMetal(const MtlBackendContext &, const ContextOptions &)
SkSurfaceProps fSurfaceProps
Definition: DisplayParams.h:68
sk_sp< SkColorSpace > fColorSpace
Definition: DisplayParams.h:59