OLD | NEW |
1 // Copyright 2013 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "content/browser/renderer_host/input/synthetic_gesture_controller.h" | 5 #include "content/browser/renderer_host/input/synthetic_gesture_controller.h" |
6 | 6 |
7 #include <utility> | 7 #include <utility> |
8 | 8 |
9 #include "base/threading/thread_task_runner_handle.h" | 9 #include "base/threading/thread_task_runner_handle.h" |
10 #include "base/trace_event/trace_event.h" | 10 #include "base/trace_event/trace_event.h" |
(...skipping 22 matching lines...) Expand all Loading... |
33 | 33 |
34 bool was_empty = pending_gesture_queue_.IsEmpty(); | 34 bool was_empty = pending_gesture_queue_.IsEmpty(); |
35 | 35 |
36 pending_gesture_queue_.Push(std::move(synthetic_gesture), | 36 pending_gesture_queue_.Push(std::move(synthetic_gesture), |
37 completion_callback); | 37 completion_callback); |
38 | 38 |
39 if (was_empty) | 39 if (was_empty) |
40 StartGesture(*pending_gesture_queue_.FrontGesture()); | 40 StartGesture(*pending_gesture_queue_.FrontGesture()); |
41 } | 41 } |
42 | 42 |
43 void SyntheticGestureController::RequestBeginFrame() { | |
44 DCHECK(!dispatch_timer_.IsRunning()); | |
45 delegate_->RequestBeginFrameForSynthesizedInput( | |
46 base::BindOnce(&SyntheticGestureController::OnBeginFrame, | |
47 weak_ptr_factory_.GetWeakPtr())); | |
48 } | |
49 | |
50 void SyntheticGestureController::OnBeginFrame() { | |
51 // In order to make sure we get consistent results across runs, we attempt to | |
52 // start the timer at a fixed offset from the vsync. Starting the timer | |
53 // shortly after a begin-frame is likely to produce latency close to the worst | |
54 // cases. We feel 2 milliseconds is a good offset for this. | |
55 constexpr base::TimeDelta kSynthesizedDispatchDelay = | |
56 base::TimeDelta::FromMilliseconds(2); | |
57 base::ThreadTaskRunnerHandle::Get()->PostDelayedTask( | |
58 FROM_HERE, | |
59 base::BindOnce(&SyntheticGestureController::StartTimer, | |
60 weak_ptr_factory_.GetWeakPtr()), | |
61 kSynthesizedDispatchDelay); | |
62 } | |
63 | |
64 void SyntheticGestureController::StartTimer() { | 43 void SyntheticGestureController::StartTimer() { |
65 // TODO(sad): Change the interval to allow sending multiple events per begin | 44 // TODO(sad): Change the interval to allow sending multiple events per begin |
66 // frame. | 45 // frame. |
67 dispatch_timer_.Start( | 46 dispatch_timer_.Start( |
68 FROM_HERE, base::TimeDelta::FromMicroseconds(16666), | 47 FROM_HERE, base::TimeDelta::FromMicroseconds(16666), |
69 base::BindRepeating( | 48 base::BindRepeating( |
70 [](base::WeakPtr<SyntheticGestureController> weak_ptr) { | 49 [](base::WeakPtr<SyntheticGestureController> weak_ptr) { |
71 if (weak_ptr) | 50 if (weak_ptr) |
72 weak_ptr->DispatchNextEvent(base::TimeTicks::Now()); | 51 weak_ptr->DispatchNextEvent(base::TimeTicks::Now()); |
73 }, | 52 }, |
(...skipping 30 matching lines...) Expand all Loading... |
104 } | 83 } |
105 StartGesture(*pending_gesture_queue_.FrontGesture()); | 84 StartGesture(*pending_gesture_queue_.FrontGesture()); |
106 return true; | 85 return true; |
107 } | 86 } |
108 | 87 |
109 void SyntheticGestureController::StartGesture(const SyntheticGesture& gesture) { | 88 void SyntheticGestureController::StartGesture(const SyntheticGesture& gesture) { |
110 TRACE_EVENT_ASYNC_BEGIN0("input,benchmark", | 89 TRACE_EVENT_ASYNC_BEGIN0("input,benchmark", |
111 "SyntheticGestureController::running", | 90 "SyntheticGestureController::running", |
112 &gesture); | 91 &gesture); |
113 if (!dispatch_timer_.IsRunning()) | 92 if (!dispatch_timer_.IsRunning()) |
114 RequestBeginFrame(); | 93 StartTimer(); |
115 } | 94 } |
116 | 95 |
117 void SyntheticGestureController::StopGesture( | 96 void SyntheticGestureController::StopGesture( |
118 const SyntheticGesture& gesture, | 97 const SyntheticGesture& gesture, |
119 const OnGestureCompleteCallback& completion_callback, | 98 const OnGestureCompleteCallback& completion_callback, |
120 SyntheticGesture::Result result) { | 99 SyntheticGesture::Result result) { |
121 DCHECK_NE(result, SyntheticGesture::GESTURE_RUNNING); | 100 DCHECK_NE(result, SyntheticGesture::GESTURE_RUNNING); |
122 TRACE_EVENT_ASYNC_END0("input,benchmark", | 101 TRACE_EVENT_ASYNC_END0("input,benchmark", |
123 "SyntheticGestureController::running", | 102 "SyntheticGestureController::running", |
124 &gesture); | 103 &gesture); |
125 | 104 |
126 completion_callback.Run(result); | 105 completion_callback.Run(result); |
127 } | 106 } |
128 | 107 |
129 SyntheticGestureController::GestureAndCallbackQueue::GestureAndCallbackQueue() { | 108 SyntheticGestureController::GestureAndCallbackQueue::GestureAndCallbackQueue() { |
130 } | 109 } |
131 | 110 |
132 SyntheticGestureController::GestureAndCallbackQueue:: | 111 SyntheticGestureController::GestureAndCallbackQueue:: |
133 ~GestureAndCallbackQueue() { | 112 ~GestureAndCallbackQueue() { |
134 } | 113 } |
135 | 114 |
136 } // namespace content | 115 } // namespace content |
OLD | NEW |