target-ppc: Split model definitions out of translate_init.c
[qemu/agraf.git] / ui / vnc-jobs.c
blob0bfc0c5485b6eceadf625d4f61bb6238f4028b17
1 /*
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
25 * THE SOFTWARE.
29 #include "vnc.h"
30 #include "vnc-jobs.h"
31 #include "qemu/sockets.h"
34 * Locking:
36 * There are three levels of locking:
37 * - jobs queue lock: for each operation on the queue (push, pop, isEmpty?)
38 * - VncDisplay global lock: mainly used for framebuffer updates to avoid
39 * screen corruption if the framebuffer is updated
40 * while the worker is doing something.
41 * - VncState::output lock: used to make sure the output buffer is not corrupted
42 * if two threads try to write on it at the same time
44 * While the VNC worker thread is working, the VncDisplay global lock is held
45 * to avoid screen corruption (this does not block vnc_refresh() because it
46 * uses trylock()) but the output lock is not held because the thread works on
47 * its own output buffer.
48 * When the encoding job is done, the worker thread will hold the output lock
49 * and copy its output buffer in vs->output.
52 struct VncJobQueue {
53 QemuCond cond;
54 QemuMutex mutex;
55 QemuThread thread;
56 Buffer buffer;
57 bool exit;
58 QTAILQ_HEAD(, VncJob) jobs;
61 typedef struct VncJobQueue VncJobQueue;
64 * We use a single global queue, but most of the functions are
65 * already reentrant, so we can easily add more than one encoding thread
67 static VncJobQueue *queue;
69 static void vnc_lock_queue(VncJobQueue *queue)
71 qemu_mutex_lock(&queue->mutex);
74 static void vnc_unlock_queue(VncJobQueue *queue)
76 qemu_mutex_unlock(&queue->mutex);
79 VncJob *vnc_job_new(VncState *vs)
81 VncJob *job = g_malloc0(sizeof(VncJob));
83 job->vs = vs;
84 vnc_lock_queue(queue);
85 QLIST_INIT(&job->rectangles);
86 vnc_unlock_queue(queue);
87 return job;
90 int vnc_job_add_rect(VncJob *job, int x, int y, int w, int h)
92 VncRectEntry *entry = g_malloc0(sizeof(VncRectEntry));
94 entry->rect.x = x;
95 entry->rect.y = y;
96 entry->rect.w = w;
97 entry->rect.h = h;
99 vnc_lock_queue(queue);
100 QLIST_INSERT_HEAD(&job->rectangles, entry, next);
101 vnc_unlock_queue(queue);
102 return 1;
105 void vnc_job_push(VncJob *job)
107 vnc_lock_queue(queue);
108 if (queue->exit || QLIST_EMPTY(&job->rectangles)) {
109 g_free(job);
110 } else {
111 QTAILQ_INSERT_TAIL(&queue->jobs, job, next);
112 qemu_cond_broadcast(&queue->cond);
114 vnc_unlock_queue(queue);
117 static bool vnc_has_job_locked(VncState *vs)
119 VncJob *job;
121 QTAILQ_FOREACH(job, &queue->jobs, next) {
122 if (job->vs == vs || !vs) {
123 return true;
126 return false;
129 bool vnc_has_job(VncState *vs)
131 bool ret;
133 vnc_lock_queue(queue);
134 ret = vnc_has_job_locked(vs);
135 vnc_unlock_queue(queue);
136 return ret;
139 void vnc_jobs_clear(VncState *vs)
141 VncJob *job, *tmp;
143 vnc_lock_queue(queue);
144 QTAILQ_FOREACH_SAFE(job, &queue->jobs, next, tmp) {
145 if (job->vs == vs || !vs) {
146 QTAILQ_REMOVE(&queue->jobs, job, next);
149 vnc_unlock_queue(queue);
152 void vnc_jobs_join(VncState *vs)
154 vnc_lock_queue(queue);
155 while (vnc_has_job_locked(vs)) {
156 qemu_cond_wait(&queue->cond, &queue->mutex);
158 vnc_unlock_queue(queue);
159 vnc_jobs_consume_buffer(vs);
162 void vnc_jobs_consume_buffer(VncState *vs)
164 bool flush;
166 vnc_lock_output(vs);
167 if (vs->jobs_buffer.offset) {
168 vnc_write(vs, vs->jobs_buffer.buffer, vs->jobs_buffer.offset);
169 buffer_reset(&vs->jobs_buffer);
171 flush = vs->csock != -1 && vs->abort != true;
172 vnc_unlock_output(vs);
174 if (flush) {
175 vnc_flush(vs);
180 * Copy data for local use
182 static void vnc_async_encoding_start(VncState *orig, VncState *local)
184 local->vnc_encoding = orig->vnc_encoding;
185 local->features = orig->features;
186 local->ds = orig->ds;
187 local->vd = orig->vd;
188 local->lossy_rect = orig->lossy_rect;
189 local->write_pixels = orig->write_pixels;
190 local->client_pf = orig->client_pf;
191 local->client_be = orig->client_be;
192 local->tight = orig->tight;
193 local->zlib = orig->zlib;
194 local->hextile = orig->hextile;
195 local->zrle = orig->zrle;
196 local->output = queue->buffer;
197 local->csock = -1; /* Don't do any network work on this thread */
199 buffer_reset(&local->output);
202 static void vnc_async_encoding_end(VncState *orig, VncState *local)
204 orig->tight = local->tight;
205 orig->zlib = local->zlib;
206 orig->hextile = local->hextile;
207 orig->zrle = local->zrle;
208 orig->lossy_rect = local->lossy_rect;
210 queue->buffer = local->output;
213 static int vnc_worker_thread_loop(VncJobQueue *queue)
215 VncJob *job;
216 VncRectEntry *entry, *tmp;
217 VncState vs;
218 int n_rectangles;
219 int saved_offset;
221 vnc_lock_queue(queue);
222 while (QTAILQ_EMPTY(&queue->jobs) && !queue->exit) {
223 qemu_cond_wait(&queue->cond, &queue->mutex);
225 /* Here job can only be NULL if queue->exit is true */
226 job = QTAILQ_FIRST(&queue->jobs);
227 vnc_unlock_queue(queue);
229 if (queue->exit) {
230 return -1;
233 vnc_lock_output(job->vs);
234 if (job->vs->csock == -1 || job->vs->abort == true) {
235 vnc_unlock_output(job->vs);
236 goto disconnected;
238 vnc_unlock_output(job->vs);
240 /* Make a local copy of vs and switch output buffers */
241 vnc_async_encoding_start(job->vs, &vs);
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);
250 vnc_lock_display(job->vs->vd);
251 QLIST_FOREACH_SAFE(entry, &job->rectangles, next, tmp) {
252 int n;
254 if (job->vs->csock == -1) {
255 vnc_unlock_display(job->vs->vd);
256 goto disconnected;
259 n = vnc_send_framebuffer_update(&vs, entry->rect.x, entry->rect.y,
260 entry->rect.w, entry->rect.h);
262 if (n >= 0) {
263 n_rectangles += n;
265 g_free(entry);
267 vnc_unlock_display(job->vs->vd);
269 /* Put n_rectangles at the beginning of the message */
270 vs.output.buffer[saved_offset] = (n_rectangles >> 8) & 0xFF;
271 vs.output.buffer[saved_offset + 1] = n_rectangles & 0xFF;
273 vnc_lock_output(job->vs);
274 if (job->vs->csock != -1) {
275 buffer_reserve(&job->vs->jobs_buffer, vs.output.offset);
276 buffer_append(&job->vs->jobs_buffer, vs.output.buffer,
277 vs.output.offset);
278 /* Copy persistent encoding data */
279 vnc_async_encoding_end(job->vs, &vs);
281 qemu_bh_schedule(job->vs->bh);
283 vnc_unlock_output(job->vs);
285 disconnected:
286 vnc_lock_queue(queue);
287 QTAILQ_REMOVE(&queue->jobs, job, next);
288 vnc_unlock_queue(queue);
289 qemu_cond_broadcast(&queue->cond);
290 g_free(job);
291 return 0;
294 static VncJobQueue *vnc_queue_init(void)
296 VncJobQueue *queue = g_malloc0(sizeof(VncJobQueue));
298 qemu_cond_init(&queue->cond);
299 qemu_mutex_init(&queue->mutex);
300 QTAILQ_INIT(&queue->jobs);
301 return queue;
304 static void vnc_queue_clear(VncJobQueue *q)
306 qemu_cond_destroy(&queue->cond);
307 qemu_mutex_destroy(&queue->mutex);
308 buffer_free(&queue->buffer);
309 g_free(q);
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);
321 return NULL;
324 static bool vnc_worker_thread_running(void)
326 return queue; /* Check global queue */
329 void vnc_start_worker_thread(void)
331 VncJobQueue *q;
333 if (vnc_worker_thread_running())
334 return ;
336 q = vnc_queue_init();
337 qemu_thread_create(&q->thread, vnc_worker_thread, q, QEMU_THREAD_DETACHED);
338 queue = q; /* Set global queue */
341 void vnc_stop_worker_thread(void)
343 if (!vnc_worker_thread_running())
344 return ;
346 /* Remove all jobs and wake up the thread */
347 vnc_lock_queue(queue);
348 queue->exit = true;
349 vnc_unlock_queue(queue);
350 vnc_jobs_clear(NULL);
351 qemu_cond_broadcast(&queue->cond);