-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathasyncrenderinternal.cpp
More file actions
203 lines (171 loc) · 4.5 KB
/
Copy pathasyncrenderinternal.cpp
File metadata and controls
203 lines (171 loc) · 4.5 KB
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
#include <QImage>
#include "geometry.h"
#include "asyncrenderthread.h"
#include "asyncrenderinternal.h"
using namespace AsyncRenderInternal;
void Job::deleteData()
{
delete result;
result = NULL;
delete geometry;
geometry = NULL;
delete data;
data = NULL;
}
Controller &Controller::controller()
{
static Controller global_controller;
return global_controller;
}
Controller::Controller() : quitThreads(false), freshId(1)
{
//create compute thread(s):
for (unsigned int i = 0; i < 1; ++i)
{
ComputeThread *thread = new ComputeThread(this);
thread->start();
threads.push_back(thread);
}
//create render threads:
for (unsigned int i = 0; i < 1; ++i)
{
RenderThread *thread = new RenderThread(this);
connect(thread, SIGNAL(jobFinished(Job*)), this, SLOT(jobFinished(Job*)));
thread->start();
threads.push_back(thread);
}
}
Controller::~Controller()
{
std::cout << "Asking render and compute threads to quit." << std::endl;
computeQueueLock.lock();
renderQueueLock.lock();
quitThreads = true;
renderQueueLock.unlock();
computeQueueLock.unlock();
computeQueueHasData.wakeAll();
renderQueueHasData.wakeAll();
for (vector< QThread * >::iterator t = threads.begin(); t != threads.end(); ++t)
{
(*t)->wait();
delete *t;
*t = NULL;
}
//Don't need to lock here, as all threads have stopped:
while (!computeQueue.empty())
{
delete computeQueue.front();
computeQueue.pop_front();
}
while (!renderQueue.empty())
{
delete renderQueue.front();
renderQueue.pop_front();
}
}
void Controller::registerWidget(AsyncRenderWidget *widget)
{
//widget gets a fresh id and gets added to our map:
assert(widget);
assert(widget->id == 0);
widget->id = freshId;
assert(widget->id != 0);
++freshId;
assert(!idToWidget.count(widget->id));
idToWidget.insert(make_pair(widget->id, widget));
}
void Controller::unregisterWidget(AsyncRenderWidget *widget)
{
//widget is deleted from our map:
assert(widget);
assert(widget->id != 0);
unordered_map< uint32_t, AsyncRenderWidget * >::iterator f = idToWidget.find(widget->id);
assert(f != idToWidget.end());
idToWidget.erase(f);
}
void replaceOrAddJob(deque< Job * > &queue, Job *job)
{
bool found = false;
for (deque< Job * >::iterator j = queue.begin(); j != queue.end(); ++j)
{
if ((*j)->requesterId == job->requesterId)
{
assert(!found);
found = true;
//There's already a job in the queue from the same requester, so take its place in line and clean up its data:
(*j)->deleteData();
delete *j;
*j = job;
}
}
if (!found)
queue.push_back(job);
}
void Controller::queue(AsyncRenderWidget *widget, Camera const &camera, RenderData *data)
{
assert(widget->id != 0);
Job *job = new Job(widget->id, camera, data);
computeQueueLock.lock();
replaceOrAddJob(computeQueue, job);
computeQueueLock.unlock();
computeQueueHasData.wakeOne();
}
void Controller::queue(AsyncRenderWidget *widget, Camera const &camera, Geometry *geometry)
{
Job *job = new Job(widget->id, camera, NULL, geometry);
renderQueueLock.lock();
replaceOrAddJob(renderQueue, job);
renderQueueLock.unlock();
renderQueueHasData.wakeOne();
}
void Controller::jobFinished(Job *job)
{
assert(job);
unordered_map< uint32_t, AsyncRenderWidget * >::iterator f = idToWidget.find(job->requesterId);
if (f == idToWidget.end())
job->deleteData();
else
{
f->second->renderFinished(job->camera, job->data, job->geometry, job->result);
job->data = NULL;
job->geometry = NULL;
job->result = NULL;
}
delete job;
}
//============================================
ComputeThread::ComputeThread(Controller *_controller) : controller(_controller)
{
assert(controller);
}
ComputeThread::~ComputeThread()
{
}
void ComputeThread::run()
{
controller->computeQueueLock.lock();
while (!controller->quitThreads)
{
if (controller->computeQueue.empty())
{
controller->computeQueueHasData.wait(&controller->computeQueueLock);
continue;
}
//pull job off the queue:
Job *job = controller->computeQueue.front();
controller->computeQueue.pop_front();
controller->computeQueueLock.unlock();
//Run geometry calculation:
assert(job->geometry == NULL);
assert(job->data);
job->geometry = job->data->getGeometry();
//Pass back to render queue:
controller->renderQueueLock.lock();
replaceOrAddJob(controller->renderQueue, job);
controller->renderQueueHasData.wakeOne();
controller->renderQueueLock.unlock();
controller->computeQueueLock.lock();
}
controller->computeQueueLock.unlock();
std::cout << "Quitting a compute thread." << std::endl;
}