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_PAINTNIG_IMMUTABLE_BUFER_H_
6#define FLUTTER_LIB_UI_PAINTNIG_IMMUTABLE_BUFER_H_
7
8#include <cstdint>
9
10#include "flutter/fml/macros.h"
11#include "flutter/lib/ui/dart_wrapper.h"
12#include "third_party/skia/include/core/SkData.h"
13#include "third_party/tonic/dart_library_natives.h"
14#include "third_party/tonic/logging/dart_invoke.h"
15#include "third_party/tonic/typed_data/typed_list.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 /// The zero indexed argument is the the caller that will be registered as the
34 /// Dart peer of the native ImmutableBuffer object.
35 ///
36 /// The first indexed argumented is a tonic::Uint8List of bytes to copy.
37 ///
38 /// The second indexed argument is expected to be a void callback to signal
39 /// when the copy has completed.
40 static void init(Dart_NativeArguments args);
41
42 /// The length of the data in bytes.
43 size_t length() const {
44 FML_DCHECK(data_);
45 return data_->size();
46 }
47
48 /// Callers should not modify the returned data. This is not exposed to Dart.
49 sk_sp<SkData> data() const { return data_; }
50
51 /// Clears the Dart native fields and removes the reference to the underlying
52 /// byte buffer.
53 ///
54 /// The byte buffer will continue to live if other objects hold a reference to
55 /// it.
56 void dispose() {
57 ClearDartWrapper();
58 data_.reset();
59 }
60
61 size_t GetAllocationSize() const override;
62
63 static void RegisterNatives(tonic::DartLibraryNatives* natives);
64
65 private:
66 explicit ImmutableBuffer(sk_sp<SkData> data) : data_(std::move(data)) {}
67
68 sk_sp<SkData> data_;
69
70 static sk_sp<SkData> MakeSkDataWithCopy(const void* data, size_t length);
71
72 DEFINE_WRAPPERTYPEINFO();
73 FML_FRIEND_MAKE_REF_COUNTED(ImmutableBuffer);
74 FML_DISALLOW_COPY_AND_ASSIGN(ImmutableBuffer);
75};
76
77} // namespace flutter
78
79#endif // FLUTTER_LIB_UI_PAINTNIG_IMMUTABLE_BUFER_H_
80