Path: blob/21.2-virgl/src/egl/drivers/dri2/platform_device.c
4570 views
/*1* Mesa 3-D graphics library2*3* Copyright 2018 Collabora4*5* Based on platform_surfaceless, which has:6*7* Copyright (c) 2014 The Chromium OS Authors.8* Copyright © 2011 Intel Corporation9*10* Permission is hereby granted, free of charge, to any person obtaining a11* copy of this software and associated documentation files (the "Software"),12* to deal in the Software without restriction, including without limitation13* the rights to use, copy, modify, merge, publish, distribute, sublicense,14* and/or sell copies of the Software, and to permit persons to whom the15* Software is furnished to do so, subject to the following conditions:16*17* The above copyright notice and this permission notice shall be included18* in all copies or substantial portions of the Software.19*20* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR21* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,22* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL23* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER24* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING25* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER26* DEALINGS IN THE SOFTWARE.27*/28#ifdef HAVE_LIBDRM29#include <xf86drm.h>30#endif3132#include <stdlib.h>33#include <stdio.h>34#include <string.h>35#include <dlfcn.h>36#include <sys/types.h>37#include <sys/stat.h>38#include <fcntl.h>39#include <unistd.h>4041#include "egl_dri2.h"42#include "loader.h"43#include "util/debug.h"4445static __DRIimage*46device_alloc_image(struct dri2_egl_display *dri2_dpy,47struct dri2_egl_surface *dri2_surf)48{49return dri2_dpy->image->createImage(50dri2_dpy->dri_screen,51dri2_surf->base.Width,52dri2_surf->base.Height,53dri2_surf->visual,540,55NULL);56}5758static void59device_free_images(struct dri2_egl_surface *dri2_surf)60{61struct dri2_egl_display *dri2_dpy =62dri2_egl_display(dri2_surf->base.Resource.Display);6364if (dri2_surf->front) {65dri2_dpy->image->destroyImage(dri2_surf->front);66dri2_surf->front = NULL;67}6869free(dri2_surf->swrast_device_buffer);70dri2_surf->swrast_device_buffer = NULL;71}7273static int74device_image_get_buffers(__DRIdrawable *driDrawable,75unsigned int format,76uint32_t *stamp,77void *loaderPrivate,78uint32_t buffer_mask,79struct __DRIimageList *buffers)80{81struct dri2_egl_surface *dri2_surf = loaderPrivate;82struct dri2_egl_display *dri2_dpy =83dri2_egl_display(dri2_surf->base.Resource.Display);8485buffers->image_mask = 0;86buffers->front = NULL;87buffers->back = NULL;8889/* The EGL 1.5 spec states that pbuffers are single-buffered. Specifically,90* the spec states that they have a back buffer but no front buffer, in91* contrast to pixmaps, which have a front buffer but no back buffer.92*93* Single-buffered surfaces with no front buffer confuse Mesa; so we deviate94* from the spec, following the precedent of Mesa's EGL X11 platform. The95* X11 platform correctly assigns pbuffers to single-buffered configs, but96* assigns the pbuffer a front buffer instead of a back buffer.97*98* Pbuffers in the X11 platform mostly work today, so let's just copy its99* behavior instead of trying to fix (and hence potentially breaking) the100* world.101*/102103if (buffer_mask & __DRI_IMAGE_BUFFER_FRONT) {104105if (!dri2_surf->front)106dri2_surf->front =107device_alloc_image(dri2_dpy, dri2_surf);108109buffers->image_mask |= __DRI_IMAGE_BUFFER_FRONT;110buffers->front = dri2_surf->front;111}112113return 1;114}115116static _EGLSurface *117dri2_device_create_surface(_EGLDisplay *disp, EGLint type, _EGLConfig *conf,118const EGLint *attrib_list)119{120struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);121struct dri2_egl_config *dri2_conf = dri2_egl_config(conf);122struct dri2_egl_surface *dri2_surf;123const __DRIconfig *config;124125/* Make sure to calloc so all pointers126* are originally NULL.127*/128dri2_surf = calloc(1, sizeof *dri2_surf);129130if (!dri2_surf) {131_eglError(EGL_BAD_ALLOC, "eglCreatePbufferSurface");132return NULL;133}134135if (!dri2_init_surface(&dri2_surf->base, disp, type, conf, attrib_list,136false, NULL))137goto cleanup_surface;138139config = dri2_get_dri_config(dri2_conf, type,140dri2_surf->base.GLColorspace);141142if (!config) {143_eglError(EGL_BAD_MATCH, "Unsupported surfacetype/colorspace configuration");144goto cleanup_surface;145}146147dri2_surf->visual = dri2_image_format_for_pbuffer_config(dri2_dpy, config);148if (dri2_surf->visual == __DRI_IMAGE_FORMAT_NONE)149goto cleanup_surface;150151if (!dri2_create_drawable(dri2_dpy, config, dri2_surf, dri2_surf))152goto cleanup_surface;153154return &dri2_surf->base;155156cleanup_surface:157free(dri2_surf);158return NULL;159}160161static EGLBoolean162device_destroy_surface(_EGLDisplay *disp, _EGLSurface *surf)163{164struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);165struct dri2_egl_surface *dri2_surf = dri2_egl_surface(surf);166167device_free_images(dri2_surf);168169dri2_dpy->core->destroyDrawable(dri2_surf->dri_drawable);170171dri2_fini_surface(surf);172free(dri2_surf);173return EGL_TRUE;174}175176static _EGLSurface *177dri2_device_create_pbuffer_surface(_EGLDisplay *disp, _EGLConfig *conf,178const EGLint *attrib_list)179{180return dri2_device_create_surface(disp, EGL_PBUFFER_BIT, conf, attrib_list);181}182183static const struct dri2_egl_display_vtbl dri2_device_display_vtbl = {184.create_pbuffer_surface = dri2_device_create_pbuffer_surface,185.destroy_surface = device_destroy_surface,186.create_image = dri2_create_image_khr,187.get_dri_drawable = dri2_surface_get_dri_drawable,188};189190static void191device_flush_front_buffer(__DRIdrawable *driDrawable, void *loaderPrivate)192{193}194195static const __DRIimageLoaderExtension image_loader_extension = {196.base = { __DRI_IMAGE_LOADER, 1 },197.getBuffers = device_image_get_buffers,198.flushFrontBuffer = device_flush_front_buffer,199};200201static const __DRIextension *image_loader_extensions[] = {202&image_loader_extension.base,203&image_lookup_extension.base,204&use_invalidate.base,205NULL,206};207208static const __DRIextension *swrast_loader_extensions[] = {209&swrast_pbuffer_loader_extension.base,210&image_lookup_extension.base,211&use_invalidate.base,212NULL,213};214215static int216device_get_fd(_EGLDisplay *disp, _EGLDevice *dev)217{218#ifdef HAVE_LIBDRM219int fd = disp->Options.fd;220/* The fcntl() code in _eglGetDeviceDisplay() ensures that valid fd >= 3,221* and invalid one is 0.222*/223if (fd) {224/* According to the spec - if the FD does not match the EGLDevice225* behaviour is undefined.226*227* Add a trivial sanity check since it doesn't cost us anything.228*/229if (dev != _eglAddDevice(fd, false))230return -1;231232/* No EGL_EXT_output* extensions are supported, hence no master perms233* are needed. Get the render one - otherwise drivers might error out.234*/235char *node = drmGetRenderDeviceNameFromFd(fd);236237/* Don't close the internal fd, get render node one based on it. */238fd = loader_open_device(node);239free(node);240return fd;241}242const char *node = _eglGetDRMDeviceRenderNode(dev);243return loader_open_device(node);244#else245_eglLog(_EGL_FATAL, "Driver bug: Built without libdrm, yet using a HW device");246return -1;247#endif248}249250static bool251device_probe_device(_EGLDisplay *disp)252{253struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);254bool request_software = env_var_as_boolean("LIBGL_ALWAYS_SOFTWARE", false);255256if (request_software)257_eglLog(_EGL_WARNING, "Not allowed to force software rendering when "258"API explicitly selects a hardware device.");259dri2_dpy->fd = device_get_fd(disp, disp->Device);260if (dri2_dpy->fd < 0)261return false;262263dri2_dpy->driver_name = loader_get_driver_for_fd(dri2_dpy->fd);264if (!dri2_dpy->driver_name)265goto err_name;266267/* When doing software rendering, some times user still want to explicitly268* choose the render node device since cross node import doesn't work between269* vgem/virtio_gpu yet. It would be nice to have a new EXTENSION for this.270* For now, just fallback to kms_swrast. */271if (disp->Options.ForceSoftware && !request_software &&272(strcmp(dri2_dpy->driver_name, "vgem") == 0 ||273strcmp(dri2_dpy->driver_name, "virtio_gpu") == 0)) {274free(dri2_dpy->driver_name);275_eglLog(_EGL_WARNING, "NEEDS EXTENSION: falling back to kms_swrast");276dri2_dpy->driver_name = strdup("kms_swrast");277}278279if (!dri2_load_driver_dri3(disp))280goto err_load;281282dri2_dpy->loader_extensions = image_loader_extensions;283return true;284285err_load:286free(dri2_dpy->driver_name);287dri2_dpy->driver_name = NULL;288289err_name:290close(dri2_dpy->fd);291dri2_dpy->fd = -1;292return false;293294}295296static bool297device_probe_device_sw(_EGLDisplay *disp)298{299struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);300301dri2_dpy->fd = -1;302dri2_dpy->driver_name = strdup("swrast");303if (!dri2_dpy->driver_name)304return false;305306/* HACK: should be driver_swrast_null */307if (!dri2_load_driver_swrast(disp)) {308free(dri2_dpy->driver_name);309dri2_dpy->driver_name = NULL;310return false;311}312313dri2_dpy->loader_extensions = swrast_loader_extensions;314return true;315}316317EGLBoolean318dri2_initialize_device(_EGLDisplay *disp)319{320_EGLDevice *dev;321struct dri2_egl_display *dri2_dpy;322const char* err;323324dri2_dpy = calloc(1, sizeof *dri2_dpy);325if (!dri2_dpy)326return _eglError(EGL_BAD_ALLOC, "eglInitialize");327328/* Extension requires a PlatformDisplay - the EGLDevice. */329dev = disp->PlatformDisplay;330331dri2_dpy->fd = -1;332disp->Device = dev;333disp->DriverData = (void *) dri2_dpy;334err = "DRI2: failed to load driver";335if (_eglDeviceSupports(dev, _EGL_DEVICE_DRM)) {336if (!device_probe_device(disp))337goto cleanup;338} else if (_eglDeviceSupports(dev, _EGL_DEVICE_SOFTWARE)) {339if (!device_probe_device_sw(disp))340goto cleanup;341} else {342_eglLog(_EGL_FATAL, "Driver bug: exposed device is neither DRM nor SOFTWARE one");343return EGL_FALSE;344}345346if (!dri2_create_screen(disp)) {347err = "DRI2: failed to create screen";348goto cleanup;349}350351if (!dri2_setup_extensions(disp)) {352err = "DRI2: failed to find required DRI extensions";353goto cleanup;354}355356dri2_setup_screen(disp);357#ifdef HAVE_WAYLAND_PLATFORM358dri2_dpy->device_name = loader_get_device_name_for_fd(dri2_dpy->fd);359#endif360dri2_set_WL_bind_wayland_display(disp);361362if (!dri2_add_pbuffer_configs_for_visuals(disp)) {363err = "DRI2: failed to add configs";364goto cleanup;365}366367/* Fill vtbl last to prevent accidentally calling virtual function during368* initialization.369*/370dri2_dpy->vtbl = &dri2_device_display_vtbl;371372return EGL_TRUE;373374cleanup:375dri2_display_destroy(disp);376return _eglError(EGL_NOT_INITIALIZED, err);377}378379380