| 1 | /* |
| 2 | * Copyright (C) 2014 Igalia S.L. |
| 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. AND ITS CONTRIBUTORS ``AS IS'' |
| 14 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, |
| 15 | * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR |
| 16 | * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS |
| 17 | * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
| 18 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
| 19 | * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
| 20 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
| 21 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
| 22 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF |
| 23 | * THE POSSIBILITY OF SUCH DAMAGE. |
| 24 | */ |
| 25 | |
| 26 | #include "config.h" |
| 27 | #include "ThreadedDisplayRefreshMonitor.h" |
| 28 | |
| 29 | #if USE(COORDINATED_GRAPHICS) && USE(REQUEST_ANIMATION_FRAME_DISPLAY_MONITOR) |
| 30 | |
| 31 | #include "CompositingRunLoop.h" |
| 32 | #include "ThreadedCompositor.h" |
| 33 | |
| 34 | #if USE(GLIB_EVENT_LOOP) |
| 35 | #include <wtf/glib/RunLoopSourcePriority.h> |
| 36 | #endif |
| 37 | |
| 38 | namespace WebKit { |
| 39 | |
| 40 | ThreadedDisplayRefreshMonitor::ThreadedDisplayRefreshMonitor(WebCore::PlatformDisplayID displayID, Client& client) |
| 41 | : WebCore::DisplayRefreshMonitor(displayID) |
| 42 | , m_displayRefreshTimer(RunLoop::main(), this, &ThreadedDisplayRefreshMonitor::displayRefreshCallback) |
| 43 | , m_client(&client) |
| 44 | { |
| 45 | #if USE(GLIB_EVENT_LOOP) |
| 46 | m_displayRefreshTimer.setPriority(RunLoopSourcePriority::DisplayRefreshMonitorTimer); |
| 47 | m_displayRefreshTimer.setName("[WebKit] ThreadedDisplayRefreshMonitor" ); |
| 48 | #endif |
| 49 | } |
| 50 | |
| 51 | bool ThreadedDisplayRefreshMonitor::requestRefreshCallback() |
| 52 | { |
| 53 | if (!m_client) |
| 54 | return false; |
| 55 | |
| 56 | bool previousFrameDone { false }; |
| 57 | { |
| 58 | LockHolder locker(mutex()); |
| 59 | setIsScheduled(true); |
| 60 | previousFrameDone = isPreviousFrameDone(); |
| 61 | } |
| 62 | |
| 63 | // Only request an update in case we're not currently handling the display |
| 64 | // refresh notifications under ThreadedDisplayRefreshMonitor::displayRefreshCallback(). |
| 65 | // Any such schedule request is handled in that method after the notifications. |
| 66 | if (previousFrameDone) |
| 67 | m_client->requestDisplayRefreshMonitorUpdate(); |
| 68 | |
| 69 | return true; |
| 70 | } |
| 71 | |
| 72 | bool ThreadedDisplayRefreshMonitor::requiresDisplayRefreshCallback() |
| 73 | { |
| 74 | LockHolder locker(mutex()); |
| 75 | return isScheduled() && isPreviousFrameDone(); |
| 76 | } |
| 77 | |
| 78 | void ThreadedDisplayRefreshMonitor::dispatchDisplayRefreshCallback() |
| 79 | { |
| 80 | if (!m_client) |
| 81 | return; |
| 82 | m_displayRefreshTimer.startOneShot(0_s); |
| 83 | } |
| 84 | |
| 85 | void ThreadedDisplayRefreshMonitor::invalidate() |
| 86 | { |
| 87 | m_displayRefreshTimer.stop(); |
| 88 | m_client = nullptr; |
| 89 | } |
| 90 | |
| 91 | void ThreadedDisplayRefreshMonitor::displayRefreshCallback() |
| 92 | { |
| 93 | bool shouldHandleDisplayRefreshNotification { false }; |
| 94 | { |
| 95 | LockHolder locker(mutex()); |
| 96 | shouldHandleDisplayRefreshNotification = isScheduled() && isPreviousFrameDone(); |
| 97 | if (shouldHandleDisplayRefreshNotification) |
| 98 | setIsPreviousFrameDone(false); |
| 99 | } |
| 100 | |
| 101 | if (shouldHandleDisplayRefreshNotification) |
| 102 | DisplayRefreshMonitor::handleDisplayRefreshedNotificationOnMainThread(this); |
| 103 | |
| 104 | // Retrieve the scheduled status for this DisplayRefreshMonitor. |
| 105 | bool hasBeenRescheduled { false }; |
| 106 | { |
| 107 | LockHolder locker(mutex()); |
| 108 | hasBeenRescheduled = isScheduled(); |
| 109 | } |
| 110 | |
| 111 | // Notify the compositor about the completed DisplayRefreshMonitor update, passing |
| 112 | // along information about any schedule request that might have occurred during |
| 113 | // the notification handling. |
| 114 | if (m_client) |
| 115 | m_client->handleDisplayRefreshMonitorUpdate(hasBeenRescheduled); |
| 116 | } |
| 117 | |
| 118 | } // namespace WebKit |
| 119 | |
| 120 | #endif // USE(COORDINATED_GRAPHICS) && USE(REQUEST_ANIMATION_FRAME_DISPLAY_MONITOR) |
| 121 | |