gbm: Add dri backend
[mesa.git] / src / gbm / main / backend.c
1 /*
2 * Copyright © 2011 Intel Corporation
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 * and/or sell copies of the Software, and to permit persons to whom the
9 * Software is furnished to do so, subject to the following conditions:
10 *
11 * The above copyright notice and this permission notice (including the next
12 * paragraph) shall be included in all copies or substantial portions of the
13 * Software.
14 *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
16 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
17 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
18 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
19 * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
20 * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
22 * DEALINGS IN THE SOFTWARE.
23 *
24 * Authors:
25 * Benjamin Franzke <benjaminfranzke@googlemail.com>
26 */
27
28 #include <stdio.h>
29 #include <stddef.h>
30 #include <stdlib.h>
31 #include <string.h>
32 #include <limits.h>
33 #include <dlfcn.h>
34
35 #include "backend.h"
36
37 #define ARRAY_SIZE(a) (sizeof(a)/sizeof((a)[0]))
38
39 extern const struct gbm_backend gbm_dri_backend;
40
41 struct backend_desc {
42 const char *name;
43 const struct gbm_backend *builtin;
44 };
45
46 static const struct backend_desc backends[] = {
47 { "gbm_dri.so", &gbm_dri_backend },
48 };
49
50 static const void *
51 load_backend(const struct backend_desc *backend)
52 {
53 char path[PATH_MAX];
54 const void *init = NULL;
55 void *module;
56 const char *name;
57 const char *entrypoint = "gbm_backend";
58
59 if (backend == NULL)
60 return NULL;
61
62 name = backend->name;
63
64 if (backend->builtin) {
65 init = backend->builtin;
66 } else {
67 if (name[0] != '/')
68 snprintf(path, sizeof path, MODULEDIR "/%s", name);
69 else
70 snprintf(path, sizeof path, "%s", name);
71
72 module = dlopen(path, RTLD_NOW | RTLD_GLOBAL);
73 if (!module) {
74 fprintf(stderr,
75 "failed to load module: %s\n", dlerror());
76 return NULL;
77 }
78
79 init = dlsym(module, entrypoint);
80 if (!init)
81 return NULL;
82 }
83
84 return init;
85 }
86
87 static const struct backend_desc *
88 find_backend(const char *name)
89 {
90 const struct backend_desc *backend = NULL;
91 int i;
92
93 for (i = 0; i < ARRAY_SIZE(backends); ++i) {
94 if (strcmp(backends[i].name, name) == 0) {
95 backend = &backends[i];
96 break;
97 }
98 }
99
100 return backend;
101 }
102
103 struct gbm_device *
104 _gbm_create_device(int fd)
105 {
106 const struct gbm_backend *backend = NULL;
107 struct gbm_device *dev = NULL;
108 int i;
109 const char *b;
110
111 b = getenv("GBM_BACKEND");
112 if (b)
113 backend = load_backend(find_backend(b));
114
115 if (backend)
116 dev = backend->create_device(fd);
117
118 for (i = 0; i < ARRAY_SIZE(backends) && dev == NULL; ++i) {
119 backend = load_backend(&backends[i]);
120 if (backend == NULL)
121 continue;
122
123 dev = backend->create_device(fd);
124 }
125
126 return dev;
127 }