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 #include "drm_format_internal.h" 15 16 /** 17 * drm_draw_color_from_xrgb8888 - convert one pixel from xrgb8888 to the desired format 18 * @color: input color, in xrgb8888 format 19 * @format: output format 20 * 21 * Returns: 22 * Color in the format specified, casted to u32. 23 * Or 0 if the format is not supported. 24 */ 25 u32 drm_draw_color_from_xrgb8888(u32 color, u32 format) 26 { 27 switch (format) { 28 case DRM_FORMAT_RGB565: 29 return drm_pixel_xrgb8888_to_rgb565(color); 30 case DRM_FORMAT_RGBA5551: 31 return drm_pixel_xrgb8888_to_rgba5551(color); 32 case DRM_FORMAT_XRGB1555: 33 return drm_pixel_xrgb8888_to_xrgb1555(color); 34 case DRM_FORMAT_ARGB1555: 35 return drm_pixel_xrgb8888_to_argb1555(color); 36 case DRM_FORMAT_RGB888: 37 case DRM_FORMAT_XRGB8888: 38 return color; 39 case DRM_FORMAT_ARGB8888: 40 return drm_pixel_xrgb8888_to_argb8888(color); 41 case DRM_FORMAT_XBGR8888: 42 return drm_pixel_xrgb8888_to_xbgr8888(color); 43 case DRM_FORMAT_ABGR8888: 44 return drm_pixel_xrgb8888_to_abgr8888(color); 45 case DRM_FORMAT_XRGB2101010: 46 return drm_pixel_xrgb8888_to_xrgb2101010(color); 47 case DRM_FORMAT_ARGB2101010: 48 return drm_pixel_xrgb8888_to_argb2101010(color); 49 case DRM_FORMAT_ABGR2101010: 50 return drm_pixel_xrgb8888_to_abgr2101010(color); 51 default: 52 WARN_ONCE(1, "Can't convert to %p4cc\n", &format); 53 return 0; 54 } 55 } 56 EXPORT_SYMBOL(drm_draw_color_from_xrgb8888); 57 58 /* 59 * Blit functions 60 */ 61 void drm_draw_blit16(struct iosys_map *dmap, unsigned int dpitch, 62 const u8 *sbuf8, unsigned int spitch, 63 unsigned int height, unsigned int width, 64 unsigned int scale, u16 fg16) 65 { 66 unsigned int y, x; 67 68 for (y = 0; y < height; y++) 69 for (x = 0; x < width; x++) 70 if (drm_draw_is_pixel_fg(sbuf8, spitch, x / scale, y / scale)) 71 iosys_map_wr(dmap, y * dpitch + x * sizeof(u16), u16, fg16); 72 } 73 EXPORT_SYMBOL(drm_draw_blit16); 74 75 void drm_draw_blit24(struct iosys_map *dmap, unsigned int dpitch, 76 const u8 *sbuf8, unsigned int spitch, 77 unsigned int height, unsigned int width, 78 unsigned int scale, u32 fg32) 79 { 80 unsigned int y, x; 81 82 for (y = 0; y < height; y++) { 83 for (x = 0; x < width; x++) { 84 u32 off = y * dpitch + x * 3; 85 86 if (drm_draw_is_pixel_fg(sbuf8, spitch, x / scale, y / scale)) { 87 /* write blue-green-red to output in little endianness */ 88 iosys_map_wr(dmap, off, u8, (fg32 & 0x000000FF) >> 0); 89 iosys_map_wr(dmap, off + 1, u8, (fg32 & 0x0000FF00) >> 8); 90 iosys_map_wr(dmap, off + 2, u8, (fg32 & 0x00FF0000) >> 16); 91 } 92 } 93 } 94 } 95 EXPORT_SYMBOL(drm_draw_blit24); 96 97 void drm_draw_blit32(struct iosys_map *dmap, unsigned int dpitch, 98 const u8 *sbuf8, unsigned int spitch, 99 unsigned int height, unsigned int width, 100 unsigned int scale, u32 fg32) 101 { 102 unsigned int y, x; 103 104 for (y = 0; y < height; y++) 105 for (x = 0; x < width; x++) 106 if (drm_draw_is_pixel_fg(sbuf8, spitch, x / scale, y / scale)) 107 iosys_map_wr(dmap, y * dpitch + x * sizeof(u32), u32, fg32); 108 } 109 EXPORT_SYMBOL(drm_draw_blit32); 110 111 /* 112 * Fill functions 113 */ 114 void drm_draw_fill16(struct iosys_map *dmap, unsigned int dpitch, 115 unsigned int height, unsigned int width, 116 u16 color) 117 { 118 unsigned int y, x; 119 120 for (y = 0; y < height; y++) 121 for (x = 0; x < width; x++) 122 iosys_map_wr(dmap, y * dpitch + x * sizeof(u16), u16, color); 123 } 124 EXPORT_SYMBOL(drm_draw_fill16); 125 126 void drm_draw_fill24(struct iosys_map *dmap, unsigned int dpitch, 127 unsigned int height, unsigned int width, 128 u16 color) 129 { 130 unsigned int y, x; 131 132 for (y = 0; y < height; y++) { 133 for (x = 0; x < width; x++) { 134 unsigned int off = y * dpitch + x * 3; 135 136 /* write blue-green-red to output in little endianness */ 137 iosys_map_wr(dmap, off, u8, (color & 0x000000FF) >> 0); 138 iosys_map_wr(dmap, off + 1, u8, (color & 0x0000FF00) >> 8); 139 iosys_map_wr(dmap, off + 2, u8, (color & 0x00FF0000) >> 16); 140 } 141 } 142 } 143 EXPORT_SYMBOL(drm_draw_fill24); 144 145 void drm_draw_fill32(struct iosys_map *dmap, unsigned int dpitch, 146 unsigned int height, unsigned int width, 147 u32 color) 148 { 149 unsigned int y, x; 150 151 for (y = 0; y < height; y++) 152 for (x = 0; x < width; x++) 153 iosys_map_wr(dmap, y * dpitch + x * sizeof(u32), u32, color); 154 } 155 EXPORT_SYMBOL(drm_draw_fill32); 156