platform_android.c revision 51727b1cf57e8c4630767eb9ead207b102ffa489
1/* 2 * Mesa 3-D graphics library 3 * 4 * Copyright (C) 2010-2011 Chia-I Wu <olvaffe@gmail.com> 5 * Copyright (C) 2010-2011 LunarG Inc. 6 * 7 * Based on platform_x11, which has 8 * 9 * Copyright © 2011 Intel Corporation 10 * 11 * Permission is hereby granted, free of charge, to any person obtaining a 12 * copy of this software and associated documentation files (the "Software"), 13 * to deal in the Software without restriction, including without limitation 14 * the rights to use, copy, modify, merge, publish, distribute, sublicense, 15 * and/or sell copies of the Software, and to permit persons to whom the 16 * Software is furnished to do so, subject to the following conditions: 17 * 18 * The above copyright notice and this permission notice shall be included 19 * in all copies or substantial portions of the Software. 20 * 21 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 22 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 23 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL 24 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 25 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 26 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER 27 * DEALINGS IN THE SOFTWARE. 28 */ 29 30#include <errno.h> 31#include <dlfcn.h> 32#include <fcntl.h> 33#include <xf86drm.h> 34#include <stdbool.h> 35 36#if ANDROID_VERSION >= 0x402 37#include <sync/sync.h> 38#endif 39 40#include "loader.h" 41#include "egl_dri2.h" 42#include "egl_dri2_fallbacks.h" 43#include "gralloc_drm.h" 44 45#define ALIGN(val, align) (((val) + (align) - 1) & ~((align) - 1)) 46 47struct droid_yuv_format { 48 /* Lookup keys */ 49 int native; /* HAL_PIXEL_FORMAT_ */ 50 int is_ycrcb; /* 0 if chroma order is {Cb, Cr}, 1 if {Cr, Cb} */ 51 int chroma_step; /* Distance in bytes between subsequent chroma pixels. */ 52 53 /* Result */ 54 int fourcc; /* __DRI_IMAGE_FOURCC_ */ 55}; 56 57/* The following table is used to look up a DRI image FourCC based 58 * on native format and information contained in android_ycbcr struct. */ 59static const struct droid_yuv_format droid_yuv_formats[] = { 60 /* Native format, YCrCb, Chroma step, DRI image FourCC */ 61 { HAL_PIXEL_FORMAT_YCbCr_420_888, 0, 2, __DRI_IMAGE_FOURCC_NV12 }, 62 { HAL_PIXEL_FORMAT_YCbCr_420_888, 0, 1, __DRI_IMAGE_FOURCC_YUV420 }, 63 { HAL_PIXEL_FORMAT_YCbCr_420_888, 1, 1, __DRI_IMAGE_FOURCC_YVU420 }, 64 { HAL_PIXEL_FORMAT_YV12, 1, 1, __DRI_IMAGE_FOURCC_YVU420 }, 65}; 66 67static int 68get_fourcc_yuv(int native, int is_ycrcb, int chroma_step) 69{ 70 int i; 71 72 for (i = 0; i < ARRAY_SIZE(droid_yuv_formats); ++i) 73 if (droid_yuv_formats[i].native == native && 74 droid_yuv_formats[i].is_ycrcb == is_ycrcb && 75 droid_yuv_formats[i].chroma_step == chroma_step) 76 return droid_yuv_formats[i].fourcc; 77 78 return -1; 79} 80 81static bool 82is_yuv(int native) 83{ 84 int i; 85 86 for (i = 0; i < ARRAY_SIZE(droid_yuv_formats); ++i) 87 if (droid_yuv_formats[i].native == native) 88 return true; 89 90 return false; 91} 92 93static int 94get_format_bpp(int native) 95{ 96 int bpp; 97 98 switch (native) { 99 case HAL_PIXEL_FORMAT_RGBA_8888: 100 case HAL_PIXEL_FORMAT_RGBX_8888: 101 case HAL_PIXEL_FORMAT_BGRA_8888: 102 bpp = 4; 103 break; 104 case HAL_PIXEL_FORMAT_RGB_565: 105 bpp = 2; 106 break; 107 default: 108 bpp = 0; 109 break; 110 } 111 112 return bpp; 113} 114 115/* createImageFromFds requires fourcc format */ 116static int get_fourcc(int native) 117{ 118 switch (native) { 119 case HAL_PIXEL_FORMAT_RGB_565: return __DRI_IMAGE_FOURCC_RGB565; 120 case HAL_PIXEL_FORMAT_BGRA_8888: return __DRI_IMAGE_FOURCC_ARGB8888; 121 case HAL_PIXEL_FORMAT_RGBA_8888: return __DRI_IMAGE_FOURCC_ABGR8888; 122 case HAL_PIXEL_FORMAT_RGBX_8888: return __DRI_IMAGE_FOURCC_XBGR8888; 123 default: 124 _eglLog(_EGL_WARNING, "unsupported native buffer format 0x%x", native); 125 } 126 return -1; 127} 128 129static int get_format(int format) 130{ 131 switch (format) { 132 case HAL_PIXEL_FORMAT_BGRA_8888: return __DRI_IMAGE_FORMAT_ARGB8888; 133 case HAL_PIXEL_FORMAT_RGB_565: return __DRI_IMAGE_FORMAT_RGB565; 134 case HAL_PIXEL_FORMAT_RGBA_8888: return __DRI_IMAGE_FORMAT_ABGR8888; 135 case HAL_PIXEL_FORMAT_RGBX_8888: return __DRI_IMAGE_FORMAT_XBGR8888; 136 default: 137 _eglLog(_EGL_WARNING, "unsupported native buffer format 0x%x", format); 138 } 139 return -1; 140} 141 142static int 143get_native_buffer_fd(struct ANativeWindowBuffer *buf) 144{ 145 native_handle_t *handle = (native_handle_t *)buf->handle; 146 /* 147 * Various gralloc implementations exist, but the dma-buf fd tends 148 * to be first. Access it directly to avoid a dependency on specific 149 * gralloc versions. 150 */ 151 return (handle && handle->numFds) ? handle->data[0] : -1; 152} 153 154static int 155get_native_buffer_name(struct ANativeWindowBuffer *buf) 156{ 157 return gralloc_drm_get_gem_handle(buf->handle); 158} 159 160static EGLBoolean 161droid_window_dequeue_buffer(struct dri2_egl_surface *dri2_surf) 162{ 163#if ANDROID_VERSION >= 0x0402 164 int fence_fd; 165 166 if (dri2_surf->window->dequeueBuffer(dri2_surf->window, &dri2_surf->buffer, 167 &fence_fd)) 168 return EGL_FALSE; 169 170 /* If access to the buffer is controlled by a sync fence, then block on the 171 * fence. 172 * 173 * It may be more performant to postpone blocking until there is an 174 * immediate need to write to the buffer. But doing so would require adding 175 * hooks to the DRI2 loader. 176 * 177 * From the ANativeWindow::dequeueBuffer documentation: 178 * 179 * The libsync fence file descriptor returned in the int pointed to by 180 * the fenceFd argument will refer to the fence that must signal 181 * before the dequeued buffer may be written to. A value of -1 182 * indicates that the caller may access the buffer immediately without 183 * waiting on a fence. If a valid file descriptor is returned (i.e. 184 * any value except -1) then the caller is responsible for closing the 185 * file descriptor. 186 */ 187 if (fence_fd >= 0) { 188 /* From the SYNC_IOC_WAIT documentation in <linux/sync.h>: 189 * 190 * Waits indefinitely if timeout < 0. 191 */ 192 int timeout = -1; 193 sync_wait(fence_fd, timeout); 194 close(fence_fd); 195 } 196 197 dri2_surf->buffer->common.incRef(&dri2_surf->buffer->common); 198#else 199 if (dri2_surf->window->dequeueBuffer(dri2_surf->window, &dri2_surf->buffer)) 200 return EGL_FALSE; 201 202 dri2_surf->buffer->common.incRef(&dri2_surf->buffer->common); 203 dri2_surf->window->lockBuffer(dri2_surf->window, dri2_surf->buffer); 204#endif 205 206 return EGL_TRUE; 207} 208 209static EGLBoolean 210droid_window_enqueue_buffer(_EGLDisplay *disp, struct dri2_egl_surface *dri2_surf) 211{ 212 struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp); 213 214 /* To avoid blocking other EGL calls, release the display mutex before 215 * we enter droid_window_enqueue_buffer() and re-acquire the mutex upon 216 * return. 217 */ 218 mtx_unlock(&disp->Mutex); 219 220#if ANDROID_VERSION >= 0x0402 221 /* Queue the buffer without a sync fence. This informs the ANativeWindow 222 * that it may access the buffer immediately. 223 * 224 * From ANativeWindow::dequeueBuffer: 225 * 226 * The fenceFd argument specifies a libsync fence file descriptor for 227 * a fence that must signal before the buffer can be accessed. If 228 * the buffer can be accessed immediately then a value of -1 should 229 * be used. The caller must not use the file descriptor after it 230 * is passed to queueBuffer, and the ANativeWindow implementation 231 * is responsible for closing it. 232 */ 233 int fence_fd = -1; 234 dri2_surf->window->queueBuffer(dri2_surf->window, dri2_surf->buffer, 235 fence_fd); 236#else 237 dri2_surf->window->queueBuffer(dri2_surf->window, dri2_surf->buffer); 238#endif 239 240 dri2_surf->buffer->common.decRef(&dri2_surf->buffer->common); 241 dri2_surf->buffer = NULL; 242 243 mtx_lock(&disp->Mutex); 244 245 if (dri2_surf->dri_image_back) { 246 dri2_dpy->image->destroyImage(dri2_surf->dri_image_back); 247 dri2_surf->dri_image_back = NULL; 248 } 249 250 return EGL_TRUE; 251} 252 253static void 254droid_window_cancel_buffer(_EGLDisplay *disp, struct dri2_egl_surface *dri2_surf) 255{ 256 /* no cancel buffer? */ 257 droid_window_enqueue_buffer(disp, dri2_surf); 258} 259 260static __DRIbuffer * 261droid_alloc_local_buffer(struct dri2_egl_surface *dri2_surf, 262 unsigned int att, unsigned int format) 263{ 264 struct dri2_egl_display *dri2_dpy = 265 dri2_egl_display(dri2_surf->base.Resource.Display); 266 267 if (att >= ARRAY_SIZE(dri2_surf->local_buffers)) 268 return NULL; 269 270 if (!dri2_surf->local_buffers[att]) { 271 dri2_surf->local_buffers[att] = 272 dri2_dpy->dri2->allocateBuffer(dri2_dpy->dri_screen, att, format, 273 dri2_surf->base.Width, dri2_surf->base.Height); 274 } 275 276 return dri2_surf->local_buffers[att]; 277} 278 279static void 280droid_free_local_buffers(struct dri2_egl_surface *dri2_surf) 281{ 282 struct dri2_egl_display *dri2_dpy = 283 dri2_egl_display(dri2_surf->base.Resource.Display); 284 int i; 285 286 for (i = 0; i < ARRAY_SIZE(dri2_surf->local_buffers); i++) { 287 if (dri2_surf->local_buffers[i]) { 288 dri2_dpy->dri2->releaseBuffer(dri2_dpy->dri_screen, 289 dri2_surf->local_buffers[i]); 290 dri2_surf->local_buffers[i] = NULL; 291 } 292 } 293} 294 295static _EGLSurface * 296droid_create_surface(_EGLDriver *drv, _EGLDisplay *disp, EGLint type, 297 _EGLConfig *conf, void *native_window, 298 const EGLint *attrib_list) 299{ 300 struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp); 301 struct dri2_egl_config *dri2_conf = dri2_egl_config(conf); 302 struct dri2_egl_surface *dri2_surf; 303 struct ANativeWindow *window = native_window; 304 const __DRIconfig *config; 305 306 dri2_surf = calloc(1, sizeof *dri2_surf); 307 if (!dri2_surf) { 308 _eglError(EGL_BAD_ALLOC, "droid_create_surface"); 309 return NULL; 310 } 311 312 if (!_eglInitSurface(&dri2_surf->base, disp, type, conf, attrib_list)) 313 goto cleanup_surface; 314 315 if (type == EGL_WINDOW_BIT) { 316 int format; 317 318 if (!window || window->common.magic != ANDROID_NATIVE_WINDOW_MAGIC) { 319 _eglError(EGL_BAD_NATIVE_WINDOW, "droid_create_surface"); 320 goto cleanup_surface; 321 } 322 if (window->query(window, NATIVE_WINDOW_FORMAT, &format)) { 323 _eglError(EGL_BAD_NATIVE_WINDOW, "droid_create_surface"); 324 goto cleanup_surface; 325 } 326 327 if (format != dri2_conf->base.NativeVisualID) { 328 _eglLog(_EGL_WARNING, "Native format mismatch: 0x%x != 0x%x", 329 format, dri2_conf->base.NativeVisualID); 330 } 331 332 window->query(window, NATIVE_WINDOW_WIDTH, &dri2_surf->base.Width); 333 window->query(window, NATIVE_WINDOW_HEIGHT, &dri2_surf->base.Height); 334 } 335 336 config = dri2_get_dri_config(dri2_conf, type, 337 dri2_surf->base.GLColorspace); 338 if (!config) 339 goto cleanup_surface; 340 341 dri2_surf->dri_drawable = 342 (*dri2_dpy->dri2->createNewDrawable)(dri2_dpy->dri_screen, config, 343 dri2_surf); 344 if (dri2_surf->dri_drawable == NULL) { 345 _eglError(EGL_BAD_ALLOC, "dri2->createNewDrawable"); 346 goto cleanup_surface; 347 } 348 349 if (window) { 350 window->common.incRef(&window->common); 351 dri2_surf->window = window; 352 } 353 354 return &dri2_surf->base; 355 356cleanup_surface: 357 free(dri2_surf); 358 359 return NULL; 360} 361 362static _EGLSurface * 363droid_create_window_surface(_EGLDriver *drv, _EGLDisplay *disp, 364 _EGLConfig *conf, void *native_window, 365 const EGLint *attrib_list) 366{ 367 return droid_create_surface(drv, disp, EGL_WINDOW_BIT, conf, 368 native_window, attrib_list); 369} 370 371static _EGLSurface * 372droid_create_pbuffer_surface(_EGLDriver *drv, _EGLDisplay *disp, 373 _EGLConfig *conf, const EGLint *attrib_list) 374{ 375 return droid_create_surface(drv, disp, EGL_PBUFFER_BIT, conf, 376 NULL, attrib_list); 377} 378 379static EGLBoolean 380droid_destroy_surface(_EGLDriver *drv, _EGLDisplay *disp, _EGLSurface *surf) 381{ 382 struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp); 383 struct dri2_egl_surface *dri2_surf = dri2_egl_surface(surf); 384 385 droid_free_local_buffers(dri2_surf); 386 387 if (dri2_surf->base.Type == EGL_WINDOW_BIT) { 388 if (dri2_surf->buffer) 389 droid_window_cancel_buffer(disp, dri2_surf); 390 391 dri2_surf->window->common.decRef(&dri2_surf->window->common); 392 } 393 394 if (dri2_surf->dri_image_back) { 395 _eglLog(_EGL_DEBUG, "%s : %d : destroy dri_image_back", __func__, __LINE__); 396 dri2_dpy->image->destroyImage(dri2_surf->dri_image_back); 397 dri2_surf->dri_image_back = NULL; 398 } 399 400 if (dri2_surf->dri_image_front) { 401 _eglLog(_EGL_DEBUG, "%s : %d : destroy dri_image_front", __func__, __LINE__); 402 dri2_dpy->image->destroyImage(dri2_surf->dri_image_front); 403 dri2_surf->dri_image_front = NULL; 404 } 405 406 (*dri2_dpy->core->destroyDrawable)(dri2_surf->dri_drawable); 407 408 free(dri2_surf); 409 410 return EGL_TRUE; 411} 412 413static int 414update_buffers(struct dri2_egl_surface *dri2_surf) 415{ 416 if (dri2_surf->base.Type != EGL_WINDOW_BIT) 417 return 0; 418 419 /* try to dequeue the next back buffer */ 420 if (!dri2_surf->buffer && !droid_window_dequeue_buffer(dri2_surf)) { 421 _eglLog(_EGL_WARNING, "Could not dequeue buffer from native window"); 422 return -1; 423 } 424 425 /* free outdated buffers and update the surface size */ 426 if (dri2_surf->base.Width != dri2_surf->buffer->width || 427 dri2_surf->base.Height != dri2_surf->buffer->height) { 428 droid_free_local_buffers(dri2_surf); 429 dri2_surf->base.Width = dri2_surf->buffer->width; 430 dri2_surf->base.Height = dri2_surf->buffer->height; 431 } 432 433 return 0; 434} 435 436static int 437get_back_bo(struct dri2_egl_surface *dri2_surf) 438{ 439 struct dri2_egl_display *dri2_dpy = 440 dri2_egl_display(dri2_surf->base.Resource.Display); 441 int fourcc, pitch; 442 int offset = 0, fd; 443 444 if (dri2_surf->dri_image_back) 445 return 0; 446 447 if (!dri2_surf->buffer) 448 return -1; 449 450 fd = get_native_buffer_fd(dri2_surf->buffer); 451 if (fd < 0) { 452 _eglLog(_EGL_WARNING, "Could not get native buffer FD"); 453 return -1; 454 } 455 456 fourcc = get_fourcc(dri2_surf->buffer->format); 457 458 pitch = dri2_surf->buffer->stride * 459 get_format_bpp(dri2_surf->buffer->format); 460 461 if (fourcc == -1 || pitch == 0) { 462 _eglLog(_EGL_WARNING, "Invalid buffer fourcc(%x) or pitch(%d)", 463 fourcc, pitch); 464 return -1; 465 } 466 467 dri2_surf->dri_image_back = 468 dri2_dpy->image->createImageFromFds(dri2_dpy->dri_screen, 469 dri2_surf->base.Width, 470 dri2_surf->base.Height, 471 fourcc, 472 &fd, 473 1, 474 &pitch, 475 &offset, 476 dri2_surf); 477 if (!dri2_surf->dri_image_back) 478 return -1; 479 480 return 0; 481} 482 483static int 484droid_image_get_buffers(__DRIdrawable *driDrawable, 485 unsigned int format, 486 uint32_t *stamp, 487 void *loaderPrivate, 488 uint32_t buffer_mask, 489 struct __DRIimageList *images) 490{ 491 struct dri2_egl_surface *dri2_surf = loaderPrivate; 492 struct dri2_egl_display *dri2_dpy = 493 dri2_egl_display(dri2_surf->base.Resource.Display); 494 495 images->image_mask = 0; 496 images->front = NULL; 497 images->back = NULL; 498 499 if (update_buffers(dri2_surf) < 0) 500 return 0; 501 502 if (buffer_mask & __DRI_IMAGE_BUFFER_FRONT) { 503 if (dri2_surf->base.Type == EGL_WINDOW_BIT) { 504 /* According current EGL spec, 505 * front buffer rendering for window surface is not supported now */ 506 _eglLog(_EGL_WARNING, 507 "%s:%d front buffer rendering for window surface is not supported!", 508 __func__, __LINE__); 509 return 0; 510 } 511 512 /* The EGL 1.5 spec states that pbuffers are single-buffered. Specifically, 513 * the spec states that they have a back buffer but no front buffer, in 514 * contrast to pixmaps, which have a front buffer but no back buffer. 515 * 516 * Single-buffered surfaces with no front buffer confuse Mesa; so we deviate 517 * from the spec, following the precedent of Mesa's EGL X11 platform. The 518 * X11 platform correctly assigns pbuffers to single-buffered configs, but 519 * assigns the pbuffer a front buffer instead of a back buffer. 520 * 521 * Pbuffers in the X11 platform mostly work today, so let's just copy its 522 * behavior instead of trying to fix (and hence potentially breaking) the 523 * world. 524 */ 525 if (!dri2_surf->dri_image_front && 526 dri2_surf->base.Type == EGL_PBUFFER_BIT) { 527 dri2_surf->dri_image_front = 528 dri2_dpy->image->createImage(dri2_dpy->dri_screen, 529 dri2_surf->base.Width, 530 dri2_surf->base.Height, 531 format, 532 0, 533 dri2_surf); 534 } 535 536 if (!dri2_surf->dri_image_front) { 537 _eglLog(_EGL_WARNING, 538 "%s:%d dri2_image front buffer allocation failed!", 539 __func__, __LINE__); 540 return 0; 541 } 542 543 images->front = dri2_surf->dri_image_front; 544 images->image_mask |= __DRI_IMAGE_BUFFER_FRONT; 545 } 546 547 if (buffer_mask & __DRI_IMAGE_BUFFER_BACK) { 548 if (dri2_surf->base.Type == EGL_WINDOW_BIT) { 549 if (get_back_bo(dri2_surf) < 0) 550 return 0; 551 } 552 553 if (!dri2_surf->dri_image_back) { 554 _eglLog(_EGL_WARNING, 555 "%s:%d dri2_image back buffer allocation failed!", 556 __func__, __LINE__); 557 return 0; 558 } 559 560 images->back = dri2_surf->dri_image_back; 561 images->image_mask |= __DRI_IMAGE_BUFFER_BACK; 562 } 563 564 return 1; 565} 566 567static EGLBoolean 568droid_swap_buffers(_EGLDriver *drv, _EGLDisplay *disp, _EGLSurface *draw) 569{ 570 struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp); 571 struct dri2_egl_surface *dri2_surf = dri2_egl_surface(draw); 572 573 if (dri2_surf->base.Type != EGL_WINDOW_BIT) 574 return EGL_TRUE; 575 576 dri2_flush_drawable_for_swapbuffers(disp, draw); 577 578 if (dri2_surf->buffer) 579 droid_window_enqueue_buffer(disp, dri2_surf); 580 581 (*dri2_dpy->flush->invalidate)(dri2_surf->dri_drawable); 582 583 return EGL_TRUE; 584} 585 586static _EGLImage * 587droid_create_image_from_prime_fd_yuv(_EGLDisplay *disp, _EGLContext *ctx, 588 struct ANativeWindowBuffer *buf, int fd) 589{ 590 struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp); 591 struct android_ycbcr ycbcr; 592 size_t offsets[3]; 593 size_t pitches[3]; 594 int is_ycrcb; 595 int fourcc; 596 int ret; 597 598 if (!dri2_dpy->gralloc->lock_ycbcr) { 599 _eglLog(_EGL_WARNING, "Gralloc does not support lock_ycbcr"); 600 return NULL; 601 } 602 603 memset(&ycbcr, 0, sizeof(ycbcr)); 604 ret = dri2_dpy->gralloc->lock_ycbcr(dri2_dpy->gralloc, buf->handle, 605 0, 0, 0, 0, 0, &ycbcr); 606 if (ret) { 607 _eglLog(_EGL_WARNING, "gralloc->lock_ycbcr failed: %d", ret); 608 return NULL; 609 } 610 dri2_dpy->gralloc->unlock(dri2_dpy->gralloc, buf->handle); 611 612 /* When lock_ycbcr's usage argument contains no SW_READ/WRITE flags 613 * it will return the .y/.cb/.cr pointers based on a NULL pointer, 614 * so they can be interpreted as offsets. */ 615 offsets[0] = (size_t)ycbcr.y; 616 /* We assume here that all the planes are located in one DMA-buf. */ 617 is_ycrcb = (size_t)ycbcr.cb < (size_t)ycbcr.cr; 618 if (is_ycrcb) { 619 offsets[1] = (size_t)ycbcr.cr; 620 offsets[2] = (size_t)ycbcr.cb; 621 } else { 622 offsets[1] = (size_t)ycbcr.cb; 623 offsets[2] = (size_t)ycbcr.cr; 624 } 625 626 /* .ystride is the line length (in bytes) of the Y plane, 627 * .cstride is the line length (in bytes) of any of the remaining 628 * Cb/Cr/CbCr planes, assumed to be the same for Cb and Cr for fully 629 * planar formats. */ 630 pitches[0] = ycbcr.ystride; 631 pitches[1] = pitches[2] = ycbcr.cstride; 632 633 /* .chroma_step is the byte distance between the same chroma channel 634 * values of subsequent pixels, assumed to be the same for Cb and Cr. */ 635 fourcc = get_fourcc_yuv(buf->format, is_ycrcb, ycbcr.chroma_step); 636 if (fourcc == -1) { 637 _eglLog(_EGL_WARNING, "unsupported YUV format, native = %x, is_ycrcb = %d, chroma_step = %d", 638 buf->format, is_ycrcb, ycbcr.chroma_step); 639 return NULL; 640 } 641 642 if (ycbcr.chroma_step == 2) { 643 /* Semi-planar Y + CbCr or Y + CbCr format. */ 644 const EGLint attr_list_2plane[] = { 645 EGL_WIDTH, buf->width, 646 EGL_HEIGHT, buf->height, 647 EGL_LINUX_DRM_FOURCC_EXT, fourcc, 648 EGL_DMA_BUF_PLANE0_FD_EXT, fd, 649 EGL_DMA_BUF_PLANE0_PITCH_EXT, pitches[0], 650 EGL_DMA_BUF_PLANE0_OFFSET_EXT, offsets[0], 651 EGL_DMA_BUF_PLANE1_FD_EXT, fd, 652 EGL_DMA_BUF_PLANE1_PITCH_EXT, pitches[1], 653 EGL_DMA_BUF_PLANE1_OFFSET_EXT, offsets[1], 654 EGL_NONE, 0 655 }; 656 657 return dri2_create_image_dma_buf(disp, ctx, NULL, attr_list_2plane); 658 } else { 659 /* Fully planar Y + Cb + Cr or Y + Cr + Cb format. */ 660 const EGLint attr_list_3plane[] = { 661 EGL_WIDTH, buf->width, 662 EGL_HEIGHT, buf->height, 663 EGL_LINUX_DRM_FOURCC_EXT, fourcc, 664 EGL_DMA_BUF_PLANE0_FD_EXT, fd, 665 EGL_DMA_BUF_PLANE0_PITCH_EXT, pitches[0], 666 EGL_DMA_BUF_PLANE0_OFFSET_EXT, offsets[0], 667 EGL_DMA_BUF_PLANE1_FD_EXT, fd, 668 EGL_DMA_BUF_PLANE1_PITCH_EXT, pitches[1], 669 EGL_DMA_BUF_PLANE1_OFFSET_EXT, offsets[1], 670 EGL_DMA_BUF_PLANE2_FD_EXT, fd, 671 EGL_DMA_BUF_PLANE2_PITCH_EXT, pitches[2], 672 EGL_DMA_BUF_PLANE2_OFFSET_EXT, offsets[2], 673 EGL_NONE, 0 674 }; 675 676 return dri2_create_image_dma_buf(disp, ctx, NULL, attr_list_3plane); 677 } 678} 679 680static _EGLImage * 681droid_create_image_from_prime_fd(_EGLDisplay *disp, _EGLContext *ctx, 682 struct ANativeWindowBuffer *buf, int fd) 683{ 684 unsigned int pitch; 685 686 if (is_yuv(buf->format)) 687 return droid_create_image_from_prime_fd_yuv(disp, ctx, buf, fd); 688 689 const int fourcc = get_fourcc(buf->format); 690 if (fourcc == -1) { 691 _eglError(EGL_BAD_PARAMETER, "eglCreateEGLImageKHR"); 692 return NULL; 693 } 694 695 pitch = buf->stride * get_format_bpp(buf->format); 696 if (pitch == 0) { 697 _eglError(EGL_BAD_PARAMETER, "eglCreateEGLImageKHR"); 698 return NULL; 699 } 700 701 const EGLint attr_list[] = { 702 EGL_WIDTH, buf->width, 703 EGL_HEIGHT, buf->height, 704 EGL_LINUX_DRM_FOURCC_EXT, fourcc, 705 EGL_DMA_BUF_PLANE0_FD_EXT, fd, 706 EGL_DMA_BUF_PLANE0_PITCH_EXT, pitch, 707 EGL_DMA_BUF_PLANE0_OFFSET_EXT, 0, 708 EGL_NONE, 0 709 }; 710 711 return dri2_create_image_dma_buf(disp, ctx, NULL, attr_list); 712} 713 714static _EGLImage * 715droid_create_image_from_name(_EGLDisplay *disp, _EGLContext *ctx, 716 struct ANativeWindowBuffer *buf) 717{ 718 struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp); 719 struct dri2_egl_image *dri2_img; 720 int name; 721 int format; 722 723 name = get_native_buffer_name(buf); 724 if (!name) { 725 _eglError(EGL_BAD_PARAMETER, "eglCreateEGLImageKHR"); 726 return NULL; 727 } 728 729 format = get_format(buf->format); 730 if (format == -1) 731 return NULL; 732 733 dri2_img = calloc(1, sizeof(*dri2_img)); 734 if (!dri2_img) { 735 _eglError(EGL_BAD_ALLOC, "droid_create_image_mesa_drm"); 736 return NULL; 737 } 738 739 if (!_eglInitImage(&dri2_img->base, disp)) { 740 free(dri2_img); 741 return NULL; 742 } 743 744 dri2_img->dri_image = 745 dri2_dpy->image->createImageFromName(dri2_dpy->dri_screen, 746 buf->width, 747 buf->height, 748 format, 749 name, 750 buf->stride, 751 dri2_img); 752 if (!dri2_img->dri_image) { 753 free(dri2_img); 754 _eglError(EGL_BAD_ALLOC, "droid_create_image_mesa_drm"); 755 return NULL; 756 } 757 758 return &dri2_img->base; 759} 760 761static EGLBoolean 762droid_query_surface(_EGLDriver *drv, _EGLDisplay *dpy, _EGLSurface *surf, 763 EGLint attribute, EGLint *value) 764{ 765 struct dri2_egl_surface *dri2_surf = dri2_egl_surface(surf); 766 switch (attribute) { 767 case EGL_WIDTH: 768 if (dri2_surf->base.Type == EGL_WINDOW_BIT && dri2_surf->window) { 769 dri2_surf->window->query(dri2_surf->window, 770 NATIVE_WINDOW_DEFAULT_WIDTH, value); 771 return EGL_TRUE; 772 } 773 break; 774 case EGL_HEIGHT: 775 if (dri2_surf->base.Type == EGL_WINDOW_BIT && dri2_surf->window) { 776 dri2_surf->window->query(dri2_surf->window, 777 NATIVE_WINDOW_DEFAULT_HEIGHT, value); 778 return EGL_TRUE; 779 } 780 break; 781 default: 782 break; 783 } 784 return _eglQuerySurface(drv, dpy, surf, attribute, value); 785} 786 787static _EGLImage * 788dri2_create_image_android_native_buffer(_EGLDisplay *disp, 789 _EGLContext *ctx, 790 struct ANativeWindowBuffer *buf) 791{ 792 int fd; 793 794 if (ctx != NULL) { 795 /* From the EGL_ANDROID_image_native_buffer spec: 796 * 797 * * If <target> is EGL_NATIVE_BUFFER_ANDROID and <ctx> is not 798 * EGL_NO_CONTEXT, the error EGL_BAD_CONTEXT is generated. 799 */ 800 _eglError(EGL_BAD_CONTEXT, "eglCreateEGLImageKHR: for " 801 "EGL_NATIVE_BUFFER_ANDROID, the context must be " 802 "EGL_NO_CONTEXT"); 803 return NULL; 804 } 805 806 if (!buf || buf->common.magic != ANDROID_NATIVE_BUFFER_MAGIC || 807 buf->common.version != sizeof(*buf)) { 808 _eglError(EGL_BAD_PARAMETER, "eglCreateEGLImageKHR"); 809 return NULL; 810 } 811 812 fd = get_native_buffer_fd(buf); 813 if (fd >= 0) 814 return droid_create_image_from_prime_fd(disp, ctx, buf, fd); 815 816 return droid_create_image_from_name(disp, ctx, buf); 817} 818 819static _EGLImage * 820droid_create_image_khr(_EGLDriver *drv, _EGLDisplay *disp, 821 _EGLContext *ctx, EGLenum target, 822 EGLClientBuffer buffer, const EGLint *attr_list) 823{ 824 switch (target) { 825 case EGL_NATIVE_BUFFER_ANDROID: 826 return dri2_create_image_android_native_buffer(disp, ctx, 827 (struct ANativeWindowBuffer *) buffer); 828 default: 829 return dri2_create_image_khr(drv, disp, ctx, target, buffer, attr_list); 830 } 831} 832 833static void 834droid_flush_front_buffer(__DRIdrawable * driDrawable, void *loaderPrivate) 835{ 836} 837 838static int 839droid_get_buffers_parse_attachments(struct dri2_egl_surface *dri2_surf, 840 unsigned int *attachments, int count) 841{ 842 int num_buffers = 0, i; 843 844 /* fill dri2_surf->buffers */ 845 for (i = 0; i < count * 2; i += 2) { 846 __DRIbuffer *buf, *local; 847 848 assert(num_buffers < ARRAY_SIZE(dri2_surf->buffers)); 849 buf = &dri2_surf->buffers[num_buffers]; 850 851 switch (attachments[i]) { 852 case __DRI_BUFFER_BACK_LEFT: 853 if (dri2_surf->base.Type == EGL_WINDOW_BIT) { 854 buf->attachment = attachments[i]; 855 buf->name = get_native_buffer_name(dri2_surf->buffer); 856 buf->cpp = get_format_bpp(dri2_surf->buffer->format); 857 buf->pitch = dri2_surf->buffer->stride * buf->cpp; 858 buf->flags = 0; 859 860 if (buf->name) 861 num_buffers++; 862 863 break; 864 } 865 /* fall through for pbuffers */ 866 case __DRI_BUFFER_DEPTH: 867 case __DRI_BUFFER_STENCIL: 868 case __DRI_BUFFER_ACCUM: 869 case __DRI_BUFFER_DEPTH_STENCIL: 870 case __DRI_BUFFER_HIZ: 871 local = droid_alloc_local_buffer(dri2_surf, 872 attachments[i], attachments[i + 1]); 873 874 if (local) { 875 *buf = *local; 876 num_buffers++; 877 } 878 break; 879 case __DRI_BUFFER_FRONT_LEFT: 880 case __DRI_BUFFER_FRONT_RIGHT: 881 case __DRI_BUFFER_FAKE_FRONT_LEFT: 882 case __DRI_BUFFER_FAKE_FRONT_RIGHT: 883 case __DRI_BUFFER_BACK_RIGHT: 884 default: 885 /* no front or right buffers */ 886 break; 887 } 888 } 889 890 return num_buffers; 891} 892 893static __DRIbuffer * 894droid_get_buffers_with_format(__DRIdrawable * driDrawable, 895 int *width, int *height, 896 unsigned int *attachments, int count, 897 int *out_count, void *loaderPrivate) 898{ 899 struct dri2_egl_surface *dri2_surf = loaderPrivate; 900 901 if (update_buffers(dri2_surf) < 0) 902 return NULL; 903 904 dri2_surf->buffer_count = 905 droid_get_buffers_parse_attachments(dri2_surf, attachments, count); 906 907 if (width) 908 *width = dri2_surf->base.Width; 909 if (height) 910 *height = dri2_surf->base.Height; 911 912 *out_count = dri2_surf->buffer_count; 913 914 return dri2_surf->buffers; 915} 916 917static EGLBoolean 918droid_add_configs_for_visuals(_EGLDriver *drv, _EGLDisplay *dpy) 919{ 920 struct dri2_egl_display *dri2_dpy = dri2_egl_display(dpy); 921 static const struct { 922 int format; 923 unsigned int rgba_masks[4]; 924 } visuals[] = { 925 { HAL_PIXEL_FORMAT_RGBA_8888, { 0xff, 0xff00, 0xff0000, 0xff000000 } }, 926 { HAL_PIXEL_FORMAT_RGBX_8888, { 0xff, 0xff00, 0xff0000, 0x0 } }, 927 { HAL_PIXEL_FORMAT_RGB_565, { 0xf800, 0x7e0, 0x1f, 0x0 } }, 928 { HAL_PIXEL_FORMAT_BGRA_8888, { 0xff0000, 0xff00, 0xff, 0xff000000 } }, 929 }; 930 EGLint config_attrs[] = { 931 EGL_NATIVE_VISUAL_ID, 0, 932 EGL_NATIVE_VISUAL_TYPE, 0, 933 EGL_FRAMEBUFFER_TARGET_ANDROID, EGL_TRUE, 934 EGL_RECORDABLE_ANDROID, EGL_TRUE, 935 EGL_NONE 936 }; 937 unsigned int format_count[ARRAY_SIZE(visuals)] = { 0 }; 938 int count, i, j; 939 940 count = 0; 941 for (i = 0; dri2_dpy->driver_configs[i]; i++) { 942 const EGLint surface_type = EGL_WINDOW_BIT | EGL_PBUFFER_BIT; 943 struct dri2_egl_config *dri2_conf; 944 945 for (j = 0; j < ARRAY_SIZE(visuals); j++) { 946 config_attrs[1] = visuals[j].format; 947 config_attrs[3] = visuals[j].format; 948 949 dri2_conf = dri2_add_config(dpy, dri2_dpy->driver_configs[i], 950 count + 1, surface_type, config_attrs, visuals[j].rgba_masks); 951 if (dri2_conf) { 952 count++; 953 format_count[j]++; 954 } 955 } 956 } 957 958 for (i = 0; i < ARRAY_SIZE(format_count); i++) { 959 if (!format_count[i]) { 960 _eglLog(_EGL_DEBUG, "No DRI config supports native format 0x%x", 961 visuals[i].format); 962 } 963 } 964 965 return (count != 0); 966} 967 968static int 969droid_open_device(struct dri2_egl_display *dri2_dpy) 970{ 971 int fd = -1, err = -EINVAL; 972 973 if (dri2_dpy->gralloc->perform) 974 err = dri2_dpy->gralloc->perform(dri2_dpy->gralloc, 975 GRALLOC_MODULE_PERFORM_GET_DRM_FD, 976 &fd); 977 if (err || fd < 0) { 978 _eglLog(_EGL_WARNING, "fail to get drm fd"); 979 fd = -1; 980 } 981 982 return (fd >= 0) ? fcntl(fd, F_DUPFD_CLOEXEC, 3) : -1; 983} 984 985/* support versions < JellyBean */ 986#ifndef ALOGW 987#define ALOGW LOGW 988#endif 989#ifndef ALOGD 990#define ALOGD LOGD 991#endif 992#ifndef ALOGI 993#define ALOGI LOGI 994#endif 995 996static void 997droid_log(EGLint level, const char *msg) 998{ 999 switch (level) { 1000 case _EGL_DEBUG: 1001 ALOGD("%s", msg); 1002 break; 1003 case _EGL_INFO: 1004 ALOGI("%s", msg); 1005 break; 1006 case _EGL_WARNING: 1007 ALOGW("%s", msg); 1008 break; 1009 case _EGL_FATAL: 1010 LOG_FATAL("%s", msg); 1011 break; 1012 default: 1013 break; 1014 } 1015} 1016 1017static struct dri2_egl_display_vtbl droid_display_vtbl = { 1018 .authenticate = NULL, 1019 .create_window_surface = droid_create_window_surface, 1020 .create_pixmap_surface = dri2_fallback_create_pixmap_surface, 1021 .create_pbuffer_surface = droid_create_pbuffer_surface, 1022 .destroy_surface = droid_destroy_surface, 1023 .create_image = droid_create_image_khr, 1024 .swap_interval = dri2_fallback_swap_interval, 1025 .swap_buffers = droid_swap_buffers, 1026 .swap_buffers_with_damage = dri2_fallback_swap_buffers_with_damage, 1027 .swap_buffers_region = dri2_fallback_swap_buffers_region, 1028 .post_sub_buffer = dri2_fallback_post_sub_buffer, 1029 .copy_buffers = dri2_fallback_copy_buffers, 1030 .query_buffer_age = dri2_fallback_query_buffer_age, 1031 .query_surface = droid_query_surface, 1032 .create_wayland_buffer_from_image = dri2_fallback_create_wayland_buffer_from_image, 1033 .get_sync_values = dri2_fallback_get_sync_values, 1034 .get_dri_drawable = dri2_surface_get_dri_drawable, 1035}; 1036 1037static const __DRIdri2LoaderExtension droid_dri2_loader_extension = { 1038 .base = { __DRI_DRI2_LOADER, 3 }, 1039 1040 .getBuffers = NULL, 1041 .flushFrontBuffer = droid_flush_front_buffer, 1042 .getBuffersWithFormat = droid_get_buffers_with_format, 1043}; 1044 1045static const __DRIimageLoaderExtension droid_image_loader_extension = { 1046 .base = { __DRI_IMAGE_LOADER, 1 }, 1047 1048 .getBuffers = droid_image_get_buffers, 1049 .flushFrontBuffer = droid_flush_front_buffer, 1050}; 1051 1052static const __DRIextension *droid_dri2_loader_extensions[] = { 1053 &droid_dri2_loader_extension.base, 1054 &image_lookup_extension.base, 1055 &use_invalidate.base, 1056 NULL, 1057}; 1058 1059static const __DRIextension *droid_image_loader_extensions[] = { 1060 &droid_image_loader_extension.base, 1061 &image_lookup_extension.base, 1062 &use_invalidate.base, 1063 NULL, 1064}; 1065 1066EGLBoolean 1067dri2_initialize_android(_EGLDriver *drv, _EGLDisplay *dpy) 1068{ 1069 struct dri2_egl_display *dri2_dpy; 1070 const char *err; 1071 int ret; 1072 1073 _eglSetLogProc(droid_log); 1074 1075 loader_set_logger(_eglLog); 1076 1077 dri2_dpy = calloc(1, sizeof(*dri2_dpy)); 1078 if (!dri2_dpy) 1079 return _eglError(EGL_BAD_ALLOC, "eglInitialize"); 1080 1081 ret = hw_get_module(GRALLOC_HARDWARE_MODULE_ID, 1082 (const hw_module_t **)&dri2_dpy->gralloc); 1083 if (ret) { 1084 err = "DRI2: failed to get gralloc module"; 1085 goto cleanup_display; 1086 } 1087 1088 dpy->DriverData = (void *) dri2_dpy; 1089 1090 dri2_dpy->fd = droid_open_device(dri2_dpy); 1091 if (dri2_dpy->fd < 0) { 1092 err = "DRI2: failed to open device"; 1093 goto cleanup_display; 1094 } 1095 1096 dri2_dpy->driver_name = loader_get_driver_for_fd(dri2_dpy->fd); 1097 if (dri2_dpy->driver_name == NULL) { 1098 err = "DRI2: failed to get driver name"; 1099 goto cleanup_device; 1100 } 1101 1102 if (!dri2_load_driver(dpy)) { 1103 err = "DRI2: failed to load driver"; 1104 goto cleanup_driver_name; 1105 } 1106 1107 dri2_dpy->is_render_node = drmGetNodeTypeFromFd(dri2_dpy->fd) == DRM_NODE_RENDER; 1108 1109 /* render nodes cannot use Gem names, and thus do not support 1110 * the __DRI_DRI2_LOADER extension */ 1111 if (!dri2_dpy->is_render_node) 1112 dri2_dpy->loader_extensions = droid_dri2_loader_extensions; 1113 else 1114 dri2_dpy->loader_extensions = droid_image_loader_extensions; 1115 1116 if (!dri2_create_screen(dpy)) { 1117 err = "DRI2: failed to create screen"; 1118 goto cleanup_driver; 1119 } 1120 1121 if (!droid_add_configs_for_visuals(drv, dpy)) { 1122 err = "DRI2: failed to add configs"; 1123 goto cleanup_screen; 1124 } 1125 1126 dpy->Extensions.ANDROID_framebuffer_target = EGL_TRUE; 1127 dpy->Extensions.ANDROID_image_native_buffer = EGL_TRUE; 1128 dpy->Extensions.ANDROID_recordable = EGL_TRUE; 1129 1130 /* Fill vtbl last to prevent accidentally calling virtual function during 1131 * initialization. 1132 */ 1133 dri2_dpy->vtbl = &droid_display_vtbl; 1134 1135 return EGL_TRUE; 1136 1137cleanup_screen: 1138 dri2_dpy->core->destroyScreen(dri2_dpy->dri_screen); 1139cleanup_driver: 1140 dlclose(dri2_dpy->driver); 1141cleanup_driver_name: 1142 free(dri2_dpy->driver_name); 1143cleanup_device: 1144 close(dri2_dpy->fd); 1145cleanup_display: 1146 free(dri2_dpy); 1147 dpy->DriverData = NULL; 1148 1149 return _eglError(EGL_NOT_INITIALIZED, err); 1150} 1151