Statistics
| Branch: | Revision:

root / ui / spice-display.c @ a3e22260

History | View | Annotate | Download (11.8 kB)

1
/*
2
 * Copyright (C) 2010 Red Hat, Inc.
3
 *
4
 * This program is free software; you can redistribute it and/or
5
 * modify it under the terms of the GNU General Public License as
6
 * published by the Free Software Foundation; either version 2 or
7
 * (at your option) version 3 of the License.
8
 *
9
 * This program is distributed in the hope that it will be useful,
10
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12
 * GNU General Public License for more details.
13
 *
14
 * You should have received a copy of the GNU General Public License
15
 * along with this program; if not, see <http://www.gnu.org/licenses/>.
16
 */
17

    
18
#include <pthread.h>
19

    
20
#include "qemu-common.h"
21
#include "qemu-spice.h"
22
#include "qemu-timer.h"
23
#include "qemu-queue.h"
24
#include "monitor.h"
25
#include "console.h"
26
#include "sysemu.h"
27

    
28
#include "spice-display.h"
29

    
30
static int debug = 0;
31

    
32
static void __attribute__((format(printf,2,3)))
33
dprint(int level, const char *fmt, ...)
34
{
35
    va_list args;
36

    
37
    if (level <= debug) {
38
        va_start(args, fmt);
39
        vfprintf(stderr, fmt, args);
40
        va_end(args);
41
    }
42
}
43

    
44
int qemu_spice_rect_is_empty(const QXLRect* r)
45
{
46
    return r->top == r->bottom || r->left == r->right;
47
}
48

    
49
void qemu_spice_rect_union(QXLRect *dest, const QXLRect *r)
50
{
51
    if (qemu_spice_rect_is_empty(r)) {
52
        return;
53
    }
54

    
55
    if (qemu_spice_rect_is_empty(dest)) {
56
        *dest = *r;
57
        return;
58
    }
59

    
60
    dest->top = MIN(dest->top, r->top);
61
    dest->left = MIN(dest->left, r->left);
62
    dest->bottom = MAX(dest->bottom, r->bottom);
63
    dest->right = MAX(dest->right, r->right);
64
}
65

    
66
/*
67
 * Called from spice server thread context (via interface_get_command).
68
 * We do *not* hold the global qemu mutex here, so extra care is needed
69
 * when calling qemu functions.  Qemu interfaces used:
70
 *    - pflib (is re-entrant).
71
 *    - qemu_malloc (underlying glibc malloc is re-entrant).
72
 */
73
SimpleSpiceUpdate *qemu_spice_create_update(SimpleSpiceDisplay *ssd)
74
{
75
    SimpleSpiceUpdate *update;
76
    QXLDrawable *drawable;
77
    QXLImage *image;
78
    QXLCommand *cmd;
79
    uint8_t *src, *dst;
80
    int by, bw, bh;
81

    
82
    if (qemu_spice_rect_is_empty(&ssd->dirty)) {
83
        return NULL;
84
    };
85

    
86
    pthread_mutex_lock(&ssd->lock);
87
    dprint(2, "%s: lr %d -> %d,  tb -> %d -> %d\n", __FUNCTION__,
88
           ssd->dirty.left, ssd->dirty.right,
89
           ssd->dirty.top, ssd->dirty.bottom);
90

    
91
    update   = qemu_mallocz(sizeof(*update));
92
    drawable = &update->drawable;
93
    image    = &update->image;
94
    cmd      = &update->ext.cmd;
95

    
96
    bw       = ssd->dirty.right - ssd->dirty.left;
97
    bh       = ssd->dirty.bottom - ssd->dirty.top;
98
    update->bitmap = qemu_malloc(bw * bh * 4);
99

    
100
    drawable->bbox            = ssd->dirty;
101
    drawable->clip.type       = SPICE_CLIP_TYPE_NONE;
102
    drawable->effect          = QXL_EFFECT_OPAQUE;
103
    drawable->release_info.id = (intptr_t)update;
104
    drawable->type            = QXL_DRAW_COPY;
105
    drawable->surfaces_dest[0] = -1;
106
    drawable->surfaces_dest[1] = -1;
107
    drawable->surfaces_dest[2] = -1;
108

    
109
    drawable->u.copy.rop_descriptor  = SPICE_ROPD_OP_PUT;
110
    drawable->u.copy.src_bitmap      = (intptr_t)image;
111
    drawable->u.copy.src_area.right  = bw;
112
    drawable->u.copy.src_area.bottom = bh;
113

    
114
    QXL_SET_IMAGE_ID(image, QXL_IMAGE_GROUP_DEVICE, ssd->unique++);
115
    image->descriptor.type   = SPICE_IMAGE_TYPE_BITMAP;
116
    image->bitmap.flags      = QXL_BITMAP_DIRECT | QXL_BITMAP_TOP_DOWN;
117
    image->bitmap.stride     = bw * 4;
118
    image->descriptor.width  = image->bitmap.x = bw;
119
    image->descriptor.height = image->bitmap.y = bh;
120
    image->bitmap.data = (intptr_t)(update->bitmap);
121
    image->bitmap.palette = 0;
122
    image->bitmap.format = SPICE_BITMAP_FMT_32BIT;
123

    
124
    if (ssd->conv == NULL) {
125
        PixelFormat dst = qemu_default_pixelformat(32);
126
        ssd->conv = qemu_pf_conv_get(&dst, &ssd->ds->surface->pf);
127
        assert(ssd->conv);
128
    }
129

    
130
    src = ds_get_data(ssd->ds) +
131
        ssd->dirty.top * ds_get_linesize(ssd->ds) +
132
        ssd->dirty.left * ds_get_bytes_per_pixel(ssd->ds);
133
    dst = update->bitmap;
134
    for (by = 0; by < bh; by++) {
135
        qemu_pf_conv_run(ssd->conv, dst, src, bw);
136
        src += ds_get_linesize(ssd->ds);
137
        dst += image->bitmap.stride;
138
    }
139

    
140
    cmd->type = QXL_CMD_DRAW;
141
    cmd->data = (intptr_t)drawable;
142

    
143
    memset(&ssd->dirty, 0, sizeof(ssd->dirty));
144
    pthread_mutex_unlock(&ssd->lock);
145
    return update;
146
}
147

    
148
/*
149
 * Called from spice server thread context (via interface_release_ressource)
150
 * We do *not* hold the global qemu mutex here, so extra care is needed
151
 * when calling qemu functions.  Qemu interfaces used:
152
 *    - qemu_free (underlying glibc free is re-entrant).
153
 */
154
void qemu_spice_destroy_update(SimpleSpiceDisplay *sdpy, SimpleSpiceUpdate *update)
155
{
156
    qemu_free(update->bitmap);
157
    qemu_free(update);
158
}
159

    
160
void qemu_spice_create_host_memslot(SimpleSpiceDisplay *ssd)
161
{
162
    QXLDevMemSlot memslot;
163

    
164
    dprint(1, "%s:\n", __FUNCTION__);
165

    
166
    memset(&memslot, 0, sizeof(memslot));
167
    memslot.slot_group_id = MEMSLOT_GROUP_HOST;
168
    memslot.virt_end = ~0;
169
    ssd->worker->add_memslot(ssd->worker, &memslot);
170
}
171

    
172
void qemu_spice_create_host_primary(SimpleSpiceDisplay *ssd)
173
{
174
    QXLDevSurfaceCreate surface;
175

    
176
    dprint(1, "%s: %dx%d\n", __FUNCTION__,
177
           ds_get_width(ssd->ds), ds_get_height(ssd->ds));
178

    
179
    surface.format     = SPICE_SURFACE_FMT_32_xRGB;
180
    surface.width      = ds_get_width(ssd->ds);
181
    surface.height     = ds_get_height(ssd->ds);
182
    surface.stride     = -surface.width * 4;
183
    surface.mouse_mode = 0;
184
    surface.flags      = 0;
185
    surface.type       = 0;
186
    surface.mem        = (intptr_t)ssd->buf;
187
    surface.group_id   = MEMSLOT_GROUP_HOST;
188
    ssd->worker->create_primary_surface(ssd->worker, 0, &surface);
189
}
190

    
191
void qemu_spice_destroy_host_primary(SimpleSpiceDisplay *ssd)
192
{
193
    dprint(1, "%s:\n", __FUNCTION__);
194

    
195
    ssd->worker->destroy_primary_surface(ssd->worker, 0);
196
}
197

    
198
void qemu_spice_vm_change_state_handler(void *opaque, int running, int reason)
199
{
200
    SimpleSpiceDisplay *ssd = opaque;
201

    
202
    if (running) {
203
        ssd->worker->start(ssd->worker);
204
    } else {
205
        ssd->worker->stop(ssd->worker);
206
    }
207
    ssd->running = running;
208
}
209

    
210
/* display listener callbacks */
211

    
212
void qemu_spice_display_update(SimpleSpiceDisplay *ssd,
213
                               int x, int y, int w, int h)
214
{
215
    QXLRect update_area;
216

    
217
    dprint(2, "%s: x %d y %d w %d h %d\n", __FUNCTION__, x, y, w, h);
218
    update_area.left = x,
219
    update_area.right = x + w;
220
    update_area.top = y;
221
    update_area.bottom = y + h;
222

    
223
    pthread_mutex_lock(&ssd->lock);
224
    if (qemu_spice_rect_is_empty(&ssd->dirty)) {
225
        ssd->notify++;
226
    }
227
    qemu_spice_rect_union(&ssd->dirty, &update_area);
228
    pthread_mutex_unlock(&ssd->lock);
229
}
230

    
231
void qemu_spice_display_resize(SimpleSpiceDisplay *ssd)
232
{
233
    dprint(1, "%s:\n", __FUNCTION__);
234

    
235
    pthread_mutex_lock(&ssd->lock);
236
    memset(&ssd->dirty, 0, sizeof(ssd->dirty));
237
    qemu_pf_conv_put(ssd->conv);
238
    ssd->conv = NULL;
239
    pthread_mutex_unlock(&ssd->lock);
240

    
241
    qemu_spice_destroy_host_primary(ssd);
242
    qemu_spice_create_host_primary(ssd);
243

    
244
    pthread_mutex_lock(&ssd->lock);
245
    memset(&ssd->dirty, 0, sizeof(ssd->dirty));
246
    ssd->notify++;
247
    pthread_mutex_unlock(&ssd->lock);
248
}
249

    
250
void qemu_spice_display_refresh(SimpleSpiceDisplay *ssd)
251
{
252
    dprint(3, "%s:\n", __FUNCTION__);
253
    vga_hw_update();
254
    if (ssd->notify) {
255
        ssd->notify = 0;
256
        ssd->worker->wakeup(ssd->worker);
257
        dprint(2, "%s: notify\n", __FUNCTION__);
258
    }
259
}
260

    
261
/* spice display interface callbacks */
262

    
263
static void interface_attach_worker(QXLInstance *sin, QXLWorker *qxl_worker)
264
{
265
    SimpleSpiceDisplay *ssd = container_of(sin, SimpleSpiceDisplay, qxl);
266

    
267
    dprint(1, "%s:\n", __FUNCTION__);
268
    ssd->worker = qxl_worker;
269
}
270

    
271
static void interface_set_compression_level(QXLInstance *sin, int level)
272
{
273
    dprint(1, "%s:\n", __FUNCTION__);
274
    /* nothing to do */
275
}
276

    
277
static void interface_set_mm_time(QXLInstance *sin, uint32_t mm_time)
278
{
279
    dprint(3, "%s:\n", __FUNCTION__);
280
    /* nothing to do */
281
}
282

    
283
static void interface_get_init_info(QXLInstance *sin, QXLDevInitInfo *info)
284
{
285
    SimpleSpiceDisplay *ssd = container_of(sin, SimpleSpiceDisplay, qxl);
286

    
287
    info->memslot_gen_bits = MEMSLOT_GENERATION_BITS;
288
    info->memslot_id_bits  = MEMSLOT_SLOT_BITS;
289
    info->num_memslots = NUM_MEMSLOTS;
290
    info->num_memslots_groups = NUM_MEMSLOTS_GROUPS;
291
    info->internal_groupslot_id = 0;
292
    info->qxl_ram_size = ssd->bufsize;
293
    info->n_surfaces = NUM_SURFACES;
294
}
295

    
296
static int interface_get_command(QXLInstance *sin, struct QXLCommandExt *ext)
297
{
298
    SimpleSpiceDisplay *ssd = container_of(sin, SimpleSpiceDisplay, qxl);
299
    SimpleSpiceUpdate *update;
300

    
301
    dprint(3, "%s:\n", __FUNCTION__);
302
    update = qemu_spice_create_update(ssd);
303
    if (update == NULL) {
304
        return false;
305
    }
306
    *ext = update->ext;
307
    return true;
308
}
309

    
310
static int interface_req_cmd_notification(QXLInstance *sin)
311
{
312
    dprint(1, "%s:\n", __FUNCTION__);
313
    return 1;
314
}
315

    
316
static void interface_release_resource(QXLInstance *sin,
317
                                       struct QXLReleaseInfoExt ext)
318
{
319
    SimpleSpiceDisplay *ssd = container_of(sin, SimpleSpiceDisplay, qxl);
320
    uintptr_t id;
321

    
322
    dprint(2, "%s:\n", __FUNCTION__);
323
    id = ext.info->id;
324
    qemu_spice_destroy_update(ssd, (void*)id);
325
}
326

    
327
static int interface_get_cursor_command(QXLInstance *sin, struct QXLCommandExt *ext)
328
{
329
    dprint(3, "%s:\n", __FUNCTION__);
330
    return false;
331
}
332

    
333
static int interface_req_cursor_notification(QXLInstance *sin)
334
{
335
    dprint(1, "%s:\n", __FUNCTION__);
336
    return 1;
337
}
338

    
339
static void interface_notify_update(QXLInstance *sin, uint32_t update_id)
340
{
341
    fprintf(stderr, "%s: abort()\n", __FUNCTION__);
342
    abort();
343
}
344

    
345
static int interface_flush_resources(QXLInstance *sin)
346
{
347
    fprintf(stderr, "%s: abort()\n", __FUNCTION__);
348
    abort();
349
    return 0;
350
}
351

    
352
static const QXLInterface dpy_interface = {
353
    .base.type               = SPICE_INTERFACE_QXL,
354
    .base.description        = "qemu simple display",
355
    .base.major_version      = SPICE_INTERFACE_QXL_MAJOR,
356
    .base.minor_version      = SPICE_INTERFACE_QXL_MINOR,
357

    
358
    .attache_worker          = interface_attach_worker,
359
    .set_compression_level   = interface_set_compression_level,
360
    .set_mm_time             = interface_set_mm_time,
361
    .get_init_info           = interface_get_init_info,
362

    
363
    /* the callbacks below are called from spice server thread context */
364
    .get_command             = interface_get_command,
365
    .req_cmd_notification    = interface_req_cmd_notification,
366
    .release_resource        = interface_release_resource,
367
    .get_cursor_command      = interface_get_cursor_command,
368
    .req_cursor_notification = interface_req_cursor_notification,
369
    .notify_update           = interface_notify_update,
370
    .flush_resources         = interface_flush_resources,
371
};
372

    
373
static SimpleSpiceDisplay sdpy;
374

    
375
static void display_update(struct DisplayState *ds, int x, int y, int w, int h)
376
{
377
    qemu_spice_display_update(&sdpy, x, y, w, h);
378
}
379

    
380
static void display_resize(struct DisplayState *ds)
381
{
382
    qemu_spice_display_resize(&sdpy);
383
}
384

    
385
static void display_refresh(struct DisplayState *ds)
386
{
387
    qemu_spice_display_refresh(&sdpy);
388
}
389

    
390
static DisplayChangeListener display_listener = {
391
    .dpy_update  = display_update,
392
    .dpy_resize  = display_resize,
393
    .dpy_refresh = display_refresh,
394
};
395

    
396
void qemu_spice_display_init(DisplayState *ds)
397
{
398
    assert(sdpy.ds == NULL);
399
    sdpy.ds = ds;
400
    sdpy.bufsize = (16 * 1024 * 1024);
401
    sdpy.buf = qemu_malloc(sdpy.bufsize);
402
    pthread_mutex_init(&sdpy.lock, NULL);
403
    register_displaychangelistener(ds, &display_listener);
404

    
405
    sdpy.qxl.base.sif = &dpy_interface.base;
406
    qemu_spice_add_interface(&sdpy.qxl.base);
407
    assert(sdpy.worker);
408

    
409
    qemu_add_vm_change_state_handler(qemu_spice_vm_change_state_handler, &sdpy);
410
    qemu_spice_create_host_memslot(&sdpy);
411
    qemu_spice_create_host_primary(&sdpy);
412
}