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/ui/painting/image.h" |
6 | |
7 | #include "flutter/lib/ui/painting/image_encoding.h" |
8 | #include "third_party/tonic/converter/dart_converter.h" |
9 | #include "third_party/tonic/dart_args.h" |
10 | #include "third_party/tonic/dart_binding_macros.h" |
11 | #include "third_party/tonic/dart_library_natives.h" |
12 | |
13 | namespace flutter { |
14 | |
15 | typedef CanvasImage Image; |
16 | |
17 | IMPLEMENT_WRAPPERTYPEINFO(ui, Image); |
18 | |
19 | #define FOR_EACH_BINDING(V) \ |
20 | V(Image, width) \ |
21 | V(Image, height) \ |
22 | V(Image, toByteData) \ |
23 | V(Image, dispose) |
24 | |
25 | FOR_EACH_BINDING(DART_NATIVE_CALLBACK) |
26 | |
27 | void CanvasImage::RegisterNatives(tonic::DartLibraryNatives* natives) { |
28 | natives->Register({FOR_EACH_BINDING(DART_REGISTER_NATIVE)}); |
29 | } |
30 | |
31 | CanvasImage::CanvasImage() = default; |
32 | |
33 | CanvasImage::~CanvasImage() = default; |
34 | |
35 | Dart_Handle CanvasImage::toByteData(int format, Dart_Handle callback) { |
36 | return EncodeImage(this, format, callback); |
37 | } |
38 | |
39 | void CanvasImage::dispose() { |
40 | image_.reset(); |
41 | ClearDartWrapper(); |
42 | } |
43 | |
44 | size_t CanvasImage::GetAllocationSize() const { |
45 | if (auto image = image_.get()) { |
46 | const auto& info = image->imageInfo(); |
47 | const auto kMipmapOverhead = 4.0 / 3.0; |
48 | const size_t image_byte_size = info.computeMinByteSize() * kMipmapOverhead; |
49 | return image_byte_size + sizeof(this); |
50 | } else { |
51 | return sizeof(CanvasImage); |
52 | } |
53 | } |
54 | |
55 | } // namespace flutter |
56 |