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