Flutter Engine
The Flutter Engine
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
5#include "flutter/lib/ui/painting/image_descriptor.h"
6
7#include "flutter/fml/build_config.h"
8#include "flutter/fml/logging.h"
9#include "flutter/fml/trace_event.h"
10#include "flutter/lib/ui/painting/multi_frame_codec.h"
11#include "flutter/lib/ui/painting/single_frame_codec.h"
12#include "flutter/lib/ui/ui_dart_state.h"
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) {
89 switch (pixel_format) {
90 case PixelFormat::kRGBA8888:
92 break;
93 case PixelFormat::kBGRA8888:
95 break;
96 case PixelFormat::kRGBAFloat32:
97 // `PixelFormat.rgbaFloat32` is documented to not use premultiplied alpha.
100 break;
101 }
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
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
kUnpremul_SkAlphaType
SkAlphaType
Definition: SkAlphaType.h:26
@ kPremul_SkAlphaType
pixel components are premultiplied by alpha
Definition: SkAlphaType.h:29
SkColorType
Definition: SkColorType.h:19
@ kBGRA_8888_SkColorType
pixel with 8 bits for blue, green, red, alpha; in 32-bit word
Definition: SkColorType.h:26
@ kRGBA_8888_SkColorType
pixel with 8 bits for red, green, blue, alpha; in 32-bit word
Definition: SkColorType.h:24
@ kRGBA_F32_SkColorType
pixel using C float for red, green, blue, alpha; in 128-bit word
Definition: SkColorType.h:40
@ kUnknown_SkColorType
uninitialized
Definition: SkColorType.h:20
size_t rowBytes() const
Definition: SkPixmap.h:145
const SkImageInfo & info() const
Definition: SkPixmap.h:135
void * writable_addr() const
Definition: SkPixmap.h:483
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.
DART_EXPORT Dart_Handle Dart_TypeVoid(void)
struct _Dart_Handle * Dart_Handle
Definition: dart_api.h:258
DART_EXPORT Dart_Handle Dart_Null(void)
DART_EXPORT bool Dart_IsClosure(Dart_Handle object)
#define FML_DCHECK(condition)
Definition: logging.h:103
sk_sp< const SkImage > image
Definition: SkRecords.h:269
IMPLEMENT_WRAPPERTYPEINFO(flutter_gpu, FlutterGpuTestClass)
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 vm service A custom Dart VM Service port The default is to pick a randomly available open port disable vm Disable the Dart VM Service The Dart VM Service is never available in release mode disable vm service Disable mDNS Dart VM Service publication Bind to the IPv6 localhost address for the Dart VM Service Ignored if vm service host is set endless trace buffer
Definition: switches.h:126
Definition: ref_ptr.h:256
Dart_Handle ToDart(const T &object)
Dart_Handle DartInvoke(Dart_Handle closure, std::initializer_list< Dart_Handle > args)
Definition: dart_invoke.cc:20
uint32_t color_type
int32_t height
int32_t width
uint32_t alpha_type
static SkImageInfo Make(int width, int height, SkColorType ct, SkAlphaType at)
std::shared_ptr< const fml::Mapping > data
Definition: texture_gles.cc:63