tests: remove unused getopt header includes
[platform/upstream/intel-gpu-tools.git] / tests / gem_render_linear_blits.c
1 /*
2  * Copyright © 2011 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  *    Chris Wilson <chris@chris-wilson.co.uk>
25  *
26  */
27
28 /** @file gem_linear_render_blits.c
29  *
30  * This is a test of doing many blits, with a working set
31  * larger than the aperture size.
32  *
33  * The goal is to simply ensure the basics work.
34  */
35
36 #ifdef HAVE_CONFIG_H
37 #include "config.h"
38 #endif
39
40 #include <stdlib.h>
41 #include <sys/ioctl.h>
42 #include <stdio.h>
43 #include <string.h>
44 #include <fcntl.h>
45 #include <inttypes.h>
46 #include <errno.h>
47 #include <sys/stat.h>
48 #include <sys/time.h>
49
50 #include <drm.h>
51
52 #include "ioctl_wrappers.h"
53 #include "drmtest.h"
54 #include "intel_bufmgr.h"
55 #include "intel_batchbuffer.h"
56 #include "intel_io.h"
57 #include "intel_chipset.h"
58 #include "igt_aux.h"
59
60 #define WIDTH 512
61 #define STRIDE (WIDTH*4)
62 #define HEIGHT 512
63 #define SIZE (HEIGHT*STRIDE)
64
65 static uint32_t linear[WIDTH*HEIGHT];
66 static igt_render_copyfunc_t render_copy;
67
68 static void
69 check_bo(int fd, uint32_t handle, uint32_t val)
70 {
71         int i;
72
73         gem_read(fd, handle, 0, linear, sizeof(linear));
74         for (i = 0; i < WIDTH*HEIGHT; i++) {
75                 igt_assert_f(linear[i] == val,
76                              "Expected 0x%08x, found 0x%08x "
77                              "at offset 0x%08x\n",
78                              val, linear[i], i * 4);
79                 val++;
80         }
81 }
82
83 int main(int argc, char **argv)
84 {
85         drm_intel_bufmgr *bufmgr;
86         struct intel_batchbuffer *batch;
87         uint32_t *start_val;
88         drm_intel_bo **bo;
89         uint32_t start = 0;
90         int i, j, fd, count;
91
92         igt_simple_init(argc, argv);
93
94         fd = drm_open_any();
95
96         render_copy = igt_get_render_copyfunc(intel_get_drm_devid(fd));
97         igt_require(render_copy);
98
99         bufmgr = drm_intel_bufmgr_gem_init(fd, 4096);
100         batch = intel_batchbuffer_alloc(bufmgr, intel_get_drm_devid(fd));
101
102         count = 0;
103         if (igt_run_in_simulation())
104                 count = 2;
105         if (argc > 1)
106                 count = atoi(argv[1]);
107
108         if (count == 0)
109                 count = 3 * gem_aperture_size(fd) / SIZE / 2;
110         else if (count < 2) {
111                 igt_warn("count must be >= 2\n");
112                 return 1;
113         }
114
115         if (count > intel_get_total_ram_mb() * 9 / 10) {
116                 count = intel_get_total_ram_mb() * 9 / 10;
117                 igt_info("not enough RAM to run test, reducing buffer count\n");
118         }
119
120         bo = malloc(sizeof(*bo)*count);
121         start_val = malloc(sizeof(*start_val)*count);
122
123         for (i = 0; i < count; i++) {
124                 bo[i] = drm_intel_bo_alloc(bufmgr, "", SIZE, 4096);
125                 start_val[i] = start;
126                 for (j = 0; j < WIDTH*HEIGHT; j++)
127                         linear[j] = start++;
128                 gem_write(fd, bo[i]->handle, 0, linear, sizeof(linear));
129         }
130
131         igt_info("Verifying initialisation...\n");
132         for (i = 0; i < count; i++)
133                 check_bo(fd, bo[i]->handle, start_val[i]);
134
135         igt_info("Cyclic blits, forward...\n");
136         for (i = 0; i < count * 4; i++) {
137                 struct igt_buf src, dst;
138
139                 src.bo = bo[i % count];
140                 src.stride = STRIDE;
141                 src.tiling = I915_TILING_NONE;
142                 src.size = SIZE;
143
144                 dst.bo = bo[(i + 1) % count];
145                 dst.stride = STRIDE;
146                 dst.tiling = I915_TILING_NONE;
147                 dst.size = SIZE;
148
149                 render_copy(batch, NULL, &src, 0, 0, WIDTH, HEIGHT, &dst, 0, 0);
150                 start_val[(i + 1) % count] = start_val[i % count];
151         }
152         for (i = 0; i < count; i++)
153                 check_bo(fd, bo[i]->handle, start_val[i]);
154
155         if (igt_run_in_simulation())
156                 return 0;
157
158         igt_info("Cyclic blits, backward...\n");
159         for (i = 0; i < count * 4; i++) {
160                 struct igt_buf src, dst;
161
162                 src.bo = bo[(i + 1) % count];
163                 src.stride = STRIDE;
164                 src.tiling = I915_TILING_NONE;
165                 src.size = SIZE;
166
167                 dst.bo = bo[i % count];
168                 dst.stride = STRIDE;
169                 dst.tiling = I915_TILING_NONE;
170                 dst.size = SIZE;
171
172                 render_copy(batch, NULL, &src, 0, 0, WIDTH, HEIGHT, &dst, 0, 0);
173                 start_val[i % count] = start_val[(i + 1) % count];
174         }
175         for (i = 0; i < count; i++)
176                 check_bo(fd, bo[i]->handle, start_val[i]);
177
178         igt_info("Random blits...\n");
179         for (i = 0; i < count * 4; i++) {
180                 struct igt_buf src, dst;
181                 int s = random() % count;
182                 int d = random() % count;
183
184                 if (s == d)
185                         continue;
186
187                 src.bo = bo[s];
188                 src.stride = STRIDE;
189                 src.tiling = I915_TILING_NONE;
190                 src.size = SIZE;
191
192                 dst.bo = bo[d];
193                 dst.stride = STRIDE;
194                 dst.tiling = I915_TILING_NONE;
195                 dst.size = SIZE;
196
197                 render_copy(batch, NULL, &src, 0, 0, WIDTH, HEIGHT, &dst, 0, 0);
198                 start_val[d] = start_val[s];
199         }
200         for (i = 0; i < count; i++)
201                 check_bo(fd, bo[i]->handle, start_val[i]);
202
203         return 0;
204 }