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