blob: ffe2cc52cc0e9b64c660f77d08768b49903d3d4d [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);
Daniel Stone7580b3c2019-06-18 11:16:53 +010051 free(fb);
52}
53
54static void
55drm_fb_destroy_dumb(struct drm_fb *fb)
56{
57 struct drm_mode_destroy_dumb destroy_arg;
58
59 assert(fb->type == BUFFER_PIXMAN_DUMB);
60
61 if (fb->map && fb->size > 0)
62 munmap(fb->map, fb->size);
63
64 memset(&destroy_arg, 0, sizeof(destroy_arg));
65 destroy_arg.handle = fb->handles[0];
66 drmIoctl(fb->fd, DRM_IOCTL_MODE_DESTROY_DUMB, &destroy_arg);
67
68 drm_fb_destroy(fb);
69}
70
Daniel Stone7580b3c2019-06-18 11:16:53 +010071static int
72drm_fb_addfb(struct drm_backend *b, struct drm_fb *fb)
73{
74 int ret = -EINVAL;
Daniel Stone7580b3c2019-06-18 11:16:53 +010075 uint64_t mods[4] = { };
76 size_t i;
Daniel Stone7580b3c2019-06-18 11:16:53 +010077
78 /* If we have a modifier set, we must only use the WithModifiers
79 * entrypoint; we cannot import it through legacy ioctls. */
80 if (b->fb_modifiers && fb->modifier != DRM_FORMAT_MOD_INVALID) {
81 /* KMS demands that if a modifier is set, it must be the same
82 * for all planes. */
Daniel Stone7580b3c2019-06-18 11:16:53 +010083 for (i = 0; i < ARRAY_LENGTH(mods) && fb->handles[i]; i++)
84 mods[i] = fb->modifier;
85 ret = drmModeAddFB2WithModifiers(fb->fd, fb->width, fb->height,
86 fb->format->format,
87 fb->handles, fb->strides,
88 fb->offsets, mods, &fb->fb_id,
89 DRM_MODE_FB_MODIFIERS);
Daniel Stone7580b3c2019-06-18 11:16:53 +010090 return ret;
91 }
92
93 ret = drmModeAddFB2(fb->fd, fb->width, fb->height, fb->format->format,
94 fb->handles, fb->strides, fb->offsets, &fb->fb_id,
95 0);
96 if (ret == 0)
97 return 0;
98
99 /* Legacy AddFB can't always infer the format from depth/bpp alone, so
100 * check if our format is one of the lucky ones. */
101 if (!fb->format->depth || !fb->format->bpp)
102 return ret;
103
104 /* Cannot fall back to AddFB for multi-planar formats either. */
105 if (fb->handles[1] || fb->handles[2] || fb->handles[3])
106 return ret;
107
108 ret = drmModeAddFB(fb->fd, fb->width, fb->height,
109 fb->format->depth, fb->format->bpp,
110 fb->strides[0], fb->handles[0], &fb->fb_id);
111 return ret;
112}
113
114struct drm_fb *
115drm_fb_create_dumb(struct drm_backend *b, int width, int height,
116 uint32_t format)
117{
118 struct drm_fb *fb;
119 int ret;
120
121 struct drm_mode_create_dumb create_arg;
122 struct drm_mode_destroy_dumb destroy_arg;
123 struct drm_mode_map_dumb map_arg;
124
125 fb = zalloc(sizeof *fb);
126 if (!fb)
127 return NULL;
128 fb->refcnt = 1;
129
130 fb->format = pixel_format_get_info(format);
131 if (!fb->format) {
132 weston_log("failed to look up format 0x%lx\n",
133 (unsigned long) format);
134 goto err_fb;
135 }
136
137 if (!fb->format->depth || !fb->format->bpp) {
138 weston_log("format 0x%lx is not compatible with dumb buffers\n",
139 (unsigned long) format);
140 goto err_fb;
141 }
142
143 memset(&create_arg, 0, sizeof create_arg);
144 create_arg.bpp = fb->format->bpp;
145 create_arg.width = width;
146 create_arg.height = height;
147
148 ret = drmIoctl(b->drm.fd, DRM_IOCTL_MODE_CREATE_DUMB, &create_arg);
149 if (ret)
150 goto err_fb;
151
152 fb->type = BUFFER_PIXMAN_DUMB;
153 fb->modifier = DRM_FORMAT_MOD_INVALID;
154 fb->handles[0] = create_arg.handle;
155 fb->strides[0] = create_arg.pitch;
156 fb->num_planes = 1;
157 fb->size = create_arg.size;
158 fb->width = width;
159 fb->height = height;
160 fb->fd = b->drm.fd;
161
162 if (drm_fb_addfb(b, fb) != 0) {
163 weston_log("failed to create kms fb: %s\n", strerror(errno));
164 goto err_bo;
165 }
166
167 memset(&map_arg, 0, sizeof map_arg);
168 map_arg.handle = fb->handles[0];
169 ret = drmIoctl(fb->fd, DRM_IOCTL_MODE_MAP_DUMB, &map_arg);
170 if (ret)
171 goto err_add_fb;
172
173 fb->map = mmap(NULL, fb->size, PROT_WRITE,
174 MAP_SHARED, b->drm.fd, map_arg.offset);
175 if (fb->map == MAP_FAILED)
176 goto err_add_fb;
177
178 return fb;
179
180err_add_fb:
181 drmModeRmFB(b->drm.fd, fb->fb_id);
182err_bo:
183 memset(&destroy_arg, 0, sizeof(destroy_arg));
184 destroy_arg.handle = create_arg.handle;
185 drmIoctl(b->drm.fd, DRM_IOCTL_MODE_DESTROY_DUMB, &destroy_arg);
186err_fb:
187 free(fb);
188 return NULL;
189}
190
191struct drm_fb *
192drm_fb_ref(struct drm_fb *fb)
193{
194 fb->refcnt++;
195 return fb;
196}
197
Stefan Agnerccf24072019-07-09 22:02:00 +0200198#ifdef BUILD_DRM_GBM
199static void
200drm_fb_destroy_gbm(struct gbm_bo *bo, void *data)
201{
202 struct drm_fb *fb = data;
203
204 assert(fb->type == BUFFER_GBM_SURFACE || fb->type == BUFFER_CLIENT ||
205 fb->type == BUFFER_CURSOR);
206 drm_fb_destroy(fb);
207}
208
Daniel Stone7580b3c2019-06-18 11:16:53 +0100209static void
210drm_fb_destroy_dmabuf(struct drm_fb *fb)
211{
212 /* We deliberately do not close the GEM handles here; GBM manages
213 * their lifetime through the BO. */
214 if (fb->bo)
215 gbm_bo_destroy(fb->bo);
216 drm_fb_destroy(fb);
217}
218
219static struct drm_fb *
220drm_fb_get_from_dmabuf(struct linux_dmabuf_buffer *dmabuf,
Leandro Ribeiro0a7034c2021-09-13 14:52:53 -0300221 struct drm_backend *backend, bool is_opaque,
222 uint32_t *try_view_on_plane_failure_reasons)
Daniel Stone7580b3c2019-06-18 11:16:53 +0100223{
Leandro Ribeiro5aea1bc2021-04-21 10:38:58 -0300224#ifndef HAVE_GBM_FD_IMPORT
225 /* Importing a buffer to KMS requires explicit modifiers, so
226 * we can't continue with the legacy GBM_BO_IMPORT_FD instead
227 * of GBM_BO_IMPORT_FD_MODIFIER. */
228 return NULL;
229#else
Daniel Stone7580b3c2019-06-18 11:16:53 +0100230 struct drm_fb *fb;
Leandro Ribeiro5aea1bc2021-04-21 10:38:58 -0300231 int i;
Daniel Stone7580b3c2019-06-18 11:16:53 +0100232 struct gbm_import_fd_modifier_data import_mod = {
233 .width = dmabuf->attributes.width,
234 .height = dmabuf->attributes.height,
235 .format = dmabuf->attributes.format,
236 .num_fds = dmabuf->attributes.n_planes,
237 .modifier = dmabuf->attributes.modifier[0],
238 };
Nicholas Niro7aab7462019-10-29 22:13:09 -0400239
Emmanuel Gil Peyroteff793a2021-07-31 17:25:41 +0200240 /* We should not import to KMS a buffer that has been allocated using no
241 * modifiers. Usually drivers use linear layouts to allocate with no
242 * modifiers, but this is not a rule. The driver could use, for
243 * instance, a tiling layout under the hood - and both Weston and the
244 * KMS driver can't know. So giving the buffer to KMS is not safe, as
245 * not knowing its layout can result in garbage being displayed. In
246 * short, importing a buffer to KMS requires explicit modifiers. */
Leandro Ribeiro0a7034c2021-09-13 14:52:53 -0300247 if (dmabuf->attributes.modifier[0] == DRM_FORMAT_MOD_INVALID) {
248 if (try_view_on_plane_failure_reasons)
249 *try_view_on_plane_failure_reasons |=
250 FAILURE_REASONS_DMABUF_MODIFIER_INVALID;
Leandro Ribeiro5aea1bc2021-04-21 10:38:58 -0300251 return NULL;
Leandro Ribeiro0a7034c2021-09-13 14:52:53 -0300252 }
Daniel Stone7580b3c2019-06-18 11:16:53 +0100253
254 /* XXX: TODO:
255 *
256 * Currently the buffer is rejected if any dmabuf attribute
257 * flag is set. This keeps us from passing an inverted /
258 * interlaced / bottom-first buffer (or any other type that may
259 * be added in the future) through to an overlay. Ultimately,
260 * these types of buffers should be handled through buffer
261 * transforms and not as spot-checks requiring specific
262 * knowledge. */
263 if (dmabuf->attributes.flags)
264 return NULL;
265
266 fb = zalloc(sizeof *fb);
267 if (fb == NULL)
268 return NULL;
269
270 fb->refcnt = 1;
271 fb->type = BUFFER_DMABUF;
272
Leandro Ribeiro3193ab52021-04-27 19:00:34 -0300273 ARRAY_COPY(import_mod.fds, dmabuf->attributes.fd);
274 ARRAY_COPY(import_mod.strides, dmabuf->attributes.stride);
275 ARRAY_COPY(import_mod.offsets, dmabuf->attributes.offset);
Daniel Stone7580b3c2019-06-18 11:16:53 +0100276
Leandro Ribeiro5aea1bc2021-04-21 10:38:58 -0300277 fb->bo = gbm_bo_import(backend->gbm, GBM_BO_IMPORT_FD_MODIFIER,
278 &import_mod, GBM_BO_USE_SCANOUT);
Daniel Stone7580b3c2019-06-18 11:16:53 +0100279 if (!fb->bo)
280 goto err_free;
281
282 fb->width = dmabuf->attributes.width;
283 fb->height = dmabuf->attributes.height;
284 fb->modifier = dmabuf->attributes.modifier[0];
285 fb->size = 0;
286 fb->fd = backend->drm.fd;
287
Leandro Ribeiro3193ab52021-04-27 19:00:34 -0300288 ARRAY_COPY(fb->strides, dmabuf->attributes.stride);
289 ARRAY_COPY(fb->offsets, dmabuf->attributes.offset);
Daniel Stone7580b3c2019-06-18 11:16:53 +0100290
291 fb->format = pixel_format_get_info(dmabuf->attributes.format);
292 if (!fb->format) {
293 weston_log("couldn't look up format info for 0x%lx\n",
294 (unsigned long) dmabuf->attributes.format);
295 goto err_free;
296 }
297
298 if (is_opaque)
299 fb->format = pixel_format_get_opaque_substitute(fb->format);
300
301 if (backend->min_width > fb->width ||
302 fb->width > backend->max_width ||
303 backend->min_height > fb->height ||
304 fb->height > backend->max_height) {
305 weston_log("bo geometry out of bounds\n");
306 goto err_free;
307 }
308
309 fb->num_planes = dmabuf->attributes.n_planes;
310 for (i = 0; i < dmabuf->attributes.n_planes; i++) {
311 union gbm_bo_handle handle;
312
313 handle = gbm_bo_get_handle_for_plane(fb->bo, i);
314 if (handle.s32 == -1)
315 goto err_free;
316 fb->handles[i] = handle.u32;
317 }
318
Leandro Ribeiro0a7034c2021-09-13 14:52:53 -0300319 if (drm_fb_addfb(backend, fb) != 0) {
320 if (try_view_on_plane_failure_reasons)
321 *try_view_on_plane_failure_reasons |=
322 FAILURE_REASONS_ADD_FB_FAILED;
Daniel Stone7580b3c2019-06-18 11:16:53 +0100323 goto err_free;
Leandro Ribeiro0a7034c2021-09-13 14:52:53 -0300324 }
Daniel Stone7580b3c2019-06-18 11:16:53 +0100325
326 return fb;
327
328err_free:
329 drm_fb_destroy_dmabuf(fb);
Daniel Stone7580b3c2019-06-18 11:16:53 +0100330 return NULL;
Leandro Ribeiro5aea1bc2021-04-21 10:38:58 -0300331#endif
Daniel Stone7580b3c2019-06-18 11:16:53 +0100332}
333
334struct drm_fb *
335drm_fb_get_from_bo(struct gbm_bo *bo, struct drm_backend *backend,
336 bool is_opaque, enum drm_fb_type type)
337{
338 struct drm_fb *fb = gbm_bo_get_user_data(bo);
339#ifdef HAVE_GBM_MODIFIERS
340 int i;
341#endif
342
343 if (fb) {
344 assert(fb->type == type);
345 return drm_fb_ref(fb);
346 }
347
348 fb = zalloc(sizeof *fb);
349 if (fb == NULL)
350 return NULL;
351
352 fb->type = type;
353 fb->refcnt = 1;
354 fb->bo = bo;
355 fb->fd = backend->drm.fd;
356
357 fb->width = gbm_bo_get_width(bo);
358 fb->height = gbm_bo_get_height(bo);
359 fb->format = pixel_format_get_info(gbm_bo_get_format(bo));
360 fb->size = 0;
361
362#ifdef HAVE_GBM_MODIFIERS
363 fb->modifier = gbm_bo_get_modifier(bo);
364 fb->num_planes = gbm_bo_get_plane_count(bo);
365 for (i = 0; i < fb->num_planes; i++) {
366 fb->strides[i] = gbm_bo_get_stride_for_plane(bo, i);
367 fb->handles[i] = gbm_bo_get_handle_for_plane(bo, i).u32;
368 fb->offsets[i] = gbm_bo_get_offset(bo, i);
369 }
370#else
371 fb->num_planes = 1;
372 fb->strides[0] = gbm_bo_get_stride(bo);
373 fb->handles[0] = gbm_bo_get_handle(bo).u32;
374 fb->modifier = DRM_FORMAT_MOD_INVALID;
375#endif
376
377 if (!fb->format) {
378 weston_log("couldn't look up format 0x%lx\n",
379 (unsigned long) gbm_bo_get_format(bo));
380 goto err_free;
381 }
382
383 /* We can scanout an ARGB buffer if the surface's opaque region covers
384 * the whole output, but we have to use XRGB as the KMS format code. */
385 if (is_opaque)
386 fb->format = pixel_format_get_opaque_substitute(fb->format);
387
388 if (backend->min_width > fb->width ||
389 fb->width > backend->max_width ||
390 backend->min_height > fb->height ||
391 fb->height > backend->max_height) {
392 weston_log("bo geometry out of bounds\n");
393 goto err_free;
394 }
395
396 if (drm_fb_addfb(backend, fb) != 0) {
397 if (type == BUFFER_GBM_SURFACE)
398 weston_log("failed to create kms fb: %s\n",
399 strerror(errno));
400 goto err_free;
401 }
402
403 gbm_bo_set_user_data(bo, fb, drm_fb_destroy_gbm);
404
405 return fb;
406
407err_free:
408 free(fb);
409 return NULL;
410}
Stefan Agnerccf24072019-07-09 22:02:00 +0200411#endif
Daniel Stone7580b3c2019-06-18 11:16:53 +0100412
413void
414drm_fb_unref(struct drm_fb *fb)
415{
416 if (!fb)
417 return;
418
419 assert(fb->refcnt > 0);
420 if (--fb->refcnt > 0)
421 return;
422
423 switch (fb->type) {
424 case BUFFER_PIXMAN_DUMB:
425 drm_fb_destroy_dumb(fb);
426 break;
Stefan Agnerccf24072019-07-09 22:02:00 +0200427#ifdef BUILD_DRM_GBM
Daniel Stone7580b3c2019-06-18 11:16:53 +0100428 case BUFFER_CURSOR:
429 case BUFFER_CLIENT:
430 gbm_bo_destroy(fb->bo);
431 break;
432 case BUFFER_GBM_SURFACE:
433 gbm_surface_release_buffer(fb->gbm_surface, fb->bo);
434 break;
435 case BUFFER_DMABUF:
436 drm_fb_destroy_dmabuf(fb);
437 break;
Stefan Agnerccf24072019-07-09 22:02:00 +0200438#endif
Daniel Stone7580b3c2019-06-18 11:16:53 +0100439 default:
440 assert(NULL);
441 break;
442 }
443}
444
Stefan Agnerccf24072019-07-09 22:02:00 +0200445#ifdef BUILD_DRM_GBM
Marius Vlad81bada52019-11-11 00:27:17 +0200446bool
447drm_can_scanout_dmabuf(struct weston_compositor *ec,
448 struct linux_dmabuf_buffer *dmabuf)
449{
450 struct drm_fb *fb;
451 struct drm_backend *b = to_drm_backend(ec);
452 bool ret = false;
453
Leandro Ribeiro0a7034c2021-09-13 14:52:53 -0300454 fb = drm_fb_get_from_dmabuf(dmabuf, b, true, NULL);
Marius Vlad81bada52019-11-11 00:27:17 +0200455 if (fb)
456 ret = true;
457
458 drm_fb_unref(fb);
459 drm_debug(b, "[dmabuf] dmabuf %p, import test %s\n", dmabuf,
460 ret ? "succeeded" : "failed");
461 return ret;
462}
463
Daniel Stone57d609a2021-11-16 18:56:09 +0000464static bool
465drm_fb_compatible_with_plane(struct drm_fb *fb, struct drm_plane *plane)
466{
467 struct drm_backend *b = plane->backend;
468 struct weston_drm_format *fmt;
469
470 /* Check whether the format is supported */
471 fmt = weston_drm_format_array_find_format(&plane->formats,
472 fb->format->format);
473 if (fmt) {
474 /* We never try to promote a dmabuf with DRM_FORMAT_MOD_INVALID
475 * to a KMS plane (see drm_fb_get_from_dmabuf() for more details).
476 * So if fb->modifier == DRM_FORMAT_MOD_INVALID, we are sure
477 * that this is for the legacy GBM import path, in which a
478 * wl_drm is being used for scanout. Mesa is the only user we
479 * care in this case (even though recent versions are also using
480 * dmabufs), and it should know better what works or not. */
481 if (fb->modifier == DRM_FORMAT_MOD_INVALID)
482 return true;
483
484 if (weston_drm_format_has_modifier(fmt, fb->modifier))
485 return true;
486 }
487
488 drm_debug(b, "\t\t\t\t[%s] not placing view on %s: "
489 "no free %s planes matching format %s (0x%lx) "
490 "modifier 0x%llx\n",
491 drm_output_get_plane_type_name(plane),
492 drm_output_get_plane_type_name(plane),
493 drm_output_get_plane_type_name(plane),
494 fb->format->drm_format_name,
495 (unsigned long) fb->format->format,
496 (unsigned long long) fb->modifier);
497
498 return false;
499}
500
Daniel Stone7d27df42021-11-18 16:01:03 +0000501static void
502drm_fb_handle_buffer_destroy(struct wl_listener *listener, void *data)
503{
504 struct drm_buffer_fb *buf_fb =
505 container_of(listener, struct drm_buffer_fb, buffer_destroy_listener);
506
507 if (buf_fb->fb) {
508 assert(buf_fb->fb->type == BUFFER_CLIENT ||
509 buf_fb->fb->type == BUFFER_DMABUF);
510 drm_fb_unref(buf_fb->fb);
511 }
512
513 free(buf_fb);
514}
515
Daniel Stone7580b3c2019-06-18 11:16:53 +0100516struct drm_fb *
Leandro Ribeiro0a7034c2021-09-13 14:52:53 -0300517drm_fb_get_from_view(struct drm_output_state *state, struct weston_view *ev,
518 uint32_t *try_view_on_plane_failure_reasons)
Daniel Stone7580b3c2019-06-18 11:16:53 +0100519{
520 struct drm_output *output = state->output;
521 struct drm_backend *b = to_drm_backend(output->base.compositor);
522 struct weston_buffer *buffer = ev->surface->buffer_ref.buffer;
Daniel Stone7d27df42021-11-18 16:01:03 +0000523 struct drm_buffer_fb *buf_fb;
Daniel Stone7580b3c2019-06-18 11:16:53 +0100524 bool is_opaque = weston_view_is_opaque(ev, &ev->transform.boundingbox);
525 struct linux_dmabuf_buffer *dmabuf;
526 struct drm_fb *fb;
Daniel Stone57d609a2021-11-16 18:56:09 +0000527 struct drm_plane *plane;
Daniel Stone7580b3c2019-06-18 11:16:53 +0100528
529 if (ev->alpha != 1.0f)
530 return NULL;
531
532 if (!drm_view_transform_supported(ev, &output->base))
533 return NULL;
534
Daniel Stoned32dfcf2019-08-19 16:53:40 +0100535 if (ev->surface->protection_mode == WESTON_SURFACE_PROTECTION_MODE_ENFORCED &&
536 ev->surface->desired_protection > output->base.current_protection)
537 return NULL;
538
Daniel Stone7580b3c2019-06-18 11:16:53 +0100539 if (!buffer)
540 return NULL;
541
Daniel Stone7d27df42021-11-18 16:01:03 +0000542 if (buffer->backend_private) {
543 buf_fb = buffer->backend_private;
544 *try_view_on_plane_failure_reasons |= buf_fb->failure_reasons;
545 return buf_fb->fb ? drm_fb_ref(buf_fb->fb) : NULL;
546 }
547
548 buf_fb = zalloc(sizeof(*buf_fb));
549 buffer->backend_private = buf_fb;
550 buf_fb->buffer_destroy_listener.notify = drm_fb_handle_buffer_destroy;
551 wl_signal_add(&buffer->destroy_signal, &buf_fb->buffer_destroy_listener);
552
Daniel Stone7580b3c2019-06-18 11:16:53 +0100553 if (wl_shm_buffer_get(buffer->resource))
Daniel Stone7d27df42021-11-18 16:01:03 +0000554 goto unsuitable;
Daniel Stone7580b3c2019-06-18 11:16:53 +0100555
556 /* GBM is used for dmabuf import as well as from client wl_buffer. */
557 if (!b->gbm)
Daniel Stone7d27df42021-11-18 16:01:03 +0000558 goto unsuitable;
Daniel Stone7580b3c2019-06-18 11:16:53 +0100559
560 dmabuf = linux_dmabuf_buffer_get(buffer->resource);
561 if (dmabuf) {
Leandro Ribeiro0a7034c2021-09-13 14:52:53 -0300562 fb = drm_fb_get_from_dmabuf(dmabuf, b, is_opaque,
Daniel Stone7d27df42021-11-18 16:01:03 +0000563 &buf_fb->failure_reasons);
Daniel Stone7580b3c2019-06-18 11:16:53 +0100564 if (!fb)
Daniel Stone7d27df42021-11-18 16:01:03 +0000565 goto unsuitable;
Daniel Stone7580b3c2019-06-18 11:16:53 +0100566 } else {
567 struct gbm_bo *bo;
568
569 bo = gbm_bo_import(b->gbm, GBM_BO_IMPORT_WL_BUFFER,
570 buffer->resource, GBM_BO_USE_SCANOUT);
571 if (!bo)
Daniel Stone7d27df42021-11-18 16:01:03 +0000572 goto unsuitable;
Daniel Stone7580b3c2019-06-18 11:16:53 +0100573
574 fb = drm_fb_get_from_bo(bo, b, is_opaque, BUFFER_CLIENT);
575 if (!fb) {
576 gbm_bo_destroy(bo);
Daniel Stone7d27df42021-11-18 16:01:03 +0000577 goto unsuitable;
Daniel Stone7580b3c2019-06-18 11:16:53 +0100578 }
579 }
580
Daniel Stone57d609a2021-11-16 18:56:09 +0000581 /* Check if this buffer can ever go on any planes. If it can't, we have
582 * no reason to ever have a drm_fb, so we fail it here. */
583 wl_list_for_each(plane, &b->plane_list, link) {
584 if (drm_fb_compatible_with_plane(fb, plane))
585 fb->plane_mask |= (1 << plane->plane_idx);
586 }
587 if (fb->plane_mask == 0) {
588 drm_fb_unref(fb);
589 buf_fb->failure_reasons |= FAILURE_REASONS_FB_FORMAT_INCOMPATIBLE;
590 goto unsuitable;
591 }
592
Daniel Stone7d27df42021-11-18 16:01:03 +0000593 /* The caller holds its own ref to the drm_fb, so when creating a new
594 * drm_fb we take an additional ref for the weston_buffer's cache. */
595 buf_fb->fb = drm_fb_ref(fb);
596
Daniel Stone7580b3c2019-06-18 11:16:53 +0100597 drm_debug(b, "\t\t\t[view] view %p format: %s\n",
598 ev, fb->format->drm_format_name);
Daniel Stone7580b3c2019-06-18 11:16:53 +0100599 return fb;
Daniel Stone7d27df42021-11-18 16:01:03 +0000600
601unsuitable:
602 *try_view_on_plane_failure_reasons |= buf_fb->failure_reasons;
603 return NULL;
Daniel Stone7580b3c2019-06-18 11:16:53 +0100604}
Stefan Agnerccf24072019-07-09 22:02:00 +0200605#endif