Debian Codegraph @

src/egl/drivers/dri2/platform_surfaceless.c

raw
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 <dlfcn.h>
27 #include <fcntl.h>
28 #include <stdio.h>
29 #include <stdlib.h>
30 #include <string.h>
31 #include <unistd.h>
32 #include "pipe/p_screen.h"
33 #include "util/libdrm.h"
34 #include <sys/stat.h>
35 #include <sys/types.h>
36
37 #include "egl_dri2.h"
38 #include "eglglobals.h"
39 #include "kopper_interface.h"
40 #include "loader.h"
41 #include "loader_dri_helper.h"
42 #include "dri_util.h"
43 #include "dri_screen.h"
44
45 static struct dri_image *
46 surfaceless_alloc_image(struct dri2_egl_display *dri2_dpy,
47 struct dri2_egl_surface *dri2_surf)
48 {
49 return dri_create_image(
50 dri2_dpy->dri_screen_render_gpu, dri2_surf->base.Width,
51 dri2_surf->base.Height, dri2_surf->visual, NULL, 0, 0, NULL);
52 }
53
54 static void
55 surfaceless_free_images(struct dri2_egl_surface *dri2_surf)
56 {
57 if (dri2_surf->front) {
58 dri2_destroy_image(dri2_surf->front);
59 dri2_surf->front = NULL;
60 }
61
62 free(dri2_surf->swrast_device_buffer);
63 dri2_surf->swrast_device_buffer = NULL;
64 }
65
66 static int
67 surfaceless_image_get_buffers(struct dri_drawable *driDrawable, unsigned int format,
68 uint32_t *stamp, void *loaderPrivate,
69 uint32_t buffer_mask,
70 struct __DRIimageList *buffers)
71 {
72 struct dri2_egl_surface *dri2_surf = loaderPrivate;
73 struct dri2_egl_display *dri2_dpy =
74 dri2_egl_display(dri2_surf->base.Resource.Display);
75
76 buffers->image_mask = 0;
77 buffers->front = NULL;
78 buffers->back = NULL;
79
80 /* The EGL 1.5 spec states that pbuffers are single-buffered. Specifically,
81 * the spec states that they have a back buffer but no front buffer, in
82 * contrast to pixmaps, which have a front buffer but no back buffer.
83 *
84 * Single-buffered surfaces with no front buffer confuse Mesa; so we deviate
85 * from the spec, following the precedent of Mesa's EGL X11 platform. The
86 * X11 platform correctly assigns pbuffers to single-buffered configs, but
87 * assigns the pbuffer a front buffer instead of a back buffer.
88 *
89 * Pbuffers in the X11 platform mostly work today, so let's just copy its
90 * behavior instead of trying to fix (and hence potentially breaking) the
91 * world.
92 */
93
94 if (buffer_mask & __DRI_IMAGE_BUFFER_FRONT) {
95
96 if (!dri2_surf->front) {
97 dri2_surf->front = surfaceless_alloc_image(dri2_dpy, dri2_surf);
98 if (!dri2_surf->front)
99 return 0;
100 }
101
102 buffers->image_mask |= __DRI_IMAGE_BUFFER_FRONT;
103 buffers->front = dri2_surf->front;
104 }
105
106 return 1;
107 }
108
109 static _EGLSurface *
110 dri2_surfaceless_create_surface(_EGLDisplay *disp, EGLint type,
111 _EGLConfig *conf, const EGLint *attrib_list)
112 {
113 struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);
114 struct dri2_egl_config *dri2_conf = dri2_egl_config(conf);
115 struct dri2_egl_surface *dri2_surf;
116 const struct dri_config *config;
117
118 /* Make sure to calloc so all pointers
119 * are originally NULL.
120 */
121 dri2_surf = calloc(1, sizeof *dri2_surf);
122
123 if (!dri2_surf) {
124 _eglError(EGL_BAD_ALLOC, "eglCreatePbufferSurface");
125 return NULL;
126 }
127
128 if (!dri2_init_surface(&dri2_surf->base, disp, type, conf, attrib_list,
129 false, NULL))
130 goto cleanup_surface;
131
132 config = dri2_get_dri_config(dri2_conf, type, dri2_surf->base.GLColorspace);
133
134 if (!config) {
135 _eglError(EGL_BAD_MATCH,
136 "Unsupported surfacetype/colorspace configuration");
137 goto cleanup_surface;
138 }
139
140 dri2_surf->visual = dri2_image_format_for_pbuffer_config(dri2_dpy, config);
141 if (dri2_surf->visual == PIPE_FORMAT_NONE)
142 goto cleanup_surface;
143
144 if (!dri2_create_drawable(dri2_dpy, config, dri2_surf, dri2_surf))
145 goto cleanup_surface;
146
147 return &dri2_surf->base;
148
149 cleanup_surface:
150 free(dri2_surf);
151 return NULL;
152 }
153
154 static EGLBoolean
155 surfaceless_destroy_surface(_EGLDisplay *disp, _EGLSurface *surf)
156 {
157 struct dri2_egl_surface *dri2_surf = dri2_egl_surface(surf);
158
159 surfaceless_free_images(dri2_surf);
160
161 driDestroyDrawable(dri2_surf->dri_drawable);
162
163 dri2_fini_surface(surf);
164 free(dri2_surf);
165 return EGL_TRUE;
166 }
167
168 static _EGLSurface *
169 dri2_surfaceless_create_pbuffer_surface(_EGLDisplay *disp, _EGLConfig *conf,
170 const EGLint *attrib_list)
171 {
172 return dri2_surfaceless_create_surface(disp, EGL_PBUFFER_BIT, conf,
173 attrib_list);
174 }
175
176 static const struct dri2_egl_display_vtbl dri2_surfaceless_display_vtbl = {
177 .create_pbuffer_surface = dri2_surfaceless_create_pbuffer_surface,
178 .destroy_surface = surfaceless_destroy_surface,
179 .create_image = dri2_create_image_khr,
180 .get_dri_drawable = dri2_surface_get_dri_drawable,
181 };
182
183 static void
184 surfaceless_flush_front_buffer(struct dri_drawable *driDrawable, void *loaderPrivate)
185 {
186 }
187
188 static unsigned
189 surfaceless_get_capability(void *loaderPrivate, enum dri_loader_cap cap)
190 {
191 /* Note: loaderPrivate is _EGLDisplay* */
192 switch (cap) {
193 case DRI_LOADER_CAP_FP16:
194 return 1;
195 case DRI_LOADER_CAP_RGBA_ORDERING:
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, &image_lookup_extension.base, NULL,
211 };
212
213 static const __DRIextension *swrast_loader_extensions[] = {
214 &swrast_pbuffer_loader_extension.base, &image_loader_extension.base,
215 &image_lookup_extension.base, NULL,
216 };
217
218 static const __DRIextension *kopper_loader_extensions[] = {
219 &kopper_pbuffer_loader_extension.base, &image_lookup_extension.base,
220 &image_lookup_extension.base, NULL,
221 };
222
223 static bool
224 surfaceless_probe_device(_EGLDisplay *disp, bool swrast, bool zink)
225 {
226 const unsigned node_type = swrast ? DRM_NODE_PRIMARY : DRM_NODE_RENDER;
227 struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);
228 _EGLDevice *dev_list = _eglGlobal.DeviceList;
229 drmDevicePtr device;
230
231 while (dev_list) {
232 if (!_eglDeviceSupports(dev_list, _EGL_DEVICE_DRM))
233 goto next;
234
235 if (_eglHasAttrib(disp, EGL_DEVICE_EXT) && dev_list != disp->Device) {
236 goto next;
237 }
238
239 device = _eglDeviceDrm(dev_list);
240 assert(device);
241
242 if (!(device->available_nodes & (1 << node_type)))
243 goto next;
244
245 dri2_dpy->fd_render_gpu = loader_open_device(device->nodes[node_type]);
246 if (dri2_dpy->fd_render_gpu < 0)
247 goto next;
248
249 #ifdef HAVE_WAYLAND_PLATFORM
250 loader_get_user_preferred_fd(&dri2_dpy->fd_render_gpu,
251 &dri2_dpy->fd_display_gpu);
252
253 if (dri2_dpy->fd_render_gpu != dri2_dpy->fd_display_gpu) {
254 free(dri2_dpy->device_name);
255 dri2_dpy->device_name =
256 loader_get_device_name_for_fd(dri2_dpy->fd_render_gpu);
257 if (!dri2_dpy->device_name) {
258 _eglError(EGL_BAD_ALLOC, "surfaceless-egl: failed to get device name "
259 "for requested GPU");
260 goto retry;
261 }
262 }
263
264 /* we have to do the check now, because loader_get_user_preferred_fd
265 * will return a render-node when the requested gpu is different
266 * to the server, but also if the client asks for the same gpu than
267 * the server by requesting its pci-id */
268 dri2_dpy->is_render_node =
269 drmGetNodeTypeFromFd(dri2_dpy->fd_render_gpu) == DRM_NODE_RENDER;
270 #endif
271 char *driver_name = loader_get_driver_for_fd(dri2_dpy->fd_render_gpu);
272
273 disp->Device = dev_list;
274 if (swrast) {
275 /* Use kms swrast only with vgem / virtio_gpu.
276 * virtio-gpu fallbacks to software rendering when 3D features
277 * are unavailable since 6c5ab, and kms_swrast is more
278 * feature complete than swrast.
279 */
280 if (driver_name && (strcmp(driver_name, "vgem") == 0 ||
281 strcmp(driver_name, "virtio_gpu") == 0))
282 dri2_dpy->driver_name = strdup("kms_swrast");
283 free(driver_name);
284 } else {
285 /* Use the given hardware driver */
286 dri2_dpy->driver_name = driver_name;
287 }
288
289 if (dri2_dpy->driver_name) {
290 dri2_detect_swrast_kopper(disp);
291 if (dri2_dpy->kopper)
292 dri2_dpy->loader_extensions = kopper_loader_extensions;
293 else if (swrast)
294 dri2_dpy->loader_extensions = swrast_loader_extensions;
295 else
296 dri2_dpy->loader_extensions = image_loader_extensions;
297
298 if (!dri2_create_screen(disp)) {
299 _eglLog(_EGL_WARNING, "DRI2: failed to create screen");
300 goto retry;
301 }
302
303 if (!dri2_dpy->dri_screen_render_gpu->base.screen->caps.graphics) {
304
305 _eglLog(_EGL_DEBUG, "DRI2: Driver %s doesn't support graphics, skipping.", dri2_dpy->driver_name);
306
307 if (dri2_dpy->dri_screen_display_gpu != dri2_dpy->dri_screen_render_gpu) {
308 driDestroyScreen(dri2_dpy->dri_screen_display_gpu);
309 dri2_dpy->dri_screen_display_gpu = NULL;
310 }
311
312 driDestroyScreen(dri2_dpy->dri_screen_render_gpu);
313 dri2_dpy->dri_screen_render_gpu = NULL;
314
315 dri2_dpy->own_dri_screen = false;
316
317 goto retry;
318 }
319
320 break;
321 }
322
323 retry:
324 free(dri2_dpy->driver_name);
325 dri2_dpy->driver_name = NULL;
326 if (dri2_dpy->fd_display_gpu != dri2_dpy->fd_render_gpu)
327 close(dri2_dpy->fd_display_gpu);
328 dri2_dpy->fd_display_gpu = -1;
329 close(dri2_dpy->fd_render_gpu);
330 dri2_dpy->fd_render_gpu = -1;
331
332 next:
333 dev_list = _eglDeviceNext(dev_list);
334 }
335
336 if (!dev_list)
337 return false;
338
339 return true;
340 }
341
342 static bool
343 surfaceless_probe_device_sw(_EGLDisplay *disp)
344 {
345 struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);
346 struct _egl_device *device = _eglFindDevice(dri2_dpy->fd_render_gpu, true);
347
348 dri2_dpy->fd_render_gpu = -1;
349
350 if (_eglHasAttrib(disp, EGL_DEVICE_EXT) && disp->Device != device) {
351 return false;
352 }
353
354 disp->Device = device;
355 assert(disp->Device);
356
357 dri2_dpy->driver_name = strdup(disp->Options.Zink ? "zink" : "swrast");
358 if (!dri2_dpy->driver_name)
359 return false;
360
361 dri2_detect_swrast_kopper(disp);
362
363 if (dri2_dpy->kopper)
364 dri2_dpy->loader_extensions = kopper_loader_extensions;
365 else
366 dri2_dpy->loader_extensions = swrast_loader_extensions;
367
368 dri2_dpy->fd_display_gpu = dri2_dpy->fd_render_gpu;
369
370 if (!dri2_create_screen(disp)) {
371 _eglLog(_EGL_WARNING, "DRI2: failed to create screen");
372 free(dri2_dpy->driver_name);
373 dri2_dpy->driver_name = NULL;
374 return false;
375 }
376
377 return true;
378 }
379
380 EGLBoolean
381 dri2_initialize_surfaceless(_EGLDisplay *disp)
382 {
383 const char *err;
384 bool driver_loaded = false;
385 struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);
386
387 /* When ForceSoftware is false, we try the HW driver. When ForceSoftware
388 * is true, we try kms_swrast and swrast in order.
389 */
390 driver_loaded = surfaceless_probe_device(disp, disp->Options.ForceSoftware,
391 disp->Options.Zink);
392 if (!driver_loaded && disp->Options.ForceSoftware) {
393 _eglLog(_EGL_DEBUG, "Falling back to surfaceless swrast without DRM.");
394 driver_loaded = surfaceless_probe_device_sw(disp);
395 }
396
397 if (!driver_loaded) {
398 err = "DRI2: failed to load driver";
399 goto cleanup;
400 }
401
402 dri2_setup_screen(disp);
403 #ifdef HAVE_WAYLAND_PLATFORM
404 dri2_dpy->device_name =
405 loader_get_device_name_for_fd(dri2_dpy->fd_render_gpu);
406 #endif
407 dri2_set_WL_bind_wayland_display(disp);
408
409 dri2_add_pbuffer_configs_for_visuals(disp);
410
411 /* Fill vtbl last to prevent accidentally calling virtual function during
412 * initialization.
413 */
414 dri2_dpy->vtbl = &dri2_surfaceless_display_vtbl;
415
416 return EGL_TRUE;
417
418 cleanup:
419 return _eglError(EGL_NOT_INITIALIZED, err);
420 }