egl: Implement getImage/putImage on pbuffer swrast.
[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 free(dri2_surf->swrast_device_buffer);
65 dri2_surf->swrast_device_buffer = NULL;
66 }
67
68 static int
69 surfaceless_image_get_buffers(__DRIdrawable *driDrawable,
70 unsigned int format,
71 uint32_t *stamp,
72 void *loaderPrivate,
73 uint32_t buffer_mask,
74 struct __DRIimageList *buffers)
75 {
76 struct dri2_egl_surface *dri2_surf = loaderPrivate;
77 struct dri2_egl_display *dri2_dpy =
78 dri2_egl_display(dri2_surf->base.Resource.Display);
79
80 buffers->image_mask = 0;
81 buffers->front = NULL;
82 buffers->back = NULL;
83
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.
87 *
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.
92 *
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
95 * world.
96 */
97
98 if (buffer_mask & __DRI_IMAGE_BUFFER_FRONT) {
99
100 if (!dri2_surf->front)
101 dri2_surf->front =
102 surfaceless_alloc_image(dri2_dpy, dri2_surf);
103
104 buffers->image_mask |= __DRI_IMAGE_BUFFER_FRONT;
105 buffers->front = dri2_surf->front;
106 }
107
108 return 1;
109 }
110
111 static _EGLSurface *
112 dri2_surfaceless_create_surface(_EGLDriver *drv, _EGLDisplay *disp, EGLint type,
113 _EGLConfig *conf, const EGLint *attrib_list)
114 {
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;
119
120 /* Make sure to calloc so all pointers
121 * are originally NULL.
122 */
123 dri2_surf = calloc(1, sizeof *dri2_surf);
124
125 if (!dri2_surf) {
126 _eglError(EGL_BAD_ALLOC, "eglCreatePbufferSurface");
127 return NULL;
128 }
129
130 if (!dri2_init_surface(&dri2_surf->base, disp, type, conf, attrib_list,
131 false, NULL))
132 goto cleanup_surface;
133
134 config = dri2_get_dri_config(dri2_conf, type,
135 dri2_surf->base.GLColorspace);
136
137 if (!config) {
138 _eglError(EGL_BAD_MATCH, "Unsupported surfacetype/colorspace configuration");
139 goto cleanup_surface;
140 }
141
142 if (!dri2_create_drawable(dri2_dpy, config, dri2_surf, dri2_surf))
143 goto cleanup_surface;
144
145 if (conf->RedSize == 5)
146 dri2_surf->visual = __DRI_IMAGE_FORMAT_RGB565;
147 else if (conf->AlphaSize == 0)
148 dri2_surf->visual = __DRI_IMAGE_FORMAT_XRGB8888;
149 else
150 dri2_surf->visual = __DRI_IMAGE_FORMAT_ARGB8888;
151
152 return &dri2_surf->base;
153
154 cleanup_surface:
155 free(dri2_surf);
156 return NULL;
157 }
158
159 static EGLBoolean
160 surfaceless_destroy_surface(_EGLDriver *drv, _EGLDisplay *disp, _EGLSurface *surf)
161 {
162 struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);
163 struct dri2_egl_surface *dri2_surf = dri2_egl_surface(surf);
164
165 surfaceless_free_images(dri2_surf);
166
167 dri2_dpy->core->destroyDrawable(dri2_surf->dri_drawable);
168
169 dri2_fini_surface(surf);
170 free(dri2_surf);
171 return EGL_TRUE;
172 }
173
174 static _EGLSurface *
175 dri2_surfaceless_create_pbuffer_surface(_EGLDriver *drv, _EGLDisplay *disp,
176 _EGLConfig *conf, const EGLint *attrib_list)
177 {
178 return dri2_surfaceless_create_surface(drv, disp, EGL_PBUFFER_BIT, conf,
179 attrib_list);
180 }
181
182 static EGLBoolean
183 surfaceless_add_configs_for_visuals(_EGLDriver *drv, _EGLDisplay *disp)
184 {
185 struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);
186 static const struct {
187 const char *format_name;
188 int rgba_shifts[4];
189 unsigned int rgba_sizes[4];
190 } visuals[] = {
191 { "ABGR16F", { 0, 16, 32, 48 }, { 16, 16, 16, 16 } },
192 { "XBGR16F", { 0, 16, 32, -1 }, { 16, 16, 16, 0 } },
193 { "A2RGB10", { 20, 10, 0, 30 }, { 10, 10, 10, 2 } },
194 { "X2RGB10", { 20, 10, 0, -1 }, { 10, 10, 10, 0 } },
195 { "ARGB8888", { 16, 8, 0, 24 }, { 8, 8, 8, 8 } },
196 { "RGB888", { 16, 8, 0, -1 }, { 8, 8, 8, 0 } },
197 { "RGB565", { 11, 5, 0, -1 }, { 5, 6, 5, 0 } },
198 };
199 unsigned int format_count[ARRAY_SIZE(visuals)] = { 0 };
200 unsigned int config_count = 0;
201
202 for (unsigned i = 0; dri2_dpy->driver_configs[i] != NULL; i++) {
203 for (unsigned j = 0; j < ARRAY_SIZE(visuals); j++) {
204 struct dri2_egl_config *dri2_conf;
205
206 dri2_conf = dri2_add_config(disp, dri2_dpy->driver_configs[i],
207 config_count + 1, EGL_PBUFFER_BIT, NULL,
208 visuals[j].rgba_shifts, visuals[j].rgba_sizes);
209
210 if (dri2_conf) {
211 if (dri2_conf->base.ConfigID == config_count + 1)
212 config_count++;
213 format_count[j]++;
214 }
215 }
216 }
217
218 for (unsigned i = 0; i < ARRAY_SIZE(format_count); i++) {
219 if (!format_count[i]) {
220 _eglLog(_EGL_DEBUG, "No DRI config supports native format %s",
221 visuals[i].format_name);
222 }
223 }
224
225 return (config_count != 0);
226 }
227
228 static const struct dri2_egl_display_vtbl dri2_surfaceless_display_vtbl = {
229 .create_pixmap_surface = dri2_fallback_create_pixmap_surface,
230 .create_pbuffer_surface = dri2_surfaceless_create_pbuffer_surface,
231 .destroy_surface = surfaceless_destroy_surface,
232 .create_image = dri2_create_image_khr,
233 .swap_buffers_region = dri2_fallback_swap_buffers_region,
234 .post_sub_buffer = dri2_fallback_post_sub_buffer,
235 .copy_buffers = dri2_fallback_copy_buffers,
236 .query_buffer_age = dri2_fallback_query_buffer_age,
237 .create_wayland_buffer_from_image = dri2_fallback_create_wayland_buffer_from_image,
238 .get_sync_values = dri2_fallback_get_sync_values,
239 .get_dri_drawable = dri2_surface_get_dri_drawable,
240 };
241
242 static void
243 surfaceless_flush_front_buffer(__DRIdrawable *driDrawable, void *loaderPrivate)
244 {
245 }
246
247 static unsigned
248 surfaceless_get_capability(void *loaderPrivate, enum dri_loader_cap cap)
249 {
250 /* Note: loaderPrivate is _EGLDisplay* */
251 switch (cap) {
252 case DRI_LOADER_CAP_FP16:
253 return 1;
254 default:
255 return 0;
256 }
257 }
258
259 static const __DRIimageLoaderExtension image_loader_extension = {
260 .base = { __DRI_IMAGE_LOADER, 2 },
261 .getBuffers = surfaceless_image_get_buffers,
262 .flushFrontBuffer = surfaceless_flush_front_buffer,
263 .getCapability = surfaceless_get_capability,
264 };
265
266 static const __DRIextension *image_loader_extensions[] = {
267 &image_loader_extension.base,
268 &image_lookup_extension.base,
269 &use_invalidate.base,
270 NULL,
271 };
272
273 static const __DRIextension *swrast_loader_extensions[] = {
274 &swrast_pbuffer_loader_extension.base,
275 &image_loader_extension.base,
276 &image_lookup_extension.base,
277 &use_invalidate.base,
278 NULL,
279 };
280
281 static bool
282 surfaceless_probe_device(_EGLDisplay *disp, bool swrast)
283 {
284 #define MAX_DRM_DEVICES 64
285 const unsigned node_type = swrast ? DRM_NODE_PRIMARY : DRM_NODE_RENDER;
286 struct dri2_egl_display *dri2_dpy = disp->DriverData;
287 drmDevicePtr device, devices[MAX_DRM_DEVICES] = { NULL };
288 int i, num_devices;
289
290 num_devices = drmGetDevices2(0, devices, ARRAY_SIZE(devices));
291 if (num_devices < 0)
292 return false;
293
294 for (i = 0; i < num_devices; ++i) {
295 device = devices[i];
296
297 if (!(device->available_nodes & (1 << node_type)))
298 continue;
299
300 dri2_dpy->fd = loader_open_device(device->nodes[node_type]);
301 if (dri2_dpy->fd < 0)
302 continue;
303
304 disp->Device = _eglAddDevice(dri2_dpy->fd, swrast);
305 if (!disp->Device) {
306 close(dri2_dpy->fd);
307 dri2_dpy->fd = -1;
308 continue;
309 }
310
311 char *driver_name = loader_get_driver_for_fd(dri2_dpy->fd);
312 if (swrast) {
313 /* Use kms swrast only with vgem / virtio_gpu.
314 * virtio-gpu fallbacks to software rendering when 3D features
315 * are unavailable since 6c5ab, and kms_swrast is more
316 * feature complete than swrast.
317 */
318 if (driver_name &&
319 (strcmp(driver_name, "vgem") == 0 ||
320 strcmp(driver_name, "virtio_gpu") == 0))
321 dri2_dpy->driver_name = strdup("kms_swrast");
322 free(driver_name);
323 } else {
324 /* Use the given hardware driver */
325 dri2_dpy->driver_name = driver_name;
326 }
327
328 if (dri2_dpy->driver_name && dri2_load_driver_dri3(disp))
329 break;
330
331 free(dri2_dpy->driver_name);
332 dri2_dpy->driver_name = NULL;
333 close(dri2_dpy->fd);
334 dri2_dpy->fd = -1;
335 }
336 drmFreeDevices(devices, num_devices);
337
338 if (i == num_devices)
339 return false;
340
341 if (swrast)
342 dri2_dpy->loader_extensions = swrast_loader_extensions;
343 else
344 dri2_dpy->loader_extensions = image_loader_extensions;
345
346 return true;
347 }
348
349 static bool
350 surfaceless_probe_device_sw(_EGLDisplay *disp)
351 {
352 struct dri2_egl_display *dri2_dpy = disp->DriverData;
353
354 dri2_dpy->fd = -1;
355 disp->Device = _eglAddDevice(dri2_dpy->fd, true);
356 assert(disp->Device);
357
358 dri2_dpy->driver_name = strdup("swrast");
359 if (!dri2_dpy->driver_name)
360 return false;
361
362 if (!dri2_load_driver_swrast(disp)) {
363 free(dri2_dpy->driver_name);
364 dri2_dpy->driver_name = NULL;
365 return false;
366 }
367
368 dri2_dpy->loader_extensions = swrast_loader_extensions;
369 return true;
370 }
371
372 EGLBoolean
373 dri2_initialize_surfaceless(_EGLDriver *drv, _EGLDisplay *disp)
374 {
375 struct dri2_egl_display *dri2_dpy;
376 const char* err;
377 bool driver_loaded = false;
378
379 dri2_dpy = calloc(1, sizeof *dri2_dpy);
380 if (!dri2_dpy)
381 return _eglError(EGL_BAD_ALLOC, "eglInitialize");
382
383 dri2_dpy->fd = -1;
384 disp->DriverData = (void *) dri2_dpy;
385
386 if (!disp->Options.ForceSoftware) {
387 driver_loaded = surfaceless_probe_device(disp, false);
388 if (!driver_loaded)
389 _eglLog(_EGL_WARNING,
390 "No hardware driver found, falling back to software rendering");
391 }
392
393 if (!driver_loaded)
394 driver_loaded = surfaceless_probe_device(disp, true);
395
396 if (!driver_loaded) {
397 _eglLog(_EGL_DEBUG, "Falling back to surfaceless swrast without DRM.");
398 if (!surfaceless_probe_device_sw(disp)) {
399 err = "DRI2: failed to load driver";
400 goto cleanup;
401 }
402 }
403
404 if (!dri2_create_screen(disp)) {
405 err = "DRI2: failed to create screen";
406 goto cleanup;
407 }
408
409 if (!dri2_setup_extensions(disp)) {
410 err = "DRI2: failed to find required DRI extensions";
411 goto cleanup;
412 }
413
414 dri2_setup_screen(disp);
415 #ifdef HAVE_WAYLAND_PLATFORM
416 dri2_dpy->device_name = loader_get_device_name_for_fd(dri2_dpy->fd);
417 #endif
418 dri2_set_WL_bind_wayland_display(drv, disp);
419
420 if (!surfaceless_add_configs_for_visuals(drv, disp)) {
421 err = "DRI2: failed to add configs";
422 goto cleanup;
423 }
424
425 /* Fill vtbl last to prevent accidentally calling virtual function during
426 * initialization.
427 */
428 dri2_dpy->vtbl = &dri2_surfaceless_display_vtbl;
429
430 return EGL_TRUE;
431
432 cleanup:
433 dri2_display_destroy(disp);
434 return _eglError(EGL_NOT_INITIALIZED, err);
435 }