File indexing completed on 2025-04-20 10:57:37

0001 /*
0002     SPDX-FileCopyrightText: 2020 Aleix Pol Gonzalez <aleixpol@kde.org>
0003 
0004     SPDX-License-Identifier: LGPL-2.0-or-later
0005 */
0006 
0007 #pragma once
0008 
0009 #include "config-kwin.h"
0010 #include "dmabuftexture.h"
0011 #include <libdrm/drm_fourcc.h>
0012 
0013 #include <gbm.h>
0014 
0015 namespace KWin
0016 {
0017 
0018 inline DmaBufAttributes dmaBufAttributesForBo(gbm_bo *bo)
0019 {
0020     DmaBufAttributes attributes;
0021     attributes.planeCount = gbm_bo_get_plane_count(bo);
0022     attributes.width = gbm_bo_get_width(bo);
0023     attributes.height = gbm_bo_get_height(bo);
0024     attributes.format = gbm_bo_get_format(bo);
0025     attributes.modifier = gbm_bo_get_modifier(bo);
0026 
0027 #if HAVE_GBM_BO_GET_FD_FOR_PLANE
0028     for (int i = 0; i < attributes.planeCount; ++i) {
0029         attributes.fd[i] = FileDescriptor{gbm_bo_get_fd_for_plane(bo, i)};
0030         attributes.offset[i] = gbm_bo_get_offset(bo, i);
0031         attributes.pitch[i] = gbm_bo_get_stride_for_plane(bo, i);
0032     }
0033 #else
0034     if (attributes.planeCount > 1) {
0035         return attributes;
0036     }
0037 
0038     attributes.fd[0] = FileDescriptor{gbm_bo_get_fd(bo)};
0039     attributes.offset[0] = gbm_bo_get_offset(bo, 0);
0040     attributes.pitch[0] = gbm_bo_get_stride_for_plane(bo, 0);
0041 #endif
0042 
0043     return attributes;
0044 }
0045 
0046 inline DmaBufParams dmaBufParamsForBo(gbm_bo *bo)
0047 {
0048     DmaBufParams attributes;
0049     attributes.planeCount = gbm_bo_get_plane_count(bo);
0050     attributes.width = gbm_bo_get_width(bo);
0051     attributes.height = gbm_bo_get_height(bo);
0052     attributes.format = gbm_bo_get_format(bo);
0053     attributes.modifier = gbm_bo_get_modifier(bo);
0054     return attributes;
0055 }
0056 
0057 inline gbm_bo *createGbmBo(gbm_device *device, const QSize &size, quint32 format, const QVector<uint64_t> &modifiers)
0058 {
0059     gbm_bo *bo = nullptr;
0060     if (modifiers.count() > 0 && !(modifiers.count() == 1 && modifiers[0] == DRM_FORMAT_MOD_INVALID)) {
0061 #if HAVE_GBM_BO_CREATE_WITH_MODIFIERS2
0062         bo = gbm_bo_create_with_modifiers2(device,
0063                                            size.width(),
0064                                            size.height(),
0065                                            format,
0066                                            modifiers.constData(), modifiers.count(),
0067                                            GBM_BO_USE_RENDERING);
0068 #else
0069         bo = gbm_bo_create_with_modifiers(device,
0070                                           size.width(),
0071                                           size.height(),
0072                                           format,
0073                                           modifiers.constData(), modifiers.count());
0074 #endif
0075     }
0076 
0077     if (!bo && (modifiers.isEmpty() || modifiers.contains(DRM_FORMAT_MOD_INVALID))) {
0078         bo = gbm_bo_create(device,
0079                            size.width(),
0080                            size.height(),
0081                            format,
0082                            GBM_BO_USE_LINEAR);
0083     }
0084     return bo;
0085 }
0086 
0087 } // namespace KWin