Flutter Engine
The Flutter Engine
Loading...
Searching...
No Matches
MtlTexture.mm
Go to the documentation of this file.
1/*
2 * Copyright 2021 Google LLC
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
13#include "src/core/SkMipmap.h"
17
18namespace skgpu::graphite {
19
20sk_cfp<id<MTLTexture>> MtlTexture::MakeMtlTexture(const MtlSharedContext* sharedContext,
21 SkISize dimensions,
22 const TextureInfo& info) {
23 const Caps* caps = sharedContext->caps();
24 if (dimensions.width() > caps->maxTextureSize() ||
25 dimensions.height() > caps->maxTextureSize()) {
26 return nullptr;
27 }
28
29 const MtlTextureSpec& mtlSpec = info.mtlTextureSpec();
30 SkASSERT(!mtlSpec.fFramebufferOnly);
31
32 if (mtlSpec.fUsage & MTLTextureUsageShaderRead && !caps->isTexturable(info)) {
33 return nullptr;
34 }
35
36 if (mtlSpec.fUsage & MTLTextureUsageRenderTarget &&
37 !(caps->isRenderable(info) || MtlFormatIsDepthOrStencil((MTLPixelFormat)mtlSpec.fFormat))) {
38 return nullptr;
39 }
40
41 if (mtlSpec.fUsage & MTLTextureUsageShaderWrite && !caps->isStorage(info)) {
42 return nullptr;
43 }
44
45 int numMipLevels = 1;
46 if (info.mipmapped() == Mipmapped::kYes) {
48 }
49
50 sk_cfp<MTLTextureDescriptor*> desc([[MTLTextureDescriptor alloc] init]);
51 (*desc).textureType = (info.numSamples() > 1) ? MTLTextureType2DMultisample : MTLTextureType2D;
52 (*desc).pixelFormat = (MTLPixelFormat)mtlSpec.fFormat;
53 (*desc).width = dimensions.width();
54 (*desc).height = dimensions.height();
55 (*desc).depth = 1;
56 (*desc).mipmapLevelCount = numMipLevels;
57 (*desc).sampleCount = info.numSamples();
58 (*desc).arrayLength = 1;
59 (*desc).usage = mtlSpec.fUsage;
60 (*desc).storageMode = (MTLStorageMode)mtlSpec.fStorageMode;
61
62 sk_cfp<id<MTLTexture>> texture([sharedContext->device() newTextureWithDescriptor:desc.get()]);
63#ifdef SK_ENABLE_MTL_DEBUG_INFO
64 if (mtlSpec.fUsage & MTLTextureUsageRenderTarget) {
65 if (MtlFormatIsDepthOrStencil((MTLPixelFormat)mtlSpec.fFormat)) {
66 (*texture).label = @"DepthStencil";
67 } else {
68 if (info.numSamples() > 1) {
69 if (mtlSpec.fUsage & MTLTextureUsageShaderRead) {
70 (*texture).label = @"MSAA SampledTexture-ColorAttachment";
71 } else {
72 (*texture).label = @"MSAA ColorAttachment";
73 }
74 } else {
75 if (mtlSpec.fUsage & MTLTextureUsageShaderRead) {
76 (*texture).label = @"SampledTexture-ColorAttachment";
77 } else {
78 (*texture).label = @"ColorAttachment";
79 }
80 }
81 }
82 } else if (mtlSpec.fUsage & MTLTextureUsageShaderWrite) {
83 SkASSERT(mtlSpec.fUsage & MTLTextureUsageShaderRead);
84 (*texture).label = @"StorageTexture";
85 } else {
86 SkASSERT(mtlSpec.fUsage & MTLTextureUsageShaderRead);
87 (*texture).label = @"SampledTexture";
88 }
89#endif
90
91 return texture;
92}
93
94MtlTexture::MtlTexture(const MtlSharedContext* sharedContext,
95 SkISize dimensions,
96 const TextureInfo& info,
97 sk_cfp<id<MTLTexture>> texture,
98 Ownership ownership,
99 skgpu::Budgeted budgeted)
100 : Texture(sharedContext, dimensions, info, /*mutableState=*/nullptr, ownership, budgeted)
101 , fTexture(std::move(texture)) {}
102
104 SkISize dimensions,
105 const TextureInfo& info,
106 skgpu::Budgeted budgeted) {
107 sk_cfp<id<MTLTexture>> texture = MakeMtlTexture(sharedContext, dimensions, info);
108 if (!texture) {
109 return nullptr;
110 }
113 info,
114 std::move(texture),
116 budgeted));
117}
118
120 SkISize dimensions,
121 const TextureInfo& info,
122 sk_cfp<id<MTLTexture>> texture) {
125 info,
126 std::move(texture),
129}
130
132 fTexture.reset();
133}
134
135} // namespace skgpu::graphite
136
static void info(const char *fmt,...) SK_PRINTF_LIKE(1
Definition DM.cpp:213
#define SkASSERT(cond)
Definition SkAssert.h:116
static int ComputeLevelCount(int baseWidth, int baseHeight)
Definition SkMipmap.cpp:134
bool isTexturable(const TextureInfo &) const
Definition Caps.cpp:65
virtual bool isStorage(const TextureInfo &) const =0
virtual bool isRenderable(const TextureInfo &) const =0
int maxTextureSize() const
Definition Caps.h:134
static sk_sp< Texture > Make(const MtlSharedContext *, SkISize dimensions, const TextureInfo &, skgpu::Budgeted)
void freeGpuData() override
static sk_cfp< id< MTLTexture > > MakeMtlTexture(const MtlSharedContext *, SkISize dimensions, const TextureInfo &)
Definition MtlTexture.mm:20
static sk_sp< Texture > MakeWrapped(const MtlSharedContext *, SkISize dimensions, const TextureInfo &, sk_cfp< id< MTLTexture > >)
skgpu::Budgeted budgeted() const
Definition Resource.h:100
const SharedContext * sharedContext() const
Definition Resource.h:187
const Caps * caps() const
SkISize dimensions() const
Definition Texture.h:31
FlTexture * texture
Budgeted
Definition GpuTypes.h:35
bool MtlFormatIsDepthOrStencil(MTLPixelFormat format)
Definition MtlUtils.mm:18
Definition ref_ptr.h:256
constexpr int32_t width() const
Definition SkSize.h:36
constexpr int32_t height() const
Definition SkSize.h:37