1 /*
2 * Copyright © Microsoft Corporation
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 * and/or sell copies of the Software, and to permit persons to whom the
9 * Software is furnished to do so, subject to the following conditions:
10 *
11 * The above copyright notice and this permission notice (including the next
12 * paragraph) shall be included in all copies or substantial portions of the
13 * Software.
14 *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
18 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
21 * IN THE SOFTWARE.
22 */
23
24 #include "pipe-loader/pipe_loader.h"
25 #include "pipe/p_screen.h"
26
27 #include "util/u_memory.h"
28 #include "vl/vl_winsys.h"
29 #include <X11/Xlib.h>
30 #include <X11/Xutil.h>
31
32 #include "gallium/include/frontend/xlibsw_api.h"
33 #include "gallium/winsys/sw/xlib/xlib_sw_winsys.h"
34 #include "loader.h"
35 #include "target-helpers/sw_helper_public.h"
36 #include "vl/vl_compositor.h"
37
38 struct vl_xlib_screen
39 {
40 struct vl_screen base;
41 struct pipe_context *pipe;
42 Display *display;
43 int screen;
44 struct u_rect dirty_area;
45 XVisualInfo xlib_drawable_visual_info;
46 struct xlib_drawable xlib_drawable_handle;
47 struct pipe_resource *drawable_texture;
48 };
49
50 static void
vl_screen_destroy(struct vl_screen *vscreen)51 vl_screen_destroy(struct vl_screen *vscreen)
52 {
53 if (vscreen == NULL)
54 return;
55
56 if (vscreen->pscreen)
57 vscreen->pscreen->destroy(vscreen->pscreen);
58
59 if (vscreen->dev)
60 pipe_loader_release(&vscreen->dev, 1);
61
62 FREE(vscreen);
63 }
64
65 struct pipe_resource *
66 vl_swrast_texture_from_drawable(struct vl_screen *vscreen, void *drawable);
67
68 struct u_rect *
69 vl_swrast_get_dirty_area(struct vl_screen *vscreen);
70
71 void *
72 vl_swrast_get_private(struct vl_screen *vscreen);
73
74 static void
vl_xlib_screen_destroy(struct vl_screen *vscreen)75 vl_xlib_screen_destroy(struct vl_screen *vscreen)
76 {
77 if (vscreen == NULL)
78 return;
79
80 struct vl_xlib_screen *xlib_screen = (struct vl_xlib_screen *) vscreen;
81
82 if (xlib_screen->drawable_texture)
83 pipe_resource_reference(&xlib_screen->drawable_texture, NULL);
84
85 if (xlib_screen->pipe)
86 xlib_screen->pipe->destroy(xlib_screen->pipe);
87
88 vl_screen_destroy(&xlib_screen->base);
89 }
90
91 struct vl_screen *
vl_xlib_swrast_screen_create(Display *display, int screen)92 vl_xlib_swrast_screen_create(Display *display, int screen)
93 {
94 struct vl_xlib_screen *vscreen = CALLOC_STRUCT(vl_xlib_screen);
95 if (!vscreen)
96 goto handle_err_xlib_swrast_create;
97
98 struct sw_winsys *xlib_winsys = xlib_create_sw_winsys(display);
99 if (xlib_winsys == NULL)
100 goto handle_err_xlib_swrast_create;
101
102 vscreen->base.pscreen = sw_screen_create(xlib_winsys);
103 if (!vscreen->base.pscreen)
104 goto handle_err_xlib_swrast_create;
105
106 vscreen->base.get_private = vl_swrast_get_private;
107 vscreen->base.texture_from_drawable = vl_swrast_texture_from_drawable;
108 vscreen->base.get_dirty_area = vl_swrast_get_dirty_area;
109 vscreen->base.destroy = vl_xlib_screen_destroy;
110 vscreen->pipe = vscreen->base.pscreen->context_create(vscreen->base.pscreen, NULL, 0);
111
112 vl_compositor_reset_dirty_area(&vscreen->dirty_area);
113 vscreen->display = display;
114 vscreen->screen = screen;
115
116 return &vscreen->base;
117
118 handle_err_xlib_swrast_create:
119 if (vscreen)
120 vl_xlib_screen_destroy(&vscreen->base);
121
122 return NULL;
123 }
124
125 void
126 vl_swrast_fill_xlib_drawable_desc(struct vl_screen *vscreen,
127 Window x11_window,
128 struct xlib_drawable *drawable_desc);
129
130 void
vl_swrast_fill_xlib_drawable_desc(struct vl_screen *vscreen, Window x11_window, struct xlib_drawable *drawable_desc)131 vl_swrast_fill_xlib_drawable_desc(struct vl_screen *vscreen,
132 Window x11_window,
133 struct xlib_drawable *drawable_desc)
134 {
135 struct vl_xlib_screen *scrn = (struct vl_xlib_screen *) vscreen;
136 XWindowAttributes x11_window_attrs = {};
137 XGetWindowAttributes(scrn->display, x11_window, &x11_window_attrs);
138 XMatchVisualInfo(scrn->display, scrn->screen, x11_window_attrs.depth, TrueColor, &scrn->xlib_drawable_visual_info);
139 scrn->xlib_drawable_handle.depth = x11_window_attrs.depth;
140 scrn->xlib_drawable_handle.drawable = x11_window;
141 scrn->xlib_drawable_handle.visual = scrn->xlib_drawable_visual_info.visual;
142 }
143
144 struct pipe_resource *
vl_swrast_texture_from_drawable(struct vl_screen *vscreen, void *drawable)145 vl_swrast_texture_from_drawable(struct vl_screen *vscreen, void *drawable)
146 {
147 struct vl_xlib_screen *scrn = (struct vl_xlib_screen *) vscreen;
148 Window x11_window = (Window) drawable;
149 vl_swrast_fill_xlib_drawable_desc(vscreen, x11_window, &scrn->xlib_drawable_handle);
150
151 XWindowAttributes x11_window_attrs = {};
152 XGetWindowAttributes(scrn->display, x11_window, &x11_window_attrs);
153 enum pipe_format x11_window_format = vl_dri2_format_for_depth(&scrn->base, x11_window_attrs.depth);
154
155 bool needs_new_back_buffer_allocation = true;
156 if (scrn->drawable_texture) {
157 needs_new_back_buffer_allocation =
158 (scrn->drawable_texture->width0 != x11_window_attrs.width || scrn->drawable_texture->height0 != x11_window_attrs.height ||
159 scrn->drawable_texture->format != x11_window_format);
160 }
161
162 if (needs_new_back_buffer_allocation) {
163 if (scrn->drawable_texture)
164 pipe_resource_reference(&scrn->drawable_texture, NULL);
165
166 struct pipe_resource templat;
167 memset(&templat, 0, sizeof(templat));
168 templat.target = PIPE_TEXTURE_2D;
169 templat.format = x11_window_format;
170 templat.width0 = x11_window_attrs.width;
171 templat.height0 = x11_window_attrs.height;
172 templat.depth0 = 1;
173 templat.array_size = 1;
174 templat.last_level = 0;
175 templat.bind = (PIPE_BIND_RENDER_TARGET | PIPE_BIND_DISPLAY_TARGET);
176
177 scrn->drawable_texture = vscreen->pscreen->resource_create(vscreen->pscreen, &templat);
178 } else {
179 struct pipe_resource *drawable_texture = NULL;
180 pipe_resource_reference(&drawable_texture, scrn->drawable_texture);
181 }
182
183 return scrn->drawable_texture;
184 }
185
186 void *
vl_swrast_get_private(struct vl_screen *vscreen)187 vl_swrast_get_private(struct vl_screen *vscreen)
188 {
189 struct vl_xlib_screen *scrn = (struct vl_xlib_screen *) vscreen;
190 return &scrn->xlib_drawable_handle;
191 }
192
193 struct u_rect *
vl_swrast_get_dirty_area(struct vl_screen *vscreen)194 vl_swrast_get_dirty_area(struct vl_screen *vscreen)
195 {
196 struct vl_xlib_screen *scrn = (struct vl_xlib_screen *) vscreen;
197 vl_compositor_reset_dirty_area(&scrn->dirty_area);
198 return &scrn->dirty_area;
199 }
200