Flutter Engine Uber Docs
Docs for the entire Flutter Engine repo.
 
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
21 const SkImageInfo& sk_image_info) {
23 switch (sk_image_info.colorType()) {
24 case kUnknown_SkColorType:
26 break;
27 case kRGBA_8888_SkColorType:
29 break;
30 case kBGRA_8888_SkColorType:
32 break;
33 case kRGBA_F32_SkColorType:
35 break;
36 case kGray_8_SkColorType:
37 format = kGray8;
38 break;
39 default:
40 FML_DCHECK(false) << "Unsupported pixel format: "
41 << sk_image_info.colorType();
43 }
44 return ImageInfo{
45 .width = static_cast<uint32_t>(sk_image_info.width()),
46 .height = static_cast<uint32_t>(sk_image_info.height()),
47 .format = format,
48 .alpha_type = sk_image_info.alphaType(),
49 .color_space = sk_image_info.refColorSpace(),
50 };
51}
52
53SkImageInfo ImageDescriptor::ToSkImageInfo(const ImageInfo& image_info) {
54 SkColorType color_type = kUnknown_SkColorType;
55 switch (image_info.format) {
57 color_type = kUnknown_SkColorType;
58 break;
60 color_type = kRGBA_8888_SkColorType;
61 break;
63 color_type = kBGRA_8888_SkColorType;
64 break;
66 color_type = kRGBA_F32_SkColorType;
67 break;
69 color_type = kGray_8_SkColorType;
70 break;
72 FML_DCHECK(false) << "not a supported skia format";
73 break;
74 }
75 return SkImageInfo::Make(image_info.width, image_info.height, color_type,
77}
78
79ImageDescriptor::ImageDescriptor(sk_sp<SkData> buffer,
80 const ImageInfo& image_info,
81 std::optional<size_t> row_bytes)
82 : buffer_(std::move(buffer)),
83 image_info_(image_info),
84 generator_(nullptr),
85 row_bytes_(row_bytes) {}
86
87ImageDescriptor::ImageDescriptor(sk_sp<SkData> buffer,
88 std::shared_ptr<ImageGenerator> generator)
89 : buffer_(std::move(buffer)),
90 image_info_(CreateImageInfo(generator->GetInfo())),
91 generator_(std::move(generator)),
92 row_bytes_(std::nullopt) {}
93
94Dart_Handle ImageDescriptor::initEncoded(Dart_Handle descriptor_handle,
95 ImmutableBuffer* immutable_buffer,
96 Dart_Handle callback_handle) {
97 if (!Dart_IsClosure(callback_handle)) {
98 return tonic::ToDart("Callback must be a function");
99 }
100
101 if (!immutable_buffer) {
102 return tonic::ToDart("Buffer parameter must not be null");
103 }
104
105 // This has to be valid because this method is called from Dart.
106 auto dart_state = UIDartState::Current();
107 auto registry = dart_state->GetImageGeneratorRegistry();
108
109 if (!registry) {
110 return tonic::ToDart(
111 "Failed to access the internal image decoder "
112 "registry on this isolate. Please file a bug on "
113 "https://github.com/flutter/flutter/issues.");
114 }
115
116 auto generator =
117 registry->CreateCompatibleGenerator(immutable_buffer->data());
118
119 if (!generator) {
120 // No compatible image decoder was found.
121 return tonic::ToDart("Invalid image data");
122 }
123
124 auto descriptor = fml::MakeRefCounted<ImageDescriptor>(
125 immutable_buffer->data(), std::move(generator));
126
127 FML_DCHECK(descriptor);
128
129 descriptor->AssociateWithDartWrapper(descriptor_handle);
130 tonic::DartInvoke(callback_handle, {Dart_TypeVoid()});
131
132 return Dart_Null();
133}
134
135namespace {
136// Must be kept in sync with painting.dart.
137ImageDescriptor::PixelFormat toImageDescriptorPixelFormat(int val) {
138 switch (val) {
139 case 0:
141 case 1:
143 case 2:
145 case 3:
147 default:
148 FML_DCHECK(false) << "unrecognized format";
150 }
151}
152} // namespace
153
154void ImageDescriptor::initRaw(Dart_Handle descriptor_handle,
156 int width,
157 int height,
158 int row_bytes,
159 int pixel_format) {
160 ImageDescriptor::PixelFormat image_descriptor_pixel_format =
161 toImageDescriptorPixelFormat(pixel_format);
162 const ImageInfo image_info = {
163 .width = static_cast<uint32_t>(width),
164 .height = static_cast<uint32_t>(height),
165 .format = image_descriptor_pixel_format,
166 .alpha_type = image_descriptor_pixel_format == PixelFormat::kRGBAFloat32
167 ? kUnpremul_SkAlphaType
168 : kPremul_SkAlphaType,
169 .color_space = SkColorSpace::MakeSRGB(),
170 };
171
172 auto descriptor = fml::MakeRefCounted<ImageDescriptor>(
173 data->data(), image_info,
174 row_bytes == -1 ? std::nullopt : std::optional<size_t>(row_bytes));
175 descriptor->AssociateWithDartWrapper(descriptor_handle);
176}
177
178// This should match the order of `TargetPixelFormat` in
179// //engine/src/flutter/lib/ui/painting.dart.
181 switch (value) {
182 case 0:
184 case 1:
186 case 2:
188 default:
189 FML_DCHECK(false) << "Unknown pixel format.";
191 }
192}
193
194void ImageDescriptor::instantiateCodec(Dart_Handle codec_handle,
195 int32_t target_width,
196 int32_t target_height,
197 int32_t destination_format) {
198 fml::RefPtr<Codec> ui_codec;
199 if (!generator_ || generator_->GetFrameCount() == 1) {
200 ui_codec = fml::MakeRefCounted<SingleFrameCodec>(
201 static_cast<fml::RefPtr<ImageDescriptor>>(this), //
202 target_width, //
203 target_height, //
204 ToImageDecoderTargetPixelFormat(destination_format));
205 } else {
206 ui_codec = fml::MakeRefCounted<MultiFrameCodec>(generator_);
207 }
208 ui_codec->AssociateWithDartWrapper(codec_handle);
209}
210
211sk_sp<SkImage> ImageDescriptor::image() const {
212 return generator_->GetImage();
213}
214
215bool ImageDescriptor::get_pixels(const SkPixmap& pixmap) const {
216 FML_DCHECK(generator_);
217 return generator_->GetPixels(pixmap.info(), pixmap.writable_addr(),
218 pixmap.rowBytes());
219}
220
222 switch (image_info_.format) {
223 case kUnknown:
224 return 0;
225 case kGray8:
226 return 1;
227 case kRGBA8888:
228 case kBGRA8888:
229 case kR32Float:
230 return 4;
231 case kRGBAFloat32:
232 return 16;
233 }
234}
235
236} // namespace flutter
@ kDontCare
Explicitly declare the target pixel is left for the engine to decide.
@ kUnknown
An unknown pixel format, reserved for error cases.
Creates an image descriptor for encoded or decoded image data, describing the width,...
sk_sp< SkImage > image() const
int width() const
The width of this image, EXIF oriented if applicable.
void instantiateCodec(Dart_Handle codec, int32_t target_width, int32_t target_height, int32_t destination_format)
Associates a flutter::Codec object with the dart.ui Codec handle.
static SkImageInfo ToSkImageInfo(const ImageInfo &image_info)
int row_bytes() const
The byte length of the first row of the image. Defaults to width() * 4.
static ImageInfo CreateImageInfo(const SkImageInfo &sk_image_info)
int height() const
The height of this image. EXIF oriented if applicable.
bool get_pixels(const SkPixmap &pixmap) const
Gets pixels for this image transformed based on the EXIF orientation tag, if applicable.
int bytesPerPixel() const
The bytes per pixel of the image.
static Dart_Handle initEncoded(Dart_Handle descriptor_handle, ImmutableBuffer *immutable_buffer, Dart_Handle callback_handle)
Asynchronously initializes an ImageDescriptor for an encoded image, as long as the format is recogniz...
static void initRaw(Dart_Handle descriptor_handle, const fml::RefPtr< ImmutableBuffer > &data, int width, int height, int row_bytes, int pixel_format)
Synchronously initializes an ImageDescriptor for decompressed image data as specified by the PixelFor...
const ImageInfo & image_info() const
The orientation corrected image info for this image.
sk_sp< SkData > data() const
The underlying buffer for this image.
sk_sp< SkData > data() const
Callers should not modify the returned data. This is not exposed to Dart.
static UIDartState * Current()
#define IMPLEMENT_WRAPPERTYPEINFO(LibraryName, ClassName)
int32_t value
uint32_t uint32_t * format
#define FML_DCHECK(condition)
Definition logging.h:122
ImageDecoder::TargetPixelFormat ToImageDecoderTargetPixelFormat(int32_t value)
DEF_SWITCHES_START aot vmservice shared library Name of the *so containing AOT compiled Dart assets for launching the service isolate vm snapshot data
Definition switch_defs.h:36
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
const sk_sp< SkColorSpace > color_space