Flutter Engine
The Flutter Engine
Loading...
Searching...
No Matches
image_encoding.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_encoding.h"
6#include "flutter/lib/ui/painting/image_encoding_impl.h"
7
8#include <memory>
9#include <utility>
10
11#include "flutter/common/task_runners.h"
12#include "flutter/fml/build_config.h"
13#include "flutter/fml/make_copyable.h"
14#include "flutter/fml/status_or.h"
15#include "flutter/fml/trace_event.h"
16#include "flutter/lib/ui/painting/image.h"
17#if IMPELLER_SUPPORTS_RENDERING
18#include "flutter/lib/ui/painting/image_encoding_impeller.h"
19#endif // IMPELLER_SUPPORTS_RENDERING
20#include "flutter/lib/ui/painting/image_encoding_skia.h"
27
30using tonic::ToDart;
31
32namespace impeller {
33class Context;
34} // namespace impeller
35namespace flutter {
36namespace {
37
38void FinalizeSkData(void* isolate_callback_data, void* peer) {
39 SkData* buffer = reinterpret_cast<SkData*>(peer);
40 buffer->unref();
41}
42
43void InvokeDataCallback(std::unique_ptr<DartPersistentValue> callback,
45 std::shared_ptr<tonic::DartState> dart_state = callback->dart_state().lock();
46 if (!dart_state) {
47 return;
48 }
49 tonic::DartState::Scope scope(dart_state);
50 if (!buffer.ok()) {
51 std::string error_copy(buffer.status().message());
52 Dart_Handle dart_string = ToDart(error_copy);
53 DartInvoke(callback->value(), {Dart_Null(), dart_string});
54 return;
55 }
56 // Skia will not modify the buffer, and it is backed by memory that is
57 // read/write, so Dart can be given direct access to the buffer through an
58 // external Uint8List.
59 void* bytes = const_cast<void*>(buffer.value()->data());
60 const intptr_t length = buffer.value()->size();
61 void* peer = reinterpret_cast<void*>(buffer.value().release());
63 Dart_TypedData_kUint8, bytes, length, peer, length, FinalizeSkData);
64 DartInvoke(callback->value(), {dart_data, Dart_Null()});
65}
66
67sk_sp<SkData> CopyImageByteData(const sk_sp<SkImage>& raster_image,
70 FML_DCHECK(raster_image);
71
72 SkPixmap pixmap;
73
74 if (!raster_image->peekPixels(&pixmap)) {
75 FML_LOG(ERROR) << "Could not copy pixels from the raster image.";
76 return nullptr;
77 }
78
79 // The color types already match. No need to swizzle. Return early.
80 if (pixmap.colorType() == color_type && pixmap.alphaType() == alpha_type) {
81 return SkData::MakeWithCopy(pixmap.addr(), pixmap.computeByteSize());
82 }
83
84 // Perform swizzle if the type doesnt match the specification.
86 SkImageInfo::Make(raster_image->width(), raster_image->height(),
87 color_type, alpha_type, nullptr));
88
89 if (!surface) {
90 FML_LOG(ERROR) << "Could not set up the surface for swizzle.";
91 return nullptr;
92 }
93
94 surface->writePixels(pixmap, 0, 0);
95
96 if (!surface->peekPixels(&pixmap)) {
97 FML_LOG(ERROR) << "Pixel address is not available.";
98 return nullptr;
99 }
100
101 return SkData::MakeWithCopy(pixmap.addr(), pixmap.computeByteSize());
102}
103
104void EncodeImageAndInvokeDataCallback(
105 const sk_sp<DlImage>& image,
106 std::unique_ptr<DartPersistentValue> callback,
108 const fml::RefPtr<fml::TaskRunner>& ui_task_runner,
109 const fml::RefPtr<fml::TaskRunner>& raster_task_runner,
110 const fml::RefPtr<fml::TaskRunner>& io_task_runner,
111 const fml::WeakPtr<GrDirectContext>& resource_context,
112 const fml::TaskRunnerAffineWeakPtr<SnapshotDelegate>& snapshot_delegate,
113 const std::shared_ptr<const fml::SyncSwitch>& is_gpu_disabled_sync_switch,
114 const std::shared_ptr<impeller::Context>& impeller_context,
115 bool is_impeller_enabled) {
116 auto callback_task =
117 fml::MakeCopyable([callback = std::move(callback)](
118 fml::StatusOr<sk_sp<SkData>>&& encoded) mutable {
119 InvokeDataCallback(std::move(callback), std::move(encoded));
120 });
121 // The static leak checker gets confused by the use of fml::MakeCopyable in
122 // EncodeImage.
123 // NOLINTNEXTLINE(clang-analyzer-cplusplus.NewDeleteLeaks)
124 auto encode_task =
125 [callback_task = std::move(callback_task), format,
126 ui_task_runner](const fml::StatusOr<sk_sp<SkImage>>& raster_image) {
127 if (raster_image.ok()) {
128 sk_sp<SkData> encoded = EncodeImage(raster_image.value(), format);
129 ui_task_runner->PostTask([callback_task = callback_task,
130 encoded = std::move(encoded)]() mutable {
131 callback_task(std::move(encoded));
132 });
133 } else {
134 ui_task_runner->PostTask([callback_task = callback_task,
135 raster_image = raster_image]() mutable {
136 callback_task(raster_image.status());
137 });
138 }
139 };
140
142#if IMPELLER_SUPPORTS_RENDERING
143 if (is_impeller_enabled) {
145 image, encode_task, raster_task_runner, io_task_runner,
146 is_gpu_disabled_sync_switch, impeller_context);
147 return;
148 }
149#endif // IMPELLER_SUPPORTS_RENDERING
150 ConvertImageToRasterSkia(image, encode_task, raster_task_runner,
151 io_task_runner, resource_context, snapshot_delegate,
152 is_gpu_disabled_sync_switch);
153}
154
155} // namespace
156
158 int format,
159 Dart_Handle callback_handle) {
160 if (!canvas_image) {
161 return ToDart("encode called with non-genuine Image.");
162 }
163
164 if (!Dart_IsClosure(callback_handle)) {
165 return ToDart("Callback must be a function.");
166 }
167
168 ImageByteFormat image_format = static_cast<ImageByteFormat>(format);
169
170 auto callback = std::make_unique<DartPersistentValue>(
171 tonic::DartState::Current(), callback_handle);
172
173 const auto& task_runners = UIDartState::Current()->GetTaskRunners();
174
175 // The static leak checker gets confused by the use of fml::MakeCopyable.
176 // NOLINTNEXTLINE(clang-analyzer-cplusplus.NewDeleteLeaks)
178 [callback = std::move(callback), image = canvas_image->image(),
179 image_format, ui_task_runner = task_runners.GetUITaskRunner(),
180 raster_task_runner = task_runners.GetRasterTaskRunner(),
181 io_task_runner = task_runners.GetIOTaskRunner(),
182 io_manager = UIDartState::Current()->GetIOManager(),
183 snapshot_delegate = UIDartState::Current()->GetSnapshotDelegate(),
184 is_impeller_enabled =
186 EncodeImageAndInvokeDataCallback(
187 image, std::move(callback), image_format, ui_task_runner,
188 raster_task_runner, io_task_runner,
189 io_manager->GetResourceContext(), snapshot_delegate,
190 io_manager->GetIsGpuDisabledSyncSwitch(),
191 io_manager->GetImpellerContext(), is_impeller_enabled);
192 }));
193
194 return Dart_Null();
195}
196
199 TRACE_EVENT0("flutter", __FUNCTION__);
200
201 if (!raster_image) {
202 return nullptr;
203 }
204
205 switch (format) {
206 case kPNG: {
207 auto png_image = SkPngEncoder::Encode(nullptr, raster_image.get(), {});
208
209 if (png_image == nullptr) {
210 FML_LOG(ERROR) << "Could not convert raster image to PNG.";
211 return nullptr;
212 };
213 return png_image;
214 }
215 case kRawRGBA:
216 return CopyImageByteData(raster_image, kRGBA_8888_SkColorType,
218
219 case kRawStraightRGBA:
220 return CopyImageByteData(raster_image, kRGBA_8888_SkColorType,
222
223 case kRawUnmodified:
224 return CopyImageByteData(raster_image, raster_image->colorType(),
225 raster_image->alphaType());
227 return CopyImageByteData(raster_image, kRGBA_F32_SkColorType,
229 }
230
231 FML_LOG(ERROR) << "Unknown error encoding image.";
232 return nullptr;
233}
234
235} // 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
@ 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
static sk_sp< SkData > MakeWithCopy(const void *data, size_t length)
Definition SkData.cpp:111
SkColorType colorType() const
Definition SkPixmap.h:173
size_t computeByteSize() const
Definition SkPixmap.h:231
const void * addr() const
Definition SkPixmap.h:153
SkAlphaType alphaType() const
Definition SkPixmap.h:175
sk_sp< DlImage > image() const
Definition image.h:42
static void ConvertImageToRaster(const sk_sp< DlImage > &dl_image, std::function< void(fml::StatusOr< sk_sp< SkImage > >)> encode_task, const fml::RefPtr< fml::TaskRunner > &raster_task_runner, const fml::RefPtr< fml::TaskRunner > &io_task_runner, const std::shared_ptr< const fml::SyncSwitch > &is_gpu_disabled_sync_switch, const std::shared_ptr< impeller::Context > &impeller_context)
fml::RefPtr< fml::TaskRunner > GetIOTaskRunner() const
fml::WeakPtr< IOManager > GetIOManager() const
const TaskRunners & GetTaskRunners() const
bool IsImpellerEnabled() const
Whether Impeller is enabled for this application.
fml::TaskRunnerAffineWeakPtr< SnapshotDelegate > GetSnapshotDelegate() const
static UIDartState * Current()
virtual void PostTask(const fml::closure &task) override
T * get() const
Definition SkRefCnt.h:303
static DartState * Current()
Definition dart_state.cc:56
struct _Dart_Handle * Dart_Handle
Definition dart_api.h:258
DART_EXPORT Dart_Handle Dart_NewExternalTypedDataWithFinalizer(Dart_TypedData_Type type, void *data, intptr_t length, void *peer, intptr_t external_allocation_size, Dart_HandleFinalizer callback)
@ Dart_TypedData_kUint8
Definition dart_api.h:2606
DART_EXPORT Dart_Handle Dart_Null(void)
DART_EXPORT bool Dart_IsClosure(Dart_Handle object)
VkSurfaceKHR surface
Definition main.cc:49
sk_sp< SkImage > image
Definition examples.cpp:29
FlKeyEvent uint64_t FlKeyResponderAsyncCallback callback
#define FML_LOG(severity)
Definition logging.h:82
#define FML_DCHECK(condition)
Definition logging.h:103
size_t length
SK_API bool Encode(SkWStream *dst, const SkPixmap &src, const Options &options)
SK_API sk_sp< SkSurface > Raster(const SkImageInfo &imageInfo, size_t rowBytes, const SkSurfaceProps *surfaceProps)
@ kRawExtendedRgba128
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
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 Enable an endless trace buffer The default is a ring buffer This is useful when very old events need to viewed For during application launch Memory usage will continue to grow indefinitely however Start app with an specific route defined on the framework flutter assets Path to the Flutter assets directory enable service port Allow the VM service to fallback to automatic port selection if binding to a specified port fails trace Trace early application lifecycle Automatically switches to an endless trace buffer trace skia Filters out all Skia trace event categories except those that are specified in this comma separated list dump skp on shader Automatically dump the skp that triggers new shader compilations This is useful for writing custom ShaderWarmUp to reduce jank By this is not enabled to reduce the overhead purge persistent Remove all existing persistent cache This is mainly for debugging purposes such as reproducing the shader compilation jank trace to Write the timeline trace to a file at the specified path The file will be in Perfetto s proto format
Definition switches.h:203
void ConvertImageToRasterSkia(const sk_sp< DlImage > &dl_image, std::function< void(sk_sp< SkImage >)> encode_task, const fml::RefPtr< fml::TaskRunner > &raster_task_runner, const fml::RefPtr< fml::TaskRunner > &io_task_runner, const fml::WeakPtr< GrDirectContext > &resource_context, const fml::TaskRunnerAffineWeakPtr< SnapshotDelegate > &snapshot_delegate, const std::shared_ptr< const fml::SyncSwitch > &is_gpu_disabled_sync_switch)
Dart_Handle EncodeImage(CanvasImage *canvas_image, int format, Dart_Handle callback_handle)
internal::CopyableLambda< T > MakeCopyable(T lambda)
Dart_Handle ToDart(const T &object)
Dart_Handle DartInvoke(Dart_Handle closure, std::initializer_list< Dart_Handle > args)
uint32_t color_type
uint32_t alpha_type
static SkImageInfo Make(int width, int height, SkColorType ct, SkAlphaType at)
#define ERROR(message)
#define TRACE_EVENT0(category_group, name)