Flutter Engine
The Flutter Engine
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
7#include "flutter/shell/common/snapshot_controller_skia.h"
8
10#include "flutter/flow/surface.h"
11#include "flutter/fml/trace_event.h"
12#include "flutter/shell/common/snapshot_controller.h"
16
17namespace flutter {
18
19namespace {
20sk_sp<SkImage> DrawSnapshot(
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()) {
46 return raster_image;
47 }
48 }
49
50 return nullptr;
51}
52} // namespace
53
55 sk_sp<DisplayList> display_list,
56 SkISize picture_size,
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__);
67 size.width(), size.height(), 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([&] {
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) {
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 SkISize size) {
144 return DoMakeRasterSnapshot(size, [display_list](SkCanvas* canvas) {
145 DlSkCanvasAdapter(canvas).DrawDisplayList(display_list);
146 });
147}
148
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 SkISize image_size = 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
175} // namespace flutter
176
177#endif // !SLIMPELLER
static GrDirectContext * GrAsDirectContext(GrContext_Base *base)
SK_API int maxRenderTargetSize() const
void scale(SkScalar sx, SkScalar sy)
Definition: SkCanvas.cpp:1289
void drawImage(const SkImage *image, SkScalar left, SkScalar top)
Definition: SkCanvas.h:1528
static sk_sp< SkColorSpace > MakeSRGB()
sk_sp< SkImage > makeRasterImage(GrDirectContext *, CachingHint cachingHint=kDisallow_CachingHint) const
Definition: SkImage.cpp:267
SkISize dimensions() const
Definition: SkImage.h:297
SkCanvas * getCanvas()
Definition: SkSurface.cpp:82
static sk_sp< DlImage > Make(const SkImage *image)
Definition: dl_image.cc:11
Backend implementation of |DlCanvas| for |SkCanvas|.
Definition: dl_sk_canvas.h:20
void DrawDisplayList(const sk_sp< DisplayList > display_list, SkScalar opacity=SK_Scalar1) override
void CacheRuntimeStage(const std::shared_ptr< impeller::RuntimeStage > &runtime_stage) override
void MakeRasterSnapshot(sk_sp< DisplayList > display_list, SkISize picture_size, std::function< void(const sk_sp< DlImage > &)> callback) override
virtual sk_sp< SkImage > ConvertToRasterImage(sk_sp< SkImage > image) override
sk_sp< DlImage > MakeRasterSnapshotSync(sk_sp< DisplayList > display_list, SkISize size) override
Abstract Base Class that represents where we will be rendering content.
Definition: surface.h:24
virtual GrDirectContext * GetContext()=0
virtual std::unique_ptr< GLContextResult > MakeRenderContextCurrent()
Definition: surface.cc:13
VkSurfaceKHR surface
Definition: main.cc:49
FlKeyEvent uint64_t FlKeyResponderAsyncCallback callback
GAsyncResult * result
#define FML_LOG(severity)
Definition: logging.h:82
#define FML_DCHECK(condition)
Definition: logging.h:103
Dart_NativeFunction function
Definition: fuchsia.cc:51
static float max(float r, float g, float b)
Definition: hsl.cpp:49
static float min(float r, float g, float b)
Definition: hsl.cpp:48
SK_API GrDirectContext * GetContext(const SkImage *src)
sk_sp< const SkImage > image
Definition: SkRecords.h:269
SK_API sk_sp< SkSurface > Raster(const SkImageInfo &imageInfo, size_t rowBytes, const SkSurfaceProps *surfaceProps)
SK_API sk_sp< SkSurface > RenderTarget(GrRecordingContext *context, skgpu::Budgeted budgeted, const SkImageInfo &imageInfo, int sampleCount, GrSurfaceOrigin surfaceOrigin, const SkSurfaceProps *surfaceProps, bool shouldCreateWithMips=false, bool isProtected=false)
it will be possible to load the file into Perfetto s trace viewer 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
Definition: switches.h:259
Definition: SkSize.h:16
SkImageInfo makeWH(int newWidth, int newHeight) const
Definition: SkImageInfo.h:444
static SkImageInfo MakeN32Premul(int width, int height)
int width() const
Definition: SkImageInfo.h:365
int height() const
Definition: SkImageInfo.h:371
Represents the 2 code paths available when calling |SyncSwitch::Execute|.
Definition: sync_switch.h:35
#define ERROR(message)
Definition: elf_loader.cc:260
#define TRACE_EVENT0(category_group, name)
Definition: trace_event.h:131