drm_draw.c 4.3 KB

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