egl: drop now empty egl_dri2_fallbacks.h
[mesa.git] / src / egl / drivers / dri2 / platform_device.c
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 <stdlib.h>
34 #include <stdio.h>
35 #include <string.h>
36 #include <dlfcn.h>
37 #include <sys/types.h>
38 #include <sys/stat.h>
39 #include <fcntl.h>
40 #include <unistd.h>
41
42 #include "egl_dri2.h"
43 #include "loader.h"
44
45 static __DRIimage*
46 device_alloc_image(struct dri2_egl_display *dri2_dpy,
47 struct dri2_egl_surface *dri2_surf)
48 {
49 return dri2_dpy->image->createImage(
50 dri2_dpy->dri_screen,
51 dri2_surf->base.Width,
52 dri2_surf->base.Height,
53 dri2_surf->visual,
54 0,
55 NULL);
56 }
57
58 static void
59 device_free_images(struct dri2_egl_surface *dri2_surf)
60 {
61 struct dri2_egl_display *dri2_dpy =
62 dri2_egl_display(dri2_surf->base.Resource.Display);
63
64 if (dri2_surf->front) {
65 dri2_dpy->image->destroyImage(dri2_surf->front);
66 dri2_surf->front = NULL;
67 }
68
69 free(dri2_surf->swrast_device_buffer);
70 dri2_surf->swrast_device_buffer = NULL;
71 }
72
73 static int
74 device_image_get_buffers(__DRIdrawable *driDrawable,
75 unsigned int format,
76 uint32_t *stamp,
77 void *loaderPrivate,
78 uint32_t buffer_mask,
79 struct __DRIimageList *buffers)
80 {
81 struct dri2_egl_surface *dri2_surf = loaderPrivate;
82 struct dri2_egl_display *dri2_dpy =
83 dri2_egl_display(dri2_surf->base.Resource.Display);
84
85 buffers->image_mask = 0;
86 buffers->front = NULL;
87 buffers->back = NULL;
88
89 /* The EGL 1.5 spec states that pbuffers are single-buffered. Specifically,
90 * the spec states that they have a back buffer but no front buffer, in
91 * contrast to pixmaps, which have a front buffer but no back buffer.
92 *
93 * Single-buffered surfaces with no front buffer confuse Mesa; so we deviate
94 * from the spec, following the precedent of Mesa's EGL X11 platform. The
95 * X11 platform correctly assigns pbuffers to single-buffered configs, but
96 * assigns the pbuffer a front buffer instead of a back buffer.
97 *
98 * Pbuffers in the X11 platform mostly work today, so let's just copy its
99 * behavior instead of trying to fix (and hence potentially breaking) the
100 * world.
101 */
102
103 if (buffer_mask & __DRI_IMAGE_BUFFER_FRONT) {
104
105 if (!dri2_surf->front)
106 dri2_surf->front =
107 device_alloc_image(dri2_dpy, dri2_surf);
108
109 buffers->image_mask |= __DRI_IMAGE_BUFFER_FRONT;
110 buffers->front = dri2_surf->front;
111 }
112
113 return 1;
114 }
115
116 static _EGLSurface *
117 dri2_device_create_surface(_EGLDriver *drv, _EGLDisplay *disp, EGLint type,
118 _EGLConfig *conf, const EGLint *attrib_list)
119 {
120 struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);
121 struct dri2_egl_config *dri2_conf = dri2_egl_config(conf);
122 struct dri2_egl_surface *dri2_surf;
123 const __DRIconfig *config;
124
125 /* Make sure to calloc so all pointers
126 * are originally NULL.
127 */
128 dri2_surf = calloc(1, sizeof *dri2_surf);
129
130 if (!dri2_surf) {
131 _eglError(EGL_BAD_ALLOC, "eglCreatePbufferSurface");
132 return NULL;
133 }
134
135 if (!dri2_init_surface(&dri2_surf->base, disp, type, conf, attrib_list,
136 false, NULL))
137 goto cleanup_surface;
138
139 config = dri2_get_dri_config(dri2_conf, type,
140 dri2_surf->base.GLColorspace);
141
142 if (!config) {
143 _eglError(EGL_BAD_MATCH, "Unsupported surfacetype/colorspace configuration");
144 goto cleanup_surface;
145 }
146
147 dri2_surf->visual = dri2_image_format_for_pbuffer_config(dri2_dpy, config);
148 if (dri2_surf->visual == __DRI_IMAGE_FORMAT_NONE)
149 goto cleanup_surface;
150
151 if (!dri2_create_drawable(dri2_dpy, config, dri2_surf, dri2_surf))
152 goto cleanup_surface;
153
154 return &dri2_surf->base;
155
156 cleanup_surface:
157 free(dri2_surf);
158 return NULL;
159 }
160
161 static EGLBoolean
162 device_destroy_surface(_EGLDriver *drv, _EGLDisplay *disp, _EGLSurface *surf)
163 {
164 struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);
165 struct dri2_egl_surface *dri2_surf = dri2_egl_surface(surf);
166
167 device_free_images(dri2_surf);
168
169 dri2_dpy->core->destroyDrawable(dri2_surf->dri_drawable);
170
171 dri2_fini_surface(surf);
172 free(dri2_surf);
173 return EGL_TRUE;
174 }
175
176 static _EGLSurface *
177 dri2_device_create_pbuffer_surface(_EGLDriver *drv, _EGLDisplay *disp,
178 _EGLConfig *conf, const EGLint *attrib_list)
179 {
180 return dri2_device_create_surface(drv, disp, EGL_PBUFFER_BIT, conf,
181 attrib_list);
182 }
183
184 static const struct dri2_egl_display_vtbl dri2_device_display_vtbl = {
185 .create_pbuffer_surface = dri2_device_create_pbuffer_surface,
186 .destroy_surface = device_destroy_surface,
187 .create_image = dri2_create_image_khr,
188 .get_dri_drawable = dri2_surface_get_dri_drawable,
189 };
190
191 static void
192 device_flush_front_buffer(__DRIdrawable *driDrawable, void *loaderPrivate)
193 {
194 }
195
196 static const __DRIimageLoaderExtension image_loader_extension = {
197 .base = { __DRI_IMAGE_LOADER, 1 },
198 .getBuffers = device_image_get_buffers,
199 .flushFrontBuffer = device_flush_front_buffer,
200 };
201
202 static const __DRIextension *image_loader_extensions[] = {
203 &image_loader_extension.base,
204 &image_lookup_extension.base,
205 &use_invalidate.base,
206 NULL,
207 };
208
209 static const __DRIextension *swrast_loader_extensions[] = {
210 &swrast_pbuffer_loader_extension.base,
211 &image_lookup_extension.base,
212 &use_invalidate.base,
213 NULL,
214 };
215
216 static int
217 device_get_fd(_EGLDisplay *disp, _EGLDevice *dev)
218 {
219 #ifdef HAVE_LIBDRM
220 int fd = disp->Options.fd;
221 /* The fcntl() code in _eglGetDeviceDisplay() ensures that valid fd >= 3,
222 * and invalid one is 0.
223 */
224 if (fd) {
225 /* According to the spec - if the FD does not match the EGLDevice
226 * behaviour is undefined.
227 *
228 * Add a trivial sanity check since it doesn't cost us anything.
229 */
230 if (dev != _eglAddDevice(fd, false))
231 return -1;
232
233 /* No EGL_EXT_output* extensions are supported, hence no master perms
234 * are needed. Get the render one - otherwise drivers might error out.
235 */
236 char *node = drmGetRenderDeviceNameFromFd(fd);
237
238 /* Don't close the internal fd, get render node one based on it. */
239 fd = loader_open_device(node);
240 free(node);
241 return fd;
242 }
243 const char *node = _eglGetDRMDeviceRenderNode(dev);
244 return loader_open_device(node);
245 #else
246 _eglLog(_EGL_FATAL, "Driver bug: Built without libdrm, yet using a HW device");
247 return -1;
248 #endif
249 }
250
251 static bool
252 device_probe_device(_EGLDisplay *disp)
253 {
254 struct dri2_egl_display *dri2_dpy = disp->DriverData;
255
256 if (disp->Options.ForceSoftware)
257 _eglLog(_EGL_WARNING, "Not allowed to force software rendering when "
258 "API explicitly selects a hardware device.");
259 dri2_dpy->fd = device_get_fd(disp, disp->Device);
260 if (dri2_dpy->fd < 0)
261 return false;
262
263 dri2_dpy->driver_name = loader_get_driver_for_fd(dri2_dpy->fd);
264 if (!dri2_dpy->driver_name)
265 goto err_name;
266
267 if (!dri2_load_driver_dri3(disp))
268 goto err_load;
269
270 dri2_dpy->loader_extensions = image_loader_extensions;
271 return true;
272
273 err_load:
274 free(dri2_dpy->driver_name);
275 dri2_dpy->driver_name = NULL;
276
277 err_name:
278 close(dri2_dpy->fd);
279 dri2_dpy->fd = -1;
280 return false;
281
282 }
283
284 static bool
285 device_probe_device_sw(_EGLDisplay *disp)
286 {
287 struct dri2_egl_display *dri2_dpy = disp->DriverData;
288
289 dri2_dpy->fd = -1;
290 dri2_dpy->driver_name = strdup("swrast");
291 if (!dri2_dpy->driver_name)
292 return false;
293
294 /* HACK: should be driver_swrast_null */
295 if (!dri2_load_driver_swrast(disp)) {
296 free(dri2_dpy->driver_name);
297 dri2_dpy->driver_name = NULL;
298 return false;
299 }
300
301 dri2_dpy->loader_extensions = swrast_loader_extensions;
302 return true;
303 }
304
305 EGLBoolean
306 dri2_initialize_device(_EGLDriver *drv, _EGLDisplay *disp)
307 {
308 _EGLDevice *dev;
309 struct dri2_egl_display *dri2_dpy;
310 const char* err;
311
312 dri2_dpy = calloc(1, sizeof *dri2_dpy);
313 if (!dri2_dpy)
314 return _eglError(EGL_BAD_ALLOC, "eglInitialize");
315
316 /* Extension requires a PlatformDisplay - the EGLDevice. */
317 dev = disp->PlatformDisplay;
318
319 dri2_dpy->fd = -1;
320 disp->Device = dev;
321 disp->DriverData = (void *) dri2_dpy;
322 err = "DRI2: failed to load driver";
323 if (_eglDeviceSupports(dev, _EGL_DEVICE_DRM)) {
324 if (!device_probe_device(disp))
325 goto cleanup;
326 } else if (_eglDeviceSupports(dev, _EGL_DEVICE_SOFTWARE)) {
327 if (!device_probe_device_sw(disp))
328 goto cleanup;
329 } else {
330 _eglLog(_EGL_FATAL, "Driver bug: exposed device is neither DRM nor SOFTWARE one");
331 return EGL_FALSE;
332 }
333
334 if (!dri2_create_screen(disp)) {
335 err = "DRI2: failed to create screen";
336 goto cleanup;
337 }
338
339 if (!dri2_setup_extensions(disp)) {
340 err = "DRI2: failed to find required DRI extensions";
341 goto cleanup;
342 }
343
344 dri2_setup_screen(disp);
345
346 if (!dri2_add_pbuffer_configs_for_visuals(drv, disp)) {
347 err = "DRI2: failed to add configs";
348 goto cleanup;
349 }
350
351 /* Fill vtbl last to prevent accidentally calling virtual function during
352 * initialization.
353 */
354 dri2_dpy->vtbl = &dri2_device_display_vtbl;
355
356 return EGL_TRUE;
357
358 cleanup:
359 dri2_display_destroy(disp);
360 return _eglError(EGL_NOT_INITIALIZED, err);
361 }