OSDN Git Service

Merge branch 'master' into modesetting-101
[android-x86/external-libdrm.git] / tests / mode / modetest.c
1
2 #include <assert.h>
3 #include <stdio.h>
4 #include <stdlib.h>
5 #include <stdint.h>
6
7 #include "xf86drm.h"
8 #include "xf86drmMode.h"
9
10 const char* getConnectionText(drmModeConnection conn)
11 {
12         switch (conn) {
13         case DRM_MODE_CONNECTED:
14                 return "connected";
15         case DRM_MODE_DISCONNECTED:
16                 return "disconnected";
17         default:
18                 return "unknown";
19         }
20
21 }
22
23 struct drm_mode_modeinfo* findMode(drmModeResPtr res, uint32_t id)
24 {
25         int i;
26         for (i = 0; i < res->count_modes; i++) {
27                 if (res->modes[i].id == id)
28                         return &res->modes[i];
29         }
30
31         return 0;
32 }
33
34 int printMode(struct drm_mode_modeinfo *mode)
35 {
36 #if 0
37         printf("Mode: %s\n", mode->name);
38         printf("\tclock       : %i\n", mode->clock);
39         printf("\thdisplay    : %i\n", mode->hdisplay);
40         printf("\thsync_start : %i\n", mode->hsync_start);
41         printf("\thsync_end   : %i\n", mode->hsync_end);
42         printf("\thtotal      : %i\n", mode->htotal);
43         printf("\thskew       : %i\n", mode->hskew);
44         printf("\tvdisplay    : %i\n", mode->vdisplay);
45         printf("\tvsync_start : %i\n", mode->vsync_start);
46         printf("\tvsync_end   : %i\n", mode->vsync_end);
47         printf("\tvtotal      : %i\n", mode->vtotal);
48         printf("\tvscan       : %i\n", mode->vscan);
49         printf("\tvrefresh    : %i\n", mode->vrefresh);
50         printf("\tflags       : %i\n", mode->flags);
51 #else
52         printf("Mode: %i \"%s\" %ix%i %.0f\n", mode->id, mode->name,
53                 mode->hdisplay, mode->vdisplay, mode->vrefresh / 1000.0);
54 #endif
55         return 0;
56 }
57
58 int printOutput(int fd, drmModeResPtr res, drmModeOutputPtr output, uint32_t id)
59 {
60         int i = 0;
61         struct drm_mode_modeinfo *mode = NULL;
62
63         printf("Output: %s\n", output->name);
64         printf("\tid           : %i\n", id);
65         printf("\tcrtc id      : %i\n", output->crtc);
66         printf("\tconn         : %s\n", getConnectionText(output->connection));
67         printf("\tsize         : %ix%i (mm)\n", output->mmWidth, output->mmHeight);
68         printf("\tcount_crtcs  : %i\n", output->count_crtcs);
69         printf("\tcrtcs        : %i\n", output->crtcs);
70         printf("\tcount_clones : %i\n", output->count_clones);
71         printf("\tclones       : %i\n", output->clones);
72         printf("\tcount_modes  : %i\n", output->count_modes);
73
74         for (i = 0; i < output->count_modes; i++) {
75                 mode = findMode(res, output->modes[i]);
76
77                 if (mode)
78                         printf("\t\tmode: %i \"%s\" %ix%i %.0f\n", mode->id, mode->name,
79                                 mode->hdisplay, mode->vdisplay, mode->vrefresh / 1000.0);
80                 else
81                         printf("\t\tmode: Invalid mode %i\n", output->modes[i]);
82         }
83
84         return 0;
85 }
86
87 int printCrtc(int fd, drmModeResPtr res, drmModeCrtcPtr crtc, uint32_t id)
88 {
89         printf("Crtc\n");
90         printf("\tid           : %i\n", id);
91         printf("\tx            : %i\n", crtc->x);
92         printf("\ty            : %i\n", crtc->y);
93         printf("\twidth        : %i\n", crtc->width);
94         printf("\theight       : %i\n", crtc->height);
95         printf("\tmode         : %i\n", crtc->mode);
96         printf("\tnum outputs  : %i\n", crtc->count_outputs);
97         printf("\toutputs      : %i\n", crtc->outputs);
98         printf("\tnum possible : %i\n", crtc->count_possibles);
99         printf("\tpossibles    : %i\n", crtc->possibles);
100
101         return 0;
102 }
103
104 int printFrameBuffer(int fd, drmModeResPtr res, drmModeFBPtr fb)
105 {
106         printf("Framebuffer\n");
107         printf("\thandle    : %i\n", fb->handle);
108         printf("\twidth     : %i\n", fb->width);
109         printf("\theight    : %i\n", fb->height);
110         printf("\tpitch     : %i\n", fb->pitch);;
111         printf("\tbpp       : %i\n", fb->bpp);
112         printf("\tdepth     : %i\n", fb->depth);
113         printf("\tbuffer_id : %i\n", fb->buffer_id);
114
115         return 0;
116 }
117
118 int printRes(int fd, drmModeResPtr res)
119 {
120         int i;
121         drmModeOutputPtr output;
122         drmModeCrtcPtr crtc;
123         drmModeFBPtr fb;
124
125         for (i = 0; i < res->count_modes; i++) {
126                 printMode(&res->modes[i]);
127         }
128
129         for (i = 0; i < res->count_outputs; i++) {
130                 output = drmModeGetOutput(fd, res->outputs[i]);
131
132                 if (!output)
133                         printf("Could not get output %i\n", i);
134                 else {
135                         printOutput(fd, res, output, res->outputs[i]);
136                         drmModeFreeOutput(output);
137                 }
138         }
139
140         for (i = 0; i < res->count_crtcs; i++) {
141                 crtc = drmModeGetCrtc(fd, res->crtcs[i]);
142
143                 if (!crtc)
144                         printf("Could not get crtc %i\n", i);
145                 else {
146                         printCrtc(fd, res, crtc, res->crtcs[i]);
147                         drmModeFreeCrtc(crtc);
148                 }
149         }
150
151         for (i = 0; i < res->count_fbs; i++) {
152                 fb = drmModeGetFB(fd, res->fbs[i]);
153
154                 if (!fb)
155                         printf("Could not get fb %i\n", res->fbs[i]);
156                 else {
157                         printFrameBuffer(fd, res, fb);
158                         drmModeFreeFB(fb);
159                 }
160         }
161
162         return 0;
163 }
164
165 static struct drm_mode_modeinfo mode = {
166         .name = "Test mode",
167         .clock = 25200,
168         .hdisplay = 640,
169         .hsync_start = 656,
170         .hsync_end = 752,
171         .htotal = 800,
172         .hskew = 0,
173         .vdisplay = 480,
174         .vsync_start = 490,
175         .vsync_end = 492,
176         .vtotal = 525,
177         .vscan = 0,
178         .vrefresh = 60000, /* vertical refresh * 1000 */
179         .flags = 10,
180 };
181
182 int testMode(int fd, drmModeResPtr res)
183 {
184         uint32_t output = res->outputs[0];
185         uint32_t newMode = 0;
186         int ret = 0;
187         int error = 0;
188
189         printf("Test: adding mode to output %i\n", output);
190
191         /* printMode(&mode); */
192
193         printf("\tAdding mode\n");
194         newMode = drmModeAddMode(fd, &mode);
195         if (!newMode)
196                 goto err;
197
198         printf("\tAttaching mode %i to output %i\n", newMode, output);
199
200         ret = drmModeAttachMode(fd, output, newMode);
201
202         if (ret)
203                 goto err_mode;
204
205         printf("\tDetaching mode %i from output %i\n", newMode, output);
206         ret = drmModeDetachMode(fd, output, newMode);
207
208         if (ret)
209                 goto err_mode;
210
211         printf("\tRemoveing new mode %i\n", newMode);
212         ret = drmModeRmMode(fd, newMode);
213         if (ret)
214                 goto err;
215
216         return 0;
217
218 err_mode:
219         error = drmModeRmMode(fd, newMode);
220
221 err:
222         printf("\tFailed\n");
223
224         if (error)
225                 printf("\tFailed to delete mode %i\n", newMode);
226
227         return 1;
228 }
229
230 /*
231 int testFrameBufferGet(int fd, uint32_t fb)
232 {
233         drmModeFBPtr frame;
234
235         printf("Test: get framebuffer %i\n", fb);
236
237         frame = drmModeGetFB(fd, fb);
238
239         if (!frame) {
240                 printf("\tFailed\n");
241         } else {
242                 printFrameBuffer(fd, frame);
243                 drmModeFreeFB(frame);
244         }
245
246         return 0;
247 }
248 */
249
250 int testFrameBufferAdd(int fd, drmModeResPtr res)
251 {
252         uint32_t fb = 0;
253         int ret = 0;
254         drmModeFBPtr frame = 0;
255         drmBO bo;
256
257         printf("Test: adding framebuffer\n");
258
259         printf("\tCreating BO\n");
260
261         /* TODO */
262         ret = 1;
263         if (ret)
264                 goto err;
265
266         printf("\tAdding FB\n");
267         ret = drmModeAddFB(fd, 640, 480, 32, 8, 0, &bo, &fb);
268         if (ret)
269                 goto err_bo;
270
271         frame = drmModeGetFB(fd, fb);
272
273         if (!frame) {
274                 printf("Couldn't retrive created framebuffer\n");
275         } else {
276                 printFrameBuffer(fd, res, frame);
277                 drmModeFreeFB(frame);
278         }
279
280         printf("\tRemoveing FB\n");
281
282         ret = drmModeRmFB(fd, fb);
283
284         if (ret) {
285                 printf("\tFailed this shouldn't happen!\n");
286                 goto err_bo;
287         }
288
289         printf("\tRemoveing BO\n");
290
291         ret = drmBODestroy(fb, &bo);
292
293         return 0;
294         
295 err_bo:
296         drmBODestroy(fd, &bo);
297
298 err:
299         printf("\tFailed\n");
300
301         return 1;
302 }
303
304
305 int main(int argc, char **argv)
306 {
307         int fd;
308         const char *driver = "i915"; /* hardcoded for now */
309         drmModeResPtr res;
310
311         printf("Starting test\n");
312
313         fd = drmOpen(driver, NULL);
314
315         if (fd < 0) {
316                 printf("Failed to open the card fb\n");
317                 return 1;
318         }
319
320         res = drmModeGetResources(fd);
321         if (res == 0) {
322                 printf("Failed to get resources from card\n");
323                 drmClose(fd);
324                 return 1;
325         }
326
327         printRes(fd, res);
328
329         testMode(fd, res);
330
331         testFrameBufferAdd(fd, res);
332
333         drmModeFreeResources(res);
334         printf("Ok\n");
335
336         return 0;
337 }