File: testglxcreatecontext.c

package info (click to toggle)
libglvnd 1.7.0-3
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 7,476 kB
  • sloc: xml: 52,727; ansic: 49,924; python: 852; makefile: 752; cpp: 596; sh: 109
file content (195 lines) | stat: -rw-r--r-- 6,653 bytes parent folder | download | duplicates (4)
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
/*
 * Copyright (c) 2017, NVIDIA CORPORATION.
 *
 * Permission is hereby granted, free of charge, to any person obtaining a
 * copy of this software and/or associated documentation files (the
 * "Materials"), to deal in the Materials without restriction, including
 * without limitation the rights to use, copy, modify, merge, publish,
 * distribute, sublicense, and/or sell copies of the Materials, and to
 * permit persons to whom the Materials are furnished to do so, subject to
 * the following conditions:
 *
 * The above copyright notice and this permission notice shall be included
 * unaltered in all copies or substantial portions of the Materials.
 * Any additions, deletions, or changes to the original source files
 * must be clearly indicated in accompanying documentation.
 *
 * THE MATERIALS ARE 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
 * MATERIALS OR THE USE OR OTHER DEALINGS IN THE MATERIALS.
 */

/**
 * \file
 *
 * This program tests the various GLX functions to create a context.
 */

#include <X11/Xlib.h>
#include <GL/glx.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>

#include "test_utils.h"
#include "dummy/GLX_dummy.h"

static int runTestGLX12(struct window_info *wi);
static int runTestGLX13(struct window_info *wi);
static int runTestGLXAttribsConfig(struct window_info *wi);
static int runTestGLXAttribsScreen(struct window_info *wi);
static int runTestGLXAttribsVendor(struct window_info *wi);
static int runTestCommon(struct window_info *wi, GLXContext ctx, const char *testName);

static PFNGLXCREATECONTEXTATTRIBSARBPROC ptr_glXCreateContextAttribsARB;
static PFNGLXCREATECONTEXTVENDORDUMMYPROC ptr_glXCreateContextVendorDUMMY;

int main(int argc, char **argv)
{
    Display *dpy = NULL;
    struct window_info wi = {};
    int result = 1;

    dpy = XOpenDisplay(NULL);
    if (dpy == NULL) {
        printError("No display! Please re-test with a running X server\n"
                   "and the DISPLAY environment variable set appropriately.\n");
        goto done;
    }

    // Call glXQueryServerString to make libGLX.so load the vendor library for
    // the screen before we try to load any extension functions.
    glXQueryServerString(dpy, DefaultScreen(dpy), GLX_EXTENSIONS);

    ptr_glXCreateContextAttribsARB = (PFNGLXCREATECONTEXTATTRIBSARBPROC)
        glXGetProcAddress((const GLubyte *) "glXCreateContextAttribsARB");
    if (ptr_glXCreateContextAttribsARB == NULL) {
        printError("Could not load glXCreateContextAttribsARB\n");
        goto done;
    }

    ptr_glXCreateContextVendorDUMMY = (PFNGLXCREATECONTEXTVENDORDUMMYPROC)
        glXGetProcAddress((const GLubyte *) "glXCreateContextVendorDUMMY");
    if (ptr_glXCreateContextVendorDUMMY == NULL) {
        printError("Could not load glXCreateContextVendorDUMMY\n");
        goto done;
    }

    if (!testUtilsCreateWindowConfig(dpy, &wi, DefaultScreen(dpy))) {
        printError("Failed to create window\n");
        goto done;
    }

    // Start by testing the core GLX functions, glXCreateContext and
    // glXCreateNewContext.
    result = runTestGLX12(&wi);
    if (result != 0) {
        goto done;
    }
    result = runTestGLX13(&wi);
    if (result != 0) {
        goto done;
    }

    // Next, test using glXCreateContextAttribsARB. This can dispatch one of
    // two ways. First, test dispatching using a GLXFBConfig handle.
    result = runTestGLXAttribsConfig(&wi);
    if (result != 0) {
        goto done;
    }

    // Next, test using the GLX_EXT_no_config_context extension. In this case,
    // we'll pass NULL for the GLXFBConfig parameter, but then specify a screen
    // number using an attribute.
    result = runTestGLXAttribsScreen(&wi);
    if (result != 0) {
        goto done;
    }

    // All of the above functions have dispatch stubs in libGLX.so itself, so
    // test to make sure that a vendor can provide an extension function and
    // dispatch stub to create a context.
    result = runTestGLXAttribsVendor(&wi);
    if (result != 0) {
        goto done;
    }

done:
    if (dpy != NULL) {
        if (wi.dpy != NULL) {
            testUtilsDestroyWindow(dpy, &wi);
        }
        XCloseDisplay(dpy);
    }
    return result;
}

int runTestGLX12(struct window_info *wi)
{
    GLXContext ctx = glXCreateContext(wi->dpy, wi->visinfo, NULL, True);
    return runTestCommon(wi, ctx, "glXCreateContext");
}

int runTestGLX13(struct window_info *wi)
{
    GLXContext ctx = glXCreateNewContext(wi->dpy, wi->config, GLX_RGBA_TYPE, NULL, True);
    return runTestCommon(wi, ctx, "glXCreateNewContext");
}

int runTestGLXAttribsConfig(struct window_info *wi)
{
    GLXContext ctx = ptr_glXCreateContextAttribsARB(wi->dpy, wi->config, NULL, True, NULL);
    return runTestCommon(wi, ctx, "glXCreateContextAttribsARB(config)");
}

int runTestGLXAttribsScreen(struct window_info *wi)
{
    // Create a context using the GLX_EXT_no_config_context extension. In this
    // case, we pass NULL for the GLXFBConfig parameter, but then specify a
    // screen number using an attribute.
    const int attribs[] = {
        GLX_SCREEN, wi->visinfo->screen,
        None
    };
    GLXContext ctx = ptr_glXCreateContextAttribsARB(wi->dpy, NULL, NULL, True, attribs);
    return runTestCommon(wi, ctx, "glXCreateContextAttribsARB(screen)");
}

int runTestGLXAttribsVendor(struct window_info *wi)
{
    GLXContext ctx = ptr_glXCreateContextAttribsARB(wi->dpy, wi->config, NULL, True, NULL);
    return runTestCommon(wi, ctx, "glXCreateContextVendorDUMMY");
}

int runTestCommon(struct window_info *wi, GLXContext ctx, const char *testName)
{
    int value = -1;
    int ret = 1;
    if (ctx == NULL) {
        printError("%s: failed to create context\n", testName);
        goto done;
    }

    // Call glXQueryContext to make sure that we can dispatch to this context
    if (glXQueryContext(wi->dpy, ctx, GLX_CONTEX_ATTRIB_DUMMY, &value) != Success) {
        printError("%s: glXQueryContext failed\n", testName);
        goto done;
    }
    if (value != 1) {
        printError("%s: glXQueryContext returned wrong value %d\n", testName, value);
        goto done;
    }

    printf("Test succeeded: %s\n", testName);
    ret = 0;

done:
    if (ctx != NULL) {
        glXDestroyContext(wi->dpy, ctx);
    }
    return ret;
}