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