[BlackBerry] Implement REQUEST_ANIMATION_FRAME_DISPLAY_MONITOR using AnimationFrameRa...
[WebKit-https.git] / Source / WebCore / platform / graphics / DisplayRefreshMonitor.cpp
1 /*
2  * Copyright (C) 2010 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 COMPUTER, 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
26 #include "config.h"
27
28 #if USE(REQUEST_ANIMATION_FRAME_DISPLAY_MONITOR)
29
30 #include "DisplayRefreshMonitor.h"
31
32 #include <wtf/CurrentTime.h>
33
34 namespace WebCore {
35
36 DisplayRefreshMonitorClient::DisplayRefreshMonitorClient()
37     : m_scheduled(false)
38     , m_displayIDIsSet(false)
39 {
40 }
41
42 DisplayRefreshMonitorClient::~DisplayRefreshMonitorClient()
43 {
44     DisplayRefreshMonitorManager::sharedManager()->unregisterClient(this);
45 }
46
47 void DisplayRefreshMonitorClient::fireDisplayRefreshIfNeeded(double timestamp)
48 {
49     if (m_scheduled) {
50         m_scheduled = false;
51         displayRefreshFired(timestamp);
52     }
53 }
54
55 DisplayRefreshMonitor::DisplayRefreshMonitor(PlatformDisplayID displayID)
56     : m_timestamp(0)
57     , m_active(true)
58     , m_scheduled(false)
59     , m_previousFrameDone(true)
60     , m_displayID(displayID)
61 #if PLATFORM(MAC)
62     , m_displayLink(0)
63 #endif
64 #if PLATFORM(BLACKBERRY)
65     , m_animationClient(0)
66 #endif
67 {
68 }
69
70 void DisplayRefreshMonitor::refreshDisplayOnMainThread(void* data)
71 {
72     DisplayRefreshMonitor* monitor = static_cast<DisplayRefreshMonitor*>(data);
73     monitor->notifyClients();
74 }
75
76 void DisplayRefreshMonitor::notifyClients()
77 {
78     double timestamp;
79     {
80         MutexLocker lock(m_mutex);
81         m_scheduled = false;
82         timestamp = m_timestamp;
83     }
84
85     for (size_t i = 0; i < m_clients.size(); ++i)
86         m_clients[i]->fireDisplayRefreshIfNeeded(timestamp);
87
88     {
89         MutexLocker lock(m_mutex);
90         m_previousFrameDone = true;
91     }
92 }
93
94 DisplayRefreshMonitorManager* DisplayRefreshMonitorManager::sharedManager()
95 {
96     DEFINE_STATIC_LOCAL(DisplayRefreshMonitorManager, manager, ());
97     return &manager;
98 }
99
100 size_t DisplayRefreshMonitorManager::findMonitor(PlatformDisplayID displayID) const
101 {
102     for (size_t i = 0; i < m_monitors.size(); ++i)
103         if (m_monitors[i]->displayID() == displayID)
104             return i;
105             
106     return notFound;
107 }
108
109 void DisplayRefreshMonitorManager::registerClient(DisplayRefreshMonitorClient* client)
110 {
111     if (!client->m_displayIDIsSet)
112         return;
113         
114     size_t index = findMonitor(client->m_displayID);
115     DisplayRefreshMonitor* monitor;
116     
117     if (index == notFound) {
118         monitor = new DisplayRefreshMonitor(client->m_displayID);
119         m_monitors.append(monitor);
120     } else
121         monitor = m_monitors[index];
122         
123     monitor->addClient(client);
124 }
125
126 void DisplayRefreshMonitorManager::unregisterClient(DisplayRefreshMonitorClient* client)
127 {
128     if (!client->m_displayIDIsSet)
129         return;
130         
131     size_t index = findMonitor(client->m_displayID);
132     if (index == notFound)
133         return;
134     
135     DisplayRefreshMonitor* monitor = m_monitors[index];
136     if (monitor->removeClient(client)) {
137         if (!monitor->hasClients()) {
138             m_monitors.remove(index);
139             delete monitor;
140         }
141     }
142 }
143
144 bool DisplayRefreshMonitorManager::scheduleAnimation(DisplayRefreshMonitorClient* client)
145 {
146     if (!client->m_displayIDIsSet)
147         return false;
148         
149     size_t i = findMonitor(client->m_displayID);
150     ASSERT(i != notFound);
151
152     client->m_scheduled = true;
153     return m_monitors[i]->requestRefreshCallback();
154 }
155
156 void DisplayRefreshMonitorManager::windowScreenDidChange(PlatformDisplayID displayID, DisplayRefreshMonitorClient* client)
157 {
158     if (client->m_displayIDIsSet && client->m_displayID == displayID)
159         return;
160     
161     unregisterClient(client);
162     client->setDisplayID(displayID);
163     registerClient(client);
164     if (client->m_scheduled)
165         scheduleAnimation(client);
166 }
167
168 }
169
170 #endif // USE(REQUEST_ANIMATION_FRAME_DISPLAY_MONITOR)