2 * Copyright (C) 2013 Samsung Electronics Co.Ltd
4 * Inki Dae <inki.dae@samsung.com>
6 * This program is free software; you can redistribute it and/or modify it
7 * under the terms of the GNU General Public License as published by the
8 * Free Software Foundation; either version 2 of the License, or (at your
9 * option) any later version.
25 #include <linux/stddef.h>
28 #include <xf86drmMode.h>
30 #include <drm_fourcc.h>
32 #include "exynos_drm.h"
33 #include "exynos_drmif.h"
34 #include "exynos_fimg2d.h"
36 #define DRM_MODULE_NAME "exynos"
37 #define MAX_TEST_CASE 8
39 static unsigned int screen_width, screen_height;
42 * A structure to test fimg2d hw.
44 * @solid_fill: fill given color data to source buffer.
45 * @copy: copy source to destination buffer.
46 * @copy_with_scale: copy source to destination buffer scaling up or
48 * @blend: blend source to destination buffer.
50 struct fimg2d_test_case {
51 int (*solid_fill)(struct exynos_device *dev, struct exynos_bo *dst);
52 int (*copy)(struct exynos_device *dev, struct exynos_bo *src,
53 struct exynos_bo *dst, enum e_g2d_buf_type);
54 int (*copy_with_scale)(struct exynos_device *dev,
55 struct exynos_bo *src, struct exynos_bo *dst,
57 int (*blend)(struct exynos_device *dev,
58 struct exynos_bo *src, struct exynos_bo *dst,
60 int (*checkerboard)(struct exynos_device *dev,
61 struct exynos_bo *src, struct exynos_bo *dst,
70 drmModeModeInfo *mode;
71 drmModeEncoder *encoder;
75 unsigned int fb_id[2], current_fb_id;
81 static void connector_find_mode(int fd, struct connector *c,
82 drmModeRes *resources)
84 drmModeConnector *connector;
87 /* First, find the connector & mode */
89 for (i = 0; i < resources->count_connectors; i++) {
90 connector = drmModeGetConnector(fd, resources->connectors[i]);
93 fprintf(stderr, "could not get connector %i: %s\n",
94 resources->connectors[i], strerror(errno));
95 drmModeFreeConnector(connector);
99 if (!connector->count_modes) {
100 drmModeFreeConnector(connector);
104 if (connector->connector_id != c->id) {
105 drmModeFreeConnector(connector);
109 for (j = 0; j < connector->count_modes; j++) {
110 c->mode = &connector->modes[j];
111 if (!strcmp(c->mode->name, c->mode_str))
115 /* Found it, break out */
119 drmModeFreeConnector(connector);
123 fprintf(stderr, "failed to find mode \"%s\"\n", c->mode_str);
127 /* Now get the encoder */
128 for (i = 0; i < resources->count_encoders; i++) {
129 c->encoder = drmModeGetEncoder(fd, resources->encoders[i]);
132 fprintf(stderr, "could not get encoder %i: %s\n",
133 resources->encoders[i], strerror(errno));
134 drmModeFreeEncoder(c->encoder);
138 if (c->encoder->encoder_id == connector->encoder_id)
141 drmModeFreeEncoder(c->encoder);
145 c->crtc = c->encoder->crtc_id;
148 static int connector_find_plane(int fd, unsigned int *plane_id)
150 drmModePlaneRes *plane_resources;
154 plane_resources = drmModeGetPlaneResources(fd);
155 if (!plane_resources) {
156 fprintf(stderr, "drmModeGetPlaneResources failed: %s\n",
161 for (i = 0; i < plane_resources->count_planes; i++) {
164 ovr = drmModeGetPlane(fd, plane_resources->planes[i]);
166 fprintf(stderr, "drmModeGetPlane failed: %s\n",
171 if (ovr->possible_crtcs & (1 << 0))
172 plane_id[i] = ovr->plane_id;
173 drmModeFreePlane(ovr);
179 static int drm_set_crtc(struct exynos_device *dev, struct connector *c,
184 ret = drmModeSetCrtc(dev->fd, c->crtc,
185 fb_id, 0, 0, &c->id, 1, c->mode);
187 drmMsg("failed to set mode: %s\n", strerror(errno));
197 static struct exynos_bo *exynos_create_buffer(struct exynos_device *dev,
201 struct exynos_bo *bo;
203 bo = exynos_bo_create(dev, size, flags);
207 if (!exynos_bo_map(bo)) {
208 exynos_bo_destroy(bo);
215 /* Allocate buffer and fill it with checkerboard pattern, where the tiles *
216 * have a random color. The caller has to free the buffer. */
217 void *create_checkerboard_pattern(unsigned int num_tiles_x,
218 unsigned int num_tiles_y, unsigned int tile_size)
221 unsigned int x, y, i, j;
222 const unsigned int stride = num_tiles_x * tile_size;
224 if (posix_memalign((void*)&buf, 64, num_tiles_y * tile_size * stride * 4) != 0)
227 for (x = 0; x < num_tiles_x; ++x) {
228 for (y = 0; y < num_tiles_y; ++y) {
229 const unsigned int color = 0xff000000 + (random() & 0xffffff);
231 for (i = 0; i < tile_size; ++i) {
232 for (j = 0; j < tile_size; ++j) {
233 buf[x * tile_size + y * stride * tile_size + i + j * stride] = color;
242 static void exynos_destroy_buffer(struct exynos_bo *bo)
244 exynos_bo_destroy(bo);
247 static void wait_for_user_input(int last)
249 printf("press <ENTER> to %s\n", last ? "exit test application" :
250 "skip to next test");
255 static int g2d_solid_fill_test(struct exynos_device *dev, struct exynos_bo *dst)
257 struct g2d_context *ctx;
258 struct g2d_image img = {0};
259 unsigned int count, img_w, img_h;
262 ctx = g2d_init(dev->fd);
266 img.bo[0] = dst->handle;
268 printf("solid fill test.\n");
271 img_w = screen_width;
272 img_h = screen_height;
274 for (count = 0; count < 2; count++) {
275 unsigned int x, y, w, h;
277 x = rand() % (img_w / 2);
278 y = rand() % (img_h / 2);
279 w = rand() % (img_w - x);
280 h = rand() % (img_h - y);
284 img.stride = img.width * 4;
285 img.color_mode = G2D_COLOR_FMT_ARGB8888 | G2D_ORDER_AXRGB;
286 img.color = 0xff000000 + (random() & 0xffffff);
288 ret = g2d_solid_fill(ctx, &img, x, y, w, h);
303 static int g2d_copy_test(struct exynos_device *dev, struct exynos_bo *src,
304 struct exynos_bo *dst,
305 enum e_g2d_buf_type type)
307 struct g2d_context *ctx;
308 struct g2d_image src_img = {0}, dst_img = {0};
309 unsigned int src_x, src_y, dst_x, dst_y, img_w, img_h;
310 unsigned long userptr, size;
313 ctx = g2d_init(dev->fd);
317 dst_img.bo[0] = dst->handle;
323 img_w = screen_width;
324 img_h = screen_height;
328 src_img.bo[0] = src->handle;
330 case G2D_IMGBUF_USERPTR:
331 size = img_w * img_h * 4;
333 userptr = (unsigned long)malloc(size);
335 fprintf(stderr, "failed to allocate userptr.\n");
339 src_img.user_ptr[0].userptr = userptr;
340 src_img.user_ptr[0].size = size;
343 type = G2D_IMGBUF_GEM;
347 printf("copy test with %s.\n",
348 type == G2D_IMGBUF_GEM ? "gem" : "userptr");
350 src_img.width = img_w;
351 src_img.height = img_h;
352 src_img.stride = src_img.width * 4;
353 src_img.buf_type = type;
354 src_img.color_mode = G2D_COLOR_FMT_ARGB8888 | G2D_ORDER_AXRGB;
355 src_img.color = 0xffff0000;
356 ret = g2d_solid_fill(ctx, &src_img, src_x, src_y, img_w, img_h);
358 goto err_free_userptr;
360 dst_img.width = img_w;
361 dst_img.height = img_h;
362 dst_img.stride = dst_img.width * 4;
363 dst_img.buf_type = G2D_IMGBUF_GEM;
364 dst_img.color_mode = G2D_COLOR_FMT_ARGB8888 | G2D_ORDER_AXRGB;
366 ret = g2d_copy(ctx, &src_img, &dst_img, src_x, src_y, dst_x, dst_y,
367 img_w - 4, img_h - 4);
369 goto err_free_userptr;
374 if (type == G2D_IMGBUF_USERPTR)
376 free((void *)userptr);
383 static int g2d_copy_with_scale_test(struct exynos_device *dev,
384 struct exynos_bo *src,
385 struct exynos_bo *dst,
386 enum e_g2d_buf_type type)
388 struct g2d_context *ctx;
389 struct g2d_image src_img = {0}, dst_img = {0};
390 unsigned int src_x, src_y, img_w, img_h;
391 unsigned long userptr, size;
394 ctx = g2d_init(dev->fd);
398 dst_img.bo[0] = dst->handle;
402 img_w = screen_width;
403 img_h = screen_height;
407 src_img.bo[0] = src->handle;
409 case G2D_IMGBUF_USERPTR:
410 size = img_w * img_h * 4;
412 userptr = (unsigned long)malloc(size);
414 fprintf(stderr, "failed to allocate userptr.\n");
418 src_img.user_ptr[0].userptr = userptr;
419 src_img.user_ptr[0].size = size;
422 type = G2D_IMGBUF_GEM;
426 printf("copy and scale test with %s.\n",
427 type == G2D_IMGBUF_GEM ? "gem" : "userptr");
429 src_img.width = img_w;
430 src_img.height = img_h;
431 src_img.stride = src_img.width * 4;
432 src_img.buf_type = type;
433 src_img.color_mode = G2D_COLOR_FMT_ARGB8888 | G2D_ORDER_AXRGB;
434 src_img.color = 0xffffffff;
435 ret = g2d_solid_fill(ctx, &src_img, src_x, src_y, img_w , img_h);
437 goto err_free_userptr;
439 src_img.color = 0xff00ff00;
440 ret = g2d_solid_fill(ctx, &src_img, 5, 5, 100, 100);
442 goto err_free_userptr;
444 dst_img.width = img_w;
445 dst_img.height = img_h;
446 dst_img.buf_type = G2D_IMGBUF_GEM;
447 dst_img.stride = dst_img.width * 4;
448 dst_img.color_mode = G2D_COLOR_FMT_ARGB8888 | G2D_ORDER_AXRGB;
450 ret = g2d_copy_with_scale(ctx, &src_img, &dst_img, 5, 5, 100, 100,
451 100, 100, 200, 200, 0);
453 goto err_free_userptr;
458 if (type == G2D_IMGBUF_USERPTR)
460 free((void *)userptr);
467 static int g2d_blend_test(struct exynos_device *dev,
468 struct exynos_bo *src,
469 struct exynos_bo *dst,
470 enum e_g2d_buf_type type)
472 struct g2d_context *ctx;
473 struct g2d_image src_img = {0}, dst_img = {0};
474 unsigned int src_x, src_y, dst_x, dst_y, img_w, img_h;
475 unsigned long userptr, size;
478 ctx = g2d_init(dev->fd);
482 dst_img.bo[0] = dst->handle;
488 img_w = screen_width;
489 img_h = screen_height;
493 src_img.bo[0] = src->handle;
495 case G2D_IMGBUF_USERPTR:
496 size = img_w * img_h * 4;
498 userptr = (unsigned long)malloc(size);
500 fprintf(stderr, "failed to allocate userptr.\n");
504 src_img.user_ptr[0].userptr = userptr;
505 src_img.user_ptr[0].size = size;
508 type = G2D_IMGBUF_GEM;
512 printf("blend test with %s.\n",
513 type == G2D_IMGBUF_GEM ? "gem" : "userptr");
515 src_img.width = img_w;
516 src_img.height = img_h;
517 src_img.stride = src_img.width * 4;
518 src_img.buf_type = type;
519 src_img.select_mode = G2D_SELECT_MODE_NORMAL;
520 src_img.color_mode = G2D_COLOR_FMT_ARGB8888 | G2D_ORDER_AXRGB;
521 src_img.color = 0xffffffff;
522 ret = g2d_solid_fill(ctx, &src_img, src_x, src_y, img_w, img_h);
524 goto err_free_userptr;
526 src_img.color = 0x770000ff;
527 ret = g2d_solid_fill(ctx, &src_img, 5, 5, 200, 200);
529 goto err_free_userptr;
531 dst_img.width = img_w;
532 dst_img.height = img_h;
533 dst_img.stride = dst_img.width * 4;
534 dst_img.buf_type = G2D_IMGBUF_GEM;
535 dst_img.select_mode = G2D_SELECT_MODE_NORMAL;
536 dst_img.color_mode = G2D_COLOR_FMT_ARGB8888 | G2D_ORDER_AXRGB;
537 dst_img.color = 0xffffffff;
538 ret = g2d_solid_fill(ctx, &dst_img, dst_x, dst_y, img_w, img_h);
540 goto err_free_userptr;
542 dst_img.color = 0x77ff0000;
543 ret = g2d_solid_fill(ctx, &dst_img, 105, 105, 200, 200);
545 goto err_free_userptr;
547 ret = g2d_blend(ctx, &src_img, &dst_img, 5, 5, 105, 105, 200, 200,
550 goto err_free_userptr;
555 if (type == G2D_IMGBUF_USERPTR)
557 free((void *)userptr);
564 static int g2d_checkerboard_test(struct exynos_device *dev,
565 struct exynos_bo *src,
566 struct exynos_bo *dst,
567 enum e_g2d_buf_type type)
569 struct g2d_context *ctx;
570 struct g2d_image src_img = {0}, dst_img = {0};
571 unsigned int src_x, src_y, dst_x, dst_y, img_w, img_h;
572 void *checkerboard = NULL;
575 ctx = g2d_init(dev->fd);
579 dst_img.bo[0] = dst->handle;
586 checkerboard = create_checkerboard_pattern(screen_width / 32, screen_height / 32, 32);
587 if (checkerboard == NULL) {
592 img_w = screen_width - (screen_width % 32);
593 img_h = screen_height - (screen_height % 32);
597 memcpy(src->vaddr, checkerboard, img_w * img_h * 4);
598 src_img.bo[0] = src->handle;
600 case G2D_IMGBUF_USERPTR:
601 src_img.user_ptr[0].userptr = (unsigned long)checkerboard;
602 src_img.user_ptr[0].size = img_w * img_h * 4;
609 printf("checkerboard test with %s.\n",
610 type == G2D_IMGBUF_GEM ? "gem" : "userptr");
612 src_img.width = img_w;
613 src_img.height = img_h;
614 src_img.stride = src_img.width * 4;
615 src_img.buf_type = type;
616 src_img.color_mode = G2D_COLOR_FMT_ARGB8888 | G2D_ORDER_AXRGB;
618 dst_img.width = screen_width;
619 dst_img.height = screen_height;
620 dst_img.stride = dst_img.width * 4;
621 dst_img.buf_type = G2D_IMGBUF_GEM;
622 dst_img.color_mode = G2D_COLOR_FMT_ARGB8888 | G2D_ORDER_AXRGB;
623 src_img.color = 0xff000000;
624 ret = g2d_solid_fill(ctx, &dst_img, src_x, src_y, screen_width, screen_height);
628 ret = g2d_copy(ctx, &src_img, &dst_img, src_x, src_y, dst_x, dst_y,
642 static struct fimg2d_test_case test_case = {
643 .solid_fill = &g2d_solid_fill_test,
644 .copy = &g2d_copy_test,
645 .copy_with_scale = &g2d_copy_with_scale_test,
646 .blend = &g2d_blend_test,
647 .checkerboard = &g2d_checkerboard_test,
650 static void usage(char *name)
652 fprintf(stderr, "usage: %s [-s]\n", name);
653 fprintf(stderr, "-s <connector_id>@<crtc_id>:<mode>\n");
658 static const char optstr[] = "s:";
660 int main(int argc, char **argv)
662 struct exynos_device *dev;
663 struct exynos_bo *bo, *src;
664 struct connector con;
666 uint32_t handles[4] = {0}, pitches[4] = {0}, offsets[4] = {0};
667 drmModeRes *resources;
670 memset(&con, 0, sizeof(struct connector));
677 while ((c = getopt(argc, argv, optstr)) != -1) {
681 if (sscanf(optarg, "%d:0x%64s",
683 con.mode_str) != 2 &&
684 sscanf(optarg, "%d@%d:%64s",
696 fd = drmOpen(DRM_MODULE_NAME, NULL);
698 fprintf(stderr, "failed to open.\n");
702 dev = exynos_device_create(fd);
708 resources = drmModeGetResources(dev->fd);
710 fprintf(stderr, "drmModeGetResources failed: %s\n",
716 connector_find_mode(dev->fd, &con, resources);
717 drmModeFreeResources(resources);
720 fprintf(stderr, "failed to find usable connector\n");
725 screen_width = con.mode->hdisplay;
726 screen_height = con.mode->vdisplay;
728 if (screen_width == 0 || screen_height == 0) {
729 fprintf(stderr, "failed to find sane resolution on connector\n");
734 printf("screen width = %d, screen height = %d\n", screen_width,
737 bo = exynos_create_buffer(dev, screen_width * screen_height * 4, 0);
743 handles[0] = bo->handle;
744 pitches[0] = screen_width * 4;
747 ret = drmModeAddFB2(dev->fd, screen_width, screen_height,
748 DRM_FORMAT_RGBA8888, handles,
749 pitches, offsets, &fb_id, 0);
751 goto err_destroy_buffer;
755 memset(bo->vaddr, 0xff, screen_width * screen_height * 4);
757 ret = drm_set_crtc(dev, &con, fb_id);
761 ret = test_case.solid_fill(dev, bo);
763 fprintf(stderr, "failed to solid fill operation.\n");
767 wait_for_user_input(0);
769 src = exynos_create_buffer(dev, screen_width * screen_height * 4, 0);
775 ret = test_case.copy(dev, src, bo, G2D_IMGBUF_GEM);
777 fprintf(stderr, "failed to test copy operation.\n");
781 wait_for_user_input(0);
783 ret = test_case.copy_with_scale(dev, src, bo, G2D_IMGBUF_GEM);
785 fprintf(stderr, "failed to test copy and scale operation.\n");
789 wait_for_user_input(0);
791 ret = test_case.checkerboard(dev, src, bo, G2D_IMGBUF_GEM);
793 fprintf(stderr, "failed to issue checkerboard test.\n");
797 wait_for_user_input(1);
800 * The blend test uses the userptr functionality of exynos-drm, which
801 * is currently not safe to use. If the kernel hasn't been build with
802 * exynos-iommu support, then the blend test is going to produce (kernel)
803 * memory corruption, eventually leading to a system crash.
805 * Disable the test for now, until the kernel code has been sanitized.
808 ret = test_case.blend(dev, src, bo, G2D_IMGBUF_USERPTR);
810 fprintf(stderr, "failed to test blend operation.\n");
817 exynos_destroy_buffer(src);
820 drmModeRmFB(dev->fd, fb_id);
823 exynos_destroy_buffer(bo);
827 exynos_device_destroy(dev);