Flutter Engine
 
Loading...
Searching...
No Matches
image_descriptor.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
15
16namespace flutter {
17
19
20const SkImageInfo ImageDescriptor::CreateImageInfo() const {
21 FML_DCHECK(generator_);
22 return generator_->GetInfo();
23}
24
25ImageDescriptor::ImageDescriptor(sk_sp<SkData> buffer,
26 const SkImageInfo& image_info,
27 std::optional<size_t> row_bytes)
28 : buffer_(std::move(buffer)),
29 generator_(nullptr),
30 image_info_(image_info),
31 row_bytes_(row_bytes) {}
32
33ImageDescriptor::ImageDescriptor(sk_sp<SkData> buffer,
34 std::shared_ptr<ImageGenerator> generator)
35 : buffer_(std::move(buffer)),
36 generator_(std::move(generator)),
37 image_info_(CreateImageInfo()),
38 row_bytes_(std::nullopt) {}
39
40Dart_Handle ImageDescriptor::initEncoded(Dart_Handle descriptor_handle,
41 ImmutableBuffer* immutable_buffer,
42 Dart_Handle callback_handle) {
43 if (!Dart_IsClosure(callback_handle)) {
44 return tonic::ToDart("Callback must be a function");
45 }
46
47 if (!immutable_buffer) {
48 return tonic::ToDart("Buffer parameter must not be null");
49 }
50
51 // This has to be valid because this method is called from Dart.
52 auto dart_state = UIDartState::Current();
53 auto registry = dart_state->GetImageGeneratorRegistry();
54
55 if (!registry) {
56 return tonic::ToDart(
57 "Failed to access the internal image decoder "
58 "registry on this isolate. Please file a bug on "
59 "https://github.com/flutter/flutter/issues.");
60 }
61
62 auto generator =
63 registry->CreateCompatibleGenerator(immutable_buffer->data());
64
65 if (!generator) {
66 // No compatible image decoder was found.
67 return tonic::ToDart("Invalid image data");
68 }
69
70 auto descriptor = fml::MakeRefCounted<ImageDescriptor>(
71 immutable_buffer->data(), std::move(generator));
72
73 FML_DCHECK(descriptor);
74
75 descriptor->AssociateWithDartWrapper(descriptor_handle);
76 tonic::DartInvoke(callback_handle, {Dart_TypeVoid()});
77
78 return Dart_Null();
79}
80
81void ImageDescriptor::initRaw(Dart_Handle descriptor_handle,
83 int width,
84 int height,
85 int row_bytes,
86 PixelFormat pixel_format) {
87 SkColorType color_type = kUnknown_SkColorType;
88 SkAlphaType alpha_type = kPremul_SkAlphaType;
89 switch (pixel_format) {
90 case PixelFormat::kRGBA8888:
91 color_type = kRGBA_8888_SkColorType;
92 break;
93 case PixelFormat::kBGRA8888:
94 color_type = kBGRA_8888_SkColorType;
95 break;
96 case PixelFormat::kRGBAFloat32:
97 // `PixelFormat.rgbaFloat32` is documented to not use premultiplied alpha.
98 color_type = kRGBA_F32_SkColorType;
99 alpha_type = kUnpremul_SkAlphaType;
100 break;
101 }
102 FML_DCHECK(color_type != kUnknown_SkColorType);
103 auto image_info = SkImageInfo::Make(width, height, color_type, alpha_type);
104 auto descriptor = fml::MakeRefCounted<ImageDescriptor>(
105 data->data(), std::move(image_info),
106 row_bytes == -1 ? std::nullopt : std::optional<size_t>(row_bytes));
107 descriptor->AssociateWithDartWrapper(descriptor_handle);
108}
109
110void ImageDescriptor::instantiateCodec(Dart_Handle codec_handle,
111 int target_width,
112 int target_height) {
113 fml::RefPtr<Codec> ui_codec;
114 if (!generator_ || generator_->GetFrameCount() == 1) {
115 ui_codec = fml::MakeRefCounted<SingleFrameCodec>(
116 static_cast<fml::RefPtr<ImageDescriptor>>(this), target_width,
117 target_height);
118 } else {
119 ui_codec = fml::MakeRefCounted<MultiFrameCodec>(generator_);
120 }
121 ui_codec->AssociateWithDartWrapper(codec_handle);
122}
123
124sk_sp<SkImage> ImageDescriptor::image() const {
125 return generator_->GetImage();
126}
127
128bool ImageDescriptor::get_pixels(const SkPixmap& pixmap) const {
129 FML_DCHECK(generator_);
130 return generator_->GetPixels(pixmap.info(), pixmap.writable_addr(),
131 pixmap.rowBytes());
132}
133
134} // namespace flutter
Creates an image descriptor for encoded or decoded image data, describing the width,...
sk_sp< SkData > data() const
Callers should not modify the returned data. This is not exposed to Dart.
#define IMPLEMENT_WRAPPERTYPEINFO(LibraryName, ClassName)
#define FML_DCHECK(condition)
Definition logging.h:122
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 disable vm Disable the Dart VM Service The Dart VM Service is never available in release mode Bind to the IPv6 localhost address for the Dart VM Service Ignored if vm service host is set profile Make the profiler discard new samples once the profiler sample buffer is full When this flag is not the profiler sample buffer is used as a ring buffer
Definition switch_defs.h:98
Definition ref_ptr.h:261
Dart_Handle ToDart(const T &object)
Dart_Handle DartInvoke(Dart_Handle closure, std::initializer_list< Dart_Handle > args)
uint32_t color_type
int32_t height
int32_t width
uint32_t alpha_type
std::shared_ptr< const fml::Mapping > data