1 /*
2 * Copyright © 2008-2011 Kristian Høgsberg
3 * Copyright © 2011 Intel Corporation
4 * Copyright © 2017, 2018 Collabora, Ltd.
5 * Copyright © 2017, 2018 General Electric Company
6 * Copyright (c) 2018 DisplayLink (UK) Ltd.
7 *
8 * Permission is hereby granted, free of charge, to any person obtaining
9 * a copy of this software and associated documentation files (the
10 * "Software"), to deal in the Software without restriction, including
11 * without limitation the rights to use, copy, modify, merge, publish,
12 * distribute, sublicense, and/or sell copies of the Software, and to
13 * permit persons to whom the Software is furnished to do so, subject to
14 * the following conditions:
15 *
16 * The above copyright notice and this permission notice (including the
17 * next paragraph) shall be included in all copies or substantial
18 * portions of the Software.
19 *
20 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
21 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
22 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
23 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
24 * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
25 * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
26 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
27 * SOFTWARE.
28 */
29
30 #include "config.h"
31
32 #include <errno.h>
33 #include <stdint.h>
34 #include <stdlib.h>
35 #include <ctype.h>
36 #include <string.h>
37 #include <dlfcn.h>
38
39 #include "drm-internal.h"
40 #include "pixman-renderer.h"
41 #include "pixel-formats.h"
42 #include "renderer-gl/gl-renderer.h"
43 #include "shared/weston-egl-ext.h"
44 #include "linux-dmabuf.h"
45 #include "linux-explicit-synchronization.h"
46
47 struct gl_renderer_interface *gl_renderer;
48
49 static struct gbm_device *
create_gbm_device(int fd)50 create_gbm_device(int fd)
51 {
52 struct gbm_device *gbm;
53
54 gl_renderer = weston_load_module("gl-renderer.so",
55 "gl_renderer_interface");
56 if (!gl_renderer)
57 return NULL;
58
59 /* GBM will load a dri driver, but even though they need symbols from
60 * libglapi, in some version of Mesa they are not linked to it. Since
61 * only the gl-renderer module links to it, the call above won't make
62 * these symbols globally available, and loading the DRI driver fails.
63 * Workaround this by dlopen()'ing libglapi with RTLD_GLOBAL. */
64 dlopen("libglapi.so.0", RTLD_LAZY | RTLD_GLOBAL);
65
66 gbm = gbm_create_device(fd);
67
68 return gbm;
69 }
70
71 /* When initializing EGL, if the preferred buffer format isn't available
72 * we may be able to substitute an ARGB format for an XRGB one.
73 *
74 * This returns 0 if substitution isn't possible, but 0 might be a
75 * legitimate format for other EGL platforms, so the caller is
76 * responsible for checking for 0 before calling gl_renderer->create().
77 *
78 * This works around https://bugs.freedesktop.org/show_bug.cgi?id=89689
79 * but it's entirely possible we'll see this again on other implementations.
80 */
81 static uint32_t
fallback_format_for(uint32_t format)82 fallback_format_for(uint32_t format)
83 {
84 const struct pixel_format_info *pf;
85
86 pf = pixel_format_get_info_by_opaque_substitute(format);
87 if (!pf)
88 return 0;
89
90 return pf->format;
91 }
92
93 static int
drm_backend_create_gl_renderer(struct drm_backend * b)94 drm_backend_create_gl_renderer(struct drm_backend *b)
95 {
96 uint32_t format[3] = {
97 b->gbm_format,
98 fallback_format_for(b->gbm_format),
99 0,
100 };
101 struct gl_renderer_display_options options = {
102 .egl_platform = EGL_PLATFORM_GBM_KHR,
103 .egl_native_display = b->gbm,
104 .egl_surface_type = EGL_WINDOW_BIT,
105 .drm_formats = format,
106 .drm_formats_count = 2,
107 };
108
109 if (format[1])
110 options.drm_formats_count = 3;
111
112 if (gl_renderer->display_create(b->compositor, &options) < 0)
113 return -1;
114
115 return 0;
116 }
117
118 int
init_egl(struct drm_backend * b)119 init_egl(struct drm_backend *b)
120 {
121 b->gbm = create_gbm_device(b->drm.fd);
122
123 if (!b->gbm)
124 return -1;
125
126 if (drm_backend_create_gl_renderer(b) < 0) {
127 gbm_device_destroy(b->gbm);
128 return -1;
129 }
130
131 return 0;
132 }
133
drm_output_fini_cursor_egl(struct drm_output * output)134 static void drm_output_fini_cursor_egl(struct drm_output *output)
135 {
136 unsigned int i;
137
138 for (i = 0; i < ARRAY_LENGTH(output->gbm_cursor_fb); i++) {
139 drm_fb_unref(output->gbm_cursor_fb[i]);
140 output->gbm_cursor_fb[i] = NULL;
141 }
142 }
143
144 static int
drm_output_init_cursor_egl(struct drm_output * output,struct drm_backend * b)145 drm_output_init_cursor_egl(struct drm_output *output, struct drm_backend *b)
146 {
147 unsigned int i;
148
149 /* No point creating cursors if we don't have a plane for them. */
150 if (!output->cursor_plane)
151 return 0;
152
153 for (i = 0; i < ARRAY_LENGTH(output->gbm_cursor_fb); i++) {
154 struct gbm_bo *bo;
155
156 bo = gbm_bo_create(b->gbm, b->cursor_width, b->cursor_height,
157 GBM_FORMAT_ARGB8888,
158 GBM_BO_USE_CURSOR | GBM_BO_USE_WRITE);
159 if (!bo)
160 goto err;
161
162 output->gbm_cursor_fb[i] =
163 drm_fb_get_from_bo(bo, b, false, BUFFER_CURSOR);
164 if (!output->gbm_cursor_fb[i]) {
165 gbm_bo_destroy(bo);
166 goto err;
167 }
168 output->gbm_cursor_handle[i] = gbm_bo_get_handle(bo).s32;
169 }
170
171 return 0;
172
173 err:
174 weston_log("cursor buffers unavailable, using gl cursors\n");
175 b->cursors_are_broken = true;
176 drm_output_fini_cursor_egl(output);
177 return -1;
178 }
179
180 /* Init output state that depends on gl or gbm */
181 int
drm_output_init_egl(struct drm_output * output,struct drm_backend * b)182 drm_output_init_egl(struct drm_output *output, struct drm_backend *b)
183 {
184 uint32_t format[2] = {
185 output->gbm_format,
186 fallback_format_for(output->gbm_format),
187 };
188 struct gl_renderer_output_options options = {
189 .drm_formats = format,
190 .drm_formats_count = 1,
191 };
192 struct weston_mode *mode = output->base.current_mode;
193 struct drm_plane *plane = output->scanout_plane;
194 unsigned int i;
195
196 assert(output->gbm_surface == NULL);
197
198 for (i = 0; i < plane->count_formats; i++) {
199 if (plane->formats[i].format == output->gbm_format)
200 break;
201 }
202
203 if (i == plane->count_formats) {
204 weston_log("format 0x%x not supported by output %s\n",
205 output->gbm_format, output->base.name);
206 return -1;
207 }
208
209 #ifdef HAVE_GBM_MODIFIERS
210 if (plane->formats[i].count_modifiers > 0) {
211 output->gbm_surface =
212 gbm_surface_create_with_modifiers(b->gbm,
213 mode->width,
214 mode->height,
215 output->gbm_format,
216 plane->formats[i].modifiers,
217 plane->formats[i].count_modifiers);
218 }
219
220 /* If allocating with modifiers fails, try again without. This can
221 * happen when the KMS display device supports modifiers but the
222 * GBM driver does not, e.g. the old i915 Mesa driver. */
223 if (!output->gbm_surface)
224 #endif
225 {
226 output->gbm_surface =
227 gbm_surface_create(b->gbm, mode->width, mode->height,
228 output->gbm_format,
229 output->gbm_bo_flags);
230 }
231
232 if (!output->gbm_surface) {
233 weston_log("failed to create gbm surface\n");
234 return -1;
235 }
236
237 if (options.drm_formats[1])
238 options.drm_formats_count = 2;
239 options.window_for_legacy = (EGLNativeWindowType) output->gbm_surface;
240 options.window_for_platform = output->gbm_surface;
241 if (gl_renderer->output_window_create(&output->base, &options) < 0) {
242 weston_log("failed to create gl renderer output state\n");
243 gbm_surface_destroy(output->gbm_surface);
244 output->gbm_surface = NULL;
245 return -1;
246 }
247
248 drm_output_init_cursor_egl(output, b);
249
250 return 0;
251 }
252
253 void
drm_output_fini_egl(struct drm_output * output)254 drm_output_fini_egl(struct drm_output *output)
255 {
256 struct drm_backend *b = to_drm_backend(output->base.compositor);
257
258 /* Destroying the GBM surface will destroy all our GBM buffers,
259 * regardless of refcount. Ensure we destroy them here. */
260 if (!b->shutting_down &&
261 output->scanout_plane->state_cur->fb &&
262 output->scanout_plane->state_cur->fb->type == BUFFER_GBM_SURFACE) {
263 drm_plane_reset_state(output->scanout_plane);
264 }
265
266 gl_renderer->output_destroy(&output->base);
267 gbm_surface_destroy(output->gbm_surface);
268 output->gbm_surface = NULL;
269 drm_output_fini_cursor_egl(output);
270 }
271
272 struct drm_fb *
drm_output_render_gl(struct drm_output_state * state,pixman_region32_t * damage)273 drm_output_render_gl(struct drm_output_state *state, pixman_region32_t *damage)
274 {
275 struct drm_output *output = state->output;
276 struct drm_backend *b = to_drm_backend(output->base.compositor);
277 struct gbm_bo *bo;
278 struct drm_fb *ret;
279
280 output->base.compositor->gpu_renderer->repaint_output(&output->base,
281 damage);
282
283 bo = gbm_surface_lock_front_buffer(output->gbm_surface);
284 if (!bo) {
285 weston_log("failed to lock front buffer: %s\n",
286 strerror(errno));
287 return NULL;
288 }
289
290 /* The renderer always produces an opaque image. */
291 ret = drm_fb_get_from_bo(bo, b, true, BUFFER_GBM_SURFACE);
292 if (!ret) {
293 weston_log("failed to get drm_fb for bo\n");
294 gbm_surface_release_buffer(output->gbm_surface, bo);
295 return NULL;
296 }
297 ret->gbm_surface = output->gbm_surface;
298
299 return ret;
300 }
301
302 static void
switch_to_gl_renderer(struct drm_backend * b)303 switch_to_gl_renderer(struct drm_backend *b)
304 {
305 struct drm_output *output;
306 bool dmabuf_support_inited;
307 bool linux_explicit_sync_inited;
308
309 if (!b->use_pixman)
310 return;
311
312 dmabuf_support_inited = !!b->compositor->gpu_renderer->import_dmabuf;
313 linux_explicit_sync_inited =
314 b->compositor->capabilities & WESTON_CAP_EXPLICIT_SYNC;
315
316 weston_log("Switching to GL renderer\n");
317
318 b->gbm = create_gbm_device(b->drm.fd);
319 if (!b->gbm) {
320 weston_log("Failed to create gbm device. "
321 "Aborting renderer switch\n");
322 return;
323 }
324
325 wl_list_for_each(output, &b->compositor->output_list, base.link)
326 pixman_renderer_output_destroy(&output->base);
327
328 b->compositor->gpu_renderer->destroy(b->compositor);
329
330 if (drm_backend_create_gl_renderer(b) < 0) {
331 gbm_device_destroy(b->gbm);
332 weston_log("Failed to create GL renderer. Quitting.\n");
333 /* FIXME: we need a function to shutdown cleanly */
334 assert(0);
335 }
336
337 wl_list_for_each(output, &b->compositor->output_list, base.link)
338 drm_output_init_egl(output, b);
339
340 b->use_pixman = 0;
341
342 if (!dmabuf_support_inited && b->compositor->gpu_renderer->import_dmabuf) {
343 if (linux_dmabuf_setup(b->compositor) < 0)
344 weston_log("Error: initializing dmabuf "
345 "support failed.\n");
346 }
347
348 if (!linux_explicit_sync_inited &&
349 (b->compositor->capabilities & WESTON_CAP_EXPLICIT_SYNC)) {
350 if (linux_explicit_synchronization_setup(b->compositor) < 0)
351 weston_log("Error: initializing explicit "
352 " synchronization support failed.\n");
353 }
354 }
355
356 void
renderer_switch_binding(struct weston_keyboard * keyboard,const struct timespec * time,uint32_t key,void * data)357 renderer_switch_binding(struct weston_keyboard *keyboard,
358 const struct timespec *time, uint32_t key, void *data)
359 {
360 struct drm_backend *b =
361 to_drm_backend(keyboard->seat->compositor);
362
363 switch_to_gl_renderer(b);
364 }
365
366