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

Side by Side Diff: ash/rotator/screen_rotation_animator.cc

Issue 2790583004: Add second copy request after screen rotation to flatten the layers in animation. (Closed)
Patch Set: Rebased and fixed a bug. Created 3 years, 8 months 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 2015 The Chromium Authors. All rights reserved. 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 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/rotator/screen_rotation_animator.h" 5 #include "ash/rotator/screen_rotation_animator.h"
6 6
7 #include <string>
8 #include <utility>
9 #include <vector>
10
11 #include "ash/ash_switches.h" 7 #include "ash/ash_switches.h"
12 #include "ash/display/window_tree_host_manager.h" 8 #include "ash/display/window_tree_host_manager.h"
13 #include "ash/public/cpp/shell_window_ids.h" 9 #include "ash/public/cpp/shell_window_ids.h"
14 #include "ash/rotator/screen_rotation_animation.h" 10 #include "ash/rotator/screen_rotation_animation.h"
15 #include "ash/rotator/screen_rotation_animator_observer.h" 11 #include "ash/rotator/screen_rotation_animator_observer.h"
16 #include "ash/shell.h" 12 #include "ash/shell.h"
13 #include "ash/utility/transformer_util.h"
17 #include "base/command_line.h" 14 #include "base/command_line.h"
18 #include "base/memory/ptr_util.h" 15 #include "base/memory/ptr_util.h"
19 #include "base/metrics/histogram_macros.h" 16 #include "base/metrics/histogram_macros.h"
20 #include "base/time/time.h" 17 #include "base/time/time.h"
21 #include "cc/output/copy_output_request.h" 18 #include "cc/output/copy_output_request.h"
22 #include "cc/output/copy_output_result.h" 19 #include "cc/output/copy_output_result.h"
23 #include "ui/aura/window.h" 20 #include "ui/aura/window.h"
21 #include "ui/compositor/callback_layer_animation_observer.h"
24 #include "ui/compositor/layer.h" 22 #include "ui/compositor/layer.h"
25 #include "ui/compositor/layer_animation_element.h" 23 #include "ui/compositor/layer_animation_element.h"
26 #include "ui/compositor/layer_animation_observer.h"
27 #include "ui/compositor/layer_animation_sequence.h" 24 #include "ui/compositor/layer_animation_sequence.h"
28 #include "ui/compositor/layer_animator.h" 25 #include "ui/compositor/layer_animator.h"
29 #include "ui/compositor/layer_owner.h" 26 #include "ui/compositor/layer_owner.h"
30 #include "ui/compositor/layer_tree_owner.h" 27 #include "ui/compositor/layer_tree_owner.h"
31 #include "ui/display/display.h" 28 #include "ui/display/display.h"
32 #include "ui/display/manager/display_manager.h" 29 #include "ui/display/manager/display_manager.h"
33 #include "ui/display/manager/managed_display_info.h" 30 #include "ui/display/manager/managed_display_info.h"
34 #include "ui/gfx/animation/tween.h" 31 #include "ui/gfx/animation/tween.h"
35 #include "ui/gfx/geometry/point.h" 32 #include "ui/gfx/geometry/point.h"
36 #include "ui/gfx/geometry/rect.h" 33 #include "ui/gfx/geometry/rect.h"
(...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after
70 return (initial_rotation + 3) % 4 == new_rotation 67 return (initial_rotation + 3) % 4 == new_rotation
71 ? kCounterClockWiseRotationFactor 68 ? kCounterClockWiseRotationFactor
72 : kClockWiseRotationFactor; 69 : kClockWiseRotationFactor;
73 } 70 }
74 71
75 aura::Window* GetRootWindow(int64_t display_id) { 72 aura::Window* GetRootWindow(int64_t display_id) {
76 return Shell::Get()->window_tree_host_manager()->GetRootWindowForDisplayId( 73 return Shell::Get()->window_tree_host_manager()->GetRootWindowForDisplayId(
77 display_id); 74 display_id);
78 } 75 }
79 76
77 aura::Window* GetScreenRotationContainer(aura::Window* root_window) {
78 return root_window->GetChildById(kShellWindowId_ScreenRotationContainer);
79 }
80
80 // Returns true if the rotation between |initial_rotation| and |new_rotation| is 81 // Returns true if the rotation between |initial_rotation| and |new_rotation| is
81 // 180 degrees. 82 // 180 degrees.
82 bool Is180DegreeFlip(display::Display::Rotation initial_rotation, 83 bool Is180DegreeFlip(display::Display::Rotation initial_rotation,
83 display::Display::Rotation new_rotation) { 84 display::Display::Rotation new_rotation) {
84 return (initial_rotation + 2) % 4 == new_rotation; 85 return (initial_rotation + 2) % 4 == new_rotation;
85 } 86 }
86 87
87 // Returns the initial degrees the old layer animation to begin with. 88 // Returns the initial degrees the old layer animation to begin with.
88 int GetInitialDegrees(display::Display::Rotation initial_rotation, 89 int GetInitialDegrees(display::Display::Rotation initial_rotation,
89 display::Display::Rotation new_rotation) { 90 display::Display::Rotation new_rotation) {
90 return (Is180DegreeFlip(initial_rotation, new_rotation) ? 180 : 90); 91 return (Is180DegreeFlip(initial_rotation, new_rotation) ? 180 : 90);
91 } 92 }
92 93
93 // A LayerAnimationObserver that will destroy the contained LayerTreeOwner 94 void AddLayerAtTopOfWindowLayers(aura::Window* root_window, ui::Layer* layer) {
94 // when notified that a layer animation has ended or was aborted. 95 // Add the cloned/copied layer tree into the root, so it will be rendered.
95 class LayerCleanupObserver : public ui::LayerAnimationObserver { 96 root_window->layer()->Add(layer);
96 public: 97 root_window->layer()->StackAtTop(layer);
97 // Takes WeakPtr of ScreenRotationAnimator. |this| may outlive the |animator_|
98 // instance and the |animator_| isn't detaching itself as an observer when
99 // being destroyed. However, ideally, when |animator_| is destroying,
100 // deleting |old_layer_tree_owner_| will trigger OnLayerAnimationAborted and
101 // delete |this| before |animator_| deleted.
102 explicit LayerCleanupObserver(base::WeakPtr<ScreenRotationAnimator> animator);
103 ~LayerCleanupObserver() override;
104
105 // ui::LayerAnimationObserver:
106 void OnLayerAnimationEnded(ui::LayerAnimationSequence* sequence) override;
107 void OnLayerAnimationAborted(ui::LayerAnimationSequence* sequence) override;
108 void OnLayerAnimationScheduled(
109 ui::LayerAnimationSequence* sequence) override {}
110
111 protected:
112 // ui::LayerAnimationObserver:
113 bool RequiresNotificationWhenAnimatorDestroyed() const override {
114 return true;
115 }
116 void OnAttachedToSequence(ui::LayerAnimationSequence* sequence) override;
117 void OnDetachedFromSequence(ui::LayerAnimationSequence* sequence) override;
118
119 private:
120 base::WeakPtr<ScreenRotationAnimator> animator_;
121
122 // The LayerAnimationSequence that |this| has been attached to. Defaults to
123 // nullptr.
124 ui::LayerAnimationSequence* sequence_;
125
126 DISALLOW_COPY_AND_ASSIGN(LayerCleanupObserver);
127 };
128
129 LayerCleanupObserver::LayerCleanupObserver(
130 base::WeakPtr<ScreenRotationAnimator> animator)
131 : animator_(animator), sequence_(nullptr) {}
132
133 LayerCleanupObserver::~LayerCleanupObserver() {
134 // We must eplicitly detach from |sequence_| because we return true from
135 // RequiresNotificationWhenAnimatorDestroyed.
136 if (sequence_)
137 sequence_->RemoveObserver(this);
138 } 98 }
139 99
140 void LayerCleanupObserver::OnLayerAnimationEnded( 100 void AddLayerBelowWindowLayer(aura::Window* root_window,
141 ui::LayerAnimationSequence* sequence) { 101 ui::Layer* top_layer,
142 if (animator_) 102 ui::Layer* layer) {
143 animator_->ProcessAnimationQueue(); 103 // Add the cloned/copied layer tree into the root, so it will be rendered.
144 104 root_window->layer()->Add(layer);
145 delete this; 105 root_window->layer()->StackBelow(layer, top_layer);
146 } 106 }
147 107
148 void LayerCleanupObserver::OnLayerAnimationAborted( 108 // The Callback will be invoked when all animation sequences have
149 ui::LayerAnimationSequence* sequence) { 109 // finished. |observer| will be destroyed after invoking the Callback if it
150 if (animator_) 110 // returns true.
151 animator_->ProcessAnimationQueue(); 111 bool AnimationEndedCallback(
152 112 base::WeakPtr<ScreenRotationAnimator> animator,
153 delete this; 113 const ui::CallbackLayerAnimationObserver& observer) {
114 if (animator)
115 animator->ProcessAnimationQueue();
116 return true;
154 } 117 }
155 118
156 void LayerCleanupObserver::OnAttachedToSequence( 119 // Creates a Transform for the old layer in screen rotation animation.
157 ui::LayerAnimationSequence* sequence) { 120 gfx::Transform CreateScreenRotationOldLayerTransformForDisplay(
158 sequence_ = sequence; 121 display::Display::Rotation old_rotation,
122 display::Display::Rotation new_rotation,
123 const display::Display& display) {
124 gfx::Transform inverse;
125 CHECK(CreateRotationTransform(old_rotation, new_rotation, display)
126 .GetInverse(&inverse));
127 return inverse;
159 } 128 }
160 129
161 void LayerCleanupObserver::OnDetachedFromSequence( 130 // The |request_id| changed since last copy request, which means a
162 ui::LayerAnimationSequence* sequence) { 131 // new rotation stated, we need to ignore this copy result.
163 DCHECK_EQ(sequence, sequence_); 132 bool IgnoreCopyResult(int64_t request_id, int64_t current_request_id) {
164 sequence_ = nullptr; 133 DCHECK(request_id <= current_request_id);
134 return request_id < current_request_id;
135 }
136
137 // In the following cases, abort rotation:
138 // 1) if the display was removed,
139 // 2) the copy request has been canceled or failed. It would fail if,
140 // for examples: a) The layer is removed from the compositor and destroye
141 // before committing the request to the compositor. b) The compositor is
142 // shutdown.
143 bool AbortRotation(int64_t display_id, cc::CopyOutputResult* result) {
144 return !IsDisplayIdValid(display_id) || result->IsEmpty();
165 } 145 }
166 146
167 class ScreenRotationAnimationMetricsReporter 147 class ScreenRotationAnimationMetricsReporter
168 : public ui::AnimationMetricsReporter { 148 : public ui::AnimationMetricsReporter {
169 public: 149 public:
170 ScreenRotationAnimationMetricsReporter() {} 150 ScreenRotationAnimationMetricsReporter() {}
171 ~ScreenRotationAnimationMetricsReporter() override {} 151 ~ScreenRotationAnimationMetricsReporter() override {}
172 152
173 void Report(int value) override { 153 void Report(int value) override {
174 UMA_HISTOGRAM_PERCENTAGE("Ash.Rotation.AnimationSmoothness", value); 154 UMA_HISTOGRAM_PERCENTAGE("Ash.Rotation.AnimationSmoothness", value);
175 } 155 }
176 156
177 private: 157 private:
178 DISALLOW_COPY_AND_ASSIGN(ScreenRotationAnimationMetricsReporter); 158 DISALLOW_COPY_AND_ASSIGN(ScreenRotationAnimationMetricsReporter);
179 }; 159 };
180 160
181 } // namespace 161 } // namespace
182 162
183 ScreenRotationAnimator::ScreenRotationAnimator(int64_t display_id) 163 ScreenRotationAnimator::ScreenRotationAnimator(int64_t display_id)
184 : display_id_(display_id), 164 : display_id_(display_id),
185 screen_rotation_state_(IDLE), 165 screen_rotation_state_(IDLE),
186 rotation_request_id_(0), 166 rotation_request_id_(0),
187 metrics_reporter_( 167 metrics_reporter_(
188 base::MakeUnique<ScreenRotationAnimationMetricsReporter>()), 168 base::MakeUnique<ScreenRotationAnimationMetricsReporter>()),
189 disable_animation_timers_for_test_(false), 169 disable_animation_timers_for_test_(false),
170 has_switch_ash_enable_smooth_screen_rotation_(
171 base::CommandLine::ForCurrentProcess()->HasSwitch(
172 switches::kAshEnableSmoothScreenRotation)),
173 root_window_(GetRootWindow(display_id_)),
174 screen_rotation_container_layer_(
175 GetScreenRotationContainer(root_window_)->layer()),
190 weak_factory_(this) {} 176 weak_factory_(this) {}
191 177
192 ScreenRotationAnimator::~ScreenRotationAnimator() { 178 ScreenRotationAnimator::~ScreenRotationAnimator() {
193 // To prevent a call to |LayerCleanupObserver::OnLayerAnimationAborted()| from 179 // To prevent a call to |LayerCleanupObserver::OnLayerAnimationAborted()| from
194 // calling a method on the |animator_|. 180 // calling a method on the |animator_|.
195 weak_factory_.InvalidateWeakPtrs(); 181 weak_factory_.InvalidateWeakPtrs();
196 182
197 // Explicitly reset the |old_layer_tree_owner_| and |metrics_reporter_| in 183 // Explicitly reset the |old_layer_tree_owner_| and |metrics_reporter_| in
198 // order to make sure |metrics_reporter_| outlives the attached animation 184 // order to make sure |metrics_reporter_| outlives the attached animation
199 // sequence. 185 // sequence.
200 old_layer_tree_owner_.reset(); 186 old_layer_tree_owner_.reset();
201 metrics_reporter_.reset(); 187 metrics_reporter_.reset();
202 } 188 }
203 189
204 void ScreenRotationAnimator::StartRotationAnimation( 190 void ScreenRotationAnimator::StartRotationAnimation(
205 std::unique_ptr<ScreenRotationRequest> rotation_request) { 191 std::unique_ptr<ScreenRotationRequest> rotation_request) {
206 if (base::CommandLine::ForCurrentProcess()->HasSwitch( 192 if (has_switch_ash_enable_smooth_screen_rotation_) {
207 switches::kAshEnableSmoothScreenRotation)) { 193 std::unique_ptr<cc::CopyOutputRequest> copy_output_request =
208 RequestCopyRootLayerAndAnimateRotation(std::move(rotation_request)); 194 cc::CopyOutputRequest::CreateRequest(
195 CreateAfterCopyCallbackBeforeRotation(std::move(rotation_request)));
196 RequestCopyScreenRotationContainerLayer(std::move(copy_output_request));
197 screen_rotation_state_ = COPY_REQUESTED;
209 } else { 198 } else {
210 CreateOldLayerTree(); 199 StartSlowAnimation(std::move(rotation_request));
211 AnimateRotation(std::move(rotation_request));
212 } 200 }
213 } 201 }
214 202
215 void ScreenRotationAnimator::RequestCopyRootLayerAndAnimateRotation( 203 void ScreenRotationAnimator::StartSlowAnimation(
216 std::unique_ptr<ScreenRotationRequest> rotation_request) { 204 std::unique_ptr<ScreenRotationRequest> rotation_request) {
217 std::unique_ptr<cc::CopyOutputRequest> copy_output_request = 205 CreateOldLayerTree();
218 cc::CopyOutputRequest::CreateRequest( 206 SetRotation(rotation_request->old_rotation, rotation_request->new_rotation,
219 CreateAfterCopyCallback(std::move(rotation_request))); 207 rotation_request->source);
220 ui::Layer* layer = GetRootWindow(display_id_)->layer(); 208 AnimateRotation(std::move(rotation_request));
221 copy_output_request->set_area(gfx::Rect(layer->size())); 209 }
222 layer->RequestCopyOfOutput(std::move(copy_output_request));
223 210
224 screen_rotation_state_ = COPY_REQUESTED; 211 void ScreenRotationAnimator::SetRotation(
212 display::Display::Rotation old_rotation,
213 display::Display::Rotation new_rotation,
214 display::Display::RotationSource source) {
215 Shell::Get()->display_manager()->SetDisplayRotation(display_id_, new_rotation,
216 source);
217 const display::Display display =
218 Shell::Get()->display_manager()->GetDisplayForId(display_id_);
219 old_layer_tree_owner_->root()->SetTransform(
220 CreateScreenRotationOldLayerTransformForDisplay(old_rotation,
221 new_rotation, display));
222 }
223
224 void ScreenRotationAnimator::RequestCopyScreenRotationContainerLayer(
225 std::unique_ptr<cc::CopyOutputRequest> copy_output_request) {
226 copy_output_request->set_area(
227 gfx::Rect(screen_rotation_container_layer_->size()));
228 screen_rotation_container_layer_->RequestCopyOfOutput(
229 std::move(copy_output_request));
225 } 230 }
226 231
227 ScreenRotationAnimator::CopyCallback 232 ScreenRotationAnimator::CopyCallback
228 ScreenRotationAnimator::CreateAfterCopyCallback( 233 ScreenRotationAnimator::CreateAfterCopyCallbackBeforeRotation(
229 std::unique_ptr<ScreenRotationRequest> rotation_request) { 234 std::unique_ptr<ScreenRotationRequest> rotation_request) {
230 return base::Bind(&ScreenRotationAnimator::OnRootLayerCopiedBeforeRotation, 235 return base::Bind(&ScreenRotationAnimator::
236 OnScreenRotationContainerLayerCopiedBeforeRotation,
231 weak_factory_.GetWeakPtr(), 237 weak_factory_.GetWeakPtr(),
232 base::Passed(&rotation_request)); 238 base::Passed(&rotation_request));
233 } 239 }
234 240
235 void ScreenRotationAnimator::OnRootLayerCopiedBeforeRotation( 241 ScreenRotationAnimator::CopyCallback
242 ScreenRotationAnimator::CreateAfterCopyCallbackAfterRotation(
243 std::unique_ptr<ScreenRotationRequest> rotation_request) {
244 return base::Bind(&ScreenRotationAnimator::
245 OnScreenRotationContainerLayerCopiedAfterRotation,
246 weak_factory_.GetWeakPtr(),
247 base::Passed(&rotation_request));
248 }
249
250 void ScreenRotationAnimator::OnScreenRotationContainerLayerCopiedBeforeRotation(
236 std::unique_ptr<ScreenRotationRequest> rotation_request, 251 std::unique_ptr<ScreenRotationRequest> rotation_request,
237 std::unique_ptr<cc::CopyOutputResult> result) { 252 std::unique_ptr<cc::CopyOutputResult> result) {
238 DCHECK(rotation_request->id <= rotation_request_id_); 253 if (IgnoreCopyResult(rotation_request->id, rotation_request_id_))
239 // The |rotation_request_id_| changed since last copy request, which means a
240 // new rotation stated, we need to ignore this copy result.
241 if (rotation_request->id < rotation_request_id_)
242 return; 254 return;
243 255 if (AbortRotation(display_id_, result.get())) {
244 // In the following cases, abort rotation:
245 // 1) if the display was removed,
246 // 2) the copy request has been canceled or failed. It would fail if,
247 // for examples: a) The layer is removed from the compositor and destroye
248 // before committing the request to the compositor. b) The compositor is
249 // shutdown.
250 if (!IsDisplayIdValid(display_id_) || result->IsEmpty()) {
251 ProcessAnimationQueue(); 256 ProcessAnimationQueue();
252 return; 257 return;
253 } 258 }
254 259
255 CopyOldLayerTree(std::move(result)); 260 old_layer_tree_owner_ = CopyLayerTree(std::move(result));
261 AddLayerAtTopOfWindowLayers(root_window_, old_layer_tree_owner_->root());
262 SetRotation(rotation_request->old_rotation, rotation_request->new_rotation,
263 rotation_request->source);
264 std::unique_ptr<cc::CopyOutputRequest> copy_output_request =
265 cc::CopyOutputRequest::CreateRequest(
266 CreateAfterCopyCallbackAfterRotation(std::move(rotation_request)));
267 RequestCopyScreenRotationContainerLayer(std::move(copy_output_request));
268 }
269
270 void ScreenRotationAnimator::OnScreenRotationContainerLayerCopiedAfterRotation(
271 std::unique_ptr<ScreenRotationRequest> rotation_request,
272 std::unique_ptr<cc::CopyOutputResult> result) {
273 if (IgnoreCopyResult(rotation_request->id, rotation_request_id_))
274 return;
275 if (AbortRotation(display_id_, result.get())) {
276 ProcessAnimationQueue();
277 return;
278 }
279
280 new_layer_tree_owner_ = CopyLayerTree(std::move(result));
281 AddLayerBelowWindowLayer(root_window_, old_layer_tree_owner_->root(),
282 new_layer_tree_owner_->root());
256 AnimateRotation(std::move(rotation_request)); 283 AnimateRotation(std::move(rotation_request));
257 } 284 }
258 285
259 void ScreenRotationAnimator::CreateOldLayerTree() { 286 void ScreenRotationAnimator::CreateOldLayerTree() {
260 old_layer_tree_owner_ = ::wm::RecreateLayers(GetRootWindow(display_id_)); 287 old_layer_tree_owner_ = ::wm::RecreateLayers(root_window_);
288 // |screen_rotation_container_layer_| needs update after |RecreateLayers()|.
289 screen_rotation_container_layer_ =
290 GetScreenRotationContainer(root_window_)->layer();
291 AddLayerAtTopOfWindowLayers(root_window_, old_layer_tree_owner_->root());
261 } 292 }
262 293
263 void ScreenRotationAnimator::CopyOldLayerTree( 294 std::unique_ptr<ui::LayerTreeOwner> ScreenRotationAnimator::CopyLayerTree(
264 std::unique_ptr<cc::CopyOutputResult> result) { 295 std::unique_ptr<cc::CopyOutputResult> result) {
265 cc::TextureMailbox texture_mailbox; 296 cc::TextureMailbox texture_mailbox;
266 std::unique_ptr<cc::SingleReleaseCallback> release_callback; 297 std::unique_ptr<cc::SingleReleaseCallback> release_callback;
267 result->TakeTexture(&texture_mailbox, &release_callback); 298 result->TakeTexture(&texture_mailbox, &release_callback);
268 DCHECK(texture_mailbox.IsTexture()); 299 DCHECK(texture_mailbox.IsTexture());
269 300
270 aura::Window* root_window = GetRootWindow(display_id_); 301 gfx::Rect rect(screen_rotation_container_layer_->size());
271 gfx::Rect rect(root_window->layer()->size());
272 std::unique_ptr<ui::Layer> copy_layer = base::MakeUnique<ui::Layer>(); 302 std::unique_ptr<ui::Layer> copy_layer = base::MakeUnique<ui::Layer>();
273 copy_layer->SetBounds(rect); 303 copy_layer->SetBounds(rect);
274 copy_layer->SetTextureMailbox(texture_mailbox, std::move(release_callback), 304 copy_layer->SetTextureMailbox(texture_mailbox, std::move(release_callback),
275 rect.size()); 305 rect.size());
276 old_layer_tree_owner_ = 306 return base::MakeUnique<ui::LayerTreeOwner>(std::move(copy_layer));
277 base::MakeUnique<ui::LayerTreeOwner>(std::move(copy_layer));
278 } 307 }
279 308
280 void ScreenRotationAnimator::AnimateRotation( 309 void ScreenRotationAnimator::AnimateRotation(
281 std::unique_ptr<ScreenRotationRequest> rotation_request) { 310 std::unique_ptr<ScreenRotationRequest> rotation_request) {
282 screen_rotation_state_ = ROTATING; 311 screen_rotation_state_ = ROTATING;
283 312 const int rotation_factor = GetRotationFactor(rotation_request->old_rotation,
284 aura::Window* root_window = GetRootWindow(display_id_); 313 rotation_request->new_rotation);
285 std::unique_ptr<LayerCleanupObserver> old_layer_cleanup_observer(
286 new LayerCleanupObserver(weak_factory_.GetWeakPtr()));
287 ui::Layer* old_root_layer = old_layer_tree_owner_->root();
288 old_root_layer->set_name("ScreenRotationAnimator:old_layer_tree");
289 // Add the cloned layer tree in to the root, so it will be rendered.
290 root_window->layer()->Add(old_root_layer);
291 root_window->layer()->StackAtTop(old_root_layer);
292
293 const gfx::Rect original_screen_bounds = root_window->GetTargetBounds();
294
295 const int rotation_factor = GetRotationFactor(
296 GetCurrentScreenRotation(display_id_), rotation_request->new_rotation);
297
298 const int old_layer_initial_rotation_degrees = GetInitialDegrees( 314 const int old_layer_initial_rotation_degrees = GetInitialDegrees(
299 GetCurrentScreenRotation(display_id_), rotation_request->new_rotation); 315 rotation_request->old_rotation, rotation_request->new_rotation);
300
301 const base::TimeDelta duration = 316 const base::TimeDelta duration =
302 base::TimeDelta::FromMilliseconds(kRotationDurationInMs); 317 base::TimeDelta::FromMilliseconds(kRotationDurationInMs);
303
304 const gfx::Tween::Type tween_type = gfx::Tween::FAST_OUT_LINEAR_IN; 318 const gfx::Tween::Type tween_type = gfx::Tween::FAST_OUT_LINEAR_IN;
305 319 const gfx::Rect rotated_screen_bounds = root_window_->GetTargetBounds();
306 Shell::Get()->display_manager()->SetDisplayRotation(
307 display_id_, rotation_request->new_rotation, rotation_request->source);
308
309 const gfx::Rect rotated_screen_bounds = root_window->GetTargetBounds();
310 const gfx::Point pivot = gfx::Point(rotated_screen_bounds.width() / 2, 320 const gfx::Point pivot = gfx::Point(rotated_screen_bounds.width() / 2,
311 rotated_screen_bounds.height() / 2); 321 rotated_screen_bounds.height() / 2);
312 322
313 ui::Layer* screen_rotation_container_layer = 323 ui::Layer* new_root_layer;
314 root_window->GetChildById(kShellWindowId_ScreenRotationContainer) 324 if (new_layer_tree_owner_ && has_switch_ash_enable_smooth_screen_rotation_) {
315 ->layer(); 325 // Make the current layers invisible if the copy request after rotation is
316 std::unique_ptr<ScreenRotationAnimation> current_layer_screen_rotation = 326 // succeesful.
327 screen_rotation_container_layer_->SetOpacity(0.0f);
oshima 2017/04/17 15:25:23 Do you need this? Putting the fully opaque layer o
wutao 2017/04/17 23:30:31 When rotating, at the four corners we can see this
328 new_root_layer = new_layer_tree_owner_->root();
329 } else {
330 new_root_layer = screen_rotation_container_layer_;
331 }
332
333 std::unique_ptr<ScreenRotationAnimation> new_layer_screen_rotation =
317 base::MakeUnique<ScreenRotationAnimation>( 334 base::MakeUnique<ScreenRotationAnimation>(
318 screen_rotation_container_layer, kRotationDegrees * rotation_factor, 335 new_root_layer, kRotationDegrees * rotation_factor,
319 0 /* end_degrees */, screen_rotation_container_layer->opacity(), 336 0 /* end_degrees */, new_root_layer->opacity(),
320 screen_rotation_container_layer->opacity() /* target_opacity */, 337 new_root_layer->opacity() /* target_opacity */, pivot, duration,
321 pivot, duration, tween_type); 338 tween_type);
322 339
323 ui::LayerAnimator* current_layer_animator = 340 ui::LayerAnimator* new_layer_animator = new_root_layer->GetAnimator();
324 screen_rotation_container_layer->GetAnimator(); 341 new_layer_animator->set_preemption_strategy(
325 current_layer_animator->set_preemption_strategy(
326 ui::LayerAnimator::REPLACE_QUEUED_ANIMATIONS); 342 ui::LayerAnimator::REPLACE_QUEUED_ANIMATIONS);
327 std::unique_ptr<ui::LayerAnimationSequence> current_layer_animation_sequence = 343 std::unique_ptr<ui::LayerAnimationSequence> new_layer_animation_sequence =
328 base::MakeUnique<ui::LayerAnimationSequence>( 344 base::MakeUnique<ui::LayerAnimationSequence>(
329 std::move(current_layer_screen_rotation)); 345 std::move(new_layer_screen_rotation));
330 current_layer_animator->StartAnimation(
331 current_layer_animation_sequence.release());
332 346
347 ui::Layer* old_root_layer = old_layer_tree_owner_->root();
348 const gfx::Rect original_screen_bounds = old_root_layer->GetTargetBounds();
333 // The old layer will also be transformed into the new orientation. We will 349 // The old layer will also be transformed into the new orientation. We will
334 // translate it so that the old layer's center point aligns with the new 350 // translate it so that the old layer's center point aligns with the new
335 // orientation's center point and use that center point as the pivot for the 351 // orientation's center point and use that center point as the pivot for the
336 // rotation animation. 352 // rotation animation.
337 gfx::Transform translate_transform; 353 gfx::Transform translate_transform;
338 translate_transform.Translate( 354 translate_transform.Translate(
339 (rotated_screen_bounds.width() - original_screen_bounds.width()) / 2, 355 (rotated_screen_bounds.width() - original_screen_bounds.width()) / 2,
340 (rotated_screen_bounds.height() - original_screen_bounds.height()) / 2); 356 (rotated_screen_bounds.height() - original_screen_bounds.height()) / 2);
341 old_root_layer->SetTransform(translate_transform); 357 old_root_layer->SetTransform(translate_transform);
342 358
343 std::unique_ptr<ScreenRotationAnimation> old_layer_screen_rotation = 359 std::unique_ptr<ScreenRotationAnimation> old_layer_screen_rotation =
344 base::MakeUnique<ScreenRotationAnimation>( 360 base::MakeUnique<ScreenRotationAnimation>(
345 old_root_layer, old_layer_initial_rotation_degrees * rotation_factor, 361 old_root_layer, old_layer_initial_rotation_degrees * rotation_factor,
346 (old_layer_initial_rotation_degrees - kRotationDegrees) * 362 (old_layer_initial_rotation_degrees - kRotationDegrees) *
347 rotation_factor, 363 rotation_factor,
348 old_root_layer->opacity(), 0.0f /* target_opacity */, pivot, duration, 364 old_root_layer->opacity(), 0.0f /* target_opacity */, pivot, duration,
349 tween_type); 365 tween_type);
350 366
351 ui::LayerAnimator* old_layer_animator = old_root_layer->GetAnimator(); 367 ui::LayerAnimator* old_layer_animator = old_root_layer->GetAnimator();
352 old_layer_animator->set_preemption_strategy( 368 old_layer_animator->set_preemption_strategy(
353 ui::LayerAnimator::REPLACE_QUEUED_ANIMATIONS); 369 ui::LayerAnimator::REPLACE_QUEUED_ANIMATIONS);
354 std::unique_ptr<ui::LayerAnimationSequence> old_layer_animation_sequence = 370 std::unique_ptr<ui::LayerAnimationSequence> old_layer_animation_sequence =
355 base::MakeUnique<ui::LayerAnimationSequence>( 371 base::MakeUnique<ui::LayerAnimationSequence>(
356 std::move(old_layer_screen_rotation)); 372 std::move(old_layer_screen_rotation));
357 // Add an observer so that the cloned layers can be cleaned up with the 373
358 // animation completes/aborts.
359 old_layer_animation_sequence->AddObserver(
360 old_layer_cleanup_observer.release());
361 // In unit test, we can use ash::test::ScreenRotationAnimatorTestApi to 374 // In unit test, we can use ash::test::ScreenRotationAnimatorTestApi to
362 // control the animation. 375 // control the animation.
363 if (disable_animation_timers_for_test_) 376 if (disable_animation_timers_for_test_) {
377 if (new_layer_tree_owner_)
378 new_layer_animator->set_disable_timer_for_test(true);
364 old_layer_animator->set_disable_timer_for_test(true); 379 old_layer_animator->set_disable_timer_for_test(true);
380 }
365 old_layer_animation_sequence->SetAnimationMetricsReporter( 381 old_layer_animation_sequence->SetAnimationMetricsReporter(
366 metrics_reporter_.get()); 382 metrics_reporter_.get());
383
384 // Add an observer so that the cloned/copied layers can be cleaned up with the
385 // animation completes/aborts.
386 ui::CallbackLayerAnimationObserver* observer =
387 new ui::CallbackLayerAnimationObserver(
388 base::Bind(&AnimationEndedCallback, weak_factory_.GetWeakPtr()));
389 if (new_layer_tree_owner_)
390 new_layer_animator->AddObserver(observer);
391 new_layer_animator->StartAnimation(new_layer_animation_sequence.release());
392 old_layer_animator->AddObserver(observer);
367 old_layer_animator->StartAnimation(old_layer_animation_sequence.release()); 393 old_layer_animator->StartAnimation(old_layer_animation_sequence.release());
368 394 observer->SetActive();
369 rotation_request.reset();
370 } 395 }
371 396
372 void ScreenRotationAnimator::Rotate(display::Display::Rotation new_rotation, 397 void ScreenRotationAnimator::Rotate(display::Display::Rotation new_rotation,
373 display::Display::RotationSource source) { 398 display::Display::RotationSource source) {
374 if (GetCurrentScreenRotation(display_id_) == new_rotation) 399 const display::Display::Rotation current_rotation =
400 GetCurrentScreenRotation(display_id_);
401 if (current_rotation == new_rotation)
375 return; 402 return;
oshima 2017/04/17 15:25:23 is this correct if there is a rotation request?
wutao 2017/04/17 23:30:31 I need to increase the |rotation_request_id_| firs
376 403
377 rotation_request_id_++; 404 rotation_request_id_++;
378 std::unique_ptr<ScreenRotationRequest> rotation_request = 405 std::unique_ptr<ScreenRotationRequest> rotation_request =
379 base::MakeUnique<ScreenRotationRequest>(rotation_request_id_, 406 base::MakeUnique<ScreenRotationRequest>(
380 new_rotation, source); 407 rotation_request_id_, current_rotation, new_rotation, source);
381
382 switch (screen_rotation_state_) { 408 switch (screen_rotation_state_) {
383 case IDLE: 409 case IDLE:
384 case COPY_REQUESTED: 410 case COPY_REQUESTED:
385 StartRotationAnimation(std::move(rotation_request)); 411 StartRotationAnimation(std::move(rotation_request));
386 break; 412 break;
387 case ROTATING: 413 case ROTATING:
388 last_pending_request_ = std::move(rotation_request); 414 last_pending_request_ = std::move(rotation_request);
389 // The pending request will be processed when the 415 // The pending request will be processed when the
390 // OnLayerAnimation(Ended|Aborted) methods should be called after 416 // OnLayerAnimation(Ended|Aborted) methods should be called after
391 // |StopAnimating()|. 417 // |StopAnimating()|.
392 StopAnimating(); 418 StopAnimating();
393 break; 419 break;
394 } 420 }
395 } 421 }
396 422
397 void ScreenRotationAnimator::AddScreenRotationAnimatorObserver( 423 void ScreenRotationAnimator::AddScreenRotationAnimatorObserver(
398 ScreenRotationAnimatorObserver* observer) { 424 ScreenRotationAnimatorObserver* observer) {
399 screen_rotation_animator_observers_.AddObserver(observer); 425 screen_rotation_animator_observers_.AddObserver(observer);
400 } 426 }
401 427
402 void ScreenRotationAnimator::RemoveScreenRotationAnimatorObserver( 428 void ScreenRotationAnimator::RemoveScreenRotationAnimatorObserver(
403 ScreenRotationAnimatorObserver* observer) { 429 ScreenRotationAnimatorObserver* observer) {
404 screen_rotation_animator_observers_.RemoveObserver(observer); 430 screen_rotation_animator_observers_.RemoveObserver(observer);
405 } 431 }
406 432
407 void ScreenRotationAnimator::ProcessAnimationQueue() { 433 void ScreenRotationAnimator::ProcessAnimationQueue() {
408 screen_rotation_state_ = IDLE; 434 screen_rotation_state_ = IDLE;
435 if (IsDisplayIdValid(display_id_))
436 screen_rotation_container_layer_->SetOpacity(1.0f);
409 old_layer_tree_owner_.reset(); 437 old_layer_tree_owner_.reset();
438 new_layer_tree_owner_.reset();
410 if (last_pending_request_ && IsDisplayIdValid(display_id_)) { 439 if (last_pending_request_ && IsDisplayIdValid(display_id_)) {
411 display::Display::Rotation new_rotation = 440 display::Display::Rotation new_rotation =
412 last_pending_request_->new_rotation; 441 last_pending_request_->new_rotation;
413 display::Display::RotationSource source = last_pending_request_->source; 442 display::Display::RotationSource source = last_pending_request_->source;
414 last_pending_request_.reset(); 443 last_pending_request_.reset();
415 Rotate(new_rotation, source); 444 Rotate(new_rotation, source);
416 return; 445 return;
417 } 446 }
418 447
419 for (auto& observer : screen_rotation_animator_observers_) 448 for (auto& observer : screen_rotation_animator_observers_)
420 observer.OnScreenRotationAnimationFinished(this); 449 observer.OnScreenRotationAnimationFinished(this);
421 } 450 }
422 451
423 void ScreenRotationAnimator::set_disable_animation_timers_for_test(
424 bool disable_timers) {
425 disable_animation_timers_for_test_ = disable_timers;
426 }
427
428 void ScreenRotationAnimator::StopAnimating() { 452 void ScreenRotationAnimator::StopAnimating() {
429 GetRootWindow(display_id_) 453 // |old_layer_tree_owner_| new_layer_tree_owner_| could be nullptr if another
430 ->GetChildById(kShellWindowId_ScreenRotationContainer) 454 // the rotation request comes before the copy request finished.
431 ->layer()
432 ->GetAnimator()
433 ->StopAnimating();
434 if (old_layer_tree_owner_) 455 if (old_layer_tree_owner_)
435 old_layer_tree_owner_->root()->GetAnimator()->StopAnimating(); 456 old_layer_tree_owner_->root()->GetAnimator()->StopAnimating();
457 if (new_layer_tree_owner_)
458 new_layer_tree_owner_->root()->GetAnimator()->StopAnimating();
459 if (IsDisplayIdValid(display_id_))
460 screen_rotation_container_layer_->SetOpacity(1.0f);
436 } 461 }
437 462
438 } // namespace ash 463 } // namespace ash
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698