1/*
2 * Copyright 2018  Emmanuele Bassi
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 epoxy_api.c
26 *
27 * Tests the Epoxy API using EGL.
28 */
29
30#ifdef __sun
31#define __EXTENSIONS__
32#else
33#define _GNU_SOURCE
34#endif
35#include <stdio.h>
36#include <string.h>
37#include <stdlib.h>
38#include <assert.h>
39#include <err.h>
40#include "epoxy/gl.h"
41#include "epoxy/egl.h"
42
43#include "egl_common.h"
44
45static bool
46make_egl_current_and_test(EGLDisplay *dpy, EGLContext ctx)
47{
48    const char *string;
49    GLuint shader;
50    bool pass = true;
51
52    eglMakeCurrent(dpy, NULL, NULL, ctx);
53
54    if (!epoxy_is_desktop_gl()) {
55        fputs("Claimed to be desktop\n", stderr);
56        pass = false;
57    }
58
59    if (epoxy_gl_version() < 20) {
60        fprintf(stderr, "Claimed to be GL version %d\n",
61                epoxy_gl_version());
62        pass = false;
63    }
64
65    if (epoxy_glsl_version() < 100) {
66        fprintf(stderr, "Claimed to have GLSL version %d\n",
67                epoxy_glsl_version());
68        pass = false;
69    }
70
71    string = (const char *)glGetString(GL_VERSION);
72    printf("GL version: %s - Epoxy: %d\n", string, epoxy_gl_version());
73
74    string = (const char *)glGetString(GL_SHADING_LANGUAGE_VERSION);
75    printf("GLSL version: %s - Epoxy: %d\n", string, epoxy_glsl_version());
76
77    shader = glCreateShader(GL_FRAGMENT_SHADER);
78    pass = glIsShader(shader);
79
80    return pass;
81}
82
83static void
84init_egl(EGLDisplay *dpy, EGLContext *out_ctx)
85{
86    static const EGLint config_attribs[] = {
87	EGL_SURFACE_TYPE, EGL_WINDOW_BIT,
88	EGL_RED_SIZE, 1,
89	EGL_GREEN_SIZE, 1,
90	EGL_BLUE_SIZE, 1,
91	EGL_RENDERABLE_TYPE, EGL_OPENGL_BIT,
92	EGL_NONE
93    };
94    static const EGLint context_attribs[] = {
95        EGL_CONTEXT_CLIENT_VERSION, 2,
96        EGL_NONE
97    };
98    EGLContext ctx;
99    EGLConfig cfg;
100    EGLint count;
101
102    if (!epoxy_has_egl_extension(dpy, "EGL_KHR_surfaceless_context"))
103        errx(77, "Test requires EGL_KHR_surfaceless_context");
104
105    if (!eglBindAPI(EGL_OPENGL_API))
106        errx(77, "Couldn't initialize EGL with desktop GL\n");
107
108    if (!eglChooseConfig(dpy, config_attribs, &cfg, 1, &count))
109        errx(77, "Couldn't get an EGLConfig\n");
110
111    ctx = eglCreateContext(dpy, cfg, NULL, context_attribs);
112    if (!ctx)
113        errx(77, "Couldn't create a GL context\n");
114
115    *out_ctx = ctx;
116}
117
118int
119main(int argc, char **argv)
120{
121    bool pass = true;
122
123    EGLContext egl_ctx;
124    EGLDisplay *dpy = get_egl_display_or_skip();
125    const char *extensions = eglQueryString(dpy, EGL_EXTENSIONS);
126    char *first_space;
127    char *an_extension;
128
129    /* We don't have any extensions guaranteed by the ABI, so for the
130     * touch test we just check if the first one is reported to be there.
131     */
132    first_space = strstr(extensions, " ");
133    if (first_space) {
134        an_extension = strndup(extensions, first_space - extensions);
135    } else {
136        an_extension = strdup(extensions);
137    }
138
139    if (!epoxy_extension_in_string(extensions, an_extension))
140        errx(1, "Implementation reported absence of %s", an_extension);
141
142    free(an_extension);
143
144    init_egl(dpy, &egl_ctx);
145    pass = make_egl_current_and_test(dpy, egl_ctx);
146
147    return pass != true;
148}
149