Upstream version 5.34.104.0
[platform/framework/web/crosswalk.git] / src / third_party / WebKit / Source / core / animation / DocumentTimeline.cpp
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 DocumentTimeline::~DocumentTimeline()
65 {
66     for (HashSet<Player*>::iterator it = m_players.begin(); it != m_players.end(); ++it)
67         (*it)->timelineDestroyed();
68 }
69
70 Player* DocumentTimeline::createPlayer(TimedItem* child)
71 {
72     RefPtr<Player> player = Player::create(*this, child);
73     Player* result = player.get();
74     m_players.add(result);
75     m_currentPlayers.append(player.release());
76     setHasPlayerNeedingUpdate();
77     return result;
78 }
79
80 Player* DocumentTimeline::play(TimedItem* child)
81 {
82     Player* player = createPlayer(child);
83     player->setStartTime(currentTime());
84     return player;
85 }
86
87 void DocumentTimeline::wake()
88 {
89     m_timing->serviceOnNextFrame();
90 }
91
92 bool DocumentTimeline::serviceAnimations()
93 {
94     TRACE_EVENT0("webkit", "DocumentTimeline::serviceAnimations");
95
96     m_timing->cancelWake();
97
98     double timeToNextEffect = std::numeric_limits<double>::infinity();
99     bool didTriggerStyleRecalc = false;
100     for (int i = m_currentPlayers.size() - 1; i >= 0; --i) {
101         RefPtr<Player> player = m_currentPlayers[i].get();
102         bool playerDidTriggerStyleRecalc;
103         if (!player->update(&playerDidTriggerStyleRecalc))
104             m_currentPlayers.remove(i);
105         timeToNextEffect = std::min(timeToNextEffect, player->timeToEffectChange());
106         didTriggerStyleRecalc |= playerDidTriggerStyleRecalc;
107     }
108
109     if (!m_currentPlayers.isEmpty()) {
110         if (timeToNextEffect < s_minimumDelay)
111             m_timing->serviceOnNextFrame();
112         else if (timeToNextEffect != std::numeric_limits<double>::infinity())
113             m_timing->wakeAfter(timeToNextEffect - s_minimumDelay);
114     }
115
116     m_hasPlayerNeedingUpdate = false;
117     return didTriggerStyleRecalc;
118 }
119
120 void DocumentTimeline::setZeroTime(double zeroTime)
121 {
122     ASSERT(isNull(m_zeroTime));
123     m_zeroTime = zeroTime;
124     ASSERT(!isNull(m_zeroTime));
125     serviceAnimations();
126 }
127
128 void DocumentTimeline::DocumentTimelineTiming::wakeAfter(double duration)
129 {
130     m_timer.startOneShot(duration);
131 }
132
133 void DocumentTimeline::DocumentTimelineTiming::cancelWake()
134 {
135     m_timer.stop();
136 }
137
138 void DocumentTimeline::DocumentTimelineTiming::serviceOnNextFrame()
139 {
140     if (m_timeline->m_document && m_timeline->m_document->view())
141         m_timeline->m_document->view()->scheduleAnimation();
142 }
143
144 double DocumentTimeline::currentTime()
145 {
146     if (!m_document)
147         return std::numeric_limits<double>::quiet_NaN();
148     return m_document->animationClock().currentTime() - m_zeroTime;
149 }
150
151 void DocumentTimeline::pauseAnimationsForTesting(double pauseTime)
152 {
153     for (size_t i = 0; i < m_currentPlayers.size(); i++)
154         m_currentPlayers[i]->pauseForTesting(pauseTime);
155     serviceAnimations();
156 }
157
158 void DocumentTimeline::setHasPlayerNeedingUpdate()
159 {
160     m_hasPlayerNeedingUpdate = true;
161     if (m_document && m_document->view() && !m_document->view()->isServicingAnimations())
162         m_timing->serviceOnNextFrame();
163 }
164
165 void DocumentTimeline::dispatchEvents()
166 {
167     Vector<EventToDispatch> events = m_events;
168     m_events.clear();
169     for (size_t i = 0; i < events.size(); i++)
170         events[i].target->dispatchEvent(events[i].event.release());
171 }
172
173 void DocumentTimeline::dispatchEventsAsync()
174 {
175     if (m_events.isEmpty() || m_eventDistpachTimer.isActive())
176         return;
177     m_eventDistpachTimer.startOneShot(0);
178 }
179
180 void DocumentTimeline::eventDispatchTimerFired(Timer<DocumentTimeline>*)
181 {
182     dispatchEvents();
183 }
184
185 size_t DocumentTimeline::numberOfActiveAnimationsForTesting() const
186 {
187     if (isNull(m_zeroTime))
188         return 0;
189     // Includes all players whose directly associated timed items
190     // are current or in effect.
191     if (isNull(m_zeroTime))
192         return 0;
193     size_t count = 0;
194     for (size_t i = 0; i < m_currentPlayers.size(); ++i) {
195         const TimedItem* timedItem = m_currentPlayers[i]->source();
196         if (m_currentPlayers[i]->hasStartTime())
197             count += (timedItem && (timedItem->isCurrent() || timedItem->isInEffect()));
198     }
199     return count;
200 }
201
202 void DocumentTimeline::detachFromDocument() {
203     m_document = 0;
204 }
205
206 } // namespace