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#include "qemu/osdep.h"
30#include "vnc.h"
31#include "vnc-jobs.h"
32#include "qemu/sockets.h"
33#include "qemu/main-loop.h"
34#include "block/aio.h"
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55struct VncJobQueue {
56 QemuCond cond;
57 QemuMutex mutex;
58 QemuThread thread;
59 bool exit;
60 QTAILQ_HEAD(, VncJob) jobs;
61};
62
63typedef struct VncJobQueue VncJobQueue;
64
65
66
67
68
69static VncJobQueue *queue;
70
71static void vnc_lock_queue(VncJobQueue *queue)
72{
73 qemu_mutex_lock(&queue->mutex);
74}
75
76static void vnc_unlock_queue(VncJobQueue *queue)
77{
78 qemu_mutex_unlock(&queue->mutex);
79}
80
81VncJob *vnc_job_new(VncState *vs)
82{
83 VncJob *job = g_new0(VncJob, 1);
84
85 job->vs = vs;
86 vnc_lock_queue(queue);
87 QLIST_INIT(&job->rectangles);
88 vnc_unlock_queue(queue);
89 return job;
90}
91
92int vnc_job_add_rect(VncJob *job, int x, int y, int w, int h)
93{
94 VncRectEntry *entry = g_new0(VncRectEntry, 1);
95
96 entry->rect.x = x;
97 entry->rect.y = y;
98 entry->rect.w = w;
99 entry->rect.h = h;
100
101 vnc_lock_queue(queue);
102 QLIST_INSERT_HEAD(&job->rectangles, entry, next);
103 vnc_unlock_queue(queue);
104 return 1;
105}
106
107void vnc_job_push(VncJob *job)
108{
109 vnc_lock_queue(queue);
110 if (queue->exit || QLIST_EMPTY(&job->rectangles)) {
111 g_free(job);
112 } else {
113 QTAILQ_INSERT_TAIL(&queue->jobs, job, next);
114 qemu_cond_broadcast(&queue->cond);
115 }
116 vnc_unlock_queue(queue);
117}
118
119static bool vnc_has_job_locked(VncState *vs)
120{
121 VncJob *job;
122
123 QTAILQ_FOREACH(job, &queue->jobs, next) {
124 if (job->vs == vs || !vs) {
125 return true;
126 }
127 }
128 return false;
129}
130
131bool vnc_has_job(VncState *vs)
132{
133 bool ret;
134
135 vnc_lock_queue(queue);
136 ret = vnc_has_job_locked(vs);
137 vnc_unlock_queue(queue);
138 return ret;
139}
140
141void vnc_jobs_clear(VncState *vs)
142{
143 VncJob *job, *tmp;
144
145 vnc_lock_queue(queue);
146 QTAILQ_FOREACH_SAFE(job, &queue->jobs, next, tmp) {
147 if (job->vs == vs || !vs) {
148 QTAILQ_REMOVE(&queue->jobs, job, next);
149 }
150 }
151 vnc_unlock_queue(queue);
152}
153
154void vnc_jobs_join(VncState *vs)
155{
156 vnc_lock_queue(queue);
157 while (vnc_has_job_locked(vs)) {
158 qemu_cond_wait(&queue->cond, &queue->mutex);
159 }
160 vnc_unlock_queue(queue);
161 vnc_jobs_consume_buffer(vs);
162}
163
164void vnc_jobs_consume_buffer(VncState *vs)
165{
166 bool flush;
167
168 vnc_lock_output(vs);
169 if (vs->jobs_buffer.offset) {
170 if (vs->ioc != NULL && buffer_empty(&vs->output)) {
171 if (vs->ioc_tag) {
172 g_source_remove(vs->ioc_tag);
173 }
174 vs->ioc_tag = qio_channel_add_watch(
175 vs->ioc, G_IO_IN | G_IO_OUT, vnc_client_io, vs, NULL);
176 }
177 buffer_move(&vs->output, &vs->jobs_buffer);
178 }
179 flush = vs->ioc != NULL && vs->abort != true;
180 vnc_unlock_output(vs);
181
182 if (flush) {
183 vnc_flush(vs);
184 }
185}
186
187
188
189
190static void vnc_async_encoding_start(VncState *orig, VncState *local)
191{
192 buffer_init(&local->output, "vnc-worker-output");
193 local->sioc = NULL;
194 local->ioc = NULL;
195
196 local->vnc_encoding = orig->vnc_encoding;
197 local->features = orig->features;
198 local->vd = orig->vd;
199 local->lossy_rect = orig->lossy_rect;
200 local->write_pixels = orig->write_pixels;
201 local->client_pf = orig->client_pf;
202 local->client_be = orig->client_be;
203 local->tight = orig->tight;
204 local->zlib = orig->zlib;
205 local->hextile = orig->hextile;
206 local->zrle = orig->zrle;
207}
208
209static void vnc_async_encoding_end(VncState *orig, VncState *local)
210{
211 orig->tight = local->tight;
212 orig->zlib = local->zlib;
213 orig->hextile = local->hextile;
214 orig->zrle = local->zrle;
215 orig->lossy_rect = local->lossy_rect;
216}
217
218static int vnc_worker_thread_loop(VncJobQueue *queue)
219{
220 VncJob *job;
221 VncRectEntry *entry, *tmp;
222 VncState vs = {};
223 int n_rectangles;
224 int saved_offset;
225
226 vnc_lock_queue(queue);
227 while (QTAILQ_EMPTY(&queue->jobs) && !queue->exit) {
228 qemu_cond_wait(&queue->cond, &queue->mutex);
229 }
230
231 job = QTAILQ_FIRST(&queue->jobs);
232 vnc_unlock_queue(queue);
233
234 if (queue->exit) {
235 return -1;
236 }
237
238 vnc_lock_output(job->vs);
239 if (job->vs->ioc == NULL || job->vs->abort == true) {
240 vnc_unlock_output(job->vs);
241 goto disconnected;
242 }
243 if (buffer_empty(&job->vs->output)) {
244
245
246
247
248
249 buffer_move_empty(&vs.output, &job->vs->output);
250 }
251 vnc_unlock_output(job->vs);
252
253
254 vnc_async_encoding_start(job->vs, &vs);
255
256
257 n_rectangles = 0;
258 vnc_write_u8(&vs, VNC_MSG_SERVER_FRAMEBUFFER_UPDATE);
259 vnc_write_u8(&vs, 0);
260 saved_offset = vs.output.offset;
261 vnc_write_u16(&vs, 0);
262
263 vnc_lock_display(job->vs->vd);
264 QLIST_FOREACH_SAFE(entry, &job->rectangles, next, tmp) {
265 int n;
266
267 if (job->vs->ioc == NULL) {
268 vnc_unlock_display(job->vs->vd);
269
270 vnc_async_encoding_end(job->vs, &vs);
271 goto disconnected;
272 }
273
274 n = vnc_send_framebuffer_update(&vs, entry->rect.x, entry->rect.y,
275 entry->rect.w, entry->rect.h);
276
277 if (n >= 0) {
278 n_rectangles += n;
279 }
280 g_free(entry);
281 }
282 vnc_unlock_display(job->vs->vd);
283
284
285 vs.output.buffer[saved_offset] = (n_rectangles >> 8) & 0xFF;
286 vs.output.buffer[saved_offset + 1] = n_rectangles & 0xFF;
287
288 vnc_lock_output(job->vs);
289 if (job->vs->ioc != NULL) {
290 buffer_move(&job->vs->jobs_buffer, &vs.output);
291
292 vnc_async_encoding_end(job->vs, &vs);
293
294 qemu_bh_schedule(job->vs->bh);
295 } else {
296 buffer_reset(&vs.output);
297
298 vnc_async_encoding_end(job->vs, &vs);
299 }
300 vnc_unlock_output(job->vs);
301
302disconnected:
303 vnc_lock_queue(queue);
304 QTAILQ_REMOVE(&queue->jobs, job, next);
305 vnc_unlock_queue(queue);
306 qemu_cond_broadcast(&queue->cond);
307 g_free(job);
308 return 0;
309}
310
311static VncJobQueue *vnc_queue_init(void)
312{
313 VncJobQueue *queue = g_new0(VncJobQueue, 1);
314
315 qemu_cond_init(&queue->cond);
316 qemu_mutex_init(&queue->mutex);
317 QTAILQ_INIT(&queue->jobs);
318 return queue;
319}
320
321static void vnc_queue_clear(VncJobQueue *q)
322{
323 qemu_cond_destroy(&queue->cond);
324 qemu_mutex_destroy(&queue->mutex);
325 g_free(q);
326 queue = NULL;
327}
328
329static void *vnc_worker_thread(void *arg)
330{
331 VncJobQueue *queue = arg;
332
333 qemu_thread_get_self(&queue->thread);
334
335 while (!vnc_worker_thread_loop(queue)) ;
336 vnc_queue_clear(queue);
337 return NULL;
338}
339
340static bool vnc_worker_thread_running(void)
341{
342 return queue;
343}
344
345void vnc_start_worker_thread(void)
346{
347 VncJobQueue *q;
348
349 if (vnc_worker_thread_running())
350 return ;
351
352 q = vnc_queue_init();
353 qemu_thread_create(&q->thread, "vnc_worker", vnc_worker_thread, q,
354 QEMU_THREAD_DETACHED);
355 queue = q;
356}
357