| 1 |
/* |
| 2 |
* Copyright © 2011 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, |
| 16 |
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF |
| 17 |
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND |
| 18 |
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT |
| 19 |
* HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, |
| 20 |
* WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 21 |
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER |
| 22 |
* DEALINGS IN THE SOFTWARE. |
| 23 |
* |
| 24 |
* Authors: |
| 25 |
* Kristian Høgsberg <[email protected]> |
| 26 |
*/ |
| 27 |
|
| 28 |
#include <dlfcn.h> |
| 29 |
#include <fcntl.h> |
| 30 |
#include <stdint.h> |
| 31 |
#include <stdio.h> |
| 32 |
#include <stdlib.h> |
| 33 |
#include <string.h> |
| 34 |
#include <unistd.h> |
| 35 |
#include <xf86drm.h> |
| 36 |
#include <sys/stat.h> |
| 37 |
#include <sys/types.h> |
| 38 |
|
| 39 |
#include "util/os_file.h" |
| 40 |
|
| 41 |
#include "main/glconfig.h" |
| 42 |
|
| 43 |
#include "egl_dri2.h" |
| 44 |
#include "egldevice.h" |
| 45 |
#include "eglglobals.h" |
| 46 |
#include "loader.h" |
| 47 |
#include "dri_util.h" |
| 48 |
|
| 49 |
static struct gbm_bo * |
| 50 |
lock_front_buffer(struct gbm_surface *_surf) |
| 51 |
{ |
| 52 |
struct gbm_dri_surface *surf = gbm_dri_surface(_surf); |
| 53 |
struct dri2_egl_surface *dri2_surf = surf->dri_private; |
| 54 |
struct gbm_dri_device *device = gbm_dri_device(_surf->gbm); |
| 55 |
struct gbm_bo *bo; |
| 56 |
|
| 57 |
if (dri2_surf->current == NULL) { |
| 58 |
_eglError(EGL_BAD_SURFACE, "no front buffer"); |
| 59 |
return NULL; |
| 60 |
} |
| 61 |
|
| 62 |
bo = dri2_surf->current->bo; |
| 63 |
|
| 64 |
if (!device->swrast) { |
| 65 |
dri2_surf->current->locked = true; |
| 66 |
dri2_surf->current = NULL; |
| 67 |
} |
| 68 |
|
| 69 |
return bo; |
| 70 |
} |
| 71 |
|
| 72 |
static void |
| 73 |
release_buffer(struct gbm_surface *_surf, struct gbm_bo *bo) |
| 74 |
{ |
| 75 |
struct gbm_dri_surface *surf = gbm_dri_surface(_surf); |
| 76 |
struct dri2_egl_surface *dri2_surf = surf->dri_private; |
| 77 |
|
| 78 |
for (unsigned i = 0; i < ARRAY_SIZE(dri2_surf->color_buffers); i++) { |
| 79 |
if (dri2_surf->color_buffers[i].bo == bo) { |
| 80 |
dri2_surf->color_buffers[i].locked = false; |
| 81 |
break; |
| 82 |
} |
| 83 |
} |
| 84 |
} |
| 85 |
|
| 86 |
static int |
| 87 |
has_free_buffers(struct gbm_surface *_surf) |
| 88 |
{ |
| 89 |
struct gbm_dri_surface *surf = gbm_dri_surface(_surf); |
| 90 |
struct dri2_egl_surface *dri2_surf = surf->dri_private; |
| 91 |
|
| 92 |
for (unsigned i = 0; i < ARRAY_SIZE(dri2_surf->color_buffers); i++) |
| 93 |
if (!dri2_surf->color_buffers[i].locked) |
| 94 |
return 1; |
| 95 |
|
| 96 |
return 0; |
| 97 |
} |
| 98 |
|
| 99 |
static bool |
| 100 |
dri2_drm_config_is_compatible(struct dri2_egl_display *dri2_dpy, |
| 101 |
const struct dri_config *config, |
| 102 |
struct gbm_surface *surface) |
| 103 |
{ |
| 104 |
const struct gl_config *gl_config = (struct gl_config *) config; |
| 105 |
const struct gbm_dri_visual *visual = NULL; |
| 106 |
int i; |
| 107 |
|
| 108 |
/* Check that the EGLConfig being used to render to the surface is |
| 109 |
* compatible with the surface format. Since mixing ARGB and XRGB of |
| 110 |
* otherwise-compatible formats is relatively common, explicitly allow |
| 111 |
* this. |
| 112 |
*/ |
| 113 |
for (i = 0; i < dri2_dpy->gbm_dri->num_visuals; i++) { |
| 114 |
visual = &dri2_dpy->gbm_dri->visual_table[i]; |
| 115 |
if (visual->gbm_format == surface->v0.format) |
| 116 |
break; |
| 117 |
} |
| 118 |
|
| 119 |
if (i == dri2_dpy->gbm_dri->num_visuals) |
| 120 |
return false; |
| 121 |
|
| 122 |
|
| 123 |
const struct util_format_description *fmt_c = |
| 124 |
util_format_description(gl_config->color_format); |
| 125 |
const struct util_format_description *fmt_s = |
| 126 |
util_format_description(visual->pipe_format); |
| 127 |
|
| 128 |
if (util_is_format_compatible(fmt_c, fmt_s) || |
| 129 |
util_is_format_compatible(fmt_s, fmt_c)) |
| 130 |
return true; |
| 131 |
|
| 132 |
return false; |
| 133 |
} |
| 134 |
|
| 135 |
static _EGLSurface * |
| 136 |
dri2_drm_create_window_surface(_EGLDisplay *disp, _EGLConfig *conf, |
| 137 |
void *native_surface, const EGLint *attrib_list) |
| 138 |
{ |
| 139 |
struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp); |
| 140 |
struct dri2_egl_config *dri2_conf = dri2_egl_config(conf); |
| 141 |
struct dri2_egl_surface *dri2_surf; |
| 142 |
struct gbm_surface *surface = native_surface; |
| 143 |
struct gbm_dri_surface *surf; |
| 144 |
const struct dri_config *config; |
| 145 |
|
| 146 |
dri2_surf = calloc(1, sizeof *dri2_surf); |
| 147 |
if (!dri2_surf) { |
| 148 |
_eglError(EGL_BAD_ALLOC, "dri2_create_surface"); |
| 149 |
return NULL; |
| 150 |
} |
| 151 |
|
| 152 |
if (!dri2_init_surface(&dri2_surf->base, disp, EGL_WINDOW_BIT, conf, |
| 153 |
attrib_list, false, native_surface)) |
| 154 |
goto cleanup_surf; |
| 155 |
|
| 156 |
config = dri2_get_dri_config(dri2_conf, EGL_WINDOW_BIT, |
| 157 |
dri2_surf->base.GLColorspace); |
| 158 |
|
| 159 |
if (!config) { |
| 160 |
_eglError(EGL_BAD_MATCH, |
| 161 |
"Unsupported surfacetype/colorspace configuration"); |
| 162 |
goto cleanup_surf; |
| 163 |
} |
| 164 |
|
| 165 |
if (!dri2_drm_config_is_compatible(dri2_dpy, config, surface)) { |
| 166 |
_eglError(EGL_BAD_MATCH, "EGL config not compatible with GBM format"); |
| 167 |
goto cleanup_surf; |
| 168 |
} |
| 169 |
|
| 170 |
surf = gbm_dri_surface(surface); |
| 171 |
dri2_surf->gbm_surf = surf; |
| 172 |
dri2_surf->base.Width = surf->base.v0.width; |
| 173 |
dri2_surf->base.Height = surf->base.v0.height; |
| 174 |
surf->dri_private = dri2_surf; |
| 175 |
|
| 176 |
if (!dri2_create_drawable(dri2_dpy, config, dri2_surf, dri2_surf->gbm_surf)) |
| 177 |
goto cleanup_surf; |
| 178 |
|
| 179 |
return &dri2_surf->base; |
| 180 |
|
| 181 |
cleanup_surf: |
| 182 |
free(dri2_surf); |
| 183 |
|
| 184 |
return NULL; |
| 185 |
} |
| 186 |
|
| 187 |
static _EGLSurface * |
| 188 |
dri2_drm_create_pixmap_surface(_EGLDisplay *disp, _EGLConfig *conf, |
| 189 |
void *native_window, const EGLint *attrib_list) |
| 190 |
{ |
| 191 |
/* From the EGL_MESA_platform_gbm spec, version 5: |
| 192 |
* |
| 193 |
* It is not valid to call eglCreatePlatformPixmapSurfaceEXT with a <dpy> |
| 194 |
* that belongs to the GBM platform. Any such call fails and generates |
| 195 |
* EGL_BAD_PARAMETER. |
| 196 |
*/ |
| 197 |
_eglError(EGL_BAD_PARAMETER, "cannot create EGL pixmap surfaces on GBM"); |
| 198 |
return NULL; |
| 199 |
} |
| 200 |
|
| 201 |
static EGLBoolean |
| 202 |
dri2_drm_destroy_surface(_EGLDisplay *disp, _EGLSurface *surf) |
| 203 |
{ |
| 204 |
struct dri2_egl_surface *dri2_surf = dri2_egl_surface(surf); |
| 205 |
|
| 206 |
driDestroyDrawable(dri2_surf->dri_drawable); |
| 207 |
|
| 208 |
for (unsigned i = 0; i < ARRAY_SIZE(dri2_surf->color_buffers); i++) { |
| 209 |
if (dri2_surf->color_buffers[i].bo) |
| 210 |
gbm_bo_destroy(dri2_surf->color_buffers[i].bo); |
| 211 |
} |
| 212 |
|
| 213 |
dri2_fini_surface(surf); |
| 214 |
free(surf); |
| 215 |
|
| 216 |
return EGL_TRUE; |
| 217 |
} |
| 218 |
|
| 219 |
static void |
| 220 |
destroy_oldest_unused_bo(struct dri2_egl_surface *dri2_surf) |
| 221 |
{ |
| 222 |
int max_age = 0; |
| 223 |
struct dri2_egl_buffer *oldest_buffer = NULL; |
| 224 |
|
| 225 |
for (unsigned i = 0; i < ARRAY_SIZE(dri2_surf->color_buffers); i++) { |
| 226 |
if (dri2_surf->color_buffers[i].locked || |
| 227 |
dri2_surf->back == &dri2_surf->color_buffers[i] || |
| 228 |
dri2_surf->current == &dri2_surf->color_buffers[i]) |
| 229 |
continue; |
| 230 |
|
| 231 |
if (!max_age || dri2_surf->color_buffers[i].age > max_age) { |
| 232 |
oldest_buffer = &dri2_surf->color_buffers[i]; |
| 233 |
max_age = dri2_surf->color_buffers[i].age; |
| 234 |
} |
| 235 |
} |
| 236 |
|
| 237 |
gbm_bo_destroy(oldest_buffer->bo); |
| 238 |
oldest_buffer->bo = NULL; |
| 239 |
oldest_buffer->age = 0; |
| 240 |
|
| 241 |
dri2_surf->excess_bo_frames = 0; |
| 242 |
} |
| 243 |
|
| 244 |
static int |
| 245 |
get_back_bo(struct dri2_egl_surface *dri2_surf) |
| 246 |
{ |
| 247 |
struct dri2_egl_display *dri2_dpy = |
| 248 |
dri2_egl_display(dri2_surf->base.Resource.Display); |
| 249 |
struct gbm_dri_surface *surf = dri2_surf->gbm_surf; |
| 250 |
int min_age = 0, max_age = 0; |
| 251 |
|
| 252 |
if (dri2_surf->back == NULL) { |
| 253 |
for (unsigned i = 0; i < ARRAY_SIZE(dri2_surf->color_buffers); i++) { |
| 254 |
if (!dri2_surf->color_buffers[i].locked) { |
| 255 |
int age = dri2_surf->color_buffers[i].age; |
| 256 |
|
| 257 |
if (!min_age || age < min_age) |
| 258 |
min_age = age; |
| 259 |
|
| 260 |
if (!max_age || age > max_age) { |
| 261 |
dri2_surf->back = &dri2_surf->color_buffers[i]; |
| 262 |
max_age = age; |
| 263 |
} |
| 264 |
} |
| 265 |
} |
| 266 |
|
| 267 |
if (min_age && min_age < max_age) { |
| 268 |
if (++dri2_surf->excess_bo_frames == 1000) |
| 269 |
destroy_oldest_unused_bo(dri2_surf); |
| 270 |
} else { |
| 271 |
dri2_surf->excess_bo_frames = 0; |
| 272 |
} |
| 273 |
} |
| 274 |
|
| 275 |
if (dri2_surf->back == NULL) |
| 276 |
return -1; |
| 277 |
if (dri2_surf->back->bo == NULL) { |
| 278 |
if (surf->base.v0.modifiers && surf->base.v0.flags) |
| 279 |
dri2_surf->back->bo = gbm_bo_create_with_modifiers2( |
| 280 |
&dri2_dpy->gbm_dri->base, surf->base.v0.width, surf->base.v0.height, |
| 281 |
surf->base.v0.format, surf->base.v0.modifiers, surf->base.v0.count, |
| 282 |
surf->base.v0.flags); |
| 283 |
else if (surf->base.v0.modifiers) |
| 284 |
dri2_surf->back->bo = gbm_bo_create_with_modifiers( |
| 285 |
&dri2_dpy->gbm_dri->base, surf->base.v0.width, surf->base.v0.height, |
| 286 |
surf->base.v0.format, surf->base.v0.modifiers, surf->base.v0.count); |
| 287 |
else { |
| 288 |
unsigned flags = surf->base.v0.flags; |
| 289 |
if (dri2_surf->base.ProtectedContent) |
| 290 |
flags |= GBM_BO_USE_PROTECTED; |
| 291 |
dri2_surf->back->bo = |
| 292 |
gbm_bo_create(&dri2_dpy->gbm_dri->base, surf->base.v0.width, |
| 293 |
surf->base.v0.height, surf->base.v0.format, flags); |
| 294 |
} |
| 295 |
} |
| 296 |
if (dri2_surf->back->bo == NULL) |
| 297 |
return -1; |
| 298 |
|
| 299 |
return 0; |
| 300 |
} |
| 301 |
|
| 302 |
static int |
| 303 |
get_swrast_front_bo(struct dri2_egl_surface *dri2_surf) |
| 304 |
{ |
| 305 |
struct dri2_egl_display *dri2_dpy = |
| 306 |
dri2_egl_display(dri2_surf->base.Resource.Display); |
| 307 |
struct gbm_dri_surface *surf = dri2_surf->gbm_surf; |
| 308 |
|
| 309 |
if (dri2_surf->current == NULL) { |
| 310 |
assert(!dri2_surf->color_buffers[0].locked); |
| 311 |
dri2_surf->current = &dri2_surf->color_buffers[0]; |
| 312 |
} |
| 313 |
|
| 314 |
if (dri2_surf->current->bo == NULL) |
| 315 |
dri2_surf->current->bo = gbm_bo_create( |
| 316 |
&dri2_dpy->gbm_dri->base, surf->base.v0.width, surf->base.v0.height, |
| 317 |
surf->base.v0.format, surf->base.v0.flags); |
| 318 |
if (dri2_surf->current->bo == NULL) |
| 319 |
return -1; |
| 320 |
|
| 321 |
return 0; |
| 322 |
} |
| 323 |
|
| 324 |
static int |
| 325 |
dri2_drm_image_get_buffers(struct dri_drawable *driDrawable, unsigned int format, |
| 326 |
uint32_t *stamp, void *loaderPrivate, |
| 327 |
uint32_t buffer_mask, struct __DRIimageList *buffers) |
| 328 |
{ |
| 329 |
struct dri2_egl_surface *dri2_surf = loaderPrivate; |
| 330 |
struct gbm_dri_bo *bo; |
| 331 |
|
| 332 |
if (get_back_bo(dri2_surf) < 0) |
| 333 |
return 0; |
| 334 |
|
| 335 |
bo = gbm_dri_bo(dri2_surf->back->bo); |
| 336 |
buffers->image_mask = __DRI_IMAGE_BUFFER_BACK; |
| 337 |
buffers->back = bo->image; |
| 338 |
|
| 339 |
return 1; |
| 340 |
} |
| 341 |
|
| 342 |
static void |
| 343 |
dri2_drm_flush_front_buffer(struct dri_drawable *driDrawable, void *loaderPrivate) |
| 344 |
{ |
| 345 |
(void)driDrawable; |
| 346 |
(void)loaderPrivate; |
| 347 |
} |
| 348 |
|
| 349 |
static EGLBoolean |
| 350 |
dri2_drm_swap_buffers(_EGLDisplay *disp, _EGLSurface *draw) |
| 351 |
{ |
| 352 |
struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp); |
| 353 |
struct dri2_egl_surface *dri2_surf = dri2_egl_surface(draw); |
| 354 |
|
| 355 |
if (dri2_dpy->swrast_not_kms) { |
| 356 |
driSwapBuffers(dri2_surf->dri_drawable); |
| 357 |
return EGL_TRUE; |
| 358 |
} |
| 359 |
|
| 360 |
if (dri2_surf->current) |
| 361 |
_eglError(EGL_BAD_SURFACE, "dri2_swap_buffers"); |
| 362 |
for (unsigned i = 0; i < ARRAY_SIZE(dri2_surf->color_buffers); i++) |
| 363 |
if (dri2_surf->color_buffers[i].age > 0) |
| 364 |
dri2_surf->color_buffers[i].age++; |
| 365 |
|
| 366 |
/* Flushing must be done before get_back_bo to make sure glthread's |
| 367 |
* unmarshalling thread is idle otherwise it might concurrently |
| 368 |
* call get_back_bo (eg: through dri2_drm_image_get_buffers). |
| 369 |
*/ |
| 370 |
dri2_flush_drawable_for_swapbuffers(disp, draw); |
| 371 |
dri_invalidate_drawable(dri2_surf->dri_drawable); |
| 372 |
|
| 373 |
/* Make sure we have a back buffer in case we're swapping without |
| 374 |
* ever rendering. */ |
| 375 |
if (get_back_bo(dri2_surf) < 0) |
| 376 |
return _eglError(EGL_BAD_ALLOC, "dri2_swap_buffers"); |
| 377 |
|
| 378 |
dri2_surf->current = dri2_surf->back; |
| 379 |
dri2_surf->current->age = 1; |
| 380 |
dri2_surf->back = NULL; |
| 381 |
|
| 382 |
return EGL_TRUE; |
| 383 |
} |
| 384 |
|
| 385 |
static EGLint |
| 386 |
dri2_drm_query_buffer_age(_EGLDisplay *disp, _EGLSurface *surface) |
| 387 |
{ |
| 388 |
struct dri2_egl_surface *dri2_surf = dri2_egl_surface(surface); |
| 389 |
|
| 390 |
if (get_back_bo(dri2_surf) < 0) { |
| 391 |
_eglError(EGL_BAD_ALLOC, "dri2_query_buffer_age"); |
| 392 |
return -1; |
| 393 |
} |
| 394 |
|
| 395 |
return dri2_surf->back->age; |
| 396 |
} |
| 397 |
|
| 398 |
static _EGLImage * |
| 399 |
dri2_drm_create_image_khr_pixmap(_EGLDisplay *disp, _EGLContext *ctx, |
| 400 |
EGLClientBuffer buffer, |
| 401 |
const EGLint *attr_list) |
| 402 |
{ |
| 403 |
struct gbm_dri_bo *dri_bo = gbm_dri_bo((struct gbm_bo *)buffer); |
| 404 |
struct dri2_egl_image *dri2_img; |
| 405 |
|
| 406 |
dri2_img = malloc(sizeof *dri2_img); |
| 407 |
if (!dri2_img) { |
| 408 |
_eglError(EGL_BAD_ALLOC, "dri2_create_image_khr_pixmap"); |
| 409 |
return NULL; |
| 410 |
} |
| 411 |
|
| 412 |
_eglInitImage(&dri2_img->base, disp); |
| 413 |
|
| 414 |
dri2_img->dri_image = dri2_dup_image(dri_bo->image, dri2_img); |
| 415 |
if (dri2_img->dri_image == NULL) { |
| 416 |
free(dri2_img); |
| 417 |
_eglError(EGL_BAD_ALLOC, "dri2_create_image_khr_pixmap"); |
| 418 |
return NULL; |
| 419 |
} |
| 420 |
|
| 421 |
return &dri2_img->base; |
| 422 |
} |
| 423 |
|
| 424 |
static _EGLImage * |
| 425 |
dri2_drm_create_image_khr(_EGLDisplay *disp, _EGLContext *ctx, EGLenum target, |
| 426 |
EGLClientBuffer buffer, const EGLint *attr_list) |
| 427 |
{ |
| 428 |
switch (target) { |
| 429 |
case EGL_NATIVE_PIXMAP_KHR: |
| 430 |
return dri2_drm_create_image_khr_pixmap(disp, ctx, buffer, attr_list); |
| 431 |
default: |
| 432 |
return dri2_create_image_khr(disp, ctx, target, buffer, attr_list); |
| 433 |
} |
| 434 |
} |
| 435 |
|
| 436 |
static int |
| 437 |
dri2_drm_authenticate(_EGLDisplay *disp, uint32_t id) |
| 438 |
{ |
| 439 |
struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp); |
| 440 |
|
| 441 |
return drmAuthMagic(dri2_dpy->fd_render_gpu, id); |
| 442 |
} |
| 443 |
|
| 444 |
static void |
| 445 |
swrast_put_image2(struct dri_drawable *driDrawable, int op, int x, int y, int width, |
| 446 |
int height, int stride, char *data, void *loaderPrivate) |
| 447 |
{ |
| 448 |
struct dri2_egl_surface *dri2_surf = loaderPrivate; |
| 449 |
int internal_stride; |
| 450 |
struct gbm_dri_bo *bo; |
| 451 |
uint32_t bpp; |
| 452 |
int x_bytes, width_bytes; |
| 453 |
char *src, *dst; |
| 454 |
|
| 455 |
if (op != __DRI_SWRAST_IMAGE_OP_DRAW && op != __DRI_SWRAST_IMAGE_OP_SWAP) |
| 456 |
return; |
| 457 |
|
| 458 |
if (get_swrast_front_bo(dri2_surf) < 0) |
| 459 |
return; |
| 460 |
|
| 461 |
bo = gbm_dri_bo(dri2_surf->current->bo); |
| 462 |
|
| 463 |
bpp = gbm_bo_get_bpp(&bo->base); |
| 464 |
if (bpp == 0) |
| 465 |
return; |
| 466 |
|
| 467 |
x_bytes = x * (bpp >> 3); |
| 468 |
width_bytes = width * (bpp >> 3); |
| 469 |
|
| 470 |
if (gbm_dri_bo_map_dumb(bo) == NULL) |
| 471 |
return; |
| 472 |
|
| 473 |
internal_stride = bo->base.v0.stride; |
| 474 |
|
| 475 |
dst = bo->map + x_bytes + (y * internal_stride); |
| 476 |
src = data; |
| 477 |
|
| 478 |
for (int i = 0; i < height; i++) { |
| 479 |
memcpy(dst, src, width_bytes); |
| 480 |
dst += internal_stride; |
| 481 |
src += stride; |
| 482 |
} |
| 483 |
|
| 484 |
gbm_dri_bo_unmap_dumb(bo); |
| 485 |
} |
| 486 |
|
| 487 |
static void |
| 488 |
swrast_get_image(struct dri_drawable *driDrawable, int x, int y, int width, |
| 489 |
int height, char *data, void *loaderPrivate) |
| 490 |
{ |
| 491 |
struct dri2_egl_surface *dri2_surf = loaderPrivate; |
| 492 |
int internal_stride, stride; |
| 493 |
struct gbm_dri_bo *bo; |
| 494 |
uint32_t bpp; |
| 495 |
int x_bytes, width_bytes; |
| 496 |
char *src, *dst; |
| 497 |
|
| 498 |
if (get_swrast_front_bo(dri2_surf) < 0) |
| 499 |
return; |
| 500 |
|
| 501 |
bo = gbm_dri_bo(dri2_surf->current->bo); |
| 502 |
|
| 503 |
bpp = gbm_bo_get_bpp(&bo->base); |
| 504 |
if (bpp == 0) |
| 505 |
return; |
| 506 |
|
| 507 |
x_bytes = x * (bpp >> 3); |
| 508 |
width_bytes = width * (bpp >> 3); |
| 509 |
|
| 510 |
internal_stride = bo->base.v0.stride; |
| 511 |
stride = width_bytes; |
| 512 |
|
| 513 |
if (gbm_dri_bo_map_dumb(bo) == NULL) |
| 514 |
return; |
| 515 |
|
| 516 |
dst = data; |
| 517 |
src = bo->map + x_bytes + (y * internal_stride); |
| 518 |
|
| 519 |
for (int i = 0; i < height; i++) { |
| 520 |
memcpy(dst, src, width_bytes); |
| 521 |
dst += stride; |
| 522 |
src += internal_stride; |
| 523 |
} |
| 524 |
|
| 525 |
gbm_dri_bo_unmap_dumb(bo); |
| 526 |
} |
| 527 |
|
| 528 |
static void |
| 529 |
drm_add_configs_for_visuals(_EGLDisplay *disp) |
| 530 |
{ |
| 531 |
struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp); |
| 532 |
const struct gbm_dri_visual *visuals = dri2_dpy->gbm_dri->visual_table; |
| 533 |
int num_visuals = dri2_dpy->gbm_dri->num_visuals; |
| 534 |
unsigned int format_count[num_visuals]; |
| 535 |
|
| 536 |
memset(format_count, 0, num_visuals * sizeof(unsigned int)); |
| 537 |
|
| 538 |
for (unsigned i = 0; dri2_dpy->driver_configs[i]; i++) { |
| 539 |
const struct dri_config *config = dri2_dpy->driver_configs[i]; |
| 540 |
struct gl_config *gl_config = (struct gl_config *) config; |
| 541 |
|
| 542 |
for (unsigned j = 0; j < num_visuals; j++) { |
| 543 |
struct dri2_egl_config *dri2_conf; |
| 544 |
EGLint config_group = 0; |
| 545 |
|
| 546 |
if (visuals[j].pipe_format != gl_config->color_format) |
| 547 |
continue; |
| 548 |
|
| 549 |
/* Put the 16 bpc rgb[a] unorm formats into a lower priority EGL config |
| 550 |
* group 1, so they don't get preferably chosen by eglChooseConfig(). |
| 551 |
*/ |
| 552 |
if (util_format_is_unorm16(util_format_description(visuals[j].pipe_format))) |
| 553 |
config_group = 1; |
| 554 |
|
| 555 |
const EGLint attr_list[] = { |
| 556 |
EGL_NATIVE_VISUAL_ID, |
| 557 |
visuals[j].gbm_format, |
| 558 |
EGL_CONFIG_SELECT_GROUP_EXT, |
| 559 |
config_group, |
| 560 |
EGL_NONE, |
| 561 |
}; |
| 562 |
|
| 563 |
dri2_conf = |
| 564 |
dri2_add_config(disp, dri2_dpy->driver_configs[i], EGL_WINDOW_BIT, |
| 565 |
attr_list); |
| 566 |
if (dri2_conf) |
| 567 |
format_count[j]++; |
| 568 |
} |
| 569 |
} |
| 570 |
|
| 571 |
for (unsigned i = 0; i < ARRAY_SIZE(format_count); i++) { |
| 572 |
if (!format_count[i]) { |
| 573 |
struct gbm_format_name_desc desc; |
| 574 |
_eglLog(_EGL_DEBUG, "No DRI config supports native format %s", |
| 575 |
gbm_format_get_name(visuals[i].gbm_format, &desc)); |
| 576 |
} |
| 577 |
} |
| 578 |
} |
| 579 |
|
| 580 |
static const struct dri2_egl_display_vtbl dri2_drm_display_vtbl = { |
| 581 |
.authenticate = dri2_drm_authenticate, |
| 582 |
.create_window_surface = dri2_drm_create_window_surface, |
| 583 |
.create_pixmap_surface = dri2_drm_create_pixmap_surface, |
| 584 |
.destroy_surface = dri2_drm_destroy_surface, |
| 585 |
.create_image = dri2_drm_create_image_khr, |
| 586 |
.swap_buffers = dri2_drm_swap_buffers, |
| 587 |
.query_buffer_age = dri2_drm_query_buffer_age, |
| 588 |
.get_dri_drawable = dri2_surface_get_dri_drawable, |
| 589 |
}; |
| 590 |
|
| 591 |
static int |
| 592 |
get_fd_render_gpu_drm(struct gbm_dri_device *gbm_dri, int fd_display_gpu) |
| 593 |
{ |
| 594 |
/* This doesn't make sense for the software case. */ |
| 595 |
assert(!gbm_dri->software); |
| 596 |
|
| 597 |
/* Render-capable device, so just return the same fd. */ |
| 598 |
if (loader_is_device_render_capable(fd_display_gpu)) |
| 599 |
return fd_display_gpu; |
| 600 |
|
| 601 |
/* Display-only device, so return a compatible render-only device. */ |
| 602 |
return dri_query_compatible_render_only_device_fd(fd_display_gpu); |
| 603 |
} |
| 604 |
|
| 605 |
EGLBoolean |
| 606 |
dri2_initialize_drm(_EGLDisplay *disp) |
| 607 |
{ |
| 608 |
struct gbm_device *gbm; |
| 609 |
const char *err; |
| 610 |
struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp); |
| 611 |
|
| 612 |
gbm = disp->PlatformDisplay; |
| 613 |
if (gbm == NULL) { |
| 614 |
if (disp->Device) { |
| 615 |
drmDevicePtr drm = _eglDeviceDrm(disp->Device); |
| 616 |
|
| 617 |
if (!_eglDeviceSupports(disp->Device, _EGL_DEVICE_DRM)) { |
| 618 |
err = "DRI2: Device isn't of _EGL_DEVICE_DRM type"; |
| 619 |
goto cleanup; |
| 620 |
} |
| 621 |
|
| 622 |
if (!(drm->available_nodes & (1 << DRM_NODE_PRIMARY))) { |
| 623 |
err = "DRI2: Device does not have DRM_NODE_PRIMARY node"; |
| 624 |
goto cleanup; |
| 625 |
} |
| 626 |
|
| 627 |
dri2_dpy->fd_display_gpu = |
| 628 |
loader_open_device(drm->nodes[DRM_NODE_PRIMARY]); |
| 629 |
} else { |
| 630 |
_EGLDevice *dev_list = _eglGlobal.DeviceList; |
| 631 |
drmDevicePtr drm; |
| 632 |
while (dev_list) { |
| 633 |
if (!_eglDeviceSupports(dev_list, _EGL_DEVICE_DRM)) |
| 634 |
goto next; |
| 635 |
|
| 636 |
drm = _eglDeviceDrm(dev_list); |
| 637 |
|
| 638 |
if (!(drm->available_nodes & (1 << DRM_NODE_PRIMARY))) |
| 639 |
goto next; |
| 640 |
|
| 641 |
dri2_dpy->fd_display_gpu = |
| 642 |
loader_open_device(drm->nodes[DRM_NODE_PRIMARY]); |
| 643 |
if (dri2_dpy->fd_display_gpu < 0) |
| 644 |
goto next; |
| 645 |
|
| 646 |
break; |
| 647 |
next: |
| 648 |
dev_list = _eglDeviceNext(dev_list); |
| 649 |
} |
| 650 |
} |
| 651 |
|
| 652 |
gbm = gbm_create_device(dri2_dpy->fd_display_gpu); |
| 653 |
if (gbm == NULL) { |
| 654 |
err = "DRI2: failed to create gbm device"; |
| 655 |
goto cleanup; |
| 656 |
} |
| 657 |
dri2_dpy->own_device = true; |
| 658 |
} else { |
| 659 |
dri2_dpy->fd_display_gpu = os_dupfd_cloexec(gbm_device_get_fd(gbm)); |
| 660 |
if (dri2_dpy->fd_display_gpu < 0) { |
| 661 |
err = "DRI2: failed to fcntl() existing gbm device"; |
| 662 |
goto cleanup; |
| 663 |
} |
| 664 |
} |
| 665 |
dri2_dpy->gbm_dri = gbm_dri_device(gbm); |
| 666 |
if (!dri2_dpy->gbm_dri->software) { |
| 667 |
dri2_dpy->fd_render_gpu = |
| 668 |
get_fd_render_gpu_drm(dri2_dpy->gbm_dri, dri2_dpy->fd_display_gpu); |
| 669 |
if (dri2_dpy->fd_render_gpu < 0) { |
| 670 |
err = "DRI2: failed to get compatible render device"; |
| 671 |
goto cleanup; |
| 672 |
} |
| 673 |
} |
| 674 |
|
| 675 |
if (strcmp(gbm_device_get_backend_name(gbm), "drm") != 0) { |
| 676 |
err = "DRI2: gbm device using incorrect/incompatible backend"; |
| 677 |
goto cleanup; |
| 678 |
} |
| 679 |
|
| 680 |
dri2_dpy->driver_name = strdup(dri2_dpy->gbm_dri->driver_name); |
| 681 |
|
| 682 |
dri2_detect_swrast_kopper(disp); |
| 683 |
|
| 684 |
dri2_dpy->dri_screen_render_gpu = dri2_dpy->gbm_dri->screen; |
| 685 |
dri2_dpy->driver_configs = dri2_dpy->gbm_dri->driver_configs; |
| 686 |
|
| 687 |
dri2_dpy->gbm_dri->validate_image = dri2_validate_egl_image; |
| 688 |
dri2_dpy->gbm_dri->lookup_image_validated = dri2_lookup_egl_image_validated; |
| 689 |
dri2_dpy->gbm_dri->lookup_user_data = disp; |
| 690 |
|
| 691 |
dri2_dpy->gbm_dri->flush_front_buffer = dri2_drm_flush_front_buffer; |
| 692 |
dri2_dpy->gbm_dri->image_get_buffers = dri2_drm_image_get_buffers; |
| 693 |
dri2_dpy->gbm_dri->swrast_put_image2 = swrast_put_image2; |
| 694 |
dri2_dpy->gbm_dri->swrast_get_image = swrast_get_image; |
| 695 |
|
| 696 |
dri2_dpy->gbm_dri->base.v0.surface_lock_front_buffer = lock_front_buffer; |
| 697 |
dri2_dpy->gbm_dri->base.v0.surface_release_buffer = release_buffer; |
| 698 |
dri2_dpy->gbm_dri->base.v0.surface_has_free_buffers = has_free_buffers; |
| 699 |
|
| 700 |
if (!dri2_setup_device(disp, dri2_dpy->gbm_dri->software)) { |
| 701 |
err = "DRI2: failed to setup EGLDevice"; |
| 702 |
goto cleanup; |
| 703 |
} |
| 704 |
|
| 705 |
dri2_setup_screen(disp); |
| 706 |
|
| 707 |
drm_add_configs_for_visuals(disp); |
| 708 |
|
| 709 |
disp->Extensions.KHR_image_pixmap = EGL_TRUE; |
| 710 |
disp->Extensions.EXT_buffer_age = EGL_TRUE; |
| 711 |
|
| 712 |
#ifdef HAVE_WAYLAND_PLATFORM |
| 713 |
dri2_dpy->device_name = |
| 714 |
loader_get_device_name_for_fd(dri2_dpy->fd_render_gpu); |
| 715 |
#endif |
| 716 |
dri2_set_WL_bind_wayland_display(disp); |
| 717 |
|
| 718 |
/* Fill vtbl last to prevent accidentally calling virtual function during |
| 719 |
* initialization. |
| 720 |
*/ |
| 721 |
dri2_dpy->vtbl = &dri2_drm_display_vtbl; |
| 722 |
|
| 723 |
return EGL_TRUE; |
| 724 |
|
| 725 |
cleanup: |
| 726 |
return _eglError(EGL_NOT_INITIALIZED, err); |
| 727 |
} |
| 728 |
|
| 729 |
void |
| 730 |
dri2_teardown_drm(struct dri2_egl_display *dri2_dpy) |
| 731 |
{ |
| 732 |
if (dri2_dpy->own_device) |
| 733 |
gbm_device_destroy(&dri2_dpy->gbm_dri->base); |
| 734 |
} |