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