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
5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
7 /* Utilities for animation of computed style values */
9 #include "mozilla/StyleAnimationValue.h"
11 #include "mozilla/ArrayUtils.h"
12 #include "mozilla/MathAlgorithms.h"
13 #include "mozilla/PresShell.h"
14 #include "mozilla/PresShellInlines.h"
15 #include "mozilla/ServoStyleSet.h"
17 #include "mozilla/UniquePtr.h"
18 #include "nsCOMArray.h"
20 #include "mozilla/ComputedStyle.h"
21 #include "nsComputedDOMStyle.h"
22 #include "nsCSSPseudoElements.h"
23 #include "mozilla/dom/Element.h"
24 #include "mozilla/FloatingPoint.h"
25 #include "mozilla/Likely.h"
26 #include "mozilla/ServoBindings.h" // StyleLockedDeclarationBlock
27 #include "mozilla/ServoCSSParser.h"
28 #include "gfxMatrix.h"
29 #include "gfxQuaternion.h"
30 #include "mozilla/dom/Document.h"
32 #include "gfx2DGlue.h"
33 #include "mozilla/ComputedStyleInlines.h"
34 #include "mozilla/layers/LayersMessages.h"
36 using namespace mozilla
;
37 using namespace mozilla::css
;
38 using namespace mozilla::dom
;
39 using namespace mozilla::gfx
;
41 bool AnimationValue::operator==(const AnimationValue
& aOther
) const {
42 if (mServo
&& aOther
.mServo
) {
43 return Servo_AnimationValue_DeepEqual(mServo
, aOther
.mServo
);
45 if (!mServo
&& !aOther
.mServo
) {
51 bool AnimationValue::operator!=(const AnimationValue
& aOther
) const {
52 return !operator==(aOther
);
55 float AnimationValue::GetOpacity() const {
57 return Servo_AnimationValue_GetOpacity(mServo
);
60 nscolor
AnimationValue::GetColor(nscolor aForegroundColor
) const {
62 return Servo_AnimationValue_GetColor(mServo
, aForegroundColor
);
65 bool AnimationValue::IsCurrentColor() const {
67 return Servo_AnimationValue_IsCurrentColor(mServo
);
70 const StyleTranslate
& AnimationValue::GetTranslateProperty() const {
72 return *Servo_AnimationValue_GetTranslate(mServo
);
75 const StyleRotate
& AnimationValue::GetRotateProperty() const {
77 return *Servo_AnimationValue_GetRotate(mServo
);
80 const StyleScale
& AnimationValue::GetScaleProperty() const {
82 return *Servo_AnimationValue_GetScale(mServo
);
85 const StyleTransform
& AnimationValue::GetTransformProperty() const {
87 return *Servo_AnimationValue_GetTransform(mServo
);
90 void AnimationValue::GetOffsetPathProperty(StyleOffsetPath
& aOffsetPath
) const {
92 Servo_AnimationValue_GetOffsetPath(mServo
, &aOffsetPath
);
95 const mozilla::LengthPercentage
& AnimationValue::GetOffsetDistanceProperty()
98 return *Servo_AnimationValue_GetOffsetDistance(mServo
);
101 const mozilla::StyleOffsetRotate
& AnimationValue::GetOffsetRotateProperty()
104 return *Servo_AnimationValue_GetOffsetRotate(mServo
);
107 const mozilla::StylePositionOrAuto
& AnimationValue::GetOffsetAnchorProperty()
110 return *Servo_AnimationValue_GetOffsetAnchor(mServo
);
113 const mozilla::StyleOffsetPosition
& AnimationValue::GetOffsetPositionProperty()
116 return *Servo_AnimationValue_GetOffsetPosition(mServo
);
119 bool AnimationValue::IsOffsetPathUrl() const {
120 return mServo
&& Servo_AnimationValue_IsOffsetPathUrl(mServo
);
123 MatrixScales
AnimationValue::GetScaleValue(const nsIFrame
* aFrame
) const {
124 using namespace nsStyleTransformMatrix
;
126 switch (Servo_AnimationValue_GetPropertyId(mServo
)) {
127 case eCSSProperty_scale
: {
128 const StyleScale
& scale
= GetScaleProperty();
129 return scale
.IsNone()
131 : MatrixScales(scale
.AsScale()._0
, scale
.AsScale()._1
);
133 case eCSSProperty_rotate
:
134 case eCSSProperty_translate
:
135 return MatrixScales();
136 case eCSSProperty_transform
:
139 MOZ_ASSERT_UNREACHABLE(
140 "Should only need to check in transform properties");
141 return MatrixScales();
144 TransformReferenceBox
refBox(aFrame
);
146 ReadTransforms(StyleTranslate::None(), StyleRotate::None(),
147 StyleScale::None(), nullptr, GetTransformProperty(),
148 refBox
, aFrame
->PresContext()->AppUnitsPerDevPixel());
150 bool canDraw2D
= t
.CanDraw2D(&transform2d
);
152 return MatrixScales(0, 0);
154 return transform2d
.ScaleFactors();
157 void AnimationValue::SerializeSpecifiedValue(
158 nsCSSPropertyID aProperty
, const StylePerDocumentStyleData
* aRawData
,
159 nsACString
& aString
) const {
161 Servo_AnimationValue_Serialize(mServo
, aProperty
, aRawData
, &aString
);
164 bool AnimationValue::IsInterpolableWith(nsCSSPropertyID aProperty
,
165 const AnimationValue
& aToValue
) const {
166 if (IsNull() || aToValue
.IsNull()) {
171 MOZ_ASSERT(aToValue
.mServo
);
172 return Servo_AnimationValues_IsInterpolable(mServo
, aToValue
.mServo
);
175 double AnimationValue::ComputeDistance(nsCSSPropertyID aProperty
,
176 const AnimationValue
& aOther
) const {
177 if (IsNull() || aOther
.IsNull()) {
182 MOZ_ASSERT(aOther
.mServo
);
185 Servo_AnimationValues_ComputeDistance(mServo
, aOther
.mServo
);
186 return distance
< 0.0 ? 0.0 : distance
;
190 AnimationValue
AnimationValue::FromString(nsCSSPropertyID aProperty
,
191 const nsACString
& aValue
,
193 MOZ_ASSERT(aElement
);
195 AnimationValue result
;
197 nsCOMPtr
<Document
> doc
= aElement
->GetComposedDoc();
202 RefPtr
<PresShell
> presShell
= doc
->GetPresShell();
207 // GetComputedStyle() flushes style, so we shouldn't assume that any
208 // non-owning references we have are still valid.
209 RefPtr
<const ComputedStyle
> computedStyle
=
210 nsComputedDOMStyle::GetComputedStyle(aElement
);
211 MOZ_ASSERT(computedStyle
);
213 RefPtr
<StyleLockedDeclarationBlock
> declarations
=
214 ServoCSSParser::ParseProperty(aProperty
, aValue
,
215 ServoCSSParser::GetParsingEnvironment(doc
),
216 StyleParsingMode::DEFAULT
);
222 result
.mServo
= presShell
->StyleSet()->ComputeAnimationValue(
223 aElement
, declarations
, computedStyle
);
228 already_AddRefed
<StyleAnimationValue
> AnimationValue::FromAnimatable(
229 nsCSSPropertyID aProperty
, const layers::Animatable
& aAnimatable
) {
230 switch (aAnimatable
.type()) {
231 case layers::Animatable::Tnull_t
:
233 case layers::Animatable::TStyleTransform
: {
234 const StyleTransform
& transform
= aAnimatable
.get_StyleTransform();
235 MOZ_ASSERT(!transform
.HasPercent(),
236 "Received transform operations should have been resolved.");
237 return Servo_AnimationValue_Transform(&transform
).Consume();
239 case layers::Animatable::Tfloat
:
240 return Servo_AnimationValue_Opacity(aAnimatable
.get_float()).Consume();
241 case layers::Animatable::Tnscolor
:
242 return Servo_AnimationValue_Color(aProperty
, aAnimatable
.get_nscolor())
244 case layers::Animatable::TStyleRotate
:
245 return Servo_AnimationValue_Rotate(&aAnimatable
.get_StyleRotate())
247 case layers::Animatable::TStyleScale
:
248 return Servo_AnimationValue_Scale(&aAnimatable
.get_StyleScale())
250 case layers::Animatable::TStyleTranslate
:
252 aAnimatable
.get_StyleTranslate().IsNone() ||
253 (!aAnimatable
.get_StyleTranslate()
256 !aAnimatable
.get_StyleTranslate().AsTranslate()._1
.HasPercent()),
257 "Should have been resolved already");
258 return Servo_AnimationValue_Translate(&aAnimatable
.get_StyleTranslate())
260 case layers::Animatable::TStyleOffsetPath
:
261 return Servo_AnimationValue_OffsetPath(&aAnimatable
.get_StyleOffsetPath())
263 case layers::Animatable::TLengthPercentage
:
264 return Servo_AnimationValue_OffsetDistance(
265 &aAnimatable
.get_LengthPercentage())
267 case layers::Animatable::TStyleOffsetRotate
:
268 return Servo_AnimationValue_OffsetRotate(
269 &aAnimatable
.get_StyleOffsetRotate())
271 case layers::Animatable::TStylePositionOrAuto
:
272 return Servo_AnimationValue_OffsetAnchor(
273 &aAnimatable
.get_StylePositionOrAuto())
275 case layers::Animatable::TStyleOffsetPosition
:
276 return Servo_AnimationValue_OffsetPosition(
277 &aAnimatable
.get_StyleOffsetPosition())
280 MOZ_ASSERT_UNREACHABLE("Unsupported type");