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 #include "SVGAnimatedLengthList.h"
11 #include "DOMSVGAnimatedLengthList.h"
12 #include "SVGLengthListSMILType.h"
13 #include "mozilla/SMILValue.h"
14 #include "mozilla/dom/SVGElement.h"
15 #include "mozilla/dom/SVGLengthBinding.h"
21 nsresult
SVGAnimatedLengthList::SetBaseValueString(const nsAString
& aValue
) {
22 SVGLengthList newBaseValue
;
23 MOZ_TRY(newBaseValue
.SetValueFromString(aValue
));
25 DOMSVGAnimatedLengthList
* domWrapper
=
26 DOMSVGAnimatedLengthList::GetDOMWrapperIfExists(this);
28 // We must send this notification *before* changing mBaseVal! If the length
29 // of our baseVal is being reduced, our baseVal's DOM wrapper list may have
30 // to remove DOM items from itself, and any removed DOM items need to copy
31 // their internal counterpart values *before* we change them.
33 domWrapper
->InternalBaseValListWillChangeTo(newBaseValue
);
36 // We don't need to call DidChange* here - we're only called by
37 // SVGElement::ParseAttribute under Element::SetAttr,
38 // which takes care of notifying.
39 mBaseVal
.SwapWith(newBaseValue
);
43 void SVGAnimatedLengthList::ClearBaseValue(uint32_t aAttrEnum
) {
44 DOMSVGAnimatedLengthList
* domWrapper
=
45 DOMSVGAnimatedLengthList::GetDOMWrapperIfExists(this);
47 // We must send this notification *before* changing mBaseVal! (See above.)
48 domWrapper
->InternalBaseValListWillChangeTo(SVGLengthList());
54 nsresult
SVGAnimatedLengthList::SetAnimValue(const SVGLengthList
& aNewAnimValue
,
57 DOMSVGAnimatedLengthList
* domWrapper
=
58 DOMSVGAnimatedLengthList::GetDOMWrapperIfExists(this);
60 // A new animation may totally change the number of items in the animVal
61 // list, replacing what was essentially a mirror of the baseVal list, or
62 // else replacing and overriding an existing animation. When this happens
63 // we must try and keep our animVal's DOM wrapper in sync (see the comment
64 // in DOMSVGAnimatedLengthList::InternalBaseValListWillChangeTo).
66 // It's not possible for us to reliably distinguish between calls to this
67 // method that are setting a new sample for an existing animation, and
68 // calls that are setting the first sample of an animation that will
69 // override an existing animation. Happily it's cheap to just blindly
70 // notify our animVal's DOM wrapper of its internal counterpart's new value
71 // each time this method is called, so that's what we do.
73 // Note that we must send this notification *before* setting or changing
74 // mAnimVal! (See the comment in SetBaseValueString above.)
76 domWrapper
->InternalAnimValListWillChangeTo(aNewAnimValue
);
79 mAnimVal
= MakeUnique
<SVGLengthList
>();
81 nsresult rv
= mAnimVal
->CopyFrom(aNewAnimValue
);
83 // OOM. We clear the animation, and, importantly, ClearAnimValue() ensures
84 // that mAnimVal and its DOM wrapper (if any) will have the same length!
85 ClearAnimValue(aElement
, aAttrEnum
);
88 aElement
->DidAnimateLengthList(aAttrEnum
);
92 void SVGAnimatedLengthList::ClearAnimValue(SVGElement
* aElement
,
94 DOMSVGAnimatedLengthList
* domWrapper
=
95 DOMSVGAnimatedLengthList::GetDOMWrapperIfExists(this);
97 // When all animation ends, animVal simply mirrors baseVal, which may have
98 // a different number of items to the last active animated value. We must
99 // keep the length of our animVal's DOM wrapper list in sync, and again we
100 // must do that before touching mAnimVal. See comments above.
102 domWrapper
->InternalAnimValListWillChangeTo(mBaseVal
);
105 aElement
->DidAnimateLengthList(aAttrEnum
);
108 UniquePtr
<SMILAttr
> SVGAnimatedLengthList::ToSMILAttr(SVGElement
* aSVGElement
,
111 bool aCanZeroPadList
) {
112 return MakeUnique
<SMILAnimatedLengthList
>(this, aSVGElement
, aAttrEnum
, aAxis
,
116 nsresult
SVGAnimatedLengthList::SMILAnimatedLengthList::ValueFromString(
117 const nsAString
& aStr
, const dom::SVGAnimationElement
* /*aSrcElement*/,
118 SMILValue
& aValue
, bool& aPreventCachingOfSandwich
) const {
119 SMILValue
val(&SVGLengthListSMILType::sSingleton
);
120 SVGLengthListAndInfo
* llai
= static_cast<SVGLengthListAndInfo
*>(val
.mU
.mPtr
);
121 nsresult rv
= llai
->SetValueFromString(aStr
);
122 if (NS_SUCCEEDED(rv
)) {
123 llai
->SetInfo(mElement
, mAxis
, mCanZeroPadList
);
124 aValue
= std::move(val
);
126 // If any of the lengths in the list depend on their context, then we must
127 // prevent caching of the entire animation sandwich. This is because the
128 // units of a length at a given index can change from sandwich layer to
129 // layer, and indeed even be different within a single sandwich layer. If
130 // any length in the result of an animation sandwich is the result of the
131 // addition of lengths where one or more of those lengths is context
132 // dependent, then naturally the resultant length is also context
133 // dependent, regardless of whether its actual unit is context dependent or
134 // not. Unfortunately normal invalidation mechanisms won't cause us to
135 // recalculate the result of the sandwich if the context changes, so we
136 // take the (substantial) performance hit of preventing caching of the
137 // sandwich layer, causing the animation sandwich to be recalculated every
140 for (uint32_t i
= 0; i
< llai
->Length(); ++i
) {
141 uint8_t unit
= (*llai
)[i
].GetUnit();
142 if (!SVGLength::IsAbsoluteUnit(unit
)) {
143 aPreventCachingOfSandwich
= true;
151 SMILValue
SVGAnimatedLengthList::SMILAnimatedLengthList::GetBaseValue() const {
152 // To benefit from Return Value Optimization and avoid copy constructor calls
153 // due to our use of return-by-value, we must return the exact same object
154 // from ALL return points. This function must only return THIS variable:
157 SMILValue
tmp(&SVGLengthListSMILType::sSingleton
);
158 SVGLengthListAndInfo
* llai
= static_cast<SVGLengthListAndInfo
*>(tmp
.mU
.mPtr
);
159 nsresult rv
= llai
->CopyFrom(mVal
->mBaseVal
);
160 if (NS_SUCCEEDED(rv
)) {
161 llai
->SetInfo(mElement
, mAxis
, mCanZeroPadList
);
162 val
= std::move(tmp
);
167 nsresult
SVGAnimatedLengthList::SMILAnimatedLengthList::SetAnimValue(
168 const SMILValue
& aValue
) {
169 NS_ASSERTION(aValue
.mType
== &SVGLengthListSMILType::sSingleton
,
170 "Unexpected type to assign animated value");
171 if (aValue
.mType
== &SVGLengthListSMILType::sSingleton
) {
172 mVal
->SetAnimValue(*static_cast<SVGLengthListAndInfo
*>(aValue
.mU
.mPtr
),
173 mElement
, mAttrEnum
);
178 void SVGAnimatedLengthList::SMILAnimatedLengthList::ClearAnimValue() {
179 if (mVal
->mAnimVal
) {
180 mVal
->ClearAnimValue(mElement
, mAttrEnum
);
184 } // namespace mozilla