Home | History | Annotate | Download | only in display
      1 // Copyright (c) 2012 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/output_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/root_window.h"
     13 #include "ui/aura/window.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 internal {
     22 namespace {
     23 
     24 const int kFadingAnimationDurationInMS = 200;
     25 const int kFadingTimeoutDurationInSeconds = 10;
     26 
     27 // CallbackRunningObserver accepts multiple layer animations and
     28 // runs the specified |callback| when all of the animations have finished.
     29 class CallbackRunningObserver {
     30  public:
     31   CallbackRunningObserver(base::Closure callback)
     32       : completed_counter_(0),
     33         animation_aborted_(false),
     34         callback_(callback) {}
     35 
     36   void AddNewAnimator(ui::LayerAnimator* animator) {
     37     Observer* observer = new Observer(animator, this);
     38     animator->AddObserver(observer);
     39     observer_list_.push_back(observer);
     40   }
     41 
     42  private:
     43   void OnSingleTaskCompleted() {
     44     completed_counter_++;
     45     if (completed_counter_ >= observer_list_.size()) {
     46       base::MessageLoopForUI::current()->DeleteSoon(FROM_HERE, this);
     47       if (!animation_aborted_)
     48         base::MessageLoopForUI::current()->PostTask(FROM_HERE, callback_);
     49     }
     50   }
     51 
     52   void OnSingleTaskAborted() {
     53     animation_aborted_ = true;
     54     OnSingleTaskCompleted();
     55   }
     56 
     57   // The actual observer to listen each animation completion.
     58   class Observer : public ui::LayerAnimationObserver {
     59    public:
     60     Observer(ui::LayerAnimator* animator,
     61              CallbackRunningObserver* observer)
     62         : animator_(animator),
     63           observer_(observer) {}
     64 
     65    protected:
     66     // ui::LayerAnimationObserver overrides:
     67     virtual void OnLayerAnimationEnded(
     68         ui::LayerAnimationSequence* sequence) OVERRIDE {
     69       animator_->RemoveObserver(this);
     70       observer_->OnSingleTaskCompleted();
     71     }
     72     virtual void OnLayerAnimationAborted(
     73         ui::LayerAnimationSequence* sequence) OVERRIDE {
     74       animator_->RemoveObserver(this);
     75       observer_->OnSingleTaskAborted();
     76     }
     77     virtual void OnLayerAnimationScheduled(
     78         ui::LayerAnimationSequence* sequence) OVERRIDE {
     79     }
     80     virtual bool RequiresNotificationWhenAnimatorDestroyed() const OVERRIDE {
     81       return true;
     82     }
     83 
     84    private:
     85     ui::LayerAnimator* animator_;
     86     CallbackRunningObserver* observer_;
     87 
     88     DISALLOW_COPY_AND_ASSIGN(Observer);
     89   };
     90 
     91   size_t completed_counter_;
     92   bool animation_aborted_;
     93   ScopedVector<Observer> observer_list_;
     94   base::Closure callback_;
     95 
     96   DISALLOW_COPY_AND_ASSIGN(CallbackRunningObserver);
     97 };
     98 
     99 }  // namespace
    100 
    101 OutputConfiguratorAnimation::OutputConfiguratorAnimation() {
    102 }
    103 
    104 OutputConfiguratorAnimation::~OutputConfiguratorAnimation() {
    105   ClearHidingLayers();
    106 }
    107 
    108 void OutputConfiguratorAnimation::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 = ash::Shell::GetContainer(
    126         root_window,
    127         ash::internal::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<OutputConfiguratorAnimation>());
    145   timer_->Start(FROM_HERE,
    146                 base::TimeDelta::FromSeconds(kFadingTimeoutDurationInSeconds),
    147                 this,
    148                 &OutputConfiguratorAnimation::ClearHidingLayers);
    149 }
    150 
    151 void OutputConfiguratorAnimation::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(&OutputConfiguratorAnimation::ClearHidingLayers,
    158                  base::Unretained(this)));
    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 = ash::Shell::GetContainer(
    185           root_window,
    186           ash::internal::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 
    206 void OutputConfiguratorAnimation::OnDisplayModeChanged(
    207     const std::vector<chromeos::OutputConfigurator::OutputSnapshot>& outputs) {
    208   if (!hiding_layers_.empty())
    209     StartFadeInAnimation();
    210 }
    211 
    212 void OutputConfiguratorAnimation::OnDisplayModeChangeFailed(
    213     chromeos::OutputState failed_new_state) {
    214   if (!hiding_layers_.empty())
    215     StartFadeInAnimation();
    216 }
    217 
    218 void OutputConfiguratorAnimation::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 internal
    229 }  // namespace ash
    230