/work/obj-fuzz/dist/include/mozilla/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 | 0 | { |
26 | 0 | return ComputedTimingFunction(x1, y1, x2, y2); |
27 | 0 | } |
28 | | static ComputedTimingFunction |
29 | | Steps(nsTimingFunction::Type aType, uint32_t aSteps) |
30 | 0 | { |
31 | 0 | MOZ_ASSERT(aType == nsTimingFunction::Type::StepStart || |
32 | 0 | aType == nsTimingFunction::Type::StepEnd, |
33 | 0 | "The type of timing function should be either step-start or " |
34 | 0 | "step-end"); |
35 | 0 | MOZ_ASSERT(aSteps > 0, "The number of steps should be 1 or more"); |
36 | 0 | return ComputedTimingFunction(aType, aSteps); |
37 | 0 | } |
38 | | static ComputedTimingFunction |
39 | | Frames(uint32_t aFrames) |
40 | 0 | { |
41 | 0 | MOZ_ASSERT(aFrames > 1, "The number of frames should be 2 or more"); |
42 | 0 | return ComputedTimingFunction(nsTimingFunction::Type::Frames, aFrames); |
43 | 0 | } |
44 | | |
45 | 0 | ComputedTimingFunction() = default; |
46 | | explicit ComputedTimingFunction(const nsTimingFunction& aFunction) |
47 | | : mStepsOrFrames(0) |
48 | 0 | { |
49 | 0 | Init(aFunction); |
50 | 0 | } |
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 | 0 | { |
62 | 0 | NS_ASSERTION(HasSpline(), "Type mismatch"); |
63 | 0 | return &mTimingFunction; |
64 | 0 | } |
65 | | nsTimingFunction::Type GetType() const { return mType; } |
66 | | bool HasSpline() const { return nsTimingFunction::IsSplineType(mType); } |
67 | | uint32_t GetSteps() const |
68 | 0 | { |
69 | 0 | MOZ_ASSERT(mType == nsTimingFunction::Type::StepStart || |
70 | 0 | mType == nsTimingFunction::Type::StepEnd); |
71 | 0 | return mStepsOrFrames; |
72 | 0 | } |
73 | | uint32_t GetFrames() const |
74 | 0 | { |
75 | 0 | MOZ_ASSERT(mType == nsTimingFunction::Type::Frames); |
76 | 0 | return mStepsOrFrames; |
77 | 0 | } |
78 | | bool operator==(const ComputedTimingFunction& aOther) const |
79 | | { |
80 | | return mType == aOther.mType && |
81 | | (HasSpline() ? |
82 | | mTimingFunction == aOther.mTimingFunction : |
83 | | mStepsOrFrames == aOther.mStepsOrFrames); |
84 | | } |
85 | | bool operator!=(const ComputedTimingFunction& aOther) const |
86 | 0 | { |
87 | 0 | return !(*this == aOther); |
88 | 0 | } |
89 | | bool operator==(const nsTimingFunction& aOther) const |
90 | 0 | { |
91 | 0 | return mType == aOther.mType && |
92 | 0 | (HasSpline() |
93 | 0 | ? mTimingFunction.X1() == aOther.mFunc.mX1 && |
94 | 0 | mTimingFunction.Y1() == aOther.mFunc.mY1 && |
95 | 0 | mTimingFunction.X2() == aOther.mFunc.mX2 && |
96 | 0 | mTimingFunction.Y2() == aOther.mFunc.mY2 |
97 | 0 | : mStepsOrFrames == aOther.mStepsOrFrames); |
98 | 0 | } |
99 | | bool operator!=(const nsTimingFunction& aOther) const |
100 | 0 | { |
101 | 0 | return !(*this == aOther); |
102 | 0 | } |
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 | 0 | { |
110 | 0 | return aFunction ? aFunction->GetValue(aPortion, aBeforeFlag) : aPortion; |
111 | 0 | } |
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 | 0 | { |
121 | 0 | } |
122 | | ComputedTimingFunction(nsTimingFunction::Type aType, uint32_t aStepsOrFrames) |
123 | | : mType(aType) |
124 | 0 | , 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 | 0 | { |
135 | 0 | if (aLHS.isNothing()) { |
136 | 0 | return aRHS.mType == nsTimingFunction::Type::Linear; |
137 | 0 | } |
138 | 0 | return aLHS.value() == aRHS; |
139 | 0 | } |
140 | | |
141 | | inline bool |
142 | | operator!=(const Maybe<ComputedTimingFunction>& aLHS, |
143 | | const nsTimingFunction& aRHS) |
144 | 0 | { |
145 | 0 | return !(aLHS == aRHS); |
146 | 0 | } |
147 | | |
148 | | } // namespace mozilla |
149 | | |
150 | | #endif // mozilla_ComputedTimingFunction_h |