xref: /aosp_15_r20/external/mesa3d/src/egl/drivers/dri2/platform_surfaceless.c (revision 6104692788411f58d303aa86923a9ff6ecaded22)
1 /*
2  * Mesa 3-D graphics library
3  *
4  * Copyright (c) 2014 The Chromium OS Authors.
5  * Copyright © 2011 Intel Corporation
6  *
7  * Permission is hereby granted, free of charge, to any person obtaining a
8  * copy of this software and associated documentation files (the "Software"),
9  * to deal in the Software without restriction, including without limitation
10  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
11  * and/or sell copies of the Software, and to permit persons to whom the
12  * Software is furnished to do so, subject to the following conditions:
13  *
14  * The above copyright notice and this permission notice shall be included
15  * in all copies or substantial portions of the Software.
16  *
17  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
20  * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
22  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
23  * DEALINGS IN THE SOFTWARE.
24  */
25 
26 #include <dlfcn.h>
27 #include <fcntl.h>
28 #include <stdio.h>
29 #include <stdlib.h>
30 #include <string.h>
31 #include <unistd.h>
32 #include "util/libdrm.h"
33 #include <sys/stat.h>
34 #include <sys/types.h>
35 
36 #include "egl_dri2.h"
37 #include "eglglobals.h"
38 #include "kopper_interface.h"
39 #include "loader.h"
40 #include "loader_dri_helper.h"
41 #include "dri_util.h"
42 
43 static __DRIimage *
surfaceless_alloc_image(struct dri2_egl_display * dri2_dpy,struct dri2_egl_surface * dri2_surf)44 surfaceless_alloc_image(struct dri2_egl_display *dri2_dpy,
45                         struct dri2_egl_surface *dri2_surf)
46 {
47    return dri_create_image(
48       dri2_dpy->dri_screen_render_gpu, dri2_surf->base.Width,
49       dri2_surf->base.Height, dri2_surf->visual, NULL, 0, 0, NULL);
50 }
51 
52 static void
surfaceless_free_images(struct dri2_egl_surface * dri2_surf)53 surfaceless_free_images(struct dri2_egl_surface *dri2_surf)
54 {
55    if (dri2_surf->front) {
56       dri2_destroy_image(dri2_surf->front);
57       dri2_surf->front = NULL;
58    }
59 
60    free(dri2_surf->swrast_device_buffer);
61    dri2_surf->swrast_device_buffer = NULL;
62 }
63 
64 static int
surfaceless_image_get_buffers(__DRIdrawable * driDrawable,unsigned int format,uint32_t * stamp,void * loaderPrivate,uint32_t buffer_mask,struct __DRIimageList * buffers)65 surfaceless_image_get_buffers(__DRIdrawable *driDrawable, unsigned int format,
66                               uint32_t *stamp, void *loaderPrivate,
67                               uint32_t buffer_mask,
68                               struct __DRIimageList *buffers)
69 {
70    struct dri2_egl_surface *dri2_surf = loaderPrivate;
71    struct dri2_egl_display *dri2_dpy =
72       dri2_egl_display(dri2_surf->base.Resource.Display);
73 
74    buffers->image_mask = 0;
75    buffers->front = NULL;
76    buffers->back = NULL;
77 
78    /* The EGL 1.5 spec states that pbuffers are single-buffered. Specifically,
79     * the spec states that they have a back buffer but no front buffer, in
80     * contrast to pixmaps, which have a front buffer but no back buffer.
81     *
82     * Single-buffered surfaces with no front buffer confuse Mesa; so we deviate
83     * from the spec, following the precedent of Mesa's EGL X11 platform. The
84     * X11 platform correctly assigns pbuffers to single-buffered configs, but
85     * assigns the pbuffer a front buffer instead of a back buffer.
86     *
87     * Pbuffers in the X11 platform mostly work today, so let's just copy its
88     * behavior instead of trying to fix (and hence potentially breaking) the
89     * world.
90     */
91 
92    if (buffer_mask & __DRI_IMAGE_BUFFER_FRONT) {
93 
94       if (!dri2_surf->front) {
95          dri2_surf->front = surfaceless_alloc_image(dri2_dpy, dri2_surf);
96          if (!dri2_surf->front)
97             return 0;
98       }
99 
100       buffers->image_mask |= __DRI_IMAGE_BUFFER_FRONT;
101       buffers->front = dri2_surf->front;
102    }
103 
104    return 1;
105 }
106 
107 static _EGLSurface *
dri2_surfaceless_create_surface(_EGLDisplay * disp,EGLint type,_EGLConfig * conf,const EGLint * attrib_list)108 dri2_surfaceless_create_surface(_EGLDisplay *disp, EGLint type,
109                                 _EGLConfig *conf, const EGLint *attrib_list)
110 {
111    struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);
112    struct dri2_egl_config *dri2_conf = dri2_egl_config(conf);
113    struct dri2_egl_surface *dri2_surf;
114    const __DRIconfig *config;
115 
116    /* Make sure to calloc so all pointers
117     * are originally NULL.
118     */
119    dri2_surf = calloc(1, sizeof *dri2_surf);
120 
121    if (!dri2_surf) {
122       _eglError(EGL_BAD_ALLOC, "eglCreatePbufferSurface");
123       return NULL;
124    }
125 
126    if (!dri2_init_surface(&dri2_surf->base, disp, type, conf, attrib_list,
127                           false, NULL))
128       goto cleanup_surface;
129 
130    config = dri2_get_dri_config(dri2_conf, type, dri2_surf->base.GLColorspace);
131 
132    if (!config) {
133       _eglError(EGL_BAD_MATCH,
134                 "Unsupported surfacetype/colorspace configuration");
135       goto cleanup_surface;
136    }
137 
138    dri2_surf->visual = dri2_image_format_for_pbuffer_config(dri2_dpy, config);
139    if (dri2_surf->visual == PIPE_FORMAT_NONE)
140       goto cleanup_surface;
141 
142    if (!dri2_create_drawable(dri2_dpy, config, dri2_surf, dri2_surf))
143       goto cleanup_surface;
144 
145    return &dri2_surf->base;
146 
147 cleanup_surface:
148    free(dri2_surf);
149    return NULL;
150 }
151 
152 static EGLBoolean
surfaceless_destroy_surface(_EGLDisplay * disp,_EGLSurface * surf)153 surfaceless_destroy_surface(_EGLDisplay *disp, _EGLSurface *surf)
154 {
155    struct dri2_egl_surface *dri2_surf = dri2_egl_surface(surf);
156 
157    surfaceless_free_images(dri2_surf);
158 
159    driDestroyDrawable(dri2_surf->dri_drawable);
160 
161    dri2_fini_surface(surf);
162    free(dri2_surf);
163    return EGL_TRUE;
164 }
165 
166 static _EGLSurface *
dri2_surfaceless_create_pbuffer_surface(_EGLDisplay * disp,_EGLConfig * conf,const EGLint * attrib_list)167 dri2_surfaceless_create_pbuffer_surface(_EGLDisplay *disp, _EGLConfig *conf,
168                                         const EGLint *attrib_list)
169 {
170    return dri2_surfaceless_create_surface(disp, EGL_PBUFFER_BIT, conf,
171                                           attrib_list);
172 }
173 
174 static const struct dri2_egl_display_vtbl dri2_surfaceless_display_vtbl = {
175    .create_pbuffer_surface = dri2_surfaceless_create_pbuffer_surface,
176    .destroy_surface = surfaceless_destroy_surface,
177    .create_image = dri2_create_image_khr,
178    .get_dri_drawable = dri2_surface_get_dri_drawable,
179 };
180 
181 static void
surfaceless_flush_front_buffer(__DRIdrawable * driDrawable,void * loaderPrivate)182 surfaceless_flush_front_buffer(__DRIdrawable *driDrawable, void *loaderPrivate)
183 {
184 }
185 
186 static unsigned
surfaceless_get_capability(void * loaderPrivate,enum dri_loader_cap cap)187 surfaceless_get_capability(void *loaderPrivate, enum dri_loader_cap cap)
188 {
189    /* Note: loaderPrivate is _EGLDisplay* */
190    switch (cap) {
191    case DRI_LOADER_CAP_FP16:
192       return 1;
193    case DRI_LOADER_CAP_RGBA_ORDERING:
194       return 1;
195    default:
196       return 0;
197    }
198 }
199 
200 static const __DRIkopperLoaderExtension kopper_loader_extension = {
201    .base = {__DRI_KOPPER_LOADER, 1},
202 
203    .SetSurfaceCreateInfo = NULL,
204 };
205 
206 static const __DRIimageLoaderExtension image_loader_extension = {
207    .base = {__DRI_IMAGE_LOADER, 2},
208    .getBuffers = surfaceless_image_get_buffers,
209    .flushFrontBuffer = surfaceless_flush_front_buffer,
210    .getCapability = surfaceless_get_capability,
211 };
212 
213 static const __DRIextension *image_loader_extensions[] = {
214    &image_loader_extension.base,  &image_lookup_extension.base,
215    &use_invalidate.base,          &background_callable_extension.base,
216    &kopper_loader_extension.base, NULL,
217 };
218 
219 static const __DRIextension *swrast_loader_extensions[] = {
220    &swrast_pbuffer_loader_extension.base, &image_loader_extension.base,
221    &image_lookup_extension.base,          &use_invalidate.base,
222    &kopper_loader_extension.base,         NULL,
223 };
224 
225 static bool
surfaceless_probe_device(_EGLDisplay * disp,bool swrast,bool zink)226 surfaceless_probe_device(_EGLDisplay *disp, bool swrast, bool zink)
227 {
228    const unsigned node_type = swrast ? DRM_NODE_PRIMARY : DRM_NODE_RENDER;
229    struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);
230    _EGLDevice *dev_list = _eglGlobal.DeviceList;
231    drmDevicePtr device;
232 
233    while (dev_list) {
234       if (!_eglDeviceSupports(dev_list, _EGL_DEVICE_DRM))
235          goto next;
236 
237       if (_eglHasAttrib(disp, EGL_DEVICE_EXT) && dev_list != disp->Device) {
238          goto next;
239       }
240 
241       device = _eglDeviceDrm(dev_list);
242       assert(device);
243 
244       if (!(device->available_nodes & (1 << node_type)))
245          goto next;
246 
247       dri2_dpy->fd_render_gpu = loader_open_device(device->nodes[node_type]);
248       if (dri2_dpy->fd_render_gpu < 0)
249          goto next;
250 
251       disp->Device = dev_list;
252 
253       char *driver_name = loader_get_driver_for_fd(dri2_dpy->fd_render_gpu);
254       if (swrast) {
255          /* Use kms swrast only with vgem / virtio_gpu.
256           * virtio-gpu fallbacks to software rendering when 3D features
257           * are unavailable since 6c5ab, and kms_swrast is more
258           * feature complete than swrast.
259           */
260          if (driver_name && (strcmp(driver_name, "vgem") == 0 ||
261                              strcmp(driver_name, "virtio_gpu") == 0))
262             dri2_dpy->driver_name = strdup("kms_swrast");
263          free(driver_name);
264       } else {
265          /* Use the given hardware driver */
266          dri2_dpy->driver_name = driver_name;
267       }
268 
269       if (dri2_dpy->driver_name && dri2_load_driver(disp)) {
270          if (swrast || zink)
271             dri2_dpy->loader_extensions = swrast_loader_extensions;
272          else
273             dri2_dpy->loader_extensions = image_loader_extensions;
274          break;
275       }
276 
277       free(dri2_dpy->driver_name);
278       dri2_dpy->driver_name = NULL;
279       close(dri2_dpy->fd_render_gpu);
280       dri2_dpy->fd_render_gpu = -1;
281 
282    next:
283       dev_list = _eglDeviceNext(dev_list);
284    }
285 
286    if (!dev_list)
287       return false;
288 
289    return true;
290 }
291 
292 static bool
surfaceless_probe_device_sw(_EGLDisplay * disp)293 surfaceless_probe_device_sw(_EGLDisplay *disp)
294 {
295    struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);
296    struct _egl_device *device = _eglFindDevice(dri2_dpy->fd_render_gpu, true);
297 
298    dri2_dpy->fd_render_gpu = -1;
299 
300    if (_eglHasAttrib(disp, EGL_DEVICE_EXT) && disp->Device != device) {
301       return false;
302    }
303 
304    disp->Device = device;
305    assert(disp->Device);
306 
307    dri2_dpy->driver_name = strdup(disp->Options.Zink ? "zink" : "swrast");
308    if (!dri2_dpy->driver_name)
309       return false;
310 
311    if (!dri2_load_driver(disp)) {
312       free(dri2_dpy->driver_name);
313       dri2_dpy->driver_name = NULL;
314       return false;
315    }
316 
317    dri2_dpy->loader_extensions = swrast_loader_extensions;
318    return true;
319 }
320 
321 EGLBoolean
dri2_initialize_surfaceless(_EGLDisplay * disp)322 dri2_initialize_surfaceless(_EGLDisplay *disp)
323 {
324    const char *err;
325    bool driver_loaded = false;
326    struct dri2_egl_display *dri2_dpy = dri2_display_create();
327    if (!dri2_dpy)
328       return EGL_FALSE;
329 
330    disp->DriverData = (void *)dri2_dpy;
331 
332    /* When ForceSoftware is false, we try the HW driver.  When ForceSoftware
333     * is true, we try kms_swrast and swrast in order.
334     */
335    driver_loaded = surfaceless_probe_device(disp, disp->Options.ForceSoftware,
336                                             disp->Options.Zink);
337    if (!driver_loaded && disp->Options.ForceSoftware) {
338       _eglLog(_EGL_DEBUG, "Falling back to surfaceless swrast without DRM.");
339       driver_loaded = surfaceless_probe_device_sw(disp);
340    }
341 
342    if (!driver_loaded) {
343       err = "DRI2: failed to load driver";
344       goto cleanup;
345    }
346 
347    dri2_dpy->fd_display_gpu = dri2_dpy->fd_render_gpu;
348 
349    if (!dri2_create_screen(disp)) {
350       err = "DRI2: failed to create screen";
351       goto cleanup;
352    }
353 
354    dri2_setup_screen(disp);
355 #ifdef HAVE_WAYLAND_PLATFORM
356    dri2_dpy->device_name =
357       loader_get_device_name_for_fd(dri2_dpy->fd_render_gpu);
358 #endif
359    dri2_set_WL_bind_wayland_display(disp);
360 
361    dri2_add_pbuffer_configs_for_visuals(disp);
362 
363    /* Fill vtbl last to prevent accidentally calling virtual function during
364     * initialization.
365     */
366    dri2_dpy->vtbl = &dri2_surfaceless_display_vtbl;
367 
368    return EGL_TRUE;
369 
370 cleanup:
371    dri2_display_destroy(disp);
372    return _eglError(EGL_NOT_INITIALIZED, err);
373 }
374