egl/surfaceless: Set disp->DriverData to NULL on error
[mesa.git] / src / egl / drivers / dri2 / platform_surfaceless.c
1 /*
2 * Mesa 3-D graphics library
3 *
4 * Copyright (c) 2014 The Chromium OS Authors.
5 * Copyright © 2011 Intel Corporation
6 *
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:
13 *
14 * The above copyright notice and this permission notice shall be included
15 * in all copies or substantial portions of the Software.
16 *
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.
24 */
25
26 #include <stdlib.h>
27 #include <stdio.h>
28 #include <string.h>
29 #include <xf86drm.h>
30 #include <dlfcn.h>
31 #include <sys/types.h>
32 #include <sys/stat.h>
33 #include <fcntl.h>
34 #include <unistd.h>
35
36 #include "egl_dri2.h"
37 #include "egl_dri2_fallbacks.h"
38 #include "loader.h"
39
40 static __DRIimage*
41 surfaceless_alloc_image(struct dri2_egl_display *dri2_dpy,
42 struct dri2_egl_surface *dri2_surf)
43 {
44 return dri2_dpy->image->createImage(
45 dri2_dpy->dri_screen,
46 dri2_surf->base.Width,
47 dri2_surf->base.Height,
48 dri2_surf->visual,
49 0,
50 NULL);
51 }
52
53 static void
54 surfaceless_free_images(struct dri2_egl_surface *dri2_surf)
55 {
56 struct dri2_egl_display *dri2_dpy =
57 dri2_egl_display(dri2_surf->base.Resource.Display);
58
59 if (dri2_surf->front) {
60 dri2_dpy->image->destroyImage(dri2_surf->front);
61 dri2_surf->front = NULL;
62 }
63 }
64
65 static int
66 surfaceless_image_get_buffers(__DRIdrawable *driDrawable,
67 unsigned int format,
68 uint32_t *stamp,
69 void *loaderPrivate,
70 uint32_t buffer_mask,
71 struct __DRIimageList *buffers)
72 {
73 struct dri2_egl_surface *dri2_surf = loaderPrivate;
74 struct dri2_egl_display *dri2_dpy =
75 dri2_egl_display(dri2_surf->base.Resource.Display);
76
77 buffers->image_mask = 0;
78 buffers->front = NULL;
79 buffers->back = NULL;
80
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.
84 *
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.
89 *
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
92 * world.
93 */
94
95 if (buffer_mask & __DRI_IMAGE_BUFFER_FRONT) {
96
97 if (!dri2_surf->front)
98 dri2_surf->front =
99 surfaceless_alloc_image(dri2_dpy, dri2_surf);
100
101 buffers->image_mask |= __DRI_IMAGE_BUFFER_FRONT;
102 buffers->front = dri2_surf->front;
103 }
104
105 return 1;
106 }
107
108 static _EGLSurface *
109 dri2_surfaceless_create_surface(_EGLDriver *drv, _EGLDisplay *disp, EGLint type,
110 _EGLConfig *conf, const EGLint *attrib_list)
111 {
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;
116
117 /* Make sure to calloc so all pointers
118 * are originally NULL.
119 */
120 dri2_surf = calloc(1, sizeof *dri2_surf);
121
122 if (!dri2_surf) {
123 _eglError(EGL_BAD_ALLOC, "eglCreatePbufferSurface");
124 return NULL;
125 }
126
127 if (!_eglInitSurface(&dri2_surf->base, disp, type, conf, attrib_list))
128 goto cleanup_surface;
129
130 config = dri2_get_dri_config(dri2_conf, type,
131 dri2_surf->base.GLColorspace);
132
133 if (!config)
134 goto cleanup_surface;
135
136 dri2_surf->dri_drawable =
137 (*dri2_dpy->dri2->createNewDrawable)(dri2_dpy->dri_screen, config,
138 dri2_surf);
139 if (dri2_surf->dri_drawable == NULL) {
140 _eglError(EGL_BAD_ALLOC, "dri2->createNewDrawable");
141 goto cleanup_surface;
142 }
143
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;
148 else
149 dri2_surf->visual = __DRI_IMAGE_FORMAT_ARGB8888;
150
151 return &dri2_surf->base;
152
153 cleanup_surface:
154 free(dri2_surf);
155 return NULL;
156 }
157
158 static EGLBoolean
159 surfaceless_destroy_surface(_EGLDriver *drv, _EGLDisplay *disp, _EGLSurface *surf)
160 {
161 struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);
162 struct dri2_egl_surface *dri2_surf = dri2_egl_surface(surf);
163
164 if (!_eglPutSurface(surf))
165 return EGL_TRUE;
166
167 surfaceless_free_images(dri2_surf);
168
169 (*dri2_dpy->core->destroyDrawable)(dri2_surf->dri_drawable);
170
171 free(dri2_surf);
172 return EGL_TRUE;
173 }
174
175 static _EGLSurface *
176 dri2_surfaceless_create_pbuffer_surface(_EGLDriver *drv, _EGLDisplay *disp,
177 _EGLConfig *conf, const EGLint *attrib_list)
178 {
179 return dri2_surfaceless_create_surface(drv, disp, EGL_PBUFFER_BIT, conf,
180 attrib_list);
181 }
182
183 static EGLBoolean
184 surfaceless_add_configs_for_visuals(_EGLDriver *drv, _EGLDisplay *dpy)
185 {
186
187 struct dri2_egl_display *dri2_dpy = dri2_egl_display(dpy);
188
189 unsigned int visuals[3][4] = {
190 { 0xff0000, 0xff00, 0xff, 0xff000000 }, // ARGB8888
191 { 0xff0000, 0xff00, 0xff, 0x0 }, // RGB888
192 { 0xf800, 0x7e0, 0x1f, 0x0 }, // RGB565
193 };
194
195 int count, i, j;
196 unsigned int r, b, g, a;
197
198 count = 0;
199 for (i = 0; i < ARRAY_SIZE(visuals); i++) {
200 for (j = 0; dri2_dpy->driver_configs[j]; j++) {
201 const EGLint surface_type = EGL_PBUFFER_BIT;
202 struct dri2_egl_config *dri2_conf;
203
204 /* Determine driver supported masks */
205 dri2_dpy->core->getConfigAttrib(dri2_dpy->driver_configs[j],
206 __DRI_ATTRIB_RED_MASK, &r);
207 dri2_dpy->core->getConfigAttrib(dri2_dpy->driver_configs[j],
208 __DRI_ATTRIB_BLUE_MASK, &b);
209 dri2_dpy->core->getConfigAttrib(dri2_dpy->driver_configs[j],
210 __DRI_ATTRIB_GREEN_MASK, &g);
211 dri2_dpy->core->getConfigAttrib(dri2_dpy->driver_configs[j],
212 __DRI_ATTRIB_ALPHA_MASK, &a);
213
214 /* Compare with advertised visuals */
215 if (r ^ visuals[i][0] || g ^ visuals[i][1]
216 || b ^ visuals[i][2] || a ^ visuals[i][3])
217 continue;
218
219 dri2_conf = dri2_add_config(dpy, dri2_dpy->driver_configs[j],
220 count + 1, surface_type, NULL, visuals[i]);
221
222 if (dri2_conf)
223 count++;
224 }
225 }
226
227 if (!count)
228 _eglLog(_EGL_DEBUG, "Can't create surfaceless visuals");
229
230 return (count != 0);
231 }
232
233 static struct dri2_egl_display_vtbl dri2_surfaceless_display_vtbl = {
234 .create_pixmap_surface = dri2_fallback_create_pixmap_surface,
235 .create_pbuffer_surface = dri2_surfaceless_create_pbuffer_surface,
236 .destroy_surface = surfaceless_destroy_surface,
237 .create_image = dri2_create_image_khr,
238 .swap_interval = dri2_fallback_swap_interval,
239 .swap_buffers_with_damage = dri2_fallback_swap_buffers_with_damage,
240 .swap_buffers_region = dri2_fallback_swap_buffers_region,
241 .post_sub_buffer = dri2_fallback_post_sub_buffer,
242 .copy_buffers = dri2_fallback_copy_buffers,
243 .query_buffer_age = dri2_fallback_query_buffer_age,
244 .create_wayland_buffer_from_image = dri2_fallback_create_wayland_buffer_from_image,
245 .get_sync_values = dri2_fallback_get_sync_values,
246 .get_dri_drawable = dri2_surface_get_dri_drawable,
247 };
248
249 static void
250 surfaceless_flush_front_buffer(__DRIdrawable *driDrawable, void *loaderPrivate)
251 {
252 }
253
254 static __DRIbuffer *
255 surfaceless_get_buffers_with_format(__DRIdrawable * driDrawable,
256 int *width, int *height,
257 unsigned int *attachments, int count,
258 int *out_count, void *loaderPrivate)
259 {
260 struct dri2_egl_surface *dri2_surf = loaderPrivate;
261
262 dri2_surf->buffer_count = 1;
263 if (width)
264 *width = dri2_surf->base.Width;
265 if (height)
266 *height = dri2_surf->base.Height;
267 *out_count = dri2_surf->buffer_count;
268 return dri2_surf->buffers;
269 }
270
271 static const __DRIimageLoaderExtension image_loader_extension = {
272 .base = { __DRI_IMAGE_LOADER, 1 },
273 .getBuffers = surfaceless_image_get_buffers,
274 .flushFrontBuffer = surfaceless_flush_front_buffer,
275 };
276
277 #define DRM_RENDER_DEV_NAME "%s/renderD%d"
278
279 EGLBoolean
280 dri2_initialize_surfaceless(_EGLDriver *drv, _EGLDisplay *disp)
281 {
282 struct dri2_egl_display *dri2_dpy;
283 const char* err;
284 int i;
285 int driver_loaded = 0;
286
287 loader_set_logger(_eglLog);
288
289 dri2_dpy = calloc(1, sizeof *dri2_dpy);
290 if (!dri2_dpy)
291 return _eglError(EGL_BAD_ALLOC, "eglInitialize");
292
293 disp->DriverData = (void *) dri2_dpy;
294
295 const int limit = 64;
296 const int base = 128;
297 for (i = 0; i < limit; ++i) {
298 char *card_path;
299 if (asprintf(&card_path, DRM_RENDER_DEV_NAME, DRM_DIR_NAME, base + i) < 0)
300 continue;
301
302 dri2_dpy->fd = loader_open_device(card_path);
303
304 free(card_path);
305 if (dri2_dpy->fd < 0)
306 continue;
307
308 dri2_dpy->driver_name = loader_get_driver_for_fd(dri2_dpy->fd, 0);
309 if (dri2_dpy->driver_name) {
310 if (dri2_load_driver(disp)) {
311 driver_loaded = 1;
312 break;
313 }
314 free(dri2_dpy->driver_name);
315 }
316 close(dri2_dpy->fd);
317 }
318
319 if (!driver_loaded) {
320 err = "DRI2: failed to load driver";
321 goto cleanup_display;
322 }
323
324 dri2_dpy->dri2_loader_extension.base.name = __DRI_DRI2_LOADER;
325 dri2_dpy->dri2_loader_extension.base.version = 3;
326 dri2_dpy->dri2_loader_extension.getBuffers = NULL;
327 dri2_dpy->dri2_loader_extension.flushFrontBuffer =
328 surfaceless_flush_front_buffer;
329 dri2_dpy->dri2_loader_extension.getBuffersWithFormat =
330 surfaceless_get_buffers_with_format;
331
332 dri2_dpy->extensions[0] = &image_loader_extension.base;
333 dri2_dpy->extensions[1] = &image_lookup_extension.base;
334 dri2_dpy->extensions[2] = &use_invalidate.base;
335 dri2_dpy->extensions[3] = NULL;
336
337 if (!dri2_create_screen(disp)) {
338 err = "DRI2: failed to create screen";
339 goto cleanup_driver;
340 }
341
342 if (!surfaceless_add_configs_for_visuals(drv, disp)) {
343 err = "DRI2: failed to add configs";
344 goto cleanup_screen;
345 }
346
347 disp->Extensions.KHR_image_base = EGL_TRUE;
348
349 /* Fill vtbl last to prevent accidentally calling virtual function during
350 * initialization.
351 */
352 dri2_dpy->vtbl = &dri2_surfaceless_display_vtbl;
353
354 return EGL_TRUE;
355
356 cleanup_screen:
357 dri2_dpy->core->destroyScreen(dri2_dpy->dri_screen);
358
359 cleanup_driver:
360 dlclose(dri2_dpy->driver);
361 free(dri2_dpy->driver_name);
362 close(dri2_dpy->fd);
363 cleanup_display:
364 free(dri2_dpy);
365 disp->DriverData = NULL;
366
367 return _eglError(EGL_NOT_INITIALIZED, err);
368 }