egl/dri2: Dispatch eglSwapBuffersWithDamage 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
439 drv->API.CreateImageKHR = droid_create_image_khr;
440 }
441
442 static void
443 droid_flush_front_buffer(__DRIdrawable * driDrawable, void *loaderPrivate)
444 {
445 }
446
447 static int
448 droid_get_buffers_parse_attachments(struct dri2_egl_surface *dri2_surf,
449 unsigned int *attachments, int count)
450 {
451 int num_buffers = 0, i;
452
453 /* fill dri2_surf->buffers */
454 for (i = 0; i < count * 2; i += 2) {
455 __DRIbuffer *buf, *local;
456
457 assert(num_buffers < ARRAY_SIZE(dri2_surf->buffers));
458 buf = &dri2_surf->buffers[num_buffers];
459
460 switch (attachments[i]) {
461 case __DRI_BUFFER_BACK_LEFT:
462 if (dri2_surf->base.Type == EGL_WINDOW_BIT) {
463 buf->attachment = attachments[i];
464 buf->name = get_native_buffer_name(dri2_surf->buffer);
465 buf->cpp = get_format_bpp(dri2_surf->buffer->format);
466 buf->pitch = dri2_surf->buffer->stride * buf->cpp;
467 buf->flags = 0;
468
469 if (buf->name)
470 num_buffers++;
471
472 break;
473 }
474 /* fall through for pbuffers */
475 case __DRI_BUFFER_DEPTH:
476 case __DRI_BUFFER_STENCIL:
477 case __DRI_BUFFER_ACCUM:
478 case __DRI_BUFFER_DEPTH_STENCIL:
479 case __DRI_BUFFER_HIZ:
480 local = droid_alloc_local_buffer(dri2_surf,
481 attachments[i], attachments[i + 1]);
482
483 if (local) {
484 *buf = *local;
485 num_buffers++;
486 }
487 break;
488 case __DRI_BUFFER_FRONT_LEFT:
489 case __DRI_BUFFER_FRONT_RIGHT:
490 case __DRI_BUFFER_FAKE_FRONT_LEFT:
491 case __DRI_BUFFER_FAKE_FRONT_RIGHT:
492 case __DRI_BUFFER_BACK_RIGHT:
493 default:
494 /* no front or right buffers */
495 break;
496 }
497 }
498
499 return num_buffers;
500 }
501
502 static __DRIbuffer *
503 droid_get_buffers_with_format(__DRIdrawable * driDrawable,
504 int *width, int *height,
505 unsigned int *attachments, int count,
506 int *out_count, void *loaderPrivate)
507 {
508 struct dri2_egl_surface *dri2_surf = loaderPrivate;
509 struct dri2_egl_display *dri2_dpy =
510 dri2_egl_display(dri2_surf->base.Resource.Display);
511 int i;
512
513 if (dri2_surf->base.Type == EGL_WINDOW_BIT) {
514 /* try to dequeue the next back buffer */
515 if (!dri2_surf->buffer && !droid_window_dequeue_buffer(dri2_surf))
516 return NULL;
517
518 /* free outdated buffers and update the surface size */
519 if (dri2_surf->base.Width != dri2_surf->buffer->width ||
520 dri2_surf->base.Height != dri2_surf->buffer->height) {
521 droid_free_local_buffers(dri2_surf);
522 dri2_surf->base.Width = dri2_surf->buffer->width;
523 dri2_surf->base.Height = dri2_surf->buffer->height;
524 }
525 }
526
527 dri2_surf->buffer_count =
528 droid_get_buffers_parse_attachments(dri2_surf, attachments, count);
529
530 if (width)
531 *width = dri2_surf->base.Width;
532 if (height)
533 *height = dri2_surf->base.Height;
534
535 *out_count = dri2_surf->buffer_count;;
536
537 return dri2_surf->buffers;
538 }
539
540 static EGLBoolean
541 droid_add_configs_for_visuals(_EGLDriver *drv, _EGLDisplay *dpy)
542 {
543 struct dri2_egl_display *dri2_dpy = dri2_egl_display(dpy);
544 const struct {
545 int format;
546 unsigned int rgba_masks[4];
547 } visuals[] = {
548 { HAL_PIXEL_FORMAT_RGBA_8888, { 0xff, 0xff00, 0xff0000, 0xff000000 } },
549 { HAL_PIXEL_FORMAT_RGBX_8888, { 0xff, 0xff00, 0xff0000, 0x0 } },
550 { HAL_PIXEL_FORMAT_RGB_888, { 0xff, 0xff00, 0xff0000, 0x0 } },
551 { HAL_PIXEL_FORMAT_RGB_565, { 0xf800, 0x7e0, 0x1f, 0x0 } },
552 { HAL_PIXEL_FORMAT_BGRA_8888, { 0xff0000, 0xff00, 0xff, 0xff000000 } },
553 { 0, 0, { 0, 0, 0, 0 } }
554 };
555 int count, i, j;
556
557 count = 0;
558 for (i = 0; visuals[i].format; i++) {
559 int format_count = 0;
560
561 for (j = 0; dri2_dpy->driver_configs[j]; j++) {
562 const EGLint surface_type = EGL_WINDOW_BIT | EGL_PBUFFER_BIT;
563 struct dri2_egl_config *dri2_conf;
564 unsigned int double_buffered = 0;
565
566 dri2_dpy->core->getConfigAttrib(dri2_dpy->driver_configs[j],
567 __DRI_ATTRIB_DOUBLE_BUFFER, &double_buffered);
568
569 /* support only double buffered configs */
570 if (!double_buffered)
571 continue;
572
573 dri2_conf = dri2_add_config(dpy, dri2_dpy->driver_configs[j],
574 count + 1, surface_type, NULL, visuals[i].rgba_masks);
575 if (dri2_conf) {
576 dri2_conf->base.NativeVisualID = visuals[i].format;
577 dri2_conf->base.NativeVisualType = visuals[i].format;
578 count++;
579 format_count++;
580 }
581 }
582
583 if (!format_count) {
584 _eglLog(_EGL_DEBUG, "No DRI config supports native format 0x%x",
585 visuals[i].format);
586 }
587 }
588
589 /* post-process configs */
590 for (i = 0; i < dpy->Configs->Size; i++) {
591 struct dri2_egl_config *dri2_conf = dri2_egl_config(dpy->Configs->Elements[i]);
592
593 /* there is no front buffer so no OpenGL */
594 dri2_conf->base.RenderableType &= ~EGL_OPENGL_BIT;
595 dri2_conf->base.Conformant &= ~EGL_OPENGL_BIT;
596 }
597
598 return (count != 0);
599 }
600
601 static int
602 droid_open_device(void)
603 {
604 const hw_module_t *mod;
605 int fd = -1, err;
606
607 err = hw_get_module(GRALLOC_HARDWARE_MODULE_ID, &mod);
608 if (!err) {
609 const gralloc_module_t *gr = (gralloc_module_t *) mod;
610
611 err = -EINVAL;
612 if (gr->perform)
613 err = gr->perform(gr, GRALLOC_MODULE_PERFORM_GET_DRM_FD, &fd);
614 }
615 if (err || fd < 0) {
616 _eglLog(_EGL_WARNING, "fail to get drm fd");
617 fd = -1;
618 }
619
620 return (fd >= 0) ? dup(fd) : -1;
621 }
622
623 /* support versions < JellyBean */
624 #ifndef ALOGW
625 #define ALOGW LOGW
626 #endif
627 #ifndef ALOGD
628 #define ALOGD LOGD
629 #endif
630 #ifndef ALOGI
631 #define ALOGI LOGI
632 #endif
633
634 static void
635 droid_log(EGLint level, const char *msg)
636 {
637 switch (level) {
638 case _EGL_DEBUG:
639 ALOGD("%s", msg);
640 break;
641 case _EGL_INFO:
642 ALOGI("%s", msg);
643 break;
644 case _EGL_WARNING:
645 ALOGW("%s", msg);
646 break;
647 case _EGL_FATAL:
648 LOG_FATAL("%s", msg);
649 break;
650 default:
651 break;
652 }
653 }
654
655 static struct dri2_egl_display_vtbl droid_display_vtbl = {
656 .authenticate = NULL,
657 .swap_interval = dri2_fallback_swap_interval,
658 .swap_buffers = droid_swap_buffers,
659 .swap_buffers_with_damage = dri2_fallback_swap_buffers_with_damage,
660 };
661
662 EGLBoolean
663 dri2_initialize_android(_EGLDriver *drv, _EGLDisplay *dpy)
664 {
665 struct dri2_egl_display *dri2_dpy;
666 const char *err;
667
668 _eglSetLogProc(droid_log);
669
670 loader_set_logger(_eglLog);
671
672 dri2_dpy = calloc(1, sizeof(*dri2_dpy));
673 if (!dri2_dpy)
674 return _eglError(EGL_BAD_ALLOC, "eglInitialize");
675
676 dpy->DriverData = (void *) dri2_dpy;
677
678 dri2_dpy->fd = droid_open_device();
679 if (dri2_dpy->fd < 0) {
680 err = "DRI2: failed to open device";
681 goto cleanup_display;
682 }
683
684 dri2_dpy->driver_name = loader_get_driver_for_fd(dri2_dpy->fd, 0);
685 if (dri2_dpy->driver_name == NULL) {
686 err = "DRI2: failed to get driver name";
687 goto cleanup_device;
688 }
689
690 if (!dri2_load_driver(dpy)) {
691 err = "DRI2: failed to load driver";
692 goto cleanup_driver_name;
693 }
694
695 dri2_dpy->dri2_loader_extension.base.name = __DRI_DRI2_LOADER;
696 dri2_dpy->dri2_loader_extension.base.version = 3;
697 dri2_dpy->dri2_loader_extension.getBuffers = NULL;
698 dri2_dpy->dri2_loader_extension.flushFrontBuffer = droid_flush_front_buffer;
699 dri2_dpy->dri2_loader_extension.getBuffersWithFormat =
700 droid_get_buffers_with_format;
701
702 dri2_dpy->extensions[0] = &dri2_dpy->dri2_loader_extension.base;
703 dri2_dpy->extensions[1] = &image_lookup_extension.base;
704 dri2_dpy->extensions[2] = &use_invalidate.base;
705 dri2_dpy->extensions[3] = NULL;
706
707 if (!dri2_create_screen(dpy)) {
708 err = "DRI2: failed to create screen";
709 goto cleanup_driver;
710 }
711
712 if (!droid_add_configs_for_visuals(drv, dpy)) {
713 err = "DRI2: failed to add configs";
714 goto cleanup_screen;
715 }
716
717 dpy->Extensions.ANDROID_image_native_buffer = EGL_TRUE;
718 dpy->Extensions.KHR_image_base = EGL_TRUE;
719
720 /* we're supporting EGL 1.4 */
721 dpy->VersionMajor = 1;
722 dpy->VersionMinor = 4;
723
724 droid_init_driver_functions(drv);
725
726 /* Fill vtbl last to prevent accidentally calling virtual function during
727 * initialization.
728 */
729 dri2_dpy->vtbl = &droid_display_vtbl;
730
731 return EGL_TRUE;
732
733 cleanup_screen:
734 dri2_dpy->core->destroyScreen(dri2_dpy->dri_screen);
735 cleanup_driver:
736 dlclose(dri2_dpy->driver);
737 cleanup_driver_name:
738 free(dri2_dpy->driver_name);
739 cleanup_device:
740 close(dri2_dpy->fd);
741 cleanup_display:
742 free(dri2_dpy);
743
744 return _eglError(EGL_NOT_INITIALIZED, err);
745 }