Flutter Engine
The Flutter Engine
immutable_buffer.h
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#ifndef FLUTTER_LIB_UI_PAINTING_IMMUTABLE_BUFFER_H_
6#define FLUTTER_LIB_UI_PAINTING_IMMUTABLE_BUFFER_H_
7
8#include <cstdint>
9
10#include "flutter/fml/macros.h"
11#include "flutter/lib/ui/dart_wrapper.h"
16
17namespace flutter {
18
19//------------------------------------------------------------------------------
20/// A simple opaque handle to an immutable byte buffer suitable for use
21/// internally by the engine.
22///
23/// This data is not known by the Dart VM.
24///
25/// It is expected that C++ users of this object will not modify the data
26/// argument. No Dart side calls are provided to do so.
27class ImmutableBuffer : public RefCountedDartWrappable<ImmutableBuffer> {
28 public:
29 ~ImmutableBuffer() override;
30
31 /// Initializes a new ImmutableData from a Dart Uint8List.
32 ///
33 /// `buffer_handle` is the caller that will be registered as the Dart peer of
34 /// the native ImmutableBuffer object.
35 ///
36 /// `data` is a tonic::Uint8List of bytes to copy.
37 ///
38 /// `callback_handle` is expected to be a void callback to signal when the
39 /// copy has completed.
40 static Dart_Handle init(Dart_Handle buffer_handle,
42 Dart_Handle callback_handle);
43
44 /// Initializes a new ImmutableData from an asset matching a provided
45 /// asset string.
46 ///
47 /// The zero indexed argument is the caller that will be registered as the
48 /// Dart peer of the native ImmutableBuffer object.
49 ///
50 /// The first indexed argumented is a String corresponding to the asset
51 /// to load.
52 ///
53 /// The second indexed argument is expected to be a void callback to signal
54 /// when the copy has completed.
55 static Dart_Handle initFromAsset(Dart_Handle buffer_handle,
56 Dart_Handle asset_name_handle,
57 Dart_Handle callback_handle);
58
59 /// Initializes a new ImmutableData from an File path.
60 ///
61 /// The zero indexed argument is the caller that will be registered as the
62 /// Dart peer of the native ImmutableBuffer object.
63 ///
64 /// The first indexed argumented is a String corresponding to the file path
65 /// to load.
66 ///
67 /// The second indexed argument is expected to be a void callback to signal
68 /// when the copy has completed.
69 static Dart_Handle initFromFile(Dart_Handle buffer_handle,
70 Dart_Handle file_path_handle,
71 Dart_Handle callback_handle);
72
73 /// The length of the data in bytes.
74 size_t length() const {
75 FML_DCHECK(data_);
76 return data_->size();
77 }
78
79 /// Callers should not modify the returned data. This is not exposed to Dart.
80 sk_sp<SkData> data() const { return data_; }
81
82 /// Clears the Dart native fields and removes the reference to the underlying
83 /// byte buffer.
84 ///
85 /// The byte buffer will continue to live if other objects hold a reference to
86 /// it.
87 void dispose() {
88 data_.reset();
90 }
91
92 private:
93 explicit ImmutableBuffer(sk_sp<SkData> data) : data_(std::move(data)) {}
94
95 sk_sp<SkData> data_;
96
97 static sk_sp<SkData> MakeSkDataWithCopy(const void* data, size_t length);
98
99 DEFINE_WRAPPERTYPEINFO();
100 FML_FRIEND_MAKE_REF_COUNTED(ImmutableBuffer);
101 FML_DISALLOW_COPY_AND_ASSIGN(ImmutableBuffer);
102};
103
104} // namespace flutter
105
106#endif // FLUTTER_LIB_UI_PAINTING_IMMUTABLE_BUFFER_H_
size_t size() const
Definition: SkData.h:30
sk_sp< SkData > data() const
Callers should not modify the returned data. This is not exposed to Dart.
static Dart_Handle initFromAsset(Dart_Handle buffer_handle, Dart_Handle asset_name_handle, Dart_Handle callback_handle)
static Dart_Handle init(Dart_Handle buffer_handle, Dart_Handle data, Dart_Handle callback_handle)
size_t length() const
The length of the data in bytes.
static Dart_Handle initFromFile(Dart_Handle buffer_handle, Dart_Handle file_path_handle, Dart_Handle callback_handle)
void reset(T *ptr=nullptr)
Definition: SkRefCnt.h:310
struct _Dart_Handle * Dart_Handle
Definition: dart_api.h:258
#define FML_DCHECK(condition)
Definition: logging.h:103
Definition: ref_ptr.h:256