Flutter Engine
The Flutter Engine
image_encoding_skia.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#if !SLIMPELLER
6
7#include "flutter/lib/ui/painting/image_encoding.h"
8#include "flutter/lib/ui/painting/image_encoding_impl.h"
9
10#include "flutter/lib/ui/painting/image.h"
11
12namespace flutter {
13
15 const sk_sp<DlImage>& dl_image,
16 std::function<void(sk_sp<SkImage>)> encode_task,
17 const fml::RefPtr<fml::TaskRunner>& raster_task_runner,
18 const fml::RefPtr<fml::TaskRunner>& io_task_runner,
19 const fml::WeakPtr<GrDirectContext>& resource_context,
21 const std::shared_ptr<const fml::SyncSwitch>& is_gpu_disabled_sync_switch) {
22 // If the owning_context is kRaster, we can't access it on this task runner.
23 if (dl_image->owning_context() != DlImage::OwningContext::kRaster) {
24 auto image = dl_image->skia_image();
25
26 // Check validity of the image.
27 if (image == nullptr) {
28 FML_LOG(ERROR) << "Image was null.";
29 encode_task(nullptr);
30 return;
31 }
32
33 auto dimensions = image->dimensions();
34
35 if (dimensions.isEmpty()) {
36 FML_LOG(ERROR) << "Image dimensions were empty.";
37 encode_task(nullptr);
38 return;
39 }
40
41 SkPixmap pixmap;
42 if (image->peekPixels(&pixmap)) {
43 // This is already a raster image.
44 encode_task(image);
45 return;
46 }
47
48 if (sk_sp<SkImage> raster_image = image->makeRasterImage()) {
49 // The image can be converted to a raster image.
50 encode_task(raster_image);
51 return;
52 }
53 }
54
55 if (!raster_task_runner) {
56 FML_LOG(ERROR) << "Raster task runner was null.";
57 encode_task(nullptr);
58 return;
59 }
60
61 if (!io_task_runner) {
62 FML_LOG(ERROR) << "IO task runner was null.";
63 encode_task(nullptr);
64 return;
65 }
66
67 // Cross-context images do not support makeRasterImage. Convert these images
68 // by drawing them into a surface. This must be done on the raster thread
69 // to prevent concurrent usage of the image on both the IO and raster threads.
70 raster_task_runner->PostTask([dl_image, encode_task = std::move(encode_task),
71 resource_context, snapshot_delegate,
72 io_task_runner, is_gpu_disabled_sync_switch,
73 raster_task_runner]() {
74 auto image = dl_image->skia_image();
75 if (!image || !snapshot_delegate) {
76 io_task_runner->PostTask(
77 [encode_task = encode_task]() mutable { encode_task(nullptr); });
78 return;
79 }
80
81 sk_sp<SkImage> raster_image =
82 snapshot_delegate->ConvertToRasterImage(image);
83
84 io_task_runner->PostTask([image, encode_task = encode_task,
85 raster_image = std::move(raster_image),
86 resource_context, is_gpu_disabled_sync_switch,
87 owning_context = dl_image->owning_context(),
88 raster_task_runner]() mutable {
89 if (!raster_image) {
90 // The rasterizer was unable to render the cross-context image
91 // (presumably because it does not have a GrContext). In that case,
92 // convert the image on the IO thread using the resource context.
94 image, resource_context, is_gpu_disabled_sync_switch);
95 }
96 encode_task(raster_image);
97 if (owning_context == DlImage::OwningContext::kRaster) {
98 raster_task_runner->PostTask([image = std::move(image)]() {});
99 }
100 });
101 });
102}
103
104} // namespace flutter
105
106#endif // !SLIMPELLER
sk_sp< SkImage > makeRasterImage(GrDirectContext *, CachingHint cachingHint=kDisallow_CachingHint) const
Definition: SkImage.cpp:267
SkISize dimensions() const
Definition: SkImage.h:297
bool peekPixels(SkPixmap *pixmap) const
Definition: SkImage.cpp:34
virtual void PostTask(const fml::closure &task) override
Definition: task_runner.cc:24
#define FML_LOG(severity)
Definition: logging.h:82
Dart_NativeFunction function
Definition: fuchsia.cc:51
sk_sp< const SkImage > image
Definition: SkRecords.h:269
sk_sp< SkImage > ConvertToRasterUsingResourceContext(const sk_sp< SkImage > &image, const fml::WeakPtr< GrDirectContext > &resource_context, const std::shared_ptr< const SyncSwitch > &is_gpu_disabled_sync_switch)
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)
#define ERROR(message)
Definition: elf_loader.cc:260