lib/igt_kms: Unify pipe name helpers
[platform/upstream/intel-gpu-tools.git] / tests / kms_flip_tiling.c
1 /*
2  * Copyright © 2014 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  *   Ander Conselvan de Oliveira <ander.conselvan.de.oliveira@intel.com>
25  */
26
27 #include <errno.h>
28 #include <stdbool.h>
29 #include <stdio.h>
30 #include <string.h>
31
32 #include "drmtest.h"
33 #include "igt_debugfs.h"
34 #include "igt_kms.h"
35 #include "ioctl_wrappers.h"
36
37 typedef struct {
38         int drm_fd;
39         igt_display_t display;
40 } data_t;
41
42 /*
43  * Test that a page flip from a tiled buffer to a linear one works
44  * correctly. First, it sets the crtc with the linear buffer and generate
45  * a reference crc for the pipe. Then, the crtc is set with the tiled one
46  * and page flip to the linear one issued. A new crc is generated and
47  * compared to the rerence one.
48  */
49
50 static void
51 fill_linear_fb(struct igt_fb *fb, data_t *data, drmModeModeInfo *mode)
52 {
53         cairo_t *cr;
54
55         cr = igt_get_cairo_ctx(data->drm_fd, fb);
56         igt_paint_test_pattern(cr, mode->hdisplay, mode->vdisplay);
57         cairo_destroy(cr);
58 }
59
60 static void
61 test_flip_changes_tiling(data_t *data, igt_output_t *output)
62 {
63         struct igt_fb linear, tiled;
64         drmModeModeInfo *mode;
65         igt_plane_t *primary;
66         igt_pipe_crc_t *pipe_crc;
67         igt_crc_t reference_crc, crc;
68         int fb_id, pipe, ret, width;
69
70         pipe = 0;
71         pipe_crc = igt_pipe_crc_new(pipe, INTEL_PIPE_CRC_SOURCE_AUTO);
72         igt_output_set_pipe(output, 0);
73
74         mode = igt_output_get_mode(output);
75         primary = igt_output_get_plane(output, 0);
76
77         /* Allocate a linear buffer. Since a page flip to a buffer with
78          * different stride doesn't work, choose width so that the stride of
79          * both buffers is the same. */
80         width = 512;
81         while (width < mode->hdisplay)
82                 width *= 2;
83         fb_id = igt_create_fb(data->drm_fd, width, mode->vdisplay,
84                               DRM_FORMAT_XRGB8888, false, &linear);
85
86         /* fill it with a pattern that will look wrong if tiling is wrong */
87         fill_linear_fb(&linear, data, mode);
88
89         /* set the crtc and generate a reference crc */
90         igt_plane_set_fb(primary, &linear);
91         igt_display_commit(&data->display);
92         igt_pipe_crc_collect_crc(pipe_crc, &reference_crc);
93
94         /* allocate a tiled buffer and set the crtc with it */
95         igt_create_color_fb(data->drm_fd, width, mode->vdisplay,
96                             DRM_FORMAT_XRGB8888, true, 0.0, 0.0, 0.0, &tiled);
97         igt_plane_set_fb(primary, &tiled);
98         igt_display_commit(&data->display);
99
100         /* flip to the linear buffer */
101         ret = drmModePageFlip(data->drm_fd, output->config.crtc->crtc_id,
102                               fb_id, 0, NULL);
103         igt_assert(ret == 0);
104
105         igt_wait_for_vblank(data->drm_fd, pipe);
106
107         /* get a crc and compare with the reference */
108         igt_pipe_crc_collect_crc(pipe_crc, &crc);
109         igt_assert(igt_crc_equal(&reference_crc, &crc));
110
111         /* clean up */
112         igt_plane_set_fb(primary, NULL);
113         igt_pipe_crc_free(pipe_crc);
114         igt_output_set_pipe(output, PIPE_ANY);
115         igt_display_commit(&data->display);
116
117         igt_remove_fb(data->drm_fd, &tiled);
118         igt_remove_fb(data->drm_fd, &linear);
119 }
120
121 static data_t data;
122 igt_output_t *output;
123
124 igt_main
125 {
126         igt_skip_on_simulation();
127
128         igt_fixture {
129                 data.drm_fd = drm_open_any();
130
131                 igt_set_vt_graphics_mode();
132
133                 igt_require_pipe_crc();
134                 igt_display_init(&data.display, data.drm_fd);
135         }
136
137         igt_subtest_f("flip-changes-tiling") {
138                 for_each_connected_output(&data.display, output)
139                         test_flip_changes_tiling(&data, output);
140         }
141
142         igt_fixture {
143                 igt_display_fini(&data.display);
144         }
145 }