Flutter Engine
The 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
5#include "flutter/lib/gpu/device_buffer.h"
6
7#include "dart_api.h"
8#include "flutter/lib/gpu/formats.h"
9#include "fml/mapping.h"
14#include "impeller/core/range.h"
17
18namespace flutter {
19namespace gpu {
20
22
24 std::shared_ptr<impeller::DeviceBuffer> device_buffer)
25 : device_buffer_(std::move(device_buffer)) {}
26
28
29std::shared_ptr<impeller::DeviceBuffer> DeviceBuffer::GetBuffer() {
30 return device_buffer_;
31}
32
34 size_t destination_offset_in_bytes) {
35 if (!device_buffer_->CopyHostBuffer(
36 reinterpret_cast<const uint8_t*>(source_bytes.data()),
37 impeller::Range(0, source_bytes.length_in_bytes()),
38 destination_offset_in_bytes)) {
39 return false;
40 }
41 return true;
42}
43
44} // namespace gpu
45} // namespace flutter
46
47//----------------------------------------------------------------------------
48/// Exports
49///
50
52 Dart_Handle wrapper,
53 flutter::gpu::Context* gpu_context,
54 int storage_mode,
55 int size_in_bytes) {
57 desc.storage_mode = flutter::gpu::ToImpellerStorageMode(storage_mode);
58 desc.size = size_in_bytes;
59 auto device_buffer =
60 gpu_context->GetContext()->GetResourceAllocator()->CreateBuffer(desc);
61 if (!device_buffer) {
62 FML_LOG(ERROR) << "Failed to create device buffer.";
63 return false;
64 }
65
66 auto res =
67 fml::MakeRefCounted<flutter::gpu::DeviceBuffer>(std::move(device_buffer));
68 res->AssociateWithDartWrapper(wrapper);
69
70 return true;
71}
72
74 Dart_Handle wrapper,
75 flutter::gpu::Context* gpu_context,
76 Dart_Handle byte_data) {
77 auto data = tonic::DartByteData(byte_data);
78 auto mapping = fml::NonOwnedMapping(reinterpret_cast<uint8_t*>(data.data()),
79 data.length_in_bytes());
80 auto device_buffer =
81 gpu_context->GetContext()->GetResourceAllocator()->CreateBufferWithCopy(
82 mapping);
83 if (!device_buffer) {
84 FML_LOG(ERROR) << "Failed to create device buffer with copy.";
85 return false;
86 }
87
88 auto res =
89 fml::MakeRefCounted<flutter::gpu::DeviceBuffer>(std::move(device_buffer));
90 res->AssociateWithDartWrapper(wrapper);
91
92 return true;
93}
94
96 flutter::gpu::DeviceBuffer* device_buffer,
97 Dart_Handle source_byte_data,
98 int destination_offset_in_bytes) {
99 return device_buffer->Overwrite(tonic::DartByteData(source_byte_data),
100 destination_offset_in_bytes);
101}
std::shared_ptr< impeller::Context > GetContext()
Definition context.cc:65
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)
const void * data() const
size_t length_in_bytes() const
struct _Dart_Handle * Dart_Handle
Definition dart_api.h:258
#define IMPLEMENT_WRAPPERTYPEINFO(LibraryName, ClassName)
#define FML_LOG(severity)
Definition logging.h:82
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:256
#define ERROR(message)