targets/egl-static: Assorted cleanups and fixes.
[mesa.git] / src / gallium / targets / egl-static / egl.c
1 /*
2 * Mesa 3-D graphics library
3 * Version: 7.10
4 *
5 * Copyright (C) 2010-2011 LunarG Inc.
6 *
7 * Permission is hereby granted, free of charge, to any person obtaining a
8 * copy of this software and associated documentation files (the "Software"),
9 * to deal in the Software without restriction, including without limitation
10 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
11 * and/or sell copies of the Software, and to permit persons to whom the
12 * Software is furnished to do so, subject to the following conditions:
13 *
14 * The above copyright notice and this permission notice shall be included
15 * in all copies or substantial portions of the Software.
16 *
17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
20 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
22 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
23 * DEALINGS IN THE SOFTWARE.
24 *
25 * Authors:
26 * Chia-I Wu <olv@lunarg.com>
27 */
28
29 #include "common/egl_g3d_loader.h"
30 #include "egldriver.h"
31
32 #include "egl_pipe.h"
33 #include "egl_st.h"
34
35 static struct egl_g3d_loader egl_g3d_loader;
36
37 static struct st_module {
38 boolean initialized;
39 struct st_api *stapi;
40 } st_modules[ST_API_COUNT];
41
42 static struct st_api *
43 get_st_api(enum st_api_type api)
44 {
45 struct st_module *stmod = &st_modules[api];
46
47 if (!stmod->initialized) {
48 stmod->stapi = egl_st_create_api(api);
49 stmod->initialized = TRUE;
50 }
51
52 return stmod->stapi;
53 }
54
55 static struct st_api *
56 guess_gl_api(enum st_profile_type profile)
57 {
58 return get_st_api(ST_API_OPENGL);
59 }
60
61 static struct pipe_screen *
62 create_drm_screen(const char *name, int fd)
63 {
64 return egl_pipe_create_drm_screen(name, fd);
65 }
66
67 static struct pipe_screen *
68 create_sw_screen(struct sw_winsys *ws)
69 {
70 return egl_pipe_create_swrast_screen(ws);
71 }
72
73 static const struct egl_g3d_loader *
74 loader_init(void)
75 {
76 int i;
77
78 for (i = 0; i < ST_API_COUNT; i++)
79 egl_g3d_loader.profile_masks[i] = egl_st_get_profile_mask(i);
80
81 egl_g3d_loader.get_st_api = get_st_api;
82 egl_g3d_loader.guess_gl_api = guess_gl_api;
83 egl_g3d_loader.create_drm_screen = create_drm_screen;
84 egl_g3d_loader.create_sw_screen = create_sw_screen;
85
86 return &egl_g3d_loader;
87 }
88
89 static void
90 loader_fini(void)
91 {
92 int i;
93
94 for (i = 0; i < ST_API_COUNT; i++) {
95 struct st_module *stmod = &st_modules[i];
96
97 if (stmod->stapi) {
98 stmod->stapi->destroy(stmod->stapi);
99 stmod->stapi = NULL;
100 }
101 stmod->initialized = FALSE;
102 }
103 }
104
105 static void
106 egl_g3d_unload(_EGLDriver *drv)
107 {
108 egl_g3d_destroy_driver(drv);
109 loader_fini();
110 }
111
112 _EGLDriver *
113 _EGL_MAIN(const char *args)
114 {
115 const struct egl_g3d_loader *loader;
116 _EGLDriver *drv;
117
118 loader = loader_init();
119 drv = egl_g3d_create_driver(loader);
120 if (!drv) {
121 loader_fini();
122 return NULL;
123 }
124
125 drv->Name = "Gallium";
126 drv->Unload = egl_g3d_unload;
127
128 return drv;
129 }