summaryrefslogtreecommitdiff
path: root/tests/qxl/qxl.c
blob: daee5fd5fc78435bac7e50e0ec3fda348be4cc0c (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
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
/*
 * Copyright © 2012 Red Hat
 *
 * Permission is hereby granted, free of charge, to any person obtaining a
 * copy of this software and associated documentation files (the "Software"),
 * to deal in the Software without restriction, including without limitation
 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
 * and/or sell copies of the Software, and to permit persons to whom the
 * Software is furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice (including the next
 * paragraph) shall be included in all copies or substantial portions of the
 * Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
 * IN THE SOFTWARE.
 *
 * Authors:
 *    Alon Levy <alevy@redhat.com>
 */
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stddef.h> // offsetof
#include <sys/mman.h>
#include <errno.h>

#include <qxl_drm.h>
#include <spice/qxl_dev.h>

int qxl_open_fd(void)
{
    return drmOpen("qxl", NULL);
}

enum {
    ALLOC_ONLY = 0,
    ALLOC_DEALLOC,
    COMMAND,
    RELOC_COMMAND,

    LAST
};

// here go any globals I'll need

int parse_command_line(int argc, char **argv)
{
    if (argc == 1) {
        return RELOC_COMMAND;
    }
    {
        int num = atoi(argv[1]);

        if (num >= ALLOC_ONLY && num < LAST) {
            return num;
        }
    }
    // TODO - getopt
}

uint64_t qxl_alloc(int fd, uint32_t size)
{
    struct drm_qxl_alloc alloc;
    int ret;

    alloc.type = QXL_ALLOC_TYPE_DATA;
    alloc.size = size;
    alloc.handle = 0;
    ret = drmIoctl(fd, DRM_IOCTL_QXL_ALLOC, &alloc);
    if (ret) {
        fprintf(stderr, "error doing QXL_ALLOC\n");
    }
    return alloc.handle;
}

void qxl_decref(int fd, uint64_t handle)
{
    struct drm_qxl_decref decref;
    int ret;

    decref.handle = handle;
    ret = drmIoctl(fd, DRM_IOCTL_QXL_DECREF, &decref);
    if (ret) {
        fprintf(stderr, "error doing QXL_DECREF\n");
    }
}

void alloc_only(int fd)
{
    qxl_alloc(fd, 8*1024);
}

void alloc_dealloc(int fd)
{
    uint64_t handle = qxl_alloc(fd, 128*1024);

    qxl_decref(fd, handle);
}

void *qxl_mmap(int fd, uint64_t handle, size_t size)
{
    struct drm_qxl_map qxl_map;
    void *map;

    memset(&qxl_map, 0, sizeof(qxl_map));
    qxl_map.handle = handle;
    qxl_map.offset = 0; // filled by kernel

    if (drmIoctl(fd, DRM_IOCTL_QXL_MAP, &qxl_map)) {
        fprintf(stderr, "error doing QXL_MAP: %s\n", strerror(errno));
        return NULL;
    }

    map = mmap(0, size, PROT_READ | PROT_WRITE, MAP_SHARED, fd,
               qxl_map.offset);
    if (map == MAP_FAILED) {
        fprintf(stderr, "mmap failure: %s\n", strerror(errno));
        return NULL;
    }
    return map;
}

static void set_rect(QXLRect *rect, int top, int left, int bottom, int right)
{
    rect->top = top;
    rect->left = left;
    rect->bottom = bottom;
    rect->right = right;
}

void whiteness_blackness(int fd, int blackness, int top, int left,
                         int bottom, int right)
{
    struct drm_qxl_execbuffer eb;
    struct drm_qxl_command c;
    QXLDrawable drawable = {0};
    int ret;

    drawable.surface_id = 0;
    drawable.type = blackness ? QXL_DRAW_BLACKNESS : QXL_DRAW_WHITENESS;
    drawable.effect = QXL_EFFECT_OPAQUE;
    drawable.self_bitmap = 0;
    drawable.mm_time = 0;
    drawable.surfaces_dest[0] = -1;
    drawable.surfaces_dest[1] = -1;
    drawable.surfaces_dest[2] = -1;
    set_rect(&drawable.bbox, top, left, bottom, right);
    c.type = QXL_CMD_DRAW;
    c.command_size = sizeof(drawable) - sizeof(drawable.release_info);
    c.command = (uint64_t)((uint8_t *)(&drawable) + sizeof(drawable.release_info));
    c.relocs_num = 0;
    c.relocs = 0;
    eb.flags = 0;
    eb.commands_num = 1;
    eb.commands = (uint64_t)&c;
    ret = drmIoctl(fd, DRM_IOCTL_QXL_EXECBUFFER, &eb);
}

void command(int fd)
{
    int r2;
    int blackness = 1;
    for (r2 = 200; r2 >= 50; r2 -= 25, blackness = !blackness) {
        whiteness_blackness(fd, blackness, 250 - r2, 250 - r2, 250 + r2, 250 + r2);
    }
}

static int verbose = 1;

#define INSIDE(x, y, top, left, bottom, right) ((x) >= (left) && (x) <= (right) && (y) >= (top) && (y) <= (bottom))

uint64_t area(uint64_t width, uint64_t height, uint64_t top, uint64_t left,
	      uint64_t bottom, uint64_t right, uint64_t incol, uint64_t outcol)
{
	uint64_t area = width * height;
	uint64_t inarea = (bottom - top + 1) * (right - left + 1);

	if (verbose)
		printf("(%d - %d + 1) (%d - %d + 1) %d\n", bottom, top, right, left,
		       inarea);
	return incol * inarea + outcol * (area - inarea);
}

void reloc_command(int fd)
{
    /* also a number of commands in a single exec_buffer */
    struct drm_qxl_execbuffer eb;
    struct drm_qxl_command c;
    struct drm_qxl_reloc r[2];
    QXLDrawable drawable = {0};
    int ret;
    uint64_t image_handle;
    uint32_t *bitmap;
    QXLImage *qxl_image;
    int x, y;
    int width = 64;
    int height = 64;
    int stride = 4 * width;
    int bitmap_size = stride * height;
    int bo_size = sizeof(*qxl_image) + bitmap_size;
    //uint32_t col[4] = {1, 0, 4, 2};
    uint32_t col[4] = {0xff0000, 0x00f000, 0x000f00, 0x0000ff};

    image_handle = qxl_alloc(fd, bo_size);
    qxl_image = qxl_mmap(fd, image_handle, bo_size);
    if (verbose) {
	    printf("%s: qxl_mmap returned %llx (handle %d, length %d)\n",
		   __FUNCTION__, qxl_image, image_handle, bitmap_size);
    }
    if (!qxl_image) {
        return;
    }
    bitmap = (uint32_t *)&qxl_image[1];
    for (y = 0 ; y < height; ++y) {
        for (x = 0 ; x < width; ++x) {
            *bitmap++ = (INSIDE(x, y, 8, 8, 32, 32) ? col[0] : col[1]) |
                        (INSIDE(x, y, 24, 24, 48, 48) ? col[2] : col[3]);
        }
    }
    /* check that the memory looks like it should */
    {
	uint64_t sum = 0;
	uint64_t expected_sum;
	bitmap = (uint32_t *)&qxl_image[1];
	for (y = 0 ; y < height; ++y) {
            for (x = 0 ; x < width; ++x) {
		sum += *bitmap++;
	    }
	}
	expected_sum = area(width, height, 8, 8, 32, 32, col[0], col[1]) +
		       area(width, height, 24, 24, 48, 48, col[2], col[3]);
	if (verbose)
		printf("%lld, %lld, %lld\n", sum, expected_sum, sum - expected_sum);
    }

    qxl_image->descriptor.id = 1234;
    qxl_image->descriptor.type = SPICE_IMAGE_TYPE_BITMAP;
    qxl_image->descriptor.flags = 0;
    qxl_image->descriptor.width = width;
    qxl_image->descriptor.height = height;
    qxl_image->bitmap.x = width;
    qxl_image->bitmap.y = height;
    qxl_image->bitmap.stride = stride;
    qxl_image->bitmap.palette = 0;
    qxl_image->bitmap.flags = QXL_BITMAP_DIRECT;
    qxl_image->bitmap.format = SPICE_BITMAP_FMT_32BIT;
    qxl_image->bitmap.data = 0xdeadbeed; /* filled in by relocation */
    r[1].src_handle = image_handle;
    r[1].src_offset = offsetof(QXLImage, bitmap.data);
    r[1].dst_handle = image_handle;
    r[1].dst_offset = sizeof(QXLImage);
    drawable.surface_id = 0;
    drawable.type = QXL_DRAW_COPY;
    drawable.effect = QXL_EFFECT_OPAQUE;
    drawable.self_bitmap = 0;
    set_rect(&drawable.self_bitmap_area, 0, 0, 0, 0);
    drawable.mm_time = 0;
    drawable.surfaces_dest[0] = -1;
    drawable.surfaces_dest[1] = -1;
    drawable.surfaces_dest[2] = -1;
    set_rect(&drawable.bbox, 0, 0, height, width);
    drawable.clip.type = SPICE_CLIP_TYPE_NONE;
    // Copy specific
    drawable.u.copy.scale_mode = SPICE_IMAGE_SCALE_MODE_INTERPOLATE;
    drawable.u.copy.rop_descriptor = SPICE_ROPD_OP_PUT;
    drawable.u.copy.mask.bitmap = 0;
    drawable.u.copy.mask.pos.x = 0;
    drawable.u.copy.mask.bitmap = 0;
    drawable.u.copy.mask.flags = 0;
    set_rect(&drawable.u.copy.src_area, 0, 0, width, height);
    drawable.u.copy.src_bitmap = 0xdeadbeed; /* filled in by relocation */
    // Relocation for src_bitmap
    r[0].src_handle = 0; // TODO - special constant for CMD_BO
    r[0].src_offset = offsetof(QXLDrawable, u.copy.src_bitmap);
    r[0].dst_handle = image_handle;
    r[0].dst_offset = 0;
    c.type = QXL_CMD_DRAW;
    c.command_size = sizeof(drawable) - sizeof(drawable.release_info);
    c.command = (uint64_t)((uint8_t *)(&drawable) + sizeof(drawable.release_info));
    c.relocs_num = 2;
    c.relocs = (uint64_t)r;
    eb.flags = 0;
    eb.commands_num = 1;
    eb.commands = (uint64_t)&c;
    ret = drmIoctl(fd, DRM_IOCTL_QXL_EXECBUFFER, &eb);
}

int main(int argc, char **argv)
{
    int fd;

    fd = qxl_open_fd();
    if (fd < 0) {
        fprintf(stderr, "failed to open qxl fd\n");
        return -1;
    }
    {
        int cmd = parse_command_line(argc, argv);

        switch (cmd) {
        case ALLOC_ONLY:
            alloc_only(fd);
            break;
        case ALLOC_DEALLOC:
            alloc_dealloc(fd);
            break;
        case COMMAND:
            command(fd);
            break;
        case RELOC_COMMAND:
            reloc_command(fd);
            break;
        default:
            fprintf(stderr, "unimplemented %d\n", cmd);
        }
    }

    close(fd);
    return 0;
}