blob: f0d5e047e076c9d800e3f34c6cf2b63eb75ae95d [file] [log] [blame]
Haixia Shi6b8accb2015-06-12 10:10:58 -07001/*
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"
Haixia Shi6b8accb2015-06-12 10:10:58 -070037#include "loader.h"
38
Gurchetan Singh63c5d5c2016-05-16 16:34:07 -070039static __DRIimage*
40surfaceless_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
52static void
53surfaceless_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 }
Mathias Fröhlichc7617d82019-12-13 17:09:56 +010062
63 free(dri2_surf->swrast_device_buffer);
64 dri2_surf->swrast_device_buffer = NULL;
Gurchetan Singh63c5d5c2016-05-16 16:34:07 -070065}
66
67static int
68surfaceless_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
110static _EGLSurface *
Eric Engestrom9c6fa942020-07-31 01:38:41 +0200111dri2_surfaceless_create_surface(const _EGLDriver *drv, _EGLDisplay *disp, EGLint type,
Gurchetan Singh63c5d5c2016-05-16 16:34:07 -0700112 _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
Paulo Zanoni04ecda32019-05-01 15:42:26 -0700129 if (!dri2_init_surface(&dri2_surf->base, disp, type, conf, attrib_list,
130 false, NULL))
Gurchetan Singh63c5d5c2016-05-16 16:34:07 -0700131 goto cleanup_surface;
132
133 config = dri2_get_dri_config(dri2_conf, type,
134 dri2_surf->base.GLColorspace);
135
Tapani Pälliaffe63b2018-05-04 08:28:50 +0300136 if (!config) {
137 _eglError(EGL_BAD_MATCH, "Unsupported surfacetype/colorspace configuration");
Gurchetan Singh63c5d5c2016-05-16 16:34:07 -0700138 goto cleanup_surface;
Tapani Pälliaffe63b2018-05-04 08:28:50 +0300139 }
Gurchetan Singh63c5d5c2016-05-16 16:34:07 -0700140
Mathias Fröhlichd32c4582020-02-09 19:01:53 +0100141 dri2_surf->visual = dri2_image_format_for_pbuffer_config(dri2_dpy, config);
142 if (dri2_surf->visual == __DRI_IMAGE_FORMAT_NONE)
Gurchetan Singh63c5d5c2016-05-16 16:34:07 -0700143 goto cleanup_surface;
Gurchetan Singh63c5d5c2016-05-16 16:34:07 -0700144
Mathias Fröhlichd32c4582020-02-09 19:01:53 +0100145 if (!dri2_create_drawable(dri2_dpy, config, dri2_surf, dri2_surf))
146 goto cleanup_surface;
Gurchetan Singh63c5d5c2016-05-16 16:34:07 -0700147
148 return &dri2_surf->base;
149
150 cleanup_surface:
151 free(dri2_surf);
152 return NULL;
153}
154
155static EGLBoolean
Eric Engestrom9c6fa942020-07-31 01:38:41 +0200156surfaceless_destroy_surface(const _EGLDriver *drv, _EGLDisplay *disp, _EGLSurface *surf)
Gurchetan Singh63c5d5c2016-05-16 16:34:07 -0700157{
158 struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);
159 struct dri2_egl_surface *dri2_surf = dri2_egl_surface(surf);
160
Gurchetan Singh63c5d5c2016-05-16 16:34:07 -0700161 surfaceless_free_images(dri2_surf);
162
Boyan Ding868ae3e2015-11-25 13:27:02 +0800163 dri2_dpy->core->destroyDrawable(dri2_surf->dri_drawable);
Gurchetan Singh63c5d5c2016-05-16 16:34:07 -0700164
Zhongmin Wue013ce82017-09-15 18:32:42 +0100165 dri2_fini_surface(surf);
Gurchetan Singh63c5d5c2016-05-16 16:34:07 -0700166 free(dri2_surf);
167 return EGL_TRUE;
168}
169
170static _EGLSurface *
Eric Engestrom9c6fa942020-07-31 01:38:41 +0200171dri2_surfaceless_create_pbuffer_surface(const _EGLDriver *drv, _EGLDisplay *disp,
Gurchetan Singh63c5d5c2016-05-16 16:34:07 -0700172 _EGLConfig *conf, const EGLint *attrib_list)
173{
174 return dri2_surfaceless_create_surface(drv, disp, EGL_PBUFFER_BIT, conf,
175 attrib_list);
176}
177
Emil Velikov79d1fb92017-05-15 16:14:17 +0100178static const struct dri2_egl_display_vtbl dri2_surfaceless_display_vtbl = {
Gurchetan Singh63c5d5c2016-05-16 16:34:07 -0700179 .create_pbuffer_surface = dri2_surfaceless_create_pbuffer_surface,
180 .destroy_surface = surfaceless_destroy_surface,
Haixia Shi6b8accb2015-06-12 10:10:58 -0700181 .create_image = dri2_create_image_khr,
Gurchetan Singh63c5d5c2016-05-16 16:34:07 -0700182 .get_dri_drawable = dri2_surface_get_dri_drawable,
Haixia Shi6b8accb2015-06-12 10:10:58 -0700183};
184
185static void
186surfaceless_flush_front_buffer(__DRIdrawable *driDrawable, void *loaderPrivate)
187{
188}
189
Adam Jackson55a1b582019-09-10 11:44:24 -0400190static unsigned
191surfaceless_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
Gurchetan Singh63c5d5c2016-05-16 16:34:07 -0700202static const __DRIimageLoaderExtension image_loader_extension = {
Adam Jackson55a1b582019-09-10 11:44:24 -0400203 .base = { __DRI_IMAGE_LOADER, 2 },
Gurchetan Singh63c5d5c2016-05-16 16:34:07 -0700204 .getBuffers = surfaceless_image_get_buffers,
205 .flushFrontBuffer = surfaceless_flush_front_buffer,
Adam Jackson55a1b582019-09-10 11:44:24 -0400206 .getCapability = surfaceless_get_capability,
Gurchetan Singh63c5d5c2016-05-16 16:34:07 -0700207};
208
Emil Velikovf8719462016-08-24 23:32:27 +0100209static const __DRIextension *image_loader_extensions[] = {
210 &image_loader_extension.base,
211 &image_lookup_extension.base,
212 &use_invalidate.base,
213 NULL,
214};
215
David Rileyb169b842018-07-17 17:12:04 -0700216static const __DRIextension *swrast_loader_extensions[] = {
Eric Anholt6a8d39dc2019-07-23 13:18:21 -0700217 &swrast_pbuffer_loader_extension.base,
David Rileyb169b842018-07-17 17:12:04 -0700218 &image_loader_extension.base,
219 &image_lookup_extension.base,
220 &use_invalidate.base,
221 NULL,
222};
223
Gurchetan Singh540c8042017-10-02 13:43:57 -0700224static bool
Eric Engestrom54fa5ec2019-02-02 11:38:45 +0000225surfaceless_probe_device(_EGLDisplay *disp, bool swrast)
Gurchetan Singh540c8042017-10-02 13:43:57 -0700226{
Emil Velikov218c7b52019-02-19 14:08:07 +0000227#define MAX_DRM_DEVICES 64
Emil Velikov7ad1a052019-02-19 14:08:08 +0000228 const unsigned node_type = swrast ? DRM_NODE_PRIMARY : DRM_NODE_RENDER;
Eric Engestrom54fa5ec2019-02-02 11:38:45 +0000229 struct dri2_egl_display *dri2_dpy = disp->DriverData;
Emil Velikov218c7b52019-02-19 14:08:07 +0000230 drmDevicePtr device, devices[MAX_DRM_DEVICES] = { NULL };
231 int i, num_devices;
Gurchetan Singh540c8042017-10-02 13:43:57 -0700232
Emil Velikov218c7b52019-02-19 14:08:07 +0000233 num_devices = drmGetDevices2(0, devices, ARRAY_SIZE(devices));
234 if (num_devices < 0)
235 return false;
236
237 for (i = 0; i < num_devices; ++i) {
238 device = devices[i];
239
Emil Velikov7ad1a052019-02-19 14:08:08 +0000240 if (!(device->available_nodes & (1 << node_type)))
Gurchetan Singh540c8042017-10-02 13:43:57 -0700241 continue;
242
Emil Velikov7ad1a052019-02-19 14:08:08 +0000243 dri2_dpy->fd = loader_open_device(device->nodes[node_type]);
Emil Velikov218c7b52019-02-19 14:08:07 +0000244 if (dri2_dpy->fd < 0)
Gurchetan Singh540c8042017-10-02 13:43:57 -0700245 continue;
246
Emil Velikov218c7b52019-02-19 14:08:07 +0000247 disp->Device = _eglAddDevice(dri2_dpy->fd, swrast);
248 if (!disp->Device) {
249 close(dri2_dpy->fd);
250 dri2_dpy->fd = -1;
251 continue;
252 }
253
Emil Velikov7ad1a052019-02-19 14:08:08 +0000254 char *driver_name = loader_get_driver_for_fd(dri2_dpy->fd);
255 if (swrast) {
Gurchetan Singh610758d2019-03-01 18:58:16 -0800256 /* Use kms swrast only with vgem / virtio_gpu.
257 * virtio-gpu fallbacks to software rendering when 3D features
258 * are unavailable since 6c5ab, and kms_swrast is more
259 * feature complete than swrast.
260 */
Eric Anholt86ae3c22019-07-29 16:25:56 -0700261 if (driver_name &&
262 (strcmp(driver_name, "vgem") == 0 ||
263 strcmp(driver_name, "virtio_gpu") == 0))
Emil Velikov7ad1a052019-02-19 14:08:08 +0000264 dri2_dpy->driver_name = strdup("kms_swrast");
265 free(driver_name);
266 } else {
267 /* Use the given hardware driver */
268 dri2_dpy->driver_name = driver_name;
269 }
Gurchetan Singh540c8042017-10-02 13:43:57 -0700270
Emil Velikov218c7b52019-02-19 14:08:07 +0000271 if (dri2_dpy->driver_name && dri2_load_driver_dri3(disp))
272 break;
Gurchetan Singh540c8042017-10-02 13:43:57 -0700273
Gurchetan Singh540c8042017-10-02 13:43:57 -0700274 free(dri2_dpy->driver_name);
275 dri2_dpy->driver_name = NULL;
Emil Velikov218c7b52019-02-19 14:08:07 +0000276 close(dri2_dpy->fd);
277 dri2_dpy->fd = -1;
Gurchetan Singh540c8042017-10-02 13:43:57 -0700278 }
Emil Velikov218c7b52019-02-19 14:08:07 +0000279 drmFreeDevices(devices, num_devices);
Gurchetan Singh540c8042017-10-02 13:43:57 -0700280
Emil Velikov218c7b52019-02-19 14:08:07 +0000281 if (i == num_devices)
282 return false;
283
284 if (swrast)
285 dri2_dpy->loader_extensions = swrast_loader_extensions;
286 else
287 dri2_dpy->loader_extensions = image_loader_extensions;
288
289 return true;
Emil Velikov893421f2019-02-19 14:08:06 +0000290}
David Rileyf94681b2018-07-17 17:12:05 -0700291
Emil Velikov893421f2019-02-19 14:08:06 +0000292static bool
293surfaceless_probe_device_sw(_EGLDisplay *disp)
294{
295 struct dri2_egl_display *dri2_dpy = disp->DriverData;
David Rileyf94681b2018-07-17 17:12:05 -0700296
Emil Velikov893421f2019-02-19 14:08:06 +0000297 dri2_dpy->fd = -1;
298 disp->Device = _eglAddDevice(dri2_dpy->fd, true);
299 assert(disp->Device);
300
301 dri2_dpy->driver_name = strdup("swrast");
302 if (!dri2_dpy->driver_name)
303 return false;
304
305 if (!dri2_load_driver_swrast(disp)) {
David Rileyf94681b2018-07-17 17:12:05 -0700306 free(dri2_dpy->driver_name);
307 dri2_dpy->driver_name = NULL;
Emil Velikov893421f2019-02-19 14:08:06 +0000308 return false;
David Rileyf94681b2018-07-17 17:12:05 -0700309 }
310
Emil Velikov893421f2019-02-19 14:08:06 +0000311 dri2_dpy->loader_extensions = swrast_loader_extensions;
312 return true;
Gurchetan Singh540c8042017-10-02 13:43:57 -0700313}
314
Haixia Shi6b8accb2015-06-12 10:10:58 -0700315EGLBoolean
Eric Engestrom9c6fa942020-07-31 01:38:41 +0200316dri2_initialize_surfaceless(const _EGLDriver *drv, _EGLDisplay *disp)
Haixia Shi6b8accb2015-06-12 10:10:58 -0700317{
318 struct dri2_egl_display *dri2_dpy;
319 const char* err;
Gurchetan Singh9d9a46d2017-10-02 13:48:24 -0700320 bool driver_loaded = false;
Haixia Shi6b8accb2015-06-12 10:10:58 -0700321
Haixia Shi6b8accb2015-06-12 10:10:58 -0700322 dri2_dpy = calloc(1, sizeof *dri2_dpy);
323 if (!dri2_dpy)
324 return _eglError(EGL_BAD_ALLOC, "eglInitialize");
325
Emil Velikovc8d366b2017-05-11 17:22:35 +0100326 dri2_dpy->fd = -1;
Haixia Shi6b8accb2015-06-12 10:10:58 -0700327 disp->DriverData = (void *) dri2_dpy;
Gurchetan Singh9d9a46d2017-10-02 13:48:24 -0700328
Eric Engestrom81cea662017-12-20 15:53:09 +0000329 if (!disp->Options.ForceSoftware) {
Gurchetan Singh9d9a46d2017-10-02 13:48:24 -0700330 driver_loaded = surfaceless_probe_device(disp, false);
331 if (!driver_loaded)
332 _eglLog(_EGL_WARNING,
333 "No hardware driver found, falling back to software rendering");
334 }
335
Emil Velikov893421f2019-02-19 14:08:06 +0000336 if (!driver_loaded)
337 driver_loaded = surfaceless_probe_device(disp, true);
338
339 if (!driver_loaded) {
340 _eglLog(_EGL_DEBUG, "Falling back to surfaceless swrast without DRM.");
341 if (!surfaceless_probe_device_sw(disp)) {
342 err = "DRI2: failed to load driver";
343 goto cleanup;
344 }
Haixia Shi6b8accb2015-06-12 10:10:58 -0700345 }
346
Haixia Shi6b8accb2015-06-12 10:10:58 -0700347 if (!dri2_create_screen(disp)) {
348 err = "DRI2: failed to create screen";
Emil Velikovc8d366b2017-05-11 17:22:35 +0100349 goto cleanup;
Haixia Shi6b8accb2015-06-12 10:10:58 -0700350 }
351
Emil Velikov2c341f22017-05-11 16:20:04 +0100352 if (!dri2_setup_extensions(disp)) {
353 err = "DRI2: failed to find required DRI extensions";
354 goto cleanup;
355 }
356
357 dri2_setup_screen(disp);
Adam Jackson3746ee92019-10-03 10:10:59 -0400358#ifdef HAVE_WAYLAND_PLATFORM
359 dri2_dpy->device_name = loader_get_device_name_for_fd(dri2_dpy->fd);
360#endif
361 dri2_set_WL_bind_wayland_display(drv, disp);
Emil Velikov2c341f22017-05-11 16:20:04 +0100362
Mathias Fröhlichf280c002020-02-16 11:13:43 +0100363 if (!dri2_add_pbuffer_configs_for_visuals(drv, disp)) {
Gurchetan Singh63c5d5c2016-05-16 16:34:07 -0700364 err = "DRI2: failed to add configs";
Emil Velikovc8d366b2017-05-11 17:22:35 +0100365 goto cleanup;
Haixia Shi6b8accb2015-06-12 10:10:58 -0700366 }
367
Haixia Shi6b8accb2015-06-12 10:10:58 -0700368 /* Fill vtbl last to prevent accidentally calling virtual function during
369 * initialization.
370 */
371 dri2_dpy->vtbl = &dri2_surfaceless_display_vtbl;
372
373 return EGL_TRUE;
374
Emil Velikovc8d366b2017-05-11 17:22:35 +0100375cleanup:
376 dri2_display_destroy(disp);
Haixia Shi6b8accb2015-06-12 10:10:58 -0700377 return _eglError(EGL_NOT_INITIALIZED, err);
378}