tests: remove unused getopt header includes
[platform/upstream/intel-gpu-tools.git] / tests / kms_sink_crc_basic.c
1 /*
2  * Copyright © 2013 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 <errno.h>
26 #include <limits.h>
27 #include <stdbool.h>
28 #include <stdio.h>
29 #include <string.h>
30
31 #include "drm_fourcc.h"
32
33 #include "drmtest.h"
34 #include "igt_debugfs.h"
35 #include "igt_kms.h"
36
37 enum color {
38         WHITE,
39         BLACK,
40         NUM_COLORS,
41 };
42
43 typedef struct {
44         struct kmstest_connector_config config;
45         struct igt_fb fb;
46 } connector_t;
47
48 typedef struct {
49         int drm_fd;
50         drmModeRes *resources;
51 } data_t;
52
53 static void get_crc(char *crc) {
54         int ret;
55         FILE *file = fopen("/sys/kernel/debug/dri/0/i915_sink_crc_eDP1", "r");
56         igt_require(file);
57
58         ret = fscanf(file, "%s\n", crc);
59         igt_require(ret > 0);
60
61         fclose(file);
62 }
63
64 static uint32_t create_fb(data_t *data,
65                           int w, int h,
66                           double r, double g, double b,
67                           struct igt_fb *fb)
68 {
69         cairo_t *cr;
70         uint32_t fb_id;
71
72         fb_id = igt_create_fb(data->drm_fd, w, h,
73                               DRM_FORMAT_XRGB8888, false, fb);
74         igt_assert(fb_id);
75
76         cr = igt_get_cairo_ctx(data->drm_fd, fb);
77         igt_paint_color(cr, 0, 0, w, h, r, g, b);
78         igt_assert(cairo_status(cr) == 0);
79
80         return fb_id;
81 }
82
83 static bool
84 connector_set_mode(data_t *data, connector_t *connector, drmModeModeInfo *mode,
85                    enum color crtc_color)
86 {
87         struct kmstest_connector_config *config = &connector->config;
88         unsigned int fb_id;
89         int ret;
90
91         if (crtc_color == WHITE)
92                 fb_id = create_fb(data, mode->hdisplay, mode->vdisplay,
93                                   1.0, 1.0, 1.0, &connector->fb);
94         else
95                 fb_id = create_fb(data, mode->hdisplay, mode->vdisplay,
96                                   0.0, 0.0, 0.0, &connector->fb);
97         igt_assert(fb_id);
98
99         ret = drmModeSetCrtc(data->drm_fd,
100                              config->crtc->crtc_id,
101                              connector->fb.fb_id,
102                              0, 0, /* x, y */
103                              &config->connector->connector_id,
104                              1,
105                              mode);
106         igt_assert(ret == 0);
107
108         return 0;
109 }
110
111 static void basic_sink_crc_check(data_t *data, uint32_t connector_id)
112 {
113         connector_t connector;
114         int ret;
115         char ref_crc_white[12];
116         char ref_crc_black[12];
117         char crc_check[12];
118
119         ret = kmstest_get_connector_config(data->drm_fd,
120                                            connector_id,
121                                            1 << 0,
122                                            &connector.config);
123         igt_require(ret == 0);
124
125         /*Go White*/
126         connector_set_mode(data, &connector, &connector.config.default_mode, WHITE);
127
128         /* get reference crc for white color */
129         get_crc(ref_crc_white);
130
131         /* Go Black */
132         connector_set_mode(data, &connector, &connector.config.default_mode, BLACK);
133
134         /* get reference crc for black color */
135         get_crc(ref_crc_black);
136
137         igt_assert(strcmp(ref_crc_black, ref_crc_white) != 0);
138
139         /*Go White again*/
140         connector_set_mode(data, &connector, &connector.config.default_mode, WHITE);
141
142         get_crc(crc_check);
143         igt_assert(strcmp(crc_check, ref_crc_white) == 0);
144
145         /* Go Black again */
146         connector_set_mode(data, &connector, &connector.config.default_mode, BLACK);
147
148         get_crc(crc_check);
149         igt_assert(strcmp(crc_check, ref_crc_black) == 0);
150
151         kmstest_free_connector_config(&connector.config);
152 }
153
154 static void run_test(data_t *data)
155 {
156         int i;
157         drmModeConnectorPtr c;
158         uint32_t connector_id = 0;
159
160         for (i = 0; i < data->resources->count_connectors; i++) {
161                 connector_id = data->resources->connectors[i];
162                 c = drmModeGetConnector(data->drm_fd, connector_id);
163
164                 if (c->connector_type != DRM_MODE_CONNECTOR_eDP ||
165                     c->connection != DRM_MODE_CONNECTED)
166                         continue;
167
168                 basic_sink_crc_check(data, connector_id);
169                 return;
170         }
171
172         igt_skip("no eDP with CRC support found\n");
173 }
174
175 igt_simple_main
176 {
177         data_t data = {};
178
179         igt_skip_on_simulation();
180
181         data.drm_fd = drm_open_any();
182
183         igt_set_vt_graphics_mode();
184
185         data.resources = drmModeGetResources(data.drm_fd);
186         igt_assert(data.resources);
187
188         run_test(&data);
189
190         drmModeFreeResources(data.resources);
191 }