]> git.proxmox.com Git - spiceterm.git/blob - test_display_base.c
protect command ring with mutex
[spiceterm.git] / test_display_base.c
1 #include <stdlib.h>
2 #include <math.h>
3 #include <string.h>
4 #include <stdio.h>
5 #include <unistd.h>
6 #include <signal.h>
7 #include <wait.h>
8 #include <sys/select.h>
9 #include <sys/types.h>
10 #include <getopt.h>
11
12 #include "glyphs.h"
13 #include <spice.h>
14 #include <spice/enums.h>
15 #include <spice/macros.h>
16 #include <spice/qxl_dev.h>
17
18 #include "test_display_base.h"
19
20 #define MEM_SLOT_GROUP_ID 0
21
22 #define NOTIFY_DISPLAY_BATCH (SINGLE_PART/2)
23 #define NOTIFY_CURSOR_BATCH 10
24
25 /* these colours are from linux kernel drivers/char/vt.c */
26 /* the default colour table, for VGA+ colour systems */
27 int default_red[] = {0x00,0xaa,0x00,0xaa,0x00,0xaa,0x00,0xaa,
28 0x55,0xff,0x55,0xff,0x55,0xff,0x55,0xff};
29 int default_grn[] = {0x00,0x00,0xaa,0x55,0x00,0x00,0xaa,0xaa,
30 0x55,0x55,0xff,0xff,0x55,0x55,0xff,0xff};
31 int default_blu[] = {0x00,0x00,0x00,0x00,0xaa,0xaa,0xaa,0xaa,
32 0x55,0x55,0x55,0x55,0xff,0xff,0xff,0xff};
33
34 /* Parts cribbed from spice-display.h/.c/qxl.c */
35
36 typedef struct SimpleSpiceUpdate {
37 QXLCommandExt ext; // first
38 QXLDrawable drawable;
39 QXLImage image;
40 uint8_t *bitmap;
41 } SimpleSpiceUpdate;
42
43 static void test_spice_destroy_update(SimpleSpiceUpdate *update)
44 {
45 if (!update) {
46 return;
47 }
48 if (update->drawable.clip.type != SPICE_CLIP_TYPE_NONE) {
49 uint8_t *ptr = (uint8_t*)update->drawable.clip.data;
50 free(ptr);
51 }
52 g_free(update->bitmap);
53 g_free(update);
54 }
55
56 #define DEFAULT_WIDTH 640
57 #define DEFAULT_HEIGHT 320
58
59 #define SINGLE_PART 4
60 static const int angle_parts = 64 / SINGLE_PART;
61 static int unique = 1;
62 static int color = -1;
63 static int c_i = 0;
64
65 __attribute__((noreturn))
66 static void sigchld_handler(int signal_num) // wait for the child process and exit
67 {
68 int status;
69 wait(&status);
70 exit(0);
71 }
72
73 static void set_cmd(QXLCommandExt *ext, uint32_t type, QXLPHYSICAL data)
74 {
75 ext->cmd.type = type;
76 ext->cmd.data = data;
77 ext->cmd.padding = 0;
78 ext->group_id = MEM_SLOT_GROUP_ID;
79 ext->flags = 0;
80 }
81
82 static void simple_set_release_info(QXLReleaseInfo *info, intptr_t ptr)
83 {
84 info->id = ptr;
85 //info->group_id = MEM_SLOT_GROUP_ID;
86 }
87
88 /* bitmap are freed, so they must be allocated with g_malloc */
89 SimpleSpiceUpdate *test_spice_create_update_from_bitmap(uint32_t surface_id,
90 QXLRect bbox,
91 uint8_t *bitmap)
92 {
93 SimpleSpiceUpdate *update;
94 QXLDrawable *drawable;
95 QXLImage *image;
96 uint32_t bw, bh;
97
98 bh = bbox.bottom - bbox.top;
99 bw = bbox.right - bbox.left;
100
101 update = g_new0(SimpleSpiceUpdate, 1);
102 update->bitmap = bitmap;
103 drawable = &update->drawable;
104 image = &update->image;
105
106 drawable->surface_id = surface_id;
107
108 drawable->bbox = bbox;
109 drawable->clip.type = SPICE_CLIP_TYPE_NONE;
110 drawable->effect = QXL_EFFECT_OPAQUE;
111 simple_set_release_info(&drawable->release_info, (intptr_t)update);
112 drawable->type = QXL_DRAW_COPY;
113 drawable->surfaces_dest[0] = -1;
114 drawable->surfaces_dest[1] = -1;
115 drawable->surfaces_dest[2] = -1;
116
117 drawable->u.copy.rop_descriptor = SPICE_ROPD_OP_PUT;
118 drawable->u.copy.src_bitmap = (intptr_t)image;
119 drawable->u.copy.src_area.right = bw;
120 drawable->u.copy.src_area.bottom = bh;
121
122 QXL_SET_IMAGE_ID(image, QXL_IMAGE_GROUP_DEVICE, unique);
123 image->descriptor.type = SPICE_IMAGE_TYPE_BITMAP;
124 image->bitmap.flags = QXL_BITMAP_DIRECT | QXL_BITMAP_TOP_DOWN;
125 image->bitmap.stride = bw * 4;
126 image->descriptor.width = image->bitmap.x = bw;
127 image->descriptor.height = image->bitmap.y = bh;
128 image->bitmap.data = (intptr_t)bitmap;
129 image->bitmap.palette = 0;
130 image->bitmap.format = SPICE_BITMAP_FMT_32BIT;
131
132 set_cmd(&update->ext, QXL_CMD_DRAW, (intptr_t)drawable);
133
134 return update;
135 }
136
137 static SimpleSpiceUpdate *test_draw_char(Test *test, int x, int y, int c, int fg, int bg)
138 {
139 int top, left;
140 uint8_t *dst;
141 uint8_t *bitmap;
142 int bw, bh;
143 int i, j;
144 QXLRect bbox;
145
146 left = x*8;
147 top = y*16;
148
149 // printf("DRAWCHAR %d %d %d\n", left, top, c);
150
151 unique++;
152
153 bw = 8;
154 bh = 16;
155
156 bitmap = dst = g_malloc(bw * bh * 4);
157
158 unsigned char *data = vt_font_data + c*16;
159 unsigned char d = *data;
160
161 g_assert(fg >= 0 && fg < 16);
162 g_assert(bg >= 0 && bg < 16);
163
164 unsigned char fgc_red = default_red[fg];
165 unsigned char fgc_blue = default_blu[fg];
166 unsigned char fgc_green = default_grn[fg];
167 unsigned char bgc_red = default_red[bg];
168 unsigned char bgc_blue = default_blu[bg];
169 unsigned char bgc_green = default_grn[bg];
170
171 for (j = 0; j < 16; j++) {
172 for (i = 0; i < 8; i++) {
173 if ((i&7) == 0) {
174 d=*data;
175 data++;
176 }
177 if (d&0x80) {
178 *(dst) = fgc_blue;
179 *(dst+1) = fgc_green;
180 *(dst+2) = fgc_red;
181 *(dst+3) = 0;
182 } else {
183 *(dst) = bgc_blue;
184 *(dst+1) = bgc_green;
185 *(dst+2) = bgc_red;
186 *(dst+3) = 0;
187 }
188 d<<=1;
189 dst += 4;
190 }
191 }
192
193 bbox.left = left; bbox.top = top;
194 bbox.right = left + bw; bbox.bottom = top + bh;
195
196 return test_spice_create_update_from_bitmap(0, bbox, bitmap);
197 }
198
199 static void create_primary_surface(Test *test, uint32_t width,
200 uint32_t height)
201 {
202 QXLWorker *qxl_worker = test->qxl_worker;
203 QXLDevSurfaceCreate surface = { 0, };
204
205 g_assert(height <= MAX_HEIGHT);
206 g_assert(width <= MAX_WIDTH);
207 g_assert(height > 0);
208 g_assert(width > 0);
209
210 surface.format = SPICE_SURFACE_FMT_32_xRGB;
211 surface.width = test->primary_width = width;
212 surface.height = test->primary_height = height;
213 surface.stride = -width * 4; /* negative? */
214 surface.mouse_mode = TRUE; /* unused by red_worker */
215 surface.flags = 0;
216 surface.type = 0; /* unused by red_worker */
217 surface.position = 0; /* unused by red_worker */
218 surface.mem = (uint64_t)&test->primary_surface;
219 surface.group_id = MEM_SLOT_GROUP_ID;
220
221 test->width = width;
222 test->height = height;
223
224 qxl_worker->create_primary_surface(qxl_worker, 0, &surface);
225 }
226
227 QXLDevMemSlot slot = {
228 .slot_group_id = MEM_SLOT_GROUP_ID,
229 .slot_id = 0,
230 .generation = 0,
231 .virt_start = 0,
232 .virt_end = ~0,
233 .addr_delta = 0,
234 .qxl_ram_size = ~0,
235 };
236
237 static void attache_worker(QXLInstance *qin, QXLWorker *_qxl_worker)
238 {
239 Test *test = SPICE_CONTAINEROF(qin, Test, qxl_instance);
240
241 if (test->qxl_worker) {
242 if (test->qxl_worker != _qxl_worker)
243 printf("%s ignored, %p is set, ignoring new %p\n", __func__,
244 test->qxl_worker, _qxl_worker);
245 else
246 printf("%s ignored, redundant\n", __func__);
247 return;
248 }
249 printf("%s\n", __func__);
250 test->qxl_worker = _qxl_worker;
251 test->qxl_worker->add_memslot(test->qxl_worker, &slot);
252 create_primary_surface(test, DEFAULT_WIDTH, DEFAULT_HEIGHT);
253 test->qxl_worker->start(test->qxl_worker);
254 }
255
256 static void set_compression_level(QXLInstance *qin, int level)
257 {
258 printf("%s\n", __func__);
259 }
260
261 static void set_mm_time(QXLInstance *qin, uint32_t mm_time)
262 {
263 }
264 static void get_init_info(QXLInstance *qin, QXLDevInitInfo *info)
265 {
266 memset(info, 0, sizeof(*info));
267 info->num_memslots = 1;
268 info->num_memslots_groups = 1;
269 info->memslot_id_bits = 1;
270 info->memslot_gen_bits = 1;
271 info->n_surfaces = 1;
272 }
273
274
275 // We shall now have a ring of commands, so that we can update
276 // it from a separate thread - since get_command is called from
277 // the worker thread, and we need to sometimes do an update_area,
278 // which cannot be done from red_worker context (not via dispatcher,
279 // since you get a deadlock, and it isn't designed to be done
280 // any other way, so no point testing that).
281
282
283 static void push_command(Test *test, QXLCommandExt *ext)
284 {
285 g_mutex_lock(test->command_mutex);
286
287 while (test->commands_end - test->commands_start >= COMMANDS_SIZE) {
288 g_cond_wait(test->command_cond, test->command_mutex);
289 }
290 g_assert(test->commands_end - test->commands_start < COMMANDS_SIZE);
291 test->commands[test->commands_end % COMMANDS_SIZE] = ext;
292 test->commands_end++;
293 g_mutex_unlock(test->command_mutex);
294
295 test->qxl_worker->wakeup(test->qxl_worker);
296 }
297
298 // called from spice_server thread (i.e. red_worker thread)
299 static int get_command(QXLInstance *qin, struct QXLCommandExt *ext)
300 {
301 Test *test = SPICE_CONTAINEROF(qin, Test, qxl_instance);
302 int res = FALSE;
303
304 g_mutex_lock(test->command_mutex);
305
306 if ((test->commands_end - test->commands_start) == 0) {
307 res = FALSE;
308 goto ret;
309 }
310
311 *ext = *test->commands[test->commands_start % COMMANDS_SIZE];
312 g_assert(test->commands_start < test->commands_end);
313 test->commands_start++;
314 g_cond_signal(test->command_cond);
315
316 res = TRUE;
317
318 ret:
319 g_mutex_unlock(test->command_mutex);
320 return res;
321 }
322
323 static int req_cmd_notification(QXLInstance *qin)
324 {
325 Test *test = SPICE_CONTAINEROF(qin, Test, qxl_instance);
326
327 //test->core->timer_start(test->wakeup_timer, test->wakeup_ms);
328 return TRUE;
329 }
330
331 static void release_resource(QXLInstance *qin, struct QXLReleaseInfoExt release_info)
332 {
333 QXLCommandExt *ext = (QXLCommandExt*)(unsigned long)release_info.info->id;
334 //printf("%s\n", __func__);
335 g_assert(release_info.group_id == MEM_SLOT_GROUP_ID);
336 switch (ext->cmd.type) {
337 case QXL_CMD_DRAW:
338 test_spice_destroy_update((void*)ext);
339 break;
340 case QXL_CMD_SURFACE:
341 free(ext);
342 break;
343 case QXL_CMD_CURSOR: {
344 QXLCursorCmd *cmd = (QXLCursorCmd *)(unsigned long)ext->cmd.data;
345 if (cmd->type == QXL_CURSOR_SET) {
346 free(cmd);
347 }
348 free(ext);
349 break;
350 }
351 default:
352 abort();
353 }
354 }
355
356 #define CURSOR_WIDTH 32
357 #define CURSOR_HEIGHT 32
358
359 static struct {
360 QXLCursor cursor;
361 uint8_t data[CURSOR_WIDTH * CURSOR_HEIGHT * 4]; // 32bit per pixel
362 } cursor;
363
364 static void cursor_init()
365 {
366 cursor.cursor.header.unique = 0;
367 cursor.cursor.header.type = SPICE_CURSOR_TYPE_COLOR32;
368 cursor.cursor.header.width = CURSOR_WIDTH;
369 cursor.cursor.header.height = CURSOR_HEIGHT;
370 cursor.cursor.header.hot_spot_x = 0;
371 cursor.cursor.header.hot_spot_y = 0;
372 cursor.cursor.data_size = CURSOR_WIDTH * CURSOR_HEIGHT * 4;
373
374 // X drivers addes it to the cursor size because it could be
375 // cursor data information or another cursor related stuffs.
376 // Otherwise, the code will break in client/cursor.cpp side,
377 // that expect the data_size plus cursor information.
378 // Blame cursor protocol for this. :-)
379 cursor.cursor.data_size += 128;
380 cursor.cursor.chunk.data_size = cursor.cursor.data_size;
381 cursor.cursor.chunk.prev_chunk = cursor.cursor.chunk.next_chunk = 0;
382 }
383
384 static int get_cursor_command(QXLInstance *qin, struct QXLCommandExt *ext)
385 {
386 Test *test = SPICE_CONTAINEROF(qin, Test, qxl_instance);
387 static int color = 0;
388 static int set = 1;
389 static int x = 0, y = 0;
390 QXLCursorCmd *cursor_cmd;
391 QXLCommandExt *cmd;
392
393 if (!test->cursor_notify) {
394 return FALSE;
395 }
396
397 test->cursor_notify--;
398 cmd = calloc(sizeof(QXLCommandExt), 1);
399 cursor_cmd = calloc(sizeof(QXLCursorCmd), 1);
400
401 cursor_cmd->release_info.id = (unsigned long)cmd;
402
403 if (set) {
404 cursor_cmd->type = QXL_CURSOR_SET;
405 cursor_cmd->u.set.position.x = 0;
406 cursor_cmd->u.set.position.y = 0;
407 cursor_cmd->u.set.visible = TRUE;
408 cursor_cmd->u.set.shape = (unsigned long)&cursor;
409 // Only a white rect (32x32) as cursor
410 memset(cursor.data, 255, sizeof(cursor.data));
411 set = 0;
412 } else {
413 cursor_cmd->type = QXL_CURSOR_MOVE;
414 cursor_cmd->u.position.x = x++ % test->primary_width;
415 cursor_cmd->u.position.y = y++ % test->primary_height;
416 }
417
418 cmd->cmd.data = (unsigned long)cursor_cmd;
419 cmd->cmd.type = QXL_CMD_CURSOR;
420 cmd->group_id = MEM_SLOT_GROUP_ID;
421 cmd->flags = 0;
422 *ext = *cmd;
423 //printf("%s\n", __func__);
424 return TRUE;
425 }
426
427 static int req_cursor_notification(QXLInstance *qin)
428 {
429 printf("%s\n", __func__);
430 return TRUE;
431 }
432
433 static void notify_update(QXLInstance *qin, uint32_t update_id)
434 {
435 printf("%s\n", __func__);
436 }
437
438 static int flush_resources(QXLInstance *qin)
439 {
440 printf("%s\n", __func__);
441 return TRUE;
442 }
443
444 static int client_monitors_config(QXLInstance *qin,
445 VDAgentMonitorsConfig *monitors_config)
446 {
447 if (!monitors_config) {
448 printf("%s: NULL monitors_config\n", __func__);
449 } else {
450 printf("%s: %d\n", __func__, monitors_config->num_of_monitors);
451 }
452 return 0;
453 }
454
455 static void set_client_capabilities(QXLInstance *qin,
456 uint8_t client_present,
457 uint8_t caps[58])
458 {
459 Test *test = SPICE_CONTAINEROF(qin, Test, qxl_instance);
460
461 printf("%s: present %d caps %d\n", __func__, client_present, caps[0]);
462 if (test->on_client_connected && client_present) {
463 test->on_client_connected(test);
464 }
465 if (test->on_client_disconnected && !client_present) {
466 test->on_client_disconnected(test);
467 }
468 }
469
470 static int client_count = 0;
471
472 static void client_connected(Test *test)
473 {
474 printf("Client connected\n");
475 client_count++;
476 }
477
478 static void client_disconnected(Test *test)
479 {
480
481 if (client_count > 0) {
482 client_count--;
483 printf("Client disconnected\n");
484 exit(0); // fixme: cleanup?
485 }
486 }
487
488 static void do_conn_timeout(void *opaque)
489 {
490 Test *test = opaque;
491
492 if (client_count <= 0) {
493 printf("do_conn_timeout\n");
494 exit (0); // fixme: cleanup?
495 }
496 }
497
498
499 QXLInterface display_sif = {
500 .base = {
501 .type = SPICE_INTERFACE_QXL,
502 .description = "test",
503 .major_version = SPICE_INTERFACE_QXL_MAJOR,
504 .minor_version = SPICE_INTERFACE_QXL_MINOR
505 },
506 .attache_worker = attache_worker,
507 .set_compression_level = set_compression_level,
508 .set_mm_time = set_mm_time,
509 .get_init_info = get_init_info,
510
511 /* the callbacks below are called from spice server thread context */
512 .get_command = get_command,
513 .req_cmd_notification = req_cmd_notification,
514 .release_resource = release_resource,
515 .get_cursor_command = get_cursor_command,
516 .req_cursor_notification = req_cursor_notification,
517 .notify_update = notify_update,
518 .flush_resources = flush_resources,
519 .client_monitors_config = client_monitors_config,
520 .set_client_capabilities = set_client_capabilities,
521 };
522
523 /* interface for tests */
524 void test_add_display_interface(Test* test)
525 {
526 spice_server_add_interface(test->server, &test->qxl_instance.base);
527 }
528
529 static int vmc_write(SpiceCharDeviceInstance *sin, const uint8_t *buf, int len)
530 {
531 // printf("%s: %d\n", __func__, len);
532 return len;
533 }
534
535 static int vmc_read(SpiceCharDeviceInstance *sin, uint8_t *buf, int len)
536 {
537 // printf("%s: %d\n", __func__, len);
538 return 0;
539 }
540
541 static void vmc_state(SpiceCharDeviceInstance *sin, int connected)
542 {
543 // printf("%s: %d\n", __func__, connected);
544 }
545
546 static SpiceCharDeviceInterface vdagent_sif = {
547 .base.type = SPICE_INTERFACE_CHAR_DEVICE,
548 .base.description = "test spice virtual channel char device",
549 .base.major_version = SPICE_INTERFACE_CHAR_DEVICE_MAJOR,
550 .base.minor_version = SPICE_INTERFACE_CHAR_DEVICE_MINOR,
551 .state = vmc_state,
552 .write = vmc_write,
553 .read = vmc_read,
554 };
555
556 SpiceCharDeviceInstance vdagent_sin = {
557 .base = {
558 .sif = &vdagent_sif.base,
559 },
560 .subtype = "vdagent",
561 };
562
563 void test_add_agent_interface(SpiceServer *server)
564 {
565 spice_server_add_interface(server, &vdagent_sin.base);
566 }
567
568 static int my_charcode = 65;
569 static int my_posx = 0;
570 static void kbd_push_key(SpiceKbdInstance *sin, uint8_t frag)
571 {
572 Test *test = SPICE_CONTAINEROF(sin, Test, keyboard_sin);
573
574 printf("KEYCODE %u %p\n", frag, test);
575
576 }
577
578 void test_draw_update_char(Test *test, int x, int y, int c, TextAttributes attrib)
579 {
580 int fg, bg;
581
582 if (attrib.invers) {
583 bg = attrib.fgcol;
584 fg = attrib.bgcol;
585 } else {
586 bg = attrib.bgcol;
587 fg = attrib.fgcol;
588 }
589
590 if (attrib.bold) {
591 fg += 8;
592 }
593
594 // unsuported attributes = (attrib.blink || attrib.unvisible)
595
596 //if (attrib.uline) {
597 //rfbDrawLine (vt->screen, rx, ry + 14, rxe, ry + 14, fg);
598 //}
599
600 SimpleSpiceUpdate *update;
601 update = test_draw_char(test, x, y, c, fg, bg);
602 push_command(test, &update->ext);
603 }
604
605 static uint8_t kbd_get_leds(SpiceKbdInstance *sin)
606 {
607 return 0;
608 }
609
610 static SpiceKbdInterface keyboard_sif = {
611 .base.type = SPICE_INTERFACE_KEYBOARD ,
612 .base.description = "spiceterm keyboard device",
613 .base.major_version = SPICE_INTERFACE_KEYBOARD_MAJOR,
614 .base.minor_version = SPICE_INTERFACE_KEYBOARD_MINOR,
615 .push_scan_freg = kbd_push_key,
616 .get_leds = kbd_get_leds,
617 };
618
619 void test_add_keyboard_interface(Test* test)
620 {
621 spice_server_add_interface(test->server, &test->keyboard_sin.base);
622 }
623
624 Test *test_new(SpiceCoreInterface *core)
625 {
626 int port = 5912;
627 Test *test = g_new0(Test, 1);
628 SpiceServer* server = spice_server_new();
629
630 test->command_cond = g_cond_new();
631 test->command_mutex = g_mutex_new();
632
633 test->on_client_connected = client_connected,
634 test->on_client_disconnected = client_disconnected,
635
636 test->qxl_instance.base.sif = &display_sif.base;
637 test->qxl_instance.id = 0;
638
639 test->keyboard_sin.base.sif = &keyboard_sif.base;
640
641 test->core = core;
642 test->server = server;
643
644 test->cursor_notify = NOTIFY_CURSOR_BATCH;
645 // some common initialization for all display tests
646 printf("TESTER: listening on port %d (unsecure)\n", port);
647 spice_server_set_port(server, port);
648 spice_server_set_noauth(server);
649 int res = spice_server_init(server, core);
650 if (res != 0) {
651 g_error("spice_server_init failed, res = %d\n", res);
652 }
653
654 cursor_init();
655 test->has_secondary = 0;
656
657 int timeout = 10; // max time to wait for client connection
658 test->conn_timeout_timer = core->timer_add(do_conn_timeout, test);
659 test->core->timer_start(test->conn_timeout_timer, timeout*1000);
660
661 return test;
662 }
663
664
665 __attribute__((noreturn))
666 void usage(const char *argv0, const int exitcode)
667 {
668
669 printf("usage: %s\n", argv0);
670 exit(exitcode);
671 }
672
673 void spice_test_config_parse_args(int argc, char **argv)
674 {
675 struct option options[] = {
676 // {"automated-tests", no_argument, &has_automated_tests, 1},
677 {NULL, 0, NULL, 0},
678 };
679 int option_index;
680 int val;
681
682 while ((val = getopt_long(argc, argv, "", options, &option_index)) != -1) {
683 switch (val) {
684 case '?':
685 printf("unrecognized option '%s'\n", argv[optind - 1]);
686 usage(argv[0], EXIT_FAILURE);
687 case 0:
688 break;
689 }
690 }
691
692 if (argc > optind) {
693 printf("unknown argument '%s'\n", argv[optind]);
694 usage(argv[0], EXIT_FAILURE);
695 }
696 return;
697 }