/
busyrunner.cpp
295 lines (242 loc) · 7.99 KB
/
busyrunner.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
/** @file busyrunner.cpp Runs busy tasks in a background thread.
* @ingroup base
*
* @authors Copyright © 2007-2015 Jaakko Keränen <jaakko.keranen@iki.fi>
* @authors Copyright © 2007-2013 Daniel Swanson <danij@dengine.net>
* @authors Copyright © 2007 Jamie Jones <jamie_jones_au@yahoo.com.au>
*
* @par License
* GPL: http://www.gnu.org/licenses/gpl.html
*
* <small>This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by the
* Free Software Foundation; either version 2 of the License, or (at your
* option) any later version. This program is distributed in the hope that it
* will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty
* of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General
* Public License for more details. You should have received a copy of the GNU
* General Public License along with this program; if not, write to the Free
* Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
* 02110-1301 USA</small>
*/
#include "busyrunner.h"
#include "dd_main.h"
#include "dd_loop.h"
#include "sys_system.h"
#include "network/net_main.h"
#include "clientapp.h"
#include "gl/gl_defer.h"
#include "ui/busyvisual.h"
#include "ui/widgets/busywidget.h"
#include "ui/clientwindow.h"
#include "ui/progress.h"
#include <doomsday/doomsdayapp.h>
#include <de/concurrency.h>
#include <de/Log>
#include <QEventLoop>
static bool animatedTransitionActive(int busyMode)
{
return (!novideo && !isDedicated && !netGame && !(busyMode & BUSYF_STARTUP) &&
rTransitionTics > 0 && (busyMode & BUSYF_TRANSITION));
}
static BusyMode &busy()
{
return DoomsdayApp::app().busyMode();
}
DENG2_PIMPL_NOREF(BusyRunner)
, DENG2_OBSERVES(BusyMode, Beginning)
, DENG2_OBSERVES(BusyMode, End)
, DENG2_OBSERVES(BusyMode, TaskWillStart)
{
QEventLoop *eventLoop = nullptr;
thread_t busyThread = nullptr;
bool busyDone = false;
timespan_t busyTime = 0;
bool busyWillAnimateTransition = false;
bool busyWasIgnoringInput = false;
bool fadeFromBlack = false;
Impl()
{
busy().audienceForBeginning() += this;
busy().audienceForEnd() += this;
busy().audienceForTaskWillStart() += this;
}
~Impl()
{
busy().setTaskRunner(nullptr);
}
void busyModeWillBegin(BusyTask &firstTask)
{
if (auto *fader = ClientWindow::main().contentFade())
{
fader->cancel();
}
// Are we doing a transition effect?
busyWillAnimateTransition = animatedTransitionActive(firstTask.mode);
if (busyWillAnimateTransition)
{
Con_TransitionConfigure();
}
fadeFromBlack = (firstTask.mode & BUSYF_STARTUP) != 0;
busyWasIgnoringInput = ClientApp::inputSystem().ignoreEvents();
// Limit frame rate to 60, no point pushing it any faster while busy.
ClientApp::app().loop().setRate(60);
// Switch the window to busy mode UI.
ClientWindow::main().setMode(ClientWindow::Busy);
}
void busyModeEnded()
{
DD_ResetTimer();
// Discard input events so that any and all accumulated input events are ignored.
ClientApp::inputSystem().ignoreEvents(busyWasIgnoringInput);
// Back to unlimited frame rate.
ClientApp::app().loop().setRate(0);
// Switch the window to normal UI.
ClientWindow::main().setMode(ClientWindow::Normal);
}
void busyTaskWillStart(BusyTask &task)
{
// Is the worker updating its progress?
if (task.maxProgress > 0)
{
Con_InitProgress2(task.maxProgress, task.progressStart, task.progressEnd);
}
}
/**
* Exits the busy mode event loop. Called in the main thread, does not return
* until the worker thread is stopped.
*/
void exitEventLoop()
{
DENG_ASSERT_IN_MAIN_THREAD();
DENG_ASSERT(eventLoop);
busyDone = true;
// Make sure the worker finishes before we continue.
int result = Sys_WaitThread(busyThread, busy().endedWithError()? 100 : 5000, nullptr);
busyThread = nullptr;
eventLoop->exit(result);
if (fadeFromBlack)
{
ClientWindow::main().fadeContentFromBlack(2);
}
}
};
static BusyRunner &busyRunner()
{
return *static_cast<BusyRunner *>(busy().taskRunner());
}
BusyRunner::BusyRunner() : d(new Impl)
{
busy().setTaskRunner(this);
}
/**
* Callback that is called from the busy worker thread when it exists.
*
* @param status Exit status.
*/
static void busyWorkerTerminated(systhreadexitstatus_t status)
{
DENG_ASSERT(busy().isActive());
if (status == DENG_THREAD_STOPPED_WITH_EXCEPTION)
{
busy().abort("Uncaught exception from busy thread");
}
// This will tell the busy event loop to stop.
busyRunner().finishTask();
}
BusyRunner::Result BusyRunner::runTask(BusyTask *task)
{
// Let's get busy!
BusyVisual_PrepareResources();
de::ProgressWidget &prog = ClientWindow::main().busy().progress();
prog.show();
prog.setText(task->name);
prog.setMode(task->mode & BUSYF_ACTIVITY? de::ProgressWidget::Indefinite :
de::ProgressWidget::Ranged);
// Start the busy worker thread, which will process the task in the
// background while we keep the user occupied with nice animations.
d->busyThread = Sys_StartThread(task->worker, task->workerData);
Thread_SetCallback(d->busyThread, busyWorkerTerminated);
DENG_ASSERT(!d->eventLoop);
// Run a local event loop since the primary event loop is blocked while
// we're busy. This event loop is able to handle window and input events
// just like the primary loop.
d->busyDone = false;
d->eventLoop = new QEventLoop;
Result result(true, d->eventLoop->exec());
delete d->eventLoop;
d->eventLoop = nullptr;
ClientWindow::main().glActivate(); // after processing other events
// Teardown.
if (d->busyWillAnimateTransition)
{
Con_TransitionBegin();
}
// Make sure that any remaining deferred content gets uploaded.
if (!(task->mode & BUSYF_NO_UPLOADS))
{
GL_ProcessDeferredTasks(0);
}
return result;
}
bool BusyRunner::isTransitionAnimated() const
{
return d->busyWillAnimateTransition;
}
void BusyRunner::loop()
{
BusyTask *const busyTask = busy().currentTask();
if (!busyTask || !busy().isActive()) return;
bool const canUpload = !(busyTask->mode & BUSYF_NO_UPLOADS);
// Post and discard all input events.
ClientApp::inputSystem().processEvents(0);
ClientApp::inputSystem().processSharpEvents(0);
if (canUpload)
{
ClientWindow::main().glActivate();
// Any deferred content needs to get uploaded.
GL_ProcessDeferredTasks(15);
}
if (!d->busyDone ||
(canUpload && GL_DeferredTaskCount() > 0) ||
!Con_IsProgressAnimationCompleted())
{
// Let's keep running the busy loop.
return;
}
d->exitEventLoop();
}
void BusyRunner::finishTask()
{
// When BusyMode_Loop() is called, it will quit the event loop as soon as possible.
d->busyDone = true;
}
void BusyMode_Loop()
{
static_cast<BusyRunner *>(busy().taskRunner())->loop();
}
bool BusyRunner::isWorkerThread(uint threadId) const
{
if (!busy().isActive() || !d->busyThread) return false;
return (Sys_ThreadId(d->busyThread) == threadId);
}
bool BusyRunner::inWorkerThread() const
{
return isWorkerThread(Sys_CurrentThreadId());
}
#undef BusyMode_FreezeGameForBusyMode
void BusyMode_FreezeGameForBusyMode(void)
{
// This is only possible from the main thread.
if (ClientWindow::mainExists() &&
DoomsdayApp::app().busyMode().taskRunner() &&
de::App::inMainThread())
{
ClientWindow::main().busy().renderTransitionFrame();
}
}
DENG_DECLARE_API(Busy) =
{
{ DE_API_BUSY },
BusyMode_FreezeGameForBusyMode
};