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 "compositor_context.h"
6
7 #include "flutter/flow/layers/layer_tree.h"
8
9 namespace flutter_runner {
10
11 class ScopedFrame final : public flutter::CompositorContext::ScopedFrame {
12 public:
ScopedFrame(flutter::CompositorContext & context,const SkMatrix & root_surface_transformation,bool instrumentation_enabled,SessionConnection & session_connection)13 ScopedFrame(flutter::CompositorContext& context,
14 const SkMatrix& root_surface_transformation,
15 bool instrumentation_enabled,
16 SessionConnection& session_connection)
17 : flutter::CompositorContext::ScopedFrame(context,
18 nullptr,
19 nullptr,
20 nullptr,
21 root_surface_transformation,
22 instrumentation_enabled,
23 nullptr),
24 session_connection_(session_connection) {}
25
26 private:
27 SessionConnection& session_connection_;
28
Raster(flutter::LayerTree & layer_tree,bool ignore_raster_cache)29 flutter::RasterStatus Raster(flutter::LayerTree& layer_tree,
30 bool ignore_raster_cache) override {
31 if (!session_connection_.has_metrics()) {
32 return flutter::RasterStatus::kSuccess;
33 }
34
35 {
36 // Preroll the Flutter layer tree. This allows Flutter to perform
37 // pre-paint optimizations.
38 TRACE_EVENT0("flutter", "Preroll");
39 layer_tree.Preroll(*this, true /* ignore raster cache */);
40 }
41
42 {
43 // Traverse the Flutter layer tree so that the necessary session ops to
44 // represent the frame are enqueued in the underlying session.
45 TRACE_EVENT0("flutter", "UpdateScene");
46 layer_tree.UpdateScene(session_connection_.scene_update_context(),
47 session_connection_.root_node());
48 }
49
50 {
51 // Flush all pending session ops.
52 TRACE_EVENT0("flutter", "SessionPresent");
53 session_connection_.Present(*this);
54 }
55
56 return flutter::RasterStatus::kSuccess;
57 }
58
59 FML_DISALLOW_COPY_AND_ASSIGN(ScopedFrame);
60 };
61
CompositorContext(std::string debug_label,fuchsia::ui::views::ViewToken view_token,fidl::InterfaceHandle<fuchsia::ui::scenic::Session> session,fml::closure session_error_callback,zx_handle_t vsync_event_handle)62 CompositorContext::CompositorContext(
63 std::string debug_label,
64 fuchsia::ui::views::ViewToken view_token,
65 fidl::InterfaceHandle<fuchsia::ui::scenic::Session> session,
66 fml::closure session_error_callback,
67 zx_handle_t vsync_event_handle)
68 : debug_label_(std::move(debug_label)),
69 session_connection_(debug_label_,
70 std::move(view_token),
71 std::move(session),
72 session_error_callback,
73 vsync_event_handle) {}
74
OnSessionMetricsDidChange(const fuchsia::ui::gfx::Metrics & metrics)75 void CompositorContext::OnSessionMetricsDidChange(
76 const fuchsia::ui::gfx::Metrics& metrics) {
77 session_connection_.set_metrics(metrics);
78 }
79
OnSessionSizeChangeHint(float width_change_factor,float height_change_factor)80 void CompositorContext::OnSessionSizeChangeHint(float width_change_factor,
81 float height_change_factor) {
82 session_connection_.OnSessionSizeChangeHint(width_change_factor,
83 height_change_factor);
84 }
85
86 CompositorContext::~CompositorContext() = default;
87
88 std::unique_ptr<flutter::CompositorContext::ScopedFrame>
AcquireFrame(GrContext * gr_context,SkCanvas * canvas,flutter::ExternalViewEmbedder * view_embedder,const SkMatrix & root_surface_transformation,bool instrumentation_enabled,fml::RefPtr<fml::GpuThreadMerger> gpu_thread_merger)89 CompositorContext::AcquireFrame(
90 GrContext* gr_context,
91 SkCanvas* canvas,
92 flutter::ExternalViewEmbedder* view_embedder,
93 const SkMatrix& root_surface_transformation,
94 bool instrumentation_enabled,
95 fml::RefPtr<fml::GpuThreadMerger> gpu_thread_merger) {
96 // TODO: The AcquireFrame interface is too broad and must be refactored to get
97 // rid of the context and canvas arguments as those seem to be only used for
98 // colorspace correctness purposes on the mobile shells.
99 return std::make_unique<flutter_runner::ScopedFrame>(
100 *this, //
101 root_surface_transformation, //
102 instrumentation_enabled, //
103 session_connection_ //
104 );
105 }
106
107 } // namespace flutter_runner
108