egldispatchstubs.c revision 01e04c3f
101e04c3fSmrg#include "egldispatchstubs.h"
201e04c3fSmrg#include "g_egldispatchstubs.h"
301e04c3fSmrg
401e04c3fSmrg#include <string.h>
501e04c3fSmrg#include <stdlib.h>
601e04c3fSmrg
701e04c3fSmrg#include "eglcurrent.h"
801e04c3fSmrg
901e04c3fSmrgstatic const __EGLapiExports *exports;
1001e04c3fSmrg
1101e04c3fSmrgconst int __EGL_DISPATCH_FUNC_COUNT = __EGL_DISPATCH_COUNT;
1201e04c3fSmrgint __EGL_DISPATCH_FUNC_INDICES[__EGL_DISPATCH_COUNT + 1];
1301e04c3fSmrg
1401e04c3fSmrgstatic int Compare(const void *l, const void *r)
1501e04c3fSmrg{
1601e04c3fSmrg    const char *s = *(const char **)r;
1701e04c3fSmrg    return strcmp(l, s);
1801e04c3fSmrg}
1901e04c3fSmrg
2001e04c3fSmrgstatic int FindProcIndex(const char *name)
2101e04c3fSmrg{
2201e04c3fSmrg    const char **match = bsearch(name, __EGL_DISPATCH_FUNC_NAMES,
2301e04c3fSmrg            __EGL_DISPATCH_COUNT, sizeof(const char *), Compare);
2401e04c3fSmrg
2501e04c3fSmrg    if (match == NULL)
2601e04c3fSmrg        return __EGL_DISPATCH_COUNT;
2701e04c3fSmrg
2801e04c3fSmrg    return match - __EGL_DISPATCH_FUNC_NAMES;
2901e04c3fSmrg}
3001e04c3fSmrg
3101e04c3fSmrgvoid __eglInitDispatchStubs(const __EGLapiExports *exportsTable)
3201e04c3fSmrg{
3301e04c3fSmrg    int i;
3401e04c3fSmrg    exports = exportsTable;
3501e04c3fSmrg    for (i=0; i<__EGL_DISPATCH_FUNC_COUNT; i++) {
3601e04c3fSmrg        __EGL_DISPATCH_FUNC_INDICES[i] = -1;
3701e04c3fSmrg    }
3801e04c3fSmrg}
3901e04c3fSmrg
4001e04c3fSmrgvoid __eglSetDispatchIndex(const char *name, int dispatchIndex)
4101e04c3fSmrg{
4201e04c3fSmrg    int index = FindProcIndex(name);
4301e04c3fSmrg    __EGL_DISPATCH_FUNC_INDICES[index] = dispatchIndex;
4401e04c3fSmrg}
4501e04c3fSmrg
4601e04c3fSmrgvoid *__eglDispatchFindDispatchFunction(const char *name)
4701e04c3fSmrg{
4801e04c3fSmrg    int index = FindProcIndex(name);
4901e04c3fSmrg    return (void *) __EGL_DISPATCH_FUNCS[index];
5001e04c3fSmrg}
5101e04c3fSmrg
5201e04c3fSmrgstatic __eglMustCastToProperFunctionPointerType FetchVendorFunc(__EGLvendorInfo *vendor,
5301e04c3fSmrg        int index, EGLint errorCode)
5401e04c3fSmrg{
5501e04c3fSmrg    __eglMustCastToProperFunctionPointerType func = NULL;
5601e04c3fSmrg
5701e04c3fSmrg    if (vendor != NULL) {
5801e04c3fSmrg        func = exports->fetchDispatchEntry(vendor, __EGL_DISPATCH_FUNC_INDICES[index]);
5901e04c3fSmrg    }
6001e04c3fSmrg    if (func == NULL) {
6101e04c3fSmrg        if (errorCode != EGL_SUCCESS) {
6201e04c3fSmrg            // Since we have no vendor, the follow-up eglGetError() call will
6301e04c3fSmrg            // end up using the GLVND error code. Set it here.
6401e04c3fSmrg            if (vendor == NULL) {
6501e04c3fSmrg                exports->setEGLError(errorCode);
6601e04c3fSmrg            }
6701e04c3fSmrg            _eglError(errorCode, __EGL_DISPATCH_FUNC_NAMES[index]);
6801e04c3fSmrg        }
6901e04c3fSmrg        return NULL;
7001e04c3fSmrg    }
7101e04c3fSmrg
7201e04c3fSmrg    if (!exports->setLastVendor(vendor)) {
7301e04c3fSmrg        // Don't bother trying to set an error code in libglvnd. If
7401e04c3fSmrg        // setLastVendor failed, then setEGLError would also fail.
7501e04c3fSmrg        _eglError(errorCode, __EGL_DISPATCH_FUNC_NAMES[index]);
7601e04c3fSmrg        return NULL;
7701e04c3fSmrg    }
7801e04c3fSmrg
7901e04c3fSmrg    return func;
8001e04c3fSmrg}
8101e04c3fSmrg
8201e04c3fSmrg__eglMustCastToProperFunctionPointerType __eglDispatchFetchByCurrent(int index)
8301e04c3fSmrg{
8401e04c3fSmrg    __EGLvendorInfo *vendor;
8501e04c3fSmrg
8601e04c3fSmrg    // Note: This is only used for the eglWait* functions. For those, if
8701e04c3fSmrg    // there's no current context, then they're supposed to do nothing but
8801e04c3fSmrg    // return success.
8901e04c3fSmrg    exports->threadInit();
9001e04c3fSmrg    vendor = exports->getCurrentVendor();
9101e04c3fSmrg    return FetchVendorFunc(vendor, index, EGL_SUCCESS);
9201e04c3fSmrg}
9301e04c3fSmrg
9401e04c3fSmrg__eglMustCastToProperFunctionPointerType __eglDispatchFetchByDisplay(EGLDisplay dpy, int index)
9501e04c3fSmrg{
9601e04c3fSmrg    __EGLvendorInfo *vendor;
9701e04c3fSmrg
9801e04c3fSmrg    exports->threadInit();
9901e04c3fSmrg    vendor = exports->getVendorFromDisplay(dpy);
10001e04c3fSmrg    return FetchVendorFunc(vendor, index, EGL_BAD_DISPLAY);
10101e04c3fSmrg}
10201e04c3fSmrg
10301e04c3fSmrg__eglMustCastToProperFunctionPointerType __eglDispatchFetchByDevice(EGLDeviceEXT dev, int index)
10401e04c3fSmrg{
10501e04c3fSmrg    __EGLvendorInfo *vendor;
10601e04c3fSmrg
10701e04c3fSmrg    exports->threadInit();
10801e04c3fSmrg    vendor = exports->getVendorFromDevice(dev);
10901e04c3fSmrg    return FetchVendorFunc(vendor, index, EGL_BAD_DEVICE_EXT);
11001e04c3fSmrg}
11101e04c3fSmrg
112