Adding Test Fixture for initial test cases for the App Remoting Test Driver. Also...
[chromium-blink-merge.git] / ash / rotator / screen_rotation_animator.cc
blob6264db7d04cb47b5e538c9c187d2df000222fb53
1 // Copyright 2015 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "ash/rotator/screen_rotation_animator.h"
7 #include <string>
8 #include <vector>
10 #include "ash/ash_switches.h"
11 #include "ash/display/display_controller.h"
12 #include "ash/display/display_info.h"
13 #include "ash/display/display_manager.h"
14 #include "ash/rotator/screen_rotation_animation.h"
15 #include "ash/shell.h"
16 #include "base/command_line.h"
17 #include "base/time/time.h"
18 #include "ui/aura/window.h"
19 #include "ui/compositor/layer.h"
20 #include "ui/compositor/layer_animation_observer.h"
21 #include "ui/compositor/layer_animation_sequence.h"
22 #include "ui/compositor/layer_animator.h"
23 #include "ui/compositor/layer_owner.h"
24 #include "ui/compositor/layer_tree_owner.h"
25 #include "ui/gfx/animation/tween.h"
26 #include "ui/gfx/display.h"
27 #include "ui/gfx/geometry/point.h"
28 #include "ui/gfx/geometry/rect.h"
29 #include "ui/gfx/geometry/rect_f.h"
30 #include "ui/gfx/transform.h"
31 #include "ui/gfx/transform_util.h"
32 #include "ui/wm/core/window_util.h"
34 namespace ash {
36 namespace {
38 // Switch value for an animation that will rotate the initial orientation's
39 // layer towards the new orientation and the new orientation's layer in to
40 // position from the initial orientation through an arc of
41 // |kPartialRotationDegrees| degrees. The initial orientation's layer will be
42 // faded out as well.
43 const char kPartialRotation[] = "partial-rotation";
45 // Switch value for the same animation as |kPartialRotation| but with a much
46 // longer duration.
47 const char kPartialRotationSlow[] = "partial-rotation-slow";
49 // Switch value for an animation that will rotate both the initial and target
50 // orientation's layers from the initial orientation into the target
51 // orientation. The initial orientation's layer will be faded out as well and an
52 // interpolated scaling factor is applied to the animation so that the initial
53 // and target layers are both the same size throughout the animation.
54 const char kFullRotation[] = "full-rotation";
56 // Switch value for the same animation as |kFullRotation| but with a much longer
57 // duration.
58 const char kFullRotationSlow[] = "full-rotation-slow";
60 // The number of degrees the partial rotation animations animate through.
61 const int kPartialRotationDegrees = 20;
63 // The time it takes for the normal speed rotation animations to run.
64 const int kRotationDurationInMs = 250;
66 // The time it takes for the slow speed rotation animations to run.
67 const int kRotationDurationSlowInMs = 2500;
69 // Gets the current display rotation for the display with the specified
70 // |display_id|.
71 gfx::Display::Rotation GetCurrentRotation(int64 display_id) {
72 return Shell::GetInstance()
73 ->display_manager()
74 ->GetDisplayInfo(display_id)
75 .rotation();
78 // Returns true if the rotation between |initial_rotation| and |new_rotation| is
79 // 180 degrees.
80 bool Is180DegreeFlip(gfx::Display::Rotation initial_rotation,
81 gfx::Display::Rotation new_rotation) {
82 return (initial_rotation + 2) % 4 == new_rotation;
85 // A LayerAnimationObserver that will destroy the contained LayerTreeOwner when
86 // notified that a layer animation has ended or was aborted.
87 class LayerCleanupObserver : public ui::LayerAnimationObserver {
88 public:
89 explicit LayerCleanupObserver(
90 scoped_ptr<ui::LayerTreeOwner> layer_tree_owner);
91 ~LayerCleanupObserver() override;
93 // Get the root layer of the owned layer tree.
94 ui::Layer* GetRootLayer();
96 // ui::LayerAnimationObserver:
97 void OnLayerAnimationEnded(ui::LayerAnimationSequence* sequence) override;
98 void OnLayerAnimationAborted(ui::LayerAnimationSequence* sequence) override;
99 void OnLayerAnimationScheduled(
100 ui::LayerAnimationSequence* sequence) override {}
102 protected:
103 // ui::LayerAnimationObserver:
104 bool RequiresNotificationWhenAnimatorDestroyed() const override {
105 return true;
107 void OnAttachedToSequence(ui::LayerAnimationSequence* sequence) override;
108 void OnDetachedFromSequence(ui::LayerAnimationSequence* sequence) override;
110 private:
111 // The owned layer tree.
112 scoped_ptr<ui::LayerTreeOwner> layer_tree_owner_;
114 // The LayerAnimationSequence that |this| has been attached to. Defaults to
115 // nullptr.
116 ui::LayerAnimationSequence* sequence_;
118 DISALLOW_COPY_AND_ASSIGN(LayerCleanupObserver);
121 LayerCleanupObserver::LayerCleanupObserver(
122 scoped_ptr<ui::LayerTreeOwner> layer_tree_owner)
123 : layer_tree_owner_(layer_tree_owner.Pass()), sequence_(nullptr) {
126 LayerCleanupObserver::~LayerCleanupObserver() {
127 // We must eplicitly detach from |sequence_| because we return true from
128 // RequiresNotificationWhenAnimatorDestroyed.
129 if (sequence_)
130 sequence_->RemoveObserver(this);
133 ui::Layer* LayerCleanupObserver::GetRootLayer() {
134 return layer_tree_owner_->root();
137 void LayerCleanupObserver::OnLayerAnimationEnded(
138 ui::LayerAnimationSequence* sequence) {
139 delete this;
142 void LayerCleanupObserver::OnLayerAnimationAborted(
143 ui::LayerAnimationSequence* sequence) {
144 delete this;
147 void LayerCleanupObserver::OnAttachedToSequence(
148 ui::LayerAnimationSequence* sequence) {
149 sequence_ = sequence;
152 void LayerCleanupObserver::OnDetachedFromSequence(
153 ui::LayerAnimationSequence* sequence) {
154 DCHECK_EQ(sequence, sequence_);
155 sequence_ = nullptr;
158 // Set the screen orientation for the given |display| to |new_rotation| and
159 // animate the change.
160 void RotateScreen(int64 display_id,
161 gfx::Display::Rotation new_rotation,
162 base::TimeDelta duration,
163 int rotation_degrees,
164 int rotation_degree_offset,
165 gfx::Tween::Type tween_type,
166 bool should_scale) {
167 aura::Window* root_window =
168 Shell::GetInstance()->display_controller()->GetRootWindowForDisplayId(
169 display_id);
171 const gfx::Display::Rotation initial_orientation =
172 GetCurrentRotation(display_id);
174 const gfx::RectF original_screen_bounds = root_window->GetTargetBounds();
175 // 180 degree rotations should animate clock-wise.
176 const int rotation_factor =
177 (initial_orientation + 3) % 4 == new_rotation ? 1 : -1;
179 scoped_ptr<ui::LayerTreeOwner> old_layer_tree =
180 wm::RecreateLayers(root_window);
182 // Add the cloned layer tree in to the root, so it will be rendered.
183 root_window->layer()->Add(old_layer_tree->root());
184 root_window->layer()->StackAtTop(old_layer_tree->root());
186 scoped_ptr<LayerCleanupObserver> layer_cleanup_observer(
187 new LayerCleanupObserver(old_layer_tree.Pass()));
189 Shell::GetInstance()->display_manager()->SetDisplayRotation(display_id,
190 new_rotation);
192 const gfx::RectF rotated_screen_bounds = root_window->GetTargetBounds();
193 const gfx::Point pivot = gfx::Point(rotated_screen_bounds.width() / 2,
194 rotated_screen_bounds.height() / 2);
196 gfx::Point3F new_layer_initial_scale = gfx::Point3F(1.0f, 1.0f, 1.0f);
197 gfx::Point3F old_layer_target_scale = gfx::Point3F(1.0f, 1.0f, 1.0f);
199 if (should_scale) {
200 new_layer_initial_scale = gfx::Point3F(
201 original_screen_bounds.width() / rotated_screen_bounds.width(),
202 original_screen_bounds.height() / rotated_screen_bounds.height(), 1.0f);
204 old_layer_target_scale = gfx::Point3F(
205 rotated_screen_bounds.width() / original_screen_bounds.width(),
206 rotated_screen_bounds.height() / original_screen_bounds.height(), 1.0f);
209 // We must animate each non-cloned child layer individually because the cloned
210 // layer was added as a child to |root_window|'s layer so that it will be
211 // rendered.
212 // TODO(bruthig): Add a NOT_DRAWN layer in between the root_window's layer and
213 // its current children so that we only need to initiate two
214 // LayerAnimationSequences. One for the new layers and one for the old layer.
215 for (ui::Layer* child_layer : root_window->layer()->children()) {
216 // Skip the cloned layer because it has a different animation.
217 if (child_layer == layer_cleanup_observer->GetRootLayer())
218 continue;
220 scoped_ptr<ScreenRotationAnimation> screen_rotation(
221 new ScreenRotationAnimation(
222 child_layer, rotation_degrees * rotation_factor,
223 0 /* end_degrees */, child_layer->opacity(),
224 1.0f /* target_opacity */, new_layer_initial_scale,
225 gfx::Point3F(1.0f, 1.0f, 1.0f), pivot, duration, tween_type));
227 ui::LayerAnimator* animator = child_layer->GetAnimator();
228 animator->set_preemption_strategy(
229 ui::LayerAnimator::REPLACE_QUEUED_ANIMATIONS);
230 scoped_ptr<ui::LayerAnimationSequence> animation_sequence(
231 new ui::LayerAnimationSequence(screen_rotation.release()));
232 animator->StartAnimation(animation_sequence.release());
235 // The old layer will also be transformed into the new orientation. We will
236 // translate it so that the old layer's center point aligns with the new
237 // orientation's center point and use that center point as the pivot for the
238 // rotation animation.
239 gfx::Transform translate_transform;
240 translate_transform.Translate(
241 (rotated_screen_bounds.width() - original_screen_bounds.width()) / 2,
242 (rotated_screen_bounds.height() - original_screen_bounds.height()) / 2);
243 layer_cleanup_observer->GetRootLayer()->SetTransform(translate_transform);
245 scoped_ptr<ScreenRotationAnimation> screen_rotation(
246 new ScreenRotationAnimation(
247 layer_cleanup_observer->GetRootLayer(),
248 (rotation_degrees + rotation_degree_offset) * rotation_factor,
249 rotation_degree_offset * rotation_factor,
250 layer_cleanup_observer->GetRootLayer()->opacity(),
251 0.0f /* target_opacity */, gfx::Point3F(1.0f, 1.0f, 1.0f),
252 old_layer_target_scale, pivot, duration, tween_type));
254 ui::LayerAnimator* animator =
255 layer_cleanup_observer->GetRootLayer()->GetAnimator();
256 animator->set_preemption_strategy(
257 ui::LayerAnimator::REPLACE_QUEUED_ANIMATIONS);
258 scoped_ptr<ui::LayerAnimationSequence> animation_sequence(
259 new ui::LayerAnimationSequence(screen_rotation.release()));
260 // Add an observer so that the cloned layers can be cleaned up with the
261 // animation completes/aborts.
262 animation_sequence->AddObserver(layer_cleanup_observer.release());
263 animator->StartAnimation(animation_sequence.release());
266 } // namespace
268 ScreenRotationAnimator::ScreenRotationAnimator(int64 display_id)
269 : display_id_(display_id) {
272 ScreenRotationAnimator::~ScreenRotationAnimator() {
275 void ScreenRotationAnimator::Rotate(gfx::Display::Rotation new_rotation) {
276 const gfx::Display::Rotation current_rotation =
277 GetCurrentRotation(display_id_);
279 if (current_rotation == new_rotation)
280 return;
282 const std::string switch_value =
283 base::CommandLine::ForCurrentProcess()->GetSwitchValueASCII(
284 switches::kAshEnableScreenRotationAnimation);
286 if (switch_value.empty()) {
287 Shell::GetInstance()->display_manager()->SetDisplayRotation(display_id_,
288 new_rotation);
289 return;
292 if (kPartialRotation == switch_value ||
293 kPartialRotationSlow == switch_value) {
294 const int durationInMs = (kPartialRotation == switch_value)
295 ? kRotationDurationInMs
296 : kRotationDurationSlowInMs;
297 const int rotation_degree_offset =
298 Is180DegreeFlip(current_rotation, new_rotation)
299 ? 180 - kPartialRotationDegrees
300 : 90 - kPartialRotationDegrees;
302 RotateScreen(display_id_, new_rotation,
303 base::TimeDelta::FromMilliseconds(durationInMs),
304 kPartialRotationDegrees, rotation_degree_offset,
305 gfx::Tween::FAST_OUT_LINEAR_IN, false /* should_scale */);
306 } else if (kFullRotation == switch_value ||
307 kFullRotationSlow == switch_value) {
308 const int durationInMs = (kFullRotation == switch_value)
309 ? kRotationDurationInMs
310 : kRotationDurationSlowInMs;
311 const int rotation_degrees =
312 Is180DegreeFlip(current_rotation, new_rotation) ? 180 : 90;
314 RotateScreen(display_id_, new_rotation,
315 base::TimeDelta::FromMilliseconds(durationInMs),
316 rotation_degrees, 0 /* rotation_degree_offset */,
317 gfx::Tween::FAST_OUT_LINEAR_IN, true /* should_scale */);
318 } else {
319 NOTREACHED();
323 } // namespace ash