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>
46 device_alloc_image(struct dri2_egl_display
*dri2_dpy
,
47 struct dri2_egl_surface
*dri2_surf
)
49 return dri2_dpy
->image
->createImage(
51 dri2_surf
->base
.Width
,
52 dri2_surf
->base
.Height
,
59 device_free_images(struct dri2_egl_surface
*dri2_surf
)
61 struct dri2_egl_display
*dri2_dpy
=
62 dri2_egl_display(dri2_surf
->base
.Resource
.Display
);
64 if (dri2_surf
->front
) {
65 dri2_dpy
->image
->destroyImage(dri2_surf
->front
);
66 dri2_surf
->front
= NULL
;
69 free(dri2_surf
->swrast_device_buffer
);
70 dri2_surf
->swrast_device_buffer
= NULL
;
74 device_image_get_buffers(__DRIdrawable
*driDrawable
,
79 struct __DRIimageList
*buffers
)
81 struct dri2_egl_surface
*dri2_surf
= loaderPrivate
;
82 struct dri2_egl_display
*dri2_dpy
=
83 dri2_egl_display(dri2_surf
->base
.Resource
.Display
);
85 buffers
->image_mask
= 0;
86 buffers
->front
= NULL
;
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.
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.
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
103 if (buffer_mask
& __DRI_IMAGE_BUFFER_FRONT
) {
105 if (!dri2_surf
->front
)
107 device_alloc_image(dri2_dpy
, dri2_surf
);
109 buffers
->image_mask
|= __DRI_IMAGE_BUFFER_FRONT
;
110 buffers
->front
= dri2_surf
->front
;
117 dri2_device_create_surface(_EGLDriver
*drv
, _EGLDisplay
*disp
, EGLint type
,
118 _EGLConfig
*conf
, const EGLint
*attrib_list
)
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
;
125 /* Make sure to calloc so all pointers
126 * are originally NULL.
128 dri2_surf
= calloc(1, sizeof *dri2_surf
);
131 _eglError(EGL_BAD_ALLOC
, "eglCreatePbufferSurface");
135 if (!dri2_init_surface(&dri2_surf
->base
, disp
, type
, conf
, attrib_list
,
137 goto cleanup_surface
;
139 config
= dri2_get_dri_config(dri2_conf
, type
,
140 dri2_surf
->base
.GLColorspace
);
143 _eglError(EGL_BAD_MATCH
, "Unsupported surfacetype/colorspace configuration");
144 goto cleanup_surface
;
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
;
151 if (!dri2_create_drawable(dri2_dpy
, config
, dri2_surf
, dri2_surf
))
152 goto cleanup_surface
;
154 return &dri2_surf
->base
;
162 device_destroy_surface(_EGLDriver
*drv
, _EGLDisplay
*disp
, _EGLSurface
*surf
)
164 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
165 struct dri2_egl_surface
*dri2_surf
= dri2_egl_surface(surf
);
167 device_free_images(dri2_surf
);
169 dri2_dpy
->core
->destroyDrawable(dri2_surf
->dri_drawable
);
171 dri2_fini_surface(surf
);
177 dri2_device_create_pbuffer_surface(_EGLDriver
*drv
, _EGLDisplay
*disp
,
178 _EGLConfig
*conf
, const EGLint
*attrib_list
)
180 return dri2_device_create_surface(drv
, disp
, EGL_PBUFFER_BIT
, conf
,
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
,
192 device_flush_front_buffer(__DRIdrawable
*driDrawable
, void *loaderPrivate
)
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
,
202 static const __DRIextension
*image_loader_extensions
[] = {
203 &image_loader_extension
.base
,
204 &image_lookup_extension
.base
,
205 &use_invalidate
.base
,
209 static const __DRIextension
*swrast_loader_extensions
[] = {
210 &swrast_pbuffer_loader_extension
.base
,
211 &image_lookup_extension
.base
,
212 &use_invalidate
.base
,
217 device_get_fd(_EGLDisplay
*disp
, _EGLDevice
*dev
)
220 int fd
= disp
->Options
.fd
;
221 /* The fcntl() code in _eglGetDeviceDisplay() ensures that valid fd >= 3,
222 * and invalid one is 0.
225 /* According to the spec - if the FD does not match the EGLDevice
226 * behaviour is undefined.
228 * Add a trivial sanity check since it doesn't cost us anything.
230 if (dev
!= _eglAddDevice(fd
, false))
233 /* No EGL_EXT_output* extensions are supported, hence no master perms
234 * are needed. Get the render one - otherwise drivers might error out.
236 char *node
= drmGetRenderDeviceNameFromFd(fd
);
238 /* Don't close the internal fd, get render node one based on it. */
239 fd
= loader_open_device(node
);
243 const char *node
= _eglGetDRMDeviceRenderNode(dev
);
244 return loader_open_device(node
);
246 _eglLog(_EGL_FATAL
, "Driver bug: Built without libdrm, yet using a HW device");
252 device_probe_device(_EGLDisplay
*disp
)
254 struct dri2_egl_display
*dri2_dpy
= disp
->DriverData
;
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)
263 dri2_dpy
->driver_name
= loader_get_driver_for_fd(dri2_dpy
->fd
);
264 if (!dri2_dpy
->driver_name
)
267 if (!dri2_load_driver_dri3(disp
))
270 dri2_dpy
->loader_extensions
= image_loader_extensions
;
274 free(dri2_dpy
->driver_name
);
275 dri2_dpy
->driver_name
= NULL
;
285 device_probe_device_sw(_EGLDisplay
*disp
)
287 struct dri2_egl_display
*dri2_dpy
= disp
->DriverData
;
290 dri2_dpy
->driver_name
= strdup("swrast");
291 if (!dri2_dpy
->driver_name
)
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
;
301 dri2_dpy
->loader_extensions
= swrast_loader_extensions
;
306 dri2_initialize_device(_EGLDriver
*drv
, _EGLDisplay
*disp
)
309 struct dri2_egl_display
*dri2_dpy
;
312 dri2_dpy
= calloc(1, sizeof *dri2_dpy
);
314 return _eglError(EGL_BAD_ALLOC
, "eglInitialize");
316 /* Extension requires a PlatformDisplay - the EGLDevice. */
317 dev
= disp
->PlatformDisplay
;
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
))
326 } else if (_eglDeviceSupports(dev
, _EGL_DEVICE_SOFTWARE
)) {
327 if (!device_probe_device_sw(disp
))
330 _eglLog(_EGL_FATAL
, "Driver bug: exposed device is neither DRM nor SOFTWARE one");
334 if (!dri2_create_screen(disp
)) {
335 err
= "DRI2: failed to create screen";
339 if (!dri2_setup_extensions(disp
)) {
340 err
= "DRI2: failed to find required DRI extensions";
344 dri2_setup_screen(disp
);
346 if (!dri2_add_pbuffer_configs_for_visuals(drv
, disp
)) {
347 err
= "DRI2: failed to add configs";
351 /* Fill vtbl last to prevent accidentally calling virtual function during
354 dri2_dpy
->vtbl
= &dri2_device_display_vtbl
;
359 dri2_display_destroy(disp
);
360 return _eglError(EGL_NOT_INITIALIZED
, err
);