1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169
|
// Copyright 2021 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "ash/utility/layer_copy_animator.h"
#include "ash/utility/layer_util.h"
#include "base/functional/bind.h"
#include "ui/aura/window.h"
#include "ui/base/class_property.h"
#include "ui/compositor/layer_animation_sequence.h"
#include "ui/compositor/layer_animator.h"
#include "ui/compositor/layer_type.h"
DEFINE_UI_CLASS_PROPERTY_TYPE(ash::LayerCopyAnimator*)
namespace ash {
namespace {
DEFINE_OWNED_UI_CLASS_PROPERTY_KEY(LayerCopyAnimator, kLayerCopyAnimatorKey)
// CopyOutputRequest's callback may be called on the different thread during
// shutdown, which results in the DCHECK failure in the weak ptr when
// referenced.
void MaybeLayerCopied(base::WeakPtr<LayerCopyAnimator> swc,
std::unique_ptr<ui::Layer> new_layer) {
if (swc) {
swc->OnLayerCopied(std::move(new_layer));
}
}
} // namespace
// static
LayerCopyAnimator* LayerCopyAnimator::Get(aura::Window* window) {
return window->GetProperty(kLayerCopyAnimatorKey);
}
LayerCopyAnimator::LayerCopyAnimator(aura::Window* window) : window_(window) {
window->SetProperty(kLayerCopyAnimatorKey, this);
observation_.Observe(window);
// Copy request will not copy NOT_DRAWN and the result may be smaller than
// requested layer. Create a transparent layer to cover the entire layer.
if (window_->layer()->type() == ui::LAYER_NOT_DRAWN) {
full_layer_.SetColor(SK_ColorTRANSPARENT);
full_layer_.SetBounds(gfx::Rect(window_->bounds().size()));
window_->layer()->Add(&full_layer_);
window_->layer()->StackAtBottom(&full_layer_);
}
window_->layer()->GetAnimator()->StopAnimating();
window_->layer()->SetOpacity(1.f);
CopyLayerContentToNewLayer(
window_->layer(),
base::BindOnce(&MaybeLayerCopied, weak_ptr_factory_.GetWeakPtr()));
}
LayerCopyAnimator::~LayerCopyAnimator() {
window_->layer()->SetOpacity(1.0f);
if (fake_sequence_)
NotifyWithFakeSequence(/*abort=*/true);
DCHECK(!observer_);
}
void LayerCopyAnimator::MaybeStartAnimation(
ui::LayerAnimationObserver* observer,
AnimationCallback callback) {
DCHECK(!animation_requested_);
observer_ = observer;
animation_callback_ = std::move(callback);
animation_requested_ = true;
if (fail_) {
FinishAndDelete(/*abort=*/true);
return;
}
if (copied_layer_) {
RunAnimation();
return;
}
if (observer_)
EnsureFakeSequence();
}
void LayerCopyAnimator::OnLayerCopied(std::unique_ptr<ui::Layer> new_layer) {
if (fail_)
return;
if (!new_layer)
fail_ = true;
copied_layer_ = std::move(new_layer);
if (animation_requested_ && !fail_)
RunAnimation();
}
void LayerCopyAnimator::OnLayerAnimationEnded(
ui::LayerAnimationSequence* sequence) {
if (last_sequence_ == sequence)
window_->ClearProperty(kLayerCopyAnimatorKey);
}
void LayerCopyAnimator::OnLayerAnimationAborted(
ui::LayerAnimationSequence* sequence) {
window_->ClearProperty(kLayerCopyAnimatorKey);
}
void LayerCopyAnimator::OnWindowBoundsChanged(aura::Window* window,
const gfx::Rect& old_bounds,
const gfx::Rect& new_bounds,
ui::PropertyChangeReason reason) {
fail_ = true;
if (copied_layer_)
copied_layer_->GetAnimator()->StopAnimating();
}
void LayerCopyAnimator::RunAnimation() {
CHECK_EQ(copied_layer_->type(), ui::LAYER_SOLID_COLOR);
auto* parent_layer = window_->layer()->parent();
parent_layer->Add(copied_layer_.get());
parent_layer->StackAbove(copied_layer_.get(), window_->layer());
window_->layer()->SetOpacity(0.f);
std::move(animation_callback_).Run(copied_layer_.get(), observer_.get());
// Callback may not run animations, in which case, just end immediately.
if (!copied_layer_->GetAnimator()->is_animating()) {
FinishAndDelete(/*abort=*/false);
return;
}
if (fake_sequence_)
NotifyWithFakeSequence(/*abort=*/false);
observer_ = nullptr;
last_sequence_ = new ui::LayerAnimationSequence(
ui::LayerAnimationElement::CreateOpacityElement(1.0, base::TimeDelta()));
copied_layer_->GetAnimator()->ScheduleAnimation(last_sequence_);
copied_layer_->GetAnimator()->AddObserver(this);
}
void LayerCopyAnimator::FinishAndDelete(bool abort) {
if (observer_) {
EnsureFakeSequence();
NotifyWithFakeSequence(abort);
}
window_->ClearProperty(kLayerCopyAnimatorKey);
}
void LayerCopyAnimator::NotifyWithFakeSequence(bool abort) {
DCHECK(fake_sequence_);
if (abort)
observer_->OnLayerAnimationAborted(fake_sequence_.get());
else
observer_->OnLayerAnimationEnded(fake_sequence_.get());
fake_sequence_.reset();
observer_ = nullptr;
}
void LayerCopyAnimator::EnsureFakeSequence() {
if (fake_sequence_)
return;
fake_sequence_ = std::make_unique<ui::LayerAnimationSequence>(
ui::LayerAnimationElement::CreateOpacityElement(0.0, base::TimeDelta()));
fake_sequence_->AddObserver(observer_);
}
} // namespace ash
|