Path: blob/21.2-virgl/src/egl/drivers/dri2/platform_surfaceless.c
4570 views
/*1* Mesa 3-D graphics library2*3* Copyright (c) 2014 The Chromium OS Authors.4* Copyright © 2011 Intel Corporation5*6* Permission is hereby granted, free of charge, to any person obtaining a7* copy of this software and associated documentation files (the "Software"),8* to deal in the Software without restriction, including without limitation9* the rights to use, copy, modify, merge, publish, distribute, sublicense,10* and/or sell copies of the Software, and to permit persons to whom the11* Software is furnished to do so, subject to the following conditions:12*13* The above copyright notice and this permission notice shall be included14* in all copies or substantial portions of the Software.15*16* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR17* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,18* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL19* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER20* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING21* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER22* DEALINGS IN THE SOFTWARE.23*/2425#include <stdlib.h>26#include <stdio.h>27#include <string.h>28#include <xf86drm.h>29#include <dlfcn.h>30#include <sys/types.h>31#include <sys/stat.h>32#include <fcntl.h>33#include <unistd.h>3435#include "egl_dri2.h"36#include "loader.h"3738static __DRIimage*39surfaceless_alloc_image(struct dri2_egl_display *dri2_dpy,40struct dri2_egl_surface *dri2_surf)41{42return dri2_dpy->image->createImage(43dri2_dpy->dri_screen,44dri2_surf->base.Width,45dri2_surf->base.Height,46dri2_surf->visual,470,48NULL);49}5051static void52surfaceless_free_images(struct dri2_egl_surface *dri2_surf)53{54struct dri2_egl_display *dri2_dpy =55dri2_egl_display(dri2_surf->base.Resource.Display);5657if (dri2_surf->front) {58dri2_dpy->image->destroyImage(dri2_surf->front);59dri2_surf->front = NULL;60}6162free(dri2_surf->swrast_device_buffer);63dri2_surf->swrast_device_buffer = NULL;64}6566static int67surfaceless_image_get_buffers(__DRIdrawable *driDrawable,68unsigned int format,69uint32_t *stamp,70void *loaderPrivate,71uint32_t buffer_mask,72struct __DRIimageList *buffers)73{74struct dri2_egl_surface *dri2_surf = loaderPrivate;75struct dri2_egl_display *dri2_dpy =76dri2_egl_display(dri2_surf->base.Resource.Display);7778buffers->image_mask = 0;79buffers->front = NULL;80buffers->back = NULL;8182/* The EGL 1.5 spec states that pbuffers are single-buffered. Specifically,83* the spec states that they have a back buffer but no front buffer, in84* contrast to pixmaps, which have a front buffer but no back buffer.85*86* Single-buffered surfaces with no front buffer confuse Mesa; so we deviate87* from the spec, following the precedent of Mesa's EGL X11 platform. The88* X11 platform correctly assigns pbuffers to single-buffered configs, but89* assigns the pbuffer a front buffer instead of a back buffer.90*91* Pbuffers in the X11 platform mostly work today, so let's just copy its92* behavior instead of trying to fix (and hence potentially breaking) the93* world.94*/9596if (buffer_mask & __DRI_IMAGE_BUFFER_FRONT) {9798if (!dri2_surf->front)99dri2_surf->front =100surfaceless_alloc_image(dri2_dpy, dri2_surf);101102buffers->image_mask |= __DRI_IMAGE_BUFFER_FRONT;103buffers->front = dri2_surf->front;104}105106return 1;107}108109static _EGLSurface *110dri2_surfaceless_create_surface(_EGLDisplay *disp, EGLint type,111_EGLConfig *conf, const EGLint *attrib_list)112{113struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);114struct dri2_egl_config *dri2_conf = dri2_egl_config(conf);115struct dri2_egl_surface *dri2_surf;116const __DRIconfig *config;117118/* Make sure to calloc so all pointers119* are originally NULL.120*/121dri2_surf = calloc(1, sizeof *dri2_surf);122123if (!dri2_surf) {124_eglError(EGL_BAD_ALLOC, "eglCreatePbufferSurface");125return NULL;126}127128if (!dri2_init_surface(&dri2_surf->base, disp, type, conf, attrib_list,129false, NULL))130goto cleanup_surface;131132config = dri2_get_dri_config(dri2_conf, type,133dri2_surf->base.GLColorspace);134135if (!config) {136_eglError(EGL_BAD_MATCH, "Unsupported surfacetype/colorspace configuration");137goto cleanup_surface;138}139140dri2_surf->visual = dri2_image_format_for_pbuffer_config(dri2_dpy, config);141if (dri2_surf->visual == __DRI_IMAGE_FORMAT_NONE)142goto cleanup_surface;143144if (!dri2_create_drawable(dri2_dpy, config, dri2_surf, dri2_surf))145goto cleanup_surface;146147return &dri2_surf->base;148149cleanup_surface:150free(dri2_surf);151return NULL;152}153154static EGLBoolean155surfaceless_destroy_surface(_EGLDisplay *disp, _EGLSurface *surf)156{157struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);158struct dri2_egl_surface *dri2_surf = dri2_egl_surface(surf);159160surfaceless_free_images(dri2_surf);161162dri2_dpy->core->destroyDrawable(dri2_surf->dri_drawable);163164dri2_fini_surface(surf);165free(dri2_surf);166return EGL_TRUE;167}168169static _EGLSurface *170dri2_surfaceless_create_pbuffer_surface(_EGLDisplay *disp, _EGLConfig *conf,171const EGLint *attrib_list)172{173return dri2_surfaceless_create_surface(disp, EGL_PBUFFER_BIT, conf,174attrib_list);175}176177static const struct dri2_egl_display_vtbl dri2_surfaceless_display_vtbl = {178.create_pbuffer_surface = dri2_surfaceless_create_pbuffer_surface,179.destroy_surface = surfaceless_destroy_surface,180.create_image = dri2_create_image_khr,181.get_dri_drawable = dri2_surface_get_dri_drawable,182};183184static void185surfaceless_flush_front_buffer(__DRIdrawable *driDrawable, void *loaderPrivate)186{187}188189static unsigned190surfaceless_get_capability(void *loaderPrivate, enum dri_loader_cap cap)191{192/* Note: loaderPrivate is _EGLDisplay* */193switch (cap) {194case DRI_LOADER_CAP_FP16:195return 1;196default:197return 0;198}199}200201static const __DRIimageLoaderExtension image_loader_extension = {202.base = { __DRI_IMAGE_LOADER, 2 },203.getBuffers = surfaceless_image_get_buffers,204.flushFrontBuffer = surfaceless_flush_front_buffer,205.getCapability = surfaceless_get_capability,206};207208static const __DRIextension *image_loader_extensions[] = {209&image_loader_extension.base,210&image_lookup_extension.base,211&use_invalidate.base,212&background_callable_extension.base,213NULL,214};215216static const __DRIextension *swrast_loader_extensions[] = {217&swrast_pbuffer_loader_extension.base,218&image_loader_extension.base,219&image_lookup_extension.base,220&use_invalidate.base,221NULL,222};223224static bool225surfaceless_probe_device(_EGLDisplay *disp, bool swrast)226{227#define MAX_DRM_DEVICES 64228const unsigned node_type = swrast ? DRM_NODE_PRIMARY : DRM_NODE_RENDER;229struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);230drmDevicePtr device, devices[MAX_DRM_DEVICES] = { NULL };231int i, num_devices;232233num_devices = drmGetDevices2(0, devices, ARRAY_SIZE(devices));234if (num_devices < 0)235return false;236237for (i = 0; i < num_devices; ++i) {238device = devices[i];239240if (!(device->available_nodes & (1 << node_type)))241continue;242243dri2_dpy->fd = loader_open_device(device->nodes[node_type]);244if (dri2_dpy->fd < 0)245continue;246247disp->Device = _eglAddDevice(dri2_dpy->fd, swrast);248if (!disp->Device) {249close(dri2_dpy->fd);250dri2_dpy->fd = -1;251continue;252}253254char *driver_name = loader_get_driver_for_fd(dri2_dpy->fd);255if (swrast) {256/* Use kms swrast only with vgem / virtio_gpu.257* virtio-gpu fallbacks to software rendering when 3D features258* are unavailable since 6c5ab, and kms_swrast is more259* feature complete than swrast.260*/261if (driver_name &&262(strcmp(driver_name, "vgem") == 0 ||263strcmp(driver_name, "virtio_gpu") == 0))264dri2_dpy->driver_name = strdup("kms_swrast");265free(driver_name);266} else {267/* Use the given hardware driver */268dri2_dpy->driver_name = driver_name;269}270271if (dri2_dpy->driver_name && dri2_load_driver_dri3(disp))272break;273274free(dri2_dpy->driver_name);275dri2_dpy->driver_name = NULL;276close(dri2_dpy->fd);277dri2_dpy->fd = -1;278}279drmFreeDevices(devices, num_devices);280281if (i == num_devices)282return false;283284if (swrast)285dri2_dpy->loader_extensions = swrast_loader_extensions;286else287dri2_dpy->loader_extensions = image_loader_extensions;288289return true;290}291292static bool293surfaceless_probe_device_sw(_EGLDisplay *disp)294{295struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);296297dri2_dpy->fd = -1;298disp->Device = _eglAddDevice(dri2_dpy->fd, true);299assert(disp->Device);300301dri2_dpy->driver_name = strdup("swrast");302if (!dri2_dpy->driver_name)303return false;304305if (!dri2_load_driver_swrast(disp)) {306free(dri2_dpy->driver_name);307dri2_dpy->driver_name = NULL;308return false;309}310311dri2_dpy->loader_extensions = swrast_loader_extensions;312return true;313}314315EGLBoolean316dri2_initialize_surfaceless(_EGLDisplay *disp)317{318struct dri2_egl_display *dri2_dpy;319const char* err;320bool driver_loaded = false;321322dri2_dpy = calloc(1, sizeof *dri2_dpy);323if (!dri2_dpy)324return _eglError(EGL_BAD_ALLOC, "eglInitialize");325326dri2_dpy->fd = -1;327disp->DriverData = (void *) dri2_dpy;328329/* When ForceSoftware is false, we try the HW driver. When ForceSoftware330* is true, we try kms_swrast and swrast in order.331*/332driver_loaded = surfaceless_probe_device(disp, disp->Options.ForceSoftware);333if (!driver_loaded && disp->Options.ForceSoftware) {334_eglLog(_EGL_DEBUG, "Falling back to surfaceless swrast without DRM.");335driver_loaded = surfaceless_probe_device_sw(disp);336}337338if (!driver_loaded) {339err = "DRI2: failed to load driver";340goto cleanup;341}342343if (!dri2_create_screen(disp)) {344err = "DRI2: failed to create screen";345goto cleanup;346}347348if (!dri2_setup_extensions(disp)) {349err = "DRI2: failed to find required DRI extensions";350goto cleanup;351}352353dri2_setup_screen(disp);354#ifdef HAVE_WAYLAND_PLATFORM355dri2_dpy->device_name = loader_get_device_name_for_fd(dri2_dpy->fd);356#endif357dri2_set_WL_bind_wayland_display(disp);358359if (!dri2_add_pbuffer_configs_for_visuals(disp)) {360err = "DRI2: failed to add configs";361goto cleanup;362}363364/* Fill vtbl last to prevent accidentally calling virtual function during365* initialization.366*/367dri2_dpy->vtbl = &dri2_surfaceless_display_vtbl;368369return EGL_TRUE;370371cleanup:372dri2_display_destroy(disp);373return _eglError(EGL_NOT_INITIALIZED, err);374}375376377