Debian Codegraph @

tools/gnome-session-check-accelerated-gles-helper.c

raw symbols SCIP
1 /* -*- mode:c; c-basic-offset: 8; indent-tabs-mode: nil; -*- */
2 /*
3 *
4 * Copyright (C) 2016 Endless Mobile, Inc
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
19 *
20 * Author:
21 * Cosimo Cecchi <[email protected]>
22 */
23
24 /* for strcasestr */
25 #define _GNU_SOURCE
26
27 #include <config.h>
28
29 #include <gtk/gtk.h>
30 #include <locale.h>
31 #include <stdlib.h>
32 #include <string.h>
33
34 #ifdef GDK_WINDOWING_X11
35 #define GL_GLEXT_PROTOTYPES
36
37 #include <gdk/gdkx.h>
38 #include <GLES2/gl2.h>
39 #include <GLES2/gl2ext.h>
40 #include <EGL/egl.h>
41 #endif
42
43 #include "gnome-session-check-accelerated-common.h"
44
45 #ifdef GDK_WINDOWING_X11
46 static char *
47 get_gles_renderer (void)
48 {
49 /* Select GLESv2 config */
50 EGLint attribs[] = {
51 EGL_RENDERABLE_TYPE, EGL_OPENGL_ES2_BIT,
52 EGL_RED_SIZE, 1,
53 EGL_GREEN_SIZE, 1,
54 EGL_BLUE_SIZE, 1,
55 EGL_NONE
56 };
57
58 EGLint ctx_attribs[] = {
59 EGL_CONTEXT_CLIENT_VERSION, 2,
60 EGL_NONE
61 };
62
63 gboolean egl_inited = FALSE;
64 Display *display;
65 Window win = None;
66 EGLContext egl_ctx = NULL;
67 EGLDisplay egl_dpy = NULL;
68 EGLSurface egl_surf = NULL;
69 char *renderer = NULL;
70
71 gdk_error_trap_push ();
72
73 display = GDK_DISPLAY_XDISPLAY (gdk_display_get_default ());
74 egl_dpy = eglGetDisplay (display);
75 if (!egl_dpy) {
76 g_warning ("eglGetDisplay() failed");
77 goto out;
78 }
79
80 EGLint egl_major, egl_minor;
81 if (!eglInitialize (egl_dpy, &egl_major, &egl_minor)) {
82 g_warning ("eglInitialize() failed");
83 goto out;
84 }
85
86 egl_inited = TRUE;
87
88 EGLint num_configs;
89 EGLConfig config;
90 if (!eglChooseConfig (egl_dpy, attribs, &config, 1, &num_configs)) {
91 g_warning ("Failed to get EGL configuration");
92 goto out;
93 }
94
95 EGLint vid;
96 if (!eglGetConfigAttrib (egl_dpy, config, EGL_NATIVE_VISUAL_ID, &vid)) {
97 g_warning ("Failed to get EGL visual");
98 goto out;
99 }
100
101 /* The X window visual must match the EGL config */
102 XVisualInfo *vis_info, vis_template;
103 int num_visuals;
104 vis_template.visualid = vid;
105 vis_info = XGetVisualInfo (display, VisualIDMask, &vis_template, &num_visuals);
106 if (!vis_info) {
107 g_warning ("Failed to get X visual");
108 goto out;
109 }
110
111 XSetWindowAttributes attr;
112 attr.colormap = XCreateColormap (display, DefaultRootWindow (display),
113 vis_info->visual, AllocNone);
114 win = XCreateWindow (display, DefaultRootWindow (display),
115 0, 0, /* x, y */
116 1, 1, /* width, height */
117 0, /* border_width */
118 vis_info->depth, InputOutput,
119 vis_info->visual, CWColormap, &attr);
120 XFree (vis_info);
121
122 eglBindAPI (EGL_OPENGL_ES_API);
123
124 egl_ctx = eglCreateContext (egl_dpy, config, EGL_NO_CONTEXT, ctx_attribs);
125 if (!egl_ctx) {
126 g_warning ("Failed to create EGL context");
127 goto out;
128 }
129
130 egl_surf = eglCreateWindowSurface (egl_dpy, config, win, NULL);
131 if (!egl_surf) {
132 g_warning ("Failed to create EGL surface");
133 goto out;
134 }
135
136 if (!eglMakeCurrent (egl_dpy, egl_surf, egl_surf, egl_ctx)) {
137 g_warning ("eglMakeCurrent() failed");
138 goto out;
139 }
140
141 renderer = g_strdup ((const char *) glGetString (GL_RENDERER));
142
143 out:
144 if (egl_ctx)
145 eglDestroyContext (egl_dpy, egl_ctx);
146 if (egl_surf)
147 eglDestroySurface (egl_dpy, egl_surf);
148 if (egl_inited)
149 eglTerminate (egl_dpy);
150 if (win != None)
151 XDestroyWindow (display, win);
152
153 gdk_error_trap_pop_ignored ();
154 return renderer;
155 }
156 #endif
157
158 static gboolean print_renderer = FALSE;
159
160 static const GOptionEntry entries[] = {
161 { "print-renderer", 'p', 0, G_OPTION_ARG_NONE, &print_renderer, "Print EGL renderer name", NULL },
162 { NULL },
163 };
164
165 int
166 main (int argc,
167 char **argv)
168 {
169 char *renderer = NULL;
170 GOptionContext *context;
171 int ret = HELPER_NO_ACCEL;
172 GError *error = NULL;
173
174 setlocale (LC_ALL, "");
175
176 context = g_option_context_new (NULL);
177 g_option_context_add_group (context, gtk_get_option_group (TRUE));
178 g_option_context_add_main_entries (context, entries, NULL);
179
180 if (!g_option_context_parse (context, &argc, &argv, &error)) {
181 g_error ("Can't parse command line: %s\n", error->message);
182 g_error_free (error);
183 goto out;
184 }
185
186 #ifdef GDK_WINDOWING_X11
187 renderer = get_gles_renderer ();
188 #endif
189
190 if (renderer != NULL) {
191 if (print_renderer)
192 g_print ("%s", renderer);
193 if (strcasestr (renderer, "llvmpipe"))
194 ret = HELPER_SOFTWARE_RENDERING;
195 else
196 ret = HELPER_ACCEL;
197 }
198
199 out:
200 return ret;
201 }