Flutter Engine
The Flutter Engine
dart_byte_data.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 <cstring>
8
10
11namespace tonic {
12
13namespace {
14
15void FreeFinalizer(void* isolate_callback_data, void* peer) {
16 free(peer);
17}
18
19} // anonymous namespace
20
21// For large objects it is more efficient to use an external typed data object
22// with a buffer allocated outside the Dart heap.
23const size_t DartByteData::kExternalSizeThreshold = 1000;
24
27 auto handle = DartByteData{data, length}.dart_handle();
28 // The destructor should release the typed data.
29 return handle;
30 } else {
31 void* buf = ::malloc(length);
32 TONIC_DCHECK(buf);
33 ::memcpy(buf, data, length);
36 }
37}
38
40 : data_(nullptr), length_in_bytes_(0), dart_handle_(nullptr) {}
41
42DartByteData::DartByteData(const void* data, size_t length)
43 : data_(nullptr),
44 length_in_bytes_(0),
46 if (!Dart_IsError(dart_handle_)) {
48 auto acquire_result = Dart_TypedDataAcquireData(dart_handle_, &type, &data_,
49 &length_in_bytes_);
50
51 if (!Dart_IsError(acquire_result)) {
52 ::memcpy(data_, data, length_in_bytes_);
53 }
54 }
55}
56
58 : data_(nullptr), length_in_bytes_(0), dart_handle_(list) {
59 if (Dart_IsNull(list))
60 return;
61
63 Dart_TypedDataAcquireData(list, &type, &data_, &length_in_bytes_);
66 Dart_ThrowException(ToDart("Non-genuine ByteData passed to engine."));
67}
68
70 : data_(other.data_),
71 length_in_bytes_(other.length_in_bytes_),
72 dart_handle_(other.dart_handle_) {
73 other.data_ = nullptr;
74 other.dart_handle_ = nullptr;
75}
76
78 Release();
79}
80
81std::vector<char> DartByteData::Copy() const {
82 const char* ptr = static_cast<const char*>(data_);
83 return std::vector<char>(ptr, ptr + length_in_bytes_);
84}
85
87 if (data_) {
88 Dart_TypedDataReleaseData(dart_handle_);
89 data_ = nullptr;
90 }
91}
92
95 int index,
96 Dart_Handle& exception) {
99 return DartByteData(data);
100}
101
103 DartByteData val) {
105}
106
107} // namespace tonic
GLenum type
Dart_Handle dart_handle() const
static const size_t kExternalSizeThreshold
static Dart_Handle Create(const void *data, size_t length)
void Release() const
const void * data() const
std::vector< char > Copy() const
struct _Dart_Handle * Dart_Handle
Definition: dart_api.h:258
DART_EXPORT Dart_Handle Dart_TypedDataReleaseData(Dart_Handle object)
DART_EXPORT Dart_Handle Dart_GetNativeArgument(Dart_NativeArguments args, int index)
DART_EXPORT Dart_Handle Dart_TypedDataAcquireData(Dart_Handle object, Dart_TypedData_Type *type, void **data, intptr_t *len)
DART_EXPORT Dart_Handle Dart_ThrowException(Dart_Handle exception)
DART_EXPORT Dart_Handle Dart_NewExternalTypedDataWithFinalizer(Dart_TypedData_Type type, void *data, intptr_t length, void *peer, intptr_t external_allocation_size, Dart_HandleFinalizer callback)
struct _Dart_NativeArguments * Dart_NativeArguments
Definition: dart_api.h:3019
Dart_TypedData_Type
Definition: dart_api.h:2612
@ Dart_TypedData_kByteData
Definition: dart_api.h:2613
DART_EXPORT Dart_Handle Dart_NewTypedData(Dart_TypedData_Type type, intptr_t length)
DART_EXPORT void Dart_SetReturnValue(Dart_NativeArguments args, Dart_Handle retval)
DART_EXPORT bool Dart_IsNull(Dart_Handle object)
DART_EXPORT bool Dart_IsError(Dart_Handle handle)
G_BEGIN_DECLS G_MODULE_EXPORT FlValue * args
size_t length
void * malloc(size_t size)
Definition: allocation.cc:19
DART_EXPORT void FreeFinalizer(void *, void *value)
Dart_Handle ToDart(const T &object)
bool CheckAndHandleError(Dart_Handle handle)
Definition: dart_error.cc:33
std::shared_ptr< const fml::Mapping > data
Definition: texture_gles.cc:63
#define TONIC_DCHECK
Definition: macros.h:32