1/*
2 * Copyright (C) 2010, 2011 Research In Motion Limited. All rights reserved.
3 *
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; either
7 * version 2 of the License, or (at your option) any later version.
8 *
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12 * Lesser General Public License for more details.
13 *
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with this library; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
17 */
18
19#include "config.h"
20
21#if USE(REQUEST_ANIMATION_FRAME_DISPLAY_MONITOR)
22
23#include "DisplayRefreshMonitor.h"
24
25#include <wtf/CurrentTime.h>
26#include <wtf/MainThread.h>
27
28namespace WebCore {
29
30class DisplayAnimationClient : public BlackBerry::Platform::AnimationFrameRateClient {
31public:
32    DisplayAnimationClient(DisplayRefreshMonitor *);
33    ~DisplayAnimationClient() { }
34private:
35    virtual void animationFrameChanged();
36    DisplayRefreshMonitor* m_monitor;
37};
38
39DisplayAnimationClient::DisplayAnimationClient(DisplayRefreshMonitor *monitor)
40    : m_monitor(monitor)
41{
42}
43
44void DisplayAnimationClient::animationFrameChanged()
45{
46    m_monitor->displayLinkFired();
47}
48
49DisplayRefreshMonitor::~DisplayRefreshMonitor()
50{
51    stopAnimationClient();
52    cancelCallOnMainThread(DisplayRefreshMonitor::handleDisplayRefreshedNotificationOnMainThread, this);
53}
54
55void DisplayRefreshMonitor::startAnimationClient()
56{
57    if (m_animationClient)
58        return;
59
60    m_animationClient = new DisplayAnimationClient(this);
61    BlackBerry::Platform::AnimationFrameRateController::instance()->addClient(m_animationClient);
62}
63
64void DisplayRefreshMonitor::stopAnimationClient()
65{
66    if (!m_animationClient)
67        return;
68
69    BlackBerry::Platform::AnimationFrameRateController::instance()->removeClient(m_animationClient);
70    delete m_animationClient;
71    m_animationClient = 0;
72}
73
74bool DisplayRefreshMonitor::requestRefreshCallback()
75{
76    MutexLocker lock(m_mutex);
77
78    startAnimationClient();
79
80    m_scheduled = true;
81    return true;
82}
83
84void DisplayRefreshMonitor::displayLinkFired()
85{
86    if (!m_mutex.tryLock())
87        return;
88
89    if (!m_previousFrameDone) {
90        m_mutex.unlock();
91        return;
92    }
93
94    m_previousFrameDone = false;
95
96    m_monotonicAnimationStartTime = monotonicallyIncreasingTime();
97
98    callOnMainThread(handleDisplayRefreshedNotificationOnMainThread, this);
99    m_mutex.unlock();
100}
101
102}
103
104#endif // USE(REQUEST_ANIMATION_FRAME_DISPLAY_MONITOR)
105