Upstream version 7.36.149.0
[platform/framework/web/crosswalk.git] / src / ash / display / display_configurator_animation.cc
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
20 namespace ash {
21 namespace {
22
23 const int kFadingAnimationDurationInMS = 200;
24 const int kFadingTimeoutDurationInSeconds = 10;
25
26 // CallbackRunningObserver accepts multiple layer animations and
27 // runs the specified |callback| when all of the animations have finished.
28 class 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
100 DisplayConfiguratorAnimation::DisplayConfiguratorAnimation() {
101 }
102
103 DisplayConfiguratorAnimation::~DisplayConfiguratorAnimation() {
104   ClearHidingLayers();
105 }
106
107 void DisplayConfiguratorAnimation::StartFadeOutAnimation(
108     base::Closure callback) {
109   CallbackRunningObserver* observer = new CallbackRunningObserver(callback);
110   ClearHidingLayers();
111
112   // Make the fade-out animation for all root windows.  Instead of actually
113   // hiding the root windows, we put a black layer over a root window for
114   // safety.  These layers remain to hide root windows and will be deleted
115   // after the animation of OnDisplayModeChanged().
116   aura::Window::Windows root_windows =
117       Shell::GetInstance()->GetAllRootWindows();
118   for (aura::Window::Windows::const_iterator it = root_windows.begin();
119        it != root_windows.end(); ++it) {
120     aura::Window* root_window = *it;
121     ui::Layer* hiding_layer = new ui::Layer(ui::LAYER_SOLID_COLOR);
122     hiding_layer->SetColor(SK_ColorBLACK);
123     hiding_layer->SetBounds(root_window->bounds());
124     ui::Layer* parent =
125         ash::Shell::GetContainer(root_window,
126                                  ash::kShellWindowId_OverlayContainer)->layer();
127     parent->Add(hiding_layer);
128
129     hiding_layer->SetOpacity(0.0);
130
131     ui::ScopedLayerAnimationSettings settings(hiding_layer->GetAnimator());
132     settings.SetTransitionDuration(base::TimeDelta::FromMilliseconds(
133         kFadingAnimationDurationInMS));
134     observer->AddNewAnimator(hiding_layer->GetAnimator());
135     hiding_layer->SetOpacity(1.0f);
136     hiding_layer->SetVisible(true);
137     hiding_layers_[root_window] = hiding_layer;
138   }
139
140   // In case that OnDisplayModeChanged() isn't called or its animator is
141   // canceled due to some unknown errors, we set a timer to clear these
142   // hiding layers.
143   timer_.reset(new base::OneShotTimer<DisplayConfiguratorAnimation>());
144   timer_->Start(FROM_HERE,
145                 base::TimeDelta::FromSeconds(kFadingTimeoutDurationInSeconds),
146                 this,
147                 &DisplayConfiguratorAnimation::ClearHidingLayers);
148 }
149
150 void DisplayConfiguratorAnimation::StartFadeInAnimation() {
151   // We want to make sure clearing all of hiding layers after the animation
152   // finished.  Note that this callback can be canceled, but the cancel only
153   // happens when the next animation is scheduled.  Thus the hiding layers
154   // should be deleted eventually.
155   CallbackRunningObserver* observer = new CallbackRunningObserver(
156       base::Bind(&DisplayConfiguratorAnimation::ClearHidingLayers,
157                  base::Unretained(this)));
158
159   // Ensure that layers are not animating.
160   for (std::map<aura::Window*, ui::Layer*>::iterator it =
161            hiding_layers_.begin(); it != hiding_layers_.end(); ++it) {
162     ui::LayerAnimator* animator = it->second->GetAnimator();
163     if (animator->is_animating())
164       animator->StopAnimating();
165   }
166
167   // Schedules the fade-in effect for all root windows.  Because we put the
168   // black layers for fade-out, here we actually turn those black layers
169   // invisible.
170   aura::Window::Windows root_windows =
171       Shell::GetInstance()->GetAllRootWindows();
172   for (aura::Window::Windows::const_iterator it = root_windows.begin();
173        it != root_windows.end(); ++it) {
174     aura::Window* root_window = *it;
175     ui::Layer* hiding_layer = NULL;
176     if (hiding_layers_.find(root_window) == hiding_layers_.end()) {
177       // In case of the transition from mirroring->non-mirroring, new root
178       // windows appear and we do not have the black layers for them.  Thus
179       // we need to create the layer and make it visible.
180       hiding_layer = new ui::Layer(ui::LAYER_SOLID_COLOR);
181       hiding_layer->SetColor(SK_ColorBLACK);
182       hiding_layer->SetBounds(root_window->bounds());
183       ui::Layer* parent =
184           ash::Shell::GetContainer(
185               root_window, ash::kShellWindowId_OverlayContainer)->layer();
186       parent->Add(hiding_layer);
187       hiding_layer->SetOpacity(1.0f);
188       hiding_layer->SetVisible(true);
189       hiding_layers_[root_window] = hiding_layer;
190     } else {
191       hiding_layer = hiding_layers_[root_window];
192       if (hiding_layer->bounds() != root_window->bounds())
193         hiding_layer->SetBounds(root_window->bounds());
194     }
195
196     ui::ScopedLayerAnimationSettings settings(hiding_layer->GetAnimator());
197     settings.SetTransitionDuration(base::TimeDelta::FromMilliseconds(
198         kFadingAnimationDurationInMS));
199     observer->AddNewAnimator(hiding_layer->GetAnimator());
200     hiding_layer->SetOpacity(0.0f);
201     hiding_layer->SetVisible(false);
202   }
203 }
204
205 void DisplayConfiguratorAnimation::OnDisplayModeChanged(
206     const ui::DisplayConfigurator::DisplayStateList& displays) {
207   if (!hiding_layers_.empty())
208     StartFadeInAnimation();
209 }
210
211 void DisplayConfiguratorAnimation::OnDisplayModeChangeFailed(
212     ui::MultipleDisplayState failed_new_state) {
213   if (!hiding_layers_.empty())
214     StartFadeInAnimation();
215 }
216
217 void DisplayConfiguratorAnimation::ClearHidingLayers() {
218   if (timer_) {
219     timer_->Stop();
220     timer_.reset();
221   }
222   STLDeleteContainerPairSecondPointers(
223       hiding_layers_.begin(), hiding_layers_.end());
224   hiding_layers_.clear();
225 }
226
227 }  // namespace ash