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