| OLD | NEW |
| 1 // Copyright 2016 The Chromium Authors. All rights reserved. | 1 // Copyright 2016 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 "ash/laser/laser_pointer_controller.h" | 5 #include "ash/laser/laser_pointer_controller.h" |
| 6 | 6 |
| 7 #include "ash/common/ash_switches.h" |
| 7 #include "ash/common/system/chromeos/palette/palette_utils.h" | 8 #include "ash/common/system/chromeos/palette/palette_utils.h" |
| 8 #include "ash/laser/laser_pointer_view.h" | 9 #include "ash/laser/laser_pointer_view.h" |
| 9 #include "ash/shell.h" | 10 #include "ash/shell.h" |
| 11 #include "base/command_line.h" |
| 12 #include "base/strings/string_number_conversions.h" |
| 10 #include "ui/display/screen.h" | 13 #include "ui/display/screen.h" |
| 14 #include "ui/events/base_event_utils.h" |
| 11 #include "ui/views/widget/widget.h" | 15 #include "ui/views/widget/widget.h" |
| 12 | 16 |
| 13 namespace ash { | 17 namespace ash { |
| 14 namespace { | 18 namespace { |
| 15 | 19 |
| 16 // A point gets removed from the collection if it is older than | 20 // A point gets removed from the collection if it is older than |
| 17 // |kPointLifeDurationMs|. | 21 // |kPointLifeDurationMs|. |
| 18 const int kPointLifeDurationMs = 200; | 22 const int kPointLifeDurationMs = 200; |
| 19 | 23 |
| 20 // When no move events are being received we add a new point every | 24 // When no move events are being received we add a new point every |
| 21 // |kAddStationaryPointsDelayMs| so that points older than | 25 // |kAddStationaryPointsDelayMs| so that points older than |
| 22 // |kPointLifeDurationMs| can get removed. | 26 // |kPointLifeDurationMs| can get removed. |
| 23 // Note: Using a delay less than the screen refresh interval will not | 27 // Note: Using a delay less than the screen refresh interval will not |
| 24 // provide a visual benefit but instead just waste time performing | 28 // provide a visual benefit but instead just waste time performing |
| 25 // unnecessary updates. 16ms is the refresh interval on most devices. | 29 // unnecessary updates. 16ms is the refresh interval on most devices. |
| 26 // TODO(reveman): Use real VSYNC interval instead of a hard-coded delay. | 30 // TODO(reveman): Use real VSYNC interval instead of a hard-coded delay. |
| 27 const int kAddStationaryPointsDelayMs = 16; | 31 const int kAddStationaryPointsDelayMs = 16; |
| 28 | 32 |
| 33 // The default amount of time used to estimate time from VSYNC event to when |
| 34 // visible light can be noticed by the user. This is used when a device |
| 35 // specific estimate was not provided using --estimated-presentation-delay. |
| 36 const int kDefaultPresentationDelayMs = 18; |
| 37 |
| 29 } // namespace | 38 } // namespace |
| 30 | 39 |
| 31 LaserPointerController::LaserPointerController() | 40 LaserPointerController::LaserPointerController() |
| 32 : stationary_timer_(new base::Timer( | 41 : stationary_timer_(new base::Timer( |
| 33 FROM_HERE, | 42 FROM_HERE, |
| 34 base::TimeDelta::FromMilliseconds(kAddStationaryPointsDelayMs), | 43 base::TimeDelta::FromMilliseconds(kAddStationaryPointsDelayMs), |
| 35 base::Bind(&LaserPointerController::AddStationaryPoint, | 44 base::Bind(&LaserPointerController::AddStationaryPoint, |
| 36 base::Unretained(this)), | 45 base::Unretained(this)), |
| 37 true /* is_repeating */)) { | 46 true /* is_repeating */)) { |
| 38 Shell::GetInstance()->AddPreTargetHandler(this); | 47 Shell::GetInstance()->AddPreTargetHandler(this); |
| 48 |
| 49 int64_t presentation_delay_ms; |
| 50 // Use device specific presentation delay if specified. |
| 51 std::string presentation_delay_string = |
| 52 base::CommandLine::ForCurrentProcess()->GetSwitchValueASCII( |
| 53 switches::kAshEstimatedPresentationDelay); |
| 54 if (!base::StringToInt64(presentation_delay_string, &presentation_delay_ms)) |
| 55 presentation_delay_ms = kDefaultPresentationDelayMs; |
| 56 presentation_delay_ = |
| 57 base::TimeDelta::FromMilliseconds(presentation_delay_ms); |
| 39 } | 58 } |
| 40 | 59 |
| 41 LaserPointerController::~LaserPointerController() { | 60 LaserPointerController::~LaserPointerController() { |
| 42 Shell::GetInstance()->RemovePreTargetHandler(this); | 61 Shell::GetInstance()->RemovePreTargetHandler(this); |
| 43 } | 62 } |
| 44 | 63 |
| 45 void LaserPointerController::SetEnabled(bool enabled) { | 64 void LaserPointerController::SetEnabled(bool enabled) { |
| 46 if (enabled == enabled_) | 65 if (enabled == enabled_) |
| 47 return; | 66 return; |
| 48 | 67 |
| (...skipping 50 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 99 } | 118 } |
| 100 | 119 |
| 101 void LaserPointerController::SwitchTargetRootWindowIfNeeded( | 120 void LaserPointerController::SwitchTargetRootWindowIfNeeded( |
| 102 aura::Window* root_window) { | 121 aura::Window* root_window) { |
| 103 if (!root_window) { | 122 if (!root_window) { |
| 104 DestroyLaserPointerView(); | 123 DestroyLaserPointerView(); |
| 105 } | 124 } |
| 106 | 125 |
| 107 if (!laser_pointer_view_ && enabled_) { | 126 if (!laser_pointer_view_ && enabled_) { |
| 108 laser_pointer_view_.reset(new LaserPointerView( | 127 laser_pointer_view_.reset(new LaserPointerView( |
| 109 base::TimeDelta::FromMilliseconds(kPointLifeDurationMs), root_window)); | 128 base::TimeDelta::FromMilliseconds(kPointLifeDurationMs), |
| 129 presentation_delay_, root_window)); |
| 110 } | 130 } |
| 111 } | 131 } |
| 112 | 132 |
| 113 void LaserPointerController::UpdateLaserPointerView( | 133 void LaserPointerController::UpdateLaserPointerView( |
| 114 aura::Window* current_window, | 134 aura::Window* current_window, |
| 115 const gfx::PointF& event_location, | 135 const gfx::PointF& event_location, |
| 116 ui::Event* event) { | 136 ui::Event* event) { |
| 117 SwitchTargetRootWindowIfNeeded(current_window); | 137 SwitchTargetRootWindowIfNeeded(current_window); |
| 118 current_stylus_location_ = event_location; | 138 current_stylus_location_ = event_location; |
| 119 laser_pointer_view_->AddNewPoint(current_stylus_location_); | 139 laser_pointer_view_->AddNewPoint(current_stylus_location_, |
| 140 event->time_stamp()); |
| 120 event->StopPropagation(); | 141 event->StopPropagation(); |
| 121 } | 142 } |
| 122 | 143 |
| 123 void LaserPointerController::DestroyLaserPointerView() { | 144 void LaserPointerController::DestroyLaserPointerView() { |
| 124 // |stationary_timer_| should also be stopped so that it does not attempt to | 145 // |stationary_timer_| should also be stopped so that it does not attempt to |
| 125 // add points when |laser_pointer_view_| is null. | 146 // add points when |laser_pointer_view_| is null. |
| 126 stationary_timer_->Stop(); | 147 stationary_timer_->Stop(); |
| 127 if (laser_pointer_view_) { | 148 if (laser_pointer_view_) { |
| 128 is_fading_away_ = false; | 149 is_fading_away_ = false; |
| 129 laser_pointer_view_.reset(); | 150 laser_pointer_view_.reset(); |
| 130 } | 151 } |
| 131 } | 152 } |
| 132 | 153 |
| 133 void LaserPointerController::RestartTimer() { | 154 void LaserPointerController::RestartTimer() { |
| 134 stationary_timer_repeat_count_ = 0; | 155 stationary_timer_repeat_count_ = 0; |
| 135 stationary_timer_->Reset(); | 156 stationary_timer_->Reset(); |
| 136 } | 157 } |
| 137 | 158 |
| 138 void LaserPointerController::AddStationaryPoint() { | 159 void LaserPointerController::AddStationaryPoint() { |
| 139 if (is_fading_away_) | 160 if (is_fading_away_) { |
| 140 laser_pointer_view_->UpdateTime(); | 161 laser_pointer_view_->UpdateTime(); |
| 141 else | 162 } else { |
| 142 laser_pointer_view_->AddNewPoint(current_stylus_location_); | 163 laser_pointer_view_->AddNewPoint(current_stylus_location_, |
| 164 ui::EventTimeForNow()); |
| 165 } |
| 143 | 166 |
| 144 // We can stop repeating the timer once the stylus has been stationary for | 167 // We can stop repeating the timer once the stylus has been stationary for |
| 145 // longer than the life of a point. | 168 // longer than the life of a point. |
| 146 if (stationary_timer_repeat_count_ * kAddStationaryPointsDelayMs >= | 169 if (stationary_timer_repeat_count_ * kAddStationaryPointsDelayMs >= |
| 147 kPointLifeDurationMs) { | 170 kPointLifeDurationMs) { |
| 148 stationary_timer_->Stop(); | 171 stationary_timer_->Stop(); |
| 149 // Reset the view if the timer expires and the view was in process of fading | 172 // Reset the view if the timer expires and the view was in process of fading |
| 150 // away. | 173 // away. |
| 151 if (is_fading_away_) | 174 if (is_fading_away_) |
| 152 DestroyLaserPointerView(); | 175 DestroyLaserPointerView(); |
| 153 } | 176 } |
| 154 stationary_timer_repeat_count_++; | 177 stationary_timer_repeat_count_++; |
| 155 } | 178 } |
| 156 } // namespace ash | 179 } // namespace ash |
| OLD | NEW |