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