1 // Copyright 2013 The Chromium 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.
5 #include "cc/base/latency_info_swap_promise_monitor.h"
7 #include "base/threading/platform_thread.h"
8 #include "cc/base/latency_info_swap_promise.h"
9 #include "cc/trees/layer_tree_host.h"
10 #include "cc/trees/layer_tree_host_impl.h"
11 #include "cc/trees/layer_tree_impl.h"
15 bool AddRenderingScheduledComponent(ui::LatencyInfo
* latency_info
,
17 ui::LatencyComponentType type
= on_main
?
18 ui::INPUT_EVENT_LATENCY_RENDERING_SCHEDULED_MAIN_COMPONENT
:
19 ui::INPUT_EVENT_LATENCY_RENDERING_SCHEDULED_IMPL_COMPONENT
;
20 if (latency_info
->FindLatency(type
, 0, nullptr))
22 latency_info
->AddLatencyNumber(type
, 0, 0);
26 bool AddForwardingScrollUpdateToMainComponent(ui::LatencyInfo
* latency_info
) {
27 if (latency_info
->FindLatency(
28 ui::INPUT_EVENT_LATENCY_FORWARD_SCROLL_UPDATE_TO_MAIN_COMPONENT
,
32 latency_info
->AddLatencyNumber(
33 ui::INPUT_EVENT_LATENCY_FORWARD_SCROLL_UPDATE_TO_MAIN_COMPONENT
,
35 latency_info
->trace_id
);
43 LatencyInfoSwapPromiseMonitor::LatencyInfoSwapPromiseMonitor(
44 ui::LatencyInfo
* latency
,
45 LayerTreeHost
* layer_tree_host
,
46 LayerTreeHostImpl
* layer_tree_host_impl
)
47 : SwapPromiseMonitor(layer_tree_host
, layer_tree_host_impl
),
50 LatencyInfoSwapPromiseMonitor::~LatencyInfoSwapPromiseMonitor() {}
52 void LatencyInfoSwapPromiseMonitor::OnSetNeedsCommitOnMain() {
53 if (AddRenderingScheduledComponent(latency_
, true /* on_main */)) {
54 scoped_ptr
<SwapPromise
> swap_promise(new LatencyInfoSwapPromise(*latency_
));
55 layer_tree_host_
->QueueSwapPromise(swap_promise
.Pass());
59 void LatencyInfoSwapPromiseMonitor::OnSetNeedsRedrawOnImpl() {
60 if (AddRenderingScheduledComponent(latency_
, false /* on_main */)) {
61 scoped_ptr
<SwapPromise
> swap_promise(new LatencyInfoSwapPromise(*latency_
));
62 layer_tree_host_impl_
->active_tree()->QueueSwapPromise(swap_promise
.Pass());
66 void LatencyInfoSwapPromiseMonitor::OnForwardScrollUpdateToMainThreadOnImpl() {
67 if (AddForwardingScrollUpdateToMainComponent(latency_
)) {
68 int64 new_sequence_number
= 0;
69 for (ui::LatencyInfo::LatencyMap::const_iterator it
=
70 latency_
->latency_components
.begin();
71 it
!= latency_
->latency_components
.end();
73 if (it
->first
.first
== ui::INPUT_EVENT_LATENCY_BEGIN_RWH_COMPONENT
) {
75 (static_cast<int64
>(base::PlatformThread::CurrentId()) << 32) |
76 (it
->second
.sequence_number
& 0xffffffff);
77 DCHECK(new_sequence_number
!= it
->second
.sequence_number
);
81 if (!new_sequence_number
)
83 ui::LatencyInfo new_latency
;
84 new_latency
.AddLatencyNumber(
85 ui::INPUT_EVENT_LATENCY_BEGIN_SCROLL_UPDATE_MAIN_COMPONENT
,
88 new_latency
.TraceEventType("ScrollUpdate");
89 new_latency
.CopyLatencyFrom(
91 ui::INPUT_EVENT_LATENCY_FORWARD_SCROLL_UPDATE_TO_MAIN_COMPONENT
);
92 scoped_ptr
<SwapPromise
> swap_promise(
93 new LatencyInfoSwapPromise(new_latency
));
94 layer_tree_host_impl_
->QueueSwapPromiseForMainThreadScrollUpdate(