Implement Scroll Container Animation Triggers
[WebKit-https.git] / Source / WebCore / platform / graphics / DisplayRefreshMonitorManager.cpp
1 /*
2  * Copyright (C) 2010, 2014 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
26 #include "config.h"
27 #include "DisplayRefreshMonitorManager.h"
28
29 #if USE(REQUEST_ANIMATION_FRAME_DISPLAY_MONITOR)
30
31 #include "DisplayRefreshMonitor.h"
32 #include "DisplayRefreshMonitorClient.h"
33 #include <wtf/CurrentTime.h>
34
35 namespace WebCore {
36
37 DisplayRefreshMonitorManager::~DisplayRefreshMonitorManager()
38 {
39 }
40
41 DisplayRefreshMonitorManager& DisplayRefreshMonitorManager::sharedManager()
42 {
43     static NeverDestroyed<DisplayRefreshMonitorManager> manager;
44     return manager.get();
45 }
46
47 DisplayRefreshMonitor* DisplayRefreshMonitorManager::ensureMonitorForClient(DisplayRefreshMonitorClient* client)
48 {
49     PlatformDisplayID clientDisplayID = client->displayID();
50     for (const RefPtr<DisplayRefreshMonitor>& monitor : m_monitors) {
51         if (monitor->displayID() != clientDisplayID)
52             continue;
53         monitor->addClient(client);
54         return monitor.get();
55     }
56
57     RefPtr<DisplayRefreshMonitor> monitor = DisplayRefreshMonitor::create(client);
58     monitor->addClient(client);
59     DisplayRefreshMonitor* result = monitor.get();
60     m_monitors.append(monitor.release());
61     return result;
62 }
63
64 void DisplayRefreshMonitorManager::registerClient(DisplayRefreshMonitorClient* client)
65 {
66     if (!client->hasDisplayID())
67         return;
68
69     ensureMonitorForClient(client);
70 }
71
72 void DisplayRefreshMonitorManager::unregisterClient(DisplayRefreshMonitorClient* client)
73 {
74     if (!client->hasDisplayID())
75         return;
76
77     PlatformDisplayID clientDisplayID = client->displayID();
78     for (size_t i = 0; i < m_monitors.size(); ++i) {
79         RefPtr<DisplayRefreshMonitor> monitor = m_monitors[i];
80         if (monitor->displayID() != clientDisplayID)
81             continue;
82         if (monitor->removeClient(client)) {
83             if (!monitor->hasClients())
84                 m_monitors.remove(i);
85         }
86         return;
87     }
88 }
89
90 bool DisplayRefreshMonitorManager::scheduleAnimation(DisplayRefreshMonitorClient* client)
91 {
92     if (!client->hasDisplayID())
93         return false;
94
95     DisplayRefreshMonitor* monitor = ensureMonitorForClient(client);
96
97     client->setIsScheduled(true);
98     return monitor->requestRefreshCallback();
99 }
100
101 void DisplayRefreshMonitorManager::displayDidRefresh(DisplayRefreshMonitor* monitor)
102 {
103     if (!monitor->shouldBeTerminated())
104         return;
105
106     size_t monitorIndex = m_monitors.find(monitor);
107     ASSERT(monitorIndex != notFound);
108     m_monitors.remove(monitorIndex);
109 }
110
111 void DisplayRefreshMonitorManager::windowScreenDidChange(PlatformDisplayID displayID, DisplayRefreshMonitorClient* client)
112 {
113     if (client->hasDisplayID() && client->displayID() == displayID)
114         return;
115     
116     unregisterClient(client);
117     client->setDisplayID(displayID);
118     registerClient(client);
119     if (client->isScheduled())
120         scheduleAnimation(client);
121 }
122
123 }
124
125 #endif // USE(REQUEST_ANIMATION_FRAME_DISPLAY_MONITOR)