summaryrefslogtreecommitdiff
path: root/src/egl/main/egldriver.c
blob: 43b1f519034b75793ce120d00b328bc7e767857f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
/**
 * Functions for choosing and opening/loading device drivers.
 */


#include <assert.h>
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#include "eglconfig.h"
#include "eglcontext.h"
#include "egldefines.h"
#include "egldisplay.h"
#include "egldriver.h"
#include "eglglobals.h"
#include "egllog.h"
#include "eglmisc.h"
#include "eglmode.h"
#include "eglscreen.h"
#include "eglstring.h"
#include "eglsurface.h"

#if defined(_EGL_PLATFORM_X)
#include <dlfcn.h>
#include "eglx.h"
#elif defined(_EGL_PLATFORM_WINDOWS)
/* Use static linking on Windows for now */
#define WINDOWS_STATIC_LINK
#endif

/**
 * Wrappers for dlopen/dlclose()
 */
#if defined(_EGL_PLATFORM_WINDOWS)
#ifdef WINDOWS_STATIC_LINK
   static const char *DefaultDriverName = "Windows EGL Static Library";
#else
   /* XXX Need to decide how to do dynamic name lookup on Windows */
   static const char *DefaultDriverName = "TBD";
#endif
   static const char *SysFS = NULL;
   typedef HMODULE lib_handle;

   static HMODULE
   open_library(const char *filename)
   {
#ifdef WINDOWS_STATIC_LINK
      return 0;
#else
      return LoadLibrary(filename);
#endif
   }

   static void
   close_library(HMODULE lib)
   {
#ifdef WINDOWS_STATIC_LINK
#else
      FreeLibrary(lib);
#endif
   }

#elif defined(_EGL_PLATFORM_X)
   static const char *DefaultDriverName = ":0";
   static const char *SysFS = "/sys/class";

   typedef void * lib_handle;

   static void *
   open_library(const char *filename)
   {
      return dlopen(filename, RTLD_LAZY);
   }

   static void
   close_library(void *lib)
   {
      dlclose(lib);
   }
   
#endif

/**
 * Given a card number, use sysfs to determine the DRI driver name.
 */
const char *
_eglChooseDRMDriver(int card)
{
#if 0
   return _eglstrdup("libEGLdri");
#else
   char path[2000], driverName[2000];
   FILE *f;
   int length;

   snprintf(path, sizeof(path), "%s/drm/card%d/dri_library_name", SysFS, card);

   f = fopen(path, "r");
   if (!f)
      return NULL;

   fgets(driverName, sizeof(driverName), f);
   fclose(f);

   if ((length = strlen(driverName)) > 1) {
      /* remove the trailing newline from sysfs */
      driverName[length - 1] = '\0';
      strncat(driverName, "_dri", sizeof(driverName));
      return _eglstrdup(driverName);
   }
   else {
      return NULL;
   }   
#endif
}

/**
 * XXX this function is totally subject change!!!
 *
 *
 * Determine/return the name of the driver to use for the given _EGLDisplay.
 *
 * Try to be clever and determine if nativeDisplay is an Xlib Display
 * ptr or a string (naming a driver or screen number, etc).
 *
 * If the first character is ':' we interpret it as a screen or card index
 * number (i.e. ":0" or ":1", etc)
 * Else if the first character is '!' we interpret it as specific driver name
 * (i.e. "!r200" or "!i830".
 *
 * Whatever follows ':' is copied and put into dpy->DriverArgs.
 *
 * The caller may free() the returned string.
 */
const char *
_eglChooseDriver(_EGLDisplay *dpy)
{
   /* Under Windows, the NativeDisplay is an HDC handle, therefore */
   /* it can't be interpreted as a string or a pointer. */
#if defined(_EGL_PLATFORM_WINDOWS)
   const char *displayString = NULL;
#else
   const char *displayString = (const char *) dpy->NativeDisplay;
#endif
   const char *driverName = NULL;

   (void) DefaultDriverName;

#if defined(_EGL_PLATFORM_X)
   /* First, if the EGL_DRIVER env var is set, use that */
   driverName = getenv("EGL_DRIVER");
   if (driverName)
      return _eglstrdup(driverName);
#endif

#if 0
   if (!displayString) {
      /* choose a default */
      displayString = DefaultDriverName;
   }
#endif
   /* extract default DriverArgs = whatever follows ':' */
   if (displayString &&
       (displayString[0] == '!' ||
        displayString[0] == ':')) {
      const char *args = strchr(displayString, ':');
      if (args)
         dpy->DriverArgs = _eglstrdup(args + 1);
   }

   /* determine driver name now */
   if (displayString && displayString[0] == ':' &&
       (displayString[1] >= '0' && displayString[1] <= '9') &&
       !displayString[2]) {
      int card = atoi(displayString + 1);
      driverName = _eglChooseDRMDriver(card);
   }
   else if (displayString && displayString[0] == '!') {
      /* use user-specified driver name */
      driverName = _eglstrdup(displayString + 1);
      /* truncate driverName at ':' if present */
      {
         char *args = strchr(driverName, ':');
         if (args) {
            *args = 0;
         }
      }
   }
   else 
   {
      /* NativeDisplay is not a string! */
#if defined(_EGL_PLATFORM_X)
      driverName = _xeglChooseDriver(dpy);
#else
      driverName = DefaultDriverName;
#endif
   }

   return driverName;
}


/**
 * Open/load the named driver and call its bootstrap function: _eglMain().
 * By the time this function is called, the dpy->DriverName should have
 * been determined.
 *
 * \return  new _EGLDriver object.
 */
_EGLDriver *
_eglOpenDriver(_EGLDisplay *dpy, const char *driverName, const char *args)
{
   _EGLDriver *drv;
   _EGLMain_t mainFunc;
   lib_handle lib;
   char driverFilename[1000];

   assert(driverName);

#if defined(_EGL_PLATFORM_WINDOWS)
/* Use static linking on Windows for now */
#ifdef WINDOWS_STATIC_LINK
   lib = 0;
   mainFunc = (_EGLMain_t)_eglMain;
#else
   /* XXX untested */
   sprintf(driverFilename, "%s.dll", driverName);
   _eglLog(_EGL_DEBUG, "dlopen(%s)", driverFilename);
   lib = open_library(driverFilename);
   if (!lib) {
      _eglLog(_EGL_WARNING, "Could not open %s",
              driverFilename);
      return NULL;
   }
   mainFunc = (_EGLMain_t) GetProcAddress(lib, "_eglMain");
#endif
#elif defined(_EGL_PLATFORM_X)
   /* XXX also prepend a directory path??? */
   sprintf(driverFilename, "%s.so", driverName);
   _eglLog(_EGL_DEBUG, "dlopen(%s)", driverFilename);
   lib = open_library(driverFilename);
   if (!lib) {
      _eglLog(_EGL_WARNING, "Could not open %s (%s)",
              driverFilename, dlerror());
      return NULL;
   }
   mainFunc = (_EGLMain_t) dlsym(lib, "_eglMain");
#endif

   if (!mainFunc) {
      _eglLog(_EGL_WARNING, "_eglMain not found in %s", driverFilename);
      close_library(lib);
      return NULL;
   }

   drv = mainFunc(dpy, args);
   if (!drv) {
      close_library(lib);
      return NULL;
   }

   /* with a recurvise open you want the inner most handle */
   if (!drv->LibHandle) {
      drv->LibHandle = lib;
   }
   else {
      close_library(lib);
   }

   /* update the global notion of supported APIs */
   _eglGlobal.ClientAPIsMask |= drv->ClientAPIsMask;

   _eglSaveDriver(drv);

   return drv;
}


EGLBoolean
_eglCloseDriver(_EGLDriver *drv, EGLDisplay dpy)
{
   void *handle = drv->LibHandle;
   EGLBoolean b;

   _eglLog(_EGL_DEBUG, "Closing %s", drv->Name);

   /*
    * XXX check for currently bound context/surfaces and delete them?
    */

   b = drv->API.Terminate(drv, dpy);

   close_library(handle);

   return b;
}


/**
 * Save the given driver pointer in the list of all known drivers.
 */
void
_eglSaveDriver(_EGLDriver *drv)
{
   _eglGlobal.Drivers[ _eglGlobal.NumDrivers++ ] = drv;
}


/**
 * Given a display handle, return the _EGLDriver for that display.
 */
_EGLDriver *
_eglLookupDriver(EGLDisplay dpy)
{
   _EGLDisplay *d = _eglLookupDisplay(dpy);
   if (d)
      return d->Driver;
   else
      return NULL;
}


/**
 * Plug all the available fallback routines into the given driver's
 * dispatch table.
 */
void
_eglInitDriverFallbacks(_EGLDriver *drv)
{
   /* If a pointer is set to NULL, then the device driver _really_ has
    * to implement it.
    */
   drv->API.Initialize = NULL;
   drv->API.Terminate = NULL;

   drv->API.GetConfigs = _eglGetConfigs;
   drv->API.ChooseConfig = _eglChooseConfig;
   drv->API.GetConfigAttrib = _eglGetConfigAttrib;

   drv->API.CreateContext = _eglCreateContext;
   drv->API.DestroyContext = _eglDestroyContext;
   drv->API.MakeCurrent = _eglMakeCurrent;
   drv->API.QueryContext = _eglQueryContext;

   drv->API.CreateWindowSurface = _eglCreateWindowSurface;
   drv->API.CreatePixmapSurface = _eglCreatePixmapSurface;
   drv->API.CreatePbufferSurface = _eglCreatePbufferSurface;
   drv->API.DestroySurface = _eglDestroySurface;
   drv->API.QuerySurface = _eglQuerySurface;
   drv->API.SurfaceAttrib = _eglSurfaceAttrib;
   drv->API.BindTexImage = _eglBindTexImage;
   drv->API.ReleaseTexImage = _eglReleaseTexImage;
   drv->API.SwapInterval = _eglSwapInterval;
   drv->API.SwapBuffers = _eglSwapBuffers;
   drv->API.CopyBuffers = _eglCopyBuffers;

   drv->API.QueryString = _eglQueryString;
   drv->API.WaitGL = _eglWaitGL;
   drv->API.WaitNative = _eglWaitNative;

#ifdef EGL_MESA_screen_surface
   drv->API.ChooseModeMESA = _eglChooseModeMESA; 
   drv->API.GetModesMESA = _eglGetModesMESA;
   drv->API.GetModeAttribMESA = _eglGetModeAttribMESA;
   drv->API.GetScreensMESA = _eglGetScreensMESA;
   drv->API.CreateScreenSurfaceMESA = _eglCreateScreenSurfaceMESA;
   drv->API.ShowScreenSurfaceMESA = _eglShowScreenSurfaceMESA;
   drv->API.ScreenPositionMESA = _eglScreenPositionMESA;
   drv->API.QueryScreenMESA = _eglQueryScreenMESA;
   drv->API.QueryScreenSurfaceMESA = _eglQueryScreenSurfaceMESA;
   drv->API.QueryScreenModeMESA = _eglQueryScreenModeMESA;
   drv->API.QueryModeStringMESA = _eglQueryModeStringMESA;
#endif /* EGL_MESA_screen_surface */

#ifdef EGL_VERSION_1_2
   drv->API.CreatePbufferFromClientBuffer = _eglCreatePbufferFromClientBuffer;
#endif /* EGL_VERSION_1_2 */
}



/**
 * Try to determine which EGL APIs (OpenGL, OpenGL ES, OpenVG, etc)
 * are supported on the system by looking for standard library names.
 */
EGLint
_eglFindAPIs(void)
{
   EGLint mask = 0x0;
   lib_handle lib;
#if defined(_EGL_PLATFORM_WINDOWS)
   /* XXX not sure about these names */
   const char *es1_libname = "libGLESv1_CM.dll";
   const char *es2_libname = "libGLESv2.dll";
   const char *gl_libname = "OpenGL32.dll";
   const char *vg_libname = "libOpenVG.dll";
#elif defined(_EGL_PLATFORM_X)
   const char *es1_libname = "libGLESv1_CM.so";
   const char *es2_libname = "libGLESv2.so";
   const char *gl_libname = "libGL.so";
   const char *vg_libname = "libOpenVG.so";
#endif

   if ((lib = open_library(es1_libname))) {
      close_library(lib);
      mask |= EGL_OPENGL_ES_BIT;
   }

   if ((lib = open_library(es2_libname))) {
      close_library(lib);
      mask |= EGL_OPENGL_ES2_BIT;
   }

   if ((lib = open_library(gl_libname))) {
      close_library(lib);
      mask |= EGL_OPENGL_BIT;
   }

   if ((lib = open_library(vg_libname))) {
      close_library(lib);
      mask |= EGL_OPENVG_BIT;
   }

   return mask;
}