1 /*
2 * Copyright © 2024 Valve 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 * Authors:
24 * Mike Blumenkrantz <michael.blumenkrantz@gmail.com>
25 */
26
27 #include "pipe-loader/pipe_loader.h"
28 #include "pipe/p_screen.h"
29 #include "util/u_memory.h"
30 #include "vl/vl_winsys.h"
31 #include "loader.h"
32 #include "pipe-loader/pipe_loader.h"
33 #include "vl/vl_compositor.h"
34 #if defined(HAVE_X11_PLATFORM) && defined(HAVE_LIBDRM)
35 #include <X11/Xlib-xcb.h>
36 #include "gallium/drivers/zink/zink_kopper.h"
37 #include <vulkan/vulkan_xcb.h>
38 #include "x11/loader_x11.h"
39 #endif
40 #include "zink_public.h"
41
42 struct vl_kopper_screen
43 {
44 struct vl_screen base;
45 struct pipe_context *pipe;
46 #if defined(HAVE_X11_PLATFORM) && defined(HAVE_LIBDRM)
47 xcb_connection_t *conn;
48 bool is_different_gpu;
49 int fd;
50 struct u_rect dirty_area;
51 struct pipe_resource *drawable_texture;
52 #endif
53 int screen;
54 };
55
56 static void
vl_screen_destroy(struct vl_screen * vscreen)57 vl_screen_destroy(struct vl_screen *vscreen)
58 {
59 if (vscreen == NULL)
60 return;
61
62 if (vscreen->pscreen)
63 vscreen->pscreen->destroy(vscreen->pscreen);
64
65 if (vscreen->dev)
66 pipe_loader_release(&vscreen->dev, 1);
67
68 FREE(vscreen);
69 }
70
71 static void
vl_kopper_screen_destroy(struct vl_screen * vscreen)72 vl_kopper_screen_destroy(struct vl_screen *vscreen)
73 {
74 if (vscreen == NULL)
75 return;
76
77 struct vl_kopper_screen *scrn = (struct vl_kopper_screen *) vscreen;
78
79 #if defined(HAVE_X11_PLATFORM) && defined(HAVE_LIBDRM)
80 if (scrn->fd != -1)
81 close(scrn->fd);
82 if (scrn->drawable_texture)
83 pipe_resource_reference(&scrn->drawable_texture, NULL);
84 #endif
85
86 if (scrn->pipe)
87 scrn->pipe->destroy(scrn->pipe);
88
89 vl_screen_destroy(&scrn->base);
90 }
91
92 #if defined(HAVE_X11_PLATFORM) && defined(HAVE_LIBDRM)
93 static void *
vl_kopper_get_private(struct vl_screen * vscreen)94 vl_kopper_get_private(struct vl_screen *vscreen)
95 {
96 return NULL;
97 }
98
99 static struct u_rect *
vl_kopper_get_dirty_area(struct vl_screen * vscreen)100 vl_kopper_get_dirty_area(struct vl_screen *vscreen)
101 {
102 struct vl_kopper_screen *scrn = (struct vl_kopper_screen *) vscreen;
103 return &scrn->dirty_area;
104 }
105
106 static struct pipe_resource *
vl_kopper_texture_from_drawable(struct vl_screen * vscreen,void * d)107 vl_kopper_texture_from_drawable(struct vl_screen *vscreen, void *d)
108 {
109 struct vl_kopper_screen *scrn = (struct vl_kopper_screen *) vscreen;
110 Drawable drawable = (Drawable)d;
111 xcb_get_geometry_cookie_t cookie;
112 xcb_get_geometry_reply_t *reply;
113 xcb_generic_error_t *error;
114 int w, h;
115
116 if (scrn->fd == -1 && scrn->drawable_texture) {
117 zink_kopper_update(vscreen->pscreen, scrn->drawable_texture, &w, &h);
118 } else {
119 cookie = xcb_get_geometry(scrn->conn, drawable);
120 reply = xcb_get_geometry_reply(scrn->conn, cookie, &error);
121 w = reply->width, h = reply->height;
122 free(reply);
123 }
124
125 bool needs_new_back_buffer_allocation = true;
126 if (scrn->drawable_texture) {
127 needs_new_back_buffer_allocation =
128 (scrn->drawable_texture->width0 != w || scrn->drawable_texture->height0 != h);
129 }
130
131 if (needs_new_back_buffer_allocation) {
132 struct kopper_loader_info info;
133 VkXcbSurfaceCreateInfoKHR *xcb = (VkXcbSurfaceCreateInfoKHR *)&info.bos;
134 xcb->sType = VK_STRUCTURE_TYPE_XCB_SURFACE_CREATE_INFO_KHR;
135 xcb->pNext = NULL;
136 xcb->flags = 0;
137 xcb->connection = scrn->conn;
138 xcb->window = drawable;
139 info.has_alpha = scrn->base.color_depth == 32;
140
141 if (scrn->drawable_texture)
142 pipe_resource_reference(&scrn->drawable_texture, NULL);
143
144 struct pipe_resource templat;
145 memset(&templat, 0, sizeof(templat));
146 templat.target = PIPE_TEXTURE_2D;
147 templat.format = vl_dri2_format_for_depth(vscreen, scrn->base.color_depth);
148 templat.width0 = w;
149 templat.height0 = h;
150 templat.depth0 = 1;
151 templat.array_size = 1;
152 templat.last_level = 0;
153 templat.bind = (PIPE_BIND_RENDER_TARGET | PIPE_BIND_DISPLAY_TARGET | PIPE_BIND_SAMPLER_VIEW);
154
155 scrn->drawable_texture = vscreen->pscreen->resource_create_drawable(vscreen->pscreen, &templat, &info);
156 vl_compositor_reset_dirty_area(&scrn->dirty_area);
157 } else {
158 struct pipe_resource *drawable_texture = NULL;
159 pipe_resource_reference(&drawable_texture, scrn->drawable_texture);
160 }
161
162 return scrn->drawable_texture;
163 }
164
165 struct vl_screen *
vl_kopper_screen_create_x11(Display * display,int screen)166 vl_kopper_screen_create_x11(Display *display, int screen)
167 {
168 xcb_get_geometry_cookie_t geom_cookie;
169 xcb_get_geometry_reply_t *geom_reply;
170 struct vl_kopper_screen *scrn = CALLOC_STRUCT(vl_kopper_screen);
171 bool err = false;
172 if (!scrn)
173 goto error;
174
175 scrn->conn = XGetXCBConnection(display);
176 if (!scrn->conn)
177 goto error;
178
179 int fd = x11_dri3_open(scrn->conn, RootWindow(display, screen), 0);
180 bool explicit_modifiers = false;
181 x11_dri3_check_multibuffer(scrn->conn, &err, &explicit_modifiers);
182 if (fd < 0 || !explicit_modifiers) {
183 goto error;
184 }
185
186 scrn->is_different_gpu = loader_get_user_preferred_fd(&fd, NULL);
187
188 geom_cookie = xcb_get_geometry(scrn->conn, RootWindow(display, screen));
189 geom_reply = xcb_get_geometry_reply(scrn->conn, geom_cookie, NULL);
190 if (!geom_reply)
191 goto error;
192
193 scrn->base.xcb_screen = vl_dri_get_screen_for_root(scrn->conn, geom_reply->root);
194 if (!scrn->base.xcb_screen) {
195 free(geom_reply);
196 goto error;
197 }
198
199 /* TODO support depth other than 24 or 30 */
200 if (geom_reply->depth != 24 && geom_reply->depth != 30) {
201 free(geom_reply);
202 goto error;
203 }
204 scrn->base.color_depth = geom_reply->depth;
205 free(geom_reply);
206
207 scrn->fd = fd;
208 bool success;
209 if (fd != -1)
210 success = pipe_loader_drm_probe_fd(&scrn->base.dev, fd, true);
211 else
212 success = pipe_loader_vk_probe_dri(&scrn->base.dev);
213
214 if (success)
215 pipe_loader_create_screen_vk(scrn->base.dev, false, false);
216 if (!scrn->base.pscreen)
217 goto error;
218
219 scrn->base.get_private = vl_kopper_get_private;
220 scrn->base.texture_from_drawable = vl_kopper_texture_from_drawable;
221 scrn->base.get_dirty_area = vl_kopper_get_dirty_area;
222 scrn->base.destroy = vl_kopper_screen_destroy;
223 scrn->pipe = scrn->base.pscreen->context_create(scrn->base.pscreen, NULL, 0);
224
225 vl_compositor_reset_dirty_area(&scrn->dirty_area);
226
227 return &scrn->base;
228
229 error:
230 vl_kopper_screen_destroy(&scrn->base);
231
232 return NULL;
233 }
234 #endif
235
236 #ifdef _WIN32
237 struct vl_screen *
vl_kopper_screen_create_win32(LUID * luid)238 vl_kopper_screen_create_win32(LUID *luid)
239 {
240 struct vl_kopper_screen *scrn = CALLOC_STRUCT(vl_kopper_screen);
241 uint64_t adapter_luid = 0;
242
243 if (luid)
244 memcpy(&adapter_luid, luid, sizeof(adapter_luid));
245 scrn->base.pscreen = zink_win32_create_screen(adapter_luid);
246 if (!scrn->base.pscreen)
247 goto error;
248
249 scrn->base.destroy = vl_kopper_screen_destroy;
250
251 scrn->pipe = scrn->base.pscreen->context_create(scrn->base.pscreen, NULL, 0);
252
253 return &scrn->base;
254
255 error:
256 vl_kopper_screen_destroy(&scrn->base);
257
258 return NULL;
259 }
260 #endif
261