• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 #include "kopper_interface.h"
39 
40 static __DRIimage*
surfaceless_alloc_image(struct dri2_egl_display * dri2_dpy,struct dri2_egl_surface * dri2_surf)41 surfaceless_alloc_image(struct dri2_egl_display *dri2_dpy,
42                      struct dri2_egl_surface *dri2_surf)
43 {
44    return dri2_dpy->image->createImage(
45             dri2_dpy->dri_screen,
46             dri2_surf->base.Width,
47             dri2_surf->base.Height,
48             dri2_surf->visual,
49             0,
50             NULL);
51 }
52 
53 static void
surfaceless_free_images(struct dri2_egl_surface * dri2_surf)54 surfaceless_free_images(struct dri2_egl_surface *dri2_surf)
55 {
56    struct dri2_egl_display *dri2_dpy =
57       dri2_egl_display(dri2_surf->base.Resource.Display);
58 
59    if (dri2_surf->front) {
60       dri2_dpy->image->destroyImage(dri2_surf->front);
61       dri2_surf->front = NULL;
62    }
63 
64    free(dri2_surf->swrast_device_buffer);
65    dri2_surf->swrast_device_buffer = NULL;
66 }
67 
68 static int
surfaceless_image_get_buffers(__DRIdrawable * driDrawable,unsigned int format,uint32_t * stamp,void * loaderPrivate,uint32_t buffer_mask,struct __DRIimageList * buffers)69 surfaceless_image_get_buffers(__DRIdrawable *driDrawable,
70                         unsigned int format,
71                         uint32_t *stamp,
72                         void *loaderPrivate,
73                         uint32_t buffer_mask,
74                         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 =
102             surfaceless_alloc_image(dri2_dpy, dri2_surf);
103 
104       buffers->image_mask |= __DRI_IMAGE_BUFFER_FRONT;
105       buffers->front = dri2_surf->front;
106    }
107 
108    return 1;
109 }
110 
111 static _EGLSurface *
dri2_surfaceless_create_surface(_EGLDisplay * disp,EGLint type,_EGLConfig * conf,const EGLint * attrib_list)112 dri2_surfaceless_create_surface(_EGLDisplay *disp, EGLint type,
113                                 _EGLConfig *conf, const EGLint *attrib_list)
114 {
115    struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);
116    struct dri2_egl_config *dri2_conf = dri2_egl_config(conf);
117    struct dri2_egl_surface *dri2_surf;
118    const __DRIconfig *config;
119 
120    /* Make sure to calloc so all pointers
121     * are originally NULL.
122     */
123    dri2_surf = calloc(1, sizeof *dri2_surf);
124 
125    if (!dri2_surf) {
126       _eglError(EGL_BAD_ALLOC, "eglCreatePbufferSurface");
127       return NULL;
128    }
129 
130    if (!dri2_init_surface(&dri2_surf->base, disp, type, conf, attrib_list,
131                           false, NULL))
132       goto cleanup_surface;
133 
134    config = dri2_get_dri_config(dri2_conf, type,
135                                 dri2_surf->base.GLColorspace);
136 
137    if (!config) {
138       _eglError(EGL_BAD_MATCH, "Unsupported surfacetype/colorspace configuration");
139       goto cleanup_surface;
140    }
141 
142    dri2_surf->visual = dri2_image_format_for_pbuffer_config(dri2_dpy, config);
143    if (dri2_surf->visual == __DRI_IMAGE_FORMAT_NONE)
144       goto cleanup_surface;
145 
146    if (!dri2_create_drawable(dri2_dpy, config, dri2_surf, dri2_surf))
147       goto cleanup_surface;
148 
149    return &dri2_surf->base;
150 
151    cleanup_surface:
152       free(dri2_surf);
153       return NULL;
154 }
155 
156 static EGLBoolean
surfaceless_destroy_surface(_EGLDisplay * disp,_EGLSurface * surf)157 surfaceless_destroy_surface(_EGLDisplay *disp, _EGLSurface *surf)
158 {
159    struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);
160    struct dri2_egl_surface *dri2_surf = dri2_egl_surface(surf);
161 
162    surfaceless_free_images(dri2_surf);
163 
164    dri2_dpy->core->destroyDrawable(dri2_surf->dri_drawable);
165 
166    dri2_fini_surface(surf);
167    free(dri2_surf);
168    return EGL_TRUE;
169 }
170 
171 static _EGLSurface *
dri2_surfaceless_create_pbuffer_surface(_EGLDisplay * disp,_EGLConfig * conf,const EGLint * attrib_list)172 dri2_surfaceless_create_pbuffer_surface(_EGLDisplay *disp, _EGLConfig *conf,
173                                         const EGLint *attrib_list)
174 {
175    return dri2_surfaceless_create_surface(disp, EGL_PBUFFER_BIT, conf,
176                                   attrib_list);
177 }
178 
179 static const struct dri2_egl_display_vtbl dri2_surfaceless_display_vtbl = {
180    .create_pbuffer_surface = dri2_surfaceless_create_pbuffer_surface,
181    .destroy_surface = surfaceless_destroy_surface,
182    .create_image = dri2_create_image_khr,
183    .get_dri_drawable = dri2_surface_get_dri_drawable,
184 };
185 
186 static void
surfaceless_flush_front_buffer(__DRIdrawable * driDrawable,void * loaderPrivate)187 surfaceless_flush_front_buffer(__DRIdrawable *driDrawable, void *loaderPrivate)
188 {
189 }
190 
191 static unsigned
surfaceless_get_capability(void * loaderPrivate,enum dri_loader_cap cap)192 surfaceless_get_capability(void *loaderPrivate, enum dri_loader_cap cap)
193 {
194    /* Note: loaderPrivate is _EGLDisplay* */
195    switch (cap) {
196    case DRI_LOADER_CAP_FP16:
197       return 1;
198    default:
199       return 0;
200    }
201 }
202 
203 static const __DRIkopperLoaderExtension kopper_loader_extension = {
204     .base = { __DRI_KOPPER_LOADER, 1 },
205 
206     .SetSurfaceCreateInfo   = NULL,
207 };
208 
209 static const __DRIimageLoaderExtension image_loader_extension = {
210    .base             = { __DRI_IMAGE_LOADER, 2 },
211    .getBuffers       = surfaceless_image_get_buffers,
212    .flushFrontBuffer = surfaceless_flush_front_buffer,
213    .getCapability    = surfaceless_get_capability,
214 };
215 
216 static const __DRIextension *image_loader_extensions[] = {
217    &image_loader_extension.base,
218    &image_lookup_extension.base,
219    &use_invalidate.base,
220    &background_callable_extension.base,
221    &kopper_loader_extension.base,
222    NULL,
223 };
224 
225 static const __DRIextension *swrast_loader_extensions[] = {
226    &swrast_pbuffer_loader_extension.base,
227    &image_loader_extension.base,
228    &image_lookup_extension.base,
229    &use_invalidate.base,
230    &kopper_loader_extension.base,
231    NULL,
232 };
233 
234 static bool
surfaceless_probe_device(_EGLDisplay * disp,bool swrast)235 surfaceless_probe_device(_EGLDisplay *disp, bool swrast)
236 {
237 #define MAX_DRM_DEVICES 64
238    const unsigned node_type = swrast ? DRM_NODE_PRIMARY : DRM_NODE_RENDER;
239    struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);
240    drmDevicePtr device, devices[MAX_DRM_DEVICES] = { NULL };
241    int i, num_devices;
242 
243    num_devices = drmGetDevices2(0, devices, ARRAY_SIZE(devices));
244    if (num_devices < 0)
245       return false;
246 
247    for (i = 0; i < num_devices; ++i) {
248       device = devices[i];
249 
250       if (!(device->available_nodes & (1 << node_type)))
251          continue;
252 
253       dri2_dpy->fd = loader_open_device(device->nodes[node_type]);
254       if (dri2_dpy->fd < 0)
255          continue;
256 
257       disp->Device = _eglAddDevice(dri2_dpy->fd, swrast);
258       if (!disp->Device) {
259          close(dri2_dpy->fd);
260          dri2_dpy->fd = -1;
261          continue;
262       }
263 
264       char *driver_name = loader_get_driver_for_fd(dri2_dpy->fd);
265       if (swrast) {
266          /* Use kms swrast only with vgem / virtio_gpu.
267           * virtio-gpu fallbacks to software rendering when 3D features
268           * are unavailable since 6c5ab, and kms_swrast is more
269           * feature complete than swrast.
270           */
271          if (driver_name &&
272              (strcmp(driver_name, "vgem") == 0 ||
273               strcmp(driver_name, "virtio_gpu") == 0))
274             dri2_dpy->driver_name = strdup("kms_swrast");
275          free(driver_name);
276       } else {
277          /* Use the given hardware driver */
278          dri2_dpy->driver_name = driver_name;
279       }
280 
281       if (dri2_dpy->driver_name && dri2_load_driver_dri3(disp))
282          break;
283 
284       free(dri2_dpy->driver_name);
285       dri2_dpy->driver_name = NULL;
286       close(dri2_dpy->fd);
287       dri2_dpy->fd = -1;
288    }
289    drmFreeDevices(devices, num_devices);
290 
291    if (i == num_devices)
292       return false;
293 
294    if (swrast)
295       dri2_dpy->loader_extensions = swrast_loader_extensions;
296    else
297       dri2_dpy->loader_extensions = image_loader_extensions;
298 
299    return true;
300 }
301 
302 static bool
surfaceless_probe_device_sw(_EGLDisplay * disp)303 surfaceless_probe_device_sw(_EGLDisplay *disp)
304 {
305    struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);
306 
307    dri2_dpy->fd = -1;
308    disp->Device = _eglAddDevice(dri2_dpy->fd, true);
309    assert(disp->Device);
310 
311    dri2_dpy->driver_name = strdup(disp->Options.Zink ? "zink" : "swrast");
312    if (!dri2_dpy->driver_name)
313       return false;
314 
315    if (!dri2_load_driver_swrast(disp)) {
316       free(dri2_dpy->driver_name);
317       dri2_dpy->driver_name = NULL;
318       return false;
319    }
320 
321    dri2_dpy->loader_extensions = swrast_loader_extensions;
322    return true;
323 }
324 
325 EGLBoolean
dri2_initialize_surfaceless(_EGLDisplay * disp)326 dri2_initialize_surfaceless(_EGLDisplay *disp)
327 {
328    struct dri2_egl_display *dri2_dpy;
329    const char* err;
330    bool driver_loaded = false;
331 
332    dri2_dpy = calloc(1, sizeof *dri2_dpy);
333    if (!dri2_dpy)
334       return _eglError(EGL_BAD_ALLOC, "eglInitialize");
335 
336    dri2_dpy->fd = -1;
337    disp->DriverData = (void *) dri2_dpy;
338 
339    /* When ForceSoftware is false, we try the HW driver.  When ForceSoftware
340     * is true, we try kms_swrast and swrast in order.
341     */
342    driver_loaded = surfaceless_probe_device(disp, disp->Options.ForceSoftware);
343    if (!driver_loaded && disp->Options.ForceSoftware) {
344       _eglLog(_EGL_DEBUG, "Falling back to surfaceless swrast without DRM.");
345       driver_loaded = surfaceless_probe_device_sw(disp);
346    }
347 
348    if (!driver_loaded) {
349       err = "DRI2: failed to load driver";
350       goto cleanup;
351    }
352 
353    if (!dri2_create_screen(disp)) {
354       err = "DRI2: failed to create screen";
355       goto cleanup;
356    }
357 
358    if (!dri2_setup_extensions(disp)) {
359       err = "DRI2: failed to find required DRI extensions";
360       goto cleanup;
361    }
362 
363    dri2_setup_screen(disp);
364 #ifdef HAVE_WAYLAND_PLATFORM
365    dri2_dpy->device_name = loader_get_device_name_for_fd(dri2_dpy->fd);
366 #endif
367    dri2_set_WL_bind_wayland_display(disp);
368 
369    if (!dri2_add_pbuffer_configs_for_visuals(disp)) {
370       err = "DRI2: failed to add configs";
371       goto cleanup;
372    }
373 
374    /* Fill vtbl last to prevent accidentally calling virtual function during
375     * initialization.
376     */
377    dri2_dpy->vtbl = &dri2_surfaceless_display_vtbl;
378 
379    return EGL_TRUE;
380 
381 cleanup:
382    dri2_display_destroy(disp);
383    return _eglError(EGL_NOT_INITIALIZED, err);
384 }
385