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 3// found in the LICENSE file. 4 5#include "ash/display/display_configurator_animation.h" 6 7#include "ash/shell.h" 8#include "ash/shell_window_ids.h" 9#include "base/bind.h" 10#include "base/stl_util.h" 11#include "base/time/time.h" 12#include "ui/aura/window.h" 13#include "ui/aura/window_event_dispatcher.h" 14#include "ui/compositor/layer.h" 15#include "ui/compositor/layer_animation_observer.h" 16#include "ui/compositor/layer_animation_sequence.h" 17#include "ui/compositor/layer_animator.h" 18#include "ui/compositor/scoped_layer_animation_settings.h" 19 20namespace ash { 21namespace { 22 23const int kFadingAnimationDurationInMS = 200; 24const int kFadingTimeoutDurationInSeconds = 10; 25 26// CallbackRunningObserver accepts multiple layer animations and 27// runs the specified |callback| when all of the animations have finished. 28class CallbackRunningObserver { 29 public: 30 CallbackRunningObserver(base::Closure callback) 31 : completed_counter_(0), 32 animation_aborted_(false), 33 callback_(callback) {} 34 35 void AddNewAnimator(ui::LayerAnimator* animator) { 36 Observer* observer = new Observer(animator, this); 37 animator->AddObserver(observer); 38 observer_list_.push_back(observer); 39 } 40 41 private: 42 void OnSingleTaskCompleted() { 43 completed_counter_++; 44 if (completed_counter_ >= observer_list_.size()) { 45 base::MessageLoopForUI::current()->DeleteSoon(FROM_HERE, this); 46 if (!animation_aborted_) 47 base::MessageLoopForUI::current()->PostTask(FROM_HERE, callback_); 48 } 49 } 50 51 void OnSingleTaskAborted() { 52 animation_aborted_ = true; 53 OnSingleTaskCompleted(); 54 } 55 56 // The actual observer to listen each animation completion. 57 class Observer : public ui::LayerAnimationObserver { 58 public: 59 Observer(ui::LayerAnimator* animator, 60 CallbackRunningObserver* observer) 61 : animator_(animator), 62 observer_(observer) {} 63 64 protected: 65 // ui::LayerAnimationObserver overrides: 66 virtual void OnLayerAnimationEnded( 67 ui::LayerAnimationSequence* sequence) OVERRIDE { 68 animator_->RemoveObserver(this); 69 observer_->OnSingleTaskCompleted(); 70 } 71 virtual void OnLayerAnimationAborted( 72 ui::LayerAnimationSequence* sequence) OVERRIDE { 73 animator_->RemoveObserver(this); 74 observer_->OnSingleTaskAborted(); 75 } 76 virtual void OnLayerAnimationScheduled( 77 ui::LayerAnimationSequence* sequence) OVERRIDE { 78 } 79 virtual bool RequiresNotificationWhenAnimatorDestroyed() const OVERRIDE { 80 return true; 81 } 82 83 private: 84 ui::LayerAnimator* animator_; 85 CallbackRunningObserver* observer_; 86 87 DISALLOW_COPY_AND_ASSIGN(Observer); 88 }; 89 90 size_t completed_counter_; 91 bool animation_aborted_; 92 ScopedVector<Observer> observer_list_; 93 base::Closure callback_; 94 95 DISALLOW_COPY_AND_ASSIGN(CallbackRunningObserver); 96}; 97 98} // namespace 99 100DisplayConfiguratorAnimation::DisplayConfiguratorAnimation() 101 : weak_ptr_factory_(this) { 102} 103 104DisplayConfiguratorAnimation::~DisplayConfiguratorAnimation() { 105 ClearHidingLayers(); 106} 107 108void DisplayConfiguratorAnimation::StartFadeOutAnimation( 109 base::Closure callback) { 110 CallbackRunningObserver* observer = new CallbackRunningObserver(callback); 111 ClearHidingLayers(); 112 113 // Make the fade-out animation for all root windows. Instead of actually 114 // hiding the root windows, we put a black layer over a root window for 115 // safety. These layers remain to hide root windows and will be deleted 116 // after the animation of OnDisplayModeChanged(). 117 aura::Window::Windows root_windows = 118 Shell::GetInstance()->GetAllRootWindows(); 119 for (aura::Window::Windows::const_iterator it = root_windows.begin(); 120 it != root_windows.end(); ++it) { 121 aura::Window* root_window = *it; 122 ui::Layer* hiding_layer = new ui::Layer(ui::LAYER_SOLID_COLOR); 123 hiding_layer->SetColor(SK_ColorBLACK); 124 hiding_layer->SetBounds(root_window->bounds()); 125 ui::Layer* parent = 126 ash::Shell::GetContainer(root_window, 127 ash::kShellWindowId_OverlayContainer)->layer(); 128 parent->Add(hiding_layer); 129 130 hiding_layer->SetOpacity(0.0); 131 132 ui::ScopedLayerAnimationSettings settings(hiding_layer->GetAnimator()); 133 settings.SetTransitionDuration(base::TimeDelta::FromMilliseconds( 134 kFadingAnimationDurationInMS)); 135 observer->AddNewAnimator(hiding_layer->GetAnimator()); 136 hiding_layer->SetOpacity(1.0f); 137 hiding_layer->SetVisible(true); 138 hiding_layers_[root_window] = hiding_layer; 139 } 140 141 // In case that OnDisplayModeChanged() isn't called or its animator is 142 // canceled due to some unknown errors, we set a timer to clear these 143 // hiding layers. 144 timer_.reset(new base::OneShotTimer<DisplayConfiguratorAnimation>()); 145 timer_->Start(FROM_HERE, 146 base::TimeDelta::FromSeconds(kFadingTimeoutDurationInSeconds), 147 this, 148 &DisplayConfiguratorAnimation::ClearHidingLayers); 149} 150 151void DisplayConfiguratorAnimation::StartFadeInAnimation() { 152 // We want to make sure clearing all of hiding layers after the animation 153 // finished. Note that this callback can be canceled, but the cancel only 154 // happens when the next animation is scheduled. Thus the hiding layers 155 // should be deleted eventually. 156 CallbackRunningObserver* observer = new CallbackRunningObserver( 157 base::Bind(&DisplayConfiguratorAnimation::ClearHidingLayers, 158 weak_ptr_factory_.GetWeakPtr())); 159 160 // Ensure that layers are not animating. 161 for (std::map<aura::Window*, ui::Layer*>::iterator it = 162 hiding_layers_.begin(); it != hiding_layers_.end(); ++it) { 163 ui::LayerAnimator* animator = it->second->GetAnimator(); 164 if (animator->is_animating()) 165 animator->StopAnimating(); 166 } 167 168 // Schedules the fade-in effect for all root windows. Because we put the 169 // black layers for fade-out, here we actually turn those black layers 170 // invisible. 171 aura::Window::Windows root_windows = 172 Shell::GetInstance()->GetAllRootWindows(); 173 for (aura::Window::Windows::const_iterator it = root_windows.begin(); 174 it != root_windows.end(); ++it) { 175 aura::Window* root_window = *it; 176 ui::Layer* hiding_layer = NULL; 177 if (hiding_layers_.find(root_window) == hiding_layers_.end()) { 178 // In case of the transition from mirroring->non-mirroring, new root 179 // windows appear and we do not have the black layers for them. Thus 180 // we need to create the layer and make it visible. 181 hiding_layer = new ui::Layer(ui::LAYER_SOLID_COLOR); 182 hiding_layer->SetColor(SK_ColorBLACK); 183 hiding_layer->SetBounds(root_window->bounds()); 184 ui::Layer* parent = 185 ash::Shell::GetContainer( 186 root_window, ash::kShellWindowId_OverlayContainer)->layer(); 187 parent->Add(hiding_layer); 188 hiding_layer->SetOpacity(1.0f); 189 hiding_layer->SetVisible(true); 190 hiding_layers_[root_window] = hiding_layer; 191 } else { 192 hiding_layer = hiding_layers_[root_window]; 193 if (hiding_layer->bounds() != root_window->bounds()) 194 hiding_layer->SetBounds(root_window->bounds()); 195 } 196 197 ui::ScopedLayerAnimationSettings settings(hiding_layer->GetAnimator()); 198 settings.SetTransitionDuration(base::TimeDelta::FromMilliseconds( 199 kFadingAnimationDurationInMS)); 200 observer->AddNewAnimator(hiding_layer->GetAnimator()); 201 hiding_layer->SetOpacity(0.0f); 202 hiding_layer->SetVisible(false); 203 } 204} 205 206void DisplayConfiguratorAnimation::OnDisplayModeChanged( 207 const ui::DisplayConfigurator::DisplayStateList& displays) { 208 if (!hiding_layers_.empty()) 209 StartFadeInAnimation(); 210} 211 212void DisplayConfiguratorAnimation::OnDisplayModeChangeFailed( 213 ui::MultipleDisplayState failed_new_state) { 214 if (!hiding_layers_.empty()) 215 StartFadeInAnimation(); 216} 217 218void DisplayConfiguratorAnimation::ClearHidingLayers() { 219 if (timer_) { 220 timer_->Stop(); 221 timer_.reset(); 222 } 223 STLDeleteContainerPairSecondPointers( 224 hiding_layers_.begin(), hiding_layers_.end()); 225 hiding_layers_.clear(); 226} 227 228} // namespace ash 229