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 nsresult rv
= newBaseValue
.SetValueFromString(aValue
);
28 DOMSVGAnimatedLengthList
* domWrapper
=
29 DOMSVGAnimatedLengthList::GetDOMWrapperIfExists(this);
31 // We must send this notification *before* changing mBaseVal! If the length
32 // of our baseVal is being reduced, our baseVal's DOM wrapper list may have
33 // to remove DOM items from itself, and any removed DOM items need to copy
34 // their internal counterpart values *before* we change them.
36 domWrapper
->InternalBaseValListWillChangeTo(newBaseValue
);
39 // We don't need to call DidChange* here - we're only called by
40 // SVGElement::ParseAttribute under Element::SetAttr,
41 // which takes care of notifying.
43 rv
= mBaseVal
.CopyFrom(newBaseValue
);
44 if (NS_FAILED(rv
) && domWrapper
) {
45 // Attempting to increase mBaseVal's length failed - reduce domWrapper
46 // back to the same length:
47 domWrapper
->InternalBaseValListWillChangeTo(mBaseVal
);
52 void SVGAnimatedLengthList::ClearBaseValue(uint32_t aAttrEnum
) {
53 DOMSVGAnimatedLengthList
* domWrapper
=
54 DOMSVGAnimatedLengthList::GetDOMWrapperIfExists(this);
56 // We must send this notification *before* changing mBaseVal! (See above.)
57 domWrapper
->InternalBaseValListWillChangeTo(SVGLengthList());
63 nsresult
SVGAnimatedLengthList::SetAnimValue(const SVGLengthList
& aNewAnimValue
,
66 DOMSVGAnimatedLengthList
* domWrapper
=
67 DOMSVGAnimatedLengthList::GetDOMWrapperIfExists(this);
69 // A new animation may totally change the number of items in the animVal
70 // list, replacing what was essentially a mirror of the baseVal list, or
71 // else replacing and overriding an existing animation. When this happens
72 // we must try and keep our animVal's DOM wrapper in sync (see the comment
73 // in DOMSVGAnimatedLengthList::InternalBaseValListWillChangeTo).
75 // It's not possible for us to reliably distinguish between calls to this
76 // method that are setting a new sample for an existing animation, and
77 // calls that are setting the first sample of an animation that will
78 // override an existing animation. Happily it's cheap to just blindly
79 // notify our animVal's DOM wrapper of its internal counterpart's new value
80 // each time this method is called, so that's what we do.
82 // Note that we must send this notification *before* setting or changing
83 // mAnimVal! (See the comment in SetBaseValueString above.)
85 domWrapper
->InternalAnimValListWillChangeTo(aNewAnimValue
);
88 mAnimVal
= MakeUnique
<SVGLengthList
>();
90 nsresult rv
= mAnimVal
->CopyFrom(aNewAnimValue
);
92 // OOM. We clear the animation, and, importantly, ClearAnimValue() ensures
93 // that mAnimVal and its DOM wrapper (if any) will have the same length!
94 ClearAnimValue(aElement
, aAttrEnum
);
97 aElement
->DidAnimateLengthList(aAttrEnum
);
101 void SVGAnimatedLengthList::ClearAnimValue(SVGElement
* aElement
,
102 uint32_t aAttrEnum
) {
103 DOMSVGAnimatedLengthList
* domWrapper
=
104 DOMSVGAnimatedLengthList::GetDOMWrapperIfExists(this);
106 // When all animation ends, animVal simply mirrors baseVal, which may have
107 // a different number of items to the last active animated value. We must
108 // keep the length of our animVal's DOM wrapper list in sync, and again we
109 // must do that before touching mAnimVal. See comments above.
111 domWrapper
->InternalAnimValListWillChangeTo(mBaseVal
);
114 aElement
->DidAnimateLengthList(aAttrEnum
);
117 UniquePtr
<SMILAttr
> SVGAnimatedLengthList::ToSMILAttr(SVGElement
* aSVGElement
,
120 bool aCanZeroPadList
) {
121 return MakeUnique
<SMILAnimatedLengthList
>(this, aSVGElement
, aAttrEnum
, aAxis
,
125 nsresult
SVGAnimatedLengthList::SMILAnimatedLengthList::ValueFromString(
126 const nsAString
& aStr
, const dom::SVGAnimationElement
* /*aSrcElement*/,
127 SMILValue
& aValue
, bool& aPreventCachingOfSandwich
) const {
128 SMILValue
val(&SVGLengthListSMILType::sSingleton
);
129 SVGLengthListAndInfo
* llai
= static_cast<SVGLengthListAndInfo
*>(val
.mU
.mPtr
);
130 nsresult rv
= llai
->SetValueFromString(aStr
);
131 if (NS_SUCCEEDED(rv
)) {
132 llai
->SetInfo(mElement
, mAxis
, mCanZeroPadList
);
133 aValue
= std::move(val
);
135 // If any of the lengths in the list depend on their context, then we must
136 // prevent caching of the entire animation sandwich. This is because the
137 // units of a length at a given index can change from sandwich layer to
138 // layer, and indeed even be different within a single sandwich layer. If
139 // any length in the result of an animation sandwich is the result of the
140 // addition of lengths where one or more of those lengths is context
141 // dependent, then naturally the resultant length is also context
142 // dependent, regardless of whether its actual unit is context dependent or
143 // not. Unfortunately normal invalidation mechanisms won't cause us to
144 // recalculate the result of the sandwich if the context changes, so we
145 // take the (substantial) performance hit of preventing caching of the
146 // sandwich layer, causing the animation sandwich to be recalculated every
149 aPreventCachingOfSandwich
= false;
150 for (uint32_t i
= 0; i
< llai
->Length(); ++i
) {
151 uint8_t unit
= (*llai
)[i
].GetUnit();
152 if (unit
== SVGLength_Binding::SVG_LENGTHTYPE_PERCENTAGE
||
153 unit
== SVGLength_Binding::SVG_LENGTHTYPE_EMS
||
154 unit
== SVGLength_Binding::SVG_LENGTHTYPE_EXS
) {
155 aPreventCachingOfSandwich
= true;
163 SMILValue
SVGAnimatedLengthList::SMILAnimatedLengthList::GetBaseValue() const {
164 // To benefit from Return Value Optimization and avoid copy constructor calls
165 // due to our use of return-by-value, we must return the exact same object
166 // from ALL return points. This function must only return THIS variable:
169 SMILValue
tmp(&SVGLengthListSMILType::sSingleton
);
170 SVGLengthListAndInfo
* llai
= static_cast<SVGLengthListAndInfo
*>(tmp
.mU
.mPtr
);
171 nsresult rv
= llai
->CopyFrom(mVal
->mBaseVal
);
172 if (NS_SUCCEEDED(rv
)) {
173 llai
->SetInfo(mElement
, mAxis
, mCanZeroPadList
);
174 val
= std::move(tmp
);
179 nsresult
SVGAnimatedLengthList::SMILAnimatedLengthList::SetAnimValue(
180 const SMILValue
& aValue
) {
181 NS_ASSERTION(aValue
.mType
== &SVGLengthListSMILType::sSingleton
,
182 "Unexpected type to assign animated value");
183 if (aValue
.mType
== &SVGLengthListSMILType::sSingleton
) {
184 mVal
->SetAnimValue(*static_cast<SVGLengthListAndInfo
*>(aValue
.mU
.mPtr
),
185 mElement
, mAttrEnum
);
190 void SVGAnimatedLengthList::SMILAnimatedLengthList::ClearAnimValue() {
191 if (mVal
->mAnimVal
) {
192 mVal
->ClearAnimValue(mElement
, mAttrEnum
);
196 } // namespace mozilla