summaryrefslogtreecommitdiffstats
path: root/server/tests/test_display_no_ssl.c
blob: 7edc8dc763a5c5ccce5afdc611028da7ce8d930e (plain)
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
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
#include <stdlib.h>
#include <math.h>
#include <string.h>
#include <stdio.h>
#include <sys/select.h>
#include <spice.h>
#include <spice/qxl_dev.h>
#include "red_channel.h"
#include "test_util.h"
#include "basic_event_loop.h"

/* Parts cribbed from spice-display.h/.c/qxl.c */

typedef struct SimpleSpiceUpdate {
    QXLDrawable drawable;
    QXLImage image;
    QXLCommandExt ext;
    uint8_t *bitmap;
} SimpleSpiceUpdate;

void test_spice_destroy_update(SimpleSpiceUpdate *update)
{
    free(update->bitmap);
    free(update);
}

#define WIDTH 800
#define HEIGHT 600

static float angle = 0;
static int unique = 1;
static int color = 0;

SimpleSpiceUpdate *test_spice_create_update()
{
    SimpleSpiceUpdate *update;
    QXLDrawable *drawable;
    QXLImage *image;
    QXLCommand *cmd;
    QXLRect bbox = {
        .top = HEIGHT/2 + (HEIGHT/3)*cos(angle),
        .left = WIDTH/2 + (WIDTH/3)*sin(angle),
    };
    uint8_t *dst;
    int bw, bh;
    int i;

    angle += 0.1;
    color = (color + 1) % 10;
    unique++;

    update   = calloc(sizeof(*update), 1);
    drawable = &update->drawable;
    image    = &update->image;
    cmd      = &update->ext.cmd;

    bw       = 64;
    bh       = 48;
    bbox.right = bbox.left + bw;
    bbox.bottom = bbox.top + bh;
    update->bitmap = malloc(bw * bh * 4);
    //printf("allocated %p, %p\n", update, update->bitmap);

    drawable->bbox            = bbox;
    drawable->clip.type       = SPICE_CLIP_TYPE_NONE;
    drawable->effect          = QXL_EFFECT_OPAQUE;
    drawable->release_info.id = (intptr_t)update;
    drawable->type            = QXL_DRAW_COPY;
    drawable->surfaces_dest[0] = -1;
    drawable->surfaces_dest[1] = -1;
    drawable->surfaces_dest[2] = -1;

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

    QXL_SET_IMAGE_ID(image, QXL_IMAGE_GROUP_DEVICE, unique);
    image->descriptor.type   = SPICE_IMAGE_TYPE_BITMAP;
    image->bitmap.flags      = QXL_BITMAP_DIRECT | QXL_BITMAP_TOP_DOWN;
    image->bitmap.stride     = bw * 4;
    image->descriptor.width  = image->bitmap.x = bw;
    image->descriptor.height = image->bitmap.y = bh;
    image->bitmap.data = (intptr_t)(update->bitmap);
    image->bitmap.palette = 0;
    image->bitmap.format = SPICE_BITMAP_FMT_32BIT;

    dst = update->bitmap;
    for (i = 0 ; i < bh * bw ; ++i, dst+=4) {
        *dst = (color+i % 255);
        *(dst+1) = 255 - color;
        *(dst+2) = (color * (color + i)) & 0xff;
        *(dst+3) = 0;
    }

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

    return update;
}

#define MEM_SLOT_GROUP_ID 0

static QXLWorker *qxl_worker = NULL;
static uint8_t primary_surface[HEIGHT * WIDTH * 4];

void create_test_primary_surface(QXLWorker *worker)
{
    QXLDevSurfaceCreate surface;

    surface.format     = SPICE_SURFACE_FMT_32_xRGB;
    surface.width      = WIDTH;
    surface.height     = HEIGHT;
    surface.stride     = -WIDTH * 4;
    surface.mouse_mode = TRUE;
    surface.flags      = 0;
    surface.type       = 0;
    surface.mem        = (intptr_t)&primary_surface;
    surface.group_id   = MEM_SLOT_GROUP_ID;

    qxl_worker->create_primary_surface(qxl_worker, 0, &surface);
}

QXLDevMemSlot slot = {
.slot_group_id = MEM_SLOT_GROUP_ID,
.slot_id = 0,
.generation = 0,
.virt_start = 0,
.virt_end = ~0,
.addr_delta = 0,
.qxl_ram_size = ~0,
};

void attache_worker(QXLInstance *qin, QXLWorker *_qxl_worker)
{
    static int count = 0;
    if (++count > 1) {
        printf("%s ignored\n", __func__);
        return;
    }
    printf("%s\n", __func__);
    qxl_worker = _qxl_worker;
    qxl_worker->add_memslot(qxl_worker, &slot);
    create_test_primary_surface(qxl_worker);
    qxl_worker->start(qxl_worker);
}

void set_compression_level(QXLInstance *qin, int level)
{
    printf("%s\n", __func__);
}

void set_mm_time(QXLInstance *qin, uint32_t mm_time)
{
}

void get_init_info(QXLInstance *qin, QXLDevInitInfo *info)
{
    bzero(info, sizeof(*info));
    info->num_memslots = 1;
    info->num_memslots_groups = 1;
    info->memslot_id_bits = 1;
    info->memslot_gen_bits = 1;
    info->n_surfaces = 1;
}

#define NOTIFY_BATCH 5

int notify = NOTIFY_BATCH;

int get_command(QXLInstance *qin, struct QXLCommandExt *ext)
{
    SimpleSpiceUpdate *update;

    if (!notify--) {
        return FALSE;
    }
    update = test_spice_create_update();
    *ext = update->ext;
    notify = FALSE;
    return TRUE;
}


SpiceTimer *wakeup_timer;
int wakeup_ms = 500;

int req_cmd_notification(QXLInstance *qin)
{
    core->timer_start(wakeup_timer, wakeup_ms);
    return TRUE;
}

void do_wakeup()
{
    notify = NOTIFY_BATCH;
    qxl_worker->wakeup(qxl_worker);
}

void release_resource(QXLInstance *qin, struct QXLReleaseInfoExt release_info)
{
    //printf("%s\n", __func__);
    ASSERT(release_info.group_id == MEM_SLOT_GROUP_ID);
    test_spice_destroy_update((void*)release_info.info->id);
}

int get_cursor_command(QXLInstance *qin, struct QXLCommandExt *cmd)
{
    //printf("%s\n", __func__);
    return FALSE;
}

int req_cursor_notification(QXLInstance *qin)
{
    printf("%s\n", __func__);
    return TRUE;
}

void notify_update(QXLInstance *qin, uint32_t update_id)
{
    printf("%s\n", __func__);
}

int flush_resources(QXLInstance *qin)
{
    printf("%s\n", __func__);
}

QXLInterface display_sif = {
    .base = {
        .type = SPICE_INTERFACE_QXL,
        .description = "test",
        .major_version = SPICE_INTERFACE_QXL_MAJOR,
        .minor_version = SPICE_INTERFACE_QXL_MINOR
    },
    .attache_worker = attache_worker,
    .set_compression_level = set_compression_level,
    .set_mm_time = set_mm_time,
    .get_init_info = get_init_info,
    .get_command = get_command,
    .req_cmd_notification = req_cmd_notification,
    .release_resource = release_resource,
    .get_cursor_command = get_cursor_command,
    .req_cursor_notification = req_cursor_notification,
    .notify_update = notify_update,
    .flush_resources = flush_resources,
};

QXLInstance display_sin = {
    .base = {
        .sif = &display_sif.base,
    },
    .id = 0,
};

SpiceServer *server;
SpiceCoreInterface *core;
SpiceTimer *ping_timer;

void show_channels(SpiceServer *server);

int ping_ms = 100;

void pinger(void *opaque)
{
    // show_channels is not thread safe - fails if disconnections / connections occur
    //show_channels(server);

    core->timer_start(ping_timer, ping_ms);
}

int main()
{
    core = basic_event_loop_init();
    server = spice_server_new();
    bzero(primary_surface, sizeof(primary_surface));
    spice_server_set_port(server, 5912);
    spice_server_set_noauth(server);
    spice_server_init(server, core);
    //spice_server_set_image_compression(server, SPICE_IMAGE_COMPRESS_OFF);
    spice_server_add_interface(server, &display_sin.base);

    ping_timer = core->timer_add(pinger, NULL);
    wakeup_timer = core->timer_add(do_wakeup, NULL);
    core->timer_start(ping_timer, ping_ms);

    basic_event_loop_mainloop();

    return 0;
}