summaryrefslogtreecommitdiff
path: root/chromium/third_party/blink/renderer/core/animation/interpolable_value.h
blob: 71b0dd8d3be45843c3015761d73a8c688e60159f (plain)
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
// Copyright 2014 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.

#ifndef THIRD_PARTY_BLINK_RENDERER_CORE_ANIMATION_INTERPOLABLE_VALUE_H_
#define THIRD_PARTY_BLINK_RENDERER_CORE_ANIMATION_INTERPOLABLE_VALUE_H_

#include <memory>
#include <utility>

#include "base/memory/ptr_util.h"
#include "third_party/blink/renderer/core/animation/animatable/animatable_value.h"
#include "third_party/blink/renderer/core/core_export.h"
#include "third_party/blink/renderer/platform/heap/handle.h"
#include "third_party/blink/renderer/platform/wtf/vector.h"

namespace blink {

// Represents the components of a PropertySpecificKeyframe's value that change
// smoothly as it interpolates to an adjacent value.
class CORE_EXPORT InterpolableValue {
  USING_FAST_MALLOC(InterpolableValue);

 public:
  virtual ~InterpolableValue() = default;

  virtual bool IsNumber() const { return false; }
  virtual bool IsBool() const { return false; }
  virtual bool IsList() const { return false; }

  virtual bool Equals(const InterpolableValue&) const = 0;
  virtual std::unique_ptr<InterpolableValue> Clone() const = 0;
  virtual std::unique_ptr<InterpolableValue> CloneAndZero() const = 0;
  virtual void Scale(double scale) = 0;
  virtual void ScaleAndAdd(double scale, const InterpolableValue& other) = 0;

 private:
  virtual void Interpolate(const InterpolableValue& to,
                           const double progress,
                           InterpolableValue& result) const = 0;

  friend class LegacyStyleInterpolation;
  friend class TransitionInterpolation;
  friend class PairwisePrimitiveInterpolation;

  // Keep interpolate private, but allow calls within the hierarchy without
  // knowledge of type.
  friend class InterpolableNumber;
  friend class InterpolableList;

  friend class AnimationInterpolableValueTest;
};

class CORE_EXPORT InterpolableNumber final : public InterpolableValue {
 public:
  static std::unique_ptr<InterpolableNumber> Create(double value) {
    return base::WrapUnique(new InterpolableNumber(value));
  }

  bool IsNumber() const final { return true; }
  double Value() const { return value_; }
  bool Equals(const InterpolableValue& other) const final;
  std::unique_ptr<InterpolableValue> Clone() const final {
    return Create(value_);
  }
  std::unique_ptr<InterpolableValue> CloneAndZero() const final {
    return Create(0);
  }
  void Scale(double scale) final;
  void ScaleAndAdd(double scale, const InterpolableValue& other) final;
  void Set(double value) { value_ = value; }

 private:
  void Interpolate(const InterpolableValue& to,
                   const double progress,
                   InterpolableValue& result) const final;
  double value_;

  explicit InterpolableNumber(double value) : value_(value) {}
};

class CORE_EXPORT InterpolableList : public InterpolableValue {
 public:
  // Explicitly delete operator= because MSVC automatically generate
  // copy constructors and operator= for dll-exported classes.
  // Since InterpolableList is not copyable, automatically generated
  // operator= causes MSVC compiler error.
  // However, we cannot use DISALLOW_COPY_AND_ASSIGN because InterpolableList
  // has its own copy constructor. So just delete operator= here.
  InterpolableList& operator=(const InterpolableList&) = delete;

  static std::unique_ptr<InterpolableList> Create(
      const InterpolableList& other) {
    return base::WrapUnique(new InterpolableList(other));
  }

  static std::unique_ptr<InterpolableList> Create(wtf_size_t size) {
    return base::WrapUnique(new InterpolableList(size));
  }

  bool IsList() const final { return true; }
  void Set(wtf_size_t position, std::unique_ptr<InterpolableValue> value) {
    values_[position] = std::move(value);
  }
  const InterpolableValue* Get(wtf_size_t position) const {
    return values_[position].get();
  }
  std::unique_ptr<InterpolableValue>& GetMutable(wtf_size_t position) {
    return values_[position];
  }
  wtf_size_t length() const { return values_.size(); }
  bool Equals(const InterpolableValue& other) const final;
  std::unique_ptr<InterpolableValue> Clone() const final {
    return Create(*this);
  }
  std::unique_ptr<InterpolableValue> CloneAndZero() const final;
  void Scale(double scale) final;
  void ScaleAndAdd(double scale, const InterpolableValue& other) final;

 private:
  void Interpolate(const InterpolableValue& to,
                   const double progress,
                   InterpolableValue& result) const final;
  explicit InterpolableList(wtf_size_t size) : values_(size) {}

  InterpolableList(const InterpolableList& other) : values_(other.length()) {
    for (wtf_size_t i = 0; i < length(); i++)
      Set(i, other.values_[i]->Clone());
  }

  Vector<std::unique_ptr<InterpolableValue>> values_;
};

DEFINE_TYPE_CASTS(InterpolableNumber,
                  InterpolableValue,
                  value,
                  value->IsNumber(),
                  value.IsNumber());
DEFINE_TYPE_CASTS(InterpolableList,
                  InterpolableValue,
                  value,
                  value->IsList(),
                  value.IsList());

}  // namespace blink

#endif