File: TimingFunction.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 (249 lines) | stat: -rw-r--r-- 8,504 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
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
/*
 * Copyright (C) 2000 Lars Knoll (knoll@kde.org)
 *           (C) 2000 Antti Koivisto (koivisto@kde.org)
 *           (C) 2000 Dirk Mueller (mueller@kde.org)
 * Copyright (C) 2003, 2005, 2006, 2007, 2008 Apple Inc. All rights reserved.
 * Copyright (C) 2006 Graham Dennis (graham.dennis@gmail.com)
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Library General Public
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Library General Public License for more details.
 *
 * You should have received a copy of the GNU Library General Public License
 * along with this library; see the file COPYING.LIB.  If not, write to
 * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
 * Boston, MA 02110-1301, USA.
 *
 */

#ifndef TimingFunction_h
#define TimingFunction_h

#include "cc/animation/timing_function.h"
#include "platform/PlatformExport.h"
#include "wtf/Assertions.h"
#include "wtf/PassRefPtr.h"
#include "wtf/RefCounted.h"
#include "wtf/StdLibExtras.h"
#include "wtf/text/WTFString.h"

namespace blink {

class PLATFORM_EXPORT TimingFunction : public RefCounted<TimingFunction> {
 public:
  using Type = cc::TimingFunction::Type;

  virtual ~TimingFunction() {}

  Type getType() const { return m_type; }

  virtual String toString() const = 0;

  // Evaluates the timing function at the given fraction. The accuracy parameter
  // provides a hint as to the required accuracy and is not guaranteed.
  virtual double evaluate(double fraction, double accuracy) const = 0;

  // This function returns the minimum and maximum values obtainable when
  // calling evaluate();
  virtual void range(double* minValue, double* maxValue) const = 0;

  // Create CC instance.
  virtual std::unique_ptr<cc::TimingFunction> cloneToCC() const = 0;

 protected:
  TimingFunction(Type type) : m_type(type) {}

 private:
  Type m_type;
};

class PLATFORM_EXPORT LinearTimingFunction final : public TimingFunction {
 public:
  static LinearTimingFunction* shared() {
    DEFINE_STATIC_REF(LinearTimingFunction, linear,
                      (adoptRef(new LinearTimingFunction())));
    return linear;
  }

  ~LinearTimingFunction() override {}

  // TimingFunction implementation.
  String toString() const override;
  double evaluate(double fraction, double) const override;
  void range(double* minValue, double* maxValue) const override;
  std::unique_ptr<cc::TimingFunction> cloneToCC() const override;

 private:
  LinearTimingFunction() : TimingFunction(Type::LINEAR) {}
};

class PLATFORM_EXPORT CubicBezierTimingFunction final : public TimingFunction {
 public:
  using EaseType = cc::CubicBezierTimingFunction::EaseType;

  static PassRefPtr<CubicBezierTimingFunction> create(double x1,
                                                      double y1,
                                                      double x2,
                                                      double y2) {
    return adoptRef(new CubicBezierTimingFunction(x1, y1, x2, y2));
  }

  static CubicBezierTimingFunction* preset(EaseType easeType) {
    DEFINE_STATIC_REF(
        CubicBezierTimingFunction, ease,
        (adoptRef(new CubicBezierTimingFunction(EaseType::EASE))));
    DEFINE_STATIC_REF(
        CubicBezierTimingFunction, easeIn,
        (adoptRef(new CubicBezierTimingFunction(EaseType::EASE_IN))));
    DEFINE_STATIC_REF(
        CubicBezierTimingFunction, easeOut,
        (adoptRef(new CubicBezierTimingFunction(EaseType::EASE_OUT))));
    DEFINE_STATIC_REF(
        CubicBezierTimingFunction, easeInOut,
        (adoptRef(new CubicBezierTimingFunction(EaseType::EASE_IN_OUT))));

    switch (easeType) {
      case EaseType::EASE:
        return ease;
      case EaseType::EASE_IN:
        return easeIn;
      case EaseType::EASE_OUT:
        return easeOut;
      case EaseType::EASE_IN_OUT:
        return easeInOut;
      default:
        NOTREACHED();
        return nullptr;
    }
  }

  ~CubicBezierTimingFunction() override {}

  // TimingFunction implementation.
  String toString() const override;
  double evaluate(double fraction, double accuracy) const override;
  void range(double* minValue, double* maxValue) const override;
  std::unique_ptr<cc::TimingFunction> cloneToCC() const override;

  double x1() const {
    DCHECK_EQ(getEaseType(), EaseType::CUSTOM);
    return m_x1;
  }
  double y1() const {
    DCHECK_EQ(getEaseType(), EaseType::CUSTOM);
    return m_y1;
  }
  double x2() const {
    DCHECK_EQ(getEaseType(), EaseType::CUSTOM);
    return m_x2;
  }
  double y2() const {
    DCHECK_EQ(getEaseType(), EaseType::CUSTOM);
    return m_y2;
  }
  EaseType getEaseType() const { return m_bezier->ease_type(); }

 private:
  explicit CubicBezierTimingFunction(EaseType easeType)
      : TimingFunction(Type::CUBIC_BEZIER),
        m_bezier(cc::CubicBezierTimingFunction::CreatePreset(easeType)),
        m_x1(),
        m_y1(),
        m_x2(),
        m_y2() {}

  CubicBezierTimingFunction(double x1, double y1, double x2, double y2)
      : TimingFunction(Type::CUBIC_BEZIER),
        m_bezier(cc::CubicBezierTimingFunction::Create(x1, y1, x2, y2)),
        m_x1(x1),
        m_y1(y1),
        m_x2(x2),
        m_y2(y2) {}

  std::unique_ptr<cc::CubicBezierTimingFunction> m_bezier;

  // TODO(loyso): Get these values from m_bezier->bezier_ (gfx::CubicBezier)
  const double m_x1;
  const double m_y1;
  const double m_x2;
  const double m_y2;
};

class PLATFORM_EXPORT StepsTimingFunction final : public TimingFunction {
 public:
  using StepPosition = cc::StepsTimingFunction::StepPosition;

  static PassRefPtr<StepsTimingFunction> create(int steps,
                                                StepPosition stepPosition) {
    return adoptRef(new StepsTimingFunction(steps, stepPosition));
  }

  static StepsTimingFunction* preset(StepPosition position) {
    DEFINE_STATIC_REF(StepsTimingFunction, start,
                      create(1, StepPosition::START));
    DEFINE_STATIC_REF(StepsTimingFunction, middle,
                      create(1, StepPosition::MIDDLE));
    DEFINE_STATIC_REF(StepsTimingFunction, end, create(1, StepPosition::END));
    switch (position) {
      case StepPosition::START:
        return start;
      case StepPosition::MIDDLE:
        return middle;
      case StepPosition::END:
        return end;
      default:
        NOTREACHED();
        return end;
    }
  }

  ~StepsTimingFunction() override {}

  // TimingFunction implementation.
  String toString() const override;
  double evaluate(double fraction, double) const override;
  void range(double* minValue, double* maxValue) const override;
  std::unique_ptr<cc::TimingFunction> cloneToCC() const override;

  int numberOfSteps() const { return m_steps->steps(); }
  StepPosition getStepPosition() const { return m_steps->step_position(); }

 private:
  StepsTimingFunction(int steps, StepPosition stepPosition)
      : TimingFunction(Type::STEPS),
        m_steps(cc::StepsTimingFunction::Create(steps, stepPosition)) {}

  std::unique_ptr<cc::StepsTimingFunction> m_steps;
};

PLATFORM_EXPORT PassRefPtr<TimingFunction> createCompositorTimingFunctionFromCC(
    const cc::TimingFunction*);

PLATFORM_EXPORT bool operator==(const LinearTimingFunction&,
                                const TimingFunction&);
PLATFORM_EXPORT bool operator==(const CubicBezierTimingFunction&,
                                const TimingFunction&);
PLATFORM_EXPORT bool operator==(const StepsTimingFunction&,
                                const TimingFunction&);

PLATFORM_EXPORT bool operator==(const TimingFunction&, const TimingFunction&);
PLATFORM_EXPORT bool operator!=(const TimingFunction&, const TimingFunction&);

#define DEFINE_TIMING_FUNCTION_TYPE_CASTS(typeName, enumName)           \
  DEFINE_TYPE_CASTS(typeName##TimingFunction, TimingFunction, value,    \
                    value->getType() == TimingFunction::Type::enumName, \
                    value.getType() == TimingFunction::Type::enumName)

DEFINE_TIMING_FUNCTION_TYPE_CASTS(Linear, LINEAR);
DEFINE_TIMING_FUNCTION_TYPE_CASTS(CubicBezier, CUBIC_BEZIER);
DEFINE_TIMING_FUNCTION_TYPE_CASTS(Steps, STEPS);

}  // namespace blink

#endif  // TimingFunction_h