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
;
66 surfaceless_image_get_buffers(__DRIdrawable
*driDrawable
,
71 struct __DRIimageList
*buffers
)
73 struct dri2_egl_surface
*dri2_surf
= loaderPrivate
;
74 struct dri2_egl_display
*dri2_dpy
=
75 dri2_egl_display(dri2_surf
->base
.Resource
.Display
);
77 buffers
->image_mask
= 0;
78 buffers
->front
= NULL
;
81 /* The EGL 1.5 spec states that pbuffers are single-buffered. Specifically,
82 * the spec states that they have a back buffer but no front buffer, in
83 * contrast to pixmaps, which have a front buffer but no back buffer.
85 * Single-buffered surfaces with no front buffer confuse Mesa; so we deviate
86 * from the spec, following the precedent of Mesa's EGL X11 platform. The
87 * X11 platform correctly assigns pbuffers to single-buffered configs, but
88 * assigns the pbuffer a front buffer instead of a back buffer.
90 * Pbuffers in the X11 platform mostly work today, so let's just copy its
91 * behavior instead of trying to fix (and hence potentially breaking) the
95 if (buffer_mask
& __DRI_IMAGE_BUFFER_FRONT
) {
97 if (!dri2_surf
->front
)
99 surfaceless_alloc_image(dri2_dpy
, dri2_surf
);
101 buffers
->image_mask
|= __DRI_IMAGE_BUFFER_FRONT
;
102 buffers
->front
= dri2_surf
->front
;
109 dri2_surfaceless_create_surface(_EGLDriver
*drv
, _EGLDisplay
*disp
, EGLint type
,
110 _EGLConfig
*conf
, const EGLint
*attrib_list
)
112 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
113 struct dri2_egl_config
*dri2_conf
= dri2_egl_config(conf
);
114 struct dri2_egl_surface
*dri2_surf
;
115 const __DRIconfig
*config
;
117 /* Make sure to calloc so all pointers
118 * are originally NULL.
120 dri2_surf
= calloc(1, sizeof *dri2_surf
);
123 _eglError(EGL_BAD_ALLOC
, "eglCreatePbufferSurface");
127 if (!_eglInitSurface(&dri2_surf
->base
, disp
, type
, conf
, attrib_list
))
128 goto cleanup_surface
;
130 config
= dri2_get_dri_config(dri2_conf
, type
,
131 dri2_surf
->base
.GLColorspace
);
134 goto cleanup_surface
;
136 dri2_surf
->dri_drawable
=
137 (*dri2_dpy
->dri2
->createNewDrawable
)(dri2_dpy
->dri_screen
, config
,
139 if (dri2_surf
->dri_drawable
== NULL
) {
140 _eglError(EGL_BAD_ALLOC
, "dri2->createNewDrawable");
141 goto cleanup_surface
;
144 if (conf
->RedSize
== 5)
145 dri2_surf
->visual
= __DRI_IMAGE_FORMAT_RGB565
;
146 else if (conf
->AlphaSize
== 0)
147 dri2_surf
->visual
= __DRI_IMAGE_FORMAT_XRGB8888
;
149 dri2_surf
->visual
= __DRI_IMAGE_FORMAT_ARGB8888
;
151 return &dri2_surf
->base
;
159 surfaceless_destroy_surface(_EGLDriver
*drv
, _EGLDisplay
*disp
, _EGLSurface
*surf
)
161 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
162 struct dri2_egl_surface
*dri2_surf
= dri2_egl_surface(surf
);
164 surfaceless_free_images(dri2_surf
);
166 (*dri2_dpy
->core
->destroyDrawable
)(dri2_surf
->dri_drawable
);
173 dri2_surfaceless_create_pbuffer_surface(_EGLDriver
*drv
, _EGLDisplay
*disp
,
174 _EGLConfig
*conf
, const EGLint
*attrib_list
)
176 return dri2_surfaceless_create_surface(drv
, disp
, EGL_PBUFFER_BIT
, conf
,
181 surfaceless_add_configs_for_visuals(_EGLDriver
*drv
, _EGLDisplay
*dpy
)
184 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(dpy
);
186 unsigned int visuals
[3][4] = {
187 { 0xff0000, 0xff00, 0xff, 0xff000000 }, // ARGB8888
188 { 0xff0000, 0xff00, 0xff, 0x0 }, // RGB888
189 { 0xf800, 0x7e0, 0x1f, 0x0 }, // RGB565
193 unsigned int r
, b
, g
, a
;
196 for (i
= 0; i
< ARRAY_SIZE(visuals
); i
++) {
197 for (j
= 0; dri2_dpy
->driver_configs
[j
]; j
++) {
198 const EGLint surface_type
= EGL_PBUFFER_BIT
;
199 struct dri2_egl_config
*dri2_conf
;
201 /* Determine driver supported masks */
202 dri2_dpy
->core
->getConfigAttrib(dri2_dpy
->driver_configs
[j
],
203 __DRI_ATTRIB_RED_MASK
, &r
);
204 dri2_dpy
->core
->getConfigAttrib(dri2_dpy
->driver_configs
[j
],
205 __DRI_ATTRIB_BLUE_MASK
, &b
);
206 dri2_dpy
->core
->getConfigAttrib(dri2_dpy
->driver_configs
[j
],
207 __DRI_ATTRIB_GREEN_MASK
, &g
);
208 dri2_dpy
->core
->getConfigAttrib(dri2_dpy
->driver_configs
[j
],
209 __DRI_ATTRIB_ALPHA_MASK
, &a
);
211 /* Compare with advertised visuals */
212 if (r
^ visuals
[i
][0] || g
^ visuals
[i
][1]
213 || b
^ visuals
[i
][2] || a
^ visuals
[i
][3])
216 dri2_conf
= dri2_add_config(dpy
, dri2_dpy
->driver_configs
[j
],
217 count
+ 1, surface_type
, NULL
, visuals
[i
]);
225 _eglLog(_EGL_DEBUG
, "Can't create surfaceless visuals");
230 static struct dri2_egl_display_vtbl dri2_surfaceless_display_vtbl
= {
231 .create_pixmap_surface
= dri2_fallback_create_pixmap_surface
,
232 .create_pbuffer_surface
= dri2_surfaceless_create_pbuffer_surface
,
233 .destroy_surface
= surfaceless_destroy_surface
,
234 .create_image
= dri2_create_image_khr
,
235 .swap_interval
= dri2_fallback_swap_interval
,
236 .swap_buffers_with_damage
= dri2_fallback_swap_buffers_with_damage
,
237 .swap_buffers_region
= dri2_fallback_swap_buffers_region
,
238 .post_sub_buffer
= dri2_fallback_post_sub_buffer
,
239 .copy_buffers
= dri2_fallback_copy_buffers
,
240 .query_buffer_age
= dri2_fallback_query_buffer_age
,
241 .create_wayland_buffer_from_image
= dri2_fallback_create_wayland_buffer_from_image
,
242 .get_sync_values
= dri2_fallback_get_sync_values
,
243 .get_dri_drawable
= dri2_surface_get_dri_drawable
,
247 surfaceless_flush_front_buffer(__DRIdrawable
*driDrawable
, void *loaderPrivate
)
252 surfaceless_get_buffers_with_format(__DRIdrawable
* driDrawable
,
253 int *width
, int *height
,
254 unsigned int *attachments
, int count
,
255 int *out_count
, void *loaderPrivate
)
257 struct dri2_egl_surface
*dri2_surf
= loaderPrivate
;
259 dri2_surf
->buffer_count
= 1;
261 *width
= dri2_surf
->base
.Width
;
263 *height
= dri2_surf
->base
.Height
;
264 *out_count
= dri2_surf
->buffer_count
;
265 return dri2_surf
->buffers
;
268 static const __DRIdri2LoaderExtension droid_dri2_loader_extension
= {
269 .base
= { __DRI_DRI2_LOADER
, 3 },
272 .flushFrontBuffer
= droid_flush_front_buffer
,
273 .getBuffersWithFormat
= droid_get_buffers_with_format
,
276 static const __DRIimageLoaderExtension image_loader_extension
= {
277 .base
= { __DRI_IMAGE_LOADER
, 1 },
278 .getBuffers
= surfaceless_image_get_buffers
,
279 .flushFrontBuffer
= surfaceless_flush_front_buffer
,
282 #define DRM_RENDER_DEV_NAME "%s/renderD%d"
285 dri2_initialize_surfaceless(_EGLDriver
*drv
, _EGLDisplay
*disp
)
287 struct dri2_egl_display
*dri2_dpy
;
290 int driver_loaded
= 0;
292 loader_set_logger(_eglLog
);
294 dri2_dpy
= calloc(1, sizeof *dri2_dpy
);
296 return _eglError(EGL_BAD_ALLOC
, "eglInitialize");
298 disp
->DriverData
= (void *) dri2_dpy
;
300 const int limit
= 64;
301 const int base
= 128;
302 for (i
= 0; i
< limit
; ++i
) {
304 if (asprintf(&card_path
, DRM_RENDER_DEV_NAME
, DRM_DIR_NAME
, base
+ i
) < 0)
307 dri2_dpy
->fd
= loader_open_device(card_path
);
310 if (dri2_dpy
->fd
< 0)
313 dri2_dpy
->driver_name
= loader_get_driver_for_fd(dri2_dpy
->fd
, 0);
314 if (dri2_dpy
->driver_name
) {
315 if (dri2_load_driver(disp
)) {
319 free(dri2_dpy
->driver_name
);
324 if (!driver_loaded
) {
325 err
= "DRI2: failed to load driver";
326 goto cleanup_display
;
329 dri2_dpy
->dri2_loader_extension
.base
.name
= __DRI_DRI2_LOADER
;
330 dri2_dpy
->dri2_loader_extension
.base
.version
= 3;
331 dri2_dpy
->dri2_loader_extension
.getBuffers
= NULL
;
332 dri2_dpy
->dri2_loader_extension
.flushFrontBuffer
=
333 surfaceless_flush_front_buffer
;
334 dri2_dpy
->dri2_loader_extension
.getBuffersWithFormat
=
335 surfaceless_get_buffers_with_format
;
337 dri2_dpy
->extensions
[0] = &image_loader_extension
.base
;
338 dri2_dpy
->extensions
[1] = &image_lookup_extension
.base
;
339 dri2_dpy
->extensions
[2] = &use_invalidate
.base
;
340 dri2_dpy
->extensions
[3] = NULL
;
342 if (!dri2_create_screen(disp
)) {
343 err
= "DRI2: failed to create screen";
347 if (!surfaceless_add_configs_for_visuals(drv
, disp
)) {
348 err
= "DRI2: failed to add configs";
352 disp
->Extensions
.KHR_image_base
= EGL_TRUE
;
354 /* Fill vtbl last to prevent accidentally calling virtual function during
357 dri2_dpy
->vtbl
= &dri2_surfaceless_display_vtbl
;
362 dri2_dpy
->core
->destroyScreen(dri2_dpy
->dri_screen
);
365 dlclose(dri2_dpy
->driver
);
366 free(dri2_dpy
->driver_name
);
370 disp
->DriverData
= NULL
;
372 return _eglError(EGL_NOT_INITIALIZED
, err
);