5#include "flutter/lib/ui/painting/image_encoding.h"
6#include "flutter/lib/ui/painting/image_encoding_impl.h"
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"
18#if IMPELLER_SUPPORTS_RENDERING
19#include "flutter/lib/ui/painting/image_encoding_impeller.h"
21#include "flutter/lib/ui/painting/image_encoding_skia.h"
39void FinalizeSkData(
void* isolate_callback_data,
void* peer) {
44void InvokeDataCallback(std::unique_ptr<DartPersistentValue>
callback,
46 std::shared_ptr<tonic::DartState> dart_state =
callback->dart_state().lock();
52 std::string error_copy(
buffer.status().message());
60 void* bytes =
const_cast<void*
>(
buffer.value()->data());
62 void* peer =
reinterpret_cast<void*
>(
buffer.value().release());
78 "Could not copy pixels from the raster image.");
93 "Could not set up the surface for swizzle.");
96 surface->writePixels(pixmap, 0, 0);
98 if (!
surface->peekPixels(&pixmap)) {
100 "Pixel address is not available.");
106void EncodeImageAndInvokeDataCallback(
108 std::unique_ptr<DartPersistentValue>
callback,
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) {
121 InvokeDataCallback(std::move(
callback), std::move(encoded));
127 [callback_task = std::move(callback_task),
format,
129 if (raster_image.ok()) {
132 ui_task_runner->
PostTask([callback_task = callback_task,
133 encoded = std::move(encoded)]()
mutable {
134 callback_task(std::move(encoded));
137 ui_task_runner->
PostTask([callback_task = callback_task,
138 raster_image = raster_image]()
mutable {
139 callback_task(raster_image.status());
145#if IMPELLER_SUPPORTS_RENDERING
146 if (is_impeller_enabled) {
148 image, encode_task, raster_task_runner, io_task_runner,
149 is_gpu_disabled_sync_switch, impeller_context);
155 io_task_runner, resource_context, snapshot_delegate,
156 is_gpu_disabled_sync_switch);
169 return ToDart(
"encode called with non-genuine Image.");
173 return ToDart(
"Callback must be a function.");
178 auto callback = std::make_unique<DartPersistentValue>(
187 image_format, ui_task_runner = task_runners.GetUITaskRunner(),
188 raster_task_runner = task_runners.GetRasterTaskRunner(),
189 io_task_runner = task_runners.GetIOTaskRunner(),
192 is_impeller_enabled =
194 EncodeImageAndInvokeDataCallback(
195 image, std::move(callback), image_format, ui_task_runner,
196 raster_task_runner, io_task_runner,
197 io_manager->GetResourceContext(), snapshot_delegate,
198 io_manager->GetIsGpuDisabledSyncSwitch(),
199 io_manager->GetImpellerContext(), is_impeller_enabled);
217 if (png_image ==
nullptr) {
219 "Could not convert raster image to PNG.");
232 return CopyImageByteData(raster_image, raster_image->
colorType(),
240 "Unknown error encoding image.");
@ kPremul_SkAlphaType
pixel components are premultiplied by alpha
@ kRGBA_8888_SkColorType
pixel with 8 bits for red, green, blue, alpha; in 32-bit word
@ kRGBA_F32_SkColorType
pixel using C float for red, green, blue, alpha; in 128-bit word
static sk_sp< SkData > MakeWithCopy(const void *data, size_t length)
SkAlphaType alphaType() const
bool peekPixels(SkPixmap *pixmap) const
SkColorType colorType() const
SkColorType colorType() const
size_t computeByteSize() const
const void * addr() const
SkAlphaType alphaType() const
sk_sp< DlImage > image() const
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
static DartState * Current()
struct _Dart_Handle * Dart_Handle
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_EXPORT Dart_Handle Dart_Null(void)
DART_EXPORT bool Dart_IsClosure(Dart_Handle object)
FlKeyEvent uint64_t FlKeyResponderAsyncCallback callback
#define FML_LOG(severity)
#define FML_DCHECK(condition)
SK_API bool Encode(SkWStream *dst, const SkPixmap &src, const Options &options)
sk_sp< const SkImage > image
SK_API sk_sp< SkSurface > Raster(const SkImageInfo &imageInfo, size_t rowBytes, const SkSurfaceProps *surfaceProps)
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
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
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)
static SkImageInfo Make(int width, int height, SkColorType ct, SkAlphaType at)
#define TRACE_EVENT0(category_group, name)