forked from chromium/chromium
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathpresentation_time_callback_buffer.h
145 lines (118 loc) · 5.92 KB
/
presentation_time_callback_buffer.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
// Copyright 2019 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#ifndef CC_TREES_PRESENTATION_TIME_CALLBACK_BUFFER_H_
#define CC_TREES_PRESENTATION_TIME_CALLBACK_BUFFER_H_
#include <vector>
#include "base/callback_forward.h"
#include "base/containers/circular_deque.h"
#include "base/sequence_checker.h"
#include "cc/cc_export.h"
#include "ui/gfx/presentation_feedback.h"
namespace cc {
// Maintains a queue of callbacks and compositor frame times that we want to
// buffer until a relevant frame is presented.
//
// Callbacks are queued through |RegisterMainThreadPresentationCallbacks| and
// |RegisterCompositorPresentationCallbacks| if the callback is to be run on
// the main thread or compositor thread respectively.
//
// Once a frame is presented, users of this class can call
// |PopPendingCallbacks| to get their callbacks back. This class never runs
// callbacks itself so it's up to calling code to |PostTask| or call |Run()| as
// needed.
//
// This class is thread unsafe so concurrent access would require external
// synchronization. In practice, however, instances of this class are only used
// on the compositor thread even though some of the buffered callbacks are
// intended to be run on the renderer main thread.
//
// CC_EXPORT is only needed for testing.
class CC_EXPORT PresentationTimeCallbackBuffer {
public:
// Maximum expected buffer size for presentation callbacks. We generally
// don't expect many frames waiting for a presentation feedback, hence we
// don't expect many presentation callbacks waiting for a frame presentation.
static constexpr size_t kMaxBufferSize = 60u;
// TODO(crbug.com/1199373): Compositor thread callbacks are only run for
// successful presentations and only need the presentation timestamp. On the
// other hand, main thread callbacks can be run on both successful and failed
// presentations and need a full `gfx::PresentationFeedback`. Conceptually,
// main thread callbacks should only be run for successful presentations, too,
// in which case the two callback signatures can be unified.
using MainCallback =
base::OnceCallback<void(const gfx::PresentationFeedback&)>;
using CompositorCallback = base::OnceCallback<void(base::TimeTicks)>;
PresentationTimeCallbackBuffer();
PresentationTimeCallbackBuffer(const PresentationTimeCallbackBuffer&) =
delete;
PresentationTimeCallbackBuffer(PresentationTimeCallbackBuffer&&);
PresentationTimeCallbackBuffer& operator=(
const PresentationTimeCallbackBuffer&) = delete;
PresentationTimeCallbackBuffer& operator=(PresentationTimeCallbackBuffer&&);
~PresentationTimeCallbackBuffer();
// Buffers the given |callbacks| in preparation for a GPU frame swap at or
// after the given |frame_token|. Calling code posts these callbacks to the
// main thread once they're popped.
void RegisterMainThreadPresentationCallbacks(
uint32_t frame_token,
std::vector<MainCallback> callbacks);
// Buffers the given |callbacks| in preparation for a GPU frame swap at or
// after the given |frame_token|. Calling code invokes these callbacks on the
// compositor thread once they're popped.
void RegisterCompositorPresentationCallbacks(
uint32_t frame_token,
std::vector<CompositorCallback> callbacks);
// Structured return value for |PopPendingCallbacks|. CC_EXPORT is only
// needed for testing.
struct CC_EXPORT PendingCallbacks {
PendingCallbacks();
PendingCallbacks(const PendingCallbacks&) = delete;
PendingCallbacks(PendingCallbacks&&);
PendingCallbacks& operator=(const PendingCallbacks&) = delete;
PendingCallbacks& operator=(PendingCallbacks&&);
~PendingCallbacks();
// Holds callbacks registered through
// |RegisterMainThreadPresentationCallbacks|.
std::vector<MainCallback> main_thread_callbacks;
// Holds callbacks registered through
// |RegisterCompositorPresentationCallbacks|.
std::vector<CompositorCallback> compositor_thread_callbacks;
};
// Call this once the presentation for the given `frame_token` has completed.
// Yields any pending callbacks that were registered against a frame token
// that was less than or equal to the given `frame_token`. If `main_only` is
// true, only callbacks for the main thread are returned. It is the caller's
// responsibility to run the callbacks on the right threads/sequences.
PendingCallbacks PopPendingCallbacks(uint32_t frame_token, bool main_only);
private:
// Stores information needed once we get a response for a particular
// presentation token.
struct FrameTokenInfo {
explicit FrameTokenInfo(uint32_t token);
FrameTokenInfo(const FrameTokenInfo&) = delete;
FrameTokenInfo(FrameTokenInfo&&);
FrameTokenInfo& operator=(const FrameTokenInfo&) = delete;
FrameTokenInfo& operator=(FrameTokenInfo&&);
~FrameTokenInfo();
// A |CompositorFrameMetadata::frame_token| that we use to associate
// presentation feedback with the relevant compositor frame.
uint32_t token;
// The callbacks to send back to the main thread.
std::vector<MainCallback> main_thread_callbacks;
// The callbacks to invoke on the compositor thread.
std::vector<CompositorCallback> compositor_thread_callbacks;
};
// Returns a reference to a |FrameTokenInfo| with the given |frame_token|.
// The instance is created if necessary and occupies the appropriate place in
// |frame_token_infos_|.
FrameTokenInfo& GetOrMakeRegistration(uint32_t frame_token);
// Queue of pending registrations ordered by |token|. We can use a deque
// because we require callers to use non-decreasing tokens when registering.
base::circular_deque<FrameTokenInfo> frame_token_infos_;
// When DCHECK is enabled, check that instances of this class aren't being
// used concurrently.
SEQUENCE_CHECKER(sequence_checker_);
};
} // namespace cc
#endif // CC_TREES_PRESENTATION_TIME_CALLBACK_BUFFER_H_