| 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 std::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 std::make_shared<TextureMTL>(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
81 bool DeviceBufferMTL::SetLabel(const std::string& label) {
82 if (label.empty()) {
83 return false;
84 }
85 [buffer_ setLabel:@(label.c_str())];
86 return true;
87 }
88
89 bool DeviceBufferMTL::SetLabel(const std::string& label, Range range) {
90 if (label.empty()) {
91 return false;
92 }
93 [buffer_ addDebugMarker:@(label.c_str())
94 range:NSMakeRange(range.offset, range.length)];
95 return true;
96 }
97
98 } // namespace impeller
impeller::DeviceBufferMTL::DeviceBufferMTL
DeviceBufferMTL()
formats_mtl.h
validation.h
impeller::ToMTLTextureDescriptor
MTLTextureDescriptor * ToMTLTextureDescriptor(const TextureDescriptor &desc)
Definition: formats_mtl.mm:86
impeller::DeviceBufferMTL::GetMTLBuffer
id< MTLBuffer > GetMTLBuffer() const
Definition: device_buffer_mtl.mm:21
VALIDATION_LOG
#define VALIDATION_LOG
Definition: validation.h:60
texture_mtl.h
impeller::DeviceBufferMTL::~DeviceBufferMTL
~DeviceBufferMTL() override
impeller
Definition: aiks_context.cc:10
device_buffer_mtl.h