]> git.proxmox.com Git - mirror_qemu.git/blame - ui/vnc-jobs.c
fuse: Implement hole detection through lseek
[mirror_qemu.git] / ui / vnc-jobs.c
CommitLineData
bd023f95
CC
1/*
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
e16f4c87 29#include "qemu/osdep.h"
bd023f95
CC
30#include "vnc.h"
31#include "vnc-jobs.h"
1de7afc9 32#include "qemu/sockets.h"
d9034011 33#include "qemu/main-loop.h"
a0b1a66e 34#include "block/aio.h"
bd023f95
CC
35
36/*
37 * Locking:
38 *
11f66978 39 * There are three levels of locking:
bd023f95
CC
40 * - jobs queue lock: for each operation on the queue (push, pop, isEmpty?)
41 * - VncDisplay global lock: mainly used for framebuffer updates to avoid
42 * screen corruption if the framebuffer is updated
11f66978 43 * while the worker is doing something.
bd023f95 44 * - VncState::output lock: used to make sure the output buffer is not corrupted
11f66978 45 * if two threads try to write on it at the same time
bd023f95 46 *
11f66978
PM
47 * While the VNC worker thread is working, the VncDisplay global lock is held
48 * to avoid screen corruption (this does not block vnc_refresh() because it
49 * uses trylock()) but the output lock is not held because the thread works on
bd023f95
CC
50 * its own output buffer.
51 * When the encoding job is done, the worker thread will hold the output lock
52 * and copy its output buffer in vs->output.
11f66978 53 */
bd023f95
CC
54
55struct VncJobQueue {
56 QemuCond cond;
57 QemuMutex mutex;
58 QemuThread thread;
bd023f95
CC
59 bool exit;
60 QTAILQ_HEAD(, VncJob) jobs;
61};
62
63typedef struct VncJobQueue VncJobQueue;
64
65/*
66 * We use a single global queue, but most of the functions are
11f66978 67 * already reentrant, so we can easily add more than one encoding thread
bd023f95
CC
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{
fedf0d35 83 VncJob *job = g_new0(VncJob, 1);
bd023f95 84
f31f9c10 85 assert(vs->magic == VNC_MAGIC);
bd023f95
CC
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{
fedf0d35 95 VncRectEntry *entry = g_new0(VncRectEntry, 1);
bd023f95
CC
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)) {
7267c094 112 g_free(job);
bd023f95
CC
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
bd023f95
CC
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);
175b2a6e
CC
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) {
04d2529d
DB
148 if (vs->ioc != NULL && buffer_empty(&vs->output)) {
149 if (vs->ioc_tag) {
150 g_source_remove(vs->ioc_tag);
151 }
d49b87f0
KK
152 if (vs->disconnecting == FALSE) {
153 vs->ioc_tag = qio_channel_add_watch(
2ddafce7
DH
154 vs->ioc, G_IO_IN | G_IO_HUP | G_IO_ERR | G_IO_OUT,
155 vnc_client_io, vs, NULL);
d49b87f0 156 }
d9034011
GH
157 }
158 buffer_move(&vs->output, &vs->jobs_buffer);
ada8d2e4
DB
159
160 if (vs->job_update == VNC_STATE_UPDATE_FORCE) {
161 vs->force_update_offset = vs->output.offset;
162 }
163 vs->job_update = VNC_STATE_UPDATE_NONE;
175b2a6e 164 }
04d2529d 165 flush = vs->ioc != NULL && vs->abort != true;
175b2a6e
CC
166 vnc_unlock_output(vs);
167
168 if (flush) {
169 vnc_flush(vs);
170 }
bd023f95
CC
171}
172
173/*
174 * Copy data for local use
175 */
176static void vnc_async_encoding_start(VncState *orig, VncState *local)
177{
2e0c90af 178 buffer_init(&local->output, "vnc-worker-output");
04d2529d
DB
179 local->sioc = NULL; /* Don't do any network work on this thread */
180 local->ioc = NULL; /* Don't do any network work on this thread */
2e0c90af 181
bd023f95
CC
182 local->vnc_encoding = orig->vnc_encoding;
183 local->features = orig->features;
bd023f95 184 local->vd = orig->vd;
7d964c9d 185 local->lossy_rect = orig->lossy_rect;
bd023f95 186 local->write_pixels = orig->write_pixels;
9f64916d
GH
187 local->client_pf = orig->client_pf;
188 local->client_be = orig->client_be;
bd023f95
CC
189 local->tight = orig->tight;
190 local->zlib = orig->zlib;
191 local->hextile = orig->hextile;
148954fa 192 local->zrle = orig->zrle;
bd023f95
CC
193}
194
195static void vnc_async_encoding_end(VncState *orig, VncState *local)
196{
0ae0b069 197 buffer_free(&local->output);
bd023f95
CC
198 orig->tight = local->tight;
199 orig->zlib = local->zlib;
200 orig->hextile = local->hextile;
148954fa 201 orig->zrle = local->zrle;
7d964c9d 202 orig->lossy_rect = local->lossy_rect;
bd023f95
CC
203}
204
205static int vnc_worker_thread_loop(VncJobQueue *queue)
206{
207 VncJob *job;
208 VncRectEntry *entry, *tmp;
2e0c90af 209 VncState vs = {};
bd023f95
CC
210 int n_rectangles;
211 int saved_offset;
bd023f95
CC
212
213 vnc_lock_queue(queue);
214 while (QTAILQ_EMPTY(&queue->jobs) && !queue->exit) {
215 qemu_cond_wait(&queue->cond, &queue->mutex);
216 }
217 /* Here job can only be NULL if queue->exit is true */
218 job = QTAILQ_FIRST(&queue->jobs);
219 vnc_unlock_queue(queue);
f31f9c10 220 assert(job->vs->magic == VNC_MAGIC);
bd023f95
CC
221
222 if (queue->exit) {
223 return -1;
224 }
225
226 vnc_lock_output(job->vs);
04d2529d 227 if (job->vs->ioc == NULL || job->vs->abort == true) {
175b2a6e 228 vnc_unlock_output(job->vs);
bd023f95
CC
229 goto disconnected;
230 }
c3d6899c
PL
231 if (buffer_empty(&job->vs->output)) {
232 /*
233 * Looks like a NOP as it obviously moves no data. But it
234 * moves the empty buffer, so we don't have to malloc a new
235 * one for vs.output
236 */
237 buffer_move_empty(&vs.output, &job->vs->output);
238 }
bd023f95
CC
239 vnc_unlock_output(job->vs);
240
241 /* Make a local copy of vs and switch output buffers */
242 vnc_async_encoding_start(job->vs, &vs);
f31f9c10 243 vs.magic = VNC_MAGIC;
bd023f95
CC
244
245 /* Start sending rectangles */
246 n_rectangles = 0;
247 vnc_write_u8(&vs, VNC_MSG_SERVER_FRAMEBUFFER_UPDATE);
248 vnc_write_u8(&vs, 0);
249 saved_offset = vs.output.offset;
250 vnc_write_u16(&vs, 0);
251
252 vnc_lock_display(job->vs->vd);
253 QLIST_FOREACH_SAFE(entry, &job->rectangles, next, tmp) {
254 int n;
255
04d2529d 256 if (job->vs->ioc == NULL) {
bd023f95 257 vnc_unlock_display(job->vs->vd);
e3c1adf1
GA
258 /* Copy persistent encoding data */
259 vnc_async_encoding_end(job->vs, &vs);
bd023f95
CC
260 goto disconnected;
261 }
262
263 n = vnc_send_framebuffer_update(&vs, entry->rect.x, entry->rect.y,
264 entry->rect.w, entry->rect.h);
265
266 if (n >= 0) {
267 n_rectangles += n;
268 }
7267c094 269 g_free(entry);
bd023f95
CC
270 }
271 vnc_unlock_display(job->vs->vd);
272
273 /* Put n_rectangles at the beginning of the message */
274 vs.output.buffer[saved_offset] = (n_rectangles >> 8) & 0xFF;
275 vs.output.buffer[saved_offset + 1] = n_rectangles & 0xFF;
276
bd023f95 277 vnc_lock_output(job->vs);
04d2529d 278 if (job->vs->ioc != NULL) {
d9034011 279 buffer_move(&job->vs->jobs_buffer, &vs.output);
175b2a6e
CC
280 /* Copy persistent encoding data */
281 vnc_async_encoding_end(job->vs, &vs);
282
0ae0b069 283 qemu_bh_schedule(job->vs->bh);
e3c1adf1 284 } else {
d9034011 285 buffer_reset(&vs.output);
e3c1adf1
GA
286 /* Copy persistent encoding data */
287 vnc_async_encoding_end(job->vs, &vs);
bd023f95 288 }
bd023f95
CC
289 vnc_unlock_output(job->vs);
290
175b2a6e 291disconnected:
bd023f95
CC
292 vnc_lock_queue(queue);
293 QTAILQ_REMOVE(&queue->jobs, job, next);
294 vnc_unlock_queue(queue);
295 qemu_cond_broadcast(&queue->cond);
7267c094 296 g_free(job);
f31f9c10 297 vs.magic = 0;
bd023f95
CC
298 return 0;
299}
300
301static VncJobQueue *vnc_queue_init(void)
302{
fedf0d35 303 VncJobQueue *queue = g_new0(VncJobQueue, 1);
bd023f95
CC
304
305 qemu_cond_init(&queue->cond);
306 qemu_mutex_init(&queue->mutex);
307 QTAILQ_INIT(&queue->jobs);
308 return queue;
309}
310
311static void vnc_queue_clear(VncJobQueue *q)
312{
313 qemu_cond_destroy(&queue->cond);
314 qemu_mutex_destroy(&queue->mutex);
7267c094 315 g_free(q);
bd023f95
CC
316 queue = NULL; /* Unset global queue */
317}
318
319static void *vnc_worker_thread(void *arg)
320{
321 VncJobQueue *queue = arg;
322
b7680cb6 323 qemu_thread_get_self(&queue->thread);
bd023f95
CC
324
325 while (!vnc_worker_thread_loop(queue)) ;
326 vnc_queue_clear(queue);
327 return NULL;
328}
329
71a8cdec
BS
330static bool vnc_worker_thread_running(void)
331{
332 return queue; /* Check global queue */
333}
334
bd023f95
CC
335void vnc_start_worker_thread(void)
336{
337 VncJobQueue *q;
338
339 if (vnc_worker_thread_running())
340 return ;
341
342 q = vnc_queue_init();
4900116e
DDAG
343 qemu_thread_create(&q->thread, "vnc_worker", vnc_worker_thread, q,
344 QEMU_THREAD_DETACHED);
bd023f95
CC
345 queue = q; /* Set global queue */
346}