aboutsummaryrefslogtreecommitdiffhomepage
path: root/src/helpers/Format.cpp
blob: 5251002cddbc563da07d252d4c1fd91d421dcf74 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
#include "Format.hpp"
#include <vector>
#include "../includes.hpp"
#include "debug/Log.hpp"
#include "../macros.hpp"

/*
    DRM formats are LE, while OGL is BE. The two primary formats
    will be flipped, so we will set flipRB which will later use swizzle
    to flip the red and blue channels.
    This will not work on GLES2, but I want to drop support for it one day anyways.
*/
inline const std::vector<SPixelFormat> GLES3_FORMATS = {
    {
        .drmFormat = DRM_FORMAT_ARGB8888,
        .flipRB    = true,
#ifndef GLES2
        .glFormat = GL_RGBA,
#else
        .glFormat = GL_BGRA_EXT,
#endif
        .glType        = GL_UNSIGNED_BYTE,
        .withAlpha     = true,
        .alphaStripped = DRM_FORMAT_XRGB8888,
        .bytesPerBlock = 4,
    },
    {
        .drmFormat = DRM_FORMAT_XRGB8888,
        .flipRB    = true,
#ifndef GLES2
        .glFormat = GL_RGBA,
#else
        .glFormat = GL_BGRA_EXT,
#endif
        .glType        = GL_UNSIGNED_BYTE,
        .withAlpha     = false,
        .alphaStripped = DRM_FORMAT_XRGB8888,
        .bytesPerBlock = 4,
    },
    {
        .drmFormat     = DRM_FORMAT_XBGR8888,
        .glFormat      = GL_RGBA,
        .glType        = GL_UNSIGNED_BYTE,
        .withAlpha     = false,
        .alphaStripped = DRM_FORMAT_XBGR8888,
        .bytesPerBlock = 4,
    },
    {
        .drmFormat     = DRM_FORMAT_ABGR8888,
        .glFormat      = GL_RGBA,
        .glType        = GL_UNSIGNED_BYTE,
        .withAlpha     = true,
        .alphaStripped = DRM_FORMAT_XBGR8888,
        .bytesPerBlock = 4,
    },
    {
        .drmFormat     = DRM_FORMAT_BGR888,
        .glFormat      = GL_RGB,
        .glType        = GL_UNSIGNED_BYTE,
        .withAlpha     = false,
        .alphaStripped = DRM_FORMAT_BGR888,
        .bytesPerBlock = 3,
    },
    {
        .drmFormat     = DRM_FORMAT_RGBX4444,
        .glFormat      = GL_RGBA,
        .glType        = GL_UNSIGNED_SHORT_4_4_4_4,
        .withAlpha     = false,
        .alphaStripped = DRM_FORMAT_RGBX4444,
        .bytesPerBlock = 2,
    },
    {
        .drmFormat     = DRM_FORMAT_RGBA4444,
        .glFormat      = GL_RGBA,
        .glType        = GL_UNSIGNED_SHORT_4_4_4_4,
        .withAlpha     = true,
        .alphaStripped = DRM_FORMAT_RGBX4444,
        .bytesPerBlock = 2,
    },
    {
        .drmFormat     = DRM_FORMAT_RGBX5551,
        .glFormat      = GL_RGBA,
        .glType        = GL_UNSIGNED_SHORT_5_5_5_1,
        .withAlpha     = false,
        .alphaStripped = DRM_FORMAT_RGBX5551,
        .bytesPerBlock = 2,
    },
    {
        .drmFormat     = DRM_FORMAT_RGBA5551,
        .glFormat      = GL_RGBA,
        .glType        = GL_UNSIGNED_SHORT_5_5_5_1,
        .withAlpha     = true,
        .alphaStripped = DRM_FORMAT_RGBX5551,
        .bytesPerBlock = 2,
    },
    {
        .drmFormat     = DRM_FORMAT_RGB565,
        .glFormat      = GL_RGB,
        .glType        = GL_UNSIGNED_SHORT_5_6_5,
        .withAlpha     = false,
        .alphaStripped = DRM_FORMAT_RGB565,
        .bytesPerBlock = 2,
    },
    {
        .drmFormat = DRM_FORMAT_XBGR2101010,
        .glFormat  = GL_RGBA,
#ifndef GLES2
        .glType = GL_UNSIGNED_INT_2_10_10_10_REV,
#else
        .glType = GL_UNSIGNED_INT_2_10_10_10_REV_EXT,
#endif
        .withAlpha     = false,
        .alphaStripped = DRM_FORMAT_XBGR2101010,
        .bytesPerBlock = 4,
    },
    {
        .drmFormat = DRM_FORMAT_ABGR2101010,
        .glFormat  = GL_RGBA,
#ifndef GLES2
        .glType = GL_UNSIGNED_INT_2_10_10_10_REV,
#else
        .glType = GL_UNSIGNED_INT_2_10_10_10_REV_EXT,
#endif
        .withAlpha     = true,
        .alphaStripped = DRM_FORMAT_XBGR2101010,
        .bytesPerBlock = 4,
    },
    {
        .drmFormat = DRM_FORMAT_XRGB2101010,
        .glFormat  = GL_RGBA,
#ifndef GLES2
        .glType = GL_UNSIGNED_INT_2_10_10_10_REV,
#else
        .glType = GL_UNSIGNED_INT_2_10_10_10_REV_EXT,
#endif
        .withAlpha     = false,
        .alphaStripped = DRM_FORMAT_XRGB2101010,
        .bytesPerBlock = 4,
    },
    {
        .drmFormat = DRM_FORMAT_ARGB2101010,
        .glFormat  = GL_RGBA,
#ifndef GLES2
        .glType = GL_UNSIGNED_INT_2_10_10_10_REV,
#else
        .glType = GL_UNSIGNED_INT_2_10_10_10_REV_EXT,
#endif
        .withAlpha     = true,
        .alphaStripped = DRM_FORMAT_XRGB2101010,
        .bytesPerBlock = 4,
    },
    {
        .drmFormat = DRM_FORMAT_XBGR16161616F,
        .glFormat  = GL_RGBA,
#ifndef GLES2
        .glType = GL_HALF_FLOAT,
#else
        .glType = GL_HALF_FLOAT_OES,
#endif
        .withAlpha     = false,
        .alphaStripped = DRM_FORMAT_XBGR16161616F,
        .bytesPerBlock = 8,
    },
    {
        .drmFormat = DRM_FORMAT_ABGR16161616F,
        .glFormat  = GL_RGBA,
#ifndef GLES2
        .glType = GL_HALF_FLOAT,
#else
        .glType = GL_HALF_FLOAT_OES,
#endif
        .withAlpha     = true,
        .alphaStripped = DRM_FORMAT_XBGR16161616F,
        .bytesPerBlock = 8,
    },
    {
        .drmFormat = DRM_FORMAT_XBGR16161616,
#ifndef GLES2
        .glFormat = GL_RGBA16UI,
#else
        .glFormat = GL_RGBA16_EXT,
#endif
        .glType        = GL_UNSIGNED_SHORT,
        .withAlpha     = false,
        .alphaStripped = DRM_FORMAT_XBGR16161616,
        .bytesPerBlock = 8,
    },
    {
        .drmFormat = DRM_FORMAT_ABGR16161616,
#ifndef GLES2
        .glFormat = GL_RGBA16UI,
#else
        .glFormat = GL_RGBA16_EXT,
#endif
        .glType        = GL_UNSIGNED_SHORT,
        .withAlpha     = true,
        .alphaStripped = DRM_FORMAT_XBGR16161616,
        .bytesPerBlock = 8,
    },
    {
        .drmFormat     = DRM_FORMAT_YVYU,
        .bytesPerBlock = 4,
        .blockSize     = {2, 1},
    },
    {
        .drmFormat     = DRM_FORMAT_VYUY,
        .bytesPerBlock = 4,
        .blockSize     = {2, 1},
    },
    {
        .drmFormat     = DRM_FORMAT_R8,
        .bytesPerBlock = 1,
    },
    {
        .drmFormat     = DRM_FORMAT_GR88,
        .bytesPerBlock = 2,
    },
    {
        .drmFormat     = DRM_FORMAT_RGB888,
        .bytesPerBlock = 3,
    },
    {
        .drmFormat     = DRM_FORMAT_BGR888,
        .bytesPerBlock = 3,
    },
    {
        .drmFormat     = DRM_FORMAT_RGBX4444,
        .bytesPerBlock = 2,
    },
};

SHMFormat FormatUtils::drmToShm(DRMFormat drm) {
    switch (drm) {
        case DRM_FORMAT_XRGB8888: return WL_SHM_FORMAT_XRGB8888;
        case DRM_FORMAT_ARGB8888: return WL_SHM_FORMAT_ARGB8888;
        default: return drm;
    }

    return drm;
}

DRMFormat FormatUtils::shmToDRM(SHMFormat shm) {
    switch (shm) {
        case WL_SHM_FORMAT_XRGB8888: return DRM_FORMAT_XRGB8888;
        case WL_SHM_FORMAT_ARGB8888: return DRM_FORMAT_ARGB8888;
        default: return shm;
    }

    return shm;
}

const SPixelFormat* FormatUtils::getPixelFormatFromDRM(DRMFormat drm) {
    for (auto& fmt : GLES3_FORMATS) {
        if (fmt.drmFormat == drm)
            return &fmt;
    }

    return nullptr;
}

const SPixelFormat* FormatUtils::getPixelFormatFromGL(uint32_t glFormat, uint32_t glType, bool alpha) {
    for (auto& fmt : GLES3_FORMATS) {
        if (fmt.glFormat == (int)glFormat && fmt.glType == (int)glType && fmt.withAlpha == alpha)
            return &fmt;
    }

    return nullptr;
}

bool FormatUtils::isFormatOpaque(DRMFormat drm) {
    const auto FMT = FormatUtils::getPixelFormatFromDRM(drm);
    if (!FMT)
        return false;

    return !FMT->withAlpha;
}

int FormatUtils::pixelsPerBlock(const SPixelFormat* const fmt) {
    return fmt->blockSize.x * fmt->blockSize.y > 0 ? fmt->blockSize.x * fmt->blockSize.y : 1;
}

int FormatUtils::minStride(const SPixelFormat* const fmt, int32_t width) {
    return std::ceil((width * fmt->bytesPerBlock) / pixelsPerBlock(fmt));
}

uint32_t FormatUtils::drmFormatToGL(DRMFormat drm) {
    switch (drm) {
        case DRM_FORMAT_XRGB8888:
        case DRM_FORMAT_XBGR8888: return GL_RGBA; // doesn't matter, opengl is gucci in this case.
        case DRM_FORMAT_XRGB2101010:
        case DRM_FORMAT_XBGR2101010:
#ifdef GLES2
            return GL_RGB10_A2_EXT;
#else
            return GL_RGB10_A2;
#endif
        default: return GL_RGBA;
    }
    UNREACHABLE();
    return GL_RGBA;
}

uint32_t FormatUtils::glFormatToType(uint32_t gl) {
    return gl != GL_RGBA ?
#ifdef GLES2
        GL_UNSIGNED_INT_2_10_10_10_REV_EXT :
#else
        GL_UNSIGNED_INT_2_10_10_10_REV :
#endif
        GL_UNSIGNED_BYTE;
}