2 * Copyright (C) 2010 Red Hat, Inc.
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.
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.
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/>.
20 #include "qemu-common.h"
21 #include "qemu-spice.h"
22 #include "qemu-timer.h"
23 #include "qemu-queue.h"
28 #include "spice-display.h"
32 static void GCC_FMT_ATTR(2, 3) dprint(int level
, const char *fmt
, ...)
38 vfprintf(stderr
, fmt
, args
);
43 int qemu_spice_rect_is_empty(const QXLRect
* r
)
45 return r
->top
== r
->bottom
|| r
->left
== r
->right
;
48 void qemu_spice_rect_union(QXLRect
*dest
, const QXLRect
*r
)
50 if (qemu_spice_rect_is_empty(r
)) {
54 if (qemu_spice_rect_is_empty(dest
)) {
59 dest
->top
= MIN(dest
->top
, r
->top
);
60 dest
->left
= MIN(dest
->left
, r
->left
);
61 dest
->bottom
= MAX(dest
->bottom
, r
->bottom
);
62 dest
->right
= MAX(dest
->right
, r
->right
);
65 static SimpleSpiceUpdate
*qemu_spice_create_update(SimpleSpiceDisplay
*ssd
)
67 SimpleSpiceUpdate
*update
;
68 QXLDrawable
*drawable
;
74 if (qemu_spice_rect_is_empty(&ssd
->dirty
)) {
78 dprint(2, "%s: lr %d -> %d, tb -> %d -> %d\n", __FUNCTION__
,
79 ssd
->dirty
.left
, ssd
->dirty
.right
,
80 ssd
->dirty
.top
, ssd
->dirty
.bottom
);
82 update
= qemu_mallocz(sizeof(*update
));
83 drawable
= &update
->drawable
;
84 image
= &update
->image
;
85 cmd
= &update
->ext
.cmd
;
87 bw
= ssd
->dirty
.right
- ssd
->dirty
.left
;
88 bh
= ssd
->dirty
.bottom
- ssd
->dirty
.top
;
89 update
->bitmap
= qemu_malloc(bw
* bh
* 4);
91 drawable
->bbox
= ssd
->dirty
;
92 drawable
->clip
.type
= SPICE_CLIP_TYPE_NONE
;
93 drawable
->effect
= QXL_EFFECT_OPAQUE
;
94 drawable
->release_info
.id
= (intptr_t)update
;
95 drawable
->type
= QXL_DRAW_COPY
;
96 drawable
->surfaces_dest
[0] = -1;
97 drawable
->surfaces_dest
[1] = -1;
98 drawable
->surfaces_dest
[2] = -1;
100 drawable
->u
.copy
.rop_descriptor
= SPICE_ROPD_OP_PUT
;
101 drawable
->u
.copy
.src_bitmap
= (intptr_t)image
;
102 drawable
->u
.copy
.src_area
.right
= bw
;
103 drawable
->u
.copy
.src_area
.bottom
= bh
;
105 QXL_SET_IMAGE_ID(image
, QXL_IMAGE_GROUP_DEVICE
, ssd
->unique
++);
106 image
->descriptor
.type
= SPICE_IMAGE_TYPE_BITMAP
;
107 image
->bitmap
.flags
= QXL_BITMAP_DIRECT
| QXL_BITMAP_TOP_DOWN
;
108 image
->bitmap
.stride
= bw
* 4;
109 image
->descriptor
.width
= image
->bitmap
.x
= bw
;
110 image
->descriptor
.height
= image
->bitmap
.y
= bh
;
111 image
->bitmap
.data
= (intptr_t)(update
->bitmap
);
112 image
->bitmap
.palette
= 0;
113 image
->bitmap
.format
= SPICE_BITMAP_FMT_32BIT
;
115 if (ssd
->conv
== NULL
) {
116 PixelFormat dst
= qemu_default_pixelformat(32);
117 ssd
->conv
= qemu_pf_conv_get(&dst
, &ssd
->ds
->surface
->pf
);
121 src
= ds_get_data(ssd
->ds
) +
122 ssd
->dirty
.top
* ds_get_linesize(ssd
->ds
) +
123 ssd
->dirty
.left
* ds_get_bytes_per_pixel(ssd
->ds
);
124 dst
= update
->bitmap
;
125 for (by
= 0; by
< bh
; by
++) {
126 qemu_pf_conv_run(ssd
->conv
, dst
, src
, bw
);
127 src
+= ds_get_linesize(ssd
->ds
);
128 dst
+= image
->bitmap
.stride
;
131 cmd
->type
= QXL_CMD_DRAW
;
132 cmd
->data
= (intptr_t)drawable
;
134 memset(&ssd
->dirty
, 0, sizeof(ssd
->dirty
));
139 * Called from spice server thread context (via interface_release_ressource)
140 * We do *not* hold the global qemu mutex here, so extra care is needed
141 * when calling qemu functions. Qemu interfaces used:
142 * - qemu_free (underlying glibc free is re-entrant).
144 void qemu_spice_destroy_update(SimpleSpiceDisplay
*sdpy
, SimpleSpiceUpdate
*update
)
146 qemu_free(update
->bitmap
);
150 void qemu_spice_create_host_memslot(SimpleSpiceDisplay
*ssd
)
152 QXLDevMemSlot memslot
;
154 dprint(1, "%s:\n", __FUNCTION__
);
156 memset(&memslot
, 0, sizeof(memslot
));
157 memslot
.slot_group_id
= MEMSLOT_GROUP_HOST
;
158 memslot
.virt_end
= ~0;
159 ssd
->worker
->add_memslot(ssd
->worker
, &memslot
);
162 void qemu_spice_create_host_primary(SimpleSpiceDisplay
*ssd
)
164 QXLDevSurfaceCreate surface
;
166 dprint(1, "%s: %dx%d\n", __FUNCTION__
,
167 ds_get_width(ssd
->ds
), ds_get_height(ssd
->ds
));
169 surface
.format
= SPICE_SURFACE_FMT_32_xRGB
;
170 surface
.width
= ds_get_width(ssd
->ds
);
171 surface
.height
= ds_get_height(ssd
->ds
);
172 surface
.stride
= -surface
.width
* 4;
173 surface
.mouse_mode
= true;
176 surface
.mem
= (intptr_t)ssd
->buf
;
177 surface
.group_id
= MEMSLOT_GROUP_HOST
;
179 qemu_mutex_unlock_iothread();
180 ssd
->worker
->create_primary_surface(ssd
->worker
, 0, &surface
);
181 qemu_mutex_lock_iothread();
184 void qemu_spice_destroy_host_primary(SimpleSpiceDisplay
*ssd
)
186 dprint(1, "%s:\n", __FUNCTION__
);
188 qemu_mutex_unlock_iothread();
189 ssd
->worker
->destroy_primary_surface(ssd
->worker
, 0);
190 qemu_mutex_lock_iothread();
193 void qemu_spice_vm_change_state_handler(void *opaque
, int running
, int reason
)
195 SimpleSpiceDisplay
*ssd
= opaque
;
198 ssd
->worker
->start(ssd
->worker
);
200 qemu_mutex_unlock_iothread();
201 ssd
->worker
->stop(ssd
->worker
);
202 qemu_mutex_lock_iothread();
204 ssd
->running
= running
;
207 /* display listener callbacks */
209 void qemu_spice_display_update(SimpleSpiceDisplay
*ssd
,
210 int x
, int y
, int w
, int h
)
214 dprint(2, "%s: x %d y %d w %d h %d\n", __FUNCTION__
, x
, y
, w
, h
);
215 update_area
.left
= x
,
216 update_area
.right
= x
+ w
;
218 update_area
.bottom
= y
+ h
;
220 if (qemu_spice_rect_is_empty(&ssd
->dirty
)) {
223 qemu_spice_rect_union(&ssd
->dirty
, &update_area
);
226 void qemu_spice_display_resize(SimpleSpiceDisplay
*ssd
)
228 dprint(1, "%s:\n", __FUNCTION__
);
230 memset(&ssd
->dirty
, 0, sizeof(ssd
->dirty
));
231 qemu_pf_conv_put(ssd
->conv
);
234 qemu_mutex_lock(&ssd
->lock
);
235 if (ssd
->update
!= NULL
) {
236 qemu_spice_destroy_update(ssd
, ssd
->update
);
239 qemu_mutex_unlock(&ssd
->lock
);
240 qemu_spice_destroy_host_primary(ssd
);
241 qemu_spice_create_host_primary(ssd
);
243 memset(&ssd
->dirty
, 0, sizeof(ssd
->dirty
));
247 void qemu_spice_display_refresh(SimpleSpiceDisplay
*ssd
)
249 dprint(3, "%s:\n", __FUNCTION__
);
252 qemu_mutex_lock(&ssd
->lock
);
253 if (ssd
->update
== NULL
) {
254 ssd
->update
= qemu_spice_create_update(ssd
);
257 qemu_mutex_unlock(&ssd
->lock
);
261 ssd
->worker
->wakeup(ssd
->worker
);
262 dprint(2, "%s: notify\n", __FUNCTION__
);
266 /* spice display interface callbacks */
268 static void interface_attach_worker(QXLInstance
*sin
, QXLWorker
*qxl_worker
)
270 SimpleSpiceDisplay
*ssd
= container_of(sin
, SimpleSpiceDisplay
, qxl
);
272 dprint(1, "%s:\n", __FUNCTION__
);
273 ssd
->worker
= qxl_worker
;
276 static void interface_set_compression_level(QXLInstance
*sin
, int level
)
278 dprint(1, "%s:\n", __FUNCTION__
);
282 static void interface_set_mm_time(QXLInstance
*sin
, uint32_t mm_time
)
284 dprint(3, "%s:\n", __FUNCTION__
);
288 static void interface_get_init_info(QXLInstance
*sin
, QXLDevInitInfo
*info
)
290 SimpleSpiceDisplay
*ssd
= container_of(sin
, SimpleSpiceDisplay
, qxl
);
292 info
->memslot_gen_bits
= MEMSLOT_GENERATION_BITS
;
293 info
->memslot_id_bits
= MEMSLOT_SLOT_BITS
;
294 info
->num_memslots
= NUM_MEMSLOTS
;
295 info
->num_memslots_groups
= NUM_MEMSLOTS_GROUPS
;
296 info
->internal_groupslot_id
= 0;
297 info
->qxl_ram_size
= ssd
->bufsize
;
298 info
->n_surfaces
= NUM_SURFACES
;
301 static int interface_get_command(QXLInstance
*sin
, struct QXLCommandExt
*ext
)
303 SimpleSpiceDisplay
*ssd
= container_of(sin
, SimpleSpiceDisplay
, qxl
);
304 SimpleSpiceUpdate
*update
;
307 dprint(3, "%s:\n", __FUNCTION__
);
309 qemu_mutex_lock(&ssd
->lock
);
310 if (ssd
->update
!= NULL
) {
311 update
= ssd
->update
;
316 qemu_mutex_unlock(&ssd
->lock
);
321 static int interface_req_cmd_notification(QXLInstance
*sin
)
323 dprint(1, "%s:\n", __FUNCTION__
);
327 static void interface_release_resource(QXLInstance
*sin
,
328 struct QXLReleaseInfoExt ext
)
330 SimpleSpiceDisplay
*ssd
= container_of(sin
, SimpleSpiceDisplay
, qxl
);
333 dprint(2, "%s:\n", __FUNCTION__
);
335 qemu_spice_destroy_update(ssd
, (void*)id
);
338 static int interface_get_cursor_command(QXLInstance
*sin
, struct QXLCommandExt
*ext
)
340 dprint(3, "%s:\n", __FUNCTION__
);
344 static int interface_req_cursor_notification(QXLInstance
*sin
)
346 dprint(1, "%s:\n", __FUNCTION__
);
350 static void interface_notify_update(QXLInstance
*sin
, uint32_t update_id
)
352 fprintf(stderr
, "%s: abort()\n", __FUNCTION__
);
356 static int interface_flush_resources(QXLInstance
*sin
)
358 fprintf(stderr
, "%s: abort()\n", __FUNCTION__
);
363 static const QXLInterface dpy_interface
= {
364 .base
.type
= SPICE_INTERFACE_QXL
,
365 .base
.description
= "qemu simple display",
366 .base
.major_version
= SPICE_INTERFACE_QXL_MAJOR
,
367 .base
.minor_version
= SPICE_INTERFACE_QXL_MINOR
,
369 .attache_worker
= interface_attach_worker
,
370 .set_compression_level
= interface_set_compression_level
,
371 .set_mm_time
= interface_set_mm_time
,
372 .get_init_info
= interface_get_init_info
,
374 /* the callbacks below are called from spice server thread context */
375 .get_command
= interface_get_command
,
376 .req_cmd_notification
= interface_req_cmd_notification
,
377 .release_resource
= interface_release_resource
,
378 .get_cursor_command
= interface_get_cursor_command
,
379 .req_cursor_notification
= interface_req_cursor_notification
,
380 .notify_update
= interface_notify_update
,
381 .flush_resources
= interface_flush_resources
,
384 static SimpleSpiceDisplay sdpy
;
386 static void display_update(struct DisplayState
*ds
, int x
, int y
, int w
, int h
)
388 qemu_spice_display_update(&sdpy
, x
, y
, w
, h
);
391 static void display_resize(struct DisplayState
*ds
)
393 qemu_spice_display_resize(&sdpy
);
396 static void display_refresh(struct DisplayState
*ds
)
398 qemu_spice_display_refresh(&sdpy
);
401 static DisplayChangeListener display_listener
= {
402 .dpy_update
= display_update
,
403 .dpy_resize
= display_resize
,
404 .dpy_refresh
= display_refresh
,
407 void qemu_spice_display_init(DisplayState
*ds
)
409 assert(sdpy
.ds
== NULL
);
411 qemu_mutex_init(&sdpy
.lock
);
412 sdpy
.bufsize
= (16 * 1024 * 1024);
413 sdpy
.buf
= qemu_malloc(sdpy
.bufsize
);
414 register_displaychangelistener(ds
, &display_listener
);
416 sdpy
.qxl
.base
.sif
= &dpy_interface
.base
;
417 qemu_spice_add_interface(&sdpy
.qxl
.base
);
420 qemu_add_vm_change_state_handler(qemu_spice_vm_change_state_handler
, &sdpy
);
421 qemu_spice_create_host_memslot(&sdpy
);
422 qemu_spice_create_host_primary(&sdpy
);