1 /* 2 * Copyright (C) 2013 Google 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 are 6 * met: 7 * 8 * * Redistributions of source code must retain the above copyright 9 * notice, this list of conditions and the following disclaimer. 10 * * Redistributions in binary form must reproduce the above 11 * copyright notice, this list of conditions and the following disclaimer 12 * in the documentation and/or other materials provided with the 13 * distribution. 14 * * Neither the name of Google Inc. nor the names of its 15 * contributors may be used to endorse or promote products derived from 16 * this software without specific prior written permission. 17 * 18 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 19 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 20 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 21 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 22 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 23 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 24 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 25 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 26 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 27 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 28 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 29 */ 30 31 #include "config.h" 32 #include "core/animation/DocumentTimeline.h" 33 34 #include "core/animation/ActiveAnimations.h" 35 #include "core/animation/AnimationClock.h" 36 #include "core/dom/Document.h" 37 #include "core/frame/FrameView.h" 38 39 namespace WebCore { 40 41 // This value represents 1 frame at 30Hz plus a little bit of wiggle room. 42 // TODO: Plumb a nominal framerate through and derive this value from that. 43 const double DocumentTimeline::s_minimumDelay = 0.04; 44 45 46 PassRefPtr<DocumentTimeline> DocumentTimeline::create(Document* document, PassOwnPtr<PlatformTiming> timing) 47 { 48 return adoptRef(new DocumentTimeline(document, timing)); 49 } 50 51 DocumentTimeline::DocumentTimeline(Document* document, PassOwnPtr<PlatformTiming> timing) 52 : m_zeroTime(nullValue()) 53 , m_document(document) 54 , m_eventDistpachTimer(this, &DocumentTimeline::eventDispatchTimerFired) 55 { 56 if (!timing) 57 m_timing = adoptPtr(new DocumentTimelineTiming(this)); 58 else 59 m_timing = timing; 60 61 ASSERT(document); 62 } 63 64 Player* DocumentTimeline::createPlayer(TimedItem* child) 65 { 66 RefPtr<Player> player = Player::create(*this, child); 67 Player* result = player.get(); 68 m_players.append(player.release()); 69 if (m_document->view()) 70 m_timing->serviceOnNextFrame(); 71 return result; 72 } 73 74 Player* DocumentTimeline::play(TimedItem* child) 75 { 76 Player* player = createPlayer(child); 77 player->setStartTime(currentTime()); 78 return player; 79 } 80 81 void DocumentTimeline::wake() 82 { 83 m_timing->serviceOnNextFrame(); 84 } 85 86 bool DocumentTimeline::serviceAnimations() 87 { 88 TRACE_EVENT0("webkit", "DocumentTimeline::serviceAnimations"); 89 90 m_timing->cancelWake(); 91 92 double timeToNextEffect = std::numeric_limits<double>::infinity(); 93 bool didTriggerStyleRecalc = false; 94 for (int i = m_players.size() - 1; i >= 0; --i) { 95 double playerNextEffect; 96 bool playerDidTriggerStyleRecalc; 97 if (!m_players[i]->update(&playerNextEffect, &playerDidTriggerStyleRecalc)) 98 m_players.remove(i); 99 didTriggerStyleRecalc |= playerDidTriggerStyleRecalc; 100 if (playerNextEffect < timeToNextEffect) 101 timeToNextEffect = playerNextEffect; 102 } 103 104 if (!m_players.isEmpty()) { 105 if (timeToNextEffect < s_minimumDelay) 106 m_timing->serviceOnNextFrame(); 107 else if (timeToNextEffect != std::numeric_limits<double>::infinity()) 108 m_timing->wakeAfter(timeToNextEffect - s_minimumDelay); 109 } 110 111 return didTriggerStyleRecalc; 112 } 113 114 void DocumentTimeline::setZeroTime(double zeroTime) 115 { 116 ASSERT(isNull(m_zeroTime)); 117 m_zeroTime = zeroTime; 118 ASSERT(!isNull(m_zeroTime)); 119 } 120 121 void DocumentTimeline::DocumentTimelineTiming::wakeAfter(double duration) 122 { 123 m_timer.startOneShot(duration); 124 } 125 126 void DocumentTimeline::DocumentTimelineTiming::cancelWake() 127 { 128 m_timer.stop(); 129 } 130 131 void DocumentTimeline::DocumentTimelineTiming::serviceOnNextFrame() 132 { 133 if (m_timeline->m_document->view()) 134 m_timeline->m_document->view()->scheduleAnimation(); 135 } 136 137 double DocumentTimeline::currentTime() 138 { 139 return m_document->animationClock().currentTime() - m_zeroTime; 140 } 141 142 void DocumentTimeline::pauseAnimationsForTesting(double pauseTime) 143 { 144 for (size_t i = 0; i < m_players.size(); i++) { 145 m_players[i]->pauseForTesting(); 146 m_players[i]->setCurrentTime(pauseTime); 147 } 148 } 149 150 void DocumentTimeline::dispatchEvents() 151 { 152 Vector<EventToDispatch> events = m_events; 153 m_events.clear(); 154 for (size_t i = 0; i < events.size(); i++) 155 events[i].target->dispatchEvent(events[i].event.release()); 156 } 157 158 void DocumentTimeline::dispatchEventsAsync() 159 { 160 if (m_events.isEmpty() || m_eventDistpachTimer.isActive()) 161 return; 162 m_eventDistpachTimer.startOneShot(0); 163 } 164 165 void DocumentTimeline::eventDispatchTimerFired(Timer<DocumentTimeline>*) 166 { 167 dispatchEvents(); 168 } 169 170 size_t DocumentTimeline::numberOfActiveAnimationsForTesting() const 171 { 172 if (isNull(m_zeroTime)) 173 return 0; 174 // Includes all players whose directly associated timed items 175 // are current or in effect. 176 if (isNull(m_zeroTime)) 177 return 0; 178 size_t count = 0; 179 for (size_t i = 0; i < m_players.size(); ++i) { 180 const TimedItem* timedItem = m_players[i]->source(); 181 if (m_players[i]->hasStartTime()) 182 count += (timedItem && (timedItem->isCurrent() || timedItem->isInEffect())); 183 } 184 return count; 185 } 186 187 } // namespace 188