Flutter Engine
The Flutter Engine
GrMtlTextureRenderTarget.mm
Go to the documentation of this file.
1/*
2 * Copyright 2018 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 */
8
14
15#if !__has_feature(objc_arc)
16#error This file must be compiled with Arc. Use -fobjc-arc flag
17#endif
18
19GR_NORETAIN_BEGIN
20
21GrMtlTextureRenderTarget::GrMtlTextureRenderTarget(GrMtlGpu* gpu,
22 skgpu::Budgeted budgeted,
23 SkISize dimensions,
25 sk_sp<GrMtlAttachment> colorAttachment,
26 sk_sp<GrMtlAttachment> resolveAttachment,
27 GrMipmapStatus mipmapStatus,
28 std::string_view label)
29 : GrSurface(gpu, dimensions, GrProtected::kNo, label)
30 , GrMtlTexture(gpu, dimensions, std::move(texture), mipmapStatus, label)
31 , GrMtlRenderTarget(gpu, dimensions, std::move(colorAttachment),
32 std::move(resolveAttachment), label) {
33 this->registerWithCache(budgeted);
34}
35
36GrMtlTextureRenderTarget::GrMtlTextureRenderTarget(GrMtlGpu* gpu,
37 SkISize dimensions,
39 sk_sp<GrMtlAttachment> colorAttachment,
40 sk_sp<GrMtlAttachment> resolveAttachment,
41 GrMipmapStatus mipmapStatus,
42 GrWrapCacheable cacheable,
43 std::string_view label)
44 : GrSurface(gpu, dimensions, GrProtected::kNo, label)
45 , GrMtlTexture(gpu, dimensions, std::move(texture), mipmapStatus, label)
46 , GrMtlRenderTarget(gpu, dimensions, std::move(colorAttachment),
47 std::move(resolveAttachment), label) {
48 this->registerWithCacheWrapped(cacheable);
49}
50
51bool create_rt_attachments(GrMtlGpu* gpu, SkISize dimensions, MTLPixelFormat format, int sampleCnt,
53 sk_sp<GrMtlAttachment>* resolveAttachment) {
54 if (sampleCnt > 1) {
55 auto rp = gpu->getContext()->priv().resourceProvider();
56 sk_sp<GrAttachment> msaaAttachment =
57 rp->makeMSAAAttachment(dimensions,
59 sampleCnt,
62 if (!msaaAttachment) {
63 return false;
64 }
65 *colorAttachment =
66 sk_sp<GrMtlAttachment>(static_cast<GrMtlAttachment*>(msaaAttachment.release()));
67 *resolveAttachment = std::move(texture);
68 } else {
69 *colorAttachment = std::move(texture);
70 }
71 return true;
72}
73
75 GrMtlGpu* gpu,
76 skgpu::Budgeted budgeted,
77 SkISize dimensions,
78 int sampleCnt,
79 MTLPixelFormat format,
80 uint32_t mipLevels,
81 GrMipmapStatus mipmapStatus,
82 std::string_view label) {
83 sk_sp<GrMtlAttachment> textureAttachment =
85 /*numSamples=*/1, budgeted);
86 if (!textureAttachment) {
87 return nullptr;
88 }
89 if (@available(macOS 10.11, iOS 9.0, tvOS 9.0, *)) {
90 SkASSERT((MTLTextureUsageShaderRead | MTLTextureUsageRenderTarget) &
91 textureAttachment->mtlTexture().usage);
92 }
93
96 if (!create_rt_attachments(gpu, dimensions, format, sampleCnt, textureAttachment,
98 return nullptr;
99 }
101 SkASSERT(sampleCnt == 1 || resolveAttachment);
102
104 gpu, budgeted, dimensions, std::move(textureAttachment), std::move(colorAttachment),
105 std::move(resolveAttachment), mipmapStatus, label));
106}
107
109 GrMtlGpu* gpu,
110 SkISize dimensions,
111 int sampleCnt,
112 id<MTLTexture> texture,
113 GrWrapCacheable cacheable) {
114 SkASSERT(nil != texture);
115 if (@available(macOS 10.11, iOS 9.0, tvOS 9.0, *)) {
116 SkASSERT((MTLTextureUsageShaderRead | MTLTextureUsageRenderTarget) & texture.usage);
117 }
118 GrMipmapStatus mipmapStatus = texture.mipmapLevelCount > 1
123 sk_sp<GrMtlAttachment> textureAttachment =
124 GrMtlAttachment::MakeWrapped(gpu, dimensions, texture, textureUsageFlags, cacheable,
125 /*label=*/"MtlAttachment_TextureAttachment");
126 if (!textureAttachment) {
127 return nullptr;
128 }
129
132 if (!create_rt_attachments(gpu, dimensions, texture.pixelFormat, sampleCnt, textureAttachment,
134 return nullptr;
135 }
137 SkASSERT(sampleCnt == 1 || resolveAttachment);
138
140 gpu, dimensions, std::move(textureAttachment), std::move(colorAttachment),
141 std::move(resolveAttachment), mipmapStatus, cacheable,
142 /*label=*/"MtlWrappedTextureRenderTarget"));
143}
144
145size_t GrMtlTextureRenderTarget::onGpuMemorySize() const {
146 // The GrTexture[RenderTarget] is built up by a bunch of attachments each of which are their
147 // own GrGpuResource. Ideally the GrRenderTarget would not be a GrGpuResource and the GrTexture
148 // would just merge with the new GrSurface/Attachment world. Then we could just depend on each
149 // attachment to give its own size since we don't have GrGpuResources owning other
150 // GrGpuResources. Until we get to that point we need to live in some hybrid world. We will let
151 // the msaa and stencil attachments track their own size because they do get cached separately.
152 // For all GrTexture* based things we will continue to to use the GrTexture* to report size and
153 // the owned attachments will have no size and be uncached.
154#ifdef SK_DEBUG
155 // The nonMSAA attachment (either color or resolve depending on numSamples should have size of
156 // zero since it is a texture attachment.
158 if (this->numSamples() > 1) {
159 // Msaa attachment should have a valid size
162 this->dimensions(),
163 this->numSamples(),
165 }
166#endif
167 return GrSurface::ComputeSize(this->backendFormat(), this->dimensions(),
168 1 /*colorSamplesPerPixel*/, this->mipmapped());
169}
170
171void GrMtlTextureRenderTarget::onSetLabel() {
174}
175
176GR_NORETAIN_END
bool create_rt_attachments(GrMtlGpu *gpu, SkISize dimensions, MTLPixelFormat format, int sampleCnt, sk_sp< GrMtlAttachment > texture, sk_sp< GrMtlAttachment > *colorAttachment, sk_sp< GrMtlAttachment > *resolveAttachment)
GrWrapCacheable
Definition: GrTypesPriv.h:85
GrMipmapStatus
Definition: GrTypesPriv.h:523
#define SkASSERT(cond)
Definition: SkAssert.h:116
@ kNo
Don't pre-clip the geometry before applying the (perspective) matrix.
GrResourceProvider * resourceProvider()
GrDirectContextPriv priv()
size_t gpuMemorySize() const
GrDirectContext * getContext()
Definition: GrGpu.h:67
static sk_sp< GrMtlAttachment > MakeTexture(GrMtlGpu *gpu, SkISize dimensions, MTLPixelFormat format, uint32_t mipLevels, GrRenderable renderable, int numSamples, skgpu::Budgeted budgeted)
static sk_sp< GrMtlAttachment > MakeWrapped(GrMtlGpu *gpu, SkISize dimensions, id< MTLTexture >, UsageFlags attachmentUsages, GrWrapCacheable, std::string_view label)
id< MTLTexture > mtlTexture() const
GrMtlAttachment * colorAttachment() const
GrMtlAttachment * nonMSAAAttachment() const
void onSetLabel() override
GrMtlAttachment * resolveAttachment() const
static sk_sp< GrMtlTextureRenderTarget > MakeNewTextureRenderTarget(GrMtlGpu *, skgpu::Budgeted, SkISize, int sampleCnt, MTLPixelFormat, uint32_t mipLevels, GrMipmapStatus, std::string_view label)
GrBackendFormat backendFormat() const override
static sk_sp< GrMtlTextureRenderTarget > MakeWrappedTextureRenderTarget(GrMtlGpu *, SkISize, int sampleCnt, id< MTLTexture >, GrWrapCacheable)
void onSetLabel() override
int numSamples() const
SkISize dimensions() const
Definition: GrSurface.h:27
static size_t ComputeSize(const GrBackendFormat &, SkISize dimensions, int colorSamplesPerPixel, skgpu::Mipmapped, bool binSize=false)
Definition: GrSurface.cpp:21
skgpu::Mipmapped mipmapped() const
Definition: GrTexture.h:62
GrMipmapStatus mipmapStatus() const
Definition: GrTexture.h:66
T * release()
Definition: SkRefCnt.h:324
uint32_t uint32_t * format
FlTexture * texture
SK_API GrBackendFormat MakeMtl(GrMTLPixelFormat format)
Budgeted
Definition: GpuTypes.h:35
Protected
Definition: GpuTypes.h:61
Definition: ref_ptr.h:256
Definition: SkSize.h:16