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/trees/latency_info_swap_promise_monitor.h"
7 #include "base/threading/platform_thread.h"
8 #include "cc/output/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
=
18 on_main
? 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
, 0,
31 latency_info
->AddLatencyNumber(
32 ui::INPUT_EVENT_LATENCY_FORWARD_SCROLL_UPDATE_TO_MAIN_COMPONENT
, 0,
33 latency_info
->trace_id());
41 LatencyInfoSwapPromiseMonitor::LatencyInfoSwapPromiseMonitor(
42 ui::LatencyInfo
* latency
,
43 LayerTreeHost
* layer_tree_host
,
44 LayerTreeHostImpl
* layer_tree_host_impl
)
45 : SwapPromiseMonitor(layer_tree_host
, layer_tree_host_impl
),
49 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 // Queue a pinned swap promise on the active tree. This will allow
63 // measurement of the time to the next SwapBuffers(). The swap
64 // promise is pinned so that it is not interrupted by new incoming
65 // activations (which would otherwise break the swap promise).
66 layer_tree_host_impl_
->active_tree()->QueuePinnedSwapPromise(
71 void LatencyInfoSwapPromiseMonitor::OnForwardScrollUpdateToMainThreadOnImpl() {
72 if (AddForwardingScrollUpdateToMainComponent(latency_
)) {
73 int64 new_sequence_number
= 0;
74 for (ui::LatencyInfo::LatencyMap::const_iterator it
=
75 latency_
->latency_components().begin();
76 it
!= latency_
->latency_components().end(); ++it
) {
77 if (it
->first
.first
== ui::INPUT_EVENT_LATENCY_BEGIN_RWH_COMPONENT
) {
79 ((static_cast<int64
>(base::PlatformThread::CurrentId()) << 32) ^
80 (reinterpret_cast<uint64
>(this) << 32)) |
81 (it
->second
.sequence_number
& 0xffffffff);
82 if (new_sequence_number
== it
->second
.sequence_number
)
87 if (!new_sequence_number
)
89 ui::LatencyInfo new_latency
;
90 new_latency
.AddLatencyNumberWithTraceName(
91 ui::LATENCY_BEGIN_SCROLL_LISTENER_UPDATE_MAIN_COMPONENT
, 0,
92 new_sequence_number
, "ScrollUpdate");
93 new_latency
.CopyLatencyFrom(
95 ui::INPUT_EVENT_LATENCY_FORWARD_SCROLL_UPDATE_TO_MAIN_COMPONENT
);
96 scoped_ptr
<SwapPromise
> swap_promise(
97 new LatencyInfoSwapPromise(new_latency
));
98 layer_tree_host_impl_
->QueueSwapPromiseForMainThreadScrollUpdate(