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<SkImage>&)> callback,
58 SnapshotPixelFormat pixel_format) {
59 callback(MakeSkiaSnapshotSync(display_list, picture_size, pixel_format));
60}
61
63 sk_sp<DisplayList> display_list,
65 SnapshotPixelFormat pixel_format) {
66 auto draw_callback = [&display_list](SkCanvas* canvas) {
67 DlSkCanvasAdapter adapter(canvas);
68 adapter.DrawDisplayList(display_list);
69 };
70 return DoMakeRasterSnapshot(size, draw_callback);
71}
72
74 sk_sp<DisplayList> display_list,
75 DlISize picture_size,
76 std::function<void(const std::shared_ptr<impeller::Texture>&)> callback,
77 SnapshotPixelFormat pixel_format) {
79}
80
81std::shared_ptr<impeller::Texture>
83 sk_sp<DisplayList> display_list,
85 SnapshotPixelFormat pixel_format) {
87}
88
89sk_sp<SkImage> SnapshotControllerSkia::DoMakeRasterSnapshot(
91 std::function<void(SkCanvas*)> draw_callback) {
92 TRACE_EVENT0("flutter", __FUNCTION__);
93 sk_sp<SkImage> result;
94 SkImageInfo image_info = SkImageInfo::MakeN32Premul(size.width, size.height,
95 SkColorSpace::MakeSRGB());
96
97 std::unique_ptr<Surface> pbuffer_surface;
98 Surface* snapshot_surface = nullptr;
99 auto& delegate = GetDelegate();
100 if (delegate.GetSurface() && delegate.GetSurface()->GetContext()) {
101 snapshot_surface = delegate.GetSurface().get();
102 } else if (delegate.GetSnapshotSurfaceProducer()) {
103 pbuffer_surface =
104 delegate.GetSnapshotSurfaceProducer()->CreateSnapshotSurface();
105 if (pbuffer_surface && pbuffer_surface->GetContext()) {
106 snapshot_surface = pbuffer_surface.get();
107 }
108 }
109
110 if (!snapshot_surface) {
111 // Raster surface is fine if there is no on screen surface. This might
112 // happen in case of software rendering.
113 sk_sp<SkSurface> sk_surface = SkSurfaces::Raster(image_info);
114 result = DrawSnapshot(sk_surface, draw_callback);
115 } else {
116 delegate.GetIsGpuDisabledSyncSwitch()->Execute(
118 .SetIfTrue([&] {
119 sk_sp<SkSurface> surface = SkSurfaces::Raster(image_info);
120 result = DrawSnapshot(surface, draw_callback);
121 })
122 .SetIfFalse([&] {
123 FML_DCHECK(snapshot_surface);
124 auto context_switch =
125 snapshot_surface->MakeRenderContextCurrent();
126 if (!context_switch->GetResult()) {
127 return;
128 }
129
130 GrRecordingContext* context = snapshot_surface->GetContext();
131 auto max_size = context->maxRenderTargetSize();
132 double scale_factor = std::min(
133 1.0, static_cast<double>(max_size) /
134 static_cast<double>(std::max(image_info.width(),
135 image_info.height())));
136
137 // Scale down the render target size to the max supported by the
138 // GPU if necessary. Exceeding the max would otherwise cause a
139 // null result.
140 if (scale_factor < 1.0) {
141 image_info = image_info.makeWH(
142 static_cast<double>(image_info.width()) * scale_factor,
143 static_cast<double>(image_info.height()) * scale_factor);
144 }
145
146 // When there is an on screen surface, we need a render target
147 // SkSurface because we want to access texture backed images.
148 sk_sp<SkSurface> sk_surface =
149 SkSurfaces::RenderTarget(context, // context
150 skgpu::Budgeted::kNo, // budgeted
151 image_info // image info
152 );
153 if (!sk_surface) {
154 FML_LOG(ERROR)
155 << "DoMakeRasterSnapshot can not create GPU render target";
156 return;
157 }
158
159 sk_surface->getCanvas()->scale(scale_factor, scale_factor);
160 result = DrawSnapshot(sk_surface, draw_callback);
161 }));
162 }
163
164 // It is up to the caller to create a DlImage if the result
165 // will interact with the UI thread.
166 return result;
167}
168
170 sk_sp<SkImage> image,
171 SnapshotPixelFormat pixel_format) {
172 return image;
173}
174
175std::shared_ptr<impeller::Texture>
177 sk_sp<SkImage> image,
178 SnapshotPixelFormat pixel_format) {
179 return nullptr;
180}
181
183 sk_sp<SkImage> image) {
184 // If the rasterizer does not have a surface with a GrContext, then it will
185 // be unable to render a cross-context SkImage. The caller will need to
186 // create the raster image on the IO thread.
187 if (GetDelegate().GetSurface() == nullptr ||
188 GetDelegate().GetSurface()->GetContext() == nullptr) {
189 return nullptr;
190 }
191
192 if (image == nullptr) {
193 return nullptr;
194 }
195
196 DlISize image_size = ToDlISize(image->dimensions());
197
198 auto result = DoMakeRasterSnapshot(
199 image_size, [image = std::move(image)](SkCanvas* canvas) {
200 canvas->drawImage(image, 0, 0);
201 });
202 return result;
203}
204
206 const std::shared_ptr<impeller::RuntimeStage>& runtime_stage) {}
207
211
212} // namespace flutter
213
214#endif // !SLIMPELLER
Backend implementation of |DlCanvas| for |SkCanvas|.
void DrawDisplayList(const sk_sp< DisplayList > display_list, DlScalar opacity=SK_Scalar1) override
std::shared_ptr< impeller::Texture > MakeImpellerSnapshotSync(sk_sp< DisplayList > display_list, DlISize size, SnapshotPixelFormat pixel_format) override
void CacheRuntimeStage(const std::shared_ptr< impeller::RuntimeStage > &runtime_stage) override
sk_sp< SkImage > MakeSkiaTextureImage(sk_sp< SkImage > image, SnapshotPixelFormat pixel_format) override
virtual sk_sp< SkImage > ConvertToRasterImage(sk_sp< SkImage > image) override
void MakeSkiaSnapshot(sk_sp< DisplayList > display_list, DlISize picture_size, std::function< void(const sk_sp< SkImage > &)> callback, SnapshotPixelFormat pixel_format) override
std::shared_ptr< impeller::Texture > MakeImpellerTextureImage(sk_sp< SkImage > image, SnapshotPixelFormat pixel_format) override
sk_sp< SkImage > MakeSkiaSnapshotSync(sk_sp< DisplayList > display_list, DlISize size, SnapshotPixelFormat pixel_format) override
void MakeImpellerSnapshot(sk_sp< DisplayList > display_list, DlISize picture_size, std::function< void(const std::shared_ptr< impeller::Texture > &)> 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)