revert 213 commits (to 56092) from the last month. 10 still need work to resolve...
[AROS.git] / workbench / libs / mesa / src / gallium / targets / egl-static / egl.c
blob9ea3a50621d5703f6ec1edd50d78904fc7a59339
1 /*
2 * Mesa 3-D graphics library
3 * Version: 7.10
5 * Copyright (C) 2010-2011 LunarG Inc.
7 * Permission is hereby granted, free of charge, to any person obtaining a
8 * copy of this software and associated documentation files (the "Software"),
9 * to deal in the Software without restriction, including without limitation
10 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
11 * and/or sell copies of the Software, and to permit persons to whom the
12 * Software is furnished to do so, subject to the following conditions:
14 * The above copyright notice and this permission notice shall be included
15 * in all copies or substantial portions of the Software.
17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
20 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
22 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
23 * DEALINGS IN THE SOFTWARE.
25 * Authors:
26 * Chia-I Wu <olv@lunarg.com>
29 #include "common/egl_g3d_loader.h"
30 #include "egldriver.h"
31 #include "egllog.h"
33 #ifdef HAVE_LIBUDEV
34 #include <stdio.h>
35 #include <libudev.h>
36 #define DRIVER_MAP_GALLIUM_ONLY
37 #include "pci_ids/pci_id_driver_map.h"
38 #endif
40 #include "egl_pipe.h"
41 #include "egl_st.h"
43 static struct egl_g3d_loader egl_g3d_loader;
45 static struct st_module {
46 boolean initialized;
47 struct st_api *stapi;
48 } st_modules[ST_API_COUNT];
50 static struct st_api *
51 get_st_api(enum st_api_type api)
53 struct st_module *stmod = &st_modules[api];
55 if (!stmod->initialized) {
56 stmod->stapi = egl_st_create_api(api);
57 stmod->initialized = TRUE;
60 return stmod->stapi;
63 #if !defined(PIPE_OS_AROS)
64 static const char *
65 drm_fd_get_screen_name(int fd)
67 #ifdef HAVE_LIBUDEV
68 struct udev *udev;
69 struct udev_device *device, *parent;
70 struct stat buf;
71 const char *pci_id;
72 int vendor_id, chip_id, idx = -1, i;
74 udev = udev_new();
75 if (fstat(fd, &buf) < 0) {
76 _eglLog(_EGL_WARNING, "failed to stat fd %d", fd);
77 return NULL;
80 device = udev_device_new_from_devnum(udev, 'c', buf.st_rdev);
81 if (device == NULL) {
82 _eglLog(_EGL_WARNING,
83 "could not create udev device for fd %d", fd);
84 return NULL;
87 parent = udev_device_get_parent(device);
88 if (parent == NULL) {
89 _eglLog(_EGL_WARNING, "could not get parent device");
90 goto out;
93 pci_id = udev_device_get_property_value(parent, "PCI_ID");
94 if (pci_id == NULL ||
95 sscanf(pci_id, "%x:%x", &vendor_id, &chip_id) != 2) {
96 _eglLog(_EGL_WARNING, "malformed or no PCI ID");
97 goto out;
100 /* find the driver index */
101 for (idx = 0; driver_map[idx].driver; idx++) {
102 if (vendor_id != driver_map[idx].vendor_id)
103 continue;
105 if (driver_map[idx].num_chips_ids == -1)
106 goto out;
108 for (i = 0; i < driver_map[idx].num_chips_ids; i++) {
109 if (driver_map[idx].chip_ids[i] == chip_id)
110 goto out;
114 out:
115 udev_device_unref(device);
116 udev_unref(udev);
118 if (idx >= 0) {
119 _eglLog((driver_map[idx].driver) ? _EGL_INFO : _EGL_WARNING,
120 "pci id for fd %d: %04x:%04x, driver %s",
121 fd, vendor_id, chip_id, driver_map[idx].driver);
123 return driver_map[idx].driver;
125 #endif
127 _eglLog(_EGL_WARNING, "failed to get driver name for fd %d", fd);
129 return NULL;
132 static struct pipe_screen *
133 create_drm_screen(const char *name, int fd)
135 if (!name) {
136 name = drm_fd_get_screen_name(fd);
137 if (!name)
138 return NULL;
141 return egl_pipe_create_drm_screen(name, fd);
144 static struct pipe_screen *
145 create_sw_screen(struct sw_winsys *ws)
147 return egl_pipe_create_swrast_screen(ws);
149 #endif
151 static const struct egl_g3d_loader *
152 loader_init(void)
154 int i;
156 for (i = 0; i < ST_API_COUNT; i++)
157 egl_g3d_loader.profile_masks[i] = egl_st_get_profile_mask(i);
159 egl_g3d_loader.get_st_api = get_st_api;
160 #if !defined(PIPE_OS_AROS)
161 egl_g3d_loader.create_drm_screen = create_drm_screen;
162 egl_g3d_loader.create_sw_screen = create_sw_screen;
163 #endif
165 return &egl_g3d_loader;
168 static void
169 loader_fini(void)
171 int i;
173 for (i = 0; i < ST_API_COUNT; i++) {
174 struct st_module *stmod = &st_modules[i];
176 if (stmod->stapi) {
177 egl_st_destroy_api(stmod->stapi);
178 stmod->stapi = NULL;
180 stmod->initialized = FALSE;
184 static void
185 egl_g3d_unload(_EGLDriver *drv)
187 egl_g3d_destroy_driver(drv);
188 loader_fini();
191 _EGLDriver *
192 _EGL_MAIN(const char *args)
194 const struct egl_g3d_loader *loader;
195 _EGLDriver *drv;
197 loader = loader_init();
198 drv = egl_g3d_create_driver(loader);
199 if (!drv) {
200 loader_fini();
201 return NULL;
204 drv->Name = "Gallium";
205 drv->Unload = egl_g3d_unload;
207 return drv;