Flutter Engine
The Flutter Engine
Loading...
Searching...
No Matches
GrMtlRenderTarget.mm
Go to the documentation of this file.
1/*
2 * Copyright 2017 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
9
16
17#if !__has_feature(objc_arc)
18#error This file must be compiled with Arc. Use -fobjc-arc flag
19#endif
20
21GR_NORETAIN_BEGIN
22
23// Called for wrapped non-texture render targets.
25 SkISize dimensions,
26 sk_sp<GrMtlAttachment> colorAttachment,
27 sk_sp<GrMtlAttachment> resolveAttachment,
28 Wrapped,
29 std::string_view label)
30 : GrSurface(gpu, dimensions, GrProtected::kNo, label)
31 , GrRenderTarget(gpu, dimensions, colorAttachment->numSamples(), GrProtected::kNo, label)
32 , fColorAttachment(std::move(colorAttachment))
33 , fResolveAttachment(std::move(resolveAttachment)) {
34 this->registerWithCacheWrapped(GrWrapCacheable::kNo);
35}
36
37// Called by subclass constructors.
39 SkISize dimensions,
40 sk_sp<GrMtlAttachment> colorAttachment,
41 sk_sp<GrMtlAttachment> resolveAttachment,
42 std::string_view label)
43 : GrSurface(gpu, dimensions, GrProtected::kNo, label)
44 , GrRenderTarget(gpu, dimensions, colorAttachment->numSamples(), GrProtected::kNo, label)
45 , fColorAttachment(std::move(colorAttachment))
46 , fResolveAttachment(std::move(resolveAttachment)) {
47}
48
50 SkISize dimensions,
51 int sampleCnt,
52 id<MTLTexture> texture) {
53 SkASSERT(nil != texture);
54 SkASSERT(1 == texture.mipmapLevelCount);
55 if (@available(macOS 10.11, iOS 9.0, tvOS 9.0, *)) {
56 SkASSERT(MTLTextureUsageRenderTarget & texture.usage);
57 }
58
59 sk_sp<GrMtlAttachment> textureAttachment =
63 /*label=*/"MtlAttachment_TextureAttachment");
64
65 GrMtlRenderTarget* mtlRT;
66 if (sampleCnt > 1) {
67 if ([texture sampleCount] == 1) {
68 MTLPixelFormat format = texture.pixelFormat;
69 if (!gpu->mtlCaps().isFormatRenderable(format, sampleCnt)) {
70 return nullptr;
71 }
72 auto rp = gpu->getContext()->priv().resourceProvider();
73 sk_sp<GrAttachment> msaaAttachment =
74 rp->makeMSAAAttachment(dimensions,
76 sampleCnt,
77 GrProtected::kNo,
79 if (!msaaAttachment) {
80 return nullptr;
81 }
83 sk_sp<GrMtlAttachment>(static_cast<GrMtlAttachment*>(msaaAttachment.release()));
84 mtlRT = new GrMtlRenderTarget(
85 gpu, dimensions, std::move(colorAttachment), std::move(textureAttachment),
86 kWrapped, /*label=*/"MakeWrappedRenderTargetWithOneTextureSampleCount");
88 } else {
89 SkASSERT(sampleCnt == static_cast<int>([texture sampleCount]));
90 mtlRT = new GrMtlRenderTarget(gpu, dimensions, std::move(textureAttachment), nil,
91 kWrapped,
92 /*label=*/"MakeWrappedRenderTargetWithManySampleCount");
93 }
94 } else {
95 mtlRT = new GrMtlRenderTarget(gpu, dimensions, std::move(textureAttachment), nil,
96 kWrapped,
97 /*label=*/"MakeWrappedRenderTargetWithOneOrLessSampleCount");
98 }
99
100 return sk_sp<GrMtlRenderTarget>(mtlRT);
101}
102
107
109 GrMtlTextureInfo info;
111 return GrBackendRenderTargets::MakeMtl(this->width(), this->height(), info);
112}
113
117
118static int renderpass_features_to_index(bool hasResolve, bool hasStencil) {
119 int index = 0;
120 if (hasResolve) {
121 index += 1;
122 }
123 if (hasStencil) {
124 index += 2;
125 }
126 return index;
127}
128
130 bool withStencil) {
131 int cacheIndex =
132 renderpass_features_to_index(withResolve, withStencil);
133 SkASSERT(cacheIndex < GrMtlRenderTarget::kNumCachedFramebuffers);
134
135 if (fCachedFramebuffers[cacheIndex]) {
136 return fCachedFramebuffers[cacheIndex].get();
137 }
138
139 GrMtlAttachment* resolve = withResolve ? this->resolveAttachment() : nullptr;
141
142 // Stencil attachment view is stored in the base RT stencil attachment
143 GrMtlAttachment* stencil =
144 withStencil ? static_cast<GrMtlAttachment*>(this->getStencilAttachment())
145 : nullptr;
146 fCachedFramebuffers[cacheIndex] =
147 GrMtlFramebuffer::Make(colorAttachment, resolve, stencil);
148 return fCachedFramebuffers[cacheIndex].get();
149}
150
152 SkASSERT(!this->wasDestroyed());
153 return static_cast<GrMtlGpu*>(this->getGpu());
154}
155
161
167
169 SkASSERT(useMSAASurface == (this->numSamples() > 1));
170 return true;
171}
172
175 if (!this->getLabel().empty()) {
176 NSString* labelStr = @(this->getLabel().c_str());
177 if (fResolveAttachment) {
179 [@"_Skia_MSAA_" stringByAppendingString:labelStr];
181 [@"_Skia_Resolve_" stringByAppendingString:labelStr];
182 } else {
183 fColorAttachment->mtlTexture().label = [@"_Skia_" stringByAppendingString:labelStr];
184 }
185 }
186}
187
188GR_NORETAIN_END
static void info(const char *fmt,...) SK_PRINTF_LIKE(1
Definition DM.cpp:213
static int renderpass_features_to_index(bool hasResolve, bool hasStencil)
SK_ALWAYS_INLINE CF_RETURNS_RETAINED const void * GrRetainPtrFromId(id idObject)
Definition GrMtlUtil.h:59
#define SkASSERT(cond)
Definition SkAssert.h:116
@ kNo
Don't pre-clip the geometry before applying the (perspective) matrix.
GrResourceProvider * resourceProvider()
GrDirectContextPriv priv()
std::string getLabel() const
GrGpu * getGpu() const
bool wasDestroyed() const
GrDirectContext * getContext()
Definition GrGpu.h:67
MTLPixelFormat mtlFormat() const
static sk_sp< GrMtlAttachment > MakeWrapped(GrMtlGpu *gpu, SkISize dimensions, id< MTLTexture >, UsageFlags attachmentUsages, GrWrapCacheable, std::string_view label)
id< MTLTexture > mtlTexture() const
bool isFormatRenderable(const GrBackendFormat &format, int sampleCount) const override
Definition GrMtlCaps.mm:490
static sk_sp< const GrMtlFramebuffer > Make(GrMtlAttachment *colorAttachment, GrMtlAttachment *resolveAttachment, GrMtlAttachment *stencilAttachment)
const GrMtlCaps & mtlCaps() const
Definition GrMtlGpu.h:47
GrMtlAttachment * colorAttachment() const
GrMtlGpu * getMtlGpu() const
void onSetLabel() override
static sk_sp< GrMtlRenderTarget > MakeWrappedRenderTarget(GrMtlGpu *, SkISize, int sampleCnt, id< MTLTexture >)
sk_sp< GrMtlAttachment > fColorAttachment
sk_sp< GrMtlAttachment > fResolveAttachment
const GrMtlFramebuffer * getFramebuffer(bool withResolve, bool withStencil)
GrBackendFormat backendFormat() const override
GrBackendRenderTarget getBackendRenderTarget() const override
void onAbandon() override
GrMtlAttachment * resolveAttachment() const
GrMtlRenderTarget(GrMtlGpu *gpu, SkISize, sk_sp< GrMtlAttachment > colorAttachment, sk_sp< GrMtlAttachment > resolveAttachment, std::string_view label)
bool completeStencilAttachment(GrAttachment *stencil, bool useMSAASurface) override
void onRelease() override
GrAttachment * getStencilAttachment() const
void onAbandon() override
void setRequiresManualMSAAResolve()
Definition GrSurface.h:121
void onRelease() override
int numSamples() const
SkISize dimensions() const
Definition GrSurface.h:27
int height() const
Definition GrSurface.h:37
int width() const
Definition GrSurface.h:32
T * get() const
Definition SkRefCnt.h:303
T * release()
Definition SkRefCnt.h:324
EMSCRIPTEN_KEEPALIVE void empty()
uint32_t uint32_t * format
FlTexture * texture
SK_API GrBackendFormat MakeMtl(GrMTLPixelFormat format)
SK_API GrBackendRenderTarget MakeMtl(int width, int height, const GrMtlTextureInfo &mtlInfo)
Protected
Definition GpuTypes.h:61
Definition ref_ptr.h:256