xref: /third_party/libdrm/tests/util/format.c (revision d722e3fb)
1d722e3fbSopenharmony_ci/*
2d722e3fbSopenharmony_ci * Copyright 2008 Tungsten Graphics
3d722e3fbSopenharmony_ci *   Jakob Bornecrantz <jakob@tungstengraphics.com>
4d722e3fbSopenharmony_ci * Copyright 2008 Intel Corporation
5d722e3fbSopenharmony_ci *   Jesse Barnes <jesse.barnes@intel.com>
6d722e3fbSopenharmony_ci *
7d722e3fbSopenharmony_ci * Permission is hereby granted, free of charge, to any person obtaining a
8d722e3fbSopenharmony_ci * copy of this software and associated documentation files (the "Software"),
9d722e3fbSopenharmony_ci * to deal in the Software without restriction, including without limitation
10d722e3fbSopenharmony_ci * the rights to use, copy, modify, merge, publish, distribute, sublicense,
11d722e3fbSopenharmony_ci * and/or sell copies of the Software, and to permit persons to whom the
12d722e3fbSopenharmony_ci * Software is furnished to do so, subject to the following conditions:
13d722e3fbSopenharmony_ci *
14d722e3fbSopenharmony_ci * The above copyright notice and this permission notice shall be included in
15d722e3fbSopenharmony_ci * all copies or substantial portions of the Software.
16d722e3fbSopenharmony_ci *
17d722e3fbSopenharmony_ci * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18d722e3fbSopenharmony_ci * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19d722e3fbSopenharmony_ci * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20d722e3fbSopenharmony_ci * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21d722e3fbSopenharmony_ci * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
22d722e3fbSopenharmony_ci * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
23d722e3fbSopenharmony_ci * IN THE SOFTWARE.
24d722e3fbSopenharmony_ci */
25d722e3fbSopenharmony_ci
26d722e3fbSopenharmony_ci#include <stdint.h>
27d722e3fbSopenharmony_ci#include <stdlib.h>
28d722e3fbSopenharmony_ci#include <string.h>
29d722e3fbSopenharmony_ci
30d722e3fbSopenharmony_ci#include <drm_fourcc.h>
31d722e3fbSopenharmony_ci
32d722e3fbSopenharmony_ci#include "common.h"
33d722e3fbSopenharmony_ci#include "format.h"
34d722e3fbSopenharmony_ci
35d722e3fbSopenharmony_ci#define MAKE_RGB_INFO(rl, ro, gl, go, bl, bo, al, ao) \
36d722e3fbSopenharmony_ci	.rgb = { { (rl), (ro) }, { (gl), (go) }, { (bl), (bo) }, { (al), (ao) } }
37d722e3fbSopenharmony_ci
38d722e3fbSopenharmony_ci#define MAKE_YUV_INFO(order, xsub, ysub, chroma_stride) \
39d722e3fbSopenharmony_ci	.yuv = { (order), (xsub), (ysub), (chroma_stride) }
40d722e3fbSopenharmony_ci
41d722e3fbSopenharmony_cistatic const struct util_format_info format_info[] = {
42d722e3fbSopenharmony_ci	/* Indexed */
43d722e3fbSopenharmony_ci	{ DRM_FORMAT_C8, "C8" },
44d722e3fbSopenharmony_ci	/* YUV packed */
45d722e3fbSopenharmony_ci	{ DRM_FORMAT_UYVY, "UYVY", MAKE_YUV_INFO(YUV_YCbCr | YUV_CY, 2, 2, 2) },
46d722e3fbSopenharmony_ci	{ DRM_FORMAT_VYUY, "VYUY", MAKE_YUV_INFO(YUV_YCrCb | YUV_CY, 2, 2, 2) },
47d722e3fbSopenharmony_ci	{ DRM_FORMAT_YUYV, "YUYV", MAKE_YUV_INFO(YUV_YCbCr | YUV_YC, 2, 2, 2) },
48d722e3fbSopenharmony_ci	{ DRM_FORMAT_YVYU, "YVYU", MAKE_YUV_INFO(YUV_YCrCb | YUV_YC, 2, 2, 2) },
49d722e3fbSopenharmony_ci	/* YUV semi-planar */
50d722e3fbSopenharmony_ci	{ DRM_FORMAT_NV12, "NV12", MAKE_YUV_INFO(YUV_YCbCr, 2, 2, 2) },
51d722e3fbSopenharmony_ci	{ DRM_FORMAT_NV21, "NV21", MAKE_YUV_INFO(YUV_YCrCb, 2, 2, 2) },
52d722e3fbSopenharmony_ci	{ DRM_FORMAT_NV16, "NV16", MAKE_YUV_INFO(YUV_YCbCr, 2, 1, 2) },
53d722e3fbSopenharmony_ci	{ DRM_FORMAT_NV61, "NV61", MAKE_YUV_INFO(YUV_YCrCb, 2, 1, 2) },
54d722e3fbSopenharmony_ci	/* YUV planar */
55d722e3fbSopenharmony_ci	{ DRM_FORMAT_YUV420, "YU12", MAKE_YUV_INFO(YUV_YCbCr, 2, 2, 1) },
56d722e3fbSopenharmony_ci	{ DRM_FORMAT_YVU420, "YV12", MAKE_YUV_INFO(YUV_YCrCb, 2, 2, 1) },
57d722e3fbSopenharmony_ci	/* RGB16 */
58d722e3fbSopenharmony_ci	{ DRM_FORMAT_ARGB4444, "AR12", MAKE_RGB_INFO(4, 8, 4, 4, 4, 0, 4, 12) },
59d722e3fbSopenharmony_ci	{ DRM_FORMAT_XRGB4444, "XR12", MAKE_RGB_INFO(4, 8, 4, 4, 4, 0, 0, 0) },
60d722e3fbSopenharmony_ci	{ DRM_FORMAT_ABGR4444, "AB12", MAKE_RGB_INFO(4, 0, 4, 4, 4, 8, 4, 12) },
61d722e3fbSopenharmony_ci	{ DRM_FORMAT_XBGR4444, "XB12", MAKE_RGB_INFO(4, 0, 4, 4, 4, 8, 0, 0) },
62d722e3fbSopenharmony_ci	{ DRM_FORMAT_RGBA4444, "RA12", MAKE_RGB_INFO(4, 12, 4, 8, 4, 4, 4, 0) },
63d722e3fbSopenharmony_ci	{ DRM_FORMAT_RGBX4444, "RX12", MAKE_RGB_INFO(4, 12, 4, 8, 4, 4, 0, 0) },
64d722e3fbSopenharmony_ci	{ DRM_FORMAT_BGRA4444, "BA12", MAKE_RGB_INFO(4, 4, 4, 8, 4, 12, 4, 0) },
65d722e3fbSopenharmony_ci	{ DRM_FORMAT_BGRX4444, "BX12", MAKE_RGB_INFO(4, 4, 4, 8, 4, 12, 0, 0) },
66d722e3fbSopenharmony_ci	{ DRM_FORMAT_ARGB1555, "AR15", MAKE_RGB_INFO(5, 10, 5, 5, 5, 0, 1, 15) },
67d722e3fbSopenharmony_ci	{ DRM_FORMAT_XRGB1555, "XR15", MAKE_RGB_INFO(5, 10, 5, 5, 5, 0, 0, 0) },
68d722e3fbSopenharmony_ci	{ DRM_FORMAT_ABGR1555, "AB15", MAKE_RGB_INFO(5, 0, 5, 5, 5, 10, 1, 15) },
69d722e3fbSopenharmony_ci	{ DRM_FORMAT_XBGR1555, "XB15", MAKE_RGB_INFO(5, 0, 5, 5, 5, 10, 0, 0) },
70d722e3fbSopenharmony_ci	{ DRM_FORMAT_RGBA5551, "RA15", MAKE_RGB_INFO(5, 11, 5, 6, 5, 1, 1, 0) },
71d722e3fbSopenharmony_ci	{ DRM_FORMAT_RGBX5551, "RX15", MAKE_RGB_INFO(5, 11, 5, 6, 5, 1, 0, 0) },
72d722e3fbSopenharmony_ci	{ DRM_FORMAT_BGRA5551, "BA15", MAKE_RGB_INFO(5, 1, 5, 6, 5, 11, 1, 0) },
73d722e3fbSopenharmony_ci	{ DRM_FORMAT_BGRX5551, "BX15", MAKE_RGB_INFO(5, 1, 5, 6, 5, 11, 0, 0) },
74d722e3fbSopenharmony_ci	{ DRM_FORMAT_RGB565, "RG16", MAKE_RGB_INFO(5, 11, 6, 5, 5, 0, 0, 0) },
75d722e3fbSopenharmony_ci	{ DRM_FORMAT_BGR565, "BG16", MAKE_RGB_INFO(5, 0, 6, 5, 5, 11, 0, 0) },
76d722e3fbSopenharmony_ci	/* RGB24 */
77d722e3fbSopenharmony_ci	{ DRM_FORMAT_BGR888, "BG24", MAKE_RGB_INFO(8, 0, 8, 8, 8, 16, 0, 0) },
78d722e3fbSopenharmony_ci	{ DRM_FORMAT_RGB888, "RG24", MAKE_RGB_INFO(8, 16, 8, 8, 8, 0, 0, 0) },
79d722e3fbSopenharmony_ci	/* RGB32 */
80d722e3fbSopenharmony_ci	{ DRM_FORMAT_ARGB8888, "AR24", MAKE_RGB_INFO(8, 16, 8, 8, 8, 0, 8, 24) },
81d722e3fbSopenharmony_ci	{ DRM_FORMAT_XRGB8888, "XR24", MAKE_RGB_INFO(8, 16, 8, 8, 8, 0, 0, 0) },
82d722e3fbSopenharmony_ci	{ DRM_FORMAT_ABGR8888, "AB24", MAKE_RGB_INFO(8, 0, 8, 8, 8, 16, 8, 24) },
83d722e3fbSopenharmony_ci	{ DRM_FORMAT_XBGR8888, "XB24", MAKE_RGB_INFO(8, 0, 8, 8, 8, 16, 0, 0) },
84d722e3fbSopenharmony_ci	{ DRM_FORMAT_RGBA8888, "RA24", MAKE_RGB_INFO(8, 24, 8, 16, 8, 8, 8, 0) },
85d722e3fbSopenharmony_ci	{ DRM_FORMAT_RGBX8888, "RX24", MAKE_RGB_INFO(8, 24, 8, 16, 8, 8, 0, 0) },
86d722e3fbSopenharmony_ci	{ DRM_FORMAT_BGRA8888, "BA24", MAKE_RGB_INFO(8, 8, 8, 16, 8, 24, 8, 0) },
87d722e3fbSopenharmony_ci	{ DRM_FORMAT_BGRX8888, "BX24", MAKE_RGB_INFO(8, 8, 8, 16, 8, 24, 0, 0) },
88d722e3fbSopenharmony_ci	{ DRM_FORMAT_ARGB2101010, "AR30", MAKE_RGB_INFO(10, 20, 10, 10, 10, 0, 2, 30) },
89d722e3fbSopenharmony_ci	{ DRM_FORMAT_XRGB2101010, "XR30", MAKE_RGB_INFO(10, 20, 10, 10, 10, 0, 0, 0) },
90d722e3fbSopenharmony_ci	{ DRM_FORMAT_ABGR2101010, "AB30", MAKE_RGB_INFO(10, 0, 10, 10, 10, 20, 2, 30) },
91d722e3fbSopenharmony_ci	{ DRM_FORMAT_XBGR2101010, "XB30", MAKE_RGB_INFO(10, 0, 10, 10, 10, 20, 0, 0) },
92d722e3fbSopenharmony_ci	{ DRM_FORMAT_RGBA1010102, "RA30", MAKE_RGB_INFO(10, 22, 10, 12, 10, 2, 2, 0) },
93d722e3fbSopenharmony_ci	{ DRM_FORMAT_RGBX1010102, "RX30", MAKE_RGB_INFO(10, 22, 10, 12, 10, 2, 0, 0) },
94d722e3fbSopenharmony_ci	{ DRM_FORMAT_BGRA1010102, "BA30", MAKE_RGB_INFO(10, 2, 10, 12, 10, 22, 2, 0) },
95d722e3fbSopenharmony_ci	{ DRM_FORMAT_BGRX1010102, "BX30", MAKE_RGB_INFO(10, 2, 10, 12, 10, 22, 0, 0) },
96d722e3fbSopenharmony_ci	{ DRM_FORMAT_XRGB16161616F, "XR4H", MAKE_RGB_INFO(16, 32, 16, 16, 16, 0, 0, 0) },
97d722e3fbSopenharmony_ci	{ DRM_FORMAT_XBGR16161616F, "XB4H", MAKE_RGB_INFO(16, 0, 16, 16, 16, 32, 0, 0) },
98d722e3fbSopenharmony_ci	{ DRM_FORMAT_ARGB16161616F, "AR4H", MAKE_RGB_INFO(16, 32, 16, 16, 16, 0, 16, 48) },
99d722e3fbSopenharmony_ci	{ DRM_FORMAT_ABGR16161616F, "AB4H", MAKE_RGB_INFO(16, 0, 16, 16, 16, 32, 16, 48) },
100d722e3fbSopenharmony_ci
101d722e3fbSopenharmony_ci};
102d722e3fbSopenharmony_ci
103d722e3fbSopenharmony_ciuint32_t util_format_fourcc(const char *name)
104d722e3fbSopenharmony_ci{
105d722e3fbSopenharmony_ci	unsigned int i;
106d722e3fbSopenharmony_ci
107d722e3fbSopenharmony_ci	for (i = 0; i < ARRAY_SIZE(format_info); i++)
108d722e3fbSopenharmony_ci		if (!strcmp(format_info[i].name, name))
109d722e3fbSopenharmony_ci			return format_info[i].format;
110d722e3fbSopenharmony_ci
111d722e3fbSopenharmony_ci	return 0;
112d722e3fbSopenharmony_ci}
113d722e3fbSopenharmony_ci
114d722e3fbSopenharmony_ciconst struct util_format_info *util_format_info_find(uint32_t format)
115d722e3fbSopenharmony_ci{
116d722e3fbSopenharmony_ci	unsigned int i;
117d722e3fbSopenharmony_ci
118d722e3fbSopenharmony_ci	for (i = 0; i < ARRAY_SIZE(format_info); i++)
119d722e3fbSopenharmony_ci		if (format_info[i].format == format)
120d722e3fbSopenharmony_ci			return &format_info[i];
121d722e3fbSopenharmony_ci
122d722e3fbSopenharmony_ci	return NULL;
123d722e3fbSopenharmony_ci}
124