lib/igt_kms: Unify pipe name helpers
[platform/upstream/intel-gpu-tools.git] / tests / kms_rotation_crc.c
1 /*
2  * Copyright © 2013,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  */
24
25 #include <math.h>
26
27 #include "drmtest.h"
28 #include "igt_debugfs.h"
29 #include "igt_kms.h"
30 #include "igt_core.h"
31
32 typedef struct {
33         int gfx_fd;
34         igt_display_t display;
35         struct igt_fb fb;
36         igt_crc_t ref_crc;
37         igt_pipe_crc_t *pipe_crc;
38 } data_t;
39
40 static void
41 paint_squares(data_t *data, struct igt_fb *fb, drmModeModeInfo *mode,
42               igt_rotation_t rotation)
43 {
44         cairo_t *cr;
45         int w, h;
46
47         w = mode->hdisplay;
48         h = mode->vdisplay;
49
50         cr = igt_get_cairo_ctx(data->gfx_fd, &data->fb);
51
52         if (rotation == IGT_ROTATION_180) {
53                 cairo_translate(cr, w, h);
54                 cairo_rotate(cr, M_PI);
55         }
56
57         /* Paint with 4 squares of Red, Green, White, Blue Clockwise */
58         igt_paint_color(cr, 0, 0, w / 2, h / 2, 1.0, 0.0, 0.0);
59         igt_paint_color(cr, w / 2, 0, w / 2, h / 2, 0.0, 1.0, 0.0);
60         igt_paint_color(cr, 0, h / 2, w / 2, h / 2, 0.0, 0.0, 1.0);
61         igt_paint_color(cr, w / 2, h / 2, w / 2, h / 2, 1.0, 1.0, 1.0);
62
63         cairo_destroy(cr);
64 }
65
66 static bool prepare_crtc(data_t *data, igt_output_t *output, enum pipe pipe,
67                          igt_plane_t *plane)
68 {
69         drmModeModeInfo *mode;
70         igt_display_t *display = &data->display;
71         int fb_id;
72
73         igt_output_set_pipe(output, pipe);
74
75
76         /* create the pipe_crc object for this pipe */
77         igt_pipe_crc_free(data->pipe_crc);
78         data->pipe_crc = igt_pipe_crc_new(pipe, INTEL_PIPE_CRC_SOURCE_AUTO);
79         if (!data->pipe_crc) {
80                 igt_info("auto crc not supported on this connector with pipe %i\n",
81                          pipe);
82                 return false;
83         }
84
85         mode = igt_output_get_mode(output);
86
87         fb_id = igt_create_fb(data->gfx_fd,
88                         mode->hdisplay, mode->vdisplay,
89                         DRM_FORMAT_XRGB8888,
90                         false, /* tiled */
91                         &data->fb);
92         igt_assert(fb_id);
93
94         /* Step 1: create a reference CRC for a software-rotated fb */
95
96         paint_squares(data, &data->fb, mode, IGT_ROTATION_180);
97
98         /*
99          * XXX: We always set the primary plane to actually enable the pipe as
100          * there's no way (that works) to light up a pipe with only a sprite
101          * plane enabled at the moment.
102          */
103         if (!plane->is_primary) {
104                 igt_plane_t *primary;
105
106                 primary = igt_output_get_plane(output, IGT_PLANE_PRIMARY);
107                 igt_plane_set_fb(primary, &data->fb);
108         }
109
110         igt_plane_set_fb(plane, &data->fb);
111         igt_display_commit(display);
112
113         igt_pipe_crc_collect_crc(data->pipe_crc, &data->ref_crc);
114
115         /*
116          * Step 2: prepare the plane with an non-rotated fb let the hw
117          * rotate it.
118          */
119         paint_squares(data, &data->fb, mode, IGT_ROTATION_0);
120
121         igt_plane_set_fb(plane, &data->fb);
122         igt_display_commit(display);
123
124         return true;
125 }
126
127 static void cleanup_crtc(data_t *data, igt_output_t *output, igt_plane_t *plane)
128 {
129         igt_display_t *display = &data->display;
130
131         igt_pipe_crc_free(data->pipe_crc);
132         data->pipe_crc = NULL;
133
134         igt_remove_fb(data->gfx_fd, &data->fb);
135
136         /* XXX: see the note in prepare_crtc() */
137         if (!plane->is_primary) {
138                 igt_plane_t *primary;
139
140                 primary = igt_output_get_plane(output, IGT_PLANE_PRIMARY);
141                 igt_plane_set_fb(primary, NULL);
142         }
143
144         igt_plane_set_fb(plane, NULL);
145         igt_output_set_pipe(output, PIPE_ANY);
146
147         igt_display_commit(display);
148 }
149
150 static void test_plane_rotation(data_t *data, enum igt_plane plane_type)
151 {
152         igt_display_t *display = &data->display;
153         igt_output_t *output;
154         enum pipe pipe;
155         int valid_tests = 0;
156         igt_crc_t crc_output;
157         enum igt_commit_style commit = COMMIT_LEGACY;
158
159         if (plane_type == IGT_PLANE_PRIMARY) {
160                 igt_require(data->display.has_universal_planes);
161                 commit = COMMIT_UNIVERSAL;
162         }
163
164         for_each_connected_output(display, output) {
165                 for_each_pipe(display, pipe) {
166                         igt_plane_t *plane;
167
168                         igt_output_set_pipe(output, pipe);
169
170                         plane = igt_output_get_plane(output, plane_type);
171                         igt_require(igt_plane_supports_rotation(plane));
172
173                         if (!prepare_crtc(data, output, pipe, plane))
174                                 continue;
175
176                         igt_plane_set_rotation(plane, IGT_ROTATION_180);
177                         igt_display_commit2(display, commit);
178
179                         igt_pipe_crc_collect_crc(data->pipe_crc, &crc_output);
180                         igt_assert(igt_crc_equal(&data->ref_crc, &crc_output));
181
182                         valid_tests++;
183                         cleanup_crtc(data, output, plane);
184                 }
185         }
186         igt_require_f(valid_tests, "no valid crtc/connector combinations found\n");
187 }
188
189 igt_main
190 {
191         data_t data = {};
192
193         igt_skip_on_simulation();
194
195         igt_fixture {
196                 data.gfx_fd = drm_open_any();
197
198                 igt_set_vt_graphics_mode();
199
200                 igt_require_pipe_crc();
201
202                 igt_display_init(&data.display, data.gfx_fd);
203         }
204
205         igt_subtest_f("primary-rotation")
206                 test_plane_rotation(&data, IGT_PLANE_PRIMARY);
207
208         igt_subtest_f("sprite-rotation")
209                 test_plane_rotation(&data, IGT_PLANE_2);
210
211         igt_fixture {
212                 igt_display_fini(&data.display);
213         }
214 }