1// Copyright (C) 2020 The Qt Company Ltd.
2// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only
3
4#ifndef QWAYLANDSHAREDMEMORYFORMATHELPER_H
5#define QWAYLANDSHAREDMEMORYFORMATHELPER_H
6
7#include <QtGui/QImage>
8#include <QtCore/private/qglobal_p.h>
9
10//the correct protocol header for the wayland server or wayland client has to be
11//included before this file is included
12
13QT_BEGIN_NAMESPACE
14
15class QWaylandSharedMemoryFormatHelper
16{
17public:
18 static inline wl_shm_format fromQImageFormat(QImage::Format format);
19 static inline QImage::Format fromWaylandShmFormat(wl_shm_format format)
20 {
21 switch (format) {
22 case WL_SHM_FORMAT_XRGB8888: return QImage::Format_RGB32;
23 case WL_SHM_FORMAT_ARGB8888: return QImage::Format_ARGB32_Premultiplied;
24 case WL_SHM_FORMAT_RGB565: return QImage::Format_RGB16;
25 case WL_SHM_FORMAT_XRGB1555: return QImage::Format_RGB555;
26 case WL_SHM_FORMAT_RGB888: return QImage::Format_RGB888;
27 case WL_SHM_FORMAT_BGR888: return QImage::Format_BGR888;
28 case WL_SHM_FORMAT_XRGB4444: return QImage::Format_RGB444;
29 case WL_SHM_FORMAT_ARGB4444: return QImage::Format_ARGB4444_Premultiplied;
30 case WL_SHM_FORMAT_XBGR8888: return QImage::Format_RGBX8888;
31 case WL_SHM_FORMAT_ABGR8888: return QImage::Format_RGBA8888_Premultiplied;
32 case WL_SHM_FORMAT_XBGR2101010: return QImage::Format_BGR30;
33 case WL_SHM_FORMAT_ABGR2101010: return QImage::Format_A2BGR30_Premultiplied;
34 case WL_SHM_FORMAT_XRGB2101010: return QImage::Format_RGB30;
35 case WL_SHM_FORMAT_ARGB2101010: return QImage::Format_A2RGB30_Premultiplied;
36 case WL_SHM_FORMAT_C8: return QImage::Format_Alpha8;
37 default: return QImage::Format_Invalid;
38 }
39 }
40
41private:
42//IMPLEMENTATION (which has to be inline in the header because of the include trick)
43 struct Array
44 {
45 Array(const size_t size, const wl_shm_format *data)
46 : size(size)
47 , data(data)
48 { }
49 const size_t size;
50 const wl_shm_format *data = nullptr;
51 };
52
53 static const Array getData()
54 {
55 static wl_shm_format formats_array[] = {
56 wl_shm_format(INT_MIN), //Format_Invalid,
57 wl_shm_format(INT_MIN), //Format_Mono,
58 wl_shm_format(INT_MIN), //Format_MonoLSB,
59 wl_shm_format(INT_MIN), //Format_Indexed8,
60 WL_SHM_FORMAT_XRGB8888, //Format_RGB32,
61 WL_SHM_FORMAT_ARGB8888, //Format_ARGB32,
62 WL_SHM_FORMAT_ARGB8888, //Format_ARGB32_Premultiplied,
63 WL_SHM_FORMAT_RGB565, //Format_RGB16,
64 wl_shm_format(INT_MIN), //Format_ARGB8565_Premultiplied,
65 wl_shm_format(INT_MIN), //Format_RGB666,
66 wl_shm_format(INT_MIN), //Format_ARGB6666_Premultiplied,
67 WL_SHM_FORMAT_XRGB1555, //Format_RGB555,
68 wl_shm_format(INT_MIN), //Format_ARGB8555_Premultiplied,
69 WL_SHM_FORMAT_RGB888, //Format_RGB888,
70 WL_SHM_FORMAT_XRGB4444, //Format_RGB444,
71 WL_SHM_FORMAT_ARGB4444, //Format_ARGB4444_Premultiplied,
72 WL_SHM_FORMAT_XBGR8888, //Format_RGBX8888,
73 WL_SHM_FORMAT_ABGR8888, //Format_RGBA8888,
74 WL_SHM_FORMAT_ABGR8888, //Format_RGBA8888_Premultiplied,
75 WL_SHM_FORMAT_XBGR2101010, //Format_BGR30,
76 WL_SHM_FORMAT_ABGR2101010, //Format_A2BGR30_Premultiplied,
77 WL_SHM_FORMAT_XRGB2101010, //Format_RGB30,
78 WL_SHM_FORMAT_ARGB2101010, //Format_A2RGB30_Premultiplied,
79 WL_SHM_FORMAT_C8, //Format_Alpha8,
80 WL_SHM_FORMAT_C8, //Format_Grayscale8,
81 wl_shm_format(INT_MIN), //Format_RGBX64,
82 wl_shm_format(INT_MIN), //Format_RGBA64,
83 wl_shm_format(INT_MIN), //Format_RGBA64_Premultiplied,
84 wl_shm_format(INT_MIN), //Format_Grayscale16,
85 WL_SHM_FORMAT_BGR888, //Format_BGR888
86
87 };
88 const size_t size = sizeof(formats_array) / sizeof(*formats_array);
89 return Array(size, formats_array);
90 }
91};
92
93wl_shm_format QWaylandSharedMemoryFormatHelper::fromQImageFormat(QImage::Format format)
94{
95 Array array = getData();
96 if (array.size <= size_t(format))
97 return wl_shm_format(INT_MIN);
98 return array.data[format];
99}
100
101QT_END_NAMESPACE
102
103#endif //QWAYLANDSHAREDMEMORYFORMATHELPER_H
104

source code of qtwayland/src/shared/qwaylandsharedmemoryformathelper_p.h