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

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

Issue 2809553002: Fix corner case if another rotation request comes before the copy request finishes. (Closed)
Patch Set: Add TODO to handle cancel the copy request or ignore the copy result. 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
« no previous file with comments | « ash/rotator/screen_rotation_animator.h ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
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> 7 #include <string>
8 #include <utility> 8 #include <utility>
9 #include <vector> 9 #include <vector>
10 10
(...skipping 171 matching lines...) Expand 10 before | Expand all | Expand 10 after
182 } 182 }
183 183
184 private: 184 private:
185 DISALLOW_COPY_AND_ASSIGN(ScreenRotationAnimationMetricsReporter); 185 DISALLOW_COPY_AND_ASSIGN(ScreenRotationAnimationMetricsReporter);
186 }; 186 };
187 187
188 } // namespace 188 } // namespace
189 189
190 ScreenRotationAnimator::ScreenRotationAnimator(int64_t display_id) 190 ScreenRotationAnimator::ScreenRotationAnimator(int64_t display_id)
191 : display_id_(display_id), 191 : display_id_(display_id),
192 is_rotating_(false), 192 screen_rotation_state_(IDLE),
193 metrics_reporter_( 193 metrics_reporter_(
194 base::MakeUnique<ScreenRotationAnimationMetricsReporter>()), 194 base::MakeUnique<ScreenRotationAnimationMetricsReporter>()),
195 disable_animation_timers_for_test_(false), 195 disable_animation_timers_for_test_(false),
196 weak_factory_(this) {} 196 weak_factory_(this) {}
197 197
198 ScreenRotationAnimator::~ScreenRotationAnimator() { 198 ScreenRotationAnimator::~ScreenRotationAnimator() {
199 // To prevent a call to |LayerCleanupObserver::OnLayerAnimationAborted()| from 199 // To prevent a call to |LayerCleanupObserver::OnLayerAnimationAborted()| from
200 // calling a method on the |animator_|. 200 // calling a method on the |animator_|.
201 weak_factory_.InvalidateWeakPtrs(); 201 weak_factory_.InvalidateWeakPtrs();
202 202
(...skipping 16 matching lines...) Expand all
219 } 219 }
220 220
221 void ScreenRotationAnimator::RequestCopyRootLayerAndAnimateRotation( 221 void ScreenRotationAnimator::RequestCopyRootLayerAndAnimateRotation(
222 std::unique_ptr<ScreenRotationRequest> rotation_request) { 222 std::unique_ptr<ScreenRotationRequest> rotation_request) {
223 std::unique_ptr<cc::CopyOutputRequest> copy_output_request = 223 std::unique_ptr<cc::CopyOutputRequest> copy_output_request =
224 cc::CopyOutputRequest::CreateRequest( 224 cc::CopyOutputRequest::CreateRequest(
225 CreateAfterCopyCallback(std::move(rotation_request))); 225 CreateAfterCopyCallback(std::move(rotation_request)));
226 ui::Layer* layer = GetRootWindow(display_id_)->layer(); 226 ui::Layer* layer = GetRootWindow(display_id_)->layer();
227 copy_output_request->set_area(gfx::Rect(layer->size())); 227 copy_output_request->set_area(gfx::Rect(layer->size()));
228 layer->RequestCopyOfOutput(std::move(copy_output_request)); 228 layer->RequestCopyOfOutput(std::move(copy_output_request));
229
230 screen_rotation_state_ = COPY_REQUESTED;
229 } 231 }
230 232
231 ScreenRotationAnimator::CopyCallback 233 ScreenRotationAnimator::CopyCallback
232 ScreenRotationAnimator::CreateAfterCopyCallback( 234 ScreenRotationAnimator::CreateAfterCopyCallback(
233 std::unique_ptr<ScreenRotationRequest> rotation_request) { 235 std::unique_ptr<ScreenRotationRequest> rotation_request) {
234 return base::Bind(&ScreenRotationAnimator::OnRootLayerCopiedBeforeRotation, 236 return base::Bind(&ScreenRotationAnimator::OnRootLayerCopiedBeforeRotation,
235 weak_factory_.GetWeakPtr(), 237 weak_factory_.GetWeakPtr(),
236 base::Passed(&rotation_request)); 238 base::Passed(&rotation_request));
237 } 239 }
238 240
239 void ScreenRotationAnimator::OnRootLayerCopiedBeforeRotation( 241 void ScreenRotationAnimator::OnRootLayerCopiedBeforeRotation(
240 std::unique_ptr<ScreenRotationRequest> rotation_request, 242 std::unique_ptr<ScreenRotationRequest> rotation_request,
241 std::unique_ptr<cc::CopyOutputResult> result) { 243 std::unique_ptr<cc::CopyOutputResult> result) {
242 // If display was removed, should abort rotation. 244 // In the following cases, abort rotation:
243 if (!IsDisplayIdValid(display_id_)) { 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()) {
244 ProcessAnimationQueue(); 251 ProcessAnimationQueue();
252 rotation_request.reset();
245 return; 253 return;
246 } 254 }
247 255
248 // Fall back to recreate layers solution when the copy request has been 256 CopyOldLayerTree(std::move(result));
249 // canceled or failed. It would fail if, for examples: a) The layer is removed
250 // from the compositor and destroyed before committing the request to the
251 // compositor. b) The compositor is shutdown.
252 if (result->IsEmpty())
253 CreateOldLayerTree();
254 else
255 CopyOldLayerTree(std::move(result));
256 AnimateRotation(std::move(rotation_request)); 257 AnimateRotation(std::move(rotation_request));
257 } 258 }
258 259
259 void ScreenRotationAnimator::CreateOldLayerTree() { 260 void ScreenRotationAnimator::CreateOldLayerTree() {
260 old_layer_tree_owner_ = ::wm::RecreateLayers(GetRootWindow(display_id_)); 261 old_layer_tree_owner_ = ::wm::RecreateLayers(GetRootWindow(display_id_));
261 } 262 }
262 263
263 void ScreenRotationAnimator::CopyOldLayerTree( 264 void ScreenRotationAnimator::CopyOldLayerTree(
264 std::unique_ptr<cc::CopyOutputResult> result) { 265 std::unique_ptr<cc::CopyOutputResult> result) {
265 cc::TextureMailbox texture_mailbox; 266 cc::TextureMailbox texture_mailbox;
266 std::unique_ptr<cc::SingleReleaseCallback> release_callback; 267 std::unique_ptr<cc::SingleReleaseCallback> release_callback;
267 result->TakeTexture(&texture_mailbox, &release_callback); 268 result->TakeTexture(&texture_mailbox, &release_callback);
268 DCHECK(texture_mailbox.IsTexture()); 269 DCHECK(texture_mailbox.IsTexture());
269 270
270 aura::Window* root_window = GetRootWindow(display_id_); 271 aura::Window* root_window = GetRootWindow(display_id_);
271 gfx::Rect rect(root_window->layer()->size()); 272 gfx::Rect rect(root_window->layer()->size());
272 std::unique_ptr<ui::Layer> copy_layer = base::MakeUnique<ui::Layer>(); 273 std::unique_ptr<ui::Layer> copy_layer = base::MakeUnique<ui::Layer>();
273 copy_layer->SetBounds(rect); 274 copy_layer->SetBounds(rect);
274 copy_layer->SetTextureMailbox(texture_mailbox, std::move(release_callback), 275 copy_layer->SetTextureMailbox(texture_mailbox, std::move(release_callback),
275 rect.size()); 276 rect.size());
276 old_layer_tree_owner_ = 277 old_layer_tree_owner_ =
277 base::MakeUnique<ui::LayerTreeOwner>(std::move(copy_layer)); 278 base::MakeUnique<ui::LayerTreeOwner>(std::move(copy_layer));
278 } 279 }
279 280
280 void ScreenRotationAnimator::AnimateRotation( 281 void ScreenRotationAnimator::AnimateRotation(
281 std::unique_ptr<ScreenRotationRequest> rotation_request) { 282 std::unique_ptr<ScreenRotationRequest> rotation_request) {
283 screen_rotation_state_ = ROTATING;
284
282 aura::Window* root_window = GetRootWindow(display_id_); 285 aura::Window* root_window = GetRootWindow(display_id_);
283 std::unique_ptr<LayerCleanupObserver> old_layer_cleanup_observer( 286 std::unique_ptr<LayerCleanupObserver> old_layer_cleanup_observer(
284 new LayerCleanupObserver(weak_factory_.GetWeakPtr())); 287 new LayerCleanupObserver(weak_factory_.GetWeakPtr()));
285 ui::Layer* old_root_layer = old_layer_tree_owner_->root(); 288 ui::Layer* old_root_layer = old_layer_tree_owner_->root();
286 old_root_layer->set_name("ScreenRotationAnimator:old_layer_tree"); 289 old_root_layer->set_name("ScreenRotationAnimator:old_layer_tree");
287 // Add the cloned layer tree in to the root, so it will be rendered. 290 // Add the cloned layer tree in to the root, so it will be rendered.
288 root_window->layer()->Add(old_root_layer); 291 root_window->layer()->Add(old_root_layer);
289 root_window->layer()->StackAtTop(old_root_layer); 292 root_window->layer()->StackAtTop(old_root_layer);
290 293
291 const gfx::Rect original_screen_bounds = root_window->GetTargetBounds(); 294 const gfx::Rect original_screen_bounds = root_window->GetTargetBounds();
(...skipping 79 matching lines...) Expand 10 before | Expand all | Expand 10 after
371 } 374 }
372 375
373 void ScreenRotationAnimator::Rotate(display::Display::Rotation new_rotation, 376 void ScreenRotationAnimator::Rotate(display::Display::Rotation new_rotation,
374 display::Display::RotationSource source) { 377 display::Display::RotationSource source) {
375 if (GetCurrentScreenRotation(display_id_) == new_rotation) 378 if (GetCurrentScreenRotation(display_id_) == new_rotation)
376 return; 379 return;
377 380
378 std::unique_ptr<ScreenRotationRequest> rotation_request = 381 std::unique_ptr<ScreenRotationRequest> rotation_request =
379 base::MakeUnique<ScreenRotationRequest>(new_rotation, source); 382 base::MakeUnique<ScreenRotationRequest>(new_rotation, source);
380 383
381 if (is_rotating_) { 384 switch (screen_rotation_state_) {
382 last_pending_request_ = std::move(rotation_request); 385 case IDLE:
383 // The pending request will be processed when the 386 StartRotationAnimation(std::move(rotation_request));
384 // OnLayerAnimation(Ended|Aborted) methods should be called after 387 break;
385 // StopAnimating(). 388 case ROTATING:
386 StopAnimating(); 389 last_pending_request_ = std::move(rotation_request);
387 } else { 390 // The pending request will be processed when the
388 is_rotating_ = true; 391 // OnLayerAnimation(Ended|Aborted) methods should be called after
389 StartRotationAnimation(std::move(rotation_request)); 392 // |StopAnimating()|.
393 StopAnimating();
394 break;
395 case COPY_REQUESTED:
396 // TODO(wutao): We need to handle this otherwise the device will be in
397 // inconsistent state.
398 break;
390 } 399 }
391 } 400 }
392 401
393 void ScreenRotationAnimator::AddScreenRotationAnimatorObserver( 402 void ScreenRotationAnimator::AddScreenRotationAnimatorObserver(
394 ScreenRotationAnimatorObserver* observer) { 403 ScreenRotationAnimatorObserver* observer) {
395 screen_rotation_animator_observers_.AddObserver(observer); 404 screen_rotation_animator_observers_.AddObserver(observer);
396 } 405 }
397 406
398 void ScreenRotationAnimator::RemoveScreenRotationAnimatorObserver( 407 void ScreenRotationAnimator::RemoveScreenRotationAnimatorObserver(
399 ScreenRotationAnimatorObserver* observer) { 408 ScreenRotationAnimatorObserver* observer) {
400 screen_rotation_animator_observers_.RemoveObserver(observer); 409 screen_rotation_animator_observers_.RemoveObserver(observer);
401 } 410 }
402 411
403 void ScreenRotationAnimator::ProcessAnimationQueue() { 412 void ScreenRotationAnimator::ProcessAnimationQueue() {
404 is_rotating_ = false; 413 screen_rotation_state_ = IDLE;
405 old_layer_tree_owner_.reset(); 414 old_layer_tree_owner_.reset();
406 if (last_pending_request_ && IsDisplayIdValid(display_id_)) { 415 if (last_pending_request_ && IsDisplayIdValid(display_id_)) {
407 std::unique_ptr<ScreenRotationRequest> rotation_request = 416 std::unique_ptr<ScreenRotationRequest> rotation_request =
408 std::move(last_pending_request_); 417 std::move(last_pending_request_);
oshima 2017/04/10 23:20:39 by the way, do you need to move this to stack?
wutao 2017/04/10 23:56:18 Talked offline. I will change the unique_ptr to lo
409 Rotate(rotation_request->new_rotation, rotation_request->source); 418 Rotate(rotation_request->new_rotation, rotation_request->source);
410 rotation_request.reset(); 419 rotation_request.reset();
411 return; 420 return;
412 } 421 }
413 422
414 for (auto& observer : screen_rotation_animator_observers_) 423 for (auto& observer : screen_rotation_animator_observers_)
415 observer.OnScreenRotationAnimationFinished(this); 424 observer.OnScreenRotationAnimationFinished(this);
416 } 425 }
417 426
418 void ScreenRotationAnimator::set_disable_animation_timers_for_test( 427 void ScreenRotationAnimator::set_disable_animation_timers_for_test(
419 bool disable_timers) { 428 bool disable_timers) {
420 disable_animation_timers_for_test_ = disable_timers; 429 disable_animation_timers_for_test_ = disable_timers;
421 } 430 }
422 431
423 void ScreenRotationAnimator::StopAnimating() { 432 void ScreenRotationAnimator::StopAnimating() {
424 aura::Window* root_window = GetRootWindow(display_id_); 433 aura::Window* root_window = GetRootWindow(display_id_);
425 for (ui::Layer* child_layer : root_window->layer()->children()) { 434 for (ui::Layer* child_layer : root_window->layer()->children()) {
426 if (child_layer == old_layer_tree_owner_->root()) 435 if (child_layer == old_layer_tree_owner_->root())
427 continue; 436 continue;
428 437
429 child_layer->GetAnimator()->StopAnimating(); 438 child_layer->GetAnimator()->StopAnimating();
430 } 439 }
431 440
432 old_layer_tree_owner_->root()->GetAnimator()->StopAnimating(); 441 old_layer_tree_owner_->root()->GetAnimator()->StopAnimating();
433 } 442 }
434 443
435 } // namespace ash 444 } // namespace ash
OLDNEW
« no previous file with comments | « ash/rotator/screen_rotation_animator.h ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698