kms_rotation_crc: Remove useless comments
[platform/upstream/intel-gpu-tools.git] / tests / gem_ctx_basic.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  *    Ben Widawsky <ben@bwidawsk.net>
25  *
26  */
27
28 /*
29  * This test is useful for finding memory and refcount leaks.
30  */
31
32 #include <pthread.h>
33 #include <stdlib.h>
34 #include <sys/ioctl.h>
35 #include <stdio.h>
36 #include <string.h>
37 #include <fcntl.h>
38 #include <inttypes.h>
39 #include <errno.h>
40 #include <sys/stat.h>
41 #include <sys/time.h>
42 #include <getopt.h>
43 #include "drm.h"
44 #include "ioctl_wrappers.h"
45 #include "drmtest.h"
46 #include "intel_bufmgr.h"
47 #include "intel_batchbuffer.h"
48 #include "intel_io.h"
49 #include "intel_chipset.h"
50
51 /* options */
52 int num_contexts = 10;
53 int uncontexted = 0; /* test only context create/destroy */
54 int multiple_fds = 1;
55 int iter = 10000;
56
57 /* globals */
58 pthread_t *threads;
59 int devid;
60 int fd;
61
62 static void init_buffer(drm_intel_bufmgr *bufmgr,
63                         struct igt_buf *buf,
64                         uint32_t size)
65 {
66         buf->bo = drm_intel_bo_alloc(bufmgr, "", size, 4096);
67         buf->size = size;
68         igt_assert(buf->bo);
69         buf->tiling = I915_TILING_NONE;
70         buf->stride = 4096;
71 }
72
73 static void *work(void *arg)
74 {
75         struct intel_batchbuffer *batch;
76         igt_render_copyfunc_t rendercopy = igt_get_render_copyfunc(devid);
77         drm_intel_context *context;
78         drm_intel_bufmgr *bufmgr;
79         int td_fd;
80         int i;
81
82         if (multiple_fds)
83                 td_fd = fd = drm_open_any_render();
84         else
85                 td_fd = fd;
86
87         igt_assert(td_fd >= 0);
88
89         bufmgr = drm_intel_bufmgr_gem_init(td_fd, 4096);
90         batch = intel_batchbuffer_alloc(bufmgr, devid);
91         context = drm_intel_gem_context_create(bufmgr);
92         igt_require(context);
93
94         for (i = 0; i < iter; i++) {
95                 struct igt_buf src, dst;
96
97                 init_buffer(bufmgr, &src, 4096);
98                 init_buffer(bufmgr, &dst, 4096);
99
100
101                 if (uncontexted) {
102                         igt_assert(rendercopy);
103                         rendercopy(batch, NULL, &src, 0, 0, 0, 0, &dst, 0, 0);
104                 } else {
105                         int ret;
106                         ret = drm_intel_bo_subdata(batch->bo, 0, 4096, batch->buffer);
107                         igt_assert(ret == 0);
108                         intel_batchbuffer_flush_with_context(batch, context);
109                 }
110         }
111
112         drm_intel_gem_context_destroy(context);
113         intel_batchbuffer_free(batch);
114         drm_intel_bufmgr_destroy(bufmgr);
115
116         if (multiple_fds)
117                 close(td_fd);
118
119         pthread_exit(NULL);
120 }
121
122 static void parse(int argc, char *argv[])
123 {
124         int opt;
125         while ((opt = getopt(argc, argv, "i:c:n:muh?")) != -1) {
126                 switch (opt) {
127                 case 'i':
128                         iter = atoi(optarg);
129                         break;
130                 case 'c':
131                         num_contexts = atoi(optarg);
132                         break;
133                 case 'm':
134                         multiple_fds = 1;
135                         break;
136                 case 'u':
137                         uncontexted = 1;
138                         break;
139                 case 'h':
140                 case '?':
141                 default:
142                         igt_success();
143                         break;
144                 }
145         }
146 }
147
148 int main(int argc, char *argv[])
149 {
150         int i;
151
152         igt_simple_init();
153
154         fd = drm_open_any_render();
155         devid = intel_get_drm_devid(fd);
156
157         if (igt_run_in_simulation()) {
158                 num_contexts = 2;
159                 iter = 4;
160         }
161
162         parse(argc, argv);
163
164         threads = calloc(num_contexts, sizeof(*threads));
165
166         for (i = 0; i < num_contexts; i++)
167                 pthread_create(&threads[i], NULL, work, &i);
168
169         for (i = 0; i < num_contexts; i++) {
170                 void *retval;
171                 igt_assert(pthread_join(threads[i], &retval) == 0);
172         }
173
174         free(threads);
175         close(fd);
176
177         return 0;
178 }