File: ambient_animation_player.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 (110 lines) | stat: -rw-r--r-- 4,615 bytes parent folder | download | duplicates (6)
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
// Copyright 2022 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/ambient/ui/ambient_animation_player.h"

#include <optional>
#include <string>
#include <utility>

#include "ash/ambient/ui/ambient_animation_progress_tracker.h"
#include "ash/public/cpp/ambient/ambient_ui_model.h"
#include "ash/utility/lottie_util.h"
#include "base/check.h"
#include "base/logging.h"
#include "base/no_destructor.h"
#include "base/strings/strcat.h"
#include "base/time/time.h"
#include "cc/paint/skottie_marker.h"
#include "cc/paint/skottie_wrapper.h"
#include "ui/views/controls/animated_image_view.h"

namespace ash {

namespace {

std::optional<base::TimeDelta> FindCycleRestartTimestamp(
    const cc::SkottieWrapper& skottie) {
  static const base::NoDestructor<std::string> kRestartMarkerName(
      base::StrCat({kLottieCustomizableIdPrefix, "_Marker_CycleRestart"}));
  DCHECK(skottie.is_valid());
  std::optional<base::TimeDelta> restart_timestamp;
  for (const cc::SkottieMarker& marker : skottie.GetAllMarkers()) {
    if (marker.name != *kRestartMarkerName) {
      continue;
    } else if (restart_timestamp.has_value()) {
      LOG(DFATAL) << "Multiple markers with name " << *kRestartMarkerName
                  << " found in animation file. Defaulting to first one.";
    } else {
      // |marker.begin_time| is a normalized timestamp in range [0, 1), where 1
      // is the animation's cycle duration.
      restart_timestamp.emplace(base::Seconds(skottie.duration()) *
                                marker.begin_time);
      DVLOG(1) << "Found restart marker at timestamp " << *restart_timestamp;
    }
  }
  return restart_timestamp;
}

}  // namespace

AmbientAnimationPlayer::AmbientAnimationPlayer(
    views::AnimatedImageView* animated_image_view,
    AmbientAnimationProgressTracker* progress_tracker)
    : animated_image_view_(animated_image_view),
      progress_tracker_(progress_tracker) {
  DCHECK(animated_image_view_);
  lottie::Animation* animation = animated_image_view_->animated_image();
  DCHECK(animation);
  DCHECK(progress_tracker_);
  lottie::Animation::PlaybackConfig playback_config;
  // If there are existing animations in this ambient mode session, start
  // playing from the existing animations' timestamps. This gives rough
  // synchronization across animations. The animations' timestamps should not
  // diverge over time because AnimatedImageView makes time steps that
  // ultimately reflect real time (i.e. it does not step by a fixed amount each
  // frame). Thus, there may be transient periods where the animations diverge
  // due to random system instability, but they should all converge again
  // eventually.
  if (progress_tracker_->HasActiveAnimations()) {
    AmbientAnimationProgressTracker::ImmutableParams immutable_params =
        progress_tracker_->GetImmutableParams();
    AmbientAnimationProgressTracker::Progress global_progress =
        progress_tracker_->GetGlobalProgress();
    playback_config = {
        immutable_params.scheduled_cycles,
        global_progress.current_timestamp * immutable_params.total_duration,
        global_progress.num_completed_cycles, immutable_params.style};
  } else {
    std::optional<base::TimeDelta> cycle_restart_timestamp_found =
        FindCycleRestartTimestamp(*animation->skottie());
    if (cycle_restart_timestamp_found.has_value()) {
      cycle_restart_timestamp_ = *cycle_restart_timestamp_found;
      if (cycle_restart_timestamp_ >= animation->GetAnimationDuration()) {
        LOG(DFATAL) << "Animation has invalid cycle restart timestamp "
                    << cycle_restart_timestamp_ << ". Total cycle duration "
                    << animation->GetAnimationDuration();
      }
    } else {
      DVLOG(1) << "Restart marker not found in animation. Defaulting to cycle "
                  "restart at timestamp 0";
      DCHECK(cycle_restart_timestamp_.is_zero());
    }
    playback_config = lottie::Animation::PlaybackConfig(
        {{base::TimeDelta(), animation->GetAnimationDuration()},
         {cycle_restart_timestamp_, animation->GetAnimationDuration()}},
        /*initial_offset=*/base::TimeDelta(), /*initial_completed_cycles=*/0,
        lottie::Animation::Style::kLoop);
  }
  animation->SetPlaybackSpeed(
      AmbientUiModel::Get()->animation_playback_speed());
  progress_tracker_->RegisterAnimation(animation);
  animated_image_view_->Play(std::move(playback_config));
}

AmbientAnimationPlayer::~AmbientAnimationPlayer() {
  animated_image_view_->Stop();
}

}  // namespace ash