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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
cc / trees / single_thread_proxy.h [blame]
// Copyright 2011 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#ifndef CC_TREES_SINGLE_THREAD_PROXY_H_
#define CC_TREES_SINGLE_THREAD_PROXY_H_
#include <limits>
#include <memory>
#include <vector>
#include "base/cancelable_callback.h"
#include "base/containers/flat_set.h"
#include "base/memory/raw_ptr.h"
#include "base/time/time.h"
#include "base/types/optional_ref.h"
#include "cc/input/browser_controls_offset_tags_info.h"
#include "cc/scheduler/scheduler.h"
#include "cc/trees/layer_tree_host_impl.h"
#include "cc/trees/paint_holding_reason.h"
#include "cc/trees/proxy.h"
#include "cc/trees/task_runner_provider.h"
#include "components/viz/common/frame_sinks/begin_frame_args.h"
#include "components/viz/common/surfaces/local_surface_id.h"
#include "components/viz/common/view_transition_element_resource_id.h"
namespace viz {
class BeginFrameSource;
struct FrameTimingDetails;
}
namespace cc {
class LayerTreeHost;
class LayerTreeHostSingleThreadClient;
class RenderFrameMetadataObserver;
class CC_EXPORT SingleThreadProxy : public Proxy,
LayerTreeHostImplClient,
public SchedulerClient {
public:
static std::unique_ptr<Proxy> Create(
LayerTreeHost* layer_tree_host,
LayerTreeHostSingleThreadClient* client,
TaskRunnerProvider* task_runner_provider);
SingleThreadProxy(const SingleThreadProxy&) = delete;
~SingleThreadProxy() override;
SingleThreadProxy& operator=(const SingleThreadProxy&) = delete;
// Proxy implementation
bool IsStarted() const override;
void SetLayerTreeFrameSink(
LayerTreeFrameSink* layer_tree_frame_sink) override;
void ReleaseLayerTreeFrameSink() override;
void SetVisible(bool visible) override;
void SetShouldWarmUp() override;
void SetNeedsAnimate() override;
void SetNeedsUpdateLayers() override;
void SetNeedsCommit() override;
void SetNeedsRedraw(const gfx::Rect& damage_rect) override;
void SetTargetLocalSurfaceId(
const viz::LocalSurfaceId& target_local_surface_id) override;
void DetachInputDelegateAndRenderFrameObserver() override;
bool RequestedAnimatePending() override;
void SetDeferMainFrameUpdate(bool defer_main_frame_update) override;
void SetPauseRendering(bool pause_rendering) override;
void SetInputResponsePending() override;
bool StartDeferringCommits(base::TimeDelta timeout,
PaintHoldingReason reason) override;
void StopDeferringCommits(PaintHoldingCommitTrigger) override;
bool IsDeferringCommits() const override;
bool CommitRequested() const override;
void Start() override;
void Stop() override;
void QueueImageDecode(int request_id, const PaintImage& image) override;
void SetMutator(std::unique_ptr<LayerTreeMutator> mutator) override;
void SetPaintWorkletLayerPainter(
std::unique_ptr<PaintWorkletLayerPainter> painter) override;
bool MainFrameWillHappenForTesting() override;
void RequestBeginMainFrameNotExpected(bool new_state) override;
void SetSourceURL(ukm::SourceId source_id, const GURL& url) override;
void SetUkmSmoothnessDestination(
base::WritableSharedMemoryMapping ukm_smoothness_data) override;
void SetRenderFrameObserver(
std::unique_ptr<RenderFrameMetadataObserver> observer) override;
void CompositeImmediatelyForTest(base::TimeTicks frame_begin_time,
bool raster,
base::OnceClosure callback) override;
double GetPercentDroppedFrames() const override;
void UpdateBrowserControlsState(
BrowserControlsState constraints,
BrowserControlsState current,
bool animate,
base::optional_ref<const BrowserControlsOffsetTagsInfo> offset_tags_info)
override;
// SchedulerClient implementation
bool WillBeginImplFrame(const viz::BeginFrameArgs& args) override;
void DidFinishImplFrame(
const viz::BeginFrameArgs& last_activated_args) override;
void DidNotProduceFrame(const viz::BeginFrameAck& ack,
FrameSkippedReason reason) override;
void WillNotReceiveBeginFrame() override;
void ScheduledActionSendBeginMainFrame(
const viz::BeginFrameArgs& args) override;
DrawResult ScheduledActionDrawIfPossible() override;
DrawResult ScheduledActionDrawForced() override;
void ScheduledActionUpdateDisplayTree() override;
void ScheduledActionCommit() override;
void ScheduledActionPostCommit() override;
void ScheduledActionActivateSyncTree() override;
void ScheduledActionBeginLayerTreeFrameSinkCreation() override;
void ScheduledActionPrepareTiles() override;
void ScheduledActionInvalidateLayerTreeFrameSink(bool needs_redraw) override;
void ScheduledActionPerformImplSideInvalidation() override;
void SendBeginMainFrameNotExpectedSoon() override;
void ScheduledActionBeginMainFrameNotExpectedUntil(
base::TimeTicks time) override;
void FrameIntervalUpdated(base::TimeDelta interval) override;
void OnBeginImplFrameDeadline() override;
// LayerTreeHostImplClient implementation
void DidLoseLayerTreeFrameSinkOnImplThread() override;
void SetBeginFrameSource(viz::BeginFrameSource* source) override;
void DidReceiveCompositorFrameAckOnImplThread() override;
void OnCanDrawStateChanged(bool can_draw) override;
void NotifyReadyToActivate() override;
bool IsReadyToActivate() override;
void NotifyReadyToDraw() override;
void SetNeedsRedrawOnImplThread() override;
void SetNeedsOneBeginImplFrameOnImplThread() override;
void SetNeedsUpdateDisplayTreeOnImplThread() override {}
void SetNeedsPrepareTilesOnImplThread() override;
void SetNeedsCommitOnImplThread() override;
void SetVideoNeedsBeginFrames(bool needs_begin_frames) override;
void SetDeferBeginMainFrameFromImpl(bool defer_begin_main_frame) override {}
bool IsInsideDraw() override;
void RenewTreePriority() override;
void PostDelayedAnimationTaskOnImplThread(base::OnceClosure task,
base::TimeDelta delay) override;
void DidActivateSyncTree() override;
void DidPrepareTiles() override;
void DidCompletePageScaleAnimationOnImplThread() override;
void OnDrawForLayerTreeFrameSink(bool resourceless_software_draw,
bool skip_draw) override;
void SetNeedsImplSideInvalidation(
bool needs_first_draw_on_activation) override;
void NotifyImageDecodeRequestFinished(int request_id,
bool decode_succeeded) override;
void NotifyTransitionRequestFinished(
uint32_t sequence_id,
const viz::ViewTransitionElementResourceRects&) override;
void DidPresentCompositorFrameOnImplThread(
uint32_t frame_token,
PresentationTimeCallbackBuffer::PendingCallbacks callbacks,
const viz::FrameTimingDetails& details) override;
void NotifyAnimationWorkletStateChange(
AnimationWorkletMutationState state,
ElementListType element_list_type) override;
void NotifyPaintWorkletStateChange(
Scheduler::PaintWorkletState state) override;
void NotifyCompositorMetricsTrackerResults(
CustomTrackerResults results) override;
bool IsInSynchronousComposite() const override;
void FrameSinksToThrottleUpdated(
const base::flat_set<viz::FrameSinkId>& ids) override;
void ClearHistory() override;
void SetHasActiveThreadedScroll(bool is_scrolling) override;
void SetWaitingForScrollEvent(bool waiting_for_scroll_event) override;
size_t CommitDurationSampleCountForTesting() const override;
void RequestNewLayerTreeFrameSink();
void DidObserveFirstScrollDelay(
int source_frame_number,
base::TimeDelta first_scroll_delay,
base::TimeTicks first_scroll_timestamp) override;
LayerTreeHostImpl* LayerTreeHostImplForTesting() const {
return host_impl_.get();
}
viz::BeginFrameArgs BeginImplFrameForTest(base::TimeTicks frame_begin_time);
protected:
SingleThreadProxy(LayerTreeHost* layer_tree_host,
LayerTreeHostSingleThreadClient* client,
TaskRunnerProvider* task_runner_provider);
private:
void BeginMainFrame(const viz::BeginFrameArgs& begin_frame_args);
void BeginMainFrameAbortedOnImplThread(CommitEarlyOutReason reason);
void DoBeginMainFrame(const viz::BeginFrameArgs& begin_frame_args);
void DoPainting(const viz::BeginFrameArgs& commit_args);
void DoCommit(const viz::BeginFrameArgs& commit_args);
void DoPostCommit();
DrawResult DoComposite(LayerTreeHostImpl::FrameData* frame);
void DoSwap();
void DidCommitAndDrawFrame(int source_frame_number);
void CommitComplete();
bool ShouldComposite() const;
void ScheduleRequestNewLayerTreeFrameSink();
void IssueImageDecodeFinishedCallbacks();
void DidReceiveCompositorFrameAck();
// Accessed on main thread only.
raw_ptr<LayerTreeHost> layer_tree_host_;
raw_ptr<LayerTreeHostSingleThreadClient> single_thread_client_;
raw_ptr<TaskRunnerProvider> task_runner_provider_;
// Used on the Thread, but checked on main thread during
// initialization/shutdown.
std::unique_ptr<LayerTreeHostImpl> host_impl_;
// Accessed from both threads.
std::unique_ptr<Scheduler> scheduler_on_impl_thread_;
// Only used when defer_commits_ is active and must be set in such cases.
base::TimeTicks commits_restart_time_;
bool next_frame_is_newly_committed_frame_;
#if DCHECK_IS_ON()
bool inside_impl_frame_;
#endif
bool inside_draw_;
bool defer_main_frame_update_;
bool pause_rendering_;
std::optional<PaintHoldingReason> paint_holding_reason_;
bool did_apply_compositor_deltas_ = false;
bool animate_requested_;
bool update_layers_requested_;
bool commit_requested_;
bool inside_synchronous_composite_;
bool needs_impl_frame_;
// True if a request to the LayerTreeHostClient to create an output surface
// is still outstanding.
bool layer_tree_frame_sink_creation_requested_;
// When output surface is lost, is set to true until a new output surface is
// initialized.
bool layer_tree_frame_sink_lost_;
// A number that kept incrementing in CompositeImmediately, which indicates a
// new impl frame.
uint64_t begin_frame_sequence_number_ = 1u;
int source_frame_number_for_next_commit_ = kInvalidSourceFrameNumber;
// This is the callback for the scheduled RequestNewLayerTreeFrameSink.
base::CancelableOnceClosure layer_tree_frame_sink_creation_callback_;
base::WeakPtr<SingleThreadProxy> frame_sink_bound_weak_ptr_;
// WeakPtrs generated by this factory will be invalidated when
// LayerTreeFrameSink is released.
base::WeakPtrFactory<SingleThreadProxy> frame_sink_bound_weak_factory_{this};
base::WeakPtrFactory<SingleThreadProxy> weak_factory_{this};
};
// For use in the single-threaded case. In debug builds, it pretends that the
// code is running on the impl thread to satisfy assertion checks.
class DebugScopedSetImplThread {
public:
#if DCHECK_IS_ON()
explicit DebugScopedSetImplThread(TaskRunnerProvider* task_runner_provider)
: task_runner_provider_(task_runner_provider) {
previous_value_ = task_runner_provider_->impl_thread_is_overridden_;
// Some test code will used this in both single- and multi-threaded mode;
// we should just ignore it in multi-threaded mode.
if (!task_runner_provider_->HasImplThread())
task_runner_provider_->SetCurrentThreadIsImplThread(true);
}
#else
explicit DebugScopedSetImplThread(TaskRunnerProvider* task_runner_provider) {}
#endif
DebugScopedSetImplThread(const DebugScopedSetImplThread&) = delete;
~DebugScopedSetImplThread() {
#if DCHECK_IS_ON()
if (!task_runner_provider_->HasImplThread())
task_runner_provider_->SetCurrentThreadIsImplThread(previous_value_);
#endif
}
DebugScopedSetImplThread& operator=(const DebugScopedSetImplThread&) = delete;
#if DCHECK_IS_ON()
private:
bool previous_value_;
raw_ptr<TaskRunnerProvider> task_runner_provider_;
#endif
};
// For use in the single-threaded case. In debug builds, it pretends that the
// code is running on the main thread to satisfy assertion checks.
class DebugScopedSetMainThread {
public:
#if DCHECK_IS_ON()
explicit DebugScopedSetMainThread(TaskRunnerProvider* task_runner_provider)
: task_runner_provider_(task_runner_provider) {
previous_value_ = task_runner_provider_->impl_thread_is_overridden_;
if (!task_runner_provider_->HasImplThread())
task_runner_provider_->SetCurrentThreadIsImplThread(false);
}
#else
explicit DebugScopedSetMainThread(TaskRunnerProvider* task_runner_provider) {}
#endif
DebugScopedSetMainThread(const DebugScopedSetMainThread&) = delete;
~DebugScopedSetMainThread() {
#if DCHECK_IS_ON()
if (!task_runner_provider_->HasImplThread())
task_runner_provider_->SetCurrentThreadIsImplThread(previous_value_);
#endif
}
DebugScopedSetMainThread& operator=(const DebugScopedSetMainThread&) = delete;
#if DCHECK_IS_ON()
private:
bool previous_value_;
raw_ptr<TaskRunnerProvider> task_runner_provider_;
#endif
};
// For use in the single-threaded case. In debug builds, it pretends that the
// code is running on the impl thread and that the main thread is blocked to
// satisfy assertion checks
class DebugScopedSetImplThreadAndMainThreadBlocked {
public:
explicit DebugScopedSetImplThreadAndMainThreadBlocked(
TaskRunnerProvider* task_runner_provider)
: impl_thread_(task_runner_provider),
main_thread_blocked_(task_runner_provider) {}
DebugScopedSetImplThreadAndMainThreadBlocked(
const DebugScopedSetImplThreadAndMainThreadBlocked&) = delete;
DebugScopedSetImplThreadAndMainThreadBlocked& operator=(
const DebugScopedSetImplThreadAndMainThreadBlocked&) = delete;
private:
DebugScopedSetImplThread impl_thread_;
DebugScopedSetMainThreadBlocked main_thread_blocked_;
};
} // namespace cc
#endif // CC_TREES_SINGLE_THREAD_PROXY_H_