Flutter Engine
The Flutter Engine
Loading...
Searching...
No Matches
GrMtlAttachment.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 */
7
9
15
16#if !__has_feature(objc_arc)
17#error This file must be compiled with Arc. Use -fobjc-arc flag
18#endif
19
20GR_NORETAIN_BEGIN
21
22GrMtlAttachment::GrMtlAttachment(GrMtlGpu* gpu,
23 SkISize dimensions,
24 UsageFlags supportedUsages,
25 id<MTLTexture> texture,
26 skgpu::Budgeted budgeted,
27 std::string_view label)
29 gpu,
30 dimensions,
31 supportedUsages,
32 texture.sampleCount,
33 texture.mipmapLevelCount > 1 ? skgpu::Mipmapped::kYes : skgpu::Mipmapped::kNo,
35 label)
36 , fTexture(texture) {
37 this->registerWithCache(budgeted);
38}
39
40GrMtlAttachment::GrMtlAttachment(GrMtlGpu* gpu,
41 SkISize dimensions,
42 UsageFlags supportedUsages,
43 id<MTLTexture> texture,
44 GrWrapCacheable cacheable,
45 std::string_view label)
47 gpu,
48 dimensions,
49 supportedUsages,
50 texture.sampleCount,
51 texture.mipmapLevelCount > 1 ? skgpu::Mipmapped::kYes : skgpu::Mipmapped::kNo,
53 label)
54 , fTexture(texture) {
55 this->registerWithCacheWrapped(cacheable);
56}
57
59 SkISize dimensions,
60 int sampleCnt,
61 MTLPixelFormat format) {
62 int textureUsage = 0;
63 int storageMode = 0;
64 if (@available(macOS 10.11, iOS 9.0, tvOS 9.0, *)) {
65 textureUsage = MTLTextureUsageRenderTarget;
66 storageMode = MTLStorageModePrivate;
67 }
68 return GrMtlAttachment::Make(gpu, dimensions, UsageFlags::kStencilAttachment, sampleCnt, format,
69 /*mipLevels=*/1, textureUsage, storageMode, skgpu::Budgeted::kYes);
70}
71
73 SkISize dimensions,
74 int sampleCnt,
75 MTLPixelFormat format) {
76 int textureUsage = 0;
77 int storageMode = 0;
78 if (@available(macOS 10.11, iOS 9.0, tvOS 9.0, *)) {
79 textureUsage = MTLTextureUsageShaderRead | MTLTextureUsageRenderTarget;
80 storageMode = MTLStorageModePrivate;
81 }
82 return GrMtlAttachment::Make(gpu, dimensions, UsageFlags::kColorAttachment, sampleCnt, format,
83 /*mipLevels=*/1, textureUsage, storageMode, skgpu::Budgeted::kYes);
84}
85
87 SkISize dimensions,
88 MTLPixelFormat format,
89 uint32_t mipLevels,
90 GrRenderable renderable,
91 int numSamples,
92 skgpu::Budgeted budgeted) {
93 int textureUsage = 0;
94 int storageMode = 0;
95 if (@available(macOS 10.11, iOS 9.0, tvOS 9.0, *)) {
96 textureUsage = MTLTextureUsageShaderRead;
97 storageMode = MTLStorageModePrivate;
98 }
100 if (renderable == GrRenderable::kYes) {
101 usageFlags |= UsageFlags::kColorAttachment;
102 if (@available(macOS 10.11, iOS 9.0, tvOS 9.0, *)) {
103 textureUsage |= MTLTextureUsageRenderTarget;
104 }
105 }
106
107 return GrMtlAttachment::Make(gpu, dimensions, usageFlags, numSamples, format, mipLevels,
108 textureUsage, storageMode, budgeted);
109}
110
111sk_sp<GrMtlAttachment> GrMtlAttachment::Make(GrMtlGpu* gpu,
112 SkISize dimensions,
113 UsageFlags attachmentUsages,
114 int sampleCnt,
115 MTLPixelFormat format,
116 uint32_t mipLevels,
117 int mtlTextureUsage,
118 int mtlStorageMode,
119 skgpu::Budgeted budgeted) {
120 auto desc = [[MTLTextureDescriptor alloc] init];
121 desc.textureType = (sampleCnt > 1) ? MTLTextureType2DMultisample : MTLTextureType2D;
122 desc.pixelFormat = format;
123 desc.width = dimensions.width();
124 desc.height = dimensions.height();
125 desc.depth = 1;
126 desc.mipmapLevelCount = mipLevels;
127 desc.sampleCount = sampleCnt;
128 desc.arrayLength = 1;
129 if (@available(macOS 10.11, iOS 9.0, tvOS 9.0, *)) {
130 desc.usage = mtlTextureUsage;
131 desc.storageMode = (MTLStorageMode)mtlStorageMode;
132 }
133 id<MTLTexture> texture = [gpu->device() newTextureWithDescriptor:desc];
134#ifdef SK_ENABLE_MTL_DEBUG_INFO
135 if (attachmentUsages == UsageFlags::kStencilAttachment) {
136 texture.label = @"Stencil";
137 } else if (SkToBool(attachmentUsages & UsageFlags::kColorAttachment)) {
138 if (sampleCnt > 1) {
139 if (SkToBool(attachmentUsages & UsageFlags::kTexture)) {
140 texture.label = @"MSAA TextureRenderTarget";
141 } else {
142 texture.label = @"MSAA RenderTarget";
143 }
144 } else {
145 if (SkToBool(attachmentUsages & UsageFlags::kTexture)) {
146 texture.label = @"TextureRenderTarget";
147 } else {
148 texture.label = @"RenderTarget";
149 }
150 }
151 } else {
152 SkASSERT(attachmentUsages == UsageFlags::kTexture);
153 texture.label = @"Texture";
154 }
155#endif
156
157 return sk_sp<GrMtlAttachment>(new GrMtlAttachment(gpu, dimensions, attachmentUsages,
158 texture, budgeted,
159 /*label=*/"MakeMtlAttachment"));
160}
161
163 GrMtlGpu* gpu,
164 SkISize dimensions,
165 id<MTLTexture> texture,
166 UsageFlags attachmentUsages,
167 GrWrapCacheable cacheable,
168 std::string_view label) {
169
170 return sk_sp<GrMtlAttachment>(new GrMtlAttachment(gpu, dimensions, attachmentUsages, texture,
171 cacheable, label));
172}
173
175 // should have been released or abandoned first
176 SkASSERT(!fTexture);
177}
178
180 return GrBackendFormats::MakeMtl(SkToU32(fTexture.pixelFormat));
181}
182
184 fTexture = nil;
186}
187
189 fTexture = nil;
191}
192
193GrMtlGpu* GrMtlAttachment::getMtlGpu() const {
194 SkASSERT(!this->wasDestroyed());
195 return static_cast<GrMtlGpu*>(this->getGpu());
196}
197
199 SkASSERT(fTexture);
200 if (!this->getLabel().empty()) {
201 NSString* labelStr = @(this->getLabel().c_str());
202 fTexture.label = [@"_Skia_" stringByAppendingString:labelStr];
203 }
204}
205GR_NORETAIN_END
GrWrapCacheable
Definition GrTypesPriv.h:84
#define SkASSERT(cond)
Definition SkAssert.h:116
@ kYes
Do pre-clip the geometry before applying the (perspective) matrix.
@ kNo
Don't pre-clip the geometry before applying the (perspective) matrix.
static constexpr bool SkToBool(const T &x)
Definition SkTo.h:35
constexpr uint32_t SkToU32(S x)
Definition SkTo.h:26
int numSamples() const
virtual void onAbandon()
std::string getLabel() const
GrGpu * getGpu() const
bool wasDestroyed() const
virtual void onRelease()
GrBackendFormat backendFormat() const override
~GrMtlAttachment() override
void onAbandon() override
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)
void onRelease() override
void onSetLabel() override
static sk_sp< GrMtlAttachment > MakeStencil(GrMtlGpu *gpu, SkISize dimensions, int sampleCnt, MTLPixelFormat format)
static sk_sp< GrMtlAttachment > MakeMSAA(GrMtlGpu *gpu, SkISize dimensions, int sampleCnt, MTLPixelFormat format)
id< MTLDevice > device() const
Definition GrMtlGpu.h:49
SkISize dimensions() const
Definition GrSurface.h:27
EMSCRIPTEN_KEEPALIVE void empty()
uint32_t uint32_t * format
FlTexture * texture
SK_API GrBackendFormat MakeMtl(GrMTLPixelFormat format)
Budgeted
Definition GpuTypes.h:35
Renderable
Definition GpuTypes.h:69
Mipmapped
Definition GpuTypes.h:53
Protected
Definition GpuTypes.h:61
constexpr int32_t width() const
Definition SkSize.h:36
constexpr int32_t height() const
Definition SkSize.h:37