File: | tests/modetest/modetest.c |
Location: | line 568, column 13 |
Description: | Access to field 'crtc_id' results in a dereference of a null pointer (loaded from field 'encoder') |
1 | /* | ||
2 | * DRM based mode setting test program | ||
3 | * Copyright 2008 Tungsten Graphics | ||
4 | * Jakob Bornecrantz <jakob@tungstengraphics.com> | ||
5 | * Copyright 2008 Intel Corporation | ||
6 | * Jesse Barnes <jesse.barnes@intel.com> | ||
7 | * | ||
8 | * Permission is hereby granted, free of charge, to any person obtaining a | ||
9 | * copy of this software and associated documentation files (the "Software"), | ||
10 | * to deal in the Software without restriction, including without limitation | ||
11 | * the rights to use, copy, modify, merge, publish, distribute, sublicense, | ||
12 | * and/or sell copies of the Software, and to permit persons to whom the | ||
13 | * Software is furnished to do so, subject to the following conditions: | ||
14 | * | ||
15 | * The above copyright notice and this permission notice shall be included in | ||
16 | * all copies or substantial portions of the Software. | ||
17 | * | ||
18 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
19 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
20 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
21 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
22 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING | ||
23 | * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS | ||
24 | * IN THE SOFTWARE. | ||
25 | */ | ||
26 | |||
27 | /* | ||
28 | * This fairly simple test program dumps output in a similar format to the | ||
29 | * "xrandr" tool everyone knows & loves. It's necessarily slightly different | ||
30 | * since the kernel separates outputs into encoder and connector structures, | ||
31 | * each with their own unique ID. The program also allows test testing of the | ||
32 | * memory management and mode setting APIs by allowing the user to specify a | ||
33 | * connector and mode to use for mode setting. If all works as expected, a | ||
34 | * blue background should be painted on the monitor attached to the specified | ||
35 | * connector after the selected mode is set. | ||
36 | * | ||
37 | * TODO: use cairo to write the mode info on the selected output once | ||
38 | * the mode has been programmed, along with possible test patterns. | ||
39 | */ | ||
40 | #include "config.h" | ||
41 | |||
42 | #include <assert.h> | ||
43 | #include <stdio.h> | ||
44 | #include <stdlib.h> | ||
45 | #include <stdint.h> | ||
46 | #include <inttypes.h> | ||
47 | #include <unistd.h> | ||
48 | #include <string.h> | ||
49 | #include <errno(*__errno_location ()).h> | ||
50 | #include <sys/poll.h> | ||
51 | #include <sys/time.h> | ||
52 | |||
53 | #include "xf86drm.h" | ||
54 | #include "xf86drmMode.h" | ||
55 | #include "drm_fourcc.h" | ||
56 | #include "libkms.h" | ||
57 | |||
58 | #ifdef HAVE_CAIRO | ||
59 | #include <math.h> | ||
60 | #include <cairo.h> | ||
61 | #endif | ||
62 | |||
63 | drmModeRes *resources; | ||
64 | int fd, modes; | ||
65 | |||
66 | #define ARRAY_SIZE(arr)(sizeof(arr) / sizeof((arr)[0])) (sizeof(arr) / sizeof((arr)[0])) | ||
67 | |||
68 | struct type_name { | ||
69 | int type; | ||
70 | char *name; | ||
71 | }; | ||
72 | |||
73 | #define type_name_fn(res)char * res_str(int type) { unsigned int i; for (i = 0; i < (sizeof(res_names) / sizeof((res_names)[0])); i++) { if (res_names [i].type == type) return res_names[i].name; } return "(invalid)" ; } \ | ||
74 | char * res##_str(int type) { \ | ||
75 | unsigned int i; \ | ||
76 | for (i = 0; i < ARRAY_SIZE(res##_names)(sizeof(res##_names) / sizeof((res##_names)[0])); i++) { \ | ||
77 | if (res##_names[i].type == type) \ | ||
78 | return res##_names[i].name; \ | ||
79 | } \ | ||
80 | return "(invalid)"; \ | ||
81 | } | ||
82 | |||
83 | struct type_name encoder_type_names[] = { | ||
84 | { DRM_MODE_ENCODER_NONE0, "none" }, | ||
85 | { DRM_MODE_ENCODER_DAC1, "DAC" }, | ||
86 | { DRM_MODE_ENCODER_TMDS2, "TMDS" }, | ||
87 | { DRM_MODE_ENCODER_LVDS3, "LVDS" }, | ||
88 | { DRM_MODE_ENCODER_TVDAC4, "TVDAC" }, | ||
89 | }; | ||
90 | |||
91 | type_name_fn(encoder_type)char * encoder_type_str(int type) { unsigned int i; for (i = 0 ; i < (sizeof(encoder_type_names) / sizeof((encoder_type_names )[0])); i++) { if (encoder_type_names[i].type == type) return encoder_type_names[i].name; } return "(invalid)"; } | ||
92 | |||
93 | struct type_name connector_status_names[] = { | ||
94 | { DRM_MODE_CONNECTED, "connected" }, | ||
95 | { DRM_MODE_DISCONNECTED, "disconnected" }, | ||
96 | { DRM_MODE_UNKNOWNCONNECTION, "unknown" }, | ||
97 | }; | ||
98 | |||
99 | type_name_fn(connector_status)char * connector_status_str(int type) { unsigned int i; for ( i = 0; i < (sizeof(connector_status_names) / sizeof((connector_status_names )[0])); i++) { if (connector_status_names[i].type == type) return connector_status_names[i].name; } return "(invalid)"; } | ||
100 | |||
101 | struct type_name connector_type_names[] = { | ||
102 | { DRM_MODE_CONNECTOR_Unknown0, "unknown" }, | ||
103 | { DRM_MODE_CONNECTOR_VGA1, "VGA" }, | ||
104 | { DRM_MODE_CONNECTOR_DVII2, "DVI-I" }, | ||
105 | { DRM_MODE_CONNECTOR_DVID3, "DVI-D" }, | ||
106 | { DRM_MODE_CONNECTOR_DVIA4, "DVI-A" }, | ||
107 | { DRM_MODE_CONNECTOR_Composite5, "composite" }, | ||
108 | { DRM_MODE_CONNECTOR_SVIDEO6, "s-video" }, | ||
109 | { DRM_MODE_CONNECTOR_LVDS7, "LVDS" }, | ||
110 | { DRM_MODE_CONNECTOR_Component8, "component" }, | ||
111 | { DRM_MODE_CONNECTOR_9PinDIN9, "9-pin DIN" }, | ||
112 | { DRM_MODE_CONNECTOR_DisplayPort10, "displayport" }, | ||
113 | { DRM_MODE_CONNECTOR_HDMIA11, "HDMI-A" }, | ||
114 | { DRM_MODE_CONNECTOR_HDMIB12, "HDMI-B" }, | ||
115 | { DRM_MODE_CONNECTOR_TV13, "TV" }, | ||
116 | { DRM_MODE_CONNECTOR_eDP14, "embedded displayport" }, | ||
117 | }; | ||
118 | |||
119 | type_name_fn(connector_type)char * connector_type_str(int type) { unsigned int i; for (i = 0; i < (sizeof(connector_type_names) / sizeof((connector_type_names )[0])); i++) { if (connector_type_names[i].type == type) return connector_type_names[i].name; } return "(invalid)"; } | ||
120 | |||
121 | #define bit_name_fn(res)char * res_str(int type) { int i; const char *sep = ""; for ( i = 0; i < (sizeof(res_names) / sizeof((res_names)[0])); i ++) { if (type & (1 << i)) { printf("%s%s", sep, res_names [i]); sep = ", "; } } } \ | ||
122 | char * res##_str(int type) { \ | ||
123 | int i; \ | ||
124 | const char *sep = ""; \ | ||
125 | for (i = 0; i < ARRAY_SIZE(res##_names)(sizeof(res##_names) / sizeof((res##_names)[0])); i++) { \ | ||
126 | if (type & (1 << i)) { \ | ||
127 | printf("%s%s", sep, res##_names[i]); \ | ||
128 | sep = ", "; \ | ||
129 | } \ | ||
130 | } \ | ||
131 | } | ||
132 | |||
133 | static const char *mode_type_names[] = { | ||
134 | "builtin", | ||
135 | "clock_c", | ||
136 | "crtc_c", | ||
137 | "preferred", | ||
138 | "default", | ||
139 | "userdef", | ||
140 | "driver", | ||
141 | }; | ||
142 | |||
143 | bit_name_fn(mode_type)char * mode_type_str(int type) { int i; const char *sep = ""; for (i = 0; i < (sizeof(mode_type_names) / sizeof((mode_type_names )[0])); i++) { if (type & (1 << i)) { printf("%s%s" , sep, mode_type_names[i]); sep = ", "; } } } | ||
144 | |||
145 | static const char *mode_flag_names[] = { | ||
146 | "phsync", | ||
147 | "nhsync", | ||
148 | "pvsync", | ||
149 | "nvsync", | ||
150 | "interlace", | ||
151 | "dblscan", | ||
152 | "csync", | ||
153 | "pcsync", | ||
154 | "ncsync", | ||
155 | "hskew", | ||
156 | "bcast", | ||
157 | "pixmux", | ||
158 | "dblclk", | ||
159 | "clkdiv2" | ||
160 | }; | ||
161 | |||
162 | bit_name_fn(mode_flag)char * mode_flag_str(int type) { int i; const char *sep = ""; for (i = 0; i < (sizeof(mode_flag_names) / sizeof((mode_flag_names )[0])); i++) { if (type & (1 << i)) { printf("%s%s" , sep, mode_flag_names[i]); sep = ", "; } } } | ||
163 | |||
164 | void dump_encoders(void) | ||
165 | { | ||
166 | drmModeEncoder *encoder; | ||
167 | int i; | ||
168 | |||
169 | printf("Encoders:\n"); | ||
170 | printf("id\tcrtc\ttype\tpossible crtcs\tpossible clones\t\n"); | ||
171 | for (i = 0; i < resources->count_encoders; i++) { | ||
172 | encoder = drmModeGetEncoder(fd, resources->encoders[i]); | ||
173 | |||
174 | if (!encoder) { | ||
175 | fprintf(stderrstderr, "could not get encoder %i: %s\n", | ||
176 | resources->encoders[i], strerror(errno(*__errno_location ()))); | ||
177 | continue; | ||
178 | } | ||
179 | printf("%d\t%d\t%s\t0x%08x\t0x%08x\n", | ||
180 | encoder->encoder_id, | ||
181 | encoder->crtc_id, | ||
182 | encoder_type_str(encoder->encoder_type), | ||
183 | encoder->possible_crtcs, | ||
184 | encoder->possible_clones); | ||
185 | drmModeFreeEncoder(encoder); | ||
186 | } | ||
187 | printf("\n"); | ||
188 | } | ||
189 | |||
190 | void dump_mode(drmModeModeInfo *mode) | ||
191 | { | ||
192 | printf(" %s %d %d %d %d %d %d %d %d %d", | ||
193 | mode->name, | ||
194 | mode->vrefresh, | ||
195 | mode->hdisplay, | ||
196 | mode->hsync_start, | ||
197 | mode->hsync_end, | ||
198 | mode->htotal, | ||
199 | mode->vdisplay, | ||
200 | mode->vsync_start, | ||
201 | mode->vsync_end, | ||
202 | mode->vtotal); | ||
203 | |||
204 | printf(" flags: "); | ||
205 | mode_flag_str(mode->flags); | ||
206 | printf("; type: "); | ||
207 | mode_type_str(mode->type); | ||
208 | printf("\n"); | ||
209 | } | ||
210 | |||
211 | static void | ||
212 | dump_blob(uint32_t blob_id) | ||
213 | { | ||
214 | uint32_t i; | ||
215 | unsigned char *blob_data; | ||
216 | drmModePropertyBlobPtr blob; | ||
217 | |||
218 | blob = drmModeGetPropertyBlob(fd, blob_id); | ||
219 | if (!blob) | ||
220 | return; | ||
221 | |||
222 | blob_data = blob->data; | ||
223 | |||
224 | for (i = 0; i < blob->length; i++) { | ||
225 | if (i % 16 == 0) | ||
226 | printf("\n\t\t\t"); | ||
227 | printf("%.2hhx", blob_data[i]); | ||
228 | } | ||
229 | printf("\n"); | ||
230 | |||
231 | drmModeFreePropertyBlob(blob); | ||
232 | } | ||
233 | |||
234 | static void | ||
235 | dump_prop(uint32_t prop_id, uint64_t value) | ||
236 | { | ||
237 | int i; | ||
238 | drmModePropertyPtr prop; | ||
239 | |||
240 | prop = drmModeGetProperty(fd, prop_id); | ||
241 | |||
242 | printf("\t%d", prop_id); | ||
243 | if (!prop) { | ||
244 | printf("\n"); | ||
245 | return; | ||
246 | } | ||
247 | |||
248 | printf(" %s:\n", prop->name); | ||
249 | |||
250 | printf("\t\tflags:"); | ||
251 | if (prop->flags & DRM_MODE_PROP_PENDING(1<<0)) | ||
252 | printf(" pending"); | ||
253 | if (prop->flags & DRM_MODE_PROP_RANGE(1<<1)) | ||
254 | printf(" range"); | ||
255 | if (prop->flags & DRM_MODE_PROP_IMMUTABLE(1<<2)) | ||
256 | printf(" immutable"); | ||
257 | if (prop->flags & DRM_MODE_PROP_ENUM(1<<3)) | ||
258 | printf(" enum"); | ||
259 | if (prop->flags & DRM_MODE_PROP_BITMASK(1<<5)) | ||
260 | printf(" bitmask"); | ||
261 | if (prop->flags & DRM_MODE_PROP_BLOB(1<<4)) | ||
262 | printf(" blob"); | ||
263 | printf("\n"); | ||
264 | |||
265 | if (prop->flags & DRM_MODE_PROP_RANGE(1<<1)) { | ||
266 | printf("\t\tvalues:"); | ||
267 | for (i = 0; i < prop->count_values; i++) | ||
268 | printf(" %"PRIu64"ll" "u", prop->values[i]); | ||
269 | printf("\n"); | ||
270 | } | ||
271 | |||
272 | if (prop->flags & DRM_MODE_PROP_ENUM(1<<3)) { | ||
273 | printf("\t\tenums:"); | ||
274 | for (i = 0; i < prop->count_enums; i++) | ||
275 | printf(" %s=%llu", prop->enums[i].name, | ||
276 | prop->enums[i].value); | ||
277 | printf("\n"); | ||
278 | } else if (prop->flags & DRM_MODE_PROP_BITMASK(1<<5)) { | ||
279 | printf("\t\tvalues:"); | ||
280 | for (i = 0; i < prop->count_enums; i++) | ||
281 | printf(" %s=0x%llx", prop->enums[i].name, | ||
282 | (1LL << prop->enums[i].value)); | ||
283 | printf("\n"); | ||
284 | } else { | ||
285 | assert(prop->count_enums == 0)((prop->count_enums == 0) ? (void) (0) : __assert_fail ("prop->count_enums == 0" , "modetest.c", 285, __PRETTY_FUNCTION__)); | ||
286 | } | ||
287 | |||
288 | if (prop->flags & DRM_MODE_PROP_BLOB(1<<4)) { | ||
289 | printf("\t\tblobs:\n"); | ||
290 | for (i = 0; i < prop->count_blobs; i++) | ||
291 | dump_blob(prop->blob_ids[i]); | ||
292 | printf("\n"); | ||
293 | } else { | ||
294 | assert(prop->count_blobs == 0)((prop->count_blobs == 0) ? (void) (0) : __assert_fail ("prop->count_blobs == 0" , "modetest.c", 294, __PRETTY_FUNCTION__)); | ||
295 | } | ||
296 | |||
297 | printf("\t\tvalue:"); | ||
298 | if (prop->flags & DRM_MODE_PROP_BLOB(1<<4)) | ||
299 | dump_blob(value); | ||
300 | else | ||
301 | printf(" %"PRIu64"ll" "u""\n", value); | ||
302 | |||
303 | drmModeFreeProperty(prop); | ||
304 | } | ||
305 | |||
306 | void dump_connectors(void) | ||
307 | { | ||
308 | drmModeConnector *connector; | ||
309 | int i, j; | ||
310 | |||
311 | printf("Connectors:\n"); | ||
312 | printf("id\tencoder\tstatus\t\ttype\tsize (mm)\tmodes\tencoders\n"); | ||
313 | for (i = 0; i < resources->count_connectors; i++) { | ||
314 | connector = drmModeGetConnector(fd, resources->connectors[i]); | ||
315 | |||
316 | if (!connector) { | ||
317 | fprintf(stderrstderr, "could not get connector %i: %s\n", | ||
318 | resources->connectors[i], strerror(errno(*__errno_location ()))); | ||
319 | continue; | ||
320 | } | ||
321 | |||
322 | printf("%d\t%d\t%s\t%s\t%dx%d\t\t%d\t", | ||
323 | connector->connector_id, | ||
324 | connector->encoder_id, | ||
325 | connector_status_str(connector->connection), | ||
326 | connector_type_str(connector->connector_type), | ||
327 | connector->mmWidth, connector->mmHeight, | ||
328 | connector->count_modes); | ||
329 | |||
330 | for (j = 0; j < connector->count_encoders; j++) | ||
331 | printf("%s%d", j > 0 ? ", " : "", connector->encoders[j]); | ||
332 | printf("\n"); | ||
333 | |||
334 | if (connector->count_modes) { | ||
335 | printf(" modes:\n"); | ||
336 | printf("\tname refresh (Hz) hdisp hss hse htot vdisp " | ||
337 | "vss vse vtot)\n"); | ||
338 | for (j = 0; j < connector->count_modes; j++) | ||
339 | dump_mode(&connector->modes[j]); | ||
340 | |||
341 | printf(" props:\n"); | ||
342 | for (j = 0; j < connector->count_props; j++) | ||
343 | dump_prop(connector->props[j], | ||
344 | connector->prop_values[j]); | ||
345 | } | ||
346 | |||
347 | drmModeFreeConnector(connector); | ||
348 | } | ||
349 | printf("\n"); | ||
350 | } | ||
351 | |||
352 | void dump_crtcs(void) | ||
353 | { | ||
354 | drmModeCrtc *crtc; | ||
355 | drmModeObjectPropertiesPtr props; | ||
356 | int i; | ||
357 | uint32_t j; | ||
358 | |||
359 | printf("CRTCs:\n"); | ||
360 | printf("id\tfb\tpos\tsize\n"); | ||
361 | for (i = 0; i < resources->count_crtcs; i++) { | ||
362 | crtc = drmModeGetCrtc(fd, resources->crtcs[i]); | ||
363 | |||
364 | if (!crtc) { | ||
365 | fprintf(stderrstderr, "could not get crtc %i: %s\n", | ||
366 | resources->crtcs[i], strerror(errno(*__errno_location ()))); | ||
367 | continue; | ||
368 | } | ||
369 | printf("%d\t%d\t(%d,%d)\t(%dx%d)\n", | ||
370 | crtc->crtc_id, | ||
371 | crtc->buffer_id, | ||
372 | crtc->x, crtc->y, | ||
373 | crtc->width, crtc->height); | ||
374 | dump_mode(&crtc->mode); | ||
375 | |||
376 | printf(" props:\n"); | ||
377 | props = drmModeObjectGetProperties(fd, crtc->crtc_id, | ||
378 | DRM_MODE_OBJECT_CRTC0xcccccccc); | ||
379 | if (props) { | ||
380 | for (j = 0; j < props->count_props; j++) | ||
381 | dump_prop(props->props[j], | ||
382 | props->prop_values[j]); | ||
383 | drmModeFreeObjectProperties(props); | ||
384 | } else { | ||
385 | printf("\tcould not get crtc properties: %s\n", | ||
386 | strerror(errno(*__errno_location ()))); | ||
387 | } | ||
388 | |||
389 | drmModeFreeCrtc(crtc); | ||
390 | } | ||
391 | printf("\n"); | ||
392 | } | ||
393 | |||
394 | void dump_framebuffers(void) | ||
395 | { | ||
396 | drmModeFB *fb; | ||
397 | int i; | ||
398 | |||
399 | printf("Frame buffers:\n"); | ||
400 | printf("id\tsize\tpitch\n"); | ||
401 | for (i = 0; i < resources->count_fbs; i++) { | ||
402 | fb = drmModeGetFB(fd, resources->fbs[i]); | ||
403 | |||
404 | if (!fb) { | ||
405 | fprintf(stderrstderr, "could not get fb %i: %s\n", | ||
406 | resources->fbs[i], strerror(errno(*__errno_location ()))); | ||
407 | continue; | ||
408 | } | ||
409 | printf("%u\t(%ux%u)\t%u\n", | ||
410 | fb->fb_id, | ||
411 | fb->width, fb->height, | ||
412 | fb->pitch); | ||
413 | |||
414 | drmModeFreeFB(fb); | ||
415 | } | ||
416 | printf("\n"); | ||
417 | } | ||
418 | |||
419 | static void dump_planes(void) | ||
420 | { | ||
421 | drmModeObjectPropertiesPtr props; | ||
422 | drmModePlaneRes *plane_resources; | ||
423 | drmModePlane *ovr; | ||
424 | unsigned int i, j; | ||
425 | |||
426 | plane_resources = drmModeGetPlaneResources(fd); | ||
427 | if (!plane_resources) { | ||
428 | fprintf(stderrstderr, "drmModeGetPlaneResources failed: %s\n", | ||
429 | strerror(errno(*__errno_location ()))); | ||
430 | return; | ||
431 | } | ||
432 | |||
433 | printf("Planes:\n"); | ||
434 | printf("id\tcrtc\tfb\tCRTC x,y\tx,y\tgamma size\n"); | ||
435 | for (i = 0; i < plane_resources->count_planes; i++) { | ||
436 | ovr = drmModeGetPlane(fd, plane_resources->planes[i]); | ||
437 | if (!ovr) { | ||
438 | fprintf(stderrstderr, "drmModeGetPlane failed: %s\n", | ||
439 | strerror(errno(*__errno_location ()))); | ||
440 | continue; | ||
441 | } | ||
442 | |||
443 | printf("%d\t%d\t%d\t%d,%d\t\t%d,%d\t%d\n", | ||
444 | ovr->plane_id, ovr->crtc_id, ovr->fb_id, | ||
445 | ovr->crtc_x, ovr->crtc_y, ovr->x, ovr->y, | ||
446 | ovr->gamma_size); | ||
447 | |||
448 | if (!ovr->count_formats) | ||
449 | continue; | ||
450 | |||
451 | printf(" formats:"); | ||
452 | for (j = 0; j < ovr->count_formats; j++) | ||
453 | printf(" %4.4s", (char *)&ovr->formats[j]); | ||
454 | printf("\n"); | ||
455 | |||
456 | printf(" props:\n"); | ||
457 | props = drmModeObjectGetProperties(fd, ovr->plane_id, | ||
458 | DRM_MODE_OBJECT_PLANE0xeeeeeeee); | ||
459 | if (props) { | ||
460 | for (j = 0; j < props->count_props; j++) | ||
461 | dump_prop(props->props[j], | ||
462 | props->prop_values[j]); | ||
463 | drmModeFreeObjectProperties(props); | ||
464 | } else { | ||
465 | printf("\tcould not get plane properties: %s\n", | ||
466 | strerror(errno(*__errno_location ()))); | ||
467 | } | ||
468 | |||
469 | drmModeFreePlane(ovr); | ||
470 | } | ||
471 | printf("\n"); | ||
472 | |||
473 | drmModeFreePlaneResources(plane_resources); | ||
474 | return; | ||
475 | } | ||
476 | |||
477 | /* | ||
478 | * Mode setting with the kernel interfaces is a bit of a chore. | ||
479 | * First you have to find the connector in question and make sure the | ||
480 | * requested mode is available. | ||
481 | * Then you need to find the encoder attached to that connector so you | ||
482 | * can bind it with a free crtc. | ||
483 | */ | ||
484 | struct connector { | ||
485 | uint32_t id; | ||
486 | char mode_str[64]; | ||
487 | drmModeModeInfo *mode; | ||
488 | drmModeEncoder *encoder; | ||
489 | int crtc; | ||
490 | int pipe; | ||
491 | unsigned int fb_id[2], current_fb_id; | ||
492 | struct timeval start; | ||
493 | |||
494 | int swap_count; | ||
495 | }; | ||
496 | |||
497 | struct plane { | ||
498 | uint32_t con_id; /* the id of connector to bind to */ | ||
499 | uint32_t w, h; | ||
500 | unsigned int fb_id; | ||
501 | char format_str[5]; /* need to leave room for terminating \0 */ | ||
502 | }; | ||
503 | |||
504 | static void | ||
505 | connector_find_mode(struct connector *c) | ||
506 | { | ||
507 | drmModeConnector *connector; | ||
508 | int i, j; | ||
509 | |||
510 | /* First, find the connector & mode */ | ||
511 | c->mode = NULL((void *)0); | ||
512 | for (i = 0; i < resources->count_connectors; i++) { | ||
| |||
| |||
513 | connector = drmModeGetConnector(fd, resources->connectors[i]); | ||
514 | |||
515 | if (!connector) { | ||
| |||
| |||
| |||
| |||
516 | fprintf(stderrstderr, "could not get connector %i: %s\n", | ||
517 | resources->connectors[i], strerror(errno(*__errno_location ()))); | ||
518 | drmModeFreeConnector(connector); | ||
519 | continue; | ||
520 | } | ||
521 | |||
522 | if (!connector->count_modes) { | ||
| |||
| |||
523 | drmModeFreeConnector(connector); | ||
524 | continue; | ||
525 | } | ||
526 | |||
527 | if (connector->connector_id != c->id) { | ||
| |||
| |||
528 | drmModeFreeConnector(connector); | ||
529 | continue; | ||
530 | } | ||
531 | |||
532 | for (j = 0; j < connector->count_modes; j++) { | ||
| |||
| |||
| |||
533 | c->mode = &connector->modes[j]; | ||
534 | if (!strcmp(c->mode->name, c->mode_str)) | ||
| |||
535 | break; | ||
536 | } | ||
537 | |||
538 | /* Found it, break out */ | ||
539 | if (c->mode) | ||
| |||
| |||
540 | break; | ||
| |||
541 | |||
542 | drmModeFreeConnector(connector); | ||
543 | } | ||
544 | |||
545 | if (!c->mode) { | ||
| |||
546 | fprintf(stderrstderr, "failed to find mode \"%s\"\n", c->mode_str); | ||
547 | return; | ||
548 | } | ||
549 | |||
550 | /* Now get the encoder */ | ||
551 | for (i = 0; i < resources->count_encoders; i++) { | ||
| |||
| |||
| |||
552 | c->encoder = drmModeGetEncoder(fd, resources->encoders[i]); | ||
553 | |||
554 | if (!c->encoder) { | ||
| |||
| |||
555 | fprintf(stderrstderr, "could not get encoder %i: %s\n", | ||
556 | resources->encoders[i], strerror(errno(*__errno_location ()))); | ||
557 | drmModeFreeEncoder(c->encoder); | ||
558 | continue; | ||
| |||
559 | } | ||
560 | |||
561 | if (c->encoder->encoder_id == connector->encoder_id) | ||
| |||
562 | break; | ||
563 | |||
564 | drmModeFreeEncoder(c->encoder); | ||
565 | } | ||
566 | |||
567 | if (c->crtc == -1) | ||
| |||
568 | c->crtc = c->encoder->crtc_id; | ||
| |||
569 | |||
570 | /* and figure out which crtc index it is: */ | ||
571 | for (i = 0; i < resources->count_crtcs; i++) { | ||
572 | if (c->crtc == resources->crtcs[i]) { | ||
573 | c->pipe = i; | ||
574 | break; | ||
575 | } | ||
576 | } | ||
577 | |||
578 | } | ||
579 | |||
580 | static struct kms_bo * | ||
581 | allocate_buffer(struct kms_driver *kms, | ||
582 | int width, int height, int *stride) | ||
583 | { | ||
584 | struct kms_bo *bo; | ||
585 | unsigned bo_attribs[] = { | ||
586 | KMS_WIDTHKMS_WIDTH, 0, | ||
587 | KMS_HEIGHTKMS_HEIGHT, 0, | ||
588 | KMS_BO_TYPEKMS_BO_TYPE, KMS_BO_TYPE_SCANOUT_X8R8G8B8KMS_BO_TYPE_SCANOUT_X8R8G8B8, | ||
589 | KMS_TERMINATE_PROP_LISTKMS_TERMINATE_PROP_LIST | ||
590 | }; | ||
591 | int ret; | ||
592 | |||
593 | bo_attribs[1] = width; | ||
594 | bo_attribs[3] = height; | ||
595 | |||
596 | ret = kms_bo_create(kms, bo_attribs, &bo); | ||
597 | if (ret) { | ||
598 | fprintf(stderrstderr, "failed to alloc buffer: %s\n", | ||
599 | strerror(-ret)); | ||
600 | return NULL((void *)0); | ||
601 | } | ||
602 | |||
603 | ret = kms_bo_get_prop(bo, KMS_PITCHKMS_PITCH, stride); | ||
604 | if (ret) { | ||
605 | fprintf(stderrstderr, "failed to retreive buffer stride: %s\n", | ||
606 | strerror(-ret)); | ||
607 | kms_bo_destroy(&bo); | ||
608 | return NULL((void *)0); | ||
609 | } | ||
610 | |||
611 | return bo; | ||
612 | } | ||
613 | |||
614 | static void | ||
615 | make_pwetty(void *data, int width, int height, int stride) | ||
616 | { | ||
617 | #ifdef HAVE_CAIRO | ||
618 | cairo_surface_t *surface; | ||
619 | cairo_t *cr; | ||
620 | int x, y; | ||
621 | |||
622 | surface = cairo_image_surface_create_for_data(data, | ||
623 | CAIRO_FORMAT_ARGB32, | ||
624 | width, height, | ||
625 | stride); | ||
626 | cr = cairo_create(surface); | ||
627 | cairo_surface_destroy(surface); | ||
628 | |||
629 | cairo_set_line_cap(cr, CAIRO_LINE_CAP_SQUARE); | ||
630 | for (x = 0; x < width; x += 250) | ||
631 | for (y = 0; y < height; y += 250) { | ||
632 | char buf[64]; | ||
633 | |||
634 | cairo_move_to(cr, x, y - 20); | ||
635 | cairo_line_to(cr, x, y + 20); | ||
636 | cairo_move_to(cr, x - 20, y); | ||
637 | cairo_line_to(cr, x + 20, y); | ||
638 | cairo_new_sub_path(cr); | ||
639 | cairo_arc(cr, x, y, 10, 0, M_PI * 2); | ||
640 | cairo_set_line_width(cr, 4); | ||
641 | cairo_set_source_rgb(cr, 0, 0, 0); | ||
642 | cairo_stroke_preserve(cr); | ||
643 | cairo_set_source_rgb(cr, 1, 1, 1); | ||
644 | cairo_set_line_width(cr, 2); | ||
645 | cairo_stroke(cr); | ||
646 | |||
647 | snprintf(buf, sizeof buf, "%d, %d", x, y); | ||
648 | cairo_move_to(cr, x + 20, y + 20); | ||
649 | cairo_text_path(cr, buf); | ||
650 | cairo_set_source_rgb(cr, 0, 0, 0); | ||
651 | cairo_stroke_preserve(cr); | ||
652 | cairo_set_source_rgb(cr, 1, 1, 1); | ||
653 | cairo_fill(cr); | ||
654 | } | ||
655 | |||
656 | cairo_destroy(cr); | ||
657 | #endif | ||
658 | } | ||
659 | |||
660 | static int | ||
661 | create_test_buffer(struct kms_driver *kms, | ||
662 | int width, int height, int *stride_out, | ||
663 | struct kms_bo **bo_out) | ||
664 | { | ||
665 | struct kms_bo *bo; | ||
666 | int ret, i, j, stride; | ||
667 | void *virtual; | ||
668 | |||
669 | bo = allocate_buffer(kms, width, height, &stride); | ||
670 | if (!bo) | ||
671 | return -1; | ||
672 | |||
673 | ret = kms_bo_map(bo, &virtual); | ||
674 | if (ret) { | ||
675 | fprintf(stderrstderr, "failed to map buffer: %s\n", | ||
676 | strerror(-ret)); | ||
677 | kms_bo_destroy(&bo); | ||
678 | return -1; | ||
679 | } | ||
680 | |||
681 | /* paint the buffer with colored tiles */ | ||
682 | for (j = 0; j < height; j++) { | ||
683 | uint32_t *fb_ptr = (uint32_t*)((char*)virtual + j * stride); | ||
684 | for (i = 0; i < width; i++) { | ||
685 | div_t d = div(i, width); | ||
686 | fb_ptr[i] = | ||
687 | 0x00130502 * (d.quot >> 6) + | ||
688 | 0x000a1120 * (d.rem >> 6); | ||
689 | } | ||
690 | } | ||
691 | |||
692 | make_pwetty(virtual, width, height, stride); | ||
693 | |||
694 | kms_bo_unmap(bo); | ||
695 | |||
696 | *bo_out = bo; | ||
697 | *stride_out = stride; | ||
698 | return 0; | ||
699 | } | ||
700 | |||
701 | static int | ||
702 | create_grey_buffer(struct kms_driver *kms, | ||
703 | int width, int height, int *stride_out, | ||
704 | struct kms_bo **bo_out) | ||
705 | { | ||
706 | struct kms_bo *bo; | ||
707 | int size, ret, stride; | ||
708 | void *virtual; | ||
709 | |||
710 | bo = allocate_buffer(kms, width, height, &stride); | ||
711 | if (!bo) | ||
712 | return -1; | ||
713 | |||
714 | ret = kms_bo_map(bo, &virtual); | ||
715 | if (ret) { | ||
716 | fprintf(stderrstderr, "failed to map buffer: %s\n", | ||
717 | strerror(-ret)); | ||
718 | kms_bo_destroy(&bo); | ||
719 | return -1; | ||
720 | } | ||
721 | |||
722 | size = stride * height; | ||
723 | memset(virtual, 0x77, size); | ||
724 | kms_bo_unmap(bo); | ||
725 | |||
726 | *bo_out = bo; | ||
727 | *stride_out = stride; | ||
728 | |||
729 | return 0; | ||
730 | } | ||
731 | |||
732 | void | ||
733 | page_flip_handler(int fd, unsigned int frame, | ||
734 | unsigned int sec, unsigned int usec, void *data) | ||
735 | { | ||
736 | struct connector *c; | ||
737 | unsigned int new_fb_id; | ||
738 | struct timeval end; | ||
739 | double t; | ||
740 | |||
741 | c = data; | ||
742 | if (c->current_fb_id == c->fb_id[0]) | ||
743 | new_fb_id = c->fb_id[1]; | ||
744 | else | ||
745 | new_fb_id = c->fb_id[0]; | ||
746 | |||
747 | drmModePageFlip(fd, c->crtc, new_fb_id, | ||
748 | DRM_MODE_PAGE_FLIP_EVENT0x01, c); | ||
749 | c->current_fb_id = new_fb_id; | ||
750 | c->swap_count++; | ||
751 | if (c->swap_count == 60) { | ||
752 | gettimeofday(&end, NULL((void *)0)); | ||
753 | t = end.tv_sec + end.tv_usec * 1e-6 - | ||
754 | (c->start.tv_sec + c->start.tv_usec * 1e-6); | ||
755 | fprintf(stderrstderr, "freq: %.02fHz\n", c->swap_count / t); | ||
756 | c->swap_count = 0; | ||
757 | c->start = end; | ||
758 | } | ||
759 | } | ||
760 | |||
761 | /* swap these for big endian.. */ | ||
762 | #define RED2 2 | ||
763 | #define GREEN1 1 | ||
764 | #define BLUE0 0 | ||
765 | |||
766 | static void | ||
767 | fill420(unsigned char *y, unsigned char *u, unsigned char *v, | ||
768 | int cs /*chroma pixel stride */, | ||
769 | int n, int width, int height, int stride) | ||
770 | { | ||
771 | int i, j; | ||
772 | |||
773 | /* paint the buffer with colored tiles, in blocks of 2x2 */ | ||
774 | for (j = 0; j < height; j+=2) { | ||
775 | unsigned char *y1p = y + j * stride; | ||
776 | unsigned char *y2p = y1p + stride; | ||
777 | unsigned char *up = u + (j/2) * stride * cs / 2; | ||
778 | unsigned char *vp = v + (j/2) * stride * cs / 2; | ||
779 | |||
780 | for (i = 0; i < width; i+=2) { | ||
781 | div_t d = div(n+i+j, width); | ||
782 | uint32_t rgb = 0x00130502 * (d.quot >> 6) + 0x000a1120 * (d.rem >> 6); | ||
783 | unsigned char *rgbp = (unsigned char *)&rgb; | ||
784 | unsigned char y = (0.299 * rgbp[RED2]) + (0.587 * rgbp[GREEN1]) + (0.114 * rgbp[BLUE0]); | ||
785 | |||
786 | *(y2p++) = *(y1p++) = y; | ||
787 | *(y2p++) = *(y1p++) = y; | ||
788 | |||
789 | *up = (rgbp[BLUE0] - y) * 0.565 + 128; | ||
790 | *vp = (rgbp[RED2] - y) * 0.713 + 128; | ||
791 | up += cs; | ||
792 | vp += cs; | ||
793 | } | ||
794 | } | ||
795 | } | ||
796 | |||
797 | static void | ||
798 | fill422(unsigned char *virtual, int n, int width, int height, int stride) | ||
799 | { | ||
800 | int i, j; | ||
801 | /* paint the buffer with colored tiles */ | ||
802 | for (j = 0; j < height; j++) { | ||
803 | uint8_t *ptr = (uint8_t*)((char*)virtual + j * stride); | ||
804 | for (i = 0; i < width; i++) { | ||
805 | div_t d = div(n+i+j, width); | ||
806 | uint32_t rgb = 0x00130502 * (d.quot >> 6) + 0x000a1120 * (d.rem >> 6); | ||
807 | unsigned char *rgbp = (unsigned char *)&rgb; | ||
808 | unsigned char y = (0.299 * rgbp[RED2]) + (0.587 * rgbp[GREEN1]) + (0.114 * rgbp[BLUE0]); | ||
809 | |||
810 | *(ptr++) = y; | ||
811 | *(ptr++) = (rgbp[BLUE0] - y) * 0.565 + 128; | ||
812 | *(ptr++) = y; | ||
813 | *(ptr++) = (rgbp[RED2] - y) * 0.713 + 128; | ||
814 | } | ||
815 | } | ||
816 | } | ||
817 | |||
818 | static void | ||
819 | fill1555(unsigned char *virtual, int n, int width, int height, int stride) | ||
820 | { | ||
821 | int i, j; | ||
822 | /* paint the buffer with colored tiles */ | ||
823 | for (j = 0; j < height; j++) { | ||
824 | uint16_t *ptr = (uint16_t*)((char*)virtual + j * stride); | ||
825 | for (i = 0; i < width; i++) { | ||
826 | div_t d = div(n+i+j, width); | ||
827 | uint32_t rgb = 0x00130502 * (d.quot >> 6) + 0x000a1120 * (d.rem >> 6); | ||
828 | unsigned char *rgbp = (unsigned char *)&rgb; | ||
829 | |||
830 | *(ptr++) = 0x8000 | | ||
831 | (rgbp[RED2] >> 3) << 10 | | ||
832 | (rgbp[GREEN1] >> 3) << 5 | | ||
833 | (rgbp[BLUE0] >> 3); | ||
834 | } | ||
835 | } | ||
836 | } | ||
837 | |||
838 | static int | ||
839 | set_plane(struct kms_driver *kms, struct connector *c, struct plane *p) | ||
840 | { | ||
841 | drmModePlaneRes *plane_resources; | ||
842 | drmModePlane *ovr; | ||
843 | uint32_t handles[4], pitches[4], offsets[4] = {0}; /* we only use [0] */ | ||
844 | uint32_t plane_id = 0; | ||
845 | struct kms_bo *plane_bo; | ||
846 | uint32_t plane_flags = 0, format; | ||
847 | int ret, crtc_x, crtc_y, crtc_w, crtc_h; | ||
848 | unsigned int i; | ||
849 | |||
850 | /* find an unused plane which can be connected to our crtc */ | ||
851 | plane_resources = drmModeGetPlaneResources(fd); | ||
852 | if (!plane_resources) { | ||
853 | fprintf(stderrstderr, "drmModeGetPlaneResources failed: %s\n", | ||
854 | strerror(errno(*__errno_location ()))); | ||
855 | return -1; | ||
856 | } | ||
857 | |||
858 | for (i = 0; i < plane_resources->count_planes && !plane_id; i++) { | ||
859 | ovr = drmModeGetPlane(fd, plane_resources->planes[i]); | ||
860 | if (!ovr) { | ||
861 | fprintf(stderrstderr, "drmModeGetPlane failed: %s\n", | ||
862 | strerror(errno(*__errno_location ()))); | ||
863 | return -1; | ||
864 | } | ||
865 | |||
866 | if ((ovr->possible_crtcs & (1 << c->pipe)) && !ovr->crtc_id) | ||
867 | plane_id = ovr->plane_id; | ||
868 | |||
869 | drmModeFreePlane(ovr); | ||
870 | } | ||
871 | |||
872 | fprintf(stderrstderr, "testing %dx%d@%s overlay plane\n", | ||
873 | p->w, p->h, p->format_str); | ||
874 | |||
875 | if (!plane_id) { | ||
876 | fprintf(stderrstderr, "failed to find plane!\n"); | ||
877 | return -1; | ||
878 | } | ||
879 | |||
880 | if (!strcmp(p->format_str, "XR24")) { | ||
881 | if (create_test_buffer(kms, p->w, p->h, &pitches[0], &plane_bo)) | ||
882 | return -1; | ||
883 | kms_bo_get_prop(plane_bo, KMS_HANDLEKMS_HANDLE, &handles[0]); | ||
884 | format = DRM_FORMAT_XRGB8888((uint32_t)('X') | ((uint32_t)('R') << 8) | ((uint32_t) ('2') << 16) | ((uint32_t)('4') << 24)); | ||
885 | } else { | ||
886 | void *virtual; | ||
887 | |||
888 | /* TODO: this always allocates a buffer for 32bpp RGB.. but for | ||
889 | * YUV formats, we don't use all of it.. since 4bytes/pixel is | ||
890 | * worst case, so live with it for now and just don't use all | ||
891 | * the buffer: | ||
892 | */ | ||
893 | plane_bo = allocate_buffer(kms, p->w, p->h, &pitches[0]); | ||
894 | if (!plane_bo) | ||
895 | return -1; | ||
896 | |||
897 | ret = kms_bo_map(plane_bo, &virtual); | ||
898 | if (ret) { | ||
899 | fprintf(stderrstderr, "failed to map buffer: %s\n", | ||
900 | strerror(-ret)); | ||
901 | kms_bo_destroy(&plane_bo); | ||
902 | return -1; | ||
903 | } | ||
904 | |||
905 | /* just testing a limited # of formats to test single | ||
906 | * and multi-planar path.. would be nice to add more.. | ||
907 | */ | ||
908 | if (!strcmp(p->format_str, "YUYV")) { | ||
909 | pitches[0] = p->w * 2; | ||
910 | offsets[0] = 0; | ||
911 | kms_bo_get_prop(plane_bo, KMS_HANDLEKMS_HANDLE, &handles[0]); | ||
912 | |||
913 | fill422(virtual, 0, p->w, p->h, pitches[0]); | ||
914 | |||
915 | format = DRM_FORMAT_YUYV((uint32_t)('Y') | ((uint32_t)('U') << 8) | ((uint32_t) ('Y') << 16) | ((uint32_t)('V') << 24)); | ||
916 | } else if (!strcmp(p->format_str, "NV12")) { | ||
917 | pitches[0] = p->w; | ||
918 | offsets[0] = 0; | ||
919 | kms_bo_get_prop(plane_bo, KMS_HANDLEKMS_HANDLE, &handles[0]); | ||
920 | pitches[1] = p->w; | ||
921 | offsets[1] = p->w * p->h; | ||
922 | kms_bo_get_prop(plane_bo, KMS_HANDLEKMS_HANDLE, &handles[1]); | ||
923 | |||
924 | fill420(virtual, virtual+offsets[1], virtual+offsets[1]+1, | ||
925 | 2, 0, p->w, p->h, pitches[0]); | ||
926 | |||
927 | format = DRM_FORMAT_NV12((uint32_t)('N') | ((uint32_t)('V') << 8) | ((uint32_t) ('1') << 16) | ((uint32_t)('2') << 24)); | ||
928 | } else if (!strcmp(p->format_str, "YV12")) { | ||
929 | pitches[0] = p->w; | ||
930 | offsets[0] = 0; | ||
931 | kms_bo_get_prop(plane_bo, KMS_HANDLEKMS_HANDLE, &handles[0]); | ||
932 | pitches[1] = p->w / 2; | ||
933 | offsets[1] = p->w * p->h; | ||
934 | kms_bo_get_prop(plane_bo, KMS_HANDLEKMS_HANDLE, &handles[1]); | ||
935 | pitches[2] = p->w / 2; | ||
936 | offsets[2] = offsets[1] + (p->w * p->h) / 4; | ||
937 | kms_bo_get_prop(plane_bo, KMS_HANDLEKMS_HANDLE, &handles[2]); | ||
938 | |||
939 | fill420(virtual, virtual+offsets[1], virtual+offsets[2], | ||
940 | 1, 0, p->w, p->h, pitches[0]); | ||
941 | |||
942 | format = DRM_FORMAT_YVU420((uint32_t)('Y') | ((uint32_t)('V') << 8) | ((uint32_t) ('1') << 16) | ((uint32_t)('2') << 24)); | ||
943 | } else if (!strcmp(p->format_str, "XR15")) { | ||
944 | pitches[0] = p->w * 2; | ||
945 | offsets[0] = 0; | ||
946 | kms_bo_get_prop(plane_bo, KMS_HANDLEKMS_HANDLE, &handles[0]); | ||
947 | |||
948 | fill1555(virtual, 0, p->w, p->h, pitches[0]); | ||
949 | |||
950 | format = DRM_FORMAT_XRGB1555((uint32_t)('X') | ((uint32_t)('R') << 8) | ((uint32_t) ('1') << 16) | ((uint32_t)('5') << 24)); | ||
951 | } else if (!strcmp(p->format_str, "AR15")) { | ||
952 | pitches[0] = p->w * 2; | ||
953 | offsets[0] = 0; | ||
954 | kms_bo_get_prop(plane_bo, KMS_HANDLEKMS_HANDLE, &handles[0]); | ||
955 | |||
956 | fill1555(virtual, 0, p->w, p->h, pitches[0]); | ||
957 | |||
958 | format = DRM_FORMAT_ARGB1555((uint32_t)('A') | ((uint32_t)('R') << 8) | ((uint32_t) ('1') << 16) | ((uint32_t)('5') << 24)); | ||
959 | } else { | ||
960 | fprintf(stderrstderr, "Unknown format: %s\n", p->format_str); | ||
961 | return -1; | ||
962 | } | ||
963 | |||
964 | kms_bo_unmap(plane_bo); | ||
965 | } | ||
966 | |||
967 | /* just use single plane format for now.. */ | ||
968 | if (drmModeAddFB2(fd, p->w, p->h, format, | ||
969 | handles, pitches, offsets, &p->fb_id, plane_flags)) { | ||
970 | fprintf(stderrstderr, "failed to add fb: %s\n", strerror(errno(*__errno_location ()))); | ||
971 | return -1; | ||
972 | } | ||
973 | |||
974 | /* ok, boring.. but for now put in middle of screen: */ | ||
975 | crtc_x = c->mode->hdisplay / 3; | ||
976 | crtc_y = c->mode->vdisplay / 3; | ||
977 | crtc_w = crtc_x; | ||
978 | crtc_h = crtc_y; | ||
979 | |||
980 | /* note src coords (last 4 args) are in Q16 format */ | ||
981 | if (drmModeSetPlane(fd, plane_id, c->crtc, p->fb_id, | ||
982 | plane_flags, crtc_x, crtc_y, crtc_w, crtc_h, | ||
983 | 0, 0, p->w << 16, p->h << 16)) { | ||
984 | fprintf(stderrstderr, "failed to enable plane: %s\n", | ||
985 | strerror(errno(*__errno_location ()))); | ||
986 | return -1; | ||
987 | } | ||
988 | |||
989 | return 0; | ||
990 | } | ||
991 | |||
992 | static void | ||
993 | set_mode(struct connector *c, int count, struct plane *p, int plane_count, | ||
994 | int page_flip) | ||
995 | { | ||
996 | struct kms_driver *kms; | ||
997 | struct kms_bo *bo, *other_bo; | ||
998 | unsigned int fb_id, other_fb_id; | ||
999 | int i, j, ret, width, height, x, stride; | ||
1000 | unsigned handle; | ||
1001 | drmEventContext evctx; | ||
1002 | |||
1003 | width = 0; | ||
1004 | height = 0; | ||
1005 | for (i = 0; i < count; i++) { | ||
1006 | connector_find_mode(&c[i]); | ||
1007 | if (c[i].mode == NULL((void *)0)) | ||
1008 | continue; | ||
1009 | width += c[i].mode->hdisplay; | ||
1010 | if (height < c[i].mode->vdisplay) | ||
1011 | height = c[i].mode->vdisplay; | ||
1012 | } | ||
1013 | |||
1014 | ret = kms_create(fd, &kms); | ||
1015 | if (ret) { | ||
1016 | fprintf(stderrstderr, "failed to create kms driver: %s\n", | ||
1017 | strerror(-ret)); | ||
1018 | return; | ||
1019 | } | ||
1020 | |||
1021 | if (create_test_buffer(kms, width, height, &stride, &bo)) | ||
1022 | return; | ||
1023 | |||
1024 | kms_bo_get_prop(bo, KMS_HANDLEKMS_HANDLE, &handle); | ||
1025 | ret = drmModeAddFB(fd, width, height, 24, 32, stride, handle, &fb_id); | ||
1026 | if (ret) { | ||
1027 | fprintf(stderrstderr, "failed to add fb (%ux%u): %s\n", | ||
1028 | width, height, strerror(errno(*__errno_location ()))); | ||
1029 | return; | ||
1030 | } | ||
1031 | |||
1032 | x = 0; | ||
1033 | for (i = 0; i < count; i++) { | ||
1034 | if (c[i].mode == NULL((void *)0)) | ||
1035 | continue; | ||
1036 | |||
1037 | printf("setting mode %s on connector %d, crtc %d\n", | ||
1038 | c[i].mode_str, c[i].id, c[i].crtc); | ||
1039 | |||
1040 | ret = drmModeSetCrtc(fd, c[i].crtc, fb_id, x, 0, | ||
1041 | &c[i].id, 1, c[i].mode); | ||
1042 | |||
1043 | /* XXX: Actually check if this is needed */ | ||
1044 | drmModeDirtyFB(fd, fb_id, NULL((void *)0), 0); | ||
1045 | |||
1046 | x += c[i].mode->hdisplay; | ||
1047 | |||
1048 | if (ret) { | ||
1049 | fprintf(stderrstderr, "failed to set mode: %s\n", strerror(errno(*__errno_location ()))); | ||
1050 | return; | ||
1051 | } | ||
1052 | |||
1053 | /* if we have a plane/overlay to show, set that up now: */ | ||
1054 | for (j = 0; j < plane_count; j++) | ||
1055 | if (p[j].con_id == c[i].id) | ||
1056 | if (set_plane(kms, &c[i], &p[j])) | ||
1057 | return; | ||
1058 | } | ||
1059 | |||
1060 | if (!page_flip) | ||
1061 | return; | ||
1062 | |||
1063 | if (create_grey_buffer(kms, width, height, &stride, &other_bo)) | ||
1064 | return; | ||
1065 | |||
1066 | kms_bo_get_prop(other_bo, KMS_HANDLEKMS_HANDLE, &handle); | ||
1067 | ret = drmModeAddFB(fd, width, height, 32, 32, stride, handle, | ||
1068 | &other_fb_id); | ||
1069 | if (ret) { | ||
1070 | fprintf(stderrstderr, "failed to add fb: %s\n", strerror(errno(*__errno_location ()))); | ||
1071 | return; | ||
1072 | } | ||
1073 | |||
1074 | for (i = 0; i < count; i++) { | ||
1075 | if (c[i].mode == NULL((void *)0)) | ||
1076 | continue; | ||
1077 | |||
1078 | ret = drmModePageFlip(fd, c[i].crtc, other_fb_id, | ||
1079 | DRM_MODE_PAGE_FLIP_EVENT0x01, &c[i]); | ||
1080 | if (ret) { | ||
1081 | fprintf(stderrstderr, "failed to page flip: %s\n", strerror(errno(*__errno_location ()))); | ||
1082 | return; | ||
1083 | } | ||
1084 | gettimeofday(&c[i].start, NULL((void *)0)); | ||
1085 | c[i].swap_count = 0; | ||
1086 | c[i].fb_id[0] = fb_id; | ||
1087 | c[i].fb_id[1] = other_fb_id; | ||
1088 | c[i].current_fb_id = other_fb_id; | ||
1089 | } | ||
1090 | |||
1091 | memset(&evctx, 0, sizeof evctx); | ||
1092 | evctx.version = DRM_EVENT_CONTEXT_VERSION2; | ||
1093 | evctx.vblank_handler = NULL((void *)0); | ||
1094 | evctx.page_flip_handler = page_flip_handler; | ||
1095 | |||
1096 | while (1) { | ||
1097 | #if 0 | ||
1098 | struct pollfd pfd[2]; | ||
1099 | |||
1100 | pfd[0].fd = 0; | ||
1101 | pfd[0].events = POLLIN0x001; | ||
1102 | pfd[1].fd = fd; | ||
1103 | pfd[1].events = POLLIN0x001; | ||
1104 | |||
1105 | if (poll(pfd, 2, -1) < 0) { | ||
1106 | fprintf(stderrstderr, "poll error\n"); | ||
1107 | break; | ||
1108 | } | ||
1109 | |||
1110 | if (pfd[0].revents) | ||
1111 | break; | ||
1112 | #else | ||
1113 | struct timeval timeout = { .tv_sec = 3, .tv_usec = 0 }; | ||
1114 | fd_set fds; | ||
1115 | int ret; | ||
1116 | |||
1117 | FD_ZERO(&fds)do { unsigned int __i; fd_set *__arr = (&fds); for (__i = 0; __i < sizeof (fd_set) / sizeof (__fd_mask); ++__i) ((__arr )->fds_bits)[__i] = 0; } while (0); | ||
1118 | FD_SET(0, &fds)((void) (((&fds)->fds_bits)[((0) / (8 * sizeof(unsigned long)))] |= ((__fd_mask) 1 << ((0) % (8 * sizeof(unsigned long)))))); | ||
1119 | FD_SET(fd, &fds)((void) (((&fds)->fds_bits)[((fd) / (8 * sizeof(unsigned long)))] |= ((__fd_mask) 1 << ((fd) % (8 * sizeof(unsigned long)))))); | ||
1120 | ret = select(fd + 1, &fds, NULL((void *)0), NULL((void *)0), &timeout); | ||
1121 | |||
1122 | if (ret <= 0) { | ||
1123 | fprintf(stderrstderr, "select timed out or error (ret %d)\n", | ||
1124 | ret); | ||
1125 | continue; | ||
1126 | } else if (FD_ISSET(0, &fds)((((&fds)->fds_bits)[((0) / (8 * sizeof(unsigned long) ))] & ((__fd_mask) 1 << ((0) % (8 * sizeof(unsigned long))))) != 0)) { | ||
1127 | break; | ||
1128 | } | ||
1129 | #endif | ||
1130 | |||
1131 | drmHandleEvent(fd, &evctx); | ||
1132 | } | ||
1133 | |||
1134 | kms_bo_destroy(&bo); | ||
1135 | kms_bo_destroy(&other_bo); | ||
1136 | kms_destroy(&kms); | ||
1137 | } | ||
1138 | |||
1139 | extern char *optarg; | ||
1140 | extern int optind, opterr, optopt; | ||
1141 | static char optstr[] = "ecpmfs:P:v"; | ||
1142 | |||
1143 | void usage(char *name) | ||
1144 | { | ||
1145 | fprintf(stderrstderr, "usage: %s [-ecpmf]\n", name); | ||
1146 | fprintf(stderrstderr, "\t-e\tlist encoders\n"); | ||
1147 | fprintf(stderrstderr, "\t-c\tlist connectors\n"); | ||
1148 | fprintf(stderrstderr, "\t-p\tlist CRTCs and planes (pipes)\n"); | ||
1149 | fprintf(stderrstderr, "\t-m\tlist modes\n"); | ||
1150 | fprintf(stderrstderr, "\t-f\tlist framebuffers\n"); | ||
1151 | fprintf(stderrstderr, "\t-v\ttest vsynced page flipping\n"); | ||
1152 | fprintf(stderrstderr, "\t-s <connector_id>:<mode>\tset a mode\n"); | ||
1153 | fprintf(stderrstderr, "\t-s <connector_id>@<crtc_id>:<mode>\tset a mode\n"); | ||
1154 | fprintf(stderrstderr, "\t-P <connector_id>:<w>x<h>\tset a plane\n"); | ||
1155 | fprintf(stderrstderr, "\t-P <connector_id>:<w>x<h>@<format>\tset a plane\n"); | ||
1156 | fprintf(stderrstderr, "\n\tDefault is to dump all info.\n"); | ||
1157 | exit(0); | ||
1158 | } | ||
1159 | |||
1160 | #define dump_resource(res)if (res) dump_res() if (res) dump_##res() | ||
1161 | |||
1162 | static int page_flipping_supported(void) | ||
1163 | { | ||
1164 | /*FIXME: generic ioctl needed? */ | ||
1165 | return 1; | ||
1166 | #if 0 | ||
1167 | int ret, value; | ||
1168 | struct drm_i915_getparam gp; | ||
1169 | |||
1170 | gp.param = I915_PARAM_HAS_PAGEFLIPPING; | ||
1171 | gp.value = &value; | ||
1172 | |||
1173 | ret = drmCommandWriteRead(fd, DRM_I915_GETPARAM, &gp, sizeof(gp)); | ||
1174 | if (ret) { | ||
1175 | fprintf(stderrstderr, "drm_i915_getparam: %m\n"); | ||
1176 | return 0; | ||
1177 | } | ||
1178 | |||
1179 | return *gp.value; | ||
1180 | #endif | ||
1181 | } | ||
1182 | |||
1183 | int main(int argc, char **argv) | ||
1184 | { | ||
1185 | int c; | ||
1186 | int encoders = 0, connectors = 0, crtcs = 0, planes = 0, framebuffers = 0; | ||
1187 | int test_vsync = 0; | ||
1188 | char *modules[] = { "i915", "radeon", "nouveau", "vmwgfx", "omapdrm", "exynos" }; | ||
1189 | unsigned int i; | ||
1190 | int count = 0, plane_count = 0; | ||
1191 | struct connector con_args[2]; | ||
1192 | struct plane plane_args[2] = {0}; | ||
1193 | |||
1194 | opterr = 0; | ||
1195 | while ((c = getopt(argc, argv, optstr)) != -1) { | ||
1196 | switch (c) { | ||
1197 | case 'e': | ||
1198 | encoders = 1; | ||
1199 | break; | ||
1200 | case 'c': | ||
1201 | connectors = 1; | ||
1202 | break; | ||
1203 | case 'p': | ||
1204 | crtcs = 1; | ||
1205 | planes = 1; | ||
1206 | break; | ||
1207 | case 'm': | ||
1208 | modes = 1; | ||
1209 | break; | ||
1210 | case 'f': | ||
1211 | framebuffers = 1; | ||
1212 | break; | ||
1213 | case 'v': | ||
1214 | test_vsync = 1; | ||
1215 | break; | ||
1216 | case 's': | ||
1217 | con_args[count].crtc = -1; | ||
1218 | if (sscanf(optarg, "%d:%64s", | ||
1219 | &con_args[count].id, | ||
1220 | con_args[count].mode_str) != 2 && | ||
1221 | sscanf(optarg, "%d@%d:%64s", | ||
1222 | &con_args[count].id, | ||
1223 | &con_args[count].crtc, | ||
1224 | con_args[count].mode_str) != 3) | ||
1225 | usage(argv[0]); | ||
1226 | count++; | ||
1227 | break; | ||
1228 | case 'P': | ||
1229 | strcpy(plane_args[plane_count].format_str, "XR24"); | ||
1230 | if (sscanf(optarg, "%d:%dx%d@%4s", | ||
1231 | &plane_args[plane_count].con_id, | ||
1232 | &plane_args[plane_count].w, | ||
1233 | &plane_args[plane_count].h, | ||
1234 | plane_args[plane_count].format_str) != 4 && | ||
1235 | sscanf(optarg, "%d:%dx%d", | ||
1236 | &plane_args[plane_count].con_id, | ||
1237 | &plane_args[plane_count].w, | ||
1238 | &plane_args[plane_count].h) != 3) | ||
1239 | usage(argv[0]); | ||
1240 | plane_count++; | ||
1241 | break; | ||
1242 | default: | ||
1243 | usage(argv[0]); | ||
1244 | break; | ||
1245 | } | ||
1246 | } | ||
1247 | |||
1248 | if (argc == 1) | ||
1249 | encoders = connectors = crtcs = planes = modes = framebuffers = 1; | ||
1250 | |||
1251 | for (i = 0; i < ARRAY_SIZE(modules)(sizeof(modules) / sizeof((modules)[0])); i++) { | ||
1252 | printf("trying to load module %s...", modules[i]); | ||
1253 | fd = drmOpen(modules[i], NULL((void *)0)); | ||
1254 | if (fd < 0) { | ||
1255 | printf("failed.\n"); | ||
1256 | } else { | ||
1257 | printf("success.\n"); | ||
1258 | break; | ||
1259 | } | ||
1260 | } | ||
1261 | |||
1262 | if (test_vsync && !page_flipping_supported()) { | ||
1263 | fprintf(stderrstderr, "page flipping not supported by drm.\n"); | ||
1264 | return -1; | ||
1265 | } | ||
1266 | |||
1267 | if (i == ARRAY_SIZE(modules)(sizeof(modules) / sizeof((modules)[0]))) { | ||
1268 | fprintf(stderrstderr, "failed to load any modules, aborting.\n"); | ||
1269 | return -1; | ||
1270 | } | ||
1271 | |||
1272 | resources = drmModeGetResources(fd); | ||
1273 | if (!resources) { | ||
1274 | fprintf(stderrstderr, "drmModeGetResources failed: %s\n", | ||
1275 | strerror(errno(*__errno_location ()))); | ||
1276 | drmClose(fd); | ||
1277 | return 1; | ||
1278 | } | ||
1279 | |||
1280 | dump_resource(encoders)if (encoders) dump_encoders(); | ||
1281 | dump_resource(connectors)if (connectors) dump_connectors(); | ||
1282 | dump_resource(crtcs)if (crtcs) dump_crtcs(); | ||
1283 | dump_resource(planes)if (planes) dump_planes(); | ||
1284 | dump_resource(framebuffers)if (framebuffers) dump_framebuffers(); | ||
1285 | |||
1286 | if (count > 0) { | ||
1287 | set_mode(con_args, count, plane_args, plane_count, test_vsync); | ||
1288 | getchar(); | ||
1289 | } | ||
1290 | |||
1291 | drmModeFreeResources(resources); | ||
1292 | |||
1293 | return 0; | ||
1294 | } |