vnc-jobs.c 9.5 KB
Newer Older
C
Corentin Chary 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28
/*
 * QEMU VNC display driver
 *
 * Copyright (C) 2006 Anthony Liguori <anthony@codemonkey.ws>
 * Copyright (C) 2006 Fabrice Bellard
 * Copyright (C) 2009 Red Hat, Inc
 * Copyright (C) 2010 Corentin Chary <corentin.chary@gmail.com>
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.
 */


P
Peter Maydell 已提交
29
#include "qemu/osdep.h"
C
Corentin Chary 已提交
30 31
#include "vnc.h"
#include "vnc-jobs.h"
32
#include "qemu/sockets.h"
33
#include "qemu/main-loop.h"
34
#include "block/aio.h"
C
Corentin Chary 已提交
35 36 37 38

/*
 * Locking:
 *
39
 * There are three levels of locking:
C
Corentin Chary 已提交
40 41 42
 * - jobs queue lock: for each operation on the queue (push, pop, isEmpty?)
 * - VncDisplay global lock: mainly used for framebuffer updates to avoid
 *                      screen corruption if the framebuffer is updated
43
 *                      while the worker is doing something.
C
Corentin Chary 已提交
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
C
Corentin Chary 已提交
46
 *
47 48 49
 * While the VNC worker thread is working, the VncDisplay global lock is held
 * to avoid screen corruption (this does not block vnc_refresh() because it
 * uses trylock()) but the output lock is not held because the thread works on
C
Corentin Chary 已提交
50 51 52
 * its own output buffer.
 * When the encoding job is done, the worker thread will hold the output lock
 * and copy its output buffer in vs->output.
53
 */
C
Corentin Chary 已提交
54 55 56 57 58 59 60 61 62 63 64 65 66

struct VncJobQueue {
    QemuCond cond;
    QemuMutex mutex;
    QemuThread thread;
    bool exit;
    QTAILQ_HEAD(, VncJob) jobs;
};

typedef struct VncJobQueue VncJobQueue;

/*
 * 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
C
Corentin Chary 已提交
68 69 70 71 72 73 74 75 76 77 78 79 80 81 82
 */
static VncJobQueue *queue;

static void vnc_lock_queue(VncJobQueue *queue)
{
    qemu_mutex_lock(&queue->mutex);
}

static void vnc_unlock_queue(VncJobQueue *queue)
{
    qemu_mutex_unlock(&queue->mutex);
}

VncJob *vnc_job_new(VncState *vs)
{
83
    VncJob *job = g_new0(VncJob, 1);
C
Corentin Chary 已提交
84

85
    assert(vs->magic == VNC_MAGIC);
C
Corentin Chary 已提交
86 87 88 89 90 91 92 93 94
    job->vs = vs;
    vnc_lock_queue(queue);
    QLIST_INIT(&job->rectangles);
    vnc_unlock_queue(queue);
    return job;
}

int vnc_job_add_rect(VncJob *job, int x, int y, int w, int h)
{
95
    VncRectEntry *entry = g_new0(VncRectEntry, 1);
C
Corentin Chary 已提交
96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111

    entry->rect.x = x;
    entry->rect.y = y;
    entry->rect.w = w;
    entry->rect.h = h;

    vnc_lock_queue(queue);
    QLIST_INSERT_HEAD(&job->rectangles, entry, next);
    vnc_unlock_queue(queue);
    return 1;
}

void vnc_job_push(VncJob *job)
{
    vnc_lock_queue(queue);
    if (queue->exit || QLIST_EMPTY(&job->rectangles)) {
112
        g_free(job);
C
Corentin Chary 已提交
113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138
    } else {
        QTAILQ_INSERT_TAIL(&queue->jobs, job, next);
        qemu_cond_broadcast(&queue->cond);
    }
    vnc_unlock_queue(queue);
}

static bool vnc_has_job_locked(VncState *vs)
{
    VncJob *job;

    QTAILQ_FOREACH(job, &queue->jobs, next) {
        if (job->vs == vs || !vs) {
            return true;
        }
    }
    return false;
}

void vnc_jobs_join(VncState *vs)
{
    vnc_lock_queue(queue);
    while (vnc_has_job_locked(vs)) {
        qemu_cond_wait(&queue->cond, &queue->mutex);
    }
    vnc_unlock_queue(queue);
139 140 141 142 143 144 145 146 147
    vnc_jobs_consume_buffer(vs);
}

void vnc_jobs_consume_buffer(VncState *vs)
{
    bool flush;

    vnc_lock_output(vs);
    if (vs->jobs_buffer.offset) {
148 149 150 151
        if (vs->ioc != NULL && buffer_empty(&vs->output)) {
            if (vs->ioc_tag) {
                g_source_remove(vs->ioc_tag);
            }
152 153 154 155
            if (vs->disconnecting == FALSE) {
                vs->ioc_tag = qio_channel_add_watch(
                    vs->ioc, G_IO_IN | G_IO_OUT, vnc_client_io, vs, NULL);
            }
156 157
        }
        buffer_move(&vs->output, &vs->jobs_buffer);
158 159 160 161 162

        if (vs->job_update == VNC_STATE_UPDATE_FORCE) {
            vs->force_update_offset = vs->output.offset;
        }
        vs->job_update = VNC_STATE_UPDATE_NONE;
163
    }
164
    flush = vs->ioc != NULL && vs->abort != true;
165 166 167 168 169
    vnc_unlock_output(vs);

    if (flush) {
      vnc_flush(vs);
    }
C
Corentin Chary 已提交
170 171 172 173 174 175 176
}

/*
 * Copy data for local use
 */
static void vnc_async_encoding_start(VncState *orig, VncState *local)
{
G
Gerd Hoffmann 已提交
177
    buffer_init(&local->output, "vnc-worker-output");
178 179
    local->sioc = NULL; /* Don't do any network work on this thread */
    local->ioc = NULL; /* Don't do any network work on this thread */
G
Gerd Hoffmann 已提交
180

C
Corentin Chary 已提交
181 182 183
    local->vnc_encoding = orig->vnc_encoding;
    local->features = orig->features;
    local->vd = orig->vd;
184
    local->lossy_rect = orig->lossy_rect;
C
Corentin Chary 已提交
185
    local->write_pixels = orig->write_pixels;
186 187
    local->client_pf = orig->client_pf;
    local->client_be = orig->client_be;
C
Corentin Chary 已提交
188 189 190
    local->tight = orig->tight;
    local->zlib = orig->zlib;
    local->hextile = orig->hextile;
191
    local->zrle = orig->zrle;
C
Corentin Chary 已提交
192 193 194 195
}

static void vnc_async_encoding_end(VncState *orig, VncState *local)
{
196
    buffer_free(&local->output);
C
Corentin Chary 已提交
197 198 199
    orig->tight = local->tight;
    orig->zlib = local->zlib;
    orig->hextile = local->hextile;
200
    orig->zrle = local->zrle;
201
    orig->lossy_rect = local->lossy_rect;
C
Corentin Chary 已提交
202 203 204 205 206 207
}

static int vnc_worker_thread_loop(VncJobQueue *queue)
{
    VncJob *job;
    VncRectEntry *entry, *tmp;
G
Gerd Hoffmann 已提交
208
    VncState vs = {};
C
Corentin Chary 已提交
209 210 211 212 213 214 215 216 217 218
    int n_rectangles;
    int saved_offset;

    vnc_lock_queue(queue);
    while (QTAILQ_EMPTY(&queue->jobs) && !queue->exit) {
        qemu_cond_wait(&queue->cond, &queue->mutex);
    }
    /* Here job can only be NULL if queue->exit is true */
    job = QTAILQ_FIRST(&queue->jobs);
    vnc_unlock_queue(queue);
219
    assert(job->vs->magic == VNC_MAGIC);
C
Corentin Chary 已提交
220 221 222 223 224 225

    if (queue->exit) {
        return -1;
    }

    vnc_lock_output(job->vs);
226
    if (job->vs->ioc == NULL || job->vs->abort == true) {
227
        vnc_unlock_output(job->vs);
C
Corentin Chary 已提交
228 229
        goto disconnected;
    }
230 231 232 233 234 235 236 237
    if (buffer_empty(&job->vs->output)) {
        /*
         * Looks like a NOP as it obviously moves no data.  But it
         * moves the empty buffer, so we don't have to malloc a new
         * one for vs.output
         */
        buffer_move_empty(&vs.output, &job->vs->output);
    }
C
Corentin Chary 已提交
238 239 240 241
    vnc_unlock_output(job->vs);

    /* Make a local copy of vs and switch output buffers */
    vnc_async_encoding_start(job->vs, &vs);
242
    vs.magic = VNC_MAGIC;
C
Corentin Chary 已提交
243 244 245 246 247 248 249 250 251 252 253 254

    /* Start sending rectangles */
    n_rectangles = 0;
    vnc_write_u8(&vs, VNC_MSG_SERVER_FRAMEBUFFER_UPDATE);
    vnc_write_u8(&vs, 0);
    saved_offset = vs.output.offset;
    vnc_write_u16(&vs, 0);

    vnc_lock_display(job->vs->vd);
    QLIST_FOREACH_SAFE(entry, &job->rectangles, next, tmp) {
        int n;

255
        if (job->vs->ioc == NULL) {
C
Corentin Chary 已提交
256
            vnc_unlock_display(job->vs->vd);
257 258
            /* Copy persistent encoding data */
            vnc_async_encoding_end(job->vs, &vs);
C
Corentin Chary 已提交
259 260 261 262 263 264 265 266 267
            goto disconnected;
        }

        n = vnc_send_framebuffer_update(&vs, entry->rect.x, entry->rect.y,
                                        entry->rect.w, entry->rect.h);

        if (n >= 0) {
            n_rectangles += n;
        }
268
        g_free(entry);
C
Corentin Chary 已提交
269 270 271 272 273 274 275 276
    }
    vnc_unlock_display(job->vs->vd);

    /* Put n_rectangles at the beginning of the message */
    vs.output.buffer[saved_offset] = (n_rectangles >> 8) & 0xFF;
    vs.output.buffer[saved_offset + 1] = n_rectangles & 0xFF;

    vnc_lock_output(job->vs);
277
    if (job->vs->ioc != NULL) {
278
        buffer_move(&job->vs->jobs_buffer, &vs.output);
279 280 281
        /* Copy persistent encoding data */
        vnc_async_encoding_end(job->vs, &vs);

282
        qemu_bh_schedule(job->vs->bh);
283
    }  else {
284
        buffer_reset(&vs.output);
285 286
        /* Copy persistent encoding data */
        vnc_async_encoding_end(job->vs, &vs);
C
Corentin Chary 已提交
287 288 289
    }
    vnc_unlock_output(job->vs);

290
disconnected:
C
Corentin Chary 已提交
291 292 293 294
    vnc_lock_queue(queue);
    QTAILQ_REMOVE(&queue->jobs, job, next);
    vnc_unlock_queue(queue);
    qemu_cond_broadcast(&queue->cond);
295
    g_free(job);
296
    vs.magic = 0;
C
Corentin Chary 已提交
297 298 299 300 301
    return 0;
}

static VncJobQueue *vnc_queue_init(void)
{
302
    VncJobQueue *queue = g_new0(VncJobQueue, 1);
C
Corentin Chary 已提交
303 304 305 306 307 308 309 310 311 312 313

    qemu_cond_init(&queue->cond);
    qemu_mutex_init(&queue->mutex);
    QTAILQ_INIT(&queue->jobs);
    return queue;
}

static void vnc_queue_clear(VncJobQueue *q)
{
    qemu_cond_destroy(&queue->cond);
    qemu_mutex_destroy(&queue->mutex);
314
    g_free(q);
C
Corentin Chary 已提交
315 316 317 318 319 320 321
    queue = NULL; /* Unset global queue */
}

static void *vnc_worker_thread(void *arg)
{
    VncJobQueue *queue = arg;

J
Jan Kiszka 已提交
322
    qemu_thread_get_self(&queue->thread);
C
Corentin Chary 已提交
323 324 325 326 327 328

    while (!vnc_worker_thread_loop(queue)) ;
    vnc_queue_clear(queue);
    return NULL;
}

B
Blue Swirl 已提交
329 330 331 332 333
static bool vnc_worker_thread_running(void)
{
    return queue; /* Check global queue */
}

C
Corentin Chary 已提交
334 335 336 337 338 339 340 341
void vnc_start_worker_thread(void)
{
    VncJobQueue *q;

    if (vnc_worker_thread_running())
        return ;

    q = vnc_queue_init();
342 343
    qemu_thread_create(&q->thread, "vnc_worker", vnc_worker_thread, q,
                       QEMU_THREAD_DETACHED);
C
Corentin Chary 已提交
344 345
    queue = q; /* Set global queue */
}