tagging audio streams and changing audio sink to pulseaudio
[profile/ivi/webkit-efl.git] / Source / WebCore / platform / graphics / DisplayRefreshMonitor.h
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 #ifndef DisplayRefreshMonitor_h
27 #define DisplayRefreshMonitor_h
28
29 #if USE(REQUEST_ANIMATION_FRAME_DISPLAY_MONITOR)
30
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>
39 #endif
40
41 #if PLATFORM(MAC)
42 typedef struct __CVDisplayLink *CVDisplayLinkRef;
43 #endif
44
45 namespace WebCore {
46
47 class DisplayRefreshMonitor;
48 class DisplayRefreshMonitorManager;
49
50 //
51 // Abstract virtual client which receives refresh fired messages on the main thread
52 //
53 class DisplayRefreshMonitorClient {
54     friend class DisplayRefreshMonitor;
55     friend class DisplayRefreshMonitorManager;
56     
57 public:
58     DisplayRefreshMonitorClient();
59     virtual ~DisplayRefreshMonitorClient();
60     
61     virtual void displayRefreshFired(double timestamp) = 0;
62
63 private:
64     void fireDisplayRefreshIfNeeded(double timestamp);
65     
66     void setDisplayID(PlatformDisplayID displayID)
67     {
68         m_displayID = displayID;
69         m_displayIDIsSet = true;
70     }
71     
72     bool m_scheduled;
73     bool m_displayIDIsSet;
74     PlatformDisplayID m_displayID;
75 };
76
77 #if PLATFORM(BLACKBERRY)
78 class DisplayAnimationClient : public BlackBerry::Platform::AnimationFrameRateClient {
79 public:
80     DisplayAnimationClient(DisplayRefreshMonitor *);
81     ~DisplayAnimationClient() { }
82 private:
83     virtual void animationFrameChanged();
84     DisplayRefreshMonitor *m_monitor;
85 };
86 #endif
87
88 //
89 // Monitor for display refresh messages for a given screen
90 //
91
92 class DisplayRefreshMonitor : public RefCounted<DisplayRefreshMonitor> {
93 public:
94     static PassRefPtr<DisplayRefreshMonitor> create(PlatformDisplayID displayID)
95     {
96         return adoptRef(new DisplayRefreshMonitor(displayID));
97     }
98     
99     ~DisplayRefreshMonitor();
100     
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);
105     
106     bool hasClients() const { return m_clients.size(); }
107     void addClient(DisplayRefreshMonitorClient*);
108     bool removeClient(DisplayRefreshMonitorClient*);
109     
110     PlatformDisplayID displayID() const { return m_displayID; }
111
112     bool shouldBeTerminated() const
113     {
114         const int maxInactiveFireCount = 10;
115         return !m_scheduled && m_unscheduledFireCount > maxInactiveFireCount;
116     }
117     
118 private:
119     DisplayRefreshMonitor(PlatformDisplayID);
120
121     void displayDidRefresh();
122     static void handleDisplayRefreshedNotificationOnMainThread(void* data);
123
124     double m_timestamp;
125     bool m_active;
126     bool m_scheduled;
127     bool m_previousFrameDone;
128     int m_unscheduledFireCount; // Number of times the display link has fired with no clients.
129     PlatformDisplayID m_displayID;
130     Mutex m_mutex;
131     
132     typedef HashSet<DisplayRefreshMonitorClient*> DisplayRefreshMonitorClientSet;
133     DisplayRefreshMonitorClientSet m_clients;
134 #if PLATFORM(BLACKBERRY)
135 public:
136     void displayLinkFired();
137 private:
138     DisplayAnimationClient *m_animationClient;
139     void startAnimationClient();
140     void stopAnimationClient();
141 #endif
142 #if PLATFORM(MAC)
143 public:
144     void displayLinkFired(double nowSeconds, double outputTimeSeconds);
145 private:
146     CVDisplayLinkRef m_displayLink;
147 #endif
148 };
149
150 //
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
154 // the main thread.
155 //
156 class DisplayRefreshMonitorManager {
157 public:
158     static DisplayRefreshMonitorManager* sharedManager();
159     
160     void registerClient(DisplayRefreshMonitorClient*);
161     void unregisterClient(DisplayRefreshMonitorClient*);
162
163     bool scheduleAnimation(DisplayRefreshMonitorClient*);
164     void windowScreenDidChange(PlatformDisplayID, DisplayRefreshMonitorClient*);
165
166 private:
167     friend class DisplayRefreshMonitor;
168     void displayDidRefresh(DisplayRefreshMonitor*);
169     
170     DisplayRefreshMonitorManager() { }
171     DisplayRefreshMonitor* ensureMonitorForClient(DisplayRefreshMonitorClient*);
172
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;
176 };
177
178 }
179
180 #endif // USE(REQUEST_ANIMATION_FRAME_DISPLAY_MONITOR)
181
182 #endif