Flutter Engine Uber Docs
Docs for the entire Flutter Engine repo.
 
Loading...
Searching...
No Matches
image.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
8
9#if IMPELLER_SUPPORTS_RENDERING
12#endif
17
18namespace flutter {
19
20typedef CanvasImage Image;
21
22// Since _Image is a private class, we can't use IMPLEMENT_WRAPPERTYPEINFO
24const tonic::DartWrapperInfo& Image::dart_wrapper_info_ =
26
27CanvasImage::CanvasImage() = default;
28
30
32 Dart_Handle ui_lib = Dart_LookupLibrary(tonic::ToDart("dart:ui"));
33 return tonic::DartInvokeField(ui_lib, "_wrapImage", {ToDart(this)});
34}
35
36Dart_Handle CanvasImage::toByteData(int format, Dart_Handle callback) {
37 return EncodeImage(this, format, callback);
38}
39
41 image_.reset();
43}
44
46 if (image_->skia_image()) {
47 return ColorSpace::kSRGB;
48 } else if (image_->impeller_texture()) {
49#if IMPELLER_SUPPORTS_RENDERING
50 return ImageEncodingImpeller::GetColorSpace(image_->impeller_texture());
51#endif // IMPELLER_SUPPORTS_RENDERING
52 }
53 return ColorSpace::kSRGB;
54}
55
56} // namespace flutter
57
58namespace flutter {
59
60namespace {
61
62int BytesPerPixel(PixelFormat pixel_format) {
63 switch (pixel_format) {
67 return 4;
69 return 16;
70 }
71 return 4;
72}
73
74SkColorType PixelFormatToSkColorType(PixelFormat pixel_format) {
75 switch (pixel_format) {
77 return kRGBA_8888_SkColorType;
79 return kBGRA_8888_SkColorType;
81 return kRGBA_F32_SkColorType;
83 return kUnknown_SkColorType;
84 }
85 return kUnknown_SkColorType;
86}
87
88// Returns only static strings.
89const char* DoDecodeImageFromPixelsSync(Dart_Handle pixels_handle,
90 uint32_t width,
91 uint32_t height,
92 int32_t pixel_format,
93 Dart_Handle raw_image_handle) {
94 auto* dart_state = UIDartState::Current();
95 if (!dart_state) {
96 return "Dart state is null.";
97 }
98
99 if (!dart_state->IsImpellerEnabled()) {
100 return "decodeImageFromPixelsSync is not implemented on Skia.";
101 }
102
103 if (width == 0 || height == 0) {
104 return "Image dimensions must be greater than zero.";
105 }
106
107 if (pixel_format < 0 ||
108 pixel_format > static_cast<int32_t>(kLastPixelFormat)) {
109 return "Invalid pixel format.";
110 }
111 PixelFormat format = static_cast<PixelFormat>(pixel_format);
112
113 sk_sp<SkData> sk_data;
114 sk_sp<SkImage> sk_image;
115 {
116 tonic::Uint8List pixels(pixels_handle);
117 if (!pixels.data()) {
118 return "Pixels must not be null.";
119 }
120
121 int32_t row_bytes = width * BytesPerPixel(format);
122 SkColorType color_type = PixelFormatToSkColorType(format);
123 if (color_type == kUnknown_SkColorType) {
124 return "Unsupported pixel format.";
125 }
126
127 SkImageInfo image_info =
128 SkImageInfo::Make(width, height, color_type, kUnpremul_SkAlphaType);
129 if (pixel_format == 2) { // rgbaFloat32
130 image_info = image_info.makeAlphaType(kUnpremul_SkAlphaType);
131 } else {
132 image_info = image_info.makeAlphaType(kPremul_SkAlphaType);
133 }
134
135 sk_data = SkData::MakeWithCopy(pixels.data(), pixels.num_elements());
136 sk_image = SkImages::RasterFromData(image_info, sk_data, row_bytes);
137 if (!sk_image) {
138 return "Failed to create image from pixels.";
139 }
140 }
141
142 auto snapshot_delegate = dart_state->GetSnapshotDelegate();
143 auto raster_task_runner = dart_state->GetTaskRunners().GetRasterTaskRunner();
144
145 auto result_image = CanvasImage::Create();
146 sk_sp<DlImage> deferred_image;
147
148#if IMPELLER_SUPPORTS_RENDERING
150 sk_image, std::move(snapshot_delegate), std::move(raster_task_runner));
151#endif // IMPELLER_SUPPORTS_RENDERING
152
153 result_image->set_image(deferred_image);
154 result_image->AssociateWithDartWrapper(raw_image_handle);
155
156 return nullptr;
157}
158} // namespace
159
160void CanvasImage::decodeImageFromPixelsSync(Dart_Handle pixels_handle,
161 uint32_t width,
162 uint32_t height,
163 int32_t pixel_format,
164 Dart_Handle raw_image_handle) {
165 const char* error = DoDecodeImageFromPixelsSync(
166 pixels_handle, width, height, pixel_format, raw_image_handle);
167 if (error) {
168 Dart_ThrowException(tonic::ToDart(error));
169 }
170}
171
172} // namespace flutter
Dart_Handle toByteData(int format, Dart_Handle callback)
Definition image.cc:36
static void decodeImageFromPixelsSync(Dart_Handle pixels_handle, uint32_t width, uint32_t height, int32_t pixel_format, Dart_Handle raw_image_handle)
Definition image.cc:160
~CanvasImage() override
static fml::RefPtr< CanvasImage > Create()
Definition image.h:36
Dart_Handle CreateOuterWrapping()
Definition image.cc:31
static int GetColorSpace(const std::shared_ptr< impeller::Texture > &texture)
static sk_sp< PixelDeferredImageGPUImpeller > Make(sk_sp< SkImage > image, fml::TaskRunnerAffineWeakPtr< SnapshotDelegate > snapshot_delegate, fml::RefPtr< fml::TaskRunner > raster_task_runner)
static UIDartState * Current()
const uint8_t uint32_t uint32_t GError ** error
uint32_t uint32_t * format
FlutterDesktopBinaryReply callback
constexpr PixelFormat kLastPixelFormat
Definition image.h:21
@ kSRGB
Definition image.h:25
static const tonic::DartWrapperInfo kDartWrapperInfoUIImage("ui", "_Image")
CanvasImage Image
Definition dart_ui.cc:51
Dart_Handle EncodeImage(CanvasImage *canvas_image, int format, Dart_Handle callback_handle)
PixelFormat
The Pixel formats supported by Impeller. The naming convention denotes the usage of the component,...
Definition formats.h:99
Dart_Handle ToDart(const T &object)
Dart_Handle DartInvokeField(Dart_Handle target, const char *name, std::initializer_list< Dart_Handle > args)
uint32_t color_type
int32_t height
int32_t width