2 * Copyright (C) 2008 Apple Inc. All rights reserved.
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
7 * 1. Redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer.
9 * 2. Redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the
11 * documentation and/or other materials provided with the distribution.
13 * THIS SOFTWARE IS PROVIDED BY APPLE INC. ``AS IS'' AND ANY
14 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
15 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
16 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR
17 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
18 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
19 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
20 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
21 * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
23 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26 #ifndef SVGSMILElement_h
27 #define SVGSMILElement_h
29 #include "core/SVGNames.h"
30 #include "core/svg/SVGElement.h"
31 #include "core/svg/SVGTests.h"
32 #include "core/svg/animation/SMILTime.h"
33 #include "platform/heap/Heap.h"
34 #include "wtf/HashMap.h"
38 class ConditionEventListener;
39 class SMILTimeContainer;
42 template<typename T> class EventSender;
43 typedef EventSender<SVGSMILElement> SMILEventSender;
45 // This class implements SMIL interval timing model as needed for SVG animation.
46 class SVGSMILElement : public SVGElement, public SVGTests {
48 SVGSMILElement(const QualifiedName&, Document&);
49 virtual ~SVGSMILElement();
51 bool isSupportedAttribute(const QualifiedName&);
52 virtual void parseAttribute(const QualifiedName&, const AtomicString&) override;
53 virtual void svgAttributeChanged(const QualifiedName&) override;
54 virtual InsertionNotificationRequest insertedInto(ContainerNode*) override;
55 virtual void removedFrom(ContainerNode*) override;
57 virtual bool hasValidAttributeType() = 0;
58 virtual bool hasValidAttributeName();
59 virtual void animationAttributeChanged() = 0;
61 SMILTimeContainer* timeContainer() const { return m_timeContainer.get(); }
63 SVGElement* targetElement() const { return m_targetElement; }
64 const QualifiedName& attributeName() const { return m_attributeName; }
66 void beginByLinkActivation();
74 Restart restart() const;
81 FillMode fill() const;
84 SMILTime repeatDur() const;
85 SMILTime repeatCount() const;
86 SMILTime maxValue() const;
87 SMILTime minValue() const;
89 SMILTime elapsed() const;
91 SMILTime intervalBegin() const { return m_interval.begin; }
92 SMILTime previousIntervalBegin() const { return m_previousIntervalBegin; }
93 SMILTime simpleDuration() const;
95 void seekToIntervalCorrespondingToTime(SMILTime elapsed);
96 bool progress(SMILTime elapsed, SVGSMILElement* resultsElement, bool seekToTime);
97 SMILTime nextProgressTime() const;
101 static SMILTime parseClockValue(const String&);
102 static SMILTime parseOffsetValue(const String&);
104 bool isContributing(SMILTime elapsed) const;
105 bool isFrozen() const;
107 unsigned documentOrderIndex() const { return m_documentOrderIndex; }
108 void setDocumentOrderIndex(unsigned index) { m_documentOrderIndex = index; }
110 virtual void resetAnimatedType() = 0;
111 virtual void clearAnimatedType(SVGElement* targetElement) = 0;
112 virtual void applyResultsToTarget() = 0;
114 void connectSyncBaseConditions();
115 void connectEventBaseConditions();
117 void dispatchPendingEvent(SMILEventSender*);
118 void dispatchRepeatEvents(unsigned);
120 virtual bool isSVGDiscardElement() const { return false; }
122 void trace(Visitor*) override;
125 void addBeginTime(SMILTime eventTime, SMILTime endTime, SMILTimeWithOrigin::Origin = SMILTimeWithOrigin::ParserOrigin);
126 void addEndTime(SMILTime eventTime, SMILTime endTime, SMILTimeWithOrigin::Origin = SMILTimeWithOrigin::ParserOrigin);
128 void setInactive() { m_activeState = Inactive; }
130 // Sub-classes may need to take action when the target is changed.
131 virtual void setTargetElement(SVGElement*);
132 virtual void setAttributeName(const QualifiedName&);
135 virtual void buildPendingResource() override;
136 void clearResourceAndEventBaseReferences();
137 void clearConditions();
139 virtual void startedActiveInterval() = 0;
140 void endedActiveInterval();
141 virtual void updateAnimation(float percent, unsigned repeat, SVGSMILElement* resultElement) = 0;
143 virtual bool rendererIsNeeded(const RenderStyle&) override { return false; }
150 SMILTime findInstanceTime(BeginOrEnd, SMILTime minimumTime, bool equalsMinimumOK) const;
152 enum ResolveInterval {
157 SMILInterval resolveInterval(ResolveInterval) const;
158 void resolveFirstInterval();
159 bool resolveNextInterval();
160 SMILTime resolveActiveEnd(SMILTime resolvedBegin, SMILTime resolvedEnd) const;
161 SMILTime repeatingDuration() const;
163 enum RestartedInterval {
164 DidNotRestartInterval,
168 RestartedInterval maybeRestartInterval(SMILTime elapsed);
169 void beginListChanged(SMILTime eventTime);
170 void endListChanged(SMILTime eventTime);
172 // This represents conditions on elements begin or end list that need to be resolved on runtime
173 // for example <animate begin="otherElement.begin + 8s; button.click" ... />
174 class Condition : public NoBaseWillBeGarbageCollectedFinalized<Condition> {
182 Condition(Type, BeginOrEnd, const String& baseID, const String& name, SMILTime offset, int repeat = -1);
183 static PassOwnPtrWillBeRawPtr<Condition> create(Type type, BeginOrEnd beginOrEnd, const String& baseID, const String& name, SMILTime offset, int repeat = -1)
185 return adoptPtrWillBeNoop(new Condition(type, beginOrEnd, baseID, name, offset, repeat));
188 void trace(Visitor*);
190 Type type() const { return m_type; }
191 BeginOrEnd beginOrEnd() const { return m_beginOrEnd; }
192 String baseID() const { return m_baseID; }
193 String name() const { return m_name; }
194 SMILTime offset() const { return m_offset; }
195 int repeat() const { return m_repeat; }
196 SVGSMILElement* syncBase() const { return m_syncBase.get(); }
197 void setSyncBase(SVGSMILElement* element) { m_syncBase = element; }
198 ConditionEventListener* eventListener() const { return m_eventListener.get(); }
199 void setEventListener(PassRefPtr<ConditionEventListener>);
203 BeginOrEnd m_beginOrEnd;
208 RefPtrWillBeMember<SVGSMILElement> m_syncBase;
209 RefPtr<ConditionEventListener> m_eventListener;
211 bool parseCondition(const String&, BeginOrEnd beginOrEnd);
212 void parseBeginOrEnd(const String&, BeginOrEnd beginOrEnd);
213 SVGElement* eventBaseFor(const Condition&);
215 void disconnectSyncBaseConditions();
216 void disconnectEventBaseConditions();
219 void handleConditionEvent(Event*, Condition*);
221 void notifyDependentsIntervalChanged();
222 void createInstanceTimesFromSyncbase(SVGSMILElement* syncbase);
223 void addSyncBaseDependent(SVGSMILElement*);
224 void removeSyncBaseDependent(SVGSMILElement*);
232 QualifiedName m_attributeName;
234 ActiveState determineActiveState(SMILTime elapsed) const;
235 float calculateAnimationPercentAndRepeat(SMILTime elapsed, unsigned& repeat) const;
236 SMILTime calculateNextProgressTime(SMILTime elapsed) const;
238 RawPtrWillBeMember<SVGElement> m_targetElement;
240 WillBeHeapVector<OwnPtrWillBeMember<Condition> > m_conditions;
241 bool m_syncBaseConditionsConnected;
242 bool m_hasEndEventConditions;
244 bool m_isWaitingForFirstInterval;
246 typedef WillBeHeapHashSet<RawPtrWillBeMember<SVGSMILElement> > TimeDependentSet;
247 TimeDependentSet m_syncBaseDependents;
249 // Instance time lists
250 Vector<SMILTimeWithOrigin> m_beginTimes;
251 Vector<SMILTimeWithOrigin> m_endTimes;
253 // This is the upcoming or current interval
254 SMILInterval m_interval;
256 SMILTime m_previousIntervalBegin;
258 ActiveState m_activeState;
260 unsigned m_lastRepeat;
262 SMILTime m_nextProgressTime;
264 RefPtrWillBeMember<SMILTimeContainer> m_timeContainer;
265 unsigned m_documentOrderIndex;
267 Vector<unsigned> m_repeatEventCountList;
269 mutable SMILTime m_cachedDur;
270 mutable SMILTime m_cachedRepeatDur;
271 mutable SMILTime m_cachedRepeatCount;
272 mutable SMILTime m_cachedMin;
273 mutable SMILTime m_cachedMax;
275 friend class ConditionEventListener;
278 inline bool isSVGSMILElement(const SVGElement& element)
280 return element.hasTagName(SVGNames::setTag) || element.hasTagName(SVGNames::animateTag) || element.hasTagName(SVGNames::animateMotionTag)
281 || element.hasTagName(SVGNames::animateTransformTag) || element.hasTagName((SVGNames::discardTag));
284 DEFINE_SVGELEMENT_TYPE_CASTS_WITH_FUNCTION(SVGSMILElement);
288 #endif // SVGSMILElement_h