File: PrimitiveInterpolation.h

package info (click to toggle)
chromium-browser 57.0.2987.98-1~deb8u1
  • links: PTS, VCS
  • area: main
  • in suites: jessie
  • size: 2,637,852 kB
  • ctags: 2,544,394
  • sloc: cpp: 12,815,961; ansic: 3,676,222; python: 1,147,112; asm: 526,608; java: 523,212; xml: 286,794; perl: 92,654; sh: 86,408; objc: 73,271; makefile: 27,698; cs: 18,487; yacc: 13,031; tcl: 12,957; pascal: 4,875; ml: 4,716; lex: 3,904; sql: 3,862; ruby: 1,982; lisp: 1,508; php: 1,368; exp: 404; awk: 325; csh: 117; jsp: 39; sed: 37
file content (146 lines) | stat: -rw-r--r-- 5,066 bytes parent folder | download
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
// Copyright 2015 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 PrimitiveInterpolation_h
#define PrimitiveInterpolation_h

#include "core/animation/TypedInterpolationValue.h"
#include "platform/animation/AnimationUtilities.h"
#include "platform/heap/Handle.h"
#include "wtf/PtrUtil.h"
#include "wtf/Vector.h"
#include <cmath>
#include <memory>

namespace blink {

// Represents an animation's effect between an adjacent pair of
// PropertySpecificKeyframes after converting the keyframes to an internal
// format with respect to the animation environment and underlying values.
class PrimitiveInterpolation {
  USING_FAST_MALLOC(PrimitiveInterpolation);
  WTF_MAKE_NONCOPYABLE(PrimitiveInterpolation);

 public:
  virtual ~PrimitiveInterpolation() {}

  virtual void interpolateValue(
      double fraction,
      std::unique_ptr<TypedInterpolationValue>& result) const = 0;
  virtual double interpolateUnderlyingFraction(double start,
                                               double end,
                                               double fraction) const = 0;
  virtual bool isFlip() const { return false; }

 protected:
  PrimitiveInterpolation() {}
};

// Represents a pair of keyframes that are compatible for "smooth" interpolation
// eg. "0px" and "100px".
class PairwisePrimitiveInterpolation : public PrimitiveInterpolation {
 public:
  ~PairwisePrimitiveInterpolation() override {}

  static std::unique_ptr<PairwisePrimitiveInterpolation> create(
      const InterpolationType& type,
      std::unique_ptr<InterpolableValue> start,
      std::unique_ptr<InterpolableValue> end,
      PassRefPtr<NonInterpolableValue> nonInterpolableValue) {
    return WTF::wrapUnique(new PairwisePrimitiveInterpolation(
        type, std::move(start), std::move(end),
        std::move(nonInterpolableValue)));
  }

  const InterpolationType& type() const { return m_type; }

  std::unique_ptr<TypedInterpolationValue> initialValue() const {
    return TypedInterpolationValue::create(m_type, m_start->clone(),
                                           m_nonInterpolableValue);
  }

 private:
  PairwisePrimitiveInterpolation(
      const InterpolationType& type,
      std::unique_ptr<InterpolableValue> start,
      std::unique_ptr<InterpolableValue> end,
      PassRefPtr<NonInterpolableValue> nonInterpolableValue)
      : m_type(type),
        m_start(std::move(start)),
        m_end(std::move(end)),
        m_nonInterpolableValue(nonInterpolableValue) {
    DCHECK(m_start);
    DCHECK(m_end);
  }

  void interpolateValue(
      double fraction,
      std::unique_ptr<TypedInterpolationValue>& result) const final {
    DCHECK(result);
    DCHECK_EQ(&result->type(), &m_type);
    DCHECK_EQ(result->getNonInterpolableValue(), m_nonInterpolableValue.get());
    m_start->interpolate(*m_end, fraction,
                         *result->mutableValue().interpolableValue);
  }

  double interpolateUnderlyingFraction(double start,
                                       double end,
                                       double fraction) const final {
    return blend(start, end, fraction);
  }

  const InterpolationType& m_type;
  std::unique_ptr<InterpolableValue> m_start;
  std::unique_ptr<InterpolableValue> m_end;
  RefPtr<NonInterpolableValue> m_nonInterpolableValue;
};

// Represents a pair of incompatible keyframes that fall back to 50% flip
// behaviour eg. "auto" and "0px".
class FlipPrimitiveInterpolation : public PrimitiveInterpolation {
 public:
  ~FlipPrimitiveInterpolation() override {}

  static std::unique_ptr<FlipPrimitiveInterpolation> create(
      std::unique_ptr<TypedInterpolationValue> start,
      std::unique_ptr<TypedInterpolationValue> end) {
    return WTF::wrapUnique(
        new FlipPrimitiveInterpolation(std::move(start), std::move(end)));
  }

 private:
  FlipPrimitiveInterpolation(std::unique_ptr<TypedInterpolationValue> start,
                             std::unique_ptr<TypedInterpolationValue> end)
      : m_start(std::move(start)),
        m_end(std::move(end)),
        m_lastFraction(std::numeric_limits<double>::quiet_NaN()) {}

  void interpolateValue(
      double fraction,
      std::unique_ptr<TypedInterpolationValue>& result) const final {
    if (!std::isnan(m_lastFraction) &&
        (fraction < 0.5) == (m_lastFraction < 0.5))
      return;
    const TypedInterpolationValue* side =
        ((fraction < 0.5) ? m_start : m_end).get();
    result = side ? side->clone() : nullptr;
    m_lastFraction = fraction;
  }

  double interpolateUnderlyingFraction(double start,
                                       double end,
                                       double fraction) const final {
    return fraction < 0.5 ? start : end;
  }

  bool isFlip() const final { return true; }

  std::unique_ptr<TypedInterpolationValue> m_start;
  std::unique_ptr<TypedInterpolationValue> m_end;
  mutable double m_lastFraction;
};

}  // namespace blink

#endif  // PrimitiveInterpolation_h