| 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 | // |
| 8 | // W A R N I N G |
| 9 | // ------------- |
| 10 | // |
| 11 | // This file is not part of the Qt API. It exists purely as an |
| 12 | // implementation detail. This header file may change from version to |
| 13 | // version without notice, or even be removed. |
| 14 | // |
| 15 | // We mean it. |
| 16 | // |
| 17 | |
| 18 | #include <QtGui/QImage> |
| 19 | #include <QtCore/private/qglobal_p.h> |
| 20 | |
| 21 | //the correct protocol header for the wayland server or wayland client has to be |
| 22 | //included before this file is included |
| 23 | |
| 24 | QT_BEGIN_NAMESPACE |
| 25 | |
| 26 | class QWaylandSharedMemoryFormatHelper |
| 27 | { |
| 28 | public: |
| 29 | static inline wl_shm_format fromQImageFormat(QImage::Format format); |
| 30 | static inline QImage::Format fromWaylandShmFormat(wl_shm_format format) |
| 31 | { |
| 32 | switch (format) { |
| 33 | case WL_SHM_FORMAT_XRGB8888: return QImage::Format_RGB32; |
| 34 | case WL_SHM_FORMAT_ARGB8888: return QImage::Format_ARGB32_Premultiplied; |
| 35 | case WL_SHM_FORMAT_RGB565: return QImage::Format_RGB16; |
| 36 | case WL_SHM_FORMAT_XRGB1555: return QImage::Format_RGB555; |
| 37 | case WL_SHM_FORMAT_RGB888: return QImage::Format_RGB888; |
| 38 | case WL_SHM_FORMAT_BGR888: return QImage::Format_BGR888; |
| 39 | case WL_SHM_FORMAT_XRGB4444: return QImage::Format_RGB444; |
| 40 | case WL_SHM_FORMAT_ARGB4444: return QImage::Format_ARGB4444_Premultiplied; |
| 41 | case WL_SHM_FORMAT_XBGR8888: return QImage::Format_RGBX8888; |
| 42 | case WL_SHM_FORMAT_ABGR8888: return QImage::Format_RGBA8888_Premultiplied; |
| 43 | case WL_SHM_FORMAT_XBGR2101010: return QImage::Format_BGR30; |
| 44 | case WL_SHM_FORMAT_ABGR2101010: return QImage::Format_A2BGR30_Premultiplied; |
| 45 | case WL_SHM_FORMAT_XRGB2101010: return QImage::Format_RGB30; |
| 46 | case WL_SHM_FORMAT_ARGB2101010: return QImage::Format_A2RGB30_Premultiplied; |
| 47 | case WL_SHM_FORMAT_C8: return QImage::Format_Alpha8; |
| 48 | default: return QImage::Format_Invalid; |
| 49 | } |
| 50 | } |
| 51 | |
| 52 | private: |
| 53 | //IMPLEMENTATION (which has to be inline in the header because of the include trick) |
| 54 | struct Array |
| 55 | { |
| 56 | Array(const size_t size, const wl_shm_format *data) |
| 57 | : size(size) |
| 58 | , data(data) |
| 59 | { } |
| 60 | const size_t size; |
| 61 | const wl_shm_format *data = nullptr; |
| 62 | }; |
| 63 | |
| 64 | static const Array getData() |
| 65 | { |
| 66 | static wl_shm_format formats_array[] = { |
| 67 | wl_shm_format(INT_MIN), //Format_Invalid, |
| 68 | wl_shm_format(INT_MIN), //Format_Mono, |
| 69 | wl_shm_format(INT_MIN), //Format_MonoLSB, |
| 70 | wl_shm_format(INT_MIN), //Format_Indexed8, |
| 71 | WL_SHM_FORMAT_XRGB8888, //Format_RGB32, |
| 72 | WL_SHM_FORMAT_ARGB8888, //Format_ARGB32, |
| 73 | WL_SHM_FORMAT_ARGB8888, //Format_ARGB32_Premultiplied, |
| 74 | WL_SHM_FORMAT_RGB565, //Format_RGB16, |
| 75 | wl_shm_format(INT_MIN), //Format_ARGB8565_Premultiplied, |
| 76 | wl_shm_format(INT_MIN), //Format_RGB666, |
| 77 | wl_shm_format(INT_MIN), //Format_ARGB6666_Premultiplied, |
| 78 | WL_SHM_FORMAT_XRGB1555, //Format_RGB555, |
| 79 | wl_shm_format(INT_MIN), //Format_ARGB8555_Premultiplied, |
| 80 | WL_SHM_FORMAT_RGB888, //Format_RGB888, |
| 81 | WL_SHM_FORMAT_XRGB4444, //Format_RGB444, |
| 82 | WL_SHM_FORMAT_ARGB4444, //Format_ARGB4444_Premultiplied, |
| 83 | WL_SHM_FORMAT_XBGR8888, //Format_RGBX8888, |
| 84 | WL_SHM_FORMAT_ABGR8888, //Format_RGBA8888, |
| 85 | WL_SHM_FORMAT_ABGR8888, //Format_RGBA8888_Premultiplied, |
| 86 | WL_SHM_FORMAT_XBGR2101010, //Format_BGR30, |
| 87 | WL_SHM_FORMAT_ABGR2101010, //Format_A2BGR30_Premultiplied, |
| 88 | WL_SHM_FORMAT_XRGB2101010, //Format_RGB30, |
| 89 | WL_SHM_FORMAT_ARGB2101010, //Format_A2RGB30_Premultiplied, |
| 90 | WL_SHM_FORMAT_C8, //Format_Alpha8, |
| 91 | WL_SHM_FORMAT_C8, //Format_Grayscale8, |
| 92 | wl_shm_format(INT_MIN), //Format_RGBX64, |
| 93 | wl_shm_format(INT_MIN), //Format_RGBA64, |
| 94 | wl_shm_format(INT_MIN), //Format_RGBA64_Premultiplied, |
| 95 | wl_shm_format(INT_MIN), //Format_Grayscale16, |
| 96 | WL_SHM_FORMAT_BGR888, //Format_BGR888 |
| 97 | |
| 98 | }; |
| 99 | const size_t size = sizeof(formats_array) / sizeof(*formats_array); |
| 100 | return Array(size, formats_array); |
| 101 | } |
| 102 | }; |
| 103 | |
| 104 | wl_shm_format QWaylandSharedMemoryFormatHelper::fromQImageFormat(QImage::Format format) |
| 105 | { |
| 106 | Array array = getData(); |
| 107 | if (array.size <= size_t(format)) |
| 108 | return wl_shm_format(INT_MIN); |
| 109 | return array.data[format]; |
| 110 | } |
| 111 | |
| 112 | QT_END_NAMESPACE |
| 113 | |
| 114 | #endif //QWAYLANDSHAREDMEMORYFORMATHELPER_H |
| 115 | |