Flutter Engine
The Flutter Engine
Public Member Functions | Static Public Member Functions | List of all members
flutter::ImmutableBuffer Class Reference

#include <immutable_buffer.h>

Inheritance diagram for flutter::ImmutableBuffer:
flutter::RefCountedDartWrappable< ImmutableBuffer > fml::RefCountedThreadSafe< ImmutableBuffer > tonic::DartWrappable fml::internal::RefCountedThreadSafeBase

Public Member Functions

 ~ImmutableBuffer () override
 
size_t length () const
 The length of the data in bytes. More...
 
sk_sp< SkDatadata () const
 Callers should not modify the returned data. This is not exposed to Dart. More...
 
void dispose ()
 
- Public Member Functions inherited from flutter::RefCountedDartWrappable< ImmutableBuffer >
virtual void RetainDartWrappableReference () const override
 
virtual void ReleaseDartWrappableReference () const override
 
- Public Member Functions inherited from fml::RefCountedThreadSafe< ImmutableBuffer >
void Release () const
 
- Public Member Functions inherited from fml::internal::RefCountedThreadSafeBase
void AddRef () const
 
bool HasOneRef () const
 
void AssertHasOneRef () const
 
- Public Member Functions inherited from tonic::DartWrappable
 DartWrappable ()
 
virtual const DartWrapperInfoGetDartWrapperInfo () const =0
 
virtual void RetainDartWrappableReference () const =0
 
virtual void ReleaseDartWrappableReference () const =0
 
Dart_Handle CreateDartWrapper (DartState *dart_state)
 
void AssociateWithDartWrapper (Dart_Handle wrappable)
 
void ClearDartWrapper ()
 
Dart_WeakPersistentHandle dart_wrapper () const
 

Static Public Member Functions

static Dart_Handle init (Dart_Handle buffer_handle, Dart_Handle data, Dart_Handle callback_handle)
 
static Dart_Handle initFromAsset (Dart_Handle buffer_handle, Dart_Handle asset_name_handle, Dart_Handle callback_handle)
 
static Dart_Handle initFromFile (Dart_Handle buffer_handle, Dart_Handle file_path_handle, Dart_Handle callback_handle)
 

Additional Inherited Members

- Public Types inherited from tonic::DartWrappable
enum  DartNativeFields { kPeerIndex , kNumberOfNativeFields }
 
- Protected Member Functions inherited from fml::RefCountedThreadSafe< ImmutableBuffer >
 RefCountedThreadSafe ()
 
 ~RefCountedThreadSafe ()
 
- Protected Member Functions inherited from fml::internal::RefCountedThreadSafeBase
 RefCountedThreadSafeBase ()
 
 ~RefCountedThreadSafeBase ()
 
bool Release () const
 
void Adopt ()
 
- Protected Member Functions inherited from tonic::DartWrappable
virtual ~DartWrappable ()
 
- Static Protected Member Functions inherited from tonic::DartWrappable
static Dart_PersistentHandle GetTypeForWrapper (tonic::DartState *dart_state, const tonic::DartWrapperInfo &wrapper_info)
 

Detailed Description

A simple opaque handle to an immutable byte buffer suitable for use internally by the engine.

This data is not known by the Dart VM.

It is expected that C++ users of this object will not modify the data argument. No Dart side calls are provided to do so.

Definition at line 27 of file immutable_buffer.h.

Constructor & Destructor Documentation

◆ ~ImmutableBuffer()

flutter::ImmutableBuffer::~ImmutableBuffer ( )
override

Definition at line 26 of file immutable_buffer.cc.

26{}

Member Function Documentation

◆ data()

sk_sp< SkData > flutter::ImmutableBuffer::data ( ) const
inline

Callers should not modify the returned data. This is not exposed to Dart.

Definition at line 80 of file immutable_buffer.h.

80{ return data_; }

◆ dispose()

void flutter::ImmutableBuffer::dispose ( )
inline

Clears the Dart native fields and removes the reference to the underlying byte buffer.

The byte buffer will continue to live if other objects hold a reference to it.

Definition at line 87 of file immutable_buffer.h.

87 {
88 data_.reset();
90 }
void reset(T *ptr=nullptr)
Definition: SkRefCnt.h:310

◆ init()

Dart_Handle flutter::ImmutableBuffer::init ( Dart_Handle  buffer_handle,
Dart_Handle  data,
Dart_Handle  callback_handle 
)
static

Initializes a new ImmutableData from a Dart Uint8List.

buffer_handle is the caller that will be registered as the Dart peer of the native ImmutableBuffer object.

data is a tonic::Uint8List of bytes to copy.

callback_handle is expected to be a void callback to signal when the copy has completed.

Definition at line 28 of file immutable_buffer.cc.

30 {
31 if (!Dart_IsClosure(callback_handle)) {
32 return tonic::ToDart("Callback must be a function");
33 }
34
35 tonic::Uint8List dataList = tonic::Uint8List(data);
36
37 auto sk_data = MakeSkDataWithCopy(dataList.data(), dataList.num_elements());
38 dataList.Release();
39 auto buffer = fml::MakeRefCounted<ImmutableBuffer>(sk_data);
40 buffer->AssociateWithDartWrapper(buffer_handle);
41 tonic::DartInvoke(callback_handle, {Dart_TypeVoid()});
42
43 return Dart_Null();
44}
sk_sp< SkData > data() const
Callers should not modify the returned data. This is not exposed to Dart.
void AssociateWithDartWrapper(Dart_Handle wrappable)
DART_EXPORT Dart_Handle Dart_TypeVoid(void)
DART_EXPORT Dart_Handle Dart_Null(void)
DART_EXPORT bool Dart_IsClosure(Dart_Handle object)
DEF_SWITCHES_START aot vmservice shared library Name of the *so containing AOT compiled Dart assets for launching the service isolate vm snapshot The VM snapshot data that will be memory mapped as read only SnapshotAssetPath must be present isolate snapshot The isolate snapshot data that will be memory mapped as read only SnapshotAssetPath must be present cache dir Path to the cache directory This is different from the persistent_cache_path in embedder which is used for Skia shader cache icu native lib Path to the library file that exports the ICU data vm service The hostname IP address on which the Dart VM Service should be served If not defaults to or::depending on whether ipv6 is specified vm service A custom Dart VM Service port The default is to pick a randomly available open port disable vm Disable the Dart VM Service The Dart VM Service is never available in release mode disable vm service Disable mDNS Dart VM Service publication Bind to the IPv6 localhost address for the Dart VM Service Ignored if vm service host is set endless trace buffer
Definition: switches.h:126
Dart_Handle ToDart(const T &object)
Dart_Handle DartInvoke(Dart_Handle closure, std::initializer_list< Dart_Handle > args)
Definition: dart_invoke.cc:20

◆ initFromAsset()

Dart_Handle flutter::ImmutableBuffer::initFromAsset ( Dart_Handle  buffer_handle,
Dart_Handle  asset_name_handle,
Dart_Handle  callback_handle 
)
static

Initializes a new ImmutableData from an asset matching a provided asset string.

The zero indexed argument is the caller that will be registered as the Dart peer of the native ImmutableBuffer object.

The first indexed argumented is a String corresponding to the asset to load.

The second indexed argument is expected to be a void callback to signal when the copy has completed.

Definition at line 46 of file immutable_buffer.cc.

48 {
50 if (!Dart_IsClosure(callback_handle)) {
51 return tonic::ToDart("Callback must be a function");
52 }
53
54 uint8_t* chars = nullptr;
55 intptr_t asset_length = 0;
57 Dart_StringToUTF8(asset_name_handle, &chars, &asset_length);
58 if (Dart_IsError(result)) {
59 return tonic::ToDart("Asset name must be valid UTF8");
60 }
61
62 std::string asset_name = std::string{reinterpret_cast<const char*>(chars),
63 static_cast<size_t>(asset_length)};
64
65 auto* dart_state = UIDartState::Current();
66 auto ui_task_runner = dart_state->GetTaskRunners().GetUITaskRunner();
67 auto* buffer_callback_ptr =
68 new tonic::DartPersistentValue(dart_state, callback_handle);
69 auto* buffer_handle_ptr =
70 new tonic::DartPersistentValue(dart_state, raw_buffer_handle);
71 auto asset_manager = UIDartState::Current()
73 ->client()
75
76 auto ui_task = fml::MakeCopyable(
77 [buffer_callback_ptr, buffer_handle_ptr](const sk_sp<SkData>& sk_data,
78 size_t buffer_size) mutable {
79 std::unique_ptr<tonic::DartPersistentValue> buffer_handle(
80 buffer_handle_ptr);
81 std::unique_ptr<tonic::DartPersistentValue> buffer_callback(
82 buffer_callback_ptr);
83
84 auto dart_state = buffer_callback->dart_state().lock();
85 if (!dart_state) {
86 return;
87 }
88 tonic::DartState::Scope scope(dart_state);
89
90 if (!sk_data) {
91 // -1 is used as a sentinel that the file could not be opened.
92 tonic::DartInvoke(buffer_callback->Get(), {tonic::ToDart(-1)});
93 return;
94 }
95 auto buffer = fml::MakeRefCounted<ImmutableBuffer>(sk_data);
96 buffer->AssociateWithDartWrapper(buffer_handle->Get());
97 tonic::DartInvoke(buffer_callback->Get(), {tonic::ToDart(buffer_size)});
98 });
99
100 dart_state->GetConcurrentTaskRunner()->PostTask(
101 [asset_name = std::move(asset_name),
102 asset_manager = std::move(asset_manager),
103 ui_task_runner = std::move(ui_task_runner), ui_task] {
104 std::unique_ptr<fml::Mapping> mapping =
105 asset_manager->GetAsMapping(asset_name);
106
107 sk_sp<SkData> sk_data;
108 size_t buffer_size = 0;
109 if (mapping != nullptr) {
110 buffer_size = mapping->GetSize();
111 const void* bytes = static_cast<const void*>(mapping->GetMapping());
112 sk_data = MakeSkDataWithCopy(bytes, buffer_size);
113 }
114 ui_task_runner->PostTask(
115 [sk_data = std::move(sk_data), ui_task = ui_task, buffer_size]() {
116 ui_task(sk_data, buffer_size);
117 });
118 });
119 return Dart_Null();
120}
static uint32_t buffer_size(uint32_t offset, uint32_t maxAlignment)
virtual std::shared_ptr< AssetManager > GetAssetManager()=0
Returns the current collection of assets available on the platform.
PlatformConfigurationClient * client() const
Access to the platform configuration client (which typically is implemented by the RuntimeController)...
PlatformConfiguration * platform_configuration() const
static UIDartState * Current()
static void ThrowIfUIOperationsProhibited()
struct _Dart_Handle * Dart_Handle
Definition: dart_api.h:258
DART_EXPORT Dart_Handle Dart_StringToUTF8(Dart_Handle str, uint8_t **utf8_array, intptr_t *length)
DART_EXPORT bool Dart_IsError(Dart_Handle handle)
GAsyncResult * result
internal::CopyableLambda< T > MakeCopyable(T lambda)
Definition: make_copyable.h:57

◆ initFromFile()

Dart_Handle flutter::ImmutableBuffer::initFromFile ( Dart_Handle  buffer_handle,
Dart_Handle  file_path_handle,
Dart_Handle  callback_handle 
)
static

Initializes a new ImmutableData from an File path.

The zero indexed argument is the caller that will be registered as the Dart peer of the native ImmutableBuffer object.

The first indexed argumented is a String corresponding to the file path to load.

The second indexed argument is expected to be a void callback to signal when the copy has completed.

Definition at line 122 of file immutable_buffer.cc.

124 {
126 if (!Dart_IsClosure(callback_handle)) {
127 return tonic::ToDart("Callback must be a function");
128 }
129
130 uint8_t* chars = nullptr;
131 intptr_t file_path_length = 0;
133 Dart_StringToUTF8(file_path_handle, &chars, &file_path_length);
134 if (Dart_IsError(result)) {
135 return tonic::ToDart("File path must be valid UTF8");
136 }
137
138 std::string file_path = std::string{reinterpret_cast<const char*>(chars),
139 static_cast<size_t>(file_path_length)};
140
141 auto* dart_state = UIDartState::Current();
142 auto ui_task_runner = dart_state->GetTaskRunners().GetUITaskRunner();
143 auto* buffer_callback_ptr =
144 new tonic::DartPersistentValue(dart_state, callback_handle);
145 auto* buffer_handle_ptr =
146 new tonic::DartPersistentValue(dart_state, raw_buffer_handle);
147
148 auto ui_task = fml::MakeCopyable(
149 [buffer_callback_ptr, buffer_handle_ptr](const sk_sp<SkData>& sk_data,
150 size_t buffer_size) mutable {
151 std::unique_ptr<tonic::DartPersistentValue> buffer_handle(
152 buffer_handle_ptr);
153 std::unique_ptr<tonic::DartPersistentValue> buffer_callback(
154 buffer_callback_ptr);
155 auto dart_state = buffer_callback->dart_state().lock();
156 if (!dart_state) {
157 return;
158 }
159 tonic::DartState::Scope scope(dart_state);
160
161 if (!sk_data) {
162 // -1 is used as a sentinel that the file could not be opened.
163 tonic::DartInvoke(buffer_callback->Get(), {tonic::ToDart(-1)});
164 return;
165 }
166 auto buffer = fml::MakeRefCounted<ImmutableBuffer>(sk_data);
167 buffer->AssociateWithDartWrapper(buffer_handle->Get());
168 tonic::DartInvoke(buffer_callback->Get(), {tonic::ToDart(buffer_size)});
169 });
170
171 dart_state->GetConcurrentTaskRunner()->PostTask(
172 [file_path = std::move(file_path),
173 ui_task_runner = std::move(ui_task_runner), ui_task] {
174 auto mapping = std::make_unique<fml::FileMapping>(fml::OpenFile(
175 file_path.c_str(), false, fml::FilePermission::kRead));
176
177 sk_sp<SkData> sk_data;
178 size_t buffer_size = 0;
179 if (mapping->IsValid()) {
180 buffer_size = mapping->GetSize();
181 const void* bytes = static_cast<const void*>(mapping->GetMapping());
182 sk_data = MakeSkDataWithCopy(bytes, buffer_size);
183 }
184 ui_task_runner->PostTask(
185 [sk_data = std::move(sk_data), ui_task = ui_task, buffer_size]() {
186 ui_task(sk_data, buffer_size);
187 });
188 });
189 return Dart_Null();
190}
fml::UniqueFD OpenFile(const char *path, bool create_if_necessary, FilePermission permission)
This can open a directory on POSIX, but not on Windows.
Definition: file_posix.cc:66

◆ length()

size_t flutter::ImmutableBuffer::length ( ) const
inline

The length of the data in bytes.

Definition at line 74 of file immutable_buffer.h.

74 {
75 FML_DCHECK(data_);
76 return data_->size();
77 }
size_t size() const
Definition: SkData.h:30
#define FML_DCHECK(condition)
Definition: logging.h:103

The documentation for this class was generated from the following files: