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 <stdlib.h>
27 #include <stdio.h>
28 #include <string.h>
29 #include <xf86drm.h>
30 #include <dlfcn.h>
31 #include <sys/types.h>
32 #include <sys/stat.h>
33 #include <fcntl.h>
34 #include <unistd.h>
35 
36 #include "egl_dri2.h"
37 #include "loader.h"
38 
39 static __DRIimage*
surfaceless_alloc_image(struct dri2_egl_display * dri2_dpy,struct dri2_egl_surface * dri2_surf)40 surfaceless_alloc_image(struct dri2_egl_display *dri2_dpy,
41                      struct dri2_egl_surface *dri2_surf)
42 {
43    return dri2_dpy->image->createImage(
44             dri2_dpy->dri_screen,
45             dri2_surf->base.Width,
46             dri2_surf->base.Height,
47             dri2_surf->visual,
48             0,
49             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    struct dri2_egl_display *dri2_dpy =
56       dri2_egl_display(dri2_surf->base.Resource.Display);
57 
58    if (dri2_surf->front) {
59       dri2_dpy->image->destroyImage(dri2_surf->front);
60       dri2_surf->front = NULL;
61    }
62 
63    free(dri2_surf->swrast_device_buffer);
64    dri2_surf->swrast_device_buffer = NULL;
65 }
66 
67 static int
surfaceless_image_get_buffers(__DRIdrawable * driDrawable,unsigned int format,uint32_t * stamp,void * loaderPrivate,uint32_t buffer_mask,struct __DRIimageList * buffers)68 surfaceless_image_get_buffers(__DRIdrawable *driDrawable,
69                         unsigned int format,
70                         uint32_t *stamp,
71                         void *loaderPrivate,
72                         uint32_t buffer_mask,
73                         struct __DRIimageList *buffers)
74 {
75    struct dri2_egl_surface *dri2_surf = loaderPrivate;
76    struct dri2_egl_display *dri2_dpy =
77       dri2_egl_display(dri2_surf->base.Resource.Display);
78 
79    buffers->image_mask = 0;
80    buffers->front = NULL;
81    buffers->back = NULL;
82 
83    /* The EGL 1.5 spec states that pbuffers are single-buffered. Specifically,
84     * the spec states that they have a back buffer but no front buffer, in
85     * contrast to pixmaps, which have a front buffer but no back buffer.
86     *
87     * Single-buffered surfaces with no front buffer confuse Mesa; so we deviate
88     * from the spec, following the precedent of Mesa's EGL X11 platform. The
89     * X11 platform correctly assigns pbuffers to single-buffered configs, but
90     * assigns the pbuffer a front buffer instead of a back buffer.
91     *
92     * Pbuffers in the X11 platform mostly work today, so let's just copy its
93     * behavior instead of trying to fix (and hence potentially breaking) the
94     * world.
95     */
96 
97    if (buffer_mask & __DRI_IMAGE_BUFFER_FRONT) {
98 
99       if (!dri2_surf->front)
100          dri2_surf->front =
101             surfaceless_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_surfaceless_create_surface(_EGLDisplay * disp,EGLint type,_EGLConfig * conf,const EGLint * attrib_list)111 dri2_surfaceless_create_surface(_EGLDisplay *disp, EGLint type,
112                                 _EGLConfig *conf, 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,
134                                 dri2_surf->base.GLColorspace);
135 
136    if (!config) {
137       _eglError(EGL_BAD_MATCH, "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 == __DRI_IMAGE_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
surfaceless_destroy_surface(_EGLDisplay * disp,_EGLSurface * surf)156 surfaceless_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    surfaceless_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_surfaceless_create_pbuffer_surface(_EGLDisplay * disp,_EGLConfig * conf,const EGLint * attrib_list)171 dri2_surfaceless_create_pbuffer_surface(_EGLDisplay *disp, _EGLConfig *conf,
172                                         const EGLint *attrib_list)
173 {
174    return dri2_surfaceless_create_surface(disp, EGL_PBUFFER_BIT, conf,
175                                   attrib_list);
176 }
177 
178 static const struct dri2_egl_display_vtbl dri2_surfaceless_display_vtbl = {
179    .create_pbuffer_surface = dri2_surfaceless_create_pbuffer_surface,
180    .destroy_surface = surfaceless_destroy_surface,
181    .create_image = dri2_create_image_khr,
182    .get_dri_drawable = dri2_surface_get_dri_drawable,
183 };
184 
185 static void
surfaceless_flush_front_buffer(__DRIdrawable * driDrawable,void * loaderPrivate)186 surfaceless_flush_front_buffer(__DRIdrawable *driDrawable, void *loaderPrivate)
187 {
188 }
189 
190 static unsigned
surfaceless_get_capability(void * loaderPrivate,enum dri_loader_cap cap)191 surfaceless_get_capability(void *loaderPrivate, enum dri_loader_cap cap)
192 {
193    /* Note: loaderPrivate is _EGLDisplay* */
194    switch (cap) {
195    case DRI_LOADER_CAP_FP16:
196       return 1;
197    default:
198       return 0;
199    }
200 }
201 
202 static const __DRIimageLoaderExtension image_loader_extension = {
203    .base             = { __DRI_IMAGE_LOADER, 2 },
204    .getBuffers       = surfaceless_image_get_buffers,
205    .flushFrontBuffer = surfaceless_flush_front_buffer,
206    .getCapability    = surfaceless_get_capability,
207 };
208 
209 static const __DRIextension *image_loader_extensions[] = {
210    &image_loader_extension.base,
211    &image_lookup_extension.base,
212    &use_invalidate.base,
213    &background_callable_extension.base,
214    NULL,
215 };
216 
217 static const __DRIextension *swrast_loader_extensions[] = {
218    &swrast_pbuffer_loader_extension.base,
219    &image_loader_extension.base,
220    &image_lookup_extension.base,
221    &use_invalidate.base,
222    NULL,
223 };
224 
225 static bool
surfaceless_probe_device(_EGLDisplay * disp,bool swrast)226 surfaceless_probe_device(_EGLDisplay *disp, bool swrast)
227 {
228 #define MAX_DRM_DEVICES 64
229    const unsigned node_type = swrast ? DRM_NODE_PRIMARY : DRM_NODE_RENDER;
230    struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);
231    drmDevicePtr device, devices[MAX_DRM_DEVICES] = { NULL };
232    int i, num_devices;
233 
234    num_devices = drmGetDevices2(0, devices, ARRAY_SIZE(devices));
235    if (num_devices < 0)
236       return false;
237 
238    for (i = 0; i < num_devices; ++i) {
239       device = devices[i];
240 
241       if (!(device->available_nodes & (1 << node_type)))
242          continue;
243 
244       dri2_dpy->fd = loader_open_device(device->nodes[node_type]);
245       if (dri2_dpy->fd < 0)
246          continue;
247 
248       disp->Device = _eglAddDevice(dri2_dpy->fd, swrast);
249       if (!disp->Device) {
250          close(dri2_dpy->fd);
251          dri2_dpy->fd = -1;
252          continue;
253       }
254 
255       char *driver_name = loader_get_driver_for_fd(dri2_dpy->fd);
256       if (swrast) {
257          /* Use kms swrast only with vgem / virtio_gpu.
258           * virtio-gpu fallbacks to software rendering when 3D features
259           * are unavailable since 6c5ab, and kms_swrast is more
260           * feature complete than swrast.
261           */
262          if (driver_name &&
263              (strcmp(driver_name, "vgem") == 0 ||
264               strcmp(driver_name, "virtio_gpu") == 0))
265             dri2_dpy->driver_name = strdup("kms_swrast");
266          free(driver_name);
267       } else {
268          /* Use the given hardware driver */
269          dri2_dpy->driver_name = driver_name;
270       }
271 
272       if (dri2_dpy->driver_name && dri2_load_driver_dri3(disp))
273          break;
274 
275       free(dri2_dpy->driver_name);
276       dri2_dpy->driver_name = NULL;
277       close(dri2_dpy->fd);
278       dri2_dpy->fd = -1;
279    }
280    drmFreeDevices(devices, num_devices);
281 
282    if (i == num_devices)
283       return false;
284 
285    if (swrast)
286       dri2_dpy->loader_extensions = swrast_loader_extensions;
287    else
288       dri2_dpy->loader_extensions = image_loader_extensions;
289 
290    return true;
291 }
292 
293 static bool
surfaceless_probe_device_sw(_EGLDisplay * disp)294 surfaceless_probe_device_sw(_EGLDisplay *disp)
295 {
296    struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);
297 
298    dri2_dpy->fd = -1;
299    disp->Device = _eglAddDevice(dri2_dpy->fd, true);
300    assert(disp->Device);
301 
302    dri2_dpy->driver_name = strdup("swrast");
303    if (!dri2_dpy->driver_name)
304       return false;
305 
306    if (!dri2_load_driver_swrast(disp)) {
307       free(dri2_dpy->driver_name);
308       dri2_dpy->driver_name = NULL;
309       return false;
310    }
311 
312    dri2_dpy->loader_extensions = swrast_loader_extensions;
313    return true;
314 }
315 
316 EGLBoolean
dri2_initialize_surfaceless(_EGLDisplay * disp)317 dri2_initialize_surfaceless(_EGLDisplay *disp)
318 {
319    struct dri2_egl_display *dri2_dpy;
320    const char* err;
321    bool driver_loaded = false;
322 
323    dri2_dpy = calloc(1, sizeof *dri2_dpy);
324    if (!dri2_dpy)
325       return _eglError(EGL_BAD_ALLOC, "eglInitialize");
326 
327    dri2_dpy->fd = -1;
328    disp->DriverData = (void *) dri2_dpy;
329 
330    /* When ForceSoftware is false, we try the HW driver.  When ForceSoftware
331     * is true, we try kms_swrast and swrast in order.
332     */
333    driver_loaded = surfaceless_probe_device(disp, disp->Options.ForceSoftware);
334    if (!driver_loaded && disp->Options.ForceSoftware) {
335       _eglLog(_EGL_DEBUG, "Falling back to surfaceless swrast without DRM.");
336       driver_loaded = surfaceless_probe_device_sw(disp);
337    }
338 
339    if (!driver_loaded) {
340       err = "DRI2: failed to load driver";
341       goto cleanup;
342    }
343 
344    if (!dri2_create_screen(disp)) {
345       err = "DRI2: failed to create screen";
346       goto cleanup;
347    }
348 
349    if (!dri2_setup_extensions(disp)) {
350       err = "DRI2: failed to find required DRI extensions";
351       goto cleanup;
352    }
353 
354    dri2_setup_screen(disp);
355 #ifdef HAVE_WAYLAND_PLATFORM
356    dri2_dpy->device_name = loader_get_device_name_for_fd(dri2_dpy->fd);
357 #endif
358    dri2_set_WL_bind_wayland_display(disp);
359 
360    if (!dri2_add_pbuffer_configs_for_visuals(disp)) {
361       err = "DRI2: failed to add configs";
362       goto cleanup;
363    }
364 
365    /* Fill vtbl last to prevent accidentally calling virtual function during
366     * initialization.
367     */
368    dri2_dpy->vtbl = &dri2_surfaceless_display_vtbl;
369 
370    return EGL_TRUE;
371 
372 cleanup:
373    dri2_display_destroy(disp);
374    return _eglError(EGL_NOT_INITIALIZED, err);
375 }
376