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