Debian Codegraph @

src/egl/drivers/dri2/platform_device.c

raw
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 struct dri_image *
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
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
70 device_image_get_buffers(struct dri_drawable *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 *
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 struct dri_config *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
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 *
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
182 device_flush_front_buffer(struct dri_drawable *driDrawable, void *loaderPrivate)
183 {
184 }
185
186 static unsigned
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 __DRIextension *image_loader_extensions[] = {
206 &image_loader_extension.base,
207 &image_lookup_extension.base,
208 &kopper_pbuffer_loader_extension.base,
209 NULL,
210 };
211
212 static const __DRIextension *swrast_loader_extensions[] = {
213 &swrast_pbuffer_loader_extension.base,
214 &image_lookup_extension.base,
215 &kopper_pbuffer_loader_extension.base,
216 NULL,
217 };
218
219 static int
220 device_get_fd(_EGLDisplay *disp, _EGLDevice *dev)
221 {
222 #ifdef HAVE_LIBDRM
223 int fd = disp->Options.fd;
224 bool kms_swrast = disp->Options.ForceSoftware;
225 /* The fcntl() code in _eglGetDeviceDisplay() ensures that valid fd >= 3,
226 * and invalid one is 0.
227 */
228 if (fd) {
229 /* According to the spec - if the FD does not match the EGLDevice
230 * behaviour is undefined.
231 *
232 * Add a trivial sanity check since it doesn't cost us anything.
233 */
234 if (dev != _eglFindDevice(fd, false))
235 return -1;
236
237 /* kms_swrast only work with primary node. It used to work with render
238 * node in the past because some downstream kernel carry a patch to enable
239 * dumb bo ioctl on render nodes.
240 */
241 char *node = kms_swrast ? drmGetPrimaryDeviceNameFromFd(fd)
242 : drmGetRenderDeviceNameFromFd(fd);
243
244 /* Don't close the internal fd, get render node one based on it. */
245 fd = loader_open_device(node);
246 free(node);
247 return fd;
248 }
249 const char *node = _eglQueryDeviceStringEXT(
250 dev, kms_swrast ? EGL_DRM_DEVICE_FILE_EXT : EGL_DRM_RENDER_NODE_FILE_EXT);
251 return loader_open_device(node);
252 #else
253 _eglLog(_EGL_FATAL,
254 "Driver bug: Built without libdrm, yet using a HW device");
255 return -1;
256 #endif
257 }
258
259 static bool
260 device_probe_device(_EGLDisplay *disp)
261 {
262 struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);
263
264 dri2_dpy->fd_render_gpu = device_get_fd(disp, disp->Device);
265 if (dri2_dpy->fd_render_gpu < 0)
266 return false;
267
268 dri2_dpy->fd_display_gpu = dri2_dpy->fd_render_gpu;
269
270 dri2_dpy->driver_name = loader_get_driver_for_fd(dri2_dpy->fd_render_gpu);
271 if (!dri2_dpy->driver_name)
272 goto err_name;
273
274 /* this is software fallback */
275 if (disp->Options.ForceSoftware) {
276 /* When doing software rendering, some times user still want to explicitly
277 * choose the render node device since cross node import doesn't work between
278 * vgem/virtio_gpu yet. It would be nice to have a new EXTENSION for this.
279 * For now, just fallback to kms_swrast. */
280 if (strcmp(dri2_dpy->driver_name, "vgem") == 0 ||
281 strcmp(dri2_dpy->driver_name, "virtio_gpu") == 0) {
282 free(dri2_dpy->driver_name);
283 _eglLog(_EGL_WARNING, "NEEDS EXTENSION: falling back to kms_swrast");
284 dri2_dpy->driver_name = strdup("kms_swrast");
285 } else if (strcmp(dri2_dpy->driver_name, "vmwgfx")) {
286 /* this is software fallback; deny progress since a hardware device was requested */
287 return false;
288 }
289 }
290
291 dri2_detect_swrast_kopper(disp);
292
293 dri2_dpy->loader_extensions = image_loader_extensions;
294 return true;
295
296 err_name:
297 close(dri2_dpy->fd_render_gpu);
298 dri2_dpy->fd_render_gpu = dri2_dpy->fd_display_gpu = -1;
299 return false;
300 }
301
302 static bool
303 device_probe_device_sw(_EGLDisplay *disp)
304 {
305 struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);
306
307 dri2_dpy->fd_render_gpu = -1;
308 dri2_dpy->fd_display_gpu = -1;
309 dri2_dpy->driver_name = strdup(disp->Options.Zink ? "zink" : "swrast");
310 if (!dri2_dpy->driver_name)
311 return false;
312
313 /* HACK: should be driver_swrast_null */
314 dri2_detect_swrast_kopper(disp);
315
316 dri2_dpy->loader_extensions = swrast_loader_extensions;
317 return true;
318 }
319
320 EGLBoolean
321 dri2_initialize_device(_EGLDisplay *disp)
322 {
323 const char *err;
324 struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);
325 bool request_software =
326 debug_get_bool_option("LIBGL_ALWAYS_SOFTWARE", false);
327
328 /* Extension requires a PlatformDisplay - the EGLDevice. */
329 disp->Device = disp->PlatformDisplay;
330
331 if (request_software)
332 _eglLog(_EGL_WARNING, "Not allowed to force software rendering when "
333 "API explicitly selects a hardware device.");
334
335 err = "DRI2: failed to load driver";
336 /* device-drm platform cannot be explicit sw (because explicit sw is llvmpipe) */
337 if (!request_software && _eglDeviceSupports(disp->Device, _EGL_DEVICE_DRM)) {
338 if (!device_probe_device(disp))
339 goto cleanup;
340 } else if (request_software || _eglDeviceSupports(disp->Device, _EGL_DEVICE_SOFTWARE)) {
341 if (!device_probe_device_sw(disp))
342 goto cleanup;
343 } else {
344 _eglLog(_EGL_FATAL,
345 "Driver bug: exposed device is neither DRM nor SOFTWARE one");
346 return EGL_FALSE;
347 }
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_device_display_vtbl;
367
368 return EGL_TRUE;
369
370 cleanup:
371 return _eglError(EGL_NOT_INITIALIZED, err);
372 }