summaryrefslogtreecommitdiff
path: root/src/gallium/targets/egl/egl.c
blob: 61fe5069e919c6edc857ee9fc1d979d9d6abe4a0 (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
/*
 * Mesa 3-D graphics library
 * Version:  7.9
 *
 * Copyright (C) 2010 LunarG Inc.
 *
 * Permission is hereby granted, free of charge, to any person obtaining a
 * copy of this software and associated documentation files (the "Software"),
 * to deal in the Software without restriction, including without limitation
 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
 * and/or sell copies of the Software, and to permit persons to whom the
 * Software is furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included
 * in all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
 * DEALINGS IN THE SOFTWARE.
 *
 * Authors:
 *    Chia-I Wu <olv@lunarg.com>
 */

#include "util/u_debug.h"
#include "util/u_string.h"
#include "util/u_memory.h"
#include "util/u_dl.h"
#include "egldriver.h"
#include "egllog.h"

#include "state_tracker/st_api.h"
#include "state_tracker/drm_driver.h"
#include "common/egl_g3d_loader.h"

#include "egl.h"

struct egl_g3d_loader egl_g3d_loader;

static struct st_module {
   boolean initialized;
   char *name;
   struct util_dl_library *lib;
   struct st_api *stapi;
} st_modules[ST_API_COUNT];

static struct pipe_module {
   boolean initialized;
   char *name;
   struct util_dl_library *lib;
   const struct drm_driver_descriptor *drmdd;
   struct pipe_screen *(*swrast_create_screen)(struct sw_winsys *);
} pipe_modules[16];

static char *
loader_strdup(const char *s)
{
   size_t len = (s) ? strlen(s) : 0;
   char *t = MALLOC(len + 1);
   if (t) {
      memcpy(t, s, len);
      t[len] = '\0';
   }
   return t;
}

static EGLBoolean
dlopen_st_module_cb(const char *dir, size_t len, void *callback_data)
{
   struct st_module *stmod =
      (struct st_module *) callback_data;
   char path[1024];
   int ret;

   if (len) {
      ret = util_snprintf(path, sizeof(path),
            "%.*s/" ST_PREFIX "%s" UTIL_DL_EXT, len, dir, stmod->name);
   }
   else {
      ret = util_snprintf(path, sizeof(path),
            ST_PREFIX "%s" UTIL_DL_EXT, stmod->name);
   }

   if (ret > 0 && ret < sizeof(path)) {
      stmod->lib = util_dl_open(path);
      if (stmod->lib)
         _eglLog(_EGL_DEBUG, "loaded %s", path);
   }

   return !(stmod->lib);
}

static boolean
load_st_module(struct st_module *stmod,
                       const char *name, const char *procname)
{
   struct st_api *(*create_api)(void);

   if (name) {
      _eglLog(_EGL_DEBUG, "searching for st module %s", name);
      stmod->name = loader_strdup(name);
   }
   else {
      stmod->name = NULL;
   }

   if (stmod->name)
      _eglSearchPathForEach(dlopen_st_module_cb, (void *) stmod);
   else
      stmod->lib = util_dl_open(NULL);

   if (stmod->lib) {
      create_api = (struct st_api *(*)(void))
         util_dl_get_proc_address(stmod->lib, procname);
      if (create_api)
         stmod->stapi = create_api();

      if (!stmod->stapi) {
         util_dl_close(stmod->lib);
         stmod->lib = NULL;
      }
   }

   if (!stmod->stapi) {
      FREE(stmod->name);
      stmod->name = NULL;
   }

   return (stmod->stapi != NULL);
}

static EGLBoolean
dlopen_pipe_module_cb(const char *dir, size_t len, void *callback_data)
{
   struct pipe_module *pmod = (struct pipe_module *) callback_data;
   char path[1024];
   int ret;

   if (len) {
      ret = util_snprintf(path, sizeof(path),
            "%.*s/" PIPE_PREFIX "%s" UTIL_DL_EXT, len, dir, pmod->name);
   }
   else {
      ret = util_snprintf(path, sizeof(path),
            PIPE_PREFIX "%s" UTIL_DL_EXT, pmod->name);
   }
   if (ret > 0 && ret < sizeof(path)) {
      pmod->lib = util_dl_open(path);
      if (pmod->lib)
         _eglLog(_EGL_DEBUG, "loaded %s", path);
   }

   return !(pmod->lib);
}

static boolean
load_pipe_module(struct pipe_module *pmod, const char *name)
{
   pmod->name = loader_strdup(name);
   if (!pmod->name)
      return FALSE;

   _eglLog(_EGL_DEBUG, "searching for pipe module %s", pmod->name);
   _eglSearchPathForEach(dlopen_pipe_module_cb, (void *) pmod);
   if (pmod->lib) {
      pmod->drmdd = (const struct drm_driver_descriptor *)
         util_dl_get_proc_address(pmod->lib, "driver_descriptor");

      /* sanity check on the name */
      if (pmod->drmdd && strcmp(pmod->drmdd->name, pmod->name) != 0)
         pmod->drmdd = NULL;

      /* swrast */
      if (pmod->drmdd && !pmod->drmdd->driver_name) {
         pmod->swrast_create_screen =
            (struct pipe_screen *(*)(struct sw_winsys *))
            util_dl_get_proc_address(pmod->lib, "swrast_create_screen");
         if (!pmod->swrast_create_screen)
            pmod->drmdd = NULL;
      }

      if (!pmod->drmdd) {
         util_dl_close(pmod->lib);
         pmod->lib = NULL;
      }
   }

   return (pmod->drmdd != NULL);
}

static struct st_api *
get_st_api_full(enum st_api_type api, enum st_profile_type profile)
{
   struct st_module *stmod = &st_modules[api];
   const char *names[8], *symbol;
   int i, count = 0;

   if (stmod->initialized)
      return stmod->stapi;

   switch (api) {
   case ST_API_OPENGL:
      symbol = ST_CREATE_OPENGL_SYMBOL;
      names[count++] = "GL";
      break;
   case ST_API_OPENVG:
      symbol = ST_CREATE_OPENVG_SYMBOL;
      names[count++] = "OpenVG";
      break;
   default:
      symbol = NULL;
      assert(!"Unknown API Type\n");
      break;
   }

   /* NULL means the process itself */
   names[count++] = NULL;

   for (i = 0; i < count; i++) {
      if (load_st_module(stmod, names[i], symbol))
         break;
   }

   /* try again with libGL.so loaded */
   if (!stmod->stapi && api == ST_API_OPENGL) {
      struct util_dl_library *glapi = util_dl_open("libGL" UTIL_DL_EXT);

      if (glapi) {
         _eglLog(_EGL_DEBUG, "retry with libGL" UTIL_DL_EXT " loaded");
         /* skip the last name (which is NULL) */
         for (i = 0; i < count - 1; i++) {
            if (load_st_module(stmod, names[i], symbol))
               break;
         }
         util_dl_close(glapi);
      }
   }

   if (!stmod->stapi) {
      EGLint level = (egl_g3d_loader.profile_masks[api]) ?
         _EGL_WARNING : _EGL_DEBUG;
      _eglLog(level, "unable to load " ST_PREFIX "%s" UTIL_DL_EXT, names[0]);
   }

   stmod->initialized = TRUE;

   return stmod->stapi;
}

static struct st_api *
get_st_api(enum st_api_type api)
{
   enum st_profile_type profile = ST_PROFILE_DEFAULT;

   /* determine the profile from the linked libraries */
   if (api == ST_API_OPENGL) {
      struct util_dl_library *self;

      self = util_dl_open(NULL);
      if (self) {
         if (util_dl_get_proc_address(self, "glColor4x"))
            profile = ST_PROFILE_OPENGL_ES1;
         else if (util_dl_get_proc_address(self, "glShaderBinary"))
            profile = ST_PROFILE_OPENGL_ES2;
         util_dl_close(self);
      }
   }

   return get_st_api_full(api, profile);
}

static struct st_api *
guess_gl_api(enum st_profile_type profile)
{
   return get_st_api_full(ST_API_OPENGL, profile);
}

static struct pipe_module *
get_pipe_module(const char *name)
{
   struct pipe_module *pmod = NULL;
   int i;

   if (!name)
      return NULL;

   for (i = 0; i < Elements(pipe_modules); i++) {
      if (!pipe_modules[i].initialized ||
          strcmp(pipe_modules[i].name, name) == 0) {
         pmod = &pipe_modules[i];
         break;
      }
   }
   if (!pmod)
      return NULL;

   if (!pmod->initialized) {
      load_pipe_module(pmod, name);
      pmod->initialized = TRUE;
   }

   return pmod;
}

static struct pipe_screen *
create_drm_screen(const char *name, int fd)
{
   struct pipe_module *pmod = get_pipe_module(name);
   return (pmod && pmod->drmdd && pmod->drmdd->create_screen) ?
      pmod->drmdd->create_screen(fd) : NULL;
}

static struct pipe_screen *
create_sw_screen(struct sw_winsys *ws)
{
   struct pipe_module *pmod = get_pipe_module("swrast");
   return (pmod && pmod->swrast_create_screen) ?
      pmod->swrast_create_screen(ws) : NULL;
}

static const struct egl_g3d_loader *
loader_init(void)
{
   /* TODO detect at runtime? */
#if FEATURE_GL
   egl_g3d_loader.profile_masks[ST_API_OPENGL] |= ST_PROFILE_DEFAULT_MASK;
#endif
#if FEATURE_ES1
   egl_g3d_loader.profile_masks[ST_API_OPENGL] |= ST_PROFILE_OPENGL_ES1_MASK;
#endif
#if FEATURE_ES2
   egl_g3d_loader.profile_masks[ST_API_OPENGL] |= ST_PROFILE_OPENGL_ES2_MASK;
#endif
#if FEATURE_VG
   egl_g3d_loader.profile_masks[ST_API_OPENVG] |= ST_PROFILE_DEFAULT_MASK;
#endif

   egl_g3d_loader.get_st_api = get_st_api;
   egl_g3d_loader.guess_gl_api = guess_gl_api;
   egl_g3d_loader.create_drm_screen = create_drm_screen;
   egl_g3d_loader.create_sw_screen = create_sw_screen;

   return &egl_g3d_loader;
}

static void
loader_fini(void)
{
   int i;

   for (i = 0; i < ST_API_COUNT; i++) {
      struct st_module *stmod = &st_modules[i];

      if (stmod->stapi) {
         stmod->stapi->destroy(stmod->stapi);
         stmod->stapi = NULL;
      }
      if (stmod->lib) {
         util_dl_close(stmod->lib);
         stmod->lib = NULL;
      }
      if (stmod->name) {
         FREE(stmod->name);
         stmod->name = NULL;
      }
      stmod->initialized = FALSE;
   }
   for (i = 0; i < Elements(pipe_modules); i++) {
      struct pipe_module *pmod = &pipe_modules[i];

      if (!pmod->initialized)
         break;

      pmod->drmdd = NULL;
      pmod->swrast_create_screen = NULL;
      if (pmod->lib) {
         util_dl_close(pmod->lib);
         pmod->lib = NULL;
      }
      if (pmod->name) {
         FREE(pmod->name);
         pmod->name = NULL;
      }
      pmod->initialized = FALSE;
   }
}

static void
egl_g3d_unload(_EGLDriver *drv)
{
   egl_g3d_destroy_driver(drv);
   loader_fini();
}

_EGLDriver *
_eglMain(const char *args)
{
   const struct egl_g3d_loader *loader;
   _EGLDriver *drv;

   loader = loader_init();
   drv = egl_g3d_create_driver(loader);
   if (!drv) {
      loader_fini();
      return NULL;
   }

   drv->Name = "Gallium";
   drv->Unload = egl_g3d_unload;

   return drv;
}