#include <unistd.h>
#include <dlfcn.h>
#include <xf86drm.h>
-#include <drm_fourcc.h>
+#include "drm-uapi/drm_fourcc.h"
#include <GL/gl.h> /* dri_interface needs GL types */
#include <GL/internal/dri_interface.h>
#include "gbmint.h"
#include "loader.h"
+#include "util/debug.h"
+#include "util/macros.h"
/* For importing wl_buffer */
#if HAVE_WAYLAND_PLATFORM
-#include "../../../egl/wayland/wayland-drm/wayland-drm.h"
-#endif
-
-#ifndef DRM_FORMAT_MOD_INVALID
-#define DRM_FORMAT_MOD_INVALID ((1ULL<<56) - 1)
-#endif
-
-#ifndef DRM_FORMAT_MOD_LINEAR
-#define DRM_FORMAT_MOD_LINEAR 0
+#include "wayland-drm.h"
#endif
static __DRIimage *
char *data,
void *loaderPrivate)
{
- return swrast_put_image2(driDrawable, op, x, y, width, height,
+ swrast_put_image2(driDrawable, op, x, y, width, height,
width * 4, data, loaderPrivate);
}
static const __DRIextension **
dri_open_driver(struct gbm_dri_device *dri)
{
- const __DRIextension **extensions = NULL;
- char path[PATH_MAX], *search_paths, *p, *next, *end;
- char *get_extensions_name;
-
- search_paths = NULL;
- /* don't allow setuid apps to use LIBGL_DRIVERS_PATH or GBM_DRIVERS_PATH */
- if (geteuid() == getuid()) {
- /* Read GBM_DRIVERS_PATH first for compatibility, but LIBGL_DRIVERS_PATH
- * is recommended over GBM_DRIVERS_PATH.
- */
- search_paths = getenv("GBM_DRIVERS_PATH");
-
- /* Read LIBGL_DRIVERS_PATH if GBM_DRIVERS_PATH was not set.
- * LIBGL_DRIVERS_PATH is recommended over GBM_DRIVERS_PATH.
- */
- if (search_paths == NULL) {
- search_paths = getenv("LIBGL_DRIVERS_PATH");
- }
- }
- if (search_paths == NULL)
- search_paths = DEFAULT_DRIVER_DIR;
-
/* Temporarily work around dri driver libs that need symbols in libglapi
* but don't automatically link it in.
*/
*/
dlopen("libglapi.so.0", RTLD_LAZY | RTLD_GLOBAL);
- dri->driver = NULL;
- end = search_paths + strlen(search_paths);
- for (p = search_paths; p < end && dri->driver == NULL; p = next + 1) {
- int len;
- next = strchr(p, ':');
- if (next == NULL)
- next = end;
-
- len = next - p;
-#if GLX_USE_TLS
- snprintf(path, sizeof path,
- "%.*s/tls/%s_dri.so", len, p, dri->driver_name);
- dri->driver = dlopen(path, RTLD_NOW | RTLD_GLOBAL);
-#endif
- if (dri->driver == NULL) {
- snprintf(path, sizeof path,
- "%.*s/%s_dri.so", len, p, dri->driver_name);
- dri->driver = dlopen(path, RTLD_NOW | RTLD_GLOBAL);
- }
- /* not need continue to loop all paths once the driver is found */
- if (dri->driver != NULL)
- break;
- }
-
- if (dri->driver == NULL) {
- fprintf(stderr, "gbm: failed to open any driver (search paths %s)\n",
- search_paths);
- fprintf(stderr, "gbm: Last dlopen error: %s\n", dlerror());
- return NULL;
- }
-
- get_extensions_name = loader_get_extensions_name(dri->driver_name);
- if (get_extensions_name) {
- const __DRIextension **(*get_extensions)(void);
-
- get_extensions = dlsym(dri->driver, get_extensions_name);
- free(get_extensions_name);
-
- if (get_extensions)
- extensions = get_extensions();
- }
-
- if (!extensions)
- extensions = dlsym(dri->driver, __DRI_DRIVER_EXTENSIONS);
- if (extensions == NULL) {
- fprintf(stderr, "gbm: driver exports no extensions (%s)", dlerror());
- dlclose(dri->driver);
- }
-
- return extensions;
+ static const char *search_path_vars[] = {
+ /* Read GBM_DRIVERS_PATH first for compatibility, but LIBGL_DRIVERS_PATH
+ * is recommended over GBM_DRIVERS_PATH.
+ */
+ "GBM_DRIVERS_PATH",
+ /* Read LIBGL_DRIVERS_PATH if GBM_DRIVERS_PATH was not set.
+ * LIBGL_DRIVERS_PATH is recommended over GBM_DRIVERS_PATH.
+ */
+ "LIBGL_DRIVERS_PATH",
+ NULL
+ };
+ return loader_open_driver(dri->driver_name, &dri->driver, search_path_vars);
}
static int
if (ret) {
fprintf(stderr, "failed to load driver: %s\n", dri->driver_name);
return ret;
- };
+ }
dri->loader_extensions = gbm_dri_screen_extensions;
return dri_screen_create_swrast(dri);
}
+static const struct gbm_dri_visual gbm_dri_visuals_table[] = {
+ {
+ GBM_FORMAT_R8, __DRI_IMAGE_FORMAT_R8,
+ { 0x000000ff, 0x00000000, 0x00000000, 0x00000000 },
+ },
+ {
+ GBM_FORMAT_GR88, __DRI_IMAGE_FORMAT_GR88,
+ { 0x000000ff, 0x0000ff00, 0x00000000, 0x00000000 },
+ },
+ {
+ GBM_FORMAT_ARGB1555, __DRI_IMAGE_FORMAT_ARGB1555,
+ { 0x00007c00, 0x000003e0, 0x0000001f, 0x00008000 },
+ },
+ {
+ GBM_FORMAT_RGB565, __DRI_IMAGE_FORMAT_RGB565,
+ { 0x0000f800, 0x000007e0, 0x0000001f, 0x00000000 },
+ },
+ {
+ GBM_FORMAT_XRGB8888, __DRI_IMAGE_FORMAT_XRGB8888,
+ { 0x00ff0000, 0x0000ff00, 0x000000ff, 0x00000000 },
+ },
+ {
+ GBM_FORMAT_ARGB8888, __DRI_IMAGE_FORMAT_ARGB8888,
+ { 0x00ff0000, 0x0000ff00, 0x000000ff, 0xff000000 },
+ },
+ {
+ GBM_FORMAT_XBGR8888, __DRI_IMAGE_FORMAT_XBGR8888,
+ { 0x000000ff, 0x0000ff00, 0x00ff0000, 0x00000000 },
+ },
+ {
+ GBM_FORMAT_ABGR8888, __DRI_IMAGE_FORMAT_ABGR8888,
+ { 0x000000ff, 0x0000ff00, 0x00ff0000, 0xff000000 },
+ },
+ {
+ GBM_FORMAT_XRGB2101010, __DRI_IMAGE_FORMAT_XRGB2101010,
+ { 0x3ff00000, 0x000ffc00, 0x000003ff, 0x00000000 },
+ },
+ {
+ GBM_FORMAT_ARGB2101010, __DRI_IMAGE_FORMAT_ARGB2101010,
+ { 0x3ff00000, 0x000ffc00, 0x000003ff, 0xc0000000 },
+ },
+ {
+ GBM_FORMAT_XBGR2101010, __DRI_IMAGE_FORMAT_XBGR2101010,
+ { 0x000003ff, 0x000ffc00, 0x3ff00000, 0x00000000 },
+ },
+ {
+ GBM_FORMAT_ABGR2101010, __DRI_IMAGE_FORMAT_ABGR2101010,
+ { 0x000003ff, 0x000ffc00, 0x3ff00000, 0xc0000000 },
+ },
+};
+
+static int
+gbm_format_to_dri_format(uint32_t gbm_format)
+{
+ int i;
+
+ gbm_format = gbm_format_canonicalize(gbm_format);
+ for (i = 0; i < ARRAY_SIZE(gbm_dri_visuals_table); i++) {
+ if (gbm_dri_visuals_table[i].gbm_format == gbm_format)
+ return gbm_dri_visuals_table[i].dri_image_format;
+ }
+
+ return 0;
+}
+
+static uint32_t
+gbm_dri_to_gbm_format(int dri_format)
+{
+ int i;
+
+ for (i = 0; i < ARRAY_SIZE(gbm_dri_visuals_table); i++) {
+ if (gbm_dri_visuals_table[i].dri_image_format == dri_format)
+ return gbm_dri_visuals_table[i].gbm_format;
+ }
+
+ return 0;
+}
+
static int
gbm_dri_is_format_supported(struct gbm_device *gbm,
uint32_t format,
uint32_t usage)
{
- switch (format) {
- case GBM_BO_FORMAT_XRGB8888:
- case GBM_FORMAT_XBGR8888:
- case GBM_FORMAT_XRGB8888:
- break;
- case GBM_BO_FORMAT_ARGB8888:
- case GBM_FORMAT_ARGB8888:
- if (usage & GBM_BO_USE_SCANOUT)
- return 0;
- break;
- default:
+ struct gbm_dri_device *dri = gbm_dri_device(gbm);
+ int count;
+
+ if ((usage & GBM_BO_USE_CURSOR) && (usage & GBM_BO_USE_RENDERING))
+ return 0;
+
+ format = gbm_format_canonicalize(format);
+ if (gbm_format_to_dri_format(format) == 0)
return 0;
+
+ /* If there is no query, fall back to the small table which was originally
+ * here. */
+ if (dri->image->base.version <= 15 || !dri->image->queryDmaBufModifiers) {
+ switch (format) {
+ case GBM_FORMAT_XRGB8888:
+ case GBM_FORMAT_ARGB8888:
+ case GBM_FORMAT_XBGR8888:
+ return 1;
+ default:
+ return 0;
+ }
}
- if (usage & GBM_BO_USE_CURSOR &&
- usage & GBM_BO_USE_RENDERING)
+ /* Check if the driver returns any modifiers for this format; since linear
+ * is counted as a modifier, we will have at least one modifier for any
+ * supported format. */
+ if (!dri->image->queryDmaBufModifiers(dri->screen, format, 0, NULL, NULL,
+ &count))
return 0;
- return 1;
+ return (count > 0);
+}
+
+static int
+gbm_dri_get_format_modifier_plane_count(struct gbm_device *gbm,
+ uint32_t format,
+ uint64_t modifier)
+{
+ struct gbm_dri_device *dri = gbm_dri_device(gbm);
+ uint64_t plane_count;
+
+ if (dri->image->base.version < 16 ||
+ !dri->image->queryDmaBufFormatModifierAttribs)
+ return -1;
+
+ format = gbm_format_canonicalize(format);
+ if (gbm_format_to_dri_format(format) == 0)
+ return -1;
+
+ if (!dri->image->queryDmaBufFormatModifierAttribs(
+ dri->screen, format, modifier,
+ __DRI_IMAGE_FORMAT_MODIFIER_ATTRIB_PLANE_COUNT, &plane_count))
+ return -1;
+
+ return plane_count;
}
static int
dri->image->queryImage(image, __DRI_IMAGE_ATTRIB_OFFSET, &offset);
dri->image->destroyImage(image);
} else {
+ assert(plane == 0);
dri->image->queryImage(bo->image, __DRI_IMAGE_ATTRIB_OFFSET, &offset);
}
free(bo);
}
-static uint32_t
-gbm_dri_to_gbm_format(uint32_t dri_format)
-{
- uint32_t ret = 0;
-
- switch (dri_format) {
- case __DRI_IMAGE_FORMAT_RGB565:
- ret = GBM_FORMAT_RGB565;
- break;
- case __DRI_IMAGE_FORMAT_XRGB8888:
- ret = GBM_FORMAT_XRGB8888;
- break;
- case __DRI_IMAGE_FORMAT_ARGB8888:
- ret = GBM_FORMAT_ARGB8888;
- break;
- case __DRI_IMAGE_FORMAT_XBGR8888:
- ret = GBM_FORMAT_XBGR8888;
- break;
- case __DRI_IMAGE_FORMAT_ABGR8888:
- ret = GBM_FORMAT_ABGR8888;
- break;
- default:
- ret = 0;
- break;
- }
-
- return ret;
-}
-
static struct gbm_bo *
gbm_dri_bo_import(struct gbm_device *gbm,
uint32_t type, void *buffer, uint32_t usage)
/* GBM's GBM_FORMAT_* tokens are a strict superset of the DRI FourCC
* tokens accepted by createImageFromFds, except for not supporting
- * the sARGB format. Also, GBM_BO_FORMAT_* are defined differently to
- * their GBM_FORMAT_* equivalents, so remap them here. */
- switch (fd_data->format) {
- case GBM_BO_FORMAT_XRGB8888:
- fourcc = GBM_FORMAT_XRGB8888;
- break;
- case GBM_BO_FORMAT_ARGB8888:
- fourcc = GBM_FORMAT_ARGB8888;
- break;
- default:
- fourcc = fd_data->format;
- }
+ * the sARGB format. */
+ fourcc = gbm_format_canonicalize(fd_data->format);
image = dri->image->createImageFromFds(dri->screen,
fd_data->width,
/* GBM's GBM_FORMAT_* tokens are a strict superset of the DRI FourCC
* tokens accepted by createImageFromDmaBufs2, except for not supporting
- * the sARGB format. Also, GBM_BO_FORMAT_* are defined differently to
- * their GBM_FORMAT_* equivalents, so remap them here. */
- switch (fd_data->format) {
- case GBM_BO_FORMAT_XRGB8888:
- fourcc = GBM_FORMAT_XRGB8888;
- break;
- case GBM_BO_FORMAT_ARGB8888:
- fourcc = GBM_FORMAT_ARGB8888;
- break;
- default:
- fourcc = fd_data->format;
- }
+ * the sARGB format. */
+ fourcc = gbm_format_canonicalize(fd_data->format);
image = dri->image->createImageFromDmaBufs2(dri->screen, fd_data->width,
fd_data->height, fourcc,
return &bo->base;
}
-static bool
-is_planar_format(uint32_t format)
-{
- switch (format) {
- case GBM_FORMAT_NV12:
- case GBM_FORMAT_NV21:
- case GBM_FORMAT_NV16:
- case GBM_FORMAT_NV61:
- case GBM_FORMAT_YUV410:
- case GBM_FORMAT_YVU410:
- case GBM_FORMAT_YUV411:
- case GBM_FORMAT_YVU411:
- case GBM_FORMAT_YUV420:
- case GBM_FORMAT_YVU420:
- case GBM_FORMAT_YUV422:
- case GBM_FORMAT_YVU422:
- case GBM_FORMAT_YUV444:
- case GBM_FORMAT_YVU444:
- return true;
- default:
- return false;
- }
-
-}
-
static struct gbm_bo *
create_dumb(struct gbm_device *gbm,
uint32_t width, uint32_t height,
return NULL;
}
- if (is_planar_format(format)) {
- errno = EINVAL;
- return NULL;
- }
-
bo = calloc(1, sizeof *bo);
if (bo == NULL)
return NULL;
*/
assert(!(usage && count));
+ format = gbm_format_canonicalize(format);
+
if (usage & GBM_BO_USE_WRITE || dri->image == NULL)
return create_dumb(gbm, width, height, format, usage);
bo->base.height = height;
bo->base.format = format;
- switch (format) {
- case GBM_FORMAT_R8:
- dri_format = __DRI_IMAGE_FORMAT_R8;
- break;
- case GBM_FORMAT_GR88:
- dri_format = __DRI_IMAGE_FORMAT_GR88;
- break;
- case GBM_FORMAT_RGB565:
- dri_format = __DRI_IMAGE_FORMAT_RGB565;
- break;
- case GBM_FORMAT_XRGB8888:
- case GBM_BO_FORMAT_XRGB8888:
- dri_format = __DRI_IMAGE_FORMAT_XRGB8888;
- break;
- case GBM_FORMAT_ARGB8888:
- case GBM_BO_FORMAT_ARGB8888:
- dri_format = __DRI_IMAGE_FORMAT_ARGB8888;
- break;
- case GBM_FORMAT_ABGR8888:
- dri_format = __DRI_IMAGE_FORMAT_ABGR8888;
- break;
- case GBM_FORMAT_XBGR8888:
- dri_format = __DRI_IMAGE_FORMAT_XBGR8888;
- break;
- case GBM_FORMAT_ARGB2101010:
- dri_format = __DRI_IMAGE_FORMAT_ARGB2101010;
- break;
- case GBM_FORMAT_XRGB2101010:
- dri_format = __DRI_IMAGE_FORMAT_XRGB2101010;
- break;
- default:
+ dri_format = gbm_format_to_dri_format(format);
+ if (dri_format == 0) {
errno = EINVAL;
goto failed;
}
surf->base.gbm = gbm;
surf->base.width = width;
surf->base.height = height;
- surf->base.format = format;
+ surf->base.format = gbm_format_canonicalize(format);
surf->base.flags = flags;
if (!modifiers) {
assert(!count);
dri_device_create(int fd)
{
struct gbm_dri_device *dri;
- int ret, force_sw;
+ int ret;
+ bool force_sw;
dri = calloc(1, sizeof *dri);
if (!dri)
dri->base.bo_map = gbm_dri_bo_map;
dri->base.bo_unmap = gbm_dri_bo_unmap;
dri->base.is_format_supported = gbm_dri_is_format_supported;
+ dri->base.get_format_modifier_plane_count =
+ gbm_dri_get_format_modifier_plane_count;
dri->base.bo_write = gbm_dri_bo_write;
dri->base.bo_get_fd = gbm_dri_bo_get_fd;
dri->base.bo_get_planes = gbm_dri_bo_get_planes;
dri->base.name = "drm";
+ dri->visual_table = gbm_dri_visuals_table;
+ dri->num_visuals = ARRAY_SIZE(gbm_dri_visuals_table);
+
mtx_init(&dri->mutex, mtx_plain);
- force_sw = getenv("GBM_ALWAYS_SOFTWARE") != NULL;
+ force_sw = env_var_as_boolean("GBM_ALWAYS_SOFTWARE", false);
if (!force_sw) {
ret = dri_screen_create(dri);
if (ret)