]> git.proxmox.com Git - mirror_qemu.git/blame - ui/vnc-jobs.c
vnc: only alloc server surface with clients connected
[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
29#include "vnc.h"
30#include "vnc-jobs.h"
1de7afc9 31#include "qemu/sockets.h"
d9034011 32#include "qemu/main-loop.h"
a0b1a66e 33#include "block/aio.h"
bd023f95
CC
34
35/*
36 * Locking:
37 *
11f66978 38 * There are three levels of locking:
bd023f95
CC
39 * - 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
11f66978 42 * while the worker is doing something.
bd023f95 43 * - VncState::output lock: used to make sure the output buffer is not corrupted
11f66978 44 * if two threads try to write on it at the same time
bd023f95 45 *
11f66978
PM
46 * 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
bd023f95
CC
49 * 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.
11f66978 52 */
bd023f95
CC
53
54struct VncJobQueue {
55 QemuCond cond;
56 QemuMutex mutex;
57 QemuThread thread;
bd023f95
CC
58 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
11f66978 66 * already reentrant, so we can easily add more than one encoding thread
bd023f95
CC
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{
7267c094 82 VncJob *job = g_malloc0(sizeof(VncJob));
bd023f95
CC
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{
7267c094 93 VncRectEntry *entry = g_malloc0(sizeof(VncRectEntry));
bd023f95
CC
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)) {
7267c094 110 g_free(job);
bd023f95
CC
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);
175b2a6e
CC
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) {
d9034011
GH
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);
175b2a6e
CC
174 }
175 flush = vs->csock != -1 && vs->abort != true;
176 vnc_unlock_output(vs);
177
178 if (flush) {
179 vnc_flush(vs);
180 }
bd023f95
CC
181}
182
183/*
184 * Copy data for local use
185 */
186static void vnc_async_encoding_start(VncState *orig, VncState *local)
187{
188 local->vnc_encoding = orig->vnc_encoding;
189 local->features = orig->features;
bd023f95 190 local->vd = orig->vd;
7d964c9d 191 local->lossy_rect = orig->lossy_rect;
bd023f95 192 local->write_pixels = orig->write_pixels;
9f64916d
GH
193 local->client_pf = orig->client_pf;
194 local->client_be = orig->client_be;
bd023f95
CC
195 local->tight = orig->tight;
196 local->zlib = orig->zlib;
197 local->hextile = orig->hextile;
148954fa 198 local->zrle = orig->zrle;
bd023f95 199 local->csock = -1; /* Don't do any network work on this thread */
bd023f95
CC
200}
201
202static void vnc_async_encoding_end(VncState *orig, VncState *local)
203{
204 orig->tight = local->tight;
205 orig->zlib = local->zlib;
206 orig->hextile = local->hextile;
148954fa 207 orig->zrle = local->zrle;
7d964c9d 208 orig->lossy_rect = local->lossy_rect;
bd023f95
CC
209}
210
211static int vnc_worker_thread_loop(VncJobQueue *queue)
212{
213 VncJob *job;
214 VncRectEntry *entry, *tmp;
215 VncState vs;
216 int n_rectangles;
217 int saved_offset;
bd023f95 218
543b9580
GH
219 buffer_init(&vs.output, "vnc-worker-output");
220
bd023f95
CC
221 vnc_lock_queue(queue);
222 while (QTAILQ_EMPTY(&queue->jobs) && !queue->exit) {
223 qemu_cond_wait(&queue->cond, &queue->mutex);
224 }
225 /* Here job can only be NULL if queue->exit is true */
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) {
175b2a6e 235 vnc_unlock_output(job->vs);
bd023f95
CC
236 goto disconnected;
237 }
238 vnc_unlock_output(job->vs);
239
240 /* Make a local copy of vs and switch output buffers */
241 vnc_async_encoding_start(job->vs, &vs);
242
243 /* Start sending rectangles */
244 n_rectangles = 0;
245 vnc_write_u8(&vs, VNC_MSG_SERVER_FRAMEBUFFER_UPDATE);
246 vnc_write_u8(&vs, 0);
247 saved_offset = vs.output.offset;
248 vnc_write_u16(&vs, 0);
249
250 vnc_lock_display(job->vs->vd);
251 QLIST_FOREACH_SAFE(entry, &job->rectangles, next, tmp) {
252 int n;
253
254 if (job->vs->csock == -1) {
255 vnc_unlock_display(job->vs->vd);
e3c1adf1
GA
256 /* Copy persistent encoding data */
257 vnc_async_encoding_end(job->vs, &vs);
bd023f95
CC
258 goto disconnected;
259 }
260
261 n = vnc_send_framebuffer_update(&vs, entry->rect.x, entry->rect.y,
262 entry->rect.w, entry->rect.h);
263
264 if (n >= 0) {
265 n_rectangles += n;
266 }
7267c094 267 g_free(entry);
bd023f95
CC
268 }
269 vnc_unlock_display(job->vs->vd);
270
271 /* Put n_rectangles at the beginning of the message */
272 vs.output.buffer[saved_offset] = (n_rectangles >> 8) & 0xFF;
273 vs.output.buffer[saved_offset + 1] = n_rectangles & 0xFF;
274
bd023f95 275 vnc_lock_output(job->vs);
175b2a6e 276 if (job->vs->csock != -1) {
d9034011 277 buffer_move(&job->vs->jobs_buffer, &vs.output);
175b2a6e
CC
278 /* Copy persistent encoding data */
279 vnc_async_encoding_end(job->vs, &vs);
280
281 qemu_bh_schedule(job->vs->bh);
e3c1adf1 282 } else {
d9034011 283 buffer_reset(&vs.output);
e3c1adf1
GA
284 /* Copy persistent encoding data */
285 vnc_async_encoding_end(job->vs, &vs);
bd023f95 286 }
bd023f95
CC
287 vnc_unlock_output(job->vs);
288
175b2a6e 289disconnected:
bd023f95
CC
290 vnc_lock_queue(queue);
291 QTAILQ_REMOVE(&queue->jobs, job, next);
292 vnc_unlock_queue(queue);
293 qemu_cond_broadcast(&queue->cond);
7267c094 294 g_free(job);
bd023f95
CC
295 return 0;
296}
297
298static VncJobQueue *vnc_queue_init(void)
299{
7267c094 300 VncJobQueue *queue = g_malloc0(sizeof(VncJobQueue));
bd023f95
CC
301
302 qemu_cond_init(&queue->cond);
303 qemu_mutex_init(&queue->mutex);
304 QTAILQ_INIT(&queue->jobs);
305 return queue;
306}
307
308static void vnc_queue_clear(VncJobQueue *q)
309{
310 qemu_cond_destroy(&queue->cond);
311 qemu_mutex_destroy(&queue->mutex);
7267c094 312 g_free(q);
bd023f95
CC
313 queue = NULL; /* Unset global queue */
314}
315
316static void *vnc_worker_thread(void *arg)
317{
318 VncJobQueue *queue = arg;
319
b7680cb6 320 qemu_thread_get_self(&queue->thread);
bd023f95
CC
321
322 while (!vnc_worker_thread_loop(queue)) ;
323 vnc_queue_clear(queue);
324 return NULL;
325}
326
71a8cdec
BS
327static bool vnc_worker_thread_running(void)
328{
329 return queue; /* Check global queue */
330}
331
bd023f95
CC
332void vnc_start_worker_thread(void)
333{
334 VncJobQueue *q;
335
336 if (vnc_worker_thread_running())
337 return ;
338
339 q = vnc_queue_init();
4900116e
DDAG
340 qemu_thread_create(&q->thread, "vnc_worker", vnc_worker_thread, q,
341 QEMU_THREAD_DETACHED);
bd023f95
CC
342 queue = q; /* Set global queue */
343}