lib/igt.cocci: Add stanza for for_each_pipe
[platform/upstream/intel-gpu-tools.git] / tests / kms_render.c
1 /*
2  * Permission is hereby granted, free of charge, to any person obtaining a
3  * copy of this software and associated documentation files (the "Software"),
4  * to deal in the Software without restriction, including without limitation
5  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
6  * and/or sell copies of the Software, and to permit persons to whom the
7  * Software is furnished to do so, subject to the following conditions:
8  *
9  * The above copyright notice and this permission notice shall be included in
10  * all copies or substantial portions of the Software.
11  *
12  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
13  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
14  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
15  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
16  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
17  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
18  * IN THE SOFTWARE.
19  *
20  * Authors:
21  *    Imre Deak <imre.deak@intel.com>
22  */
23 #ifdef HAVE_CONFIG_H
24 #include "config.h"
25 #endif
26
27 #include <cairo.h>
28 #include <errno.h>
29 #include <stdint.h>
30 #include <unistd.h>
31 #include <sys/time.h>
32
33 #include "drmtest.h"
34 #include "intel_bufmgr.h"
35 #include "intel_batchbuffer.h"
36 #include "intel_io.h"
37 #include "intel_chipset.h"
38 #include "igt_kms.h"
39 #include "ioctl_wrappers.h"
40
41 drmModeRes *resources;
42 int drm_fd;
43 static drm_intel_bufmgr *bufmgr;
44 struct intel_batchbuffer *batch;
45 uint32_t devid;
46
47 enum test_flags {
48         TEST_DIRECT_RENDER      = 0x01,
49         TEST_GPU_BLIT           = 0x02,
50 };
51
52 static int paint_fb(struct igt_fb *fb, const char *test_name,
53                     const char *mode_format_str, const char *cconf_str)
54 {
55         cairo_t *cr;
56
57         cr = igt_get_cairo_ctx(drm_fd, fb);
58
59         igt_paint_color_gradient(cr, 0, 0, fb->width, fb->height, 1, 1, 1);
60         igt_paint_test_pattern(cr, fb->width, fb->height);
61
62         cairo_move_to(cr, fb->width / 2, fb->height / 2);
63         cairo_set_font_size(cr, 36);
64         igt_cairo_printf_line(cr, align_hcenter, 10, "%s", test_name);
65         igt_cairo_printf_line(cr, align_hcenter, 10, "%s", mode_format_str);
66         igt_cairo_printf_line(cr, align_hcenter, 10, "%s", cconf_str);
67
68         cairo_destroy(cr);
69
70         return 0;
71 }
72
73 static void gpu_blit(struct igt_fb *dst_fb, struct igt_fb *src_fb)
74 {
75         drm_intel_bo *dst_bo;
76         drm_intel_bo *src_bo;
77         int bpp;
78
79         igt_assert(dst_fb->drm_format == src_fb->drm_format);
80         igt_assert(src_fb->drm_format == DRM_FORMAT_RGB565 ||
81                igt_drm_format_to_bpp(src_fb->drm_format) != 16);
82         bpp = igt_drm_format_to_bpp(src_fb->drm_format);
83         dst_bo = gem_handle_to_libdrm_bo(bufmgr, drm_fd, "destination",
84                                          dst_fb->gem_handle);
85         igt_assert(dst_bo);
86         src_bo = gem_handle_to_libdrm_bo(bufmgr, drm_fd, "source",
87                                          src_fb->gem_handle);
88         igt_assert(src_bo);
89
90         intel_blt_copy(batch,
91                        src_bo, 0, 0, src_fb->width * bpp / 8,
92                        dst_bo, 0, 0, dst_fb->width * bpp / 8,
93                        src_fb->width, src_fb->height, bpp);
94         intel_batchbuffer_flush(batch);
95         gem_quiescent_gpu(drm_fd);
96
97         drm_intel_bo_unreference(src_bo);
98         drm_intel_bo_unreference(dst_bo);
99 }
100
101 static int test_format(const char *test_name,
102                        struct kmstest_connector_config *cconf,
103                        drmModeModeInfo *mode, uint32_t format,
104                        enum test_flags flags)
105 {
106         int width;
107         int height;
108         struct igt_fb fb[2];
109         char *mode_format_str;
110         char *cconf_str;
111         int ret;
112
113         ret = asprintf(&mode_format_str, "%s @ %dHz / %s",
114                  mode->name, mode->vrefresh, igt_format_str(format));
115         igt_assert(ret > 0);
116         ret = asprintf(&cconf_str, "pipe %s, encoder %s, connector %s",
117                        kmstest_pipe_str(cconf->pipe),
118                        kmstest_encoder_type_str(cconf->encoder->encoder_type),
119                        kmstest_connector_type_str(cconf->connector->connector_type));
120         igt_assert(ret > 0);
121
122         igt_info("Beginning test %s with %s on %s\n",
123                  test_name, mode_format_str, cconf_str);
124
125         width = mode->hdisplay;
126         height = mode->vdisplay;
127
128         if (!igt_create_fb(drm_fd, width, height, format, false, &fb[0]))
129                 goto err1;
130
131         if (!igt_create_fb(drm_fd, width, height, format, false, &fb[1]))
132                 goto err2;
133
134         if (drmModeSetCrtc(drm_fd, cconf->crtc->crtc_id, fb[0].fb_id,
135                                  0, 0, &cconf->connector->connector_id, 1,
136                                  mode))
137                 goto err2;
138         do_or_die(drmModePageFlip(drm_fd, cconf->crtc->crtc_id, fb[0].fb_id,
139                                   0, NULL));
140         sleep(2);
141
142         if (flags & TEST_DIRECT_RENDER) {
143                 paint_fb(&fb[0], test_name, mode_format_str, cconf_str);
144         } else if (flags & TEST_GPU_BLIT) {
145                 paint_fb(&fb[1], test_name, mode_format_str, cconf_str);
146                 gpu_blit(&fb[0], &fb[1]);
147         }
148         sleep(5);
149
150         igt_info("Test %s with %s on %s: PASSED\n",
151                  test_name, mode_format_str, cconf_str);
152         free(mode_format_str);
153         free(cconf_str);
154
155         igt_remove_fb(drm_fd, &fb[1]);
156         igt_remove_fb(drm_fd, &fb[0]);
157
158         return 0;
159
160 err2:
161         igt_remove_fb(drm_fd, &fb[0]);
162 err1:
163         igt_info("Test %s with %s on %s: SKIPPED\n",
164                  test_name, mode_format_str, cconf_str);
165         free(mode_format_str);
166         free(cconf_str);
167
168         return -1;
169 }
170
171 static void test_connector(const char *test_name,
172                            struct kmstest_connector_config *cconf,
173                            enum test_flags flags)
174 {
175         const uint32_t *formats;
176         int format_count;
177         int i;
178
179         igt_get_all_formats(&formats, &format_count);
180         for (i = 0; i < format_count; i++)
181                 test_format(test_name,
182                             cconf, &cconf->connector->modes[i],
183                             formats[i], flags);
184 }
185
186 static int run_test(const char *test_name, enum test_flags flags)
187 {
188         int i;
189
190         resources = drmModeGetResources(drm_fd);
191         igt_assert(resources);
192
193         /* Find any connected displays */
194         for (i = 0; i < resources->count_connectors; i++) {
195                 uint32_t connector_id;
196                 int j;
197
198                 connector_id = resources->connectors[i];
199                 for (j = 0; j < resources->count_crtcs; j++) {
200                         struct kmstest_connector_config cconf;
201                         int ret;
202
203                         ret = kmstest_get_connector_config(drm_fd, connector_id,
204                                                            1 << j, &cconf);
205                         if (ret < 0)
206                                 continue;
207
208                         test_connector(test_name, &cconf, flags);
209
210                         kmstest_free_connector_config(&cconf);
211                 }
212         }
213
214         drmModeFreeResources(resources);
215
216         return 1;
217 }
218
219 igt_main
220 {
221         struct {
222                 enum test_flags flags;
223                 const char *name;
224         } tests[] = {
225                 { TEST_DIRECT_RENDER,   "direct-render" },
226                 { TEST_GPU_BLIT,        "gpu-blit" },
227         };
228         int i;
229
230         igt_skip_on_simulation();
231
232         igt_fixture {
233                 drm_fd = drm_open_any();
234
235                 bufmgr = drm_intel_bufmgr_gem_init(drm_fd, 4096);
236                 devid = intel_get_drm_devid(drm_fd);
237                 batch = intel_batchbuffer_alloc(bufmgr, devid);
238
239                 igt_set_vt_graphics_mode();
240         }
241
242         for (i = 0; i < ARRAY_SIZE(tests); i++) {
243                 igt_subtest(tests[i].name)
244                         run_test(tests[i].name, tests[i].flags);
245         }
246
247         igt_fixture
248                 close(drm_fd);
249 }