Flutter Engine
The Flutter Engine
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
6#include <memory>
7
10
11namespace impeller {
12
13std::shared_ptr<Texture> WrapperMTL(TextureDescriptor desc,
14 const void* mtl_texture,
15 std::function<void()> deletion_proc) {
16 return TextureMTL::Wrapper(desc, (__bridge id<MTLTexture>)mtl_texture,
17 std::move(deletion_proc));
18}
19
21 const AcquireTextureProc& aquire_proc,
22 bool wrapped,
23 bool drawable)
24 : Texture(p_desc), aquire_proc_(aquire_proc), is_drawable_(drawable) {
25 const auto& desc = GetTextureDescriptor();
26
27 if (!desc.IsValid() || !aquire_proc) {
28 return;
29 }
30
31 if (desc.size != GetSize()) {
32 VALIDATION_LOG << "The texture and its descriptor disagree about its size.";
33 return;
34 }
35
36 is_wrapped_ = wrapped;
37 is_valid_ = true;
38}
39
40std::shared_ptr<TextureMTL> TextureMTL::Wrapper(
42 id<MTLTexture> texture,
43 std::function<void()> deletion_proc) {
44 if (deletion_proc) {
45 return std::shared_ptr<TextureMTL>(
46 new TextureMTL(
47 desc, [texture]() { return texture; }, true),
48 [deletion_proc = std::move(deletion_proc)](TextureMTL* t) {
49 deletion_proc();
50 delete t;
51 });
52 }
53 return std::shared_ptr<TextureMTL>(
54 new TextureMTL(desc, [texture]() { return texture; }, true));
55}
56
57std::shared_ptr<TextureMTL> TextureMTL::Create(TextureDescriptor desc,
58 id<MTLTexture> texture) {
59 return std::make_shared<TextureMTL>(desc, [texture]() { return texture; });
60}
61
62TextureMTL::~TextureMTL() = default;
63
64void TextureMTL::SetLabel(std::string_view label) {
65 if (is_drawable_) {
66 return;
67 }
68 [aquire_proc_() setLabel:@(label.data())];
69}
70
71// |Texture|
72bool TextureMTL::OnSetContents(std::shared_ptr<const fml::Mapping> mapping,
73 size_t slice) {
74 // Metal has no threading restrictions. So we can pass this data along to the
75 // client rendering API immediately.
76 return OnSetContents(mapping->GetMapping(), mapping->GetSize(), slice);
77}
78
79// |Texture|
80bool TextureMTL::OnSetContents(const uint8_t* contents,
81 size_t length,
82 size_t slice) {
83 if (!IsValid() || !contents || is_wrapped_ || is_drawable_) {
84 return false;
85 }
86
87 const auto& desc = GetTextureDescriptor();
88
89 // Out of bounds access.
90 if (length != desc.GetByteSizeOfBaseMipLevel()) {
91 return false;
92 }
93
94 const auto region =
95 MTLRegionMake2D(0u, 0u, desc.size.width, desc.size.height);
96 [aquire_proc_() replaceRegion:region //
97 mipmapLevel:0u //
98 slice:slice //
99 withBytes:contents //
100 bytesPerRow:desc.GetBytesPerRow() //
101 bytesPerImage:desc.GetByteSizeOfBaseMipLevel() //
102 ];
103
104 return true;
105}
106
107ISize TextureMTL::GetSize() const {
108 if (is_drawable_) {
109 return GetTextureDescriptor().size;
110 }
111 const auto& texture = aquire_proc_();
112 return {static_cast<ISize::Type>(texture.width),
113 static_cast<ISize::Type>(texture.height)};
114}
115
116id<MTLTexture> TextureMTL::GetMTLTexture() const {
117 return aquire_proc_();
118}
119
121 return is_valid_;
122}
123
125 return is_wrapped_;
126}
127
129 return is_drawable_;
130}
131
132bool TextureMTL::GenerateMipmap(id<MTLBlitCommandEncoder> encoder) {
133 if (is_drawable_) {
134 return false;
135 }
136
137 auto texture = aquire_proc_();
138 if (!texture) {
139 return false;
140 }
141
142 [encoder generateMipmapsForTexture:texture];
143 mipmap_generated_ = true;
144
145 return true;
146}
147
148} // namespace impeller
static sk_sp< GrTextureProxy > wrapped(skiatest::Reporter *reporter, GrRecordingContext *rContext, GrProxyProvider *proxyProvider, SkBackingFit fit)
id< MTLTexture > GetMTLTexture() const
Definition: texture_mtl.mm:116
~TextureMTL() override
TextureMTL(TextureDescriptor desc, const AcquireTextureProc &aquire_proc, bool wrapped=false, bool drawable=false)
Definition: texture_mtl.mm:20
bool IsDrawable() const
Whether or not this texture is wrapping a Metal drawable.
Definition: texture_mtl.mm:128
bool IsValid() const override
Definition: texture_mtl.mm:120
std::function< id< MTLTexture >()> AcquireTextureProc
This callback needs to always return the same texture when called multiple times.
Definition: texture_mtl.h:20
bool IsWrapped() const
Definition: texture_mtl.mm:124
bool GenerateMipmap(id< MTLBlitCommandEncoder > encoder)
Definition: texture_mtl.mm:132
static std::shared_ptr< TextureMTL > Wrapper(TextureDescriptor desc, id< MTLTexture > texture, std::function< void()> deletion_proc=nullptr)
Definition: texture_mtl.mm:40
static std::shared_ptr< TextureMTL > Create(TextureDescriptor desc, id< MTLTexture > texture)
Definition: texture_mtl.mm:57
const TextureDescriptor & GetTextureDescriptor() const
Definition: texture.cc:57
bool mipmap_generated_
Definition: texture.h:70
Dart_NativeFunction function
Definition: fuchsia.cc:51
size_t length
FlTexture * texture
ClipOpAndAA opAA SkRegion region
Definition: SkRecords.h:238
Definition: ascii_trie.cc:9
std::shared_ptr< Texture > WrapperMTL(TextureDescriptor desc, const void *mtl_texture, std::function< void()> deletion_proc)
Definition: texture_mtl.mm:13
ISize64 ISize
Definition: size.h:140
Definition: ref_ptr.h:256
A lightweight object that describes the attributes of a texture that can then used an allocator to cr...
std::shared_ptr< const fml::Mapping > data
Definition: texture_gles.cc:63
#define VALIDATION_LOG
Definition: validation.h:73