lib/intel_batchbuffer: igt_ prefix for rendercopy/mediafill funcs
[platform/upstream/intel-gpu-tools.git] / tests / gem_render_copy.c
1 /*
2  * Copyright © 2013 Intel Corporation
3  *
4  * Permission is hereby granted, free of charge, to any person obtaining a
5  * copy of this software and associated documentation files (the "Software"),
6  * to deal in the Software without restriction, including without limitation
7  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8  * and/or sell copies of the Software, and to permit persons to whom the
9  * Software is furnished to do so, subject to the following conditions:
10  *
11  * The above copyright notice and this permission notice (including the next
12  * paragraph) shall be included in all copies or substantial portions of the
13  * Software.
14  *
15  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
18  * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
21  * IN THE SOFTWARE.
22  *
23  * Authors:
24  *    Damien Lespiau <damien.lespiau@intel.com>
25  */
26
27 /*
28  * This file is a basic test for the render_copy() function, a very simple
29  * workload for the 3D engine.
30  */
31
32 #include <stdbool.h>
33 #include <unistd.h>
34 #include <cairo.h>
35 #include <assert.h>
36 #include <stdlib.h>
37 #include <sys/ioctl.h>
38 #include <stdio.h>
39 #include <string.h>
40 #include <assert.h>
41 #include <fcntl.h>
42 #include <inttypes.h>
43 #include <errno.h>
44 #include <sys/stat.h>
45 #include <sys/time.h>
46 #include <getopt.h>
47 #include "drm.h"
48 #include "i915_drm.h"
49 #include "drmtest.h"
50 #include "intel_bufmgr.h"
51 #include "intel_batchbuffer.h"
52 #include "intel_gpu_tools.h"
53
54 #define WIDTH 512
55 #define STRIDE (WIDTH*4)
56 #define HEIGHT 512
57 #define SIZE (HEIGHT*STRIDE)
58
59 #define SRC_COLOR       0xffff00ff
60 #define DST_COLOR       0xfff0ff00
61
62 typedef struct {
63         int drm_fd;
64         uint32_t devid;
65         drm_intel_bufmgr *bufmgr;
66         uint32_t linear[WIDTH * HEIGHT];
67 } data_t;
68
69 static void scratch_buf_write_to_png(struct igt_buf *buf, const char *filename)
70 {
71         cairo_surface_t *surface;
72         cairo_status_t ret;
73
74         drm_intel_bo_map(buf->bo, 0);
75         surface = cairo_image_surface_create_for_data(buf->bo->virtual,
76                                                       CAIRO_FORMAT_RGB24,
77                                                       igt_buf_width(buf),
78                                                       igt_buf_height(buf),
79                                                       buf->stride);
80         ret = cairo_surface_write_to_png(surface, filename);
81         if (ret != CAIRO_STATUS_SUCCESS) {
82                 fprintf(stderr, "%s: %s\n", __func__,
83                         cairo_status_to_string(ret));
84         }
85         cairo_surface_destroy(surface);
86         drm_intel_bo_unmap(buf->bo);
87 }
88
89 static void scratch_buf_init(data_t *data, struct igt_buf *buf,
90                              int width, int height, int stride, uint32_t color)
91 {
92         drm_intel_bo *bo;
93         int i;
94
95         bo = drm_intel_bo_alloc(data->bufmgr, "", SIZE, 4096);
96         for (i = 0; i < width * height; i++)
97                 data->linear[i] = color;
98         gem_write(data->drm_fd, bo->handle, 0, data->linear,
99                   sizeof(data->linear));
100
101         buf->bo = bo;
102         buf->stride = stride;
103         buf->tiling = I915_TILING_NONE;
104         buf->size = SIZE;
105 }
106
107 static void
108 scratch_buf_check(data_t *data, struct igt_buf *buf, int x, int y,
109                   uint32_t color)
110 {
111         uint32_t val;
112
113         gem_read(data->drm_fd, buf->bo->handle, 0,
114                  data->linear, sizeof(data->linear));
115         val = data->linear[y * WIDTH + x];
116         if (val != color) {
117                 fprintf(stderr, "Expected 0x%08x, found 0x%08x at (%d,%d)\n",
118                         color, val, x, y);
119                 abort();
120         }
121 }
122
123 int main(int argc, char **argv)
124 {
125         data_t data = {0, };
126         struct intel_batchbuffer *batch = NULL;
127         struct igt_buf src, dst;
128         igt_render_copyfunc_t render_copy = NULL;
129         int opt;
130         int opt_dump_png = false;
131         int opt_dump_aub = drmtest_dump_aub();
132
133         igt_simple_init();
134
135         while ((opt = getopt(argc, argv, "d")) != -1) {
136                 switch (opt) {
137                 case 'd':
138                         opt_dump_png = true;
139                         break;
140                 default:
141                         break;
142                 }
143         }
144
145         igt_fixture {
146                 data.drm_fd = drm_open_any_render();
147                 data.devid = intel_get_drm_devid(data.drm_fd);
148
149                 data.bufmgr = drm_intel_bufmgr_gem_init(data.drm_fd, 4096);
150                 igt_assert(data.bufmgr);
151
152                 render_copy = igt_get_render_copyfunc(data.devid);
153                 igt_require_f(render_copy,
154                               "no render-copy function\n");
155
156                 batch = intel_batchbuffer_alloc(data.bufmgr, data.devid);
157                 igt_assert(batch);
158         }
159
160         scratch_buf_init(&data, &src, WIDTH, HEIGHT, STRIDE, SRC_COLOR);
161         scratch_buf_init(&data, &dst, WIDTH, HEIGHT, STRIDE, DST_COLOR);
162
163         scratch_buf_check(&data, &src, WIDTH / 2, HEIGHT / 2, SRC_COLOR);
164         scratch_buf_check(&data, &dst, WIDTH / 2, HEIGHT / 2, DST_COLOR);
165
166         if (opt_dump_png) {
167                 scratch_buf_write_to_png(&src, "source.png");
168                 scratch_buf_write_to_png(&dst, "destination.png");
169         }
170
171         if (opt_dump_aub) {
172                 drm_intel_bufmgr_gem_set_aub_filename(data.bufmgr,
173                                                       "rendercopy.aub");
174                 drm_intel_bufmgr_gem_set_aub_dump(data.bufmgr, true);
175         }
176
177         render_copy(batch, NULL,
178                     &src, 0, 0, WIDTH, HEIGHT,
179                     &dst, WIDTH / 2, HEIGHT / 2);
180
181         if (opt_dump_png)
182                 scratch_buf_write_to_png(&dst, "result.png");
183
184         if (opt_dump_aub) {
185                 drm_intel_gem_bo_aub_dump_bmp(dst.bo,
186                         0, 0, WIDTH, HEIGHT,
187                         AUB_DUMP_BMP_FORMAT_ARGB_8888,
188                         STRIDE, 0);
189                 drm_intel_bufmgr_gem_set_aub_dump(data.bufmgr, false);
190         } else {
191                 scratch_buf_check(&data, &dst, 10, 10, DST_COLOR);
192                 scratch_buf_check(&data, &dst, WIDTH - 10, HEIGHT - 10, SRC_COLOR);
193         }
194
195         return 0;
196 }