blob: 546635aabe587a88975ba3fd3c4c14141130ebab [file] [log] [blame]
Corentin Charybd023f92010-07-07 20:58:02 +02001/*
2 * QEMU VNC display driver
3 *
4 * Copyright (C) 2006 Anthony Liguori <anthony@codemonkey.ws>
5 * Copyright (C) 2006 Fabrice Bellard
6 * Copyright (C) 2009 Red Hat, Inc
7 * Copyright (C) 2010 Corentin Chary <corentin.chary@gmail.com>
8 *
9 * Permission is hereby granted, free of charge, to any person obtaining a copy
10 * of this software and associated documentation files (the "Software"), to deal
11 * in the Software without restriction, including without limitation the rights
12 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
13 * copies of the Software, and to permit persons to whom the Software is
14 * furnished to do so, subject to the following conditions:
15 *
16 * The above copyright notice and this permission notice shall be included in
17 * all copies or substantial portions of the Software.
18 *
19 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
20 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
21 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
22 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
23 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
24 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
25 * THE SOFTWARE.
26 */
27
28
29#include "vnc.h"
30#include "vnc-jobs.h"
Paolo Bonzini1de7afc2012-12-17 18:20:00 +010031#include "qemu/sockets.h"
Gerd Hoffmannd9034012015-10-30 12:10:04 +010032#include "qemu/main-loop.h"
Markus Armbrustera0b1a662015-03-17 18:16:21 +010033#include "block/aio.h"
Corentin Charybd023f92010-07-07 20:58:02 +020034
35/*
36 * Locking:
37 *
Peter Maydell11f66972012-10-18 17:40:53 +010038 * There are three levels of locking:
Corentin Charybd023f92010-07-07 20:58:02 +020039 * - jobs queue lock: for each operation on the queue (push, pop, isEmpty?)
40 * - VncDisplay global lock: mainly used for framebuffer updates to avoid
41 * screen corruption if the framebuffer is updated
Peter Maydell11f66972012-10-18 17:40:53 +010042 * while the worker is doing something.
Corentin Charybd023f92010-07-07 20:58:02 +020043 * - VncState::output lock: used to make sure the output buffer is not corrupted
Peter Maydell11f66972012-10-18 17:40:53 +010044 * if two threads try to write on it at the same time
Corentin Charybd023f92010-07-07 20:58:02 +020045 *
Peter Maydell11f66972012-10-18 17:40:53 +010046 * While the VNC worker thread is working, the VncDisplay global lock is held
47 * to avoid screen corruption (this does not block vnc_refresh() because it
48 * uses trylock()) but the output lock is not held because the thread works on
Corentin Charybd023f92010-07-07 20:58:02 +020049 * its own output buffer.
50 * When the encoding job is done, the worker thread will hold the output lock
51 * and copy its output buffer in vs->output.
Peter Maydell11f66972012-10-18 17:40:53 +010052 */
Corentin Charybd023f92010-07-07 20:58:02 +020053
54struct VncJobQueue {
55 QemuCond cond;
56 QemuMutex mutex;
57 QemuThread thread;
Corentin Charybd023f92010-07-07 20:58:02 +020058 bool exit;
59 QTAILQ_HEAD(, VncJob) jobs;
60};
61
62typedef struct VncJobQueue VncJobQueue;
63
64/*
65 * We use a single global queue, but most of the functions are
Peter Maydell11f66972012-10-18 17:40:53 +010066 * already reentrant, so we can easily add more than one encoding thread
Corentin Charybd023f92010-07-07 20:58:02 +020067 */
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{
Markus Armbrusterfedf0d32015-11-03 17:12:03 +010082 VncJob *job = g_new0(VncJob, 1);
Corentin Charybd023f92010-07-07 20:58:02 +020083
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{
Markus Armbrusterfedf0d32015-11-03 17:12:03 +010093 VncRectEntry *entry = g_new0(VncRectEntry, 1);
Corentin Charybd023f92010-07-07 20:58:02 +020094
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)) {
Anthony Liguori7267c092011-08-20 22:09:37 -0500110 g_free(job);
Corentin Charybd023f92010-07-07 20:58:02 +0200111 } 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);
Corentin Chary175b2a62012-03-14 07:58:47 +0100160 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) {
Daniel P. Berrange04d25292015-02-27 16:20:57 +0000169 if (vs->ioc != NULL && buffer_empty(&vs->output)) {
170 if (vs->ioc_tag) {
171 g_source_remove(vs->ioc_tag);
172 }
173 vs->ioc_tag = qio_channel_add_watch(
174 vs->ioc, G_IO_IN | G_IO_OUT, vnc_client_io, vs, NULL);
Gerd Hoffmannd9034012015-10-30 12:10:04 +0100175 }
176 buffer_move(&vs->output, &vs->jobs_buffer);
Corentin Chary175b2a62012-03-14 07:58:47 +0100177 }
Daniel P. Berrange04d25292015-02-27 16:20:57 +0000178 flush = vs->ioc != NULL && vs->abort != true;
Corentin Chary175b2a62012-03-14 07:58:47 +0100179 vnc_unlock_output(vs);
180
181 if (flush) {
182 vnc_flush(vs);
183 }
Corentin Charybd023f92010-07-07 20:58:02 +0200184}
185
186/*
187 * Copy data for local use
188 */
189static void vnc_async_encoding_start(VncState *orig, VncState *local)
190{
Gerd Hoffmann2e0c90a2015-10-30 12:10:10 +0100191 buffer_init(&local->output, "vnc-worker-output");
Daniel P. Berrange04d25292015-02-27 16:20:57 +0000192 local->sioc = NULL; /* Don't do any network work on this thread */
193 local->ioc = NULL; /* Don't do any network work on this thread */
Gerd Hoffmann2e0c90a2015-10-30 12:10:10 +0100194
Corentin Charybd023f92010-07-07 20:58:02 +0200195 local->vnc_encoding = orig->vnc_encoding;
196 local->features = orig->features;
Corentin Charybd023f92010-07-07 20:58:02 +0200197 local->vd = orig->vd;
Corentin Chary7d964c92011-02-04 09:05:56 +0100198 local->lossy_rect = orig->lossy_rect;
Corentin Charybd023f92010-07-07 20:58:02 +0200199 local->write_pixels = orig->write_pixels;
Gerd Hoffmann9f649162012-10-10 13:29:43 +0200200 local->client_pf = orig->client_pf;
201 local->client_be = orig->client_be;
Corentin Charybd023f92010-07-07 20:58:02 +0200202 local->tight = orig->tight;
203 local->zlib = orig->zlib;
204 local->hextile = orig->hextile;
Corentin Chary148954f2011-02-04 09:06:01 +0100205 local->zrle = orig->zrle;
Corentin Charybd023f92010-07-07 20:58:02 +0200206}
207
208static void vnc_async_encoding_end(VncState *orig, VncState *local)
209{
210 orig->tight = local->tight;
211 orig->zlib = local->zlib;
212 orig->hextile = local->hextile;
Corentin Chary148954f2011-02-04 09:06:01 +0100213 orig->zrle = local->zrle;
Corentin Chary7d964c92011-02-04 09:05:56 +0100214 orig->lossy_rect = local->lossy_rect;
Corentin Charybd023f92010-07-07 20:58:02 +0200215}
216
217static int vnc_worker_thread_loop(VncJobQueue *queue)
218{
219 VncJob *job;
220 VncRectEntry *entry, *tmp;
Gerd Hoffmann2e0c90a2015-10-30 12:10:10 +0100221 VncState vs = {};
Corentin Charybd023f92010-07-07 20:58:02 +0200222 int n_rectangles;
223 int saved_offset;
Corentin Charybd023f92010-07-07 20:58:02 +0200224
225 vnc_lock_queue(queue);
226 while (QTAILQ_EMPTY(&queue->jobs) && !queue->exit) {
227 qemu_cond_wait(&queue->cond, &queue->mutex);
228 }
229 /* Here job can only be NULL if queue->exit is true */
230 job = QTAILQ_FIRST(&queue->jobs);
231 vnc_unlock_queue(queue);
232
233 if (queue->exit) {
234 return -1;
235 }
236
237 vnc_lock_output(job->vs);
Daniel P. Berrange04d25292015-02-27 16:20:57 +0000238 if (job->vs->ioc == NULL || job->vs->abort == true) {
Corentin Chary175b2a62012-03-14 07:58:47 +0100239 vnc_unlock_output(job->vs);
Corentin Charybd023f92010-07-07 20:58:02 +0200240 goto disconnected;
241 }
Peter Lievenc3d68992015-10-30 12:10:11 +0100242 if (buffer_empty(&job->vs->output)) {
243 /*
244 * Looks like a NOP as it obviously moves no data. But it
245 * moves the empty buffer, so we don't have to malloc a new
246 * one for vs.output
247 */
248 buffer_move_empty(&vs.output, &job->vs->output);
249 }
Corentin Charybd023f92010-07-07 20:58:02 +0200250 vnc_unlock_output(job->vs);
251
252 /* Make a local copy of vs and switch output buffers */
253 vnc_async_encoding_start(job->vs, &vs);
254
255 /* Start sending rectangles */
256 n_rectangles = 0;
257 vnc_write_u8(&vs, VNC_MSG_SERVER_FRAMEBUFFER_UPDATE);
258 vnc_write_u8(&vs, 0);
259 saved_offset = vs.output.offset;
260 vnc_write_u16(&vs, 0);
261
262 vnc_lock_display(job->vs->vd);
263 QLIST_FOREACH_SAFE(entry, &job->rectangles, next, tmp) {
264 int n;
265
Daniel P. Berrange04d25292015-02-27 16:20:57 +0000266 if (job->vs->ioc == NULL) {
Corentin Charybd023f92010-07-07 20:58:02 +0200267 vnc_unlock_display(job->vs->vd);
Gonglei (Arei)e3c1adf2014-01-23 13:30:57 +0000268 /* Copy persistent encoding data */
269 vnc_async_encoding_end(job->vs, &vs);
Corentin Charybd023f92010-07-07 20:58:02 +0200270 goto disconnected;
271 }
272
273 n = vnc_send_framebuffer_update(&vs, entry->rect.x, entry->rect.y,
274 entry->rect.w, entry->rect.h);
275
276 if (n >= 0) {
277 n_rectangles += n;
278 }
Anthony Liguori7267c092011-08-20 22:09:37 -0500279 g_free(entry);
Corentin Charybd023f92010-07-07 20:58:02 +0200280 }
281 vnc_unlock_display(job->vs->vd);
282
283 /* Put n_rectangles at the beginning of the message */
284 vs.output.buffer[saved_offset] = (n_rectangles >> 8) & 0xFF;
285 vs.output.buffer[saved_offset + 1] = n_rectangles & 0xFF;
286
Corentin Charybd023f92010-07-07 20:58:02 +0200287 vnc_lock_output(job->vs);
Daniel P. Berrange04d25292015-02-27 16:20:57 +0000288 if (job->vs->ioc != NULL) {
Gerd Hoffmannd9034012015-10-30 12:10:04 +0100289 buffer_move(&job->vs->jobs_buffer, &vs.output);
Corentin Chary175b2a62012-03-14 07:58:47 +0100290 /* Copy persistent encoding data */
291 vnc_async_encoding_end(job->vs, &vs);
292
293 qemu_bh_schedule(job->vs->bh);
Gonglei (Arei)e3c1adf2014-01-23 13:30:57 +0000294 } else {
Gerd Hoffmannd9034012015-10-30 12:10:04 +0100295 buffer_reset(&vs.output);
Gonglei (Arei)e3c1adf2014-01-23 13:30:57 +0000296 /* Copy persistent encoding data */
297 vnc_async_encoding_end(job->vs, &vs);
Corentin Charybd023f92010-07-07 20:58:02 +0200298 }
Corentin Charybd023f92010-07-07 20:58:02 +0200299 vnc_unlock_output(job->vs);
300
Corentin Chary175b2a62012-03-14 07:58:47 +0100301disconnected:
Corentin Charybd023f92010-07-07 20:58:02 +0200302 vnc_lock_queue(queue);
303 QTAILQ_REMOVE(&queue->jobs, job, next);
304 vnc_unlock_queue(queue);
305 qemu_cond_broadcast(&queue->cond);
Anthony Liguori7267c092011-08-20 22:09:37 -0500306 g_free(job);
Corentin Charybd023f92010-07-07 20:58:02 +0200307 return 0;
308}
309
310static VncJobQueue *vnc_queue_init(void)
311{
Markus Armbrusterfedf0d32015-11-03 17:12:03 +0100312 VncJobQueue *queue = g_new0(VncJobQueue, 1);
Corentin Charybd023f92010-07-07 20:58:02 +0200313
314 qemu_cond_init(&queue->cond);
315 qemu_mutex_init(&queue->mutex);
316 QTAILQ_INIT(&queue->jobs);
317 return queue;
318}
319
320static void vnc_queue_clear(VncJobQueue *q)
321{
322 qemu_cond_destroy(&queue->cond);
323 qemu_mutex_destroy(&queue->mutex);
Anthony Liguori7267c092011-08-20 22:09:37 -0500324 g_free(q);
Corentin Charybd023f92010-07-07 20:58:02 +0200325 queue = NULL; /* Unset global queue */
326}
327
328static void *vnc_worker_thread(void *arg)
329{
330 VncJobQueue *queue = arg;
331
Jan Kiszkab7680cb2011-03-12 17:43:51 +0100332 qemu_thread_get_self(&queue->thread);
Corentin Charybd023f92010-07-07 20:58:02 +0200333
334 while (!vnc_worker_thread_loop(queue)) ;
335 vnc_queue_clear(queue);
336 return NULL;
337}
338
Blue Swirl71a8cde2012-10-28 11:04:48 +0000339static bool vnc_worker_thread_running(void)
340{
341 return queue; /* Check global queue */
342}
343
Corentin Charybd023f92010-07-07 20:58:02 +0200344void vnc_start_worker_thread(void)
345{
346 VncJobQueue *q;
347
348 if (vnc_worker_thread_running())
349 return ;
350
351 q = vnc_queue_init();
Dr. David Alan Gilbert49001162014-01-30 10:20:32 +0000352 qemu_thread_create(&q->thread, "vnc_worker", vnc_worker_thread, q,
353 QEMU_THREAD_DETACHED);
Corentin Charybd023f92010-07-07 20:58:02 +0200354 queue = q; /* Set global queue */
355}