KWin
Loading...
Searching...
No Matches
graphicsbufferview.cpp
Go to the documentation of this file.
1/*
2 SPDX-FileCopyrightText: 2023 Vlad Zahorodnii <vlad.zahorodnii@kde.org>
3
4 SPDX-License-Identifier: GPL-2.0-or-later
5*/
6
9#include "utils/common.h"
10
11#include <drm_fourcc.h>
12
13namespace KWin
14{
15
16static QImage::Format drmFormatToQImageFormat(uint32_t drmFormat)
17{
18 switch (drmFormat) {
19#if Q_BYTE_ORDER == Q_LITTLE_ENDIAN
20 case DRM_FORMAT_ABGR16161616:
21 return QImage::Format_RGBA64_Premultiplied;
22 case DRM_FORMAT_XBGR16161616:
23 return QImage::Format_RGBX64;
24 case DRM_FORMAT_ARGB2101010:
25 return QImage::Format_A2RGB30_Premultiplied;
26 case DRM_FORMAT_XRGB2101010:
27 return QImage::Format_RGB30;
28 case DRM_FORMAT_ABGR2101010:
29 return QImage::Format_A2BGR30_Premultiplied;
30 case DRM_FORMAT_XBGR2101010:
31 return QImage::Format_BGR30;
32#endif
33 case DRM_FORMAT_ARGB8888:
34 return QImage::Format_ARGB32_Premultiplied;
35 case DRM_FORMAT_XRGB8888:
36 return QImage::Format_RGB32;
37 default:
38 return QImage::Format_Invalid;
39 }
40}
41
42GraphicsBufferView::GraphicsBufferView(GraphicsBuffer *buffer, GraphicsBuffer::MapFlags accessFlags)
43 : m_buffer(buffer)
44{
45 int width;
46 int height;
47 int format;
48
49 if (auto dmabuf = buffer->dmabufAttributes()) {
50 if (dmabuf->planeCount != 1) {
51 return;
52 }
53 width = dmabuf->width;
54 height = dmabuf->height;
55 format = dmabuf->format;
56 } else if (auto shm = buffer->shmAttributes()) {
57 width = shm->size.width();
58 height = shm->size.height();
59 format = shm->format;
60 } else {
61 qCWarning(KWIN_CORE) << "Cannot create a graphics buffer view for unknown buffer type" << buffer;
62 return;
63 }
64
65 const auto [data, stride] = buffer->map(accessFlags);
66 if (data) {
67 m_image = QImage(static_cast<uchar *>(data), width, height, stride, drmFormatToQImageFormat(format));
68 }
69}
70
72{
73 if (!m_image.isNull()) {
74 m_buffer->unmap();
75 }
76}
77
79{
80 return m_image.isNull();
81}
82
84{
85 return &m_image;
86}
87
88const QImage *GraphicsBufferView::image() const
89{
90 return &m_image;
91}
92
93} // namespace KWin
virtual Map map(MapFlags flags)
virtual const DmaBufAttributes * dmabufAttributes() const
virtual const ShmAttributes * shmAttributes() const
GraphicsBufferView(GraphicsBuffer *buffer, GraphicsBuffer::MapFlags accessFlags=GraphicsBuffer::Read)
GLenum format
Definition gltexture.cpp:49