Flutter Engine
The Flutter Engine
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
5#include "flutter/shell/common/snapshot_controller_impeller.h"
6
7#include <algorithm>
8
9#include "flutter/flow/surface.h"
10#include "flutter/fml/trace_event.h"
11#include "flutter/impeller/display_list/dl_dispatcher.h"
12#include "flutter/impeller/display_list/dl_image_impeller.h"
13#include "flutter/impeller/geometry/size.h"
14#include "flutter/shell/common/snapshot_controller.h"
15
16namespace flutter {
17
18namespace {
19sk_sp<DlImage> DoMakeRasterSnapshot(
20 const sk_sp<DisplayList>& display_list,
22 const std::shared_ptr<impeller::AiksContext>& context) {
23 TRACE_EVENT0("flutter", __FUNCTION__);
24 impeller::DlDispatcher dispatcher;
25 display_list->Dispatch(dispatcher);
27 if (context) {
28 auto max_size = context->GetContext()
29 ->GetResourceAllocator()
30 ->GetMaxTextureSizeSupported();
31 double scale_factor_x =
32 static_cast<double>(max_size.width) / static_cast<double>(size.width());
33 double scale_factor_y = static_cast<double>(max_size.height) /
34 static_cast<double>(size.height());
35 double scale_factor =
36 std::min(1.0, std::min(scale_factor_x, scale_factor_y));
37
38 auto render_target_size = impeller::ISize(size.width(), size.height());
39
40 // Scale down the render target size to the max supported by the
41 // GPU if necessary. Exceeding the max would otherwise cause a
42 // null result.
43 if (scale_factor < 1.0) {
44 render_target_size.width *= scale_factor;
45 render_target_size.height *= scale_factor;
46 }
47
48 std::shared_ptr<impeller::Image> image =
49 picture.ToImage(*context, render_target_size);
50 if (image) {
51 return impeller::DlImageImpeller::Make(image->GetTexture(),
53 }
54 }
55
56 return nullptr;
57}
58
59sk_sp<DlImage> DoMakeRasterSnapshot(
60 sk_sp<DisplayList> display_list,
61 SkISize picture_size,
62 const std::shared_ptr<const fml::SyncSwitch>& sync_switch,
63 const std::shared_ptr<impeller::AiksContext>& context) {
65 sync_switch->Execute(fml::SyncSwitch::Handlers()
66 .SetIfTrue([&] {
67 // Do nothing.
68 })
69 .SetIfFalse([&] {
70 result = DoMakeRasterSnapshot(
71 display_list, picture_size, context);
72 }));
73
74 return result;
75}
76} // namespace
77
79 sk_sp<DisplayList> display_list,
80 SkISize picture_size,
83 std::shared_ptr<const fml::SyncSwitch> sync_switch =
85 sync_switch->Execute(
87 .SetIfTrue([&] {
88 std::shared_ptr<impeller::AiksContext> context =
90 if (context) {
91 context->GetContext()->StoreTaskForGPU(
92 [context, sync_switch, display_list = std::move(display_list),
93 picture_size, callback = std::move(callback)] {
94 callback(DoMakeRasterSnapshot(display_list, picture_size,
95 sync_switch, context));
96 });
97 } else {
98 callback(nullptr);
99 }
100 })
101 .SetIfFalse([&] {
102 callback(DoMakeRasterSnapshot(display_list, picture_size,
103 GetDelegate().GetAiksContext()));
104 }));
105}
106
108 sk_sp<DisplayList> display_list,
109 SkISize picture_size) {
110 return DoMakeRasterSnapshot(display_list, picture_size,
111 GetDelegate().GetIsGpuDisabledSyncSwitch(),
112 GetDelegate().GetAiksContext());
113}
114
116 const std::shared_ptr<impeller::RuntimeStage>& runtime_stage) {
117 impeller::RuntimeEffectContents runtime_effect;
118 runtime_effect.SetRuntimeStage(runtime_stage);
119 auto context = GetDelegate().GetAiksContext();
120 if (!context) {
121 return;
122 }
123 runtime_effect.BootstrapShader(context->GetContentContext());
124}
125
129}
130
131} // namespace flutter
sk_sp< SkImage > ConvertToRasterImage(sk_sp< SkImage > image) override
void CacheRuntimeStage(const std::shared_ptr< impeller::RuntimeStage > &runtime_stage) override
sk_sp< DlImage > MakeRasterSnapshotSync(sk_sp< DisplayList > display_list, SkISize picture_size) override
void MakeRasterSnapshot(sk_sp< DisplayList > display_list, SkISize picture_size, std::function< void(const sk_sp< DlImage > &)> callback) override
virtual std::shared_ptr< const fml::SyncSwitch > GetIsGpuDisabledSyncSwitch() const =0
virtual std::shared_ptr< impeller::AiksContext > GetAiksContext() const =0
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)
FlKeyEvent uint64_t FlKeyResponderAsyncCallback callback
GAsyncResult * result
#define FML_UNREACHABLE()
Definition: logging.h:109
Dart_NativeFunction function
Definition: fuchsia.cc:51
static float min(float r, float g, float b)
Definition: hsl.cpp:48
sk_sp< const SkImage > image
Definition: SkRecords.h:269
sk_sp< const SkPicture > picture
Definition: SkRecords.h:299
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
ISize64 ISize
Definition: size.h:140
Definition: SkSize.h:16
Represents the 2 code paths available when calling |SyncSwitch::Execute|.
Definition: sync_switch.h:35
#define TRACE_EVENT0(category_group, name)
Definition: trace_event.h:131