logo

qmk_firmware

custom branch of QMK firmware git clone https://anongit.hacktivis.me/git/qmk_firmware.git

qp_surface_rgb565.c (6304B)


  1. // Copyright 2022 Nick Brassel (@tzarc)
  2. // SPDX-License-Identifier: GPL-2.0-or-later
  3. #ifdef QUANTUM_PAINTER_SURFACE_ENABLE
  4. # include "color.h"
  5. # include "qp_draw.h"
  6. # include "qp_surface_internal.h"
  7. # include "qp_comms_dummy.h"
  8. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  9. // Surface driver impl: rgb565
  10. static inline void setpixel_rgb565(surface_painter_device_t *surface, uint16_t x, uint16_t y, uint16_t rgb565) {
  11. uint16_t w = surface->base.panel_width;
  12. uint16_t h = surface->base.panel_height;
  13. // Drop out if it's off-screen
  14. if (x >= w || y >= h) {
  15. return;
  16. }
  17. // Skip messing with the dirty info if the original value already matches
  18. if (surface->u16buffer[y * w + x] != rgb565) {
  19. // Update the dirty region
  20. qp_surface_update_dirty(&surface->dirty, x, y);
  21. // Update the pixel data in the buffer
  22. surface->u16buffer[y * w + x] = rgb565;
  23. }
  24. }
  25. static inline void append_pixel_rgb565(surface_painter_device_t *surface, uint16_t rgb565) {
  26. setpixel_rgb565(surface, surface->viewport.pixdata_x, surface->viewport.pixdata_y, rgb565);
  27. qp_surface_increment_pixdata_location(&surface->viewport);
  28. }
  29. static inline void stream_pixdata_rgb565(surface_painter_device_t *surface, const uint16_t *data, uint32_t native_pixel_count) {
  30. for (uint32_t pixel_counter = 0; pixel_counter < native_pixel_count; ++pixel_counter) {
  31. append_pixel_rgb565(surface, data[pixel_counter]);
  32. }
  33. }
  34. // Stream pixel data to the current write position in GRAM
  35. static bool qp_surface_pixdata_rgb565(painter_device_t device, const void *pixel_data, uint32_t native_pixel_count) {
  36. painter_driver_t * driver = (painter_driver_t *)device;
  37. surface_painter_device_t *surface = (surface_painter_device_t *)driver;
  38. stream_pixdata_rgb565(surface, (const uint16_t *)pixel_data, native_pixel_count);
  39. return true;
  40. }
  41. // Pixel colour conversion
  42. static bool qp_surface_palette_convert_rgb565_swapped(painter_device_t device, int16_t palette_size, qp_pixel_t *palette) {
  43. for (int16_t i = 0; i < palette_size; ++i) {
  44. rgb_t rgb = hsv_to_rgb_nocie((hsv_t){palette[i].hsv888.h, palette[i].hsv888.s, palette[i].hsv888.v});
  45. uint16_t rgb565 = (((uint16_t)rgb.r) >> 3) << 11 | (((uint16_t)rgb.g) >> 2) << 5 | (((uint16_t)rgb.b) >> 3);
  46. palette[i].rgb565 = __builtin_bswap16(rgb565);
  47. }
  48. return true;
  49. }
  50. // Append pixels to the target location, keyed by the pixel index
  51. static bool qp_surface_append_pixels_rgb565(painter_device_t device, uint8_t *target_buffer, qp_pixel_t *palette, uint32_t pixel_offset, uint32_t pixel_count, uint8_t *palette_indices) {
  52. uint16_t *buf = (uint16_t *)target_buffer;
  53. for (uint32_t i = 0; i < pixel_count; ++i) {
  54. buf[pixel_offset + i] = palette[palette_indices[i]].rgb565;
  55. }
  56. return true;
  57. }
  58. static bool rgb565_target_pixdata_transfer(painter_driver_t *surface_driver, painter_driver_t *target_driver, uint16_t x, uint16_t y, bool entire_surface) {
  59. surface_painter_device_t *surface_handle = (surface_painter_device_t *)surface_driver;
  60. uint16_t l = entire_surface ? 0 : surface_handle->dirty.l;
  61. uint16_t t = entire_surface ? 0 : surface_handle->dirty.t;
  62. uint16_t r = entire_surface ? (surface_handle->base.panel_width - 1) : surface_handle->dirty.r;
  63. uint16_t b = entire_surface ? (surface_handle->base.panel_height - 1) : surface_handle->dirty.b;
  64. // Set the target drawing area
  65. bool ok = qp_viewport((painter_device_t)target_driver, x + l, y + t, x + r, y + b);
  66. if (!ok) {
  67. qp_dprintf("rgb565_target_pixdata_transfer: fail (could not set target viewport)\n");
  68. return false;
  69. }
  70. // Housekeeping of the amount of pixels to transfer
  71. uint32_t total_pixel_count = (8 * QUANTUM_PAINTER_PIXDATA_BUFFER_SIZE) / surface_driver->native_bits_per_pixel;
  72. uint32_t pixel_counter = 0;
  73. uint16_t *target_buffer = (uint16_t *)qp_internal_global_pixdata_buffer;
  74. // Fill the global pixdata area so that we can start transferring to the panel
  75. for (uint16_t y = t; y <= b; ++y) {
  76. for (uint16_t x = l; x <= r; ++x) {
  77. // Update the target buffer
  78. target_buffer[pixel_counter++] = surface_handle->u16buffer[y * surface_handle->base.panel_width + x];
  79. // If we've accumulated enough data, send it
  80. if (pixel_counter == total_pixel_count) {
  81. ok = qp_pixdata((painter_device_t)target_driver, qp_internal_global_pixdata_buffer, pixel_counter);
  82. if (!ok) {
  83. qp_dprintf("rgb565_target_pixdata_transfer: fail (could not stream pixdata to target)\n");
  84. return false;
  85. }
  86. // Reset the counter
  87. pixel_counter = 0;
  88. }
  89. }
  90. }
  91. // If there's any leftover data, send it
  92. if (pixel_counter > 0) {
  93. ok = qp_pixdata((painter_device_t)target_driver, qp_internal_global_pixdata_buffer, pixel_counter);
  94. if (!ok) {
  95. qp_dprintf("rgb565_target_pixdata_transfer: fail (could not stream pixdata to target)\n");
  96. return false;
  97. }
  98. }
  99. return true;
  100. }
  101. static bool qp_surface_append_pixdata_rgb565(painter_device_t device, uint8_t *target_buffer, uint32_t pixdata_offset, uint8_t pixdata_byte) {
  102. target_buffer[pixdata_offset] = pixdata_byte;
  103. return true;
  104. }
  105. const surface_painter_driver_vtable_t rgb565_surface_driver_vtable = {
  106. .base =
  107. {
  108. .init = qp_surface_init,
  109. .power = qp_surface_power,
  110. .clear = qp_surface_clear,
  111. .flush = qp_surface_flush,
  112. .pixdata = qp_surface_pixdata_rgb565,
  113. .viewport = qp_surface_viewport,
  114. .palette_convert = qp_surface_palette_convert_rgb565_swapped,
  115. .append_pixels = qp_surface_append_pixels_rgb565,
  116. .append_pixdata = qp_surface_append_pixdata_rgb565,
  117. },
  118. .target_pixdata_transfer = rgb565_target_pixdata_transfer,
  119. };
  120. SURFACE_FACTORY_FUNCTION_IMPL(qp_make_rgb565_surface, rgb565_surface_driver_vtable, 16);
  121. #endif // QUANTUM_PAINTER_SURFACE_ENABLE