Path: blob/21.2-virgl/src/gallium/auxiliary/pipe-loader/pipe_loader.c
4561 views
/**************************************************************************1*2* Copyright 2012 Francisco Jerez3* All Rights Reserved.4*5* Permission is hereby granted, free of charge, to any person obtaining a6* copy of this software and associated documentation files (the7* "Software"), to deal in the Software without restriction, including8* without limitation the rights to use, copy, modify, merge, publish,9* distribute, sub license, and/or sell copies of the Software, and to10* permit persons to whom the Software is furnished to do so, subject to11* the following conditions:12*13* The above copyright notice and this permission notice (including the14* next paragraph) shall be included in all copies or substantial portions15* of the Software.16*17* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS18* OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF19* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.20* IN NO EVENT SHALL VMWARE AND/OR ITS SUPPLIERS BE LIABLE FOR21* ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,22* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE23* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.24*25**************************************************************************/2627#include "pipe_loader_priv.h"2829#include "util/u_cpu_detect.h"30#include "util/u_inlines.h"31#include "util/u_memory.h"32#include "util/u_string.h"33#include "util/u_dl.h"34#include "util/u_file.h"35#include "util/xmlconfig.h"36#include "util/driconf.h"3738#include <string.h>3940#ifdef _MSC_VER41#include <stdlib.h>42#define PATH_MAX _MAX_PATH43#endif4445#define MODULE_PREFIX "pipe_"4647static int (*backends[])(struct pipe_loader_device **, int) = {48#ifdef HAVE_LIBDRM49&pipe_loader_drm_probe,50#endif51&pipe_loader_sw_probe52};5354const driOptionDescription gallium_driconf[] = {55#include "driinfo_gallium.h"56};5758int59pipe_loader_probe(struct pipe_loader_device **devs, int ndev)60{61int i, n = 0;6263for (i = 0; i < ARRAY_SIZE(backends); i++)64n += backends[i](&devs[n], MAX2(0, ndev - n));6566return n;67}6869void70pipe_loader_release(struct pipe_loader_device **devs, int ndev)71{72int i;7374for (i = 0; i < ndev; i++)75devs[i]->ops->release(&devs[i]);76}7778void79pipe_loader_base_release(struct pipe_loader_device **dev)80{81driDestroyOptionCache(&(*dev)->option_cache);82driDestroyOptionInfo(&(*dev)->option_info);8384FREE(*dev);85*dev = NULL;86}8788static driOptionDescription *89merge_driconf(const driOptionDescription *driver_driconf, unsigned driver_count,90unsigned *merged_count)91{92unsigned gallium_count = ARRAY_SIZE(gallium_driconf);93driOptionDescription *merged = malloc((driver_count + gallium_count) *94sizeof(*merged));95if (!merged) {96*merged_count = 0;97return NULL;98}99100memcpy(merged, gallium_driconf, sizeof(*merged) * gallium_count);101memcpy(&merged[gallium_count], driver_driconf, sizeof(*merged) * driver_count);102103*merged_count = driver_count + gallium_count;104return merged;105}106107void108pipe_loader_load_options(struct pipe_loader_device *dev)109{110if (dev->option_info.info)111return;112113unsigned driver_count, merged_count;114const driOptionDescription *driver_driconf =115dev->ops->get_driconf(dev, &driver_count);116117const driOptionDescription *merged_driconf =118merge_driconf(driver_driconf, driver_count, &merged_count);119120driParseOptionInfo(&dev->option_info, merged_driconf, merged_count);121driParseConfigFiles(&dev->option_cache, &dev->option_info, 0,122dev->driver_name, NULL, NULL, 0, NULL, 0);123free((void *)merged_driconf);124}125126char *127pipe_loader_get_driinfo_xml(const char *driver_name)128{129unsigned driver_count = 0;130const driOptionDescription *driver_driconf = NULL;131132#ifdef HAVE_LIBDRM133driver_driconf = pipe_loader_drm_get_driconf_by_name(driver_name,134&driver_count);135#endif136137unsigned merged_count;138const driOptionDescription *merged_driconf =139merge_driconf(driver_driconf, driver_count, &merged_count);140free((void *)driver_driconf);141142char *xml = driGetOptionsXml(merged_driconf, merged_count);143144free((void *)merged_driconf);145146return xml;147}148149struct pipe_screen *150pipe_loader_create_screen_vk(struct pipe_loader_device *dev, bool sw_vk)151{152struct pipe_screen_config config;153154util_cpu_detect();155pipe_loader_load_options(dev);156config.options = &dev->option_cache;157158return dev->ops->create_screen(dev, &config, sw_vk);159}160161struct pipe_screen *162pipe_loader_create_screen(struct pipe_loader_device *dev)163{164return pipe_loader_create_screen_vk(dev, false);165}166167struct util_dl_library *168pipe_loader_find_module(const char *driver_name,169const char *library_paths)170{171struct util_dl_library *lib;172const char *next;173char path[PATH_MAX];174int len, ret;175176for (next = library_paths; *next; library_paths = next + 1) {177next = strchrnul(library_paths, ':');178len = next - library_paths;179180if (len)181ret = snprintf(path, sizeof(path), "%.*s/%s%s%s",182len, library_paths,183MODULE_PREFIX, driver_name, UTIL_DL_EXT);184else185ret = snprintf(path, sizeof(path), "%s%s%s",186MODULE_PREFIX, driver_name, UTIL_DL_EXT);187188if (ret > 0 && ret < sizeof(path) && u_file_access(path, 0) != -1) {189lib = util_dl_open(path);190if (lib) {191return lib;192}193fprintf(stderr, "ERROR: Failed to load pipe driver at `%s': %s\n",194path, util_dl_error());195}196}197198return NULL;199}200201202