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