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