xref: /aosp_15_r20/external/igt-gpu-tools/overlay/x11/x11-window.c (revision d83cc019efdc2edc6c4b16e9034a3ceb8d35d77c)
1*d83cc019SAndroid Build Coastguard Worker /*
2*d83cc019SAndroid Build Coastguard Worker  * Copyright © 2013 Intel Corporation
3*d83cc019SAndroid Build Coastguard Worker  *
4*d83cc019SAndroid Build Coastguard Worker  * Permission is hereby granted, free of charge, to any person obtaining a
5*d83cc019SAndroid Build Coastguard Worker  * copy of this software and associated documentation files (the "Software"),
6*d83cc019SAndroid Build Coastguard Worker  * to deal in the Software without restriction, including without limitation
7*d83cc019SAndroid Build Coastguard Worker  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8*d83cc019SAndroid Build Coastguard Worker  * and/or sell copies of the Software, and to permit persons to whom the
9*d83cc019SAndroid Build Coastguard Worker  * Software is furnished to do so, subject to the following conditions:
10*d83cc019SAndroid Build Coastguard Worker  *
11*d83cc019SAndroid Build Coastguard Worker  * The above copyright notice and this permission notice (including the next
12*d83cc019SAndroid Build Coastguard Worker  * paragraph) shall be included in all copies or substantial portions of the
13*d83cc019SAndroid Build Coastguard Worker  * Software.
14*d83cc019SAndroid Build Coastguard Worker  *
15*d83cc019SAndroid Build Coastguard Worker  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16*d83cc019SAndroid Build Coastguard Worker  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17*d83cc019SAndroid Build Coastguard Worker  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
18*d83cc019SAndroid Build Coastguard Worker  * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19*d83cc019SAndroid Build Coastguard Worker  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20*d83cc019SAndroid Build Coastguard Worker  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
21*d83cc019SAndroid Build Coastguard Worker  * IN THE SOFTWARE.
22*d83cc019SAndroid Build Coastguard Worker  *
23*d83cc019SAndroid Build Coastguard Worker  */
24*d83cc019SAndroid Build Coastguard Worker 
25*d83cc019SAndroid Build Coastguard Worker #include <X11/Xlib.h>
26*d83cc019SAndroid Build Coastguard Worker #include <cairo.h>
27*d83cc019SAndroid Build Coastguard Worker #include <cairo-xlib.h>
28*d83cc019SAndroid Build Coastguard Worker #include <stdio.h>
29*d83cc019SAndroid Build Coastguard Worker #include <stdlib.h>
30*d83cc019SAndroid Build Coastguard Worker #include <stdbool.h>
31*d83cc019SAndroid Build Coastguard Worker #include <string.h>
32*d83cc019SAndroid Build Coastguard Worker #include <unistd.h>
33*d83cc019SAndroid Build Coastguard Worker 
34*d83cc019SAndroid Build Coastguard Worker #include "../overlay.h"
35*d83cc019SAndroid Build Coastguard Worker #include "position.h"
36*d83cc019SAndroid Build Coastguard Worker 
37*d83cc019SAndroid Build Coastguard Worker struct x11_window {
38*d83cc019SAndroid Build Coastguard Worker 	struct overlay base;
39*d83cc019SAndroid Build Coastguard Worker 	cairo_surface_t *front;
40*d83cc019SAndroid Build Coastguard Worker 	Display *dpy;
41*d83cc019SAndroid Build Coastguard Worker 	Window win;
42*d83cc019SAndroid Build Coastguard Worker 	int width, height;
43*d83cc019SAndroid Build Coastguard Worker 	int visible;
44*d83cc019SAndroid Build Coastguard Worker };
45*d83cc019SAndroid Build Coastguard Worker 
to_x11_window(struct overlay * o)46*d83cc019SAndroid Build Coastguard Worker static inline struct x11_window *to_x11_window(struct overlay *o)
47*d83cc019SAndroid Build Coastguard Worker {
48*d83cc019SAndroid Build Coastguard Worker 	return (struct x11_window *)o;
49*d83cc019SAndroid Build Coastguard Worker }
50*d83cc019SAndroid Build Coastguard Worker 
noop(Display * dpy,XErrorEvent * event)51*d83cc019SAndroid Build Coastguard Worker static int noop(Display *dpy, XErrorEvent *event)
52*d83cc019SAndroid Build Coastguard Worker {
53*d83cc019SAndroid Build Coastguard Worker 	return 0;
54*d83cc019SAndroid Build Coastguard Worker }
55*d83cc019SAndroid Build Coastguard Worker 
x11_window_show(struct overlay * overlay)56*d83cc019SAndroid Build Coastguard Worker static void x11_window_show(struct overlay *overlay)
57*d83cc019SAndroid Build Coastguard Worker {
58*d83cc019SAndroid Build Coastguard Worker 	struct x11_window *priv = to_x11_window(overlay);
59*d83cc019SAndroid Build Coastguard Worker 	cairo_t *cr;
60*d83cc019SAndroid Build Coastguard Worker 
61*d83cc019SAndroid Build Coastguard Worker 	cr = cairo_create(priv->front);
62*d83cc019SAndroid Build Coastguard Worker 	cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
63*d83cc019SAndroid Build Coastguard Worker 	cairo_set_source_surface(cr, priv->base.surface, 0, 0);
64*d83cc019SAndroid Build Coastguard Worker 	cairo_paint(cr);
65*d83cc019SAndroid Build Coastguard Worker 	cairo_destroy(cr);
66*d83cc019SAndroid Build Coastguard Worker 
67*d83cc019SAndroid Build Coastguard Worker 	cairo_surface_flush(priv->front);
68*d83cc019SAndroid Build Coastguard Worker 
69*d83cc019SAndroid Build Coastguard Worker 	if (!priv->visible) {
70*d83cc019SAndroid Build Coastguard Worker 		XMapWindow(priv->dpy, priv->win);
71*d83cc019SAndroid Build Coastguard Worker 		priv->visible = true;
72*d83cc019SAndroid Build Coastguard Worker 	}
73*d83cc019SAndroid Build Coastguard Worker 
74*d83cc019SAndroid Build Coastguard Worker 	XFlush(priv->dpy);
75*d83cc019SAndroid Build Coastguard Worker }
76*d83cc019SAndroid Build Coastguard Worker 
x11_window_hide(struct overlay * overlay)77*d83cc019SAndroid Build Coastguard Worker static void x11_window_hide(struct overlay *overlay)
78*d83cc019SAndroid Build Coastguard Worker {
79*d83cc019SAndroid Build Coastguard Worker 	struct x11_window *priv = to_x11_window(overlay);
80*d83cc019SAndroid Build Coastguard Worker 	if (priv->visible) {
81*d83cc019SAndroid Build Coastguard Worker 		XUnmapWindow(priv->dpy, priv->win);
82*d83cc019SAndroid Build Coastguard Worker 		XFlush(priv->dpy);
83*d83cc019SAndroid Build Coastguard Worker 		priv->visible = false;
84*d83cc019SAndroid Build Coastguard Worker 	}
85*d83cc019SAndroid Build Coastguard Worker }
86*d83cc019SAndroid Build Coastguard Worker 
x11_window_destroy(void * data)87*d83cc019SAndroid Build Coastguard Worker static void x11_window_destroy(void *data)
88*d83cc019SAndroid Build Coastguard Worker {
89*d83cc019SAndroid Build Coastguard Worker 	struct x11_window *priv = data;
90*d83cc019SAndroid Build Coastguard Worker 	cairo_surface_destroy(priv->front);
91*d83cc019SAndroid Build Coastguard Worker 	XDestroyWindow(priv->dpy, priv->win);
92*d83cc019SAndroid Build Coastguard Worker 	XCloseDisplay(priv->dpy);
93*d83cc019SAndroid Build Coastguard Worker 	free(priv);
94*d83cc019SAndroid Build Coastguard Worker }
95*d83cc019SAndroid Build Coastguard Worker 
prefer_image(struct config * config)96*d83cc019SAndroid Build Coastguard Worker static int prefer_image(struct config *config)
97*d83cc019SAndroid Build Coastguard Worker {
98*d83cc019SAndroid Build Coastguard Worker 	const char *v = config_get_value(config, "x11", "prefer-image");
99*d83cc019SAndroid Build Coastguard Worker 
100*d83cc019SAndroid Build Coastguard Worker 	if (v == NULL)
101*d83cc019SAndroid Build Coastguard Worker 		return 0;
102*d83cc019SAndroid Build Coastguard Worker 	if (*v == '\0')
103*d83cc019SAndroid Build Coastguard Worker 		return 1;
104*d83cc019SAndroid Build Coastguard Worker 
105*d83cc019SAndroid Build Coastguard Worker 	return atoi(v);
106*d83cc019SAndroid Build Coastguard Worker }
107*d83cc019SAndroid Build Coastguard Worker 
108*d83cc019SAndroid Build Coastguard Worker cairo_surface_t *
x11_window_create(struct config * config,int * width,int * height)109*d83cc019SAndroid Build Coastguard Worker x11_window_create(struct config *config, int *width, int *height)
110*d83cc019SAndroid Build Coastguard Worker {
111*d83cc019SAndroid Build Coastguard Worker 	Display *dpy;
112*d83cc019SAndroid Build Coastguard Worker 	Window win;
113*d83cc019SAndroid Build Coastguard Worker 	int screen;
114*d83cc019SAndroid Build Coastguard Worker 	cairo_surface_t *surface;
115*d83cc019SAndroid Build Coastguard Worker 	XSetWindowAttributes attr;
116*d83cc019SAndroid Build Coastguard Worker 	struct x11_window *priv;
117*d83cc019SAndroid Build Coastguard Worker 	int x, y, w, h;
118*d83cc019SAndroid Build Coastguard Worker 
119*d83cc019SAndroid Build Coastguard Worker 	dpy = XOpenDisplay(NULL);
120*d83cc019SAndroid Build Coastguard Worker 	if (dpy == NULL)
121*d83cc019SAndroid Build Coastguard Worker 		return NULL;
122*d83cc019SAndroid Build Coastguard Worker 
123*d83cc019SAndroid Build Coastguard Worker 	screen = DefaultScreen(dpy);
124*d83cc019SAndroid Build Coastguard Worker 
125*d83cc019SAndroid Build Coastguard Worker 	XSetErrorHandler(noop);
126*d83cc019SAndroid Build Coastguard Worker 
127*d83cc019SAndroid Build Coastguard Worker 	x11_position(dpy, *width, *height, config, &x, &y, &w, &h);
128*d83cc019SAndroid Build Coastguard Worker 
129*d83cc019SAndroid Build Coastguard Worker 	attr.override_redirect = True;
130*d83cc019SAndroid Build Coastguard Worker 	win = XCreateWindow(dpy, DefaultRootWindow(dpy),
131*d83cc019SAndroid Build Coastguard Worker 			   x, y, w, h, 0,
132*d83cc019SAndroid Build Coastguard Worker 			   DefaultDepth(dpy, screen),
133*d83cc019SAndroid Build Coastguard Worker 			   InputOutput,
134*d83cc019SAndroid Build Coastguard Worker 			   DefaultVisual(dpy, screen),
135*d83cc019SAndroid Build Coastguard Worker 			   CWOverrideRedirect, &attr);
136*d83cc019SAndroid Build Coastguard Worker 
137*d83cc019SAndroid Build Coastguard Worker 	surface = cairo_xlib_surface_create(dpy, win, DefaultVisual (dpy, screen), w, h);
138*d83cc019SAndroid Build Coastguard Worker 	if (cairo_surface_status(surface))
139*d83cc019SAndroid Build Coastguard Worker 		goto err_win;
140*d83cc019SAndroid Build Coastguard Worker 
141*d83cc019SAndroid Build Coastguard Worker 	priv = malloc(sizeof(*priv));
142*d83cc019SAndroid Build Coastguard Worker 	if (priv == NULL)
143*d83cc019SAndroid Build Coastguard Worker 		goto err_surface;
144*d83cc019SAndroid Build Coastguard Worker 
145*d83cc019SAndroid Build Coastguard Worker 	if (prefer_image(config))
146*d83cc019SAndroid Build Coastguard Worker 		priv->base.surface = cairo_image_surface_create(CAIRO_FORMAT_RGB24, w, h);
147*d83cc019SAndroid Build Coastguard Worker 	else
148*d83cc019SAndroid Build Coastguard Worker 		priv->base.surface = cairo_surface_create_similar(surface, CAIRO_CONTENT_COLOR, w, h);
149*d83cc019SAndroid Build Coastguard Worker 	if (cairo_surface_status(priv->base.surface))
150*d83cc019SAndroid Build Coastguard Worker 		goto err_priv;
151*d83cc019SAndroid Build Coastguard Worker 
152*d83cc019SAndroid Build Coastguard Worker 	priv->base.show = x11_window_show;
153*d83cc019SAndroid Build Coastguard Worker 	priv->base.hide = x11_window_hide;
154*d83cc019SAndroid Build Coastguard Worker 
155*d83cc019SAndroid Build Coastguard Worker 	priv->dpy = dpy;
156*d83cc019SAndroid Build Coastguard Worker 	priv->win = win;
157*d83cc019SAndroid Build Coastguard Worker 	priv->front = surface;
158*d83cc019SAndroid Build Coastguard Worker 	priv->visible = false;
159*d83cc019SAndroid Build Coastguard Worker 
160*d83cc019SAndroid Build Coastguard Worker 	priv->width = w;
161*d83cc019SAndroid Build Coastguard Worker 	priv->height = h;
162*d83cc019SAndroid Build Coastguard Worker 
163*d83cc019SAndroid Build Coastguard Worker 	cairo_surface_set_user_data(priv->base.surface, &overlay_key, priv, x11_window_destroy);
164*d83cc019SAndroid Build Coastguard Worker 
165*d83cc019SAndroid Build Coastguard Worker 	*width = w;
166*d83cc019SAndroid Build Coastguard Worker 	*height = h;
167*d83cc019SAndroid Build Coastguard Worker 	return priv->base.surface;
168*d83cc019SAndroid Build Coastguard Worker 
169*d83cc019SAndroid Build Coastguard Worker err_priv:
170*d83cc019SAndroid Build Coastguard Worker 	free(priv);
171*d83cc019SAndroid Build Coastguard Worker err_surface:
172*d83cc019SAndroid Build Coastguard Worker 	cairo_surface_destroy(surface);
173*d83cc019SAndroid Build Coastguard Worker err_win:
174*d83cc019SAndroid Build Coastguard Worker 	XDestroyWindow(dpy, win);
175*d83cc019SAndroid Build Coastguard Worker 	XCloseDisplay(dpy);
176*d83cc019SAndroid Build Coastguard Worker 	return NULL;
177*d83cc019SAndroid Build Coastguard Worker }
178