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
7
8#include <memory>
9#include <utility>
10
17#include "fml/status.h"
18#if IMPELLER_SUPPORTS_RENDERING
20#endif // IMPELLER_SUPPORTS_RENDERING
22#include "third_party/skia/include/core/SkImage.h"
23#include "third_party/skia/include/core/SkSurface.h"
24#include "third_party/skia/include/encode/SkPngEncoder.h"
28
31using tonic::ToDart;
32
33namespace impeller {
34class Context;
35} // namespace impeller
36namespace flutter {
37namespace {
38
39void FinalizeSkData(void* isolate_callback_data, void* peer) {
40 SkData* buffer = reinterpret_cast<SkData*>(peer);
41 buffer->unref();
42}
43
44void InvokeDataCallback(std::unique_ptr<DartPersistentValue> callback,
45 fml::StatusOr<sk_sp<SkData>>&& buffer) {
46 std::shared_ptr<tonic::DartState> dart_state = callback->dart_state().lock();
47 if (!dart_state) {
48 return;
49 }
50 tonic::DartState::Scope scope(dart_state);
51 if (!buffer.ok()) {
52 std::string error_copy(buffer.status().message());
53 Dart_Handle dart_string = ToDart(error_copy);
54 DartInvoke(callback->value(), {Dart_Null(), dart_string});
55 return;
56 }
57 // Skia will not modify the buffer, and it is backed by memory that is
58 // read/write, so Dart can be given direct access to the buffer through an
59 // external Uint8List.
60 void* bytes = const_cast<void*>(buffer.value()->data());
61 const intptr_t length = buffer.value()->size();
62 void* peer = reinterpret_cast<void*>(buffer.value().release());
63 Dart_Handle dart_data = Dart_NewExternalTypedDataWithFinalizer(
64 Dart_TypedData_kUint8, bytes, length, peer, length, FinalizeSkData);
65 DartInvoke(callback->value(), {dart_data, Dart_Null()});
66}
67
68fml::StatusOr<sk_sp<SkData>> CopyImageByteData(
69 const sk_sp<SkImage>& raster_image,
70 SkColorType color_type,
71 SkAlphaType alpha_type) {
72 FML_DCHECK(raster_image);
73
74 SkPixmap pixmap;
75
76 if (!raster_image->peekPixels(&pixmap)) {
78 "Could not copy pixels from the raster image.");
79 }
80
81 // The color types already match. No need to swizzle. Return early.
82 if (pixmap.colorType() == color_type && pixmap.alphaType() == alpha_type) {
83 return SkData::MakeWithCopy(pixmap.addr(), pixmap.computeByteSize());
84 }
85
86 // Perform swizzle if the type doesnt match the specification.
87 auto surface = SkSurfaces::Raster(
88 SkImageInfo::Make(raster_image->width(), raster_image->height(),
89 color_type, alpha_type, nullptr));
90
91 if (!surface) {
93 "Could not set up the surface for swizzle.");
94 }
95
96 surface->writePixels(pixmap, 0, 0);
97
98 if (!surface->peekPixels(&pixmap)) {
100 "Pixel address is not available.");
101 }
102
103 return SkData::MakeWithCopy(pixmap.addr(), pixmap.computeByteSize());
104}
105
106void EncodeImageAndInvokeDataCallback(
107 const sk_sp<DlImage>& image,
108 std::unique_ptr<DartPersistentValue> callback,
109 ImageByteFormat format,
110 const fml::RefPtr<fml::TaskRunner>& ui_task_runner,
111 const fml::RefPtr<fml::TaskRunner>& raster_task_runner,
112 const fml::RefPtr<fml::TaskRunner>& io_task_runner,
113 const fml::WeakPtr<GrDirectContext>& resource_context,
114 const fml::TaskRunnerAffineWeakPtr<SnapshotDelegate>& snapshot_delegate,
115 const std::shared_ptr<const fml::SyncSwitch>& is_gpu_disabled_sync_switch,
116 const std::shared_ptr<impeller::Context>& impeller_context,
117 bool is_impeller_enabled) {
118 auto callback_task =
119 fml::MakeCopyable([callback = std::move(callback)](
120 fml::StatusOr<sk_sp<SkData>>&& encoded) mutable {
121 InvokeDataCallback(std::move(callback), std::move(encoded));
122 });
123 // The static leak checker gets confused by the use of fml::MakeCopyable in
124 // EncodeImage.
125 // NOLINTNEXTLINE(clang-analyzer-cplusplus.NewDeleteLeaks)
126 auto encode_task =
127 [callback_task = std::move(callback_task), format,
128 ui_task_runner](const fml::StatusOr<sk_sp<SkImage>>& raster_image) {
129 if (raster_image.ok()) {
131 EncodeImage(raster_image.value(), format);
132 ui_task_runner->PostTask([callback_task = callback_task,
133 encoded = std::move(encoded)]() mutable {
134 callback_task(std::move(encoded));
135 });
136 } else {
137 ui_task_runner->PostTask([callback_task = callback_task,
138 raster_image = raster_image]() mutable {
139 callback_task(raster_image.status());
140 });
141 }
142 };
143
145#if IMPELLER_SUPPORTS_RENDERING
146 if (is_impeller_enabled) {
148 image, encode_task, raster_task_runner, io_task_runner,
149 snapshot_delegate, is_gpu_disabled_sync_switch, impeller_context);
150 return;
151 }
152#endif // IMPELLER_SUPPORTS_RENDERING
153#if !SLIMPELLER
154 ConvertImageToRasterSkia(image, encode_task, raster_task_runner,
155 io_task_runner, resource_context, snapshot_delegate,
156 is_gpu_disabled_sync_switch);
157
158#else // !SLIMPELLER
159 FML_LOG(FATAL) << "Unsupported renderer.";
160#endif // !SLIMPELLER
161}
162
163} // namespace
164
165Dart_Handle EncodeImage(CanvasImage* canvas_image,
166 int format,
167 Dart_Handle callback_handle) {
168 if (!canvas_image) {
169 return ToDart("encode called with non-genuine Image.");
170 }
171
172 if (!Dart_IsClosure(callback_handle)) {
173 return ToDart("Callback must be a function.");
174 }
175
176 ImageByteFormat image_format = static_cast<ImageByteFormat>(format);
177
178 auto callback = std::make_unique<DartPersistentValue>(
179 tonic::DartState::Current(), callback_handle);
180
181#if IMPELLER_SUPPORTS_RENDERING && FML_OS_IOS_SIMULATOR
182 if (canvas_image->image()->IsFakeImage()) {
183 sk_sp<SkData> data = SkData::MakeEmpty();
184 InvokeDataCallback(std::move(callback), data);
185 return Dart_Null();
186 }
187#endif // IMPELLER_SUPPORTS_RENDERING && FML_OS_IOS_SIMULATOR
188
189 const auto& task_runners = UIDartState::Current()->GetTaskRunners();
190
191 // The static leak checker gets confused by the use of fml::MakeCopyable.
192 // NOLINTNEXTLINE(clang-analyzer-cplusplus.NewDeleteLeaks)
194 [callback = std::move(callback), image = canvas_image->image(),
195 image_format, ui_task_runner = task_runners.GetUITaskRunner(),
196 raster_task_runner = task_runners.GetRasterTaskRunner(),
197 io_task_runner = task_runners.GetIOTaskRunner(),
198 io_manager = UIDartState::Current()->GetIOManager(),
199 snapshot_delegate = UIDartState::Current()->GetSnapshotDelegate(),
200 is_impeller_enabled =
202 EncodeImageAndInvokeDataCallback(
203 image, std::move(callback), image_format, ui_task_runner,
204 raster_task_runner, io_task_runner,
205 io_manager->GetResourceContext(), snapshot_delegate,
206 io_manager->GetIsGpuDisabledSyncSwitch(),
207 io_manager->GetImpellerContext(), is_impeller_enabled);
208 }));
209
210 return Dart_Null();
211}
212
213fml::StatusOr<sk_sp<SkData>> EncodeImage(const sk_sp<SkImage>& raster_image,
214 ImageByteFormat format) {
215 TRACE_EVENT0("flutter", __FUNCTION__);
216
217 if (!raster_image) {
218 return fml::Status(fml::StatusCode::kInternal, "Missing raster image.");
219 }
220
221 switch (format) {
222 case kPNG: {
223 auto png_image = SkPngEncoder::Encode(nullptr, raster_image.get(), {});
224
225 if (png_image == nullptr) {
227 "Could not convert raster image to PNG.");
228 };
229 return png_image;
230 }
231 case kRawRGBA:
232 return CopyImageByteData(raster_image, kRGBA_8888_SkColorType,
233 kPremul_SkAlphaType);
234
235 case kRawStraightRGBA:
236 return CopyImageByteData(raster_image, kRGBA_8888_SkColorType,
237 kUnpremul_SkAlphaType);
238
239 case kRawUnmodified:
240 return CopyImageByteData(raster_image, raster_image->colorType(),
241 raster_image->alphaType());
243 return CopyImageByteData(raster_image, kRGBA_F32_SkColorType,
244 kUnpremul_SkAlphaType);
245 }
246
248 "Unknown error encoding image.");
249}
250
251} // namespace flutter
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 fml::TaskRunnerAffineWeakPtr< SnapshotDelegate > &snapshot_delegate, 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
static DartState * Current()
Definition dart_state.cc:56
FlutterVulkanImage * image
VkSurfaceKHR surface
Definition main.cc:65
uint32_t uint32_t * format
FlutterDesktopBinaryReply callback
#define FML_LOG(severity)
Definition logging.h:101
#define FML_DCHECK(condition)
Definition logging.h:122
size_t length
@ kRawExtendedRgba128
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
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)
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
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
#define TRACE_EVENT0(category_group, name)