nv50,nvc0: buffer resources can be bound as other things down the line
[mesa.git] / src / gallium / targets / egl-static / egl_st.c
1 /*
2 * Mesa 3-D graphics library
3 *
4 * Copyright (C) 2011 LunarG Inc.
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a
7 * copy of this software and associated documentation files (the "Software"),
8 * to deal in the Software without restriction, including without limitation
9 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
10 * and/or sell copies of the Software, and to permit persons to whom the
11 * Software is furnished to do so, subject to the following conditions:
12 *
13 * The above copyright notice and this permission notice shall be included
14 * in all copies or substantial portions of the Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
21 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
22 * DEALINGS IN THE SOFTWARE.
23 *
24 * Authors:
25 * Chia-I Wu <olv@lunarg.com>
26 */
27 #include "util/u_debug.h"
28 #include "state_tracker/st_api.h"
29 #include "egl_st.h"
30
31 #if FEATURE_GL || FEATURE_ES1 || FEATURE_ES2
32 #include "state_tracker/st_gl_api.h"
33 #endif
34
35 #if FEATURE_VG
36 #include "vg_api.h"
37 #endif
38
39 #if _EGL_EXTERNAL_GL
40
41 #include "util/u_string.h"
42 #include "util/u_dl.h"
43 #include "egldriver.h"
44 #include "egllog.h"
45
46 static struct util_dl_library *egl_st_gl_lib;
47
48 static EGLBoolean
49 dlopen_gl_lib_cb(const char *dir, size_t len, void *callback_data)
50 {
51 const char *name = (const char *) callback_data;
52 char path[1024];
53 int ret;
54
55 if (len) {
56 assert(len <= INT_MAX && "path is insanely long!");
57 ret = util_snprintf(path, sizeof(path), "%.*s/%s" UTIL_DL_EXT,
58 (int)len, dir, name);
59 }
60 else {
61 ret = util_snprintf(path, sizeof(path), "%s" UTIL_DL_EXT, name);
62 }
63
64 if (ret > 0 && ret < sizeof(path)) {
65 egl_st_gl_lib = util_dl_open(path);
66 if (egl_st_gl_lib)
67 _eglLog(_EGL_DEBUG, "loaded %s", path);
68 }
69
70 return !egl_st_gl_lib;
71 }
72
73 static struct st_api *
74 load_gl(const char *name, const char *procname)
75 {
76 struct st_api *(*create_api)(void);
77 struct st_api *stapi = NULL;
78
79 _eglSearchPathForEach(dlopen_gl_lib_cb, (void *) name);
80 if (!egl_st_gl_lib)
81 return NULL;
82
83 create_api = (struct st_api *(*)(void))
84 util_dl_get_proc_address(egl_st_gl_lib, procname);
85 if (create_api)
86 stapi = create_api();
87
88 if (!stapi) {
89 util_dl_close(egl_st_gl_lib);
90 egl_st_gl_lib = NULL;
91 }
92
93 return stapi;
94 }
95
96 static struct st_api *
97 egl_st_load_gl(void)
98 {
99 const char module[] = "st_GL";
100 const char symbol[] = "st_api_create_OpenGL";
101 struct st_api *stapi;
102
103 stapi = load_gl(module, symbol);
104
105 /* try again with libglapi.so loaded */
106 if (!stapi) {
107 struct util_dl_library *glapi = util_dl_open("libglapi" UTIL_DL_EXT);
108
109 if (glapi) {
110 _eglLog(_EGL_DEBUG, "retry with libglapi" UTIL_DL_EXT " loaded");
111
112 stapi = load_gl(module, symbol);
113 util_dl_close(glapi);
114 }
115 }
116 if (!stapi)
117 _eglLog(_EGL_WARNING, "unable to load %s" UTIL_DL_EXT, module);
118
119 return stapi;
120 }
121
122 #endif /* _EGL_EXTERNAL_GL */
123
124 struct st_api *
125 egl_st_create_api(enum st_api_type api)
126 {
127 struct st_api *stapi = NULL;
128
129 switch (api) {
130 case ST_API_OPENGL:
131 #if FEATURE_GL || FEATURE_ES1 || FEATURE_ES2
132 #if _EGL_EXTERNAL_GL
133 stapi = egl_st_load_gl();
134 #else
135 stapi = st_gl_api_create();
136 #endif
137 #endif
138 break;
139 case ST_API_OPENVG:
140 #if FEATURE_VG
141 stapi = (struct st_api *) vg_api_get();
142 #endif
143 break;
144 default:
145 assert(!"Unknown API Type\n");
146 break;
147 }
148
149 return stapi;
150 }
151
152 void
153 egl_st_destroy_api(struct st_api *stapi)
154 {
155 #if _EGL_EXTERNAL_GL
156 boolean is_gl = (stapi->api == ST_API_OPENGL);
157
158 stapi->destroy(stapi);
159
160 if (is_gl) {
161 util_dl_close(egl_st_gl_lib);
162 egl_st_gl_lib = NULL;
163 }
164 #else
165 stapi->destroy(stapi);
166 #endif
167 }