kms_rotation_crc: Remove useless comments
[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 <stdlib.h>
36 #include <sys/ioctl.h>
37 #include <stdio.h>
38 #include <string.h>
39 #include <fcntl.h>
40 #include <inttypes.h>
41 #include <errno.h>
42 #include <sys/stat.h>
43 #include <sys/time.h>
44 #include <getopt.h>
45
46 #include <drm.h>
47
48 #include "ioctl_wrappers.h"
49 #include "drmtest.h"
50 #include "intel_bufmgr.h"
51 #include "intel_batchbuffer.h"
52 #include "intel_io.h"
53 #include "intel_chipset.h"
54 #include "igt_aux.h"
55
56 #define WIDTH 512
57 #define STRIDE (WIDTH*4)
58 #define HEIGHT 512
59 #define SIZE (HEIGHT*STRIDE)
60
61 #define SRC_COLOR       0xffff00ff
62 #define DST_COLOR       0xfff0ff00
63
64 typedef struct {
65         int drm_fd;
66         uint32_t devid;
67         drm_intel_bufmgr *bufmgr;
68         uint32_t linear[WIDTH * HEIGHT];
69 } data_t;
70
71 static void scratch_buf_write_to_png(struct igt_buf *buf, const char *filename)
72 {
73         cairo_surface_t *surface;
74         cairo_status_t ret;
75
76         drm_intel_bo_map(buf->bo, 0);
77         surface = cairo_image_surface_create_for_data(buf->bo->virtual,
78                                                       CAIRO_FORMAT_RGB24,
79                                                       igt_buf_width(buf),
80                                                       igt_buf_height(buf),
81                                                       buf->stride);
82         ret = cairo_surface_write_to_png(surface, filename);
83         igt_assert(ret == CAIRO_STATUS_SUCCESS);
84         cairo_surface_destroy(surface);
85         drm_intel_bo_unmap(buf->bo);
86 }
87
88 static void scratch_buf_init(data_t *data, struct igt_buf *buf,
89                              int width, int height, int stride, uint32_t color)
90 {
91         drm_intel_bo *bo;
92         int i;
93
94         bo = drm_intel_bo_alloc(data->bufmgr, "", SIZE, 4096);
95         for (i = 0; i < width * height; i++)
96                 data->linear[i] = color;
97         gem_write(data->drm_fd, bo->handle, 0, data->linear,
98                   sizeof(data->linear));
99
100         buf->bo = bo;
101         buf->stride = stride;
102         buf->tiling = I915_TILING_NONE;
103         buf->size = SIZE;
104 }
105
106 static void
107 scratch_buf_check(data_t *data, struct igt_buf *buf, int x, int y,
108                   uint32_t color)
109 {
110         uint32_t val;
111
112         gem_read(data->drm_fd, buf->bo->handle, 0,
113                  data->linear, sizeof(data->linear));
114         val = data->linear[y * WIDTH + x];
115         igt_assert_f(val == color,
116                      "Expected 0x%08x, found 0x%08x at (%d,%d)\n",
117                      color, val, x, y);
118 }
119
120 int main(int argc, char **argv)
121 {
122         data_t data = {0, };
123         struct intel_batchbuffer *batch = NULL;
124         struct igt_buf src, dst;
125         igt_render_copyfunc_t render_copy = NULL;
126         int opt;
127         int opt_dump_png = false;
128         int opt_dump_aub = igt_aub_dump_enabled();
129
130         igt_simple_init();
131
132         while ((opt = getopt(argc, argv, "d")) != -1) {
133                 switch (opt) {
134                 case 'd':
135                         opt_dump_png = true;
136                         break;
137                 default:
138                         break;
139                 }
140         }
141
142         igt_fixture {
143                 data.drm_fd = drm_open_any_render();
144                 data.devid = intel_get_drm_devid(data.drm_fd);
145
146                 data.bufmgr = drm_intel_bufmgr_gem_init(data.drm_fd, 4096);
147                 igt_assert(data.bufmgr);
148
149                 render_copy = igt_get_render_copyfunc(data.devid);
150                 igt_require_f(render_copy,
151                               "no render-copy function\n");
152
153                 batch = intel_batchbuffer_alloc(data.bufmgr, data.devid);
154                 igt_assert(batch);
155         }
156
157         scratch_buf_init(&data, &src, WIDTH, HEIGHT, STRIDE, SRC_COLOR);
158         scratch_buf_init(&data, &dst, WIDTH, HEIGHT, STRIDE, DST_COLOR);
159
160         scratch_buf_check(&data, &src, WIDTH / 2, HEIGHT / 2, SRC_COLOR);
161         scratch_buf_check(&data, &dst, WIDTH / 2, HEIGHT / 2, DST_COLOR);
162
163         if (opt_dump_png) {
164                 scratch_buf_write_to_png(&src, "source.png");
165                 scratch_buf_write_to_png(&dst, "destination.png");
166         }
167
168         if (opt_dump_aub) {
169                 drm_intel_bufmgr_gem_set_aub_filename(data.bufmgr,
170                                                       "rendercopy.aub");
171                 drm_intel_bufmgr_gem_set_aub_dump(data.bufmgr, true);
172         }
173
174         render_copy(batch, NULL,
175                     &src, 0, 0, WIDTH, HEIGHT,
176                     &dst, WIDTH / 2, HEIGHT / 2);
177
178         if (opt_dump_png)
179                 scratch_buf_write_to_png(&dst, "result.png");
180
181         if (opt_dump_aub) {
182                 drm_intel_gem_bo_aub_dump_bmp(dst.bo,
183                         0, 0, WIDTH, HEIGHT,
184                         AUB_DUMP_BMP_FORMAT_ARGB_8888,
185                         STRIDE, 0);
186                 drm_intel_bufmgr_gem_set_aub_dump(data.bufmgr, false);
187         } else {
188                 scratch_buf_check(&data, &dst, 10, 10, DST_COLOR);
189                 scratch_buf_check(&data, &dst, WIDTH - 10, HEIGHT - 10, SRC_COLOR);
190         }
191
192         return 0;
193 }