| 1 |
/* |
| 2 |
* Mesa 3-D graphics library |
| 3 |
* |
| 4 |
* Copyright (C) 2010-2011 Chia-I Wu <[email protected]> |
| 5 |
* Copyright (C) 2010-2011 LunarG Inc. |
| 6 |
* |
| 7 |
* Based on platform_x11, which has |
| 8 |
* |
| 9 |
* Copyright © 2011 Intel Corporation |
| 10 |
* |
| 11 |
* Permission is hereby granted, free of charge, to any person obtaining a |
| 12 |
* copy of this software and associated documentation files (the "Software"), |
| 13 |
* to deal in the Software without restriction, including without limitation |
| 14 |
* the rights to use, copy, modify, merge, publish, distribute, sublicense, |
| 15 |
* and/or sell copies of the Software, and to permit persons to whom the |
| 16 |
* Software is furnished to do so, subject to the following conditions: |
| 17 |
* |
| 18 |
* The above copyright notice and this permission notice shall be included |
| 19 |
* in all copies or substantial portions of the Software. |
| 20 |
* |
| 21 |
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 22 |
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 23 |
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL |
| 24 |
* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 25 |
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING |
| 26 |
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER |
| 27 |
* DEALINGS IN THE SOFTWARE. |
| 28 |
*/ |
| 29 |
|
| 30 |
#include <dirent.h> |
| 31 |
#include <dlfcn.h> |
| 32 |
#include <errno.h> |
| 33 |
#include <fcntl.h> |
| 34 |
#include <stdbool.h> |
| 35 |
#include <stdio.h> |
| 36 |
#include "util/libdrm.h" |
| 37 |
#include <cutils/properties.h> |
| 38 |
#include <drm-uapi/drm_fourcc.h> |
| 39 |
#include <sync/sync.h> |
| 40 |
#include <sys/types.h> |
| 41 |
|
| 42 |
#include "util/compiler.h" |
| 43 |
#include "util/libsync.h" |
| 44 |
#include "util/os_file.h" |
| 45 |
|
| 46 |
#include "main/glconfig.h" |
| 47 |
#include "egl_dri2.h" |
| 48 |
#include "eglglobals.h" |
| 49 |
#include "loader.h" |
| 50 |
#include "loader_dri_helper.h" |
| 51 |
#include "platform_android.h" |
| 52 |
#include "dri_util.h" |
| 53 |
|
| 54 |
static struct dri_image * |
| 55 |
droid_create_image_from_buffer_info( |
| 56 |
struct dri2_egl_display *dri2_dpy, int width, int height, |
| 57 |
struct u_gralloc_buffer_basic_info *buf_info, |
| 58 |
struct u_gralloc_buffer_color_info *color_info, void *priv) |
| 59 |
{ |
| 60 |
unsigned error; |
| 61 |
|
| 62 |
return dri2_from_dma_bufs( |
| 63 |
dri2_dpy->dri_screen_render_gpu, width, height, buf_info->drm_fourcc, |
| 64 |
buf_info->modifier, buf_info->fds, buf_info->num_planes, |
| 65 |
buf_info->strides, buf_info->offsets, color_info->yuv_color_space, |
| 66 |
color_info->sample_range, color_info->horizontal_siting, |
| 67 |
color_info->vertical_siting, 0, &error, priv); |
| 68 |
} |
| 69 |
|
| 70 |
static struct dri_image * |
| 71 |
droid_create_image_from_native_buffer(_EGLDisplay *disp, |
| 72 |
struct ANativeWindowBuffer *buf, |
| 73 |
void *priv) |
| 74 |
{ |
| 75 |
struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp); |
| 76 |
struct u_gralloc_buffer_basic_info buf_info; |
| 77 |
struct u_gralloc_buffer_color_info color_info; |
| 78 |
struct u_gralloc_buffer_handle gr_handle = { |
| 79 |
.handle = buf->handle, |
| 80 |
.hal_format = buf->format, |
| 81 |
.pixel_stride = buf->stride, |
| 82 |
}; |
| 83 |
struct dri_image *img = NULL; |
| 84 |
|
| 85 |
if (u_gralloc_get_buffer_basic_info(dri2_dpy->gralloc, &gr_handle, |
| 86 |
&buf_info)) |
| 87 |
return NULL; |
| 88 |
|
| 89 |
if (u_gralloc_get_buffer_color_info(dri2_dpy->gralloc, &gr_handle, |
| 90 |
&color_info)) |
| 91 |
return NULL; |
| 92 |
|
| 93 |
img = droid_create_image_from_buffer_info(dri2_dpy, buf->width, buf->height, |
| 94 |
&buf_info, &color_info, priv); |
| 95 |
|
| 96 |
if (!img) { |
| 97 |
/* If dri driver is gallium virgl, real modifier info queried back from |
| 98 |
* CrOS info (and potentially mapper metadata if integrated later) cannot |
| 99 |
* get resolved and the buffer import will fail. Thus the fallback |
| 100 |
* behavior is preserved so that the buffer can be imported without |
| 101 |
* modifier info as a last resort. |
| 102 |
*/ |
| 103 |
buf_info.modifier = DRM_FORMAT_MOD_INVALID; |
| 104 |
img = droid_create_image_from_buffer_info( |
| 105 |
dri2_dpy, buf->width, buf->height, &buf_info, &color_info, priv); |
| 106 |
} |
| 107 |
|
| 108 |
return img; |
| 109 |
} |
| 110 |
|
| 111 |
static void |
| 112 |
handle_in_fence_fd(struct dri2_egl_surface *dri2_surf, struct dri_image *img) |
| 113 |
{ |
| 114 |
_EGLDisplay *disp = dri2_surf->base.Resource.Display; |
| 115 |
struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp); |
| 116 |
|
| 117 |
if (dri2_surf->in_fence_fd < 0) |
| 118 |
return; |
| 119 |
|
| 120 |
validate_fence_fd(dri2_surf->in_fence_fd); |
| 121 |
|
| 122 |
if (dri2_dpy->has_native_fence_fd) { |
| 123 |
dri2_set_in_fence_fd(img, dri2_surf->in_fence_fd); |
| 124 |
} else { |
| 125 |
sync_wait(dri2_surf->in_fence_fd, -1); |
| 126 |
} |
| 127 |
} |
| 128 |
|
| 129 |
static void |
| 130 |
close_in_fence_fd(struct dri2_egl_surface *dri2_surf) |
| 131 |
{ |
| 132 |
validate_fence_fd(dri2_surf->in_fence_fd); |
| 133 |
if (dri2_surf->in_fence_fd >= 0) |
| 134 |
close(dri2_surf->in_fence_fd); |
| 135 |
dri2_surf->in_fence_fd = -1; |
| 136 |
} |
| 137 |
|
| 138 |
static EGLBoolean |
| 139 |
droid_window_dequeue_buffer(struct dri2_egl_surface *dri2_surf) |
| 140 |
{ |
| 141 |
int fence_fd; |
| 142 |
|
| 143 |
if (ANativeWindow_dequeueBuffer(dri2_surf->window, &dri2_surf->buffer, |
| 144 |
&fence_fd)) |
| 145 |
return EGL_FALSE; |
| 146 |
|
| 147 |
close_in_fence_fd(dri2_surf); |
| 148 |
|
| 149 |
validate_fence_fd(fence_fd); |
| 150 |
|
| 151 |
dri2_surf->in_fence_fd = fence_fd; |
| 152 |
|
| 153 |
/* Record all the buffers created by ANativeWindow and update back buffer |
| 154 |
* for updating buffer's age in swap_buffers. |
| 155 |
*/ |
| 156 |
EGLBoolean updated = EGL_FALSE; |
| 157 |
for (int i = 0; i < dri2_surf->color_buffers_count; i++) { |
| 158 |
if (!dri2_surf->color_buffers[i].buffer) { |
| 159 |
dri2_surf->color_buffers[i].buffer = dri2_surf->buffer; |
| 160 |
} |
| 161 |
if (dri2_surf->color_buffers[i].buffer == dri2_surf->buffer) { |
| 162 |
dri2_surf->back = &dri2_surf->color_buffers[i]; |
| 163 |
updated = EGL_TRUE; |
| 164 |
break; |
| 165 |
} |
| 166 |
} |
| 167 |
|
| 168 |
if (!updated) { |
| 169 |
/* In case of all the buffers were recreated by ANativeWindow, reset |
| 170 |
* the color_buffers |
| 171 |
*/ |
| 172 |
for (int i = 0; i < dri2_surf->color_buffers_count; i++) { |
| 173 |
dri2_surf->color_buffers[i].buffer = NULL; |
| 174 |
dri2_surf->color_buffers[i].age = 0; |
| 175 |
} |
| 176 |
dri2_surf->color_buffers[0].buffer = dri2_surf->buffer; |
| 177 |
dri2_surf->back = &dri2_surf->color_buffers[0]; |
| 178 |
} |
| 179 |
|
| 180 |
return EGL_TRUE; |
| 181 |
} |
| 182 |
|
| 183 |
static EGLBoolean |
| 184 |
droid_window_enqueue_buffer(_EGLDisplay *disp, |
| 185 |
struct dri2_egl_surface *dri2_surf) |
| 186 |
{ |
| 187 |
/* Queue the buffer with stored out fence fd. The ANativeWindow or buffer |
| 188 |
* consumer may choose to wait for the fence to signal before accessing |
| 189 |
* it. If fence fd value is -1, buffer can be accessed by consumer |
| 190 |
* immediately. Consumer or application shouldn't rely on timestamp |
| 191 |
* associated with fence if the fence fd is -1. |
| 192 |
* |
| 193 |
* Ownership of fd is transferred to consumer after queueBuffer and the |
| 194 |
* consumer is responsible for closing it. Caller must not use the fd |
| 195 |
* after passing it to queueBuffer. |
| 196 |
*/ |
| 197 |
int fence_fd = dri2_surf->out_fence_fd; |
| 198 |
dri2_surf->out_fence_fd = -1; |
| 199 |
ANativeWindow_queueBuffer(dri2_surf->window, dri2_surf->buffer, fence_fd); |
| 200 |
|
| 201 |
dri2_surf->buffer = NULL; |
| 202 |
dri2_surf->back = NULL; |
| 203 |
|
| 204 |
if (dri2_surf->dri_image_back) { |
| 205 |
dri2_destroy_image(dri2_surf->dri_image_back); |
| 206 |
dri2_surf->dri_image_back = NULL; |
| 207 |
} |
| 208 |
|
| 209 |
return EGL_TRUE; |
| 210 |
} |
| 211 |
|
| 212 |
static void |
| 213 |
droid_window_cancel_buffer(struct dri2_egl_surface *dri2_surf) |
| 214 |
{ |
| 215 |
int ret; |
| 216 |
int fence_fd = dri2_surf->out_fence_fd; |
| 217 |
|
| 218 |
dri2_surf->out_fence_fd = -1; |
| 219 |
ret = ANativeWindow_cancelBuffer(dri2_surf->window, dri2_surf->buffer, |
| 220 |
fence_fd); |
| 221 |
dri2_surf->buffer = NULL; |
| 222 |
if (ret < 0) { |
| 223 |
_eglLog(_EGL_WARNING, "ANativeWindow_cancelBuffer failed"); |
| 224 |
dri2_surf->base.Lost = EGL_TRUE; |
| 225 |
} |
| 226 |
|
| 227 |
close_in_fence_fd(dri2_surf); |
| 228 |
} |
| 229 |
|
| 230 |
static bool |
| 231 |
droid_set_shared_buffer_mode(_EGLDisplay *disp, _EGLSurface *surf, bool mode) |
| 232 |
{ |
| 233 |
#if ANDROID_API_LEVEL >= 24 |
| 234 |
struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp); |
| 235 |
struct dri2_egl_surface *dri2_surf = dri2_egl_surface(surf); |
| 236 |
struct ANativeWindow *window = dri2_surf->window; |
| 237 |
|
| 238 |
assert(surf->Type == EGL_WINDOW_BIT); |
| 239 |
assert(_eglSurfaceHasMutableRenderBuffer(&dri2_surf->base)); |
| 240 |
|
| 241 |
_eglLog(_EGL_DEBUG, "%s: mode=%d", __func__, mode); |
| 242 |
|
| 243 |
if (ANativeWindow_setSharedBufferMode(window, mode)) { |
| 244 |
_eglLog(_EGL_WARNING, |
| 245 |
"failed ANativeWindow_setSharedBufferMode" |
| 246 |
"(window=%p, mode=%d)", |
| 247 |
window, mode); |
| 248 |
return false; |
| 249 |
} |
| 250 |
|
| 251 |
if (mode) |
| 252 |
dri2_surf->gralloc_usage |= dri2_dpy->front_rendering_usage; |
| 253 |
else |
| 254 |
dri2_surf->gralloc_usage &= ~dri2_dpy->front_rendering_usage; |
| 255 |
|
| 256 |
if (ANativeWindow_setUsage(window, dri2_surf->gralloc_usage)) { |
| 257 |
_eglLog(_EGL_WARNING, |
| 258 |
"failed ANativeWindow_setUsage(window=%p, usage=%u)", window, |
| 259 |
dri2_surf->gralloc_usage); |
| 260 |
return false; |
| 261 |
} |
| 262 |
|
| 263 |
return true; |
| 264 |
#else |
| 265 |
_eglLog(_EGL_FATAL, "%s:%d: internal error: unreachable", __FILE__, |
| 266 |
__LINE__); |
| 267 |
return false; |
| 268 |
#endif |
| 269 |
} |
| 270 |
|
| 271 |
static _EGLSurface * |
| 272 |
droid_create_surface(_EGLDisplay *disp, EGLint type, _EGLConfig *conf, |
| 273 |
void *native_window, const EGLint *attrib_list) |
| 274 |
{ |
| 275 |
struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp); |
| 276 |
struct dri2_egl_config *dri2_conf = dri2_egl_config(conf); |
| 277 |
struct dri2_egl_surface *dri2_surf; |
| 278 |
struct ANativeWindow *window = native_window; |
| 279 |
const struct dri_config *config; |
| 280 |
|
| 281 |
dri2_surf = calloc(1, sizeof *dri2_surf); |
| 282 |
if (!dri2_surf) { |
| 283 |
_eglError(EGL_BAD_ALLOC, "droid_create_surface"); |
| 284 |
return NULL; |
| 285 |
} |
| 286 |
|
| 287 |
dri2_surf->in_fence_fd = -1; |
| 288 |
|
| 289 |
if (!dri2_init_surface(&dri2_surf->base, disp, type, conf, attrib_list, true, |
| 290 |
native_window)) |
| 291 |
goto cleanup_surface; |
| 292 |
|
| 293 |
if (type == EGL_WINDOW_BIT) { |
| 294 |
int format; |
| 295 |
int buffer_count; |
| 296 |
int min_undequeued_buffers; |
| 297 |
|
| 298 |
format = ANativeWindow_getFormat(window); |
| 299 |
if (format < 0) { |
| 300 |
_eglError(EGL_BAD_NATIVE_WINDOW, "droid_create_surface"); |
| 301 |
goto cleanup_surface; |
| 302 |
} |
| 303 |
|
| 304 |
/* Query ANativeWindow for MIN_UNDEQUEUED_BUFFER, minimum amount |
| 305 |
* of undequeued buffers. |
| 306 |
*/ |
| 307 |
if (ANativeWindow_query(window, |
| 308 |
ANATIVEWINDOW_QUERY_MIN_UNDEQUEUED_BUFFERS, |
| 309 |
&min_undequeued_buffers)) { |
| 310 |
_eglError(EGL_BAD_NATIVE_WINDOW, "droid_create_surface"); |
| 311 |
goto cleanup_surface; |
| 312 |
} |
| 313 |
|
| 314 |
/* Required buffer caching slots. */ |
| 315 |
buffer_count = min_undequeued_buffers + 2; |
| 316 |
|
| 317 |
dri2_surf->color_buffers = |
| 318 |
calloc(buffer_count, sizeof(*dri2_surf->color_buffers)); |
| 319 |
if (!dri2_surf->color_buffers) { |
| 320 |
_eglError(EGL_BAD_ALLOC, "droid_create_surface"); |
| 321 |
goto cleanup_surface; |
| 322 |
} |
| 323 |
dri2_surf->color_buffers_count = buffer_count; |
| 324 |
|
| 325 |
if (format != dri2_conf->base.NativeVisualID) { |
| 326 |
_eglLog(_EGL_WARNING, "Native format mismatch: 0x%x != 0x%x", format, |
| 327 |
dri2_conf->base.NativeVisualID); |
| 328 |
} |
| 329 |
|
| 330 |
ANativeWindow_query(window, ANATIVEWINDOW_QUERY_DEFAULT_WIDTH, |
| 331 |
&dri2_surf->base.Width); |
| 332 |
ANativeWindow_query(window, ANATIVEWINDOW_QUERY_DEFAULT_HEIGHT, |
| 333 |
&dri2_surf->base.Height); |
| 334 |
|
| 335 |
dri2_surf->gralloc_usage = |
| 336 |
((strcmp(dri2_dpy->driver_name, "kms_swrast") == 0) || |
| 337 |
(strcmp(dri2_dpy->driver_name, "swrast") == 0)) |
| 338 |
? GRALLOC_USAGE_SW_READ_OFTEN | GRALLOC_USAGE_SW_WRITE_OFTEN |
| 339 |
: GRALLOC_USAGE_HW_RENDER; |
| 340 |
|
| 341 |
if (dri2_surf->base.ActiveRenderBuffer == EGL_SINGLE_BUFFER) |
| 342 |
dri2_surf->gralloc_usage |= dri2_dpy->front_rendering_usage; |
| 343 |
|
| 344 |
if (ANativeWindow_setUsage(window, dri2_surf->gralloc_usage)) { |
| 345 |
_eglError(EGL_BAD_NATIVE_WINDOW, "droid_create_surface"); |
| 346 |
goto cleanup_surface; |
| 347 |
} |
| 348 |
} |
| 349 |
|
| 350 |
config = dri2_get_dri_config(dri2_conf, type, dri2_surf->base.GLColorspace); |
| 351 |
if (!config) { |
| 352 |
_eglError(EGL_BAD_MATCH, |
| 353 |
"Unsupported surfacetype/colorspace configuration"); |
| 354 |
goto cleanup_surface; |
| 355 |
} |
| 356 |
|
| 357 |
if (!dri2_create_drawable(dri2_dpy, config, dri2_surf, dri2_surf)) |
| 358 |
goto cleanup_surface; |
| 359 |
|
| 360 |
if (window) { |
| 361 |
ANativeWindow_acquire(window); |
| 362 |
dri2_surf->window = window; |
| 363 |
} |
| 364 |
|
| 365 |
return &dri2_surf->base; |
| 366 |
|
| 367 |
cleanup_surface: |
| 368 |
if (dri2_surf->color_buffers_count) |
| 369 |
free(dri2_surf->color_buffers); |
| 370 |
free(dri2_surf); |
| 371 |
|
| 372 |
return NULL; |
| 373 |
} |
| 374 |
|
| 375 |
static _EGLSurface * |
| 376 |
droid_create_window_surface(_EGLDisplay *disp, _EGLConfig *conf, |
| 377 |
void *native_window, const EGLint *attrib_list) |
| 378 |
{ |
| 379 |
return droid_create_surface(disp, EGL_WINDOW_BIT, conf, native_window, |
| 380 |
attrib_list); |
| 381 |
} |
| 382 |
|
| 383 |
static _EGLSurface * |
| 384 |
droid_create_pbuffer_surface(_EGLDisplay *disp, _EGLConfig *conf, |
| 385 |
const EGLint *attrib_list) |
| 386 |
{ |
| 387 |
return droid_create_surface(disp, EGL_PBUFFER_BIT, conf, NULL, attrib_list); |
| 388 |
} |
| 389 |
|
| 390 |
static EGLBoolean |
| 391 |
droid_destroy_surface(_EGLDisplay *disp, _EGLSurface *surf) |
| 392 |
{ |
| 393 |
struct dri2_egl_surface *dri2_surf = dri2_egl_surface(surf); |
| 394 |
|
| 395 |
if (dri2_surf->base.Type == EGL_WINDOW_BIT) { |
| 396 |
if (dri2_surf->buffer) |
| 397 |
droid_window_cancel_buffer(dri2_surf); |
| 398 |
|
| 399 |
ANativeWindow_release(dri2_surf->window); |
| 400 |
} |
| 401 |
|
| 402 |
if (dri2_surf->dri_image_back) { |
| 403 |
_eglLog(_EGL_DEBUG, "%s : %d : destroy dri_image_back", __func__, |
| 404 |
__LINE__); |
| 405 |
dri2_destroy_image(dri2_surf->dri_image_back); |
| 406 |
dri2_surf->dri_image_back = NULL; |
| 407 |
} |
| 408 |
|
| 409 |
if (dri2_surf->dri_image_front) { |
| 410 |
_eglLog(_EGL_DEBUG, "%s : %d : destroy dri_image_front", __func__, |
| 411 |
__LINE__); |
| 412 |
dri2_destroy_image(dri2_surf->dri_image_front); |
| 413 |
dri2_surf->dri_image_front = NULL; |
| 414 |
} |
| 415 |
|
| 416 |
driDestroyDrawable(dri2_surf->dri_drawable); |
| 417 |
|
| 418 |
close_in_fence_fd(dri2_surf); |
| 419 |
dri2_fini_surface(surf); |
| 420 |
free(dri2_surf->color_buffers); |
| 421 |
free(dri2_surf); |
| 422 |
|
| 423 |
return EGL_TRUE; |
| 424 |
} |
| 425 |
|
| 426 |
static EGLBoolean |
| 427 |
droid_swap_interval(_EGLDisplay *disp, _EGLSurface *surf, EGLint interval) |
| 428 |
{ |
| 429 |
struct dri2_egl_surface *dri2_surf = dri2_egl_surface(surf); |
| 430 |
struct ANativeWindow *window = dri2_surf->window; |
| 431 |
|
| 432 |
if (ANativeWindow_setSwapInterval(window, interval)) |
| 433 |
return EGL_FALSE; |
| 434 |
|
| 435 |
surf->SwapInterval = interval; |
| 436 |
return EGL_TRUE; |
| 437 |
} |
| 438 |
|
| 439 |
static int |
| 440 |
update_buffers(struct dri2_egl_surface *dri2_surf) |
| 441 |
{ |
| 442 |
if (dri2_surf->base.Lost) |
| 443 |
return -1; |
| 444 |
|
| 445 |
if (dri2_surf->base.Type != EGL_WINDOW_BIT) |
| 446 |
return 0; |
| 447 |
|
| 448 |
/* try to dequeue the next back buffer */ |
| 449 |
if (!dri2_surf->buffer && !droid_window_dequeue_buffer(dri2_surf)) { |
| 450 |
_eglLog(_EGL_WARNING, "Could not dequeue buffer from native window"); |
| 451 |
dri2_surf->base.Lost = EGL_TRUE; |
| 452 |
return -1; |
| 453 |
} |
| 454 |
|
| 455 |
/* free outdated buffers and update the surface size */ |
| 456 |
if (dri2_surf->base.Width != dri2_surf->buffer->width || |
| 457 |
dri2_surf->base.Height != dri2_surf->buffer->height) { |
| 458 |
dri2_surf->base.Width = dri2_surf->buffer->width; |
| 459 |
dri2_surf->base.Height = dri2_surf->buffer->height; |
| 460 |
} |
| 461 |
|
| 462 |
return 0; |
| 463 |
} |
| 464 |
|
| 465 |
static int |
| 466 |
get_front_bo(struct dri2_egl_surface *dri2_surf, unsigned int format) |
| 467 |
{ |
| 468 |
struct dri2_egl_display *dri2_dpy = |
| 469 |
dri2_egl_display(dri2_surf->base.Resource.Display); |
| 470 |
|
| 471 |
if (dri2_surf->dri_image_front) |
| 472 |
return 0; |
| 473 |
|
| 474 |
if (dri2_surf->base.Type == EGL_WINDOW_BIT) { |
| 475 |
/* According current EGL spec, front buffer rendering |
| 476 |
* for window surface is not supported now. |
| 477 |
* and mesa doesn't have the implementation of this case. |
| 478 |
* Add warning message, but not treat it as error. |
| 479 |
*/ |
| 480 |
_eglLog( |
| 481 |
_EGL_DEBUG, |
| 482 |
"DRI driver requested unsupported front buffer for window surface"); |
| 483 |
} else if (dri2_surf->base.Type == EGL_PBUFFER_BIT) { |
| 484 |
dri2_surf->dri_image_front = dri_create_image( |
| 485 |
dri2_dpy->dri_screen_render_gpu, dri2_surf->base.Width, |
| 486 |
dri2_surf->base.Height, format, NULL, 0, 0, NULL); |
| 487 |
if (!dri2_surf->dri_image_front) { |
| 488 |
_eglLog(_EGL_WARNING, "dri2_image_front allocation failed"); |
| 489 |
return -1; |
| 490 |
} |
| 491 |
} |
| 492 |
|
| 493 |
return 0; |
| 494 |
} |
| 495 |
|
| 496 |
static int |
| 497 |
get_back_bo(struct dri2_egl_surface *dri2_surf) |
| 498 |
{ |
| 499 |
_EGLDisplay *disp = dri2_surf->base.Resource.Display; |
| 500 |
|
| 501 |
if (dri2_surf->dri_image_back) |
| 502 |
return 0; |
| 503 |
|
| 504 |
if (dri2_surf->base.Type == EGL_WINDOW_BIT) { |
| 505 |
if (!dri2_surf->buffer) { |
| 506 |
_eglLog(_EGL_WARNING, "Could not get native buffer"); |
| 507 |
return -1; |
| 508 |
} |
| 509 |
|
| 510 |
dri2_surf->dri_image_back = |
| 511 |
droid_create_image_from_native_buffer(disp, dri2_surf->buffer, NULL); |
| 512 |
if (!dri2_surf->dri_image_back) { |
| 513 |
_eglLog(_EGL_WARNING, "failed to create DRI image from FD"); |
| 514 |
return -1; |
| 515 |
} |
| 516 |
|
| 517 |
handle_in_fence_fd(dri2_surf, dri2_surf->dri_image_back); |
| 518 |
|
| 519 |
} else if (dri2_surf->base.Type == EGL_PBUFFER_BIT) { |
| 520 |
/* The EGL 1.5 spec states that pbuffers are single-buffered. |
| 521 |
* Specifically, the spec states that they have a back buffer but no front |
| 522 |
* buffer, in contrast to pixmaps, which have a front buffer but no back |
| 523 |
* buffer. |
| 524 |
* |
| 525 |
* Single-buffered surfaces with no front buffer confuse Mesa; so we |
| 526 |
* deviate from the spec, following the precedent of Mesa's EGL X11 |
| 527 |
* platform. The X11 platform correctly assigns pbuffers to |
| 528 |
* single-buffered configs, but assigns the pbuffer a front buffer instead |
| 529 |
* of a back buffer. |
| 530 |
* |
| 531 |
* Pbuffers in the X11 platform mostly work today, so let's just copy its |
| 532 |
* behavior instead of trying to fix (and hence potentially breaking) the |
| 533 |
* world. |
| 534 |
*/ |
| 535 |
_eglLog( |
| 536 |
_EGL_DEBUG, |
| 537 |
"DRI driver requested unsupported back buffer for pbuffer surface"); |
| 538 |
} |
| 539 |
|
| 540 |
return 0; |
| 541 |
} |
| 542 |
|
| 543 |
/* Some drivers will pass multiple bits in buffer_mask. |
| 544 |
* For such case, will go through all the bits, and |
| 545 |
* will not return error when unsupported buffer is requested, only |
| 546 |
* return error when the allocation for supported buffer failed. |
| 547 |
*/ |
| 548 |
static int |
| 549 |
droid_image_get_buffers(struct dri_drawable *driDrawable, unsigned int format, |
| 550 |
uint32_t *stamp, void *loaderPrivate, |
| 551 |
uint32_t buffer_mask, struct __DRIimageList *images) |
| 552 |
{ |
| 553 |
struct dri2_egl_surface *dri2_surf = loaderPrivate; |
| 554 |
|
| 555 |
images->image_mask = 0; |
| 556 |
images->front = NULL; |
| 557 |
images->back = NULL; |
| 558 |
|
| 559 |
if (update_buffers(dri2_surf) < 0) |
| 560 |
return 0; |
| 561 |
|
| 562 |
if (_eglSurfaceInSharedBufferMode(&dri2_surf->base)) { |
| 563 |
if (get_back_bo(dri2_surf) < 0) |
| 564 |
return 0; |
| 565 |
|
| 566 |
/* We have dri_image_back because this is a window surface and |
| 567 |
* get_back_bo() succeeded. |
| 568 |
*/ |
| 569 |
assert(dri2_surf->dri_image_back); |
| 570 |
images->back = dri2_surf->dri_image_back; |
| 571 |
images->image_mask |= __DRI_IMAGE_BUFFER_SHARED; |
| 572 |
|
| 573 |
/* There exists no accompanying back nor front buffer. */ |
| 574 |
return 1; |
| 575 |
} |
| 576 |
|
| 577 |
if (buffer_mask & __DRI_IMAGE_BUFFER_FRONT) { |
| 578 |
if (get_front_bo(dri2_surf, format) < 0) |
| 579 |
return 0; |
| 580 |
|
| 581 |
if (dri2_surf->dri_image_front) { |
| 582 |
images->front = dri2_surf->dri_image_front; |
| 583 |
images->image_mask |= __DRI_IMAGE_BUFFER_FRONT; |
| 584 |
} |
| 585 |
} |
| 586 |
|
| 587 |
if (buffer_mask & __DRI_IMAGE_BUFFER_BACK) { |
| 588 |
if (get_back_bo(dri2_surf) < 0) |
| 589 |
return 0; |
| 590 |
|
| 591 |
if (dri2_surf->dri_image_back) { |
| 592 |
images->back = dri2_surf->dri_image_back; |
| 593 |
images->image_mask |= __DRI_IMAGE_BUFFER_BACK; |
| 594 |
} |
| 595 |
} |
| 596 |
|
| 597 |
return 1; |
| 598 |
} |
| 599 |
|
| 600 |
static EGLint |
| 601 |
droid_query_buffer_age(_EGLDisplay *disp, _EGLSurface *surface) |
| 602 |
{ |
| 603 |
struct dri2_egl_surface *dri2_surf = dri2_egl_surface(surface); |
| 604 |
|
| 605 |
if (update_buffers(dri2_surf) < 0) { |
| 606 |
_eglError(EGL_BAD_ALLOC, "droid_query_buffer_age"); |
| 607 |
return -1; |
| 608 |
} |
| 609 |
|
| 610 |
return dri2_surf->back ? dri2_surf->back->age : 0; |
| 611 |
} |
| 612 |
|
| 613 |
static EGLBoolean |
| 614 |
droid_swap_buffers(_EGLDisplay *disp, _EGLSurface *draw) |
| 615 |
{ |
| 616 |
struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp); |
| 617 |
struct dri2_egl_surface *dri2_surf = dri2_egl_surface(draw); |
| 618 |
const bool has_mutable_rb = _eglSurfaceHasMutableRenderBuffer(draw); |
| 619 |
|
| 620 |
/* From the EGL_KHR_mutable_render_buffer spec (v12): |
| 621 |
* |
| 622 |
* If surface is a single-buffered window, pixmap, or pbuffer surface |
| 623 |
* for which there is no pending change to the EGL_RENDER_BUFFER |
| 624 |
* attribute, eglSwapBuffers has no effect. |
| 625 |
*/ |
| 626 |
if (has_mutable_rb && draw->RequestedRenderBuffer == EGL_SINGLE_BUFFER && |
| 627 |
draw->ActiveRenderBuffer == EGL_SINGLE_BUFFER) { |
| 628 |
_eglLog(_EGL_DEBUG, "%s: remain in shared buffer mode", __func__); |
| 629 |
return EGL_TRUE; |
| 630 |
} |
| 631 |
|
| 632 |
for (int i = 0; i < dri2_surf->color_buffers_count; i++) { |
| 633 |
if (dri2_surf->color_buffers[i].age > 0) |
| 634 |
dri2_surf->color_buffers[i].age++; |
| 635 |
} |
| 636 |
|
| 637 |
/* "XXX: we don't use get_back_bo() since it causes regressions in |
| 638 |
* several dEQP tests. |
| 639 |
*/ |
| 640 |
if (dri2_surf->back) |
| 641 |
dri2_surf->back->age = 1; |
| 642 |
|
| 643 |
dri2_flush_drawable_for_swapbuffers_flags(disp, draw, |
| 644 |
__DRI2_NOTHROTTLE_SWAPBUFFER); |
| 645 |
|
| 646 |
if (dri2_dpy->pure_swrast) { |
| 647 |
driSwapBuffers(dri2_surf->dri_drawable); |
| 648 |
if (dri2_surf->buffer) |
| 649 |
droid_window_enqueue_buffer(disp, dri2_surf); |
| 650 |
} else { |
| 651 |
/* dri2_surf->buffer can be null even when no error has occurred. For |
| 652 |
* example, if the user has called no GL rendering commands since the |
| 653 |
* previous eglSwapBuffers, then the driver may have not triggered |
| 654 |
* a callback to ANativeWindow_dequeueBuffer, in which case |
| 655 |
* dri2_surf->buffer remains null. |
| 656 |
*/ |
| 657 |
if (dri2_surf->buffer) |
| 658 |
droid_window_enqueue_buffer(disp, dri2_surf); |
| 659 |
|
| 660 |
dri_invalidate_drawable(dri2_surf->dri_drawable); |
| 661 |
} |
| 662 |
|
| 663 |
/* Update the shared buffer mode */ |
| 664 |
if (has_mutable_rb && |
| 665 |
draw->ActiveRenderBuffer != draw->RequestedRenderBuffer) { |
| 666 |
bool mode = (draw->RequestedRenderBuffer == EGL_SINGLE_BUFFER); |
| 667 |
_eglLog(_EGL_DEBUG, "%s: change to shared buffer mode %d", __func__, |
| 668 |
mode); |
| 669 |
|
| 670 |
if (!droid_set_shared_buffer_mode(disp, draw, mode)) |
| 671 |
return EGL_FALSE; |
| 672 |
draw->ActiveRenderBuffer = draw->RequestedRenderBuffer; |
| 673 |
} |
| 674 |
|
| 675 |
return EGL_TRUE; |
| 676 |
} |
| 677 |
|
| 678 |
static EGLBoolean |
| 679 |
droid_query_surface(_EGLDisplay *disp, _EGLSurface *surf, EGLint attribute, |
| 680 |
EGLint *value) |
| 681 |
{ |
| 682 |
struct dri2_egl_surface *dri2_surf = dri2_egl_surface(surf); |
| 683 |
switch (attribute) { |
| 684 |
case EGL_WIDTH: |
| 685 |
if (dri2_surf->base.Type == EGL_WINDOW_BIT && dri2_surf->window) { |
| 686 |
ANativeWindow_query(dri2_surf->window, |
| 687 |
ANATIVEWINDOW_QUERY_DEFAULT_WIDTH, value); |
| 688 |
return EGL_TRUE; |
| 689 |
} |
| 690 |
break; |
| 691 |
case EGL_HEIGHT: |
| 692 |
if (dri2_surf->base.Type == EGL_WINDOW_BIT && dri2_surf->window) { |
| 693 |
ANativeWindow_query(dri2_surf->window, |
| 694 |
ANATIVEWINDOW_QUERY_DEFAULT_HEIGHT, value); |
| 695 |
return EGL_TRUE; |
| 696 |
} |
| 697 |
break; |
| 698 |
default: |
| 699 |
break; |
| 700 |
} |
| 701 |
return _eglQuerySurface(disp, surf, attribute, value); |
| 702 |
} |
| 703 |
|
| 704 |
static _EGLImage * |
| 705 |
dri2_create_image_android_native_buffer(_EGLDisplay *disp, _EGLContext *ctx, |
| 706 |
struct ANativeWindowBuffer *buf) |
| 707 |
{ |
| 708 |
if (ctx != NULL) { |
| 709 |
/* From the EGL_ANDROID_image_native_buffer spec: |
| 710 |
* |
| 711 |
* * If <target> is EGL_NATIVE_BUFFER_ANDROID and <ctx> is not |
| 712 |
* EGL_NO_CONTEXT, the error EGL_BAD_CONTEXT is generated. |
| 713 |
*/ |
| 714 |
_eglError(EGL_BAD_CONTEXT, |
| 715 |
"eglCreateEGLImageKHR: for " |
| 716 |
"EGL_NATIVE_BUFFER_ANDROID, the context must be " |
| 717 |
"EGL_NO_CONTEXT"); |
| 718 |
return NULL; |
| 719 |
} |
| 720 |
|
| 721 |
if (!buf || buf->common.magic != ANDROID_NATIVE_BUFFER_MAGIC || |
| 722 |
buf->common.version != sizeof(*buf)) { |
| 723 |
_eglError(EGL_BAD_PARAMETER, "eglCreateEGLImageKHR"); |
| 724 |
return NULL; |
| 725 |
} |
| 726 |
|
| 727 |
struct dri_image *dri_image = |
| 728 |
droid_create_image_from_native_buffer(disp, buf, buf); |
| 729 |
|
| 730 |
if (dri_image) { |
| 731 |
#if ANDROID_API_LEVEL >= 26 |
| 732 |
AHardwareBuffer_acquire(ANativeWindowBuffer_getHardwareBuffer(buf)); |
| 733 |
#endif |
| 734 |
return dri2_create_image_from_dri(disp, dri_image); |
| 735 |
} |
| 736 |
|
| 737 |
return NULL; |
| 738 |
} |
| 739 |
|
| 740 |
static _EGLImage * |
| 741 |
droid_create_image_khr(_EGLDisplay *disp, _EGLContext *ctx, EGLenum target, |
| 742 |
EGLClientBuffer buffer, const EGLint *attr_list) |
| 743 |
{ |
| 744 |
switch (target) { |
| 745 |
case EGL_NATIVE_BUFFER_ANDROID: |
| 746 |
return dri2_create_image_android_native_buffer( |
| 747 |
disp, ctx, (struct ANativeWindowBuffer *)buffer); |
| 748 |
default: |
| 749 |
return dri2_create_image_khr(disp, ctx, target, buffer, attr_list); |
| 750 |
} |
| 751 |
} |
| 752 |
|
| 753 |
static void |
| 754 |
droid_flush_front_buffer(struct dri_drawable *driDrawable, void *loaderPrivate) |
| 755 |
{ |
| 756 |
} |
| 757 |
|
| 758 |
static unsigned |
| 759 |
droid_get_capability(void *loaderPrivate, enum dri_loader_cap cap) |
| 760 |
{ |
| 761 |
/* Note: loaderPrivate is _EGLDisplay* */ |
| 762 |
switch (cap) { |
| 763 |
case DRI_LOADER_CAP_RGBA_ORDERING: |
| 764 |
return 1; |
| 765 |
default: |
| 766 |
return 0; |
| 767 |
} |
| 768 |
} |
| 769 |
|
| 770 |
static void |
| 771 |
droid_destroy_loader_image_state(void *loaderPrivate) |
| 772 |
{ |
| 773 |
#if ANDROID_API_LEVEL >= 26 |
| 774 |
if (loaderPrivate) { |
| 775 |
AHardwareBuffer_release( |
| 776 |
ANativeWindowBuffer_getHardwareBuffer(loaderPrivate)); |
| 777 |
} |
| 778 |
#endif |
| 779 |
} |
| 780 |
|
| 781 |
static void |
| 782 |
droid_add_configs_for_visuals(_EGLDisplay *disp) |
| 783 |
{ |
| 784 |
struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp); |
| 785 |
static const struct { |
| 786 |
int hal_format; |
| 787 |
enum pipe_format pipe_format; |
| 788 |
} visuals[] = { |
| 789 |
{HAL_PIXEL_FORMAT_RGBA_8888, PIPE_FORMAT_RGBA8888_UNORM}, |
| 790 |
{HAL_PIXEL_FORMAT_RGBX_8888, PIPE_FORMAT_RGBX8888_UNORM}, |
| 791 |
{HAL_PIXEL_FORMAT_RGB_565, PIPE_FORMAT_B5G6R5_UNORM}, |
| 792 |
/* This must be after HAL_PIXEL_FORMAT_RGBA_8888, we only keep BGRA |
| 793 |
* visual if it turns out RGBA visual is not available. |
| 794 |
*/ |
| 795 |
{HAL_PIXEL_FORMAT_BGRA_8888, PIPE_FORMAT_BGRA8888_UNORM}, |
| 796 |
}; |
| 797 |
|
| 798 |
unsigned int format_count[ARRAY_SIZE(visuals)] = {0}; |
| 799 |
|
| 800 |
/* The nesting of loops is significant here. Also significant is the order |
| 801 |
* of the HAL pixel formats. Many Android apps (such as Google's official |
| 802 |
* NDK GLES2 example app), and even portions the core framework code (such |
| 803 |
* as SystemServiceManager in Nougat), incorrectly choose their EGLConfig. |
| 804 |
* They neglect to match the EGLConfig's EGL_NATIVE_VISUAL_ID against the |
| 805 |
* window's native format, and instead choose the first EGLConfig whose |
| 806 |
* channel sizes match those of the native window format while ignoring the |
| 807 |
* channel *ordering*. |
| 808 |
* |
| 809 |
* We can detect such buggy clients in logcat when they call |
| 810 |
* eglCreateSurface, by detecting the mismatch between the EGLConfig's |
| 811 |
* format and the window's format. |
| 812 |
* |
| 813 |
* As a workaround, we generate EGLConfigs such that all EGLConfigs for HAL |
| 814 |
* pixel format i precede those for HAL pixel format i+1. In my |
| 815 |
* (chadversary) testing on Android Nougat, this was good enough to pacify |
| 816 |
* the buggy clients. |
| 817 |
*/ |
| 818 |
bool has_rgba = false; |
| 819 |
for (int i = 0; i < ARRAY_SIZE(visuals); i++) { |
| 820 |
/* Only enable BGRA configs when RGBA is not available. BGRA configs are |
| 821 |
* buggy on stock Android. |
| 822 |
*/ |
| 823 |
if (visuals[i].hal_format == HAL_PIXEL_FORMAT_BGRA_8888 && has_rgba) |
| 824 |
continue; |
| 825 |
for (int j = 0; dri2_dpy->driver_configs[j]; j++) { |
| 826 |
const struct gl_config *gl_config = |
| 827 |
(struct gl_config *) dri2_dpy->driver_configs[j]; |
| 828 |
|
| 829 |
/* Rather than have duplicate table entries for _SRGB formats, just |
| 830 |
* use the linear version of the format for the comparision: |
| 831 |
*/ |
| 832 |
enum pipe_format linear_format = |
| 833 |
util_format_linear(gl_config->color_format); |
| 834 |
if (linear_format != visuals[i].pipe_format) |
| 835 |
continue; |
| 836 |
|
| 837 |
const EGLint surface_type = EGL_WINDOW_BIT | EGL_PBUFFER_BIT; |
| 838 |
const EGLint config_attrs[] = { |
| 839 |
EGL_NATIVE_VISUAL_ID, |
| 840 |
visuals[i].hal_format, |
| 841 |
EGL_NATIVE_VISUAL_TYPE, |
| 842 |
visuals[i].hal_format, |
| 843 |
EGL_FRAMEBUFFER_TARGET_ANDROID, |
| 844 |
EGL_TRUE, |
| 845 |
EGL_RECORDABLE_ANDROID, |
| 846 |
EGL_TRUE, |
| 847 |
EGL_NONE, |
| 848 |
}; |
| 849 |
|
| 850 |
struct dri2_egl_config *dri2_conf = dri2_add_config( |
| 851 |
disp, dri2_dpy->driver_configs[j], surface_type, config_attrs); |
| 852 |
if (dri2_conf) |
| 853 |
format_count[i]++; |
| 854 |
} |
| 855 |
|
| 856 |
if (visuals[i].hal_format == HAL_PIXEL_FORMAT_RGBA_8888 && format_count[i]) |
| 857 |
has_rgba = true; |
| 858 |
} |
| 859 |
|
| 860 |
for (int i = 0; i < ARRAY_SIZE(format_count); i++) { |
| 861 |
if (!format_count[i]) { |
| 862 |
_eglLog(_EGL_DEBUG, "No DRI config supports native format 0x%x", |
| 863 |
visuals[i].hal_format); |
| 864 |
} |
| 865 |
} |
| 866 |
} |
| 867 |
|
| 868 |
static const struct dri2_egl_display_vtbl droid_display_vtbl = { |
| 869 |
.authenticate = NULL, |
| 870 |
.create_window_surface = droid_create_window_surface, |
| 871 |
.create_pbuffer_surface = droid_create_pbuffer_surface, |
| 872 |
.destroy_surface = droid_destroy_surface, |
| 873 |
.create_image = droid_create_image_khr, |
| 874 |
.swap_buffers = droid_swap_buffers, |
| 875 |
.swap_interval = droid_swap_interval, |
| 876 |
.query_buffer_age = droid_query_buffer_age, |
| 877 |
.query_surface = droid_query_surface, |
| 878 |
.get_dri_drawable = dri2_surface_get_dri_drawable, |
| 879 |
.set_shared_buffer_mode = droid_set_shared_buffer_mode, |
| 880 |
}; |
| 881 |
|
| 882 |
static const __DRIimageLoaderExtension droid_image_loader_extension = { |
| 883 |
.base = {__DRI_IMAGE_LOADER, 4}, |
| 884 |
|
| 885 |
.getBuffers = droid_image_get_buffers, |
| 886 |
.flushFrontBuffer = droid_flush_front_buffer, |
| 887 |
.getCapability = droid_get_capability, |
| 888 |
.flushSwapBuffers = NULL, |
| 889 |
.destroyLoaderImageState = droid_destroy_loader_image_state, |
| 890 |
}; |
| 891 |
|
| 892 |
static void |
| 893 |
droid_display_shared_buffer(struct dri_drawable *driDrawable, int fence_fd, |
| 894 |
void *loaderPrivate) |
| 895 |
{ |
| 896 |
struct dri2_egl_surface *dri2_surf = loaderPrivate; |
| 897 |
struct ANativeWindowBuffer *old_buffer UNUSED = dri2_surf->buffer; |
| 898 |
|
| 899 |
if (!_eglSurfaceInSharedBufferMode(&dri2_surf->base)) { |
| 900 |
_eglLog(_EGL_WARNING, "%s: internal error: buffer is not shared", |
| 901 |
__func__); |
| 902 |
return; |
| 903 |
} |
| 904 |
|
| 905 |
if (fence_fd >= 0) { |
| 906 |
/* The driver's fence is more recent than the surface's out fence, if it |
| 907 |
* exists at all. So use the driver's fence. |
| 908 |
*/ |
| 909 |
if (dri2_surf->out_fence_fd >= 0) { |
| 910 |
close(dri2_surf->out_fence_fd); |
| 911 |
dri2_surf->out_fence_fd = -1; |
| 912 |
} |
| 913 |
} else if (dri2_surf->out_fence_fd >= 0) { |
| 914 |
fence_fd = dri2_surf->out_fence_fd; |
| 915 |
dri2_surf->out_fence_fd = -1; |
| 916 |
} |
| 917 |
|
| 918 |
if (ANativeWindow_queueBuffer(dri2_surf->window, dri2_surf->buffer, |
| 919 |
fence_fd)) { |
| 920 |
_eglLog(_EGL_WARNING, "%s: ANativeWindow_queueBuffer failed", __func__); |
| 921 |
close(fence_fd); |
| 922 |
return; |
| 923 |
} |
| 924 |
|
| 925 |
fence_fd = -1; |
| 926 |
|
| 927 |
if (ANativeWindow_dequeueBuffer(dri2_surf->window, &dri2_surf->buffer, |
| 928 |
&fence_fd)) { |
| 929 |
/* Tear down the surface because it no longer has a back buffer. */ |
| 930 |
_eglLog(_EGL_WARNING, "%s: ANativeWindow_dequeueBuffer failed", __func__); |
| 931 |
|
| 932 |
dri2_surf->base.Lost = true; |
| 933 |
dri2_surf->buffer = NULL; |
| 934 |
dri2_surf->back = NULL; |
| 935 |
|
| 936 |
if (dri2_surf->dri_image_back) { |
| 937 |
dri2_destroy_image(dri2_surf->dri_image_back); |
| 938 |
dri2_surf->dri_image_back = NULL; |
| 939 |
} |
| 940 |
|
| 941 |
dri_invalidate_drawable(dri2_surf->dri_drawable); |
| 942 |
return; |
| 943 |
} |
| 944 |
|
| 945 |
close_in_fence_fd(dri2_surf); |
| 946 |
validate_fence_fd(fence_fd); |
| 947 |
dri2_surf->in_fence_fd = fence_fd; |
| 948 |
handle_in_fence_fd(dri2_surf, dri2_surf->dri_image_back); |
| 949 |
} |
| 950 |
|
| 951 |
static void |
| 952 |
droid_swrast_get_drawable_info(struct dri_drawable *drawable, |
| 953 |
int *x, int *y, int *width, int *height, |
| 954 |
void *loaderPrivate) |
| 955 |
{ |
| 956 |
struct dri2_egl_surface *dri2_surf = loaderPrivate; |
| 957 |
|
| 958 |
update_buffers(dri2_surf); |
| 959 |
|
| 960 |
*x = 0; |
| 961 |
*y = 0; |
| 962 |
*width = dri2_surf->base.Width; |
| 963 |
*height = dri2_surf->base.Height; |
| 964 |
} |
| 965 |
|
| 966 |
static void |
| 967 |
droid_swrast_put_image2(struct dri_drawable *draw, int op, int x, int y, int w, |
| 968 |
int h, int stride, char *data, void *loaderPrivate) |
| 969 |
{ |
| 970 |
return; |
| 971 |
} |
| 972 |
|
| 973 |
static void |
| 974 |
droid_swrast_put_image(struct dri_drawable *draw, int op, int x, int y, int w, |
| 975 |
int h, char *data, void *loaderPrivate) |
| 976 |
{ |
| 977 |
return; |
| 978 |
} |
| 979 |
|
| 980 |
static void |
| 981 |
droid_swrast_get_image(struct dri_drawable *read, int x, int y, int w, int h, |
| 982 |
char *data, void *loaderPrivate) |
| 983 |
{ |
| 984 |
return; |
| 985 |
} |
| 986 |
|
| 987 |
static const __DRIswrastLoaderExtension swrast_loader_extension = { |
| 988 |
.base = {__DRI_SWRAST_LOADER, 2}, |
| 989 |
|
| 990 |
.getDrawableInfo = droid_swrast_get_drawable_info, |
| 991 |
.putImage = droid_swrast_put_image, |
| 992 |
.getImage = droid_swrast_get_image, |
| 993 |
.putImage2 = droid_swrast_put_image2, |
| 994 |
}; |
| 995 |
|
| 996 |
static const __DRImutableRenderBufferLoaderExtension |
| 997 |
droid_mutable_render_buffer_extension = { |
| 998 |
.base = {__DRI_MUTABLE_RENDER_BUFFER_LOADER, 1}, |
| 999 |
.displaySharedBuffer = droid_display_shared_buffer, |
| 1000 |
}; |
| 1001 |
|
| 1002 |
static const __DRIextension *droid_image_loader_extensions[] = { |
| 1003 |
&droid_image_loader_extension.base, |
| 1004 |
&image_lookup_extension.base, |
| 1005 |
&droid_mutable_render_buffer_extension.base, |
| 1006 |
NULL, |
| 1007 |
}; |
| 1008 |
|
| 1009 |
static const __DRIextension *droid_swrast_image_loader_extensions[] = { |
| 1010 |
&droid_image_loader_extension.base, |
| 1011 |
&image_lookup_extension.base, |
| 1012 |
&droid_mutable_render_buffer_extension.base, |
| 1013 |
&swrast_loader_extension.base, |
| 1014 |
NULL, |
| 1015 |
}; |
| 1016 |
|
| 1017 |
static EGLBoolean |
| 1018 |
droid_load_driver(_EGLDisplay *disp, bool swrast) |
| 1019 |
{ |
| 1020 |
struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp); |
| 1021 |
|
| 1022 |
if (disp->Options.Zink) |
| 1023 |
dri2_dpy->driver_name = strdup("zink"); |
| 1024 |
else |
| 1025 |
dri2_dpy->driver_name = loader_get_driver_for_fd(dri2_dpy->fd_render_gpu); |
| 1026 |
if (dri2_dpy->driver_name == NULL) |
| 1027 |
return false; |
| 1028 |
|
| 1029 |
if (swrast && !disp->Options.Zink) { |
| 1030 |
/* Use kms swrast only with vgem / virtio_gpu. |
| 1031 |
* virtio-gpu fallbacks to software rendering when 3D features |
| 1032 |
* are unavailable since 6c5ab. |
| 1033 |
*/ |
| 1034 |
if (strcmp(dri2_dpy->driver_name, "vgem") == 0 || |
| 1035 |
strcmp(dri2_dpy->driver_name, "virtio_gpu") == 0) { |
| 1036 |
free(dri2_dpy->driver_name); |
| 1037 |
dri2_dpy->driver_name = strdup("kms_swrast"); |
| 1038 |
} else { |
| 1039 |
goto error; |
| 1040 |
} |
| 1041 |
} |
| 1042 |
|
| 1043 |
dri2_dpy->loader_extensions = droid_image_loader_extensions; |
| 1044 |
dri2_detect_swrast_kopper(disp); |
| 1045 |
|
| 1046 |
return true; |
| 1047 |
|
| 1048 |
error: |
| 1049 |
free(dri2_dpy->driver_name); |
| 1050 |
dri2_dpy->driver_name = NULL; |
| 1051 |
return false; |
| 1052 |
} |
| 1053 |
|
| 1054 |
static void |
| 1055 |
droid_unload_driver(_EGLDisplay *disp) |
| 1056 |
{ |
| 1057 |
struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp); |
| 1058 |
|
| 1059 |
free(dri2_dpy->driver_name); |
| 1060 |
dri2_dpy->driver_name = NULL; |
| 1061 |
} |
| 1062 |
|
| 1063 |
static int |
| 1064 |
droid_filter_device(_EGLDisplay *disp, int fd, const char *vendor) |
| 1065 |
{ |
| 1066 |
drmVersionPtr ver = drmGetVersion(fd); |
| 1067 |
if (!ver) |
| 1068 |
return -1; |
| 1069 |
|
| 1070 |
if (strcmp(vendor, ver->name) != 0) { |
| 1071 |
drmFreeVersion(ver); |
| 1072 |
return -1; |
| 1073 |
} |
| 1074 |
|
| 1075 |
drmFreeVersion(ver); |
| 1076 |
return 0; |
| 1077 |
} |
| 1078 |
|
| 1079 |
static EGLBoolean |
| 1080 |
droid_probe_device(_EGLDisplay *disp, bool swrast) |
| 1081 |
{ |
| 1082 |
/* Check that the device is supported, by attempting to: |
| 1083 |
* - load the dri module |
| 1084 |
* - and, create a screen |
| 1085 |
*/ |
| 1086 |
if (!droid_load_driver(disp, swrast)) |
| 1087 |
return EGL_FALSE; |
| 1088 |
|
| 1089 |
if (!dri2_create_screen(disp)) { |
| 1090 |
_eglLog(_EGL_WARNING, "DRI2: failed to create screen"); |
| 1091 |
droid_unload_driver(disp); |
| 1092 |
return EGL_FALSE; |
| 1093 |
} |
| 1094 |
return EGL_TRUE; |
| 1095 |
} |
| 1096 |
|
| 1097 |
static EGLBoolean |
| 1098 |
droid_open_device(_EGLDisplay *disp, bool swrast) |
| 1099 |
{ |
| 1100 |
#define MAX_DRM_DEVICES 64 |
| 1101 |
struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp); |
| 1102 |
_EGLDevice *dev_list = _eglGlobal.DeviceList; |
| 1103 |
drmDevicePtr device; |
| 1104 |
|
| 1105 |
char *vendor_name = NULL; |
| 1106 |
char vendor_buf[PROPERTY_VALUE_MAX]; |
| 1107 |
|
| 1108 |
#ifdef EGL_FORCE_RENDERNODE |
| 1109 |
const unsigned node_type = DRM_NODE_RENDER; |
| 1110 |
#else |
| 1111 |
const unsigned node_type = swrast ? DRM_NODE_PRIMARY : DRM_NODE_RENDER; |
| 1112 |
#endif |
| 1113 |
|
| 1114 |
if (property_get("drm.gpu.vendor_name", vendor_buf, NULL) > 0) |
| 1115 |
vendor_name = vendor_buf; |
| 1116 |
|
| 1117 |
while (dev_list) { |
| 1118 |
if (!_eglDeviceSupports(dev_list, _EGL_DEVICE_DRM)) |
| 1119 |
goto next; |
| 1120 |
|
| 1121 |
device = _eglDeviceDrm(dev_list); |
| 1122 |
assert(device); |
| 1123 |
|
| 1124 |
if (!(device->available_nodes & (1 << node_type))) |
| 1125 |
goto next; |
| 1126 |
|
| 1127 |
dri2_dpy->fd_render_gpu = loader_open_device(device->nodes[node_type]); |
| 1128 |
if (dri2_dpy->fd_render_gpu < 0) { |
| 1129 |
_eglLog(_EGL_WARNING, "%s() Failed to open DRM device %s", __func__, |
| 1130 |
device->nodes[node_type]); |
| 1131 |
goto next; |
| 1132 |
} |
| 1133 |
|
| 1134 |
/* If a vendor is explicitly provided, we use only that. |
| 1135 |
* Otherwise we fall-back the first device that is supported. |
| 1136 |
*/ |
| 1137 |
if (vendor_name) { |
| 1138 |
if (droid_filter_device(disp, dri2_dpy->fd_render_gpu, vendor_name)) { |
| 1139 |
/* Device does not match - try next device */ |
| 1140 |
close(dri2_dpy->fd_render_gpu); |
| 1141 |
dri2_dpy->fd_render_gpu = -1; |
| 1142 |
goto next; |
| 1143 |
} |
| 1144 |
/* If the requested device matches - use it. Regardless if |
| 1145 |
* init fails, do not fall-back to any other device. |
| 1146 |
*/ |
| 1147 |
if (!droid_probe_device(disp, false)) { |
| 1148 |
close(dri2_dpy->fd_render_gpu); |
| 1149 |
dri2_dpy->fd_render_gpu = -1; |
| 1150 |
} |
| 1151 |
|
| 1152 |
break; |
| 1153 |
} |
| 1154 |
if (droid_probe_device(disp, swrast)) |
| 1155 |
break; |
| 1156 |
|
| 1157 |
/* No explicit request - attempt the next device */ |
| 1158 |
close(dri2_dpy->fd_render_gpu); |
| 1159 |
dri2_dpy->fd_render_gpu = -1; |
| 1160 |
|
| 1161 |
next: |
| 1162 |
dev_list = _eglDeviceNext(dev_list); |
| 1163 |
} |
| 1164 |
|
| 1165 |
if (dri2_dpy->fd_render_gpu < 0) { |
| 1166 |
_eglLog(_EGL_WARNING, "Failed to open %s DRM device", |
| 1167 |
vendor_name ? "desired" : "any"); |
| 1168 |
return EGL_FALSE; |
| 1169 |
} |
| 1170 |
|
| 1171 |
return EGL_TRUE; |
| 1172 |
} |
| 1173 |
|
| 1174 |
EGLBoolean |
| 1175 |
dri2_initialize_android(_EGLDisplay *disp) |
| 1176 |
{ |
| 1177 |
bool device_opened = false; |
| 1178 |
struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp); |
| 1179 |
const char *err; |
| 1180 |
|
| 1181 |
dri2_dpy->gralloc = u_gralloc_create(U_GRALLOC_TYPE_AUTO); |
| 1182 |
if (dri2_dpy->gralloc == NULL) { |
| 1183 |
err = "DRI2: failed to get gralloc"; |
| 1184 |
goto cleanup; |
| 1185 |
} |
| 1186 |
|
| 1187 |
bool force_pure_swrast = debug_get_bool_option("MESA_ANDROID_NO_KMS_SWRAST", false); |
| 1188 |
|
| 1189 |
if (!force_pure_swrast) |
| 1190 |
device_opened = droid_open_device(disp, disp->Options.ForceSoftware); |
| 1191 |
|
| 1192 |
if ((!device_opened && disp->Options.ForceSoftware) || |
| 1193 |
force_pure_swrast) { |
| 1194 |
dri2_dpy->driver_name = strdup("swrast"); |
| 1195 |
dri2_dpy->loader_extensions = droid_swrast_image_loader_extensions; |
| 1196 |
dri2_dpy->fd_render_gpu = -1; |
| 1197 |
dri2_dpy->pure_swrast = true; |
| 1198 |
dri2_detect_swrast_kopper(disp); |
| 1199 |
|
| 1200 |
if (!dri2_create_screen(disp)) { |
| 1201 |
err = "DRI2: Failed to create swrast screen"; |
| 1202 |
goto cleanup; |
| 1203 |
} |
| 1204 |
|
| 1205 |
device_opened = EGL_TRUE; |
| 1206 |
} |
| 1207 |
|
| 1208 |
if (!device_opened) { |
| 1209 |
err = "DRI2: failed to open device"; |
| 1210 |
goto cleanup; |
| 1211 |
} |
| 1212 |
|
| 1213 |
dri2_dpy->fd_display_gpu = dri2_dpy->fd_render_gpu; |
| 1214 |
|
| 1215 |
if (!dri2_dpy->pure_swrast && !dri2_setup_device(disp, false)) { |
| 1216 |
err = "DRI2: failed to setup EGLDevice"; |
| 1217 |
goto cleanup; |
| 1218 |
} |
| 1219 |
|
| 1220 |
dri2_setup_screen(disp); |
| 1221 |
|
| 1222 |
/* We set the maximum swap interval as 1 for Android platform, since it is |
| 1223 |
* the maximum value supported by Android according to the value of |
| 1224 |
* ANativeWindow::maxSwapInterval. |
| 1225 |
*/ |
| 1226 |
dri2_setup_swap_interval(disp, 1); |
| 1227 |
|
| 1228 |
disp->Extensions.ANDROID_framebuffer_target = EGL_TRUE; |
| 1229 |
disp->Extensions.ANDROID_image_native_buffer = EGL_TRUE; |
| 1230 |
disp->Extensions.ANDROID_recordable = EGL_TRUE; |
| 1231 |
|
| 1232 |
/* Querying buffer age requires a buffer to be dequeued. Without |
| 1233 |
* EGL_ANDROID_native_fence_sync, dequeue might call eglClientWaitSync and |
| 1234 |
* result in a deadlock (the lock is already held by eglQuerySurface). |
| 1235 |
*/ |
| 1236 |
if (disp->Extensions.ANDROID_native_fence_sync) { |
| 1237 |
disp->Extensions.EXT_buffer_age = EGL_TRUE; |
| 1238 |
} else { |
| 1239 |
/* disable KHR_partial_update that might have been enabled in |
| 1240 |
* dri2_setup_screen |
| 1241 |
*/ |
| 1242 |
disp->Extensions.KHR_partial_update = EGL_FALSE; |
| 1243 |
} |
| 1244 |
|
| 1245 |
disp->Extensions.KHR_image = EGL_TRUE; |
| 1246 |
|
| 1247 |
dri2_dpy->front_rendering_usage = 0; |
| 1248 |
#if ANDROID_API_LEVEL >= 24 |
| 1249 |
if (!dri2_dpy->swrast_not_kms && |
| 1250 |
dri2_dpy->loader_extensions == droid_image_loader_extensions && |
| 1251 |
/* In big GL, front rendering is done at the core API level by directly |
| 1252 |
* rendering on the front buffer. However, in ES, the front buffer is |
| 1253 |
* completely inaccessible through the core ES API. |
| 1254 |
* |
| 1255 |
* EGL_KHR_mutable_render_buffer is Android's attempt to re-introduce |
| 1256 |
* front rendering into ES by squeezing into EGL. Unlike big GL, this |
| 1257 |
* extension redirects GL_BACK used by ES for front rendering. Thus we |
| 1258 |
* restrict the enabling of this extension to ES only. |
| 1259 |
*/ |
| 1260 |
(disp->ClientAPIs & ~(EGL_OPENGL_ES_BIT | EGL_OPENGL_ES2_BIT | |
| 1261 |
EGL_OPENGL_ES3_BIT_KHR)) == 0) { |
| 1262 |
/* For cros gralloc, if the front rendering query is supported, then all |
| 1263 |
* available window surface configs support front rendering because: |
| 1264 |
* |
| 1265 |
* 1) EGL queries cros gralloc for the front rendering usage bit here |
| 1266 |
* 2) EGL combines the front rendering usage bit with the existing usage |
| 1267 |
* if the window surface requests mutable render buffer |
| 1268 |
* 3) EGL sets the combined usage onto the ANativeWindow and the next |
| 1269 |
* dequeueBuffer will ask gralloc for an allocation/re-allocation with |
| 1270 |
* the new combined usage |
| 1271 |
* 4) cros gralloc(on top of minigbm) resolves the front rendering usage |
| 1272 |
* bit into either BO_USE_FRONT_RENDERING or BO_USE_LINEAR based on |
| 1273 |
* the format support checking. |
| 1274 |
* |
| 1275 |
* So at least we can force BO_USE_LINEAR as the fallback. |
| 1276 |
*/ |
| 1277 |
uint64_t front_rendering_usage = 0; |
| 1278 |
if (!u_gralloc_get_front_rendering_usage(dri2_dpy->gralloc, |
| 1279 |
&front_rendering_usage)) { |
| 1280 |
dri2_dpy->front_rendering_usage = front_rendering_usage; |
| 1281 |
disp->Extensions.KHR_mutable_render_buffer = EGL_TRUE; |
| 1282 |
} |
| 1283 |
} |
| 1284 |
#endif |
| 1285 |
|
| 1286 |
/* Create configs *after* enabling extensions because presence of DRI |
| 1287 |
* driver extensions can affect the capabilities of EGLConfigs. |
| 1288 |
*/ |
| 1289 |
droid_add_configs_for_visuals(disp); |
| 1290 |
|
| 1291 |
/* Fill vtbl last to prevent accidentally calling virtual function during |
| 1292 |
* initialization. |
| 1293 |
*/ |
| 1294 |
dri2_dpy->vtbl = &droid_display_vtbl; |
| 1295 |
|
| 1296 |
return EGL_TRUE; |
| 1297 |
|
| 1298 |
cleanup: |
| 1299 |
return _eglError(EGL_NOT_INITIALIZED, err); |
| 1300 |
} |