| Flutter Impeller |

texture_mtl.mm
Go to the documentation of this file.
1 // Copyright 2013 The Flutter Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4
5 #include "impeller/renderer/backend/metal/texture_mtl.h"
6
7 #include "impeller/base/validation.h"
8 #include "impeller/core/texture_descriptor.h"
9
10 namespace impeller {
11
12 std::shared_ptr<Texture> WrapperMTL(TextureDescriptor desc,
13 const void* mtl_texture,
14 std::function<void()> deletion_proc) {
15 return TextureMTL::Wrapper(desc, (__bridge id<MTLTexture>)mtl_texture,
16 std::move(deletion_proc));
17 }
18
19 TextureMTL::TextureMTL(TextureDescriptor p_desc,
20 id<MTLTexture> texture,
21 bool wrapped)
22 : Texture(p_desc), texture_(texture) {
23 const auto& desc = GetTextureDescriptor();
24
25 if (!desc.IsValid() || !texture_) {
26 return;
27 }
28
29 if (desc.size != GetSize()) {
30 VALIDATION_LOG << "The texture and its descriptor disagree about its size.";
31 return;
32 }
33
34 is_wrapped_ = wrapped;
35 is_valid_ = true;
36 }
37
38 std::shared_ptr<TextureMTL> TextureMTL::Wrapper(
39 TextureDescriptor desc,
40 id<MTLTexture> texture,
41 std::function<void()> deletion_proc) {
42 if (deletion_proc) {
43 return std::shared_ptr<TextureMTL>(
44 new TextureMTL(desc, texture, true),
45 [deletion_proc = std::move(deletion_proc)](TextureMTL* t) {
46 deletion_proc();
47 delete t;
48 });
49 }
50 return std::shared_ptr<TextureMTL>(new TextureMTL(desc, texture, true));
51 }
52
53 TextureMTL::~TextureMTL() = default;
54
55 void TextureMTL::SetLabel(std::string_view label) {
56 [texture_ setLabel:@(label.data())];
57 }
58
59 // |Texture|
60 bool TextureMTL::OnSetContents(std::shared_ptr<const fml::Mapping> mapping,
61 size_t slice) {
62 // Metal has no threading restrictions. So we can pass this data along to the
63 // client rendering API immediately.
64 return OnSetContents(mapping->GetMapping(), mapping->GetSize(), slice);
65 }
66
67 // |Texture|
68 bool TextureMTL::OnSetContents(const uint8_t* contents,
69 size_t length,
70 size_t slice) {
71 if (!IsValid() || !contents || is_wrapped_) {
72 return false;
73 }
74
75 const auto& desc = GetTextureDescriptor();
76
77 // Out of bounds access.
78 if (length != desc.GetByteSizeOfBaseMipLevel()) {
79 return false;
80 }
81
82 const auto region =
83 MTLRegionMake2D(0u, 0u, desc.size.width, desc.size.height);
84 [texture_ replaceRegion:region //
85 mipmapLevel:0u //
86 slice:slice //
87 withBytes:contents //
88 bytesPerRow:desc.GetBytesPerRow() //
89 bytesPerImage:desc.GetByteSizeOfBaseMipLevel() //
90 ];
91
92 return true;
93 }
94
95 ISize TextureMTL::GetSize() const {
96 return {static_cast<ISize::Type>(texture_.width),
97 static_cast<ISize::Type>(texture_.height)};
98 }
99
100 id<MTLTexture> TextureMTL::GetMTLTexture() const {
101 return texture_;
102 }
103
104 bool TextureMTL::IsValid() const {
105 return is_valid_;
106 }
107
108 bool TextureMTL::IsWrapped() const {
109 return is_wrapped_;
110 }
111
112 bool TextureMTL::GenerateMipmap(id<MTLBlitCommandEncoder> encoder) {
113 if (!texture_) {
114 return false;
115 }
116
117 [encoder generateMipmapsForTexture:texture_];
118 mipmap_generated_ = true;
119
120 return true;
121 }
122
123 } // namespace impeller
impeller::TextureMTL::GenerateMipmap
bool GenerateMipmap(id< MTLBlitCommandEncoder > encoder)
Definition: texture_mtl.mm:112
impeller::Texture::GetTextureDescriptor
const TextureDescriptor & GetTextureDescriptor() const
Definition: texture.cc:57
texture_descriptor.h
impeller::TextureMTL::TextureMTL
TextureMTL(TextureDescriptor desc, id< MTLTexture > texture, bool wrapped=false)
Definition: texture_mtl.mm:19
impeller::TextureMTL::GetMTLTexture
id< MTLTexture > GetMTLTexture() const
Definition: texture_mtl.mm:100
impeller::TextureMTL
Definition: texture_mtl.h:15
validation.h
impeller::TSize< int64_t >::Type
int64_t Type
Definition: size.h:19
impeller::TextureDescriptor::IsValid
constexpr bool IsValid() const
Definition: texture_descriptor.h:84
impeller::Texture
Definition: texture.h:17
impeller::WrapperMTL
std::shared_ptr< Texture > WrapperMTL(TextureDescriptor desc, const void *mtl_texture, std::function< void()> deletion_proc)
Definition: texture_mtl.mm:12
impeller::Texture::mipmap_generated_
bool mipmap_generated_
Definition: texture.h:61
impeller::TextureMTL::Wrapper
static std::shared_ptr< TextureMTL > Wrapper(TextureDescriptor desc, id< MTLTexture > texture, std::function< void()> deletion_proc=nullptr)
Definition: texture_mtl.mm:38
impeller::TSize::width
Type width
Definition: size.h:21
impeller::TextureMTL::~TextureMTL
~TextureMTL() override
impeller::ISize
TSize< int64_t > ISize
Definition: size.h:136
impeller::TextureDescriptor::size
ISize size
Definition: texture_descriptor.h:43
VALIDATION_LOG
#define VALIDATION_LOG
Definition: validation.h:60
impeller::TextureDescriptor::GetByteSizeOfBaseMipLevel
constexpr size_t GetByteSizeOfBaseMipLevel() const
Definition: texture_descriptor.h:50
texture_mtl.h
impeller::TextureMTL::IsWrapped
bool IsWrapped() const
Definition: texture_mtl.mm:108
impeller::TSize::height
Type height
Definition: size.h:22
impeller::TextureDescriptor
A lightweight object that describes the attributes of a texture that can then used an allocator to cr...
Definition: texture_descriptor.h:39
impeller
Definition: aiks_context.cc:10
impeller::TextureDescriptor::GetBytesPerRow
constexpr size_t GetBytesPerRow() const
Definition: texture_descriptor.h:57