OSDN Git Service

egl: remove unused Display field
[android-x86/external-mesa.git] / src / egl / main / egldriver.c
1 /**
2  * Functions for choosing and opening/loading device drivers.
3  */
4
5
6 #include <assert.h>
7 #include <string.h>
8 #include <dlfcn.h>
9 #include <stdio.h>
10 #include <stdlib.h>
11 #include "eglconfig.h"
12 #include "eglcontext.h"
13 #include "egldefines.h"
14 #include "egldisplay.h"
15 #include "egldriver.h"
16 #include "eglglobals.h"
17 #include "egllog.h"
18 #include "eglmisc.h"
19 #include "eglmode.h"
20 #include "eglscreen.h"
21 #include "eglstring.h"
22 #include "eglsurface.h"
23
24 #if defined(_EGL_PLATFORM_X)
25 #include "eglx.h"
26 #elif defined(_EGL_PLATFORM_WINDOWS)
27 /* XXX to do */
28 #elif defined(_EGL_PLATFORM_WINCE)
29 /* XXX to do */
30 #endif
31
32
33 const char *DefaultDriverName = ":0";
34 const char *SysFS = "/sys/class";
35
36
37
38
39 /**
40  * Given a card number, use sysfs to determine the DRI driver name.
41  */
42 static const char *
43 _eglChooseDRMDriver(int card)
44 {
45 #if 0
46    return _eglstrdup("libEGLdri");
47 #else
48    char path[2000], driverName[2000];
49    FILE *f;
50    int length;
51
52    snprintf(path, sizeof(path), "%s/drm/card%d/dri_library_name", SysFS, card);
53
54    f = fopen(path, "r");
55    if (!f)
56       return NULL;
57
58    fgets(driverName, sizeof(driverName), f);
59    fclose(f);
60
61    if ((length = strlen(driverName)) > 1) {
62       /* remove the trailing newline from sysfs */
63       driverName[length - 1] = '\0';
64       strncat(driverName, "_dri", sizeof(driverName));
65       return _eglstrdup(driverName);
66    }
67    else {
68       return NULL;
69    }   
70 #endif
71 }
72
73
74 /**
75  * Determine/return the name of the driver to use for the given _EGLDisplay.
76  *
77  * Try to be clever and determine if nativeDisplay is an Xlib Display
78  * ptr or a string (naming a driver or screen number, etc).
79  *
80  * If the first character is ':' we interpret it as a screen or card index
81  * number (i.e. ":0" or ":1", etc)
82  * Else if the first character is '!' we interpret it as specific driver name
83  * (i.e. "!r200" or "!i830".
84  *
85  * Whatever follows ':' is copied and put into dpy->DriverArgs.
86  *
87  * The caller may free() the returned string.
88  */
89 const char *
90 _eglChooseDriver(_EGLDisplay *dpy)
91 {
92    const char *displayString = (const char *) dpy->NativeDisplay;
93    const char *driverName = NULL;
94
95    if (!displayString) {
96       /* choose a default */
97       displayString = DefaultDriverName;
98    }
99
100    /* extract default DriverArgs = whatever follows ':' */
101    if (displayString[0] == '!' ||
102        displayString[0] == ':') {
103       const char *args = strchr(displayString, ':');
104       if (args)
105          dpy->DriverArgs = _eglstrdup(args + 1);
106    }
107
108    /* determine driver name now */
109    if (displayString && displayString[0] == ':' &&
110        (displayString[1] >= '0' && displayString[1] <= '9') &&
111        !displayString[2]) {
112       int card = atoi(displayString + 1);
113       driverName = _eglChooseDRMDriver(card);
114    }
115    else if (displayString && displayString[0] == '!') {
116       /* use user-specified driver name */
117       driverName = _eglstrdup(displayString + 1);
118       /* truncate driverName at ':' if present */
119       {
120          char *args = strchr(driverName, ':');
121          if (args) {
122             *args = 0;
123          }
124       }
125    }
126    else {
127       /* NativeDisplay is not a string! */
128 #if defined(_EGL_PLATFORM_X)
129       driverName = _xeglChooseDriver(dpy);
130 #elif defined(_EGL_PLATFORM_WINDOWS)
131       /* XXX to do */
132       driverName = _weglChooseDriver(dpy);
133 #elif defined(_EGL_PLATFORM_WINCE)
134       /* XXX to do */
135 #endif
136    }
137
138    return driverName;
139 }
140
141
142 /**
143  * Open/load the named driver and call its bootstrap function: _eglMain().
144  * By the time this function is called, the dpy->DriverName should have
145  * been determined.
146  *
147  * \return  new _EGLDriver object.
148  */
149 _EGLDriver *
150 _eglOpenDriver(_EGLDisplay *dpy, const char *driverName, const char *args)
151 {
152    _EGLDriver *drv;
153    _EGLMain_t mainFunc;
154    void *lib;
155    char driverFilename[1000];
156
157    assert(driverName);
158
159    /* XXX also prepend a directory path??? */
160    sprintf(driverFilename, "%s.so", driverName);
161
162    _eglLog(_EGL_DEBUG, "dlopen(%s)", driverFilename);
163    lib = dlopen(driverFilename, RTLD_NOW);
164    if (!lib) {
165       _eglLog(_EGL_WARNING, "Could not open %s (%s)",
166               driverFilename, dlerror());
167       return NULL;
168    }
169
170    mainFunc = (_EGLMain_t) dlsym(lib, "_eglMain");
171    if (!mainFunc) {
172       _eglLog(_EGL_WARNING, "_eglMain not found in %s", driverFilename);
173       dlclose(lib);
174       return NULL;
175    }
176
177    drv = mainFunc(dpy, args);
178    if (!drv) {
179       dlclose(lib);
180       return NULL;
181    }
182    /* with a recurvise open you want the inner most handle */
183    if (!drv->LibHandle)
184       drv->LibHandle = lib;
185    else
186       dlclose(lib);
187
188    return drv;
189 }
190
191
192 EGLBoolean
193 _eglCloseDriver(_EGLDriver *drv, EGLDisplay dpy)
194 {
195    void *handle = drv->LibHandle;
196    EGLBoolean b;
197
198    _eglLog(_EGL_INFO, "Closing driver");
199
200    /*
201     * XXX check for currently bound context/surfaces and delete them?
202     */
203
204    b = drv->API.Terminate(drv, dpy);
205    dlclose(handle);
206    return b;
207 }
208
209
210 /**
211  * Given a display handle, return the _EGLDriver for that display.
212  */
213 _EGLDriver *
214 _eglLookupDriver(EGLDisplay dpy)
215 {
216    _EGLDisplay *d = _eglLookupDisplay(dpy);
217    if (d)
218       return d->Driver;
219    else
220       return NULL;
221 }
222
223
224 /**
225  * Plug all the available fallback routines into the given driver's
226  * dispatch table.
227  */
228 void
229 _eglInitDriverFallbacks(_EGLDriver *drv)
230 {
231    /* If a pointer is set to NULL, then the device driver _really_ has
232     * to implement it.
233     */
234    drv->API.Initialize = NULL;
235    drv->API.Terminate = NULL;
236
237    drv->API.GetConfigs = _eglGetConfigs;
238    drv->API.ChooseConfig = _eglChooseConfig;
239    drv->API.GetConfigAttrib = _eglGetConfigAttrib;
240
241    drv->API.CreateContext = _eglCreateContext;
242    drv->API.DestroyContext = _eglDestroyContext;
243    drv->API.MakeCurrent = _eglMakeCurrent;
244    drv->API.QueryContext = _eglQueryContext;
245
246    drv->API.CreateWindowSurface = _eglCreateWindowSurface;
247    drv->API.CreatePixmapSurface = _eglCreatePixmapSurface;
248    drv->API.CreatePbufferSurface = _eglCreatePbufferSurface;
249    drv->API.DestroySurface = _eglDestroySurface;
250    drv->API.QuerySurface = _eglQuerySurface;
251    drv->API.SurfaceAttrib = _eglSurfaceAttrib;
252    drv->API.BindTexImage = _eglBindTexImage;
253    drv->API.ReleaseTexImage = _eglReleaseTexImage;
254    drv->API.SwapInterval = _eglSwapInterval;
255    drv->API.SwapBuffers = _eglSwapBuffers;
256    drv->API.CopyBuffers = _eglCopyBuffers;
257
258    drv->API.QueryString = _eglQueryString;
259    drv->API.WaitGL = _eglWaitGL;
260    drv->API.WaitNative = _eglWaitNative;
261
262 #ifdef EGL_MESA_screen_surface
263    drv->API.ChooseModeMESA = _eglChooseModeMESA; 
264    drv->API.GetModesMESA = _eglGetModesMESA;
265    drv->API.GetModeAttribMESA = _eglGetModeAttribMESA;
266    drv->API.GetScreensMESA = _eglGetScreensMESA;
267    drv->API.CreateScreenSurfaceMESA = _eglCreateScreenSurfaceMESA;
268    drv->API.ShowScreenSurfaceMESA = _eglShowScreenSurfaceMESA;
269    drv->API.ScreenPositionMESA = _eglScreenPositionMESA;
270    drv->API.QueryScreenMESA = _eglQueryScreenMESA;
271    drv->API.QueryScreenSurfaceMESA = _eglQueryScreenSurfaceMESA;
272    drv->API.QueryScreenModeMESA = _eglQueryScreenModeMESA;
273    drv->API.QueryModeStringMESA = _eglQueryModeStringMESA;
274 #endif /* EGL_MESA_screen_surface */
275
276 #ifdef EGL_VERSION_1_2
277    drv->API.CreatePbufferFromClientBuffer = _eglCreatePbufferFromClientBuffer;
278 #endif /* EGL_VERSION_1_2 */
279 }