xref: /linux/drivers/gpu/drm/drm_draw.c (revision 2c1ed907520c50326b8f604907a8478b27881a2e)
1 // SPDX-License-Identifier: GPL-2.0 or MIT
2 /*
3  * Copyright (c) 2023 Red Hat.
4  * Author: Jocelyn Falempe <jfalempe@redhat.com>
5  */
6 
7 #include <linux/bits.h>
8 #include <linux/iosys-map.h>
9 #include <linux/types.h>
10 
11 #include <drm/drm_fourcc.h>
12 
13 #include "drm_draw_internal.h"
14 
15 /*
16  * Conversions from xrgb8888
17  */
18 
convert_xrgb8888_to_rgb565(u32 pix)19 static u16 convert_xrgb8888_to_rgb565(u32 pix)
20 {
21 	return ((pix & 0x00F80000) >> 8) |
22 	       ((pix & 0x0000FC00) >> 5) |
23 	       ((pix & 0x000000F8) >> 3);
24 }
25 
convert_xrgb8888_to_rgba5551(u32 pix)26 static u16 convert_xrgb8888_to_rgba5551(u32 pix)
27 {
28 	return ((pix & 0x00f80000) >> 8) |
29 	       ((pix & 0x0000f800) >> 5) |
30 	       ((pix & 0x000000f8) >> 2) |
31 	       BIT(0); /* set alpha bit */
32 }
33 
convert_xrgb8888_to_xrgb1555(u32 pix)34 static u16 convert_xrgb8888_to_xrgb1555(u32 pix)
35 {
36 	return ((pix & 0x00f80000) >> 9) |
37 	       ((pix & 0x0000f800) >> 6) |
38 	       ((pix & 0x000000f8) >> 3);
39 }
40 
convert_xrgb8888_to_argb1555(u32 pix)41 static u16 convert_xrgb8888_to_argb1555(u32 pix)
42 {
43 	return BIT(15) | /* set alpha bit */
44 	       ((pix & 0x00f80000) >> 9) |
45 	       ((pix & 0x0000f800) >> 6) |
46 	       ((pix & 0x000000f8) >> 3);
47 }
48 
convert_xrgb8888_to_argb8888(u32 pix)49 static u32 convert_xrgb8888_to_argb8888(u32 pix)
50 {
51 	return pix | GENMASK(31, 24); /* fill alpha bits */
52 }
53 
convert_xrgb8888_to_xbgr8888(u32 pix)54 static u32 convert_xrgb8888_to_xbgr8888(u32 pix)
55 {
56 	return ((pix & 0x00ff0000) >> 16) <<  0 |
57 	       ((pix & 0x0000ff00) >>  8) <<  8 |
58 	       ((pix & 0x000000ff) >>  0) << 16 |
59 	       ((pix & 0xff000000) >> 24) << 24;
60 }
61 
convert_xrgb8888_to_abgr8888(u32 pix)62 static u32 convert_xrgb8888_to_abgr8888(u32 pix)
63 {
64 	return ((pix & 0x00ff0000) >> 16) <<  0 |
65 	       ((pix & 0x0000ff00) >>  8) <<  8 |
66 	       ((pix & 0x000000ff) >>  0) << 16 |
67 	       GENMASK(31, 24); /* fill alpha bits */
68 }
69 
convert_xrgb8888_to_xrgb2101010(u32 pix)70 static u32 convert_xrgb8888_to_xrgb2101010(u32 pix)
71 {
72 	pix = ((pix & 0x000000FF) << 2) |
73 	      ((pix & 0x0000FF00) << 4) |
74 	      ((pix & 0x00FF0000) << 6);
75 	return pix | ((pix >> 8) & 0x00300C03);
76 }
77 
convert_xrgb8888_to_argb2101010(u32 pix)78 static u32 convert_xrgb8888_to_argb2101010(u32 pix)
79 {
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);
84 }
85 
convert_xrgb8888_to_abgr2101010(u32 pix)86 static u32 convert_xrgb8888_to_abgr2101010(u32 pix)
87 {
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);
92 }
93 
94 /**
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
98  *
99  * Returns:
100  * Color in the format specified, casted to u32.
101  * Or 0 if the format is not supported.
102  */
drm_draw_color_from_xrgb8888(u32 color,u32 format)103 u32 drm_draw_color_from_xrgb8888(u32 color, u32 format)
104 {
105 	switch (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:
116 		return color;
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);
129 	default:
130 		WARN_ONCE(1, "Can't convert to %p4cc\n", &format);
131 		return 0;
132 	}
133 }
134 EXPORT_SYMBOL(drm_draw_color_from_xrgb8888);
135 
136 /*
137  * Blit functions
138  */
drm_draw_blit16(struct iosys_map * dmap,unsigned int dpitch,const u8 * sbuf8,unsigned int spitch,unsigned int height,unsigned int width,unsigned int scale,u16 fg16)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)
143 {
144 	unsigned int y, x;
145 
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);
150 }
151 EXPORT_SYMBOL(drm_draw_blit16);
152 
drm_draw_blit24(struct iosys_map * dmap,unsigned int dpitch,const u8 * sbuf8,unsigned int spitch,unsigned int height,unsigned int width,unsigned int scale,u32 fg32)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)
157 {
158 	unsigned int y, x;
159 
160 	for (y = 0; y < height; y++) {
161 		for (x = 0; x < width; x++) {
162 			u32 off = y * dpitch + x * 3;
163 
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);
169 			}
170 		}
171 	}
172 }
173 EXPORT_SYMBOL(drm_draw_blit24);
174 
drm_draw_blit32(struct iosys_map * dmap,unsigned int dpitch,const u8 * sbuf8,unsigned int spitch,unsigned int height,unsigned int width,unsigned int scale,u32 fg32)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)
179 {
180 	unsigned int y, x;
181 
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);
186 }
187 EXPORT_SYMBOL(drm_draw_blit32);
188 
189 /*
190  * Fill functions
191  */
drm_draw_fill16(struct iosys_map * dmap,unsigned int dpitch,unsigned int height,unsigned int width,u16 color)192 void drm_draw_fill16(struct iosys_map *dmap, unsigned int dpitch,
193 		     unsigned int height, unsigned int width,
194 		     u16 color)
195 {
196 	unsigned int y, x;
197 
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);
201 }
202 EXPORT_SYMBOL(drm_draw_fill16);
203 
drm_draw_fill24(struct iosys_map * dmap,unsigned int dpitch,unsigned int height,unsigned int width,u16 color)204 void drm_draw_fill24(struct iosys_map *dmap, unsigned int dpitch,
205 		     unsigned int height, unsigned int width,
206 		     u16 color)
207 {
208 	unsigned int y, x;
209 
210 	for (y = 0; y < height; y++) {
211 		for (x = 0; x < width; x++) {
212 			unsigned int off = y * dpitch + x * 3;
213 
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);
218 		}
219 	}
220 }
221 EXPORT_SYMBOL(drm_draw_fill24);
222 
drm_draw_fill32(struct iosys_map * dmap,unsigned int dpitch,unsigned int height,unsigned int width,u32 color)223 void drm_draw_fill32(struct iosys_map *dmap, unsigned int dpitch,
224 		     unsigned int height, unsigned int width,
225 		     u32 color)
226 {
227 	unsigned int y, x;
228 
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);
232 }
233 EXPORT_SYMBOL(drm_draw_fill32);
234