egl.c revision 7451bffad41a8ab7c61c9799b351c031852eb780
1/*
2 * Mesa 3-D graphics library
3 * Version:  7.10
4 *
5 * Copyright (C) 2010-2011 LunarG Inc.
6 *
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:
13 *
14 * The above copyright notice and this permission notice shall be included
15 * in all copies or substantial portions of the Software.
16 *
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.
24 *
25 * Authors:
26 *    Chia-I Wu <olv@lunarg.com>
27 */
28
29#include "common/egl_g3d_loader.h"
30#include "egldriver.h"
31#include "egllog.h"
32
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
39
40#include "egl_pipe.h"
41#include "egl_st.h"
42
43static struct egl_g3d_loader egl_g3d_loader;
44
45static struct st_module {
46   boolean initialized;
47   struct st_api *stapi;
48} st_modules[ST_API_COUNT];
49
50static struct st_api *
51get_st_api(enum st_api_type api)
52{
53   struct st_module *stmod = &st_modules[api];
54
55   if (!stmod->initialized) {
56      stmod->stapi = egl_st_create_api(api);
57      stmod->initialized = TRUE;
58   }
59
60   return stmod->stapi;
61}
62
63static const char *
64drm_fd_get_screen_name(int fd)
65{
66#ifdef HAVE_LIBUDEV
67   struct udev *udev;
68   struct udev_device *device, *parent;
69   struct stat buf;
70   const char *pci_id;
71   int vendor_id, chip_id, idx = -1, i;
72
73   udev = udev_new();
74   if (fstat(fd, &buf) < 0) {
75      _eglLog(_EGL_WARNING, "failed to stat fd %d", fd);
76      return NULL;
77   }
78
79   device = udev_device_new_from_devnum(udev, 'c', buf.st_rdev);
80   if (device == NULL) {
81      _eglLog(_EGL_WARNING,
82              "could not create udev device for fd %d", fd);
83      return NULL;
84   }
85
86   parent = udev_device_get_parent(device);
87   if (parent == NULL) {
88      _eglLog(_EGL_WARNING, "could not get parent device");
89      goto out;
90   }
91
92   pci_id = udev_device_get_property_value(parent, "PCI_ID");
93   if (pci_id == NULL ||
94       sscanf(pci_id, "%x:%x", &vendor_id, &chip_id) != 2) {
95      _eglLog(_EGL_WARNING, "malformed or no PCI ID");
96      goto out;
97   }
98
99   /* find the driver index */
100   for (idx = 0; driver_map[idx].driver; idx++) {
101      if (vendor_id != driver_map[idx].vendor_id)
102         continue;
103
104      if (driver_map[idx].num_chips_ids == -1)
105         goto out;
106
107      for (i = 0; i < driver_map[idx].num_chips_ids; i++) {
108         if (driver_map[idx].chip_ids[i] == chip_id)
109            goto out;
110      }
111   }
112
113out:
114   udev_device_unref(device);
115   udev_unref(udev);
116
117   if (idx >= 0) {
118      _eglLog((driver_map[idx].driver) ? _EGL_INFO : _EGL_WARNING,
119            "pci id for fd %d: %04x:%04x, driver %s",
120            fd, vendor_id, chip_id, driver_map[idx].driver);
121
122      return driver_map[idx].driver;
123   }
124#endif
125
126   _eglLog(_EGL_WARNING, "failed to get driver name for fd %d", fd);
127
128   return NULL;
129}
130
131static struct pipe_screen *
132create_drm_screen(const char *name, int fd)
133{
134   if (!name) {
135      name = drm_fd_get_screen_name(fd);
136      if (!name)
137         return NULL;
138   }
139
140   return egl_pipe_create_drm_screen(name, fd);
141}
142
143static struct pipe_screen *
144create_sw_screen(struct sw_winsys *ws)
145{
146   return egl_pipe_create_swrast_screen(ws);
147}
148
149static const struct egl_g3d_loader *
150loader_init(void)
151{
152   int i;
153
154   for (i = 0; i < ST_API_COUNT; i++)
155      egl_g3d_loader.profile_masks[i] = egl_st_get_profile_mask(i);
156
157   egl_g3d_loader.get_st_api = get_st_api;
158   egl_g3d_loader.create_drm_screen = create_drm_screen;
159   egl_g3d_loader.create_sw_screen = create_sw_screen;
160
161   return &egl_g3d_loader;
162}
163
164static void
165loader_fini(void)
166{
167   int i;
168
169   for (i = 0; i < ST_API_COUNT; i++) {
170      struct st_module *stmod = &st_modules[i];
171
172      if (stmod->stapi) {
173         egl_st_destroy_api(stmod->stapi);
174         stmod->stapi = NULL;
175      }
176      stmod->initialized = FALSE;
177   }
178}
179
180static void
181egl_g3d_unload(_EGLDriver *drv)
182{
183   egl_g3d_destroy_driver(drv);
184   loader_fini();
185}
186
187_EGLDriver *
188_EGL_MAIN(const char *args)
189{
190   const struct egl_g3d_loader *loader;
191   _EGLDriver *drv;
192
193   loader = loader_init();
194   drv = egl_g3d_create_driver(loader);
195   if (!drv) {
196      loader_fini();
197      return NULL;
198   }
199
200   drv->Name = "Gallium";
201   drv->Unload = egl_g3d_unload;
202
203   return drv;
204}
205