egl/dri2/android: free driver_name in dri2_initialize_android error path
[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 "gralloc_drm.h"
40
41 static int
42 get_format_bpp(int native)
43 {
44 int bpp;
45
46 switch (native) {
47 case HAL_PIXEL_FORMAT_RGBA_8888:
48 case HAL_PIXEL_FORMAT_RGBX_8888:
49 case HAL_PIXEL_FORMAT_BGRA_8888:
50 bpp = 4;
51 break;
52 case HAL_PIXEL_FORMAT_RGB_888:
53 bpp = 3;
54 break;
55 case HAL_PIXEL_FORMAT_RGB_565:
56 case HAL_PIXEL_FORMAT_RGBA_5551:
57 case HAL_PIXEL_FORMAT_RGBA_4444:
58 bpp = 2;
59 break;
60 default:
61 bpp = 0;
62 break;
63 }
64
65 return bpp;
66 }
67
68 static int
69 get_native_buffer_name(struct ANativeWindowBuffer *buf)
70 {
71 return gralloc_drm_get_gem_handle(buf->handle);
72 }
73
74 static EGLBoolean
75 droid_window_dequeue_buffer(struct dri2_egl_surface *dri2_surf)
76 {
77 #if ANDROID_VERSION >= 0x0402
78 int fence_fd;
79
80 if (dri2_surf->window->dequeueBuffer(dri2_surf->window, &dri2_surf->buffer,
81 &fence_fd))
82 return EGL_FALSE;
83
84 /* If access to the buffer is controlled by a sync fence, then block on the
85 * fence.
86 *
87 * It may be more performant to postpone blocking until there is an
88 * immediate need to write to the buffer. But doing so would require adding
89 * hooks to the DRI2 loader.
90 *
91 * From the ANativeWindow::dequeueBuffer documentation:
92 *
93 * The libsync fence file descriptor returned in the int pointed to by
94 * the fenceFd argument will refer to the fence that must signal
95 * before the dequeued buffer may be written to. A value of -1
96 * indicates that the caller may access the buffer immediately without
97 * waiting on a fence. If a valid file descriptor is returned (i.e.
98 * any value except -1) then the caller is responsible for closing the
99 * file descriptor.
100 */
101 if (fence_fd >= 0) {
102 /* From the SYNC_IOC_WAIT documentation in <linux/sync.h>:
103 *
104 * Waits indefinitely if timeout < 0.
105 */
106 int timeout = -1;
107 sync_wait(fence_fd, timeout);
108 close(fence_fd);
109 }
110
111 dri2_surf->buffer->common.incRef(&dri2_surf->buffer->common);
112 #else
113 if (dri2_surf->window->dequeueBuffer(dri2_surf->window, &dri2_surf->buffer))
114 return EGL_FALSE;
115
116 dri2_surf->buffer->common.incRef(&dri2_surf->buffer->common);
117 dri2_surf->window->lockBuffer(dri2_surf->window, dri2_surf->buffer);
118 #endif
119
120 return EGL_TRUE;
121 }
122
123 static EGLBoolean
124 droid_window_enqueue_buffer(struct dri2_egl_surface *dri2_surf)
125 {
126 #if ANDROID_VERSION >= 0x0402
127 /* Queue the buffer without a sync fence. This informs the ANativeWindow
128 * that it may access the buffer immediately.
129 *
130 * From ANativeWindow::dequeueBuffer:
131 *
132 * The fenceFd argument specifies a libsync fence file descriptor for
133 * a fence that must signal before the buffer can be accessed. If
134 * the buffer can be accessed immediately then a value of -1 should
135 * be used. The caller must not use the file descriptor after it
136 * is passed to queueBuffer, and the ANativeWindow implementation
137 * is responsible for closing it.
138 */
139 int fence_fd = -1;
140 dri2_surf->window->queueBuffer(dri2_surf->window, dri2_surf->buffer,
141 fence_fd);
142 #else
143 dri2_surf->window->queueBuffer(dri2_surf->window, dri2_surf->buffer);
144 #endif
145
146 dri2_surf->buffer->common.decRef(&dri2_surf->buffer->common);
147 dri2_surf->buffer = NULL;
148
149 return EGL_TRUE;
150 }
151
152 static void
153 droid_window_cancel_buffer(struct dri2_egl_surface *dri2_surf)
154 {
155 /* no cancel buffer? */
156 droid_window_enqueue_buffer(dri2_surf);
157 }
158
159 static __DRIbuffer *
160 droid_alloc_local_buffer(struct dri2_egl_surface *dri2_surf,
161 unsigned int att, unsigned int format)
162 {
163 struct dri2_egl_display *dri2_dpy =
164 dri2_egl_display(dri2_surf->base.Resource.Display);
165
166 if (att >= ARRAY_SIZE(dri2_surf->local_buffers))
167 return NULL;
168
169 if (!dri2_surf->local_buffers[att]) {
170 dri2_surf->local_buffers[att] =
171 dri2_dpy->dri2->allocateBuffer(dri2_dpy->dri_screen, att, format,
172 dri2_surf->base.Width, dri2_surf->base.Height);
173 }
174
175 return dri2_surf->local_buffers[att];
176 }
177
178 static void
179 droid_free_local_buffers(struct dri2_egl_surface *dri2_surf)
180 {
181 struct dri2_egl_display *dri2_dpy =
182 dri2_egl_display(dri2_surf->base.Resource.Display);
183 int i;
184
185 for (i = 0; i < ARRAY_SIZE(dri2_surf->local_buffers); i++) {
186 if (dri2_surf->local_buffers[i]) {
187 dri2_dpy->dri2->releaseBuffer(dri2_dpy->dri_screen,
188 dri2_surf->local_buffers[i]);
189 dri2_surf->local_buffers[i] = NULL;
190 }
191 }
192 }
193
194 static _EGLSurface *
195 droid_create_surface(_EGLDriver *drv, _EGLDisplay *disp, EGLint type,
196 _EGLConfig *conf, EGLNativeWindowType window,
197 const EGLint *attrib_list)
198 {
199 struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);
200 struct dri2_egl_config *dri2_conf = dri2_egl_config(conf);
201 struct dri2_egl_surface *dri2_surf;
202 dri2_surf = calloc(1, sizeof *dri2_surf);
203 if (!dri2_surf) {
204 _eglError(EGL_BAD_ALLOC, "droid_create_surface");
205 return NULL;
206 }
207
208 if (!_eglInitSurface(&dri2_surf->base, disp, type, conf, attrib_list))
209 goto cleanup_surface;
210
211 if (type == EGL_WINDOW_BIT) {
212 int format;
213
214 if (!window || window->common.magic != ANDROID_NATIVE_WINDOW_MAGIC) {
215 _eglError(EGL_BAD_NATIVE_WINDOW, "droid_create_surface");
216 goto cleanup_surface;
217 }
218 if (window->query(window, NATIVE_WINDOW_FORMAT, &format)) {
219 _eglError(EGL_BAD_NATIVE_WINDOW, "droid_create_surface");
220 goto cleanup_surface;
221 }
222
223 if (format != dri2_conf->base.NativeVisualID) {
224 _eglLog(_EGL_WARNING, "Native format mismatch: 0x%x != 0x%x",
225 format, dri2_conf->base.NativeVisualID);
226 }
227
228 window->query(window, NATIVE_WINDOW_WIDTH, &dri2_surf->base.Width);
229 window->query(window, NATIVE_WINDOW_HEIGHT, &dri2_surf->base.Height);
230 }
231
232 dri2_surf->dri_drawable =
233 (*dri2_dpy->dri2->createNewDrawable)(dri2_dpy->dri_screen,
234 dri2_conf->dri_double_config,
235 dri2_surf);
236 if (dri2_surf->dri_drawable == NULL) {
237 _eglError(EGL_BAD_ALLOC, "dri2->createNewDrawable");
238 goto cleanup_surface;
239 }
240
241 if (window) {
242 window->common.incRef(&window->common);
243 dri2_surf->window = window;
244 }
245
246 return &dri2_surf->base;
247
248 cleanup_surface:
249 free(dri2_surf);
250
251 return NULL;
252 }
253
254 static _EGLSurface *
255 droid_create_window_surface(_EGLDriver *drv, _EGLDisplay *disp,
256 _EGLConfig *conf, EGLNativeWindowType window,
257 const EGLint *attrib_list)
258 {
259 return droid_create_surface(drv, disp, EGL_WINDOW_BIT, conf,
260 window, attrib_list);
261 }
262
263 static _EGLSurface *
264 droid_create_pixmap_surface(_EGLDriver *drv, _EGLDisplay *disp,
265 _EGLConfig *conf, EGLNativePixmapType pixmap,
266 const EGLint *attrib_list)
267 {
268 return NULL;
269 }
270
271 static _EGLSurface *
272 droid_create_pbuffer_surface(_EGLDriver *drv, _EGLDisplay *disp,
273 _EGLConfig *conf, const EGLint *attrib_list)
274 {
275 return droid_create_surface(drv, disp, EGL_PBUFFER_BIT, conf,
276 NULL, attrib_list);
277 }
278
279 static EGLBoolean
280 droid_destroy_surface(_EGLDriver *drv, _EGLDisplay *disp, _EGLSurface *surf)
281 {
282 struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);
283 struct dri2_egl_surface *dri2_surf = dri2_egl_surface(surf);
284
285 if (!_eglPutSurface(surf))
286 return EGL_TRUE;
287
288 droid_free_local_buffers(dri2_surf);
289
290 if (dri2_surf->base.Type == EGL_WINDOW_BIT) {
291 if (dri2_surf->buffer)
292 droid_window_cancel_buffer(dri2_surf);
293
294 dri2_surf->window->common.decRef(&dri2_surf->window->common);
295 }
296
297 (*dri2_dpy->core->destroyDrawable)(dri2_surf->dri_drawable);
298
299 free(dri2_surf);
300
301 return EGL_TRUE;
302 }
303
304 static EGLBoolean
305 droid_swap_buffers(_EGLDriver *drv, _EGLDisplay *disp, _EGLSurface *draw)
306 {
307 struct dri2_egl_driver *dri2_drv = dri2_egl_driver(drv);
308 struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);
309 struct dri2_egl_surface *dri2_surf = dri2_egl_surface(draw);
310 _EGLContext *ctx;
311
312 if (dri2_surf->base.Type != EGL_WINDOW_BIT)
313 return EGL_TRUE;
314
315 if (dri2_drv->glFlush) {
316 ctx = _eglGetCurrentContext();
317 if (ctx && ctx->DrawSurface == &dri2_surf->base)
318 dri2_drv->glFlush();
319 }
320
321 (*dri2_dpy->flush->flush)(dri2_surf->dri_drawable);
322
323 if (dri2_surf->buffer)
324 droid_window_enqueue_buffer(dri2_surf);
325
326 (*dri2_dpy->flush->invalidate)(dri2_surf->dri_drawable);
327
328 return EGL_TRUE;
329 }
330
331 static _EGLImage *
332 dri2_create_image_android_native_buffer(_EGLDisplay *disp, _EGLContext *ctx,
333 struct ANativeWindowBuffer *buf)
334 {
335 struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);
336 struct dri2_egl_image *dri2_img;
337 int name;
338 EGLint format;
339
340 if (ctx != NULL) {
341 /* From the EGL_ANDROID_image_native_buffer spec:
342 *
343 * * If <target> is EGL_NATIVE_BUFFER_ANDROID and <ctx> is not
344 * EGL_NO_CONTEXT, the error EGL_BAD_CONTEXT is generated.
345 */
346 _eglError(EGL_BAD_CONTEXT, "eglCreateEGLImageKHR: for "
347 "EGL_NATIVE_BUFFER_ANDROID, the context must be "
348 "EGL_NO_CONTEXT");
349 return NULL;
350 }
351
352 if (!buf || buf->common.magic != ANDROID_NATIVE_BUFFER_MAGIC ||
353 buf->common.version != sizeof(*buf)) {
354 _eglError(EGL_BAD_PARAMETER, "eglCreateEGLImageKHR");
355 return NULL;
356 }
357
358 name = get_native_buffer_name(buf);
359 if (!name) {
360 _eglError(EGL_BAD_PARAMETER, "eglCreateEGLImageKHR");
361 return NULL;
362 }
363
364 /* see the table in droid_add_configs_for_visuals */
365 switch (buf->format) {
366 case HAL_PIXEL_FORMAT_BGRA_8888:
367 format = __DRI_IMAGE_FORMAT_ARGB8888;
368 break;
369 case HAL_PIXEL_FORMAT_RGB_565:
370 format = __DRI_IMAGE_FORMAT_RGB565;
371 break;
372 case HAL_PIXEL_FORMAT_RGBA_8888:
373 format = __DRI_IMAGE_FORMAT_ABGR8888;
374 break;
375 case HAL_PIXEL_FORMAT_RGBX_8888:
376 format = __DRI_IMAGE_FORMAT_XBGR8888;
377 break;
378 case HAL_PIXEL_FORMAT_RGB_888:
379 case HAL_PIXEL_FORMAT_RGBA_5551:
380 case HAL_PIXEL_FORMAT_RGBA_4444:
381 /* unsupported */
382 default:
383 _eglLog(_EGL_WARNING, "unsupported native buffer format 0x%x", buf->format);
384 return NULL;
385 break;
386 }
387
388 dri2_img = calloc(1, sizeof(*dri2_img));
389 if (!dri2_img) {
390 _eglError(EGL_BAD_ALLOC, "droid_create_image_mesa_drm");
391 return NULL;
392 }
393
394 if (!_eglInitImage(&dri2_img->base, disp)) {
395 free(dri2_img);
396 return NULL;
397 }
398
399 dri2_img->dri_image =
400 dri2_dpy->image->createImageFromName(dri2_dpy->dri_screen,
401 buf->width,
402 buf->height,
403 format,
404 name,
405 buf->stride,
406 dri2_img);
407 if (!dri2_img->dri_image) {
408 free(dri2_img);
409 _eglError(EGL_BAD_ALLOC, "droid_create_image_mesa_drm");
410 return NULL;
411 }
412
413 return &dri2_img->base;
414 }
415
416 static _EGLImage *
417 droid_create_image_khr(_EGLDriver *drv, _EGLDisplay *disp,
418 _EGLContext *ctx, EGLenum target,
419 EGLClientBuffer buffer, const EGLint *attr_list)
420 {
421 switch (target) {
422 case EGL_NATIVE_BUFFER_ANDROID:
423 return dri2_create_image_android_native_buffer(disp, ctx,
424 (struct ANativeWindowBuffer *) buffer);
425 default:
426 return dri2_create_image_khr(drv, disp, ctx, target, buffer, attr_list);
427 }
428 }
429
430 static void
431 droid_init_driver_functions(_EGLDriver *drv)
432 {
433 drv->API.CreateWindowSurface = droid_create_window_surface;
434 drv->API.CreatePixmapSurface = droid_create_pixmap_surface;
435 drv->API.CreatePbufferSurface = droid_create_pbuffer_surface;
436 drv->API.DestroySurface = droid_destroy_surface;
437 drv->API.SwapBuffers = droid_swap_buffers;
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 EGLBoolean
656 dri2_initialize_android(_EGLDriver *drv, _EGLDisplay *dpy)
657 {
658 struct dri2_egl_display *dri2_dpy;
659 const char *err;
660
661 _eglSetLogProc(droid_log);
662
663 loader_set_logger(_eglLog);
664
665 dri2_dpy = calloc(1, sizeof(*dri2_dpy));
666 if (!dri2_dpy)
667 return _eglError(EGL_BAD_ALLOC, "eglInitialize");
668
669 dpy->DriverData = (void *) dri2_dpy;
670
671 dri2_dpy->fd = droid_open_device();
672 if (dri2_dpy->fd < 0) {
673 err = "DRI2: failed to open device";
674 goto cleanup_display;
675 }
676
677 dri2_dpy->driver_name = loader_get_driver_for_fd(dri2_dpy->fd, 0);
678 if (dri2_dpy->driver_name == NULL) {
679 err = "DRI2: failed to get driver name";
680 goto cleanup_device;
681 }
682
683 if (!dri2_load_driver(dpy)) {
684 err = "DRI2: failed to load driver";
685 goto cleanup_driver_name;
686 }
687
688 dri2_dpy->dri2_loader_extension.base.name = __DRI_DRI2_LOADER;
689 dri2_dpy->dri2_loader_extension.base.version = 3;
690 dri2_dpy->dri2_loader_extension.getBuffers = NULL;
691 dri2_dpy->dri2_loader_extension.flushFrontBuffer = droid_flush_front_buffer;
692 dri2_dpy->dri2_loader_extension.getBuffersWithFormat =
693 droid_get_buffers_with_format;
694
695 dri2_dpy->extensions[0] = &dri2_dpy->dri2_loader_extension.base;
696 dri2_dpy->extensions[1] = &image_lookup_extension.base;
697 dri2_dpy->extensions[2] = &use_invalidate.base;
698 dri2_dpy->extensions[3] = NULL;
699
700 if (!dri2_create_screen(dpy)) {
701 err = "DRI2: failed to create screen";
702 goto cleanup_driver;
703 }
704
705 if (!droid_add_configs_for_visuals(drv, dpy)) {
706 err = "DRI2: failed to add configs";
707 goto cleanup_screen;
708 }
709
710 dpy->Extensions.ANDROID_image_native_buffer = EGL_TRUE;
711 dpy->Extensions.KHR_image_base = EGL_TRUE;
712
713 /* we're supporting EGL 1.4 */
714 dpy->VersionMajor = 1;
715 dpy->VersionMinor = 4;
716
717 droid_init_driver_functions(drv);
718
719 return EGL_TRUE;
720
721 cleanup_screen:
722 dri2_dpy->core->destroyScreen(dri2_dpy->dri_screen);
723 cleanup_driver:
724 dlclose(dri2_dpy->driver);
725 cleanup_driver_name:
726 free(dri2_dpy->driver_name);
727 cleanup_device:
728 close(dri2_dpy->fd);
729 cleanup_display:
730 free(dri2_dpy);
731
732 return _eglError(EGL_NOT_INITIALIZED, err);
733 }