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 
6 
7 #include "flutter/fml/logging.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 
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 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 
81 void 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 
91 bool 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 
99 bool 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
impeller::DeviceBufferMTL::DeviceBufferMTL
DeviceBufferMTL()
formats_mtl.h
impeller::DeviceBufferDescriptor::size
size_t size
Definition: device_buffer_descriptor.h:16
validation.h
impeller::ToMTLTextureDescriptor
MTLTextureDescriptor * ToMTLTextureDescriptor(const TextureDescriptor &desc)
Definition: formats_mtl.mm:86
impeller::TextureMTL::Create
static std::shared_ptr< TextureMTL > Create(TextureDescriptor desc, id< MTLTexture > texture)
Definition: texture_mtl.mm:57
impeller::DeviceBuffer::GetDeviceBufferDescriptor
const DeviceBufferDescriptor & GetDeviceBufferDescriptor() const
Definition: device_buffer.cc:40
impeller::DeviceBufferMTL::GetMTLBuffer
id< MTLBuffer > GetMTLBuffer() const
Definition: device_buffer_mtl.mm:21
VALIDATION_LOG
#define VALIDATION_LOG
Definition: validation.h:73
texture_mtl.h
offset
Point offset
Definition: stroke_path_geometry.cc:300
impeller::DeviceBufferMTL::~DeviceBufferMTL
~DeviceBufferMTL() override
impeller
Definition: aiks_blur_unittests.cc:20
device_buffer_mtl.h