• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /* SPDX-License-Identifier: GPL-2.0 or MIT */
2 
3 #ifndef DRM_FORMAT_INTERNAL_H
4 #define DRM_FORMAT_INTERNAL_H
5 
6 #include <linux/bits.h>
7 #include <linux/types.h>
8 
9 /*
10  * Each pixel-format conversion helper takes a raw pixel in a
11  * specific input format and returns a raw pixel in a specific
12  * output format. All pixels are in little-endian byte order.
13  *
14  * Function names are
15  *
16  *   drm_pixel_<input>_to_<output>_<algorithm>()
17  *
18  * where <input> and <output> refer to pixel formats. The
19  * <algorithm> is optional and hints to the method used for the
20  * conversion. Helpers with no algorithm given apply pixel-bit
21  * shifting.
22  *
23  * The argument type is u32. We expect this to be wide enough to
24  * hold all conversion input from 32-bit RGB to any output format.
25  * The Linux kernel should avoid format conversion for anything
26  * but XRGB8888 input data. Converting from other format can still
27  * be acceptable in some cases.
28  *
29  * The return type is u32. It is wide enough to hold all conversion
30  * output from XRGB8888. For output formats wider than 32 bit, a
31  * return type of u64 would be acceptable.
32  */
33 
34 /*
35  * Conversions from XRGB8888
36  */
37 
drm_pixel_xrgb8888_to_rgb565(u32 pix)38 static inline u32 drm_pixel_xrgb8888_to_rgb565(u32 pix)
39 {
40 	return ((pix & 0x00f80000) >> 8) |
41 	       ((pix & 0x0000fc00) >> 5) |
42 	       ((pix & 0x000000f8) >> 3);
43 }
44 
drm_pixel_xrgb8888_to_rgbx5551(u32 pix)45 static inline u32 drm_pixel_xrgb8888_to_rgbx5551(u32 pix)
46 {
47 	return ((pix & 0x00f80000) >> 8) |
48 	       ((pix & 0x0000f800) >> 5) |
49 	       ((pix & 0x000000f8) >> 2);
50 }
51 
drm_pixel_xrgb8888_to_rgba5551(u32 pix)52 static inline u32 drm_pixel_xrgb8888_to_rgba5551(u32 pix)
53 {
54 	return drm_pixel_xrgb8888_to_rgbx5551(pix) |
55 	       BIT(0); /* set alpha bit */
56 }
57 
drm_pixel_xrgb8888_to_xrgb1555(u32 pix)58 static inline u32 drm_pixel_xrgb8888_to_xrgb1555(u32 pix)
59 {
60 	return ((pix & 0x00f80000) >> 9) |
61 	       ((pix & 0x0000f800) >> 6) |
62 	       ((pix & 0x000000f8) >> 3);
63 }
64 
drm_pixel_xrgb8888_to_argb1555(u32 pix)65 static inline u32 drm_pixel_xrgb8888_to_argb1555(u32 pix)
66 {
67 	return BIT(15) | /* set alpha bit */
68 	       drm_pixel_xrgb8888_to_xrgb1555(pix);
69 }
70 
drm_pixel_xrgb8888_to_argb8888(u32 pix)71 static inline u32 drm_pixel_xrgb8888_to_argb8888(u32 pix)
72 {
73 	return GENMASK(31, 24) | /* fill alpha bits */
74 	       pix;
75 }
76 
drm_pixel_xrgb8888_to_xbgr8888(u32 pix)77 static inline u32 drm_pixel_xrgb8888_to_xbgr8888(u32 pix)
78 {
79 	return ((pix & 0xff000000)) | /* also copy filler bits */
80 	       ((pix & 0x00ff0000) >> 16) |
81 	       ((pix & 0x0000ff00)) |
82 	       ((pix & 0x000000ff) << 16);
83 }
84 
drm_pixel_xrgb8888_to_bgrx8888(u32 pix)85 static inline u32 drm_pixel_xrgb8888_to_bgrx8888(u32 pix)
86 {
87 	return ((pix & 0xff000000) >> 24) | /* also copy filler bits */
88 	       ((pix & 0x00ff0000) >> 8) |
89 	       ((pix & 0x0000ff00) << 8) |
90 	       ((pix & 0x000000ff) << 24);
91 }
92 
drm_pixel_xrgb8888_to_abgr8888(u32 pix)93 static inline u32 drm_pixel_xrgb8888_to_abgr8888(u32 pix)
94 {
95 	return GENMASK(31, 24) | /* fill alpha bits */
96 	       drm_pixel_xrgb8888_to_xbgr8888(pix);
97 }
98 
drm_pixel_xrgb8888_to_xrgb2101010(u32 pix)99 static inline u32 drm_pixel_xrgb8888_to_xrgb2101010(u32 pix)
100 {
101 	pix = ((pix & 0x000000ff) << 2) |
102 	      ((pix & 0x0000ff00) << 4) |
103 	      ((pix & 0x00ff0000) << 6);
104 	return pix | ((pix >> 8) & 0x00300c03);
105 }
106 
drm_pixel_xrgb8888_to_argb2101010(u32 pix)107 static inline u32 drm_pixel_xrgb8888_to_argb2101010(u32 pix)
108 {
109 	return GENMASK(31, 30) | /* set alpha bits */
110 	       drm_pixel_xrgb8888_to_xrgb2101010(pix);
111 }
112 
drm_pixel_xrgb8888_to_xbgr2101010(u32 pix)113 static inline u32 drm_pixel_xrgb8888_to_xbgr2101010(u32 pix)
114 {
115 	pix = ((pix & 0x00ff0000) >> 14) |
116 	      ((pix & 0x0000ff00) << 4) |
117 	      ((pix & 0x000000ff) << 22);
118 	return pix | ((pix >> 8) & 0x00300c03);
119 }
120 
drm_pixel_xrgb8888_to_abgr2101010(u32 pix)121 static inline u32 drm_pixel_xrgb8888_to_abgr2101010(u32 pix)
122 {
123 	return GENMASK(31, 30) | /* set alpha bits */
124 	       drm_pixel_xrgb8888_to_xbgr2101010(pix);
125 }
126 
127 #endif
128