Flutter Engine
 
Loading...
Searching...
No Matches
snapshot_controller_impeller.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
6
7#include <algorithm>
8
17
18namespace flutter {
19
20namespace {
21
22sk_sp<DlImage> DoMakeRasterSnapshot(
23 const sk_sp<DisplayList>& display_list,
25 const std::shared_ptr<impeller::AiksContext>& context) {
26 TRACE_EVENT0("flutter", __FUNCTION__);
27 if (!context) {
28 return nullptr;
29 }
30 // Determine render target size.
31 auto max_size = context->GetContext()
32 ->GetResourceAllocator()
33 ->GetMaxTextureSizeSupported();
34 double scale_factor_x =
35 static_cast<double>(max_size.width) / static_cast<double>(size.width);
36 double scale_factor_y =
37 static_cast<double>(max_size.height) / static_cast<double>(size.height);
38 double scale_factor = std::min({1.0, scale_factor_x, scale_factor_y});
39
40 auto render_target_size = impeller::ISize(size.width, size.height);
41
42 // Scale down the render target size to the max supported by the
43 // GPU if necessary. Exceeding the max would otherwise cause a
44 // null result.
45 if (scale_factor < 1.0) {
46 render_target_size.width *= scale_factor;
47 render_target_size.height *= scale_factor;
48 }
49
51 impeller::DisplayListToTexture(display_list, render_target_size, *context,
52 /*reset_host_buffer=*/false,
53 /*generate_mips=*/true),
55}
56
57sk_sp<DlImage> DoMakeRasterSnapshot(
58 const sk_sp<DisplayList>& display_list,
60 const SnapshotController::Delegate& delegate) {
61 // Ensure that the current thread has a rendering context. This must be done
62 // before calling GetAiksContext because constructing the AiksContext may
63 // invoke graphics APIs.
64 std::unique_ptr<Surface> pbuffer_surface;
65 if (delegate.GetSurface()) {
66 delegate.GetSurface()->MakeRenderContextCurrent();
67 } else if (delegate.GetSnapshotSurfaceProducer()) {
68 pbuffer_surface =
69 delegate.GetSnapshotSurfaceProducer()->CreateSnapshotSurface();
70 if (pbuffer_surface) {
71 pbuffer_surface->MakeRenderContextCurrent();
72 }
73 }
74
75 return DoMakeRasterSnapshot(display_list, size, delegate.GetAiksContext());
76}
77
78sk_sp<DlImage> DoMakeRasterSnapshot(
79 sk_sp<DisplayList> display_list,
80 DlISize picture_size,
81 const std::shared_ptr<const fml::SyncSwitch>& sync_switch,
82 const std::shared_ptr<impeller::AiksContext>& context) {
83 sk_sp<DlImage> result;
84 sync_switch->Execute(fml::SyncSwitch::Handlers()
85 .SetIfTrue([&] {
86 // Do nothing.
87 })
88 .SetIfFalse([&] {
89 result = DoMakeRasterSnapshot(
90 display_list, picture_size, context);
91 }));
92
93 return result;
94}
95} // namespace
96
98 sk_sp<DisplayList> display_list,
99 DlISize picture_size,
100 std::function<void(const sk_sp<DlImage>&)> callback) {
101 std::shared_ptr<const fml::SyncSwitch> sync_switch =
103 sync_switch->Execute(
105 .SetIfTrue([&] {
106 std::shared_ptr<impeller::AiksContext> context =
108 if (context) {
109 context->GetContext()->StoreTaskForGPU(
110 [context, sync_switch, display_list = std::move(display_list),
111 picture_size, callback] {
112 callback(DoMakeRasterSnapshot(display_list, picture_size,
113 sync_switch, context));
114 },
115 [callback]() { callback(nullptr); });
116 } else {
117#if FML_OS_IOS_SIMULATOR
120 /*is_fake_image=*/true));
121#else
122 callback(nullptr);
123
124#endif // FML_OS_IOS_SIMULATOR
125 }
126 })
127 .SetIfFalse([&] {
128#if FML_OS_IOS_SIMULATOR
129 if (!GetDelegate().GetAiksContext()) {
132 /*is_fake_image=*/true));
133 return;
134 }
135#endif
136 callback(DoMakeRasterSnapshot(display_list, picture_size,
137 GetDelegate()));
138 }));
139}
140
142 sk_sp<DisplayList> display_list,
143 DlISize picture_size) {
144 return DoMakeRasterSnapshot(display_list, picture_size, GetDelegate());
145}
146
148 const std::shared_ptr<impeller::RuntimeStage>& runtime_stage) {
149 if (!GetDelegate().IsAiksContextInitialized()) {
150 return;
151 }
152 auto context = GetDelegate().GetAiksContext();
153 if (!context) {
154 return;
155 }
156 impeller::RuntimeEffectContents runtime_effect;
157 runtime_effect.SetRuntimeStage(runtime_stage);
158 runtime_effect.BootstrapShader(context->GetContentContext());
159}
160
162 sk_sp<SkImage> image) {
164}
165
167 const std::unique_ptr<Surface>& surface = GetDelegate().GetSurface();
168 if (!surface) {
169 // Some backends (such as Metal) can operate without a surface and do not
170 // require MakeRenderContextCurrent.
171 return true;
172 }
173 return surface->MakeRenderContextCurrent()->GetResult();
174}
175
176} // namespace flutter
virtual const std::unique_ptr< Surface > & GetSurface() const =0
virtual std::shared_ptr< const fml::SyncSwitch > GetIsGpuDisabledSyncSwitch() const =0
virtual std::shared_ptr< impeller::AiksContext > GetAiksContext() const =0
void MakeRasterSnapshot(sk_sp< DisplayList > display_list, DlISize picture_size, std::function< void(const sk_sp< DlImage > &)> callback) override
sk_sp< SkImage > ConvertToRasterImage(sk_sp< SkImage > image) override
sk_sp< DlImage > MakeRasterSnapshotSync(sk_sp< DisplayList > display_list, DlISize picture_size) override
void CacheRuntimeStage(const std::shared_ptr< impeller::RuntimeStage > &runtime_stage) override
static sk_sp< DlImageImpeller > Make(std::shared_ptr< Texture > texture, OwningContext owning_context=OwningContext::kIO)
bool BootstrapShader(const ContentContext &renderer) const
Load the runtime effect and ensure a default PSO is initialized.
void SetRuntimeStage(std::shared_ptr< RuntimeStage > runtime_stage)
FlutterVulkanImage * image
VkSurfaceKHR surface
Definition main.cc:65
FlutterDesktopBinaryReply callback
#define FML_UNREACHABLE()
Definition logging.h:128
impeller::ISize32 DlISize
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
std::shared_ptr< Texture > DisplayListToTexture(const sk_sp< flutter::DisplayList > &display_list, ISize size, AiksContext &context, bool reset_host_buffer, bool generate_mips)
Render the provided display list to a texture with the given size.
ISize64 ISize
Definition size.h:162
Represents the 2 code paths available when calling |SyncSwitchExecute|.
Definition sync_switch.h:35
#define TRACE_EVENT0(category_group, name)