1 /* -*- mode:c; c-basic-offset: 8; indent-tabs-mode: nil; -*- */
2 /* Tool to set the property _GNOME_SESSION_ACCELERATED on the root window */
3 /*
4  * Copyright (C) 2011 Red Hat, 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  *   Colin Walters <walters@verbum.org>
22  */
23 
24 #include <string.h>
25 #include <unistd.h>
26 #include <stdlib.h>
27 
28 #include <gtk/gtk.h>
29 #include <epoxy/gl.h>
30 #include <gdk/gdkx.h>
31 #include <X11/Xatom.h>
32 #include <sys/wait.h>
33 
34 #include "gnome-session-check-accelerated-common.h"
35 
36 /* Wait up to this long for a running check to finish */
37 #define PROPERTY_CHANGE_TIMEOUT 5000
38 
39 /* Values used for the _GNOME_SESSION_ACCELERATED root window property */
40 #define NO_ACCEL            0
41 #define HAVE_ACCEL          1
42 #define ACCEL_CHECK_RUNNING 2
43 
44 static Atom is_accelerated_atom;
45 static Atom is_software_rendering_atom;
46 static Atom renderer_atom;
47 static gboolean property_changed;
48 
49 static gboolean
on_property_notify_timeout(gpointer data)50 on_property_notify_timeout (gpointer data)
51 {
52         gtk_main_quit ();
53         return FALSE;
54 }
55 
56 static GdkFilterReturn
property_notify_filter(GdkXEvent * xevent,GdkEvent * event,gpointer data)57 property_notify_filter (GdkXEvent *xevent,
58                         GdkEvent  *event,
59                         gpointer   data)
60 {
61         XPropertyEvent *ev = xevent;
62 
63         if (ev->type == PropertyNotify && ev->atom == is_accelerated_atom) {
64                 property_changed = TRUE;
65                 gtk_main_quit ();
66         }
67 
68         return GDK_FILTER_CONTINUE;
69 }
70 
71 static gboolean
wait_for_property_notify(void)72 wait_for_property_notify (void)
73 {
74         GdkDisplay *display;
75         GdkScreen *screen;
76         GdkWindow *root;
77         Window rootwin;
78 
79         property_changed = FALSE;
80 
81         display = gdk_display_get_default ();
82         screen = gdk_display_get_default_screen (display);
83         root = gdk_screen_get_root_window (screen);
84         rootwin = gdk_x11_window_get_xid (root);
85 
86         XSelectInput (GDK_DISPLAY_XDISPLAY (display), rootwin, PropertyChangeMask);
87         gdk_window_add_filter (root, property_notify_filter, NULL);
88         g_timeout_add (PROPERTY_CHANGE_TIMEOUT, on_property_notify_timeout, NULL);
89 
90         gtk_main ();
91 
92         return property_changed;
93 }
94 
95 static char *
get_gtk_gles_renderer(void)96 get_gtk_gles_renderer (void)
97 {
98         GtkWidget *win;
99         GdkGLContext *context;
100         char *renderer = NULL;
101 
102         win = gtk_window_new (GTK_WINDOW_TOPLEVEL);
103         gtk_widget_realize (win);
104         context = gdk_window_create_gl_context (gtk_widget_get_window (win), NULL);
105         if (!context)
106                 return NULL;
107         gdk_gl_context_make_current (context);
108         renderer = g_strdup ((char *) glGetString (GL_RENDERER));
109         gdk_gl_context_clear_current ();
110         g_object_unref (context);
111 
112         return renderer;
113 }
114 
115 static gboolean
is_discrete_gpu_check(void)116 is_discrete_gpu_check (void)
117 {
118 	const char *dri_prime;
119 
120 	dri_prime = g_getenv ("DRI_PRIME");
121 	if (!dri_prime)
122 		return FALSE;
123 	if (*dri_prime != '1')
124 		return FALSE;
125 	return TRUE;
126 }
127 
128 int
main(int argc,char ** argv)129 main (int argc, char **argv)
130 {
131         GdkDisplay *display = NULL;
132         int estatus;
133         char *gl_helper_argv[] = { LIBEXECDIR "/gnome-session-check-accelerated-gl-helper", "--print-renderer", NULL };
134         char *gles_helper_argv[] = { LIBEXECDIR "/gnome-session-check-accelerated-gles-helper", "--print-renderer", NULL };
135         char *renderer_string = NULL;
136         char *gl_renderer_string = NULL, *gles_renderer_string = NULL;
137         gboolean gl_software_rendering = FALSE, gles_software_rendering = FALSE;
138         Window rootwin;
139         glong is_accelerated, is_software_rendering;
140         GError *gl_error = NULL, *gles_error = NULL;
141 
142         gtk_init (NULL, NULL);
143 
144         /* gnome-session-check-accelerated gets run before X is started in the wayland
145          * case, and it currently requires X. Until we have that working, just always
146          * assume wayland will work.
147          * Also make sure that we don't read cached information about the first GPU
148          * when requesting information about the second.
149          */
150         if (is_discrete_gpu_check () || g_strcmp0 (g_getenv ("XDG_SESSION_TYPE"), "x11") != 0) {
151                 renderer_string = get_gtk_gles_renderer ();
152                 if (renderer_string) {
153                         g_print ("%s", renderer_string);
154                         return 0;
155                 }
156                 return 1;
157         }
158 
159         display = gdk_display_get_default ();
160         /* when running on X11 with a nested wayland GDK will default to wayland
161          * so looking for X11 atoms will not work (and crash).
162          */
163         if (!GDK_IS_X11_DISPLAY (display)) {
164                 g_printerr ("gnome-session-check-accelerated: no X11 display found\n");
165                 return 1;
166         }
167 
168         rootwin = gdk_x11_get_default_root_xwindow ();
169 
170         is_accelerated_atom = gdk_x11_get_xatom_by_name_for_display (display, "_GNOME_SESSION_ACCELERATED");
171         is_software_rendering_atom = gdk_x11_get_xatom_by_name_for_display (display, "_GNOME_IS_SOFTWARE_RENDERING");
172         renderer_atom = gdk_x11_get_xatom_by_name_for_display (display, "_GNOME_SESSION_RENDERER");
173 
174         {
175                 Atom type;
176                 gint format;
177                 gulong nitems;
178                 gulong bytes_after;
179                 guchar *data;
180 
181  read:
182                 gdk_x11_display_error_trap_push (display);
183                 XGetWindowProperty (GDK_DISPLAY_XDISPLAY (display), rootwin,
184                                     is_accelerated_atom,
185                                     0, G_MAXLONG, False, XA_CARDINAL, &type, &format, &nitems,
186                                     &bytes_after, &data);
187                 gdk_x11_display_error_trap_pop_ignored (display);
188 
189                 if (type == XA_CARDINAL) {
190                         glong *is_accelerated_ptr = (glong*) data;
191 
192                         if (*is_accelerated_ptr == ACCEL_CHECK_RUNNING) {
193                                 /* Test in progress, wait */
194                                 if (wait_for_property_notify ())
195                                         goto read;
196                                 /* else fall through and do the check ourselves */
197 
198                         } else {
199                                 gdk_x11_display_error_trap_push (display);
200                                 XGetWindowProperty (GDK_DISPLAY_XDISPLAY (display), rootwin,
201                                                     renderer_atom,
202                                                     0, G_MAXLONG, False, XA_STRING, &type, &format, &nitems,
203                                                     &bytes_after, &data);
204                                 gdk_x11_display_error_trap_pop_ignored (display);
205 
206                                 if (type == XA_STRING) {
207                                         g_print ("%s", data);
208                                 }
209 
210                                 return (*is_accelerated_ptr == 0 ? 1 : 0);
211                         }
212                 }
213         }
214 
215         /* We don't have the property or it's the wrong type.
216          * Try to compute it now.
217          */
218 
219         /* First indicate that a test is in progress */
220         is_accelerated = ACCEL_CHECK_RUNNING;
221         is_software_rendering = FALSE;
222         estatus = 1;
223 
224         XChangeProperty (GDK_DISPLAY_XDISPLAY (display),
225                          rootwin,
226                          is_accelerated_atom,
227                          XA_CARDINAL, 32, PropModeReplace, (guchar *) &is_accelerated, 1);
228 
229         gdk_display_sync (display);
230 
231         /* First, try the GL helper */
232         if (g_spawn_sync (NULL, (char **) gl_helper_argv, NULL, 0,
233                            NULL, NULL, &gl_renderer_string, NULL, &estatus, &gl_error)) {
234                 is_accelerated = (WEXITSTATUS(estatus) == HELPER_ACCEL);
235                 gl_software_rendering = (WEXITSTATUS(estatus) == HELPER_SOFTWARE_RENDERING);
236                 if (is_accelerated) {
237                         renderer_string = gl_renderer_string;
238                         goto finish;
239                 }
240 
241                 g_printerr ("gnome-session-check-accelerated: GL Helper exited with code %d\n", estatus);
242         }
243 
244         /* Then, try the GLES helper */
245         if (g_spawn_sync (NULL, (char **) gles_helper_argv, NULL, 0,
246                            NULL, NULL, &gles_renderer_string, NULL, &estatus, &gles_error)) {
247                 is_accelerated = (WEXITSTATUS(estatus) == HELPER_ACCEL);
248                 gles_software_rendering = (WEXITSTATUS(estatus) == HELPER_SOFTWARE_RENDERING);
249                 if (is_accelerated) {
250                         renderer_string = gles_renderer_string;
251                         goto finish;
252                 }
253 
254                 g_printerr ("gnome-session-check-accelerated: GLES Helper exited with code %d\n", estatus);
255         }
256 
257         /* If we got here, GL software rendering is our best bet */
258         if (gl_software_rendering || gles_software_rendering) {
259                 is_software_rendering = TRUE;
260                 is_accelerated = TRUE;
261 
262                 if (gl_software_rendering)
263                         renderer_string = gl_renderer_string;
264                 else if (gles_software_rendering)
265                         renderer_string = gles_renderer_string;
266 
267                 goto finish;
268         }
269 
270         /* Both helpers failed; print their error messages */
271         if (gl_error != NULL) {
272                 g_printerr ("gnome-session-check-accelerated: Failed to run GL helper: %s\n", gl_error->message);
273                 g_clear_error (&gl_error);
274         }
275 
276         if (gles_error != NULL) {
277                 g_printerr ("gnome-session-check-accelerated: Failed to run GLES helper: %s\n", gles_error->message);
278                 g_clear_error (&gles_error);
279         }
280 
281  finish:
282 	if (is_accelerated) {
283 		XChangeProperty (GDK_DISPLAY_XDISPLAY (display),
284 				rootwin,
285 				is_accelerated_atom,
286 				XA_CARDINAL, 32, PropModeReplace, (guchar *) &is_accelerated, 1);
287 	}
288 
289 	if (is_software_rendering) {
290 		XChangeProperty (GDK_DISPLAY_XDISPLAY (display),
291 				rootwin,
292 				is_software_rendering_atom,
293 				XA_CARDINAL, 32, PropModeReplace, (guchar *) &is_software_rendering, 1);
294 	}
295 
296         if (renderer_string != NULL) {
297                 XChangeProperty (GDK_DISPLAY_XDISPLAY (display),
298 				rootwin,
299 				renderer_atom,
300 				XA_STRING, 8, PropModeReplace, (guchar *) renderer_string, strlen (renderer_string));
301 
302                 /* Print the renderer */
303                 g_print ("%s", renderer_string);
304         }
305 
306         gdk_display_sync (display);
307 
308         g_free (gl_renderer_string);
309         g_free (gles_renderer_string);
310 
311         return is_accelerated ? 0 : 1;
312 }
313