15#include "third_party/skia/include/core/SkBitmap.h"
16#include "third_party/skia/include/core/SkImage.h"
24 return SkColorType::kRGBA_8888_SkColorType;
26 return SkColorType::kRGBA_F16_SkColorType;
28 return SkColorType::kBGRA_8888_SkColorType;
30 return SkColorType::kBGR_101010x_XR_SkColorType;
32 return SkColorType::kBGRA_10101010_XR_SkColorType;
34 return SkColorType::kRGBA_F32_SkColorType;
40sk_sp<SkImage> ConvertBufferToSkImage(
41 const std::shared_ptr<impeller::DeviceBuffer>& buffer,
44 SkImageInfo image_info = SkImageInfo::Make(dimensions,
color_type,
45 SkAlphaType::kPremul_SkAlphaType);
47 auto func = [](
void* addr,
void*
context) {
49 static_cast<std::shared_ptr<impeller::DeviceBuffer>*
>(
context);
53 auto bytes_per_pixel = image_info.bytesPerPixel();
54 bitmap.installPixels(image_info,
buffer->OnGetContents(),
55 dimensions.width() * bytes_per_pixel, func,
56 new std::shared_ptr<impeller::DeviceBuffer>(buffer));
59 sk_sp<SkImage> raster_image = SkImages::RasterFromBitmap(
bitmap);
63[[nodiscard]]
fml::Status DoConvertImageToRasterImpeller(
64 const sk_sp<DlImage>& dl_image,
65 const std::function<
void(
fml::StatusOr<sk_sp<SkImage>>)>& encode_task,
67 const std::shared_ptr<const fml::SyncSwitch>& is_gpu_disabled_sync_switch,
68 const std::shared_ptr<impeller::Context>& impeller_context) {
70 is_gpu_disabled_sync_switch->Execute(
72 .SetIfTrue([&result] {
77 [&dl_image, &encode_task, &snapshot_delegate, &impeller_context] {
79 dl_image, encode_task, snapshot_delegate, impeller_context);
87void DoConvertImageToRasterImpellerWithRetry(
88 const sk_sp<DlImage>& dl_image,
89 std::function<
void(
fml::StatusOr<sk_sp<SkImage>>)>&& encode_task,
91 const std::shared_ptr<const fml::SyncSwitch>& is_gpu_disabled_sync_switch,
92 const std::shared_ptr<impeller::Context>& impeller_context,
94 fml::Status status = DoConvertImageToRasterImpeller(
95 dl_image, encode_task, snapshot_delegate, is_gpu_disabled_sync_switch,
101 impeller_context->StoreTaskForGPU(
102 [dl_image, encode_task, snapshot_delegate,
103 is_gpu_disabled_sync_switch, impeller_context,
104 retry_runner]()
mutable {
105 auto retry_task = [dl_image, encode_task = std::move(encode_task),
106 snapshot_delegate, is_gpu_disabled_sync_switch,
108 fml::Status retry_status = DoConvertImageToRasterImpeller(
109 dl_image, encode_task, snapshot_delegate,
110 is_gpu_disabled_sync_switch, impeller_context);
111 if (!retry_status.
ok()) {
114 encode_task(retry_status);
120 retry_runner->PostTask(retry_task);
139 const sk_sp<DlImage>& dl_image,
140 std::function<
void(
fml::StatusOr<sk_sp<SkImage>>)> encode_task,
142 const std::shared_ptr<impeller::Context>& impeller_context) {
143 auto impeller_image = dl_image->asImpellerImage();
144 if (!impeller_image) {
146 "Image is not an Impeller image."));
149 auto texture = impeller_image->GetImpellerTexture(impeller_context);
151 if (impeller_context ==
nullptr) {
153 "Impeller context was null."));
168 if (dimensions.IsEmpty()) {
170 "Image dimensions were empty."));
176 "Failed to get color type from pixel format."));
184 if (snapshot_delegate) {
185 if (!snapshot_delegate->MakeRenderContextCurrent()) {
187 "Failed to bind the render context."));
196 texture->GetTextureDescriptor().GetByteSizeOfBaseMipLevel();
198 impeller_context->GetResourceAllocator()->CreateBuffer(buffer_desc);
201 "Failed to allocate destination buffer."));
208 pass->SetLabel(
"BlitTextureToBuffer Blit Pass");
210 pass->EncodeCommands();
212 encode_task = std::move(encode_task)](
221 encode_task(sk_image);
224 if (!impeller_context->GetCommandQueue()
225 ->Submit({command_buffer}, completion)
227 FML_LOG(ERROR) <<
"Failed to submit commands.";
230 impeller_context->DisposeThreadLocalCachedResources();
234 const sk_sp<DlImage>& dl_image,
235 std::function<
void(
fml::StatusOr<sk_sp<SkImage>>)> encode_task,
239 const std::shared_ptr<const fml::SyncSwitch>& is_gpu_disabled_sync_switch,
240 const std::shared_ptr<impeller::Context>& impeller_context) {
241 auto original_encode_task = std::move(encode_task);
242 encode_task = [original_encode_task = std::move(original_encode_task),
246 [original_encode_task = std::move(original_encode_task),
251 DoConvertImageToRasterImpellerWithRetry(
252 dl_image, std::move(encode_task),
253 {}, is_gpu_disabled_sync_switch, impeller_context,
258 raster_task_runner->PostTask([dl_image, encode_task = std::move(encode_task),
259 io_task_runner, snapshot_delegate,
260 is_gpu_disabled_sync_switch, impeller_context,
261 raster_task_runner]()
mutable {
262 DoConvertImageToRasterImpellerWithRetry(
263 dl_image, std::move(encode_task), snapshot_delegate,
264 is_gpu_disabled_sync_switch, impeller_context, raster_task_runner);
269 const std::shared_ptr<impeller::Texture>&
texture) {
static int GetColorSpace(const std::shared_ptr< impeller::Texture > &texture)
static void ConvertDlImageToSkImage(const sk_sp< DlImage > &dl_image, std::function< void(fml::StatusOr< sk_sp< SkImage > >)> encode_task, const fml::TaskRunnerAffineWeakPtr< SnapshotDelegate > &snapshot_delegate, const std::shared_ptr< impeller::Context > &impeller_context)
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::StatusCode code() const
static void RunNowOrPostTask(const fml::RefPtr< fml::TaskRunner > &runner, const fml::closure &task)
FlutterVulkanImage * image
#define FML_LOG(severity)
std::shared_ptr< SkBitmap > bitmap
impeller::ISize32 DlISize
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
const SkISize & ToSkISize(const DlISize &size)
PixelFormat
The Pixel formats supported by Impeller. The naming convention denotes the usage of the component,...
std::shared_ptr< ContextGLES > context
std::shared_ptr< CommandBuffer > command_buffer
Represents the 2 code paths available when calling |SyncSwitchExecute|.
A lightweight object that describes the attributes of a texture that can then used an allocator to cr...