1/*
2 * Copyright © 2014 Intel Corporation
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 * and/or sell copies of the Software, and to permit persons to whom the
9 * Software is furnished to do so, subject to the following conditions:
10 *
11 * The above copyright notice and this permission notice (including the next
12 * paragraph) shall be included in all copies or substantial portions of the
13 * Software.
14 *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
18 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
21 * IN THE SOFTWARE.
22 */
23
24/**
25 * @file egl_gl.c
26 *
27 * Tests that epoxy works with EGL using desktop OpenGL.
28 */
29
30#define _GNU_SOURCE
31
32#include <stdio.h>
33#include <string.h>
34#include <stdlib.h>
35#include <assert.h>
36#include <err.h>
37#include <dlfcn.h>
38#include "epoxy/gl.h"
39#include "epoxy/egl.h"
40#include "epoxy/glx.h"
41
42#include "egl_common.h"
43#include "glx_common.h"
44#include "dlwrap.h"
45
46static bool
47make_egl_current_and_test(EGLDisplay *dpy, EGLContext ctx)
48{
49    const char *string;
50    GLuint shader;
51    bool pass = true;
52
53    eglMakeCurrent(dpy, NULL, NULL, ctx);
54
55    if (!epoxy_is_desktop_gl()) {
56        fputs("Claimed to be desktop\n", stderr);
57        pass = false;
58    }
59
60    if (epoxy_gl_version() < 20) {
61        fprintf(stderr, "Claimed to be GL version %d\n",
62                epoxy_gl_version());
63        pass = false;
64    }
65
66    string = (const char *)glGetString(GL_VERSION);
67    printf("GL version: %s\n", string);
68
69    shader = glCreateShader(GL_FRAGMENT_SHADER);
70    pass = glIsShader(shader);
71
72    return pass;
73}
74
75static void
76init_egl(EGLDisplay **out_dpy, EGLContext *out_ctx)
77{
78    EGLDisplay *dpy = get_egl_display_or_skip();
79    static const EGLint config_attribs[] = {
80	EGL_SURFACE_TYPE, EGL_WINDOW_BIT,
81	EGL_RED_SIZE, 1,
82	EGL_GREEN_SIZE, 1,
83	EGL_BLUE_SIZE, 1,
84	EGL_RENDERABLE_TYPE, EGL_OPENGL_BIT,
85	EGL_NONE
86    };
87    static const EGLint context_attribs[] = {
88        EGL_CONTEXT_CLIENT_VERSION, 2,
89        EGL_NONE
90    };
91    EGLContext ctx;
92    EGLConfig cfg;
93    EGLint count;
94
95    if (!epoxy_has_egl_extension(dpy, "EGL_KHR_surfaceless_context"))
96        errx(77, "Test requires EGL_KHR_surfaceless_context");
97
98    if (!eglBindAPI(EGL_OPENGL_API))
99        errx(77, "Couldn't initialize EGL with desktop GL\n");
100
101    if (!eglChooseConfig(dpy, config_attribs, &cfg, 1, &count))
102        errx(77, "Couldn't get an EGLConfig\n");
103
104    ctx = eglCreateContext(dpy, cfg, NULL, context_attribs);
105    if (!ctx)
106        errx(77, "Couldn't create a GL context\n");
107
108    *out_dpy = dpy;
109    *out_ctx = ctx;
110}
111
112int
113main(int argc, char **argv)
114{
115    bool pass = true;
116    EGLDisplay *egl_dpy;
117    EGLContext egl_ctx;
118
119    /* Force epoxy to have loaded both EGL and GLX libs already -- we
120     * can't assume anything about symbol resolution based on having
121     * EGL or GLX loaded.
122     */
123    (void)glXGetCurrentContext();
124    (void)eglGetCurrentContext();
125
126    init_egl(&egl_dpy, &egl_ctx);
127    pass = make_egl_current_and_test(egl_dpy, egl_ctx) && pass;
128
129    return pass != true;
130}
131