virgl/egl: separate EGL and GBM functions

GBM and EGL are separate libraries.

v2: - don't fallback if drm fd specified (@davidriley)
    - close fd on failure (@davidriley)
v3:
    - if drm fd is specified, assume caller retains ownership (@davidriley)
v4:
    - add comments (@davidriley)

Reviewed-by: David Riley <davidriley@chromium.org>
Signed-off-by: Gurchetan Singh <gurchetansingh@chromium.org>
macos/master
Gurchetan Singh 6 years ago
parent 4253b10072
commit 4cade493dc
  1. 2
      src/Makefile.am
  2. 2
      src/virgl_egl.h
  3. 129
      src/virgl_egl_context.c
  4. 127
      src/virgl_gbm.c
  5. 46
      src/virgl_gbm.h

@ -42,6 +42,8 @@ libvrend_la_SOURCES = \
if HAVE_EPOXY_EGL
libvrend_la_SOURCES += \
virgl_egl.h \
virgl_gbm.h \
virgl_gbm.c \
virgl_egl_context.c
endif

@ -49,6 +49,4 @@ int virgl_egl_get_fd_for_texture(struct virgl_egl *egl, uint32_t tex_id, int *fd
int virgl_egl_get_fd_for_texture2(struct virgl_egl *egl, uint32_t tex_id, int *fd, int *stride,
int *offset);
uint32_t virgl_egl_get_gbm_format(uint32_t format);
#endif

@ -31,24 +31,18 @@
#endif
#define EGL_EGLEXT_PROTOTYPES
#include <dirent.h>
#include <fcntl.h>
#include <unistd.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <errno.h>
#include <fcntl.h>
#include <stdbool.h>
#include <epoxy/egl.h>
#include <gbm.h>
#include <xf86drm.h>
#include "virglrenderer.h"
#include "virgl_egl.h"
#include "virgl_hw.h"
#include "virgl_gbm.h"
struct virgl_egl {
int fd;
struct gbm_device *gbm_dev;
struct virgl_gbm *gbm;
EGLDisplay egl_display;
EGLConfig egl_conf;
EGLContext egl_ctx;
@ -59,44 +53,6 @@ struct virgl_egl {
bool have_ext_image_dma_buf_import_modifiers;
};
static int egl_rendernode_open(void)
{
DIR *dir;
struct dirent *dir_ent;
int ret, fd;
char *rendernode_name;
dir = opendir("/dev/dri");
if (!dir)
return -1;
fd = -1;
while ((dir_ent = readdir(dir))) {
if (dir_ent->d_type != DT_CHR)
continue;
if (strncmp(dir_ent->d_name, "renderD", 7))
continue;
ret = asprintf(&rendernode_name, "/dev/dri/%s", dir_ent->d_name);
if (ret < 0)
return -1;
fd = open(rendernode_name, O_RDWR | O_CLOEXEC | O_NOCTTY | O_NONBLOCK);
if (fd < 0){
free(rendernode_name);
continue;
}
free(rendernode_name);
break;
}
closedir(dir);
if (fd < 0)
return -1;
return fd;
}
static bool virgl_egl_has_extension_in_string(const char *haystack, const char *needle)
{
const unsigned needle_len = strlen(needle);
@ -169,18 +125,10 @@ struct virgl_egl *virgl_egl_init(int fd, bool surfaceless, bool gles)
if (surfaceless) {
conf_att[1] = EGL_PBUFFER_BIT;
egl->fd = -1;
egl->gbm_dev = NULL;
egl->gbm = NULL;
} else {
if (fd >= 0) {
egl->fd = fd;
} else {
egl->fd = egl_rendernode_open();
}
if (egl->fd == -1)
goto fail;
egl->gbm_dev = gbm_create_device(egl->fd);
if (!egl->gbm_dev)
egl->gbm = virgl_gbm_init(fd);
if (!egl->gbm)
goto fail;
}
@ -198,7 +146,7 @@ struct virgl_egl *virgl_egl_init(int fd, bool surfaceless, bool gles)
EGL_DEFAULT_DISPLAY, NULL);
} else
egl->egl_display = get_platform_display (EGL_PLATFORM_GBM_KHR,
(EGLNativeDisplayType)egl->gbm_dev, NULL);
(EGLNativeDisplayType)egl->gbm->device, NULL);
} else if (strstr (client_extensions, "EGL_EXT_platform_base")) {
PFNEGLGETPLATFORMDISPLAYEXTPROC get_platform_display =
(PFNEGLGETPLATFORMDISPLAYEXTPROC) eglGetProcAddress ("eglGetPlatformDisplayEXT");
@ -211,28 +159,27 @@ struct virgl_egl *virgl_egl_init(int fd, bool surfaceless, bool gles)
EGL_DEFAULT_DISPLAY, NULL);
} else
egl->egl_display = get_platform_display (EGL_PLATFORM_GBM_KHR,
(EGLNativeDisplayType)egl->gbm_dev, NULL);
(EGLNativeDisplayType)egl->gbm->device, NULL);
} else {
egl->egl_display = eglGetDisplay((EGLNativeDisplayType)egl->gbm_dev);
egl->egl_display = eglGetDisplay((EGLNativeDisplayType)egl->gbm->device);
}
if (!egl->egl_display) {
if (egl->gbm_dev) {
gbm_device_destroy(egl->gbm_dev);
egl->gbm_dev = NULL;
}
/*
* Don't fallback to the default display if the fd provided by (*get_drm_fd)
* can't be used.
*/
if (fd >= 0)
goto fail;
if (egl->fd >= 0) {
close(egl->fd);
egl->fd = -1;
if (egl->gbm) {
virgl_gbm_fini(egl->gbm);
egl->gbm = NULL;
}
/* Fallback to using the default display unless an fd was specified. */
if (fd < 0)
egl->egl_display = eglGetDisplay(EGL_DEFAULT_DISPLAY);
egl->egl_display = eglGetDisplay(EGL_DEFAULT_DISPLAY);
if (!egl->egl_display)
goto fail;
goto fail;
}
success = eglInitialize(egl->egl_display, &major, &minor);
@ -299,13 +246,8 @@ struct virgl_egl *virgl_egl_init(int fd, bool surfaceless, bool gles)
return egl;
fail:
if (egl->gbm_dev) {
gbm_device_destroy(egl->gbm_dev);
}
if (egl->fd >= 0) {
close(egl->fd);
}
if (egl->gbm)
virgl_gbm_fini(egl->gbm);
free(egl);
return NULL;
@ -317,10 +259,8 @@ void virgl_egl_destroy(struct virgl_egl *egl)
EGL_NO_CONTEXT);
eglDestroyContext(egl->egl_display, egl->egl_ctx);
eglTerminate(egl->egl_display);
if (egl->gbm_dev)
gbm_device_destroy(egl->gbm_dev);
if (egl->fd >= 0)
close(egl->fd);
if (egl->gbm)
virgl_gbm_fini(egl->gbm);
free(egl);
}
@ -386,7 +326,7 @@ int virgl_egl_get_fourcc_for_texture(struct virgl_egl *egl, uint32_t tex_id, uin
return ret;
fallback:
*fourcc = virgl_egl_get_gbm_format(format);
*fourcc = virgl_gbm_convert_format(format);
return ret;
}
@ -440,9 +380,10 @@ int virgl_egl_get_fd_for_texture(struct virgl_egl *egl, uint32_t tex_id, int *fd
if (!success)
goto out_destroy;
vrend_printf("image exported %d %d\n", handle, stride);
if (!egl->gbm)
goto out_destroy;
ret = drmPrimeHandleToFD(egl->fd, handle, DRM_CLOEXEC, fd);
ret = drmPrimeHandleToFD(gbm_device_get_fd(egl->gbm->device), handle, DRM_CLOEXEC, fd);
if (ret < 0)
goto out_destroy;
} else {
@ -459,15 +400,3 @@ bool virgl_has_egl_khr_gl_colorspace(struct virgl_egl *egl)
{
return egl->have_khr_gl_colorspace;
}
uint32_t virgl_egl_get_gbm_format(uint32_t format)
{
switch (format) {
case VIRGL_FORMAT_B8G8R8X8_UNORM:
case VIRGL_FORMAT_B8G8R8A8_UNORM:
return GBM_FORMAT_ARGB8888;
default:
vrend_printf( "unknown format to convert to GBM %d\n", format);
return 0;
}
}

@ -0,0 +1,127 @@
/**************************************************************************
*
* Copyright (C) 2019 Chromium.
*
* Permission is hereby granted, free of charge, to any person obtaining a
* copy of this software and associated documentation files (the "Software"),
* to deal in the Software without restriction, including without limitation
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
* and/or sell copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included
* in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
* OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
* OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
* ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
* OTHER DEALINGS IN THE SOFTWARE.
*
**************************************************************************/
#ifndef _GNU_SOURCE
#define _GNU_SOURCE 1
#endif
#include <stdio.h>
#include <dirent.h>
#include <fcntl.h>
#include <stdlib.h>
#include <string.h>
#include <xf86drm.h>
#include <unistd.h>
#include "virgl_gbm.h"
#include "virgl_hw.h"
#include "vrend_debug.h"
static int rendernode_open(void)
{
DIR *dir;
struct dirent *dir_ent;
int ret, fd;
char *rendernode_name;
dir = opendir("/dev/dri");
if (!dir)
return -1;
fd = -1;
while ((dir_ent = readdir(dir))) {
if (dir_ent->d_type != DT_CHR)
continue;
if (strncmp(dir_ent->d_name, "renderD", 7))
continue;
ret = asprintf(&rendernode_name, "/dev/dri/%s", dir_ent->d_name);
if (ret < 0)
return -1;
fd = open(rendernode_name, O_RDWR | O_CLOEXEC | O_NOCTTY | O_NONBLOCK);
if (fd < 0){
free(rendernode_name);
continue;
}
free(rendernode_name);
break;
}
closedir(dir);
if (fd < 0)
return -1;
return fd;
}
struct virgl_gbm *virgl_gbm_init(int fd)
{
struct virgl_gbm *gbm = calloc(1, sizeof(struct virgl_gbm));
if (!gbm)
return NULL;
gbm->fd = -1;
if (fd < 0) {
gbm->fd = rendernode_open();
if (gbm->fd < 0)
goto out_error;
gbm->device = gbm_create_device(gbm->fd);
if (!gbm->device) {
close(gbm->fd);
goto out_error;
}
} else {
gbm->device = gbm_create_device(fd);
if (!gbm->device)
goto out_error;
}
return gbm;
out_error:
free(gbm);
return NULL;
}
void virgl_gbm_fini(struct virgl_gbm *gbm)
{
gbm_device_destroy(gbm->device);
if (gbm->fd >= 0)
close(gbm->fd);
free(gbm);
}
uint32_t virgl_gbm_convert_format(uint32_t virgl_format)
{
switch (virgl_format) {
case VIRGL_FORMAT_B8G8R8X8_UNORM:
case VIRGL_FORMAT_B8G8R8A8_UNORM:
return GBM_FORMAT_ARGB8888;
default:
vrend_printf("unsupported virgl format: %u\n", virgl_format);
return 0;
}
}

@ -0,0 +1,46 @@
/**************************************************************************
*
* Copyright (C) 2019 Chromium.
*
* Permission is hereby granted, free of charge, to any person obtaining a
* copy of this software and associated documentation files (the "Software"),
* to deal in the Software without restriction, including without limitation
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
* and/or sell copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included
* in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
* OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
* OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
* ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
* OTHER DEALINGS IN THE SOFTWARE.
*
**************************************************************************/
#ifndef VIRGL_GBM_H
#define VIRGL_GBM_H
#include <gbm.h>
/*
* If fd >= 0, virglrenderer owns the fd since it was opened via a rendernode
* query. If fd < 0, the gbm device was opened with the fd provided by the
* (*get_drm_fd) hook.
*/
struct virgl_gbm {
int fd;
struct gbm_device *device;
};
struct virgl_gbm *virgl_gbm_init(int fd);
void virgl_gbm_fini(struct virgl_gbm *gbm);
uint32_t virgl_gbm_convert_format(uint32_t virgl_format);
#endif
Loading…
Cancel
Save