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 | |
Robert Foss | c7bb821 | 2018-04-18 17:27:40 +0200 | [diff] [blame] | 30 | #include <cutils/properties.h> |
Chia-I Wu | 9779f6f | 2011-08-05 14:39:18 +0900 | [diff] [blame] | 31 | #include <errno.h> |
Robert Foss | c7bb821 | 2018-04-18 17:27:40 +0200 | [diff] [blame] | 32 | #include <dirent.h> |
Chia-I Wu | 9779f6f | 2011-08-05 14:39:18 +0900 | [diff] [blame] | 33 | #include <dlfcn.h> |
Matt Whitlock | c8fd7d0 | 2016-10-01 23:49:41 -0400 | [diff] [blame] | 34 | #include <fcntl.h> |
Rob Herring | d45884e | 2016-04-28 15:37:28 -0500 | [diff] [blame] | 35 | #include <xf86drm.h> |
Tomasz Figa | 51727b1 | 2016-11-10 16:55:53 +0900 | [diff] [blame] | 36 | #include <stdbool.h> |
Robert Foss | c7bb821 | 2018-04-18 17:27:40 +0200 | [diff] [blame] | 37 | #include <stdio.h> |
Chad Versace | bfe28b8 | 2012-12-20 14:16:50 -0800 | [diff] [blame] | 38 | #include <sync/sync.h> |
Robert Foss | c7bb821 | 2018-04-18 17:27:40 +0200 | [diff] [blame] | 39 | #include <sys/types.h> |
Chad Versace | bfe28b8 | 2012-12-20 14:16:50 -0800 | [diff] [blame] | 40 | |
Emil Velikov | 8d4357b | 2014-01-11 04:52:48 +0000 | [diff] [blame] | 41 | #include "loader.h" |
Chia-I Wu | 9779f6f | 2011-08-05 14:39:18 +0900 | [diff] [blame] | 42 | #include "egl_dri2.h" |
Chad Versace | 8b9298a | 2014-01-28 12:34:19 -0800 | [diff] [blame] | 43 | #include "egl_dri2_fallbacks.h" |
Rob Herring | 3f7bca4 | 2018-04-26 16:02:01 +0200 | [diff] [blame] | 44 | |
| 45 | #ifdef HAVE_DRM_GRALLOC |
| 46 | #include <gralloc_drm_handle.h> |
Tapani Pälli | 4070295 | 2013-01-24 09:56:47 +0200 | [diff] [blame] | 47 | #include "gralloc_drm.h" |
Rob Herring | 3f7bca4 | 2018-04-26 16:02:01 +0200 | [diff] [blame] | 48 | #endif /* HAVE_DRM_GRALLOC */ |
Chia-I Wu | 9779f6f | 2011-08-05 14:39:18 +0900 | [diff] [blame] | 49 | |
Tomasz Figa | 3723e98 | 2016-08-02 20:07:54 +0900 | [diff] [blame] | 50 | #define ALIGN(val, align) (((val) + (align) - 1) & ~((align) - 1)) |
| 51 | |
Eric Engestrom | 0c3287e | 2018-11-09 11:55:10 +0000 | [diff] [blame] | 52 | enum chroma_order { |
| 53 | YCbCr, |
| 54 | YCrCb, |
| 55 | }; |
| 56 | |
Tomasz Figa | 51727b1 | 2016-11-10 16:55:53 +0900 | [diff] [blame] | 57 | struct droid_yuv_format { |
| 58 | /* Lookup keys */ |
| 59 | int native; /* HAL_PIXEL_FORMAT_ */ |
Eric Engestrom | 0c3287e | 2018-11-09 11:55:10 +0000 | [diff] [blame] | 60 | enum chroma_order chroma_order; /* chroma order is {Cb, Cr} or {Cr, Cb} */ |
Tomasz Figa | 51727b1 | 2016-11-10 16:55:53 +0900 | [diff] [blame] | 61 | int chroma_step; /* Distance in bytes between subsequent chroma pixels. */ |
| 62 | |
| 63 | /* Result */ |
| 64 | int fourcc; /* __DRI_IMAGE_FOURCC_ */ |
| 65 | }; |
| 66 | |
| 67 | /* The following table is used to look up a DRI image FourCC based |
| 68 | * on native format and information contained in android_ycbcr struct. */ |
| 69 | static const struct droid_yuv_format droid_yuv_formats[] = { |
| 70 | /* Native format, YCrCb, Chroma step, DRI image FourCC */ |
Eric Engestrom | 0c3287e | 2018-11-09 11:55:10 +0000 | [diff] [blame] | 71 | { HAL_PIXEL_FORMAT_YCbCr_420_888, YCbCr, 2, __DRI_IMAGE_FOURCC_NV12 }, |
| 72 | { HAL_PIXEL_FORMAT_YCbCr_420_888, YCbCr, 1, __DRI_IMAGE_FOURCC_YUV420 }, |
| 73 | { HAL_PIXEL_FORMAT_YCbCr_420_888, YCrCb, 1, __DRI_IMAGE_FOURCC_YVU420 }, |
| 74 | { HAL_PIXEL_FORMAT_YV12, YCrCb, 1, __DRI_IMAGE_FOURCC_YVU420 }, |
Tomasz Figa | 5364e73 | 2017-12-04 19:22:39 +0100 | [diff] [blame] | 75 | /* HACK: See droid_create_image_from_prime_fd() and |
| 76 | * https://issuetracker.google.com/32077885. */ |
Eric Engestrom | 0c3287e | 2018-11-09 11:55:10 +0000 | [diff] [blame] | 77 | { HAL_PIXEL_FORMAT_IMPLEMENTATION_DEFINED, YCbCr, 2, __DRI_IMAGE_FOURCC_NV12 }, |
| 78 | { HAL_PIXEL_FORMAT_IMPLEMENTATION_DEFINED, YCbCr, 1, __DRI_IMAGE_FOURCC_YUV420 }, |
| 79 | { HAL_PIXEL_FORMAT_IMPLEMENTATION_DEFINED, YCrCb, 1, __DRI_IMAGE_FOURCC_YVU420 }, |
| 80 | { HAL_PIXEL_FORMAT_IMPLEMENTATION_DEFINED, YCrCb, 1, __DRI_IMAGE_FOURCC_AYUV }, |
| 81 | { HAL_PIXEL_FORMAT_IMPLEMENTATION_DEFINED, YCrCb, 1, __DRI_IMAGE_FOURCC_XYUV8888 }, |
Tomasz Figa | 51727b1 | 2016-11-10 16:55:53 +0900 | [diff] [blame] | 82 | }; |
| 83 | |
| 84 | static int |
Eric Engestrom | 0c3287e | 2018-11-09 11:55:10 +0000 | [diff] [blame] | 85 | get_fourcc_yuv(int native, enum chroma_order chroma_order, int chroma_step) |
Tomasz Figa | 51727b1 | 2016-11-10 16:55:53 +0900 | [diff] [blame] | 86 | { |
Chad Versace | 0945512 | 2017-06-22 11:00:40 -0700 | [diff] [blame] | 87 | for (int i = 0; i < ARRAY_SIZE(droid_yuv_formats); ++i) |
Tomasz Figa | 51727b1 | 2016-11-10 16:55:53 +0900 | [diff] [blame] | 88 | if (droid_yuv_formats[i].native == native && |
Eric Engestrom | 0c3287e | 2018-11-09 11:55:10 +0000 | [diff] [blame] | 89 | droid_yuv_formats[i].chroma_order == chroma_order && |
Tomasz Figa | 51727b1 | 2016-11-10 16:55:53 +0900 | [diff] [blame] | 90 | droid_yuv_formats[i].chroma_step == chroma_step) |
| 91 | return droid_yuv_formats[i].fourcc; |
| 92 | |
| 93 | return -1; |
| 94 | } |
| 95 | |
| 96 | static bool |
| 97 | is_yuv(int native) |
| 98 | { |
Chad Versace | 0945512 | 2017-06-22 11:00:40 -0700 | [diff] [blame] | 99 | for (int i = 0; i < ARRAY_SIZE(droid_yuv_formats); ++i) |
Tomasz Figa | 51727b1 | 2016-11-10 16:55:53 +0900 | [diff] [blame] | 100 | if (droid_yuv_formats[i].native == native) |
| 101 | return true; |
| 102 | |
| 103 | return false; |
| 104 | } |
| 105 | |
Chia-I Wu | 9779f6f | 2011-08-05 14:39:18 +0900 | [diff] [blame] | 106 | static int |
| 107 | get_format_bpp(int native) |
| 108 | { |
| 109 | int bpp; |
| 110 | |
| 111 | switch (native) { |
| 112 | case HAL_PIXEL_FORMAT_RGBA_8888: |
Tomasz Figa | 5364e73 | 2017-12-04 19:22:39 +0100 | [diff] [blame] | 113 | case HAL_PIXEL_FORMAT_IMPLEMENTATION_DEFINED: |
| 114 | /* |
| 115 | * HACK: Hardcode this to RGBX_8888 as per cros_gralloc hack. |
| 116 | * TODO: Remove this once https://issuetracker.google.com/32077885 is fixed. |
| 117 | */ |
Chia-I Wu | 9779f6f | 2011-08-05 14:39:18 +0900 | [diff] [blame] | 118 | case HAL_PIXEL_FORMAT_RGBX_8888: |
| 119 | case HAL_PIXEL_FORMAT_BGRA_8888: |
| 120 | bpp = 4; |
| 121 | break; |
Chia-I Wu | 9779f6f | 2011-08-05 14:39:18 +0900 | [diff] [blame] | 122 | case HAL_PIXEL_FORMAT_RGB_565: |
Chia-I Wu | 9779f6f | 2011-08-05 14:39:18 +0900 | [diff] [blame] | 123 | bpp = 2; |
| 124 | break; |
| 125 | default: |
| 126 | bpp = 0; |
| 127 | break; |
| 128 | } |
| 129 | |
| 130 | return bpp; |
| 131 | } |
| 132 | |
Rob Herring | 34ddef3 | 2016-05-01 09:35:28 +0100 | [diff] [blame] | 133 | /* createImageFromFds requires fourcc format */ |
Tomasz Figa | 7dfb1a4 | 2016-08-02 20:07:53 +0900 | [diff] [blame] | 134 | static int get_fourcc(int native) |
Rob Herring | 34ddef3 | 2016-05-01 09:35:28 +0100 | [diff] [blame] | 135 | { |
Tomasz Figa | 7dfb1a4 | 2016-08-02 20:07:53 +0900 | [diff] [blame] | 136 | switch (native) { |
| 137 | case HAL_PIXEL_FORMAT_RGB_565: return __DRI_IMAGE_FOURCC_RGB565; |
| 138 | case HAL_PIXEL_FORMAT_BGRA_8888: return __DRI_IMAGE_FOURCC_ARGB8888; |
| 139 | case HAL_PIXEL_FORMAT_RGBA_8888: return __DRI_IMAGE_FOURCC_ABGR8888; |
Tomasz Figa | 5364e73 | 2017-12-04 19:22:39 +0100 | [diff] [blame] | 140 | case HAL_PIXEL_FORMAT_IMPLEMENTATION_DEFINED: |
| 141 | /* |
| 142 | * HACK: Hardcode this to RGBX_8888 as per cros_gralloc hack. |
| 143 | * TODO: Remove this once https://issuetracker.google.com/32077885 is fixed. |
| 144 | */ |
Tomasz Figa | 7dfb1a4 | 2016-08-02 20:07:53 +0900 | [diff] [blame] | 145 | case HAL_PIXEL_FORMAT_RGBX_8888: return __DRI_IMAGE_FOURCC_XBGR8888; |
| 146 | default: |
| 147 | _eglLog(_EGL_WARNING, "unsupported native buffer format 0x%x", native); |
Rob Herring | 34ddef3 | 2016-05-01 09:35:28 +0100 | [diff] [blame] | 148 | } |
| 149 | return -1; |
| 150 | } |
| 151 | |
Rob Herring | dfaccf2 | 2016-04-28 15:37:29 -0500 | [diff] [blame] | 152 | static int get_format(int format) |
| 153 | { |
| 154 | switch (format) { |
| 155 | case HAL_PIXEL_FORMAT_BGRA_8888: return __DRI_IMAGE_FORMAT_ARGB8888; |
| 156 | case HAL_PIXEL_FORMAT_RGB_565: return __DRI_IMAGE_FORMAT_RGB565; |
| 157 | case HAL_PIXEL_FORMAT_RGBA_8888: return __DRI_IMAGE_FORMAT_ABGR8888; |
Tomasz Figa | 5364e73 | 2017-12-04 19:22:39 +0100 | [diff] [blame] | 158 | case HAL_PIXEL_FORMAT_IMPLEMENTATION_DEFINED: |
| 159 | /* |
| 160 | * HACK: Hardcode this to RGBX_8888 as per cros_gralloc hack. |
| 161 | * TODO: Revert this once https://issuetracker.google.com/32077885 is fixed. |
| 162 | */ |
Rob Herring | dfaccf2 | 2016-04-28 15:37:29 -0500 | [diff] [blame] | 163 | case HAL_PIXEL_FORMAT_RGBX_8888: return __DRI_IMAGE_FORMAT_XBGR8888; |
Rob Herring | dfaccf2 | 2016-04-28 15:37:29 -0500 | [diff] [blame] | 164 | default: |
| 165 | _eglLog(_EGL_WARNING, "unsupported native buffer format 0x%x", format); |
| 166 | } |
| 167 | return -1; |
| 168 | } |
Tomasz Figa | 7dfb1a4 | 2016-08-02 20:07:53 +0900 | [diff] [blame] | 169 | |
Chia-I Wu | 9779f6f | 2011-08-05 14:39:18 +0900 | [diff] [blame] | 170 | static int |
Rob Herring | 34ddef3 | 2016-05-01 09:35:28 +0100 | [diff] [blame] | 171 | get_native_buffer_fd(struct ANativeWindowBuffer *buf) |
| 172 | { |
| 173 | native_handle_t *handle = (native_handle_t *)buf->handle; |
| 174 | /* |
| 175 | * Various gralloc implementations exist, but the dma-buf fd tends |
| 176 | * to be first. Access it directly to avoid a dependency on specific |
| 177 | * gralloc versions. |
| 178 | */ |
| 179 | return (handle && handle->numFds) ? handle->data[0] : -1; |
| 180 | } |
| 181 | |
Rob Herring | 3f7bca4 | 2018-04-26 16:02:01 +0200 | [diff] [blame] | 182 | #ifdef HAVE_DRM_GRALLOC |
Rob Herring | 34ddef3 | 2016-05-01 09:35:28 +0100 | [diff] [blame] | 183 | static int |
Chia-I Wu | 75cc24c | 2011-11-25 11:59:02 +0800 | [diff] [blame] | 184 | get_native_buffer_name(struct ANativeWindowBuffer *buf) |
Chia-I Wu | 9779f6f | 2011-08-05 14:39:18 +0900 | [diff] [blame] | 185 | { |
Tapani Pälli | 4070295 | 2013-01-24 09:56:47 +0200 | [diff] [blame] | 186 | return gralloc_drm_get_gem_handle(buf->handle); |
Chia-I Wu | 9779f6f | 2011-08-05 14:39:18 +0900 | [diff] [blame] | 187 | } |
Rob Herring | 3f7bca4 | 2018-04-26 16:02:01 +0200 | [diff] [blame] | 188 | #endif /* HAVE_DRM_GRALLOC */ |
Chia-I Wu | 9779f6f | 2011-08-05 14:39:18 +0900 | [diff] [blame] | 189 | |
| 190 | static EGLBoolean |
| 191 | droid_window_dequeue_buffer(struct dri2_egl_surface *dri2_surf) |
| 192 | { |
Chad Versace | bfe28b8 | 2012-12-20 14:16:50 -0800 | [diff] [blame] | 193 | int fence_fd; |
| 194 | |
| 195 | if (dri2_surf->window->dequeueBuffer(dri2_surf->window, &dri2_surf->buffer, |
| 196 | &fence_fd)) |
| 197 | return EGL_FALSE; |
| 198 | |
| 199 | /* If access to the buffer is controlled by a sync fence, then block on the |
| 200 | * fence. |
| 201 | * |
| 202 | * It may be more performant to postpone blocking until there is an |
| 203 | * immediate need to write to the buffer. But doing so would require adding |
| 204 | * hooks to the DRI2 loader. |
| 205 | * |
| 206 | * From the ANativeWindow::dequeueBuffer documentation: |
| 207 | * |
| 208 | * The libsync fence file descriptor returned in the int pointed to by |
| 209 | * the fenceFd argument will refer to the fence that must signal |
| 210 | * before the dequeued buffer may be written to. A value of -1 |
| 211 | * indicates that the caller may access the buffer immediately without |
| 212 | * waiting on a fence. If a valid file descriptor is returned (i.e. |
| 213 | * any value except -1) then the caller is responsible for closing the |
| 214 | * file descriptor. |
| 215 | */ |
| 216 | if (fence_fd >= 0) { |
| 217 | /* From the SYNC_IOC_WAIT documentation in <linux/sync.h>: |
| 218 | * |
| 219 | * Waits indefinitely if timeout < 0. |
| 220 | */ |
| 221 | int timeout = -1; |
| 222 | sync_wait(fence_fd, timeout); |
| 223 | close(fence_fd); |
| 224 | } |
| 225 | |
| 226 | dri2_surf->buffer->common.incRef(&dri2_surf->buffer->common); |
Chia-I Wu | 9779f6f | 2011-08-05 14:39:18 +0900 | [diff] [blame] | 227 | |
Xiaosong Wei | 2acc69d | 2017-02-08 10:46:02 +0800 | [diff] [blame] | 228 | /* Record all the buffers created by ANativeWindow and update back buffer |
| 229 | * for updating buffer's age in swap_buffers. |
| 230 | */ |
| 231 | EGLBoolean updated = EGL_FALSE; |
| 232 | for (int i = 0; i < ARRAY_SIZE(dri2_surf->color_buffers); i++) { |
| 233 | if (!dri2_surf->color_buffers[i].buffer) { |
| 234 | dri2_surf->color_buffers[i].buffer = dri2_surf->buffer; |
| 235 | } |
| 236 | if (dri2_surf->color_buffers[i].buffer == dri2_surf->buffer) { |
| 237 | dri2_surf->back = &dri2_surf->color_buffers[i]; |
| 238 | updated = EGL_TRUE; |
| 239 | break; |
| 240 | } |
| 241 | } |
| 242 | |
| 243 | if (!updated) { |
| 244 | /* In case of all the buffers were recreated by ANativeWindow, reset |
| 245 | * the color_buffers |
| 246 | */ |
| 247 | for (int i = 0; i < ARRAY_SIZE(dri2_surf->color_buffers); i++) { |
| 248 | dri2_surf->color_buffers[i].buffer = NULL; |
| 249 | dri2_surf->color_buffers[i].age = 0; |
| 250 | } |
| 251 | dri2_surf->color_buffers[0].buffer = dri2_surf->buffer; |
| 252 | dri2_surf->back = &dri2_surf->color_buffers[0]; |
| 253 | } |
| 254 | |
Chia-I Wu | 9779f6f | 2011-08-05 14:39:18 +0900 | [diff] [blame] | 255 | return EGL_TRUE; |
| 256 | } |
| 257 | |
| 258 | static EGLBoolean |
Haixia Shi | 1ea233c | 2016-06-02 12:48:23 -0700 | [diff] [blame] | 259 | 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] | 260 | { |
Tomasz Figa | 9e1248d | 2016-07-15 16:53:50 +0900 | [diff] [blame] | 261 | struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp); |
| 262 | |
Haixia Shi | 1ea233c | 2016-06-02 12:48:23 -0700 | [diff] [blame] | 263 | /* To avoid blocking other EGL calls, release the display mutex before |
| 264 | * we enter droid_window_enqueue_buffer() and re-acquire the mutex upon |
| 265 | * return. |
| 266 | */ |
| 267 | mtx_unlock(&disp->Mutex); |
| 268 | |
Zhongmin Wu | 7343d27 | 2017-09-15 18:32:43 +0100 | [diff] [blame] | 269 | /* Queue the buffer with stored out fence fd. The ANativeWindow or buffer |
| 270 | * consumer may choose to wait for the fence to signal before accessing |
| 271 | * it. If fence fd value is -1, buffer can be accessed by consumer |
| 272 | * immediately. Consumer or application shouldn't rely on timestamp |
| 273 | * associated with fence if the fence fd is -1. |
Chad Versace | bfe28b8 | 2012-12-20 14:16:50 -0800 | [diff] [blame] | 274 | * |
Zhongmin Wu | 7343d27 | 2017-09-15 18:32:43 +0100 | [diff] [blame] | 275 | * Ownership of fd is transferred to consumer after queueBuffer and the |
| 276 | * consumer is responsible for closing it. Caller must not use the fd |
| 277 | * after passing it to queueBuffer. |
Chad Versace | bfe28b8 | 2012-12-20 14:16:50 -0800 | [diff] [blame] | 278 | */ |
Zhongmin Wu | 7343d27 | 2017-09-15 18:32:43 +0100 | [diff] [blame] | 279 | int fence_fd = dri2_surf->out_fence_fd; |
| 280 | dri2_surf->out_fence_fd = -1; |
Chad Versace | bfe28b8 | 2012-12-20 14:16:50 -0800 | [diff] [blame] | 281 | dri2_surf->window->queueBuffer(dri2_surf->window, dri2_surf->buffer, |
| 282 | fence_fd); |
Chia-I Wu | 9779f6f | 2011-08-05 14:39:18 +0900 | [diff] [blame] | 283 | |
| 284 | dri2_surf->buffer->common.decRef(&dri2_surf->buffer->common); |
| 285 | dri2_surf->buffer = NULL; |
Xiaosong Wei | 2acc69d | 2017-02-08 10:46:02 +0800 | [diff] [blame] | 286 | dri2_surf->back = NULL; |
Chia-I Wu | 9779f6f | 2011-08-05 14:39:18 +0900 | [diff] [blame] | 287 | |
Haixia Shi | 1ea233c | 2016-06-02 12:48:23 -0700 | [diff] [blame] | 288 | mtx_lock(&disp->Mutex); |
Tomasz Figa | 9e1248d | 2016-07-15 16:53:50 +0900 | [diff] [blame] | 289 | |
Liu Zhiquan | b663753 | 2016-11-16 10:11:28 +0800 | [diff] [blame] | 290 | if (dri2_surf->dri_image_back) { |
| 291 | dri2_dpy->image->destroyImage(dri2_surf->dri_image_back); |
| 292 | dri2_surf->dri_image_back = NULL; |
Tomasz Figa | 9e1248d | 2016-07-15 16:53:50 +0900 | [diff] [blame] | 293 | } |
| 294 | |
Chia-I Wu | 9779f6f | 2011-08-05 14:39:18 +0900 | [diff] [blame] | 295 | return EGL_TRUE; |
| 296 | } |
| 297 | |
| 298 | static void |
Chad Versace | 0212db3 | 2017-05-04 17:46:33 -0700 | [diff] [blame] | 299 | droid_window_cancel_buffer(struct dri2_egl_surface *dri2_surf) |
Chia-I Wu | 9779f6f | 2011-08-05 14:39:18 +0900 | [diff] [blame] | 300 | { |
Chad Versace | 0212db3 | 2017-05-04 17:46:33 -0700 | [diff] [blame] | 301 | int ret; |
Zhongmin Wu | 7343d27 | 2017-09-15 18:32:43 +0100 | [diff] [blame] | 302 | int fence_fd = dri2_surf->out_fence_fd; |
Chad Versace | 0212db3 | 2017-05-04 17:46:33 -0700 | [diff] [blame] | 303 | |
Zhongmin Wu | 7343d27 | 2017-09-15 18:32:43 +0100 | [diff] [blame] | 304 | dri2_surf->out_fence_fd = -1; |
| 305 | ret = dri2_surf->window->cancelBuffer(dri2_surf->window, |
| 306 | dri2_surf->buffer, fence_fd); |
Chad Versace | 0212db3 | 2017-05-04 17:46:33 -0700 | [diff] [blame] | 307 | if (ret < 0) { |
| 308 | _eglLog(_EGL_WARNING, "ANativeWindow::cancelBuffer failed"); |
Nicolas Boichat | 63b12b0 | 2017-05-05 10:43:50 +0800 | [diff] [blame] | 309 | dri2_surf->base.Lost = EGL_TRUE; |
Chad Versace | 0212db3 | 2017-05-04 17:46:33 -0700 | [diff] [blame] | 310 | } |
Chia-I Wu | 9779f6f | 2011-08-05 14:39:18 +0900 | [diff] [blame] | 311 | } |
| 312 | |
Chad Versace | ed7c694 | 2018-04-30 22:35:17 -0700 | [diff] [blame] | 313 | static bool |
| 314 | droid_set_shared_buffer_mode(_EGLDisplay *disp, _EGLSurface *surf, bool mode) |
| 315 | { |
| 316 | #if ANDROID_API_LEVEL >= 24 |
| 317 | struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp); |
| 318 | struct dri2_egl_surface *dri2_surf = dri2_egl_surface(surf); |
| 319 | struct ANativeWindow *window = dri2_surf->window; |
| 320 | |
| 321 | assert(surf->Type == EGL_WINDOW_BIT); |
| 322 | assert(_eglSurfaceHasMutableRenderBuffer(&dri2_surf->base)); |
| 323 | |
| 324 | _eglLog(_EGL_DEBUG, "%s: mode=%d", __func__, mode); |
| 325 | |
| 326 | if (native_window_set_shared_buffer_mode(window, mode)) { |
| 327 | _eglLog(_EGL_WARNING, "failed native_window_set_shared_buffer_mode" |
| 328 | "(window=%p, mode=%d)", window, mode); |
| 329 | return false; |
| 330 | } |
| 331 | |
| 332 | return true; |
| 333 | #else |
| 334 | _eglLog(_EGL_FATAL, "%s:%d: internal error: unreachable", __FILE__, __LINE__); |
| 335 | return false; |
| 336 | #endif |
| 337 | } |
| 338 | |
Chia-I Wu | 9779f6f | 2011-08-05 14:39:18 +0900 | [diff] [blame] | 339 | static _EGLSurface * |
| 340 | droid_create_surface(_EGLDriver *drv, _EGLDisplay *disp, EGLint type, |
Chad Versace | 6d1f83e | 2014-01-07 14:54:51 -0800 | [diff] [blame] | 341 | _EGLConfig *conf, void *native_window, |
Chia-I Wu | 9779f6f | 2011-08-05 14:39:18 +0900 | [diff] [blame] | 342 | const EGLint *attrib_list) |
| 343 | { |
Emil Velikov | acf125e | 2017-05-08 18:39:12 +0100 | [diff] [blame] | 344 | __DRIcreateNewDrawableFunc createNewDrawable; |
Chia-I Wu | 9779f6f | 2011-08-05 14:39:18 +0900 | [diff] [blame] | 345 | struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp); |
| 346 | struct dri2_egl_config *dri2_conf = dri2_egl_config(conf); |
| 347 | struct dri2_egl_surface *dri2_surf; |
Chad Versace | 6d1f83e | 2014-01-07 14:54:51 -0800 | [diff] [blame] | 348 | struct ANativeWindow *window = native_window; |
Marek Olšák | c2c2e9a | 2015-06-10 02:49:29 +0200 | [diff] [blame] | 349 | const __DRIconfig *config; |
Chad Versace | 6d1f83e | 2014-01-07 14:54:51 -0800 | [diff] [blame] | 350 | |
Chia-I Wu | 9779f6f | 2011-08-05 14:39:18 +0900 | [diff] [blame] | 351 | dri2_surf = calloc(1, sizeof *dri2_surf); |
| 352 | if (!dri2_surf) { |
| 353 | _eglError(EGL_BAD_ALLOC, "droid_create_surface"); |
| 354 | return NULL; |
| 355 | } |
| 356 | |
Paulo Zanoni | 04ecda3 | 2019-05-01 15:42:26 -0700 | [diff] [blame^] | 357 | if (!dri2_init_surface(&dri2_surf->base, disp, type, conf, attrib_list, |
| 358 | true, native_window)) |
Chia-I Wu | c8e18f8 | 2011-08-26 22:09:56 +0800 | [diff] [blame] | 359 | goto cleanup_surface; |
| 360 | |
| 361 | if (type == EGL_WINDOW_BIT) { |
| 362 | int format; |
| 363 | |
Emil Velikov | 92b2368 | 2017-08-05 00:25:47 +0100 | [diff] [blame] | 364 | if (window->common.magic != ANDROID_NATIVE_WINDOW_MAGIC) { |
Chia-I Wu | c8e18f8 | 2011-08-26 22:09:56 +0800 | [diff] [blame] | 365 | _eglError(EGL_BAD_NATIVE_WINDOW, "droid_create_surface"); |
| 366 | goto cleanup_surface; |
| 367 | } |
| 368 | if (window->query(window, NATIVE_WINDOW_FORMAT, &format)) { |
| 369 | _eglError(EGL_BAD_NATIVE_WINDOW, "droid_create_surface"); |
| 370 | goto cleanup_surface; |
| 371 | } |
| 372 | |
| 373 | if (format != dri2_conf->base.NativeVisualID) { |
| 374 | _eglLog(_EGL_WARNING, "Native format mismatch: 0x%x != 0x%x", |
| 375 | format, dri2_conf->base.NativeVisualID); |
| 376 | } |
| 377 | |
| 378 | window->query(window, NATIVE_WINDOW_WIDTH, &dri2_surf->base.Width); |
| 379 | window->query(window, NATIVE_WINDOW_HEIGHT, &dri2_surf->base.Height); |
| 380 | } |
Chia-I Wu | 9779f6f | 2011-08-05 14:39:18 +0900 | [diff] [blame] | 381 | |
Liu Zhiquan | b663753 | 2016-11-16 10:11:28 +0800 | [diff] [blame] | 382 | config = dri2_get_dri_config(dri2_conf, type, |
Marek Olšák | c2c2e9a | 2015-06-10 02:49:29 +0200 | [diff] [blame] | 383 | dri2_surf->base.GLColorspace); |
Tapani Pälli | affe63b | 2018-05-04 08:28:50 +0300 | [diff] [blame] | 384 | if (!config) { |
| 385 | _eglError(EGL_BAD_MATCH, "Unsupported surfacetype/colorspace configuration"); |
Tomasz Figa | 94282b6 | 2016-07-15 16:53:48 +0900 | [diff] [blame] | 386 | goto cleanup_surface; |
Tapani Pälli | affe63b | 2018-05-04 08:28:50 +0300 | [diff] [blame] | 387 | } |
Marek Olšák | c2c2e9a | 2015-06-10 02:49:29 +0200 | [diff] [blame] | 388 | |
Emil Velikov | acf125e | 2017-05-08 18:39:12 +0100 | [diff] [blame] | 389 | if (dri2_dpy->image_driver) |
| 390 | createNewDrawable = dri2_dpy->image_driver->createNewDrawable; |
| 391 | else |
| 392 | createNewDrawable = dri2_dpy->dri2->createNewDrawable; |
| 393 | |
| 394 | dri2_surf->dri_drawable = (*createNewDrawable)(dri2_dpy->dri_screen, config, |
| 395 | dri2_surf); |
Chia-I Wu | 9779f6f | 2011-08-05 14:39:18 +0900 | [diff] [blame] | 396 | if (dri2_surf->dri_drawable == NULL) { |
Emil Velikov | acf125e | 2017-05-08 18:39:12 +0100 | [diff] [blame] | 397 | _eglError(EGL_BAD_ALLOC, "createNewDrawable"); |
Chia-I Wu | c8e18f8 | 2011-08-26 22:09:56 +0800 | [diff] [blame] | 398 | goto cleanup_surface; |
Chia-I Wu | 9779f6f | 2011-08-05 14:39:18 +0900 | [diff] [blame] | 399 | } |
| 400 | |
Chia-I Wu | c8e18f8 | 2011-08-26 22:09:56 +0800 | [diff] [blame] | 401 | if (window) { |
| 402 | window->common.incRef(&window->common); |
| 403 | dri2_surf->window = window; |
| 404 | } |
Chia-I Wu | 9779f6f | 2011-08-05 14:39:18 +0900 | [diff] [blame] | 405 | |
| 406 | return &dri2_surf->base; |
| 407 | |
Chia-I Wu | c8e18f8 | 2011-08-26 22:09:56 +0800 | [diff] [blame] | 408 | cleanup_surface: |
Chia-I Wu | 9779f6f | 2011-08-05 14:39:18 +0900 | [diff] [blame] | 409 | free(dri2_surf); |
| 410 | |
| 411 | return NULL; |
| 412 | } |
| 413 | |
| 414 | static _EGLSurface * |
| 415 | droid_create_window_surface(_EGLDriver *drv, _EGLDisplay *disp, |
Chad Versace | 6d1f83e | 2014-01-07 14:54:51 -0800 | [diff] [blame] | 416 | _EGLConfig *conf, void *native_window, |
| 417 | const EGLint *attrib_list) |
Chia-I Wu | 9779f6f | 2011-08-05 14:39:18 +0900 | [diff] [blame] | 418 | { |
| 419 | return droid_create_surface(drv, disp, EGL_WINDOW_BIT, conf, |
Chad Versace | 6d1f83e | 2014-01-07 14:54:51 -0800 | [diff] [blame] | 420 | native_window, attrib_list); |
Chia-I Wu | 9779f6f | 2011-08-05 14:39:18 +0900 | [diff] [blame] | 421 | } |
| 422 | |
| 423 | static _EGLSurface * |
Chia-I Wu | 9779f6f | 2011-08-05 14:39:18 +0900 | [diff] [blame] | 424 | droid_create_pbuffer_surface(_EGLDriver *drv, _EGLDisplay *disp, |
| 425 | _EGLConfig *conf, const EGLint *attrib_list) |
| 426 | { |
Chia-I Wu | 93d5963 | 2011-08-27 00:00:18 +0800 | [diff] [blame] | 427 | return droid_create_surface(drv, disp, EGL_PBUFFER_BIT, conf, |
| 428 | NULL, attrib_list); |
Chia-I Wu | 9779f6f | 2011-08-05 14:39:18 +0900 | [diff] [blame] | 429 | } |
| 430 | |
| 431 | static EGLBoolean |
| 432 | droid_destroy_surface(_EGLDriver *drv, _EGLDisplay *disp, _EGLSurface *surf) |
| 433 | { |
| 434 | struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp); |
| 435 | struct dri2_egl_surface *dri2_surf = dri2_egl_surface(surf); |
| 436 | |
Gwan-gyeong Mun | 640b6e6 | 2017-08-05 00:16:11 +0900 | [diff] [blame] | 437 | dri2_egl_surface_free_local_buffers(dri2_surf); |
Chia-I Wu | 9779f6f | 2011-08-05 14:39:18 +0900 | [diff] [blame] | 438 | |
Chia-I Wu | c8e18f8 | 2011-08-26 22:09:56 +0800 | [diff] [blame] | 439 | if (dri2_surf->base.Type == EGL_WINDOW_BIT) { |
| 440 | if (dri2_surf->buffer) |
Chad Versace | 0212db3 | 2017-05-04 17:46:33 -0700 | [diff] [blame] | 441 | droid_window_cancel_buffer(dri2_surf); |
Chia-I Wu | 9779f6f | 2011-08-05 14:39:18 +0900 | [diff] [blame] | 442 | |
Chia-I Wu | c8e18f8 | 2011-08-26 22:09:56 +0800 | [diff] [blame] | 443 | dri2_surf->window->common.decRef(&dri2_surf->window->common); |
| 444 | } |
Chia-I Wu | 9779f6f | 2011-08-05 14:39:18 +0900 | [diff] [blame] | 445 | |
Liu Zhiquan | b663753 | 2016-11-16 10:11:28 +0800 | [diff] [blame] | 446 | if (dri2_surf->dri_image_back) { |
| 447 | _eglLog(_EGL_DEBUG, "%s : %d : destroy dri_image_back", __func__, __LINE__); |
| 448 | dri2_dpy->image->destroyImage(dri2_surf->dri_image_back); |
| 449 | dri2_surf->dri_image_back = NULL; |
| 450 | } |
| 451 | |
| 452 | if (dri2_surf->dri_image_front) { |
| 453 | _eglLog(_EGL_DEBUG, "%s : %d : destroy dri_image_front", __func__, __LINE__); |
| 454 | dri2_dpy->image->destroyImage(dri2_surf->dri_image_front); |
| 455 | dri2_surf->dri_image_front = NULL; |
| 456 | } |
| 457 | |
Boyan Ding | 868ae3e | 2015-11-25 13:27:02 +0800 | [diff] [blame] | 458 | dri2_dpy->core->destroyDrawable(dri2_surf->dri_drawable); |
Chia-I Wu | 9779f6f | 2011-08-05 14:39:18 +0900 | [diff] [blame] | 459 | |
Zhongmin Wu | e013ce8 | 2017-09-15 18:32:42 +0100 | [diff] [blame] | 460 | dri2_fini_surface(surf); |
Chia-I Wu | 9779f6f | 2011-08-05 14:39:18 +0900 | [diff] [blame] | 461 | free(dri2_surf); |
| 462 | |
| 463 | return EGL_TRUE; |
| 464 | } |
| 465 | |
Wu, Zhongmin | 5fc21c6 | 2018-01-18 15:39:22 +0800 | [diff] [blame] | 466 | static EGLBoolean |
Eric Engestrom | 54fa5ec | 2019-02-02 11:38:45 +0000 | [diff] [blame] | 467 | droid_swap_interval(_EGLDriver *drv, _EGLDisplay *disp, |
Wu, Zhongmin | 5fc21c6 | 2018-01-18 15:39:22 +0800 | [diff] [blame] | 468 | _EGLSurface *surf, EGLint interval) |
| 469 | { |
| 470 | struct dri2_egl_surface *dri2_surf = dri2_egl_surface(surf); |
| 471 | struct ANativeWindow *window = dri2_surf->window; |
| 472 | |
| 473 | if (window->setSwapInterval(window, interval)) |
| 474 | return EGL_FALSE; |
| 475 | |
| 476 | surf->SwapInterval = interval; |
| 477 | return EGL_TRUE; |
| 478 | } |
| 479 | |
Rob Herring | 81a6fff | 2016-04-28 15:37:30 -0500 | [diff] [blame] | 480 | static int |
| 481 | update_buffers(struct dri2_egl_surface *dri2_surf) |
| 482 | { |
Chad Versace | e5eace5 | 2017-05-04 17:46:34 -0700 | [diff] [blame] | 483 | if (dri2_surf->base.Lost) |
| 484 | return -1; |
| 485 | |
Rob Herring | 81a6fff | 2016-04-28 15:37:30 -0500 | [diff] [blame] | 486 | if (dri2_surf->base.Type != EGL_WINDOW_BIT) |
| 487 | return 0; |
| 488 | |
| 489 | /* try to dequeue the next back buffer */ |
Tomasz Figa | 565fa6b | 2016-07-15 16:53:49 +0900 | [diff] [blame] | 490 | if (!dri2_surf->buffer && !droid_window_dequeue_buffer(dri2_surf)) { |
| 491 | _eglLog(_EGL_WARNING, "Could not dequeue buffer from native window"); |
Nicolas Boichat | 63b12b0 | 2017-05-05 10:43:50 +0800 | [diff] [blame] | 492 | dri2_surf->base.Lost = EGL_TRUE; |
Rob Herring | 81a6fff | 2016-04-28 15:37:30 -0500 | [diff] [blame] | 493 | return -1; |
Tomasz Figa | 565fa6b | 2016-07-15 16:53:49 +0900 | [diff] [blame] | 494 | } |
Rob Herring | 81a6fff | 2016-04-28 15:37:30 -0500 | [diff] [blame] | 495 | |
| 496 | /* free outdated buffers and update the surface size */ |
| 497 | if (dri2_surf->base.Width != dri2_surf->buffer->width || |
| 498 | dri2_surf->base.Height != dri2_surf->buffer->height) { |
Gwan-gyeong Mun | 640b6e6 | 2017-08-05 00:16:11 +0900 | [diff] [blame] | 499 | dri2_egl_surface_free_local_buffers(dri2_surf); |
Rob Herring | 81a6fff | 2016-04-28 15:37:30 -0500 | [diff] [blame] | 500 | dri2_surf->base.Width = dri2_surf->buffer->width; |
| 501 | dri2_surf->base.Height = dri2_surf->buffer->height; |
| 502 | } |
| 503 | |
| 504 | return 0; |
| 505 | } |
| 506 | |
Rob Herring | 34ddef3 | 2016-05-01 09:35:28 +0100 | [diff] [blame] | 507 | static int |
Liu Zhiquan | e2610bf | 2016-12-09 19:29:56 +0800 | [diff] [blame] | 508 | get_front_bo(struct dri2_egl_surface *dri2_surf, unsigned int format) |
| 509 | { |
| 510 | struct dri2_egl_display *dri2_dpy = |
| 511 | dri2_egl_display(dri2_surf->base.Resource.Display); |
| 512 | |
| 513 | if (dri2_surf->dri_image_front) |
| 514 | return 0; |
| 515 | |
| 516 | if (dri2_surf->base.Type == EGL_WINDOW_BIT) { |
| 517 | /* According current EGL spec, front buffer rendering |
| 518 | * for window surface is not supported now. |
| 519 | * and mesa doesn't have the implementation of this case. |
| 520 | * Add warning message, but not treat it as error. |
| 521 | */ |
| 522 | _eglLog(_EGL_DEBUG, "DRI driver requested unsupported front buffer for window surface"); |
| 523 | } else if (dri2_surf->base.Type == EGL_PBUFFER_BIT) { |
| 524 | dri2_surf->dri_image_front = |
| 525 | dri2_dpy->image->createImage(dri2_dpy->dri_screen, |
| 526 | dri2_surf->base.Width, |
| 527 | dri2_surf->base.Height, |
| 528 | format, |
| 529 | 0, |
| 530 | dri2_surf); |
| 531 | if (!dri2_surf->dri_image_front) { |
| 532 | _eglLog(_EGL_WARNING, "dri2_image_front allocation failed"); |
| 533 | return -1; |
| 534 | } |
| 535 | } |
| 536 | |
| 537 | return 0; |
| 538 | } |
| 539 | |
| 540 | static int |
Chad Versace | 22d6b08 | 2017-05-23 17:26:52 -0700 | [diff] [blame] | 541 | get_back_bo(struct dri2_egl_surface *dri2_surf) |
Rob Herring | 34ddef3 | 2016-05-01 09:35:28 +0100 | [diff] [blame] | 542 | { |
| 543 | struct dri2_egl_display *dri2_dpy = |
| 544 | dri2_egl_display(dri2_surf->base.Resource.Display); |
Emil Velikov | fb65364 | 2016-05-01 09:35:56 +0100 | [diff] [blame] | 545 | int fourcc, pitch; |
Rob Herring | 34ddef3 | 2016-05-01 09:35:28 +0100 | [diff] [blame] | 546 | int offset = 0, fd; |
| 547 | |
Liu Zhiquan | b663753 | 2016-11-16 10:11:28 +0800 | [diff] [blame] | 548 | if (dri2_surf->dri_image_back) |
| 549 | return 0; |
Tomasz Figa | 9e1248d | 2016-07-15 16:53:50 +0900 | [diff] [blame] | 550 | |
Liu Zhiquan | e2610bf | 2016-12-09 19:29:56 +0800 | [diff] [blame] | 551 | if (dri2_surf->base.Type == EGL_WINDOW_BIT) { |
| 552 | if (!dri2_surf->buffer) { |
| 553 | _eglLog(_EGL_WARNING, "Could not get native buffer"); |
| 554 | return -1; |
Liu Zhiquan | b663753 | 2016-11-16 10:11:28 +0800 | [diff] [blame] | 555 | } |
| 556 | |
Liu Zhiquan | e2610bf | 2016-12-09 19:29:56 +0800 | [diff] [blame] | 557 | fd = get_native_buffer_fd(dri2_surf->buffer); |
| 558 | if (fd < 0) { |
| 559 | _eglLog(_EGL_WARNING, "Could not get native buffer FD"); |
| 560 | return -1; |
| 561 | } |
| 562 | |
| 563 | fourcc = get_fourcc(dri2_surf->buffer->format); |
| 564 | |
| 565 | pitch = dri2_surf->buffer->stride * |
| 566 | get_format_bpp(dri2_surf->buffer->format); |
| 567 | |
| 568 | if (fourcc == -1 || pitch == 0) { |
| 569 | _eglLog(_EGL_WARNING, "Invalid buffer fourcc(%x) or pitch(%d)", |
| 570 | fourcc, pitch); |
| 571 | return -1; |
| 572 | } |
| 573 | |
| 574 | dri2_surf->dri_image_back = |
| 575 | dri2_dpy->image->createImageFromFds(dri2_dpy->dri_screen, |
| 576 | dri2_surf->base.Width, |
| 577 | dri2_surf->base.Height, |
| 578 | fourcc, |
| 579 | &fd, |
| 580 | 1, |
| 581 | &pitch, |
| 582 | &offset, |
| 583 | dri2_surf); |
| 584 | if (!dri2_surf->dri_image_back) { |
| 585 | _eglLog(_EGL_WARNING, "failed to create DRI image from FD"); |
| 586 | return -1; |
| 587 | } |
| 588 | } else if (dri2_surf->base.Type == EGL_PBUFFER_BIT) { |
Liu Zhiquan | b663753 | 2016-11-16 10:11:28 +0800 | [diff] [blame] | 589 | /* The EGL 1.5 spec states that pbuffers are single-buffered. Specifically, |
| 590 | * the spec states that they have a back buffer but no front buffer, in |
| 591 | * contrast to pixmaps, which have a front buffer but no back buffer. |
| 592 | * |
| 593 | * Single-buffered surfaces with no front buffer confuse Mesa; so we deviate |
| 594 | * from the spec, following the precedent of Mesa's EGL X11 platform. The |
| 595 | * X11 platform correctly assigns pbuffers to single-buffered configs, but |
| 596 | * assigns the pbuffer a front buffer instead of a back buffer. |
| 597 | * |
| 598 | * Pbuffers in the X11 platform mostly work today, so let's just copy its |
| 599 | * behavior instead of trying to fix (and hence potentially breaking) the |
| 600 | * world. |
Tomasz Figa | 217af75 | 2016-08-02 20:07:50 +0900 | [diff] [blame] | 601 | */ |
Liu Zhiquan | e2610bf | 2016-12-09 19:29:56 +0800 | [diff] [blame] | 602 | _eglLog(_EGL_DEBUG, "DRI driver requested unsupported back buffer for pbuffer surface"); |
| 603 | } |
Liu Zhiquan | b663753 | 2016-11-16 10:11:28 +0800 | [diff] [blame] | 604 | |
Liu Zhiquan | e2610bf | 2016-12-09 19:29:56 +0800 | [diff] [blame] | 605 | return 0; |
| 606 | } |
| 607 | |
| 608 | /* Some drivers will pass multiple bits in buffer_mask. |
| 609 | * For such case, will go through all the bits, and |
| 610 | * will not return error when unsupported buffer is requested, only |
| 611 | * return error when the allocation for supported buffer failed. |
| 612 | */ |
| 613 | static int |
| 614 | droid_image_get_buffers(__DRIdrawable *driDrawable, |
| 615 | unsigned int format, |
| 616 | uint32_t *stamp, |
| 617 | void *loaderPrivate, |
| 618 | uint32_t buffer_mask, |
| 619 | struct __DRIimageList *images) |
| 620 | { |
| 621 | struct dri2_egl_surface *dri2_surf = loaderPrivate; |
| 622 | |
| 623 | images->image_mask = 0; |
| 624 | images->front = NULL; |
| 625 | images->back = NULL; |
| 626 | |
| 627 | if (update_buffers(dri2_surf) < 0) |
| 628 | return 0; |
| 629 | |
Chad Versace | ed7c694 | 2018-04-30 22:35:17 -0700 | [diff] [blame] | 630 | if (_eglSurfaceInSharedBufferMode(&dri2_surf->base)) { |
| 631 | if (get_back_bo(dri2_surf) < 0) |
| 632 | return 0; |
| 633 | |
| 634 | /* We have dri_image_back because this is a window surface and |
| 635 | * get_back_bo() succeeded. |
| 636 | */ |
| 637 | assert(dri2_surf->dri_image_back); |
| 638 | images->back = dri2_surf->dri_image_back; |
| 639 | images->image_mask |= __DRI_IMAGE_BUFFER_SHARED; |
| 640 | |
| 641 | /* There exists no accompanying back nor front buffer. */ |
| 642 | return 1; |
| 643 | } |
| 644 | |
Liu Zhiquan | e2610bf | 2016-12-09 19:29:56 +0800 | [diff] [blame] | 645 | if (buffer_mask & __DRI_IMAGE_BUFFER_FRONT) { |
| 646 | if (get_front_bo(dri2_surf, format) < 0) |
Liu Zhiquan | b663753 | 2016-11-16 10:11:28 +0800 | [diff] [blame] | 647 | return 0; |
Liu Zhiquan | b663753 | 2016-11-16 10:11:28 +0800 | [diff] [blame] | 648 | |
Liu Zhiquan | e2610bf | 2016-12-09 19:29:56 +0800 | [diff] [blame] | 649 | if (dri2_surf->dri_image_front) { |
| 650 | images->front = dri2_surf->dri_image_front; |
| 651 | images->image_mask |= __DRI_IMAGE_BUFFER_FRONT; |
| 652 | } |
Rob Herring | 34ddef3 | 2016-05-01 09:35:28 +0100 | [diff] [blame] | 653 | } |
| 654 | |
Tomasz Figa | 217af75 | 2016-08-02 20:07:50 +0900 | [diff] [blame] | 655 | if (buffer_mask & __DRI_IMAGE_BUFFER_BACK) { |
Chad Versace | 22d6b08 | 2017-05-23 17:26:52 -0700 | [diff] [blame] | 656 | if (get_back_bo(dri2_surf) < 0) |
Liu Zhiquan | b663753 | 2016-11-16 10:11:28 +0800 | [diff] [blame] | 657 | return 0; |
Liu Zhiquan | b663753 | 2016-11-16 10:11:28 +0800 | [diff] [blame] | 658 | |
Liu Zhiquan | e2610bf | 2016-12-09 19:29:56 +0800 | [diff] [blame] | 659 | if (dri2_surf->dri_image_back) { |
| 660 | images->back = dri2_surf->dri_image_back; |
| 661 | images->image_mask |= __DRI_IMAGE_BUFFER_BACK; |
| 662 | } |
Tomasz Figa | 217af75 | 2016-08-02 20:07:50 +0900 | [diff] [blame] | 663 | } |
Rob Herring | 34ddef3 | 2016-05-01 09:35:28 +0100 | [diff] [blame] | 664 | |
| 665 | return 1; |
| 666 | } |
| 667 | |
Xiaosong Wei | 2acc69d | 2017-02-08 10:46:02 +0800 | [diff] [blame] | 668 | static EGLint |
| 669 | droid_query_buffer_age(_EGLDriver *drv, |
| 670 | _EGLDisplay *disp, _EGLSurface *surface) |
| 671 | { |
| 672 | struct dri2_egl_surface *dri2_surf = dri2_egl_surface(surface); |
| 673 | |
| 674 | if (update_buffers(dri2_surf) < 0) { |
| 675 | _eglError(EGL_BAD_ALLOC, "droid_query_buffer_age"); |
Tapani Pälli | 8fac894 | 2017-06-08 12:24:24 +0300 | [diff] [blame] | 676 | return -1; |
Xiaosong Wei | 2acc69d | 2017-02-08 10:46:02 +0800 | [diff] [blame] | 677 | } |
| 678 | |
Tapani Pälli | 8fac894 | 2017-06-08 12:24:24 +0300 | [diff] [blame] | 679 | return dri2_surf->back ? dri2_surf->back->age : 0; |
Xiaosong Wei | 2acc69d | 2017-02-08 10:46:02 +0800 | [diff] [blame] | 680 | } |
| 681 | |
Chia-I Wu | 9779f6f | 2011-08-05 14:39:18 +0900 | [diff] [blame] | 682 | static EGLBoolean |
| 683 | droid_swap_buffers(_EGLDriver *drv, _EGLDisplay *disp, _EGLSurface *draw) |
| 684 | { |
Chia-I Wu | 9779f6f | 2011-08-05 14:39:18 +0900 | [diff] [blame] | 685 | struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp); |
| 686 | struct dri2_egl_surface *dri2_surf = dri2_egl_surface(draw); |
Chad Versace | ed7c694 | 2018-04-30 22:35:17 -0700 | [diff] [blame] | 687 | const bool has_mutable_rb = _eglSurfaceHasMutableRenderBuffer(draw); |
| 688 | |
| 689 | /* From the EGL_KHR_mutable_render_buffer spec (v12): |
| 690 | * |
| 691 | * If surface is a single-buffered window, pixmap, or pbuffer surface |
| 692 | * for which there is no pending change to the EGL_RENDER_BUFFER |
| 693 | * attribute, eglSwapBuffers has no effect. |
| 694 | */ |
| 695 | if (has_mutable_rb && |
| 696 | draw->RequestedRenderBuffer == EGL_SINGLE_BUFFER && |
| 697 | draw->ActiveRenderBuffer == EGL_SINGLE_BUFFER) { |
| 698 | _eglLog(_EGL_DEBUG, "%s: remain in shared buffer mode", __func__); |
| 699 | return EGL_TRUE; |
| 700 | } |
| 701 | |
Xiaosong Wei | 2acc69d | 2017-02-08 10:46:02 +0800 | [diff] [blame] | 702 | for (int i = 0; i < ARRAY_SIZE(dri2_surf->color_buffers); i++) { |
| 703 | if (dri2_surf->color_buffers[i].age > 0) |
| 704 | dri2_surf->color_buffers[i].age++; |
| 705 | } |
Tapani Pälli | f347bac | 2017-05-18 10:21:59 +0300 | [diff] [blame] | 706 | |
| 707 | /* "XXX: we don't use get_back_bo() since it causes regressions in |
| 708 | * several dEQP tests. |
| 709 | */ |
| 710 | if (dri2_surf->back) |
| 711 | dri2_surf->back->age = 1; |
Xiaosong Wei | 2acc69d | 2017-02-08 10:46:02 +0800 | [diff] [blame] | 712 | |
Eric Anholt | 70e8ccc | 2014-12-21 11:51:33 -0800 | [diff] [blame] | 713 | dri2_flush_drawable_for_swapbuffers(disp, draw); |
Chia-I Wu | 9779f6f | 2011-08-05 14:39:18 +0900 | [diff] [blame] | 714 | |
Chad Versace | e5eace5 | 2017-05-04 17:46:34 -0700 | [diff] [blame] | 715 | /* dri2_surf->buffer can be null even when no error has occured. For |
| 716 | * example, if the user has called no GL rendering commands since the |
| 717 | * previous eglSwapBuffers, then the driver may have not triggered |
| 718 | * a callback to ANativeWindow::dequeueBuffer, in which case |
| 719 | * dri2_surf->buffer remains null. |
| 720 | */ |
Chia-I Wu | 9779f6f | 2011-08-05 14:39:18 +0900 | [diff] [blame] | 721 | if (dri2_surf->buffer) |
Haixia Shi | 1ea233c | 2016-06-02 12:48:23 -0700 | [diff] [blame] | 722 | droid_window_enqueue_buffer(disp, dri2_surf); |
Chia-I Wu | 9779f6f | 2011-08-05 14:39:18 +0900 | [diff] [blame] | 723 | |
Boyan Ding | 868ae3e | 2015-11-25 13:27:02 +0800 | [diff] [blame] | 724 | dri2_dpy->flush->invalidate(dri2_surf->dri_drawable); |
Chia-I Wu | 9779f6f | 2011-08-05 14:39:18 +0900 | [diff] [blame] | 725 | |
Chad Versace | ed7c694 | 2018-04-30 22:35:17 -0700 | [diff] [blame] | 726 | /* Update the shared buffer mode */ |
| 727 | if (has_mutable_rb && |
| 728 | draw->ActiveRenderBuffer != draw->RequestedRenderBuffer) { |
| 729 | bool mode = (draw->RequestedRenderBuffer == EGL_SINGLE_BUFFER); |
| 730 | _eglLog(_EGL_DEBUG, "%s: change to shared buffer mode %d", |
| 731 | __func__, mode); |
| 732 | |
| 733 | if (!droid_set_shared_buffer_mode(disp, draw, mode)) |
| 734 | return EGL_FALSE; |
| 735 | draw->ActiveRenderBuffer = draw->RequestedRenderBuffer; |
| 736 | } |
| 737 | |
Chia-I Wu | 9779f6f | 2011-08-05 14:39:18 +0900 | [diff] [blame] | 738 | return EGL_TRUE; |
| 739 | } |
| 740 | |
Harish Krupo | 9827547 | 2017-06-09 20:13:34 +0530 | [diff] [blame] | 741 | #if ANDROID_API_LEVEL >= 23 |
| 742 | static EGLBoolean |
| 743 | droid_set_damage_region(_EGLDriver *drv, |
| 744 | _EGLDisplay *disp, |
| 745 | _EGLSurface *draw, const EGLint* rects, EGLint n_rects) |
| 746 | { |
| 747 | struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp); |
| 748 | struct dri2_egl_surface *dri2_surf = dri2_egl_surface(draw); |
| 749 | android_native_rect_t* droid_rects = NULL; |
| 750 | int ret; |
| 751 | |
| 752 | if (n_rects == 0) |
| 753 | return EGL_TRUE; |
| 754 | |
| 755 | droid_rects = malloc(n_rects * sizeof(android_native_rect_t)); |
Emil Velikov | c58af5c | 2017-06-20 15:40:28 +0100 | [diff] [blame] | 756 | if (droid_rects == NULL) |
| 757 | return _eglError(EGL_BAD_ALLOC, "eglSetDamageRegionKHR"); |
Harish Krupo | 9827547 | 2017-06-09 20:13:34 +0530 | [diff] [blame] | 758 | |
| 759 | for (EGLint num_drects = 0; num_drects < n_rects; num_drects++) { |
| 760 | EGLint i = num_drects * 4; |
| 761 | droid_rects[num_drects].left = rects[i]; |
| 762 | droid_rects[num_drects].bottom = rects[i + 1]; |
| 763 | droid_rects[num_drects].right = rects[i] + rects[i + 2]; |
| 764 | droid_rects[num_drects].top = rects[i + 1] + rects[i + 3]; |
| 765 | } |
| 766 | |
| 767 | /* |
| 768 | * XXX/TODO: Need to check for other return values |
| 769 | */ |
| 770 | |
| 771 | ret = native_window_set_surface_damage(dri2_surf->window, droid_rects, n_rects); |
| 772 | free(droid_rects); |
| 773 | |
| 774 | return ret == 0 ? EGL_TRUE : EGL_FALSE; |
| 775 | } |
| 776 | #endif |
| 777 | |
Chia-I Wu | 9779f6f | 2011-08-05 14:39:18 +0900 | [diff] [blame] | 778 | static _EGLImage * |
Tomasz Figa | 51727b1 | 2016-11-10 16:55:53 +0900 | [diff] [blame] | 779 | droid_create_image_from_prime_fd_yuv(_EGLDisplay *disp, _EGLContext *ctx, |
| 780 | struct ANativeWindowBuffer *buf, int fd) |
Tomasz Figa | e77b493 | 2016-08-02 20:07:52 +0900 | [diff] [blame] | 781 | { |
Tomasz Figa | 51727b1 | 2016-11-10 16:55:53 +0900 | [diff] [blame] | 782 | struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp); |
| 783 | struct android_ycbcr ycbcr; |
| 784 | size_t offsets[3]; |
| 785 | size_t pitches[3]; |
Eric Engestrom | 0c3287e | 2018-11-09 11:55:10 +0000 | [diff] [blame] | 786 | enum chroma_order chroma_order; |
Tomasz Figa | 51727b1 | 2016-11-10 16:55:53 +0900 | [diff] [blame] | 787 | int fourcc; |
| 788 | int ret; |
Tomasz Figa | e77b493 | 2016-08-02 20:07:52 +0900 | [diff] [blame] | 789 | |
Tomasz Figa | 51727b1 | 2016-11-10 16:55:53 +0900 | [diff] [blame] | 790 | if (!dri2_dpy->gralloc->lock_ycbcr) { |
| 791 | _eglLog(_EGL_WARNING, "Gralloc does not support lock_ycbcr"); |
| 792 | return NULL; |
| 793 | } |
| 794 | |
| 795 | memset(&ycbcr, 0, sizeof(ycbcr)); |
| 796 | ret = dri2_dpy->gralloc->lock_ycbcr(dri2_dpy->gralloc, buf->handle, |
| 797 | 0, 0, 0, 0, 0, &ycbcr); |
| 798 | if (ret) { |
Tomasz Figa | 5364e73 | 2017-12-04 19:22:39 +0100 | [diff] [blame] | 799 | /* HACK: See droid_create_image_from_prime_fd() and |
| 800 | * https://issuetracker.google.com/32077885.*/ |
| 801 | if (buf->format == HAL_PIXEL_FORMAT_IMPLEMENTATION_DEFINED) |
| 802 | return NULL; |
| 803 | |
Tomasz Figa | 51727b1 | 2016-11-10 16:55:53 +0900 | [diff] [blame] | 804 | _eglLog(_EGL_WARNING, "gralloc->lock_ycbcr failed: %d", ret); |
| 805 | return NULL; |
| 806 | } |
| 807 | dri2_dpy->gralloc->unlock(dri2_dpy->gralloc, buf->handle); |
| 808 | |
| 809 | /* When lock_ycbcr's usage argument contains no SW_READ/WRITE flags |
| 810 | * it will return the .y/.cb/.cr pointers based on a NULL pointer, |
| 811 | * so they can be interpreted as offsets. */ |
| 812 | offsets[0] = (size_t)ycbcr.y; |
| 813 | /* We assume here that all the planes are located in one DMA-buf. */ |
Eric Engestrom | 0c3287e | 2018-11-09 11:55:10 +0000 | [diff] [blame] | 814 | if ((size_t)ycbcr.cr < (size_t)ycbcr.cb) { |
| 815 | chroma_order = YCrCb; |
Tomasz Figa | 51727b1 | 2016-11-10 16:55:53 +0900 | [diff] [blame] | 816 | offsets[1] = (size_t)ycbcr.cr; |
| 817 | offsets[2] = (size_t)ycbcr.cb; |
| 818 | } else { |
Eric Engestrom | 0c3287e | 2018-11-09 11:55:10 +0000 | [diff] [blame] | 819 | chroma_order = YCbCr; |
Tomasz Figa | 51727b1 | 2016-11-10 16:55:53 +0900 | [diff] [blame] | 820 | offsets[1] = (size_t)ycbcr.cb; |
| 821 | offsets[2] = (size_t)ycbcr.cr; |
| 822 | } |
| 823 | |
| 824 | /* .ystride is the line length (in bytes) of the Y plane, |
| 825 | * .cstride is the line length (in bytes) of any of the remaining |
| 826 | * Cb/Cr/CbCr planes, assumed to be the same for Cb and Cr for fully |
| 827 | * planar formats. */ |
| 828 | pitches[0] = ycbcr.ystride; |
| 829 | pitches[1] = pitches[2] = ycbcr.cstride; |
| 830 | |
| 831 | /* .chroma_step is the byte distance between the same chroma channel |
| 832 | * values of subsequent pixels, assumed to be the same for Cb and Cr. */ |
Eric Engestrom | 0c3287e | 2018-11-09 11:55:10 +0000 | [diff] [blame] | 833 | fourcc = get_fourcc_yuv(buf->format, chroma_order, ycbcr.chroma_step); |
Tomasz Figa | 3723e98 | 2016-08-02 20:07:54 +0900 | [diff] [blame] | 834 | if (fourcc == -1) { |
Eric Engestrom | 0c3287e | 2018-11-09 11:55:10 +0000 | [diff] [blame] | 835 | _eglLog(_EGL_WARNING, "unsupported YUV format, native = %x, chroma_order = %s, chroma_step = %d", |
| 836 | buf->format, chroma_order == YCbCr ? "YCbCr" : "YCrCb", ycbcr.chroma_step); |
Tomasz Figa | 3723e98 | 2016-08-02 20:07:54 +0900 | [diff] [blame] | 837 | return NULL; |
| 838 | } |
| 839 | |
Tomasz Figa | 51727b1 | 2016-11-10 16:55:53 +0900 | [diff] [blame] | 840 | if (ycbcr.chroma_step == 2) { |
Robert Foss | a4d3371 | 2017-06-15 16:47:53 -0400 | [diff] [blame] | 841 | /* Semi-planar Y + CbCr or Y + CrCb format. */ |
Tomasz Figa | 51727b1 | 2016-11-10 16:55:53 +0900 | [diff] [blame] | 842 | const EGLint attr_list_2plane[] = { |
| 843 | EGL_WIDTH, buf->width, |
| 844 | EGL_HEIGHT, buf->height, |
| 845 | EGL_LINUX_DRM_FOURCC_EXT, fourcc, |
| 846 | EGL_DMA_BUF_PLANE0_FD_EXT, fd, |
| 847 | EGL_DMA_BUF_PLANE0_PITCH_EXT, pitches[0], |
| 848 | EGL_DMA_BUF_PLANE0_OFFSET_EXT, offsets[0], |
| 849 | EGL_DMA_BUF_PLANE1_FD_EXT, fd, |
| 850 | EGL_DMA_BUF_PLANE1_PITCH_EXT, pitches[1], |
| 851 | EGL_DMA_BUF_PLANE1_OFFSET_EXT, offsets[1], |
| 852 | EGL_NONE, 0 |
| 853 | }; |
Tomasz Figa | 3723e98 | 2016-08-02 20:07:54 +0900 | [diff] [blame] | 854 | |
Tomasz Figa | 51727b1 | 2016-11-10 16:55:53 +0900 | [diff] [blame] | 855 | return dri2_create_image_dma_buf(disp, ctx, NULL, attr_list_2plane); |
| 856 | } else { |
| 857 | /* Fully planar Y + Cb + Cr or Y + Cr + Cb format. */ |
| 858 | const EGLint attr_list_3plane[] = { |
Tomasz Figa | 3723e98 | 2016-08-02 20:07:54 +0900 | [diff] [blame] | 859 | EGL_WIDTH, buf->width, |
| 860 | EGL_HEIGHT, buf->height, |
| 861 | EGL_LINUX_DRM_FOURCC_EXT, fourcc, |
| 862 | EGL_DMA_BUF_PLANE0_FD_EXT, fd, |
| 863 | EGL_DMA_BUF_PLANE0_PITCH_EXT, pitches[0], |
| 864 | EGL_DMA_BUF_PLANE0_OFFSET_EXT, offsets[0], |
| 865 | EGL_DMA_BUF_PLANE1_FD_EXT, fd, |
| 866 | EGL_DMA_BUF_PLANE1_PITCH_EXT, pitches[1], |
| 867 | EGL_DMA_BUF_PLANE1_OFFSET_EXT, offsets[1], |
| 868 | EGL_DMA_BUF_PLANE2_FD_EXT, fd, |
| 869 | EGL_DMA_BUF_PLANE2_PITCH_EXT, pitches[2], |
| 870 | EGL_DMA_BUF_PLANE2_OFFSET_EXT, offsets[2], |
| 871 | EGL_NONE, 0 |
| 872 | }; |
| 873 | |
Tomasz Figa | 51727b1 | 2016-11-10 16:55:53 +0900 | [diff] [blame] | 874 | return dri2_create_image_dma_buf(disp, ctx, NULL, attr_list_3plane); |
| 875 | } |
| 876 | } |
| 877 | |
| 878 | static _EGLImage * |
| 879 | droid_create_image_from_prime_fd(_EGLDisplay *disp, _EGLContext *ctx, |
| 880 | struct ANativeWindowBuffer *buf, int fd) |
| 881 | { |
| 882 | unsigned int pitch; |
| 883 | |
Tomasz Figa | 5364e73 | 2017-12-04 19:22:39 +0100 | [diff] [blame] | 884 | if (is_yuv(buf->format)) { |
| 885 | _EGLImage *image; |
| 886 | |
| 887 | image = droid_create_image_from_prime_fd_yuv(disp, ctx, buf, fd); |
| 888 | /* |
| 889 | * HACK: https://issuetracker.google.com/32077885 |
| 890 | * There is no API available to properly query the IMPLEMENTATION_DEFINED |
| 891 | * format. As a workaround we rely here on gralloc allocating either |
| 892 | * an arbitrary YCbCr 4:2:0 or RGBX_8888, with the latter being recognized |
| 893 | * by lock_ycbcr failing. |
| 894 | */ |
| 895 | if (image || buf->format != HAL_PIXEL_FORMAT_IMPLEMENTATION_DEFINED) |
| 896 | return image; |
| 897 | } |
Tomasz Figa | 51727b1 | 2016-11-10 16:55:53 +0900 | [diff] [blame] | 898 | |
| 899 | const int fourcc = get_fourcc(buf->format); |
| 900 | if (fourcc == -1) { |
| 901 | _eglError(EGL_BAD_PARAMETER, "eglCreateEGLImageKHR"); |
| 902 | return NULL; |
| 903 | } |
| 904 | |
| 905 | pitch = buf->stride * get_format_bpp(buf->format); |
| 906 | if (pitch == 0) { |
| 907 | _eglError(EGL_BAD_PARAMETER, "eglCreateEGLImageKHR"); |
| 908 | return NULL; |
Tomasz Figa | 3723e98 | 2016-08-02 20:07:54 +0900 | [diff] [blame] | 909 | } |
| 910 | |
| 911 | const EGLint attr_list[] = { |
Tomasz Figa | e77b493 | 2016-08-02 20:07:52 +0900 | [diff] [blame] | 912 | EGL_WIDTH, buf->width, |
| 913 | EGL_HEIGHT, buf->height, |
| 914 | EGL_LINUX_DRM_FOURCC_EXT, fourcc, |
| 915 | EGL_DMA_BUF_PLANE0_FD_EXT, fd, |
Tomasz Figa | 51727b1 | 2016-11-10 16:55:53 +0900 | [diff] [blame] | 916 | EGL_DMA_BUF_PLANE0_PITCH_EXT, pitch, |
Tomasz Figa | e77b493 | 2016-08-02 20:07:52 +0900 | [diff] [blame] | 917 | EGL_DMA_BUF_PLANE0_OFFSET_EXT, 0, |
| 918 | EGL_NONE, 0 |
| 919 | }; |
| 920 | |
Tomasz Figa | e77b493 | 2016-08-02 20:07:52 +0900 | [diff] [blame] | 921 | return dri2_create_image_dma_buf(disp, ctx, NULL, attr_list); |
| 922 | } |
| 923 | |
Rob Herring | 3f7bca4 | 2018-04-26 16:02:01 +0200 | [diff] [blame] | 924 | #ifdef HAVE_DRM_GRALLOC |
Tomasz Figa | e77b493 | 2016-08-02 20:07:52 +0900 | [diff] [blame] | 925 | static _EGLImage * |
| 926 | droid_create_image_from_name(_EGLDisplay *disp, _EGLContext *ctx, |
| 927 | struct ANativeWindowBuffer *buf) |
Chia-I Wu | 9779f6f | 2011-08-05 14:39:18 +0900 | [diff] [blame] | 928 | { |
| 929 | struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp); |
| 930 | struct dri2_egl_image *dri2_img; |
Tomasz Figa | e77b493 | 2016-08-02 20:07:52 +0900 | [diff] [blame] | 931 | int name; |
Emil Velikov | fb65364 | 2016-05-01 09:35:56 +0100 | [diff] [blame] | 932 | int format; |
Chia-I Wu | 9779f6f | 2011-08-05 14:39:18 +0900 | [diff] [blame] | 933 | |
Chia-I Wu | 9779f6f | 2011-08-05 14:39:18 +0900 | [diff] [blame] | 934 | name = get_native_buffer_name(buf); |
| 935 | if (!name) { |
| 936 | _eglError(EGL_BAD_PARAMETER, "eglCreateEGLImageKHR"); |
| 937 | return NULL; |
| 938 | } |
| 939 | |
Emil Velikov | fb65364 | 2016-05-01 09:35:56 +0100 | [diff] [blame] | 940 | format = get_format(buf->format); |
| 941 | if (format == -1) |
| 942 | return NULL; |
| 943 | |
Chia-I Wu | 9779f6f | 2011-08-05 14:39:18 +0900 | [diff] [blame] | 944 | dri2_img = calloc(1, sizeof(*dri2_img)); |
| 945 | if (!dri2_img) { |
| 946 | _eglError(EGL_BAD_ALLOC, "droid_create_image_mesa_drm"); |
| 947 | return NULL; |
| 948 | } |
| 949 | |
Emil Velikov | d42b095 | 2017-06-20 15:22:39 +0100 | [diff] [blame] | 950 | _eglInitImage(&dri2_img->base, disp); |
Chia-I Wu | 9779f6f | 2011-08-05 14:39:18 +0900 | [diff] [blame] | 951 | |
| 952 | dri2_img->dri_image = |
| 953 | dri2_dpy->image->createImageFromName(dri2_dpy->dri_screen, |
| 954 | buf->width, |
| 955 | buf->height, |
Emil Velikov | fb65364 | 2016-05-01 09:35:56 +0100 | [diff] [blame] | 956 | format, |
Chia-I Wu | 9779f6f | 2011-08-05 14:39:18 +0900 | [diff] [blame] | 957 | name, |
| 958 | buf->stride, |
| 959 | dri2_img); |
| 960 | if (!dri2_img->dri_image) { |
| 961 | free(dri2_img); |
| 962 | _eglError(EGL_BAD_ALLOC, "droid_create_image_mesa_drm"); |
| 963 | return NULL; |
| 964 | } |
| 965 | |
| 966 | return &dri2_img->base; |
| 967 | } |
Rob Herring | 3f7bca4 | 2018-04-26 16:02:01 +0200 | [diff] [blame] | 968 | #endif /* HAVE_DRM_GRALLOC */ |
Chia-I Wu | 9779f6f | 2011-08-05 14:39:18 +0900 | [diff] [blame] | 969 | |
Haixia Shi | a7c6993 | 2016-07-28 10:51:12 -0700 | [diff] [blame] | 970 | static EGLBoolean |
Eric Engestrom | 54fa5ec | 2019-02-02 11:38:45 +0000 | [diff] [blame] | 971 | droid_query_surface(_EGLDriver *drv, _EGLDisplay *disp, _EGLSurface *surf, |
Haixia Shi | a7c6993 | 2016-07-28 10:51:12 -0700 | [diff] [blame] | 972 | EGLint attribute, EGLint *value) |
| 973 | { |
| 974 | struct dri2_egl_surface *dri2_surf = dri2_egl_surface(surf); |
| 975 | switch (attribute) { |
| 976 | case EGL_WIDTH: |
| 977 | if (dri2_surf->base.Type == EGL_WINDOW_BIT && dri2_surf->window) { |
| 978 | dri2_surf->window->query(dri2_surf->window, |
| 979 | NATIVE_WINDOW_DEFAULT_WIDTH, value); |
| 980 | return EGL_TRUE; |
| 981 | } |
| 982 | break; |
| 983 | case EGL_HEIGHT: |
| 984 | if (dri2_surf->base.Type == EGL_WINDOW_BIT && dri2_surf->window) { |
| 985 | dri2_surf->window->query(dri2_surf->window, |
| 986 | NATIVE_WINDOW_DEFAULT_HEIGHT, value); |
| 987 | return EGL_TRUE; |
| 988 | } |
| 989 | break; |
| 990 | default: |
| 991 | break; |
| 992 | } |
Eric Engestrom | 54fa5ec | 2019-02-02 11:38:45 +0000 | [diff] [blame] | 993 | return _eglQuerySurface(drv, disp, surf, attribute, value); |
Haixia Shi | a7c6993 | 2016-07-28 10:51:12 -0700 | [diff] [blame] | 994 | } |
| 995 | |
Chia-I Wu | 9779f6f | 2011-08-05 14:39:18 +0900 | [diff] [blame] | 996 | static _EGLImage * |
Tomasz Figa | e77b493 | 2016-08-02 20:07:52 +0900 | [diff] [blame] | 997 | dri2_create_image_android_native_buffer(_EGLDisplay *disp, |
| 998 | _EGLContext *ctx, |
| 999 | struct ANativeWindowBuffer *buf) |
| 1000 | { |
| 1001 | int fd; |
| 1002 | |
| 1003 | if (ctx != NULL) { |
| 1004 | /* From the EGL_ANDROID_image_native_buffer spec: |
| 1005 | * |
| 1006 | * * If <target> is EGL_NATIVE_BUFFER_ANDROID and <ctx> is not |
| 1007 | * EGL_NO_CONTEXT, the error EGL_BAD_CONTEXT is generated. |
| 1008 | */ |
| 1009 | _eglError(EGL_BAD_CONTEXT, "eglCreateEGLImageKHR: for " |
| 1010 | "EGL_NATIVE_BUFFER_ANDROID, the context must be " |
| 1011 | "EGL_NO_CONTEXT"); |
| 1012 | return NULL; |
| 1013 | } |
| 1014 | |
| 1015 | if (!buf || buf->common.magic != ANDROID_NATIVE_BUFFER_MAGIC || |
| 1016 | buf->common.version != sizeof(*buf)) { |
| 1017 | _eglError(EGL_BAD_PARAMETER, "eglCreateEGLImageKHR"); |
| 1018 | return NULL; |
| 1019 | } |
| 1020 | |
| 1021 | fd = get_native_buffer_fd(buf); |
| 1022 | if (fd >= 0) |
| 1023 | return droid_create_image_from_prime_fd(disp, ctx, buf, fd); |
| 1024 | |
Rob Herring | 3f7bca4 | 2018-04-26 16:02:01 +0200 | [diff] [blame] | 1025 | #ifdef HAVE_DRM_GRALLOC |
Tomasz Figa | e77b493 | 2016-08-02 20:07:52 +0900 | [diff] [blame] | 1026 | return droid_create_image_from_name(disp, ctx, buf); |
Rob Herring | 3f7bca4 | 2018-04-26 16:02:01 +0200 | [diff] [blame] | 1027 | #else |
| 1028 | return NULL; |
| 1029 | #endif |
Tomasz Figa | e77b493 | 2016-08-02 20:07:52 +0900 | [diff] [blame] | 1030 | } |
| 1031 | |
| 1032 | static _EGLImage * |
Chia-I Wu | 9779f6f | 2011-08-05 14:39:18 +0900 | [diff] [blame] | 1033 | droid_create_image_khr(_EGLDriver *drv, _EGLDisplay *disp, |
| 1034 | _EGLContext *ctx, EGLenum target, |
| 1035 | EGLClientBuffer buffer, const EGLint *attr_list) |
| 1036 | { |
| 1037 | switch (target) { |
| 1038 | case EGL_NATIVE_BUFFER_ANDROID: |
Emil Velikov | 9b3c748 | 2016-05-01 12:42:54 +0100 | [diff] [blame] | 1039 | return dri2_create_image_android_native_buffer(disp, ctx, |
Chia-I Wu | 75cc24c | 2011-11-25 11:59:02 +0800 | [diff] [blame] | 1040 | (struct ANativeWindowBuffer *) buffer); |
Chia-I Wu | 9779f6f | 2011-08-05 14:39:18 +0900 | [diff] [blame] | 1041 | default: |
| 1042 | return dri2_create_image_khr(drv, disp, ctx, target, buffer, attr_list); |
| 1043 | } |
| 1044 | } |
| 1045 | |
| 1046 | static void |
Chia-I Wu | 9779f6f | 2011-08-05 14:39:18 +0900 | [diff] [blame] | 1047 | droid_flush_front_buffer(__DRIdrawable * driDrawable, void *loaderPrivate) |
| 1048 | { |
| 1049 | } |
| 1050 | |
Rob Herring | 3f7bca4 | 2018-04-26 16:02:01 +0200 | [diff] [blame] | 1051 | #ifdef HAVE_DRM_GRALLOC |
Chia-I Wu | 384f228 | 2011-08-26 23:49:04 +0800 | [diff] [blame] | 1052 | static int |
| 1053 | droid_get_buffers_parse_attachments(struct dri2_egl_surface *dri2_surf, |
| 1054 | unsigned int *attachments, int count) |
| 1055 | { |
Chad Versace | 0945512 | 2017-06-22 11:00:40 -0700 | [diff] [blame] | 1056 | int num_buffers = 0; |
Chia-I Wu | 384f228 | 2011-08-26 23:49:04 +0800 | [diff] [blame] | 1057 | |
| 1058 | /* fill dri2_surf->buffers */ |
Chad Versace | 0945512 | 2017-06-22 11:00:40 -0700 | [diff] [blame] | 1059 | for (int i = 0; i < count * 2; i += 2) { |
Chia-I Wu | 384f228 | 2011-08-26 23:49:04 +0800 | [diff] [blame] | 1060 | __DRIbuffer *buf, *local; |
| 1061 | |
| 1062 | assert(num_buffers < ARRAY_SIZE(dri2_surf->buffers)); |
| 1063 | buf = &dri2_surf->buffers[num_buffers]; |
| 1064 | |
| 1065 | switch (attachments[i]) { |
| 1066 | case __DRI_BUFFER_BACK_LEFT: |
Chia-I Wu | c8e18f8 | 2011-08-26 22:09:56 +0800 | [diff] [blame] | 1067 | if (dri2_surf->base.Type == EGL_WINDOW_BIT) { |
| 1068 | buf->attachment = attachments[i]; |
| 1069 | buf->name = get_native_buffer_name(dri2_surf->buffer); |
| 1070 | buf->cpp = get_format_bpp(dri2_surf->buffer->format); |
| 1071 | buf->pitch = dri2_surf->buffer->stride * buf->cpp; |
| 1072 | buf->flags = 0; |
Chia-I Wu | 384f228 | 2011-08-26 23:49:04 +0800 | [diff] [blame] | 1073 | |
Chia-I Wu | c8e18f8 | 2011-08-26 22:09:56 +0800 | [diff] [blame] | 1074 | if (buf->name) |
| 1075 | num_buffers++; |
Chia-I Wu | 93d5963 | 2011-08-27 00:00:18 +0800 | [diff] [blame] | 1076 | |
| 1077 | break; |
Chia-I Wu | c8e18f8 | 2011-08-26 22:09:56 +0800 | [diff] [blame] | 1078 | } |
Chia-I Wu | 93d5963 | 2011-08-27 00:00:18 +0800 | [diff] [blame] | 1079 | /* fall through for pbuffers */ |
Chia-I Wu | 384f228 | 2011-08-26 23:49:04 +0800 | [diff] [blame] | 1080 | case __DRI_BUFFER_DEPTH: |
| 1081 | case __DRI_BUFFER_STENCIL: |
| 1082 | case __DRI_BUFFER_ACCUM: |
| 1083 | case __DRI_BUFFER_DEPTH_STENCIL: |
| 1084 | case __DRI_BUFFER_HIZ: |
Gwan-gyeong Mun | 640b6e6 | 2017-08-05 00:16:11 +0900 | [diff] [blame] | 1085 | local = dri2_egl_surface_alloc_local_buffer(dri2_surf, |
Chia-I Wu | 384f228 | 2011-08-26 23:49:04 +0800 | [diff] [blame] | 1086 | attachments[i], attachments[i + 1]); |
| 1087 | |
| 1088 | if (local) { |
| 1089 | *buf = *local; |
| 1090 | num_buffers++; |
| 1091 | } |
| 1092 | break; |
| 1093 | case __DRI_BUFFER_FRONT_LEFT: |
| 1094 | case __DRI_BUFFER_FRONT_RIGHT: |
| 1095 | case __DRI_BUFFER_FAKE_FRONT_LEFT: |
| 1096 | case __DRI_BUFFER_FAKE_FRONT_RIGHT: |
| 1097 | case __DRI_BUFFER_BACK_RIGHT: |
| 1098 | default: |
| 1099 | /* no front or right buffers */ |
| 1100 | break; |
| 1101 | } |
| 1102 | } |
| 1103 | |
| 1104 | return num_buffers; |
| 1105 | } |
| 1106 | |
Chia-I Wu | 9779f6f | 2011-08-05 14:39:18 +0900 | [diff] [blame] | 1107 | static __DRIbuffer * |
| 1108 | droid_get_buffers_with_format(__DRIdrawable * driDrawable, |
| 1109 | int *width, int *height, |
| 1110 | unsigned int *attachments, int count, |
| 1111 | int *out_count, void *loaderPrivate) |
| 1112 | { |
| 1113 | struct dri2_egl_surface *dri2_surf = loaderPrivate; |
Chia-I Wu | 9779f6f | 2011-08-05 14:39:18 +0900 | [diff] [blame] | 1114 | |
Rob Herring | 81a6fff | 2016-04-28 15:37:30 -0500 | [diff] [blame] | 1115 | if (update_buffers(dri2_surf) < 0) |
| 1116 | return NULL; |
Chia-I Wu | 9779f6f | 2011-08-05 14:39:18 +0900 | [diff] [blame] | 1117 | |
Gwan-gyeong Mun | 3f6cc93 | 2017-07-18 17:12:26 +0900 | [diff] [blame] | 1118 | *out_count = droid_get_buffers_parse_attachments(dri2_surf, attachments, count); |
Chia-I Wu | 9779f6f | 2011-08-05 14:39:18 +0900 | [diff] [blame] | 1119 | |
| 1120 | if (width) |
| 1121 | *width = dri2_surf->base.Width; |
| 1122 | if (height) |
| 1123 | *height = dri2_surf->base.Height; |
| 1124 | |
Chia-I Wu | 9779f6f | 2011-08-05 14:39:18 +0900 | [diff] [blame] | 1125 | return dri2_surf->buffers; |
| 1126 | } |
Rob Herring | 3f7bca4 | 2018-04-26 16:02:01 +0200 | [diff] [blame] | 1127 | #endif /* HAVE_DRM_GRALLOC */ |
Chia-I Wu | 9779f6f | 2011-08-05 14:39:18 +0900 | [diff] [blame] | 1128 | |
Marek Olšák | 1bf703e | 2017-07-28 17:30:34 +0200 | [diff] [blame] | 1129 | static unsigned |
| 1130 | droid_get_capability(void *loaderPrivate, enum dri_loader_cap cap) |
| 1131 | { |
| 1132 | /* Note: loaderPrivate is _EGLDisplay* */ |
| 1133 | switch (cap) { |
| 1134 | case DRI_LOADER_CAP_RGBA_ORDERING: |
| 1135 | return 1; |
| 1136 | default: |
| 1137 | return 0; |
| 1138 | } |
| 1139 | } |
| 1140 | |
Chia-I Wu | 9779f6f | 2011-08-05 14:39:18 +0900 | [diff] [blame] | 1141 | static EGLBoolean |
Eric Engestrom | 54fa5ec | 2019-02-02 11:38:45 +0000 | [diff] [blame] | 1142 | droid_add_configs_for_visuals(_EGLDriver *drv, _EGLDisplay *disp) |
Chia-I Wu | 9779f6f | 2011-08-05 14:39:18 +0900 | [diff] [blame] | 1143 | { |
Eric Engestrom | 54fa5ec | 2019-02-02 11:38:45 +0000 | [diff] [blame] | 1144 | struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp); |
Emil Velikov | 4472b6e | 2016-08-25 12:08:07 +0100 | [diff] [blame] | 1145 | static const struct { |
Chia-I Wu | 9779f6f | 2011-08-05 14:39:18 +0900 | [diff] [blame] | 1146 | int format; |
Chia-I Wu | 9779f6f | 2011-08-05 14:39:18 +0900 | [diff] [blame] | 1147 | unsigned int rgba_masks[4]; |
| 1148 | } visuals[] = { |
Chad Versace | 0bcdceb | 2017-05-23 15:50:13 -0700 | [diff] [blame] | 1149 | { HAL_PIXEL_FORMAT_RGBA_8888, { 0x000000ff, 0x0000ff00, 0x00ff0000, 0xff000000 } }, |
| 1150 | { HAL_PIXEL_FORMAT_RGBX_8888, { 0x000000ff, 0x0000ff00, 0x00ff0000, 0x00000000 } }, |
| 1151 | { HAL_PIXEL_FORMAT_RGB_565, { 0x0000f800, 0x000007e0, 0x0000001f, 0x00000000 } }, |
| 1152 | { HAL_PIXEL_FORMAT_BGRA_8888, { 0x00ff0000, 0x0000ff00, 0x000000ff, 0xff000000 } }, |
Chia-I Wu | 9779f6f | 2011-08-05 14:39:18 +0900 | [diff] [blame] | 1153 | }; |
Chad Versace | 0945512 | 2017-06-22 11:00:40 -0700 | [diff] [blame] | 1154 | |
Emil Velikov | acd35c8 | 2016-08-25 14:28:48 +0100 | [diff] [blame] | 1155 | unsigned int format_count[ARRAY_SIZE(visuals)] = { 0 }; |
Chad Versace | ffbf50b | 2017-06-22 11:00:41 -0700 | [diff] [blame] | 1156 | int config_count = 0; |
Chia-I Wu | 9779f6f | 2011-08-05 14:39:18 +0900 | [diff] [blame] | 1157 | |
Chad Versace | 5e88435 | 2017-06-16 19:11:21 -0700 | [diff] [blame] | 1158 | /* The nesting of loops is significant here. Also significant is the order |
| 1159 | * of the HAL pixel formats. Many Android apps (such as Google's official |
| 1160 | * NDK GLES2 example app), and even portions the core framework code (such |
| 1161 | * as SystemServiceManager in Nougat), incorrectly choose their EGLConfig. |
| 1162 | * They neglect to match the EGLConfig's EGL_NATIVE_VISUAL_ID against the |
| 1163 | * window's native format, and instead choose the first EGLConfig whose |
| 1164 | * channel sizes match those of the native window format while ignoring the |
| 1165 | * channel *ordering*. |
| 1166 | * |
| 1167 | * We can detect such buggy clients in logcat when they call |
| 1168 | * eglCreateSurface, by detecting the mismatch between the EGLConfig's |
| 1169 | * format and the window's format. |
| 1170 | * |
| 1171 | * As a workaround, we generate EGLConfigs such that all EGLConfigs for HAL |
| 1172 | * pixel format i precede those for HAL pixel format i+1. In my |
| 1173 | * (chadversary) testing on Android Nougat, this was good enough to pacify |
| 1174 | * the buggy clients. |
| 1175 | */ |
Chad Versace | 0945512 | 2017-06-22 11:00:40 -0700 | [diff] [blame] | 1176 | for (int i = 0; i < ARRAY_SIZE(visuals); i++) { |
| 1177 | for (int j = 0; dri2_dpy->driver_configs[j]; j++) { |
| 1178 | const EGLint surface_type = EGL_WINDOW_BIT | EGL_PBUFFER_BIT; |
Tapani Pälli | d58ca43 | 2012-08-13 16:47:52 +0300 | [diff] [blame] | 1179 | |
Chad Versace | bd78909 | 2017-06-22 11:00:41 -0700 | [diff] [blame] | 1180 | const EGLint config_attrs[] = { |
| 1181 | EGL_NATIVE_VISUAL_ID, visuals[i].format, |
| 1182 | EGL_NATIVE_VISUAL_TYPE, visuals[i].format, |
| 1183 | EGL_FRAMEBUFFER_TARGET_ANDROID, EGL_TRUE, |
| 1184 | EGL_RECORDABLE_ANDROID, EGL_TRUE, |
| 1185 | EGL_NONE |
| 1186 | }; |
Chia-I Wu | 9779f6f | 2011-08-05 14:39:18 +0900 | [diff] [blame] | 1187 | |
Chad Versace | 0945512 | 2017-06-22 11:00:40 -0700 | [diff] [blame] | 1188 | struct dri2_egl_config *dri2_conf = |
Eric Engestrom | 54fa5ec | 2019-02-02 11:38:45 +0000 | [diff] [blame] | 1189 | dri2_add_config(disp, dri2_dpy->driver_configs[j], |
Chad Versace | ffbf50b | 2017-06-22 11:00:41 -0700 | [diff] [blame] | 1190 | config_count + 1, surface_type, config_attrs, |
Chad Versace | 0945512 | 2017-06-22 11:00:40 -0700 | [diff] [blame] | 1191 | visuals[i].rgba_masks); |
Chia-I Wu | 9779f6f | 2011-08-05 14:39:18 +0900 | [diff] [blame] | 1192 | if (dri2_conf) { |
Chad Versace | ffbf50b | 2017-06-22 11:00:41 -0700 | [diff] [blame] | 1193 | if (dri2_conf->base.ConfigID == config_count + 1) |
| 1194 | config_count++; |
Chad Versace | 5e88435 | 2017-06-16 19:11:21 -0700 | [diff] [blame] | 1195 | format_count[i]++; |
Chia-I Wu | 9779f6f | 2011-08-05 14:39:18 +0900 | [diff] [blame] | 1196 | } |
| 1197 | } |
Emil Velikov | acd35c8 | 2016-08-25 14:28:48 +0100 | [diff] [blame] | 1198 | } |
Chia-I Wu | 9779f6f | 2011-08-05 14:39:18 +0900 | [diff] [blame] | 1199 | |
Chad Versace | 0945512 | 2017-06-22 11:00:40 -0700 | [diff] [blame] | 1200 | for (int i = 0; i < ARRAY_SIZE(format_count); i++) { |
Emil Velikov | acd35c8 | 2016-08-25 14:28:48 +0100 | [diff] [blame] | 1201 | if (!format_count[i]) { |
Chia-I Wu | 9779f6f | 2011-08-05 14:39:18 +0900 | [diff] [blame] | 1202 | _eglLog(_EGL_DEBUG, "No DRI config supports native format 0x%x", |
Emil Velikov | acd35c8 | 2016-08-25 14:28:48 +0100 | [diff] [blame] | 1203 | visuals[i].format); |
Chia-I Wu | 9779f6f | 2011-08-05 14:39:18 +0900 | [diff] [blame] | 1204 | } |
| 1205 | } |
| 1206 | |
Chad Versace | ffbf50b | 2017-06-22 11:00:41 -0700 | [diff] [blame] | 1207 | return (config_count != 0); |
Chia-I Wu | 9779f6f | 2011-08-05 14:39:18 +0900 | [diff] [blame] | 1208 | } |
| 1209 | |
Emil Velikov | 79d1fb9 | 2017-05-15 16:14:17 +0100 | [diff] [blame] | 1210 | static const struct dri2_egl_display_vtbl droid_display_vtbl = { |
Chad Versace | 90502b1 | 2014-01-28 11:41:46 -0800 | [diff] [blame] | 1211 | .authenticate = NULL, |
Chad Versace | 0a0c881 | 2014-01-28 16:39:09 -0800 | [diff] [blame] | 1212 | .create_window_surface = droid_create_window_surface, |
Adrian Negreanu | 6980cae | 2014-06-06 12:16:10 +0300 | [diff] [blame] | 1213 | .create_pixmap_surface = dri2_fallback_create_pixmap_surface, |
Chad Versace | bf20076 | 2014-01-28 17:03:03 -0800 | [diff] [blame] | 1214 | .create_pbuffer_surface = droid_create_pbuffer_surface, |
Chad Versace | 958dd80 | 2014-01-28 17:03:03 -0800 | [diff] [blame] | 1215 | .destroy_surface = droid_destroy_surface, |
Chad Versace | eef68a9 | 2014-01-28 17:03:03 -0800 | [diff] [blame] | 1216 | .create_image = droid_create_image_khr, |
Chad Versace | ad173bc | 2014-01-28 16:21:21 -0800 | [diff] [blame] | 1217 | .swap_buffers = droid_swap_buffers, |
Emil Velikov | 61e99ce | 2017-10-16 17:10:42 +0100 | [diff] [blame] | 1218 | .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] | 1219 | .swap_buffers_region = dri2_fallback_swap_buffers_region, |
Wu, Zhongmin | 5fc21c6 | 2018-01-18 15:39:22 +0800 | [diff] [blame] | 1220 | .swap_interval = droid_swap_interval, |
Harish Krupo | 9827547 | 2017-06-09 20:13:34 +0530 | [diff] [blame] | 1221 | #if ANDROID_API_LEVEL >= 23 |
| 1222 | .set_damage_region = droid_set_damage_region, |
| 1223 | #else |
| 1224 | .set_damage_region = dri2_fallback_set_damage_region, |
| 1225 | #endif |
Chad Versace | 688a0e8 | 2014-01-28 17:03:03 -0800 | [diff] [blame] | 1226 | .post_sub_buffer = dri2_fallback_post_sub_buffer, |
Chad Versace | bc2cbc0 | 2014-01-28 17:03:03 -0800 | [diff] [blame] | 1227 | .copy_buffers = dri2_fallback_copy_buffers, |
Xiaosong Wei | 2acc69d | 2017-02-08 10:46:02 +0800 | [diff] [blame] | 1228 | .query_buffer_age = droid_query_buffer_age, |
Haixia Shi | a7c6993 | 2016-07-28 10:51:12 -0700 | [diff] [blame] | 1229 | .query_surface = droid_query_surface, |
Chad Versace | eadd5e0 | 2014-01-28 17:03:03 -0800 | [diff] [blame] | 1230 | .create_wayland_buffer_from_image = dri2_fallback_create_wayland_buffer_from_image, |
Sarah Sharp | c524f3e | 2014-05-06 12:10:57 -0700 | [diff] [blame] | 1231 | .get_sync_values = dri2_fallback_get_sync_values, |
Boyan Ding | a25df54 | 2015-07-21 23:43:59 +0800 | [diff] [blame] | 1232 | .get_dri_drawable = dri2_surface_get_dri_drawable, |
Chad Versace | ed7c694 | 2018-04-30 22:35:17 -0700 | [diff] [blame] | 1233 | .set_shared_buffer_mode = droid_set_shared_buffer_mode, |
Chad Versace | 90502b1 | 2014-01-28 11:41:46 -0800 | [diff] [blame] | 1234 | }; |
| 1235 | |
Rob Herring | 3f7bca4 | 2018-04-26 16:02:01 +0200 | [diff] [blame] | 1236 | #ifdef HAVE_DRM_GRALLOC |
Emil Velikov | 6a8fe32 | 2016-08-16 17:23:23 +0100 | [diff] [blame] | 1237 | static const __DRIdri2LoaderExtension droid_dri2_loader_extension = { |
Marek Olšák | 1bf703e | 2017-07-28 17:30:34 +0200 | [diff] [blame] | 1238 | .base = { __DRI_DRI2_LOADER, 4 }, |
Emil Velikov | 6a8fe32 | 2016-08-16 17:23:23 +0100 | [diff] [blame] | 1239 | |
| 1240 | .getBuffers = NULL, |
| 1241 | .flushFrontBuffer = droid_flush_front_buffer, |
| 1242 | .getBuffersWithFormat = droid_get_buffers_with_format, |
Rob Herring | be5773f | 2017-07-31 09:32:10 -0500 | [diff] [blame] | 1243 | .getCapability = droid_get_capability, |
Emil Velikov | 6a8fe32 | 2016-08-16 17:23:23 +0100 | [diff] [blame] | 1244 | }; |
| 1245 | |
Emil Velikov | f871946 | 2016-08-24 23:32:27 +0100 | [diff] [blame] | 1246 | static const __DRIextension *droid_dri2_loader_extensions[] = { |
| 1247 | &droid_dri2_loader_extension.base, |
| 1248 | &image_lookup_extension.base, |
| 1249 | &use_invalidate.base, |
Chad Versace | ed7c694 | 2018-04-30 22:35:17 -0700 | [diff] [blame] | 1250 | /* No __DRI_MUTABLE_RENDER_BUFFER_LOADER because it requires |
| 1251 | * __DRI_IMAGE_LOADER. |
| 1252 | */ |
Emil Velikov | f871946 | 2016-08-24 23:32:27 +0100 | [diff] [blame] | 1253 | NULL, |
| 1254 | }; |
Rob Herring | 3f7bca4 | 2018-04-26 16:02:01 +0200 | [diff] [blame] | 1255 | #endif /* HAVE_DRM_GRALLOC */ |
Emil Velikov | f871946 | 2016-08-24 23:32:27 +0100 | [diff] [blame] | 1256 | |
Gurchetan Singh | 49d5253 | 2019-03-25 16:44:34 -0700 | [diff] [blame] | 1257 | static const __DRIimageLoaderExtension droid_image_loader_extension = { |
| 1258 | .base = { __DRI_IMAGE_LOADER, 2 }, |
| 1259 | |
| 1260 | .getBuffers = droid_image_get_buffers, |
| 1261 | .flushFrontBuffer = droid_flush_front_buffer, |
| 1262 | .getCapability = droid_get_capability, |
| 1263 | }; |
| 1264 | |
Chad Versace | ed7c694 | 2018-04-30 22:35:17 -0700 | [diff] [blame] | 1265 | static void |
| 1266 | droid_display_shared_buffer(__DRIdrawable *driDrawable, int fence_fd, |
| 1267 | void *loaderPrivate) |
| 1268 | { |
| 1269 | struct dri2_egl_surface *dri2_surf = loaderPrivate; |
| 1270 | struct ANativeWindowBuffer *old_buffer UNUSED = dri2_surf->buffer; |
| 1271 | |
| 1272 | if (!_eglSurfaceInSharedBufferMode(&dri2_surf->base)) { |
| 1273 | _eglLog(_EGL_WARNING, "%s: internal error: buffer is not shared", |
| 1274 | __func__); |
| 1275 | return; |
| 1276 | } |
| 1277 | |
| 1278 | if (fence_fd >= 0) { |
| 1279 | /* The driver's fence is more recent than the surface's out fence, if it |
| 1280 | * exists at all. So use the driver's fence. |
| 1281 | */ |
| 1282 | if (dri2_surf->out_fence_fd >= 0) { |
| 1283 | close(dri2_surf->out_fence_fd); |
| 1284 | dri2_surf->out_fence_fd = -1; |
| 1285 | } |
| 1286 | } else if (dri2_surf->out_fence_fd >= 0) { |
| 1287 | fence_fd = dri2_surf->out_fence_fd; |
| 1288 | dri2_surf->out_fence_fd = -1; |
| 1289 | } |
| 1290 | |
| 1291 | if (dri2_surf->window->queueBuffer(dri2_surf->window, dri2_surf->buffer, |
| 1292 | fence_fd)) { |
| 1293 | _eglLog(_EGL_WARNING, "%s: ANativeWindow::queueBuffer failed", __func__); |
| 1294 | close(fence_fd); |
| 1295 | return; |
| 1296 | } |
| 1297 | |
| 1298 | fence_fd = -1; |
| 1299 | |
| 1300 | if (dri2_surf->window->dequeueBuffer(dri2_surf->window, &dri2_surf->buffer, |
| 1301 | &fence_fd)) { |
| 1302 | /* Tear down the surface because it no longer has a back buffer. */ |
| 1303 | struct dri2_egl_display *dri2_dpy = |
| 1304 | dri2_egl_display(dri2_surf->base.Resource.Display); |
| 1305 | |
| 1306 | _eglLog(_EGL_WARNING, "%s: ANativeWindow::dequeueBuffer failed", __func__); |
| 1307 | |
| 1308 | dri2_surf->base.Lost = true; |
| 1309 | dri2_surf->buffer = NULL; |
| 1310 | dri2_surf->back = NULL; |
| 1311 | |
| 1312 | if (dri2_surf->dri_image_back) { |
| 1313 | dri2_dpy->image->destroyImage(dri2_surf->dri_image_back); |
| 1314 | dri2_surf->dri_image_back = NULL; |
| 1315 | } |
| 1316 | |
| 1317 | dri2_dpy->flush->invalidate(dri2_surf->dri_drawable); |
| 1318 | return; |
| 1319 | } |
| 1320 | |
| 1321 | if (fence_fd < 0) |
| 1322 | return; |
| 1323 | |
| 1324 | /* Access to the buffer is controlled by a sync fence. Block on it. |
| 1325 | * |
| 1326 | * Ideally, we would submit the fence to the driver, and the driver would |
| 1327 | * postpone command execution until it signalled. But DRI lacks API for |
| 1328 | * that (as of 2018-04-11). |
| 1329 | * |
| 1330 | * SYNC_IOC_WAIT waits forever if timeout < 0 |
| 1331 | */ |
| 1332 | sync_wait(fence_fd, -1); |
| 1333 | close(fence_fd); |
| 1334 | } |
| 1335 | |
| 1336 | static const __DRImutableRenderBufferLoaderExtension droid_mutable_render_buffer_extension = { |
| 1337 | .base = { __DRI_MUTABLE_RENDER_BUFFER_LOADER, 1 }, |
| 1338 | .displaySharedBuffer = droid_display_shared_buffer, |
| 1339 | }; |
| 1340 | |
Emil Velikov | f871946 | 2016-08-24 23:32:27 +0100 | [diff] [blame] | 1341 | static const __DRIextension *droid_image_loader_extensions[] = { |
| 1342 | &droid_image_loader_extension.base, |
| 1343 | &image_lookup_extension.base, |
| 1344 | &use_invalidate.base, |
Chad Versace | ed7c694 | 2018-04-30 22:35:17 -0700 | [diff] [blame] | 1345 | &droid_mutable_render_buffer_extension.base, |
Emil Velikov | f871946 | 2016-08-24 23:32:27 +0100 | [diff] [blame] | 1346 | NULL, |
| 1347 | }; |
| 1348 | |
Kristian H. Kristensen | aaafae4 | 2018-09-12 11:38:22 -0700 | [diff] [blame] | 1349 | static EGLBoolean |
Gurchetan Singh | f90fc10 | 2019-03-13 10:43:35 -0700 | [diff] [blame] | 1350 | droid_load_driver(_EGLDisplay *disp, bool swrast) |
Robert Foss | c7bb821 | 2018-04-18 17:27:40 +0200 | [diff] [blame] | 1351 | { |
| 1352 | struct dri2_egl_display *dri2_dpy = disp->DriverData; |
| 1353 | const char *err; |
| 1354 | |
| 1355 | dri2_dpy->driver_name = loader_get_driver_for_fd(dri2_dpy->fd); |
| 1356 | if (dri2_dpy->driver_name == NULL) |
| 1357 | return false; |
| 1358 | |
Mauro Rossi | ac0856a | 2018-08-15 14:46:25 +0200 | [diff] [blame] | 1359 | #ifdef HAVE_DRM_GRALLOC |
Gurchetan Singh | 7d9719d | 2019-03-26 17:13:01 -0700 | [diff] [blame] | 1360 | /* Handle control nodes using __DRI_DRI2_LOADER extension and GEM names |
| 1361 | * for backwards compatibility with drm_gralloc. (Do not use on new |
| 1362 | * systems.) */ |
| 1363 | dri2_dpy->loader_extensions = droid_dri2_loader_extensions; |
| 1364 | if (!dri2_load_driver(disp)) { |
| 1365 | err = "DRI2: failed to load driver"; |
| 1366 | goto error; |
| 1367 | } |
Mauro Rossi | ac0856a | 2018-08-15 14:46:25 +0200 | [diff] [blame] | 1368 | #else |
Gurchetan Singh | d4e7982 | 2019-03-13 10:49:20 -0700 | [diff] [blame] | 1369 | if (swrast) { |
| 1370 | /* Use kms swrast only with vgem / virtio_gpu. |
| 1371 | * virtio-gpu fallbacks to software rendering when 3D features |
| 1372 | * are unavailable since 6c5ab. |
| 1373 | */ |
| 1374 | if (strcmp(dri2_dpy->driver_name, "vgem") == 0 || |
| 1375 | strcmp(dri2_dpy->driver_name, "virtio_gpu") == 0) { |
| 1376 | free(dri2_dpy->driver_name); |
| 1377 | dri2_dpy->driver_name = strdup("kms_swrast"); |
| 1378 | } else { |
| 1379 | err = "DRI3: failed to find software capable driver"; |
| 1380 | goto error; |
| 1381 | } |
| 1382 | } |
| 1383 | |
Gurchetan Singh | 7d9719d | 2019-03-26 17:13:01 -0700 | [diff] [blame] | 1384 | dri2_dpy->loader_extensions = droid_image_loader_extensions; |
| 1385 | if (!dri2_load_driver_dri3(disp)) { |
| 1386 | err = "DRI3: failed to load driver"; |
| 1387 | goto error; |
| 1388 | } |
Mauro Rossi | ac0856a | 2018-08-15 14:46:25 +0200 | [diff] [blame] | 1389 | #endif |
Robert Foss | c7bb821 | 2018-04-18 17:27:40 +0200 | [diff] [blame] | 1390 | |
| 1391 | return true; |
| 1392 | |
| 1393 | error: |
| 1394 | free(dri2_dpy->driver_name); |
| 1395 | dri2_dpy->driver_name = NULL; |
| 1396 | return false; |
| 1397 | } |
| 1398 | |
Emil Velikov | b8b3517 | 2018-09-03 13:37:47 +0100 | [diff] [blame] | 1399 | static void |
| 1400 | droid_unload_driver(_EGLDisplay *disp) |
| 1401 | { |
| 1402 | struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp); |
| 1403 | |
| 1404 | dlclose(dri2_dpy->driver); |
| 1405 | dri2_dpy->driver = NULL; |
| 1406 | free(dri2_dpy->driver_name); |
| 1407 | dri2_dpy->driver_name = NULL; |
| 1408 | } |
| 1409 | |
Emil Velikov | 2c04938 | 2018-08-08 15:40:56 +0100 | [diff] [blame] | 1410 | static int |
| 1411 | droid_filter_device(_EGLDisplay *disp, int fd, const char *vendor) |
Robert Foss | c7bb821 | 2018-04-18 17:27:40 +0200 | [diff] [blame] | 1412 | { |
Robert Foss | c7bb821 | 2018-04-18 17:27:40 +0200 | [diff] [blame] | 1413 | drmVersionPtr ver = drmGetVersion(fd); |
| 1414 | if (!ver) |
Emil Velikov | 2c04938 | 2018-08-08 15:40:56 +0100 | [diff] [blame] | 1415 | return -1; |
Robert Foss | c7bb821 | 2018-04-18 17:27:40 +0200 | [diff] [blame] | 1416 | |
Emil Velikov | 2c04938 | 2018-08-08 15:40:56 +0100 | [diff] [blame] | 1417 | if (strcmp(vendor, ver->name) != 0) { |
| 1418 | drmFreeVersion(ver); |
| 1419 | return -1; |
Robert Foss | c7bb821 | 2018-04-18 17:27:40 +0200 | [diff] [blame] | 1420 | } |
| 1421 | |
Robert Foss | c7bb821 | 2018-04-18 17:27:40 +0200 | [diff] [blame] | 1422 | drmFreeVersion(ver); |
Emil Velikov | 2c04938 | 2018-08-08 15:40:56 +0100 | [diff] [blame] | 1423 | return 0; |
Robert Foss | c7bb821 | 2018-04-18 17:27:40 +0200 | [diff] [blame] | 1424 | } |
| 1425 | |
Emil Velikov | b8b3517 | 2018-09-03 13:37:47 +0100 | [diff] [blame] | 1426 | static EGLBoolean |
Gurchetan Singh | f90fc10 | 2019-03-13 10:43:35 -0700 | [diff] [blame] | 1427 | droid_probe_device(_EGLDisplay *disp, bool swrast) |
Emil Velikov | b8b3517 | 2018-09-03 13:37:47 +0100 | [diff] [blame] | 1428 | { |
| 1429 | /* Check that the device is supported, by attempting to: |
| 1430 | * - load the dri module |
| 1431 | * - and, create a screen |
| 1432 | */ |
Gurchetan Singh | f90fc10 | 2019-03-13 10:43:35 -0700 | [diff] [blame] | 1433 | if (!droid_load_driver(disp, swrast)) |
Emil Velikov | b8b3517 | 2018-09-03 13:37:47 +0100 | [diff] [blame] | 1434 | return EGL_FALSE; |
| 1435 | |
| 1436 | if (!dri2_create_screen(disp)) { |
| 1437 | _eglLog(_EGL_WARNING, "DRI2: failed to create screen"); |
| 1438 | droid_unload_driver(disp); |
| 1439 | return EGL_FALSE; |
| 1440 | } |
| 1441 | return EGL_TRUE; |
| 1442 | } |
| 1443 | |
Gurchetan Singh | 95ad174 | 2019-03-25 16:46:31 -0700 | [diff] [blame] | 1444 | #ifdef HAVE_DRM_GRALLOC |
| 1445 | static EGLBoolean |
Gurchetan Singh | f90fc10 | 2019-03-13 10:43:35 -0700 | [diff] [blame] | 1446 | droid_open_device(_EGLDisplay *disp, bool swrast) |
Gurchetan Singh | 95ad174 | 2019-03-25 16:46:31 -0700 | [diff] [blame] | 1447 | { |
| 1448 | struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp); |
| 1449 | int fd = -1, err = -EINVAL; |
| 1450 | |
Gurchetan Singh | d4e7982 | 2019-03-13 10:49:20 -0700 | [diff] [blame] | 1451 | if (swrast) |
| 1452 | return EGL_FALSE; |
| 1453 | |
Gurchetan Singh | 95ad174 | 2019-03-25 16:46:31 -0700 | [diff] [blame] | 1454 | if (dri2_dpy->gralloc->perform) |
Gurchetan Singh | 7d9719d | 2019-03-26 17:13:01 -0700 | [diff] [blame] | 1455 | err = dri2_dpy->gralloc->perform(dri2_dpy->gralloc, |
| 1456 | GRALLOC_MODULE_PERFORM_GET_DRM_FD, |
| 1457 | &fd); |
Gurchetan Singh | 95ad174 | 2019-03-25 16:46:31 -0700 | [diff] [blame] | 1458 | if (err || fd < 0) { |
| 1459 | _eglLog(_EGL_WARNING, "fail to get drm fd"); |
| 1460 | return EGL_FALSE; |
| 1461 | } |
| 1462 | |
| 1463 | dri2_dpy->fd = fcntl(fd, F_DUPFD_CLOEXEC, 3); |
| 1464 | if (dri2_dpy->fd < 0) |
Gurchetan Singh | 7d9719d | 2019-03-26 17:13:01 -0700 | [diff] [blame] | 1465 | return EGL_FALSE; |
| 1466 | |
| 1467 | if (drmGetNodeTypeFromFd(dri2_dpy->fd) == DRM_NODE_RENDER) |
| 1468 | return EGL_FALSE; |
Gurchetan Singh | 95ad174 | 2019-03-25 16:46:31 -0700 | [diff] [blame] | 1469 | |
Gurchetan Singh | f90fc10 | 2019-03-13 10:43:35 -0700 | [diff] [blame] | 1470 | return droid_probe_device(disp, swrast); |
Gurchetan Singh | 95ad174 | 2019-03-25 16:46:31 -0700 | [diff] [blame] | 1471 | } |
Gurchetan Singh | f1dd1be | 2019-03-25 16:50:46 -0700 | [diff] [blame] | 1472 | #else |
Emil Velikov | b8b3517 | 2018-09-03 13:37:47 +0100 | [diff] [blame] | 1473 | static EGLBoolean |
Gurchetan Singh | f90fc10 | 2019-03-13 10:43:35 -0700 | [diff] [blame] | 1474 | droid_open_device(_EGLDisplay *disp, bool swrast) |
Robert Foss | c7bb821 | 2018-04-18 17:27:40 +0200 | [diff] [blame] | 1475 | { |
Emil Velikov | d9d1cb4 | 2019-02-19 15:30:41 +0000 | [diff] [blame] | 1476 | #define MAX_DRM_DEVICES 64 |
Emil Velikov | b8b3517 | 2018-09-03 13:37:47 +0100 | [diff] [blame] | 1477 | struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp); |
Emil Velikov | 3827966 | 2018-08-08 14:19:05 +0100 | [diff] [blame] | 1478 | drmDevicePtr device, devices[MAX_DRM_DEVICES] = { NULL }; |
Emil Velikov | b8b3517 | 2018-09-03 13:37:47 +0100 | [diff] [blame] | 1479 | int num_devices; |
Robert Foss | c7bb821 | 2018-04-18 17:27:40 +0200 | [diff] [blame] | 1480 | |
| 1481 | char *vendor_name = NULL; |
| 1482 | char vendor_buf[PROPERTY_VALUE_MAX]; |
| 1483 | |
Gurchetan Singh | ac839bb | 2019-03-26 19:20:13 -0700 | [diff] [blame] | 1484 | #ifdef EGL_FORCE_RENDERNODE |
| 1485 | const unsigned node_type = DRM_NODE_RENDER; |
| 1486 | #else |
| 1487 | const unsigned node_type = swrast ? DRM_NODE_PRIMARY : DRM_NODE_RENDER; |
| 1488 | #endif |
| 1489 | |
Robert Foss | c7bb821 | 2018-04-18 17:27:40 +0200 | [diff] [blame] | 1490 | if (property_get("drm.gpu.vendor_name", vendor_buf, NULL) > 0) |
| 1491 | vendor_name = vendor_buf; |
| 1492 | |
Emil Velikov | 3827966 | 2018-08-08 14:19:05 +0100 | [diff] [blame] | 1493 | num_devices = drmGetDevices2(0, devices, ARRAY_SIZE(devices)); |
| 1494 | if (num_devices < 0) |
Emil Velikov | b8b3517 | 2018-09-03 13:37:47 +0100 | [diff] [blame] | 1495 | return EGL_FALSE; |
Robert Foss | c7bb821 | 2018-04-18 17:27:40 +0200 | [diff] [blame] | 1496 | |
Emil Velikov | 3827966 | 2018-08-08 14:19:05 +0100 | [diff] [blame] | 1497 | for (int i = 0; i < num_devices; i++) { |
| 1498 | device = devices[i]; |
Robert Foss | c7bb821 | 2018-04-18 17:27:40 +0200 | [diff] [blame] | 1499 | |
Gurchetan Singh | ac839bb | 2019-03-26 19:20:13 -0700 | [diff] [blame] | 1500 | if (!(device->available_nodes & (1 << node_type))) |
Robert Foss | c7bb821 | 2018-04-18 17:27:40 +0200 | [diff] [blame] | 1501 | continue; |
| 1502 | |
Gurchetan Singh | ac839bb | 2019-03-26 19:20:13 -0700 | [diff] [blame] | 1503 | dri2_dpy->fd = loader_open_device(device->nodes[node_type]); |
Emil Velikov | b8b3517 | 2018-09-03 13:37:47 +0100 | [diff] [blame] | 1504 | if (dri2_dpy->fd < 0) { |
Robert Foss | c7bb821 | 2018-04-18 17:27:40 +0200 | [diff] [blame] | 1505 | _eglLog(_EGL_WARNING, "%s() Failed to open DRM device %s", |
Gurchetan Singh | ac839bb | 2019-03-26 19:20:13 -0700 | [diff] [blame] | 1506 | __func__, device->nodes[node_type]); |
Robert Foss | c7bb821 | 2018-04-18 17:27:40 +0200 | [diff] [blame] | 1507 | continue; |
| 1508 | } |
| 1509 | |
Emil Velikov | b8b3517 | 2018-09-03 13:37:47 +0100 | [diff] [blame] | 1510 | /* If a vendor is explicitly provided, we use only that. |
| 1511 | * Otherwise we fall-back the first device that is supported. |
| 1512 | */ |
| 1513 | if (vendor_name) { |
| 1514 | if (droid_filter_device(disp, dri2_dpy->fd, vendor_name)) { |
| 1515 | /* Device does not match - try next device */ |
| 1516 | close(dri2_dpy->fd); |
| 1517 | dri2_dpy->fd = -1; |
| 1518 | continue; |
| 1519 | } |
| 1520 | /* If the requested device matches - use it. Regardless if |
| 1521 | * init fails, do not fall-back to any other device. |
| 1522 | */ |
Gurchetan Singh | f90fc10 | 2019-03-13 10:43:35 -0700 | [diff] [blame] | 1523 | if (!droid_probe_device(disp, false)) { |
Emil Velikov | b8b3517 | 2018-09-03 13:37:47 +0100 | [diff] [blame] | 1524 | close(dri2_dpy->fd); |
| 1525 | dri2_dpy->fd = -1; |
Emil Velikov | 2c04938 | 2018-08-08 15:40:56 +0100 | [diff] [blame] | 1526 | } |
| 1527 | |
Emil Velikov | b8b3517 | 2018-09-03 13:37:47 +0100 | [diff] [blame] | 1528 | break; |
Robert Foss | c7bb821 | 2018-04-18 17:27:40 +0200 | [diff] [blame] | 1529 | } |
Gurchetan Singh | f90fc10 | 2019-03-13 10:43:35 -0700 | [diff] [blame] | 1530 | if (droid_probe_device(disp, swrast)) |
Emil Velikov | b8b3517 | 2018-09-03 13:37:47 +0100 | [diff] [blame] | 1531 | break; |
| 1532 | |
| 1533 | /* No explicit request - attempt the next device */ |
| 1534 | close(dri2_dpy->fd); |
| 1535 | dri2_dpy->fd = -1; |
Robert Foss | c7bb821 | 2018-04-18 17:27:40 +0200 | [diff] [blame] | 1536 | } |
Emil Velikov | 3827966 | 2018-08-08 14:19:05 +0100 | [diff] [blame] | 1537 | drmFreeDevices(devices, num_devices); |
Robert Foss | c7bb821 | 2018-04-18 17:27:40 +0200 | [diff] [blame] | 1538 | |
Emil Velikov | b8b3517 | 2018-09-03 13:37:47 +0100 | [diff] [blame] | 1539 | if (dri2_dpy->fd < 0) { |
| 1540 | _eglLog(_EGL_WARNING, "Failed to open %s DRM device", |
| 1541 | vendor_name ? "desired": "any"); |
| 1542 | return EGL_FALSE; |
Robert Foss | c7bb821 | 2018-04-18 17:27:40 +0200 | [diff] [blame] | 1543 | } |
| 1544 | |
Emil Velikov | b8b3517 | 2018-09-03 13:37:47 +0100 | [diff] [blame] | 1545 | return EGL_TRUE; |
Emil Velikov | 3827966 | 2018-08-08 14:19:05 +0100 | [diff] [blame] | 1546 | #undef MAX_DRM_DEVICES |
Robert Foss | c7bb821 | 2018-04-18 17:27:40 +0200 | [diff] [blame] | 1547 | } |
| 1548 | |
Gurchetan Singh | f1dd1be | 2019-03-25 16:50:46 -0700 | [diff] [blame] | 1549 | #endif |
| 1550 | |
Robert Foss | c7bb821 | 2018-04-18 17:27:40 +0200 | [diff] [blame] | 1551 | EGLBoolean |
Rob Herring | aa187fe | 2018-01-03 10:16:23 -0600 | [diff] [blame] | 1552 | dri2_initialize_android(_EGLDriver *drv, _EGLDisplay *disp) |
Chia-I Wu | 9779f6f | 2011-08-05 14:39:18 +0900 | [diff] [blame] | 1553 | { |
Emil Velikov | 0099270 | 2018-09-04 11:20:03 +0100 | [diff] [blame] | 1554 | _EGLDevice *dev; |
Gurchetan Singh | a87096b | 2019-03-13 10:59:59 -0700 | [diff] [blame] | 1555 | bool device_opened = false; |
Chia-I Wu | 9779f6f | 2011-08-05 14:39:18 +0900 | [diff] [blame] | 1556 | struct dri2_egl_display *dri2_dpy; |
| 1557 | const char *err; |
Tomasz Figa | 859d0b0 | 2016-11-10 16:55:52 +0900 | [diff] [blame] | 1558 | int ret; |
Chia-I Wu | 9779f6f | 2011-08-05 14:39:18 +0900 | [diff] [blame] | 1559 | |
Chia-I Wu | 9779f6f | 2011-08-05 14:39:18 +0900 | [diff] [blame] | 1560 | dri2_dpy = calloc(1, sizeof(*dri2_dpy)); |
| 1561 | if (!dri2_dpy) |
| 1562 | return _eglError(EGL_BAD_ALLOC, "eglInitialize"); |
| 1563 | |
Emil Velikov | 898d785 | 2017-05-11 17:18:13 +0100 | [diff] [blame] | 1564 | dri2_dpy->fd = -1; |
Tomasz Figa | 859d0b0 | 2016-11-10 16:55:52 +0900 | [diff] [blame] | 1565 | ret = hw_get_module(GRALLOC_HARDWARE_MODULE_ID, |
| 1566 | (const hw_module_t **)&dri2_dpy->gralloc); |
| 1567 | if (ret) { |
| 1568 | err = "DRI2: failed to get gralloc module"; |
Emil Velikov | 898d785 | 2017-05-11 17:18:13 +0100 | [diff] [blame] | 1569 | goto cleanup; |
Tomasz Figa | 859d0b0 | 2016-11-10 16:55:52 +0900 | [diff] [blame] | 1570 | } |
| 1571 | |
Rob Herring | aa187fe | 2018-01-03 10:16:23 -0600 | [diff] [blame] | 1572 | disp->DriverData = (void *) dri2_dpy; |
Gurchetan Singh | a87096b | 2019-03-13 10:59:59 -0700 | [diff] [blame] | 1573 | if (!disp->Options.ForceSoftware) |
| 1574 | device_opened = droid_open_device(disp, false); |
| 1575 | if (!device_opened) |
| 1576 | device_opened = droid_open_device(disp, true); |
Chia-I Wu | 9779f6f | 2011-08-05 14:39:18 +0900 | [diff] [blame] | 1577 | |
Gurchetan Singh | a87096b | 2019-03-13 10:59:59 -0700 | [diff] [blame] | 1578 | if (!device_opened) { |
Chia-I Wu | 9779f6f | 2011-08-05 14:39:18 +0900 | [diff] [blame] | 1579 | err = "DRI2: failed to open device"; |
Emil Velikov | 898d785 | 2017-05-11 17:18:13 +0100 | [diff] [blame] | 1580 | goto cleanup; |
Chia-I Wu | 9779f6f | 2011-08-05 14:39:18 +0900 | [diff] [blame] | 1581 | } |
| 1582 | |
Emil Velikov | 0099270 | 2018-09-04 11:20:03 +0100 | [diff] [blame] | 1583 | dev = _eglAddDevice(dri2_dpy->fd, false); |
| 1584 | if (!dev) { |
| 1585 | err = "DRI2: failed to find EGLDevice"; |
| 1586 | goto cleanup; |
| 1587 | } |
| 1588 | |
| 1589 | disp->Device = dev; |
| 1590 | |
Rob Herring | aa187fe | 2018-01-03 10:16:23 -0600 | [diff] [blame] | 1591 | if (!dri2_setup_extensions(disp)) { |
Tomasz Figa | 0ede0f9 | 2017-07-05 23:56:51 +0900 | [diff] [blame] | 1592 | err = "DRI2: failed to setup extensions"; |
Emil Velikov | 2c341f2 | 2017-05-11 16:20:04 +0100 | [diff] [blame] | 1593 | goto cleanup; |
Tomasz Figa | 0ede0f9 | 2017-07-05 23:56:51 +0900 | [diff] [blame] | 1594 | } |
Emil Velikov | 2c341f2 | 2017-05-11 16:20:04 +0100 | [diff] [blame] | 1595 | |
Rob Herring | aa187fe | 2018-01-03 10:16:23 -0600 | [diff] [blame] | 1596 | dri2_setup_screen(disp); |
Emil Velikov | 2c341f2 | 2017-05-11 16:20:04 +0100 | [diff] [blame] | 1597 | |
Wu, Zhongmin | 5fc21c6 | 2018-01-18 15:39:22 +0800 | [diff] [blame] | 1598 | /* We set the maximum swap interval as 1 for Android platform, since it is |
| 1599 | * the maximum value supported by Android according to the value of |
| 1600 | * ANativeWindow::maxSwapInterval. |
| 1601 | */ |
| 1602 | dri2_setup_swap_interval(disp, 1); |
| 1603 | |
Rob Herring | aa187fe | 2018-01-03 10:16:23 -0600 | [diff] [blame] | 1604 | disp->Extensions.ANDROID_framebuffer_target = EGL_TRUE; |
| 1605 | disp->Extensions.ANDROID_image_native_buffer = EGL_TRUE; |
| 1606 | disp->Extensions.ANDROID_recordable = EGL_TRUE; |
| 1607 | disp->Extensions.EXT_buffer_age = EGL_TRUE; |
Harish Krupo | 9827547 | 2017-06-09 20:13:34 +0530 | [diff] [blame] | 1608 | #if ANDROID_API_LEVEL >= 23 |
Rob Herring | aa187fe | 2018-01-03 10:16:23 -0600 | [diff] [blame] | 1609 | disp->Extensions.KHR_partial_update = EGL_TRUE; |
Harish Krupo | 9827547 | 2017-06-09 20:13:34 +0530 | [diff] [blame] | 1610 | #endif |
Rob Herring | aa187fe | 2018-01-03 10:16:23 -0600 | [diff] [blame] | 1611 | disp->Extensions.KHR_image = EGL_TRUE; |
Chad Versace | ed7c694 | 2018-04-30 22:35:17 -0700 | [diff] [blame] | 1612 | #if ANDROID_API_LEVEL >= 24 |
| 1613 | if (dri2_dpy->mutable_render_buffer && |
| 1614 | dri2_dpy->loader_extensions == droid_image_loader_extensions) { |
| 1615 | disp->Extensions.KHR_mutable_render_buffer = EGL_TRUE; |
| 1616 | } |
| 1617 | #endif |
| 1618 | |
| 1619 | /* Create configs *after* enabling extensions because presence of DRI |
| 1620 | * driver extensions can affect the capabilities of EGLConfigs. |
| 1621 | */ |
| 1622 | if (!droid_add_configs_for_visuals(drv, disp)) { |
| 1623 | err = "DRI2: failed to add configs"; |
| 1624 | goto cleanup; |
| 1625 | } |
Chia-I Wu | 9779f6f | 2011-08-05 14:39:18 +0900 | [diff] [blame] | 1626 | |
Chad Versace | 90502b1 | 2014-01-28 11:41:46 -0800 | [diff] [blame] | 1627 | /* Fill vtbl last to prevent accidentally calling virtual function during |
| 1628 | * initialization. |
| 1629 | */ |
| 1630 | dri2_dpy->vtbl = &droid_display_vtbl; |
| 1631 | |
Chia-I Wu | 9779f6f | 2011-08-05 14:39:18 +0900 | [diff] [blame] | 1632 | return EGL_TRUE; |
| 1633 | |
Emil Velikov | 898d785 | 2017-05-11 17:18:13 +0100 | [diff] [blame] | 1634 | cleanup: |
Rob Herring | aa187fe | 2018-01-03 10:16:23 -0600 | [diff] [blame] | 1635 | dri2_display_destroy(disp); |
Chia-I Wu | 9779f6f | 2011-08-05 14:39:18 +0900 | [diff] [blame] | 1636 | return _eglError(EGL_NOT_INITIALIZED, err); |
| 1637 | } |