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