Flutter Engine
The Flutter Engine
Loading...
Searching...
No Matches
device_buffer_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
7#include "flutter/fml/logging.h"
11
12namespace impeller {
13
14DeviceBufferMTL::DeviceBufferMTL(DeviceBufferDescriptor desc,
15 id<MTLBuffer> buffer,
16 MTLStorageMode storage_mode)
17 : DeviceBuffer(desc), buffer_(buffer), storage_mode_(storage_mode) {}
18
19DeviceBufferMTL::~DeviceBufferMTL() = default;
20
21id<MTLBuffer> DeviceBufferMTL::GetMTLBuffer() const {
22 return buffer_;
23}
24
25uint8_t* DeviceBufferMTL::OnGetContents() const {
26 if (storage_mode_ != MTLStorageModeShared) {
27 return nullptr;
28 }
29 return reinterpret_cast<uint8_t*>(buffer_.contents);
30}
31
32std::shared_ptr<Texture> DeviceBufferMTL::AsTexture(
33 Allocator& allocator,
34 const TextureDescriptor& descriptor,
35 uint16_t row_bytes) const {
36 auto mtl_texture_desc = ToMTLTextureDescriptor(descriptor);
37
38 if (!mtl_texture_desc) {
39 VALIDATION_LOG << "Texture descriptor was invalid.";
40 return nullptr;
41 }
42
43 if (@available(iOS 13.0, macos 10.15, *)) {
44 mtl_texture_desc.resourceOptions = buffer_.resourceOptions;
45 }
46
47 auto texture = [buffer_ newTextureWithDescriptor:mtl_texture_desc
48 offset:0
49 bytesPerRow:row_bytes];
50 if (!texture) {
51 return nullptr;
52 }
53 return TextureMTL::Create(descriptor, texture);
54}
55
56[[nodiscard]] bool DeviceBufferMTL::OnCopyHostBuffer(const uint8_t* source,
57 Range source_range,
58 size_t offset) {
59 auto dest = static_cast<uint8_t*>(buffer_.contents);
60
61 if (!dest) {
62 return false;
63 }
64
65 if (source) {
66 ::memmove(dest + offset, source + source_range.offset, source_range.length);
67 }
68
69// MTLStorageModeManaged is never present on always returns false on iOS. But
70// the compiler is mad that `didModifyRange:` appears in a TU meant for iOS. So,
71// just compile it away.
72#if !FML_OS_IOS
73 if (storage_mode_ == MTLStorageModeManaged) {
74 [buffer_ didModifyRange:NSMakeRange(offset, source_range.length)];
75 }
76#endif
77
78 return true;
79}
80
81void DeviceBufferMTL::Flush(std::optional<Range> range) const {
82#if !FML_OS_IOS
83 auto flush_range = range.value_or(Range{0, GetDeviceBufferDescriptor().size});
84 if (storage_mode_ == MTLStorageModeManaged) {
85 [buffer_
86 didModifyRange:NSMakeRange(flush_range.offset, flush_range.length)];
87 }
88#endif
89}
90
91bool DeviceBufferMTL::SetLabel(const std::string& label) {
92 if (label.empty()) {
93 return false;
94 }
95 [buffer_ setLabel:@(label.c_str())];
96 return true;
97}
98
99bool DeviceBufferMTL::SetLabel(const std::string& label, Range range) {
100 if (label.empty()) {
101 return false;
102 }
103 [buffer_ addDebugMarker:@(label.c_str())
104 range:NSMakeRange(range.offset, range.length)];
105 return true;
106}
107
108} // namespace impeller
An object that allocates device memory.
Definition allocator.h:22
SkBitmap source
Definition examples.cpp:28
static const uint8_t buffer[]
FlTexture * texture
MTLTextureDescriptor * ToMTLTextureDescriptor(const TextureDescriptor &desc)
Point offset
size_t length
Definition range.h:16
size_t offset
Definition range.h:15
A lightweight object that describes the attributes of a texture that can then used an allocator to cr...
#define VALIDATION_LOG
Definition validation.h:73