2 * Mesa 3-D graphics library
5 * Copyright (C) 2010 LunarG Inc.
7 * Permission is hereby granted, free of charge, to any person obtaining a
8 * copy of this software and associated documentation files (the "Software"),
9 * to deal in the Software without restriction, including without limitation
10 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
11 * and/or sell copies of the Software, and to permit persons to whom the
12 * Software is furnished to do so, subject to the following conditions:
14 * The above copyright notice and this permission notice shall be included
15 * in all copies or substantial portions of the Software.
17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
20 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
22 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
23 * DEALINGS IN THE SOFTWARE.
26 * Chia-I Wu <olv@lunarg.com>
29 #include "util/u_memory.h"
30 #include "util/u_inlines.h"
33 #include "native_drm.h"
36 drm_surface_validate(struct native_surface
*nsurf
, uint attachment_mask
,
37 unsigned int *seq_num
, struct pipe_resource
**textures
,
38 int *width
, int *height
)
40 struct drm_surface
*drmsurf
= drm_surface(nsurf
);
42 if (!resource_surface_add_resources(drmsurf
->rsurf
, attachment_mask
))
45 resource_surface_get_resources(drmsurf
->rsurf
, textures
, attachment_mask
);
48 *seq_num
= drmsurf
->sequence_number
;
50 *width
= drmsurf
->width
;
52 *height
= drmsurf
->height
;
58 * Add textures as DRM framebuffers.
61 drm_surface_init_framebuffers(struct native_surface
*nsurf
, boolean need_back
)
63 struct drm_surface
*drmsurf
= drm_surface(nsurf
);
64 struct drm_display
*drmdpy
= drmsurf
->drmdpy
;
65 int num_framebuffers
= (need_back
) ? 2 : 1;
68 for (i
= 0; i
< num_framebuffers
; i
++) {
69 struct drm_framebuffer
*fb
;
70 enum native_attachment natt
;
71 struct winsys_handle whandle
;
75 fb
= &drmsurf
->front_fb
;
76 natt
= NATIVE_ATTACHMENT_FRONT_LEFT
;
79 fb
= &drmsurf
->back_fb
;
80 natt
= NATIVE_ATTACHMENT_BACK_LEFT
;
84 /* make sure the texture has been allocated */
85 resource_surface_add_resources(drmsurf
->rsurf
, 1 << natt
);
87 resource_surface_get_single_resource(drmsurf
->rsurf
, natt
);
92 /* already initialized */
96 /* TODO detect the real value */
97 fb
->is_passive
= TRUE
;
99 memset(&whandle
, 0, sizeof(whandle
));
100 whandle
.type
= DRM_API_HANDLE_TYPE_KMS
;
102 if (!drmdpy
->base
.screen
->resource_get_handle(drmdpy
->base
.screen
,
103 fb
->texture
, &whandle
))
106 block_bits
= util_format_get_blocksizebits(drmsurf
->color_format
);
107 err
= drmModeAddFB(drmdpy
->fd
, drmsurf
->width
, drmsurf
->height
,
108 block_bits
, block_bits
, whandle
.stride
, whandle
.handle
,
120 drm_surface_flush_frontbuffer(struct native_surface
*nsurf
)
122 #ifdef DRM_MODE_FEATURE_DIRTYFB
123 struct drm_surface
*drmsurf
= drm_surface(nsurf
);
124 struct drm_display
*drmdpy
= drmsurf
->drmdpy
;
126 if (drmsurf
->front_fb
.is_passive
)
127 drmModeDirtyFB(drmdpy
->fd
, drmsurf
->front_fb
.buffer_id
, NULL
, 0);
134 drm_surface_swap_buffers(struct native_surface
*nsurf
)
136 struct drm_surface
*drmsurf
= drm_surface(nsurf
);
137 struct drm_crtc
*drmcrtc
= &drmsurf
->current_crtc
;
138 struct drm_display
*drmdpy
= drmsurf
->drmdpy
;
139 struct drm_framebuffer tmp_fb
;
142 if (!drmsurf
->back_fb
.buffer_id
) {
143 if (!drm_surface_init_framebuffers(&drmsurf
->base
, TRUE
))
147 if (drmsurf
->is_shown
&& drmcrtc
->crtc
) {
148 err
= drmModeSetCrtc(drmdpy
->fd
, drmcrtc
->crtc
->crtc_id
,
149 drmsurf
->back_fb
.buffer_id
, drmcrtc
->crtc
->x
, drmcrtc
->crtc
->y
,
150 drmcrtc
->connectors
, drmcrtc
->num_connectors
, &drmcrtc
->crtc
->mode
);
155 /* swap the buffers */
156 tmp_fb
= drmsurf
->front_fb
;
157 drmsurf
->front_fb
= drmsurf
->back_fb
;
158 drmsurf
->back_fb
= tmp_fb
;
160 resource_surface_swap_buffers(drmsurf
->rsurf
,
161 NATIVE_ATTACHMENT_FRONT_LEFT
, NATIVE_ATTACHMENT_BACK_LEFT
, FALSE
);
162 /* the front/back textures are swapped */
163 drmsurf
->sequence_number
++;
164 drmdpy
->event_handler
->invalid_surface(&drmdpy
->base
,
165 &drmsurf
->base
, drmsurf
->sequence_number
);
171 drm_surface_present(struct native_surface
*nsurf
,
172 enum native_attachment natt
,
178 if (preserve
|| swap_interval
)
182 case NATIVE_ATTACHMENT_FRONT_LEFT
:
183 ret
= drm_surface_flush_frontbuffer(nsurf
);
185 case NATIVE_ATTACHMENT_BACK_LEFT
:
186 ret
= drm_surface_swap_buffers(nsurf
);
197 drm_surface_wait(struct native_surface
*nsurf
)
203 drm_surface_destroy(struct native_surface
*nsurf
)
205 struct drm_surface
*drmsurf
= drm_surface(nsurf
);
207 if (drmsurf
->current_crtc
.crtc
)
208 drmModeFreeCrtc(drmsurf
->current_crtc
.crtc
);
210 if (drmsurf
->front_fb
.buffer_id
)
211 drmModeRmFB(drmsurf
->drmdpy
->fd
, drmsurf
->front_fb
.buffer_id
);
212 pipe_resource_reference(&drmsurf
->front_fb
.texture
, NULL
);
214 if (drmsurf
->back_fb
.buffer_id
)
215 drmModeRmFB(drmsurf
->drmdpy
->fd
, drmsurf
->back_fb
.buffer_id
);
216 pipe_resource_reference(&drmsurf
->back_fb
.texture
, NULL
);
218 resource_surface_destroy(drmsurf
->rsurf
);
222 static struct drm_surface
*
223 drm_display_create_surface(struct native_display
*ndpy
,
224 const struct native_config
*nconf
,
225 uint width
, uint height
)
227 struct drm_display
*drmdpy
= drm_display(ndpy
);
228 struct drm_config
*drmconf
= drm_config(nconf
);
229 struct drm_surface
*drmsurf
;
231 drmsurf
= CALLOC_STRUCT(drm_surface
);
235 drmsurf
->drmdpy
= drmdpy
;
236 drmsurf
->color_format
= drmconf
->base
.color_format
;
237 drmsurf
->width
= width
;
238 drmsurf
->height
= height
;
240 drmsurf
->rsurf
= resource_surface_create(drmdpy
->base
.screen
,
241 drmsurf
->color_format
,
242 PIPE_BIND_RENDER_TARGET
|
243 PIPE_BIND_SAMPLER_VIEW
|
244 PIPE_BIND_DISPLAY_TARGET
|
246 if (!drmsurf
->rsurf
) {
251 resource_surface_set_size(drmsurf
->rsurf
, drmsurf
->width
, drmsurf
->height
);
253 drmsurf
->base
.destroy
= drm_surface_destroy
;
254 drmsurf
->base
.present
= drm_surface_present
;
255 drmsurf
->base
.validate
= drm_surface_validate
;
256 drmsurf
->base
.wait
= drm_surface_wait
;
262 * Choose a CRTC that supports all given connectors.
265 drm_display_choose_crtc(struct native_display
*ndpy
,
266 uint32_t *connectors
, int num_connectors
)
268 struct drm_display
*drmdpy
= drm_display(ndpy
);
271 for (idx
= 0; idx
< drmdpy
->resources
->count_crtcs
; idx
++) {
272 boolean found_crtc
= TRUE
;
275 for (i
= 0; i
< num_connectors
; i
++) {
276 drmModeConnectorPtr connector
;
277 int encoder_idx
= -1;
279 connector
= drmModeGetConnector(drmdpy
->fd
, connectors
[i
]);
285 /* find an encoder the CRTC supports */
286 for (j
= 0; j
< connector
->count_encoders
; j
++) {
287 drmModeEncoderPtr encoder
=
288 drmModeGetEncoder(drmdpy
->fd
, connector
->encoders
[j
]);
289 if (encoder
->possible_crtcs
& (1 << idx
)) {
293 drmModeFreeEncoder(encoder
);
296 drmModeFreeConnector(connector
);
297 if (encoder_idx
< 0) {
307 if (idx
>= drmdpy
->resources
->count_crtcs
) {
308 _eglLog(_EGL_WARNING
,
309 "failed to find a CRTC that supports the given %d connectors",
314 return drmdpy
->resources
->crtcs
[idx
];
318 * Remember the original CRTC status and set the CRTC
321 drm_display_set_crtc(struct native_display
*ndpy
, int crtc_idx
,
322 uint32_t buffer_id
, uint32_t x
, uint32_t y
,
323 uint32_t *connectors
, int num_connectors
,
324 drmModeModeInfoPtr mode
)
326 struct drm_display
*drmdpy
= drm_display(ndpy
);
327 struct drm_crtc
*drmcrtc
= &drmdpy
->saved_crtcs
[crtc_idx
];
332 crtc_id
= drmcrtc
->crtc
->crtc_id
;
338 * Choose the CRTC once. It could be more dynamic, but let's keep it
341 crtc_id
= drm_display_choose_crtc(&drmdpy
->base
,
342 connectors
, num_connectors
);
344 /* save the original CRTC status */
345 drmcrtc
->crtc
= drmModeGetCrtc(drmdpy
->fd
, crtc_id
);
349 for (i
= 0; i
< drmdpy
->num_connectors
; i
++) {
350 struct drm_connector
*drmconn
= &drmdpy
->connectors
[i
];
351 drmModeConnectorPtr connector
= drmconn
->connector
;
352 drmModeEncoderPtr encoder
;
354 encoder
= drmModeGetEncoder(drmdpy
->fd
, connector
->encoder_id
);
356 if (encoder
->crtc_id
== crtc_id
) {
357 drmcrtc
->connectors
[count
++] = connector
->connector_id
;
358 if (count
>= Elements(drmcrtc
->connectors
))
361 drmModeFreeEncoder(encoder
);
365 drmcrtc
->num_connectors
= count
;
368 err
= drmModeSetCrtc(drmdpy
->fd
, crtc_id
, buffer_id
, x
, y
,
369 connectors
, num_connectors
, mode
);
371 drmModeFreeCrtc(drmcrtc
->crtc
);
372 drmcrtc
->crtc
= NULL
;
373 drmcrtc
->num_connectors
= 0;
382 drm_display_program(struct native_display
*ndpy
, int crtc_idx
,
383 struct native_surface
*nsurf
, uint x
, uint y
,
384 const struct native_connector
**nconns
, int num_nconns
,
385 const struct native_mode
*nmode
)
387 struct drm_display
*drmdpy
= drm_display(ndpy
);
388 struct drm_surface
*drmsurf
= drm_surface(nsurf
);
389 const struct drm_mode
*drmmode
= drm_mode(nmode
);
390 uint32_t connector_ids
[32];
392 drmModeModeInfo mode_tmp
, *mode
;
395 if (num_nconns
> Elements(connector_ids
)) {
396 _eglLog(_EGL_WARNING
, "too many connectors (%d)", num_nconns
);
397 num_nconns
= Elements(connector_ids
);
401 if (!drm_surface_init_framebuffers(&drmsurf
->base
, FALSE
))
404 buffer_id
= drmsurf
->front_fb
.buffer_id
;
405 /* the mode argument of drmModeSetCrtc is not constified */
406 mode_tmp
= drmmode
->mode
;
410 /* disable the CRTC */
416 for (i
= 0; i
< num_nconns
; i
++) {
417 struct drm_connector
*drmconn
= drm_connector(nconns
[i
]);
418 connector_ids
[i
] = drmconn
->connector
->connector_id
;
421 if (!drm_display_set_crtc(&drmdpy
->base
, crtc_idx
, buffer_id
, x
, y
,
422 connector_ids
, num_nconns
, mode
)) {
423 _eglLog(_EGL_WARNING
, "failed to set CRTC %d", crtc_idx
);
428 if (drmdpy
->shown_surfaces
[crtc_idx
])
429 drmdpy
->shown_surfaces
[crtc_idx
]->is_shown
= FALSE
;
430 drmdpy
->shown_surfaces
[crtc_idx
] = drmsurf
;
432 /* remember the settings for buffer swapping */
434 uint32_t crtc_id
= drmdpy
->saved_crtcs
[crtc_idx
].crtc
->crtc_id
;
435 struct drm_crtc
*drmcrtc
= &drmsurf
->current_crtc
;
438 drmModeFreeCrtc(drmcrtc
->crtc
);
439 drmcrtc
->crtc
= drmModeGetCrtc(drmdpy
->fd
, crtc_id
);
441 assert(num_nconns
< Elements(drmcrtc
->connectors
));
442 memcpy(drmcrtc
->connectors
, connector_ids
,
443 sizeof(*connector_ids
) * num_nconns
);
444 drmcrtc
->num_connectors
= num_nconns
;
446 drmsurf
->is_shown
= TRUE
;
452 static const struct native_mode
**
453 drm_display_get_modes(struct native_display
*ndpy
,
454 const struct native_connector
*nconn
,
457 struct drm_display
*drmdpy
= drm_display(ndpy
);
458 struct drm_connector
*drmconn
= drm_connector(nconn
);
459 const struct native_mode
**nmodes_return
;
462 /* delete old data */
463 if (drmconn
->connector
) {
464 drmModeFreeConnector(drmconn
->connector
);
465 FREE(drmconn
->drm_modes
);
467 drmconn
->connector
= NULL
;
468 drmconn
->drm_modes
= NULL
;
469 drmconn
->num_modes
= 0;
473 drmconn
->connector
= drmModeGetConnector(drmdpy
->fd
, drmconn
->connector_id
);
474 if (!drmconn
->connector
)
477 count
= drmconn
->connector
->count_modes
;
478 drmconn
->drm_modes
= CALLOC(count
, sizeof(*drmconn
->drm_modes
));
479 if (!drmconn
->drm_modes
) {
480 drmModeFreeConnector(drmconn
->connector
);
481 drmconn
->connector
= NULL
;
486 for (i
= 0; i
< count
; i
++) {
487 struct drm_mode
*drmmode
= &drmconn
->drm_modes
[i
];
488 drmModeModeInfoPtr mode
= &drmconn
->connector
->modes
[i
];
490 drmmode
->mode
= *mode
;
492 drmmode
->base
.desc
= drmmode
->mode
.name
;
493 drmmode
->base
.width
= drmmode
->mode
.hdisplay
;
494 drmmode
->base
.height
= drmmode
->mode
.vdisplay
;
495 drmmode
->base
.refresh_rate
= drmmode
->mode
.vrefresh
;
496 /* not all kernels have vrefresh = refresh_rate * 1000 */
497 if (drmmode
->base
.refresh_rate
< 1000)
498 drmmode
->base
.refresh_rate
*= 1000;
501 nmodes_return
= MALLOC(count
* sizeof(*nmodes_return
));
503 for (i
= 0; i
< count
; i
++)
504 nmodes_return
[i
] = &drmconn
->drm_modes
[i
].base
;
509 return nmodes_return
;
512 static const struct native_connector
**
513 drm_display_get_connectors(struct native_display
*ndpy
, int *num_connectors
,
516 struct drm_display
*drmdpy
= drm_display(ndpy
);
517 const struct native_connector
**connectors
;
520 if (!drmdpy
->connectors
) {
522 CALLOC(drmdpy
->resources
->count_connectors
, sizeof(*drmdpy
->connectors
));
523 if (!drmdpy
->connectors
)
526 for (i
= 0; i
< drmdpy
->resources
->count_connectors
; i
++) {
527 struct drm_connector
*drmconn
= &drmdpy
->connectors
[i
];
529 drmconn
->connector_id
= drmdpy
->resources
->connectors
[i
];
530 /* drmconn->connector is allocated when the modes are asked */
533 drmdpy
->num_connectors
= drmdpy
->resources
->count_connectors
;
536 connectors
= MALLOC(drmdpy
->num_connectors
* sizeof(*connectors
));
538 for (i
= 0; i
< drmdpy
->num_connectors
; i
++)
539 connectors
[i
] = &drmdpy
->connectors
[i
].base
;
541 *num_connectors
= drmdpy
->num_connectors
;
545 *num_crtc
= drmdpy
->resources
->count_crtcs
;
550 static struct native_surface
*
551 drm_display_create_scanout_surface(struct native_display
*ndpy
,
552 const struct native_config
*nconf
,
553 uint width
, uint height
)
555 struct drm_surface
*drmsurf
;
557 drmsurf
= drm_display_create_surface(ndpy
, nconf
, width
, height
);
558 return &drmsurf
->base
;
561 static struct native_display_modeset drm_display_modeset
= {
562 .get_connectors
= drm_display_get_connectors
,
563 .get_modes
= drm_display_get_modes
,
564 .create_scanout_surface
= drm_display_create_scanout_surface
,
565 .program
= drm_display_program
569 drm_display_fini_modeset(struct native_display
*ndpy
)
571 struct drm_display
*drmdpy
= drm_display(ndpy
);
574 if (drmdpy
->connectors
) {
575 for (i
= 0; i
< drmdpy
->num_connectors
; i
++) {
576 struct drm_connector
*drmconn
= &drmdpy
->connectors
[i
];
577 if (drmconn
->connector
) {
578 drmModeFreeConnector(drmconn
->connector
);
579 FREE(drmconn
->drm_modes
);
582 FREE(drmdpy
->connectors
);
585 if (drmdpy
->shown_surfaces
) {
586 FREE(drmdpy
->shown_surfaces
);
587 drmdpy
->shown_surfaces
= NULL
;
590 if (drmdpy
->saved_crtcs
) {
591 for (i
= 0; i
< drmdpy
->resources
->count_crtcs
; i
++) {
592 struct drm_crtc
*drmcrtc
= &drmdpy
->saved_crtcs
[i
];
596 drmModeSetCrtc(drmdpy
->fd
, drmcrtc
->crtc
->crtc_id
,
597 drmcrtc
->crtc
->buffer_id
, drmcrtc
->crtc
->x
, drmcrtc
->crtc
->y
,
598 drmcrtc
->connectors
, drmcrtc
->num_connectors
,
599 &drmcrtc
->crtc
->mode
);
601 drmModeFreeCrtc(drmcrtc
->crtc
);
604 FREE(drmdpy
->saved_crtcs
);
607 if (drmdpy
->resources
) {
608 drmModeFreeResources(drmdpy
->resources
);
609 drmdpy
->resources
= NULL
;
612 drmdpy
->base
.modeset
= NULL
;
616 drm_display_init_modeset(struct native_display
*ndpy
)
618 struct drm_display
*drmdpy
= drm_display(ndpy
);
620 /* resources are fixed, unlike crtc, connector, or encoder */
621 drmdpy
->resources
= drmModeGetResources(drmdpy
->fd
);
622 if (!drmdpy
->resources
) {
623 _eglLog(_EGL_DEBUG
, "Failed to get KMS resources. Disable modeset.");
627 drmdpy
->saved_crtcs
=
628 CALLOC(drmdpy
->resources
->count_crtcs
, sizeof(*drmdpy
->saved_crtcs
));
629 if (!drmdpy
->saved_crtcs
) {
630 drm_display_fini_modeset(&drmdpy
->base
);
634 drmdpy
->shown_surfaces
=
635 CALLOC(drmdpy
->resources
->count_crtcs
, sizeof(*drmdpy
->shown_surfaces
));
636 if (!drmdpy
->shown_surfaces
) {
637 drm_display_fini_modeset(&drmdpy
->base
);
641 drmdpy
->base
.modeset
= &drm_display_modeset
;