| 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 23 matching lines...) Expand all Loading... |
| 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() { | 43 void SyntheticGestureController::RequestBeginFrame() { |
| 44 DCHECK(!dispatch_timer_.IsRunning()); |
| 44 delegate_->RequestBeginFrameForSynthesizedInput( | 45 delegate_->RequestBeginFrameForSynthesizedInput( |
| 45 base::BindOnce(&SyntheticGestureController::OnBeginFrame, | 46 base::BindOnce(&SyntheticGestureController::OnBeginFrame, |
| 46 weak_ptr_factory_.GetWeakPtr())); | 47 weak_ptr_factory_.GetWeakPtr())); |
| 47 } | 48 } |
| 48 | 49 |
| 49 void SyntheticGestureController::OnBeginFrame() { | 50 void SyntheticGestureController::OnBeginFrame() { |
| 50 // TODO(sad): Instead of dispatching the events immediately, dispatch after an | 51 // In order to make sure we get consistent results across runs, we attempt to |
| 51 // offset. | 52 // start the timer at a fixed offset from the vsync. Starting the timer |
| 52 DispatchNextEvent(); | 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() { |
| 65 // TODO(sad): Change the interval to allow sending multiple events per begin |
| 66 // frame. |
| 67 dispatch_timer_.Start( |
| 68 FROM_HERE, base::TimeDelta::FromMicroseconds(16666), |
| 69 base::BindRepeating( |
| 70 [](base::WeakPtr<SyntheticGestureController> weak_ptr) { |
| 71 if (weak_ptr) |
| 72 weak_ptr->DispatchNextEvent(base::TimeTicks::Now()); |
| 73 }, |
| 74 weak_ptr_factory_.GetWeakPtr())); |
| 53 } | 75 } |
| 54 | 76 |
| 55 bool SyntheticGestureController::DispatchNextEvent(base::TimeTicks timestamp) { | 77 bool SyntheticGestureController::DispatchNextEvent(base::TimeTicks timestamp) { |
| 78 DCHECK(dispatch_timer_.IsRunning()); |
| 56 TRACE_EVENT0("input", "SyntheticGestureController::Flush"); | 79 TRACE_EVENT0("input", "SyntheticGestureController::Flush"); |
| 57 if (pending_gesture_queue_.IsEmpty()) | 80 if (pending_gesture_queue_.IsEmpty()) |
| 58 return false; | 81 return false; |
| 59 | 82 |
| 60 if (!pending_gesture_queue_.is_current_gesture_complete()) { | 83 if (!pending_gesture_queue_.is_current_gesture_complete()) { |
| 61 SyntheticGesture::Result result = | 84 SyntheticGesture::Result result = |
| 62 pending_gesture_queue_.FrontGesture()->ForwardInputEvents( | 85 pending_gesture_queue_.FrontGesture()->ForwardInputEvents( |
| 63 timestamp, gesture_target_.get()); | 86 timestamp, gesture_target_.get()); |
| 64 | 87 |
| 65 if (result == SyntheticGesture::GESTURE_RUNNING) { | 88 if (result == SyntheticGesture::GESTURE_RUNNING) { |
| 66 RequestBeginFrame(); | |
| 67 return true; | 89 return true; |
| 68 } | 90 } |
| 69 pending_gesture_queue_.mark_current_gesture_complete(result); | 91 pending_gesture_queue_.mark_current_gesture_complete(result); |
| 70 } | 92 } |
| 71 | 93 |
| 72 if (!delegate_->HasGestureStopped()) { | 94 if (!delegate_->HasGestureStopped()) |
| 73 RequestBeginFrame(); | |
| 74 return true; | 95 return true; |
| 75 } | |
| 76 | 96 |
| 77 StopGesture(*pending_gesture_queue_.FrontGesture(), | 97 StopGesture(*pending_gesture_queue_.FrontGesture(), |
| 78 pending_gesture_queue_.FrontCallback(), | 98 pending_gesture_queue_.FrontCallback(), |
| 79 pending_gesture_queue_.current_gesture_result()); | 99 pending_gesture_queue_.current_gesture_result()); |
| 80 pending_gesture_queue_.Pop(); | 100 pending_gesture_queue_.Pop(); |
| 81 if (pending_gesture_queue_.IsEmpty()) | 101 if (pending_gesture_queue_.IsEmpty()) { |
| 102 dispatch_timer_.Stop(); |
| 82 return false; | 103 return false; |
| 104 } |
| 83 StartGesture(*pending_gesture_queue_.FrontGesture()); | 105 StartGesture(*pending_gesture_queue_.FrontGesture()); |
| 84 return true; | 106 return true; |
| 85 } | 107 } |
| 86 | 108 |
| 87 void SyntheticGestureController::StartGesture(const SyntheticGesture& gesture) { | 109 void SyntheticGestureController::StartGesture(const SyntheticGesture& gesture) { |
| 88 TRACE_EVENT_ASYNC_BEGIN0("input,benchmark", | 110 TRACE_EVENT_ASYNC_BEGIN0("input,benchmark", |
| 89 "SyntheticGestureController::running", | 111 "SyntheticGestureController::running", |
| 90 &gesture); | 112 &gesture); |
| 91 RequestBeginFrame(); | 113 if (!dispatch_timer_.IsRunning()) |
| 114 RequestBeginFrame(); |
| 92 } | 115 } |
| 93 | 116 |
| 94 void SyntheticGestureController::StopGesture( | 117 void SyntheticGestureController::StopGesture( |
| 95 const SyntheticGesture& gesture, | 118 const SyntheticGesture& gesture, |
| 96 const OnGestureCompleteCallback& completion_callback, | 119 const OnGestureCompleteCallback& completion_callback, |
| 97 SyntheticGesture::Result result) { | 120 SyntheticGesture::Result result) { |
| 98 DCHECK_NE(result, SyntheticGesture::GESTURE_RUNNING); | 121 DCHECK_NE(result, SyntheticGesture::GESTURE_RUNNING); |
| 99 TRACE_EVENT_ASYNC_END0("input,benchmark", | 122 TRACE_EVENT_ASYNC_END0("input,benchmark", |
| 100 "SyntheticGestureController::running", | 123 "SyntheticGestureController::running", |
| 101 &gesture); | 124 &gesture); |
| 102 | 125 |
| 103 completion_callback.Run(result); | 126 completion_callback.Run(result); |
| 104 } | 127 } |
| 105 | 128 |
| 106 SyntheticGestureController::GestureAndCallbackQueue::GestureAndCallbackQueue() { | 129 SyntheticGestureController::GestureAndCallbackQueue::GestureAndCallbackQueue() { |
| 107 } | 130 } |
| 108 | 131 |
| 109 SyntheticGestureController::GestureAndCallbackQueue:: | 132 SyntheticGestureController::GestureAndCallbackQueue:: |
| 110 ~GestureAndCallbackQueue() { | 133 ~GestureAndCallbackQueue() { |
| 111 } | 134 } |
| 112 | 135 |
| 113 } // namespace content | 136 } // namespace content |
| OLD | NEW |