Blame src/EGL/libeglerror.c

Packit 5af8b3
/*
Packit 5af8b3
 * Copyright (c) 2016, NVIDIA CORPORATION.
Packit 5af8b3
 *
Packit 5af8b3
 * Permission is hereby granted, free of charge, to any person obtaining a
Packit 5af8b3
 * copy of this software and/or associated documentation files (the
Packit 5af8b3
 * "Materials"), to deal in the Materials without restriction, including
Packit 5af8b3
 * without limitation the rights to use, copy, modify, merge, publish,
Packit 5af8b3
 * distribute, sublicense, and/or sell copies of the Materials, and to
Packit 5af8b3
 * permit persons to whom the Materials are furnished to do so, subject to
Packit 5af8b3
 * the following conditions:
Packit 5af8b3
 *
Packit 5af8b3
 * The above copyright notice and this permission notice shall be included
Packit 5af8b3
 * unaltered in all copies or substantial portions of the Materials.
Packit 5af8b3
 * Any additions, deletions, or changes to the original source files
Packit 5af8b3
 * must be clearly indicated in accompanying documentation.
Packit 5af8b3
 *
Packit 5af8b3
 * If only executable code is distributed, then the accompanying
Packit 5af8b3
 * documentation must state that "this software is based in part on the
Packit 5af8b3
 * work of the Khronos Group."
Packit 5af8b3
 *
Packit 5af8b3
 * THE MATERIALS ARE PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
Packit 5af8b3
 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
Packit 5af8b3
 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
Packit 5af8b3
 * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
Packit 5af8b3
 * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
Packit 5af8b3
 * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
Packit 5af8b3
 * MATERIALS OR THE USE OR OTHER DEALINGS IN THE MATERIALS.
Packit 5af8b3
 */
Packit 5af8b3
Packit 5af8b3
#include "libeglerror.h"
Packit 5af8b3
Packit 5af8b3
#include <string.h>
Packit 5af8b3
#include <assert.h>
Packit 5af8b3
Packit 5af8b3
#include <EGL/egl.h>
Packit 5af8b3
#include <EGL/eglext.h>
Packit 5af8b3
Packit 5af8b3
#include "glvnd_pthread.h"
Packit 5af8b3
#include "libeglmapping.h"
Packit 5af8b3
#include "libeglcurrent.h"
Packit 5af8b3
#include "utils_misc.h"
Packit 5af8b3
Packit 5af8b3
enum
Packit 5af8b3
{
Packit 5af8b3
    __EGL_DEBUG_BIT_CRITICAL = 0x1,
Packit 5af8b3
    __EGL_DEBUG_BIT_ERROR = 0x2,
Packit 5af8b3
    __EGL_DEBUG_BIT_WARN = 0x4,
Packit 5af8b3
    __EGL_DEBUG_BIT_INFO = 0x8,
Packit 5af8b3
};
Packit 5af8b3
Packit 5af8b3
static inline unsigned int DebugBitFromType(GLenum type);
Packit 5af8b3
Packit 5af8b3
static EGLDEBUGPROCKHR debugCallback = NULL;
Packit 5af8b3
static unsigned int debugTypeEnabled = __EGL_DEBUG_BIT_CRITICAL | __EGL_DEBUG_BIT_ERROR;
Packit 5af8b3
static glvnd_rwlock_t debugLock = GLVND_RWLOCK_INITIALIZER;
Packit 5af8b3
Packit 5af8b3
static inline unsigned int DebugBitFromType(EGLenum type)
Packit 5af8b3
{
Packit 5af8b3
    assert(type >= EGL_DEBUG_MSG_CRITICAL_KHR &&
Packit 5af8b3
            type <= EGL_DEBUG_MSG_INFO_KHR);
Packit 5af8b3
    return (1 << (type - EGL_DEBUG_MSG_CRITICAL_KHR));
Packit 5af8b3
}
Packit 5af8b3
Packit 5af8b3
EGLint eglDebugMessageControlKHR(EGLDEBUGPROCKHR callback,
Packit 5af8b3
        const EGLAttrib *attrib_list)
Packit 5af8b3
{
Packit 5af8b3
    unsigned int newEnabled = debugTypeEnabled;
Packit 5af8b3
    struct glvnd_list *vendorList;
Packit 5af8b3
    __EGLvendorInfo *vendor;
Packit 5af8b3
    int i;
Packit 5af8b3
Packit 5af8b3
    __eglEntrypointCommon();
Packit 5af8b3
Packit 5af8b3
    // Parse the attribute list. Note that if (callback != NULL), then we'll
Packit 5af8b3
    // check for errors even though we otherwise ignore it.
Packit 5af8b3
    if (attrib_list != NULL) {
Packit 5af8b3
        for (i = 0; attrib_list[i] != EGL_NONE; i += 2) {
Packit 5af8b3
            if (attrib_list[i] >= EGL_DEBUG_MSG_CRITICAL_KHR &&
Packit 5af8b3
                    attrib_list[i] <= EGL_DEBUG_MSG_INFO_KHR) {
Packit 5af8b3
                if (attrib_list[i + 1]) {
Packit 5af8b3
                    newEnabled |= DebugBitFromType(attrib_list[i]);
Packit 5af8b3
                } else {
Packit 5af8b3
                    newEnabled &= ~DebugBitFromType(attrib_list[i]);
Packit 5af8b3
                }
Packit 5af8b3
            } else {
Packit 5af8b3
                // On error, set the last error code, call the current
Packit 5af8b3
                // debug callback, and return the error code.
Packit 5af8b3
                __eglReportError(EGL_BAD_ATTRIBUTE, "eglDebugMessageControlKHR", NULL,
Packit 5af8b3
                        "Invalid attribute 0x%04lx", (unsigned long) attrib_list[i]);
Packit 5af8b3
                return EGL_BAD_ATTRIBUTE;
Packit 5af8b3
            }
Packit 5af8b3
        }
Packit 5af8b3
    }
Packit 5af8b3
Packit 5af8b3
    __glvndPthreadFuncs.rwlock_wrlock(&debugLock);
Packit 5af8b3
Packit 5af8b3
    if (callback != NULL) {
Packit 5af8b3
        debugCallback = callback;
Packit 5af8b3
        debugTypeEnabled = newEnabled;
Packit 5af8b3
    } else {
Packit 5af8b3
        debugCallback = NULL;
Packit 5af8b3
        debugTypeEnabled = __EGL_DEBUG_BIT_CRITICAL | __EGL_DEBUG_BIT_ERROR;
Packit 5af8b3
    }
Packit 5af8b3
Packit 5af8b3
    // Call into each vendor library.
Packit 5af8b3
    vendorList = __eglLoadVendors();
Packit 5af8b3
    glvnd_list_for_each_entry(vendor, vendorList, entry) {
Packit 5af8b3
        if (vendor->staticDispatch.debugMessageControlKHR != NULL) {
Packit 5af8b3
            EGLint result = vendor->staticDispatch.debugMessageControlKHR(callback, attrib_list);
Packit 5af8b3
            if (result != EGL_SUCCESS && (debugTypeEnabled & __EGL_DEBUG_BIT_WARN) && callback != NULL) {
Packit 5af8b3
                char buf[200];
Packit 5af8b3
                snprintf(buf, sizeof(buf), "eglDebugMessageControlKHR failed in vendor library with error 0x%04x. Error reporting may not work correctly.", result);
Packit 5af8b3
                callback(EGL_SUCCESS, "eglDebugMessageControlKHR", EGL_DEBUG_MSG_WARN_KHR,
Packit 5af8b3
                        __eglGetThreadLabel(), NULL, buf);
Packit 5af8b3
            }
Packit 5af8b3
        } else if ((debugTypeEnabled & __EGL_DEBUG_BIT_WARN) && callback != NULL) {
Packit 5af8b3
            callback(EGL_SUCCESS, "eglDebugMessageControlKHR", EGL_DEBUG_MSG_WARN_KHR,
Packit 5af8b3
                    __eglGetThreadLabel(), NULL,
Packit 5af8b3
                    "eglDebugMessageControlKHR is not supported by vendor library. Error reporting may not work correctly.");
Packit 5af8b3
        }
Packit 5af8b3
    }
Packit 5af8b3
Packit 5af8b3
    __glvndPthreadFuncs.rwlock_unlock(&debugLock);
Packit 5af8b3
    return EGL_SUCCESS;
Packit 5af8b3
}
Packit 5af8b3
Packit 5af8b3
EGLBoolean eglQueryDebugKHR(EGLint attribute, EGLAttrib *value)
Packit 5af8b3
{
Packit 5af8b3
    __eglEntrypointCommon();
Packit 5af8b3
Packit 5af8b3
    __glvndPthreadFuncs.rwlock_rdlock(&debugLock);
Packit 5af8b3
    if (attribute >= EGL_DEBUG_MSG_CRITICAL_KHR &&
Packit 5af8b3
            attribute <= EGL_DEBUG_MSG_INFO_KHR) {
Packit 5af8b3
        if (debugTypeEnabled & DebugBitFromType(attribute)) {
Packit 5af8b3
            *value = EGL_TRUE;
Packit 5af8b3
        } else {
Packit 5af8b3
            *value = EGL_FALSE;
Packit 5af8b3
        }
Packit 5af8b3
    } else if (attribute == EGL_DEBUG_CALLBACK_KHR) {
Packit 5af8b3
        *value = (EGLAttrib) debugCallback;
Packit 5af8b3
    } else {
Packit 5af8b3
        __glvndPthreadFuncs.rwlock_unlock(&debugLock);
Packit 5af8b3
        __eglReportError(EGL_BAD_ATTRIBUTE, "eglQueryDebugKHR", NULL,
Packit 5af8b3
                "Invalid attribute 0x%04lx", (unsigned long) attribute);
Packit 5af8b3
        return EGL_FALSE;
Packit 5af8b3
    }
Packit 5af8b3
    __glvndPthreadFuncs.rwlock_unlock(&debugLock);
Packit 5af8b3
    return EGL_TRUE;
Packit 5af8b3
}
Packit 5af8b3
Packit 5af8b3
EGLint eglLabelObjectKHR(
Packit 5af8b3
        EGLDisplay display,
Packit 5af8b3
        EGLenum objectType,
Packit 5af8b3
        EGLObjectKHR object,
Packit 5af8b3
        EGLLabelKHR label)
Packit 5af8b3
{
Packit 5af8b3
    __eglEntrypointCommon();
Packit 5af8b3
Packit 5af8b3
    if (objectType == EGL_OBJECT_THREAD_KHR) {
Packit 5af8b3
        struct glvnd_list *vendorList;
Packit 5af8b3
        __EGLThreadAPIState *state = __eglGetCurrentThreadAPIState(label != NULL);
Packit 5af8b3
        __EGLvendorInfo *vendor;
Packit 5af8b3
Packit 5af8b3
        if (state != NULL) {
Packit 5af8b3
            if (state->label == label) {
Packit 5af8b3
                return EGL_SUCCESS;
Packit 5af8b3
            }
Packit 5af8b3
            state->label = label;
Packit 5af8b3
        } else {
Packit 5af8b3
            if (label == NULL) {
Packit 5af8b3
                return EGL_SUCCESS;
Packit 5af8b3
            }
Packit 5af8b3
        }
Packit 5af8b3
Packit 5af8b3
        vendorList = __eglLoadVendors();
Packit 5af8b3
        glvnd_list_for_each_entry(vendor, vendorList, entry) {
Packit 5af8b3
            if (vendor->staticDispatch.labelObjectKHR != NULL) {
Packit 5af8b3
                EGLint result = vendor->staticDispatch.labelObjectKHR(NULL, objectType, NULL, label);
Packit 5af8b3
                if (result != EGL_SUCCESS) {
Packit 5af8b3
                    __eglReportWarn("eglLabelObjectKHR", NULL,
Packit 5af8b3
                            "eglLabelObjectKHR failed in vendor library with error 0x%04x. Thread label may not be reported correctly.",
Packit 5af8b3
                            result);
Packit 5af8b3
                }
Packit 5af8b3
            } else {
Packit 5af8b3
                __eglReportWarn("eglLabelObjectKHR", NULL,
Packit 5af8b3
                        "eglLabelObjectKHR is not supported by vendor library. Thread label may not be reported correctly.");
Packit 5af8b3
            }
Packit 5af8b3
        }
Packit 5af8b3
        return EGL_SUCCESS;
Packit 5af8b3
    } else {
Packit 5af8b3
        __EGLdisplayInfo *dpyInfo = __eglLookupDisplay(display);
Packit 5af8b3
        if (dpyInfo == NULL) {
Packit 5af8b3
            __eglReportError(EGL_BAD_DISPLAY, "eglLabelObjectKHR", NULL,
Packit 5af8b3
                    "Invalid display %p", display);
Packit 5af8b3
            return EGL_BAD_DISPLAY;
Packit 5af8b3
        }
Packit 5af8b3
Packit 5af8b3
        if (objectType == EGL_OBJECT_DISPLAY_KHR) {
Packit 5af8b3
            if (display != (EGLDisplay) object) {
Packit 5af8b3
                __eglReportError(EGL_BAD_PARAMETER, "eglLabelObjectKHR", NULL,
Packit 5af8b3
                        "Display %p and object %p do not match", display, object);
Packit 5af8b3
                return EGL_BAD_PARAMETER;
Packit 5af8b3
            }
Packit 5af8b3
        }
Packit 5af8b3
Packit 5af8b3
        if (dpyInfo->vendor->staticDispatch.labelObjectKHR != NULL) {
Packit 5af8b3
            __eglSetLastVendor(dpyInfo->vendor);
Packit 5af8b3
            return dpyInfo->vendor->staticDispatch.labelObjectKHR(display, objectType, object, label);
Packit 5af8b3
        } else {
Packit 5af8b3
            __eglReportError(EGL_BAD_PARAMETER, "eglLabelObjectKHR", NULL,
Packit 5af8b3
                    "eglLabelObjectKHR is not supported by vendor library. Object label may not be reported correctly.");
Packit 5af8b3
            return EGL_BAD_PARAMETER;
Packit 5af8b3
        }
Packit 5af8b3
    }
Packit 5af8b3
}
Packit 5af8b3
Packit 5af8b3
EGLLabelKHR __eglGetThreadLabel(void)
Packit 5af8b3
{
Packit 5af8b3
    __EGLThreadAPIState *state = __eglGetCurrentThreadAPIState(EGL_FALSE);
Packit 5af8b3
    if (state != NULL) {
Packit 5af8b3
        return state->label;
Packit 5af8b3
    } else {
Packit 5af8b3
        return NULL;
Packit 5af8b3
    }
Packit 5af8b3
}
Packit 5af8b3
Packit 5af8b3
void __eglDebugReport(EGLenum error, const char *command, EGLint type, EGLLabelKHR objectLabel, const char *message, ...)
Packit 5af8b3
{
Packit 5af8b3
    EGLDEBUGPROCKHR callback = NULL;
Packit 5af8b3
Packit 5af8b3
    __glvndPthreadFuncs.rwlock_rdlock(&debugLock);
Packit 5af8b3
    if (debugTypeEnabled & DebugBitFromType(type)) {
Packit 5af8b3
        callback = debugCallback;
Packit 5af8b3
    }
Packit 5af8b3
    __glvndPthreadFuncs.rwlock_unlock(&debugLock);
Packit 5af8b3
Packit 5af8b3
    if (callback != NULL) {
Packit 5af8b3
        char *buf = NULL;
Packit 5af8b3
Packit 5af8b3
        if (message != NULL) {
Packit 5af8b3
            va_list args;
Packit 5af8b3
            va_start(args, message);
Packit 5af8b3
            if (glvnd_vasprintf(&buf, message, args) < 0) {
Packit 5af8b3
                buf = NULL;
Packit 5af8b3
            }
Packit 5af8b3
            va_end(args);
Packit 5af8b3
        }
Packit 5af8b3
        callback(error, command, type, __eglGetThreadLabel(),
Packit 5af8b3
                objectLabel, buf);
Packit 5af8b3
        free(buf);
Packit 5af8b3
    }
Packit 5af8b3
Packit 5af8b3
    if (type == EGL_DEBUG_MSG_CRITICAL_KHR || type == EGL_DEBUG_MSG_ERROR_KHR) {
Packit 5af8b3
        __eglSetError(error);
Packit 5af8b3
    }
Packit 5af8b3
}
Packit 5af8b3