summaryrefslogtreecommitdiff
path: root/tools/mate-session-check-accelerated-gles-helper.c
blob: 4dac1d573df3ce42173d9fa0a4a03335591b68d0 (plain)
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
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
/* -*- mode:c; c-basic-offset: 8; indent-tabs-mode: nil; -*- */
/*
 *
 * Copyright (C) 2016 Endless Mobile, Inc
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
 *
 * Author:
 *   Cosimo Cecchi <cosimo@endlessm.com>
 */

/* for strcasestr */
#define _GNU_SOURCE

#include <config.h>

#include <gtk/gtk.h>
#include <locale.h>
#include <stdlib.h>
#include <string.h>

#ifdef GDK_WINDOWING_X11
#define GL_GLEXT_PROTOTYPES

#include <gdk/gdkx.h>
#include <GLES2/gl2.h>
#include <GLES2/gl2ext.h>
#include <EGL/egl.h>
#include <EGL/eglext.h>
#endif

#include "mate-session-check-accelerated-common.h"

#ifdef GDK_WINDOWING_X11
static EGLDisplay
get_display (void *native)
{
        EGLDisplay dpy = NULL;
        const char *client_exts = eglQueryString (NULL, EGL_EXTENSIONS);

        if (g_strstr_len (client_exts, -1, "EGL_KHR_platform_base")) {
                PFNEGLGETPLATFORMDISPLAYEXTPROC get_platform_display =
                        (void *) eglGetProcAddress ("eglGetPlatformDisplay");

                if (get_platform_display)
                        dpy = get_platform_display (EGL_PLATFORM_X11_KHR, native, NULL);

                if (dpy)
                        return dpy;
        }

        if (g_strstr_len (client_exts, -1, "EGL_EXT_platform_base")) {
                PFNEGLGETPLATFORMDISPLAYEXTPROC get_platform_display =
                        (void *) eglGetProcAddress ("eglGetPlatformDisplayEXT");

                if (get_platform_display)
                        dpy = get_platform_display (EGL_PLATFORM_X11_KHR, native, NULL);

                if (dpy)
                        return dpy;
        }

        return eglGetDisplay ((EGLNativeDisplayType) native);
}

static char *
get_gles_renderer (void)
{
        /* Select GLESv2 config */
        EGLint attribs[] = {
                EGL_RENDERABLE_TYPE, EGL_OPENGL_ES2_BIT,
                EGL_RED_SIZE, 1,
                EGL_GREEN_SIZE, 1,
                EGL_BLUE_SIZE, 1,
                EGL_NONE
        };

        EGLint ctx_attribs[] = {
                EGL_CONTEXT_CLIENT_VERSION, 2,
                EGL_NONE
        };

        gboolean egl_inited = FALSE;
        GdkDisplay *gdk_dpy;
        Display *display;
        Window win = None;
        EGLContext egl_ctx = NULL;
        EGLDisplay egl_dpy = NULL;
        EGLSurface egl_surf = NULL;
        char *renderer = NULL;

        gdk_dpy = gdk_display_get_default ();
        gdk_x11_display_error_trap_push (gdk_dpy);
        display = GDK_DISPLAY_XDISPLAY (gdk_dpy);
        egl_dpy = get_display (display);
        if (!egl_dpy) {
                g_warning ("eglGetDisplay() failed");
                goto out;
        }

        EGLint egl_major, egl_minor;
        if (!eglInitialize (egl_dpy, &egl_major, &egl_minor)) {
                g_warning ("eglInitialize() failed");
                goto out;
        }

        egl_inited = TRUE;

        EGLint num_configs;
        EGLConfig config;
        if (!eglChooseConfig (egl_dpy, attribs, &config, 1, &num_configs)) {
                g_warning ("Failed to get EGL configuration");
                goto out;
        }

        EGLint vid;
        if (!eglGetConfigAttrib (egl_dpy, config, EGL_NATIVE_VISUAL_ID, &vid)) {
                g_warning ("Failed to get EGL visual");
                goto out;
        }

        /* The X window visual must match the EGL config */
        XVisualInfo *vis_info, vis_template;
        int num_visuals;
        vis_template.visualid = vid;
        vis_info = XGetVisualInfo (display, VisualIDMask, &vis_template, &num_visuals);
        if (!vis_info) {
                g_warning ("Failed to get X visual");
                goto out;
        }

        XSetWindowAttributes attr;
        attr.colormap = XCreateColormap (display, DefaultRootWindow (display),
                                         vis_info->visual, AllocNone);
        win = XCreateWindow (display, DefaultRootWindow (display),
                             0, 0, /* x, y */
                             1, 1, /* width, height */
                             0,    /* border_width */
                             vis_info->depth, InputOutput,
                             vis_info->visual, CWColormap, &attr);
        XFree (vis_info);

        eglBindAPI (EGL_OPENGL_ES_API);

        egl_ctx = eglCreateContext (egl_dpy, config, EGL_NO_CONTEXT, ctx_attribs);
        if (!egl_ctx) {
                g_warning ("Failed to create EGL context");
                goto out;
        }

        egl_surf = eglCreateWindowSurface (egl_dpy, config, win, NULL);
        if (!egl_surf) {
                g_warning ("Failed to create EGL surface");
                goto out;
        }

        if (!eglMakeCurrent (egl_dpy, egl_surf, egl_surf, egl_ctx)) {
                g_warning ("eglMakeCurrent() failed");
                goto out;
        }

        renderer = g_strdup ((const char *) glGetString (GL_RENDERER));

 out:
        if (egl_ctx)
                eglDestroyContext (egl_dpy, egl_ctx);
        if (egl_surf)
                eglDestroySurface (egl_dpy, egl_surf);
        if (egl_inited)
                eglTerminate (egl_dpy);
        if (win != None)
                XDestroyWindow (display, win);

        gdk_x11_display_error_trap_pop_ignored (gdk_dpy);
        return renderer;
}
#endif

static gboolean print_renderer = FALSE;

static const GOptionEntry entries[] = {
        { "print-renderer", 'p', 0, G_OPTION_ARG_NONE, &print_renderer, "Print EGL renderer name", NULL },
        { NULL },
};

int
main (int argc,
      char **argv)
{
        GOptionContext *context;
        int ret = HELPER_NO_ACCEL;
        GError *error = NULL;

        setlocale (LC_ALL, "");

        context = g_option_context_new (NULL);
        g_option_context_add_group (context, gtk_get_option_group (TRUE));
        g_option_context_add_main_entries (context, entries, NULL);

        if (!g_option_context_parse (context, &argc, &argv, &error)) {
                g_error ("Can't parse command line: %s\n", error->message);
                g_error_free (error);
                goto out;
        }

#ifdef GDK_WINDOWING_X11
        char *renderer = get_gles_renderer ();
        if (renderer != NULL) {
                if (print_renderer)
                        g_print ("%s", renderer);
                if (strcasestr (renderer, "llvmpipe"))
                        ret = HELPER_SOFTWARE_RENDERING;
                else
                        ret = HELPER_ACCEL;
        }
#endif

out:
        return ret;
}