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