PageRenderTime 26ms CodeModel.GetById 16ms RepoModel.GetById 0ms app.codeStats 0ms

/src/qt/qtwebkit/Source/WebCore/svg/animation/SMILTimeContainer.cpp

https://gitlab.com/x33n/phantomjs
C++ | 327 lines | 232 code | 55 blank | 40 comment | 39 complexity | 385cfd46c7b187a859fc46afc046e84f MD5 | raw file
  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 "Document.h"
  29. #include "NodeTraversal.h"
  30. #include "SVGNames.h"
  31. #include "SVGSMILElement.h"
  32. #include "SVGSVGElement.h"
  33. #include <wtf/CurrentTime.h>
  34. using namespace std;
  35. namespace WebCore {
  36. static const double animationFrameDelay = 0.025;
  37. SMILTimeContainer::SMILTimeContainer(SVGSVGElement* owner)
  38. : m_beginTime(0)
  39. , m_pauseTime(0)
  40. , m_accumulatedPauseTime(0)
  41. , m_presetStartTime(0)
  42. , m_documentOrderIndexesDirty(false)
  43. , m_timer(this, &SMILTimeContainer::timerFired)
  44. , m_ownerSVGElement(owner)
  45. #ifndef NDEBUG
  46. , m_preventScheduledAnimationsChanges(false)
  47. #endif
  48. {
  49. }
  50. SMILTimeContainer::~SMILTimeContainer()
  51. {
  52. #ifndef NDEBUG
  53. ASSERT(!m_preventScheduledAnimationsChanges);
  54. #endif
  55. }
  56. void SMILTimeContainer::schedule(SVGSMILElement* animation, SVGElement* target, const QualifiedName& attributeName)
  57. {
  58. ASSERT(animation->timeContainer() == this);
  59. ASSERT(target);
  60. ASSERT(animation->hasValidAttributeName());
  61. #ifndef NDEBUG
  62. ASSERT(!m_preventScheduledAnimationsChanges);
  63. #endif
  64. ElementAttributePair key(target, attributeName);
  65. OwnPtr<AnimationsVector>& scheduled = m_scheduledAnimations.add(key, nullptr).iterator->value;
  66. if (!scheduled)
  67. scheduled = adoptPtr(new AnimationsVector);
  68. ASSERT(!scheduled->contains(animation));
  69. scheduled->append(animation);
  70. SMILTime nextFireTime = animation->nextProgressTime();
  71. if (nextFireTime.isFinite())
  72. notifyIntervalsChanged();
  73. }
  74. void SMILTimeContainer::unschedule(SVGSMILElement* animation, SVGElement* target, const QualifiedName& attributeName)
  75. {
  76. ASSERT(animation->timeContainer() == this);
  77. #ifndef NDEBUG
  78. ASSERT(!m_preventScheduledAnimationsChanges);
  79. #endif
  80. ElementAttributePair key(target, attributeName);
  81. AnimationsVector* scheduled = m_scheduledAnimations.get(key);
  82. ASSERT(scheduled);
  83. size_t idx = scheduled->find(animation);
  84. ASSERT(idx != notFound);
  85. scheduled->remove(idx);
  86. }
  87. void SMILTimeContainer::notifyIntervalsChanged()
  88. {
  89. // Schedule updateAnimations() to be called asynchronously so multiple intervals
  90. // can change with updateAnimations() only called once at the end.
  91. startTimer(0);
  92. }
  93. SMILTime SMILTimeContainer::elapsed() const
  94. {
  95. if (!m_beginTime)
  96. return 0;
  97. return currentTime() - m_beginTime - m_accumulatedPauseTime;
  98. }
  99. bool SMILTimeContainer::isActive() const
  100. {
  101. return m_beginTime && !isPaused();
  102. }
  103. bool SMILTimeContainer::isPaused() const
  104. {
  105. return m_pauseTime;
  106. }
  107. bool SMILTimeContainer::isStarted() const
  108. {
  109. return m_beginTime;
  110. }
  111. void SMILTimeContainer::begin()
  112. {
  113. ASSERT(!m_beginTime);
  114. double now = currentTime();
  115. // If 'm_presetStartTime' is set, the timeline was modified via setElapsed() before the document began.
  116. // In this case pass on 'seekToTime=true' to updateAnimations().
  117. m_beginTime = now - m_presetStartTime;
  118. updateAnimations(SMILTime(m_presetStartTime), m_presetStartTime ? true : false);
  119. m_presetStartTime = 0;
  120. if (m_pauseTime) {
  121. m_pauseTime = now;
  122. m_timer.stop();
  123. }
  124. }
  125. void SMILTimeContainer::pause()
  126. {
  127. ASSERT(!isPaused());
  128. m_pauseTime = currentTime();
  129. if (m_beginTime)
  130. m_timer.stop();
  131. }
  132. void SMILTimeContainer::resume()
  133. {
  134. ASSERT(isPaused());
  135. if (m_beginTime)
  136. m_accumulatedPauseTime += currentTime() - m_pauseTime;
  137. m_pauseTime = 0;
  138. startTimer(0);
  139. }
  140. void SMILTimeContainer::setElapsed(SMILTime time)
  141. {
  142. // If the documment didn't begin yet, record a new start time, we'll seek to once its possible.
  143. if (!m_beginTime) {
  144. m_presetStartTime = time.value();
  145. return;
  146. }
  147. if (m_beginTime)
  148. m_timer.stop();
  149. double now = currentTime();
  150. m_beginTime = now - time.value();
  151. m_accumulatedPauseTime = 0;
  152. if (m_pauseTime)
  153. m_pauseTime = now;
  154. #ifndef NDEBUG
  155. m_preventScheduledAnimationsChanges = true;
  156. #endif
  157. GroupedAnimationsMap::iterator end = m_scheduledAnimations.end();
  158. for (GroupedAnimationsMap::iterator it = m_scheduledAnimations.begin(); it != end; ++it) {
  159. AnimationsVector* scheduled = it->value.get();
  160. unsigned size = scheduled->size();
  161. for (unsigned n = 0; n < size; n++)
  162. scheduled->at(n)->reset();
  163. }
  164. #ifndef NDEBUG
  165. m_preventScheduledAnimationsChanges = false;
  166. #endif
  167. updateAnimations(time, true);
  168. }
  169. void SMILTimeContainer::startTimer(SMILTime fireTime, SMILTime minimumDelay)
  170. {
  171. if (!m_beginTime || isPaused())
  172. return;
  173. if (!fireTime.isFinite())
  174. return;
  175. SMILTime delay = max(fireTime - elapsed(), minimumDelay);
  176. m_timer.startOneShot(delay.value());
  177. }
  178. void SMILTimeContainer::timerFired(Timer<SMILTimeContainer>*)
  179. {
  180. ASSERT(m_beginTime);
  181. ASSERT(!m_pauseTime);
  182. updateAnimations(elapsed());
  183. }
  184. void SMILTimeContainer::updateDocumentOrderIndexes()
  185. {
  186. unsigned timingElementCount = 0;
  187. for (Element* element = m_ownerSVGElement; element; element = ElementTraversal::next(element, m_ownerSVGElement)) {
  188. if (SVGSMILElement::isSMILElement(element))
  189. static_cast<SVGSMILElement*>(element)->setDocumentOrderIndex(timingElementCount++);
  190. }
  191. m_documentOrderIndexesDirty = false;
  192. }
  193. struct PriorityCompare {
  194. PriorityCompare(SMILTime elapsed) : m_elapsed(elapsed) {}
  195. bool operator()(SVGSMILElement* a, SVGSMILElement* b)
  196. {
  197. // FIXME: This should also consider possible timing relations between the elements.
  198. SMILTime aBegin = a->intervalBegin();
  199. SMILTime bBegin = b->intervalBegin();
  200. // Frozen elements need to be prioritized based on their previous interval.
  201. aBegin = a->isFrozen() && m_elapsed < aBegin ? a->previousIntervalBegin() : aBegin;
  202. bBegin = b->isFrozen() && m_elapsed < bBegin ? b->previousIntervalBegin() : bBegin;
  203. if (aBegin == bBegin)
  204. return a->documentOrderIndex() < b->documentOrderIndex();
  205. return aBegin < bBegin;
  206. }
  207. SMILTime m_elapsed;
  208. };
  209. void SMILTimeContainer::sortByPriority(Vector<SVGSMILElement*>& smilElements, SMILTime elapsed)
  210. {
  211. if (m_documentOrderIndexesDirty)
  212. updateDocumentOrderIndexes();
  213. std::sort(smilElements.begin(), smilElements.end(), PriorityCompare(elapsed));
  214. }
  215. void SMILTimeContainer::updateAnimations(SMILTime elapsed, bool seekToTime)
  216. {
  217. SMILTime earliestFireTime = SMILTime::unresolved();
  218. #ifndef NDEBUG
  219. // This boolean will catch any attempts to schedule/unschedule scheduledAnimations during this critical section.
  220. // Similarly, any elements removed will unschedule themselves, so this will catch modification of animationsToApply.
  221. m_preventScheduledAnimationsChanges = true;
  222. #endif
  223. AnimationsVector animationsToApply;
  224. GroupedAnimationsMap::iterator end = m_scheduledAnimations.end();
  225. for (GroupedAnimationsMap::iterator it = m_scheduledAnimations.begin(); it != end; ++it) {
  226. AnimationsVector* scheduled = it->value.get();
  227. // Sort according to priority. Elements with later begin time have higher priority.
  228. // In case of a tie, document order decides.
  229. // FIXME: This should also consider timing relationships between the elements. Dependents
  230. // have higher priority.
  231. sortByPriority(*scheduled, elapsed);
  232. SVGSMILElement* resultElement = 0;
  233. unsigned size = scheduled->size();
  234. for (unsigned n = 0; n < size; n++) {
  235. SVGSMILElement* animation = scheduled->at(n);
  236. ASSERT(animation->timeContainer() == this);
  237. ASSERT(animation->targetElement());
  238. ASSERT(animation->hasValidAttributeName());
  239. // Results are accumulated to the first animation that animates and contributes to a particular element/attribute pair.
  240. if (!resultElement) {
  241. if (!animation->hasValidAttributeType())
  242. continue;
  243. resultElement = animation;
  244. }
  245. // This will calculate the contribution from the animation and add it to the resultsElement.
  246. if (!animation->progress(elapsed, resultElement, seekToTime) && resultElement == animation)
  247. resultElement = 0;
  248. SMILTime nextFireTime = animation->nextProgressTime();
  249. if (nextFireTime.isFinite())
  250. earliestFireTime = min(nextFireTime, earliestFireTime);
  251. }
  252. if (resultElement)
  253. animationsToApply.append(resultElement);
  254. }
  255. unsigned animationsToApplySize = animationsToApply.size();
  256. if (!animationsToApplySize) {
  257. #ifndef NDEBUG
  258. m_preventScheduledAnimationsChanges = false;
  259. #endif
  260. startTimer(earliestFireTime, animationFrameDelay);
  261. return;
  262. }
  263. // Apply results to target elements.
  264. for (unsigned i = 0; i < animationsToApplySize; ++i)
  265. animationsToApply[i]->applyResultsToTarget();
  266. #ifndef NDEBUG
  267. m_preventScheduledAnimationsChanges = false;
  268. #endif
  269. startTimer(earliestFireTime, animationFrameDelay);
  270. }
  271. }
  272. #endif // ENABLE(SVG)