Coverage Report

Created: 2018-09-25 14:53

/src/mozilla-central/dom/animation/ComputedTimingFunction.h
Line
Count
Source (jump to first uncovered line)
1
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2
/* vim: set ts=8 sts=2 et sw=2 tw=80: */
3
/* This Source Code Form is subject to the terms of the Mozilla Public
4
 * License, v. 2.0. If a copy of the MPL was not distributed with this file,
5
 * You can obtain one at http://mozilla.org/MPL/2.0/. */
6
7
#ifndef mozilla_ComputedTimingFunction_h
8
#define mozilla_ComputedTimingFunction_h
9
10
#include "nsDebug.h"
11
#include "nsSMILKeySpline.h"  // nsSMILKeySpline
12
#include "nsStringFwd.h"
13
#include "nsTimingFunction.h"
14
15
#include "mozilla/Assertions.h"
16
#include "mozilla/Maybe.h"
17
18
namespace mozilla {
19
20
class ComputedTimingFunction
21
{
22
public:
23
  static ComputedTimingFunction
24
  CubicBezier(double x1, double y1, double x2, double y2)
25
  {
26
    return ComputedTimingFunction(x1, y1, x2, y2);
27
  }
28
  static ComputedTimingFunction
29
  Steps(nsTimingFunction::Type aType, uint32_t aSteps)
30
  {
31
    MOZ_ASSERT(aType == nsTimingFunction::Type::StepStart ||
32
               aType == nsTimingFunction::Type::StepEnd,
33
               "The type of timing function should be either step-start or "
34
               "step-end");
35
    MOZ_ASSERT(aSteps > 0, "The number of steps should be 1 or more");
36
    return ComputedTimingFunction(aType, aSteps);
37
  }
38
  static ComputedTimingFunction
39
  Frames(uint32_t aFrames)
40
  {
41
    MOZ_ASSERT(aFrames > 1, "The number of frames should be 2 or more");
42
    return ComputedTimingFunction(nsTimingFunction::Type::Frames, aFrames);
43
  }
44
45
  ComputedTimingFunction() = default;
46
  explicit ComputedTimingFunction(const nsTimingFunction& aFunction)
47
    : mStepsOrFrames(0)
48
  {
49
    Init(aFunction);
50
  }
51
  void Init(const nsTimingFunction& aFunction);
52
53
  // BeforeFlag is used in step timing function.
54
  // https://drafts.csswg.org/css-timing/#before-flag
55
  enum class BeforeFlag {
56
    Unset,
57
    Set
58
  };
59
  double GetValue(double aPortion, BeforeFlag aBeforeFlag) const;
60
  const nsSMILKeySpline* GetFunction() const
61
  {
62
    NS_ASSERTION(HasSpline(), "Type mismatch");
63
    return &mTimingFunction;
64
  }
65
0
  nsTimingFunction::Type GetType() const { return mType; }
66
0
  bool HasSpline() const { return nsTimingFunction::IsSplineType(mType); }
67
  uint32_t GetSteps() const
68
  {
69
    MOZ_ASSERT(mType == nsTimingFunction::Type::StepStart ||
70
               mType == nsTimingFunction::Type::StepEnd);
71
    return mStepsOrFrames;
72
  }
73
  uint32_t GetFrames() const
74
  {
75
    MOZ_ASSERT(mType == nsTimingFunction::Type::Frames);
76
    return mStepsOrFrames;
77
  }
78
  bool operator==(const ComputedTimingFunction& aOther) const
79
0
  {
80
0
    return mType == aOther.mType &&
81
0
           (HasSpline() ?
82
0
            mTimingFunction == aOther.mTimingFunction :
83
0
            mStepsOrFrames == aOther.mStepsOrFrames);
84
0
  }
85
  bool operator!=(const ComputedTimingFunction& aOther) const
86
  {
87
    return !(*this == aOther);
88
  }
89
  bool operator==(const nsTimingFunction& aOther) const
90
  {
91
    return mType == aOther.mType &&
92
           (HasSpline()
93
            ? mTimingFunction.X1() == aOther.mFunc.mX1 &&
94
              mTimingFunction.Y1() == aOther.mFunc.mY1 &&
95
              mTimingFunction.X2() == aOther.mFunc.mX2 &&
96
              mTimingFunction.Y2() == aOther.mFunc.mY2
97
            : mStepsOrFrames == aOther.mStepsOrFrames);
98
  }
99
  bool operator!=(const nsTimingFunction& aOther) const
100
  {
101
    return !(*this == aOther);
102
  }
103
  int32_t Compare(const ComputedTimingFunction& aRhs) const;
104
  void AppendToString(nsAString& aResult) const;
105
106
  static double GetPortion(const Maybe<ComputedTimingFunction>& aFunction,
107
                           double aPortion,
108
                           BeforeFlag aBeforeFlag)
109
  {
110
    return aFunction ? aFunction->GetValue(aPortion, aBeforeFlag) : aPortion;
111
  }
112
  static int32_t Compare(const Maybe<ComputedTimingFunction>& aLhs,
113
                         const Maybe<ComputedTimingFunction>& aRhs);
114
115
private:
116
  ComputedTimingFunction(double x1, double y1, double x2, double y2)
117
    : mType(nsTimingFunction::Type::CubicBezier)
118
    , mTimingFunction(x1, y1, x2, y2)
119
    , mStepsOrFrames(0)
120
  {
121
  }
122
  ComputedTimingFunction(nsTimingFunction::Type aType, uint32_t aStepsOrFrames)
123
    : mType(aType)
124
    , mStepsOrFrames(aStepsOrFrames) { }
125
126
  nsTimingFunction::Type mType = nsTimingFunction::Type::Linear;
127
  nsSMILKeySpline mTimingFunction;
128
  uint32_t mStepsOrFrames;
129
};
130
131
inline bool
132
operator==(const Maybe<ComputedTimingFunction>& aLHS,
133
           const nsTimingFunction& aRHS)
134
{
135
  if (aLHS.isNothing()) {
136
    return aRHS.mType == nsTimingFunction::Type::Linear;
137
  }
138
  return aLHS.value() == aRHS;
139
}
140
141
inline bool
142
operator!=(const Maybe<ComputedTimingFunction>& aLHS,
143
           const nsTimingFunction& aRHS)
144
{
145
  return !(aLHS == aRHS);
146
}
147
148
} // namespace mozilla
149
150
#endif // mozilla_ComputedTimingFunction_h