8 #include "qemu-common.h"
9 #include "qemu-spice.h"
10 #include "qemu-timer.h"
11 #include "qemu-queue.h"
16 #include "spice-display.h"
20 int qemu_spice_rect_is_empty(const SpiceRect
* r
)
22 return r
->top
== r
->bottom
|| r
->left
== r
->right
;
25 void qemu_spice_rect_union(SpiceRect
*dest
, const SpiceRect
*r
)
27 if (qemu_spice_rect_is_empty(r
)) {
31 if (qemu_spice_rect_is_empty(dest
)) {
36 dest
->top
= MIN(dest
->top
, r
->top
);
37 dest
->left
= MIN(dest
->left
, r
->left
);
38 dest
->bottom
= MAX(dest
->bottom
, r
->bottom
);
39 dest
->right
= MAX(dest
->right
, r
->right
);
42 SimpleSpiceUpdate
*qemu_spice_create_update(SimpleSpiceDisplay
*ssd
)
44 SimpleSpiceUpdate
*update
;
45 QXLDrawable
*drawable
;
51 if (qemu_spice_rect_is_empty(&ssd
->dirty
)) {
55 pthread_mutex_lock(&ssd
->lock
);
57 fprintf(stderr
, "%s: lr %d -> %d, tb -> %d -> %d\n", __FUNCTION__
,
58 ssd
->dirty
.left
, ssd
->dirty
.right
,
59 ssd
->dirty
.top
, ssd
->dirty
.bottom
);
61 update
= qemu_mallocz(sizeof(*update
));
62 drawable
= &update
->drawable
;
63 image
= &update
->image
;
64 cmd
= &update
->ext
.cmd
;
66 bw
= ssd
->dirty
.right
- ssd
->dirty
.left
;
67 bh
= ssd
->dirty
.bottom
- ssd
->dirty
.top
;
68 update
->bitmap
= qemu_malloc(bw
* bh
* 4);
70 drawable
->bbox
= ssd
->dirty
;
71 drawable
->clip
.type
= SPICE_CLIP_TYPE_NONE
;
72 drawable
->effect
= QXL_EFFECT_OPAQUE
;
73 drawable
->release_info
.id
= (intptr_t)update
;
74 drawable
->type
= QXL_DRAW_COPY
;
76 drawable
->u
.copy
.rop_decriptor
= SPICE_ROPD_OP_PUT
;
77 drawable
->u
.copy
.src_bitmap
= (intptr_t)image
;
78 drawable
->u
.copy
.src_area
.right
= bw
;
79 drawable
->u
.copy
.src_area
.bottom
= bh
;
81 QXL_SET_IMAGE_ID(image
, QXL_IMAGE_GROUP_DEVICE
, ssd
->unique
++);
82 image
->descriptor
.type
= SPICE_IMAGE_TYPE_BITMAP
;
83 image
->bitmap
.flags
= QXL_BITMAP_DIRECT
| QXL_BITMAP_TOP_DOWN
;
84 image
->bitmap
.stride
= bw
* 4;
85 image
->descriptor
.width
= image
->bitmap
.x
= bw
;
86 image
->descriptor
.height
= image
->bitmap
.y
= bh
;
87 image
->bitmap
.data
= (intptr_t)(update
->bitmap
);
88 image
->bitmap
.palette
= 0;
89 image
->bitmap
.format
= SPICE_BITMAP_FMT_32BIT
;
91 if (ssd
->conv
== NULL
) {
92 PixelFormat dst
= qemu_default_pixelformat(32);
93 ssd
->conv
= qemu_pf_conv_get(&dst
, &ssd
->ds
->surface
->pf
);
97 src
= ds_get_data(ssd
->ds
) +
98 ssd
->dirty
.top
* ds_get_linesize(ssd
->ds
) +
99 ssd
->dirty
.left
* ds_get_bytes_per_pixel(ssd
->ds
);
100 dst
= update
->bitmap
;
101 for (by
= 0; by
< bh
; by
++) {
102 qemu_pf_conv_run(ssd
->conv
, dst
, src
, bw
);
103 src
+= ds_get_linesize(ssd
->ds
);
104 dst
+= image
->bitmap
.stride
;
107 cmd
->type
= QXL_CMD_DRAW
;
108 cmd
->data
= (intptr_t)drawable
;
110 memset(&ssd
->dirty
, 0, sizeof(ssd
->dirty
));
111 pthread_mutex_unlock(&ssd
->lock
);
115 void qemu_spice_destroy_update(SimpleSpiceDisplay
*sdpy
, SimpleSpiceUpdate
*update
)
117 qemu_free(update
->bitmap
);
121 void qemu_spice_create_host_memslot(SimpleSpiceDisplay
*ssd
)
123 QXLDevMemSlot memslot
;
126 fprintf(stderr
, "%s:\n", __FUNCTION__
);
128 memset(&memslot
, 0, sizeof(memslot
));
129 memslot
.slot_group_id
= MEMSLOT_GROUP_HOST
;
130 memslot
.virt_end
= ~0;
131 ssd
->worker
->add_memslot(ssd
->worker
, &memslot
);
134 void qemu_spice_create_host_primary(SimpleSpiceDisplay
*ssd
)
136 QXLDevSurfaceCreate surface
;
139 fprintf(stderr
, "%s: %dx%d\n", __FUNCTION__
,
140 ds_get_width(ssd
->ds
), ds_get_height(ssd
->ds
));
142 surface
.format
= SPICE_SURFACE_FMT_32_xRGB
;
143 surface
.width
= ds_get_width(ssd
->ds
);
144 surface
.height
= ds_get_height(ssd
->ds
);
145 surface
.stride
= -surface
.width
* 4;
146 surface
.mouse_mode
= true;
149 surface
.mem
= (intptr_t)ssd
->buf
;
150 surface
.group_id
= MEMSLOT_GROUP_HOST
;
151 ssd
->worker
->create_primary_surface(ssd
->worker
, 0, &surface
);
154 void qemu_spice_destroy_host_primary(SimpleSpiceDisplay
*ssd
)
157 fprintf(stderr
, "%s:\n", __FUNCTION__
);
159 ssd
->worker
->destroy_primary_surface(ssd
->worker
, 0);
162 void qemu_spice_vm_change_state_handler(void *opaque
, int running
, int reason
)
164 SimpleSpiceDisplay
*ssd
= opaque
;
167 ssd
->worker
->start(ssd
->worker
);
169 ssd
->worker
->stop(ssd
->worker
);
171 ssd
->running
= running
;
174 /* display listener callbacks */
176 void qemu_spice_display_update(SimpleSpiceDisplay
*ssd
,
177 int x
, int y
, int w
, int h
)
179 SpiceRect update_area
;
182 fprintf(stderr
, "%s: x %d y %d w %d h %d\n", __FUNCTION__
, x
, y
, w
, h
);
183 update_area
.left
= x
,
184 update_area
.right
= x
+ w
;
186 update_area
.bottom
= y
+ h
;
188 pthread_mutex_lock(&ssd
->lock
);
189 if (qemu_spice_rect_is_empty(&ssd
->dirty
)) {
192 qemu_spice_rect_union(&ssd
->dirty
, &update_area
);
193 pthread_mutex_unlock(&ssd
->lock
);
196 void qemu_spice_display_resize(SimpleSpiceDisplay
*ssd
)
199 fprintf(stderr
, "%s:\n", __FUNCTION__
);
201 pthread_mutex_lock(&ssd
->lock
);
202 memset(&ssd
->dirty
, 0, sizeof(ssd
->dirty
));
203 pthread_mutex_unlock(&ssd
->lock
);
205 qemu_spice_destroy_host_primary(ssd
);
206 qemu_spice_create_host_primary(ssd
);
207 qemu_pf_conv_put(ssd
->conv
);
210 pthread_mutex_lock(&ssd
->lock
);
212 ssd
->dirty
.right
= ds_get_width(ssd
->ds
);
214 ssd
->dirty
.bottom
= ds_get_height(ssd
->ds
);
216 pthread_mutex_unlock(&ssd
->lock
);
219 void qemu_spice_display_refresh(SimpleSpiceDisplay
*ssd
)
222 fprintf(stderr
, "%s:\n", __FUNCTION__
);
226 ssd
->worker
->wakeup(ssd
->worker
);
228 fprintf(stderr
, "%s: notify\n", __FUNCTION__
);
232 /* spice display interface callbacks */
234 static void interface_attach_worker(QXLInstance
*sin
, QXLWorker
*qxl_worker
)
236 SimpleSpiceDisplay
*ssd
= container_of(sin
, SimpleSpiceDisplay
, qxl
);
239 fprintf(stderr
, "%s:\n", __FUNCTION__
);
240 ssd
->worker
= qxl_worker
;
243 static void interface_set_compression_level(QXLInstance
*sin
, int level
)
246 fprintf(stderr
, "%s:\n", __FUNCTION__
);
250 static void interface_set_mm_time(QXLInstance
*sin
, uint32_t mm_time
)
253 fprintf(stderr
, "%s:\n", __FUNCTION__
);
257 static void interface_get_init_info(QXLInstance
*sin
, QXLDevInitInfo
*info
)
259 SimpleSpiceDisplay
*ssd
= container_of(sin
, SimpleSpiceDisplay
, qxl
);
261 info
->memslot_gen_bits
= MEMSLOT_GENERATION_BITS
;
262 info
->memslot_id_bits
= MEMSLOT_SLOT_BITS
;
263 info
->num_memslots
= NUM_MEMSLOTS
;
264 info
->num_memslots_groups
= NUM_MEMSLOTS_GROUPS
;
265 info
->internal_groupslot_id
= 0;
266 info
->qxl_ram_size
= ssd
->bufsize
;
267 info
->n_surfaces
= NUM_SURFACES
;
270 static int interface_get_command(QXLInstance
*sin
, struct QXLCommandExt
*ext
)
272 SimpleSpiceDisplay
*ssd
= container_of(sin
, SimpleSpiceDisplay
, qxl
);
273 SimpleSpiceUpdate
*update
;
276 fprintf(stderr
, "%s:\n", __FUNCTION__
);
277 update
= qemu_spice_create_update(ssd
);
278 if (update
== NULL
) {
285 static int interface_req_cmd_notification(QXLInstance
*sin
)
288 fprintf(stderr
, "%s:\n", __FUNCTION__
);
292 static int interface_has_command(QXLInstance
*sin
)
294 SimpleSpiceDisplay
*ssd
= container_of(sin
, SimpleSpiceDisplay
, qxl
);
297 fprintf(stderr
, "%s:\n", __FUNCTION__
);
298 return !qemu_spice_rect_is_empty(&ssd
->dirty
);
301 static void interface_release_resource(QXLInstance
*sin
,
302 struct QXLReleaseInfoExt ext
)
304 SimpleSpiceDisplay
*ssd
= container_of(sin
, SimpleSpiceDisplay
, qxl
);
308 fprintf(stderr
, "%s:\n", __FUNCTION__
);
310 qemu_spice_destroy_update(ssd
, (void*)id
);
313 static int interface_get_cursor_command(QXLInstance
*sin
, struct QXLCommandExt
*ext
)
316 fprintf(stderr
, "%s:\n", __FUNCTION__
);
320 static int interface_req_cursor_notification(QXLInstance
*sin
)
323 fprintf(stderr
, "%s:\n", __FUNCTION__
);
327 static void interface_notify_update(QXLInstance
*sin
, uint32_t update_id
)
329 fprintf(stderr
, "%s: abort()\n", __FUNCTION__
);
333 static int interface_flush_resources(QXLInstance
*sin
)
335 fprintf(stderr
, "%s: abort()\n", __FUNCTION__
);
340 static const QXLInterface dpy_interface
= {
341 .base
.type
= SPICE_INTERFACE_QXL
,
342 .base
.description
= "qemu simple display",
343 .base
.major_version
= SPICE_INTERFACE_QXL_MAJOR
,
344 .base
.minor_version
= SPICE_INTERFACE_QXL_MINOR
,
346 .pci_vendor
= REDHAT_PCI_VENDOR_ID
,
347 .pci_id
= QXL_DEVICE_ID
,
348 .pci_revision
= QXL_REVISION
,
350 .attache_worker
= interface_attach_worker
,
351 .set_compression_level
= interface_set_compression_level
,
352 .set_mm_time
= interface_set_mm_time
,
354 .get_init_info
= interface_get_init_info
,
355 .get_command
= interface_get_command
,
356 .req_cmd_notification
= interface_req_cmd_notification
,
357 .has_command
= interface_has_command
,
358 .release_resource
= interface_release_resource
,
359 .get_cursor_command
= interface_get_cursor_command
,
360 .req_cursor_notification
= interface_req_cursor_notification
,
361 .notify_update
= interface_notify_update
,
362 .flush_resources
= interface_flush_resources
,
365 static SimpleSpiceDisplay sdpy
;
367 static void display_update(struct DisplayState
*ds
, int x
, int y
, int w
, int h
)
369 qemu_spice_display_update(&sdpy
, x
, y
, w
, h
);
372 static void display_resize(struct DisplayState
*ds
)
374 qemu_spice_display_resize(&sdpy
);
377 static void display_refresh(struct DisplayState
*ds
)
379 qemu_spice_display_refresh(&sdpy
);
382 static DisplayChangeListener display_listener
= {
383 .dpy_update
= display_update
,
384 .dpy_resize
= display_resize
,
385 .dpy_refresh
= display_refresh
,
388 void qemu_spice_display_init(DisplayState
*ds
)
390 assert(sdpy
.ds
== NULL
);
392 sdpy
.bufsize
= (16 * 1024 * 1024);
393 sdpy
.buf
= qemu_malloc(sdpy
.bufsize
);
394 pthread_mutex_init(&sdpy
.lock
, NULL
);
395 register_displaychangelistener(ds
, &display_listener
);
397 sdpy
.qxl
.base
.sif
= &dpy_interface
.base
;
398 spice_server_add_interface(spice_server
, &sdpy
.qxl
.base
);
401 qemu_add_vm_change_state_handler(qemu_spice_vm_change_state_handler
, &sdpy
);
402 qemu_spice_create_host_memslot(&sdpy
);
403 qemu_spice_create_host_primary(&sdpy
);