Flutter Engine Uber Docs
Docs for the entire Flutter Engine repo.
 
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
22std::shared_ptr<impeller::Texture> DoMakeRasterSnapshot(
23 const sk_sp<DisplayList>& display_list,
25 const std::shared_ptr<impeller::AiksContext>& context,
26 SnapshotPixelFormat pixel_format) {
27 TRACE_EVENT0("flutter", __FUNCTION__);
28 if (!context) {
29 return nullptr;
30 }
31 // Determine render target size.
32 auto max_size = context->GetContext()
33 ->GetResourceAllocator()
34 ->GetMaxTextureSizeSupported();
35 double scale_factor_x =
36 static_cast<double>(max_size.width) / static_cast<double>(size.width);
37 double scale_factor_y =
38 static_cast<double>(max_size.height) / static_cast<double>(size.height);
39 double scale_factor = std::min({1.0, scale_factor_x, scale_factor_y});
40
41 auto render_target_size = impeller::ISize(size.width, size.height);
42
43 // Scale down the render target size to the max supported by the
44 // GPU if necessary. Exceeding the max would otherwise cause a
45 // null result.
46 if (scale_factor < 1.0) {
47 render_target_size.width *= scale_factor;
48 render_target_size.height *= scale_factor;
49 }
50
51 std::optional<impeller::PixelFormat> impeller_pixel_format;
52 switch (pixel_format) {
54 impeller_pixel_format = std::nullopt;
55 break;
57 impeller_pixel_format = impeller::PixelFormat::kR32G32B32A32Float;
58 break;
60 impeller_pixel_format = impeller::PixelFormat::kR32Float;
61 break;
62 }
63
65 display_list, render_target_size, *context,
66 /*reset_host_buffer=*/false,
67 /*generate_mips=*/true, impeller_pixel_format);
68}
69
70std::shared_ptr<impeller::Texture> DoMakeRasterSnapshot(
71 const sk_sp<DisplayList>& display_list,
73 const SnapshotController::Delegate& delegate,
74 SnapshotPixelFormat pixel_format) {
75 // Ensure that the current thread has a rendering context. This must be done
76 // before calling GetAiksContext because constructing the AiksContext may
77 // invoke graphics APIs.
78 std::unique_ptr<Surface> pbuffer_surface;
79 std::unique_ptr<GLContextResult> context_result;
80 if (delegate.GetSurface()) {
81 context_result = delegate.GetSurface()->MakeRenderContextCurrent();
82 } else if (delegate.GetSnapshotSurfaceProducer()) {
83 pbuffer_surface =
84 delegate.GetSnapshotSurfaceProducer()->CreateSnapshotSurface();
85 if (pbuffer_surface) {
86 context_result = pbuffer_surface->MakeRenderContextCurrent();
87 }
88 }
89
90 return DoMakeRasterSnapshot(display_list, size, delegate.GetAiksContext(),
91 pixel_format);
92}
93
94std::shared_ptr<impeller::Texture> DoMakeRasterSnapshot(
95 sk_sp<DisplayList> display_list,
96 DlISize picture_size,
97 const std::shared_ptr<const fml::SyncSwitch>& sync_switch,
98 const std::shared_ptr<impeller::AiksContext>& context,
99 SnapshotPixelFormat pixel_format) {
100 std::shared_ptr<impeller::Texture> result;
101 sync_switch->Execute(fml::SyncSwitch::Handlers()
102 .SetIfTrue([&] {
103 // Do nothing.
104 })
105 .SetIfFalse([&] {
106 result = DoMakeRasterSnapshot(
107 display_list, picture_size, context,
108 pixel_format);
109 }));
110
111 return result;
112}
113} // namespace
114
116 sk_sp<DisplayList> display_list,
117 DlISize picture_size,
118 std::function<void(const sk_sp<SkImage>&)> callback,
119 SnapshotPixelFormat pixel_format) {
121}
122
124 sk_sp<DisplayList> display_list,
125 DlISize picture_size,
126 SnapshotPixelFormat pixel_format) {
128}
129
131 sk_sp<DisplayList> display_list,
132 DlISize picture_size,
133 std::function<void(const std::shared_ptr<impeller::Texture>&)> callback,
134 SnapshotPixelFormat pixel_format) {
135 std::shared_ptr<const fml::SyncSwitch> sync_switch =
137 sync_switch->Execute(
139 .SetIfTrue([&] {
140 std::shared_ptr<impeller::AiksContext> context =
142 if (context) {
143 context->GetContext()->StoreTaskForGPU(
144 [context, sync_switch, display_list = std::move(display_list),
145 picture_size, callback, pixel_format] {
146 callback(DoMakeRasterSnapshot(display_list, picture_size,
147 sync_switch, context,
148 pixel_format));
149 },
150 [callback]() { callback(nullptr); });
151 } else {
152 callback(nullptr);
153 }
154 })
155 .SetIfFalse([&] {
156 callback(DoMakeRasterSnapshot(display_list, picture_size,
157 GetDelegate(), pixel_format));
158 }));
159}
160
161std::shared_ptr<impeller::Texture>
163 sk_sp<DisplayList> display_list,
164 DlISize picture_size,
165 SnapshotPixelFormat pixel_format) {
166 return DoMakeRasterSnapshot(display_list, picture_size, GetDelegate(),
167 pixel_format);
168}
169
171 sk_sp<SkImage> image,
172 SnapshotPixelFormat pixel_format) {
173 return nullptr;
174}
175
176std::shared_ptr<impeller::Texture>
178 sk_sp<SkImage> image,
179 SnapshotPixelFormat pixel_format) {
180 auto aiks_context = GetDelegate().GetAiksContext();
181 if (!aiks_context) {
182 return nullptr;
183 }
184 auto context = aiks_context->GetContext();
185 if (!context) {
186 return nullptr;
187 }
188
192 desc.size = impeller::ISize(image->width(), image->height());
193 desc.mip_count = 1;
194
195 auto texture = context->GetResourceAllocator()->CreateTexture(desc);
196 if (!texture) {
197 return nullptr;
198 }
199
200 size_t byte_size = image->width() * image->height() * 4;
201 auto buffer = context->GetResourceAllocator()->CreateBuffer(
204 .size = byte_size,
205 });
206
207 if (!buffer) {
208 return nullptr;
209 }
210
211 {
212 uint8_t* map = buffer->OnGetContents();
213 if (!map) {
214 return nullptr;
215 }
216 SkImageInfo info =
217 SkImageInfo::Make(image->width(), image->height(),
218 kRGBA_8888_SkColorType, kPremul_SkAlphaType);
219 if (!image->readPixels(info, map, image->width() * 4, 0, 0)) {
220 return nullptr;
221 }
222 buffer->Flush(impeller::Range(0, byte_size));
223 }
224
225 auto command_buffer = context->CreateCommandBuffer();
226 if (!command_buffer) {
227 return nullptr;
228 }
229 auto blit_pass = command_buffer->CreateBlitPass();
230 if (!blit_pass) {
231 return nullptr;
232 }
233
234 blit_pass->AddCopy(
236 blit_pass->EncodeCommands();
237
238 if (!context->GetCommandQueue()->Submit({command_buffer}).ok()) {
239 return nullptr;
240 }
241
242 return texture;
243}
244
246 const std::shared_ptr<impeller::RuntimeStage>& runtime_stage) {
247 if (!GetDelegate().IsAiksContextInitialized()) {
248 return;
249 }
250 auto context = GetDelegate().GetAiksContext();
251 if (!context) {
252 return;
253 }
254 impeller::RuntimeEffectContents runtime_effect(nullptr);
255 runtime_effect.SetRuntimeStage(runtime_stage);
256 runtime_effect.BootstrapShader(context->GetContentContext());
257}
258
260 sk_sp<SkImage> image) {
262}
263
265 const std::unique_ptr<Surface>& surface = GetDelegate().GetSurface();
266 if (!surface) {
267 // Some backends (such as Metal) can operate without a surface and do not
268 // require MakeRenderContextCurrent.
269 return true;
270 }
271 return surface->MakeRenderContextCurrent()->GetResult();
272}
273
274} // 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
sk_sp< SkImage > MakeSkiaSnapshotSync(sk_sp< DisplayList > display_list, DlISize size, SnapshotPixelFormat pixel_format) 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
std::shared_ptr< impeller::Texture > MakeImpellerSnapshotSync(sk_sp< DisplayList > display_list, DlISize picture_size, SnapshotPixelFormat pixel_format) override
sk_sp< SkImage > ConvertToRasterImage(sk_sp< SkImage > image) 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
sk_sp< SkImage > MakeSkiaTextureImage(sk_sp< SkImage > image, SnapshotPixelFormat pixel_format) override
void CacheRuntimeStage(const std::shared_ptr< impeller::RuntimeStage > &runtime_stage) override
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
FlTexture * texture
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
DEF_SWITCHES_START aot vmservice shared library Name of the *so containing AOT compiled Dart assets for launching the service isolate vm snapshot The VM snapshot data that will be memory mapped as read only SnapshotAssetPath must be present isolate snapshot The isolate snapshot data that will be memory mapped as read only SnapshotAssetPath must be present cache dir Path to the cache directory This is different from the persistent_cache_path in embedder which is used for Skia shader cache icu native lib Path to the library file that exports the ICU data vm service The hostname IP address on which the Dart VM Service should be served If not defaults to or::depending on whether ipv6 is specified disable vm Disable the Dart VM Service The Dart VM Service is never available in release mode Bind to the IPv6 localhost address for the Dart VM Service Ignored if vm service host is set profile Make the profiler discard new samples once the profiler sample buffer is full When this flag is not the profiler sample buffer is used as a ring buffer
Definition switch_defs.h:98
std::shared_ptr< Texture > DisplayListToTexture(const sk_sp< flutter::DisplayList > &display_list, ISize size, AiksContext &context, bool reset_host_buffer, bool generate_mips, std::optional< PixelFormat > target_pixel_format)
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
A lightweight object that describes the attributes of a texture that can then used an allocator to cr...
#define TRACE_EVENT0(category_group, name)