Flutter Engine
 
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 callback(MakeRasterSnapshotSync(display_list, picture_size));
59}
60
61sk_sp<DlImage> SnapshotControllerSkia::DoMakeRasterSnapshot(
63 std::function<void(SkCanvas*)> draw_callback) {
64 TRACE_EVENT0("flutter", __FUNCTION__);
65 sk_sp<SkImage> result;
66 SkImageInfo image_info = SkImageInfo::MakeN32Premul(size.width, size.height,
67 SkColorSpace::MakeSRGB());
68
69 std::unique_ptr<Surface> pbuffer_surface;
70 Surface* snapshot_surface = nullptr;
71 auto& delegate = GetDelegate();
72 if (delegate.GetSurface() && delegate.GetSurface()->GetContext()) {
73 snapshot_surface = delegate.GetSurface().get();
74 } else if (delegate.GetSnapshotSurfaceProducer()) {
75 pbuffer_surface =
76 delegate.GetSnapshotSurfaceProducer()->CreateSnapshotSurface();
77 if (pbuffer_surface && pbuffer_surface->GetContext()) {
78 snapshot_surface = pbuffer_surface.get();
79 }
80 }
81
82 if (!snapshot_surface) {
83 // Raster surface is fine if there is no on screen surface. This might
84 // happen in case of software rendering.
85 sk_sp<SkSurface> sk_surface = SkSurfaces::Raster(image_info);
86 result = DrawSnapshot(sk_surface, draw_callback);
87 } else {
88 delegate.GetIsGpuDisabledSyncSwitch()->Execute(
90 .SetIfTrue([&] {
91 sk_sp<SkSurface> surface = SkSurfaces::Raster(image_info);
92 result = DrawSnapshot(surface, draw_callback);
93 })
94 .SetIfFalse([&] {
95 FML_DCHECK(snapshot_surface);
96 auto context_switch =
97 snapshot_surface->MakeRenderContextCurrent();
98 if (!context_switch->GetResult()) {
99 return;
100 }
101
102 GrRecordingContext* context = snapshot_surface->GetContext();
103 auto max_size = context->maxRenderTargetSize();
104 double scale_factor = std::min(
105 1.0, static_cast<double>(max_size) /
106 static_cast<double>(std::max(image_info.width(),
107 image_info.height())));
108
109 // Scale down the render target size to the max supported by the
110 // GPU if necessary. Exceeding the max would otherwise cause a
111 // null result.
112 if (scale_factor < 1.0) {
113 image_info = image_info.makeWH(
114 static_cast<double>(image_info.width()) * scale_factor,
115 static_cast<double>(image_info.height()) * scale_factor);
116 }
117
118 // When there is an on screen surface, we need a render target
119 // SkSurface because we want to access texture backed images.
120 sk_sp<SkSurface> sk_surface =
121 SkSurfaces::RenderTarget(context, // context
122 skgpu::Budgeted::kNo, // budgeted
123 image_info // image info
124 );
125 if (!sk_surface) {
126 FML_LOG(ERROR)
127 << "DoMakeRasterSnapshot can not create GPU render target";
128 return;
129 }
130
131 sk_surface->getCanvas()->scale(scale_factor, scale_factor);
132 result = DrawSnapshot(sk_surface, draw_callback);
133 }));
134 }
135
136 // It is up to the caller to create a DlImageGPU version of this image
137 // if the result will interact with the UI thread.
138 return DlImage::Make(result);
139}
140
142 sk_sp<DisplayList> display_list,
143 DlISize size) {
144 return DoMakeRasterSnapshot(size, [display_list](SkCanvas* canvas) {
145 DlSkCanvasAdapter(canvas).DrawDisplayList(display_list);
146 });
147}
148
150 sk_sp<SkImage> image) {
151 // If the rasterizer does not have a surface with a GrContext, then it will
152 // be unable to render a cross-context SkImage. The caller will need to
153 // create the raster image on the IO thread.
154 if (GetDelegate().GetSurface() == nullptr ||
155 GetDelegate().GetSurface()->GetContext() == nullptr) {
156 return nullptr;
157 }
158
159 if (image == nullptr) {
160 return nullptr;
161 }
162
163 DlISize image_size = ToDlISize(image->dimensions());
164
165 auto result = DoMakeRasterSnapshot(
166 image_size, [image = std::move(image)](SkCanvas* canvas) {
167 canvas->drawImage(image, 0, 0);
168 });
169 return result->skia_image();
170}
171
173 const std::shared_ptr<impeller::RuntimeStage>& runtime_stage) {}
174
179
180} // namespace flutter
181
182#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
void CacheRuntimeStage(const std::shared_ptr< impeller::RuntimeStage > &runtime_stage) 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) override
sk_sp< DlImage > MakeRasterSnapshotSync(sk_sp< DisplayList > display_list, DlISize size) 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)