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 #include "dri_util.h"
47
48 static __DRIimage *
device_alloc_image(struct dri2_egl_display * dri2_dpy,struct dri2_egl_surface * dri2_surf)49 device_alloc_image(struct dri2_egl_display *dri2_dpy,
50 struct dri2_egl_surface *dri2_surf)
51 {
52 return dri_create_image(
53 dri2_dpy->dri_screen_render_gpu, dri2_surf->base.Width,
54 dri2_surf->base.Height, dri2_surf->visual, NULL, 0, 0, NULL);
55 }
56
57 static void
device_free_images(struct dri2_egl_surface * dri2_surf)58 device_free_images(struct dri2_egl_surface *dri2_surf)
59 {
60 if (dri2_surf->front) {
61 dri2_destroy_image(dri2_surf->front);
62 dri2_surf->front = NULL;
63 }
64
65 free(dri2_surf->swrast_device_buffer);
66 dri2_surf->swrast_device_buffer = NULL;
67 }
68
69 static int
device_image_get_buffers(__DRIdrawable * driDrawable,unsigned int format,uint32_t * stamp,void * loaderPrivate,uint32_t buffer_mask,struct __DRIimageList * buffers)70 device_image_get_buffers(__DRIdrawable *driDrawable, unsigned int format,
71 uint32_t *stamp, void *loaderPrivate,
72 uint32_t buffer_mask, struct __DRIimageList *buffers)
73 {
74 struct dri2_egl_surface *dri2_surf = loaderPrivate;
75 struct dri2_egl_display *dri2_dpy =
76 dri2_egl_display(dri2_surf->base.Resource.Display);
77
78 buffers->image_mask = 0;
79 buffers->front = NULL;
80 buffers->back = NULL;
81
82 /* The EGL 1.5 spec states that pbuffers are single-buffered. Specifically,
83 * the spec states that they have a back buffer but no front buffer, in
84 * contrast to pixmaps, which have a front buffer but no back buffer.
85 *
86 * Single-buffered surfaces with no front buffer confuse Mesa; so we deviate
87 * from the spec, following the precedent of Mesa's EGL X11 platform. The
88 * X11 platform correctly assigns pbuffers to single-buffered configs, but
89 * assigns the pbuffer a front buffer instead of a back buffer.
90 *
91 * Pbuffers in the X11 platform mostly work today, so let's just copy its
92 * behavior instead of trying to fix (and hence potentially breaking) the
93 * world.
94 */
95
96 if (buffer_mask & __DRI_IMAGE_BUFFER_FRONT) {
97
98 if (!dri2_surf->front)
99 dri2_surf->front = device_alloc_image(dri2_dpy, dri2_surf);
100
101 buffers->image_mask |= __DRI_IMAGE_BUFFER_FRONT;
102 buffers->front = dri2_surf->front;
103 }
104
105 return 1;
106 }
107
108 static _EGLSurface *
dri2_device_create_surface(_EGLDisplay * disp,EGLint type,_EGLConfig * conf,const EGLint * attrib_list)109 dri2_device_create_surface(_EGLDisplay *disp, EGLint type, _EGLConfig *conf,
110 const EGLint *attrib_list)
111 {
112 struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);
113 struct dri2_egl_config *dri2_conf = dri2_egl_config(conf);
114 struct dri2_egl_surface *dri2_surf;
115 const __DRIconfig *config;
116
117 /* Make sure to calloc so all pointers
118 * are originally NULL.
119 */
120 dri2_surf = calloc(1, sizeof *dri2_surf);
121
122 if (!dri2_surf) {
123 _eglError(EGL_BAD_ALLOC, "eglCreatePbufferSurface");
124 return NULL;
125 }
126
127 if (!dri2_init_surface(&dri2_surf->base, disp, type, conf, attrib_list,
128 false, NULL))
129 goto cleanup_surface;
130
131 config = dri2_get_dri_config(dri2_conf, type, dri2_surf->base.GLColorspace);
132
133 if (!config) {
134 _eglError(EGL_BAD_MATCH,
135 "Unsupported surfacetype/colorspace configuration");
136 goto cleanup_surface;
137 }
138
139 dri2_surf->visual = dri2_image_format_for_pbuffer_config(dri2_dpy, config);
140 if (dri2_surf->visual == PIPE_FORMAT_NONE)
141 goto cleanup_surface;
142
143 if (!dri2_create_drawable(dri2_dpy, config, dri2_surf, dri2_surf))
144 goto cleanup_surface;
145
146 return &dri2_surf->base;
147
148 cleanup_surface:
149 free(dri2_surf);
150 return NULL;
151 }
152
153 static EGLBoolean
device_destroy_surface(_EGLDisplay * disp,_EGLSurface * surf)154 device_destroy_surface(_EGLDisplay *disp, _EGLSurface *surf)
155 {
156 struct dri2_egl_surface *dri2_surf = dri2_egl_surface(surf);
157
158 device_free_images(dri2_surf);
159
160 driDestroyDrawable(dri2_surf->dri_drawable);
161
162 dri2_fini_surface(surf);
163 free(dri2_surf);
164 return EGL_TRUE;
165 }
166
167 static _EGLSurface *
dri2_device_create_pbuffer_surface(_EGLDisplay * disp,_EGLConfig * conf,const EGLint * attrib_list)168 dri2_device_create_pbuffer_surface(_EGLDisplay *disp, _EGLConfig *conf,
169 const EGLint *attrib_list)
170 {
171 return dri2_device_create_surface(disp, EGL_PBUFFER_BIT, conf, attrib_list);
172 }
173
174 static const struct dri2_egl_display_vtbl dri2_device_display_vtbl = {
175 .create_pbuffer_surface = dri2_device_create_pbuffer_surface,
176 .destroy_surface = device_destroy_surface,
177 .create_image = dri2_create_image_khr,
178 .get_dri_drawable = dri2_surface_get_dri_drawable,
179 };
180
181 static void
device_flush_front_buffer(__DRIdrawable * driDrawable,void * loaderPrivate)182 device_flush_front_buffer(__DRIdrawable *driDrawable, void *loaderPrivate)
183 {
184 }
185
186 static unsigned
device_get_capability(void * loaderPrivate,enum dri_loader_cap cap)187 device_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 default:
194 return 0;
195 }
196 }
197
198 static const __DRIimageLoaderExtension image_loader_extension = {
199 .base = {__DRI_IMAGE_LOADER, 2},
200 .getBuffers = device_image_get_buffers,
201 .flushFrontBuffer = device_flush_front_buffer,
202 .getCapability = device_get_capability,
203 };
204
205 static const __DRIkopperLoaderExtension kopper_loader_extension = {
206 .base = {__DRI_KOPPER_LOADER, 1},
207
208 .SetSurfaceCreateInfo = NULL,
209 };
210
211 static const __DRIextension *image_loader_extensions[] = {
212 &image_loader_extension.base,
213 &image_lookup_extension.base,
214 &use_invalidate.base,
215 &kopper_loader_extension.base,
216 NULL,
217 };
218
219 static const __DRIextension *swrast_loader_extensions[] = {
220 &swrast_pbuffer_loader_extension.base,
221 &image_lookup_extension.base,
222 &use_invalidate.base,
223 &kopper_loader_extension.base,
224 NULL,
225 };
226
227 static int
device_get_fd(_EGLDisplay * disp,_EGLDevice * dev)228 device_get_fd(_EGLDisplay *disp, _EGLDevice *dev)
229 {
230 #ifdef HAVE_LIBDRM
231 int fd = disp->Options.fd;
232 bool kms_swrast = disp->Options.ForceSoftware;
233 /* The fcntl() code in _eglGetDeviceDisplay() ensures that valid fd >= 3,
234 * and invalid one is 0.
235 */
236 if (fd) {
237 /* According to the spec - if the FD does not match the EGLDevice
238 * behaviour is undefined.
239 *
240 * Add a trivial sanity check since it doesn't cost us anything.
241 */
242 if (dev != _eglFindDevice(fd, false))
243 return -1;
244
245 /* kms_swrast only work with primary node. It used to work with render
246 * node in the past because some downstream kernel carry a patch to enable
247 * dumb bo ioctl on render nodes.
248 */
249 char *node = kms_swrast ? drmGetPrimaryDeviceNameFromFd(fd)
250 : drmGetRenderDeviceNameFromFd(fd);
251
252 /* Don't close the internal fd, get render node one based on it. */
253 fd = loader_open_device(node);
254 free(node);
255 return fd;
256 }
257 const char *node = _eglQueryDeviceStringEXT(
258 dev, kms_swrast ? EGL_DRM_DEVICE_FILE_EXT : EGL_DRM_RENDER_NODE_FILE_EXT);
259 return loader_open_device(node);
260 #else
261 _eglLog(_EGL_FATAL,
262 "Driver bug: Built without libdrm, yet using a HW device");
263 return -1;
264 #endif
265 }
266
267 static bool
device_probe_device(_EGLDisplay * disp)268 device_probe_device(_EGLDisplay *disp)
269 {
270 struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);
271 bool request_software =
272 debug_get_bool_option("LIBGL_ALWAYS_SOFTWARE", false);
273
274 if (request_software)
275 _eglLog(_EGL_WARNING, "Not allowed to force software rendering when "
276 "API explicitly selects a hardware device.");
277 dri2_dpy->fd_render_gpu = device_get_fd(disp, disp->Device);
278 if (dri2_dpy->fd_render_gpu < 0)
279 return false;
280
281 dri2_dpy->fd_display_gpu = dri2_dpy->fd_render_gpu;
282
283 dri2_dpy->driver_name = loader_get_driver_for_fd(dri2_dpy->fd_render_gpu);
284 if (!dri2_dpy->driver_name)
285 goto err_name;
286
287 /* When doing software rendering, some times user still want to explicitly
288 * choose the render node device since cross node import doesn't work between
289 * vgem/virtio_gpu yet. It would be nice to have a new EXTENSION for this.
290 * For now, just fallback to kms_swrast. */
291 if (disp->Options.ForceSoftware && !request_software &&
292 (strcmp(dri2_dpy->driver_name, "vgem") == 0 ||
293 strcmp(dri2_dpy->driver_name, "virtio_gpu") == 0)) {
294 free(dri2_dpy->driver_name);
295 _eglLog(_EGL_WARNING, "NEEDS EXTENSION: falling back to kms_swrast");
296 dri2_dpy->driver_name = strdup("kms_swrast");
297 }
298
299 if (!dri2_load_driver(disp))
300 goto err_load;
301
302 dri2_dpy->loader_extensions = image_loader_extensions;
303 return true;
304
305 err_load:
306 free(dri2_dpy->driver_name);
307 dri2_dpy->driver_name = NULL;
308
309 err_name:
310 close(dri2_dpy->fd_render_gpu);
311 dri2_dpy->fd_render_gpu = dri2_dpy->fd_display_gpu = -1;
312 return false;
313 }
314
315 static bool
device_probe_device_sw(_EGLDisplay * disp)316 device_probe_device_sw(_EGLDisplay *disp)
317 {
318 struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);
319
320 dri2_dpy->fd_render_gpu = -1;
321 dri2_dpy->fd_display_gpu = -1;
322 dri2_dpy->driver_name = strdup(disp->Options.Zink ? "zink" : "swrast");
323 if (!dri2_dpy->driver_name)
324 return false;
325
326 /* HACK: should be driver_swrast_null */
327 if (!dri2_load_driver(disp)) {
328 free(dri2_dpy->driver_name);
329 dri2_dpy->driver_name = NULL;
330 return false;
331 }
332
333 dri2_dpy->loader_extensions = swrast_loader_extensions;
334 return true;
335 }
336
337 EGLBoolean
dri2_initialize_device(_EGLDisplay * disp)338 dri2_initialize_device(_EGLDisplay *disp)
339 {
340 const char *err;
341 struct dri2_egl_display *dri2_dpy = dri2_display_create();
342 if (!dri2_dpy)
343 return EGL_FALSE;
344
345 /* Extension requires a PlatformDisplay - the EGLDevice. */
346 disp->Device = disp->PlatformDisplay;
347
348 disp->DriverData = (void *)dri2_dpy;
349 err = "DRI2: failed to load driver";
350 if (_eglDeviceSupports(disp->Device, _EGL_DEVICE_DRM)) {
351 if (!device_probe_device(disp))
352 goto cleanup;
353 } else if (_eglDeviceSupports(disp->Device, _EGL_DEVICE_SOFTWARE)) {
354 if (!device_probe_device_sw(disp))
355 goto cleanup;
356 } else {
357 _eglLog(_EGL_FATAL,
358 "Driver bug: exposed device is neither DRM nor SOFTWARE one");
359 return EGL_FALSE;
360 }
361
362 if (!dri2_create_screen(disp)) {
363 err = "DRI2: failed to create screen";
364 goto cleanup;
365 }
366
367 dri2_setup_screen(disp);
368 #ifdef HAVE_WAYLAND_PLATFORM
369 dri2_dpy->device_name =
370 loader_get_device_name_for_fd(dri2_dpy->fd_render_gpu);
371 #endif
372 dri2_set_WL_bind_wayland_display(disp);
373
374 dri2_add_pbuffer_configs_for_visuals(disp);
375
376 /* Fill vtbl last to prevent accidentally calling virtual function during
377 * initialization.
378 */
379 dri2_dpy->vtbl = &dri2_device_display_vtbl;
380
381 return EGL_TRUE;
382
383 cleanup:
384 dri2_display_destroy(disp);
385 return _eglError(EGL_NOT_INITIALIZED, err);
386 }
387