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.
5 #include "ash/display/display_configurator_animation.h"
8 #include "ash/shell_window_ids.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"
23 const int kFadingAnimationDurationInMS
= 200;
24 const int kFadingTimeoutDurationInSeconds
= 10;
26 // CallbackRunningObserver accepts multiple layer animations and
27 // runs the specified |callback| when all of the animations have finished.
28 class CallbackRunningObserver
{
30 CallbackRunningObserver(base::Closure callback
)
31 : completed_counter_(0),
32 animation_aborted_(false),
33 callback_(callback
) {}
35 void AddNewAnimator(ui::LayerAnimator
* animator
) {
36 Observer
* observer
= new Observer(animator
, this);
37 animator
->AddObserver(observer
);
38 observer_list_
.push_back(observer
);
42 void OnSingleTaskCompleted() {
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_
);
51 void OnSingleTaskAborted() {
52 animation_aborted_
= true;
53 OnSingleTaskCompleted();
56 // The actual observer to listen each animation completion.
57 class Observer
: public ui::LayerAnimationObserver
{
59 Observer(ui::LayerAnimator
* animator
,
60 CallbackRunningObserver
* observer
)
61 : animator_(animator
),
62 observer_(observer
) {}
65 // ui::LayerAnimationObserver overrides:
66 virtual void OnLayerAnimationEnded(
67 ui::LayerAnimationSequence
* sequence
) OVERRIDE
{
68 animator_
->RemoveObserver(this);
69 observer_
->OnSingleTaskCompleted();
71 virtual void OnLayerAnimationAborted(
72 ui::LayerAnimationSequence
* sequence
) OVERRIDE
{
73 animator_
->RemoveObserver(this);
74 observer_
->OnSingleTaskAborted();
76 virtual void OnLayerAnimationScheduled(
77 ui::LayerAnimationSequence
* sequence
) OVERRIDE
{
79 virtual bool RequiresNotificationWhenAnimatorDestroyed() const OVERRIDE
{
84 ui::LayerAnimator
* animator_
;
85 CallbackRunningObserver
* observer_
;
87 DISALLOW_COPY_AND_ASSIGN(Observer
);
90 size_t completed_counter_
;
91 bool animation_aborted_
;
92 ScopedVector
<Observer
> observer_list_
;
93 base::Closure callback_
;
95 DISALLOW_COPY_AND_ASSIGN(CallbackRunningObserver
);
100 DisplayConfiguratorAnimation::DisplayConfiguratorAnimation()
101 : weak_ptr_factory_(this) {
104 DisplayConfiguratorAnimation::~DisplayConfiguratorAnimation() {
108 void DisplayConfiguratorAnimation::StartFadeOutAnimation(
109 base::Closure callback
) {
110 CallbackRunningObserver
* observer
= new CallbackRunningObserver(callback
);
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());
126 ash::Shell::GetContainer(root_window
,
127 ash::kShellWindowId_OverlayContainer
)->layer();
128 parent
->Add(hiding_layer
);
130 hiding_layer
->SetOpacity(0.0);
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
;
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
144 timer_
.reset(new base::OneShotTimer
<DisplayConfiguratorAnimation
>());
145 timer_
->Start(FROM_HERE
,
146 base::TimeDelta::FromSeconds(kFadingTimeoutDurationInSeconds
),
148 &DisplayConfiguratorAnimation::ClearHidingLayers
);
151 void 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()));
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();
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
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());
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
;
192 hiding_layer
= hiding_layers_
[root_window
];
193 if (hiding_layer
->bounds() != root_window
->bounds())
194 hiding_layer
->SetBounds(root_window
->bounds());
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);
206 void DisplayConfiguratorAnimation::OnDisplayModeChanged(
207 const ui::DisplayConfigurator::DisplayStateList
& displays
) {
208 if (!hiding_layers_
.empty())
209 StartFadeInAnimation();
212 void DisplayConfiguratorAnimation::OnDisplayModeChangeFailed(
213 ui::MultipleDisplayState failed_new_state
) {
214 if (!hiding_layers_
.empty())
215 StartFadeInAnimation();
218 void DisplayConfiguratorAnimation::ClearHidingLayers() {
223 STLDeleteContainerPairSecondPointers(
224 hiding_layers_
.begin(), hiding_layers_
.end());
225 hiding_layers_
.clear();