blob: 1d35b4934b4b5e7f127fdf5f98775fafd5e31644 [file] [log] [blame]
Daniel Stone7580b3c2019-06-18 11:16:53 +01001/*
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 <stdint.h>
33
34#include <xf86drm.h>
35#include <xf86drmMode.h>
Daniel Stone7580b3c2019-06-18 11:16:53 +010036
Daniel Stone7580b3c2019-06-18 11:16:53 +010037#include <libweston/libweston.h>
38#include <libweston/backend-drm.h>
39#include <libweston/pixel-formats.h>
40#include <libweston/linux-dmabuf.h>
41#include "shared/helpers.h"
Pekka Paalanen4b301fe2021-02-04 17:39:45 +020042#include "shared/weston-drm-fourcc.h"
Daniel Stone7580b3c2019-06-18 11:16:53 +010043#include "drm-internal.h"
44#include "linux-dmabuf.h"
45
46static void
47drm_fb_destroy(struct drm_fb *fb)
48{
49 if (fb->fb_id != 0)
50 drmModeRmFB(fb->fd, fb->fb_id);
51 weston_buffer_reference(&fb->buffer_ref, NULL);
52 weston_buffer_release_reference(&fb->buffer_release_ref, NULL);
53 free(fb);
54}
55
56static void
57drm_fb_destroy_dumb(struct drm_fb *fb)
58{
59 struct drm_mode_destroy_dumb destroy_arg;
60
61 assert(fb->type == BUFFER_PIXMAN_DUMB);
62
63 if (fb->map && fb->size > 0)
64 munmap(fb->map, fb->size);
65
66 memset(&destroy_arg, 0, sizeof(destroy_arg));
67 destroy_arg.handle = fb->handles[0];
68 drmIoctl(fb->fd, DRM_IOCTL_MODE_DESTROY_DUMB, &destroy_arg);
69
70 drm_fb_destroy(fb);
71}
72
Daniel Stone7580b3c2019-06-18 11:16:53 +010073static int
74drm_fb_addfb(struct drm_backend *b, struct drm_fb *fb)
75{
76 int ret = -EINVAL;
Daniel Stone7580b3c2019-06-18 11:16:53 +010077 uint64_t mods[4] = { };
78 size_t i;
Daniel Stone7580b3c2019-06-18 11:16:53 +010079
80 /* If we have a modifier set, we must only use the WithModifiers
81 * entrypoint; we cannot import it through legacy ioctls. */
82 if (b->fb_modifiers && fb->modifier != DRM_FORMAT_MOD_INVALID) {
83 /* KMS demands that if a modifier is set, it must be the same
84 * for all planes. */
Daniel Stone7580b3c2019-06-18 11:16:53 +010085 for (i = 0; i < ARRAY_LENGTH(mods) && fb->handles[i]; i++)
86 mods[i] = fb->modifier;
87 ret = drmModeAddFB2WithModifiers(fb->fd, fb->width, fb->height,
88 fb->format->format,
89 fb->handles, fb->strides,
90 fb->offsets, mods, &fb->fb_id,
91 DRM_MODE_FB_MODIFIERS);
Daniel Stone7580b3c2019-06-18 11:16:53 +010092 return ret;
93 }
94
95 ret = drmModeAddFB2(fb->fd, fb->width, fb->height, fb->format->format,
96 fb->handles, fb->strides, fb->offsets, &fb->fb_id,
97 0);
98 if (ret == 0)
99 return 0;
100
101 /* Legacy AddFB can't always infer the format from depth/bpp alone, so
102 * check if our format is one of the lucky ones. */
103 if (!fb->format->depth || !fb->format->bpp)
104 return ret;
105
106 /* Cannot fall back to AddFB for multi-planar formats either. */
107 if (fb->handles[1] || fb->handles[2] || fb->handles[3])
108 return ret;
109
110 ret = drmModeAddFB(fb->fd, fb->width, fb->height,
111 fb->format->depth, fb->format->bpp,
112 fb->strides[0], fb->handles[0], &fb->fb_id);
113 return ret;
114}
115
116struct drm_fb *
117drm_fb_create_dumb(struct drm_backend *b, int width, int height,
118 uint32_t format)
119{
120 struct drm_fb *fb;
121 int ret;
122
123 struct drm_mode_create_dumb create_arg;
124 struct drm_mode_destroy_dumb destroy_arg;
125 struct drm_mode_map_dumb map_arg;
126
127 fb = zalloc(sizeof *fb);
128 if (!fb)
129 return NULL;
130 fb->refcnt = 1;
131
132 fb->format = pixel_format_get_info(format);
133 if (!fb->format) {
134 weston_log("failed to look up format 0x%lx\n",
135 (unsigned long) format);
136 goto err_fb;
137 }
138
139 if (!fb->format->depth || !fb->format->bpp) {
140 weston_log("format 0x%lx is not compatible with dumb buffers\n",
141 (unsigned long) format);
142 goto err_fb;
143 }
144
145 memset(&create_arg, 0, sizeof create_arg);
146 create_arg.bpp = fb->format->bpp;
147 create_arg.width = width;
148 create_arg.height = height;
149
150 ret = drmIoctl(b->drm.fd, DRM_IOCTL_MODE_CREATE_DUMB, &create_arg);
151 if (ret)
152 goto err_fb;
153
154 fb->type = BUFFER_PIXMAN_DUMB;
155 fb->modifier = DRM_FORMAT_MOD_INVALID;
156 fb->handles[0] = create_arg.handle;
157 fb->strides[0] = create_arg.pitch;
158 fb->num_planes = 1;
159 fb->size = create_arg.size;
160 fb->width = width;
161 fb->height = height;
162 fb->fd = b->drm.fd;
163
164 if (drm_fb_addfb(b, fb) != 0) {
165 weston_log("failed to create kms fb: %s\n", strerror(errno));
166 goto err_bo;
167 }
168
169 memset(&map_arg, 0, sizeof map_arg);
170 map_arg.handle = fb->handles[0];
171 ret = drmIoctl(fb->fd, DRM_IOCTL_MODE_MAP_DUMB, &map_arg);
172 if (ret)
173 goto err_add_fb;
174
175 fb->map = mmap(NULL, fb->size, PROT_WRITE,
176 MAP_SHARED, b->drm.fd, map_arg.offset);
177 if (fb->map == MAP_FAILED)
178 goto err_add_fb;
179
180 return fb;
181
182err_add_fb:
183 drmModeRmFB(b->drm.fd, fb->fb_id);
184err_bo:
185 memset(&destroy_arg, 0, sizeof(destroy_arg));
186 destroy_arg.handle = create_arg.handle;
187 drmIoctl(b->drm.fd, DRM_IOCTL_MODE_DESTROY_DUMB, &destroy_arg);
188err_fb:
189 free(fb);
190 return NULL;
191}
192
193struct drm_fb *
194drm_fb_ref(struct drm_fb *fb)
195{
196 fb->refcnt++;
197 return fb;
198}
199
Stefan Agnerccf24072019-07-09 22:02:00 +0200200#ifdef BUILD_DRM_GBM
201static void
202drm_fb_destroy_gbm(struct gbm_bo *bo, void *data)
203{
204 struct drm_fb *fb = data;
205
206 assert(fb->type == BUFFER_GBM_SURFACE || fb->type == BUFFER_CLIENT ||
207 fb->type == BUFFER_CURSOR);
208 drm_fb_destroy(fb);
209}
210
Daniel Stone7580b3c2019-06-18 11:16:53 +0100211static void
212drm_fb_destroy_dmabuf(struct drm_fb *fb)
213{
214 /* We deliberately do not close the GEM handles here; GBM manages
215 * their lifetime through the BO. */
216 if (fb->bo)
217 gbm_bo_destroy(fb->bo);
218 drm_fb_destroy(fb);
219}
220
221static struct drm_fb *
222drm_fb_get_from_dmabuf(struct linux_dmabuf_buffer *dmabuf,
223 struct drm_backend *backend, bool is_opaque)
224{
Leandro Ribeiro5aea1bc2021-04-21 10:38:58 -0300225#ifndef HAVE_GBM_FD_IMPORT
226 /* Importing a buffer to KMS requires explicit modifiers, so
227 * we can't continue with the legacy GBM_BO_IMPORT_FD instead
228 * of GBM_BO_IMPORT_FD_MODIFIER. */
229 return NULL;
230#else
Daniel Stone7580b3c2019-06-18 11:16:53 +0100231 struct drm_fb *fb;
Leandro Ribeiro5aea1bc2021-04-21 10:38:58 -0300232 int i;
Daniel Stone7580b3c2019-06-18 11:16:53 +0100233 struct gbm_import_fd_modifier_data import_mod = {
234 .width = dmabuf->attributes.width,
235 .height = dmabuf->attributes.height,
236 .format = dmabuf->attributes.format,
237 .num_fds = dmabuf->attributes.n_planes,
238 .modifier = dmabuf->attributes.modifier[0],
239 };
Nicholas Niro7aab7462019-10-29 22:13:09 -0400240
Emmanuel Gil Peyroteff793a2021-07-31 17:25:41 +0200241 /* We should not import to KMS a buffer that has been allocated using no
242 * modifiers. Usually drivers use linear layouts to allocate with no
243 * modifiers, but this is not a rule. The driver could use, for
244 * instance, a tiling layout under the hood - and both Weston and the
245 * KMS driver can't know. So giving the buffer to KMS is not safe, as
246 * not knowing its layout can result in garbage being displayed. In
247 * short, importing a buffer to KMS requires explicit modifiers. */
Leandro Ribeiro5aea1bc2021-04-21 10:38:58 -0300248 if (dmabuf->attributes.modifier[0] == DRM_FORMAT_MOD_INVALID)
249 return NULL;
Daniel Stone7580b3c2019-06-18 11:16:53 +0100250
251 /* XXX: TODO:
252 *
253 * Currently the buffer is rejected if any dmabuf attribute
254 * flag is set. This keeps us from passing an inverted /
255 * interlaced / bottom-first buffer (or any other type that may
256 * be added in the future) through to an overlay. Ultimately,
257 * these types of buffers should be handled through buffer
258 * transforms and not as spot-checks requiring specific
259 * knowledge. */
260 if (dmabuf->attributes.flags)
261 return NULL;
262
263 fb = zalloc(sizeof *fb);
264 if (fb == NULL)
265 return NULL;
266
267 fb->refcnt = 1;
268 fb->type = BUFFER_DMABUF;
269
Leandro Ribeiro3193ab52021-04-27 19:00:34 -0300270 ARRAY_COPY(import_mod.fds, dmabuf->attributes.fd);
271 ARRAY_COPY(import_mod.strides, dmabuf->attributes.stride);
272 ARRAY_COPY(import_mod.offsets, dmabuf->attributes.offset);
Daniel Stone7580b3c2019-06-18 11:16:53 +0100273
Leandro Ribeiro5aea1bc2021-04-21 10:38:58 -0300274 fb->bo = gbm_bo_import(backend->gbm, GBM_BO_IMPORT_FD_MODIFIER,
275 &import_mod, GBM_BO_USE_SCANOUT);
Daniel Stone7580b3c2019-06-18 11:16:53 +0100276 if (!fb->bo)
277 goto err_free;
278
279 fb->width = dmabuf->attributes.width;
280 fb->height = dmabuf->attributes.height;
281 fb->modifier = dmabuf->attributes.modifier[0];
282 fb->size = 0;
283 fb->fd = backend->drm.fd;
284
Leandro Ribeiro3193ab52021-04-27 19:00:34 -0300285 ARRAY_COPY(fb->strides, dmabuf->attributes.stride);
286 ARRAY_COPY(fb->offsets, dmabuf->attributes.offset);
Daniel Stone7580b3c2019-06-18 11:16:53 +0100287
288 fb->format = pixel_format_get_info(dmabuf->attributes.format);
289 if (!fb->format) {
290 weston_log("couldn't look up format info for 0x%lx\n",
291 (unsigned long) dmabuf->attributes.format);
292 goto err_free;
293 }
294
295 if (is_opaque)
296 fb->format = pixel_format_get_opaque_substitute(fb->format);
297
298 if (backend->min_width > fb->width ||
299 fb->width > backend->max_width ||
300 backend->min_height > fb->height ||
301 fb->height > backend->max_height) {
302 weston_log("bo geometry out of bounds\n");
303 goto err_free;
304 }
305
306 fb->num_planes = dmabuf->attributes.n_planes;
307 for (i = 0; i < dmabuf->attributes.n_planes; i++) {
308 union gbm_bo_handle handle;
309
310 handle = gbm_bo_get_handle_for_plane(fb->bo, i);
311 if (handle.s32 == -1)
312 goto err_free;
313 fb->handles[i] = handle.u32;
314 }
315
316 if (drm_fb_addfb(backend, fb) != 0)
317 goto err_free;
318
319 return fb;
320
321err_free:
322 drm_fb_destroy_dmabuf(fb);
Daniel Stone7580b3c2019-06-18 11:16:53 +0100323 return NULL;
Leandro Ribeiro5aea1bc2021-04-21 10:38:58 -0300324#endif
Daniel Stone7580b3c2019-06-18 11:16:53 +0100325}
326
327struct drm_fb *
328drm_fb_get_from_bo(struct gbm_bo *bo, struct drm_backend *backend,
329 bool is_opaque, enum drm_fb_type type)
330{
331 struct drm_fb *fb = gbm_bo_get_user_data(bo);
332#ifdef HAVE_GBM_MODIFIERS
333 int i;
334#endif
335
336 if (fb) {
337 assert(fb->type == type);
338 return drm_fb_ref(fb);
339 }
340
341 fb = zalloc(sizeof *fb);
342 if (fb == NULL)
343 return NULL;
344
345 fb->type = type;
346 fb->refcnt = 1;
347 fb->bo = bo;
348 fb->fd = backend->drm.fd;
349
350 fb->width = gbm_bo_get_width(bo);
351 fb->height = gbm_bo_get_height(bo);
352 fb->format = pixel_format_get_info(gbm_bo_get_format(bo));
353 fb->size = 0;
354
355#ifdef HAVE_GBM_MODIFIERS
356 fb->modifier = gbm_bo_get_modifier(bo);
357 fb->num_planes = gbm_bo_get_plane_count(bo);
358 for (i = 0; i < fb->num_planes; i++) {
359 fb->strides[i] = gbm_bo_get_stride_for_plane(bo, i);
360 fb->handles[i] = gbm_bo_get_handle_for_plane(bo, i).u32;
361 fb->offsets[i] = gbm_bo_get_offset(bo, i);
362 }
363#else
364 fb->num_planes = 1;
365 fb->strides[0] = gbm_bo_get_stride(bo);
366 fb->handles[0] = gbm_bo_get_handle(bo).u32;
367 fb->modifier = DRM_FORMAT_MOD_INVALID;
368#endif
369
370 if (!fb->format) {
371 weston_log("couldn't look up format 0x%lx\n",
372 (unsigned long) gbm_bo_get_format(bo));
373 goto err_free;
374 }
375
376 /* We can scanout an ARGB buffer if the surface's opaque region covers
377 * the whole output, but we have to use XRGB as the KMS format code. */
378 if (is_opaque)
379 fb->format = pixel_format_get_opaque_substitute(fb->format);
380
381 if (backend->min_width > fb->width ||
382 fb->width > backend->max_width ||
383 backend->min_height > fb->height ||
384 fb->height > backend->max_height) {
385 weston_log("bo geometry out of bounds\n");
386 goto err_free;
387 }
388
389 if (drm_fb_addfb(backend, fb) != 0) {
390 if (type == BUFFER_GBM_SURFACE)
391 weston_log("failed to create kms fb: %s\n",
392 strerror(errno));
393 goto err_free;
394 }
395
396 gbm_bo_set_user_data(bo, fb, drm_fb_destroy_gbm);
397
398 return fb;
399
400err_free:
401 free(fb);
402 return NULL;
403}
404
405static void
406drm_fb_set_buffer(struct drm_fb *fb, struct weston_buffer *buffer,
407 struct weston_buffer_release *buffer_release)
408{
409 assert(fb->buffer_ref.buffer == NULL);
410 assert(fb->type == BUFFER_CLIENT || fb->type == BUFFER_DMABUF);
411 weston_buffer_reference(&fb->buffer_ref, buffer);
412 weston_buffer_release_reference(&fb->buffer_release_ref,
413 buffer_release);
414}
Stefan Agnerccf24072019-07-09 22:02:00 +0200415#endif
Daniel Stone7580b3c2019-06-18 11:16:53 +0100416
417void
418drm_fb_unref(struct drm_fb *fb)
419{
420 if (!fb)
421 return;
422
423 assert(fb->refcnt > 0);
424 if (--fb->refcnt > 0)
425 return;
426
427 switch (fb->type) {
428 case BUFFER_PIXMAN_DUMB:
429 drm_fb_destroy_dumb(fb);
430 break;
Stefan Agnerccf24072019-07-09 22:02:00 +0200431#ifdef BUILD_DRM_GBM
Daniel Stone7580b3c2019-06-18 11:16:53 +0100432 case BUFFER_CURSOR:
433 case BUFFER_CLIENT:
434 gbm_bo_destroy(fb->bo);
435 break;
436 case BUFFER_GBM_SURFACE:
437 gbm_surface_release_buffer(fb->gbm_surface, fb->bo);
438 break;
439 case BUFFER_DMABUF:
440 drm_fb_destroy_dmabuf(fb);
441 break;
Stefan Agnerccf24072019-07-09 22:02:00 +0200442#endif
Daniel Stone7580b3c2019-06-18 11:16:53 +0100443 default:
444 assert(NULL);
445 break;
446 }
447}
448
Stefan Agnerccf24072019-07-09 22:02:00 +0200449#ifdef BUILD_DRM_GBM
Marius Vlad81bada52019-11-11 00:27:17 +0200450bool
451drm_can_scanout_dmabuf(struct weston_compositor *ec,
452 struct linux_dmabuf_buffer *dmabuf)
453{
454 struct drm_fb *fb;
455 struct drm_backend *b = to_drm_backend(ec);
456 bool ret = false;
457
458 fb = drm_fb_get_from_dmabuf(dmabuf, b, true);
459 if (fb)
460 ret = true;
461
462 drm_fb_unref(fb);
463 drm_debug(b, "[dmabuf] dmabuf %p, import test %s\n", dmabuf,
464 ret ? "succeeded" : "failed");
465 return ret;
466}
467
Daniel Stone7580b3c2019-06-18 11:16:53 +0100468struct drm_fb *
469drm_fb_get_from_view(struct drm_output_state *state, struct weston_view *ev)
470{
471 struct drm_output *output = state->output;
472 struct drm_backend *b = to_drm_backend(output->base.compositor);
473 struct weston_buffer *buffer = ev->surface->buffer_ref.buffer;
474 bool is_opaque = weston_view_is_opaque(ev, &ev->transform.boundingbox);
475 struct linux_dmabuf_buffer *dmabuf;
476 struct drm_fb *fb;
477
478 if (ev->alpha != 1.0f)
479 return NULL;
480
481 if (!drm_view_transform_supported(ev, &output->base))
482 return NULL;
483
Daniel Stoned32dfcf2019-08-19 16:53:40 +0100484 if (ev->surface->protection_mode == WESTON_SURFACE_PROTECTION_MODE_ENFORCED &&
485 ev->surface->desired_protection > output->base.current_protection)
486 return NULL;
487
Daniel Stone7580b3c2019-06-18 11:16:53 +0100488 if (!buffer)
489 return NULL;
490
491 if (wl_shm_buffer_get(buffer->resource))
492 return NULL;
493
494 /* GBM is used for dmabuf import as well as from client wl_buffer. */
495 if (!b->gbm)
496 return NULL;
497
498 dmabuf = linux_dmabuf_buffer_get(buffer->resource);
499 if (dmabuf) {
500 fb = drm_fb_get_from_dmabuf(dmabuf, b, is_opaque);
501 if (!fb)
502 return NULL;
503 } else {
504 struct gbm_bo *bo;
505
506 bo = gbm_bo_import(b->gbm, GBM_BO_IMPORT_WL_BUFFER,
507 buffer->resource, GBM_BO_USE_SCANOUT);
508 if (!bo)
509 return NULL;
510
511 fb = drm_fb_get_from_bo(bo, b, is_opaque, BUFFER_CLIENT);
512 if (!fb) {
513 gbm_bo_destroy(bo);
514 return NULL;
515 }
516 }
517
518 drm_debug(b, "\t\t\t[view] view %p format: %s\n",
519 ev, fb->format->drm_format_name);
520 drm_fb_set_buffer(fb, buffer,
521 ev->surface->buffer_release_ref.buffer_release);
522 return fb;
523}
Stefan Agnerccf24072019-07-09 22:02:00 +0200524#endif