Merge remote-tracking branch 'mesa-public/master' into vulkan
[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 struct dri2_egl_display_vtbl dri2_surfaceless_display_vtbl = {
41 .create_pixmap_surface = dri2_fallback_create_pixmap_surface,
42 .create_image = dri2_create_image_khr,
43 .swap_interval = dri2_fallback_swap_interval,
44 .swap_buffers_with_damage = dri2_fallback_swap_buffers_with_damage,
45 .swap_buffers_region = dri2_fallback_swap_buffers_region,
46 .post_sub_buffer = dri2_fallback_post_sub_buffer,
47 .copy_buffers = dri2_fallback_copy_buffers,
48 .query_buffer_age = dri2_fallback_query_buffer_age,
49 .create_wayland_buffer_from_image = dri2_fallback_create_wayland_buffer_from_image,
50 .get_sync_values = dri2_fallback_get_sync_values,
51 };
52
53 static void
54 surfaceless_flush_front_buffer(__DRIdrawable *driDrawable, void *loaderPrivate)
55 {
56 }
57
58 static __DRIbuffer *
59 surfaceless_get_buffers_with_format(__DRIdrawable * driDrawable,
60 int *width, int *height,
61 unsigned int *attachments, int count,
62 int *out_count, void *loaderPrivate)
63 {
64 struct dri2_egl_surface *dri2_surf = loaderPrivate;
65
66 dri2_surf->buffer_count = 1;
67 if (width)
68 *width = dri2_surf->base.Width;
69 if (height)
70 *height = dri2_surf->base.Height;
71 *out_count = dri2_surf->buffer_count;;
72 return dri2_surf->buffers;
73 }
74
75 #define DRM_RENDER_DEV_NAME "%s/renderD%d"
76
77 EGLBoolean
78 dri2_initialize_surfaceless(_EGLDriver *drv, _EGLDisplay *disp)
79 {
80 struct dri2_egl_display *dri2_dpy;
81 const char* err;
82 int i;
83 int driver_loaded = 0;
84
85 loader_set_logger(_eglLog);
86
87 dri2_dpy = calloc(1, sizeof *dri2_dpy);
88 if (!dri2_dpy)
89 return _eglError(EGL_BAD_ALLOC, "eglInitialize");
90
91 disp->DriverData = (void *) dri2_dpy;
92
93 const int limit = 64;
94 const int base = 128;
95 for (i = 0; i < limit; ++i) {
96 char *card_path;
97 if (asprintf(&card_path, DRM_RENDER_DEV_NAME, DRM_DIR_NAME, base + i) < 0)
98 continue;
99
100 dri2_dpy->fd = loader_open_device(card_path);
101
102 free(card_path);
103 if (dri2_dpy->fd < 0)
104 continue;
105
106 dri2_dpy->driver_name = loader_get_driver_for_fd(dri2_dpy->fd, 0);
107 if (dri2_dpy->driver_name) {
108 if (dri2_load_driver(disp)) {
109 driver_loaded = 1;
110 break;
111 }
112 free(dri2_dpy->driver_name);
113 }
114 close(dri2_dpy->fd);
115 }
116
117 if (!driver_loaded) {
118 err = "DRI2: failed to load driver";
119 goto cleanup_display;
120 }
121
122 dri2_dpy->dri2_loader_extension.base.name = __DRI_DRI2_LOADER;
123 dri2_dpy->dri2_loader_extension.base.version = 3;
124 dri2_dpy->dri2_loader_extension.getBuffers = NULL;
125 dri2_dpy->dri2_loader_extension.flushFrontBuffer =
126 surfaceless_flush_front_buffer;
127 dri2_dpy->dri2_loader_extension.getBuffersWithFormat =
128 surfaceless_get_buffers_with_format;
129
130 dri2_dpy->extensions[0] = &dri2_dpy->dri2_loader_extension.base;
131 dri2_dpy->extensions[1] = &image_lookup_extension.base;
132 dri2_dpy->extensions[2] = &use_invalidate.base;
133 dri2_dpy->extensions[3] = NULL;
134
135 if (!dri2_create_screen(disp)) {
136 err = "DRI2: failed to create screen";
137 goto cleanup_driver;
138 }
139
140 for (i = 0; dri2_dpy->driver_configs[i]; i++) {
141 dri2_add_config(disp, dri2_dpy->driver_configs[i],
142 i + 1, EGL_WINDOW_BIT, NULL, NULL);
143 }
144
145 disp->Extensions.KHR_image_base = EGL_TRUE;
146
147 /* Fill vtbl last to prevent accidentally calling virtual function during
148 * initialization.
149 */
150 dri2_dpy->vtbl = &dri2_surfaceless_display_vtbl;
151
152 return EGL_TRUE;
153
154 cleanup_driver:
155 dlclose(dri2_dpy->driver);
156 free(dri2_dpy->driver_name);
157 close(dri2_dpy->fd);
158 cleanup_display:
159 free(dri2_dpy);
160
161 return _eglError(EGL_NOT_INITIALIZED, err);
162 }