| Flutter Impeller |

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
5 #include "impeller/renderer/backend/metal/device_buffer_mtl.h"
6
7 #include "flutter/fml/logging.h"
8 #include "impeller/base/validation.h"
9 #include "impeller/renderer/backend/metal/formats_mtl.h"
10 #include "impeller/renderer/backend/metal/texture_mtl.h"
11
12 namespace impeller {
13
14 DeviceBufferMTL::DeviceBufferMTL(DeviceBufferDescriptor desc,
15 id<MTLBuffer> buffer,
16 MTLStorageMode storage_mode)
17 : DeviceBuffer(desc), buffer_(buffer), storage_mode_(storage_mode) {}
18
19 DeviceBufferMTL::~DeviceBufferMTL() = default;
20
21 id<MTLBuffer> DeviceBufferMTL::GetMTLBuffer() const {
22 return buffer_;
23 }
24
25 uint8_t* DeviceBufferMTL::OnGetContents() const {
26 if (storage_mode_ != MTLStorageModeShared) {
27 return nullptr;
28 }
29 return reinterpret_cast<uint8_t*>(buffer_.contents);
30 }
31
32 [[nodiscard]] bool DeviceBufferMTL::OnCopyHostBuffer(const uint8_t* source,
33 Range source_range,
34 size_t offset) {
35 auto dest = static_cast<uint8_t*>(buffer_.contents);
36
37 if (!dest) {
38 return false;
39 }
40
41 if (source) {
42 ::memmove(dest + offset, source + source_range.offset, source_range.length);
43 }
44
45 // MTLStorageModeManaged is never present on always returns false on iOS. But
46 // the compiler is mad that `didModifyRange:` appears in a TU meant for iOS. So,
47 // just compile it away.
48 #if !FML_OS_IOS
49 if (storage_mode_ == MTLStorageModeManaged) {
50 [buffer_ didModifyRange:NSMakeRange(offset, source_range.length)];
51 }
52 #endif
53
54 return true;
55 }
56
57 void DeviceBufferMTL::Flush(std::optional<Range> range) const {
58 #if !FML_OS_IOS
59 auto flush_range = range.value_or(Range{0, GetDeviceBufferDescriptor().size});
60 if (storage_mode_ == MTLStorageModeManaged) {
61 [buffer_
62 didModifyRange:NSMakeRange(flush_range.offset, flush_range.length)];
63 }
64 #endif
65 }
66
67 bool DeviceBufferMTL::SetLabel(const std::string& label) {
68 if (label.empty()) {
69 return false;
70 }
71 [buffer_ setLabel:@(label.c_str())];
72 return true;
73 }
74
75 bool DeviceBufferMTL::SetLabel(const std::string& label, Range range) {
76 if (label.empty()) {
77 return false;
78 }
79 [buffer_ addDebugMarker:@(label.c_str())
80 range:NSMakeRange(range.offset, range.length)];
81 return true;
82 }
83
84 } // namespace impeller
impeller::DeviceBufferMTL::DeviceBufferMTL
DeviceBufferMTL()
formats_mtl.h
impeller::DeviceBufferDescriptor::size
size_t size
Definition: device_buffer_descriptor.h:16
validation.h
offset
SeparatedVector2 offset
Definition: stroke_path_geometry.cc:311
impeller::DeviceBuffer::GetDeviceBufferDescriptor
const DeviceBufferDescriptor & GetDeviceBufferDescriptor() const
Definition: device_buffer.cc:25
impeller::DeviceBufferMTL::GetMTLBuffer
id< MTLBuffer > GetMTLBuffer() const
Definition: device_buffer_mtl.mm:21
texture_mtl.h
impeller::DeviceBufferMTL::~DeviceBufferMTL
~DeviceBufferMTL() override
impeller
Definition: aiks_blend_unittests.cc:18
device_buffer_mtl.h