blob: 2320fb6f214968cd090e75e4d5f1c41b525889ae [file] [log] [blame]
Chia-I Wu9779f6f2011-08-05 14:39:18 +09001/*
2 * Mesa 3-D graphics library
Chia-I Wu9779f6f2011-08-05 14:39:18 +09003 *
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>
Matt Whitlockc8fd7d02016-10-01 23:49:41 -040032#include <fcntl.h>
Rob Herringd45884e2016-04-28 15:37:28 -050033#include <xf86drm.h>
Tomasz Figa51727b12016-11-10 16:55:53 +090034#include <stdbool.h>
Chad Versacebfe28b82012-12-20 14:16:50 -080035#include <sync/sync.h>
Chad Versacebfe28b82012-12-20 14:16:50 -080036
Emil Velikov8d4357b2014-01-11 04:52:48 +000037#include "loader.h"
Chia-I Wu9779f6f2011-08-05 14:39:18 +090038#include "egl_dri2.h"
Chad Versace8b9298a2014-01-28 12:34:19 -080039#include "egl_dri2_fallbacks.h"
Tapani Pälli40702952013-01-24 09:56:47 +020040#include "gralloc_drm.h"
Chia-I Wu9779f6f2011-08-05 14:39:18 +090041
Tomasz Figa3723e982016-08-02 20:07:54 +090042#define ALIGN(val, align) (((val) + (align) - 1) & ~((align) - 1))
43
Tomasz Figa51727b12016-11-10 16:55:53 +090044struct 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. */
56static 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 },
Tomasz Figa5364e732017-12-04 19:22:39 +010062 /* HACK: See droid_create_image_from_prime_fd() and
63 * https://issuetracker.google.com/32077885. */
64 { HAL_PIXEL_FORMAT_IMPLEMENTATION_DEFINED, 0, 2, __DRI_IMAGE_FOURCC_NV12 },
65 { HAL_PIXEL_FORMAT_IMPLEMENTATION_DEFINED, 0, 1, __DRI_IMAGE_FOURCC_YUV420 },
66 { HAL_PIXEL_FORMAT_IMPLEMENTATION_DEFINED, 1, 1, __DRI_IMAGE_FOURCC_YVU420 },
Tomasz Figa51727b12016-11-10 16:55:53 +090067};
68
69static int
70get_fourcc_yuv(int native, int is_ycrcb, int chroma_step)
71{
Chad Versace09455122017-06-22 11:00:40 -070072 for (int i = 0; i < ARRAY_SIZE(droid_yuv_formats); ++i)
Tomasz Figa51727b12016-11-10 16:55:53 +090073 if (droid_yuv_formats[i].native == native &&
74 droid_yuv_formats[i].is_ycrcb == is_ycrcb &&
75 droid_yuv_formats[i].chroma_step == chroma_step)
76 return droid_yuv_formats[i].fourcc;
77
78 return -1;
79}
80
81static bool
82is_yuv(int native)
83{
Chad Versace09455122017-06-22 11:00:40 -070084 for (int i = 0; i < ARRAY_SIZE(droid_yuv_formats); ++i)
Tomasz Figa51727b12016-11-10 16:55:53 +090085 if (droid_yuv_formats[i].native == native)
86 return true;
87
88 return false;
89}
90
Chia-I Wu9779f6f2011-08-05 14:39:18 +090091static int
92get_format_bpp(int native)
93{
94 int bpp;
95
96 switch (native) {
97 case HAL_PIXEL_FORMAT_RGBA_8888:
Tomasz Figa5364e732017-12-04 19:22:39 +010098 case HAL_PIXEL_FORMAT_IMPLEMENTATION_DEFINED:
99 /*
100 * HACK: Hardcode this to RGBX_8888 as per cros_gralloc hack.
101 * TODO: Remove this once https://issuetracker.google.com/32077885 is fixed.
102 */
Chia-I Wu9779f6f2011-08-05 14:39:18 +0900103 case HAL_PIXEL_FORMAT_RGBX_8888:
104 case HAL_PIXEL_FORMAT_BGRA_8888:
105 bpp = 4;
106 break;
Chia-I Wu9779f6f2011-08-05 14:39:18 +0900107 case HAL_PIXEL_FORMAT_RGB_565:
Chia-I Wu9779f6f2011-08-05 14:39:18 +0900108 bpp = 2;
109 break;
110 default:
111 bpp = 0;
112 break;
113 }
114
115 return bpp;
116}
117
Rob Herring34ddef32016-05-01 09:35:28 +0100118/* createImageFromFds requires fourcc format */
Tomasz Figa7dfb1a42016-08-02 20:07:53 +0900119static int get_fourcc(int native)
Rob Herring34ddef32016-05-01 09:35:28 +0100120{
Tomasz Figa7dfb1a42016-08-02 20:07:53 +0900121 switch (native) {
122 case HAL_PIXEL_FORMAT_RGB_565: return __DRI_IMAGE_FOURCC_RGB565;
123 case HAL_PIXEL_FORMAT_BGRA_8888: return __DRI_IMAGE_FOURCC_ARGB8888;
124 case HAL_PIXEL_FORMAT_RGBA_8888: return __DRI_IMAGE_FOURCC_ABGR8888;
Tomasz Figa5364e732017-12-04 19:22:39 +0100125 case HAL_PIXEL_FORMAT_IMPLEMENTATION_DEFINED:
126 /*
127 * HACK: Hardcode this to RGBX_8888 as per cros_gralloc hack.
128 * TODO: Remove this once https://issuetracker.google.com/32077885 is fixed.
129 */
Tomasz Figa7dfb1a42016-08-02 20:07:53 +0900130 case HAL_PIXEL_FORMAT_RGBX_8888: return __DRI_IMAGE_FOURCC_XBGR8888;
131 default:
132 _eglLog(_EGL_WARNING, "unsupported native buffer format 0x%x", native);
Rob Herring34ddef32016-05-01 09:35:28 +0100133 }
134 return -1;
135}
136
Rob Herringdfaccf22016-04-28 15:37:29 -0500137static int get_format(int format)
138{
139 switch (format) {
140 case HAL_PIXEL_FORMAT_BGRA_8888: return __DRI_IMAGE_FORMAT_ARGB8888;
141 case HAL_PIXEL_FORMAT_RGB_565: return __DRI_IMAGE_FORMAT_RGB565;
142 case HAL_PIXEL_FORMAT_RGBA_8888: return __DRI_IMAGE_FORMAT_ABGR8888;
Tomasz Figa5364e732017-12-04 19:22:39 +0100143 case HAL_PIXEL_FORMAT_IMPLEMENTATION_DEFINED:
144 /*
145 * HACK: Hardcode this to RGBX_8888 as per cros_gralloc hack.
146 * TODO: Revert this once https://issuetracker.google.com/32077885 is fixed.
147 */
Rob Herringdfaccf22016-04-28 15:37:29 -0500148 case HAL_PIXEL_FORMAT_RGBX_8888: return __DRI_IMAGE_FORMAT_XBGR8888;
Rob Herringdfaccf22016-04-28 15:37:29 -0500149 default:
150 _eglLog(_EGL_WARNING, "unsupported native buffer format 0x%x", format);
151 }
152 return -1;
153}
Tomasz Figa7dfb1a42016-08-02 20:07:53 +0900154
Chia-I Wu9779f6f2011-08-05 14:39:18 +0900155static int
Rob Herring34ddef32016-05-01 09:35:28 +0100156get_native_buffer_fd(struct ANativeWindowBuffer *buf)
157{
158 native_handle_t *handle = (native_handle_t *)buf->handle;
159 /*
160 * Various gralloc implementations exist, but the dma-buf fd tends
161 * to be first. Access it directly to avoid a dependency on specific
162 * gralloc versions.
163 */
164 return (handle && handle->numFds) ? handle->data[0] : -1;
165}
166
167static int
Chia-I Wu75cc24c2011-11-25 11:59:02 +0800168get_native_buffer_name(struct ANativeWindowBuffer *buf)
Chia-I Wu9779f6f2011-08-05 14:39:18 +0900169{
Tapani Pälli40702952013-01-24 09:56:47 +0200170 return gralloc_drm_get_gem_handle(buf->handle);
Chia-I Wu9779f6f2011-08-05 14:39:18 +0900171}
172
173static EGLBoolean
174droid_window_dequeue_buffer(struct dri2_egl_surface *dri2_surf)
175{
Chad Versacebfe28b82012-12-20 14:16:50 -0800176 int fence_fd;
177
178 if (dri2_surf->window->dequeueBuffer(dri2_surf->window, &dri2_surf->buffer,
179 &fence_fd))
180 return EGL_FALSE;
181
182 /* If access to the buffer is controlled by a sync fence, then block on the
183 * fence.
184 *
185 * It may be more performant to postpone blocking until there is an
186 * immediate need to write to the buffer. But doing so would require adding
187 * hooks to the DRI2 loader.
188 *
189 * From the ANativeWindow::dequeueBuffer documentation:
190 *
191 * The libsync fence file descriptor returned in the int pointed to by
192 * the fenceFd argument will refer to the fence that must signal
193 * before the dequeued buffer may be written to. A value of -1
194 * indicates that the caller may access the buffer immediately without
195 * waiting on a fence. If a valid file descriptor is returned (i.e.
196 * any value except -1) then the caller is responsible for closing the
197 * file descriptor.
198 */
199 if (fence_fd >= 0) {
200 /* From the SYNC_IOC_WAIT documentation in <linux/sync.h>:
201 *
202 * Waits indefinitely if timeout < 0.
203 */
204 int timeout = -1;
205 sync_wait(fence_fd, timeout);
206 close(fence_fd);
207 }
208
209 dri2_surf->buffer->common.incRef(&dri2_surf->buffer->common);
Chia-I Wu9779f6f2011-08-05 14:39:18 +0900210
Xiaosong Wei2acc69d2017-02-08 10:46:02 +0800211 /* Record all the buffers created by ANativeWindow and update back buffer
212 * for updating buffer's age in swap_buffers.
213 */
214 EGLBoolean updated = EGL_FALSE;
215 for (int i = 0; i < ARRAY_SIZE(dri2_surf->color_buffers); i++) {
216 if (!dri2_surf->color_buffers[i].buffer) {
217 dri2_surf->color_buffers[i].buffer = dri2_surf->buffer;
218 }
219 if (dri2_surf->color_buffers[i].buffer == dri2_surf->buffer) {
220 dri2_surf->back = &dri2_surf->color_buffers[i];
221 updated = EGL_TRUE;
222 break;
223 }
224 }
225
226 if (!updated) {
227 /* In case of all the buffers were recreated by ANativeWindow, reset
228 * the color_buffers
229 */
230 for (int i = 0; i < ARRAY_SIZE(dri2_surf->color_buffers); i++) {
231 dri2_surf->color_buffers[i].buffer = NULL;
232 dri2_surf->color_buffers[i].age = 0;
233 }
234 dri2_surf->color_buffers[0].buffer = dri2_surf->buffer;
235 dri2_surf->back = &dri2_surf->color_buffers[0];
236 }
237
Chia-I Wu9779f6f2011-08-05 14:39:18 +0900238 return EGL_TRUE;
239}
240
241static EGLBoolean
Haixia Shi1ea233c2016-06-02 12:48:23 -0700242droid_window_enqueue_buffer(_EGLDisplay *disp, struct dri2_egl_surface *dri2_surf)
Chia-I Wu9779f6f2011-08-05 14:39:18 +0900243{
Tomasz Figa9e1248d2016-07-15 16:53:50 +0900244 struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);
245
Haixia Shi1ea233c2016-06-02 12:48:23 -0700246 /* To avoid blocking other EGL calls, release the display mutex before
247 * we enter droid_window_enqueue_buffer() and re-acquire the mutex upon
248 * return.
249 */
250 mtx_unlock(&disp->Mutex);
251
Zhongmin Wu7343d272017-09-15 18:32:43 +0100252 /* Queue the buffer with stored out fence fd. The ANativeWindow or buffer
253 * consumer may choose to wait for the fence to signal before accessing
254 * it. If fence fd value is -1, buffer can be accessed by consumer
255 * immediately. Consumer or application shouldn't rely on timestamp
256 * associated with fence if the fence fd is -1.
Chad Versacebfe28b82012-12-20 14:16:50 -0800257 *
Zhongmin Wu7343d272017-09-15 18:32:43 +0100258 * Ownership of fd is transferred to consumer after queueBuffer and the
259 * consumer is responsible for closing it. Caller must not use the fd
260 * after passing it to queueBuffer.
Chad Versacebfe28b82012-12-20 14:16:50 -0800261 */
Zhongmin Wu7343d272017-09-15 18:32:43 +0100262 int fence_fd = dri2_surf->out_fence_fd;
263 dri2_surf->out_fence_fd = -1;
Chad Versacebfe28b82012-12-20 14:16:50 -0800264 dri2_surf->window->queueBuffer(dri2_surf->window, dri2_surf->buffer,
265 fence_fd);
Chia-I Wu9779f6f2011-08-05 14:39:18 +0900266
267 dri2_surf->buffer->common.decRef(&dri2_surf->buffer->common);
268 dri2_surf->buffer = NULL;
Xiaosong Wei2acc69d2017-02-08 10:46:02 +0800269 dri2_surf->back = NULL;
Chia-I Wu9779f6f2011-08-05 14:39:18 +0900270
Haixia Shi1ea233c2016-06-02 12:48:23 -0700271 mtx_lock(&disp->Mutex);
Tomasz Figa9e1248d2016-07-15 16:53:50 +0900272
Liu Zhiquanb6637532016-11-16 10:11:28 +0800273 if (dri2_surf->dri_image_back) {
274 dri2_dpy->image->destroyImage(dri2_surf->dri_image_back);
275 dri2_surf->dri_image_back = NULL;
Tomasz Figa9e1248d2016-07-15 16:53:50 +0900276 }
277
Chia-I Wu9779f6f2011-08-05 14:39:18 +0900278 return EGL_TRUE;
279}
280
281static void
Chad Versace0212db32017-05-04 17:46:33 -0700282droid_window_cancel_buffer(struct dri2_egl_surface *dri2_surf)
Chia-I Wu9779f6f2011-08-05 14:39:18 +0900283{
Chad Versace0212db32017-05-04 17:46:33 -0700284 int ret;
Zhongmin Wu7343d272017-09-15 18:32:43 +0100285 int fence_fd = dri2_surf->out_fence_fd;
Chad Versace0212db32017-05-04 17:46:33 -0700286
Zhongmin Wu7343d272017-09-15 18:32:43 +0100287 dri2_surf->out_fence_fd = -1;
288 ret = dri2_surf->window->cancelBuffer(dri2_surf->window,
289 dri2_surf->buffer, fence_fd);
Chad Versace0212db32017-05-04 17:46:33 -0700290 if (ret < 0) {
291 _eglLog(_EGL_WARNING, "ANativeWindow::cancelBuffer failed");
Nicolas Boichat63b12b02017-05-05 10:43:50 +0800292 dri2_surf->base.Lost = EGL_TRUE;
Chad Versace0212db32017-05-04 17:46:33 -0700293 }
Chia-I Wu9779f6f2011-08-05 14:39:18 +0900294}
295
Chia-I Wu9779f6f2011-08-05 14:39:18 +0900296static _EGLSurface *
297droid_create_surface(_EGLDriver *drv, _EGLDisplay *disp, EGLint type,
Chad Versace6d1f83e2014-01-07 14:54:51 -0800298 _EGLConfig *conf, void *native_window,
Chia-I Wu9779f6f2011-08-05 14:39:18 +0900299 const EGLint *attrib_list)
300{
Emil Velikovacf125e2017-05-08 18:39:12 +0100301 __DRIcreateNewDrawableFunc createNewDrawable;
Chia-I Wu9779f6f2011-08-05 14:39:18 +0900302 struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);
303 struct dri2_egl_config *dri2_conf = dri2_egl_config(conf);
304 struct dri2_egl_surface *dri2_surf;
Chad Versace6d1f83e2014-01-07 14:54:51 -0800305 struct ANativeWindow *window = native_window;
Marek Olšákc2c2e9a2015-06-10 02:49:29 +0200306 const __DRIconfig *config;
Chad Versace6d1f83e2014-01-07 14:54:51 -0800307
Chia-I Wu9779f6f2011-08-05 14:39:18 +0900308 dri2_surf = calloc(1, sizeof *dri2_surf);
309 if (!dri2_surf) {
310 _eglError(EGL_BAD_ALLOC, "droid_create_surface");
311 return NULL;
312 }
313
Zhongmin Wu7343d272017-09-15 18:32:43 +0100314 if (!dri2_init_surface(&dri2_surf->base, disp, type, conf, attrib_list, true))
Chia-I Wuc8e18f82011-08-26 22:09:56 +0800315 goto cleanup_surface;
316
317 if (type == EGL_WINDOW_BIT) {
318 int format;
319
Emil Velikov92b23682017-08-05 00:25:47 +0100320 if (window->common.magic != ANDROID_NATIVE_WINDOW_MAGIC) {
Chia-I Wuc8e18f82011-08-26 22:09:56 +0800321 _eglError(EGL_BAD_NATIVE_WINDOW, "droid_create_surface");
322 goto cleanup_surface;
323 }
324 if (window->query(window, NATIVE_WINDOW_FORMAT, &format)) {
325 _eglError(EGL_BAD_NATIVE_WINDOW, "droid_create_surface");
326 goto cleanup_surface;
327 }
328
329 if (format != dri2_conf->base.NativeVisualID) {
330 _eglLog(_EGL_WARNING, "Native format mismatch: 0x%x != 0x%x",
331 format, dri2_conf->base.NativeVisualID);
332 }
333
334 window->query(window, NATIVE_WINDOW_WIDTH, &dri2_surf->base.Width);
335 window->query(window, NATIVE_WINDOW_HEIGHT, &dri2_surf->base.Height);
336 }
Chia-I Wu9779f6f2011-08-05 14:39:18 +0900337
Liu Zhiquanb6637532016-11-16 10:11:28 +0800338 config = dri2_get_dri_config(dri2_conf, type,
Marek Olšákc2c2e9a2015-06-10 02:49:29 +0200339 dri2_surf->base.GLColorspace);
Tomasz Figa94282b62016-07-15 16:53:48 +0900340 if (!config)
341 goto cleanup_surface;
Marek Olšákc2c2e9a2015-06-10 02:49:29 +0200342
Emil Velikovacf125e2017-05-08 18:39:12 +0100343 if (dri2_dpy->image_driver)
344 createNewDrawable = dri2_dpy->image_driver->createNewDrawable;
345 else
346 createNewDrawable = dri2_dpy->dri2->createNewDrawable;
347
348 dri2_surf->dri_drawable = (*createNewDrawable)(dri2_dpy->dri_screen, config,
349 dri2_surf);
Chia-I Wu9779f6f2011-08-05 14:39:18 +0900350 if (dri2_surf->dri_drawable == NULL) {
Emil Velikovacf125e2017-05-08 18:39:12 +0100351 _eglError(EGL_BAD_ALLOC, "createNewDrawable");
Chia-I Wuc8e18f82011-08-26 22:09:56 +0800352 goto cleanup_surface;
Chia-I Wu9779f6f2011-08-05 14:39:18 +0900353 }
354
Chia-I Wuc8e18f82011-08-26 22:09:56 +0800355 if (window) {
356 window->common.incRef(&window->common);
357 dri2_surf->window = window;
358 }
Chia-I Wu9779f6f2011-08-05 14:39:18 +0900359
360 return &dri2_surf->base;
361
Chia-I Wuc8e18f82011-08-26 22:09:56 +0800362cleanup_surface:
Chia-I Wu9779f6f2011-08-05 14:39:18 +0900363 free(dri2_surf);
364
365 return NULL;
366}
367
368static _EGLSurface *
369droid_create_window_surface(_EGLDriver *drv, _EGLDisplay *disp,
Chad Versace6d1f83e2014-01-07 14:54:51 -0800370 _EGLConfig *conf, void *native_window,
371 const EGLint *attrib_list)
Chia-I Wu9779f6f2011-08-05 14:39:18 +0900372{
373 return droid_create_surface(drv, disp, EGL_WINDOW_BIT, conf,
Chad Versace6d1f83e2014-01-07 14:54:51 -0800374 native_window, attrib_list);
Chia-I Wu9779f6f2011-08-05 14:39:18 +0900375}
376
377static _EGLSurface *
Chia-I Wu9779f6f2011-08-05 14:39:18 +0900378droid_create_pbuffer_surface(_EGLDriver *drv, _EGLDisplay *disp,
379 _EGLConfig *conf, const EGLint *attrib_list)
380{
Chia-I Wu93d59632011-08-27 00:00:18 +0800381 return droid_create_surface(drv, disp, EGL_PBUFFER_BIT, conf,
382 NULL, attrib_list);
Chia-I Wu9779f6f2011-08-05 14:39:18 +0900383}
384
385static EGLBoolean
386droid_destroy_surface(_EGLDriver *drv, _EGLDisplay *disp, _EGLSurface *surf)
387{
388 struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);
389 struct dri2_egl_surface *dri2_surf = dri2_egl_surface(surf);
390
Gwan-gyeong Mun640b6e62017-08-05 00:16:11 +0900391 dri2_egl_surface_free_local_buffers(dri2_surf);
Chia-I Wu9779f6f2011-08-05 14:39:18 +0900392
Chia-I Wuc8e18f82011-08-26 22:09:56 +0800393 if (dri2_surf->base.Type == EGL_WINDOW_BIT) {
394 if (dri2_surf->buffer)
Chad Versace0212db32017-05-04 17:46:33 -0700395 droid_window_cancel_buffer(dri2_surf);
Chia-I Wu9779f6f2011-08-05 14:39:18 +0900396
Chia-I Wuc8e18f82011-08-26 22:09:56 +0800397 dri2_surf->window->common.decRef(&dri2_surf->window->common);
398 }
Chia-I Wu9779f6f2011-08-05 14:39:18 +0900399
Liu Zhiquanb6637532016-11-16 10:11:28 +0800400 if (dri2_surf->dri_image_back) {
401 _eglLog(_EGL_DEBUG, "%s : %d : destroy dri_image_back", __func__, __LINE__);
402 dri2_dpy->image->destroyImage(dri2_surf->dri_image_back);
403 dri2_surf->dri_image_back = NULL;
404 }
405
406 if (dri2_surf->dri_image_front) {
407 _eglLog(_EGL_DEBUG, "%s : %d : destroy dri_image_front", __func__, __LINE__);
408 dri2_dpy->image->destroyImage(dri2_surf->dri_image_front);
409 dri2_surf->dri_image_front = NULL;
410 }
411
Boyan Ding868ae3e2015-11-25 13:27:02 +0800412 dri2_dpy->core->destroyDrawable(dri2_surf->dri_drawable);
Chia-I Wu9779f6f2011-08-05 14:39:18 +0900413
Zhongmin Wue013ce82017-09-15 18:32:42 +0100414 dri2_fini_surface(surf);
Chia-I Wu9779f6f2011-08-05 14:39:18 +0900415 free(dri2_surf);
416
417 return EGL_TRUE;
418}
419
Rob Herring81a6fff2016-04-28 15:37:30 -0500420static int
421update_buffers(struct dri2_egl_surface *dri2_surf)
422{
Chad Versacee5eace52017-05-04 17:46:34 -0700423 if (dri2_surf->base.Lost)
424 return -1;
425
Rob Herring81a6fff2016-04-28 15:37:30 -0500426 if (dri2_surf->base.Type != EGL_WINDOW_BIT)
427 return 0;
428
429 /* try to dequeue the next back buffer */
Tomasz Figa565fa6b2016-07-15 16:53:49 +0900430 if (!dri2_surf->buffer && !droid_window_dequeue_buffer(dri2_surf)) {
431 _eglLog(_EGL_WARNING, "Could not dequeue buffer from native window");
Nicolas Boichat63b12b02017-05-05 10:43:50 +0800432 dri2_surf->base.Lost = EGL_TRUE;
Rob Herring81a6fff2016-04-28 15:37:30 -0500433 return -1;
Tomasz Figa565fa6b2016-07-15 16:53:49 +0900434 }
Rob Herring81a6fff2016-04-28 15:37:30 -0500435
436 /* free outdated buffers and update the surface size */
437 if (dri2_surf->base.Width != dri2_surf->buffer->width ||
438 dri2_surf->base.Height != dri2_surf->buffer->height) {
Gwan-gyeong Mun640b6e62017-08-05 00:16:11 +0900439 dri2_egl_surface_free_local_buffers(dri2_surf);
Rob Herring81a6fff2016-04-28 15:37:30 -0500440 dri2_surf->base.Width = dri2_surf->buffer->width;
441 dri2_surf->base.Height = dri2_surf->buffer->height;
442 }
443
444 return 0;
445}
446
Rob Herring34ddef32016-05-01 09:35:28 +0100447static int
Liu Zhiquane2610bf2016-12-09 19:29:56 +0800448get_front_bo(struct dri2_egl_surface *dri2_surf, unsigned int format)
449{
450 struct dri2_egl_display *dri2_dpy =
451 dri2_egl_display(dri2_surf->base.Resource.Display);
452
453 if (dri2_surf->dri_image_front)
454 return 0;
455
456 if (dri2_surf->base.Type == EGL_WINDOW_BIT) {
457 /* According current EGL spec, front buffer rendering
458 * for window surface is not supported now.
459 * and mesa doesn't have the implementation of this case.
460 * Add warning message, but not treat it as error.
461 */
462 _eglLog(_EGL_DEBUG, "DRI driver requested unsupported front buffer for window surface");
463 } else if (dri2_surf->base.Type == EGL_PBUFFER_BIT) {
464 dri2_surf->dri_image_front =
465 dri2_dpy->image->createImage(dri2_dpy->dri_screen,
466 dri2_surf->base.Width,
467 dri2_surf->base.Height,
468 format,
469 0,
470 dri2_surf);
471 if (!dri2_surf->dri_image_front) {
472 _eglLog(_EGL_WARNING, "dri2_image_front allocation failed");
473 return -1;
474 }
475 }
476
477 return 0;
478}
479
480static int
Chad Versace22d6b082017-05-23 17:26:52 -0700481get_back_bo(struct dri2_egl_surface *dri2_surf)
Rob Herring34ddef32016-05-01 09:35:28 +0100482{
483 struct dri2_egl_display *dri2_dpy =
484 dri2_egl_display(dri2_surf->base.Resource.Display);
Emil Velikovfb653642016-05-01 09:35:56 +0100485 int fourcc, pitch;
Rob Herring34ddef32016-05-01 09:35:28 +0100486 int offset = 0, fd;
487
Liu Zhiquanb6637532016-11-16 10:11:28 +0800488 if (dri2_surf->dri_image_back)
489 return 0;
Tomasz Figa9e1248d2016-07-15 16:53:50 +0900490
Liu Zhiquane2610bf2016-12-09 19:29:56 +0800491 if (dri2_surf->base.Type == EGL_WINDOW_BIT) {
492 if (!dri2_surf->buffer) {
493 _eglLog(_EGL_WARNING, "Could not get native buffer");
494 return -1;
Liu Zhiquanb6637532016-11-16 10:11:28 +0800495 }
496
Liu Zhiquane2610bf2016-12-09 19:29:56 +0800497 fd = get_native_buffer_fd(dri2_surf->buffer);
498 if (fd < 0) {
499 _eglLog(_EGL_WARNING, "Could not get native buffer FD");
500 return -1;
501 }
502
503 fourcc = get_fourcc(dri2_surf->buffer->format);
504
505 pitch = dri2_surf->buffer->stride *
506 get_format_bpp(dri2_surf->buffer->format);
507
508 if (fourcc == -1 || pitch == 0) {
509 _eglLog(_EGL_WARNING, "Invalid buffer fourcc(%x) or pitch(%d)",
510 fourcc, pitch);
511 return -1;
512 }
513
514 dri2_surf->dri_image_back =
515 dri2_dpy->image->createImageFromFds(dri2_dpy->dri_screen,
516 dri2_surf->base.Width,
517 dri2_surf->base.Height,
518 fourcc,
519 &fd,
520 1,
521 &pitch,
522 &offset,
523 dri2_surf);
524 if (!dri2_surf->dri_image_back) {
525 _eglLog(_EGL_WARNING, "failed to create DRI image from FD");
526 return -1;
527 }
528 } else if (dri2_surf->base.Type == EGL_PBUFFER_BIT) {
Liu Zhiquanb6637532016-11-16 10:11:28 +0800529 /* The EGL 1.5 spec states that pbuffers are single-buffered. Specifically,
530 * the spec states that they have a back buffer but no front buffer, in
531 * contrast to pixmaps, which have a front buffer but no back buffer.
532 *
533 * Single-buffered surfaces with no front buffer confuse Mesa; so we deviate
534 * from the spec, following the precedent of Mesa's EGL X11 platform. The
535 * X11 platform correctly assigns pbuffers to single-buffered configs, but
536 * assigns the pbuffer a front buffer instead of a back buffer.
537 *
538 * Pbuffers in the X11 platform mostly work today, so let's just copy its
539 * behavior instead of trying to fix (and hence potentially breaking) the
540 * world.
Tomasz Figa217af752016-08-02 20:07:50 +0900541 */
Liu Zhiquane2610bf2016-12-09 19:29:56 +0800542 _eglLog(_EGL_DEBUG, "DRI driver requested unsupported back buffer for pbuffer surface");
543 }
Liu Zhiquanb6637532016-11-16 10:11:28 +0800544
Liu Zhiquane2610bf2016-12-09 19:29:56 +0800545 return 0;
546}
547
548/* Some drivers will pass multiple bits in buffer_mask.
549 * For such case, will go through all the bits, and
550 * will not return error when unsupported buffer is requested, only
551 * return error when the allocation for supported buffer failed.
552 */
553static int
554droid_image_get_buffers(__DRIdrawable *driDrawable,
555 unsigned int format,
556 uint32_t *stamp,
557 void *loaderPrivate,
558 uint32_t buffer_mask,
559 struct __DRIimageList *images)
560{
561 struct dri2_egl_surface *dri2_surf = loaderPrivate;
562
563 images->image_mask = 0;
564 images->front = NULL;
565 images->back = NULL;
566
567 if (update_buffers(dri2_surf) < 0)
568 return 0;
569
570 if (buffer_mask & __DRI_IMAGE_BUFFER_FRONT) {
571 if (get_front_bo(dri2_surf, format) < 0)
Liu Zhiquanb6637532016-11-16 10:11:28 +0800572 return 0;
Liu Zhiquanb6637532016-11-16 10:11:28 +0800573
Liu Zhiquane2610bf2016-12-09 19:29:56 +0800574 if (dri2_surf->dri_image_front) {
575 images->front = dri2_surf->dri_image_front;
576 images->image_mask |= __DRI_IMAGE_BUFFER_FRONT;
577 }
Rob Herring34ddef32016-05-01 09:35:28 +0100578 }
579
Tomasz Figa217af752016-08-02 20:07:50 +0900580 if (buffer_mask & __DRI_IMAGE_BUFFER_BACK) {
Chad Versace22d6b082017-05-23 17:26:52 -0700581 if (get_back_bo(dri2_surf) < 0)
Liu Zhiquanb6637532016-11-16 10:11:28 +0800582 return 0;
Liu Zhiquanb6637532016-11-16 10:11:28 +0800583
Liu Zhiquane2610bf2016-12-09 19:29:56 +0800584 if (dri2_surf->dri_image_back) {
585 images->back = dri2_surf->dri_image_back;
586 images->image_mask |= __DRI_IMAGE_BUFFER_BACK;
587 }
Tomasz Figa217af752016-08-02 20:07:50 +0900588 }
Rob Herring34ddef32016-05-01 09:35:28 +0100589
590 return 1;
591}
592
Xiaosong Wei2acc69d2017-02-08 10:46:02 +0800593static EGLint
594droid_query_buffer_age(_EGLDriver *drv,
595 _EGLDisplay *disp, _EGLSurface *surface)
596{
597 struct dri2_egl_surface *dri2_surf = dri2_egl_surface(surface);
598
599 if (update_buffers(dri2_surf) < 0) {
600 _eglError(EGL_BAD_ALLOC, "droid_query_buffer_age");
Tapani Pälli8fac8942017-06-08 12:24:24 +0300601 return -1;
Xiaosong Wei2acc69d2017-02-08 10:46:02 +0800602 }
603
Tapani Pälli8fac8942017-06-08 12:24:24 +0300604 return dri2_surf->back ? dri2_surf->back->age : 0;
Xiaosong Wei2acc69d2017-02-08 10:46:02 +0800605}
606
Chia-I Wu9779f6f2011-08-05 14:39:18 +0900607static EGLBoolean
608droid_swap_buffers(_EGLDriver *drv, _EGLDisplay *disp, _EGLSurface *draw)
609{
Chia-I Wu9779f6f2011-08-05 14:39:18 +0900610 struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);
611 struct dri2_egl_surface *dri2_surf = dri2_egl_surface(draw);
Chia-I Wu9779f6f2011-08-05 14:39:18 +0900612
Chia-I Wuc8e18f82011-08-26 22:09:56 +0800613 if (dri2_surf->base.Type != EGL_WINDOW_BIT)
614 return EGL_TRUE;
615
Xiaosong Wei2acc69d2017-02-08 10:46:02 +0800616 for (int i = 0; i < ARRAY_SIZE(dri2_surf->color_buffers); i++) {
617 if (dri2_surf->color_buffers[i].age > 0)
618 dri2_surf->color_buffers[i].age++;
619 }
Tapani Pällif347bac2017-05-18 10:21:59 +0300620
621 /* "XXX: we don't use get_back_bo() since it causes regressions in
622 * several dEQP tests.
623 */
624 if (dri2_surf->back)
625 dri2_surf->back->age = 1;
Xiaosong Wei2acc69d2017-02-08 10:46:02 +0800626
Eric Anholt70e8ccc2014-12-21 11:51:33 -0800627 dri2_flush_drawable_for_swapbuffers(disp, draw);
Chia-I Wu9779f6f2011-08-05 14:39:18 +0900628
Chad Versacee5eace52017-05-04 17:46:34 -0700629 /* dri2_surf->buffer can be null even when no error has occured. For
630 * example, if the user has called no GL rendering commands since the
631 * previous eglSwapBuffers, then the driver may have not triggered
632 * a callback to ANativeWindow::dequeueBuffer, in which case
633 * dri2_surf->buffer remains null.
634 */
Chia-I Wu9779f6f2011-08-05 14:39:18 +0900635 if (dri2_surf->buffer)
Haixia Shi1ea233c2016-06-02 12:48:23 -0700636 droid_window_enqueue_buffer(disp, dri2_surf);
Chia-I Wu9779f6f2011-08-05 14:39:18 +0900637
Boyan Ding868ae3e2015-11-25 13:27:02 +0800638 dri2_dpy->flush->invalidate(dri2_surf->dri_drawable);
Chia-I Wu9779f6f2011-08-05 14:39:18 +0900639
640 return EGL_TRUE;
641}
642
Harish Krupo98275472017-06-09 20:13:34 +0530643#if ANDROID_API_LEVEL >= 23
644static EGLBoolean
645droid_set_damage_region(_EGLDriver *drv,
646 _EGLDisplay *disp,
647 _EGLSurface *draw, const EGLint* rects, EGLint n_rects)
648{
649 struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);
650 struct dri2_egl_surface *dri2_surf = dri2_egl_surface(draw);
651 android_native_rect_t* droid_rects = NULL;
652 int ret;
653
654 if (n_rects == 0)
655 return EGL_TRUE;
656
657 droid_rects = malloc(n_rects * sizeof(android_native_rect_t));
Emil Velikovc58af5c2017-06-20 15:40:28 +0100658 if (droid_rects == NULL)
659 return _eglError(EGL_BAD_ALLOC, "eglSetDamageRegionKHR");
Harish Krupo98275472017-06-09 20:13:34 +0530660
661 for (EGLint num_drects = 0; num_drects < n_rects; num_drects++) {
662 EGLint i = num_drects * 4;
663 droid_rects[num_drects].left = rects[i];
664 droid_rects[num_drects].bottom = rects[i + 1];
665 droid_rects[num_drects].right = rects[i] + rects[i + 2];
666 droid_rects[num_drects].top = rects[i + 1] + rects[i + 3];
667 }
668
669 /*
670 * XXX/TODO: Need to check for other return values
671 */
672
673 ret = native_window_set_surface_damage(dri2_surf->window, droid_rects, n_rects);
674 free(droid_rects);
675
676 return ret == 0 ? EGL_TRUE : EGL_FALSE;
677}
678#endif
679
Chia-I Wu9779f6f2011-08-05 14:39:18 +0900680static _EGLImage *
Tomasz Figa51727b12016-11-10 16:55:53 +0900681droid_create_image_from_prime_fd_yuv(_EGLDisplay *disp, _EGLContext *ctx,
682 struct ANativeWindowBuffer *buf, int fd)
Tomasz Figae77b4932016-08-02 20:07:52 +0900683{
Tomasz Figa51727b12016-11-10 16:55:53 +0900684 struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);
685 struct android_ycbcr ycbcr;
686 size_t offsets[3];
687 size_t pitches[3];
688 int is_ycrcb;
689 int fourcc;
690 int ret;
Tomasz Figae77b4932016-08-02 20:07:52 +0900691
Tomasz Figa51727b12016-11-10 16:55:53 +0900692 if (!dri2_dpy->gralloc->lock_ycbcr) {
693 _eglLog(_EGL_WARNING, "Gralloc does not support lock_ycbcr");
694 return NULL;
695 }
696
697 memset(&ycbcr, 0, sizeof(ycbcr));
698 ret = dri2_dpy->gralloc->lock_ycbcr(dri2_dpy->gralloc, buf->handle,
699 0, 0, 0, 0, 0, &ycbcr);
700 if (ret) {
Tomasz Figa5364e732017-12-04 19:22:39 +0100701 /* HACK: See droid_create_image_from_prime_fd() and
702 * https://issuetracker.google.com/32077885.*/
703 if (buf->format == HAL_PIXEL_FORMAT_IMPLEMENTATION_DEFINED)
704 return NULL;
705
Tomasz Figa51727b12016-11-10 16:55:53 +0900706 _eglLog(_EGL_WARNING, "gralloc->lock_ycbcr failed: %d", ret);
707 return NULL;
708 }
709 dri2_dpy->gralloc->unlock(dri2_dpy->gralloc, buf->handle);
710
711 /* When lock_ycbcr's usage argument contains no SW_READ/WRITE flags
712 * it will return the .y/.cb/.cr pointers based on a NULL pointer,
713 * so they can be interpreted as offsets. */
714 offsets[0] = (size_t)ycbcr.y;
715 /* We assume here that all the planes are located in one DMA-buf. */
Tapani Pälli0a2dcd32017-02-02 14:05:46 +0200716 is_ycrcb = (size_t)ycbcr.cr < (size_t)ycbcr.cb;
Tomasz Figa51727b12016-11-10 16:55:53 +0900717 if (is_ycrcb) {
718 offsets[1] = (size_t)ycbcr.cr;
719 offsets[2] = (size_t)ycbcr.cb;
720 } else {
721 offsets[1] = (size_t)ycbcr.cb;
722 offsets[2] = (size_t)ycbcr.cr;
723 }
724
725 /* .ystride is the line length (in bytes) of the Y plane,
726 * .cstride is the line length (in bytes) of any of the remaining
727 * Cb/Cr/CbCr planes, assumed to be the same for Cb and Cr for fully
728 * planar formats. */
729 pitches[0] = ycbcr.ystride;
730 pitches[1] = pitches[2] = ycbcr.cstride;
731
732 /* .chroma_step is the byte distance between the same chroma channel
733 * values of subsequent pixels, assumed to be the same for Cb and Cr. */
734 fourcc = get_fourcc_yuv(buf->format, is_ycrcb, ycbcr.chroma_step);
Tomasz Figa3723e982016-08-02 20:07:54 +0900735 if (fourcc == -1) {
Tomasz Figa51727b12016-11-10 16:55:53 +0900736 _eglLog(_EGL_WARNING, "unsupported YUV format, native = %x, is_ycrcb = %d, chroma_step = %d",
737 buf->format, is_ycrcb, ycbcr.chroma_step);
Tomasz Figa3723e982016-08-02 20:07:54 +0900738 return NULL;
739 }
740
Tomasz Figa51727b12016-11-10 16:55:53 +0900741 if (ycbcr.chroma_step == 2) {
Robert Fossa4d33712017-06-15 16:47:53 -0400742 /* Semi-planar Y + CbCr or Y + CrCb format. */
Tomasz Figa51727b12016-11-10 16:55:53 +0900743 const EGLint attr_list_2plane[] = {
744 EGL_WIDTH, buf->width,
745 EGL_HEIGHT, buf->height,
746 EGL_LINUX_DRM_FOURCC_EXT, fourcc,
747 EGL_DMA_BUF_PLANE0_FD_EXT, fd,
748 EGL_DMA_BUF_PLANE0_PITCH_EXT, pitches[0],
749 EGL_DMA_BUF_PLANE0_OFFSET_EXT, offsets[0],
750 EGL_DMA_BUF_PLANE1_FD_EXT, fd,
751 EGL_DMA_BUF_PLANE1_PITCH_EXT, pitches[1],
752 EGL_DMA_BUF_PLANE1_OFFSET_EXT, offsets[1],
753 EGL_NONE, 0
754 };
Tomasz Figa3723e982016-08-02 20:07:54 +0900755
Tomasz Figa51727b12016-11-10 16:55:53 +0900756 return dri2_create_image_dma_buf(disp, ctx, NULL, attr_list_2plane);
757 } else {
758 /* Fully planar Y + Cb + Cr or Y + Cr + Cb format. */
759 const EGLint attr_list_3plane[] = {
Tomasz Figa3723e982016-08-02 20:07:54 +0900760 EGL_WIDTH, buf->width,
761 EGL_HEIGHT, buf->height,
762 EGL_LINUX_DRM_FOURCC_EXT, fourcc,
763 EGL_DMA_BUF_PLANE0_FD_EXT, fd,
764 EGL_DMA_BUF_PLANE0_PITCH_EXT, pitches[0],
765 EGL_DMA_BUF_PLANE0_OFFSET_EXT, offsets[0],
766 EGL_DMA_BUF_PLANE1_FD_EXT, fd,
767 EGL_DMA_BUF_PLANE1_PITCH_EXT, pitches[1],
768 EGL_DMA_BUF_PLANE1_OFFSET_EXT, offsets[1],
769 EGL_DMA_BUF_PLANE2_FD_EXT, fd,
770 EGL_DMA_BUF_PLANE2_PITCH_EXT, pitches[2],
771 EGL_DMA_BUF_PLANE2_OFFSET_EXT, offsets[2],
772 EGL_NONE, 0
773 };
774
Tomasz Figa51727b12016-11-10 16:55:53 +0900775 return dri2_create_image_dma_buf(disp, ctx, NULL, attr_list_3plane);
776 }
777}
778
779static _EGLImage *
780droid_create_image_from_prime_fd(_EGLDisplay *disp, _EGLContext *ctx,
781 struct ANativeWindowBuffer *buf, int fd)
782{
783 unsigned int pitch;
784
Tomasz Figa5364e732017-12-04 19:22:39 +0100785 if (is_yuv(buf->format)) {
786 _EGLImage *image;
787
788 image = droid_create_image_from_prime_fd_yuv(disp, ctx, buf, fd);
789 /*
790 * HACK: https://issuetracker.google.com/32077885
791 * There is no API available to properly query the IMPLEMENTATION_DEFINED
792 * format. As a workaround we rely here on gralloc allocating either
793 * an arbitrary YCbCr 4:2:0 or RGBX_8888, with the latter being recognized
794 * by lock_ycbcr failing.
795 */
796 if (image || buf->format != HAL_PIXEL_FORMAT_IMPLEMENTATION_DEFINED)
797 return image;
798 }
Tomasz Figa51727b12016-11-10 16:55:53 +0900799
800 const int fourcc = get_fourcc(buf->format);
801 if (fourcc == -1) {
802 _eglError(EGL_BAD_PARAMETER, "eglCreateEGLImageKHR");
803 return NULL;
804 }
805
806 pitch = buf->stride * get_format_bpp(buf->format);
807 if (pitch == 0) {
808 _eglError(EGL_BAD_PARAMETER, "eglCreateEGLImageKHR");
809 return NULL;
Tomasz Figa3723e982016-08-02 20:07:54 +0900810 }
811
812 const EGLint attr_list[] = {
Tomasz Figae77b4932016-08-02 20:07:52 +0900813 EGL_WIDTH, buf->width,
814 EGL_HEIGHT, buf->height,
815 EGL_LINUX_DRM_FOURCC_EXT, fourcc,
816 EGL_DMA_BUF_PLANE0_FD_EXT, fd,
Tomasz Figa51727b12016-11-10 16:55:53 +0900817 EGL_DMA_BUF_PLANE0_PITCH_EXT, pitch,
Tomasz Figae77b4932016-08-02 20:07:52 +0900818 EGL_DMA_BUF_PLANE0_OFFSET_EXT, 0,
819 EGL_NONE, 0
820 };
821
Tomasz Figae77b4932016-08-02 20:07:52 +0900822 return dri2_create_image_dma_buf(disp, ctx, NULL, attr_list);
823}
824
825static _EGLImage *
826droid_create_image_from_name(_EGLDisplay *disp, _EGLContext *ctx,
827 struct ANativeWindowBuffer *buf)
Chia-I Wu9779f6f2011-08-05 14:39:18 +0900828{
829 struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);
830 struct dri2_egl_image *dri2_img;
Tomasz Figae77b4932016-08-02 20:07:52 +0900831 int name;
Emil Velikovfb653642016-05-01 09:35:56 +0100832 int format;
Chia-I Wu9779f6f2011-08-05 14:39:18 +0900833
Chia-I Wu9779f6f2011-08-05 14:39:18 +0900834 name = get_native_buffer_name(buf);
835 if (!name) {
836 _eglError(EGL_BAD_PARAMETER, "eglCreateEGLImageKHR");
837 return NULL;
838 }
839
Emil Velikovfb653642016-05-01 09:35:56 +0100840 format = get_format(buf->format);
841 if (format == -1)
842 return NULL;
843
Chia-I Wu9779f6f2011-08-05 14:39:18 +0900844 dri2_img = calloc(1, sizeof(*dri2_img));
845 if (!dri2_img) {
846 _eglError(EGL_BAD_ALLOC, "droid_create_image_mesa_drm");
847 return NULL;
848 }
849
Emil Velikovd42b0952017-06-20 15:22:39 +0100850 _eglInitImage(&dri2_img->base, disp);
Chia-I Wu9779f6f2011-08-05 14:39:18 +0900851
852 dri2_img->dri_image =
853 dri2_dpy->image->createImageFromName(dri2_dpy->dri_screen,
854 buf->width,
855 buf->height,
Emil Velikovfb653642016-05-01 09:35:56 +0100856 format,
Chia-I Wu9779f6f2011-08-05 14:39:18 +0900857 name,
858 buf->stride,
859 dri2_img);
860 if (!dri2_img->dri_image) {
861 free(dri2_img);
862 _eglError(EGL_BAD_ALLOC, "droid_create_image_mesa_drm");
863 return NULL;
864 }
865
866 return &dri2_img->base;
867}
868
Haixia Shia7c69932016-07-28 10:51:12 -0700869static EGLBoolean
870droid_query_surface(_EGLDriver *drv, _EGLDisplay *dpy, _EGLSurface *surf,
871 EGLint attribute, EGLint *value)
872{
873 struct dri2_egl_surface *dri2_surf = dri2_egl_surface(surf);
874 switch (attribute) {
875 case EGL_WIDTH:
876 if (dri2_surf->base.Type == EGL_WINDOW_BIT && dri2_surf->window) {
877 dri2_surf->window->query(dri2_surf->window,
878 NATIVE_WINDOW_DEFAULT_WIDTH, value);
879 return EGL_TRUE;
880 }
881 break;
882 case EGL_HEIGHT:
883 if (dri2_surf->base.Type == EGL_WINDOW_BIT && dri2_surf->window) {
884 dri2_surf->window->query(dri2_surf->window,
885 NATIVE_WINDOW_DEFAULT_HEIGHT, value);
886 return EGL_TRUE;
887 }
888 break;
889 default:
890 break;
891 }
892 return _eglQuerySurface(drv, dpy, surf, attribute, value);
893}
894
Chia-I Wu9779f6f2011-08-05 14:39:18 +0900895static _EGLImage *
Tomasz Figae77b4932016-08-02 20:07:52 +0900896dri2_create_image_android_native_buffer(_EGLDisplay *disp,
897 _EGLContext *ctx,
898 struct ANativeWindowBuffer *buf)
899{
900 int fd;
901
902 if (ctx != NULL) {
903 /* From the EGL_ANDROID_image_native_buffer spec:
904 *
905 * * If <target> is EGL_NATIVE_BUFFER_ANDROID and <ctx> is not
906 * EGL_NO_CONTEXT, the error EGL_BAD_CONTEXT is generated.
907 */
908 _eglError(EGL_BAD_CONTEXT, "eglCreateEGLImageKHR: for "
909 "EGL_NATIVE_BUFFER_ANDROID, the context must be "
910 "EGL_NO_CONTEXT");
911 return NULL;
912 }
913
914 if (!buf || buf->common.magic != ANDROID_NATIVE_BUFFER_MAGIC ||
915 buf->common.version != sizeof(*buf)) {
916 _eglError(EGL_BAD_PARAMETER, "eglCreateEGLImageKHR");
917 return NULL;
918 }
919
920 fd = get_native_buffer_fd(buf);
921 if (fd >= 0)
922 return droid_create_image_from_prime_fd(disp, ctx, buf, fd);
923
924 return droid_create_image_from_name(disp, ctx, buf);
925}
926
927static _EGLImage *
Chia-I Wu9779f6f2011-08-05 14:39:18 +0900928droid_create_image_khr(_EGLDriver *drv, _EGLDisplay *disp,
929 _EGLContext *ctx, EGLenum target,
930 EGLClientBuffer buffer, const EGLint *attr_list)
931{
932 switch (target) {
933 case EGL_NATIVE_BUFFER_ANDROID:
Emil Velikov9b3c7482016-05-01 12:42:54 +0100934 return dri2_create_image_android_native_buffer(disp, ctx,
Chia-I Wu75cc24c2011-11-25 11:59:02 +0800935 (struct ANativeWindowBuffer *) buffer);
Chia-I Wu9779f6f2011-08-05 14:39:18 +0900936 default:
937 return dri2_create_image_khr(drv, disp, ctx, target, buffer, attr_list);
938 }
939}
940
941static void
Chia-I Wu9779f6f2011-08-05 14:39:18 +0900942droid_flush_front_buffer(__DRIdrawable * driDrawable, void *loaderPrivate)
943{
944}
945
Chia-I Wu384f2282011-08-26 23:49:04 +0800946static int
947droid_get_buffers_parse_attachments(struct dri2_egl_surface *dri2_surf,
948 unsigned int *attachments, int count)
949{
Chad Versace09455122017-06-22 11:00:40 -0700950 int num_buffers = 0;
Chia-I Wu384f2282011-08-26 23:49:04 +0800951
952 /* fill dri2_surf->buffers */
Chad Versace09455122017-06-22 11:00:40 -0700953 for (int i = 0; i < count * 2; i += 2) {
Chia-I Wu384f2282011-08-26 23:49:04 +0800954 __DRIbuffer *buf, *local;
955
956 assert(num_buffers < ARRAY_SIZE(dri2_surf->buffers));
957 buf = &dri2_surf->buffers[num_buffers];
958
959 switch (attachments[i]) {
960 case __DRI_BUFFER_BACK_LEFT:
Chia-I Wuc8e18f82011-08-26 22:09:56 +0800961 if (dri2_surf->base.Type == EGL_WINDOW_BIT) {
962 buf->attachment = attachments[i];
963 buf->name = get_native_buffer_name(dri2_surf->buffer);
964 buf->cpp = get_format_bpp(dri2_surf->buffer->format);
965 buf->pitch = dri2_surf->buffer->stride * buf->cpp;
966 buf->flags = 0;
Chia-I Wu384f2282011-08-26 23:49:04 +0800967
Chia-I Wuc8e18f82011-08-26 22:09:56 +0800968 if (buf->name)
969 num_buffers++;
Chia-I Wu93d59632011-08-27 00:00:18 +0800970
971 break;
Chia-I Wuc8e18f82011-08-26 22:09:56 +0800972 }
Chia-I Wu93d59632011-08-27 00:00:18 +0800973 /* fall through for pbuffers */
Chia-I Wu384f2282011-08-26 23:49:04 +0800974 case __DRI_BUFFER_DEPTH:
975 case __DRI_BUFFER_STENCIL:
976 case __DRI_BUFFER_ACCUM:
977 case __DRI_BUFFER_DEPTH_STENCIL:
978 case __DRI_BUFFER_HIZ:
Gwan-gyeong Mun640b6e62017-08-05 00:16:11 +0900979 local = dri2_egl_surface_alloc_local_buffer(dri2_surf,
Chia-I Wu384f2282011-08-26 23:49:04 +0800980 attachments[i], attachments[i + 1]);
981
982 if (local) {
983 *buf = *local;
984 num_buffers++;
985 }
986 break;
987 case __DRI_BUFFER_FRONT_LEFT:
988 case __DRI_BUFFER_FRONT_RIGHT:
989 case __DRI_BUFFER_FAKE_FRONT_LEFT:
990 case __DRI_BUFFER_FAKE_FRONT_RIGHT:
991 case __DRI_BUFFER_BACK_RIGHT:
992 default:
993 /* no front or right buffers */
994 break;
995 }
996 }
997
998 return num_buffers;
999}
1000
Chia-I Wu9779f6f2011-08-05 14:39:18 +09001001static __DRIbuffer *
1002droid_get_buffers_with_format(__DRIdrawable * driDrawable,
1003 int *width, int *height,
1004 unsigned int *attachments, int count,
1005 int *out_count, void *loaderPrivate)
1006{
1007 struct dri2_egl_surface *dri2_surf = loaderPrivate;
Chia-I Wu9779f6f2011-08-05 14:39:18 +09001008
Rob Herring81a6fff2016-04-28 15:37:30 -05001009 if (update_buffers(dri2_surf) < 0)
1010 return NULL;
Chia-I Wu9779f6f2011-08-05 14:39:18 +09001011
Gwan-gyeong Mun3f6cc932017-07-18 17:12:26 +09001012 *out_count = droid_get_buffers_parse_attachments(dri2_surf, attachments, count);
Chia-I Wu9779f6f2011-08-05 14:39:18 +09001013
1014 if (width)
1015 *width = dri2_surf->base.Width;
1016 if (height)
1017 *height = dri2_surf->base.Height;
1018
Chia-I Wu9779f6f2011-08-05 14:39:18 +09001019 return dri2_surf->buffers;
1020}
1021
Marek Olšák1bf703e2017-07-28 17:30:34 +02001022static unsigned
1023droid_get_capability(void *loaderPrivate, enum dri_loader_cap cap)
1024{
1025 /* Note: loaderPrivate is _EGLDisplay* */
1026 switch (cap) {
1027 case DRI_LOADER_CAP_RGBA_ORDERING:
1028 return 1;
1029 default:
1030 return 0;
1031 }
1032}
1033
Chia-I Wu9779f6f2011-08-05 14:39:18 +09001034static EGLBoolean
1035droid_add_configs_for_visuals(_EGLDriver *drv, _EGLDisplay *dpy)
1036{
1037 struct dri2_egl_display *dri2_dpy = dri2_egl_display(dpy);
Emil Velikov4472b6e2016-08-25 12:08:07 +01001038 static const struct {
Chia-I Wu9779f6f2011-08-05 14:39:18 +09001039 int format;
Chia-I Wu9779f6f2011-08-05 14:39:18 +09001040 unsigned int rgba_masks[4];
1041 } visuals[] = {
Chad Versace0bcdceb2017-05-23 15:50:13 -07001042 { HAL_PIXEL_FORMAT_RGBA_8888, { 0x000000ff, 0x0000ff00, 0x00ff0000, 0xff000000 } },
1043 { HAL_PIXEL_FORMAT_RGBX_8888, { 0x000000ff, 0x0000ff00, 0x00ff0000, 0x00000000 } },
1044 { HAL_PIXEL_FORMAT_RGB_565, { 0x0000f800, 0x000007e0, 0x0000001f, 0x00000000 } },
1045 { HAL_PIXEL_FORMAT_BGRA_8888, { 0x00ff0000, 0x0000ff00, 0x000000ff, 0xff000000 } },
Chia-I Wu9779f6f2011-08-05 14:39:18 +09001046 };
Chad Versace09455122017-06-22 11:00:40 -07001047
Emil Velikovacd35c82016-08-25 14:28:48 +01001048 unsigned int format_count[ARRAY_SIZE(visuals)] = { 0 };
Chad Versaceffbf50b2017-06-22 11:00:41 -07001049 int config_count = 0;
Chia-I Wu9779f6f2011-08-05 14:39:18 +09001050
Chad Versace5e884352017-06-16 19:11:21 -07001051 /* The nesting of loops is significant here. Also significant is the order
1052 * of the HAL pixel formats. Many Android apps (such as Google's official
1053 * NDK GLES2 example app), and even portions the core framework code (such
1054 * as SystemServiceManager in Nougat), incorrectly choose their EGLConfig.
1055 * They neglect to match the EGLConfig's EGL_NATIVE_VISUAL_ID against the
1056 * window's native format, and instead choose the first EGLConfig whose
1057 * channel sizes match those of the native window format while ignoring the
1058 * channel *ordering*.
1059 *
1060 * We can detect such buggy clients in logcat when they call
1061 * eglCreateSurface, by detecting the mismatch between the EGLConfig's
1062 * format and the window's format.
1063 *
1064 * As a workaround, we generate EGLConfigs such that all EGLConfigs for HAL
1065 * pixel format i precede those for HAL pixel format i+1. In my
1066 * (chadversary) testing on Android Nougat, this was good enough to pacify
1067 * the buggy clients.
1068 */
Chad Versace09455122017-06-22 11:00:40 -07001069 for (int i = 0; i < ARRAY_SIZE(visuals); i++) {
1070 for (int j = 0; dri2_dpy->driver_configs[j]; j++) {
1071 const EGLint surface_type = EGL_WINDOW_BIT | EGL_PBUFFER_BIT;
Tapani Pällid58ca432012-08-13 16:47:52 +03001072
Chad Versacebd789092017-06-22 11:00:41 -07001073 const EGLint config_attrs[] = {
1074 EGL_NATIVE_VISUAL_ID, visuals[i].format,
1075 EGL_NATIVE_VISUAL_TYPE, visuals[i].format,
1076 EGL_FRAMEBUFFER_TARGET_ANDROID, EGL_TRUE,
1077 EGL_RECORDABLE_ANDROID, EGL_TRUE,
1078 EGL_NONE
1079 };
Chia-I Wu9779f6f2011-08-05 14:39:18 +09001080
Chad Versace09455122017-06-22 11:00:40 -07001081 struct dri2_egl_config *dri2_conf =
1082 dri2_add_config(dpy, dri2_dpy->driver_configs[j],
Chad Versaceffbf50b2017-06-22 11:00:41 -07001083 config_count + 1, surface_type, config_attrs,
Chad Versace09455122017-06-22 11:00:40 -07001084 visuals[i].rgba_masks);
Chia-I Wu9779f6f2011-08-05 14:39:18 +09001085 if (dri2_conf) {
Chad Versaceffbf50b2017-06-22 11:00:41 -07001086 if (dri2_conf->base.ConfigID == config_count + 1)
1087 config_count++;
Chad Versace5e884352017-06-16 19:11:21 -07001088 format_count[i]++;
Chia-I Wu9779f6f2011-08-05 14:39:18 +09001089 }
1090 }
Emil Velikovacd35c82016-08-25 14:28:48 +01001091 }
Chia-I Wu9779f6f2011-08-05 14:39:18 +09001092
Chad Versace09455122017-06-22 11:00:40 -07001093 for (int i = 0; i < ARRAY_SIZE(format_count); i++) {
Emil Velikovacd35c82016-08-25 14:28:48 +01001094 if (!format_count[i]) {
Chia-I Wu9779f6f2011-08-05 14:39:18 +09001095 _eglLog(_EGL_DEBUG, "No DRI config supports native format 0x%x",
Emil Velikovacd35c82016-08-25 14:28:48 +01001096 visuals[i].format);
Chia-I Wu9779f6f2011-08-05 14:39:18 +09001097 }
1098 }
1099
Chad Versaceffbf50b2017-06-22 11:00:41 -07001100 return (config_count != 0);
Chia-I Wu9779f6f2011-08-05 14:39:18 +09001101}
1102
Chia-I Wu9779f6f2011-08-05 14:39:18 +09001103static int
Tomasz Figa859d0b02016-11-10 16:55:52 +09001104droid_open_device(struct dri2_egl_display *dri2_dpy)
Chia-I Wu9779f6f2011-08-05 14:39:18 +09001105{
Tomasz Figa859d0b02016-11-10 16:55:52 +09001106 int fd = -1, err = -EINVAL;
Chia-I Wu9779f6f2011-08-05 14:39:18 +09001107
Tomasz Figa859d0b02016-11-10 16:55:52 +09001108 if (dri2_dpy->gralloc->perform)
1109 err = dri2_dpy->gralloc->perform(dri2_dpy->gralloc,
1110 GRALLOC_MODULE_PERFORM_GET_DRM_FD,
1111 &fd);
Chia-I Wu9779f6f2011-08-05 14:39:18 +09001112 if (err || fd < 0) {
1113 _eglLog(_EGL_WARNING, "fail to get drm fd");
1114 fd = -1;
1115 }
1116
Matt Whitlockc8fd7d02016-10-01 23:49:41 -04001117 return (fd >= 0) ? fcntl(fd, F_DUPFD_CLOEXEC, 3) : -1;
Chia-I Wu9779f6f2011-08-05 14:39:18 +09001118}
1119
Emil Velikov79d1fb92017-05-15 16:14:17 +01001120static const struct dri2_egl_display_vtbl droid_display_vtbl = {
Chad Versace90502b12014-01-28 11:41:46 -08001121 .authenticate = NULL,
Chad Versace0a0c8812014-01-28 16:39:09 -08001122 .create_window_surface = droid_create_window_surface,
Adrian Negreanu6980cae2014-06-06 12:16:10 +03001123 .create_pixmap_surface = dri2_fallback_create_pixmap_surface,
Chad Versacebf200762014-01-28 17:03:03 -08001124 .create_pbuffer_surface = droid_create_pbuffer_surface,
Chad Versace958dd802014-01-28 17:03:03 -08001125 .destroy_surface = droid_destroy_surface,
Chad Versaceeef68a92014-01-28 17:03:03 -08001126 .create_image = droid_create_image_khr,
Chad Versacead173bc2014-01-28 16:21:21 -08001127 .swap_buffers = droid_swap_buffers,
Emil Velikov61e99ce2017-10-16 17:10:42 +01001128 .swap_buffers_with_damage = dri2_fallback_swap_buffers_with_damage, /* Android implements the function */
Chad Versace75d398e2014-01-28 17:03:03 -08001129 .swap_buffers_region = dri2_fallback_swap_buffers_region,
Harish Krupo98275472017-06-09 20:13:34 +05301130#if ANDROID_API_LEVEL >= 23
1131 .set_damage_region = droid_set_damage_region,
1132#else
1133 .set_damage_region = dri2_fallback_set_damage_region,
1134#endif
Chad Versace688a0e82014-01-28 17:03:03 -08001135 .post_sub_buffer = dri2_fallback_post_sub_buffer,
Chad Versacebc2cbc02014-01-28 17:03:03 -08001136 .copy_buffers = dri2_fallback_copy_buffers,
Xiaosong Wei2acc69d2017-02-08 10:46:02 +08001137 .query_buffer_age = droid_query_buffer_age,
Haixia Shia7c69932016-07-28 10:51:12 -07001138 .query_surface = droid_query_surface,
Chad Versaceeadd5e02014-01-28 17:03:03 -08001139 .create_wayland_buffer_from_image = dri2_fallback_create_wayland_buffer_from_image,
Sarah Sharpc524f3e2014-05-06 12:10:57 -07001140 .get_sync_values = dri2_fallback_get_sync_values,
Boyan Dinga25df542015-07-21 23:43:59 +08001141 .get_dri_drawable = dri2_surface_get_dri_drawable,
Chad Versace90502b12014-01-28 11:41:46 -08001142};
1143
Emil Velikov6a8fe322016-08-16 17:23:23 +01001144static const __DRIdri2LoaderExtension droid_dri2_loader_extension = {
Marek Olšák1bf703e2017-07-28 17:30:34 +02001145 .base = { __DRI_DRI2_LOADER, 4 },
Emil Velikov6a8fe322016-08-16 17:23:23 +01001146
1147 .getBuffers = NULL,
1148 .flushFrontBuffer = droid_flush_front_buffer,
1149 .getBuffersWithFormat = droid_get_buffers_with_format,
Rob Herringbe5773f2017-07-31 09:32:10 -05001150 .getCapability = droid_get_capability,
Emil Velikov6a8fe322016-08-16 17:23:23 +01001151};
1152
Rob Herring34ddef32016-05-01 09:35:28 +01001153static const __DRIimageLoaderExtension droid_image_loader_extension = {
Marek Olšák1bf703e2017-07-28 17:30:34 +02001154 .base = { __DRI_IMAGE_LOADER, 2 },
Rob Herring34ddef32016-05-01 09:35:28 +01001155
1156 .getBuffers = droid_image_get_buffers,
1157 .flushFrontBuffer = droid_flush_front_buffer,
Marek Olšák1bf703e2017-07-28 17:30:34 +02001158 .getCapability = droid_get_capability,
Rob Herring34ddef32016-05-01 09:35:28 +01001159};
1160
Emil Velikovf8719462016-08-24 23:32:27 +01001161static const __DRIextension *droid_dri2_loader_extensions[] = {
1162 &droid_dri2_loader_extension.base,
1163 &image_lookup_extension.base,
1164 &use_invalidate.base,
1165 NULL,
1166};
1167
1168static const __DRIextension *droid_image_loader_extensions[] = {
1169 &droid_image_loader_extension.base,
1170 &image_lookup_extension.base,
1171 &use_invalidate.base,
1172 NULL,
1173};
1174
Chia-I Wu9779f6f2011-08-05 14:39:18 +09001175EGLBoolean
1176dri2_initialize_android(_EGLDriver *drv, _EGLDisplay *dpy)
1177{
1178 struct dri2_egl_display *dri2_dpy;
1179 const char *err;
Tomasz Figa859d0b02016-11-10 16:55:52 +09001180 int ret;
Chia-I Wu9779f6f2011-08-05 14:39:18 +09001181
Emil Velikov8d4357b2014-01-11 04:52:48 +00001182 loader_set_logger(_eglLog);
1183
Chia-I Wu9779f6f2011-08-05 14:39:18 +09001184 dri2_dpy = calloc(1, sizeof(*dri2_dpy));
1185 if (!dri2_dpy)
1186 return _eglError(EGL_BAD_ALLOC, "eglInitialize");
1187
Emil Velikov898d7852017-05-11 17:18:13 +01001188 dri2_dpy->fd = -1;
Tomasz Figa859d0b02016-11-10 16:55:52 +09001189 ret = hw_get_module(GRALLOC_HARDWARE_MODULE_ID,
1190 (const hw_module_t **)&dri2_dpy->gralloc);
1191 if (ret) {
1192 err = "DRI2: failed to get gralloc module";
Emil Velikov898d7852017-05-11 17:18:13 +01001193 goto cleanup;
Tomasz Figa859d0b02016-11-10 16:55:52 +09001194 }
1195
Chia-I Wu9779f6f2011-08-05 14:39:18 +09001196 dpy->DriverData = (void *) dri2_dpy;
1197
Tomasz Figa859d0b02016-11-10 16:55:52 +09001198 dri2_dpy->fd = droid_open_device(dri2_dpy);
Chia-I Wu9779f6f2011-08-05 14:39:18 +09001199 if (dri2_dpy->fd < 0) {
1200 err = "DRI2: failed to open device";
Emil Velikov898d7852017-05-11 17:18:13 +01001201 goto cleanup;
Chia-I Wu9779f6f2011-08-05 14:39:18 +09001202 }
1203
Emil Velikovaf7abc52016-09-12 17:48:18 +01001204 dri2_dpy->driver_name = loader_get_driver_for_fd(dri2_dpy->fd);
Chia-I Wu9779f6f2011-08-05 14:39:18 +09001205 if (dri2_dpy->driver_name == NULL) {
1206 err = "DRI2: failed to get driver name";
Emil Velikov898d7852017-05-11 17:18:13 +01001207 goto cleanup;
Chia-I Wu9779f6f2011-08-05 14:39:18 +09001208 }
1209
Rob Herringd45884e2016-04-28 15:37:28 -05001210 dri2_dpy->is_render_node = drmGetNodeTypeFromFd(dri2_dpy->fd) == DRM_NODE_RENDER;
Chia-I Wu9779f6f2011-08-05 14:39:18 +09001211
Rob Herringd45884e2016-04-28 15:37:28 -05001212 /* render nodes cannot use Gem names, and thus do not support
1213 * the __DRI_DRI2_LOADER extension */
Emil Velikovacf125e2017-05-08 18:39:12 +01001214 if (!dri2_dpy->is_render_node) {
Emil Velikovf8719462016-08-24 23:32:27 +01001215 dri2_dpy->loader_extensions = droid_dri2_loader_extensions;
Emil Velikovacf125e2017-05-08 18:39:12 +01001216 if (!dri2_load_driver(dpy)) {
1217 err = "DRI2: failed to load driver";
1218 goto cleanup;
1219 }
1220 } else {
Emil Velikovf8719462016-08-24 23:32:27 +01001221 dri2_dpy->loader_extensions = droid_image_loader_extensions;
Emil Velikovacf125e2017-05-08 18:39:12 +01001222 if (!dri2_load_driver_dri3(dpy)) {
1223 err = "DRI3: failed to load driver";
1224 goto cleanup;
1225 }
1226 }
Chia-I Wu9779f6f2011-08-05 14:39:18 +09001227
1228 if (!dri2_create_screen(dpy)) {
1229 err = "DRI2: failed to create screen";
Emil Velikov898d7852017-05-11 17:18:13 +01001230 goto cleanup;
Chia-I Wu9779f6f2011-08-05 14:39:18 +09001231 }
1232
Tomasz Figa0ede0f92017-07-05 23:56:51 +09001233 if (!dri2_setup_extensions(dpy)) {
1234 err = "DRI2: failed to setup extensions";
Emil Velikov2c341f22017-05-11 16:20:04 +01001235 goto cleanup;
Tomasz Figa0ede0f92017-07-05 23:56:51 +09001236 }
Emil Velikov2c341f22017-05-11 16:20:04 +01001237
1238 dri2_setup_screen(dpy);
1239
Chia-I Wu9779f6f2011-08-05 14:39:18 +09001240 if (!droid_add_configs_for_visuals(drv, dpy)) {
1241 err = "DRI2: failed to add configs";
Emil Velikov898d7852017-05-11 17:18:13 +01001242 goto cleanup;
Chia-I Wu9779f6f2011-08-05 14:39:18 +09001243 }
1244
Rob Herring952720c2016-02-02 14:23:11 -06001245 dpy->Extensions.ANDROID_framebuffer_target = EGL_TRUE;
Chia-I Wu9779f6f2011-08-05 14:39:18 +09001246 dpy->Extensions.ANDROID_image_native_buffer = EGL_TRUE;
Rob Herring952720c2016-02-02 14:23:11 -06001247 dpy->Extensions.ANDROID_recordable = EGL_TRUE;
Xiaosong Wei2acc69d2017-02-08 10:46:02 +08001248 dpy->Extensions.EXT_buffer_age = EGL_TRUE;
Harish Krupo98275472017-06-09 20:13:34 +05301249#if ANDROID_API_LEVEL >= 23
1250 dpy->Extensions.KHR_partial_update = EGL_TRUE;
1251#endif
Chia-I Wu9779f6f2011-08-05 14:39:18 +09001252
Chad Versace90502b12014-01-28 11:41:46 -08001253 /* Fill vtbl last to prevent accidentally calling virtual function during
1254 * initialization.
1255 */
1256 dri2_dpy->vtbl = &droid_display_vtbl;
1257
Chia-I Wu9779f6f2011-08-05 14:39:18 +09001258 return EGL_TRUE;
1259
Emil Velikov898d7852017-05-11 17:18:13 +01001260cleanup:
1261 dri2_display_destroy(dpy);
Chia-I Wu9779f6f2011-08-05 14:39:18 +09001262 return _eglError(EGL_NOT_INITIALIZED, err);
1263}