2 * Mesa 3-D graphics library
4 * Copyright (c) 2014 The Chromium OS Authors.
5 * Copyright © 2011 Intel Corporation
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:
14 * The above copyright notice and this permission notice shall be included
15 * in all copies or substantial portions of the Software.
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.
31 #include <sys/types.h>
37 #include "egl_dri2_fallbacks.h"
41 surfaceless_alloc_image(struct dri2_egl_display
*dri2_dpy
,
42 struct dri2_egl_surface
*dri2_surf
)
44 return dri2_dpy
->image
->createImage(
46 dri2_surf
->base
.Width
,
47 dri2_surf
->base
.Height
,
54 surfaceless_free_images(struct dri2_egl_surface
*dri2_surf
)
56 struct dri2_egl_display
*dri2_dpy
=
57 dri2_egl_display(dri2_surf
->base
.Resource
.Display
);
59 if (dri2_surf
->front
) {
60 dri2_dpy
->image
->destroyImage(dri2_surf
->front
);
61 dri2_surf
->front
= NULL
;
64 free(dri2_surf
->swrast_device_buffer
);
65 dri2_surf
->swrast_device_buffer
= NULL
;
69 surfaceless_image_get_buffers(__DRIdrawable
*driDrawable
,
74 struct __DRIimageList
*buffers
)
76 struct dri2_egl_surface
*dri2_surf
= loaderPrivate
;
77 struct dri2_egl_display
*dri2_dpy
=
78 dri2_egl_display(dri2_surf
->base
.Resource
.Display
);
80 buffers
->image_mask
= 0;
81 buffers
->front
= NULL
;
84 /* The EGL 1.5 spec states that pbuffers are single-buffered. Specifically,
85 * the spec states that they have a back buffer but no front buffer, in
86 * contrast to pixmaps, which have a front buffer but no back buffer.
88 * Single-buffered surfaces with no front buffer confuse Mesa; so we deviate
89 * from the spec, following the precedent of Mesa's EGL X11 platform. The
90 * X11 platform correctly assigns pbuffers to single-buffered configs, but
91 * assigns the pbuffer a front buffer instead of a back buffer.
93 * Pbuffers in the X11 platform mostly work today, so let's just copy its
94 * behavior instead of trying to fix (and hence potentially breaking) the
98 if (buffer_mask
& __DRI_IMAGE_BUFFER_FRONT
) {
100 if (!dri2_surf
->front
)
102 surfaceless_alloc_image(dri2_dpy
, dri2_surf
);
104 buffers
->image_mask
|= __DRI_IMAGE_BUFFER_FRONT
;
105 buffers
->front
= dri2_surf
->front
;
112 dri2_surfaceless_create_surface(_EGLDriver
*drv
, _EGLDisplay
*disp
, EGLint type
,
113 _EGLConfig
*conf
, const EGLint
*attrib_list
)
115 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
116 struct dri2_egl_config
*dri2_conf
= dri2_egl_config(conf
);
117 struct dri2_egl_surface
*dri2_surf
;
118 const __DRIconfig
*config
;
120 /* Make sure to calloc so all pointers
121 * are originally NULL.
123 dri2_surf
= calloc(1, sizeof *dri2_surf
);
126 _eglError(EGL_BAD_ALLOC
, "eglCreatePbufferSurface");
130 if (!dri2_init_surface(&dri2_surf
->base
, disp
, type
, conf
, attrib_list
,
132 goto cleanup_surface
;
134 config
= dri2_get_dri_config(dri2_conf
, type
,
135 dri2_surf
->base
.GLColorspace
);
138 _eglError(EGL_BAD_MATCH
, "Unsupported surfacetype/colorspace configuration");
139 goto cleanup_surface
;
142 dri2_surf
->visual
= dri2_image_format_for_pbuffer_config(dri2_dpy
, config
);
143 if (dri2_surf
->visual
== __DRI_IMAGE_FORMAT_NONE
)
144 goto cleanup_surface
;
146 if (!dri2_create_drawable(dri2_dpy
, config
, dri2_surf
, dri2_surf
))
147 goto cleanup_surface
;
149 return &dri2_surf
->base
;
157 surfaceless_destroy_surface(_EGLDriver
*drv
, _EGLDisplay
*disp
, _EGLSurface
*surf
)
159 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
160 struct dri2_egl_surface
*dri2_surf
= dri2_egl_surface(surf
);
162 surfaceless_free_images(dri2_surf
);
164 dri2_dpy
->core
->destroyDrawable(dri2_surf
->dri_drawable
);
166 dri2_fini_surface(surf
);
172 dri2_surfaceless_create_pbuffer_surface(_EGLDriver
*drv
, _EGLDisplay
*disp
,
173 _EGLConfig
*conf
, const EGLint
*attrib_list
)
175 return dri2_surfaceless_create_surface(drv
, disp
, EGL_PBUFFER_BIT
, conf
,
179 static const struct dri2_egl_display_vtbl dri2_surfaceless_display_vtbl
= {
180 .create_pbuffer_surface
= dri2_surfaceless_create_pbuffer_surface
,
181 .destroy_surface
= surfaceless_destroy_surface
,
182 .create_image
= dri2_create_image_khr
,
183 .query_buffer_age
= dri2_fallback_query_buffer_age
,
184 .create_wayland_buffer_from_image
= dri2_fallback_create_wayland_buffer_from_image
,
185 .get_sync_values
= dri2_fallback_get_sync_values
,
186 .get_dri_drawable
= dri2_surface_get_dri_drawable
,
190 surfaceless_flush_front_buffer(__DRIdrawable
*driDrawable
, void *loaderPrivate
)
195 surfaceless_get_capability(void *loaderPrivate
, enum dri_loader_cap cap
)
197 /* Note: loaderPrivate is _EGLDisplay* */
199 case DRI_LOADER_CAP_FP16
:
206 static const __DRIimageLoaderExtension image_loader_extension
= {
207 .base
= { __DRI_IMAGE_LOADER
, 2 },
208 .getBuffers
= surfaceless_image_get_buffers
,
209 .flushFrontBuffer
= surfaceless_flush_front_buffer
,
210 .getCapability
= surfaceless_get_capability
,
213 static const __DRIextension
*image_loader_extensions
[] = {
214 &image_loader_extension
.base
,
215 &image_lookup_extension
.base
,
216 &use_invalidate
.base
,
220 static const __DRIextension
*swrast_loader_extensions
[] = {
221 &swrast_pbuffer_loader_extension
.base
,
222 &image_loader_extension
.base
,
223 &image_lookup_extension
.base
,
224 &use_invalidate
.base
,
229 surfaceless_probe_device(_EGLDisplay
*disp
, bool swrast
)
231 #define MAX_DRM_DEVICES 64
232 const unsigned node_type
= swrast
? DRM_NODE_PRIMARY
: DRM_NODE_RENDER
;
233 struct dri2_egl_display
*dri2_dpy
= disp
->DriverData
;
234 drmDevicePtr device
, devices
[MAX_DRM_DEVICES
] = { NULL
};
237 num_devices
= drmGetDevices2(0, devices
, ARRAY_SIZE(devices
));
241 for (i
= 0; i
< num_devices
; ++i
) {
244 if (!(device
->available_nodes
& (1 << node_type
)))
247 dri2_dpy
->fd
= loader_open_device(device
->nodes
[node_type
]);
248 if (dri2_dpy
->fd
< 0)
251 disp
->Device
= _eglAddDevice(dri2_dpy
->fd
, swrast
);
258 char *driver_name
= loader_get_driver_for_fd(dri2_dpy
->fd
);
260 /* Use kms swrast only with vgem / virtio_gpu.
261 * virtio-gpu fallbacks to software rendering when 3D features
262 * are unavailable since 6c5ab, and kms_swrast is more
263 * feature complete than swrast.
266 (strcmp(driver_name
, "vgem") == 0 ||
267 strcmp(driver_name
, "virtio_gpu") == 0))
268 dri2_dpy
->driver_name
= strdup("kms_swrast");
271 /* Use the given hardware driver */
272 dri2_dpy
->driver_name
= driver_name
;
275 if (dri2_dpy
->driver_name
&& dri2_load_driver_dri3(disp
))
278 free(dri2_dpy
->driver_name
);
279 dri2_dpy
->driver_name
= NULL
;
283 drmFreeDevices(devices
, num_devices
);
285 if (i
== num_devices
)
289 dri2_dpy
->loader_extensions
= swrast_loader_extensions
;
291 dri2_dpy
->loader_extensions
= image_loader_extensions
;
297 surfaceless_probe_device_sw(_EGLDisplay
*disp
)
299 struct dri2_egl_display
*dri2_dpy
= disp
->DriverData
;
302 disp
->Device
= _eglAddDevice(dri2_dpy
->fd
, true);
303 assert(disp
->Device
);
305 dri2_dpy
->driver_name
= strdup("swrast");
306 if (!dri2_dpy
->driver_name
)
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_surfaceless(_EGLDriver
*drv
, _EGLDisplay
*disp
)
322 struct dri2_egl_display
*dri2_dpy
;
324 bool driver_loaded
= false;
326 dri2_dpy
= calloc(1, sizeof *dri2_dpy
);
328 return _eglError(EGL_BAD_ALLOC
, "eglInitialize");
331 disp
->DriverData
= (void *) dri2_dpy
;
333 if (!disp
->Options
.ForceSoftware
) {
334 driver_loaded
= surfaceless_probe_device(disp
, false);
336 _eglLog(_EGL_WARNING
,
337 "No hardware driver found, falling back to software rendering");
341 driver_loaded
= surfaceless_probe_device(disp
, true);
343 if (!driver_loaded
) {
344 _eglLog(_EGL_DEBUG
, "Falling back to surfaceless swrast without DRM.");
345 if (!surfaceless_probe_device_sw(disp
)) {
346 err
= "DRI2: failed to load driver";
351 if (!dri2_create_screen(disp
)) {
352 err
= "DRI2: failed to create screen";
356 if (!dri2_setup_extensions(disp
)) {
357 err
= "DRI2: failed to find required DRI extensions";
361 dri2_setup_screen(disp
);
362 #ifdef HAVE_WAYLAND_PLATFORM
363 dri2_dpy
->device_name
= loader_get_device_name_for_fd(dri2_dpy
->fd
);
365 dri2_set_WL_bind_wayland_display(drv
, disp
);
367 if (!dri2_add_pbuffer_configs_for_visuals(drv
, disp
)) {
368 err
= "DRI2: failed to add configs";
372 /* Fill vtbl last to prevent accidentally calling virtual function during
375 dri2_dpy
->vtbl
= &dri2_surfaceless_display_vtbl
;
380 dri2_display_destroy(disp
);
381 return _eglError(EGL_NOT_INITIALIZED
, err
);