File: animation_host.cc

package info (click to toggle)
chromium 139.0.7258.127-1
  • links: PTS, VCS
  • area: main
  • in suites:
  • size: 6,122,068 kB
  • sloc: cpp: 35,100,771; ansic: 7,163,530; javascript: 4,103,002; python: 1,436,920; asm: 946,517; xml: 746,709; pascal: 187,653; perl: 88,691; sh: 88,436; objc: 79,953; sql: 51,488; cs: 44,583; fortran: 24,137; makefile: 22,147; tcl: 15,277; php: 13,980; yacc: 8,984; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (952 lines) | stat: -rw-r--r-- 34,422 bytes parent folder | download | duplicates (3)
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
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
// Copyright 2015 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "cc/animation/animation_host.h"

#include <algorithm>
#include <memory>
#include <utility>

#include "base/auto_reset.h"
#include "base/containers/contains.h"
#include "base/functional/bind.h"
#include "base/functional/callback.h"
#include "base/memory/ptr_util.h"
#include "base/memory/raw_ptr.h"
#include "base/trace_event/trace_event.h"
#include "base/trace_event/traced_value.h"
#include "cc/animation/animation.h"
#include "cc/animation/animation_delegate.h"
#include "cc/animation/animation_events.h"
#include "cc/animation/animation_id_provider.h"
#include "cc/animation/animation_timeline.h"
#include "cc/animation/element_animations.h"
#include "cc/animation/keyframe_effect.h"
#include "cc/animation/scroll_offset_animation_curve.h"
#include "cc/animation/scroll_offset_animations.h"
#include "cc/animation/scroll_offset_animations_impl.h"
#include "cc/animation/scroll_timeline.h"
#include "cc/animation/worklet_animation.h"
#include "ui/gfx/animation/keyframe/timing_function.h"
#include "ui/gfx/geometry/vector2d_f.h"

namespace cc {

namespace {

AnimationWorkletMutationState ToAnimationWorkletMutationState(
    MutateStatus status) {
  switch (status) {
    case MutateStatus::kCompletedWithUpdate:
      return AnimationWorkletMutationState::COMPLETED_WITH_UPDATE;

    case MutateStatus::kCompletedNoUpdate:
      return AnimationWorkletMutationState::COMPLETED_NO_UPDATE;

    case MutateStatus::kCanceled:
      return AnimationWorkletMutationState::CANCELED;
  }
}

}  // namespace

std::unique_ptr<AnimationHost> AnimationHost::CreateMainInstance() {
  return base::WrapUnique(new AnimationHost(ThreadInstance::kMain));
}

std::unique_ptr<AnimationHost> AnimationHost::CreateForTesting(
    ThreadInstance thread_instance) {
  auto animation_host = base::WrapUnique(new AnimationHost(thread_instance));

  return animation_host;
}

AnimationHost::AnimationHost(ThreadInstance thread_instance)
    : thread_instance_(thread_instance) {}

AnimationHost::~AnimationHost() {
  ClearMutators();
  DCHECK(!mutator_host_client());
}

std::unique_ptr<MutatorHost> AnimationHost::CreateImplInstance() const {
  DCHECK_EQ(thread_instance_, ThreadInstance::kMain);
  auto mutator_host_impl =
      base::WrapUnique<MutatorHost>(new AnimationHost(ThreadInstance::kImpl));
  return mutator_host_impl;
}

const AnimationTimeline* AnimationHost::GetTimelineById(int timeline_id) const {
  auto f = id_to_timeline_map_.Read(*this).find(timeline_id);
  return f == id_to_timeline_map_.Read(*this).end() ? nullptr : f->second.get();
}

AnimationTimeline* AnimationHost::GetTimelineById(int timeline_id) {
  auto f = id_to_timeline_map_.Write(*this).find(timeline_id);
  return f == id_to_timeline_map_.Write(*this).end() ? nullptr
                                                     : f->second.get();
}

void AnimationHost::ClearMutators() {
  for (auto& kv : id_to_timeline_map_.Read(*this))
    EraseTimeline(kv.second);
  id_to_timeline_map_.Write(*this).clear();
}

base::TimeDelta AnimationHost::MinimumTickInterval() const {
  base::TimeDelta min_interval = base::TimeDelta::Max();
  for (const auto& animation : ticking_animations_.Read(*this)) {
    DCHECK(animation->keyframe_effect());
    base::TimeDelta interval =
        animation->keyframe_effect()->MinimumTickInterval();
    if (interval.is_zero())
      return interval;
    if (interval < min_interval)
      min_interval = interval;
  }
  return min_interval;
}

void AnimationHost::EraseTimeline(scoped_refptr<AnimationTimeline> timeline) {
  timeline->ClearAnimations();
  timeline->SetAnimationHost(nullptr);
}

void AnimationHost::AddAnimationTimeline(
    scoped_refptr<AnimationTimeline> timeline) {
  DCHECK(timeline->id());
  id_to_timeline_map_.Write(*this).insert(
      std::make_pair(timeline->id(), timeline));
  timeline->SetAnimationHost(this);
  SetNeedsPushProperties();
}

void AnimationHost::RemoveAnimationTimeline(
    scoped_refptr<AnimationTimeline> timeline) {
  DCHECK(timeline->id());
  EraseTimeline(timeline);
  id_to_timeline_map_.Write(*this).erase(timeline->id());
  SetNeedsPushProperties();
}

void AnimationHost::DetachAnimationTimeline(
    scoped_refptr<AnimationTimeline> timeline) {
  if (InProtectedSequence()) {
    // Defer cleanup until post-commit.
    detached_timeline_map_.Write(*this).insert(
        std::make_pair(timeline->id(), timeline));
  } else {
    RemoveAnimationTimeline(timeline);
  }
}

void AnimationHost::SetHasCanvasInvalidation(bool has_canvas_invalidation) {
  has_canvas_invalidation_.Write(*this) = has_canvas_invalidation;
}

bool AnimationHost::HasCanvasInvalidation() const {
  return has_canvas_invalidation_.Read(*this);
}

bool AnimationHost::HasJSAnimation() const {
  return has_inline_style_mutation_.Read(*this);
}

void AnimationHost::SetHasInlineStyleMutation(bool has_inline_style_mutation) {
  has_inline_style_mutation_.Write(*this) = has_inline_style_mutation;
}

bool AnimationHost::HasSmilAnimation() const {
  return has_smil_animation_.Read(*this);
}

void AnimationHost::SetHasSmilAnimation(bool has_smil_animation) {
  has_smil_animation_.Write(*this) = has_smil_animation;
}

bool AnimationHost::HasViewTransition() const {
  return has_view_transition_.Read(*this);
}

void AnimationHost::SetHasViewTransition(bool has_view_transition) {
  has_view_transition_.Write(*this) = has_view_transition;
}

void AnimationHost::SetCurrentFrameHadRaf(bool current_frame_had_raf) {
  current_frame_had_raf_.Write(*this) = current_frame_had_raf;
}

bool AnimationHost::CurrentFrameHadRAF() const {
  return current_frame_had_raf_.Read(*this);
}

void AnimationHost::SetNextFrameHasPendingRaf(bool next_frame_has_pending_raf) {
  next_frame_has_pending_raf_.Write(*this) = next_frame_has_pending_raf;
}

bool AnimationHost::NextFrameHasPendingRAF() const {
  return next_frame_has_pending_raf_.Read(*this);
}

void AnimationHost::InitClientAnimationState() {
  for (auto map_entry : element_to_animations_map_.Write(*this))
    map_entry.second->InitClientAnimationState();
}

void AnimationHost::RemoveElementId(ElementId element_id) {
  scoped_refptr<ElementAnimations> element_animations =
      GetElementAnimationsForElementId(element_id);
  if (element_animations) {
    DCHECK(!element_animations->HasTickingKeyframeEffect());
    element_animations->RemoveKeyframeEffects();
  }
}

void AnimationHost::RegisterAnimationForElement(ElementId element_id,
                                                Animation* animation) {
  DCHECK(element_id);
  DCHECK(animation);
#if DCHECK_IS_ON()
  for (const auto& keyframe_model :
       animation->keyframe_effect()->keyframe_models()) {
    KeyframeModel* cc_keyframe_model =
        KeyframeModel::ToCcKeyframeModel(keyframe_model.get());
    ElementId model_element_id = cc_keyframe_model->element_id()
                                     ? cc_keyframe_model->element_id()
                                     : element_id;
    DCHECK(cc_keyframe_model->affects_active_elements() ||
           cc_keyframe_model->affects_pending_elements());
    DCHECK(!cc_keyframe_model->affects_active_elements() ||
           mutator_host_client()->IsElementInPropertyTrees(
               model_element_id, ElementListType::ACTIVE));
    // Test thread_instance_ because LayerTreeHost has no pending tree.
    DCHECK(thread_instance_ == ThreadInstance::kMain ||
           !cc_keyframe_model->affects_pending_elements() ||
           mutator_host_client()->IsElementInPropertyTrees(
               model_element_id, ElementListType::PENDING));
  }
#endif

  scoped_refptr<ElementAnimations> element_animations =
      GetElementAnimationsForElementId(element_id);
  if (!element_animations) {
    element_animations = ElementAnimations::Create(this, element_id);
    element_to_animations_map_.Write(*this)[element_animations->element_id()] =
        element_animations;
  }

  DCHECK(element_animations->AnimationHostIs(this));

  element_animations->AddKeyframeEffect(animation->keyframe_effect());
}

void AnimationHost::UnregisterAnimationForElement(ElementId element_id,
                                                  Animation* animation) {
  DCHECK(element_id);
  DCHECK(animation);

  scoped_refptr<ElementAnimations> element_animations =
      GetElementAnimationsForElementId(element_id);
  DCHECK(element_animations);

  // |ClearAffectedElementTypes| requires an ElementId map in order to update
  // the property trees. Generating that map requires walking the keyframe
  // effects, so we have to do it before removing this one.
  PropertyToElementIdMap element_id_map =
      element_animations->GetPropertyToElementIdMap();

  element_animations->RemoveKeyframeEffect(animation->keyframe_effect());

  if (element_animations->IsEmpty()) {
    element_animations->ClearAffectedElementTypes(element_id_map);
    element_to_animations_map_.Write(*this).erase(
        element_animations->element_id());
    element_animations->ClearAnimationHost();
  }

  RemoveFromTicking(animation);
}

void AnimationHost::UpdateClientAnimationStateForElementAnimations(
    ElementId element_id) {
  auto* element_animations = GetElementAnimationsForElementId(element_id).get();
  if (element_animations)
    element_animations->UpdateClientAnimationState();
}

void AnimationHost::SetMutatorHostClient(MutatorHostClient* client) {
  if (mutator_host_client() == client)
    return;

  WaitForProtectedSequenceCompletion();

  if (!client) {
    scroll_offset_animations_impl_.Write(*this).reset();
    scroll_offset_animations_.Write(*this).reset();
    ClearMutators();
  }

  mutator_host_client_ = client;

  // Creating ScrollOffsetAnimationsImpl calls back into this, triggering
  // DCHECKs that are easier to verify once `mutator_host_client_` has been
  // set.
  if (mutator_host_client() && !scroll_offset_animations_impl_.Read(*this)) {
    if (thread_instance_ == ThreadInstance::kImpl) {
      scroll_offset_animations_impl_.Write(*this) =
          std::make_unique<ScrollOffsetAnimationsImpl>(this);
    } else {
      scroll_offset_animations_.Write(*this) =
          std::make_unique<ScrollOffsetAnimations>(this);
    }
  }

  if (mutator_host_client() && needs_push_properties_.Read(*this))
    mutator_host_client()->SetMutatorsNeedCommit();
}

bool AnimationHost::IsOwnerThread() const {
  return !mutator_host_client_ || mutator_host_client_->IsOwnerThread();
}

bool AnimationHost::InProtectedSequence() const {
  return !mutator_host_client_ || mutator_host_client_->InProtectedSequence();
}

void AnimationHost::WaitForProtectedSequenceCompletion() const {
  if (mutator_host_client_)
    mutator_host_client_->WaitForProtectedSequenceCompletion();
}

void AnimationHost::SetNeedsCommit() {
  DCHECK(mutator_host_client());
  DCHECK(IsOwnerThread());
  DCHECK(!InProtectedSequence());
  mutator_host_client()->SetMutatorsNeedCommit();
  // TODO(loyso): Invalidate property trees only if really needed.
  mutator_host_client()->SetMutatorsNeedRebuildPropertyTrees();
}

void AnimationHost::SetNeedsPushProperties() {
  if (needs_push_properties())
    return;
  needs_push_properties_.Write(*this) = true;
  if (mutator_host_client())
    mutator_host_client()->SetMutatorsNeedCommit();
}

void AnimationHost::ResetNeedsPushProperties() {
  needs_push_properties_.Write(*this) = false;
}

void AnimationHost::PushPropertiesTo(MutatorHost* mutator_host_impl,
                                     const PropertyTrees& property_trees) {
  auto* host_impl = static_cast<AnimationHost*>(mutator_host_impl);

  base::AutoReset<raw_ptr<const PropertyTrees>> properties(&property_trees_,
                                                           &property_trees);

  // Update animation counts and whether raf was requested. These explicitly
  // do not request push properties and are pushed as part of the next commit
  // when it happens as requesting a commit leads to performance issues:
  // https://crbug.com/1083244
  host_impl->main_thread_animations_count_.Write(*host_impl) =
      main_thread_animations_count_.Read(*this);
  host_impl->SetCurrentFrameHadRaf(CurrentFrameHadRAF());
  host_impl->SetNextFrameHasPendingRaf(NextFrameHasPendingRAF());
  host_impl->SetHasCanvasInvalidation(HasCanvasInvalidation());
  host_impl->SetHasInlineStyleMutation(HasJSAnimation());
  host_impl->SetHasSmilAnimation(HasSmilAnimation());
  host_impl->SetHasViewTransition(HasViewTransition());

  if (needs_push_properties()) {
    needs_push_properties_.Write(*this) = false;
    PushTimelinesToImplThread(host_impl);
    RemoveTimelinesFromImplThread(host_impl);
    PushPropertiesToImplThread(host_impl);

    // When using a display tree this ensures that any new animation updates are
    // pushed to Viz on next display tree update. When not using display trees,
    // setting this flag here is meaningless.
    host_impl->needs_push_properties_.Write(*host_impl) = true;
  }
}

void AnimationHost::RemoveStaleTimelines() {
  DCHECK(!InProtectedSequence());
  if (detached_timeline_map_.Read(*this).empty()) {
    return;
  }

  for (auto& kv : detached_timeline_map_.Read(*this)) {
    RemoveAnimationTimeline(kv.second);
  }
  detached_timeline_map_.Write(*this).clear();
}

void AnimationHost::PushTimelinesToImplThread(AnimationHost* host_impl) const {
  for (auto& kv : id_to_timeline_map_.Read(*this)) {
    auto& timeline = kv.second;
    const AnimationTimeline* timeline_impl =
        host_impl->GetTimelineById(timeline->id());
    if (timeline_impl)
      continue;

    scoped_refptr<AnimationTimeline> to_add = timeline->CreateImplInstance();
    host_impl->AddAnimationTimeline(std::move(to_add));
  }
}

void AnimationHost::RemoveTimelinesFromImplThread(
    AnimationHost* host_impl) const {
  IdToTimelineMap& timelines_impl =
      host_impl->id_to_timeline_map_.Write(*host_impl);

  // Erase all the impl timelines which |this| doesn't have.
  for (auto it = timelines_impl.begin(); it != timelines_impl.end();) {
    auto& timeline_impl = it->second;
    if (timeline_impl->is_impl_only() || GetTimelineById(timeline_impl->id())) {
      ++it;
    } else {
      host_impl->EraseTimeline(it->second);
      it = timelines_impl.erase(it);
    }
  }
}

void AnimationHost::PushPropertiesToImplThread(AnimationHost* host_impl) {
  base::AutoReset<raw_ptr<const PropertyTrees>> properties(
      &host_impl->property_trees_, property_trees_);

  // Sync all animations with impl thread to create ElementAnimations. This
  // needs to happen before the element animations are synced below.
  for (auto& kv : id_to_timeline_map_.Read(*this)) {
    AnimationTimeline* timeline = kv.second.get();
    if (AnimationTimeline* timeline_impl =
            host_impl->GetTimelineById(timeline->id())) {
      timeline->PushPropertiesTo(timeline_impl);
    }
  }

  // Sync properties for created ElementAnimations.
  for (auto& kv : element_to_animations_map_.Read(*this)) {
    const auto& element_animations = kv.second;
    if (auto element_animations_impl =
            host_impl->GetElementAnimationsForElementId(kv.first)) {
      element_animations->PushPropertiesTo(std::move(element_animations_impl));
    }
  }

  // Update the impl-only scroll offset animations.
  scroll_offset_animations_.Write(*this)->PushPropertiesTo(
      host_impl->scroll_offset_animations_impl_.Write(*host_impl).get());

  // The pending info list is cleared in LayerTreeHostImpl::CommitComplete
  // and should be empty when pushing properties.
  DCHECK(host_impl->pending_compositor_metrics_tracker_infos_.Read(*host_impl)
             .empty());
  host_impl->pending_compositor_metrics_tracker_infos_.Write(*host_impl) =
      TakePendingCompositorMetricsTrackerInfos();
}

const ElementAnimations* AnimationHost::GetElementAnimationsForElementId(
    ElementId element_id) const {
  if (!element_id)
    return nullptr;
  auto iter = element_to_animations_map_.Read(*this).find(element_id);
  return iter == element_to_animations_map_.Read(*this).end()
             ? nullptr
             : iter->second.get();
}

scoped_refptr<ElementAnimations>
AnimationHost::GetElementAnimationsForElementId(ElementId element_id) {
  if (!element_id)
    return nullptr;
  auto iter = element_to_animations_map_.Write(*this).find(element_id);
  return iter == element_to_animations_map_.Write(*this).end() ? nullptr
                                                               : iter->second;
}

scoped_refptr<const ElementAnimations>
AnimationHost::GetElementAnimationsForElementIdForTesting(
    ElementId element_id) const {
  return GetElementAnimationsForElementId(element_id);
}

gfx::PointF AnimationHost::GetScrollOffsetForAnimation(
    ElementId element_id) const {
  DCHECK(property_trees_);
  return property_trees_->scroll_tree().current_scroll_offset(element_id);
}

void AnimationHost::SetScrollAnimationDurationForTesting(
    base::TimeDelta duration) {
  ScrollOffsetAnimationCurve::SetAnimationDurationForTesting(duration);
}

bool AnimationHost::NeedsTickAnimations() const {
  for (auto& animation : ticking_animations_.Read(*this)) {
    if (!animation->keyframe_effect()->awaiting_deletion()) {
      return true;
    }
  }
  return false;
}

void AnimationHost::TickMutator(base::TimeTicks monotonic_time,
                                const ScrollTree& scroll_tree,
                                bool is_active_tree) {
  LayerTreeMutator* mutator = mutator_.Write(*this).get();
  if (!mutator || !mutator->HasMutators())
    return;

  DCHECK(IsOwnerThread());
  DCHECK(!InProtectedSequence());
  std::unique_ptr<MutatorInputState> state = CollectWorkletAnimationsState(
      monotonic_time, scroll_tree, is_active_tree);
  if (state->IsEmpty())
    return;

  ElementListType tree_type =
      is_active_tree ? ElementListType::ACTIVE : ElementListType::PENDING;

  auto on_done = base::BindOnce(
      [](base::WeakPtr<AnimationHost> animation_host, ElementListType tree_type,
         MutateStatus status) {
        if (animation_host->mutator_host_client()) {
          animation_host->mutator_host_client()
              ->NotifyAnimationWorkletStateChange(
                  ToAnimationWorkletMutationState(status), tree_type);
        }
      },
      weak_factory_.GetWeakPtr(), tree_type);

  MutateQueuingStrategy queuing_strategy =
      is_active_tree ? MutateQueuingStrategy::kQueueAndReplaceNormalPriority
                     : MutateQueuingStrategy::kQueueHighPriority;
  if (mutator->Mutate(std::move(state), queuing_strategy, std::move(on_done))) {
    mutator_host_client()->NotifyAnimationWorkletStateChange(
        AnimationWorkletMutationState::STARTED, tree_type);
  }
}

bool AnimationHost::ActivateAnimations(MutatorEvents* mutator_events) {
  if (!NeedsTickAnimations())
    return false;

  auto* animation_events = static_cast<AnimationEvents*>(mutator_events);

  TRACE_EVENT0("cc", "AnimationHost::ActivateAnimations");
  AnimationsList ticking_animations_copy = ticking_animations_.Read(*this);
  for (auto& it : ticking_animations_copy) {
    it->ActivateKeyframeModels();
    // Finish animations which no longer affect active or pending elements.
    it->UpdateState(false, animation_events);
  }

  return true;
}

bool AnimationHost::TickAnimations(base::TimeTicks monotonic_time,
                                   const ScrollTree& scroll_tree,
                                   bool is_active_tree) {
  TRACE_EVENT0("cc", "AnimationHost::TickAnimations");
  // We tick animations in the following order:
  // 1. regular animations 2. mutator 3. worklet animations
  //
  // Mutator may depend on scroll offset as its time input e.g., when there is
  // a worklet animation attached to a scroll timeline.
  // This ordering ensures we use the latest scroll offset as the input to the
  // mutator even if there are active scroll animations.
  // The ticking of worklet animations is deferred until draw to ensure that
  // mutator output takes effect in the same impl frame that it was mutated.
  if (is_active_tree && !NeedsTickAnimations()) {
    return false;
  }

  TRACE_EVENT_INSTANT0("cc", "NeedsTickAnimations", TRACE_EVENT_SCOPE_THREAD);

  bool animated = false;
  std::vector<AnimationTimeline*> scroll_timelines;
  for (auto& kv : id_to_timeline_map_.Read(*this)) {
    AnimationTimeline* timeline = kv.second.get();
    if (timeline->IsScrollTimeline()) {
      scroll_timelines.push_back(timeline);
    } else {
      animated |= timeline->TickTimeLinkedAnimations(
          ticking_animations_.Read(*this), monotonic_time, !is_active_tree);
    }
  }
  // Tick the scroll-linked animations last, since a smooth scroll (time-linked)
  // might update the scroll offset.
  for (auto* timeline : scroll_timelines) {
    animated |= timeline->TickScrollLinkedAnimations(
        ticking_animations_.Read(*this), scroll_tree, is_active_tree);
  }

  // TODO(majidvp): At the moment we call this for both active and pending
  // trees similar to other animations. However our final goal is to only call
  // it once, ideally after activation, and only when the input
  // to an active timeline has changed. http://crbug.com/767210
  // Note that the TickMutator does not set the animated flag since these
  // mutations are processed asynchronously. Additional actions required to
  // handle these mutations are performed on receiving the asynchronous results.
  TickMutator(monotonic_time, scroll_tree, is_active_tree);

  return animated;
}

void AnimationHost::TickScrollAnimations(base::TimeTicks monotonic_time,
                                         const ScrollTree& scroll_tree) {
  // TODO(majidvp): We need to return a boolean here so that LTHI knows
  // whether it needs to schedule another frame.
  TickMutator(monotonic_time, scroll_tree, true /* is_active_tree */);
}

void AnimationHost::TickWorkletAnimations() {
  for (auto& animation : ticking_animations_.Read(*this)) {
    if (!animation->IsWorkletAnimation())
      continue;
    animation->Tick(base::TimeTicks());
  }
}

std::unique_ptr<MutatorInputState> AnimationHost::CollectWorkletAnimationsState(
    base::TimeTicks monotonic_time,
    const ScrollTree& scroll_tree,
    bool is_active_tree) {
  TRACE_EVENT0("cc", "AnimationHost::CollectWorkletAnimationsState");
  std::unique_ptr<MutatorInputState> result =
      std::make_unique<MutatorInputState>();

  for (auto& animation : ticking_animations_.Read(*this)) {
    if (!animation->IsWorkletAnimation())
      continue;

    ToWorkletAnimation(animation.get())
        ->UpdateInputState(result.get(), monotonic_time, scroll_tree,
                           is_active_tree);
  }

  return result;
}

bool AnimationHost::UpdateAnimationState(bool start_ready_animations,
                                         MutatorEvents* mutator_events) {
  if (!NeedsTickAnimations())
    return false;

  auto* animation_events = static_cast<AnimationEvents*>(mutator_events);

  TRACE_EVENT0("cc", "AnimationHost::UpdateAnimationState");
  AnimationsList ticking_animations_copy = ticking_animations_.Read(*this);
  for (auto& it : ticking_animations_copy)
    it->UpdateState(start_ready_animations, animation_events);

  return true;
}

void AnimationHost::TakeTimeUpdatedEvents(MutatorEvents* events) {
  auto* animation_events = static_cast<AnimationEvents*>(events);
  if (!animation_events->needs_time_updated_events())
    return;

  for (auto& it : ticking_animations_.Read(*this))
    it->TakeTimeUpdatedEvent(animation_events);

  animation_events->set_needs_time_updated_events(false);
}

void AnimationHost::PromoteScrollTimelinesPendingToActive() {
  for (auto& kv : id_to_timeline_map_.Read(*this)) {
    auto& timeline = kv.second;
    timeline->ActivateTimeline();
  }
}

std::unique_ptr<MutatorEvents> AnimationHost::CreateEvents() {
  return std::make_unique<AnimationEvents>();
}

void AnimationHost::SetAnimationEvents(
    std::unique_ptr<MutatorEvents> mutator_events) {
  DCHECK_EQ(thread_instance_, ThreadInstance::kMain);
  auto events =
      base::WrapUnique(static_cast<AnimationEvents*>(mutator_events.release()));

  for (const AnimationEvent& event : events->events()) {
    AnimationTimeline* timeline = GetTimelineById(event.uid.timeline_id);
    if (timeline) {
      Animation* animation = timeline->GetAnimationById(event.uid.animation_id);
      if (animation)
        animation->DispatchAndDelegateAnimationEvent(event);
    }
  }
}

bool AnimationHost::ScrollOffsetAnimationWasInterrupted(
    ElementId element_id) const {
  const auto* element_animations = GetElementAnimationsForElementId(element_id);
  return element_animations &&
         element_animations->ScrollOffsetAnimationWasInterrupted();
}

bool AnimationHost::IsAnimatingProperty(ElementId element_id,
                                        ElementListType list_type,
                                        TargetProperty::Type property) const {
  const auto* element_animations = GetElementAnimationsForElementId(element_id);
  return element_animations &&
         element_animations->IsCurrentlyAnimatingProperty(property, list_type);
}

bool AnimationHost::HasPotentiallyRunningAnimationForProperty(
    ElementId element_id,
    ElementListType list_type,
    TargetProperty::Type property) const {
  const auto* element_animations = GetElementAnimationsForElementId(element_id);
  return element_animations &&
         element_animations->IsPotentiallyAnimatingProperty(property,
                                                            list_type);
}

bool AnimationHost::HasAnyAnimationTargetingProperty(
    ElementId element_id,
    TargetProperty::Type property) const {
  const auto* element_animations = GetElementAnimationsForElementId(element_id);
  return element_animations &&
         element_animations->HasAnyAnimationTargetingProperty(property,
                                                              element_id);
}

bool AnimationHost::AnimationsPreserveAxisAlignment(
    ElementId element_id) const {
  const auto* element_animations = GetElementAnimationsForElementId(element_id);
  return !element_animations ||
         element_animations->AnimationsPreserveAxisAlignment();
}

float AnimationHost::MaximumScale(ElementId element_id,
                                  ElementListType list_type) const {
  if (const auto* element_animations =
          GetElementAnimationsForElementId(element_id)) {
    return element_animations->MaximumScale(element_id, list_type);
  }
  return kInvalidScale;
}

bool AnimationHost::IsElementAnimating(ElementId element_id) const {
  const auto* element_animations = GetElementAnimationsForElementId(element_id);
  return element_animations && element_animations->HasAnyKeyframeModel();
}

bool AnimationHost::HasTickingKeyframeModelForTesting(
    ElementId element_id) const {
  const auto* element_animations = GetElementAnimationsForElementId(element_id);
  return element_animations && element_animations->HasTickingKeyframeEffect();
}

void AnimationHost::ImplOnlyAutoScrollAnimationCreate(
    ElementId element_id,
    const gfx::PointF& target_offset,
    const gfx::PointF& current_offset,
    float autoscroll_velocity,
    base::TimeDelta animation_start_offset) {
  DCHECK(scroll_offset_animations_impl_.Read(*this));
  scroll_offset_animations_impl_.Write(*this)->AutoScrollAnimationCreate(
      element_id, target_offset, current_offset, autoscroll_velocity,
      animation_start_offset);
}

void AnimationHost::ImplOnlyScrollAnimationCreate(
    ElementId element_id,
    const gfx::PointF& target_offset,
    const gfx::PointF& current_offset,
    base::TimeDelta delayed_by,
    base::TimeDelta animation_start_offset) {
  DCHECK(scroll_offset_animations_impl_.Read(*this));
  scroll_offset_animations_impl_.Write(*this)->MouseWheelScrollAnimationCreate(
      element_id, target_offset, current_offset, delayed_by,
      animation_start_offset);
}

std::optional<gfx::PointF> AnimationHost::ImplOnlyScrollAnimationUpdateTarget(
    const gfx::Vector2dF& scroll_delta,
    const gfx::PointF& max_scroll_offset,
    base::TimeTicks frame_monotonic_time,
    base::TimeDelta delayed_by,
    ElementId element_id) {
  DCHECK(scroll_offset_animations_impl_.Read(*this));
  return scroll_offset_animations_impl_.Write(*this)
      ->ScrollAnimationUpdateTarget(scroll_delta, max_scroll_offset,
                                    frame_monotonic_time, delayed_by,
                                    element_id);
}

ScrollOffsetAnimations& AnimationHost::scroll_offset_animations() {
  DCHECK(scroll_offset_animations_.Read(*this));
  return *scroll_offset_animations_.Write(*this).get();
}

void AnimationHost::ScrollAnimationAbort(ElementId element_id) {
  DCHECK(scroll_offset_animations_impl_.Read(*this));
  scroll_offset_animations_impl_.Write(*this)->ScrollAnimationAbort(
      false /* needs_completion */, element_id);
}

bool AnimationHost::ElementHasImplOnlyScrollAnimation(
    ElementId element_id) const {
  return scroll_offset_animations_impl_.Read(*this)
      ->ElementHasImplOnlyScrollAnimation(element_id);
}

bool AnimationHost::HasImplOnlyScrollAnimatingElement() const {
  return scroll_offset_animations_impl_.Read(*this)
      ->HasImplOnlyScrollAnimatingElement();
}

bool AnimationHost::HasImplOnlyAutoScrollAnimatingElement() const {
  return scroll_offset_animations_impl_.Read(*this)
      ->HasImplOnlyAutoScrollAnimatingElement();
}

bool AnimationHost::IsElementInPropertyTrees(ElementId element_id,
                                             bool commits_to_active) const {
  return mutator_host_client()->IsElementInPropertyTrees(
      element_id,
      commits_to_active ? ElementListType::ACTIVE : ElementListType::PENDING);
}

void AnimationHost::HandleRemovedScrollAnimatingElements(
    bool commits_to_active) {
  scroll_offset_animations_impl_.Write(*this)
      ->HandleRemovedScrollAnimatingElements(commits_to_active);
}

void AnimationHost::AddToTicking(scoped_refptr<Animation> animation) {
  DCHECK(!base::Contains(ticking_animations_.Read(*this), animation));
  ticking_animations_.Write(*this).push_back(animation);
}

void AnimationHost::RemoveFromTicking(scoped_refptr<Animation> animation) {
  auto to_erase =
      std::ranges::find(ticking_animations_.Write(*this), animation);
  if (to_erase != ticking_animations_.Write(*this).end()) {
    ticking_animations_.Write(*this).erase(to_erase);
  }
}

const AnimationHost::AnimationsList&
AnimationHost::ticking_animations_for_testing() const {
  return ticking_animations_.Read(*this);
}

const AnimationHost::ElementToAnimationsMap&
AnimationHost::element_animations_for_testing() const {
  return element_to_animations_map_.Read(*this);
}

void AnimationHost::SetLayerTreeMutator(
    std::unique_ptr<LayerTreeMutator> mutator) {
  mutator_.Write(*this) = std::move(mutator);
  mutator_.Write(*this)->SetClient(this);
}

WorkletAnimation* AnimationHost::FindWorkletAnimation(WorkletAnimationId id) {
  // TODO(majidvp): Use a map to make lookup O(1)
  auto animation =
      std::ranges::find_if(ticking_animations_.Read(*this), [id](auto& it) {
        return it->IsWorkletAnimation() &&
               ToWorkletAnimation(it.get())->worklet_animation_id() == id;
      });

  if (animation == ticking_animations_.Read(*this).end())
    return nullptr;

  return ToWorkletAnimation(animation->get());
}

void AnimationHost::SetMutationUpdate(
    std::unique_ptr<MutatorOutputState> output_state) {
  if (!output_state)
    return;

  TRACE_EVENT0("cc", "AnimationHost::SetMutationUpdate");
  for (auto& animation_state : output_state->animations) {
    WorkletAnimationId id = animation_state.worklet_animation_id;

    WorkletAnimation* to_update = FindWorkletAnimation(id);
    if (to_update)
      to_update->SetOutputState(animation_state);
  }
}

void AnimationHost::SetAnimationCounts(size_t total_animations_count) {
  // Though these changes are pushed as part of AnimationHost::PushPropertiesTo
  // we don't SetNeedsPushProperties as pushing the values requires a commit.
  // Instead we allow them to be pushed whenever the next required commit
  // happens to avoid unnecessary work. See https://crbug.com/1083244.

  // If an animation is being run on the compositor, it will have a ticking
  // Animation (which will have a corresponding impl-thread version). Therefore
  // to find the count of main-only animations, we can simply subtract the
  // number of ticking animations from the total count.
  size_t ticking_animations_count = ticking_animations_.Read(*this).size();
  main_thread_animations_count_.Write(*this) =
      total_animations_count - ticking_animations_count;
  DCHECK_GE(main_thread_animations_count_.Read(*this), 0u);
}

size_t AnimationHost::MainThreadAnimationsCount() const {
  return main_thread_animations_count_.Read(*this);
}

bool AnimationHost::HasInvalidationAnimation() const {
  for (const auto& it : ticking_animations_.Read(*this))
    if (it->RequiresInvalidation())
      return true;
  return false;
}

bool AnimationHost::HasNativePropertyAnimation() const {
  for (const auto& it : ticking_animations_.Read(*this))
    if (it->AffectsNativeProperty())
      return true;
  return false;
}

AnimationHost::PendingCompositorMetricsTrackerInfos
AnimationHost::TakePendingCompositorMetricsTrackerInfos() {
  PendingCompositorMetricsTrackerInfos infos =
      std::move(pending_compositor_metrics_tracker_infos_.Write(*this));
  pending_compositor_metrics_tracker_infos_.Write(*this) = {};
  return infos;
}

void AnimationHost::StartCompositorMetricsTracking(
    TrackedAnimationSequenceId sequence_id) {
  pending_compositor_metrics_tracker_infos_.Write(*this).push_back(
      {sequence_id, true});
  SetNeedsPushProperties();
}

void AnimationHost::StopCompositorMetricsTracking(
    TrackedAnimationSequenceId sequence_id) {
  pending_compositor_metrics_tracker_infos_.Write(*this).push_back(
      {sequence_id, false});
  SetNeedsPushProperties();
}

bool AnimationHost::HasScrollLinkedAnimation(ElementId for_scroller) const {
  for (auto& animation : ticking_animations_.Read(*this)) {
    if (auto* timeline = animation->animation_timeline()) {
      if (timeline->IsLinkedToScroller(for_scroller)) {
        return true;
      }
    }
  }
  return false;
}

}  // namespace cc