Chia-I Wu | 9779f6f | 2011-08-05 14:39:18 +0900 | [diff] [blame] | 1 | /* |
| 2 | * Mesa 3-D graphics library |
Chia-I Wu | 9779f6f | 2011-08-05 14:39:18 +0900 | [diff] [blame] | 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> |
Matt Whitlock | c8fd7d0 | 2016-10-01 23:49:41 -0400 | [diff] [blame] | 32 | #include <fcntl.h> |
Rob Herring | d45884e | 2016-04-28 15:37:28 -0500 | [diff] [blame] | 33 | #include <xf86drm.h> |
Tomasz Figa | 51727b1 | 2016-11-10 16:55:53 +0900 | [diff] [blame] | 34 | #include <stdbool.h> |
Chad Versace | bfe28b8 | 2012-12-20 14:16:50 -0800 | [diff] [blame] | 35 | #include <sync/sync.h> |
Chad Versace | bfe28b8 | 2012-12-20 14:16:50 -0800 | [diff] [blame] | 36 | |
Emil Velikov | 8d4357b | 2014-01-11 04:52:48 +0000 | [diff] [blame] | 37 | #include "loader.h" |
Chia-I Wu | 9779f6f | 2011-08-05 14:39:18 +0900 | [diff] [blame] | 38 | #include "egl_dri2.h" |
Chad Versace | 8b9298a | 2014-01-28 12:34:19 -0800 | [diff] [blame] | 39 | #include "egl_dri2_fallbacks.h" |
Tapani Pälli | 4070295 | 2013-01-24 09:56:47 +0200 | [diff] [blame] | 40 | #include "gralloc_drm.h" |
Chia-I Wu | 9779f6f | 2011-08-05 14:39:18 +0900 | [diff] [blame] | 41 | |
Tomasz Figa | 3723e98 | 2016-08-02 20:07:54 +0900 | [diff] [blame] | 42 | #define ALIGN(val, align) (((val) + (align) - 1) & ~((align) - 1)) |
| 43 | |
Tomasz Figa | 51727b1 | 2016-11-10 16:55:53 +0900 | [diff] [blame] | 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 }, |
Tomasz Figa | 5364e73 | 2017-12-04 19:22:39 +0100 | [diff] [blame^] | 62 | /* 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 Figa | 51727b1 | 2016-11-10 16:55:53 +0900 | [diff] [blame] | 67 | }; |
| 68 | |
| 69 | static int |
| 70 | get_fourcc_yuv(int native, int is_ycrcb, int chroma_step) |
| 71 | { |
Chad Versace | 0945512 | 2017-06-22 11:00:40 -0700 | [diff] [blame] | 72 | for (int i = 0; i < ARRAY_SIZE(droid_yuv_formats); ++i) |
Tomasz Figa | 51727b1 | 2016-11-10 16:55:53 +0900 | [diff] [blame] | 73 | 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 | |
| 81 | static bool |
| 82 | is_yuv(int native) |
| 83 | { |
Chad Versace | 0945512 | 2017-06-22 11:00:40 -0700 | [diff] [blame] | 84 | for (int i = 0; i < ARRAY_SIZE(droid_yuv_formats); ++i) |
Tomasz Figa | 51727b1 | 2016-11-10 16:55:53 +0900 | [diff] [blame] | 85 | if (droid_yuv_formats[i].native == native) |
| 86 | return true; |
| 87 | |
| 88 | return false; |
| 89 | } |
| 90 | |
Chia-I Wu | 9779f6f | 2011-08-05 14:39:18 +0900 | [diff] [blame] | 91 | static int |
| 92 | get_format_bpp(int native) |
| 93 | { |
| 94 | int bpp; |
| 95 | |
| 96 | switch (native) { |
| 97 | case HAL_PIXEL_FORMAT_RGBA_8888: |
Tomasz Figa | 5364e73 | 2017-12-04 19:22:39 +0100 | [diff] [blame^] | 98 | 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 Wu | 9779f6f | 2011-08-05 14:39:18 +0900 | [diff] [blame] | 103 | case HAL_PIXEL_FORMAT_RGBX_8888: |
| 104 | case HAL_PIXEL_FORMAT_BGRA_8888: |
| 105 | bpp = 4; |
| 106 | break; |
Chia-I Wu | 9779f6f | 2011-08-05 14:39:18 +0900 | [diff] [blame] | 107 | case HAL_PIXEL_FORMAT_RGB_565: |
Chia-I Wu | 9779f6f | 2011-08-05 14:39:18 +0900 | [diff] [blame] | 108 | bpp = 2; |
| 109 | break; |
| 110 | default: |
| 111 | bpp = 0; |
| 112 | break; |
| 113 | } |
| 114 | |
| 115 | return bpp; |
| 116 | } |
| 117 | |
Rob Herring | 34ddef3 | 2016-05-01 09:35:28 +0100 | [diff] [blame] | 118 | /* createImageFromFds requires fourcc format */ |
Tomasz Figa | 7dfb1a4 | 2016-08-02 20:07:53 +0900 | [diff] [blame] | 119 | static int get_fourcc(int native) |
Rob Herring | 34ddef3 | 2016-05-01 09:35:28 +0100 | [diff] [blame] | 120 | { |
Tomasz Figa | 7dfb1a4 | 2016-08-02 20:07:53 +0900 | [diff] [blame] | 121 | 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 Figa | 5364e73 | 2017-12-04 19:22:39 +0100 | [diff] [blame^] | 125 | 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 Figa | 7dfb1a4 | 2016-08-02 20:07:53 +0900 | [diff] [blame] | 130 | 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 Herring | 34ddef3 | 2016-05-01 09:35:28 +0100 | [diff] [blame] | 133 | } |
| 134 | return -1; |
| 135 | } |
| 136 | |
Rob Herring | dfaccf2 | 2016-04-28 15:37:29 -0500 | [diff] [blame] | 137 | static 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 Figa | 5364e73 | 2017-12-04 19:22:39 +0100 | [diff] [blame^] | 143 | 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 Herring | dfaccf2 | 2016-04-28 15:37:29 -0500 | [diff] [blame] | 148 | case HAL_PIXEL_FORMAT_RGBX_8888: return __DRI_IMAGE_FORMAT_XBGR8888; |
Rob Herring | dfaccf2 | 2016-04-28 15:37:29 -0500 | [diff] [blame] | 149 | default: |
| 150 | _eglLog(_EGL_WARNING, "unsupported native buffer format 0x%x", format); |
| 151 | } |
| 152 | return -1; |
| 153 | } |
Tomasz Figa | 7dfb1a4 | 2016-08-02 20:07:53 +0900 | [diff] [blame] | 154 | |
Chia-I Wu | 9779f6f | 2011-08-05 14:39:18 +0900 | [diff] [blame] | 155 | static int |
Rob Herring | 34ddef3 | 2016-05-01 09:35:28 +0100 | [diff] [blame] | 156 | get_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 | |
| 167 | static int |
Chia-I Wu | 75cc24c | 2011-11-25 11:59:02 +0800 | [diff] [blame] | 168 | get_native_buffer_name(struct ANativeWindowBuffer *buf) |
Chia-I Wu | 9779f6f | 2011-08-05 14:39:18 +0900 | [diff] [blame] | 169 | { |
Tapani Pälli | 4070295 | 2013-01-24 09:56:47 +0200 | [diff] [blame] | 170 | return gralloc_drm_get_gem_handle(buf->handle); |
Chia-I Wu | 9779f6f | 2011-08-05 14:39:18 +0900 | [diff] [blame] | 171 | } |
| 172 | |
| 173 | static EGLBoolean |
| 174 | droid_window_dequeue_buffer(struct dri2_egl_surface *dri2_surf) |
| 175 | { |
Chad Versace | bfe28b8 | 2012-12-20 14:16:50 -0800 | [diff] [blame] | 176 | 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 Wu | 9779f6f | 2011-08-05 14:39:18 +0900 | [diff] [blame] | 210 | |
Xiaosong Wei | 2acc69d | 2017-02-08 10:46:02 +0800 | [diff] [blame] | 211 | /* 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 Wu | 9779f6f | 2011-08-05 14:39:18 +0900 | [diff] [blame] | 238 | return EGL_TRUE; |
| 239 | } |
| 240 | |
| 241 | static EGLBoolean |
Haixia Shi | 1ea233c | 2016-06-02 12:48:23 -0700 | [diff] [blame] | 242 | droid_window_enqueue_buffer(_EGLDisplay *disp, struct dri2_egl_surface *dri2_surf) |
Chia-I Wu | 9779f6f | 2011-08-05 14:39:18 +0900 | [diff] [blame] | 243 | { |
Tomasz Figa | 9e1248d | 2016-07-15 16:53:50 +0900 | [diff] [blame] | 244 | struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp); |
| 245 | |
Haixia Shi | 1ea233c | 2016-06-02 12:48:23 -0700 | [diff] [blame] | 246 | /* 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 Wu | 7343d27 | 2017-09-15 18:32:43 +0100 | [diff] [blame] | 252 | /* 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 Versace | bfe28b8 | 2012-12-20 14:16:50 -0800 | [diff] [blame] | 257 | * |
Zhongmin Wu | 7343d27 | 2017-09-15 18:32:43 +0100 | [diff] [blame] | 258 | * 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 Versace | bfe28b8 | 2012-12-20 14:16:50 -0800 | [diff] [blame] | 261 | */ |
Zhongmin Wu | 7343d27 | 2017-09-15 18:32:43 +0100 | [diff] [blame] | 262 | int fence_fd = dri2_surf->out_fence_fd; |
| 263 | dri2_surf->out_fence_fd = -1; |
Chad Versace | bfe28b8 | 2012-12-20 14:16:50 -0800 | [diff] [blame] | 264 | dri2_surf->window->queueBuffer(dri2_surf->window, dri2_surf->buffer, |
| 265 | fence_fd); |
Chia-I Wu | 9779f6f | 2011-08-05 14:39:18 +0900 | [diff] [blame] | 266 | |
| 267 | dri2_surf->buffer->common.decRef(&dri2_surf->buffer->common); |
| 268 | dri2_surf->buffer = NULL; |
Xiaosong Wei | 2acc69d | 2017-02-08 10:46:02 +0800 | [diff] [blame] | 269 | dri2_surf->back = NULL; |
Chia-I Wu | 9779f6f | 2011-08-05 14:39:18 +0900 | [diff] [blame] | 270 | |
Haixia Shi | 1ea233c | 2016-06-02 12:48:23 -0700 | [diff] [blame] | 271 | mtx_lock(&disp->Mutex); |
Tomasz Figa | 9e1248d | 2016-07-15 16:53:50 +0900 | [diff] [blame] | 272 | |
Liu Zhiquan | b663753 | 2016-11-16 10:11:28 +0800 | [diff] [blame] | 273 | if (dri2_surf->dri_image_back) { |
| 274 | dri2_dpy->image->destroyImage(dri2_surf->dri_image_back); |
| 275 | dri2_surf->dri_image_back = NULL; |
Tomasz Figa | 9e1248d | 2016-07-15 16:53:50 +0900 | [diff] [blame] | 276 | } |
| 277 | |
Chia-I Wu | 9779f6f | 2011-08-05 14:39:18 +0900 | [diff] [blame] | 278 | return EGL_TRUE; |
| 279 | } |
| 280 | |
| 281 | static void |
Chad Versace | 0212db3 | 2017-05-04 17:46:33 -0700 | [diff] [blame] | 282 | droid_window_cancel_buffer(struct dri2_egl_surface *dri2_surf) |
Chia-I Wu | 9779f6f | 2011-08-05 14:39:18 +0900 | [diff] [blame] | 283 | { |
Chad Versace | 0212db3 | 2017-05-04 17:46:33 -0700 | [diff] [blame] | 284 | int ret; |
Zhongmin Wu | 7343d27 | 2017-09-15 18:32:43 +0100 | [diff] [blame] | 285 | int fence_fd = dri2_surf->out_fence_fd; |
Chad Versace | 0212db3 | 2017-05-04 17:46:33 -0700 | [diff] [blame] | 286 | |
Zhongmin Wu | 7343d27 | 2017-09-15 18:32:43 +0100 | [diff] [blame] | 287 | dri2_surf->out_fence_fd = -1; |
| 288 | ret = dri2_surf->window->cancelBuffer(dri2_surf->window, |
| 289 | dri2_surf->buffer, fence_fd); |
Chad Versace | 0212db3 | 2017-05-04 17:46:33 -0700 | [diff] [blame] | 290 | if (ret < 0) { |
| 291 | _eglLog(_EGL_WARNING, "ANativeWindow::cancelBuffer failed"); |
Nicolas Boichat | 63b12b0 | 2017-05-05 10:43:50 +0800 | [diff] [blame] | 292 | dri2_surf->base.Lost = EGL_TRUE; |
Chad Versace | 0212db3 | 2017-05-04 17:46:33 -0700 | [diff] [blame] | 293 | } |
Chia-I Wu | 9779f6f | 2011-08-05 14:39:18 +0900 | [diff] [blame] | 294 | } |
| 295 | |
Chia-I Wu | 9779f6f | 2011-08-05 14:39:18 +0900 | [diff] [blame] | 296 | static _EGLSurface * |
| 297 | droid_create_surface(_EGLDriver *drv, _EGLDisplay *disp, EGLint type, |
Chad Versace | 6d1f83e | 2014-01-07 14:54:51 -0800 | [diff] [blame] | 298 | _EGLConfig *conf, void *native_window, |
Chia-I Wu | 9779f6f | 2011-08-05 14:39:18 +0900 | [diff] [blame] | 299 | const EGLint *attrib_list) |
| 300 | { |
Emil Velikov | acf125e | 2017-05-08 18:39:12 +0100 | [diff] [blame] | 301 | __DRIcreateNewDrawableFunc createNewDrawable; |
Chia-I Wu | 9779f6f | 2011-08-05 14:39:18 +0900 | [diff] [blame] | 302 | 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 Versace | 6d1f83e | 2014-01-07 14:54:51 -0800 | [diff] [blame] | 305 | struct ANativeWindow *window = native_window; |
Marek Olšák | c2c2e9a | 2015-06-10 02:49:29 +0200 | [diff] [blame] | 306 | const __DRIconfig *config; |
Chad Versace | 6d1f83e | 2014-01-07 14:54:51 -0800 | [diff] [blame] | 307 | |
Chia-I Wu | 9779f6f | 2011-08-05 14:39:18 +0900 | [diff] [blame] | 308 | 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 Wu | 7343d27 | 2017-09-15 18:32:43 +0100 | [diff] [blame] | 314 | if (!dri2_init_surface(&dri2_surf->base, disp, type, conf, attrib_list, true)) |
Chia-I Wu | c8e18f8 | 2011-08-26 22:09:56 +0800 | [diff] [blame] | 315 | goto cleanup_surface; |
| 316 | |
| 317 | if (type == EGL_WINDOW_BIT) { |
| 318 | int format; |
| 319 | |
Emil Velikov | 92b2368 | 2017-08-05 00:25:47 +0100 | [diff] [blame] | 320 | if (window->common.magic != ANDROID_NATIVE_WINDOW_MAGIC) { |
Chia-I Wu | c8e18f8 | 2011-08-26 22:09:56 +0800 | [diff] [blame] | 321 | _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 Wu | 9779f6f | 2011-08-05 14:39:18 +0900 | [diff] [blame] | 337 | |
Liu Zhiquan | b663753 | 2016-11-16 10:11:28 +0800 | [diff] [blame] | 338 | config = dri2_get_dri_config(dri2_conf, type, |
Marek Olšák | c2c2e9a | 2015-06-10 02:49:29 +0200 | [diff] [blame] | 339 | dri2_surf->base.GLColorspace); |
Tomasz Figa | 94282b6 | 2016-07-15 16:53:48 +0900 | [diff] [blame] | 340 | if (!config) |
| 341 | goto cleanup_surface; |
Marek Olšák | c2c2e9a | 2015-06-10 02:49:29 +0200 | [diff] [blame] | 342 | |
Emil Velikov | acf125e | 2017-05-08 18:39:12 +0100 | [diff] [blame] | 343 | 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 Wu | 9779f6f | 2011-08-05 14:39:18 +0900 | [diff] [blame] | 350 | if (dri2_surf->dri_drawable == NULL) { |
Emil Velikov | acf125e | 2017-05-08 18:39:12 +0100 | [diff] [blame] | 351 | _eglError(EGL_BAD_ALLOC, "createNewDrawable"); |
Chia-I Wu | c8e18f8 | 2011-08-26 22:09:56 +0800 | [diff] [blame] | 352 | goto cleanup_surface; |
Chia-I Wu | 9779f6f | 2011-08-05 14:39:18 +0900 | [diff] [blame] | 353 | } |
| 354 | |
Chia-I Wu | c8e18f8 | 2011-08-26 22:09:56 +0800 | [diff] [blame] | 355 | if (window) { |
| 356 | window->common.incRef(&window->common); |
| 357 | dri2_surf->window = window; |
| 358 | } |
Chia-I Wu | 9779f6f | 2011-08-05 14:39:18 +0900 | [diff] [blame] | 359 | |
| 360 | return &dri2_surf->base; |
| 361 | |
Chia-I Wu | c8e18f8 | 2011-08-26 22:09:56 +0800 | [diff] [blame] | 362 | cleanup_surface: |
Chia-I Wu | 9779f6f | 2011-08-05 14:39:18 +0900 | [diff] [blame] | 363 | free(dri2_surf); |
| 364 | |
| 365 | return NULL; |
| 366 | } |
| 367 | |
| 368 | static _EGLSurface * |
| 369 | droid_create_window_surface(_EGLDriver *drv, _EGLDisplay *disp, |
Chad Versace | 6d1f83e | 2014-01-07 14:54:51 -0800 | [diff] [blame] | 370 | _EGLConfig *conf, void *native_window, |
| 371 | const EGLint *attrib_list) |
Chia-I Wu | 9779f6f | 2011-08-05 14:39:18 +0900 | [diff] [blame] | 372 | { |
| 373 | return droid_create_surface(drv, disp, EGL_WINDOW_BIT, conf, |
Chad Versace | 6d1f83e | 2014-01-07 14:54:51 -0800 | [diff] [blame] | 374 | native_window, attrib_list); |
Chia-I Wu | 9779f6f | 2011-08-05 14:39:18 +0900 | [diff] [blame] | 375 | } |
| 376 | |
| 377 | static _EGLSurface * |
Chia-I Wu | 9779f6f | 2011-08-05 14:39:18 +0900 | [diff] [blame] | 378 | droid_create_pbuffer_surface(_EGLDriver *drv, _EGLDisplay *disp, |
| 379 | _EGLConfig *conf, const EGLint *attrib_list) |
| 380 | { |
Chia-I Wu | 93d5963 | 2011-08-27 00:00:18 +0800 | [diff] [blame] | 381 | return droid_create_surface(drv, disp, EGL_PBUFFER_BIT, conf, |
| 382 | NULL, attrib_list); |
Chia-I Wu | 9779f6f | 2011-08-05 14:39:18 +0900 | [diff] [blame] | 383 | } |
| 384 | |
| 385 | static EGLBoolean |
| 386 | droid_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 Mun | 640b6e6 | 2017-08-05 00:16:11 +0900 | [diff] [blame] | 391 | dri2_egl_surface_free_local_buffers(dri2_surf); |
Chia-I Wu | 9779f6f | 2011-08-05 14:39:18 +0900 | [diff] [blame] | 392 | |
Chia-I Wu | c8e18f8 | 2011-08-26 22:09:56 +0800 | [diff] [blame] | 393 | if (dri2_surf->base.Type == EGL_WINDOW_BIT) { |
| 394 | if (dri2_surf->buffer) |
Chad Versace | 0212db3 | 2017-05-04 17:46:33 -0700 | [diff] [blame] | 395 | droid_window_cancel_buffer(dri2_surf); |
Chia-I Wu | 9779f6f | 2011-08-05 14:39:18 +0900 | [diff] [blame] | 396 | |
Chia-I Wu | c8e18f8 | 2011-08-26 22:09:56 +0800 | [diff] [blame] | 397 | dri2_surf->window->common.decRef(&dri2_surf->window->common); |
| 398 | } |
Chia-I Wu | 9779f6f | 2011-08-05 14:39:18 +0900 | [diff] [blame] | 399 | |
Liu Zhiquan | b663753 | 2016-11-16 10:11:28 +0800 | [diff] [blame] | 400 | 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 Ding | 868ae3e | 2015-11-25 13:27:02 +0800 | [diff] [blame] | 412 | dri2_dpy->core->destroyDrawable(dri2_surf->dri_drawable); |
Chia-I Wu | 9779f6f | 2011-08-05 14:39:18 +0900 | [diff] [blame] | 413 | |
Zhongmin Wu | e013ce8 | 2017-09-15 18:32:42 +0100 | [diff] [blame] | 414 | dri2_fini_surface(surf); |
Chia-I Wu | 9779f6f | 2011-08-05 14:39:18 +0900 | [diff] [blame] | 415 | free(dri2_surf); |
| 416 | |
| 417 | return EGL_TRUE; |
| 418 | } |
| 419 | |
Rob Herring | 81a6fff | 2016-04-28 15:37:30 -0500 | [diff] [blame] | 420 | static int |
| 421 | update_buffers(struct dri2_egl_surface *dri2_surf) |
| 422 | { |
Chad Versace | e5eace5 | 2017-05-04 17:46:34 -0700 | [diff] [blame] | 423 | if (dri2_surf->base.Lost) |
| 424 | return -1; |
| 425 | |
Rob Herring | 81a6fff | 2016-04-28 15:37:30 -0500 | [diff] [blame] | 426 | if (dri2_surf->base.Type != EGL_WINDOW_BIT) |
| 427 | return 0; |
| 428 | |
| 429 | /* try to dequeue the next back buffer */ |
Tomasz Figa | 565fa6b | 2016-07-15 16:53:49 +0900 | [diff] [blame] | 430 | if (!dri2_surf->buffer && !droid_window_dequeue_buffer(dri2_surf)) { |
| 431 | _eglLog(_EGL_WARNING, "Could not dequeue buffer from native window"); |
Nicolas Boichat | 63b12b0 | 2017-05-05 10:43:50 +0800 | [diff] [blame] | 432 | dri2_surf->base.Lost = EGL_TRUE; |
Rob Herring | 81a6fff | 2016-04-28 15:37:30 -0500 | [diff] [blame] | 433 | return -1; |
Tomasz Figa | 565fa6b | 2016-07-15 16:53:49 +0900 | [diff] [blame] | 434 | } |
Rob Herring | 81a6fff | 2016-04-28 15:37:30 -0500 | [diff] [blame] | 435 | |
| 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 Mun | 640b6e6 | 2017-08-05 00:16:11 +0900 | [diff] [blame] | 439 | dri2_egl_surface_free_local_buffers(dri2_surf); |
Rob Herring | 81a6fff | 2016-04-28 15:37:30 -0500 | [diff] [blame] | 440 | 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 Herring | 34ddef3 | 2016-05-01 09:35:28 +0100 | [diff] [blame] | 447 | static int |
Liu Zhiquan | e2610bf | 2016-12-09 19:29:56 +0800 | [diff] [blame] | 448 | get_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 | |
| 480 | static int |
Chad Versace | 22d6b08 | 2017-05-23 17:26:52 -0700 | [diff] [blame] | 481 | get_back_bo(struct dri2_egl_surface *dri2_surf) |
Rob Herring | 34ddef3 | 2016-05-01 09:35:28 +0100 | [diff] [blame] | 482 | { |
| 483 | struct dri2_egl_display *dri2_dpy = |
| 484 | dri2_egl_display(dri2_surf->base.Resource.Display); |
Emil Velikov | fb65364 | 2016-05-01 09:35:56 +0100 | [diff] [blame] | 485 | int fourcc, pitch; |
Rob Herring | 34ddef3 | 2016-05-01 09:35:28 +0100 | [diff] [blame] | 486 | int offset = 0, fd; |
| 487 | |
Liu Zhiquan | b663753 | 2016-11-16 10:11:28 +0800 | [diff] [blame] | 488 | if (dri2_surf->dri_image_back) |
| 489 | return 0; |
Tomasz Figa | 9e1248d | 2016-07-15 16:53:50 +0900 | [diff] [blame] | 490 | |
Liu Zhiquan | e2610bf | 2016-12-09 19:29:56 +0800 | [diff] [blame] | 491 | 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 Zhiquan | b663753 | 2016-11-16 10:11:28 +0800 | [diff] [blame] | 495 | } |
| 496 | |
Liu Zhiquan | e2610bf | 2016-12-09 19:29:56 +0800 | [diff] [blame] | 497 | 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 Zhiquan | b663753 | 2016-11-16 10:11:28 +0800 | [diff] [blame] | 529 | /* 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 Figa | 217af75 | 2016-08-02 20:07:50 +0900 | [diff] [blame] | 541 | */ |
Liu Zhiquan | e2610bf | 2016-12-09 19:29:56 +0800 | [diff] [blame] | 542 | _eglLog(_EGL_DEBUG, "DRI driver requested unsupported back buffer for pbuffer surface"); |
| 543 | } |
Liu Zhiquan | b663753 | 2016-11-16 10:11:28 +0800 | [diff] [blame] | 544 | |
Liu Zhiquan | e2610bf | 2016-12-09 19:29:56 +0800 | [diff] [blame] | 545 | 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 | */ |
| 553 | static int |
| 554 | droid_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 Zhiquan | b663753 | 2016-11-16 10:11:28 +0800 | [diff] [blame] | 572 | return 0; |
Liu Zhiquan | b663753 | 2016-11-16 10:11:28 +0800 | [diff] [blame] | 573 | |
Liu Zhiquan | e2610bf | 2016-12-09 19:29:56 +0800 | [diff] [blame] | 574 | if (dri2_surf->dri_image_front) { |
| 575 | images->front = dri2_surf->dri_image_front; |
| 576 | images->image_mask |= __DRI_IMAGE_BUFFER_FRONT; |
| 577 | } |
Rob Herring | 34ddef3 | 2016-05-01 09:35:28 +0100 | [diff] [blame] | 578 | } |
| 579 | |
Tomasz Figa | 217af75 | 2016-08-02 20:07:50 +0900 | [diff] [blame] | 580 | if (buffer_mask & __DRI_IMAGE_BUFFER_BACK) { |
Chad Versace | 22d6b08 | 2017-05-23 17:26:52 -0700 | [diff] [blame] | 581 | if (get_back_bo(dri2_surf) < 0) |
Liu Zhiquan | b663753 | 2016-11-16 10:11:28 +0800 | [diff] [blame] | 582 | return 0; |
Liu Zhiquan | b663753 | 2016-11-16 10:11:28 +0800 | [diff] [blame] | 583 | |
Liu Zhiquan | e2610bf | 2016-12-09 19:29:56 +0800 | [diff] [blame] | 584 | if (dri2_surf->dri_image_back) { |
| 585 | images->back = dri2_surf->dri_image_back; |
| 586 | images->image_mask |= __DRI_IMAGE_BUFFER_BACK; |
| 587 | } |
Tomasz Figa | 217af75 | 2016-08-02 20:07:50 +0900 | [diff] [blame] | 588 | } |
Rob Herring | 34ddef3 | 2016-05-01 09:35:28 +0100 | [diff] [blame] | 589 | |
| 590 | return 1; |
| 591 | } |
| 592 | |
Xiaosong Wei | 2acc69d | 2017-02-08 10:46:02 +0800 | [diff] [blame] | 593 | static EGLint |
| 594 | droid_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älli | 8fac894 | 2017-06-08 12:24:24 +0300 | [diff] [blame] | 601 | return -1; |
Xiaosong Wei | 2acc69d | 2017-02-08 10:46:02 +0800 | [diff] [blame] | 602 | } |
| 603 | |
Tapani Pälli | 8fac894 | 2017-06-08 12:24:24 +0300 | [diff] [blame] | 604 | return dri2_surf->back ? dri2_surf->back->age : 0; |
Xiaosong Wei | 2acc69d | 2017-02-08 10:46:02 +0800 | [diff] [blame] | 605 | } |
| 606 | |
Chia-I Wu | 9779f6f | 2011-08-05 14:39:18 +0900 | [diff] [blame] | 607 | static EGLBoolean |
| 608 | droid_swap_buffers(_EGLDriver *drv, _EGLDisplay *disp, _EGLSurface *draw) |
| 609 | { |
Chia-I Wu | 9779f6f | 2011-08-05 14:39:18 +0900 | [diff] [blame] | 610 | struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp); |
| 611 | struct dri2_egl_surface *dri2_surf = dri2_egl_surface(draw); |
Chia-I Wu | 9779f6f | 2011-08-05 14:39:18 +0900 | [diff] [blame] | 612 | |
Chia-I Wu | c8e18f8 | 2011-08-26 22:09:56 +0800 | [diff] [blame] | 613 | if (dri2_surf->base.Type != EGL_WINDOW_BIT) |
| 614 | return EGL_TRUE; |
| 615 | |
Xiaosong Wei | 2acc69d | 2017-02-08 10:46:02 +0800 | [diff] [blame] | 616 | 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älli | f347bac | 2017-05-18 10:21:59 +0300 | [diff] [blame] | 620 | |
| 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 Wei | 2acc69d | 2017-02-08 10:46:02 +0800 | [diff] [blame] | 626 | |
Eric Anholt | 70e8ccc | 2014-12-21 11:51:33 -0800 | [diff] [blame] | 627 | dri2_flush_drawable_for_swapbuffers(disp, draw); |
Chia-I Wu | 9779f6f | 2011-08-05 14:39:18 +0900 | [diff] [blame] | 628 | |
Chad Versace | e5eace5 | 2017-05-04 17:46:34 -0700 | [diff] [blame] | 629 | /* 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 Wu | 9779f6f | 2011-08-05 14:39:18 +0900 | [diff] [blame] | 635 | if (dri2_surf->buffer) |
Haixia Shi | 1ea233c | 2016-06-02 12:48:23 -0700 | [diff] [blame] | 636 | droid_window_enqueue_buffer(disp, dri2_surf); |
Chia-I Wu | 9779f6f | 2011-08-05 14:39:18 +0900 | [diff] [blame] | 637 | |
Boyan Ding | 868ae3e | 2015-11-25 13:27:02 +0800 | [diff] [blame] | 638 | dri2_dpy->flush->invalidate(dri2_surf->dri_drawable); |
Chia-I Wu | 9779f6f | 2011-08-05 14:39:18 +0900 | [diff] [blame] | 639 | |
| 640 | return EGL_TRUE; |
| 641 | } |
| 642 | |
Harish Krupo | 9827547 | 2017-06-09 20:13:34 +0530 | [diff] [blame] | 643 | #if ANDROID_API_LEVEL >= 23 |
| 644 | static EGLBoolean |
| 645 | droid_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 Velikov | c58af5c | 2017-06-20 15:40:28 +0100 | [diff] [blame] | 658 | if (droid_rects == NULL) |
| 659 | return _eglError(EGL_BAD_ALLOC, "eglSetDamageRegionKHR"); |
Harish Krupo | 9827547 | 2017-06-09 20:13:34 +0530 | [diff] [blame] | 660 | |
| 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 Wu | 9779f6f | 2011-08-05 14:39:18 +0900 | [diff] [blame] | 680 | static _EGLImage * |
Tomasz Figa | 51727b1 | 2016-11-10 16:55:53 +0900 | [diff] [blame] | 681 | droid_create_image_from_prime_fd_yuv(_EGLDisplay *disp, _EGLContext *ctx, |
| 682 | struct ANativeWindowBuffer *buf, int fd) |
Tomasz Figa | e77b493 | 2016-08-02 20:07:52 +0900 | [diff] [blame] | 683 | { |
Tomasz Figa | 51727b1 | 2016-11-10 16:55:53 +0900 | [diff] [blame] | 684 | 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 Figa | e77b493 | 2016-08-02 20:07:52 +0900 | [diff] [blame] | 691 | |
Tomasz Figa | 51727b1 | 2016-11-10 16:55:53 +0900 | [diff] [blame] | 692 | 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 Figa | 5364e73 | 2017-12-04 19:22:39 +0100 | [diff] [blame^] | 701 | /* 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 Figa | 51727b1 | 2016-11-10 16:55:53 +0900 | [diff] [blame] | 706 | _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älli | 0a2dcd3 | 2017-02-02 14:05:46 +0200 | [diff] [blame] | 716 | is_ycrcb = (size_t)ycbcr.cr < (size_t)ycbcr.cb; |
Tomasz Figa | 51727b1 | 2016-11-10 16:55:53 +0900 | [diff] [blame] | 717 | 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 Figa | 3723e98 | 2016-08-02 20:07:54 +0900 | [diff] [blame] | 735 | if (fourcc == -1) { |
Tomasz Figa | 51727b1 | 2016-11-10 16:55:53 +0900 | [diff] [blame] | 736 | _eglLog(_EGL_WARNING, "unsupported YUV format, native = %x, is_ycrcb = %d, chroma_step = %d", |
| 737 | buf->format, is_ycrcb, ycbcr.chroma_step); |
Tomasz Figa | 3723e98 | 2016-08-02 20:07:54 +0900 | [diff] [blame] | 738 | return NULL; |
| 739 | } |
| 740 | |
Tomasz Figa | 51727b1 | 2016-11-10 16:55:53 +0900 | [diff] [blame] | 741 | if (ycbcr.chroma_step == 2) { |
Robert Foss | a4d3371 | 2017-06-15 16:47:53 -0400 | [diff] [blame] | 742 | /* Semi-planar Y + CbCr or Y + CrCb format. */ |
Tomasz Figa | 51727b1 | 2016-11-10 16:55:53 +0900 | [diff] [blame] | 743 | 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 Figa | 3723e98 | 2016-08-02 20:07:54 +0900 | [diff] [blame] | 755 | |
Tomasz Figa | 51727b1 | 2016-11-10 16:55:53 +0900 | [diff] [blame] | 756 | 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 Figa | 3723e98 | 2016-08-02 20:07:54 +0900 | [diff] [blame] | 760 | 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 Figa | 51727b1 | 2016-11-10 16:55:53 +0900 | [diff] [blame] | 775 | return dri2_create_image_dma_buf(disp, ctx, NULL, attr_list_3plane); |
| 776 | } |
| 777 | } |
| 778 | |
| 779 | static _EGLImage * |
| 780 | droid_create_image_from_prime_fd(_EGLDisplay *disp, _EGLContext *ctx, |
| 781 | struct ANativeWindowBuffer *buf, int fd) |
| 782 | { |
| 783 | unsigned int pitch; |
| 784 | |
Tomasz Figa | 5364e73 | 2017-12-04 19:22:39 +0100 | [diff] [blame^] | 785 | 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 Figa | 51727b1 | 2016-11-10 16:55:53 +0900 | [diff] [blame] | 799 | |
| 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 Figa | 3723e98 | 2016-08-02 20:07:54 +0900 | [diff] [blame] | 810 | } |
| 811 | |
| 812 | const EGLint attr_list[] = { |
Tomasz Figa | e77b493 | 2016-08-02 20:07:52 +0900 | [diff] [blame] | 813 | 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 Figa | 51727b1 | 2016-11-10 16:55:53 +0900 | [diff] [blame] | 817 | EGL_DMA_BUF_PLANE0_PITCH_EXT, pitch, |
Tomasz Figa | e77b493 | 2016-08-02 20:07:52 +0900 | [diff] [blame] | 818 | EGL_DMA_BUF_PLANE0_OFFSET_EXT, 0, |
| 819 | EGL_NONE, 0 |
| 820 | }; |
| 821 | |
Tomasz Figa | e77b493 | 2016-08-02 20:07:52 +0900 | [diff] [blame] | 822 | return dri2_create_image_dma_buf(disp, ctx, NULL, attr_list); |
| 823 | } |
| 824 | |
| 825 | static _EGLImage * |
| 826 | droid_create_image_from_name(_EGLDisplay *disp, _EGLContext *ctx, |
| 827 | struct ANativeWindowBuffer *buf) |
Chia-I Wu | 9779f6f | 2011-08-05 14:39:18 +0900 | [diff] [blame] | 828 | { |
| 829 | struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp); |
| 830 | struct dri2_egl_image *dri2_img; |
Tomasz Figa | e77b493 | 2016-08-02 20:07:52 +0900 | [diff] [blame] | 831 | int name; |
Emil Velikov | fb65364 | 2016-05-01 09:35:56 +0100 | [diff] [blame] | 832 | int format; |
Chia-I Wu | 9779f6f | 2011-08-05 14:39:18 +0900 | [diff] [blame] | 833 | |
Chia-I Wu | 9779f6f | 2011-08-05 14:39:18 +0900 | [diff] [blame] | 834 | name = get_native_buffer_name(buf); |
| 835 | if (!name) { |
| 836 | _eglError(EGL_BAD_PARAMETER, "eglCreateEGLImageKHR"); |
| 837 | return NULL; |
| 838 | } |
| 839 | |
Emil Velikov | fb65364 | 2016-05-01 09:35:56 +0100 | [diff] [blame] | 840 | format = get_format(buf->format); |
| 841 | if (format == -1) |
| 842 | return NULL; |
| 843 | |
Chia-I Wu | 9779f6f | 2011-08-05 14:39:18 +0900 | [diff] [blame] | 844 | 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 Velikov | d42b095 | 2017-06-20 15:22:39 +0100 | [diff] [blame] | 850 | _eglInitImage(&dri2_img->base, disp); |
Chia-I Wu | 9779f6f | 2011-08-05 14:39:18 +0900 | [diff] [blame] | 851 | |
| 852 | dri2_img->dri_image = |
| 853 | dri2_dpy->image->createImageFromName(dri2_dpy->dri_screen, |
| 854 | buf->width, |
| 855 | buf->height, |
Emil Velikov | fb65364 | 2016-05-01 09:35:56 +0100 | [diff] [blame] | 856 | format, |
Chia-I Wu | 9779f6f | 2011-08-05 14:39:18 +0900 | [diff] [blame] | 857 | 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 Shi | a7c6993 | 2016-07-28 10:51:12 -0700 | [diff] [blame] | 869 | static EGLBoolean |
| 870 | droid_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 Wu | 9779f6f | 2011-08-05 14:39:18 +0900 | [diff] [blame] | 895 | static _EGLImage * |
Tomasz Figa | e77b493 | 2016-08-02 20:07:52 +0900 | [diff] [blame] | 896 | dri2_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 | |
| 927 | static _EGLImage * |
Chia-I Wu | 9779f6f | 2011-08-05 14:39:18 +0900 | [diff] [blame] | 928 | droid_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 Velikov | 9b3c748 | 2016-05-01 12:42:54 +0100 | [diff] [blame] | 934 | return dri2_create_image_android_native_buffer(disp, ctx, |
Chia-I Wu | 75cc24c | 2011-11-25 11:59:02 +0800 | [diff] [blame] | 935 | (struct ANativeWindowBuffer *) buffer); |
Chia-I Wu | 9779f6f | 2011-08-05 14:39:18 +0900 | [diff] [blame] | 936 | default: |
| 937 | return dri2_create_image_khr(drv, disp, ctx, target, buffer, attr_list); |
| 938 | } |
| 939 | } |
| 940 | |
| 941 | static void |
Chia-I Wu | 9779f6f | 2011-08-05 14:39:18 +0900 | [diff] [blame] | 942 | droid_flush_front_buffer(__DRIdrawable * driDrawable, void *loaderPrivate) |
| 943 | { |
| 944 | } |
| 945 | |
Chia-I Wu | 384f228 | 2011-08-26 23:49:04 +0800 | [diff] [blame] | 946 | static int |
| 947 | droid_get_buffers_parse_attachments(struct dri2_egl_surface *dri2_surf, |
| 948 | unsigned int *attachments, int count) |
| 949 | { |
Chad Versace | 0945512 | 2017-06-22 11:00:40 -0700 | [diff] [blame] | 950 | int num_buffers = 0; |
Chia-I Wu | 384f228 | 2011-08-26 23:49:04 +0800 | [diff] [blame] | 951 | |
| 952 | /* fill dri2_surf->buffers */ |
Chad Versace | 0945512 | 2017-06-22 11:00:40 -0700 | [diff] [blame] | 953 | for (int i = 0; i < count * 2; i += 2) { |
Chia-I Wu | 384f228 | 2011-08-26 23:49:04 +0800 | [diff] [blame] | 954 | __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 Wu | c8e18f8 | 2011-08-26 22:09:56 +0800 | [diff] [blame] | 961 | 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 Wu | 384f228 | 2011-08-26 23:49:04 +0800 | [diff] [blame] | 967 | |
Chia-I Wu | c8e18f8 | 2011-08-26 22:09:56 +0800 | [diff] [blame] | 968 | if (buf->name) |
| 969 | num_buffers++; |
Chia-I Wu | 93d5963 | 2011-08-27 00:00:18 +0800 | [diff] [blame] | 970 | |
| 971 | break; |
Chia-I Wu | c8e18f8 | 2011-08-26 22:09:56 +0800 | [diff] [blame] | 972 | } |
Chia-I Wu | 93d5963 | 2011-08-27 00:00:18 +0800 | [diff] [blame] | 973 | /* fall through for pbuffers */ |
Chia-I Wu | 384f228 | 2011-08-26 23:49:04 +0800 | [diff] [blame] | 974 | 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 Mun | 640b6e6 | 2017-08-05 00:16:11 +0900 | [diff] [blame] | 979 | local = dri2_egl_surface_alloc_local_buffer(dri2_surf, |
Chia-I Wu | 384f228 | 2011-08-26 23:49:04 +0800 | [diff] [blame] | 980 | 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 Wu | 9779f6f | 2011-08-05 14:39:18 +0900 | [diff] [blame] | 1001 | static __DRIbuffer * |
| 1002 | droid_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 Wu | 9779f6f | 2011-08-05 14:39:18 +0900 | [diff] [blame] | 1008 | |
Rob Herring | 81a6fff | 2016-04-28 15:37:30 -0500 | [diff] [blame] | 1009 | if (update_buffers(dri2_surf) < 0) |
| 1010 | return NULL; |
Chia-I Wu | 9779f6f | 2011-08-05 14:39:18 +0900 | [diff] [blame] | 1011 | |
Gwan-gyeong Mun | 3f6cc93 | 2017-07-18 17:12:26 +0900 | [diff] [blame] | 1012 | *out_count = droid_get_buffers_parse_attachments(dri2_surf, attachments, count); |
Chia-I Wu | 9779f6f | 2011-08-05 14:39:18 +0900 | [diff] [blame] | 1013 | |
| 1014 | if (width) |
| 1015 | *width = dri2_surf->base.Width; |
| 1016 | if (height) |
| 1017 | *height = dri2_surf->base.Height; |
| 1018 | |
Chia-I Wu | 9779f6f | 2011-08-05 14:39:18 +0900 | [diff] [blame] | 1019 | return dri2_surf->buffers; |
| 1020 | } |
| 1021 | |
Marek Olšák | 1bf703e | 2017-07-28 17:30:34 +0200 | [diff] [blame] | 1022 | static unsigned |
| 1023 | droid_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 Wu | 9779f6f | 2011-08-05 14:39:18 +0900 | [diff] [blame] | 1034 | static EGLBoolean |
| 1035 | droid_add_configs_for_visuals(_EGLDriver *drv, _EGLDisplay *dpy) |
| 1036 | { |
| 1037 | struct dri2_egl_display *dri2_dpy = dri2_egl_display(dpy); |
Emil Velikov | 4472b6e | 2016-08-25 12:08:07 +0100 | [diff] [blame] | 1038 | static const struct { |
Chia-I Wu | 9779f6f | 2011-08-05 14:39:18 +0900 | [diff] [blame] | 1039 | int format; |
Chia-I Wu | 9779f6f | 2011-08-05 14:39:18 +0900 | [diff] [blame] | 1040 | unsigned int rgba_masks[4]; |
| 1041 | } visuals[] = { |
Chad Versace | 0bcdceb | 2017-05-23 15:50:13 -0700 | [diff] [blame] | 1042 | { 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 Wu | 9779f6f | 2011-08-05 14:39:18 +0900 | [diff] [blame] | 1046 | }; |
Chad Versace | 0945512 | 2017-06-22 11:00:40 -0700 | [diff] [blame] | 1047 | |
Emil Velikov | acd35c8 | 2016-08-25 14:28:48 +0100 | [diff] [blame] | 1048 | unsigned int format_count[ARRAY_SIZE(visuals)] = { 0 }; |
Chad Versace | ffbf50b | 2017-06-22 11:00:41 -0700 | [diff] [blame] | 1049 | int config_count = 0; |
Chia-I Wu | 9779f6f | 2011-08-05 14:39:18 +0900 | [diff] [blame] | 1050 | |
Chad Versace | 5e88435 | 2017-06-16 19:11:21 -0700 | [diff] [blame] | 1051 | /* 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 Versace | 0945512 | 2017-06-22 11:00:40 -0700 | [diff] [blame] | 1069 | 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älli | d58ca43 | 2012-08-13 16:47:52 +0300 | [diff] [blame] | 1072 | |
Chad Versace | bd78909 | 2017-06-22 11:00:41 -0700 | [diff] [blame] | 1073 | 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 Wu | 9779f6f | 2011-08-05 14:39:18 +0900 | [diff] [blame] | 1080 | |
Chad Versace | 0945512 | 2017-06-22 11:00:40 -0700 | [diff] [blame] | 1081 | struct dri2_egl_config *dri2_conf = |
| 1082 | dri2_add_config(dpy, dri2_dpy->driver_configs[j], |
Chad Versace | ffbf50b | 2017-06-22 11:00:41 -0700 | [diff] [blame] | 1083 | config_count + 1, surface_type, config_attrs, |
Chad Versace | 0945512 | 2017-06-22 11:00:40 -0700 | [diff] [blame] | 1084 | visuals[i].rgba_masks); |
Chia-I Wu | 9779f6f | 2011-08-05 14:39:18 +0900 | [diff] [blame] | 1085 | if (dri2_conf) { |
Chad Versace | ffbf50b | 2017-06-22 11:00:41 -0700 | [diff] [blame] | 1086 | if (dri2_conf->base.ConfigID == config_count + 1) |
| 1087 | config_count++; |
Chad Versace | 5e88435 | 2017-06-16 19:11:21 -0700 | [diff] [blame] | 1088 | format_count[i]++; |
Chia-I Wu | 9779f6f | 2011-08-05 14:39:18 +0900 | [diff] [blame] | 1089 | } |
| 1090 | } |
Emil Velikov | acd35c8 | 2016-08-25 14:28:48 +0100 | [diff] [blame] | 1091 | } |
Chia-I Wu | 9779f6f | 2011-08-05 14:39:18 +0900 | [diff] [blame] | 1092 | |
Chad Versace | 0945512 | 2017-06-22 11:00:40 -0700 | [diff] [blame] | 1093 | for (int i = 0; i < ARRAY_SIZE(format_count); i++) { |
Emil Velikov | acd35c8 | 2016-08-25 14:28:48 +0100 | [diff] [blame] | 1094 | if (!format_count[i]) { |
Chia-I Wu | 9779f6f | 2011-08-05 14:39:18 +0900 | [diff] [blame] | 1095 | _eglLog(_EGL_DEBUG, "No DRI config supports native format 0x%x", |
Emil Velikov | acd35c8 | 2016-08-25 14:28:48 +0100 | [diff] [blame] | 1096 | visuals[i].format); |
Chia-I Wu | 9779f6f | 2011-08-05 14:39:18 +0900 | [diff] [blame] | 1097 | } |
| 1098 | } |
| 1099 | |
Chad Versace | ffbf50b | 2017-06-22 11:00:41 -0700 | [diff] [blame] | 1100 | return (config_count != 0); |
Chia-I Wu | 9779f6f | 2011-08-05 14:39:18 +0900 | [diff] [blame] | 1101 | } |
| 1102 | |
Chia-I Wu | 9779f6f | 2011-08-05 14:39:18 +0900 | [diff] [blame] | 1103 | static int |
Tomasz Figa | 859d0b0 | 2016-11-10 16:55:52 +0900 | [diff] [blame] | 1104 | droid_open_device(struct dri2_egl_display *dri2_dpy) |
Chia-I Wu | 9779f6f | 2011-08-05 14:39:18 +0900 | [diff] [blame] | 1105 | { |
Tomasz Figa | 859d0b0 | 2016-11-10 16:55:52 +0900 | [diff] [blame] | 1106 | int fd = -1, err = -EINVAL; |
Chia-I Wu | 9779f6f | 2011-08-05 14:39:18 +0900 | [diff] [blame] | 1107 | |
Tomasz Figa | 859d0b0 | 2016-11-10 16:55:52 +0900 | [diff] [blame] | 1108 | 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 Wu | 9779f6f | 2011-08-05 14:39:18 +0900 | [diff] [blame] | 1112 | if (err || fd < 0) { |
| 1113 | _eglLog(_EGL_WARNING, "fail to get drm fd"); |
| 1114 | fd = -1; |
| 1115 | } |
| 1116 | |
Matt Whitlock | c8fd7d0 | 2016-10-01 23:49:41 -0400 | [diff] [blame] | 1117 | return (fd >= 0) ? fcntl(fd, F_DUPFD_CLOEXEC, 3) : -1; |
Chia-I Wu | 9779f6f | 2011-08-05 14:39:18 +0900 | [diff] [blame] | 1118 | } |
| 1119 | |
Emil Velikov | 79d1fb9 | 2017-05-15 16:14:17 +0100 | [diff] [blame] | 1120 | static const struct dri2_egl_display_vtbl droid_display_vtbl = { |
Chad Versace | 90502b1 | 2014-01-28 11:41:46 -0800 | [diff] [blame] | 1121 | .authenticate = NULL, |
Chad Versace | 0a0c881 | 2014-01-28 16:39:09 -0800 | [diff] [blame] | 1122 | .create_window_surface = droid_create_window_surface, |
Adrian Negreanu | 6980cae | 2014-06-06 12:16:10 +0300 | [diff] [blame] | 1123 | .create_pixmap_surface = dri2_fallback_create_pixmap_surface, |
Chad Versace | bf20076 | 2014-01-28 17:03:03 -0800 | [diff] [blame] | 1124 | .create_pbuffer_surface = droid_create_pbuffer_surface, |
Chad Versace | 958dd80 | 2014-01-28 17:03:03 -0800 | [diff] [blame] | 1125 | .destroy_surface = droid_destroy_surface, |
Chad Versace | eef68a9 | 2014-01-28 17:03:03 -0800 | [diff] [blame] | 1126 | .create_image = droid_create_image_khr, |
Chad Versace | ad173bc | 2014-01-28 16:21:21 -0800 | [diff] [blame] | 1127 | .swap_buffers = droid_swap_buffers, |
Emil Velikov | 61e99ce | 2017-10-16 17:10:42 +0100 | [diff] [blame] | 1128 | .swap_buffers_with_damage = dri2_fallback_swap_buffers_with_damage, /* Android implements the function */ |
Chad Versace | 75d398e | 2014-01-28 17:03:03 -0800 | [diff] [blame] | 1129 | .swap_buffers_region = dri2_fallback_swap_buffers_region, |
Harish Krupo | 9827547 | 2017-06-09 20:13:34 +0530 | [diff] [blame] | 1130 | #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 Versace | 688a0e8 | 2014-01-28 17:03:03 -0800 | [diff] [blame] | 1135 | .post_sub_buffer = dri2_fallback_post_sub_buffer, |
Chad Versace | bc2cbc0 | 2014-01-28 17:03:03 -0800 | [diff] [blame] | 1136 | .copy_buffers = dri2_fallback_copy_buffers, |
Xiaosong Wei | 2acc69d | 2017-02-08 10:46:02 +0800 | [diff] [blame] | 1137 | .query_buffer_age = droid_query_buffer_age, |
Haixia Shi | a7c6993 | 2016-07-28 10:51:12 -0700 | [diff] [blame] | 1138 | .query_surface = droid_query_surface, |
Chad Versace | eadd5e0 | 2014-01-28 17:03:03 -0800 | [diff] [blame] | 1139 | .create_wayland_buffer_from_image = dri2_fallback_create_wayland_buffer_from_image, |
Sarah Sharp | c524f3e | 2014-05-06 12:10:57 -0700 | [diff] [blame] | 1140 | .get_sync_values = dri2_fallback_get_sync_values, |
Boyan Ding | a25df54 | 2015-07-21 23:43:59 +0800 | [diff] [blame] | 1141 | .get_dri_drawable = dri2_surface_get_dri_drawable, |
Chad Versace | 90502b1 | 2014-01-28 11:41:46 -0800 | [diff] [blame] | 1142 | }; |
| 1143 | |
Emil Velikov | 6a8fe32 | 2016-08-16 17:23:23 +0100 | [diff] [blame] | 1144 | static const __DRIdri2LoaderExtension droid_dri2_loader_extension = { |
Marek Olšák | 1bf703e | 2017-07-28 17:30:34 +0200 | [diff] [blame] | 1145 | .base = { __DRI_DRI2_LOADER, 4 }, |
Emil Velikov | 6a8fe32 | 2016-08-16 17:23:23 +0100 | [diff] [blame] | 1146 | |
| 1147 | .getBuffers = NULL, |
| 1148 | .flushFrontBuffer = droid_flush_front_buffer, |
| 1149 | .getBuffersWithFormat = droid_get_buffers_with_format, |
Rob Herring | be5773f | 2017-07-31 09:32:10 -0500 | [diff] [blame] | 1150 | .getCapability = droid_get_capability, |
Emil Velikov | 6a8fe32 | 2016-08-16 17:23:23 +0100 | [diff] [blame] | 1151 | }; |
| 1152 | |
Rob Herring | 34ddef3 | 2016-05-01 09:35:28 +0100 | [diff] [blame] | 1153 | static const __DRIimageLoaderExtension droid_image_loader_extension = { |
Marek Olšák | 1bf703e | 2017-07-28 17:30:34 +0200 | [diff] [blame] | 1154 | .base = { __DRI_IMAGE_LOADER, 2 }, |
Rob Herring | 34ddef3 | 2016-05-01 09:35:28 +0100 | [diff] [blame] | 1155 | |
| 1156 | .getBuffers = droid_image_get_buffers, |
| 1157 | .flushFrontBuffer = droid_flush_front_buffer, |
Marek Olšák | 1bf703e | 2017-07-28 17:30:34 +0200 | [diff] [blame] | 1158 | .getCapability = droid_get_capability, |
Rob Herring | 34ddef3 | 2016-05-01 09:35:28 +0100 | [diff] [blame] | 1159 | }; |
| 1160 | |
Emil Velikov | f871946 | 2016-08-24 23:32:27 +0100 | [diff] [blame] | 1161 | static 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 | |
| 1168 | static 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 Wu | 9779f6f | 2011-08-05 14:39:18 +0900 | [diff] [blame] | 1175 | EGLBoolean |
| 1176 | dri2_initialize_android(_EGLDriver *drv, _EGLDisplay *dpy) |
| 1177 | { |
| 1178 | struct dri2_egl_display *dri2_dpy; |
| 1179 | const char *err; |
Tomasz Figa | 859d0b0 | 2016-11-10 16:55:52 +0900 | [diff] [blame] | 1180 | int ret; |
Chia-I Wu | 9779f6f | 2011-08-05 14:39:18 +0900 | [diff] [blame] | 1181 | |
Emil Velikov | 8d4357b | 2014-01-11 04:52:48 +0000 | [diff] [blame] | 1182 | loader_set_logger(_eglLog); |
| 1183 | |
Chia-I Wu | 9779f6f | 2011-08-05 14:39:18 +0900 | [diff] [blame] | 1184 | dri2_dpy = calloc(1, sizeof(*dri2_dpy)); |
| 1185 | if (!dri2_dpy) |
| 1186 | return _eglError(EGL_BAD_ALLOC, "eglInitialize"); |
| 1187 | |
Emil Velikov | 898d785 | 2017-05-11 17:18:13 +0100 | [diff] [blame] | 1188 | dri2_dpy->fd = -1; |
Tomasz Figa | 859d0b0 | 2016-11-10 16:55:52 +0900 | [diff] [blame] | 1189 | 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 Velikov | 898d785 | 2017-05-11 17:18:13 +0100 | [diff] [blame] | 1193 | goto cleanup; |
Tomasz Figa | 859d0b0 | 2016-11-10 16:55:52 +0900 | [diff] [blame] | 1194 | } |
| 1195 | |
Chia-I Wu | 9779f6f | 2011-08-05 14:39:18 +0900 | [diff] [blame] | 1196 | dpy->DriverData = (void *) dri2_dpy; |
| 1197 | |
Tomasz Figa | 859d0b0 | 2016-11-10 16:55:52 +0900 | [diff] [blame] | 1198 | dri2_dpy->fd = droid_open_device(dri2_dpy); |
Chia-I Wu | 9779f6f | 2011-08-05 14:39:18 +0900 | [diff] [blame] | 1199 | if (dri2_dpy->fd < 0) { |
| 1200 | err = "DRI2: failed to open device"; |
Emil Velikov | 898d785 | 2017-05-11 17:18:13 +0100 | [diff] [blame] | 1201 | goto cleanup; |
Chia-I Wu | 9779f6f | 2011-08-05 14:39:18 +0900 | [diff] [blame] | 1202 | } |
| 1203 | |
Emil Velikov | af7abc5 | 2016-09-12 17:48:18 +0100 | [diff] [blame] | 1204 | dri2_dpy->driver_name = loader_get_driver_for_fd(dri2_dpy->fd); |
Chia-I Wu | 9779f6f | 2011-08-05 14:39:18 +0900 | [diff] [blame] | 1205 | if (dri2_dpy->driver_name == NULL) { |
| 1206 | err = "DRI2: failed to get driver name"; |
Emil Velikov | 898d785 | 2017-05-11 17:18:13 +0100 | [diff] [blame] | 1207 | goto cleanup; |
Chia-I Wu | 9779f6f | 2011-08-05 14:39:18 +0900 | [diff] [blame] | 1208 | } |
| 1209 | |
Rob Herring | d45884e | 2016-04-28 15:37:28 -0500 | [diff] [blame] | 1210 | dri2_dpy->is_render_node = drmGetNodeTypeFromFd(dri2_dpy->fd) == DRM_NODE_RENDER; |
Chia-I Wu | 9779f6f | 2011-08-05 14:39:18 +0900 | [diff] [blame] | 1211 | |
Rob Herring | d45884e | 2016-04-28 15:37:28 -0500 | [diff] [blame] | 1212 | /* render nodes cannot use Gem names, and thus do not support |
| 1213 | * the __DRI_DRI2_LOADER extension */ |
Emil Velikov | acf125e | 2017-05-08 18:39:12 +0100 | [diff] [blame] | 1214 | if (!dri2_dpy->is_render_node) { |
Emil Velikov | f871946 | 2016-08-24 23:32:27 +0100 | [diff] [blame] | 1215 | dri2_dpy->loader_extensions = droid_dri2_loader_extensions; |
Emil Velikov | acf125e | 2017-05-08 18:39:12 +0100 | [diff] [blame] | 1216 | if (!dri2_load_driver(dpy)) { |
| 1217 | err = "DRI2: failed to load driver"; |
| 1218 | goto cleanup; |
| 1219 | } |
| 1220 | } else { |
Emil Velikov | f871946 | 2016-08-24 23:32:27 +0100 | [diff] [blame] | 1221 | dri2_dpy->loader_extensions = droid_image_loader_extensions; |
Emil Velikov | acf125e | 2017-05-08 18:39:12 +0100 | [diff] [blame] | 1222 | if (!dri2_load_driver_dri3(dpy)) { |
| 1223 | err = "DRI3: failed to load driver"; |
| 1224 | goto cleanup; |
| 1225 | } |
| 1226 | } |
Chia-I Wu | 9779f6f | 2011-08-05 14:39:18 +0900 | [diff] [blame] | 1227 | |
| 1228 | if (!dri2_create_screen(dpy)) { |
| 1229 | err = "DRI2: failed to create screen"; |
Emil Velikov | 898d785 | 2017-05-11 17:18:13 +0100 | [diff] [blame] | 1230 | goto cleanup; |
Chia-I Wu | 9779f6f | 2011-08-05 14:39:18 +0900 | [diff] [blame] | 1231 | } |
| 1232 | |
Tomasz Figa | 0ede0f9 | 2017-07-05 23:56:51 +0900 | [diff] [blame] | 1233 | if (!dri2_setup_extensions(dpy)) { |
| 1234 | err = "DRI2: failed to setup extensions"; |
Emil Velikov | 2c341f2 | 2017-05-11 16:20:04 +0100 | [diff] [blame] | 1235 | goto cleanup; |
Tomasz Figa | 0ede0f9 | 2017-07-05 23:56:51 +0900 | [diff] [blame] | 1236 | } |
Emil Velikov | 2c341f2 | 2017-05-11 16:20:04 +0100 | [diff] [blame] | 1237 | |
| 1238 | dri2_setup_screen(dpy); |
| 1239 | |
Chia-I Wu | 9779f6f | 2011-08-05 14:39:18 +0900 | [diff] [blame] | 1240 | if (!droid_add_configs_for_visuals(drv, dpy)) { |
| 1241 | err = "DRI2: failed to add configs"; |
Emil Velikov | 898d785 | 2017-05-11 17:18:13 +0100 | [diff] [blame] | 1242 | goto cleanup; |
Chia-I Wu | 9779f6f | 2011-08-05 14:39:18 +0900 | [diff] [blame] | 1243 | } |
| 1244 | |
Rob Herring | 952720c | 2016-02-02 14:23:11 -0600 | [diff] [blame] | 1245 | dpy->Extensions.ANDROID_framebuffer_target = EGL_TRUE; |
Chia-I Wu | 9779f6f | 2011-08-05 14:39:18 +0900 | [diff] [blame] | 1246 | dpy->Extensions.ANDROID_image_native_buffer = EGL_TRUE; |
Rob Herring | 952720c | 2016-02-02 14:23:11 -0600 | [diff] [blame] | 1247 | dpy->Extensions.ANDROID_recordable = EGL_TRUE; |
Xiaosong Wei | 2acc69d | 2017-02-08 10:46:02 +0800 | [diff] [blame] | 1248 | dpy->Extensions.EXT_buffer_age = EGL_TRUE; |
Harish Krupo | 9827547 | 2017-06-09 20:13:34 +0530 | [diff] [blame] | 1249 | #if ANDROID_API_LEVEL >= 23 |
| 1250 | dpy->Extensions.KHR_partial_update = EGL_TRUE; |
| 1251 | #endif |
Chia-I Wu | 9779f6f | 2011-08-05 14:39:18 +0900 | [diff] [blame] | 1252 | |
Chad Versace | 90502b1 | 2014-01-28 11:41:46 -0800 | [diff] [blame] | 1253 | /* Fill vtbl last to prevent accidentally calling virtual function during |
| 1254 | * initialization. |
| 1255 | */ |
| 1256 | dri2_dpy->vtbl = &droid_display_vtbl; |
| 1257 | |
Chia-I Wu | 9779f6f | 2011-08-05 14:39:18 +0900 | [diff] [blame] | 1258 | return EGL_TRUE; |
| 1259 | |
Emil Velikov | 898d785 | 2017-05-11 17:18:13 +0100 | [diff] [blame] | 1260 | cleanup: |
| 1261 | dri2_display_destroy(dpy); |
Chia-I Wu | 9779f6f | 2011-08-05 14:39:18 +0900 | [diff] [blame] | 1262 | return _eglError(EGL_NOT_INITIALIZED, err); |
| 1263 | } |