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