Flutter Engine
 
Loading...
Searching...
No Matches
device_buffer.cc
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 "dart_api.h"
9#include "fml/mapping.h"
13#include "impeller/core/range.h"
16
17namespace flutter {
18namespace gpu {
19
21
23 std::shared_ptr<impeller::DeviceBuffer> device_buffer)
24 : device_buffer_(std::move(device_buffer)) {}
25
27
28std::shared_ptr<impeller::DeviceBuffer> DeviceBuffer::GetBuffer() {
29 return device_buffer_;
30}
31
33 size_t destination_offset_in_bytes) {
34 if (!device_buffer_->CopyHostBuffer(
35 reinterpret_cast<const uint8_t*>(source_bytes.data()),
36 impeller::Range(0, source_bytes.length_in_bytes()),
37 destination_offset_in_bytes)) {
38 return false;
39 }
40 return true;
41}
42
43} // namespace gpu
44} // namespace flutter
45
46//----------------------------------------------------------------------------
47/// Exports
48///
49
51 Dart_Handle wrapper,
52 flutter::gpu::Context* gpu_context,
53 int storage_mode,
54 int size_in_bytes) {
57 desc.size = size_in_bytes;
58 auto device_buffer =
59 gpu_context->GetContext().GetResourceAllocator()->CreateBuffer(desc);
60 if (!device_buffer) {
61 FML_LOG(ERROR) << "Failed to create device buffer.";
62 return false;
63 }
64
65 auto res =
66 fml::MakeRefCounted<flutter::gpu::DeviceBuffer>(std::move(device_buffer));
67 res->AssociateWithDartWrapper(wrapper);
68
69 return true;
70}
71
73 Dart_Handle wrapper,
74 flutter::gpu::Context* gpu_context,
75 Dart_Handle byte_data) {
76 std::shared_ptr<impeller::DeviceBuffer> device_buffer = nullptr;
77 {
78 // `DartByteData` gets raw pointers into the Dart heap via
79 // `Dart_TypedDataAcquireData`. So it must be destructed before
80 // `AssociateWithDartWrapper` is called, which mutates the heap.
81 auto data = tonic::DartByteData(byte_data);
82 auto mapping = fml::NonOwnedMapping(reinterpret_cast<uint8_t*>(data.data()),
83 data.length_in_bytes());
84 device_buffer =
85 gpu_context->GetContext().GetResourceAllocator()->CreateBufferWithCopy(
86 mapping);
87 }
88 if (!device_buffer) {
89 FML_LOG(ERROR) << "Failed to create device buffer with copy.";
90 return false;
91 }
92
93 auto res =
94 fml::MakeRefCounted<flutter::gpu::DeviceBuffer>(std::move(device_buffer));
95 res->AssociateWithDartWrapper(wrapper);
96
97 return true;
98}
99
101 flutter::gpu::DeviceBuffer* device_buffer,
102 Dart_Handle source_byte_data,
103 int destination_offset_in_bytes) {
104 return device_buffer->Overwrite(tonic::DartByteData(source_byte_data),
105 destination_offset_in_bytes);
106}
107
109 flutter::gpu::DeviceBuffer* device_buffer,
110 int offset_in_bytes,
111 int size_in_bytes) {
112 device_buffer->GetBuffer()->Flush(
113 impeller::Range(offset_in_bytes, size_in_bytes));
114 return true;
115}
impeller::Context & GetContext()
Definition context.cc:77
bool Overwrite(const tonic::DartByteData &source_bytes, size_t destination_offset_in_bytes)
std::shared_ptr< impeller::DeviceBuffer > GetBuffer()
DeviceBuffer(std::shared_ptr< impeller::DeviceBuffer > device_buffer)
virtual std::shared_ptr< Allocator > GetResourceAllocator() const =0
Returns the allocator used to create textures and buffers on the device.
const void * data() const
size_t length_in_bytes() const
#define IMPLEMENT_WRAPPERTYPEINFO(LibraryName, ClassName)
#define FML_LOG(severity)
Definition logging.h:101
bool InternalFlutterGpu_DeviceBuffer_Flush(flutter::gpu::DeviceBuffer *device_buffer, int offset_in_bytes, int size_in_bytes)
bool InternalFlutterGpu_DeviceBuffer_Initialize(Dart_Handle wrapper, flutter::gpu::Context *gpu_context, int storage_mode, int size_in_bytes)
bool InternalFlutterGpu_DeviceBuffer_InitializeWithHostData(Dart_Handle wrapper, flutter::gpu::Context *gpu_context, Dart_Handle byte_data)
bool InternalFlutterGpu_DeviceBuffer_Overwrite(flutter::gpu::DeviceBuffer *device_buffer, Dart_Handle source_byte_data, int destination_offset_in_bytes)
constexpr impeller::StorageMode ToImpellerStorageMode(FlutterGPUStorageMode value)
Definition formats.h:25
Definition ref_ptr.h:261
std::shared_ptr< const fml::Mapping > data