Blame tests/testeglmakecurrent.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 <EGL/egl.h>
Packit 5af8b3
#include <EGL/eglext.h>
Packit 5af8b3
#include <GL/gl.h>
Packit 5af8b3
#include <stdio.h>
Packit 5af8b3
#include <stdlib.h>
Packit 5af8b3
#include <string.h>
Packit 5af8b3
#include <assert.h>
Packit 5af8b3
Packit 5af8b3
#include "dummy/EGL_dummy.h"
Packit 5af8b3
#include "egl_test_utils.h"
Packit 5af8b3
#include "utils_misc.h"
Packit 5af8b3
Packit 5af8b3
typedef struct {
Packit 5af8b3
    const char *vendorName;
Packit 5af8b3
    EGLDisplay dpy;
Packit 5af8b3
    EGLContext ctx;
Packit 5af8b3
} TestContextInfo;
Packit 5af8b3
Packit 5af8b3
void checkIsCurrent(const TestContextInfo *ci);
Packit 5af8b3
void testSwitchContext(const TestContextInfo *oldCi, const TestContextInfo *ci);
Packit 5af8b3
void testSwitchContextFail(const TestContextInfo *oldCi,
Packit 5af8b3
        const TestContextInfo *newCi, const TestContextInfo *failCi);
Packit 5af8b3
Packit 5af8b3
int main(int argc, char **argv)
Packit 5af8b3
{
Packit 5af8b3
    TestContextInfo contexts[3];
Packit 5af8b3
    int i;
Packit 5af8b3
Packit 5af8b3
    loadEGLExtensions();
Packit 5af8b3
Packit 5af8b3
    contexts[0].vendorName = DUMMY_VENDOR_NAMES[0];
Packit 5af8b3
    contexts[1].vendorName = DUMMY_VENDOR_NAMES[0];
Packit 5af8b3
    contexts[2].vendorName = DUMMY_VENDOR_NAMES[1];
Packit 5af8b3
Packit 5af8b3
    for (i=0; i
Packit 5af8b3
        DummyEGLContext *dctx;
Packit 5af8b3
Packit 5af8b3
        contexts[i].dpy = eglGetPlatformDisplay(EGL_DUMMY_PLATFORM,
Packit 5af8b3
                (void *) contexts[i].vendorName, NULL);
Packit 5af8b3
        if (contexts[i].dpy == EGL_NO_DISPLAY) {
Packit 5af8b3
            printf("eglGetPlatformDisplay failed\n");
Packit 5af8b3
            return 1;
Packit 5af8b3
        }
Packit 5af8b3
Packit 5af8b3
        contexts[i].ctx = eglCreateContext(contexts[i].dpy, NULL, EGL_NO_CONTEXT, NULL);
Packit 5af8b3
        if (contexts[i].ctx == EGL_NO_CONTEXT) {
Packit 5af8b3
            printf("Failed to create context for vendor %s\n", contexts[i].vendorName);
Packit 5af8b3
            return 1;
Packit 5af8b3
        }
Packit 5af8b3
Packit 5af8b3
        // Make sure the context came from the correct vendor library.
Packit 5af8b3
        dctx = (DummyEGLContext *) contexts[i].ctx;
Packit 5af8b3
        if (strcmp(dctx->vendorName, contexts[i].vendorName) != 0) {
Packit 5af8b3
            printf("EGLContext is from the wrong vendor: Expected \"%s\", but got \"%s\"\n",
Packit 5af8b3
                    contexts[i].vendorName, dctx->vendorName);
Packit 5af8b3
            return 1;
Packit 5af8b3
        }
Packit 5af8b3
        printf("Created context %d = %p\n", i, contexts[i].ctx);
Packit 5af8b3
    }
Packit 5af8b3
Packit 5af8b3
    // Test successful calls to eglMakeCurrent.
Packit 5af8b3
Packit 5af8b3
    printf("Test NULL -> ctx1\n");
Packit 5af8b3
    testSwitchContext(NULL, &contexts[0]);
Packit 5af8b3
Packit 5af8b3
    printf("Test ctx1 -> ctx1\n");
Packit 5af8b3
    testSwitchContext(&contexts[0], &contexts[0]);
Packit 5af8b3
Packit 5af8b3
    printf("Test ctx1 -> ctx2 (same vendor)\n");
Packit 5af8b3
    testSwitchContext(&contexts[0], &contexts[1]);
Packit 5af8b3
Packit 5af8b3
    printf("Test ctx2 -> ctx3 (different vendor)\n");
Packit 5af8b3
    testSwitchContext(&contexts[1], &contexts[2]);
Packit 5af8b3
Packit 5af8b3
    printf("Test ctx3 -> NULL\n");
Packit 5af8b3
    testSwitchContext(&contexts[2], NULL);
Packit 5af8b3
Packit 5af8b3
    // Next, make sure libEGL can deal with cases where the vendor's
Packit 5af8b3
    // eglMakeCurrent call fails.
Packit 5af8b3
Packit 5af8b3
    printf("Test failed NULL -> ctx1\n");
Packit 5af8b3
    testSwitchContextFail(NULL, &contexts[0], &contexts[0]);
Packit 5af8b3
Packit 5af8b3
    printf("Test failed ctx1 -> ctx2 (same vendor)\n");
Packit 5af8b3
    if (!eglMakeCurrent(contexts[0].dpy, EGL_NO_SURFACE, EGL_NO_SURFACE, contexts[0].ctx)) {
Packit 5af8b3
        printf("eglMakeCurrent failed\n");
Packit 5af8b3
        return 1;
Packit 5af8b3
    }
Packit 5af8b3
    testSwitchContextFail(&contexts[0], &contexts[1], &contexts[1]);
Packit 5af8b3
Packit 5af8b3
    printf("Test failed ctx1 -> NULL\n");
Packit 5af8b3
    testSwitchContextFail(&contexts[0], NULL, &contexts[0]);
Packit 5af8b3
Packit 5af8b3
    // If the current vendor library fails to release the current context, then
Packit 5af8b3
    // libEGL should return immediately, so the old context will still be
Packit 5af8b3
    // current.
Packit 5af8b3
    printf("Test failed ctx1 -> ctx3 (different vendor, old vendor fails)\n");
Packit 5af8b3
    testSwitchContextFail(&contexts[0], &contexts[2], &contexts[0]);
Packit 5af8b3
Packit 5af8b3
    // In this case, the old vendor library succeeds, but the new vendor
Packit 5af8b3
    // library fails. libEGL doesn't keep track of whether the previous context
Packit 5af8b3
    // is still valid, so it should be left with no current context.
Packit 5af8b3
    printf("Test failed ctx1 -> ctx3 (different vendor, new vendor fails)\n");
Packit 5af8b3
    testSwitchContextFail(NULL, &contexts[2], &contexts[2]);
Packit 5af8b3
Packit 5af8b3
    // Cleanup.
Packit 5af8b3
Packit 5af8b3
    eglMakeCurrent(EGL_NO_DISPLAY, EGL_NO_SURFACE, EGL_NO_SURFACE, EGL_NO_CONTEXT);
Packit 5af8b3
    for (i=0; i
Packit 5af8b3
        eglDestroyContext(contexts[i].dpy, contexts[i].ctx);
Packit 5af8b3
    }
Packit 5af8b3
Packit 5af8b3
    return 0;
Packit 5af8b3
}
Packit 5af8b3
Packit 5af8b3
void checkIsCurrent(const TestContextInfo *ci)
Packit 5af8b3
{
Packit 5af8b3
    EGLDisplay dpy = (ci != NULL ? ci->dpy : EGL_NO_DISPLAY);
Packit 5af8b3
    EGLContext ctx = (ci != NULL ? ci->ctx : EGL_NO_CONTEXT);
Packit 5af8b3
    EGLDisplay currDpy;
Packit 5af8b3
    EGLContext currCtx;
Packit 5af8b3
Packit 5af8b3
    // Make sure the current display and context are correct.
Packit 5af8b3
    currDpy = eglGetCurrentDisplay();
Packit 5af8b3
    if (currDpy != dpy) {
Packit 5af8b3
        printf("eglGetCurrentDisplay returned %p, expected %p\n", currDpy, dpy);
Packit 5af8b3
        exit(1);
Packit 5af8b3
    }
Packit 5af8b3
    currCtx = eglGetCurrentContext();
Packit 5af8b3
    if (currCtx != ctx) {
Packit 5af8b3
        printf("eglGetCurrentContext returned %p, expected %p\n", currCtx, ctx);
Packit 5af8b3
        exit(1);
Packit 5af8b3
    }
Packit 5af8b3
Packit 5af8b3
    if (ci != NULL) {
Packit 5af8b3
        const char *str;
Packit 5af8b3
Packit 5af8b3
        // Make sure the vendor library's view of things matches libEGL's.
Packit 5af8b3
        currCtx = ptr_eglTestDispatchDisplay(dpy, DUMMY_COMMAND_GET_CURRENT_CONTEXT, 0);
Packit 5af8b3
        if (currCtx != ctx) {
Packit 5af8b3
            printf("eglTestDispatchDisplay returned %p, expected %p\n", currCtx, ctx);
Packit 5af8b3
            exit(1);
Packit 5af8b3
        }
Packit 5af8b3
Packit 5af8b3
        // Make sure the correct dispatch table is set in libGLdispatch.
Packit 5af8b3
        str = (const char *) glGetString(GL_VENDOR);
Packit 5af8b3
        if (str != NULL) {
Packit 5af8b3
            if (strcmp(str, ci->vendorName) != 0) {
Packit 5af8b3
                printf("glGetString returned wrong name: Expected \"%s\", got \"%s\"\n",
Packit 5af8b3
                        ci->vendorName, str);
Packit 5af8b3
                exit(1);
Packit 5af8b3
            }
Packit 5af8b3
        } else {
Packit 5af8b3
            printf("glGetString returned NULL, expected \"%s\"\n", ci->vendorName);
Packit 5af8b3
            exit(1);
Packit 5af8b3
        }
Packit 5af8b3
    }
Packit 5af8b3
}
Packit 5af8b3
Packit 5af8b3
void testSwitchContext(const TestContextInfo *oldCi, const TestContextInfo *newCi)
Packit 5af8b3
{
Packit 5af8b3
    EGLDisplay newDpy = (newCi != NULL ? newCi->dpy : oldCi->dpy);
Packit 5af8b3
    EGLContext newCtx = (newCi != NULL ? newCi->ctx : EGL_NO_CONTEXT);
Packit 5af8b3
Packit 5af8b3
    if (!eglMakeCurrent(newDpy, EGL_NO_SURFACE, EGL_NO_SURFACE, newCtx)) {
Packit 5af8b3
        printf("eglMakeCurrent failed with error 0x%04x\n", eglGetError());
Packit 5af8b3
        exit(1);
Packit 5af8b3
    }
Packit 5af8b3
Packit 5af8b3
    checkIsCurrent(newCi);
Packit 5af8b3
Packit 5af8b3
    if (oldCi != NULL && newCi != NULL && oldCi->dpy != newCi->dpy) {
Packit 5af8b3
        // If we're switching vendors, then make sure the old display got the
Packit 5af8b3
        // eglMakeCurrent call to release the old context.
Packit 5af8b3
        EGLContext currCtx = ptr_eglTestDispatchDisplay(oldCi->dpy, DUMMY_COMMAND_GET_CURRENT_CONTEXT, 0);
Packit 5af8b3
        if (currCtx != EGL_NO_CONTEXT) {
Packit 5af8b3
            printf("eglGetCurrentContext returned %p, expected EGL_NO_CONTEXT\n", currCtx);
Packit 5af8b3
            exit(1);
Packit 5af8b3
        }
Packit 5af8b3
    }
Packit 5af8b3
}
Packit 5af8b3
Packit 5af8b3
void testSwitchContextFail(const TestContextInfo *oldCi,
Packit 5af8b3
        const TestContextInfo *newCi, const TestContextInfo *failCi)
Packit 5af8b3
{
Packit 5af8b3
    EGLDisplay newDpy = (newCi != NULL ? newCi->dpy : oldCi->dpy);
Packit 5af8b3
    EGLContext newCtx = (newCi != NULL ? newCi->ctx : EGL_NO_CONTEXT);
Packit 5af8b3
    EGLint error;
Packit 5af8b3
Packit 5af8b3
    assert(failCi != NULL);
Packit 5af8b3
    assert(oldCi == failCi || newCi == failCi);
Packit 5af8b3
Packit 5af8b3
    if (!ptr_eglTestDispatchDisplay(failCi->dpy,
Packit 5af8b3
                DUMMY_COMMAND_FAIL_NEXT_MAKE_CURRENT, EGL_BAD_ACCESS)) {
Packit 5af8b3
        printf("eglFailNextMakeCurrent failed\n");
Packit 5af8b3
        exit(1);
Packit 5af8b3
    }
Packit 5af8b3
Packit 5af8b3
    if (eglMakeCurrent(newDpy, EGL_NO_SURFACE, EGL_NO_SURFACE, newCtx)) {
Packit 5af8b3
        printf("eglMakeCurrent succeeded, but should have failed.\n");
Packit 5af8b3
        exit(1);
Packit 5af8b3
    }
Packit 5af8b3
Packit 5af8b3
    error = eglGetError();
Packit 5af8b3
    if (error != EGL_BAD_ACCESS) {
Packit 5af8b3
        printf("eglMakeCurrent set the wrong error\n");
Packit 5af8b3
        exit(1);
Packit 5af8b3
    }
Packit 5af8b3
Packit 5af8b3
    checkIsCurrent(oldCi);
Packit 5af8b3
}
Packit 5af8b3