Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(618)

Side by Side Diff: ash/content/display/screen_orientation_controller_chromeos.cc

Issue 759063002: Move Screen Rotation from MaximizeModeController to ScreenOrientationController (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Change similarity to account for moved files Created 6 years ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 // Copyright 2014 The Chromium Authors. All rights reserved. 1 // Copyright 2014 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/content/display/screen_orientation_delegate_chromeos.h" 5 #include "ash/content/display/screen_orientation_controller_chromeos.h"
6 6
7 #include "ash/accelerometer/accelerometer_controller.h"
7 #include "ash/ash_switches.h" 8 #include "ash/ash_switches.h"
8 #include "ash/display/display_info.h" 9 #include "ash/display/display_info.h"
9 #include "ash/display/display_manager.h" 10 #include "ash/display/display_manager.h"
10 #include "ash/shell.h" 11 #include "ash/shell.h"
11 #include "ash/wm/maximize_mode/maximize_mode_controller.h" 12 #include "ash/wm/maximize_mode/maximize_mode_controller.h"
13 #include "base/auto_reset.h"
12 #include "base/command_line.h" 14 #include "base/command_line.h"
13 #include "content/public/browser/screen_orientation_provider.h" 15 #include "content/public/browser/screen_orientation_provider.h"
14 #include "content/public/browser/web_contents.h" 16 #include "content/public/browser/web_contents.h"
15 #include "ui/aura/window.h" 17 #include "ui/aura/window.h"
16 #include "ui/gfx/display.h" 18 #include "ui/gfx/display.h"
17 #include "ui/gfx/geometry/size.h" 19 #include "ui/gfx/geometry/size.h"
18 20
19 namespace { 21 namespace {
20 22
23 // The maximum deviation from the acceleration expected due to gravity under
24 // which to detect hinge angle and screen rotation in m/s^2
25 const float kDeviationFromGravityThreshold = 1.0f;
26
27 // The angle which the screen has to be rotated past before the display will
28 // rotate to match it (i.e. 45.0f is no stickiness).
29 const float kDisplayRotationStickyAngleDegrees = 60.0f;
30
31 // The mean acceleration due to gravity on Earth in m/s^2.
32 const float kMeanGravity = 9.80665f;
33
34 // The minimum acceleration in m/s^2 in a direction required to trigger screen
35 // rotation. This prevents rapid toggling of rotation when the device is near
36 // flat and there is very little screen aligned force on it. The value is
37 // effectively the sine of the rise angle required times the acceleration due
38 // to gravity, with the current value requiring at least a 25 degree rise.
39 const float kMinimumAccelerationScreenRotation = 4.2f;
40
41 const float kRadiansToDegrees = 180.0f / 3.14159265f;
42
21 blink::WebScreenOrientationLockType GetDisplayNaturalOrientation() { 43 blink::WebScreenOrientationLockType GetDisplayNaturalOrientation() {
22 ash::DisplayManager* display_manager = 44 ash::DisplayManager* display_manager =
23 ash::Shell::GetInstance()->display_manager(); 45 ash::Shell::GetInstance()->display_manager();
24 if (!display_manager->HasInternalDisplay()) 46 if (!display_manager->HasInternalDisplay())
25 return blink::WebScreenOrientationLockLandscape; 47 return blink::WebScreenOrientationLockLandscape;
26 48
27 ash::DisplayInfo info = 49 ash::DisplayInfo info =
28 display_manager->GetDisplayInfo(gfx::Display::InternalDisplayId()); 50 display_manager->GetDisplayInfo(gfx::Display::InternalDisplayId());
29 gfx::Size size = info.size_in_pixel(); 51 gfx::Size size = info.size_in_pixel();
30 switch (info.rotation()) { 52 switch (info.rotation()) {
31 case gfx::Display::ROTATE_0: 53 case gfx::Display::ROTATE_0:
32 case gfx::Display::ROTATE_180: 54 case gfx::Display::ROTATE_180:
33 return size.height() >= size.width() 55 return size.height() >= size.width()
34 ? blink::WebScreenOrientationLockPortrait 56 ? blink::WebScreenOrientationLockPortrait
35 : blink::WebScreenOrientationLockLandscape; 57 : blink::WebScreenOrientationLockLandscape;
36 case gfx::Display::ROTATE_90: 58 case gfx::Display::ROTATE_90:
37 case gfx::Display::ROTATE_270: 59 case gfx::Display::ROTATE_270:
38 return size.height() < size.width() 60 return size.height() < size.width()
39 ? blink::WebScreenOrientationLockPortrait 61 ? blink::WebScreenOrientationLockPortrait
40 : blink::WebScreenOrientationLockLandscape; 62 : blink::WebScreenOrientationLockLandscape;
41 } 63 }
42 NOTREACHED(); 64 NOTREACHED();
43 return blink::WebScreenOrientationLockLandscape; 65 return blink::WebScreenOrientationLockLandscape;
44 } 66 }
45 67
68 // Returns the angle between |base| and |other| in degrees.
69 float AngleBetweenVectorsInDegrees(const gfx::Vector3dF& base,
70 const gfx::Vector3dF& other) {
71 return acos(gfx::DotProduct(base, other) / base.Length() / other.Length()) *
72 kRadiansToDegrees;
73 }
74
75 // Returns the clockwise angle between |base| and |other| where |normal| is the
76 // normal of the virtual surface to measure clockwise according to.
77 float ClockwiseAngleBetweenVectorsInDegrees(const gfx::Vector3dF& base,
flackr 2014/12/12 20:46:06 These are still used in maximize_mode_controller a
jonross 2015/01/06 19:56:10 Done.
78 const gfx::Vector3dF& other,
79 const gfx::Vector3dF& normal) {
80 float angle = AngleBetweenVectorsInDegrees(base, other);
81 gfx::Vector3dF cross(base);
82 cross.Cross(other);
83
84 // If the dot product of this cross product is normal, it means that the
85 // shortest angle between |base| and |other| was counterclockwise with respect
86 // to the surface represented by |normal| and this angle must be reversed.
87 if (gfx::DotProduct(cross, normal) > 0.0f)
88 angle = 360.0f - angle;
89 return angle;
90 }
91
46 } // namespace 92 } // namespace
47 93
48 namespace ash { 94 namespace ash {
49 95
50 ScreenOrientationDelegate::ScreenOrientationDelegate() 96 ScreenOrientationController::ScreenOrientationController()
51 : locking_window_(NULL), 97 : locking_window_(NULL),
52 natural_orientation_(GetDisplayNaturalOrientation()) { 98 natural_orientation_(GetDisplayNaturalOrientation()),
99 ignore_display_configuration_updates_(false),
100 rotation_locked_(false),
101 user_rotation_(gfx::Display::ROTATE_0),
102 current_rotation_(gfx::Display::ROTATE_0) {
53 content::ScreenOrientationProvider::SetDelegate(this); 103 content::ScreenOrientationProvider::SetDelegate(this);
104 Shell::GetInstance()->AddShellObserver(this);
54 } 105 }
55 106
56 ScreenOrientationDelegate::~ScreenOrientationDelegate() { 107 ScreenOrientationController::~ScreenOrientationController() {
57 content::ScreenOrientationProvider::SetDelegate(NULL); 108 content::ScreenOrientationProvider::SetDelegate(NULL);
109 Shell::GetInstance()->RemoveShellObserver(this);
110 Shell::GetInstance()->accelerometer_controller()->RemoveObserver(this);
111 Shell::GetInstance()->display_controller()->RemoveObserver(this);
58 } 112 }
59 113
60 bool ScreenOrientationDelegate::FullScreenRequired( 114 void ScreenOrientationController::AddObserver(Observer* observer) {
115 observers_.AddObserver(observer);
116 }
117
118 void ScreenOrientationController::RemoveObserver(Observer* observer) {
119 observers_.RemoveObserver(observer);
120 }
121
122 void ScreenOrientationController::SetRotationLocked(bool rotation_locked) {
123 if (rotation_locked_ == rotation_locked)
124 return;
125 rotation_locked_ = rotation_locked;
126 FOR_EACH_OBSERVER(Observer, observers_,
127 OnRotationLockChanged(rotation_locked_));
128 DisplayManager* display_manager = Shell::GetInstance()->display_manager();
129 if (!display_manager->HasInternalDisplay())
130 return;
131 base::AutoReset<bool> auto_ignore_display_configuration_updates(
132 &ignore_display_configuration_updates_, true);
133 display_manager->RegisterDisplayRotationProperties(rotation_locked_,
134 current_rotation_);
135 }
136
137 void ScreenOrientationController::SetDisplayRotation(
138 gfx::Display::Rotation rotation) {
139 DisplayManager* display_manager = Shell::GetInstance()->display_manager();
140 if (!display_manager->HasInternalDisplay())
141 return;
142 current_rotation_ = rotation;
143 base::AutoReset<bool> auto_ignore_display_configuration_updates(
144 &ignore_display_configuration_updates_, true);
145 display_manager->SetDisplayRotation(gfx::Display::InternalDisplayId(),
146 rotation);
147 }
148
149 void ScreenOrientationController::OnAccelerometerUpdated(
150 const ui::AccelerometerUpdate& update) {
151 if (rotation_locked_)
152 return;
153 if (!update.has(ui::ACCELEROMETER_SOURCE_SCREEN))
154 return;
155 // Ignore the reading if it appears unstable. The reading is considered
156 // unstable if it deviates too much from gravity
157 if (!(std::abs(update.get(ui::ACCELEROMETER_SOURCE_SCREEN).Length() -
158 kMeanGravity) <= kDeviationFromGravityThreshold)) {
oshima 2014/12/12 19:59:53 this one too?
jonross 2015/01/06 19:56:10 Done.
159 return;
160 }
161 HandleScreenRotation(update.get(ui::ACCELEROMETER_SOURCE_SCREEN));
162 }
163
164 bool ScreenOrientationController::FullScreenRequired(
61 content::WebContents* web_contents) { 165 content::WebContents* web_contents) {
62 return true; 166 return true;
63 } 167 }
64 168
65 void ScreenOrientationDelegate::Lock( 169 void ScreenOrientationController::Lock(
66 content::WebContents* web_contents, 170 content::WebContents* web_contents,
67 blink::WebScreenOrientationLockType lock_orientation) { 171 blink::WebScreenOrientationLockType lock_orientation) {
68 aura::Window* requesting_window = web_contents->GetNativeView(); 172 aura::Window* requesting_window = web_contents->GetNativeView();
69
70 // TODO(jonross): Make ScreenOrientationDelegate responsible for rotation
71 // lock. Have MaximizeModeController, and TrayRotationLock both use it
72 // instead.
73 MaximizeModeController* controller =
74 Shell::GetInstance()->maximize_mode_controller();
75
76 // TODO(jonross): Track one rotation lock per window. When the active window 173 // TODO(jonross): Track one rotation lock per window. When the active window
77 // changes apply any corresponding rotation lock. 174 // changes apply any corresponding rotation lock.
78 if (!locking_window_) 175 if (!locking_window_)
79 locking_window_ = requesting_window; 176 locking_window_ = requesting_window;
80 else if (requesting_window != locking_window_) 177 else if (requesting_window != locking_window_)
81 return; 178 return;
82 179
83 switch (lock_orientation) { 180 switch (lock_orientation) {
84 case blink::WebScreenOrientationLockAny: 181 case blink::WebScreenOrientationLockAny:
85 controller->SetRotationLocked(false); 182 SetRotationLocked(false);
86 locking_window_ = NULL; 183 locking_window_ = NULL;
87 break; 184 break;
88 case blink::WebScreenOrientationLockDefault: 185 case blink::WebScreenOrientationLockDefault:
89 NOTREACHED(); 186 NOTREACHED();
90 break; 187 break;
91 case blink::WebScreenOrientationLockPortraitPrimary: 188 case blink::WebScreenOrientationLockPortraitPrimary:
92 LockRotationToPrimaryOrientation(blink::WebScreenOrientationLockPortrait); 189 LockRotationToPrimaryOrientation(blink::WebScreenOrientationLockPortrait);
93 break; 190 break;
94 case blink::WebScreenOrientationLockLandscape: 191 case blink::WebScreenOrientationLockLandscape:
95 case blink::WebScreenOrientationLockPortrait: 192 case blink::WebScreenOrientationLockPortrait:
96 LockToRotationMatchingOrientation(lock_orientation); 193 LockToRotationMatchingOrientation(lock_orientation);
97 break; 194 break;
98 case blink::WebScreenOrientationLockPortraitSecondary: 195 case blink::WebScreenOrientationLockPortraitSecondary:
99 LockRotationToSecondaryOrientation( 196 LockRotationToSecondaryOrientation(
100 blink::WebScreenOrientationLockPortrait); 197 blink::WebScreenOrientationLockPortrait);
101 break; 198 break;
102 case blink::WebScreenOrientationLockLandscapeSecondary: 199 case blink::WebScreenOrientationLockLandscapeSecondary:
103 LockRotationToSecondaryOrientation( 200 LockRotationToSecondaryOrientation(
104 blink::WebScreenOrientationLockLandscape); 201 blink::WebScreenOrientationLockLandscape);
105 break; 202 break;
106 case blink::WebScreenOrientationLockLandscapePrimary: 203 case blink::WebScreenOrientationLockLandscapePrimary:
107 LockRotationToPrimaryOrientation( 204 LockRotationToPrimaryOrientation(
108 blink::WebScreenOrientationLockLandscape); 205 blink::WebScreenOrientationLockLandscape);
109 break; 206 break;
110 case blink::WebScreenOrientationLockNatural: 207 case blink::WebScreenOrientationLockNatural:
111 controller->LockRotation(gfx::Display::ROTATE_0); 208 LockRotation(gfx::Display::ROTATE_0);
112 break; 209 break;
113 default: 210 default:
114 NOTREACHED(); 211 NOTREACHED();
115 break; 212 break;
116 } 213 }
117 } 214 }
118 215
119 bool ScreenOrientationDelegate::ScreenOrientationProviderSupported() { 216 bool ScreenOrientationController::ScreenOrientationProviderSupported() {
120 return Shell::GetInstance() 217 return Shell::GetInstance()
121 ->maximize_mode_controller() 218 ->maximize_mode_controller()
122 ->IsMaximizeModeWindowManagerEnabled() && 219 ->IsMaximizeModeWindowManagerEnabled() &&
123 CommandLine::ForCurrentProcess()->HasSwitch( 220 CommandLine::ForCurrentProcess()->HasSwitch(
124 switches::kAshEnableTouchViewTesting); 221 switches::kAshEnableTouchViewTesting);
125 } 222 }
126 223
127 void ScreenOrientationDelegate::Unlock(content::WebContents* web_contents) { 224 void ScreenOrientationController::Unlock(content::WebContents* web_contents) {
128 aura::Window* requesting_window = web_contents->GetNativeView(); 225 aura::Window* requesting_window = web_contents->GetNativeView();
129 if (requesting_window != locking_window_) 226 if (requesting_window != locking_window_)
130 return; 227 return;
131 locking_window_ = NULL; 228 locking_window_ = NULL;
132 Shell::GetInstance()->maximize_mode_controller()->SetRotationLocked(false); 229 SetRotationLocked(false);
133 } 230 }
134 231
135 void ScreenOrientationDelegate::LockRotationToPrimaryOrientation( 232 void ScreenOrientationController::OnDisplayConfigurationChanged() {
136 blink::WebScreenOrientationLockType lock_orientation) { 233 if (ignore_display_configuration_updates_)
137 Shell::GetInstance()->maximize_mode_controller()->LockRotation( 234 return;
138 natural_orientation_ == lock_orientation ? gfx::Display::ROTATE_0 235 gfx::Display::Rotation user_rotation =
139 : gfx::Display::ROTATE_90); 236 Shell::GetInstance()
237 ->display_manager()
238 ->GetDisplayInfo(gfx::Display::InternalDisplayId())
239 .rotation();
240 if (user_rotation != current_rotation_) {
241 // A user may change other display configuration settings. When the user
242 // does change the rotation setting, then lock rotation to prevent the
243 // accelerometer from erasing their change.
244 SetRotationLocked(true);
245 user_rotation_ = current_rotation_ = user_rotation;
246 }
140 } 247 }
141 248
142 void ScreenOrientationDelegate::LockRotationToSecondaryOrientation( 249 void ScreenOrientationController::OnMaximizeModeStarted() {
143 blink::WebScreenOrientationLockType lock_orientation) { 250 DisplayManager* display_manager = Shell::GetInstance()->display_manager();
144 Shell::GetInstance()->maximize_mode_controller()->LockRotation( 251 if (!display_manager->HasInternalDisplay())
145 natural_orientation_ == lock_orientation ? gfx::Display::ROTATE_180 252 return;
146 : gfx::Display::ROTATE_270); 253 current_rotation_ = user_rotation_ =
254 display_manager->GetDisplayInfo(gfx::Display::InternalDisplayId())
255 .rotation();
256 LoadDisplayRotationProperties();
257 Shell::GetInstance()->accelerometer_controller()->AddObserver(this);
flackr 2014/12/12 20:46:06 Hmm, this means the rotation update will be delaye
jonross 2015/01/06 19:56:10 Done.
258 Shell::GetInstance()->display_controller()->AddObserver(this);
147 } 259 }
148 260
149 void ScreenOrientationDelegate::LockToRotationMatchingOrientation( 261 void ScreenOrientationController::OnMaximizeModeEnded() {
262 if (!Shell::GetInstance()->display_manager()->HasInternalDisplay())
263 return;
264 Shell::GetInstance()->accelerometer_controller()->RemoveObserver(this);
265 Shell::GetInstance()->display_controller()->RemoveObserver(this);
266 if (current_rotation_ != user_rotation_)
267 SetDisplayRotation(user_rotation_);
268 }
269
270 void ScreenOrientationController::LockRotation(
271 gfx::Display::Rotation rotation) {
272 SetRotationLocked(true);
273 SetDisplayRotation(rotation);
274 }
275
276 void ScreenOrientationController::LockRotationToPrimaryOrientation(
277 blink::WebScreenOrientationLockType lock_orientation) {
278 LockRotation(natural_orientation_ == lock_orientation
279 ? gfx::Display::ROTATE_0
280 : gfx::Display::ROTATE_90);
281 }
282
283 void ScreenOrientationController::LockRotationToSecondaryOrientation(
284 blink::WebScreenOrientationLockType lock_orientation) {
285 LockRotation(natural_orientation_ == lock_orientation
286 ? gfx::Display::ROTATE_180
287 : gfx::Display::ROTATE_270);
288 }
289
290 void ScreenOrientationController::LockToRotationMatchingOrientation(
150 blink::WebScreenOrientationLockType lock_orientation) { 291 blink::WebScreenOrientationLockType lock_orientation) {
151 // TODO(jonross): Update MaximizeModeController to allow rotation between 292 // TODO(jonross): Update MaximizeModeController to allow rotation between
152 // two angles of an orientation (e.g. from ROTATE_0 to ROTATE_180, and from 293 // two angles of an orientation (e.g. from ROTATE_0 to ROTATE_180, and from
153 // ROTATE_90 to ROTATE_270) 294 // ROTATE_90 to ROTATE_270)
154 DisplayManager* display_manager = Shell::GetInstance()->display_manager(); 295 DisplayManager* display_manager = Shell::GetInstance()->display_manager();
155 if (!display_manager->HasInternalDisplay()) 296 if (!display_manager->HasInternalDisplay())
156 return; 297 return;
157 298
158 gfx::Display::Rotation rotation = 299 gfx::Display::Rotation rotation =
159 display_manager->GetDisplayInfo(gfx::Display::InternalDisplayId()) 300 display_manager->GetDisplayInfo(gfx::Display::InternalDisplayId())
160 .rotation(); 301 .rotation();
161 MaximizeModeController* controller =
162 Shell::GetInstance()->maximize_mode_controller();
163 if (natural_orientation_ == lock_orientation) { 302 if (natural_orientation_ == lock_orientation) {
164 if (rotation == gfx::Display::ROTATE_0 || 303 if (rotation == gfx::Display::ROTATE_0 ||
165 rotation == gfx::Display::ROTATE_180) { 304 rotation == gfx::Display::ROTATE_180) {
166 controller->SetRotationLocked(true); 305 SetRotationLocked(true);
167 } else { 306 } else {
168 controller->LockRotation(gfx::Display::ROTATE_0); 307 LockRotation(gfx::Display::ROTATE_0);
169 } 308 }
170 } else { 309 } else {
171 if (rotation == gfx::Display::ROTATE_90 || 310 if (rotation == gfx::Display::ROTATE_90 ||
172 rotation == gfx::Display::ROTATE_270) { 311 rotation == gfx::Display::ROTATE_270) {
173 controller->SetRotationLocked(true); 312 SetRotationLocked(true);
174 } else { 313 } else {
175 controller->LockRotation(gfx::Display::ROTATE_90); 314 LockRotation(gfx::Display::ROTATE_90);
176 } 315 }
177 } 316 }
178 } 317 }
179 318
319 void ScreenOrientationController::HandleScreenRotation(
320 const gfx::Vector3dF& lid) {
321 gfx::Vector3dF lid_flattened(lid.x(), lid.y(), 0.0f);
322 float lid_flattened_length = lid_flattened.Length();
323 // When the lid is close to being flat, don't change rotation as it is too
324 // sensitive to slight movements.
325 if (lid_flattened_length < kMinimumAccelerationScreenRotation)
326 return;
327
328 // The reference vector is the angle of gravity when the device is rotated
329 // clockwise by 45 degrees. Computing the angle between this vector and
330 // gravity we can easily determine the expected display rotation.
331 static const gfx::Vector3dF rotation_reference(-1.0f, -1.0f, 0.0f);
332
333 // Set the down vector to match the expected direction of gravity given the
334 // last configured rotation. This is used to enforce a stickiness that the
335 // user must overcome to rotate the display and prevents frequent rotations
336 // when holding the device near 45 degrees.
337 gfx::Vector3dF down(0.0f, 0.0f, 0.0f);
338 if (current_rotation_ == gfx::Display::ROTATE_0)
339 down.set_y(-1.0f);
340 else if (current_rotation_ == gfx::Display::ROTATE_90)
341 down.set_x(-1.0f);
342 else if (current_rotation_ == gfx::Display::ROTATE_180)
343 down.set_y(1.0f);
344 else
345 down.set_x(1.0f);
346
347 // Don't rotate if the screen has not passed the threshold.
348 if (AngleBetweenVectorsInDegrees(down, lid_flattened) <
349 kDisplayRotationStickyAngleDegrees) {
350 return;
351 }
352
353 float angle = ClockwiseAngleBetweenVectorsInDegrees(
354 rotation_reference, lid_flattened, gfx::Vector3dF(0.0f, 0.0f, -1.0f));
355
356 gfx::Display::Rotation new_rotation = gfx::Display::ROTATE_90;
357 if (angle < 90.0f)
358 new_rotation = gfx::Display::ROTATE_0;
359 else if (angle < 180.0f)
360 new_rotation = gfx::Display::ROTATE_270;
361 else if (angle < 270.0f)
362 new_rotation = gfx::Display::ROTATE_180;
363
364 if (new_rotation != current_rotation_)
365 SetDisplayRotation(new_rotation);
366 }
367
368 void ScreenOrientationController::LoadDisplayRotationProperties() {
369 DisplayManager* display_manager = Shell::GetInstance()->display_manager();
370 if (!display_manager->registered_internal_display_rotation_lock())
371 return;
372 SetDisplayRotation(display_manager->registered_internal_display_rotation());
373 SetRotationLocked(true);
374 }
375
180 } // namespace ash 376 } // namespace ash
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698