• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Mesa 3-D graphics library
3  *
4  * Copyright 2018 Collabora
5  *
6  * Based on platform_surfaceless, which has:
7  *
8  * Copyright (c) 2014 The Chromium OS Authors.
9  * Copyright © 2011 Intel Corporation
10  *
11  * Permission is hereby granted, free of charge, to any person obtaining a
12  * copy of this software and associated documentation files (the "Software"),
13  * to deal in the Software without restriction, including without limitation
14  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
15  * and/or sell copies of the Software, and to permit persons to whom the
16  * Software is furnished to do so, subject to the following conditions:
17  *
18  * The above copyright notice and this permission notice shall be included
19  * in all copies or substantial portions of the Software.
20  *
21  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
22  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
23  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
24  * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
25  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
26  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
27  * DEALINGS IN THE SOFTWARE.
28  */
29 #ifdef HAVE_LIBDRM
30 #include <xf86drm.h>
31 #endif
32 
33 #include <dlfcn.h>
34 #include <fcntl.h>
35 #include <stdio.h>
36 #include <stdlib.h>
37 #include <string.h>
38 #include <unistd.h>
39 #include <sys/stat.h>
40 #include <sys/types.h>
41 
42 #include "util/u_debug.h"
43 #include "egl_dri2.h"
44 #include "kopper_interface.h"
45 #include "loader.h"
46 
47 static __DRIimage *
device_alloc_image(struct dri2_egl_display * dri2_dpy,struct dri2_egl_surface * dri2_surf)48 device_alloc_image(struct dri2_egl_display *dri2_dpy,
49                    struct dri2_egl_surface *dri2_surf)
50 {
51    return dri2_dpy->image->createImage(
52       dri2_dpy->dri_screen_render_gpu, dri2_surf->base.Width,
53       dri2_surf->base.Height, dri2_surf->visual, 0, NULL);
54 }
55 
56 static void
device_free_images(struct dri2_egl_surface * dri2_surf)57 device_free_images(struct dri2_egl_surface *dri2_surf)
58 {
59    struct dri2_egl_display *dri2_dpy =
60       dri2_egl_display(dri2_surf->base.Resource.Display);
61 
62    if (dri2_surf->front) {
63       dri2_dpy->image->destroyImage(dri2_surf->front);
64       dri2_surf->front = NULL;
65    }
66 
67    free(dri2_surf->swrast_device_buffer);
68    dri2_surf->swrast_device_buffer = NULL;
69 }
70 
71 static int
device_image_get_buffers(__DRIdrawable * driDrawable,unsigned int format,uint32_t * stamp,void * loaderPrivate,uint32_t buffer_mask,struct __DRIimageList * buffers)72 device_image_get_buffers(__DRIdrawable *driDrawable, unsigned int format,
73                          uint32_t *stamp, void *loaderPrivate,
74                          uint32_t buffer_mask, struct __DRIimageList *buffers)
75 {
76    struct dri2_egl_surface *dri2_surf = loaderPrivate;
77    struct dri2_egl_display *dri2_dpy =
78       dri2_egl_display(dri2_surf->base.Resource.Display);
79 
80    buffers->image_mask = 0;
81    buffers->front = NULL;
82    buffers->back = NULL;
83 
84    /* The EGL 1.5 spec states that pbuffers are single-buffered. Specifically,
85     * the spec states that they have a back buffer but no front buffer, in
86     * contrast to pixmaps, which have a front buffer but no back buffer.
87     *
88     * Single-buffered surfaces with no front buffer confuse Mesa; so we deviate
89     * from the spec, following the precedent of Mesa's EGL X11 platform. The
90     * X11 platform correctly assigns pbuffers to single-buffered configs, but
91     * assigns the pbuffer a front buffer instead of a back buffer.
92     *
93     * Pbuffers in the X11 platform mostly work today, so let's just copy its
94     * behavior instead of trying to fix (and hence potentially breaking) the
95     * world.
96     */
97 
98    if (buffer_mask & __DRI_IMAGE_BUFFER_FRONT) {
99 
100       if (!dri2_surf->front)
101          dri2_surf->front = device_alloc_image(dri2_dpy, dri2_surf);
102 
103       buffers->image_mask |= __DRI_IMAGE_BUFFER_FRONT;
104       buffers->front = dri2_surf->front;
105    }
106 
107    return 1;
108 }
109 
110 static _EGLSurface *
dri2_device_create_surface(_EGLDisplay * disp,EGLint type,_EGLConfig * conf,const EGLint * attrib_list)111 dri2_device_create_surface(_EGLDisplay *disp, EGLint type, _EGLConfig *conf,
112                            const EGLint *attrib_list)
113 {
114    struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);
115    struct dri2_egl_config *dri2_conf = dri2_egl_config(conf);
116    struct dri2_egl_surface *dri2_surf;
117    const __DRIconfig *config;
118 
119    /* Make sure to calloc so all pointers
120     * are originally NULL.
121     */
122    dri2_surf = calloc(1, sizeof *dri2_surf);
123 
124    if (!dri2_surf) {
125       _eglError(EGL_BAD_ALLOC, "eglCreatePbufferSurface");
126       return NULL;
127    }
128 
129    if (!dri2_init_surface(&dri2_surf->base, disp, type, conf, attrib_list,
130                           false, NULL))
131       goto cleanup_surface;
132 
133    config = dri2_get_dri_config(dri2_conf, type, dri2_surf->base.GLColorspace);
134 
135    if (!config) {
136       _eglError(EGL_BAD_MATCH,
137                 "Unsupported surfacetype/colorspace configuration");
138       goto cleanup_surface;
139    }
140 
141    dri2_surf->visual = dri2_image_format_for_pbuffer_config(dri2_dpy, config);
142    if (dri2_surf->visual == PIPE_FORMAT_NONE)
143       goto cleanup_surface;
144 
145    if (!dri2_create_drawable(dri2_dpy, config, dri2_surf, dri2_surf))
146       goto cleanup_surface;
147 
148    return &dri2_surf->base;
149 
150 cleanup_surface:
151    free(dri2_surf);
152    return NULL;
153 }
154 
155 static EGLBoolean
device_destroy_surface(_EGLDisplay * disp,_EGLSurface * surf)156 device_destroy_surface(_EGLDisplay *disp, _EGLSurface *surf)
157 {
158    struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);
159    struct dri2_egl_surface *dri2_surf = dri2_egl_surface(surf);
160 
161    device_free_images(dri2_surf);
162 
163    dri2_dpy->core->destroyDrawable(dri2_surf->dri_drawable);
164 
165    dri2_fini_surface(surf);
166    free(dri2_surf);
167    return EGL_TRUE;
168 }
169 
170 static _EGLSurface *
dri2_device_create_pbuffer_surface(_EGLDisplay * disp,_EGLConfig * conf,const EGLint * attrib_list)171 dri2_device_create_pbuffer_surface(_EGLDisplay *disp, _EGLConfig *conf,
172                                    const EGLint *attrib_list)
173 {
174    return dri2_device_create_surface(disp, EGL_PBUFFER_BIT, conf, attrib_list);
175 }
176 
177 static const struct dri2_egl_display_vtbl dri2_device_display_vtbl = {
178    .create_pbuffer_surface = dri2_device_create_pbuffer_surface,
179    .destroy_surface = device_destroy_surface,
180    .create_image = dri2_create_image_khr,
181    .get_dri_drawable = dri2_surface_get_dri_drawable,
182 };
183 
184 static void
device_flush_front_buffer(__DRIdrawable * driDrawable,void * loaderPrivate)185 device_flush_front_buffer(__DRIdrawable *driDrawable, void *loaderPrivate)
186 {
187 }
188 
189 static unsigned
device_get_capability(void * loaderPrivate,enum dri_loader_cap cap)190 device_get_capability(void *loaderPrivate, enum dri_loader_cap cap)
191 {
192    /* Note: loaderPrivate is _EGLDisplay* */
193    switch (cap) {
194    case DRI_LOADER_CAP_FP16:
195       return 1;
196    default:
197       return 0;
198    }
199 }
200 
201 static const __DRIimageLoaderExtension image_loader_extension = {
202    .base = {__DRI_IMAGE_LOADER, 2},
203    .getBuffers = device_image_get_buffers,
204    .flushFrontBuffer = device_flush_front_buffer,
205    .getCapability = device_get_capability,
206 };
207 
208 static const __DRIkopperLoaderExtension kopper_loader_extension = {
209    .base = {__DRI_KOPPER_LOADER, 1},
210 
211    .SetSurfaceCreateInfo = NULL,
212 };
213 
214 static const __DRIextension *image_loader_extensions[] = {
215    &image_loader_extension.base,
216    &image_lookup_extension.base,
217    &use_invalidate.base,
218    &kopper_loader_extension.base,
219    NULL,
220 };
221 
222 static const __DRIextension *swrast_loader_extensions[] = {
223    &swrast_pbuffer_loader_extension.base,
224    &image_lookup_extension.base,
225    &use_invalidate.base,
226    &kopper_loader_extension.base,
227    NULL,
228 };
229 
230 static int
device_get_fd(_EGLDisplay * disp,_EGLDevice * dev)231 device_get_fd(_EGLDisplay *disp, _EGLDevice *dev)
232 {
233 #ifdef HAVE_LIBDRM
234    int fd = disp->Options.fd;
235    bool kms_swrast = disp->Options.ForceSoftware;
236    /* The fcntl() code in _eglGetDeviceDisplay() ensures that valid fd >= 3,
237     * and invalid one is 0.
238     */
239    if (fd) {
240       /* According to the spec - if the FD does not match the EGLDevice
241        * behaviour is undefined.
242        *
243        * Add a trivial sanity check since it doesn't cost us anything.
244        */
245       if (dev != _eglFindDevice(fd, false))
246          return -1;
247 
248       /* kms_swrast only work with primary node. It used to work with render
249        * node in the past because some downstream kernel carry a patch to enable
250        * dumb bo ioctl on render nodes.
251        */
252       char *node = kms_swrast ? drmGetPrimaryDeviceNameFromFd(fd)
253                               : drmGetRenderDeviceNameFromFd(fd);
254 
255       /* Don't close the internal fd, get render node one based on it. */
256       fd = loader_open_device(node);
257       free(node);
258       return fd;
259    }
260    const char *node = _eglQueryDeviceStringEXT(
261       dev, kms_swrast ? EGL_DRM_DEVICE_FILE_EXT : EGL_DRM_RENDER_NODE_FILE_EXT);
262    return loader_open_device(node);
263 #else
264    _eglLog(_EGL_FATAL,
265            "Driver bug: Built without libdrm, yet using a HW device");
266    return -1;
267 #endif
268 }
269 
270 static bool
device_probe_device(_EGLDisplay * disp)271 device_probe_device(_EGLDisplay *disp)
272 {
273    struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);
274    bool request_software =
275       debug_get_bool_option("LIBGL_ALWAYS_SOFTWARE", false);
276 
277    if (request_software)
278       _eglLog(_EGL_WARNING, "Not allowed to force software rendering when "
279                             "API explicitly selects a hardware device.");
280    dri2_dpy->fd_render_gpu = device_get_fd(disp, disp->Device);
281    if (dri2_dpy->fd_render_gpu < 0)
282       return false;
283 
284    dri2_dpy->fd_display_gpu = dri2_dpy->fd_render_gpu;
285 
286    dri2_dpy->driver_name = loader_get_driver_for_fd(dri2_dpy->fd_render_gpu);
287    if (!dri2_dpy->driver_name)
288       goto err_name;
289 
290    /* When doing software rendering, some times user still want to explicitly
291     * choose the render node device since cross node import doesn't work between
292     * vgem/virtio_gpu yet. It would be nice to have a new EXTENSION for this.
293     * For now, just fallback to kms_swrast. */
294    if (disp->Options.ForceSoftware && !request_software &&
295        (strcmp(dri2_dpy->driver_name, "vgem") == 0 ||
296         strcmp(dri2_dpy->driver_name, "virtio_gpu") == 0)) {
297       free(dri2_dpy->driver_name);
298       _eglLog(_EGL_WARNING, "NEEDS EXTENSION: falling back to kms_swrast");
299       dri2_dpy->driver_name = strdup("kms_swrast");
300    }
301 
302    if (!dri2_load_driver_dri3(disp))
303       goto err_load;
304 
305    dri2_dpy->loader_extensions = image_loader_extensions;
306    return true;
307 
308 err_load:
309    free(dri2_dpy->driver_name);
310    dri2_dpy->driver_name = NULL;
311 
312 err_name:
313    close(dri2_dpy->fd_render_gpu);
314    dri2_dpy->fd_render_gpu = dri2_dpy->fd_display_gpu = -1;
315    return false;
316 }
317 
318 static bool
device_probe_device_sw(_EGLDisplay * disp)319 device_probe_device_sw(_EGLDisplay *disp)
320 {
321    struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);
322 
323    dri2_dpy->fd_render_gpu = -1;
324    dri2_dpy->fd_display_gpu = -1;
325    dri2_dpy->driver_name = strdup(disp->Options.Zink ? "zink" : "swrast");
326    if (!dri2_dpy->driver_name)
327       return false;
328 
329    /* HACK: should be driver_swrast_null */
330    if (!dri2_load_driver_swrast(disp)) {
331       free(dri2_dpy->driver_name);
332       dri2_dpy->driver_name = NULL;
333       return false;
334    }
335 
336    dri2_dpy->loader_extensions = swrast_loader_extensions;
337    return true;
338 }
339 
340 EGLBoolean
dri2_initialize_device(_EGLDisplay * disp)341 dri2_initialize_device(_EGLDisplay *disp)
342 {
343    _EGLDevice *dev;
344    const char *err;
345    struct dri2_egl_display *dri2_dpy = dri2_display_create();
346    if (!dri2_dpy)
347       return EGL_FALSE;
348 
349    /* Extension requires a PlatformDisplay - the EGLDevice. */
350    dev = disp->PlatformDisplay;
351 
352    disp->Device = dev;
353    disp->DriverData = (void *)dri2_dpy;
354    err = "DRI2: failed to load driver";
355    if (_eglDeviceSupports(dev, _EGL_DEVICE_DRM)) {
356       if (!device_probe_device(disp))
357          goto cleanup;
358    } else if (_eglDeviceSupports(dev, _EGL_DEVICE_SOFTWARE)) {
359       if (!device_probe_device_sw(disp))
360          goto cleanup;
361    } else {
362       _eglLog(_EGL_FATAL,
363               "Driver bug: exposed device is neither DRM nor SOFTWARE one");
364       return EGL_FALSE;
365    }
366 
367    if (!dri2_create_screen(disp)) {
368       err = "DRI2: failed to create screen";
369       goto cleanup;
370    }
371 
372    if (!dri2_setup_extensions(disp)) {
373       err = "DRI2: failed to find required DRI extensions";
374       goto cleanup;
375    }
376 
377    dri2_setup_screen(disp);
378 #ifdef HAVE_WAYLAND_PLATFORM
379    dri2_dpy->device_name =
380       loader_get_device_name_for_fd(dri2_dpy->fd_render_gpu);
381 #endif
382    dri2_set_WL_bind_wayland_display(disp);
383 
384    dri2_add_pbuffer_configs_for_visuals(disp);
385 
386    /* Fill vtbl last to prevent accidentally calling virtual function during
387     * initialization.
388     */
389    dri2_dpy->vtbl = &dri2_device_display_vtbl;
390 
391    return EGL_TRUE;
392 
393 cleanup:
394    dri2_display_destroy(disp);
395    return _eglError(EGL_NOT_INITIALIZED, err);
396 }
397