xref: /aosp_15_r20/external/wayland/egl/wayland-egl.c (revision 84e872a0dc482bffdb63672969dd03a827d67c73)
1 /*
2  * Copyright © 2011 Kristian Høgsberg
3  * Copyright © 2011 Benjamin Franzke
4  *
5  * Permission is hereby granted, free of charge, to any person obtaining a
6  * copy of this software and associated documentation files (the "Software"),
7  * to deal in the Software without restriction, including without limitation
8  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
9  * and/or sell copies of the Software, and to permit persons to whom the
10  * Software is furnished to do so, subject to the following conditions:
11  *
12  * The above copyright notice and this permission notice (including the next
13  * paragraph) shall be included in all copies or substantial portions of the
14  * Software.
15  *
16  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19  * NONINFRINGEMENT.  IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
20  * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
21  * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
23  * DEALINGS IN THE SOFTWARE.
24  *
25  * Authors:
26  *    Kristian Høgsberg <[email protected]>
27  *    Benjamin Franzke <[email protected]>
28  */
29 
30 #include <stdlib.h>
31 #include <string.h>
32 
33 #include "wayland-egl.h"
34 #include "wayland-egl-backend.h"
35 #include "wayland-util.h"
36 
37 
38 /** Resize the EGL window
39  *
40  * \param egl_window A pointer to a struct wl_egl_window
41  * \param width The new width
42  * \param height The new height
43  * \param dx Offset on the X axis
44  * \param dy Offset on the Y axis
45  *
46  * Note that applications should prefer using the wl_surface.offset request if
47  * the associated wl_surface has the interface version 5 or higher.
48  *
49  * If the wl_surface.offset request is used, applications MUST pass 0 to both
50  * dx and dy.
51  */
52 WL_EXPORT void
wl_egl_window_resize(struct wl_egl_window * egl_window,int width,int height,int dx,int dy)53 wl_egl_window_resize(struct wl_egl_window *egl_window,
54 		     int width, int height,
55 		     int dx, int dy)
56 {
57 	if (width <= 0 || height <= 0)
58 		return;
59 
60 	egl_window->width  = width;
61 	egl_window->height = height;
62 	egl_window->dx     = dx;
63 	egl_window->dy     = dy;
64 
65 	if (egl_window->resize_callback)
66 		egl_window->resize_callback(egl_window, egl_window->driver_private);
67 }
68 
69 WL_EXPORT struct wl_egl_window *
wl_egl_window_create(struct wl_surface * surface,int width,int height)70 wl_egl_window_create(struct wl_surface *surface,
71 		     int width, int height)
72 {
73 	struct wl_egl_window *egl_window;
74 
75 	if (width <= 0 || height <= 0)
76 		return NULL;
77 
78 	egl_window = calloc(1, sizeof *egl_window);
79 	if (!egl_window)
80 		return NULL;
81 
82 	/* Cast away the constness to set the version number.
83 	 *
84 	 * We want the const notation since it gives an explicit
85 	 * feedback to the backend implementation, should it try to
86 	 * change it.
87 	 *
88 	 * The latter in itself is not too surprising as these days APIs
89 	 * tend to provide bidirectional version field.
90 	 */
91 	intptr_t *version = (intptr_t *)&egl_window->version;
92 	*version = WL_EGL_WINDOW_VERSION;
93 
94 	egl_window->surface = surface;
95 
96 	egl_window->width  = width;
97 	egl_window->height = height;
98 
99 	return egl_window;
100 }
101 
102 WL_EXPORT void
wl_egl_window_destroy(struct wl_egl_window * egl_window)103 wl_egl_window_destroy(struct wl_egl_window *egl_window)
104 {
105 	if (egl_window->destroy_window_callback)
106 		egl_window->destroy_window_callback(egl_window->driver_private);
107 	free(egl_window);
108 }
109 
110 WL_EXPORT void
wl_egl_window_get_attached_size(struct wl_egl_window * egl_window,int * width,int * height)111 wl_egl_window_get_attached_size(struct wl_egl_window *egl_window,
112 				int *width, int *height)
113 {
114 	if (width)
115 		*width = egl_window->attached_width;
116 	if (height)
117 		*height = egl_window->attached_height;
118 }
119