Flutter Engine
The Flutter Engine
embedder_external_view.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/platform/embedder/embedder_external_view.h"
6
7#include "flutter/display_list/dl_builder.h"
8#include "flutter/fml/trace_event.h"
9#include "flutter/shell/common/dl_op_spy.h"
10
11#ifdef IMPELLER_SUPPORTS_RENDERING
12#include "impeller/display_list/dl_dispatcher.h" // nogncheck
13#define ENABLE_EXPERIMENTAL_CANVAS false
14#endif // IMPELLER_SUPPORTS_RENDERING
15
16namespace flutter {
17
19 const SkMatrix& transformation) {
20 const auto source_rect = SkRect::MakeWH(size.width(), size.height());
21 const auto transformed_rect = transformation.mapRect(source_rect);
22 return SkISize::Make(transformed_rect.width(), transformed_rect.height());
23}
24
26 const SkISize& frame_size,
27 const SkMatrix& surface_transformation)
28 : EmbedderExternalView(frame_size, surface_transformation, {}, nullptr) {}
29
31 const SkISize& frame_size,
32 const SkMatrix& surface_transformation,
34 std::unique_ptr<EmbeddedViewParams> params)
35 : render_surface_size_(
36 TransformedSurfaceSize(frame_size, surface_transformation)),
37 surface_transformation_(surface_transformation),
38 view_identifier_(view_identifier),
39 embedded_view_params_(std::move(params)),
40 slice_(std::make_unique<DisplayListEmbedderViewSlice>(
41 SkRect::Make(frame_size))) {}
42
44
47 return RenderTargetDescriptor(render_surface_size_);
48}
49
51 return slice_->canvas();
52}
53
55 return render_surface_size_;
56}
57
59 return !HasPlatformView();
60}
61
63 return view_identifier_.platform_view_id.has_value();
64}
65
67 return slice_->getRegion();
68}
69
71 if (has_engine_rendered_contents_.has_value()) {
72 return has_engine_rendered_contents_.value();
73 }
74 TryEndRecording();
75 DlOpSpy dl_op_spy;
76 slice_->dispatch(dl_op_spy);
77 has_engine_rendered_contents_ = dl_op_spy.did_draw() && !slice_->is_empty();
78 // NOLINTNEXTLINE(bugprone-unchecked-optional-access)
79 return has_engine_rendered_contents_.value();
80}
81
83 const {
84 return view_identifier_;
85}
86
88 return embedded_view_params_.get();
89}
90
92 bool clear_surface) {
93 TRACE_EVENT0("flutter", "EmbedderExternalView::Render");
94 TryEndRecording();
96 << "Unnecessarily asked to render into a render target when there was "
97 "nothing to render.";
98
99#ifdef IMPELLER_SUPPORTS_RENDERING
100 auto* impeller_target = render_target.GetImpellerRenderTarget();
101 if (impeller_target) {
102 auto aiks_context = render_target.GetAiksContext();
103
104 auto dl_builder = DisplayListBuilder();
105 dl_builder.SetTransform(&surface_transformation_);
106 slice_->render_into(&dl_builder);
107 auto display_list = dl_builder.Build();
108
109#if ENABLE_EXPERIMENTAL_CANVAS
110 auto cull_rect =
111 impeller::IRect::MakeSize(impeller_target->GetRenderTargetSize());
112 SkIRect sk_cull_rect =
113 SkIRect::MakeWH(cull_rect.GetWidth(), cull_rect.GetHeight());
114
115 impeller::TextFrameDispatcher collector(aiks_context->GetContentContext(),
117
118 impeller::ExperimentalDlDispatcher impeller_dispatcher(
119 aiks_context->GetContentContext(), *impeller_target,
120 display_list->root_has_backdrop_filter(),
121 display_list->max_root_blend_mode(), cull_rect);
122 display_list->Dispatch(impeller_dispatcher, sk_cull_rect);
123 impeller_dispatcher.FinishRecording();
124 aiks_context->GetContentContext().GetTransientsBuffer().Reset();
125 aiks_context->GetContentContext().GetLazyGlyphAtlas()->ResetTextFrames();
126
127 return true;
128#else
129 auto dispatcher = impeller::DlDispatcher();
130 dispatcher.drawDisplayList(display_list, 1);
131 return aiks_context->Render(dispatcher.EndRecordingAsPicture(),
132 *impeller_target, /*reset_host_buffer=*/true);
133#endif
134 }
135#endif // IMPELLER_SUPPORTS_RENDERING
136
137#if SLIMPELLER
138 FML_LOG(FATAL) << "Impeller opt-out unavailable.";
139 return false;
140#else // SLIMPELLER
141 auto skia_surface = render_target.GetSkiaSurface();
142 if (!skia_surface) {
143 return false;
144 }
145
146 FML_DCHECK(render_target.GetRenderTargetSize() == render_surface_size_);
147
148 auto canvas = skia_surface->getCanvas();
149 if (!canvas) {
150 return false;
151 }
152 DlSkCanvasAdapter dl_canvas(canvas);
153 int restore_count = dl_canvas.GetSaveCount();
154 dl_canvas.SetTransform(surface_transformation_);
155 if (clear_surface) {
156 dl_canvas.Clear(DlColor::kTransparent());
157 }
158 slice_->render_into(&dl_canvas);
159 dl_canvas.RestoreToCount(restore_count);
160 dl_canvas.Flush();
161#endif // !SLIMPELLER
162
163 return true;
164}
165
166void EmbedderExternalView::TryEndRecording() const {
167 if (slice_->recording_ended()) {
168 return;
169 }
170 slice_->end_recording();
171}
172
173} // namespace flutter
bool mapRect(SkRect *dst, const SkRect &src, SkApplyPerspectiveClip pc=SkApplyPerspectiveClip::kYes) const
Definition: SkMatrix.cpp:1141
Developer-facing API for rendering anything within the engine.
Definition: dl_canvas.h:38
void Clear(DlColor color)
Definition: dl_canvas.h:132
bool did_draw()
Returns true if any non transparent content has been drawn.
Definition: dl_op_spy.cc:9
Backend implementation of |DlCanvas| for |SkCanvas|.
Definition: dl_sk_canvas.h:20
int GetSaveCount() const override
Definition: dl_sk_canvas.cc:69
void SetTransform(const SkMatrix *matrix) override
void RestoreToCount(int restore_count) override
Definition: dl_sk_canvas.cc:73
const EmbeddedViewParams * GetEmbeddedViewParams() const
RenderTargetDescriptor CreateRenderTargetDescriptor() const
bool Render(const EmbedderRenderTarget &render_target, bool clear_surface=true)
const DlRegion & GetDlRegion() const
ViewIdentifier GetViewIdentifier() const
EmbedderExternalView(const SkISize &frame_size, const SkMatrix &surface_transformation)
Describes a surface whose backing store is managed by the embedder. The type of surface depends on th...
virtual impeller::RenderTarget * GetImpellerRenderTarget() const =0
An impeller render target the rasterizer can use to draw into the backing store.
virtual sk_sp< SkSurface > GetSkiaSurface() const =0
A render surface the rasterizer can use to draw into the backing store of this render target.
virtual std::shared_ptr< impeller::AiksContext > GetAiksContext() const =0
Returns the AiksContext that should be used for rendering, if this render target is backed by Impelle...
virtual SkISize GetRenderTargetSize() const =0
Returns the size of the render target.
Performs a first pass over the display list to collect all text frames.
EmbedderExternalView::ViewIdentifier view_identifier
const EmbeddedViewParams * params
#define FATAL(error)
#define FML_LOG(severity)
Definition: logging.h:82
#define FML_DCHECK(condition)
Definition: logging.h:103
SK_API sk_sp< SkDocument > Make(SkWStream *dst, const SkSerialProcs *=nullptr, std::function< void(const SkPicture *)> onEndPage=nullptr)
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
static SkISize TransformedSurfaceSize(const SkISize &size, const SkMatrix &transformation)
Definition: ref_ptr.h:256
Definition: SkRect.h:32
static constexpr SkIRect MakeWH(int32_t w, int32_t h)
Definition: SkRect.h:56
Definition: SkSize.h:16
static constexpr SkISize Make(int32_t w, int32_t h)
Definition: SkSize.h:20
static constexpr SkRect MakeWH(float w, float h)
Definition: SkRect.h:609
static constexpr DlColor kTransparent()
Definition: dl_color.h:21
std::optional< PlatformViewID > platform_view_id
A 4x4 matrix using column-major storage.
Definition: matrix.h:37
static constexpr TRect MakeSize(const TSize< U > &size)
Definition: rect.h:146
#define TRACE_EVENT0(category_group, name)
Definition: trace_event.h:131