egl/dri2: Dispatch eglSwapInterval by display, not driver
[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
33 #if ANDROID_VERSION >= 0x402
34 #include <sync/sync.h>
35 #endif
36
37 #include "loader.h"
38 #include "egl_dri2.h"
39 #include "egl_dri2_fallbacks.h"
40 #include "gralloc_drm.h"
41
42 static int
43 get_format_bpp(int native)
44 {
45 int bpp;
46
47 switch (native) {
48 case HAL_PIXEL_FORMAT_RGBA_8888:
49 case HAL_PIXEL_FORMAT_RGBX_8888:
50 case HAL_PIXEL_FORMAT_BGRA_8888:
51 bpp = 4;
52 break;
53 case HAL_PIXEL_FORMAT_RGB_888:
54 bpp = 3;
55 break;
56 case HAL_PIXEL_FORMAT_RGB_565:
57 case HAL_PIXEL_FORMAT_RGBA_5551:
58 case HAL_PIXEL_FORMAT_RGBA_4444:
59 bpp = 2;
60 break;
61 default:
62 bpp = 0;
63 break;
64 }
65
66 return bpp;
67 }
68
69 static int
70 get_native_buffer_name(struct ANativeWindowBuffer *buf)
71 {
72 return gralloc_drm_get_gem_handle(buf->handle);
73 }
74
75 static EGLBoolean
76 droid_window_dequeue_buffer(struct dri2_egl_surface *dri2_surf)
77 {
78 #if ANDROID_VERSION >= 0x0402
79 int fence_fd;
80
81 if (dri2_surf->window->dequeueBuffer(dri2_surf->window, &dri2_surf->buffer,
82 &fence_fd))
83 return EGL_FALSE;
84
85 /* If access to the buffer is controlled by a sync fence, then block on the
86 * fence.
87 *
88 * It may be more performant to postpone blocking until there is an
89 * immediate need to write to the buffer. But doing so would require adding
90 * hooks to the DRI2 loader.
91 *
92 * From the ANativeWindow::dequeueBuffer documentation:
93 *
94 * The libsync fence file descriptor returned in the int pointed to by
95 * the fenceFd argument will refer to the fence that must signal
96 * before the dequeued buffer may be written to. A value of -1
97 * indicates that the caller may access the buffer immediately without
98 * waiting on a fence. If a valid file descriptor is returned (i.e.
99 * any value except -1) then the caller is responsible for closing the
100 * file descriptor.
101 */
102 if (fence_fd >= 0) {
103 /* From the SYNC_IOC_WAIT documentation in <linux/sync.h>:
104 *
105 * Waits indefinitely if timeout < 0.
106 */
107 int timeout = -1;
108 sync_wait(fence_fd, timeout);
109 close(fence_fd);
110 }
111
112 dri2_surf->buffer->common.incRef(&dri2_surf->buffer->common);
113 #else
114 if (dri2_surf->window->dequeueBuffer(dri2_surf->window, &dri2_surf->buffer))
115 return EGL_FALSE;
116
117 dri2_surf->buffer->common.incRef(&dri2_surf->buffer->common);
118 dri2_surf->window->lockBuffer(dri2_surf->window, dri2_surf->buffer);
119 #endif
120
121 return EGL_TRUE;
122 }
123
124 static EGLBoolean
125 droid_window_enqueue_buffer(struct dri2_egl_surface *dri2_surf)
126 {
127 #if ANDROID_VERSION >= 0x0402
128 /* Queue the buffer without a sync fence. This informs the ANativeWindow
129 * that it may access the buffer immediately.
130 *
131 * From ANativeWindow::dequeueBuffer:
132 *
133 * The fenceFd argument specifies a libsync fence file descriptor for
134 * a fence that must signal before the buffer can be accessed. If
135 * the buffer can be accessed immediately then a value of -1 should
136 * be used. The caller must not use the file descriptor after it
137 * is passed to queueBuffer, and the ANativeWindow implementation
138 * is responsible for closing it.
139 */
140 int fence_fd = -1;
141 dri2_surf->window->queueBuffer(dri2_surf->window, dri2_surf->buffer,
142 fence_fd);
143 #else
144 dri2_surf->window->queueBuffer(dri2_surf->window, dri2_surf->buffer);
145 #endif
146
147 dri2_surf->buffer->common.decRef(&dri2_surf->buffer->common);
148 dri2_surf->buffer = NULL;
149
150 return EGL_TRUE;
151 }
152
153 static void
154 droid_window_cancel_buffer(struct dri2_egl_surface *dri2_surf)
155 {
156 /* no cancel buffer? */
157 droid_window_enqueue_buffer(dri2_surf);
158 }
159
160 static __DRIbuffer *
161 droid_alloc_local_buffer(struct dri2_egl_surface *dri2_surf,
162 unsigned int att, unsigned int format)
163 {
164 struct dri2_egl_display *dri2_dpy =
165 dri2_egl_display(dri2_surf->base.Resource.Display);
166
167 if (att >= ARRAY_SIZE(dri2_surf->local_buffers))
168 return NULL;
169
170 if (!dri2_surf->local_buffers[att]) {
171 dri2_surf->local_buffers[att] =
172 dri2_dpy->dri2->allocateBuffer(dri2_dpy->dri_screen, att, format,
173 dri2_surf->base.Width, dri2_surf->base.Height);
174 }
175
176 return dri2_surf->local_buffers[att];
177 }
178
179 static void
180 droid_free_local_buffers(struct dri2_egl_surface *dri2_surf)
181 {
182 struct dri2_egl_display *dri2_dpy =
183 dri2_egl_display(dri2_surf->base.Resource.Display);
184 int i;
185
186 for (i = 0; i < ARRAY_SIZE(dri2_surf->local_buffers); i++) {
187 if (dri2_surf->local_buffers[i]) {
188 dri2_dpy->dri2->releaseBuffer(dri2_dpy->dri_screen,
189 dri2_surf->local_buffers[i]);
190 dri2_surf->local_buffers[i] = NULL;
191 }
192 }
193 }
194
195 static _EGLSurface *
196 droid_create_surface(_EGLDriver *drv, _EGLDisplay *disp, EGLint type,
197 _EGLConfig *conf, EGLNativeWindowType window,
198 const EGLint *attrib_list)
199 {
200 struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);
201 struct dri2_egl_config *dri2_conf = dri2_egl_config(conf);
202 struct dri2_egl_surface *dri2_surf;
203 dri2_surf = calloc(1, sizeof *dri2_surf);
204 if (!dri2_surf) {
205 _eglError(EGL_BAD_ALLOC, "droid_create_surface");
206 return NULL;
207 }
208
209 if (!_eglInitSurface(&dri2_surf->base, disp, type, conf, attrib_list))
210 goto cleanup_surface;
211
212 if (type == EGL_WINDOW_BIT) {
213 int format;
214
215 if (!window || window->common.magic != ANDROID_NATIVE_WINDOW_MAGIC) {
216 _eglError(EGL_BAD_NATIVE_WINDOW, "droid_create_surface");
217 goto cleanup_surface;
218 }
219 if (window->query(window, NATIVE_WINDOW_FORMAT, &format)) {
220 _eglError(EGL_BAD_NATIVE_WINDOW, "droid_create_surface");
221 goto cleanup_surface;
222 }
223
224 if (format != dri2_conf->base.NativeVisualID) {
225 _eglLog(_EGL_WARNING, "Native format mismatch: 0x%x != 0x%x",
226 format, dri2_conf->base.NativeVisualID);
227 }
228
229 window->query(window, NATIVE_WINDOW_WIDTH, &dri2_surf->base.Width);
230 window->query(window, NATIVE_WINDOW_HEIGHT, &dri2_surf->base.Height);
231 }
232
233 dri2_surf->dri_drawable =
234 (*dri2_dpy->dri2->createNewDrawable)(dri2_dpy->dri_screen,
235 dri2_conf->dri_double_config,
236 dri2_surf);
237 if (dri2_surf->dri_drawable == NULL) {
238 _eglError(EGL_BAD_ALLOC, "dri2->createNewDrawable");
239 goto cleanup_surface;
240 }
241
242 if (window) {
243 window->common.incRef(&window->common);
244 dri2_surf->window = window;
245 }
246
247 return &dri2_surf->base;
248
249 cleanup_surface:
250 free(dri2_surf);
251
252 return NULL;
253 }
254
255 static _EGLSurface *
256 droid_create_window_surface(_EGLDriver *drv, _EGLDisplay *disp,
257 _EGLConfig *conf, EGLNativeWindowType window,
258 const EGLint *attrib_list)
259 {
260 return droid_create_surface(drv, disp, EGL_WINDOW_BIT, conf,
261 window, attrib_list);
262 }
263
264 static _EGLSurface *
265 droid_create_pixmap_surface(_EGLDriver *drv, _EGLDisplay *disp,
266 _EGLConfig *conf, EGLNativePixmapType pixmap,
267 const EGLint *attrib_list)
268 {
269 return NULL;
270 }
271
272 static _EGLSurface *
273 droid_create_pbuffer_surface(_EGLDriver *drv, _EGLDisplay *disp,
274 _EGLConfig *conf, const EGLint *attrib_list)
275 {
276 return droid_create_surface(drv, disp, EGL_PBUFFER_BIT, conf,
277 NULL, attrib_list);
278 }
279
280 static EGLBoolean
281 droid_destroy_surface(_EGLDriver *drv, _EGLDisplay *disp, _EGLSurface *surf)
282 {
283 struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);
284 struct dri2_egl_surface *dri2_surf = dri2_egl_surface(surf);
285
286 if (!_eglPutSurface(surf))
287 return EGL_TRUE;
288
289 droid_free_local_buffers(dri2_surf);
290
291 if (dri2_surf->base.Type == EGL_WINDOW_BIT) {
292 if (dri2_surf->buffer)
293 droid_window_cancel_buffer(dri2_surf);
294
295 dri2_surf->window->common.decRef(&dri2_surf->window->common);
296 }
297
298 (*dri2_dpy->core->destroyDrawable)(dri2_surf->dri_drawable);
299
300 free(dri2_surf);
301
302 return EGL_TRUE;
303 }
304
305 static EGLBoolean
306 droid_swap_buffers(_EGLDriver *drv, _EGLDisplay *disp, _EGLSurface *draw)
307 {
308 struct dri2_egl_driver *dri2_drv = dri2_egl_driver(drv);
309 struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);
310 struct dri2_egl_surface *dri2_surf = dri2_egl_surface(draw);
311 _EGLContext *ctx;
312
313 if (dri2_surf->base.Type != EGL_WINDOW_BIT)
314 return EGL_TRUE;
315
316 if (dri2_drv->glFlush) {
317 ctx = _eglGetCurrentContext();
318 if (ctx && ctx->DrawSurface == &dri2_surf->base)
319 dri2_drv->glFlush();
320 }
321
322 (*dri2_dpy->flush->flush)(dri2_surf->dri_drawable);
323
324 if (dri2_surf->buffer)
325 droid_window_enqueue_buffer(dri2_surf);
326
327 (*dri2_dpy->flush->invalidate)(dri2_surf->dri_drawable);
328
329 return EGL_TRUE;
330 }
331
332 static _EGLImage *
333 dri2_create_image_android_native_buffer(_EGLDisplay *disp, _EGLContext *ctx,
334 struct ANativeWindowBuffer *buf)
335 {
336 struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);
337 struct dri2_egl_image *dri2_img;
338 int name;
339 EGLint format;
340
341 if (ctx != NULL) {
342 /* From the EGL_ANDROID_image_native_buffer spec:
343 *
344 * * If <target> is EGL_NATIVE_BUFFER_ANDROID and <ctx> is not
345 * EGL_NO_CONTEXT, the error EGL_BAD_CONTEXT is generated.
346 */
347 _eglError(EGL_BAD_CONTEXT, "eglCreateEGLImageKHR: for "
348 "EGL_NATIVE_BUFFER_ANDROID, the context must be "
349 "EGL_NO_CONTEXT");
350 return NULL;
351 }
352
353 if (!buf || buf->common.magic != ANDROID_NATIVE_BUFFER_MAGIC ||
354 buf->common.version != sizeof(*buf)) {
355 _eglError(EGL_BAD_PARAMETER, "eglCreateEGLImageKHR");
356 return NULL;
357 }
358
359 name = get_native_buffer_name(buf);
360 if (!name) {
361 _eglError(EGL_BAD_PARAMETER, "eglCreateEGLImageKHR");
362 return NULL;
363 }
364
365 /* see the table in droid_add_configs_for_visuals */
366 switch (buf->format) {
367 case HAL_PIXEL_FORMAT_BGRA_8888:
368 format = __DRI_IMAGE_FORMAT_ARGB8888;
369 break;
370 case HAL_PIXEL_FORMAT_RGB_565:
371 format = __DRI_IMAGE_FORMAT_RGB565;
372 break;
373 case HAL_PIXEL_FORMAT_RGBA_8888:
374 format = __DRI_IMAGE_FORMAT_ABGR8888;
375 break;
376 case HAL_PIXEL_FORMAT_RGBX_8888:
377 format = __DRI_IMAGE_FORMAT_XBGR8888;
378 break;
379 case HAL_PIXEL_FORMAT_RGB_888:
380 case HAL_PIXEL_FORMAT_RGBA_5551:
381 case HAL_PIXEL_FORMAT_RGBA_4444:
382 /* unsupported */
383 default:
384 _eglLog(_EGL_WARNING, "unsupported native buffer format 0x%x", buf->format);
385 return NULL;
386 break;
387 }
388
389 dri2_img = calloc(1, sizeof(*dri2_img));
390 if (!dri2_img) {
391 _eglError(EGL_BAD_ALLOC, "droid_create_image_mesa_drm");
392 return NULL;
393 }
394
395 if (!_eglInitImage(&dri2_img->base, disp)) {
396 free(dri2_img);
397 return NULL;
398 }
399
400 dri2_img->dri_image =
401 dri2_dpy->image->createImageFromName(dri2_dpy->dri_screen,
402 buf->width,
403 buf->height,
404 format,
405 name,
406 buf->stride,
407 dri2_img);
408 if (!dri2_img->dri_image) {
409 free(dri2_img);
410 _eglError(EGL_BAD_ALLOC, "droid_create_image_mesa_drm");
411 return NULL;
412 }
413
414 return &dri2_img->base;
415 }
416
417 static _EGLImage *
418 droid_create_image_khr(_EGLDriver *drv, _EGLDisplay *disp,
419 _EGLContext *ctx, EGLenum target,
420 EGLClientBuffer buffer, const EGLint *attr_list)
421 {
422 switch (target) {
423 case EGL_NATIVE_BUFFER_ANDROID:
424 return dri2_create_image_android_native_buffer(disp, ctx,
425 (struct ANativeWindowBuffer *) buffer);
426 default:
427 return dri2_create_image_khr(drv, disp, ctx, target, buffer, attr_list);
428 }
429 }
430
431 static void
432 droid_init_driver_functions(_EGLDriver *drv)
433 {
434 drv->API.CreateWindowSurface = droid_create_window_surface;
435 drv->API.CreatePixmapSurface = droid_create_pixmap_surface;
436 drv->API.CreatePbufferSurface = droid_create_pbuffer_surface;
437 drv->API.DestroySurface = droid_destroy_surface;
438 drv->API.SwapBuffers = droid_swap_buffers;
439
440 drv->API.CreateImageKHR = droid_create_image_khr;
441 }
442
443 static void
444 droid_flush_front_buffer(__DRIdrawable * driDrawable, void *loaderPrivate)
445 {
446 }
447
448 static int
449 droid_get_buffers_parse_attachments(struct dri2_egl_surface *dri2_surf,
450 unsigned int *attachments, int count)
451 {
452 int num_buffers = 0, i;
453
454 /* fill dri2_surf->buffers */
455 for (i = 0; i < count * 2; i += 2) {
456 __DRIbuffer *buf, *local;
457
458 assert(num_buffers < ARRAY_SIZE(dri2_surf->buffers));
459 buf = &dri2_surf->buffers[num_buffers];
460
461 switch (attachments[i]) {
462 case __DRI_BUFFER_BACK_LEFT:
463 if (dri2_surf->base.Type == EGL_WINDOW_BIT) {
464 buf->attachment = attachments[i];
465 buf->name = get_native_buffer_name(dri2_surf->buffer);
466 buf->cpp = get_format_bpp(dri2_surf->buffer->format);
467 buf->pitch = dri2_surf->buffer->stride * buf->cpp;
468 buf->flags = 0;
469
470 if (buf->name)
471 num_buffers++;
472
473 break;
474 }
475 /* fall through for pbuffers */
476 case __DRI_BUFFER_DEPTH:
477 case __DRI_BUFFER_STENCIL:
478 case __DRI_BUFFER_ACCUM:
479 case __DRI_BUFFER_DEPTH_STENCIL:
480 case __DRI_BUFFER_HIZ:
481 local = droid_alloc_local_buffer(dri2_surf,
482 attachments[i], attachments[i + 1]);
483
484 if (local) {
485 *buf = *local;
486 num_buffers++;
487 }
488 break;
489 case __DRI_BUFFER_FRONT_LEFT:
490 case __DRI_BUFFER_FRONT_RIGHT:
491 case __DRI_BUFFER_FAKE_FRONT_LEFT:
492 case __DRI_BUFFER_FAKE_FRONT_RIGHT:
493 case __DRI_BUFFER_BACK_RIGHT:
494 default:
495 /* no front or right buffers */
496 break;
497 }
498 }
499
500 return num_buffers;
501 }
502
503 static __DRIbuffer *
504 droid_get_buffers_with_format(__DRIdrawable * driDrawable,
505 int *width, int *height,
506 unsigned int *attachments, int count,
507 int *out_count, void *loaderPrivate)
508 {
509 struct dri2_egl_surface *dri2_surf = loaderPrivate;
510 struct dri2_egl_display *dri2_dpy =
511 dri2_egl_display(dri2_surf->base.Resource.Display);
512 int i;
513
514 if (dri2_surf->base.Type == EGL_WINDOW_BIT) {
515 /* try to dequeue the next back buffer */
516 if (!dri2_surf->buffer && !droid_window_dequeue_buffer(dri2_surf))
517 return NULL;
518
519 /* free outdated buffers and update the surface size */
520 if (dri2_surf->base.Width != dri2_surf->buffer->width ||
521 dri2_surf->base.Height != dri2_surf->buffer->height) {
522 droid_free_local_buffers(dri2_surf);
523 dri2_surf->base.Width = dri2_surf->buffer->width;
524 dri2_surf->base.Height = dri2_surf->buffer->height;
525 }
526 }
527
528 dri2_surf->buffer_count =
529 droid_get_buffers_parse_attachments(dri2_surf, attachments, count);
530
531 if (width)
532 *width = dri2_surf->base.Width;
533 if (height)
534 *height = dri2_surf->base.Height;
535
536 *out_count = dri2_surf->buffer_count;;
537
538 return dri2_surf->buffers;
539 }
540
541 static EGLBoolean
542 droid_add_configs_for_visuals(_EGLDriver *drv, _EGLDisplay *dpy)
543 {
544 struct dri2_egl_display *dri2_dpy = dri2_egl_display(dpy);
545 const struct {
546 int format;
547 unsigned int rgba_masks[4];
548 } visuals[] = {
549 { HAL_PIXEL_FORMAT_RGBA_8888, { 0xff, 0xff00, 0xff0000, 0xff000000 } },
550 { HAL_PIXEL_FORMAT_RGBX_8888, { 0xff, 0xff00, 0xff0000, 0x0 } },
551 { HAL_PIXEL_FORMAT_RGB_888, { 0xff, 0xff00, 0xff0000, 0x0 } },
552 { HAL_PIXEL_FORMAT_RGB_565, { 0xf800, 0x7e0, 0x1f, 0x0 } },
553 { HAL_PIXEL_FORMAT_BGRA_8888, { 0xff0000, 0xff00, 0xff, 0xff000000 } },
554 { 0, 0, { 0, 0, 0, 0 } }
555 };
556 int count, i, j;
557
558 count = 0;
559 for (i = 0; visuals[i].format; i++) {
560 int format_count = 0;
561
562 for (j = 0; dri2_dpy->driver_configs[j]; j++) {
563 const EGLint surface_type = EGL_WINDOW_BIT | EGL_PBUFFER_BIT;
564 struct dri2_egl_config *dri2_conf;
565 unsigned int double_buffered = 0;
566
567 dri2_dpy->core->getConfigAttrib(dri2_dpy->driver_configs[j],
568 __DRI_ATTRIB_DOUBLE_BUFFER, &double_buffered);
569
570 /* support only double buffered configs */
571 if (!double_buffered)
572 continue;
573
574 dri2_conf = dri2_add_config(dpy, dri2_dpy->driver_configs[j],
575 count + 1, surface_type, NULL, visuals[i].rgba_masks);
576 if (dri2_conf) {
577 dri2_conf->base.NativeVisualID = visuals[i].format;
578 dri2_conf->base.NativeVisualType = visuals[i].format;
579 count++;
580 format_count++;
581 }
582 }
583
584 if (!format_count) {
585 _eglLog(_EGL_DEBUG, "No DRI config supports native format 0x%x",
586 visuals[i].format);
587 }
588 }
589
590 /* post-process configs */
591 for (i = 0; i < dpy->Configs->Size; i++) {
592 struct dri2_egl_config *dri2_conf = dri2_egl_config(dpy->Configs->Elements[i]);
593
594 /* there is no front buffer so no OpenGL */
595 dri2_conf->base.RenderableType &= ~EGL_OPENGL_BIT;
596 dri2_conf->base.Conformant &= ~EGL_OPENGL_BIT;
597 }
598
599 return (count != 0);
600 }
601
602 static int
603 droid_open_device(void)
604 {
605 const hw_module_t *mod;
606 int fd = -1, err;
607
608 err = hw_get_module(GRALLOC_HARDWARE_MODULE_ID, &mod);
609 if (!err) {
610 const gralloc_module_t *gr = (gralloc_module_t *) mod;
611
612 err = -EINVAL;
613 if (gr->perform)
614 err = gr->perform(gr, GRALLOC_MODULE_PERFORM_GET_DRM_FD, &fd);
615 }
616 if (err || fd < 0) {
617 _eglLog(_EGL_WARNING, "fail to get drm fd");
618 fd = -1;
619 }
620
621 return (fd >= 0) ? dup(fd) : -1;
622 }
623
624 /* support versions < JellyBean */
625 #ifndef ALOGW
626 #define ALOGW LOGW
627 #endif
628 #ifndef ALOGD
629 #define ALOGD LOGD
630 #endif
631 #ifndef ALOGI
632 #define ALOGI LOGI
633 #endif
634
635 static void
636 droid_log(EGLint level, const char *msg)
637 {
638 switch (level) {
639 case _EGL_DEBUG:
640 ALOGD("%s", msg);
641 break;
642 case _EGL_INFO:
643 ALOGI("%s", msg);
644 break;
645 case _EGL_WARNING:
646 ALOGW("%s", msg);
647 break;
648 case _EGL_FATAL:
649 LOG_FATAL("%s", msg);
650 break;
651 default:
652 break;
653 }
654 }
655
656 static struct dri2_egl_display_vtbl droid_display_vtbl = {
657 .authenticate = NULL,
658 .swap_interval = dri2_fallback_swap_interval,
659 };
660
661 EGLBoolean
662 dri2_initialize_android(_EGLDriver *drv, _EGLDisplay *dpy)
663 {
664 struct dri2_egl_display *dri2_dpy;
665 const char *err;
666
667 _eglSetLogProc(droid_log);
668
669 loader_set_logger(_eglLog);
670
671 dri2_dpy = calloc(1, sizeof(*dri2_dpy));
672 if (!dri2_dpy)
673 return _eglError(EGL_BAD_ALLOC, "eglInitialize");
674
675 dpy->DriverData = (void *) dri2_dpy;
676
677 dri2_dpy->fd = droid_open_device();
678 if (dri2_dpy->fd < 0) {
679 err = "DRI2: failed to open device";
680 goto cleanup_display;
681 }
682
683 dri2_dpy->driver_name = loader_get_driver_for_fd(dri2_dpy->fd, 0);
684 if (dri2_dpy->driver_name == NULL) {
685 err = "DRI2: failed to get driver name";
686 goto cleanup_device;
687 }
688
689 if (!dri2_load_driver(dpy)) {
690 err = "DRI2: failed to load driver";
691 goto cleanup_driver_name;
692 }
693
694 dri2_dpy->dri2_loader_extension.base.name = __DRI_DRI2_LOADER;
695 dri2_dpy->dri2_loader_extension.base.version = 3;
696 dri2_dpy->dri2_loader_extension.getBuffers = NULL;
697 dri2_dpy->dri2_loader_extension.flushFrontBuffer = droid_flush_front_buffer;
698 dri2_dpy->dri2_loader_extension.getBuffersWithFormat =
699 droid_get_buffers_with_format;
700
701 dri2_dpy->extensions[0] = &dri2_dpy->dri2_loader_extension.base;
702 dri2_dpy->extensions[1] = &image_lookup_extension.base;
703 dri2_dpy->extensions[2] = &use_invalidate.base;
704 dri2_dpy->extensions[3] = NULL;
705
706 if (!dri2_create_screen(dpy)) {
707 err = "DRI2: failed to create screen";
708 goto cleanup_driver;
709 }
710
711 if (!droid_add_configs_for_visuals(drv, dpy)) {
712 err = "DRI2: failed to add configs";
713 goto cleanup_screen;
714 }
715
716 dpy->Extensions.ANDROID_image_native_buffer = EGL_TRUE;
717 dpy->Extensions.KHR_image_base = EGL_TRUE;
718
719 /* we're supporting EGL 1.4 */
720 dpy->VersionMajor = 1;
721 dpy->VersionMinor = 4;
722
723 droid_init_driver_functions(drv);
724
725 /* Fill vtbl last to prevent accidentally calling virtual function during
726 * initialization.
727 */
728 dri2_dpy->vtbl = &droid_display_vtbl;
729
730 return EGL_TRUE;
731
732 cleanup_screen:
733 dri2_dpy->core->destroyScreen(dri2_dpy->dri_screen);
734 cleanup_driver:
735 dlclose(dri2_dpy->driver);
736 cleanup_driver_name:
737 free(dri2_dpy->driver_name);
738 cleanup_device:
739 close(dri2_dpy->fd);
740 cleanup_display:
741 free(dri2_dpy);
742
743 return _eglError(EGL_NOT_INITIALIZED, err);
744 }