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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
ash / wm / raster_scale / raster_scale_layer_observer.cc [blame]
// Copyright 2023 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/wm/raster_scale/raster_scale_layer_observer.h"
#include "ash/shell.h"
#include "ash/wm/raster_scale/raster_scale_controller.h"
#include "ash/wm/window_util.h"
#include "base/logging.h"
#include "ui/aura/client/transient_window_client.h"
#include "ui/compositor/layer_animation_element.h"
#include "ui/compositor/layer_animation_sequence.h"
#include "ui/compositor/property_change_reason.h"
#include "ui/wm/core/window_util.h"
namespace ash {
namespace {
#if DCHECK_IS_ON()
gfx::Transform AncestorTransform(aura::Window* window) {
gfx::Transform transform;
while (window->parent() != nullptr) {
window = window->parent();
transform = window->transform() * transform;
}
return transform;
}
#endif
void DLogIfAncestorTransformIsNotIdentity(aura::Window* window) {
// Currently, TSR computes the raster scale purely from the local transform.
// We don't expect there to be a non-identity transform for ancestors of TSR
// applied windows, but check it in debug builds here. If there is a
// non-identity transform, we need to include it somehow. The full (and
// overcomplex) fix for this would be observing animations and window
// transforms on every ancestor window and computing the maximum raster scale
// for each span between each event point we are okay taking the compositor
// lock at (e.g. animation start/stop on any ancestor window).
DLOG_IF(ERROR, !AncestorTransform(window).IsIdentity())
<< "Unexpected non-identity transform from parent window coordinates to "
"root window coordinates";
}
} // namespace
ScopedRasterScaleLayerObserverLock::ScopedRasterScaleLayerObserverLock(
base::WeakPtr<RasterScaleLayerObserver> observer)
: observer_(observer) {
observer_->IncrementRefCount();
}
ScopedRasterScaleLayerObserverLock::~ScopedRasterScaleLayerObserverLock() {
if (observer_) {
observer_->DecrementRefCount();
}
}
ScopedRasterScaleLayerObserverLock::ScopedRasterScaleLayerObserverLock(
ScopedRasterScaleLayerObserverLock&&) = default;
ScopedRasterScaleLayerObserverLock&
ScopedRasterScaleLayerObserverLock::operator=(
ScopedRasterScaleLayerObserverLock&&) = default;
RasterScaleLayerObserver::RasterScaleLayerObserver(aura::Window* observe_window,
ui::Layer* observe_layer,
aura::Window* apply_window)
: observe_window_(observe_window),
observe_layer_(observe_layer),
apply_window_(apply_window) {
for (aura::Window* transient_child :
GetTransientTreeIterator(apply_window_)) {
transient_windows_.insert(transient_child);
}
if (observe_window->IsVisible()) {
UpdateRasterScaleFromTransform(observe_layer_->transform());
}
if (!windows_observation_.IsObservingSource(observe_window_)) {
windows_observation_.AddObservation(observe_window_);
}
if (!windows_observation_.IsObservingSource(apply_window_)) {
windows_observation_.AddObservation(apply_window_);
}
observe_layer_->AddObserver(this);
observe_layer_->GetAnimator()->AddObserver(this);
aura::client::GetTransientWindowClient()->AddObserver(this);
}
RasterScaleLayerObserver::~RasterScaleLayerObserver() {
aura::client::GetTransientWindowClient()->RemoveObserver(this);
if (observe_layer_) {
observe_layer_->GetAnimator()->RemoveObserver(this);
observe_layer_->RemoveObserver(this);
}
}
void RasterScaleLayerObserver::OnLayerAnimationStarted(
ui::LayerAnimationSequence* sequence) {
animation_count_++;
if (observe_window_ == nullptr || observe_layer_ == nullptr ||
apply_window_ == nullptr) {
return;
}
// It's complex to support more than one element in the
// LayerAnimationSequence, because we would need to match raster scale
// updates with the progression of the animation, which would need
// framework changes. Also, it would introduce jank at each new element
// during the animation, since changing the raster scale will take a
// compositor lock to synchronize the raster scale update. Currently, the
// only animation with at least one element affecting the transform and
// with at least two elements that would apply to windows is the window
// bounce animation, which we do not want to update raster scale for,
// since it's very transient and doesn't change the scale much. So, we
// will do nothing here if there is more than one element in the animation
// sequence.
if (sequence->size() > 1) {
return;
}
ui::LayerAnimationElement::TargetValue value;
sequence->FirstElement()->GetTargetValue(&value);
// Don't do anything if we will never be visible.
if (!observe_window_->IsVisible() && !value.visibility) {
return;
}
// Don't do anything for things not animating the transform.
if (!(sequence->properties() &
ui::LayerAnimationElement::AnimatableProperty::TRANSFORM)) {
return;
}
// Don't update if nothing happens until the animation is done:
if (sequence->FirstElement()->tween_type() == gfx::Tween::ZERO) {
return;
}
const auto new_scale =
RasterScaleController::RasterScaleFromTransform(value.transform);
const auto old_scale =
Shell::Get()->raster_scale_controller()->ComputeRasterScaleForWindow(
apply_window_);
if (new_scale >= old_scale) {
SetRasterScales(new_scale);
}
}
void RasterScaleLayerObserver::OnLayerAnimationEnded(
ui::LayerAnimationSequence* sequence) {
animation_count_--;
if (animation_count_ == 0 && ref_count_ != 0) {
// We can always apply no matter what here, since the animation is done.
UpdateRasterScaleFromTransform(observe_layer_->transform());
}
MaybeShutdown();
}
void RasterScaleLayerObserver::OnLayerAnimationWillRepeat(
ui::LayerAnimationSequence* sequence) {
DLOG(ERROR) << "Unexpected repeating layer animation.";
}
void RasterScaleLayerObserver::OnLayerAnimationAborted(
ui::LayerAnimationSequence* sequence) {
animation_count_--;
MaybeShutdown();
}
void RasterScaleLayerObserver::OnLayerAnimationScheduled(
ui::LayerAnimationSequence* sequence) {}
void RasterScaleLayerObserver::OnWindowVisibilityChanged(aura::Window* window,
bool visible) {
if (observe_window_ == nullptr || observe_layer_ == nullptr ||
apply_window_ == nullptr) {
return;
}
if (window != observe_window_) {
return;
}
UpdateRasterScaleFromTransform(observe_layer_->transform());
}
void RasterScaleLayerObserver::OnWindowDestroying(aura::Window* window) {
windows_observation_.RemoveObservation(window);
if (window == observe_window_) {
observe_window_ = nullptr;
}
if (window == apply_window_) {
apply_window_ = nullptr;
}
MaybeShutdown();
}
void RasterScaleLayerObserver::OnWindowBoundsChanged(
aura::Window* window,
const gfx::Rect& old_bounds,
const gfx::Rect& new_bounds,
ui::PropertyChangeReason reason) {
if (observe_window_ == nullptr || observe_layer_ == nullptr ||
apply_window_ == nullptr) {
return;
}
if (window != observe_window_) {
return;
}
// Needed for showing minimized windows via mirror view.
if (reason == ui::PropertyChangeReason::NOT_FROM_ANIMATION) {
UpdateRasterScaleFromTransform(observe_layer_->transform());
}
}
void RasterScaleLayerObserver::OnWindowTransformed(
aura::Window* window,
ui::PropertyChangeReason reason) {
if (observe_window_ == nullptr || observe_layer_ == nullptr ||
apply_window_ == nullptr) {
return;
}
if (window != observe_window_) {
return;
}
// Transformations happening in an animation are handled separately.
if (reason == ui::PropertyChangeReason::NOT_FROM_ANIMATION) {
UpdateRasterScaleFromTransform(observe_layer_->transform());
}
}
void RasterScaleLayerObserver::OnWindowLayerRecreated(aura::Window* window) {
if (window != observe_window_) {
return;
}
// OnWindowLayerRecreated shouldn't be called for mirror layers, so safe to
// update here.
if (observe_layer_) {
observe_layer_->GetAnimator()->RemoveObserver(this);
observe_layer_->RemoveObserver(this);
}
// Animations are not carried over to the new layer.
animation_count_ = 0;
observe_layer_ = window->layer();
observe_layer_->AddObserver(this);
observe_layer_->GetAnimator()->AddObserver(this);
MaybeShutdown();
}
void RasterScaleLayerObserver::LayerDestroyed(ui::Layer* layer) {
CHECK_EQ(layer, observe_layer_);
observe_layer_ = nullptr;
animation_count_ = 0;
MaybeShutdown();
}
void RasterScaleLayerObserver::OnTransientChildWindowAdded(
aura::Window* parent,
aura::Window* transient_child) {
if (parent != apply_window_ &&
!::wm::HasTransientAncestor(parent, apply_window_)) {
return;
}
transient_windows_.insert(transient_child);
// Apply the existing raster scale to the new transient window.
auto iter = raster_scales_.find(apply_window_);
if (iter == raster_scales_.end()) {
return;
}
const float raster_scale = iter->second->raster_scale();
ScopedSetRasterScale::SetOrUpdateRasterScale(
transient_child, raster_scale, &raster_scales_[transient_child]);
}
void RasterScaleLayerObserver::OnTransientChildWindowRemoved(
aura::Window* parent,
aura::Window* transient_child) {
if (parent != apply_window_ &&
!wm::HasTransientAncestor(parent, apply_window_)) {
return;
}
// Stop applying raster scale to the transient window and forget it.
transient_windows_.erase(transient_child);
raster_scales_.erase(transient_child);
}
void RasterScaleLayerObserver::SetRasterScales(float raster_scale) {
DLogIfAncestorTransformIsNotIdentity(observe_window_);
ScopedSetRasterScale::SetOrUpdateRasterScale(apply_window_, raster_scale,
&raster_scales_[apply_window_]);
for (aura::Window* window : transient_windows_) {
ScopedSetRasterScale::SetOrUpdateRasterScale(window, raster_scale,
&raster_scales_[window]);
}
}
void RasterScaleLayerObserver::UpdateRasterScaleFromTransform(
const gfx::Transform& transform) {
if (observe_window_ == nullptr || observe_layer_ == nullptr ||
apply_window_ == nullptr) {
return;
}
if (observe_window_->IsVisible()) {
const auto raster_scale =
RasterScaleController::RasterScaleFromTransform(transform);
SetRasterScales(raster_scale);
} else {
// This content is not shown via the window, so stop holding a raster scale
// lock on it.
raster_scales_.clear();
}
}
void RasterScaleLayerObserver::IncrementRefCount() {
ref_count_++;
}
void RasterScaleLayerObserver::DecrementRefCount() {
ref_count_--;
MaybeShutdown();
}
void RasterScaleLayerObserver::MaybeShutdown() {
// If there are no animations (we won't do anything in the future) and no more
// refs, we can delete.
if (ref_count_ != 0 || animation_count_ != 0) {
return;
}
delete this;
}
} // namespace ash