2 * Mesa 3-D graphics library
4 * Copyright 2018 Collabora
6 * Based on platform_surfaceless, which has:
8 * Copyright (c) 2014 The Chromium OS Authors.
9 * Copyright © 2011 Intel Corporation
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:
18 * The above copyright notice and this permission notice shall be included
19 * in all copies or substantial portions of the Software.
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.
37 #include <sys/types.h>
44 #include "util/debug.h"
47 device_alloc_image(struct dri2_egl_display
*dri2_dpy
,
48 struct dri2_egl_surface
*dri2_surf
)
50 return dri2_dpy
->image
->createImage(
52 dri2_surf
->base
.Width
,
53 dri2_surf
->base
.Height
,
60 device_free_images(struct dri2_egl_surface
*dri2_surf
)
62 struct dri2_egl_display
*dri2_dpy
=
63 dri2_egl_display(dri2_surf
->base
.Resource
.Display
);
65 if (dri2_surf
->front
) {
66 dri2_dpy
->image
->destroyImage(dri2_surf
->front
);
67 dri2_surf
->front
= NULL
;
70 free(dri2_surf
->swrast_device_buffer
);
71 dri2_surf
->swrast_device_buffer
= NULL
;
75 device_image_get_buffers(__DRIdrawable
*driDrawable
,
80 struct __DRIimageList
*buffers
)
82 struct dri2_egl_surface
*dri2_surf
= loaderPrivate
;
83 struct dri2_egl_display
*dri2_dpy
=
84 dri2_egl_display(dri2_surf
->base
.Resource
.Display
);
86 buffers
->image_mask
= 0;
87 buffers
->front
= NULL
;
90 /* The EGL 1.5 spec states that pbuffers are single-buffered. Specifically,
91 * the spec states that they have a back buffer but no front buffer, in
92 * contrast to pixmaps, which have a front buffer but no back buffer.
94 * Single-buffered surfaces with no front buffer confuse Mesa; so we deviate
95 * from the spec, following the precedent of Mesa's EGL X11 platform. The
96 * X11 platform correctly assigns pbuffers to single-buffered configs, but
97 * assigns the pbuffer a front buffer instead of a back buffer.
99 * Pbuffers in the X11 platform mostly work today, so let's just copy its
100 * behavior instead of trying to fix (and hence potentially breaking) the
104 if (buffer_mask
& __DRI_IMAGE_BUFFER_FRONT
) {
106 if (!dri2_surf
->front
)
108 device_alloc_image(dri2_dpy
, dri2_surf
);
110 buffers
->image_mask
|= __DRI_IMAGE_BUFFER_FRONT
;
111 buffers
->front
= dri2_surf
->front
;
118 dri2_device_create_surface(const _EGLDriver
*drv
, _EGLDisplay
*disp
, EGLint type
,
119 _EGLConfig
*conf
, const EGLint
*attrib_list
)
121 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
122 struct dri2_egl_config
*dri2_conf
= dri2_egl_config(conf
);
123 struct dri2_egl_surface
*dri2_surf
;
124 const __DRIconfig
*config
;
126 /* Make sure to calloc so all pointers
127 * are originally NULL.
129 dri2_surf
= calloc(1, sizeof *dri2_surf
);
132 _eglError(EGL_BAD_ALLOC
, "eglCreatePbufferSurface");
136 if (!dri2_init_surface(&dri2_surf
->base
, disp
, type
, conf
, attrib_list
,
138 goto cleanup_surface
;
140 config
= dri2_get_dri_config(dri2_conf
, type
,
141 dri2_surf
->base
.GLColorspace
);
144 _eglError(EGL_BAD_MATCH
, "Unsupported surfacetype/colorspace configuration");
145 goto cleanup_surface
;
148 dri2_surf
->visual
= dri2_image_format_for_pbuffer_config(dri2_dpy
, config
);
149 if (dri2_surf
->visual
== __DRI_IMAGE_FORMAT_NONE
)
150 goto cleanup_surface
;
152 if (!dri2_create_drawable(dri2_dpy
, config
, dri2_surf
, dri2_surf
))
153 goto cleanup_surface
;
155 return &dri2_surf
->base
;
163 device_destroy_surface(const _EGLDriver
*drv
, _EGLDisplay
*disp
, _EGLSurface
*surf
)
165 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
166 struct dri2_egl_surface
*dri2_surf
= dri2_egl_surface(surf
);
168 device_free_images(dri2_surf
);
170 dri2_dpy
->core
->destroyDrawable(dri2_surf
->dri_drawable
);
172 dri2_fini_surface(surf
);
178 dri2_device_create_pbuffer_surface(const _EGLDriver
*drv
, _EGLDisplay
*disp
,
179 _EGLConfig
*conf
, const EGLint
*attrib_list
)
181 return dri2_device_create_surface(drv
, disp
, EGL_PBUFFER_BIT
, conf
,
185 static const struct dri2_egl_display_vtbl dri2_device_display_vtbl
= {
186 .create_pbuffer_surface
= dri2_device_create_pbuffer_surface
,
187 .destroy_surface
= device_destroy_surface
,
188 .create_image
= dri2_create_image_khr
,
189 .get_dri_drawable
= dri2_surface_get_dri_drawable
,
193 device_flush_front_buffer(__DRIdrawable
*driDrawable
, void *loaderPrivate
)
197 static const __DRIimageLoaderExtension image_loader_extension
= {
198 .base
= { __DRI_IMAGE_LOADER
, 1 },
199 .getBuffers
= device_image_get_buffers
,
200 .flushFrontBuffer
= device_flush_front_buffer
,
203 static const __DRIextension
*image_loader_extensions
[] = {
204 &image_loader_extension
.base
,
205 &image_lookup_extension
.base
,
206 &use_invalidate
.base
,
210 static const __DRIextension
*swrast_loader_extensions
[] = {
211 &swrast_pbuffer_loader_extension
.base
,
212 &image_lookup_extension
.base
,
213 &use_invalidate
.base
,
218 device_get_fd(_EGLDisplay
*disp
, _EGLDevice
*dev
)
221 int fd
= disp
->Options
.fd
;
222 /* The fcntl() code in _eglGetDeviceDisplay() ensures that valid fd >= 3,
223 * and invalid one is 0.
226 /* According to the spec - if the FD does not match the EGLDevice
227 * behaviour is undefined.
229 * Add a trivial sanity check since it doesn't cost us anything.
231 if (dev
!= _eglAddDevice(fd
, false))
234 /* No EGL_EXT_output* extensions are supported, hence no master perms
235 * are needed. Get the render one - otherwise drivers might error out.
237 char *node
= drmGetRenderDeviceNameFromFd(fd
);
239 /* Don't close the internal fd, get render node one based on it. */
240 fd
= loader_open_device(node
);
244 const char *node
= _eglGetDRMDeviceRenderNode(dev
);
245 return loader_open_device(node
);
247 _eglLog(_EGL_FATAL
, "Driver bug: Built without libdrm, yet using a HW device");
253 device_probe_device(_EGLDisplay
*disp
)
255 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
256 bool request_software
= env_var_as_boolean("LIBGL_ALWAYS_SOFTWARE", false);
258 if (request_software
)
259 _eglLog(_EGL_WARNING
, "Not allowed to force software rendering when "
260 "API explicitly selects a hardware device.");
261 dri2_dpy
->fd
= device_get_fd(disp
, disp
->Device
);
262 if (dri2_dpy
->fd
< 0)
265 dri2_dpy
->driver_name
= loader_get_driver_for_fd(dri2_dpy
->fd
);
266 if (!dri2_dpy
->driver_name
)
269 /* When doing software rendering, some times user still want to explicitly
270 * choose the render node device since cross node import doesn't work between
271 * vgem/virtio_gpu yet. It would be nice to have a new EXTENSION for this.
272 * For now, just fallback to kms_swrast. */
273 if (disp
->Options
.ForceSoftware
&& !request_software
&&
274 (strcmp(dri2_dpy
->driver_name
, "vgem") == 0 ||
275 strcmp(dri2_dpy
->driver_name
, "virtio_gpu") == 0)) {
276 free(dri2_dpy
->driver_name
);
277 _eglLog(_EGL_WARNING
, "NEEDS EXTENSION: falling back to kms_swrast");
278 dri2_dpy
->driver_name
= strdup("kms_swrast");
281 if (!dri2_load_driver_dri3(disp
))
284 dri2_dpy
->loader_extensions
= image_loader_extensions
;
288 free(dri2_dpy
->driver_name
);
289 dri2_dpy
->driver_name
= NULL
;
299 device_probe_device_sw(_EGLDisplay
*disp
)
301 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
304 dri2_dpy
->driver_name
= strdup("swrast");
305 if (!dri2_dpy
->driver_name
)
308 /* HACK: should be driver_swrast_null */
309 if (!dri2_load_driver_swrast(disp
)) {
310 free(dri2_dpy
->driver_name
);
311 dri2_dpy
->driver_name
= NULL
;
315 dri2_dpy
->loader_extensions
= swrast_loader_extensions
;
320 dri2_initialize_device(_EGLDisplay
*disp
)
323 struct dri2_egl_display
*dri2_dpy
;
326 dri2_dpy
= calloc(1, sizeof *dri2_dpy
);
328 return _eglError(EGL_BAD_ALLOC
, "eglInitialize");
330 /* Extension requires a PlatformDisplay - the EGLDevice. */
331 dev
= disp
->PlatformDisplay
;
335 disp
->DriverData
= (void *) dri2_dpy
;
336 err
= "DRI2: failed to load driver";
337 if (_eglDeviceSupports(dev
, _EGL_DEVICE_DRM
)) {
338 if (!device_probe_device(disp
))
340 } else if (_eglDeviceSupports(dev
, _EGL_DEVICE_SOFTWARE
)) {
341 if (!device_probe_device_sw(disp
))
344 _eglLog(_EGL_FATAL
, "Driver bug: exposed device is neither DRM nor SOFTWARE one");
348 if (!dri2_create_screen(disp
)) {
349 err
= "DRI2: failed to create screen";
353 if (!dri2_setup_extensions(disp
)) {
354 err
= "DRI2: failed to find required DRI extensions";
358 dri2_setup_screen(disp
);
360 if (!dri2_add_pbuffer_configs_for_visuals(disp
)) {
361 err
= "DRI2: failed to add configs";
365 /* Fill vtbl last to prevent accidentally calling virtual function during
368 dri2_dpy
->vtbl
= &dri2_device_display_vtbl
;
373 dri2_display_destroy(disp
);
374 return _eglError(EGL_NOT_INITIALIZED
, err
);