Flutter Engine Uber Docs
Docs for the entire Flutter Engine repo.
 
Loading...
Searching...
No Matches
snapshot_controller_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
8
13#include "third_party/skia/include/core/SkColorSpace.h"
14#include "third_party/skia/include/core/SkSurface.h"
15#include "third_party/skia/include/gpu/ganesh/SkSurfaceGanesh.h"
16
17namespace flutter {
18
19namespace {
20sk_sp<SkImage> DrawSnapshot(
21 const sk_sp<SkSurface>& surface,
22 const std::function<void(SkCanvas*)>& draw_callback) {
23 if (surface == nullptr || surface->getCanvas() == nullptr) {
24 return nullptr;
25 }
26
27 draw_callback(surface->getCanvas());
28 auto dContext = GrAsDirectContext(surface->recordingContext());
29 if (dContext) {
30 dContext->flushAndSubmit();
31 }
32
33 sk_sp<SkImage> device_snapshot;
34 {
35 TRACE_EVENT0("flutter", "MakeDeviceSnapshot");
36 device_snapshot = surface->makeImageSnapshot();
37 }
38
39 if (device_snapshot == nullptr) {
40 return nullptr;
41 }
42
43 {
44 TRACE_EVENT0("flutter", "DeviceHostTransfer");
45 if (auto raster_image = device_snapshot->makeRasterImage(nullptr)) {
46 return raster_image;
47 }
48 }
49
50 return nullptr;
51}
52} // namespace
53
55 sk_sp<DisplayList> display_list,
56 DlISize picture_size,
57 std::function<void(const sk_sp<DlImage>&)> callback,
58 SnapshotPixelFormat pixel_format) {
59 callback(MakeRasterSnapshotSync(display_list, picture_size, pixel_format));
60}
61
62sk_sp<DlImage> SnapshotControllerSkia::DoMakeRasterSnapshot(
64 std::function<void(SkCanvas*)> draw_callback) {
65 TRACE_EVENT0("flutter", __FUNCTION__);
66 sk_sp<SkImage> result;
67 SkImageInfo image_info = SkImageInfo::MakeN32Premul(size.width, size.height,
68 SkColorSpace::MakeSRGB());
69
70 std::unique_ptr<Surface> pbuffer_surface;
71 Surface* snapshot_surface = nullptr;
72 auto& delegate = GetDelegate();
73 if (delegate.GetSurface() && delegate.GetSurface()->GetContext()) {
74 snapshot_surface = delegate.GetSurface().get();
75 } else if (delegate.GetSnapshotSurfaceProducer()) {
76 pbuffer_surface =
77 delegate.GetSnapshotSurfaceProducer()->CreateSnapshotSurface();
78 if (pbuffer_surface && pbuffer_surface->GetContext()) {
79 snapshot_surface = pbuffer_surface.get();
80 }
81 }
82
83 if (!snapshot_surface) {
84 // Raster surface is fine if there is no on screen surface. This might
85 // happen in case of software rendering.
86 sk_sp<SkSurface> sk_surface = SkSurfaces::Raster(image_info);
87 result = DrawSnapshot(sk_surface, draw_callback);
88 } else {
89 delegate.GetIsGpuDisabledSyncSwitch()->Execute(
91 .SetIfTrue([&] {
92 sk_sp<SkSurface> surface = SkSurfaces::Raster(image_info);
93 result = DrawSnapshot(surface, draw_callback);
94 })
95 .SetIfFalse([&] {
96 FML_DCHECK(snapshot_surface);
97 auto context_switch =
98 snapshot_surface->MakeRenderContextCurrent();
99 if (!context_switch->GetResult()) {
100 return;
101 }
102
103 GrRecordingContext* context = snapshot_surface->GetContext();
104 auto max_size = context->maxRenderTargetSize();
105 double scale_factor = std::min(
106 1.0, static_cast<double>(max_size) /
107 static_cast<double>(std::max(image_info.width(),
108 image_info.height())));
109
110 // Scale down the render target size to the max supported by the
111 // GPU if necessary. Exceeding the max would otherwise cause a
112 // null result.
113 if (scale_factor < 1.0) {
114 image_info = image_info.makeWH(
115 static_cast<double>(image_info.width()) * scale_factor,
116 static_cast<double>(image_info.height()) * scale_factor);
117 }
118
119 // When there is an on screen surface, we need a render target
120 // SkSurface because we want to access texture backed images.
121 sk_sp<SkSurface> sk_surface =
122 SkSurfaces::RenderTarget(context, // context
123 skgpu::Budgeted::kNo, // budgeted
124 image_info // image info
125 );
126 if (!sk_surface) {
127 FML_LOG(ERROR)
128 << "DoMakeRasterSnapshot can not create GPU render target";
129 return;
130 }
131
132 sk_surface->getCanvas()->scale(scale_factor, scale_factor);
133 result = DrawSnapshot(sk_surface, draw_callback);
134 }));
135 }
136
137 // It is up to the caller to create a DlImageGPU version of this image
138 // if the result will interact with the UI thread.
139 return DlImage::Make(result);
140}
141
143 sk_sp<DisplayList> display_list,
145 SnapshotPixelFormat pixel_format) {
146 return DoMakeRasterSnapshot(size, [display_list](SkCanvas* canvas) {
147 DlSkCanvasAdapter(canvas).DrawDisplayList(display_list);
148 });
149}
150
152 sk_sp<SkImage> image,
153 SnapshotPixelFormat pixel_format) {
154 return DlImage::Make(image);
155}
156
158 sk_sp<SkImage> image) {
159 // If the rasterizer does not have a surface with a GrContext, then it will
160 // be unable to render a cross-context SkImage. The caller will need to
161 // create the raster image on the IO thread.
162 if (GetDelegate().GetSurface() == nullptr ||
163 GetDelegate().GetSurface()->GetContext() == nullptr) {
164 return nullptr;
165 }
166
167 if (image == nullptr) {
168 return nullptr;
169 }
170
171 DlISize image_size = ToDlISize(image->dimensions());
172
173 auto result = DoMakeRasterSnapshot(
174 image_size, [image = std::move(image)](SkCanvas* canvas) {
175 canvas->drawImage(image, 0, 0);
176 });
177 return result->skia_image();
178}
179
181 const std::shared_ptr<impeller::RuntimeStage>& runtime_stage) {}
182
187
188} // namespace flutter
189
190#endif // !SLIMPELLER
static sk_sp< DlImage > Make(const SkImage *image)
Definition dl_image.cc:11
Backend implementation of |DlCanvas| for |SkCanvas|.
void DrawDisplayList(const sk_sp< DisplayList > display_list, DlScalar opacity=SK_Scalar1) override
sk_sp< DlImage > MakeTextureImage(sk_sp< SkImage > image, SnapshotPixelFormat pixel_format) override
void CacheRuntimeStage(const std::shared_ptr< impeller::RuntimeStage > &runtime_stage) override
sk_sp< DlImage > MakeRasterSnapshotSync(sk_sp< DisplayList > display_list, DlISize size, SnapshotPixelFormat pixel_format) override
virtual sk_sp< SkImage > ConvertToRasterImage(sk_sp< SkImage > image) override
void MakeRasterSnapshot(sk_sp< DisplayList > display_list, DlISize picture_size, std::function< void(const sk_sp< DlImage > &)> callback, SnapshotPixelFormat pixel_format) override
Abstract Base Class that represents where we will be rendering content.
Definition surface.h:26
virtual std::unique_ptr< GLContextResult > MakeRenderContextCurrent()
Definition surface.cc:13
virtual GrDirectContext * GetContext()=0
FlutterVulkanImage * image
VkSurfaceKHR surface
Definition main.cc:65
FlutterDesktopBinaryReply callback
#define FML_LOG(severity)
Definition logging.h:101
#define FML_UNREACHABLE()
Definition logging.h:128
#define FML_DCHECK(condition)
Definition logging.h:122
const DlISize & ToDlISize(const SkISize &size)
it will be possible to load the file into Perfetto s trace viewer use test Running tests that layout and measure text will not yield consistent results across various platforms Enabling this option will make font resolution default to the Ahem test font on all disable asset Prevents usage of any non test fonts unless they were explicitly Loaded via prefetched default font Indicates whether the embedding started a prefetch of the default font manager before creating the engine run In non interactive keep the shell running after the Dart script has completed enable serial On low power devices with low core running concurrent GC tasks on threads can cause them to contend with the UI thread which could potentially lead to jank This option turns off all concurrent GC activities domain network JSON encoded network policy per domain This overrides the DisallowInsecureConnections switch Embedder can specify whether to allow or disallow insecure connections at a domain level old gen heap size
Represents the 2 code paths available when calling |SyncSwitchExecute|.
Definition sync_switch.h:35
#define TRACE_EVENT0(category_group, name)