1b8e80941Smrg#include "egldispatchstubs.h"
2b8e80941Smrg#include "g_egldispatchstubs.h"
3b8e80941Smrg
4b8e80941Smrg#include <string.h>
5b8e80941Smrg#include <stdlib.h>
6b8e80941Smrg
7b8e80941Smrg#include "eglcurrent.h"
8b8e80941Smrg
9b8e80941Smrgstatic const __EGLapiExports *exports;
10b8e80941Smrg
11b8e80941Smrgconst int __EGL_DISPATCH_FUNC_COUNT = __EGL_DISPATCH_COUNT;
12b8e80941Smrgint __EGL_DISPATCH_FUNC_INDICES[__EGL_DISPATCH_COUNT + 1];
13b8e80941Smrg
14b8e80941Smrgstatic int Compare(const void *l, const void *r)
15b8e80941Smrg{
16b8e80941Smrg    const char *s = *(const char **)r;
17b8e80941Smrg    return strcmp(l, s);
18b8e80941Smrg}
19b8e80941Smrg
20b8e80941Smrgstatic int FindProcIndex(const char *name)
21b8e80941Smrg{
22b8e80941Smrg    const char **match = bsearch(name, __EGL_DISPATCH_FUNC_NAMES,
23b8e80941Smrg            __EGL_DISPATCH_COUNT, sizeof(const char *), Compare);
24b8e80941Smrg
25b8e80941Smrg    if (match == NULL)
26b8e80941Smrg        return __EGL_DISPATCH_COUNT;
27b8e80941Smrg
28b8e80941Smrg    return match - __EGL_DISPATCH_FUNC_NAMES;
29b8e80941Smrg}
30b8e80941Smrg
31b8e80941Smrgvoid __eglInitDispatchStubs(const __EGLapiExports *exportsTable)
32b8e80941Smrg{
33b8e80941Smrg    int i;
34b8e80941Smrg    exports = exportsTable;
35b8e80941Smrg    for (i=0; i<__EGL_DISPATCH_FUNC_COUNT; i++) {
36b8e80941Smrg        __EGL_DISPATCH_FUNC_INDICES[i] = -1;
37b8e80941Smrg    }
38b8e80941Smrg}
39b8e80941Smrg
40b8e80941Smrgvoid __eglSetDispatchIndex(const char *name, int dispatchIndex)
41b8e80941Smrg{
42b8e80941Smrg    int index = FindProcIndex(name);
43b8e80941Smrg    __EGL_DISPATCH_FUNC_INDICES[index] = dispatchIndex;
44b8e80941Smrg}
45b8e80941Smrg
46b8e80941Smrgvoid *__eglDispatchFindDispatchFunction(const char *name)
47b8e80941Smrg{
48b8e80941Smrg    int index = FindProcIndex(name);
49b8e80941Smrg    return (void *) __EGL_DISPATCH_FUNCS[index];
50b8e80941Smrg}
51b8e80941Smrg
52b8e80941Smrgstatic __eglMustCastToProperFunctionPointerType FetchVendorFunc(__EGLvendorInfo *vendor,
53b8e80941Smrg        int index, EGLint errorCode)
54b8e80941Smrg{
55b8e80941Smrg    __eglMustCastToProperFunctionPointerType func = NULL;
56b8e80941Smrg
57b8e80941Smrg    if (vendor != NULL) {
58b8e80941Smrg        func = exports->fetchDispatchEntry(vendor, __EGL_DISPATCH_FUNC_INDICES[index]);
59b8e80941Smrg    }
60b8e80941Smrg    if (func == NULL) {
61b8e80941Smrg        if (errorCode != EGL_SUCCESS) {
62b8e80941Smrg            // Since we have no vendor, the follow-up eglGetError() call will
63b8e80941Smrg            // end up using the GLVND error code. Set it here.
64b8e80941Smrg            if (vendor == NULL) {
65b8e80941Smrg                exports->setEGLError(errorCode);
66b8e80941Smrg            }
67b8e80941Smrg            _eglError(errorCode, __EGL_DISPATCH_FUNC_NAMES[index]);
68b8e80941Smrg        }
69b8e80941Smrg        return NULL;
70b8e80941Smrg    }
71b8e80941Smrg
72b8e80941Smrg    if (!exports->setLastVendor(vendor)) {
73b8e80941Smrg        // Don't bother trying to set an error code in libglvnd. If
74b8e80941Smrg        // setLastVendor failed, then setEGLError would also fail.
75b8e80941Smrg        _eglError(errorCode, __EGL_DISPATCH_FUNC_NAMES[index]);
76b8e80941Smrg        return NULL;
77b8e80941Smrg    }
78b8e80941Smrg
79b8e80941Smrg    return func;
80b8e80941Smrg}
81b8e80941Smrg
82b8e80941Smrg__eglMustCastToProperFunctionPointerType __eglDispatchFetchByCurrent(int index)
83b8e80941Smrg{
84b8e80941Smrg    __EGLvendorInfo *vendor;
85b8e80941Smrg
86b8e80941Smrg    // Note: This is only used for the eglWait* functions. For those, if
87b8e80941Smrg    // there's no current context, then they're supposed to do nothing but
88b8e80941Smrg    // return success.
89b8e80941Smrg    exports->threadInit();
90b8e80941Smrg    vendor = exports->getCurrentVendor();
91b8e80941Smrg    return FetchVendorFunc(vendor, index, EGL_SUCCESS);
92b8e80941Smrg}
93b8e80941Smrg
94b8e80941Smrg__eglMustCastToProperFunctionPointerType __eglDispatchFetchByDisplay(EGLDisplay dpy, int index)
95b8e80941Smrg{
96b8e80941Smrg    __EGLvendorInfo *vendor;
97b8e80941Smrg
98b8e80941Smrg    exports->threadInit();
99b8e80941Smrg    vendor = exports->getVendorFromDisplay(dpy);
100b8e80941Smrg    return FetchVendorFunc(vendor, index, EGL_BAD_DISPLAY);
101b8e80941Smrg}
102b8e80941Smrg
103b8e80941Smrg__eglMustCastToProperFunctionPointerType __eglDispatchFetchByDevice(EGLDeviceEXT dev, int index)
104b8e80941Smrg{
105b8e80941Smrg    __EGLvendorInfo *vendor;
106b8e80941Smrg
107b8e80941Smrg    exports->threadInit();
108b8e80941Smrg    vendor = exports->getVendorFromDevice(dev);
109b8e80941Smrg    return FetchVendorFunc(vendor, index, EGL_BAD_DEVICE_EXT);
110b8e80941Smrg}
111b8e80941Smrg
112