drm_format_internal.h 3.4 KB

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