1 // SPDX-License-Identifier: GPL-2.0 or MIT
3 * Copyright (c) 2023 Red Hat.
7 #include <linux/bits.h>
8 #include <linux/iosys-map.h>
9 #include <linux/types.h>
11 #include <drm/drm_fourcc.h>
13 #include "drm_draw_internal.h"
16 * Conversions from xrgb8888
19 static u16 convert_xrgb8888_to_rgb565(u32 pix)
21 return ((pix & 0x00F80000) >> 8) |
22 ((pix & 0x0000FC00) >> 5) |
23 ((pix & 0x000000F8) >> 3);
26 static u16 convert_xrgb8888_to_rgba5551(u32 pix)
28 return ((pix & 0x00f80000) >> 8) |
29 ((pix & 0x0000f800) >> 5) |
30 ((pix & 0x000000f8) >> 2) |
31 BIT(0); /* set alpha bit */
34 static u16 convert_xrgb8888_to_xrgb1555(u32 pix)
36 return ((pix & 0x00f80000) >> 9) |
37 ((pix & 0x0000f800) >> 6) |
38 ((pix & 0x000000f8) >> 3);
41 static u16 convert_xrgb8888_to_argb1555(u32 pix)
43 return BIT(15) | /* set alpha bit */
44 ((pix & 0x00f80000) >> 9) |
45 ((pix & 0x0000f800) >> 6) |
46 ((pix & 0x000000f8) >> 3);
49 static u32 convert_xrgb8888_to_argb8888(u32 pix)
51 return pix | GENMASK(31, 24); /* fill alpha bits */
54 static u32 convert_xrgb8888_to_xbgr8888(u32 pix)
56 return ((pix & 0x00ff0000) >> 16) << 0 |
57 ((pix & 0x0000ff00) >> 8) << 8 |
58 ((pix & 0x000000ff) >> 0) << 16 |
59 ((pix & 0xff000000) >> 24) << 24;
62 static u32 convert_xrgb8888_to_abgr8888(u32 pix)
64 return ((pix & 0x00ff0000) >> 16) << 0 |
65 ((pix & 0x0000ff00) >> 8) << 8 |
66 ((pix & 0x000000ff) >> 0) << 16 |
67 GENMASK(31, 24); /* fill alpha bits */
70 static u32 convert_xrgb8888_to_xrgb2101010(u32 pix)
72 pix = ((pix & 0x000000FF) << 2) |
73 ((pix & 0x0000FF00) << 4) |
74 ((pix & 0x00FF0000) << 6);
75 return pix | ((pix >> 8) & 0x00300C03);
78 static u32 convert_xrgb8888_to_argb2101010(u32 pix)
80 pix = ((pix & 0x000000FF) << 2) |
81 ((pix & 0x0000FF00) << 4) |
82 ((pix & 0x00FF0000) << 6);
83 return GENMASK(31, 30) /* set alpha bits */ | pix | ((pix >> 8) & 0x00300C03);
86 static u32 convert_xrgb8888_to_abgr2101010(u32 pix)
88 pix = ((pix & 0x00FF0000) >> 14) |
89 ((pix & 0x0000FF00) << 4) |
90 ((pix & 0x000000FF) << 22);
91 return GENMASK(31, 30) /* set alpha bits */ | pix | ((pix >> 8) & 0x00300C03);
95 * drm_draw_color_from_xrgb8888 - convert one pixel from xrgb8888 to the desired format
96 * @color: input color, in xrgb8888 format
97 * @format: output format
100 * Color in the format specified, casted to u32.
101 * Or 0 if the format is not supported.
103 u32 drm_draw_color_from_xrgb8888(u32 color, u32 format)
106 case DRM_FORMAT_RGB565:
107 return convert_xrgb8888_to_rgb565(color);
108 case DRM_FORMAT_RGBA5551:
109 return convert_xrgb8888_to_rgba5551(color);
110 case DRM_FORMAT_XRGB1555:
111 return convert_xrgb8888_to_xrgb1555(color);
112 case DRM_FORMAT_ARGB1555:
113 return convert_xrgb8888_to_argb1555(color);
114 case DRM_FORMAT_RGB888:
115 case DRM_FORMAT_XRGB8888:
117 case DRM_FORMAT_ARGB8888:
118 return convert_xrgb8888_to_argb8888(color);
119 case DRM_FORMAT_XBGR8888:
120 return convert_xrgb8888_to_xbgr8888(color);
121 case DRM_FORMAT_ABGR8888:
122 return convert_xrgb8888_to_abgr8888(color);
123 case DRM_FORMAT_XRGB2101010:
124 return convert_xrgb8888_to_xrgb2101010(color);
125 case DRM_FORMAT_ARGB2101010:
126 return convert_xrgb8888_to_argb2101010(color);
127 case DRM_FORMAT_ABGR2101010:
128 return convert_xrgb8888_to_abgr2101010(color);
130 WARN_ONCE(1, "Can't convert to %p4cc\n", &format);
134 EXPORT_SYMBOL(drm_draw_color_from_xrgb8888);
139 void drm_draw_blit16(struct iosys_map *dmap, unsigned int dpitch,
140 const u8 *sbuf8, unsigned int spitch,
141 unsigned int height, unsigned int width,
142 unsigned int scale, u16 fg16)
146 for (y = 0; y < height; y++)
147 for (x = 0; x < width; x++)
148 if (drm_draw_is_pixel_fg(sbuf8, spitch, x / scale, y / scale))
149 iosys_map_wr(dmap, y * dpitch + x * sizeof(u16), u16, fg16);
151 EXPORT_SYMBOL(drm_draw_blit16);
153 void drm_draw_blit24(struct iosys_map *dmap, unsigned int dpitch,
154 const u8 *sbuf8, unsigned int spitch,
155 unsigned int height, unsigned int width,
156 unsigned int scale, u32 fg32)
160 for (y = 0; y < height; y++) {
161 for (x = 0; x < width; x++) {
162 u32 off = y * dpitch + x * 3;
164 if (drm_draw_is_pixel_fg(sbuf8, spitch, x / scale, y / scale)) {
165 /* write blue-green-red to output in little endianness */
166 iosys_map_wr(dmap, off, u8, (fg32 & 0x000000FF) >> 0);
167 iosys_map_wr(dmap, off + 1, u8, (fg32 & 0x0000FF00) >> 8);
168 iosys_map_wr(dmap, off + 2, u8, (fg32 & 0x00FF0000) >> 16);
173 EXPORT_SYMBOL(drm_draw_blit24);
175 void drm_draw_blit32(struct iosys_map *dmap, unsigned int dpitch,
176 const u8 *sbuf8, unsigned int spitch,
177 unsigned int height, unsigned int width,
178 unsigned int scale, u32 fg32)
182 for (y = 0; y < height; y++)
183 for (x = 0; x < width; x++)
184 if (drm_draw_is_pixel_fg(sbuf8, spitch, x / scale, y / scale))
185 iosys_map_wr(dmap, y * dpitch + x * sizeof(u32), u32, fg32);
187 EXPORT_SYMBOL(drm_draw_blit32);
192 void drm_draw_fill16(struct iosys_map *dmap, unsigned int dpitch,
193 unsigned int height, unsigned int width,
198 for (y = 0; y < height; y++)
199 for (x = 0; x < width; x++)
200 iosys_map_wr(dmap, y * dpitch + x * sizeof(u16), u16, color);
202 EXPORT_SYMBOL(drm_draw_fill16);
204 void drm_draw_fill24(struct iosys_map *dmap, unsigned int dpitch,
205 unsigned int height, unsigned int width,
210 for (y = 0; y < height; y++) {
211 for (x = 0; x < width; x++) {
212 unsigned int off = y * dpitch + x * 3;
214 /* write blue-green-red to output in little endianness */
215 iosys_map_wr(dmap, off, u8, (color & 0x000000FF) >> 0);
216 iosys_map_wr(dmap, off + 1, u8, (color & 0x0000FF00) >> 8);
217 iosys_map_wr(dmap, off + 2, u8, (color & 0x00FF0000) >> 16);
221 EXPORT_SYMBOL(drm_draw_fill24);
223 void drm_draw_fill32(struct iosys_map *dmap, unsigned int dpitch,
224 unsigned int height, unsigned int width,
229 for (y = 0; y < height; y++)
230 for (x = 0; x < width; x++)
231 iosys_map_wr(dmap, y * dpitch + x * sizeof(u32), u32, color);
233 EXPORT_SYMBOL(drm_draw_fill32);