summaryrefslogtreecommitdiff
path: root/src/dispatch_common.c
blob: 1b453896843dd561fd716b1f23ef5ac82eba1ec6 (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
/*
 * Copyright © 2013 Intel Corporation
 *
 * 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 (including the next
 * paragraph) 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.
 */

/**
 * @file dispatch_common.c
 *
 * Implements common code shared by the generated GL/EGL/GLX dispatch code.
 *
 * A collection of some important specs on getting GL function pointers.
 *
 * From the linux GL ABI (http://www.opengl.org/registry/ABI/):
 *
 *     "3.4. The libraries must export all OpenGL 1.2, GLU 1.3, GLX 1.3, and
 *           ARB_multitexture entry points statically.
 *
 *      3.5. Because non-ARB extensions vary so widely and are constantly
 *           increasing in number, it's infeasible to require that they all be
 *           supported, and extensions can always be added to hardware drivers
 *           after the base link libraries are released. These drivers are
 *           dynamically loaded by libGL, so extensions not in the base
 *           library must also be obtained dynamically.
 *
 *      3.6. To perform the dynamic query, libGL also must export an entry
 *           point called
 *
 *           void (*glXGetProcAddressARB(const GLubyte *))(); 
 *
 *      The full specification of this function is available separately. It
 *      takes the string name of a GL or GLX entry point and returns a pointer
 *      to a function implementing that entry point. It is functionally
 *      identical to the wglGetProcAddress query defined by the Windows OpenGL
 *      library, except that the function pointers returned are context
 *      independent, unlike the WGL query."
 *
 * From the EGL 1.4 spec:
 *
 *    "Client API function pointers returned by eglGetProcAddress are
 *     independent of the display and the currently bound client API context,
 *     and may be used by any client API context which supports the extension.
 *
 *     eglGetProcAddress may be queried for all of the following functions:
 *
 *     • All EGL and client API extension functions supported by the
 *       implementation (whether those extensions are supported by the current
 *       client API context or not). This includes any mandatory OpenGL ES
 *       extensions.
 *
 *     eglGetProcAddress may not be queried for core (non-extension) functions
 *     in EGL or client APIs 20 .
 *
 *     For functions that are queryable with eglGetProcAddress,
 *     implementations may choose to also export those functions statically
 *     from the object libraries im- plementing those functions. However,
 *     portable clients cannot rely on this behavior.
 *
 * From the GLX 1.4 spec:
 *
 *     "glXGetProcAddress may be queried for all of the following functions:
 *
 *      • All GL and GLX extension functions supported by the implementation
 *        (whether those extensions are supported by the current context or
 *        not).
 *
 *      • All core (non-extension) functions in GL and GLX from version 1.0 up
 *        to and including the versions of those specifications supported by
 *        the implementation, as determined by glGetString(GL VERSION) and
 *        glXQueryVersion queries."
 */

#include <assert.h>
#include <stdlib.h>
#include <dlfcn.h>
#include <string.h>
#include <stdio.h>
#include "epoxy/gl.h"
#include "epoxy/glx.h"
#include "dispatch_common.h"

/* XXX: Make this thread local */
struct api local_api;

struct api *api = &local_api;

bool
epoxy_is_desktop_gl(void)
{
    const char *es_prefix = "OpenGL ES ";
    const char *version = (const char *)glGetString(GL_VERSION);

    printf("VERSION %s\n", version);
    return !strncmp(es_prefix, version, strlen(es_prefix));
}

PUBLIC int
epoxy_gl_version(void)
{
    GLint major, minor;

    glGetIntegerv(GL_MAJOR_VERSION, &major);
    glGetIntegerv(GL_MAJOR_VERSION, &minor);

    return major * 10 + minor;
}

bool
epoxy_is_glx(void)
{
    return true; /* XXX */
}

int
epoxy_glx_version(void)
{
    return 14; /* XXX */
}

static bool
epoxy_extension_in_string(const char *extension_list, const char *ext)
{
    const char *ptr = extension_list;
    int len = strlen(ext);

    /* Make sure that don't just find an extension with our name as a prefix. */
    do {
        ptr = strstr(ptr, ext);
    } while (ptr && (ptr[len] != ' ' && ptr[len] != 0));

    return ptr != NULL;
}

PUBLIC bool
epoxy_has_gl_extension(const char *ext)
{
    return epoxy_extension_in_string((const char *)glGetString(GL_EXTENSIONS),
				     ext);
}

#if 0
PUBLIC bool
epoxy_has_egl_extension(const char *ext)
{
    return epoxy_extension_in_string(eglQueryString(EGL_EXTENSIONS), ext);
}
#endif

PUBLIC bool
epoxy_has_glx_extension(const char *ext)
{
    Display *dpy = glXGetCurrentDisplay();
    int screen = 0;

    if (!dpy) {
        fprintf(stderr, "waffle needs a display!"); /* XXX */
        return false;
    }

    /* No, you can't just use glXGetClientString or glXGetServerString() here.
     * Those each tell you about one half of what's needed for an extension to
     * be supported, and glXQueryExtensionsString().
     */
    return epoxy_extension_in_string(glXQueryExtensionsString(dpy, screen), ext);
}

void *
epoxy_dlsym(const char *name)
{
	assert(api->gl_handle);
	return dlsym(api->gl_handle, name);
}

void *
epoxy_get_proc_address(const char *name)
{
	return glXGetProcAddress((const GLubyte *)name);
}

void
epoxy_glx_autoinit(void)
{
	if (api->gl_handle)
		return;

	api->gl_handle = dlopen("libGL.so.1", RTLD_LAZY | RTLD_LOCAL);
	if (!api->gl_handle) {
		fprintf(stderr, "Couldn't open libGL.so.1: %s", dlerror());
		exit(1);
	}

	api->winsys_handle = api->gl_handle;
}

void
epoxy_platform_autoinit(void)
{
	epoxy_glx_autoinit();
}