#include <stdio.h>
#include <sync/sync.h>
#include <sys/types.h>
+#include <drm-uapi/drm_fourcc.h>
+
+#include "util/os_file.h"
#include "loader.h"
#include "egl_dri2.h"
-#include "egl_dri2_fallbacks.h"
#ifdef HAVE_DRM_GRALLOC
#include <gralloc_drm_handle.h>
#define ALIGN(val, align) (((val) + (align) - 1) & ~((align) - 1))
+enum chroma_order {
+ YCbCr,
+ YCrCb,
+};
+
struct droid_yuv_format {
/* Lookup keys */
int native; /* HAL_PIXEL_FORMAT_ */
- int is_ycrcb; /* 0 if chroma order is {Cb, Cr}, 1 if {Cr, Cb} */
+ enum chroma_order chroma_order; /* chroma order is {Cb, Cr} or {Cr, Cb} */
int chroma_step; /* Distance in bytes between subsequent chroma pixels. */
/* Result */
- int fourcc; /* __DRI_IMAGE_FOURCC_ */
+ int fourcc; /* DRM_FORMAT_ */
};
/* The following table is used to look up a DRI image FourCC based
* on native format and information contained in android_ycbcr struct. */
static const struct droid_yuv_format droid_yuv_formats[] = {
/* Native format, YCrCb, Chroma step, DRI image FourCC */
- { HAL_PIXEL_FORMAT_YCbCr_420_888, 0, 2, __DRI_IMAGE_FOURCC_NV12 },
- { HAL_PIXEL_FORMAT_YCbCr_420_888, 0, 1, __DRI_IMAGE_FOURCC_YUV420 },
- { HAL_PIXEL_FORMAT_YCbCr_420_888, 1, 1, __DRI_IMAGE_FOURCC_YVU420 },
- { HAL_PIXEL_FORMAT_YV12, 1, 1, __DRI_IMAGE_FOURCC_YVU420 },
- /* HACK: See droid_create_image_from_prime_fd() and
+ { HAL_PIXEL_FORMAT_YCbCr_420_888, YCbCr, 2, DRM_FORMAT_NV12 },
+ { HAL_PIXEL_FORMAT_YCbCr_420_888, YCbCr, 1, DRM_FORMAT_YUV420 },
+ { HAL_PIXEL_FORMAT_YCbCr_420_888, YCrCb, 1, DRM_FORMAT_YVU420 },
+ { HAL_PIXEL_FORMAT_YV12, YCrCb, 1, DRM_FORMAT_YVU420 },
+ /* HACK: See droid_create_image_from_prime_fds() and
* https://issuetracker.google.com/32077885. */
- { HAL_PIXEL_FORMAT_IMPLEMENTATION_DEFINED, 0, 2, __DRI_IMAGE_FOURCC_NV12 },
- { HAL_PIXEL_FORMAT_IMPLEMENTATION_DEFINED, 0, 1, __DRI_IMAGE_FOURCC_YUV420 },
- { HAL_PIXEL_FORMAT_IMPLEMENTATION_DEFINED, 1, 1, __DRI_IMAGE_FOURCC_YVU420 },
+ { HAL_PIXEL_FORMAT_IMPLEMENTATION_DEFINED, YCbCr, 2, DRM_FORMAT_NV12 },
+ { HAL_PIXEL_FORMAT_IMPLEMENTATION_DEFINED, YCbCr, 1, DRM_FORMAT_YUV420 },
+ { HAL_PIXEL_FORMAT_IMPLEMENTATION_DEFINED, YCrCb, 1, DRM_FORMAT_YVU420 },
+ { HAL_PIXEL_FORMAT_IMPLEMENTATION_DEFINED, YCrCb, 1, DRM_FORMAT_AYUV },
+ { HAL_PIXEL_FORMAT_IMPLEMENTATION_DEFINED, YCrCb, 1, DRM_FORMAT_XYUV8888 },
};
static int
-get_fourcc_yuv(int native, int is_ycrcb, int chroma_step)
+get_fourcc_yuv(int native, enum chroma_order chroma_order, int chroma_step)
{
for (int i = 0; i < ARRAY_SIZE(droid_yuv_formats); ++i)
if (droid_yuv_formats[i].native == native &&
- droid_yuv_formats[i].is_ycrcb == is_ycrcb &&
+ droid_yuv_formats[i].chroma_order == chroma_order &&
droid_yuv_formats[i].chroma_step == chroma_step)
return droid_yuv_formats[i].fourcc;
int bpp;
switch (native) {
+ case HAL_PIXEL_FORMAT_RGBA_FP16:
+ bpp = 8;
+ break;
case HAL_PIXEL_FORMAT_RGBA_8888:
case HAL_PIXEL_FORMAT_IMPLEMENTATION_DEFINED:
/*
*/
case HAL_PIXEL_FORMAT_RGBX_8888:
case HAL_PIXEL_FORMAT_BGRA_8888:
+ case HAL_PIXEL_FORMAT_RGBA_1010102:
bpp = 4;
break;
case HAL_PIXEL_FORMAT_RGB_565:
static int get_fourcc(int native)
{
switch (native) {
- case HAL_PIXEL_FORMAT_RGB_565: return __DRI_IMAGE_FOURCC_RGB565;
- case HAL_PIXEL_FORMAT_BGRA_8888: return __DRI_IMAGE_FOURCC_ARGB8888;
- case HAL_PIXEL_FORMAT_RGBA_8888: return __DRI_IMAGE_FOURCC_ABGR8888;
+ case HAL_PIXEL_FORMAT_RGB_565: return DRM_FORMAT_RGB565;
+ case HAL_PIXEL_FORMAT_BGRA_8888: return DRM_FORMAT_ARGB8888;
+ case HAL_PIXEL_FORMAT_RGBA_8888: return DRM_FORMAT_ABGR8888;
case HAL_PIXEL_FORMAT_IMPLEMENTATION_DEFINED:
/*
* HACK: Hardcode this to RGBX_8888 as per cros_gralloc hack.
* TODO: Remove this once https://issuetracker.google.com/32077885 is fixed.
*/
- case HAL_PIXEL_FORMAT_RGBX_8888: return __DRI_IMAGE_FOURCC_XBGR8888;
+ case HAL_PIXEL_FORMAT_RGBX_8888: return DRM_FORMAT_XBGR8888;
+ case HAL_PIXEL_FORMAT_RGBA_FP16: return DRM_FORMAT_ABGR16161616F;
+ case HAL_PIXEL_FORMAT_RGBA_1010102: return DRM_FORMAT_ABGR2101010;
default:
_eglLog(_EGL_WARNING, "unsupported native buffer format 0x%x", native);
}
return -1;
}
-static int get_format(int format)
-{
- switch (format) {
- case HAL_PIXEL_FORMAT_BGRA_8888: return __DRI_IMAGE_FORMAT_ARGB8888;
- case HAL_PIXEL_FORMAT_RGB_565: return __DRI_IMAGE_FORMAT_RGB565;
- case HAL_PIXEL_FORMAT_RGBA_8888: return __DRI_IMAGE_FORMAT_ABGR8888;
- case HAL_PIXEL_FORMAT_IMPLEMENTATION_DEFINED:
- /*
- * HACK: Hardcode this to RGBX_8888 as per cros_gralloc hack.
- * TODO: Revert this once https://issuetracker.google.com/32077885 is fixed.
- */
- case HAL_PIXEL_FORMAT_RGBX_8888: return __DRI_IMAGE_FORMAT_XBGR8888;
- default:
- _eglLog(_EGL_WARNING, "unsupported native buffer format 0x%x", format);
- }
- return -1;
-}
-
-static int
-get_native_buffer_fd(struct ANativeWindowBuffer *buf)
+/* returns # of fds, and by reference the actual fds */
+static unsigned
+get_native_buffer_fds(struct ANativeWindowBuffer *buf, int fds[3])
{
native_handle_t *handle = (native_handle_t *)buf->handle;
+
+ if (!handle)
+ return 0;
+
/*
* Various gralloc implementations exist, but the dma-buf fd tends
* to be first. Access it directly to avoid a dependency on specific
* gralloc versions.
*/
- return (handle && handle->numFds) ? handle->data[0] : -1;
+ for (int i = 0; i < handle->numFds; i++)
+ fds[i] = handle->data[i];
+
+ return handle->numFds;
}
#ifdef HAVE_DRM_GRALLOC
}
#endif /* HAVE_DRM_GRALLOC */
+static __DRIimage *
+droid_create_image_from_prime_fds_yuv(_EGLDisplay *disp,
+ struct ANativeWindowBuffer *buf,
+ int num_fds, int fds[3])
+{
+ struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);
+ struct android_ycbcr ycbcr;
+ int offsets[3];
+ int pitches[3];
+ enum chroma_order chroma_order;
+ int fourcc;
+ int ret;
+ unsigned error;
+
+ if (!dri2_dpy->gralloc->lock_ycbcr) {
+ _eglLog(_EGL_WARNING, "Gralloc does not support lock_ycbcr");
+ return NULL;
+ }
+
+ memset(&ycbcr, 0, sizeof(ycbcr));
+ ret = dri2_dpy->gralloc->lock_ycbcr(dri2_dpy->gralloc, buf->handle,
+ 0, 0, 0, 0, 0, &ycbcr);
+ if (ret) {
+ /* HACK: See droid_create_image_from_prime_fds() and
+ * https://issuetracker.google.com/32077885.*/
+ if (buf->format == HAL_PIXEL_FORMAT_IMPLEMENTATION_DEFINED)
+ return NULL;
+
+ _eglLog(_EGL_WARNING, "gralloc->lock_ycbcr failed: %d", ret);
+ return NULL;
+ }
+ dri2_dpy->gralloc->unlock(dri2_dpy->gralloc, buf->handle);
+
+ /* When lock_ycbcr's usage argument contains no SW_READ/WRITE flags
+ * it will return the .y/.cb/.cr pointers based on a NULL pointer,
+ * so they can be interpreted as offsets. */
+ offsets[0] = (size_t)ycbcr.y;
+ /* We assume here that all the planes are located in one DMA-buf. */
+ if ((size_t)ycbcr.cr < (size_t)ycbcr.cb) {
+ chroma_order = YCrCb;
+ offsets[1] = (size_t)ycbcr.cr;
+ offsets[2] = (size_t)ycbcr.cb;
+ } else {
+ chroma_order = YCbCr;
+ offsets[1] = (size_t)ycbcr.cb;
+ offsets[2] = (size_t)ycbcr.cr;
+ }
+
+ /* .ystride is the line length (in bytes) of the Y plane,
+ * .cstride is the line length (in bytes) of any of the remaining
+ * Cb/Cr/CbCr planes, assumed to be the same for Cb and Cr for fully
+ * planar formats. */
+ pitches[0] = ycbcr.ystride;
+ pitches[1] = pitches[2] = ycbcr.cstride;
+
+ /* .chroma_step is the byte distance between the same chroma channel
+ * values of subsequent pixels, assumed to be the same for Cb and Cr. */
+ fourcc = get_fourcc_yuv(buf->format, chroma_order, ycbcr.chroma_step);
+ if (fourcc == -1) {
+ _eglLog(_EGL_WARNING, "unsupported YUV format, native = %x, chroma_order = %s, chroma_step = %d",
+ buf->format, chroma_order == YCbCr ? "YCbCr" : "YCrCb", ycbcr.chroma_step);
+ return NULL;
+ }
+
+ /*
+ * Since this is EGL_NATIVE_BUFFER_ANDROID don't assume that
+ * the single-fd case cannot happen. So handle eithe single
+ * fd or fd-per-plane case:
+ */
+ if (num_fds == 1) {
+ fds[2] = fds[1] = fds[0];
+ } else {
+ int expected_planes = (ycbcr.chroma_step == 2) ? 2 : 3;
+ assert(num_fds == expected_planes);
+ }
+
+ return dri2_dpy->image->createImageFromDmaBufs(dri2_dpy->dri_screen,
+ buf->width, buf->height, fourcc,
+ fds, num_fds, pitches, offsets,
+ EGL_ITU_REC601_EXT,
+ EGL_YUV_NARROW_RANGE_EXT,
+ EGL_YUV_CHROMA_SITING_0_EXT,
+ EGL_YUV_CHROMA_SITING_0_EXT,
+ &error,
+ NULL);
+}
+
+static __DRIimage *
+droid_create_image_from_prime_fds(_EGLDisplay *disp,
+ struct ANativeWindowBuffer *buf)
+{
+ struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);
+ int pitches[4] = { 0 }, offsets[4] = { 0 };
+ unsigned error;
+ int num_fds;
+ int fds[3];
+
+ num_fds = get_native_buffer_fds(buf, fds);
+ if (num_fds == 0)
+ return NULL;
+
+ if (is_yuv(buf->format)) {
+ __DRIimage *image;
+
+ image = droid_create_image_from_prime_fds_yuv(disp, buf, num_fds, fds);
+ /*
+ * HACK: https://issuetracker.google.com/32077885
+ * There is no API available to properly query the IMPLEMENTATION_DEFINED
+ * format. As a workaround we rely here on gralloc allocating either
+ * an arbitrary YCbCr 4:2:0 or RGBX_8888, with the latter being recognized
+ * by lock_ycbcr failing.
+ */
+ if (image || buf->format != HAL_PIXEL_FORMAT_IMPLEMENTATION_DEFINED)
+ return image;
+ }
+
+ /*
+ * Non-YUV formats could *also* have multiple planes, such as ancillary
+ * color compression state buffer, but the rest of the code isn't ready
+ * yet to deal with modifiers:
+ */
+ assert(num_fds == 1);
+
+ const int fourcc = get_fourcc(buf->format);
+ if (fourcc == -1) {
+ _eglError(EGL_BAD_PARAMETER, "eglCreateEGLImageKHR");
+ return NULL;
+ }
+
+ pitches[0] = buf->stride * get_format_bpp(buf->format);
+ if (pitches[0] == 0) {
+ _eglError(EGL_BAD_PARAMETER, "eglCreateEGLImageKHR");
+ return NULL;
+ }
+
+ return dri2_dpy->image->createImageFromDmaBufs(dri2_dpy->dri_screen,
+ buf->width, buf->height, fourcc,
+ fds, num_fds, pitches, offsets,
+ EGL_ITU_REC601_EXT,
+ EGL_YUV_NARROW_RANGE_EXT,
+ EGL_YUV_CHROMA_SITING_0_EXT,
+ EGL_YUV_CHROMA_SITING_0_EXT,
+ &error,
+ NULL);
+}
+
+/* More recent CrOS gralloc has a perform op that fills out the struct below
+ * with canonical information about the buffer and its modifier, planes,
+ * offsets and strides. If we have this, we can skip straight to
+ * createImageFromDmaBufs2() and avoid all the guessing and recalculations.
+ * This also gives us the modifier and plane offsets/strides for multiplanar
+ * compressed buffers (eg Intel CCS buffers) in order to make that work in Android.
+ */
+
+static const char cros_gralloc_module_name[] = "CrOS Gralloc";
+
+#define CROS_GRALLOC_DRM_GET_BUFFER_INFO 4
+
+struct cros_gralloc0_buffer_info {
+ uint32_t drm_fourcc;
+ int num_fds;
+ int fds[4];
+ uint64_t modifier;
+ int offset[4];
+ int stride[4];
+};
+
+static __DRIimage *
+droid_create_image_from_cros_info(_EGLDisplay *disp,
+ struct ANativeWindowBuffer *buf)
+{
+ struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);
+ struct cros_gralloc0_buffer_info info;
+ unsigned error;
+
+ if (strcmp(dri2_dpy->gralloc->common.name, cros_gralloc_module_name) == 0 &&
+ dri2_dpy->gralloc->perform &&
+ dri2_dpy->image->base.version >= 15 &&
+ dri2_dpy->image->createImageFromDmaBufs2 != NULL &&
+ dri2_dpy->gralloc->perform(dri2_dpy->gralloc,
+ CROS_GRALLOC_DRM_GET_BUFFER_INFO,
+ buf->handle, &info) == 0) {
+ return dri2_dpy->image->createImageFromDmaBufs2(dri2_dpy->dri_screen,
+ buf->width, buf->height,
+ info.drm_fourcc, info.modifier,
+ info.fds, info.num_fds,
+ info.stride, info.offset,
+ EGL_ITU_REC601_EXT,
+ EGL_YUV_FULL_RANGE_EXT,
+ EGL_YUV_CHROMA_SITING_0_EXT,
+ EGL_YUV_CHROMA_SITING_0_EXT,
+ &error,
+ NULL);
+ }
+
+ return NULL;
+}
+
+static __DRIimage *
+droid_create_image_from_native_buffer(_EGLDisplay *disp,
+ struct ANativeWindowBuffer *buf)
+{
+ __DRIimage *dri_image;
+
+ dri_image = droid_create_image_from_cros_info(disp, buf);
+ if (dri_image)
+ return dri_image;
+
+ return droid_create_image_from_prime_fds(disp, buf);
+}
+
static EGLBoolean
droid_window_dequeue_buffer(struct dri2_egl_surface *dri2_surf)
{
close(fence_fd);
}
- dri2_surf->buffer->common.incRef(&dri2_surf->buffer->common);
-
/* Record all the buffers created by ANativeWindow and update back buffer
* for updating buffer's age in swap_buffers.
*/
EGLBoolean updated = EGL_FALSE;
- for (int i = 0; i < ARRAY_SIZE(dri2_surf->color_buffers); i++) {
+ for (int i = 0; i < dri2_surf->color_buffers_count; i++) {
if (!dri2_surf->color_buffers[i].buffer) {
dri2_surf->color_buffers[i].buffer = dri2_surf->buffer;
}
/* In case of all the buffers were recreated by ANativeWindow, reset
* the color_buffers
*/
- for (int i = 0; i < ARRAY_SIZE(dri2_surf->color_buffers); i++) {
+ for (int i = 0; i < dri2_surf->color_buffers_count; i++) {
dri2_surf->color_buffers[i].buffer = NULL;
dri2_surf->color_buffers[i].age = 0;
}
dri2_surf->window->queueBuffer(dri2_surf->window, dri2_surf->buffer,
fence_fd);
- dri2_surf->buffer->common.decRef(&dri2_surf->buffer->common);
dri2_surf->buffer = NULL;
dri2_surf->back = NULL;
dri2_surf->out_fence_fd = -1;
ret = dri2_surf->window->cancelBuffer(dri2_surf->window,
dri2_surf->buffer, fence_fd);
+ dri2_surf->buffer = NULL;
if (ret < 0) {
_eglLog(_EGL_WARNING, "ANativeWindow::cancelBuffer failed");
dri2_surf->base.Lost = EGL_TRUE;
droid_set_shared_buffer_mode(_EGLDisplay *disp, _EGLSurface *surf, bool mode)
{
#if ANDROID_API_LEVEL >= 24
- struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);
struct dri2_egl_surface *dri2_surf = dri2_egl_surface(surf);
struct ANativeWindow *window = dri2_surf->window;
}
static _EGLSurface *
-droid_create_surface(_EGLDriver *drv, _EGLDisplay *disp, EGLint type,
- _EGLConfig *conf, void *native_window,
- const EGLint *attrib_list)
+droid_create_surface(_EGLDisplay *disp, EGLint type, _EGLConfig *conf,
+ void *native_window, const EGLint *attrib_list)
{
- __DRIcreateNewDrawableFunc createNewDrawable;
struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);
struct dri2_egl_config *dri2_conf = dri2_egl_config(conf);
struct dri2_egl_surface *dri2_surf;
return NULL;
}
- if (!dri2_init_surface(&dri2_surf->base, disp, type, conf, attrib_list, true))
+ if (!dri2_init_surface(&dri2_surf->base, disp, type, conf, attrib_list,
+ true, native_window))
goto cleanup_surface;
if (type == EGL_WINDOW_BIT) {
int format;
+ int buffer_count;
+ int min_buffer_count, max_buffer_count;
+
+ /* Prefer triple buffering for performance reasons. */
+ const int preferred_buffer_count = 3;
if (window->common.magic != ANDROID_NATIVE_WINDOW_MAGIC) {
_eglError(EGL_BAD_NATIVE_WINDOW, "droid_create_surface");
goto cleanup_surface;
}
+ /* Query ANativeWindow for MIN_UNDEQUEUED_BUFFER, minimum amount
+ * of undequeued buffers.
+ */
+ if (window->query(window, NATIVE_WINDOW_MIN_UNDEQUEUED_BUFFERS,
+ &min_buffer_count)) {
+ _eglError(EGL_BAD_NATIVE_WINDOW, "droid_create_surface");
+ goto cleanup_surface;
+ }
+
+ /* Query for maximum buffer count, application can set this
+ * to limit the total amount of buffers.
+ */
+ if (window->query(window, NATIVE_WINDOW_MAX_BUFFER_COUNT,
+ &max_buffer_count)) {
+ _eglError(EGL_BAD_NATIVE_WINDOW, "droid_create_surface");
+ goto cleanup_surface;
+ }
+
+ /* Clamp preferred between minimum (min undequeued + 1 dequeued)
+ * and maximum.
+ */
+ buffer_count = CLAMP(preferred_buffer_count, min_buffer_count + 1,
+ max_buffer_count);
+
+ if (native_window_set_buffer_count(window, buffer_count)) {
+ _eglError(EGL_BAD_NATIVE_WINDOW, "droid_create_surface");
+ goto cleanup_surface;
+ }
+ dri2_surf->color_buffers = calloc(buffer_count,
+ sizeof(*dri2_surf->color_buffers));
+ if (!dri2_surf->color_buffers) {
+ _eglError(EGL_BAD_ALLOC, "droid_create_surface");
+ goto cleanup_surface;
+ }
+ dri2_surf->color_buffers_count = buffer_count;
+
if (format != dri2_conf->base.NativeVisualID) {
_eglLog(_EGL_WARNING, "Native format mismatch: 0x%x != 0x%x",
format, dri2_conf->base.NativeVisualID);
window->query(window, NATIVE_WINDOW_WIDTH, &dri2_surf->base.Width);
window->query(window, NATIVE_WINDOW_HEIGHT, &dri2_surf->base.Height);
+
+ uint32_t usage = strcmp(dri2_dpy->driver_name, "kms_swrast") == 0
+ ? GRALLOC_USAGE_SW_READ_OFTEN | GRALLOC_USAGE_SW_WRITE_OFTEN
+ : GRALLOC_USAGE_HW_RENDER;
+ native_window_set_usage(window, usage);
}
config = dri2_get_dri_config(dri2_conf, type,
goto cleanup_surface;
}
- if (dri2_dpy->image_driver)
- createNewDrawable = dri2_dpy->image_driver->createNewDrawable;
- else
- createNewDrawable = dri2_dpy->dri2->createNewDrawable;
-
- dri2_surf->dri_drawable = (*createNewDrawable)(dri2_dpy->dri_screen, config,
- dri2_surf);
- if (dri2_surf->dri_drawable == NULL) {
- _eglError(EGL_BAD_ALLOC, "createNewDrawable");
+ if (!dri2_create_drawable(dri2_dpy, config, dri2_surf, dri2_surf))
goto cleanup_surface;
- }
if (window) {
window->common.incRef(&window->common);
return &dri2_surf->base;
cleanup_surface:
+ if (dri2_surf->color_buffers_count)
+ free(dri2_surf->color_buffers);
free(dri2_surf);
return NULL;
}
static _EGLSurface *
-droid_create_window_surface(_EGLDriver *drv, _EGLDisplay *disp,
- _EGLConfig *conf, void *native_window,
- const EGLint *attrib_list)
+droid_create_window_surface(_EGLDisplay *disp, _EGLConfig *conf,
+ void *native_window, const EGLint *attrib_list)
{
- return droid_create_surface(drv, disp, EGL_WINDOW_BIT, conf,
+ return droid_create_surface(disp, EGL_WINDOW_BIT, conf,
native_window, attrib_list);
}
static _EGLSurface *
-droid_create_pbuffer_surface(_EGLDriver *drv, _EGLDisplay *disp,
- _EGLConfig *conf, const EGLint *attrib_list)
+droid_create_pbuffer_surface(_EGLDisplay *disp, _EGLConfig *conf,
+ const EGLint *attrib_list)
{
- return droid_create_surface(drv, disp, EGL_PBUFFER_BIT, conf,
- NULL, attrib_list);
+ return droid_create_surface(disp, EGL_PBUFFER_BIT, conf,
+ NULL, attrib_list);
}
static EGLBoolean
-droid_destroy_surface(_EGLDriver *drv, _EGLDisplay *disp, _EGLSurface *surf)
+droid_destroy_surface(_EGLDisplay *disp, _EGLSurface *surf)
{
struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);
struct dri2_egl_surface *dri2_surf = dri2_egl_surface(surf);
dri2_dpy->core->destroyDrawable(dri2_surf->dri_drawable);
dri2_fini_surface(surf);
+ free(dri2_surf->color_buffers);
free(dri2_surf);
return EGL_TRUE;
}
static EGLBoolean
-droid_swap_interval(_EGLDriver *drv, _EGLDisplay *dpy,
- _EGLSurface *surf, EGLint interval)
+droid_swap_interval(_EGLDisplay *disp, _EGLSurface *surf, EGLint interval)
{
struct dri2_egl_surface *dri2_surf = dri2_egl_surface(surf);
struct ANativeWindow *window = dri2_surf->window;
static int
get_back_bo(struct dri2_egl_surface *dri2_surf)
{
- struct dri2_egl_display *dri2_dpy =
- dri2_egl_display(dri2_surf->base.Resource.Display);
- int fourcc, pitch;
- int offset = 0, fd;
+ _EGLDisplay *disp = dri2_surf->base.Resource.Display;
if (dri2_surf->dri_image_back)
return 0;
return -1;
}
- fd = get_native_buffer_fd(dri2_surf->buffer);
- if (fd < 0) {
- _eglLog(_EGL_WARNING, "Could not get native buffer FD");
- return -1;
- }
-
- fourcc = get_fourcc(dri2_surf->buffer->format);
-
- pitch = dri2_surf->buffer->stride *
- get_format_bpp(dri2_surf->buffer->format);
-
- if (fourcc == -1 || pitch == 0) {
- _eglLog(_EGL_WARNING, "Invalid buffer fourcc(%x) or pitch(%d)",
- fourcc, pitch);
- return -1;
- }
-
dri2_surf->dri_image_back =
- dri2_dpy->image->createImageFromFds(dri2_dpy->dri_screen,
- dri2_surf->base.Width,
- dri2_surf->base.Height,
- fourcc,
- &fd,
- 1,
- &pitch,
- &offset,
- dri2_surf);
+ droid_create_image_from_native_buffer(disp, dri2_surf->buffer);
if (!dri2_surf->dri_image_back) {
_eglLog(_EGL_WARNING, "failed to create DRI image from FD");
return -1;
}
static EGLint
-droid_query_buffer_age(_EGLDriver *drv,
- _EGLDisplay *disp, _EGLSurface *surface)
+droid_query_buffer_age(_EGLDisplay *disp, _EGLSurface *surface)
{
struct dri2_egl_surface *dri2_surf = dri2_egl_surface(surface);
}
static EGLBoolean
-droid_swap_buffers(_EGLDriver *drv, _EGLDisplay *disp, _EGLSurface *draw)
+droid_swap_buffers(_EGLDisplay *disp, _EGLSurface *draw)
{
struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);
struct dri2_egl_surface *dri2_surf = dri2_egl_surface(draw);
-
- if (dri2_surf->base.Type != EGL_WINDOW_BIT)
- return EGL_TRUE;
-
const bool has_mutable_rb = _eglSurfaceHasMutableRenderBuffer(draw);
/* From the EGL_KHR_mutable_render_buffer spec (v12):
return EGL_TRUE;
}
- for (int i = 0; i < ARRAY_SIZE(dri2_surf->color_buffers); i++) {
+ for (int i = 0; i < dri2_surf->color_buffers_count; i++) {
if (dri2_surf->color_buffers[i].age > 0)
dri2_surf->color_buffers[i].age++;
}
return EGL_TRUE;
}
-#if ANDROID_API_LEVEL >= 23
-static EGLBoolean
-droid_set_damage_region(_EGLDriver *drv,
- _EGLDisplay *disp,
- _EGLSurface *draw, const EGLint* rects, EGLint n_rects)
-{
- struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);
- struct dri2_egl_surface *dri2_surf = dri2_egl_surface(draw);
- android_native_rect_t* droid_rects = NULL;
- int ret;
-
- if (n_rects == 0)
- return EGL_TRUE;
-
- droid_rects = malloc(n_rects * sizeof(android_native_rect_t));
- if (droid_rects == NULL)
- return _eglError(EGL_BAD_ALLOC, "eglSetDamageRegionKHR");
-
- for (EGLint num_drects = 0; num_drects < n_rects; num_drects++) {
- EGLint i = num_drects * 4;
- droid_rects[num_drects].left = rects[i];
- droid_rects[num_drects].bottom = rects[i + 1];
- droid_rects[num_drects].right = rects[i] + rects[i + 2];
- droid_rects[num_drects].top = rects[i + 1] + rects[i + 3];
- }
-
- /*
- * XXX/TODO: Need to check for other return values
- */
-
- ret = native_window_set_surface_damage(dri2_surf->window, droid_rects, n_rects);
- free(droid_rects);
-
- return ret == 0 ? EGL_TRUE : EGL_FALSE;
-}
-#endif
-
-static _EGLImage *
-droid_create_image_from_prime_fd_yuv(_EGLDisplay *disp, _EGLContext *ctx,
- struct ANativeWindowBuffer *buf, int fd)
-{
- struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);
- struct android_ycbcr ycbcr;
- size_t offsets[3];
- size_t pitches[3];
- int is_ycrcb;
- int fourcc;
- int ret;
-
- if (!dri2_dpy->gralloc->lock_ycbcr) {
- _eglLog(_EGL_WARNING, "Gralloc does not support lock_ycbcr");
- return NULL;
- }
-
- memset(&ycbcr, 0, sizeof(ycbcr));
- ret = dri2_dpy->gralloc->lock_ycbcr(dri2_dpy->gralloc, buf->handle,
- 0, 0, 0, 0, 0, &ycbcr);
- if (ret) {
- /* HACK: See droid_create_image_from_prime_fd() and
- * https://issuetracker.google.com/32077885.*/
- if (buf->format == HAL_PIXEL_FORMAT_IMPLEMENTATION_DEFINED)
- return NULL;
-
- _eglLog(_EGL_WARNING, "gralloc->lock_ycbcr failed: %d", ret);
- return NULL;
- }
- dri2_dpy->gralloc->unlock(dri2_dpy->gralloc, buf->handle);
-
- /* When lock_ycbcr's usage argument contains no SW_READ/WRITE flags
- * it will return the .y/.cb/.cr pointers based on a NULL pointer,
- * so they can be interpreted as offsets. */
- offsets[0] = (size_t)ycbcr.y;
- /* We assume here that all the planes are located in one DMA-buf. */
- is_ycrcb = (size_t)ycbcr.cr < (size_t)ycbcr.cb;
- if (is_ycrcb) {
- offsets[1] = (size_t)ycbcr.cr;
- offsets[2] = (size_t)ycbcr.cb;
- } else {
- offsets[1] = (size_t)ycbcr.cb;
- offsets[2] = (size_t)ycbcr.cr;
- }
-
- /* .ystride is the line length (in bytes) of the Y plane,
- * .cstride is the line length (in bytes) of any of the remaining
- * Cb/Cr/CbCr planes, assumed to be the same for Cb and Cr for fully
- * planar formats. */
- pitches[0] = ycbcr.ystride;
- pitches[1] = pitches[2] = ycbcr.cstride;
-
- /* .chroma_step is the byte distance between the same chroma channel
- * values of subsequent pixels, assumed to be the same for Cb and Cr. */
- fourcc = get_fourcc_yuv(buf->format, is_ycrcb, ycbcr.chroma_step);
- if (fourcc == -1) {
- _eglLog(_EGL_WARNING, "unsupported YUV format, native = %x, is_ycrcb = %d, chroma_step = %d",
- buf->format, is_ycrcb, ycbcr.chroma_step);
- return NULL;
- }
-
- if (ycbcr.chroma_step == 2) {
- /* Semi-planar Y + CbCr or Y + CrCb format. */
- const EGLint attr_list_2plane[] = {
- EGL_WIDTH, buf->width,
- EGL_HEIGHT, buf->height,
- EGL_LINUX_DRM_FOURCC_EXT, fourcc,
- EGL_DMA_BUF_PLANE0_FD_EXT, fd,
- EGL_DMA_BUF_PLANE0_PITCH_EXT, pitches[0],
- EGL_DMA_BUF_PLANE0_OFFSET_EXT, offsets[0],
- EGL_DMA_BUF_PLANE1_FD_EXT, fd,
- EGL_DMA_BUF_PLANE1_PITCH_EXT, pitches[1],
- EGL_DMA_BUF_PLANE1_OFFSET_EXT, offsets[1],
- EGL_NONE, 0
- };
-
- return dri2_create_image_dma_buf(disp, ctx, NULL, attr_list_2plane);
- } else {
- /* Fully planar Y + Cb + Cr or Y + Cr + Cb format. */
- const EGLint attr_list_3plane[] = {
- EGL_WIDTH, buf->width,
- EGL_HEIGHT, buf->height,
- EGL_LINUX_DRM_FOURCC_EXT, fourcc,
- EGL_DMA_BUF_PLANE0_FD_EXT, fd,
- EGL_DMA_BUF_PLANE0_PITCH_EXT, pitches[0],
- EGL_DMA_BUF_PLANE0_OFFSET_EXT, offsets[0],
- EGL_DMA_BUF_PLANE1_FD_EXT, fd,
- EGL_DMA_BUF_PLANE1_PITCH_EXT, pitches[1],
- EGL_DMA_BUF_PLANE1_OFFSET_EXT, offsets[1],
- EGL_DMA_BUF_PLANE2_FD_EXT, fd,
- EGL_DMA_BUF_PLANE2_PITCH_EXT, pitches[2],
- EGL_DMA_BUF_PLANE2_OFFSET_EXT, offsets[2],
- EGL_NONE, 0
- };
-
- return dri2_create_image_dma_buf(disp, ctx, NULL, attr_list_3plane);
- }
-}
-
-static _EGLImage *
-droid_create_image_from_prime_fd(_EGLDisplay *disp, _EGLContext *ctx,
- struct ANativeWindowBuffer *buf, int fd)
+#ifdef HAVE_DRM_GRALLOC
+static int get_format(int format)
{
- unsigned int pitch;
-
- if (is_yuv(buf->format)) {
- _EGLImage *image;
-
- image = droid_create_image_from_prime_fd_yuv(disp, ctx, buf, fd);
+ switch (format) {
+ case HAL_PIXEL_FORMAT_BGRA_8888: return __DRI_IMAGE_FORMAT_ARGB8888;
+ case HAL_PIXEL_FORMAT_RGB_565: return __DRI_IMAGE_FORMAT_RGB565;
+ case HAL_PIXEL_FORMAT_RGBA_8888: return __DRI_IMAGE_FORMAT_ABGR8888;
+ case HAL_PIXEL_FORMAT_IMPLEMENTATION_DEFINED:
/*
- * HACK: https://issuetracker.google.com/32077885
- * There is no API available to properly query the IMPLEMENTATION_DEFINED
- * format. As a workaround we rely here on gralloc allocating either
- * an arbitrary YCbCr 4:2:0 or RGBX_8888, with the latter being recognized
- * by lock_ycbcr failing.
+ * HACK: Hardcode this to RGBX_8888 as per cros_gralloc hack.
+ * TODO: Revert this once https://issuetracker.google.com/32077885 is fixed.
*/
- if (image || buf->format != HAL_PIXEL_FORMAT_IMPLEMENTATION_DEFINED)
- return image;
- }
-
- const int fourcc = get_fourcc(buf->format);
- if (fourcc == -1) {
- _eglError(EGL_BAD_PARAMETER, "eglCreateEGLImageKHR");
- return NULL;
- }
-
- pitch = buf->stride * get_format_bpp(buf->format);
- if (pitch == 0) {
- _eglError(EGL_BAD_PARAMETER, "eglCreateEGLImageKHR");
- return NULL;
+ case HAL_PIXEL_FORMAT_RGBX_8888: return __DRI_IMAGE_FORMAT_XBGR8888;
+ case HAL_PIXEL_FORMAT_RGBA_FP16: return __DRI_IMAGE_FORMAT_ABGR16161616F;
+ case HAL_PIXEL_FORMAT_RGBA_1010102: return __DRI_IMAGE_FORMAT_ABGR2101010;
+ default:
+ _eglLog(_EGL_WARNING, "unsupported native buffer format 0x%x", format);
}
-
- const EGLint attr_list[] = {
- EGL_WIDTH, buf->width,
- EGL_HEIGHT, buf->height,
- EGL_LINUX_DRM_FOURCC_EXT, fourcc,
- EGL_DMA_BUF_PLANE0_FD_EXT, fd,
- EGL_DMA_BUF_PLANE0_PITCH_EXT, pitch,
- EGL_DMA_BUF_PLANE0_OFFSET_EXT, 0,
- EGL_NONE, 0
- };
-
- return dri2_create_image_dma_buf(disp, ctx, NULL, attr_list);
+ return -1;
}
-#ifdef HAVE_DRM_GRALLOC
static _EGLImage *
-droid_create_image_from_name(_EGLDisplay *disp, _EGLContext *ctx,
+droid_create_image_from_name(_EGLDisplay *disp,
struct ANativeWindowBuffer *buf)
{
struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);
- struct dri2_egl_image *dri2_img;
+ __DRIimage *dri_image;
int name;
int format;
if (format == -1)
return NULL;
- dri2_img = calloc(1, sizeof(*dri2_img));
- if (!dri2_img) {
- _eglError(EGL_BAD_ALLOC, "droid_create_image_mesa_drm");
- return NULL;
- }
-
- _eglInitImage(&dri2_img->base, disp);
-
- dri2_img->dri_image =
+ return
dri2_dpy->image->createImageFromName(dri2_dpy->dri_screen,
buf->width,
buf->height,
name,
buf->stride,
dri2_img);
- if (!dri2_img->dri_image) {
- free(dri2_img);
- _eglError(EGL_BAD_ALLOC, "droid_create_image_mesa_drm");
- return NULL;
- }
-
- return &dri2_img->base;
}
#endif /* HAVE_DRM_GRALLOC */
static EGLBoolean
-droid_query_surface(_EGLDriver *drv, _EGLDisplay *dpy, _EGLSurface *surf,
+droid_query_surface(_EGLDisplay *disp, _EGLSurface *surf,
EGLint attribute, EGLint *value)
{
struct dri2_egl_surface *dri2_surf = dri2_egl_surface(surf);
default:
break;
}
- return _eglQuerySurface(drv, dpy, surf, attribute, value);
+ return _eglQuerySurface(disp, surf, attribute, value);
}
static _EGLImage *
_EGLContext *ctx,
struct ANativeWindowBuffer *buf)
{
- int fd;
-
if (ctx != NULL) {
/* From the EGL_ANDROID_image_native_buffer spec:
*
return NULL;
}
- fd = get_native_buffer_fd(buf);
- if (fd >= 0)
- return droid_create_image_from_prime_fd(disp, ctx, buf, fd);
+ __DRIimage *dri_image =
+ droid_create_image_from_native_buffer(disp, buf);
#ifdef HAVE_DRM_GRALLOC
- return droid_create_image_from_name(disp, ctx, buf);
-#else
- return NULL;
+ if (dri_image == NULL)
+ dri_image = droid_create_image_from_name(disp, buf);
#endif
+
+ if (dri_image)
+ return dri2_create_image_from_dri(disp, dri_image);
+
+ return NULL;
}
static _EGLImage *
-droid_create_image_khr(_EGLDriver *drv, _EGLDisplay *disp,
- _EGLContext *ctx, EGLenum target,
- EGLClientBuffer buffer, const EGLint *attr_list)
+droid_create_image_khr(_EGLDisplay *disp, _EGLContext *ctx, EGLenum target,
+ EGLClientBuffer buffer, const EGLint *attr_list)
{
switch (target) {
case EGL_NATIVE_BUFFER_ANDROID:
return dri2_create_image_android_native_buffer(disp, ctx,
(struct ANativeWindowBuffer *) buffer);
default:
- return dri2_create_image_khr(drv, disp, ctx, target, buffer, attr_list);
+ return dri2_create_image_khr(disp, ctx, target, buffer, attr_list);
}
}
}
static EGLBoolean
-droid_add_configs_for_visuals(_EGLDriver *drv, _EGLDisplay *dpy)
+droid_add_configs_for_visuals(_EGLDisplay *disp)
{
- struct dri2_egl_display *dri2_dpy = dri2_egl_display(dpy);
+ struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);
static const struct {
int format;
- unsigned int rgba_masks[4];
+ int rgba_shifts[4];
+ unsigned int rgba_sizes[4];
} visuals[] = {
- { HAL_PIXEL_FORMAT_RGBA_8888, { 0x000000ff, 0x0000ff00, 0x00ff0000, 0xff000000 } },
- { HAL_PIXEL_FORMAT_RGBX_8888, { 0x000000ff, 0x0000ff00, 0x00ff0000, 0x00000000 } },
- { HAL_PIXEL_FORMAT_RGB_565, { 0x0000f800, 0x000007e0, 0x0000001f, 0x00000000 } },
- { HAL_PIXEL_FORMAT_BGRA_8888, { 0x00ff0000, 0x0000ff00, 0x000000ff, 0xff000000 } },
+ { HAL_PIXEL_FORMAT_RGBA_8888, { 0, 8, 16, 24 }, { 8, 8, 8, 8 } },
+ { HAL_PIXEL_FORMAT_RGBX_8888, { 0, 8, 16, -1 }, { 8, 8, 8, 0 } },
+ { HAL_PIXEL_FORMAT_RGB_565, { 11, 5, 0, -1 }, { 5, 6, 5, 0 } },
+ /* This must be after HAL_PIXEL_FORMAT_RGBA_8888, we only keep BGRA
+ * visual if it turns out RGBA visual is not available.
+ */
+ { HAL_PIXEL_FORMAT_BGRA_8888, { 16, 8, 0, 24 }, { 8, 8, 8, 8 } },
};
unsigned int format_count[ARRAY_SIZE(visuals)] = { 0 };
* (chadversary) testing on Android Nougat, this was good enough to pacify
* the buggy clients.
*/
+ bool has_rgba = false;
for (int i = 0; i < ARRAY_SIZE(visuals); i++) {
+ /* Only enable BGRA configs when RGBA is not available. BGRA configs are
+ * buggy on stock Android.
+ */
+ if (visuals[i].format == HAL_PIXEL_FORMAT_BGRA_8888 && has_rgba)
+ continue;
for (int j = 0; dri2_dpy->driver_configs[j]; j++) {
const EGLint surface_type = EGL_WINDOW_BIT | EGL_PBUFFER_BIT;
};
struct dri2_egl_config *dri2_conf =
- dri2_add_config(dpy, dri2_dpy->driver_configs[j],
+ dri2_add_config(disp, dri2_dpy->driver_configs[j],
config_count + 1, surface_type, config_attrs,
- visuals[i].rgba_masks);
+ visuals[i].rgba_shifts, visuals[i].rgba_sizes);
if (dri2_conf) {
if (dri2_conf->base.ConfigID == config_count + 1)
config_count++;
format_count[i]++;
}
}
+ if (visuals[i].format == HAL_PIXEL_FORMAT_RGBA_8888 && format_count[i])
+ has_rgba = true;
}
for (int i = 0; i < ARRAY_SIZE(format_count); i++) {
return (config_count != 0);
}
-static EGLBoolean
-droid_probe_device(_EGLDisplay *disp);
-
-#ifdef HAVE_DRM_GRALLOC
-static EGLBoolean
-droid_open_device_drm_gralloc(_EGLDisplay *disp)
-{
- struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);
- int fd = -1, err = -EINVAL;
-
- if (dri2_dpy->gralloc->perform)
- err = dri2_dpy->gralloc->perform(dri2_dpy->gralloc,
- GRALLOC_MODULE_PERFORM_GET_DRM_FD,
- &fd);
- if (err || fd < 0) {
- _eglLog(_EGL_WARNING, "fail to get drm fd");
- return EGL_FALSE;
- }
-
- dri2_dpy->fd = fcntl(fd, F_DUPFD_CLOEXEC, 3);
- if (dri2_dpy->fd < 0)
- return EGL_FALSE;
-
- return droid_probe_device(disp);
-}
-#endif /* HAVE_DRM_GRALLOC */
-
static const struct dri2_egl_display_vtbl droid_display_vtbl = {
.authenticate = NULL,
.create_window_surface = droid_create_window_surface,
- .create_pixmap_surface = dri2_fallback_create_pixmap_surface,
.create_pbuffer_surface = droid_create_pbuffer_surface,
.destroy_surface = droid_destroy_surface,
.create_image = droid_create_image_khr,
.swap_buffers = droid_swap_buffers,
- .swap_buffers_with_damage = dri2_fallback_swap_buffers_with_damage, /* Android implements the function */
- .swap_buffers_region = dri2_fallback_swap_buffers_region,
.swap_interval = droid_swap_interval,
-#if ANDROID_API_LEVEL >= 23
- .set_damage_region = droid_set_damage_region,
-#else
- .set_damage_region = dri2_fallback_set_damage_region,
-#endif
- .post_sub_buffer = dri2_fallback_post_sub_buffer,
- .copy_buffers = dri2_fallback_copy_buffers,
.query_buffer_age = droid_query_buffer_age,
.query_surface = droid_query_surface,
- .create_wayland_buffer_from_image = dri2_fallback_create_wayland_buffer_from_image,
- .get_sync_values = dri2_fallback_get_sync_values,
.get_dri_drawable = dri2_surface_get_dri_drawable,
.set_shared_buffer_mode = droid_set_shared_buffer_mode,
};
.getBuffersWithFormat = droid_get_buffers_with_format,
.getCapability = droid_get_capability,
};
-#endif /* HAVE_DRM_GRALLOC */
-
-static const __DRIimageLoaderExtension droid_image_loader_extension = {
- .base = { __DRI_IMAGE_LOADER, 2 },
-
- .getBuffers = droid_image_get_buffers,
- .flushFrontBuffer = droid_flush_front_buffer,
- .getCapability = droid_get_capability,
-};
-#ifdef HAVE_DRM_GRALLOC
static const __DRIextension *droid_dri2_loader_extensions[] = {
&droid_dri2_loader_extension.base,
&image_lookup_extension.base,
};
#endif /* HAVE_DRM_GRALLOC */
+static const __DRIimageLoaderExtension droid_image_loader_extension = {
+ .base = { __DRI_IMAGE_LOADER, 2 },
+
+ .getBuffers = droid_image_get_buffers,
+ .flushFrontBuffer = droid_flush_front_buffer,
+ .getCapability = droid_get_capability,
+};
+
static void
droid_display_shared_buffer(__DRIdrawable *driDrawable, int fence_fd,
void *loaderPrivate)
};
static EGLBoolean
-droid_load_driver(_EGLDisplay *disp)
+droid_load_driver(_EGLDisplay *disp, bool swrast)
{
- struct dri2_egl_display *dri2_dpy = disp->DriverData;
- const char *err;
+ struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);
dri2_dpy->driver_name = loader_get_driver_for_fd(dri2_dpy->fd);
if (dri2_dpy->driver_name == NULL)
return false;
- dri2_dpy->is_render_node = drmGetNodeTypeFromFd(dri2_dpy->fd) == DRM_NODE_RENDER;
-
- if (!dri2_dpy->is_render_node) {
#ifdef HAVE_DRM_GRALLOC
- /* Handle control nodes using __DRI_DRI2_LOADER extension and GEM names
- * for backwards compatibility with drm_gralloc. (Do not use on new
- * systems.) */
- dri2_dpy->loader_extensions = droid_dri2_loader_extensions;
- if (!dri2_load_driver(disp)) {
- err = "DRI2: failed to load driver";
- goto error;
- }
+ /* Handle control nodes using __DRI_DRI2_LOADER extension and GEM names
+ * for backwards compatibility with drm_gralloc. (Do not use on new
+ * systems.) */
+ dri2_dpy->loader_extensions = droid_dri2_loader_extensions;
+ if (!dri2_load_driver(disp)) {
+ goto error;
+ }
#else
- err = "DRI2: handle is not for a render node";
- goto error;
+ if (swrast) {
+ /* Use kms swrast only with vgem / virtio_gpu.
+ * virtio-gpu fallbacks to software rendering when 3D features
+ * are unavailable since 6c5ab.
+ */
+ if (strcmp(dri2_dpy->driver_name, "vgem") == 0 ||
+ strcmp(dri2_dpy->driver_name, "virtio_gpu") == 0) {
+ free(dri2_dpy->driver_name);
+ dri2_dpy->driver_name = strdup("kms_swrast");
+ } else {
+ goto error;
+ }
+ }
+
+ dri2_dpy->loader_extensions = droid_image_loader_extensions;
+ if (!dri2_load_driver_dri3(disp)) {
+ goto error;
+ }
#endif
- } else {
- dri2_dpy->loader_extensions = droid_image_loader_extensions;
- if (!dri2_load_driver_dri3(disp)) {
- err = "DRI3: failed to load driver";
- goto error;
- }
- }
return true;
}
static EGLBoolean
-droid_probe_device(_EGLDisplay *disp)
+droid_probe_device(_EGLDisplay *disp, bool swrast)
{
/* Check that the device is supported, by attempting to:
* - load the dri module
* - and, create a screen
*/
- if (!droid_load_driver(disp))
+ if (!droid_load_driver(disp, swrast))
return EGL_FALSE;
if (!dri2_create_screen(disp)) {
return EGL_TRUE;
}
+#ifdef HAVE_DRM_GRALLOC
static EGLBoolean
-droid_open_device(_EGLDisplay *disp)
+droid_open_device(_EGLDisplay *disp, bool swrast)
{
-#define MAX_DRM_DEVICES 32
+ struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);
+ int fd = -1, err = -EINVAL;
+
+ if (swrast)
+ return EGL_FALSE;
+
+ if (dri2_dpy->gralloc->perform)
+ err = dri2_dpy->gralloc->perform(dri2_dpy->gralloc,
+ GRALLOC_MODULE_PERFORM_GET_DRM_FD,
+ &fd);
+ if (err || fd < 0) {
+ _eglLog(_EGL_WARNING, "fail to get drm fd");
+ return EGL_FALSE;
+ }
+
+ dri2_dpy->fd = os_dupfd_cloexec(fd);
+ if (dri2_dpy->fd < 0)
+ return EGL_FALSE;
+
+ if (drmGetNodeTypeFromFd(dri2_dpy->fd) == DRM_NODE_RENDER)
+ return EGL_FALSE;
+
+ return droid_probe_device(disp, swrast);
+}
+#else
+static EGLBoolean
+droid_open_device(_EGLDisplay *disp, bool swrast)
+{
+#define MAX_DRM_DEVICES 64
struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);
drmDevicePtr device, devices[MAX_DRM_DEVICES] = { NULL };
int num_devices;
char *vendor_name = NULL;
char vendor_buf[PROPERTY_VALUE_MAX];
+#ifdef EGL_FORCE_RENDERNODE
+ const unsigned node_type = DRM_NODE_RENDER;
+#else
+ const unsigned node_type = swrast ? DRM_NODE_PRIMARY : DRM_NODE_RENDER;
+#endif
+
if (property_get("drm.gpu.vendor_name", vendor_buf, NULL) > 0)
vendor_name = vendor_buf;
for (int i = 0; i < num_devices; i++) {
device = devices[i];
- if (!(device->available_nodes & (1 << DRM_NODE_RENDER)))
+ if (!(device->available_nodes & (1 << node_type)))
continue;
- dri2_dpy->fd = loader_open_device(device->nodes[DRM_NODE_RENDER]);
+ dri2_dpy->fd = loader_open_device(device->nodes[node_type]);
if (dri2_dpy->fd < 0) {
_eglLog(_EGL_WARNING, "%s() Failed to open DRM device %s",
- __func__, device->nodes[DRM_NODE_RENDER]);
+ __func__, device->nodes[node_type]);
continue;
}
/* If the requested device matches - use it. Regardless if
* init fails, do not fall-back to any other device.
*/
- if (!droid_probe_device(disp)) {
+ if (!droid_probe_device(disp, false)) {
close(dri2_dpy->fd);
dri2_dpy->fd = -1;
}
break;
}
- if (droid_probe_device(disp))
+ if (droid_probe_device(disp, swrast))
break;
/* No explicit request - attempt the next device */
#undef MAX_DRM_DEVICES
}
+#endif
+
EGLBoolean
-dri2_initialize_android(_EGLDriver *drv, _EGLDisplay *disp)
+dri2_initialize_android(_EGLDisplay *disp)
{
+ _EGLDevice *dev;
+ bool device_opened = false;
struct dri2_egl_display *dri2_dpy;
const char *err;
int ret;
- /* Not supported yet */
- if (disp->Options.ForceSoftware)
- return EGL_FALSE;
-
- loader_set_logger(_eglLog);
-
dri2_dpy = calloc(1, sizeof(*dri2_dpy));
if (!dri2_dpy)
return _eglError(EGL_BAD_ALLOC, "eglInitialize");
}
disp->DriverData = (void *) dri2_dpy;
+ if (!disp->Options.ForceSoftware)
+ device_opened = droid_open_device(disp, false);
+ if (!device_opened)
+ device_opened = droid_open_device(disp, true);
-#ifdef HAVE_DRM_GRALLOC
- if (!droid_open_device_drm_gralloc(disp)) {
-#else
- if (!droid_open_device(disp)) {
-#endif
+ if (!device_opened) {
err = "DRI2: failed to open device";
goto cleanup;
}
+ dev = _eglAddDevice(dri2_dpy->fd, false);
+ if (!dev) {
+ err = "DRI2: failed to find EGLDevice";
+ goto cleanup;
+ }
+
+ disp->Device = dev;
+
if (!dri2_setup_extensions(disp)) {
err = "DRI2: failed to setup extensions";
goto cleanup;
disp->Extensions.ANDROID_framebuffer_target = EGL_TRUE;
disp->Extensions.ANDROID_image_native_buffer = EGL_TRUE;
disp->Extensions.ANDROID_recordable = EGL_TRUE;
- disp->Extensions.EXT_buffer_age = EGL_TRUE;
-#if ANDROID_API_LEVEL >= 23
- disp->Extensions.KHR_partial_update = EGL_TRUE;
-#endif
+
+ /* Querying buffer age requires a buffer to be dequeued. Without
+ * EGL_ANDROID_native_fence_sync, dequeue might call eglClientWaitSync and
+ * result in a deadlock (the lock is already held by eglQuerySurface).
+ */
+ if (disp->Extensions.ANDROID_native_fence_sync) {
+ disp->Extensions.EXT_buffer_age = EGL_TRUE;
+ } else {
+ /* disable KHR_partial_update that might have been enabled in
+ * dri2_setup_screen
+ */
+ disp->Extensions.KHR_partial_update = EGL_FALSE;
+ }
+
disp->Extensions.KHR_image = EGL_TRUE;
#if ANDROID_API_LEVEL >= 24
if (dri2_dpy->mutable_render_buffer &&
/* Create configs *after* enabling extensions because presence of DRI
* driver extensions can affect the capabilities of EGLConfigs.
*/
- if (!droid_add_configs_for_visuals(drv, disp)) {
+ if (!droid_add_configs_for_visuals(disp)) {
err = "DRI2: failed to add configs";
goto cleanup;
}