PageRenderTime 40ms CodeModel.GetById 12ms RepoModel.GetById 1ms app.codeStats 0ms

/webkit-1.8.1/Source/WebCore/svg/animation/SMILTimeContainer.cpp

#
C++ | 300 lines | 212 code | 49 blank | 39 comment | 38 complexity | 0c574828d3cab5736367b5cc8096191d MD5 | raw file
Possible License(s): BSD-3-Clause, LGPL-2.1, LGPL-2.0
  1. /*
  2. * Copyright (C) 2008 Apple Inc. All rights reserved.
  3. *
  4. * Redistribution and use in source and binary forms, with or without
  5. * modification, are permitted provided that the following conditions
  6. * are met:
  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.
  12. *
  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.
  24. */
  25. #include "config.h"
  26. #include "SMILTimeContainer.h"
  27. #if ENABLE(SVG)
  28. #include "CSSComputedStyleDeclaration.h"
  29. #include "CSSParser.h"
  30. #include "Document.h"
  31. #include "SVGAnimationElement.h"
  32. #include "SVGNames.h"
  33. #include "SVGSMILElement.h"
  34. #include "SVGSVGElement.h"
  35. #include <wtf/CurrentTime.h>
  36. using namespace std;
  37. namespace WebCore {
  38. static const double animationFrameDelay = 0.025;
  39. SMILTimeContainer::SMILTimeContainer(SVGSVGElement* owner)
  40. : m_beginTime(0)
  41. , m_pauseTime(0)
  42. , m_accumulatedPauseTime(0)
  43. , m_presetStartTime(0)
  44. , m_documentOrderIndexesDirty(false)
  45. , m_timer(this, &SMILTimeContainer::timerFired)
  46. , m_ownerSVGElement(owner)
  47. {
  48. }
  49. void SMILTimeContainer::schedule(SVGSMILElement* animation)
  50. {
  51. ASSERT(animation->timeContainer() == this);
  52. SMILTime nextFireTime = animation->nextProgressTime();
  53. if (!nextFireTime.isFinite())
  54. return;
  55. m_scheduledAnimations.add(animation);
  56. startTimer(0);
  57. }
  58. void SMILTimeContainer::unschedule(SVGSMILElement* animation)
  59. {
  60. ASSERT(animation->timeContainer() == this);
  61. m_scheduledAnimations.remove(animation);
  62. }
  63. SMILTime SMILTimeContainer::elapsed() const
  64. {
  65. if (!m_beginTime)
  66. return 0;
  67. return currentTime() - m_beginTime - m_accumulatedPauseTime;
  68. }
  69. bool SMILTimeContainer::isActive() const
  70. {
  71. return m_beginTime && !isPaused();
  72. }
  73. bool SMILTimeContainer::isPaused() const
  74. {
  75. return m_pauseTime;
  76. }
  77. void SMILTimeContainer::begin()
  78. {
  79. ASSERT(!m_beginTime);
  80. double now = currentTime();
  81. m_beginTime = now - m_presetStartTime;
  82. updateAnimations(SMILTime(m_presetStartTime));
  83. m_presetStartTime = 0;
  84. if (m_pauseTime) {
  85. m_pauseTime = now;
  86. m_timer.stop();
  87. }
  88. }
  89. void SMILTimeContainer::pause()
  90. {
  91. ASSERT(!isPaused());
  92. m_pauseTime = currentTime();
  93. if (m_beginTime)
  94. m_timer.stop();
  95. }
  96. void SMILTimeContainer::resume()
  97. {
  98. if (!m_beginTime)
  99. return;
  100. ASSERT(isPaused());
  101. m_accumulatedPauseTime += currentTime() - m_pauseTime;
  102. m_pauseTime = 0;
  103. startTimer(0);
  104. }
  105. void SMILTimeContainer::setElapsed(SMILTime time)
  106. {
  107. // If the documment didn't begin yet, record a new start time, we'll seek to once its possible.
  108. if (!m_beginTime) {
  109. m_presetStartTime = time.value();
  110. return;
  111. }
  112. m_beginTime = currentTime() - time.value();
  113. m_accumulatedPauseTime = 0;
  114. Vector<SVGSMILElement*> toReset;
  115. copyToVector(m_scheduledAnimations, toReset);
  116. for (unsigned n = 0; n < toReset.size(); ++n)
  117. toReset[n]->reset();
  118. updateAnimations(time);
  119. }
  120. void SMILTimeContainer::startTimer(SMILTime fireTime, SMILTime minimumDelay)
  121. {
  122. if (!m_beginTime || isPaused())
  123. return;
  124. if (!fireTime.isFinite())
  125. return;
  126. SMILTime delay = max(fireTime - elapsed(), minimumDelay);
  127. m_timer.startOneShot(delay.value());
  128. }
  129. void SMILTimeContainer::timerFired(Timer<SMILTimeContainer>*)
  130. {
  131. ASSERT(m_beginTime);
  132. ASSERT(!m_pauseTime);
  133. updateAnimations(elapsed());
  134. }
  135. void SMILTimeContainer::updateDocumentOrderIndexes()
  136. {
  137. unsigned timingElementCount = 0;
  138. for (Node* node = m_ownerSVGElement; node; node = node->traverseNextNode(m_ownerSVGElement)) {
  139. if (SVGSMILElement::isSMILElement(node))
  140. static_cast<SVGSMILElement*>(node)->setDocumentOrderIndex(timingElementCount++);
  141. }
  142. m_documentOrderIndexesDirty = false;
  143. }
  144. struct PriorityCompare {
  145. PriorityCompare(SMILTime elapsed) : m_elapsed(elapsed) {}
  146. bool operator()(SVGSMILElement* a, SVGSMILElement* b)
  147. {
  148. // FIXME: This should also consider possible timing relations between the elements.
  149. SMILTime aBegin = a->intervalBegin();
  150. SMILTime bBegin = b->intervalBegin();
  151. // Frozen elements need to be prioritized based on their previous interval.
  152. aBegin = a->isFrozen() && m_elapsed < aBegin ? a->previousIntervalBegin() : aBegin;
  153. bBegin = b->isFrozen() && m_elapsed < bBegin ? b->previousIntervalBegin() : bBegin;
  154. if (aBegin == bBegin)
  155. return a->documentOrderIndex() < b->documentOrderIndex();
  156. return aBegin < bBegin;
  157. }
  158. SMILTime m_elapsed;
  159. };
  160. void SMILTimeContainer::sortByPriority(Vector<SVGSMILElement*>& smilElements, SMILTime elapsed)
  161. {
  162. if (m_documentOrderIndexesDirty)
  163. updateDocumentOrderIndexes();
  164. std::sort(smilElements.begin(), smilElements.end(), PriorityCompare(elapsed));
  165. }
  166. static bool applyOrderSortFunction(SVGSMILElement* a, SVGSMILElement* b)
  167. {
  168. if (!a->hasTagName(SVGNames::animateTransformTag) && b->hasTagName(SVGNames::animateTransformTag))
  169. return true;
  170. return false;
  171. }
  172. static void sortByApplyOrder(Vector<SVGSMILElement*>& smilElements)
  173. {
  174. std::sort(smilElements.begin(), smilElements.end(), applyOrderSortFunction);
  175. }
  176. String SMILTimeContainer::baseValueFor(ElementAttributePair key)
  177. {
  178. // FIXME: We wouldn't need to do this if we were keeping base values around properly in DOM.
  179. // Currently animation overwrites them so we need to save them somewhere.
  180. BaseValueMap::iterator it = m_savedBaseValues.find(key);
  181. if (it != m_savedBaseValues.end())
  182. return it->second;
  183. SVGElement* targetElement = key.first;
  184. QualifiedName attributeName = key.second;
  185. ASSERT(targetElement);
  186. ASSERT(attributeName != anyQName());
  187. String baseValue;
  188. if (SVGAnimationElement::isTargetAttributeCSSProperty(targetElement, attributeName))
  189. baseValue = CSSComputedStyleDeclaration::create(targetElement)->getPropertyValue(cssPropertyID(attributeName.localName()));
  190. else
  191. baseValue = targetElement->getAttribute(attributeName);
  192. m_savedBaseValues.add(key, baseValue);
  193. return baseValue;
  194. }
  195. void SMILTimeContainer::updateAnimations(SMILTime elapsed)
  196. {
  197. SMILTime earliersFireTime = SMILTime::unresolved();
  198. Vector<SVGSMILElement*> toAnimate;
  199. copyToVector(m_scheduledAnimations, toAnimate);
  200. // Sort according to priority. Elements with later begin time have higher priority.
  201. // In case of a tie, document order decides.
  202. // FIXME: This should also consider timing relationships between the elements. Dependents
  203. // have higher priority.
  204. sortByPriority(toAnimate, elapsed);
  205. // Calculate animation contributions.
  206. typedef HashMap<ElementAttributePair, RefPtr<SVGSMILElement> > ResultElementMap;
  207. ResultElementMap resultsElements;
  208. for (unsigned n = 0; n < toAnimate.size(); ++n) {
  209. SVGSMILElement* animation = toAnimate[n];
  210. ASSERT(animation->timeContainer() == this);
  211. SVGElement* targetElement = animation->targetElement();
  212. if (!targetElement)
  213. continue;
  214. QualifiedName attributeName = animation->attributeName();
  215. if (attributeName == anyQName()) {
  216. if (animation->hasTagName(SVGNames::animateMotionTag))
  217. attributeName = SVGNames::animateMotionTag;
  218. else
  219. continue;
  220. }
  221. // Results are accumulated to the first animation that animates a particular element/attribute pair.
  222. ElementAttributePair key(targetElement, attributeName);
  223. SVGSMILElement* resultElement = resultsElements.get(key).get();
  224. if (!resultElement) {
  225. if (!animation->hasValidAttributeType())
  226. continue;
  227. resultElement = animation;
  228. resultElement->resetToBaseValue(baseValueFor(key));
  229. resultsElements.add(key, resultElement);
  230. }
  231. // This will calculate the contribution from the animation and add it to the resultsElement.
  232. animation->progress(elapsed, resultElement);
  233. SMILTime nextFireTime = animation->nextProgressTime();
  234. if (nextFireTime.isFinite())
  235. earliersFireTime = min(nextFireTime, earliersFireTime);
  236. }
  237. Vector<SVGSMILElement*> animationsToApply;
  238. ResultElementMap::iterator end = resultsElements.end();
  239. for (ResultElementMap::iterator it = resultsElements.begin(); it != end; ++it)
  240. animationsToApply.append(it->second.get());
  241. // Sort <animateTranform> to be the last one to be applied. <animate> may change transform attribute as
  242. // well (directly or indirectly by modifying <use> x/y) and this way transforms combine properly.
  243. sortByApplyOrder(animationsToApply);
  244. // Apply results to target elements.
  245. for (unsigned n = 0; n < animationsToApply.size(); ++n)
  246. animationsToApply[n]->applyResultsToTarget();
  247. startTimer(earliersFireTime, animationFrameDelay);
  248. Document::updateStyleForAllDocuments();
  249. }
  250. }
  251. #endif // ENABLE(SVG)