Flutter Engine
The Flutter Engine
flatland_connection.h
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// Maintains a connection to Fuchsia's Flatland protocol used for rendering
6// 2D graphics scenes.
7
8#ifndef FLUTTER_SHELL_PLATFORM_FUCHSIA_FLUTTER_FLATLAND_CONNECTION_H_
9#define FLUTTER_SHELL_PLATFORM_FUCHSIA_FLUTTER_FLATLAND_CONNECTION_H_
10
11#include <fuchsia/ui/composition/cpp/fidl.h>
12#include <lib/async/default.h>
13
14#include "flutter/fml/closure.h"
15#include "flutter/fml/macros.h"
16#include "flutter/fml/time/time_delta.h"
17#include "flutter/fml/time/time_point.h"
18
19#include "vsync_waiter.h"
20
21#include <cstdint>
22#include <mutex>
23#include <queue>
24#include <string>
25
26namespace flutter_runner {
27
28// The maximum number of fences that can be signaled at a time.
29static constexpr size_t kMaxFences =
30 fuchsia::ui::composition::MAX_ACQUIRE_RELEASE_FENCE_COUNT;
31
32// A helper to ferry around multiplexed events for signaling. Helps move
33// non-copyable events into closures.
34class Overflow {
35 public:
36 std::vector<zx::event> fences_;
38 Overflow() { fences_.reserve(kMaxFences); };
39};
40
42 std::function<void(fuchsia::scenic::scheduling::FramePresentedInfo)>;
43
44// 10ms interval to target vsync is only used until Scenic sends presentation
45// feedback.
48
49// The component residing on the raster thread that is responsible for
50// maintaining the Flatland instance connection and presenting updates.
51class FlatlandConnection final {
52 public:
54 const std::string& debug_label,
55 fuchsia::ui::composition::FlatlandHandle flatland,
56 fml::closure error_callback,
57 on_frame_presented_event on_frame_presented_callback,
58 async_dispatcher_t* dispatcher = async_get_default_dispatcher());
59
61
62 void Present();
63
64 // Used to implement VsyncWaiter functionality.
65 // Note that these two methods are called from the UI thread while the
66 // rest of the methods on this class are called from the raster thread.
69
70 fuchsia::ui::composition::Flatland* flatland() { return flatland_.get(); }
71
72 fuchsia::ui::composition::TransformId NextTransformId() {
73 return {++next_transform_id_};
74 }
75
76 fuchsia::ui::composition::ContentId NextContentId() {
77 return {++next_content_id_};
78 }
79
80 // Adds a new acquire fence to be sent out to the next Present() call.
81 //
82 // Acquire fences must all be signaled by the user.
83 //
84 // PERFORMANCE NOTES:
85 //
86 // Enqueuing more than 16 fences per frame incurs a performance penalty, so
87 // use them sparingly.
88 //
89 // Skipped frames may cause the number of fences to increase, leading to
90 // more performance issues. Ideally, the flow of the frames should be smooth.
92
93 // Adds a new release fence to be sent out to the next Present() call.
94 //
95 // Release fences are all signaled by Scenic (Flatland server).
96 //
97 // See the performance notes on EnqueueAcquireFence for performance details.
99
100 private:
101 void OnError(fuchsia::ui::composition::FlatlandError error);
102
103 void OnNextFrameBegin(
104 fuchsia::ui::composition::OnNextFrameBeginValues values);
105 void OnFramePresented(fuchsia::scenic::scheduling::FramePresentedInfo info);
106 void DoPresent();
107
108 fml::TimePoint GetNextPresentationTime(const fml::TimePoint& now);
109 bool MaybeRunInitialVsyncCallback(const fml::TimePoint& now,
111 void RunVsyncCallback(const fml::TimePoint& now,
113
114 async_dispatcher_t* dispatcher_;
115
116 fuchsia::ui::composition::FlatlandPtr flatland_;
117
118 fml::closure error_callback_;
119
120 uint64_t next_transform_id_ = 0;
121 uint64_t next_content_id_ = 0;
122
123 on_frame_presented_event on_frame_presented_callback_;
124 bool present_waiting_for_credit_ = false;
125
126 // A flow event trace id for following |Flatland::Present| calls into Scenic.
127 uint64_t next_present_trace_id_ = 0;
128
129 // This struct contains state that is accessed from both from the UI thread
130 // (in AwaitVsync) and the raster thread (in OnNextFrameBegin and Present).
131 // You should always lock mutex_ before touching anything in this struct
132 struct {
133 std::mutex mutex_;
134 std::queue<fml::TimePoint> next_presentation_times_;
139 uint32_t present_credits_ = 1;
141 } threadsafe_state_;
142
143 // Acquire fences sent to Flatland.
144 std::vector<zx::event> acquire_fences_;
145
146 // Multiplexed acquire fences over the critical number of ~16.
147 std::shared_ptr<Overflow> acquire_overflow_;
148
149 // Release fences sent to Flatland. Similar to acquire fences above.
150 std::vector<zx::event> current_present_release_fences_;
151 std::shared_ptr<Overflow> current_release_overflow_;
152
153 // Release fences from the prior call to DoPresent().
154 // Similar to acquire fences above.
155 std::vector<zx::event> previous_present_release_fences_;
156 std::shared_ptr<Overflow> previous_release_overflow_;
157
158 std::string debug_label_;
159
160 FML_DISALLOW_COPY_AND_ASSIGN(FlatlandConnection);
161};
162
163} // namespace flutter_runner
164
165#endif // FLUTTER_SHELL_PLATFORM_FUCHSIA_FLUTTER_FLATLAND_CONNECTION_H_
static void info(const char *fmt,...) SK_PRINTF_LIKE(1
Definition: DM.cpp:213
fuchsia::ui::composition::ContentId NextContentId()
fuchsia::ui::composition::Flatland * flatland()
std::queue< fml::TimePoint > next_presentation_times_
void AwaitVsyncForSecondaryCallback(FireCallbackCallback callback)
FlatlandConnection(const std::string &debug_label, fuchsia::ui::composition::FlatlandHandle flatland, fml::closure error_callback, on_frame_presented_event on_frame_presented_callback, async_dispatcher_t *dispatcher=async_get_default_dispatcher())
void AwaitVsync(FireCallbackCallback callback)
fuchsia::ui::composition::TransformId NextTransformId()
std::vector< zx::event > fences_
static constexpr TimeDelta FromMilliseconds(int64_t millis)
Definition: time_delta.h:46
FlKeyEvent uint64_t FlKeyResponderAsyncCallback callback
FlKeyEvent * event
const uint8_t uint32_t uint32_t GError ** error
Dart_NativeFunction function
Definition: fuchsia.cc:51
static constexpr size_t kMaxFences
std::function< void(fuchsia::scenic::scheduling::FramePresentedInfo)> on_frame_presented_event
std::function< void(fml::TimePoint, fml::TimePoint)> FireCallbackCallback
Definition: vsync_waiter.h:20
static constexpr fml::TimeDelta kInitialFlatlandVsyncOffset
std::function< void()> closure
Definition: closure.h:14