Chromium Code Reviews| 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" |
| 11 #include "content/browser/renderer_host/input/synthetic_gesture_target.h" | 11 #include "content/browser/renderer_host/input/synthetic_gesture_target.h" |
| 12 #include "content/common/input/synthetic_smooth_scroll_gesture_params.h" | 12 #include "content/common/input/synthetic_smooth_scroll_gesture_params.h" |
| 13 #include "content/common/input_messages.h" | 13 #include "content/common/input_messages.h" |
| 14 #include "content/public/browser/render_widget_host.h" | 14 #include "content/public/browser/render_widget_host.h" |
| 15 | 15 |
| 16 namespace content { | 16 namespace content { |
| 17 | 17 |
| 18 SyntheticGestureController::SyntheticGestureController( | 18 SyntheticGestureController::SyntheticGestureController( |
| 19 std::unique_ptr<SyntheticGestureTarget> gesture_target, | 19 Delegate* delegate, |
| 20 BeginFrameRequestCallback begin_frame_callback) | 20 std::unique_ptr<SyntheticGestureTarget> gesture_target) |
| 21 : gesture_target_(std::move(gesture_target)), | 21 : delegate_(delegate), |
| 22 begin_frame_callback_(std::move(begin_frame_callback)), | 22 gesture_target_(std::move(gesture_target)), |
| 23 weak_ptr_factory_(this) {} | 23 weak_ptr_factory_(this) { |
| 24 DCHECK(delegate_); | |
| 25 } | |
| 24 | 26 |
| 25 SyntheticGestureController::~SyntheticGestureController() {} | 27 SyntheticGestureController::~SyntheticGestureController() {} |
| 26 | 28 |
| 27 void SyntheticGestureController::QueueSyntheticGesture( | 29 void SyntheticGestureController::QueueSyntheticGesture( |
| 28 std::unique_ptr<SyntheticGesture> synthetic_gesture, | 30 std::unique_ptr<SyntheticGesture> synthetic_gesture, |
| 29 const OnGestureCompleteCallback& completion_callback) { | 31 const OnGestureCompleteCallback& completion_callback) { |
| 30 DCHECK(synthetic_gesture); | 32 DCHECK(synthetic_gesture); |
| 31 | 33 |
| 32 bool was_empty = pending_gesture_queue_.IsEmpty(); | 34 bool was_empty = pending_gesture_queue_.IsEmpty(); |
| 33 | 35 |
| 34 pending_gesture_queue_.Push(std::move(synthetic_gesture), | 36 pending_gesture_queue_.Push(std::move(synthetic_gesture), |
| 35 completion_callback); | 37 completion_callback); |
| 36 | 38 |
| 37 if (was_empty) | 39 if (was_empty) |
| 38 StartGesture(*pending_gesture_queue_.FrontGesture()); | 40 StartGesture(*pending_gesture_queue_.FrontGesture()); |
| 39 } | 41 } |
| 40 | 42 |
| 43 void SyntheticGestureController::RequestBeginFrame() { | |
| 44 delegate_->RequestBeginFrameForSynthesizedInput( | |
| 45 base::BindOnce(&SyntheticGestureController::OnBeginFrame, | |
| 46 weak_ptr_factory_.GetWeakPtr())); | |
| 47 } | |
| 48 | |
| 41 void SyntheticGestureController::OnBeginFrame() { | 49 void SyntheticGestureController::OnBeginFrame() { |
| 42 // TODO(sad): Instead of dispatching the events immediately, dispatch after an | 50 // TODO(sad): Instead of dispatching the events immediately, dispatch after an |
| 43 // offset. | 51 // offset. |
| 44 DispatchNextEvent(); | 52 DispatchNextEvent(); |
| 45 } | 53 } |
| 46 | 54 |
| 47 bool SyntheticGestureController::DispatchNextEvent(base::TimeTicks timestamp) { | 55 bool SyntheticGestureController::DispatchNextEvent(base::TimeTicks timestamp) { |
| 48 TRACE_EVENT0("input", "SyntheticGestureController::Flush"); | 56 TRACE_EVENT0("input", "SyntheticGestureController::Flush"); |
| 49 if (pending_gesture_queue_.IsEmpty()) | 57 if (pending_gesture_queue_.IsEmpty()) |
| 50 return false; | 58 return false; |
| 51 | 59 |
| 52 SyntheticGesture::Result result = | 60 if (!pending_gesture_queue_.is_current_gesture_complete()) { |
| 53 pending_gesture_queue_.FrontGesture()->ForwardInputEvents( | 61 SyntheticGesture::Result result = |
| 54 timestamp, gesture_target_.get()); | 62 pending_gesture_queue_.FrontGesture()->ForwardInputEvents( |
| 63 timestamp, gesture_target_.get()); | |
| 55 | 64 |
| 56 if (result == SyntheticGesture::GESTURE_RUNNING) { | 65 if (result == SyntheticGesture::GESTURE_RUNNING) { |
| 57 begin_frame_callback_.Run( | 66 RequestBeginFrame(); |
| 58 base::BindOnce(&SyntheticGestureController::OnBeginFrame, | 67 return true; |
| 59 weak_ptr_factory_.GetWeakPtr())); | 68 } |
| 69 pending_gesture_queue_.mark_current_gesture_complete(result); | |
| 70 } | |
| 71 | |
| 72 if (!delegate_->HasGestureStopped()) { | |
| 73 RequestBeginFrame(); | |
|
dtapuska
2017/05/19 13:20:36
This will still add an additional frame of latency
tdresser
2017/05/19 13:53:32
Yup, this should be fine.
sadrul
2017/05/19 16:55:49
This will add at most one frame to the end of the
| |
| 60 return true; | 74 return true; |
| 61 } | 75 } |
| 62 | 76 |
| 63 StopGesture(*pending_gesture_queue_.FrontGesture(), | 77 StopGesture(*pending_gesture_queue_.FrontGesture(), |
| 64 pending_gesture_queue_.FrontCallback(), result); | 78 pending_gesture_queue_.FrontCallback(), |
| 79 pending_gesture_queue_.current_gesture_result()); | |
| 65 pending_gesture_queue_.Pop(); | 80 pending_gesture_queue_.Pop(); |
| 66 if (pending_gesture_queue_.IsEmpty()) | 81 if (pending_gesture_queue_.IsEmpty()) |
| 67 return false; | 82 return false; |
| 68 StartGesture(*pending_gesture_queue_.FrontGesture()); | 83 StartGesture(*pending_gesture_queue_.FrontGesture()); |
| 69 return true; | 84 return true; |
| 70 } | 85 } |
| 71 | 86 |
| 72 void SyntheticGestureController::StartGesture(const SyntheticGesture& gesture) { | 87 void SyntheticGestureController::StartGesture(const SyntheticGesture& gesture) { |
| 73 TRACE_EVENT_ASYNC_BEGIN0("input,benchmark", | 88 TRACE_EVENT_ASYNC_BEGIN0("input,benchmark", |
| 74 "SyntheticGestureController::running", | 89 "SyntheticGestureController::running", |
| 75 &gesture); | 90 &gesture); |
| 76 begin_frame_callback_.Run( | 91 RequestBeginFrame(); |
| 77 base::BindOnce(&SyntheticGestureController::OnBeginFrame, | |
| 78 weak_ptr_factory_.GetWeakPtr())); | |
| 79 } | 92 } |
| 80 | 93 |
| 81 void SyntheticGestureController::StopGesture( | 94 void SyntheticGestureController::StopGesture( |
| 82 const SyntheticGesture& gesture, | 95 const SyntheticGesture& gesture, |
| 83 const OnGestureCompleteCallback& completion_callback, | 96 const OnGestureCompleteCallback& completion_callback, |
| 84 SyntheticGesture::Result result) { | 97 SyntheticGesture::Result result) { |
| 85 DCHECK_NE(result, SyntheticGesture::GESTURE_RUNNING); | 98 DCHECK_NE(result, SyntheticGesture::GESTURE_RUNNING); |
| 86 TRACE_EVENT_ASYNC_END0("input,benchmark", | 99 TRACE_EVENT_ASYNC_END0("input,benchmark", |
| 87 "SyntheticGestureController::running", | 100 "SyntheticGestureController::running", |
| 88 &gesture); | 101 &gesture); |
| 89 | 102 |
| 90 completion_callback.Run(result); | 103 completion_callback.Run(result); |
| 91 } | 104 } |
| 92 | 105 |
| 93 SyntheticGestureController::GestureAndCallbackQueue::GestureAndCallbackQueue() { | 106 SyntheticGestureController::GestureAndCallbackQueue::GestureAndCallbackQueue() { |
| 94 } | 107 } |
| 95 | 108 |
| 96 SyntheticGestureController::GestureAndCallbackQueue:: | 109 SyntheticGestureController::GestureAndCallbackQueue:: |
| 97 ~GestureAndCallbackQueue() { | 110 ~GestureAndCallbackQueue() { |
| 98 } | 111 } |
| 99 | 112 |
| 100 } // namespace content | 113 } // namespace content |
| OLD | NEW |