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/. */
6
7 #include "SVGAnimatedLengthList.h"
8
9 #include "DOMSVGAnimatedLengthList.h"
10 #include "mozilla/Move.h"
11 #include "nsSVGElement.h"
12 #include "nsSVGAttrTearoffTable.h"
13 #include "nsSMILValue.h"
14 #include "SVGLengthListSMILType.h"
15
16 namespace mozilla {
17
18 nsresult
SetBaseValueString(const nsAString & aValue)19 SVGAnimatedLengthList::SetBaseValueString(const nsAString& aValue)
20 {
21 SVGLengthList newBaseValue;
22 nsresult rv = newBaseValue.SetValueFromString(aValue);
23 if (NS_FAILED(rv)) {
24 return rv;
25 }
26
27 DOMSVGAnimatedLengthList *domWrapper =
28 DOMSVGAnimatedLengthList::GetDOMWrapperIfExists(this);
29 if (domWrapper) {
30 // We must send this notification *before* changing mBaseVal! If the length
31 // of our baseVal is being reduced, our baseVal's DOM wrapper list may have
32 // to remove DOM items from itself, and any removed DOM items need to copy
33 // their internal counterpart values *before* we change them.
34 //
35 domWrapper->InternalBaseValListWillChangeTo(newBaseValue);
36 }
37
38 // We don't need to call DidChange* here - we're only called by
39 // nsSVGElement::ParseAttribute under Element::SetAttr,
40 // which takes care of notifying.
41
42 rv = mBaseVal.CopyFrom(newBaseValue);
43 if (NS_FAILED(rv) && domWrapper) {
44 // Attempting to increase mBaseVal's length failed - reduce domWrapper
45 // back to the same length:
46 domWrapper->InternalBaseValListWillChangeTo(mBaseVal);
47 }
48 return rv;
49 }
50
51 void
ClearBaseValue(uint32_t aAttrEnum)52 SVGAnimatedLengthList::ClearBaseValue(uint32_t aAttrEnum)
53 {
54 DOMSVGAnimatedLengthList *domWrapper =
55 DOMSVGAnimatedLengthList::GetDOMWrapperIfExists(this);
56 if (domWrapper) {
57 // We must send this notification *before* changing mBaseVal! (See above.)
58 domWrapper->InternalBaseValListWillChangeTo(SVGLengthList());
59 }
60 mBaseVal.Clear();
61 // Caller notifies
62 }
63
64 nsresult
SetAnimValue(const SVGLengthList & aNewAnimValue,nsSVGElement * aElement,uint32_t aAttrEnum)65 SVGAnimatedLengthList::SetAnimValue(const SVGLengthList& aNewAnimValue,
66 nsSVGElement *aElement,
67 uint32_t aAttrEnum)
68 {
69 DOMSVGAnimatedLengthList *domWrapper =
70 DOMSVGAnimatedLengthList::GetDOMWrapperIfExists(this);
71 if (domWrapper) {
72 // A new animation may totally change the number of items in the animVal
73 // list, replacing what was essentially a mirror of the baseVal list, or
74 // else replacing and overriding an existing animation. When this happens
75 // we must try and keep our animVal's DOM wrapper in sync (see the comment
76 // in DOMSVGAnimatedLengthList::InternalBaseValListWillChangeTo).
77 //
78 // It's not possible for us to reliably distinguish between calls to this
79 // method that are setting a new sample for an existing animation, and
80 // calls that are setting the first sample of an animation that will
81 // override an existing animation. Happily it's cheap to just blindly
82 // notify our animVal's DOM wrapper of its internal counterpart's new value
83 // each time this method is called, so that's what we do.
84 //
85 // Note that we must send this notification *before* setting or changing
86 // mAnimVal! (See the comment in SetBaseValueString above.)
87 //
88 domWrapper->InternalAnimValListWillChangeTo(aNewAnimValue);
89 }
90 if (!mAnimVal) {
91 mAnimVal = new SVGLengthList();
92 }
93 nsresult rv = mAnimVal->CopyFrom(aNewAnimValue);
94 if (NS_FAILED(rv)) {
95 // OOM. We clear the animation, and, importantly, ClearAnimValue() ensures
96 // that mAnimVal and its DOM wrapper (if any) will have the same length!
97 ClearAnimValue(aElement, aAttrEnum);
98 return rv;
99 }
100 aElement->DidAnimateLengthList(aAttrEnum);
101 return NS_OK;
102 }
103
104 void
ClearAnimValue(nsSVGElement * aElement,uint32_t aAttrEnum)105 SVGAnimatedLengthList::ClearAnimValue(nsSVGElement *aElement,
106 uint32_t aAttrEnum)
107 {
108 DOMSVGAnimatedLengthList *domWrapper =
109 DOMSVGAnimatedLengthList::GetDOMWrapperIfExists(this);
110 if (domWrapper) {
111 // When all animation ends, animVal simply mirrors baseVal, which may have
112 // a different number of items to the last active animated value. We must
113 // keep the length of our animVal's DOM wrapper list in sync, and again we
114 // must do that before touching mAnimVal. See comments above.
115 //
116 domWrapper->InternalAnimValListWillChangeTo(mBaseVal);
117 }
118 mAnimVal = nullptr;
119 aElement->DidAnimateLengthList(aAttrEnum);
120 }
121
122 nsISMILAttr*
ToSMILAttr(nsSVGElement * aSVGElement,uint8_t aAttrEnum,uint8_t aAxis,bool aCanZeroPadList)123 SVGAnimatedLengthList::ToSMILAttr(nsSVGElement *aSVGElement,
124 uint8_t aAttrEnum,
125 uint8_t aAxis,
126 bool aCanZeroPadList)
127 {
128 return new SMILAnimatedLengthList(this, aSVGElement, aAttrEnum, aAxis, aCanZeroPadList);
129 }
130
131 nsresult
132 SVGAnimatedLengthList::
ValueFromString(const nsAString & aStr,const dom::SVGAnimationElement *,nsSMILValue & aValue,bool & aPreventCachingOfSandwich) const133 SMILAnimatedLengthList::ValueFromString(const nsAString& aStr,
134 const dom::SVGAnimationElement* /*aSrcElement*/,
135 nsSMILValue& aValue,
136 bool& aPreventCachingOfSandwich) const
137 {
138 nsSMILValue val(&SVGLengthListSMILType::sSingleton);
139 SVGLengthListAndInfo *llai = static_cast<SVGLengthListAndInfo*>(val.mU.mPtr);
140 nsresult rv = llai->SetValueFromString(aStr);
141 if (NS_SUCCEEDED(rv)) {
142 llai->SetInfo(mElement, mAxis, mCanZeroPadList);
143 aValue = Move(val);
144
145 // If any of the lengths in the list depend on their context, then we must
146 // prevent caching of the entire animation sandwich. This is because the
147 // units of a length at a given index can change from sandwich layer to
148 // layer, and indeed even be different within a single sandwich layer. If
149 // any length in the result of an animation sandwich is the result of the
150 // addition of lengths where one or more of those lengths is context
151 // dependent, then naturally the resultant length is also context
152 // dependent, regardless of whether its actual unit is context dependent or
153 // not. Unfortunately normal invalidation mechanisms won't cause us to
154 // recalculate the result of the sandwich if the context changes, so we
155 // take the (substantial) performance hit of preventing caching of the
156 // sandwich layer, causing the animation sandwich to be recalculated every
157 // single sample.
158
159 aPreventCachingOfSandwich = false;
160 for (uint32_t i = 0; i < llai->Length(); ++i) {
161 uint8_t unit = (*llai)[i].GetUnit();
162 if (unit == nsIDOMSVGLength::SVG_LENGTHTYPE_PERCENTAGE ||
163 unit == nsIDOMSVGLength::SVG_LENGTHTYPE_EMS ||
164 unit == nsIDOMSVGLength::SVG_LENGTHTYPE_EXS) {
165 aPreventCachingOfSandwich = true;
166 break;
167 }
168 }
169 }
170 return rv;
171 }
172
173 nsSMILValue
GetBaseValue() const174 SVGAnimatedLengthList::SMILAnimatedLengthList::GetBaseValue() const
175 {
176 // To benefit from Return Value Optimization and avoid copy constructor calls
177 // due to our use of return-by-value, we must return the exact same object
178 // from ALL return points. This function must only return THIS variable:
179 nsSMILValue val;
180
181 nsSMILValue tmp(&SVGLengthListSMILType::sSingleton);
182 SVGLengthListAndInfo *llai = static_cast<SVGLengthListAndInfo*>(tmp.mU.mPtr);
183 nsresult rv = llai->CopyFrom(mVal->mBaseVal);
184 if (NS_SUCCEEDED(rv)) {
185 llai->SetInfo(mElement, mAxis, mCanZeroPadList);
186 val = Move(tmp);
187 }
188 return val;
189 }
190
191 nsresult
SetAnimValue(const nsSMILValue & aValue)192 SVGAnimatedLengthList::SMILAnimatedLengthList::SetAnimValue(const nsSMILValue& aValue)
193 {
194 NS_ASSERTION(aValue.mType == &SVGLengthListSMILType::sSingleton,
195 "Unexpected type to assign animated value");
196 if (aValue.mType == &SVGLengthListSMILType::sSingleton) {
197 mVal->SetAnimValue(*static_cast<SVGLengthListAndInfo*>(aValue.mU.mPtr),
198 mElement,
199 mAttrEnum);
200 }
201 return NS_OK;
202 }
203
204 void
ClearAnimValue()205 SVGAnimatedLengthList::SMILAnimatedLengthList::ClearAnimValue()
206 {
207 if (mVal->mAnimVal) {
208 mVal->ClearAnimValue(mElement, mAttrEnum);
209 }
210 }
211
212 } // namespace mozilla
213