2 * Copyright (C) 2010 Apple Inc. All rights reserved.
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
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.
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.
26 #ifndef DisplayRefreshMonitor_h
27 #define DisplayRefreshMonitor_h
29 #if USE(REQUEST_ANIMATION_FRAME_DISPLAY_MONITOR)
31 #include "PlatformScreen.h"
32 #include <wtf/HashMap.h>
33 #include <wtf/HashSet.h>
34 #include <wtf/RefCounted.h>
35 #include <wtf/RefPtr.h>
36 #include <wtf/Threading.h>
37 #if PLATFORM(BLACKBERRY)
38 #include <BlackBerryPlatformAnimationFrameRateController.h>
42 typedef struct __CVDisplayLink *CVDisplayLinkRef;
47 class DisplayRefreshMonitor;
48 class DisplayRefreshMonitorManager;
51 // Abstract virtual client which receives refresh fired messages on the main thread
53 class DisplayRefreshMonitorClient {
54 friend class DisplayRefreshMonitor;
55 friend class DisplayRefreshMonitorManager;
58 DisplayRefreshMonitorClient();
59 virtual ~DisplayRefreshMonitorClient();
61 virtual void displayRefreshFired(double timestamp) = 0;
64 void fireDisplayRefreshIfNeeded(double timestamp);
66 void setDisplayID(PlatformDisplayID displayID)
68 m_displayID = displayID;
69 m_displayIDIsSet = true;
73 bool m_displayIDIsSet;
74 PlatformDisplayID m_displayID;
77 #if PLATFORM(BLACKBERRY)
78 class DisplayAnimationClient : public BlackBerry::Platform::AnimationFrameRateClient {
80 DisplayAnimationClient(DisplayRefreshMonitor *);
81 ~DisplayAnimationClient() { }
83 virtual void animationFrameChanged();
84 DisplayRefreshMonitor *m_monitor;
89 // Monitor for display refresh messages for a given screen
92 class DisplayRefreshMonitor : public RefCounted<DisplayRefreshMonitor> {
94 static PassRefPtr<DisplayRefreshMonitor> create(PlatformDisplayID displayID)
96 return adoptRef(new DisplayRefreshMonitor(displayID));
99 ~DisplayRefreshMonitor();
101 // Return true if callback request was scheduled, false if it couldn't be
102 // (e.g., hardware refresh is not available)
103 bool requestRefreshCallback();
104 void windowScreenDidChange(PlatformDisplayID);
106 bool hasClients() const { return m_clients.size(); }
107 void addClient(DisplayRefreshMonitorClient*);
108 bool removeClient(DisplayRefreshMonitorClient*);
110 PlatformDisplayID displayID() const { return m_displayID; }
112 bool shouldBeTerminated() const
114 const int maxInactiveFireCount = 10;
115 return !m_scheduled && m_unscheduledFireCount > maxInactiveFireCount;
119 DisplayRefreshMonitor(PlatformDisplayID);
121 void displayDidRefresh();
122 static void handleDisplayRefreshedNotificationOnMainThread(void* data);
127 bool m_previousFrameDone;
128 int m_unscheduledFireCount; // Number of times the display link has fired with no clients.
129 PlatformDisplayID m_displayID;
132 typedef HashSet<DisplayRefreshMonitorClient*> DisplayRefreshMonitorClientSet;
133 DisplayRefreshMonitorClientSet m_clients;
134 #if PLATFORM(BLACKBERRY)
136 void displayLinkFired();
138 DisplayAnimationClient *m_animationClient;
139 void startAnimationClient();
140 void stopAnimationClient();
144 void displayLinkFired(double nowSeconds, double outputTimeSeconds);
146 CVDisplayLinkRef m_displayLink;
151 // Singleton manager for all the DisplayRefreshMonitors. This is the interface to the
152 // outside world. It distributes requests to the appropriate monitor. When the display
153 // refresh event fires, the passed DisplayRefreshMonitorClient is called directly on
156 class DisplayRefreshMonitorManager {
158 static DisplayRefreshMonitorManager* sharedManager();
160 void registerClient(DisplayRefreshMonitorClient*);
161 void unregisterClient(DisplayRefreshMonitorClient*);
163 bool scheduleAnimation(DisplayRefreshMonitorClient*);
164 void windowScreenDidChange(PlatformDisplayID, DisplayRefreshMonitorClient*);
167 friend class DisplayRefreshMonitor;
168 void displayDidRefresh(DisplayRefreshMonitor*);
170 DisplayRefreshMonitorManager() { }
171 DisplayRefreshMonitor* ensureMonitorForClient(DisplayRefreshMonitorClient*);
173 // We know nothing about the values of PlatformDisplayIDs, so use UnsignedWithZeroKeyHashTraits.
174 typedef HashMap<uint64_t, RefPtr<DisplayRefreshMonitor>, WTF::IntHash<uint64_t>, WTF::UnsignedWithZeroKeyHashTraits<uint64_t> > DisplayRefreshMonitorMap;
175 DisplayRefreshMonitorMap m_monitors;
180 #endif // USE(REQUEST_ANIMATION_FRAME_DISPLAY_MONITOR)