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 "CompositingRunLoop.h"
28
29#if USE(COORDINATED_GRAPHICS)
30
31#include <wtf/HashMap.h>
32#include <wtf/MainThread.h>
33#include <wtf/Threading.h>
34#include <wtf/threads/BinarySemaphore.h>
35
36#if USE(GLIB_EVENT_LOOP)
37#include <wtf/glib/RunLoopSourcePriority.h>
38#endif
39
40namespace WebKit {
41
42static RunLoop* createRunLoop()
43{
44 RunLoop* runLoop = nullptr;
45 BinarySemaphore semaphore;
46 Thread::create("org.webkit.ThreadedCompositor", [&] {
47 runLoop = &RunLoop::current();
48 semaphore.signal();
49 runLoop->run();
50 })->detach();
51 semaphore.wait();
52
53 return runLoop;
54}
55
56CompositingRunLoop::CompositingRunLoop(Function<void ()>&& updateFunction)
57 : m_runLoop(createRunLoop())
58 , m_updateTimer(*m_runLoop, this, &CompositingRunLoop::updateTimerFired)
59 , m_updateFunction(WTFMove(updateFunction))
60{
61#if USE(GLIB_EVENT_LOOP)
62 m_updateTimer.setPriority(RunLoopSourcePriority::CompositingThreadUpdateTimer);
63 m_updateTimer.setName("[WebKit] CompositingRunLoop");
64#endif
65}
66
67CompositingRunLoop::~CompositingRunLoop()
68{
69 ASSERT(RunLoop::isMain());
70 // Make sure the RunLoop is stopped after the CompositingRunLoop, because m_updateTimer has a reference.
71 RunLoop::main().dispatch([runLoop = makeRef(*m_runLoop)] {
72 runLoop->stop();
73 runLoop->dispatch([] {
74 RunLoop::current().stop();
75 });
76 });
77}
78
79void CompositingRunLoop::performTask(Function<void ()>&& function)
80{
81 ASSERT(RunLoop::isMain());
82 m_runLoop->dispatch(WTFMove(function));
83}
84
85void CompositingRunLoop::performTaskSync(Function<void ()>&& function)
86{
87 ASSERT(RunLoop::isMain());
88 LockHolder locker(m_dispatchSyncConditionMutex);
89 m_runLoop->dispatch([this, function = WTFMove(function)] {
90 function();
91 LockHolder locker(m_dispatchSyncConditionMutex);
92 m_dispatchSyncCondition.notifyOne();
93 });
94 m_dispatchSyncCondition.wait(m_dispatchSyncConditionMutex);
95}
96
97void CompositingRunLoop::suspend()
98{
99 LockHolder stateLocker(m_state.lock);
100 m_state.isSuspended = true;
101 m_updateTimer.stop();
102}
103
104void CompositingRunLoop::resume()
105{
106 LockHolder stateLocker(m_state.lock);
107 m_state.isSuspended = false;
108 if (m_state.update == UpdateState::Scheduled)
109 m_updateTimer.startOneShot(0_s);
110}
111
112void CompositingRunLoop::scheduleUpdate()
113{
114 LockHolder stateLocker(m_state.lock);
115 scheduleUpdate(stateLocker);
116}
117
118void CompositingRunLoop::scheduleUpdate(LockHolder& stateLocker)
119{
120 // An update was requested. Depending on the state:
121 // - if Idle, enter the Scheduled state and start the update timer,
122 // - if Scheduled, do nothing,
123 // - if InProgress mark an update as pending, meaning another update will be
124 // scheduled as soon as the current one is completed.
125
126 UNUSED_PARAM(stateLocker);
127
128 switch (m_state.update) {
129 case UpdateState::Idle:
130 m_state.update = UpdateState::Scheduled;
131 if (!m_state.isSuspended)
132 m_updateTimer.startOneShot(0_s);
133 return;
134 case UpdateState::Scheduled:
135 return;
136 case UpdateState::InProgress:
137 m_state.pendingUpdate = true;
138 return;
139 }
140}
141
142void CompositingRunLoop::stopUpdates()
143{
144 // Stop everything.
145
146 LockHolder locker(m_state.lock);
147 m_updateTimer.stop();
148 m_state.update = UpdateState::Idle;
149 m_state.pendingUpdate = false;
150}
151
152void CompositingRunLoop::updateCompleted(LockHolder& stateLocker)
153{
154 // Scene update has been signaled as completed. Depending on the state:
155 // - if Idle, Scheduled or InProgress, do nothing,
156 // - if InProgress, schedule a new update in case a pending update was marked,
157 // otherwise push the scene update state into Idle.
158
159 UNUSED_PARAM(stateLocker);
160
161 switch (m_state.update) {
162 case UpdateState::Idle:
163 case UpdateState::Scheduled:
164 return;
165 case UpdateState::InProgress:
166 if (m_state.pendingUpdate) {
167 m_state.pendingUpdate = false;
168 m_state.update = UpdateState::Scheduled;
169 if (!m_state.isSuspended)
170 m_updateTimer.startOneShot(0_s);
171 return;
172 }
173
174 m_state.update = UpdateState::Idle;
175 return;
176 }
177}
178
179void CompositingRunLoop::updateTimerFired()
180{
181 {
182 // Scene update is now in progress.
183 LockHolder locker(m_state.lock);
184 if (m_state.isSuspended)
185 return;
186 m_state.update = UpdateState::InProgress;
187 }
188 m_updateFunction();
189}
190
191} // namespace WebKit
192
193#endif // USE(COORDINATED_GRAPHICS)
194