2 * QEMU VNC display driver
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>
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:
16 * The above copyright notice and this permission notice shall be included in
17 * all copies or substantial portions of the Software.
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
29 #include "qemu/osdep.h"
32 #include "qemu/sockets.h"
33 #include "qemu/main-loop.h"
34 #include "block/aio.h"
39 * There are three levels of locking:
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
43 * while the worker is doing something.
44 * - VncState::output lock: used to make sure the output buffer is not corrupted
45 * if two threads try to write on it at the same time
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
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.
60 QTAILQ_HEAD(, VncJob
) jobs
;
63 typedef struct VncJobQueue VncJobQueue
;
66 * We use a single global queue, but most of the functions are
67 * already reentrant, so we can easily add more than one encoding thread
69 static VncJobQueue
*queue
;
71 static void vnc_lock_queue(VncJobQueue
*queue
)
73 qemu_mutex_lock(&queue
->mutex
);
76 static void vnc_unlock_queue(VncJobQueue
*queue
)
78 qemu_mutex_unlock(&queue
->mutex
);
81 VncJob
*vnc_job_new(VncState
*vs
)
83 VncJob
*job
= g_new0(VncJob
, 1);
86 vnc_lock_queue(queue
);
87 QLIST_INIT(&job
->rectangles
);
88 vnc_unlock_queue(queue
);
92 int vnc_job_add_rect(VncJob
*job
, int x
, int y
, int w
, int h
)
94 VncRectEntry
*entry
= g_new0(VncRectEntry
, 1);
101 vnc_lock_queue(queue
);
102 QLIST_INSERT_HEAD(&job
->rectangles
, entry
, next
);
103 vnc_unlock_queue(queue
);
107 void vnc_job_push(VncJob
*job
)
109 vnc_lock_queue(queue
);
110 if (queue
->exit
|| QLIST_EMPTY(&job
->rectangles
)) {
113 QTAILQ_INSERT_TAIL(&queue
->jobs
, job
, next
);
114 qemu_cond_broadcast(&queue
->cond
);
116 vnc_unlock_queue(queue
);
119 static bool vnc_has_job_locked(VncState
*vs
)
123 QTAILQ_FOREACH(job
, &queue
->jobs
, next
) {
124 if (job
->vs
== vs
|| !vs
) {
131 void vnc_jobs_join(VncState
*vs
)
133 vnc_lock_queue(queue
);
134 while (vnc_has_job_locked(vs
)) {
135 qemu_cond_wait(&queue
->cond
, &queue
->mutex
);
137 vnc_unlock_queue(queue
);
138 vnc_jobs_consume_buffer(vs
);
141 void vnc_jobs_consume_buffer(VncState
*vs
)
146 if (vs
->jobs_buffer
.offset
) {
147 if (vs
->ioc
!= NULL
&& buffer_empty(&vs
->output
)) {
149 g_source_remove(vs
->ioc_tag
);
151 if (vs
->disconnecting
== FALSE
) {
152 vs
->ioc_tag
= qio_channel_add_watch(
153 vs
->ioc
, G_IO_IN
| G_IO_OUT
, vnc_client_io
, vs
, NULL
);
156 buffer_move(&vs
->output
, &vs
->jobs_buffer
);
158 if (vs
->job_update
== VNC_STATE_UPDATE_FORCE
) {
159 vs
->force_update_offset
= vs
->output
.offset
;
161 vs
->job_update
= VNC_STATE_UPDATE_NONE
;
163 flush
= vs
->ioc
!= NULL
&& vs
->abort
!= true;
164 vnc_unlock_output(vs
);
172 * Copy data for local use
174 static void vnc_async_encoding_start(VncState
*orig
, VncState
*local
)
176 buffer_init(&local
->output
, "vnc-worker-output");
177 local
->sioc
= NULL
; /* Don't do any network work on this thread */
178 local
->ioc
= NULL
; /* Don't do any network work on this thread */
180 local
->vnc_encoding
= orig
->vnc_encoding
;
181 local
->features
= orig
->features
;
182 local
->vd
= orig
->vd
;
183 local
->lossy_rect
= orig
->lossy_rect
;
184 local
->write_pixels
= orig
->write_pixels
;
185 local
->client_pf
= orig
->client_pf
;
186 local
->client_be
= orig
->client_be
;
187 local
->tight
= orig
->tight
;
188 local
->zlib
= orig
->zlib
;
189 local
->hextile
= orig
->hextile
;
190 local
->zrle
= orig
->zrle
;
193 static void vnc_async_encoding_end(VncState
*orig
, VncState
*local
)
195 orig
->tight
= local
->tight
;
196 orig
->zlib
= local
->zlib
;
197 orig
->hextile
= local
->hextile
;
198 orig
->zrle
= local
->zrle
;
199 orig
->lossy_rect
= local
->lossy_rect
;
202 static int vnc_worker_thread_loop(VncJobQueue
*queue
)
205 VncRectEntry
*entry
, *tmp
;
210 vnc_lock_queue(queue
);
211 while (QTAILQ_EMPTY(&queue
->jobs
) && !queue
->exit
) {
212 qemu_cond_wait(&queue
->cond
, &queue
->mutex
);
214 /* Here job can only be NULL if queue->exit is true */
215 job
= QTAILQ_FIRST(&queue
->jobs
);
216 vnc_unlock_queue(queue
);
222 vnc_lock_output(job
->vs
);
223 if (job
->vs
->ioc
== NULL
|| job
->vs
->abort
== true) {
224 vnc_unlock_output(job
->vs
);
227 if (buffer_empty(&job
->vs
->output
)) {
229 * Looks like a NOP as it obviously moves no data. But it
230 * moves the empty buffer, so we don't have to malloc a new
233 buffer_move_empty(&vs
.output
, &job
->vs
->output
);
235 vnc_unlock_output(job
->vs
);
237 /* Make a local copy of vs and switch output buffers */
238 vnc_async_encoding_start(job
->vs
, &vs
);
240 /* Start sending rectangles */
242 vnc_write_u8(&vs
, VNC_MSG_SERVER_FRAMEBUFFER_UPDATE
);
243 vnc_write_u8(&vs
, 0);
244 saved_offset
= vs
.output
.offset
;
245 vnc_write_u16(&vs
, 0);
247 vnc_lock_display(job
->vs
->vd
);
248 QLIST_FOREACH_SAFE(entry
, &job
->rectangles
, next
, tmp
) {
251 if (job
->vs
->ioc
== NULL
) {
252 vnc_unlock_display(job
->vs
->vd
);
253 /* Copy persistent encoding data */
254 vnc_async_encoding_end(job
->vs
, &vs
);
258 n
= vnc_send_framebuffer_update(&vs
, entry
->rect
.x
, entry
->rect
.y
,
259 entry
->rect
.w
, entry
->rect
.h
);
266 vnc_unlock_display(job
->vs
->vd
);
268 /* Put n_rectangles at the beginning of the message */
269 vs
.output
.buffer
[saved_offset
] = (n_rectangles
>> 8) & 0xFF;
270 vs
.output
.buffer
[saved_offset
+ 1] = n_rectangles
& 0xFF;
272 vnc_lock_output(job
->vs
);
273 if (job
->vs
->ioc
!= NULL
) {
274 buffer_move(&job
->vs
->jobs_buffer
, &vs
.output
);
275 /* Copy persistent encoding data */
276 vnc_async_encoding_end(job
->vs
, &vs
);
278 qemu_bh_schedule(job
->vs
->bh
);
280 buffer_reset(&vs
.output
);
281 /* Copy persistent encoding data */
282 vnc_async_encoding_end(job
->vs
, &vs
);
284 vnc_unlock_output(job
->vs
);
287 vnc_lock_queue(queue
);
288 QTAILQ_REMOVE(&queue
->jobs
, job
, next
);
289 vnc_unlock_queue(queue
);
290 qemu_cond_broadcast(&queue
->cond
);
295 static VncJobQueue
*vnc_queue_init(void)
297 VncJobQueue
*queue
= g_new0(VncJobQueue
, 1);
299 qemu_cond_init(&queue
->cond
);
300 qemu_mutex_init(&queue
->mutex
);
301 QTAILQ_INIT(&queue
->jobs
);
305 static void vnc_queue_clear(VncJobQueue
*q
)
307 qemu_cond_destroy(&queue
->cond
);
308 qemu_mutex_destroy(&queue
->mutex
);
310 queue
= NULL
; /* Unset global queue */
313 static void *vnc_worker_thread(void *arg
)
315 VncJobQueue
*queue
= arg
;
317 qemu_thread_get_self(&queue
->thread
);
319 while (!vnc_worker_thread_loop(queue
)) ;
320 vnc_queue_clear(queue
);
324 static bool vnc_worker_thread_running(void)
326 return queue
; /* Check global queue */
329 void vnc_start_worker_thread(void)
333 if (vnc_worker_thread_running())
336 q
= vnc_queue_init();
337 qemu_thread_create(&q
->thread
, "vnc_worker", vnc_worker_thread
, q
,
338 QEMU_THREAD_DETACHED
);
339 queue
= q
; /* Set global queue */