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 /* representation of a value for a SMIL-animated CSS property */
9 #include "SMILCSSValueType.h"
11 #include "nsComputedDOMStyle.h"
13 #include "nsCSSProps.h"
14 #include "nsCSSValue.h"
16 #include "nsPresContextInlines.h"
17 #include "nsPresContext.h"
19 #include "nsStyleUtil.h"
20 #include "mozilla/DeclarationBlock.h"
21 #include "mozilla/PresShell.h"
22 #include "mozilla/PresShellInlines.h"
23 #include "mozilla/ServoBindings.h"
24 #include "mozilla/StyleAnimationValue.h"
25 #include "mozilla/ServoCSSParser.h"
26 #include "mozilla/ServoStyleSet.h"
27 #include "mozilla/SMILParserUtils.h"
28 #include "mozilla/SMILValue.h"
29 #include "mozilla/dom/BaseKeyframeTypesBinding.h"
30 #include "mozilla/dom/Document.h"
31 #include "mozilla/dom/Element.h"
33 using namespace mozilla::dom
;
37 using ServoAnimationValues
= CopyableAutoTArray
<RefPtr
<StyleAnimationValue
>, 1>;
40 SMILCSSValueType
SMILCSSValueType::sSingleton
;
43 ValueWrapper(nsCSSPropertyID aPropID
, const AnimationValue
& aValue
)
45 MOZ_ASSERT(!aValue
.IsNull());
46 mServoValues
.AppendElement(aValue
.mServo
);
48 ValueWrapper(nsCSSPropertyID aPropID
,
49 const RefPtr
<StyleAnimationValue
>& aValue
)
50 : mPropID(aPropID
), mServoValues
{(aValue
)} {}
51 ValueWrapper(nsCSSPropertyID aPropID
, ServoAnimationValues
&& aValues
)
52 : mPropID(aPropID
), mServoValues
{std::move(aValues
)} {}
54 bool operator==(const ValueWrapper
& aOther
) const {
55 if (mPropID
!= aOther
.mPropID
) {
59 MOZ_ASSERT(!mServoValues
.IsEmpty());
60 size_t len
= mServoValues
.Length();
61 if (len
!= aOther
.mServoValues
.Length()) {
64 for (size_t i
= 0; i
< len
; i
++) {
65 if (!Servo_AnimationValue_DeepEqual(mServoValues
[i
],
66 aOther
.mServoValues
[i
])) {
73 bool operator!=(const ValueWrapper
& aOther
) const {
74 return !(*this == aOther
);
77 nsCSSPropertyID mPropID
;
78 ServoAnimationValues mServoValues
;
84 // If one argument is null, this method updates it to point to "zero"
85 // for the other argument's Unit (if applicable; otherwise, we return false).
87 // If neither argument is null, this method simply returns true.
89 // If both arguments are null, this method returns false.
91 // |aZeroValueStorage| should be a reference to a
92 // RefPtr<StyleAnimationValue>. This is used where we may need to allocate a
93 // new ServoAnimationValue to represent the appropriate zero value.
95 // Returns true on success, or otherwise.
96 static bool FinalizeServoAnimationValues(
97 const RefPtr
<StyleAnimationValue
>*& aValue1
,
98 const RefPtr
<StyleAnimationValue
>*& aValue2
,
99 RefPtr
<StyleAnimationValue
>& aZeroValueStorage
) {
100 if (!aValue1
&& !aValue2
) {
104 // Are we missing either val? (If so, it's an implied 0 in other val's units)
107 aZeroValueStorage
= Servo_AnimationValues_GetZeroValue(*aValue2
).Consume();
108 aValue1
= &aZeroValueStorage
;
109 } else if (!aValue2
) {
110 aZeroValueStorage
= Servo_AnimationValues_GetZeroValue(*aValue1
).Consume();
111 aValue2
= &aZeroValueStorage
;
113 return *aValue1
&& *aValue2
;
116 static ValueWrapper
* ExtractValueWrapper(SMILValue
& aValue
) {
117 return static_cast<ValueWrapper
*>(aValue
.mU
.mPtr
);
120 static const ValueWrapper
* ExtractValueWrapper(const SMILValue
& aValue
) {
121 return static_cast<const ValueWrapper
*>(aValue
.mU
.mPtr
);
126 void SMILCSSValueType::Init(SMILValue
& aValue
) const {
127 MOZ_ASSERT(aValue
.IsNull(), "Unexpected SMIL value type");
129 aValue
.mU
.mPtr
= nullptr;
133 void SMILCSSValueType::Destroy(SMILValue
& aValue
) const {
134 MOZ_ASSERT(aValue
.mType
== this, "Unexpected SMIL value type");
135 delete static_cast<ValueWrapper
*>(aValue
.mU
.mPtr
);
136 aValue
.mType
= SMILNullType::Singleton();
139 nsresult
SMILCSSValueType::Assign(SMILValue
& aDest
,
140 const SMILValue
& aSrc
) const {
141 MOZ_ASSERT(aDest
.mType
== aSrc
.mType
, "Incompatible SMIL types");
142 MOZ_ASSERT(aDest
.mType
== this, "Unexpected SMIL value type");
143 const ValueWrapper
* srcWrapper
= ExtractValueWrapper(aSrc
);
144 ValueWrapper
* destWrapper
= ExtractValueWrapper(aDest
);
148 // barely-initialized dest -- need to alloc & copy
149 aDest
.mU
.mPtr
= new ValueWrapper(*srcWrapper
);
151 // both already fully-initialized -- just copy straight across
152 *destWrapper
= *srcWrapper
;
154 } else if (destWrapper
) {
155 // fully-initialized dest, barely-initialized src -- clear dest
157 aDest
.mU
.mPtr
= destWrapper
= nullptr;
158 } // else, both are barely-initialized -- nothing to do.
163 bool SMILCSSValueType::IsEqual(const SMILValue
& aLeft
,
164 const SMILValue
& aRight
) const {
165 MOZ_ASSERT(aLeft
.mType
== aRight
.mType
, "Incompatible SMIL types");
166 MOZ_ASSERT(aLeft
.mType
== this, "Unexpected SMIL value");
167 const ValueWrapper
* leftWrapper
= ExtractValueWrapper(aLeft
);
168 const ValueWrapper
* rightWrapper
= ExtractValueWrapper(aRight
);
173 NS_WARNING_ASSERTION(leftWrapper
!= rightWrapper
,
174 "Two SMILValues with matching ValueWrapper ptr");
175 return *leftWrapper
== *rightWrapper
;
177 // Left non-null, right null
181 // Left null, right non-null
188 static bool AddOrAccumulate(SMILValue
& aDest
, const SMILValue
& aValueToAdd
,
189 CompositeOperation aCompositeOp
, uint64_t aCount
) {
190 MOZ_ASSERT(aValueToAdd
.mType
== aDest
.mType
,
191 "Trying to add mismatching types");
192 MOZ_ASSERT(aValueToAdd
.mType
== &SMILCSSValueType::sSingleton
,
193 "Unexpected SMIL value type");
194 MOZ_ASSERT(aCompositeOp
== CompositeOperation::Add
||
195 aCompositeOp
== CompositeOperation::Accumulate
,
196 "Composite operation should be add or accumulate");
197 MOZ_ASSERT(aCompositeOp
!= CompositeOperation::Add
|| aCount
== 1,
198 "Count should be 1 if composite operation is add");
200 ValueWrapper
* destWrapper
= ExtractValueWrapper(aDest
);
201 const ValueWrapper
* valueToAddWrapper
= ExtractValueWrapper(aValueToAdd
);
203 // If both of the values are empty just fail. This can happen in rare cases
204 // such as when the underlying animation produced an empty value.
206 // Technically, it doesn't matter what we return here since in either case it
207 // will produce the same result: an empty value.
208 if (!destWrapper
&& !valueToAddWrapper
) {
212 nsCSSPropertyID property
=
213 valueToAddWrapper
? valueToAddWrapper
->mPropID
: destWrapper
->mPropID
;
214 // Special case: font-size-adjust and stroke-dasharray are explicitly
215 // non-additive (even though StyleAnimationValue *could* support adding them)
216 if (property
== eCSSProperty_font_size_adjust
||
217 property
== eCSSProperty_stroke_dasharray
) {
220 // Skip font shorthand since it includes font-size-adjust.
221 if (property
== eCSSProperty_font
) {
225 size_t len
= valueToAddWrapper
? valueToAddWrapper
->mServoValues
.Length()
226 : destWrapper
->mServoValues
.Length();
228 MOZ_ASSERT(!valueToAddWrapper
|| !destWrapper
||
229 valueToAddWrapper
->mServoValues
.Length() ==
230 destWrapper
->mServoValues
.Length(),
231 "Both of values' length in the wrappers should be the same if "
232 "both of them exist");
234 for (size_t i
= 0; i
< len
; i
++) {
235 const RefPtr
<StyleAnimationValue
>* valueToAdd
=
236 valueToAddWrapper
? &valueToAddWrapper
->mServoValues
[i
] : nullptr;
237 const RefPtr
<StyleAnimationValue
>* destValue
=
238 destWrapper
? &destWrapper
->mServoValues
[i
] : nullptr;
239 RefPtr
<StyleAnimationValue
> zeroValueStorage
;
240 if (!FinalizeServoAnimationValues(valueToAdd
, destValue
,
245 // FinalizeServoAnimationValues may have updated destValue so we should make
246 // sure the aDest and aDestWrapper outparams are up-to-date.
248 destWrapper
->mServoValues
[i
] = *destValue
;
250 // aDest may be a barely-initialized "zero" destination.
251 aDest
.mU
.mPtr
= destWrapper
= new ValueWrapper(property
, *destValue
);
252 destWrapper
->mServoValues
.SetLength(len
);
255 RefPtr
<StyleAnimationValue
> result
;
256 if (aCompositeOp
== CompositeOperation::Add
) {
257 result
= Servo_AnimationValues_Add(*destValue
, *valueToAdd
).Consume();
259 result
= Servo_AnimationValues_Accumulate(*destValue
, *valueToAdd
, aCount
)
266 destWrapper
->mServoValues
[i
] = result
;
272 nsresult
SMILCSSValueType::SandwichAdd(SMILValue
& aDest
,
273 const SMILValue
& aValueToAdd
) const {
274 return AddOrAccumulate(aDest
, aValueToAdd
, CompositeOperation::Add
, 1)
279 nsresult
SMILCSSValueType::Add(SMILValue
& aDest
, const SMILValue
& aValueToAdd
,
280 uint32_t aCount
) const {
281 return AddOrAccumulate(aDest
, aValueToAdd
, CompositeOperation::Accumulate
,
287 nsresult
SMILCSSValueType::ComputeDistance(const SMILValue
& aFrom
,
288 const SMILValue
& aTo
,
289 double& aDistance
) const {
290 MOZ_ASSERT(aFrom
.mType
== aTo
.mType
, "Trying to compare different types");
291 MOZ_ASSERT(aFrom
.mType
== this, "Unexpected source type");
293 const ValueWrapper
* fromWrapper
= ExtractValueWrapper(aFrom
);
294 const ValueWrapper
* toWrapper
= ExtractValueWrapper(aTo
);
295 MOZ_ASSERT(toWrapper
, "expecting non-null endpoint");
297 size_t len
= toWrapper
->mServoValues
.Length();
298 MOZ_ASSERT(!fromWrapper
|| fromWrapper
->mServoValues
.Length() == len
,
299 "From and to values length should be the same if "
300 "The start value exists");
302 double squareDistance
= 0;
304 for (size_t i
= 0; i
< len
; i
++) {
305 const RefPtr
<StyleAnimationValue
>* fromValue
=
306 fromWrapper
? &fromWrapper
->mServoValues
[i
] : nullptr;
307 const RefPtr
<StyleAnimationValue
>* toValue
= &toWrapper
->mServoValues
[i
];
308 RefPtr
<StyleAnimationValue
> zeroValueStorage
;
309 if (!FinalizeServoAnimationValues(fromValue
, toValue
, zeroValueStorage
)) {
310 return NS_ERROR_FAILURE
;
314 Servo_AnimationValues_ComputeDistance(*fromValue
, *toValue
);
315 if (distance
< 0.0) {
316 return NS_ERROR_FAILURE
;
320 aDistance
= distance
;
323 squareDistance
+= distance
* distance
;
326 aDistance
= sqrt(squareDistance
);
331 nsresult
SMILCSSValueType::Interpolate(const SMILValue
& aStartVal
,
332 const SMILValue
& aEndVal
,
333 double aUnitDistance
,
334 SMILValue
& aResult
) const {
335 MOZ_ASSERT(aStartVal
.mType
== aEndVal
.mType
,
336 "Trying to interpolate different types");
337 MOZ_ASSERT(aStartVal
.mType
== this, "Unexpected types for interpolation");
338 MOZ_ASSERT(aResult
.mType
== this, "Unexpected result type");
339 MOZ_ASSERT(aUnitDistance
>= 0.0 && aUnitDistance
<= 1.0,
340 "unit distance value out of bounds");
341 MOZ_ASSERT(!aResult
.mU
.mPtr
, "expecting barely-initialized outparam");
343 const ValueWrapper
* startWrapper
= ExtractValueWrapper(aStartVal
);
344 const ValueWrapper
* endWrapper
= ExtractValueWrapper(aEndVal
);
345 MOZ_ASSERT(endWrapper
, "expecting non-null endpoint");
347 // For discretely-animated properties Servo_AnimationValues_Interpolate will
348 // perform the discrete animation (i.e. 50% flip) and return a success result.
349 // However, SMIL has its own special discrete animation behavior that it uses
350 // when keyTimes are specified, but we won't run that unless that this method
351 // returns a failure to indicate that the property cannot be smoothly
352 // interpolated, i.e. that we need to use a discrete calcMode.
354 // For shorthands, Servo_Property_IsDiscreteAnimatable will always return
355 // false. That's fine since most shorthands (like 'font' and
356 // 'text-decoration') include non-discrete components. If authors want to
357 // treat all components as discrete then they should use calcMode="discrete".
358 if (Servo_Property_IsDiscreteAnimatable(endWrapper
->mPropID
)) {
359 return NS_ERROR_FAILURE
;
362 ServoAnimationValues results
;
363 size_t len
= endWrapper
->mServoValues
.Length();
364 results
.SetCapacity(len
);
365 MOZ_ASSERT(!startWrapper
|| startWrapper
->mServoValues
.Length() == len
,
366 "Start and end values length should be the same if "
367 "the start value exists");
368 for (size_t i
= 0; i
< len
; i
++) {
369 const RefPtr
<StyleAnimationValue
>* startValue
=
370 startWrapper
? &startWrapper
->mServoValues
[i
] : nullptr;
371 const RefPtr
<StyleAnimationValue
>* endValue
= &endWrapper
->mServoValues
[i
];
372 RefPtr
<StyleAnimationValue
> zeroValueStorage
;
373 if (!FinalizeServoAnimationValues(startValue
, endValue
, zeroValueStorage
)) {
374 return NS_ERROR_FAILURE
;
377 RefPtr
<StyleAnimationValue
> result
=
378 Servo_AnimationValues_Interpolate(*startValue
, *endValue
, aUnitDistance
)
381 return NS_ERROR_FAILURE
;
383 results
.AppendElement(result
);
385 aResult
.mU
.mPtr
= new ValueWrapper(endWrapper
->mPropID
, std::move(results
));
390 static ServoAnimationValues
ValueFromStringHelper(
391 nsCSSPropertyID aPropID
, Element
* aTargetElement
,
392 nsPresContext
* aPresContext
, const ComputedStyle
* aComputedStyle
,
393 const nsAString
& aString
) {
394 ServoAnimationValues result
;
396 Document
* doc
= aTargetElement
->GetComposedDoc();
402 ServoCSSParser::ParsingEnvironment env
=
403 ServoCSSParser::GetParsingEnvironment(doc
);
404 RefPtr
<StyleLockedDeclarationBlock
> servoDeclarationBlock
=
405 ServoCSSParser::ParseProperty(
406 aPropID
, NS_ConvertUTF16toUTF8(aString
), env
,
407 StyleParsingMode::ALLOW_UNITLESS_LENGTH
|
408 StyleParsingMode::ALLOW_ALL_NUMERIC_VALUES
);
409 if (!servoDeclarationBlock
) {
414 aPresContext
->StyleSet()->GetAnimationValues(
415 servoDeclarationBlock
, aTargetElement
, aComputedStyle
, result
);
421 void SMILCSSValueType::ValueFromString(nsCSSPropertyID aPropID
,
422 Element
* aTargetElement
,
423 const nsAString
& aString
,
425 bool* aIsContextSensitive
) {
426 MOZ_ASSERT(aValue
.IsNull(), "Outparam should be null-typed");
427 nsPresContext
* presContext
=
428 nsContentUtils::GetContextForContent(aTargetElement
);
430 NS_WARNING("Not parsing animation value; unable to get PresContext");
434 Document
* doc
= aTargetElement
->GetComposedDoc();
435 if (doc
&& !nsStyleUtil::CSPAllowsInlineStyle(nullptr, doc
, nullptr, 0, 1,
440 RefPtr
<const ComputedStyle
> computedStyle
=
441 nsComputedDOMStyle::GetComputedStyle(aTargetElement
);
442 if (!computedStyle
) {
446 ServoAnimationValues parsedValues
= ValueFromStringHelper(
447 aPropID
, aTargetElement
, presContext
, computedStyle
, aString
);
448 if (aIsContextSensitive
) {
449 // FIXME: Bug 1358955 - detect context-sensitive values and set this value
451 *aIsContextSensitive
= false;
454 if (!parsedValues
.IsEmpty()) {
455 sSingleton
.Init(aValue
);
456 aValue
.mU
.mPtr
= new ValueWrapper(aPropID
, std::move(parsedValues
));
461 SMILValue
SMILCSSValueType::ValueFromAnimationValue(
462 nsCSSPropertyID aPropID
, Element
* aTargetElement
,
463 const AnimationValue
& aValue
) {
466 Document
* doc
= aTargetElement
->GetComposedDoc();
467 // We'd like to avoid serializing |aValue| if possible, and since the
468 // string passed to CSPAllowsInlineStyle is only used for reporting violations
469 // and an intermediate CSS value is not likely to be particularly useful
470 // in that case, we just use a generic placeholder string instead.
471 static const nsLiteralString kPlaceholderText
= u
"[SVG animation of CSS]"_ns
;
472 if (doc
&& !nsStyleUtil::CSPAllowsInlineStyle(nullptr, doc
, nullptr, 0, 1,
473 kPlaceholderText
, nullptr)) {
477 sSingleton
.Init(result
);
478 result
.mU
.mPtr
= new ValueWrapper(aPropID
, aValue
);
484 bool SMILCSSValueType::SetPropertyValues(const SMILValue
& aValue
,
485 DeclarationBlock
& aDecl
) {
486 MOZ_ASSERT(aValue
.mType
== &SMILCSSValueType::sSingleton
,
487 "Unexpected SMIL value type");
488 const ValueWrapper
* wrapper
= ExtractValueWrapper(aValue
);
493 bool changed
= false;
494 for (const auto& value
: wrapper
->mServoValues
) {
495 changed
|= Servo_DeclarationBlock_SetPropertyToAnimationValue(aDecl
.Raw(),
503 nsCSSPropertyID
SMILCSSValueType::PropertyFromValue(const SMILValue
& aValue
) {
504 if (aValue
.mType
!= &SMILCSSValueType::sSingleton
) {
505 return eCSSProperty_UNKNOWN
;
508 const ValueWrapper
* wrapper
= ExtractValueWrapper(aValue
);
510 return eCSSProperty_UNKNOWN
;
513 return wrapper
->mPropID
;
517 void SMILCSSValueType::FinalizeValue(SMILValue
& aValue
,
518 const SMILValue
& aValueToMatch
) {
519 MOZ_ASSERT(aValue
.mType
== aValueToMatch
.mType
, "Incompatible SMIL types");
520 MOZ_ASSERT(aValue
.mType
== &SMILCSSValueType::sSingleton
,
521 "Unexpected SMIL value type");
523 ValueWrapper
* valueWrapper
= ExtractValueWrapper(aValue
);
524 // If |aValue| already has a value, there's nothing to do here.
529 const ValueWrapper
* valueToMatchWrapper
= ExtractValueWrapper(aValueToMatch
);
530 if (!valueToMatchWrapper
) {
531 MOZ_ASSERT_UNREACHABLE("Value to match is empty");
535 ServoAnimationValues zeroValues
;
536 zeroValues
.SetCapacity(valueToMatchWrapper
->mServoValues
.Length());
538 for (const auto& valueToMatch
: valueToMatchWrapper
->mServoValues
) {
539 RefPtr
<StyleAnimationValue
> zeroValue
=
540 Servo_AnimationValues_GetZeroValue(valueToMatch
).Consume();
544 zeroValues
.AppendElement(std::move(zeroValue
));
547 new ValueWrapper(valueToMatchWrapper
->mPropID
, std::move(zeroValues
));
550 } // namespace mozilla