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#ifndef DisplayRefreshMonitor_h
27#define DisplayRefreshMonitor_h
28
29#if USE(REQUEST_ANIMATION_FRAME_DISPLAY_MONITOR)
30
31#include "PlatformScreen.h"
32#include <wtf/HashSet.h>
33#include <wtf/RefCounted.h>
34#include <wtf/RefPtr.h>
35#include <wtf/Threading.h>
36
37namespace WebCore {
38
39class DisplayAnimationClient;
40class DisplayRefreshMonitorClient;
41
42class DisplayRefreshMonitor : public RefCounted<DisplayRefreshMonitor> {
43public:
44    static PassRefPtr<DisplayRefreshMonitor> create(DisplayRefreshMonitorClient*);
45    virtual ~DisplayRefreshMonitor();
46
47    // Return true if callback request was scheduled, false if it couldn't be
48    // (e.g., hardware refresh is not available)
49    virtual bool requestRefreshCallback() = 0;
50    void windowScreenDidChange(PlatformDisplayID);
51
52    bool hasClients() const { return m_clients.size(); }
53    void addClient(DisplayRefreshMonitorClient*);
54    bool removeClient(DisplayRefreshMonitorClient*);
55
56    PlatformDisplayID displayID() const { return m_displayID; }
57
58    bool shouldBeTerminated() const
59    {
60        const int maxInactiveFireCount = 10;
61        return !m_scheduled && m_unscheduledFireCount > maxInactiveFireCount;
62    }
63
64    bool isActive() const { return m_active; }
65    void setIsActive(bool active) { m_active = active; }
66
67    bool isScheduled() const { return m_scheduled; }
68    void setIsScheduled(bool scheduled) { m_scheduled = scheduled; }
69
70    bool isPreviousFrameDone() const { return m_previousFrameDone; }
71    void setIsPreviousFrameDone(bool done) { m_previousFrameDone = done; }
72
73    void setMonotonicAnimationStartTime(double startTime) { m_monotonicAnimationStartTime = startTime; }
74
75    Mutex& mutex() { return m_mutex; }
76
77protected:
78    explicit DisplayRefreshMonitor(PlatformDisplayID);
79    static void handleDisplayRefreshedNotificationOnMainThread(void* data);
80
81private:
82    void displayDidRefresh();
83
84    double m_monotonicAnimationStartTime;
85    bool m_active;
86    bool m_scheduled;
87    bool m_previousFrameDone;
88    int m_unscheduledFireCount; // Number of times the display link has fired with no clients.
89    PlatformDisplayID m_displayID;
90    Mutex m_mutex;
91
92    HashSet<DisplayRefreshMonitorClient*> m_clients;
93    HashSet<DisplayRefreshMonitorClient*>* m_clientsToBeNotified;
94};
95
96}
97
98#endif // USE(REQUEST_ANIMATION_FRAME_DISPLAY_MONITOR)
99
100#endif
101