forked from chromium/chromium
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathimage_animation_controller.cc
431 lines (357 loc) · 15 KB
/
image_animation_controller.cc
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
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
// Copyright 2017 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "cc/trees/image_animation_controller.h"
#include "base/bind.h"
#include "base/metrics/histogram_macros.h"
#include "base/trace_event/trace_event.h"
#include "cc/paint/image_animation_count.h"
namespace cc {
namespace {
// The maximum number of time an animation can be delayed before it is reset to
// start from the beginning, instead of fast-forwarding to catch up to the
// desired frame.
const base::TimeDelta kAnimationResyncCutoff = base::TimeDelta::FromMinutes(5);
} // namespace
ImageAnimationController::ImageAnimationController(
base::SingleThreadTaskRunner* task_runner,
base::Closure invalidation_callback)
: notifier_(task_runner, invalidation_callback) {}
ImageAnimationController::~ImageAnimationController() = default;
void ImageAnimationController::UpdateAnimatedImage(
const DiscardableImageMap::AnimatedImageMetadata& data) {
AnimationState& animation_state = animation_state_map_[data.paint_image_id];
animation_state.UpdateMetadata(data);
}
void ImageAnimationController::RegisterAnimationDriver(
PaintImage::Id paint_image_id,
AnimationDriver* driver) {
auto it = animation_state_map_.find(paint_image_id);
DCHECK(it != animation_state_map_.end());
it->second.AddDriver(driver);
registered_animations_.insert(paint_image_id);
}
void ImageAnimationController::UnregisterAnimationDriver(
PaintImage::Id paint_image_id,
AnimationDriver* driver) {
auto it = animation_state_map_.find(paint_image_id);
DCHECK(it != animation_state_map_.end());
it->second.RemoveDriver(driver);
if (!it->second.has_drivers())
registered_animations_.erase(paint_image_id);
}
const PaintImageIdFlatSet& ImageAnimationController::AnimateForSyncTree(
base::TimeTicks now) {
TRACE_EVENT0("cc", "ImageAnimationController::AnimateImagesForSyncTree");
DCHECK(images_animated_on_sync_tree_.empty());
notifier_.WillAnimate();
base::Optional<base::TimeTicks> next_invalidation_time;
for (auto id : registered_animations_) {
auto it = animation_state_map_.find(id);
DCHECK(it != animation_state_map_.end());
AnimationState& state = it->second;
// Is anyone still interested in animating this image?
state.UpdateStateFromDrivers();
if (!state.ShouldAnimate())
continue;
// If we were able to advance this animation, invalidate it on the sync
// tree.
if (state.AdvanceFrame(now))
images_animated_on_sync_tree_.insert(id);
// Update the next invalidation time to the earliest time at which we need
// a frame to animate an image.
// Note its important to check ShouldAnimate() here again since advancing to
// a new frame on the sync tree means we might not need to animate this
// image any longer.
if (!state.ShouldAnimate())
continue;
DCHECK_GT(state.next_desired_frame_time(), now);
if (!next_invalidation_time.has_value()) {
next_invalidation_time.emplace(state.next_desired_frame_time());
} else {
next_invalidation_time = std::min(state.next_desired_frame_time(),
next_invalidation_time.value());
}
}
if (next_invalidation_time.has_value())
notifier_.Schedule(now, next_invalidation_time.value());
else
notifier_.Cancel();
return images_animated_on_sync_tree_;
}
void ImageAnimationController::UpdateStateFromDrivers(base::TimeTicks now) {
TRACE_EVENT0("cc", "UpdateStateFromAnimationDrivers");
base::Optional<base::TimeTicks> next_invalidation_time;
for (auto image_id : registered_animations_) {
auto it = animation_state_map_.find(image_id);
DCHECK(it != animation_state_map_.end());
AnimationState& state = it->second;
state.UpdateStateFromDrivers();
// Note that by not updating the |next_invalidation_time| from this image
// here, we will cancel any pending invalidation scheduled for this image
// when updating the |notifier_| at the end of this loop.
if (!state.ShouldAnimate())
continue;
if (!next_invalidation_time.has_value()) {
next_invalidation_time.emplace(state.next_desired_frame_time());
} else {
next_invalidation_time = std::min(next_invalidation_time.value(),
state.next_desired_frame_time());
}
}
if (next_invalidation_time.has_value())
notifier_.Schedule(now, next_invalidation_time.value());
else
notifier_.Cancel();
}
void ImageAnimationController::DidActivate() {
TRACE_EVENT0("cc", "ImageAnimationController::WillActivate");
for (auto id : images_animated_on_sync_tree_) {
auto it = animation_state_map_.find(id);
DCHECK(it != animation_state_map_.end());
it->second.PushPendingToActive();
}
images_animated_on_sync_tree_.clear();
}
size_t ImageAnimationController::GetFrameIndexForImage(
PaintImage::Id paint_image_id,
WhichTree tree) const {
const auto& it = animation_state_map_.find(paint_image_id);
DCHECK(it != animation_state_map_.end());
return tree == WhichTree::PENDING_TREE ? it->second.pending_index()
: it->second.active_index();
}
const base::flat_set<ImageAnimationController::AnimationDriver*>&
ImageAnimationController::GetDriversForTesting(
PaintImage::Id paint_image_id) const {
const auto& it = animation_state_map_.find(paint_image_id);
DCHECK(it != animation_state_map_.end());
return it->second.drivers_for_testing();
}
size_t ImageAnimationController::GetLastNumOfFramesSkippedForTesting(
PaintImage::Id paint_image_id) const {
const auto& it = animation_state_map_.find(paint_image_id);
DCHECK(it != animation_state_map_.end());
return it->second.last_num_frames_skipped_for_testing();
}
ImageAnimationController::AnimationState::AnimationState() = default;
ImageAnimationController::AnimationState::AnimationState(
AnimationState&& other) = default;
ImageAnimationController::AnimationState&
ImageAnimationController::AnimationState::operator=(AnimationState&& other) =
default;
ImageAnimationController::AnimationState::~AnimationState() {
DCHECK(drivers_.empty());
}
bool ImageAnimationController::AnimationState::ShouldAnimate() const {
DCHECK(repetitions_completed_ == 0 || is_complete());
// If we have no drivers for this image, no need to animate it.
if (!should_animate_from_drivers_)
return false;
switch (requested_repetitions_) {
case kAnimationLoopOnce:
if (repetitions_completed_ >= 1)
return false;
break;
case kAnimationNone:
NOTREACHED() << "We shouldn't be tracking kAnimationNone images";
break;
case kAnimationLoopInfinite:
break;
default:
if (requested_repetitions_ <= repetitions_completed_)
return false;
}
// If we have not yet received all data for this image, we can not advance to
// an incomplete frame.
if (!frames_[NextFrameIndex()].complete)
return false;
// If we don't have all data for this image, we can not trust the frame count
// and loop back to the first frame.
size_t last_frame_index = frames_.size() - 1;
if (completion_state_ != PaintImage::CompletionState::DONE &&
pending_index_ == last_frame_index)
return false;
return true;
}
bool ImageAnimationController::AnimationState::AdvanceFrame(
base::TimeTicks now) {
DCHECK(ShouldAnimate());
// Start the animation from the first frame, if not yet started. We don't need
// an invalidation here if the pending and active tree are both displaying the
// first frame. Its possible for the 2 to be different if the animation was
// reset, in which case we are starting again from the first frame on the
// pending tree.
if (!animation_started_) {
DCHECK_EQ(pending_index_, 0u);
next_desired_frame_time_ = now + frames_[0].duration;
animation_started_ = true;
return pending_index_ != active_index_;
}
// Don't advance the animation if its not time yet to move to the next frame.
if (now < next_desired_frame_time_)
return false;
// If the animation is more than 5 min out of date, we don't bother catching
// up and start again from the current frame.
// Note that we don't need to invalidate this image since the active tree
// is already displaying the current frame.
if (now - next_desired_frame_time_ > kAnimationResyncCutoff) {
DCHECK_EQ(pending_index_, active_index_);
next_desired_frame_time_ = now + frames_[pending_index_].duration;
return false;
}
// Keep catching up the animation until we reach the frame we should be
// displaying now.
// TODO(khushalsagar): Avoid unnecessary iterations for skipping whole loops
// in the animations.
size_t last_frame_index = frames_.size() - 1;
size_t num_of_frames_advanced = 0u;
while (next_desired_frame_time_ <= now && ShouldAnimate()) {
num_of_frames_advanced++;
size_t next_frame_index = NextFrameIndex();
base::TimeTicks next_desired_frame_time =
next_desired_frame_time_ + frames_[next_frame_index].duration;
// The image may load more slowly than it's supposed to animate, so that by
// the time we reach the end of the first repetition, we're well behind.
// Start the animation from the first frame in this case, so that we don't
// skip frames (or whole iterations) trying to "catch up". This is a
// tradeoff: It guarantees users see the whole animation the second time
// through and don't miss any repetitions, and is closer to what other
// browsers do; on the other hand, it makes animations "less accurate" for
// pages that try to sync an image and some other resource (e.g. audio),
// especially if users switch tabs (and thus stop drawing the animation,
// which will pause it) during that initial loop, then switch back later.
if (next_frame_index == 0u && repetitions_completed_ == 1 &&
next_desired_frame_time <= now) {
pending_index_ = 0u;
next_desired_frame_time_ = now + frames_[0].duration;
repetitions_completed_ = 0;
break;
}
pending_index_ = next_frame_index;
next_desired_frame_time_ = next_desired_frame_time;
// If we are advancing to the last frame and the image has been completely
// loaded (which means that the frame count is known to be accurate), we
// just finished a loop in the animation.
if (pending_index_ == last_frame_index && is_complete())
repetitions_completed_++;
}
// We should have advanced a single frame, anything more than that are frames
// skipped trying to catch up.
DCHECK_GT(num_of_frames_advanced, 0u);
last_num_frames_skipped_ = num_of_frames_advanced - 1u;
UMA_HISTOGRAM_COUNTS_100000("AnimatedImage.NumOfFramesSkipped.Compositor",
last_num_frames_skipped_);
return pending_index_ != active_index_;
}
void ImageAnimationController::AnimationState::UpdateMetadata(
const DiscardableImageMap::AnimatedImageMetadata& data) {
paint_image_id_ = data.paint_image_id;
DCHECK_NE(data.repetition_count, kAnimationNone);
requested_repetitions_ = data.repetition_count;
DCHECK(frames_.size() <= data.frames.size())
<< "Updated recordings can only append frames";
frames_ = data.frames;
DCHECK_GT(frames_.size(), 1u);
DCHECK(completion_state_ != PaintImage::CompletionState::DONE ||
data.completion_state == PaintImage::CompletionState::DONE)
<< "If the image was marked complete before, it can not be incomplete in "
"a new update";
completion_state_ = data.completion_state;
// Update the repetition count in case we have displayed the last frame and
// we now know the frame count to be accurate.
size_t last_frame_index = frames_.size() - 1;
if (pending_index_ == last_frame_index && is_complete() &&
repetitions_completed_ == 0)
repetitions_completed_++;
// Reset the animation if the sequence id received in this recording was
// incremented.
if (reset_animation_sequence_id_ < data.reset_animation_sequence_id) {
reset_animation_sequence_id_ = data.reset_animation_sequence_id;
ResetAnimation();
}
}
void ImageAnimationController::AnimationState::PushPendingToActive() {
active_index_ = pending_index_;
}
void ImageAnimationController::AnimationState::AddDriver(
AnimationDriver* driver) {
drivers_.insert(driver);
}
void ImageAnimationController::AnimationState::RemoveDriver(
AnimationDriver* driver) {
drivers_.erase(driver);
}
void ImageAnimationController::AnimationState::UpdateStateFromDrivers() {
should_animate_from_drivers_ = false;
for (auto* driver : drivers_) {
if (driver->ShouldAnimate(paint_image_id_)) {
should_animate_from_drivers_ = true;
break;
}
}
}
void ImageAnimationController::AnimationState::ResetAnimation() {
animation_started_ = false;
next_desired_frame_time_ = base::TimeTicks();
repetitions_completed_ = 0;
pending_index_ = 0u;
// Don't reset the |active_index_|, tiles on the active tree still need it.
}
size_t ImageAnimationController::AnimationState::NextFrameIndex() const {
if (!animation_started_)
return 0u;
return (pending_index_ + 1) % frames_.size();
}
ImageAnimationController::DelayedNotifier::DelayedNotifier(
base::SingleThreadTaskRunner* task_runner,
base::Closure closure)
: task_runner_(task_runner),
closure_(std::move(closure)),
weak_factory_(this) {
DCHECK(task_runner_->BelongsToCurrentThread());
}
ImageAnimationController::DelayedNotifier::~DelayedNotifier() {
DCHECK(task_runner_->BelongsToCurrentThread());
}
void ImageAnimationController::DelayedNotifier::Schedule(
base::TimeTicks now,
base::TimeTicks notification_time) {
// If an animation is already pending, don't schedule another invalidation.
// We will schedule the next invalidation based on the latest animation state
// during AnimateForSyncTree.
if (animation_pending_)
return;
// The requested notification time can be in the past. For instance, if an
// animation was paused because the image became invisible.
if (notification_time < now)
notification_time = now;
// If we already have a notification scheduled to run at this time, no need to
// Cancel it.
if (pending_notification_time_.has_value() &&
notification_time == pending_notification_time_.value())
return;
// Cancel the pending notification since we the requested notification time
// has changed.
Cancel();
TRACE_EVENT2("cc", "ScheduleInvalidationForImageAnimation",
"notification_time", notification_time, "now", now);
pending_notification_time_.emplace(notification_time);
task_runner_->PostDelayedTask(
FROM_HERE,
base::Bind(&DelayedNotifier::Notify, weak_factory_.GetWeakPtr()),
notification_time - now);
}
void ImageAnimationController::DelayedNotifier::Cancel() {
pending_notification_time_.reset();
weak_factory_.InvalidateWeakPtrs();
}
void ImageAnimationController::DelayedNotifier::Notify() {
pending_notification_time_.reset();
animation_pending_ = true;
closure_.Run();
}
void ImageAnimationController::DelayedNotifier::WillAnimate() {
animation_pending_ = false;
}
} // namespace cc