qp_surface_rgb888.c 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  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: rgb888
  10. static inline void setpixel_rgb888(surface_painter_device_t *surface, uint16_t x, uint16_t y, rgb_t rgb888) {
  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 (memcmp(&surface->rgbbuffer[y * w + x], &rgb888, sizeof(rgb_t)) != 0) {
  19. // Update the dirty region
  20. qp_surface_update_dirty(&surface->dirty, x, y);
  21. // Update the pixel data in the buffer
  22. surface->rgbbuffer[y * w + x] = rgb888;
  23. }
  24. }
  25. static inline void append_pixel_rgb888(surface_painter_device_t *surface, rgb_t rgb888) {
  26. setpixel_rgb888(surface, surface->viewport.pixdata_x, surface->viewport.pixdata_y, rgb888);
  27. qp_surface_increment_pixdata_location(&surface->viewport);
  28. }
  29. static inline void stream_pixdata_rgb888(surface_painter_device_t *surface, const rgb_t *data, uint32_t native_pixel_count) {
  30. for (uint32_t pixel_counter = 0; pixel_counter < native_pixel_count; ++pixel_counter) {
  31. append_pixel_rgb888(surface, data[pixel_counter]);
  32. }
  33. }
  34. // Stream pixel data to the current write position in GRAM
  35. static bool qp_surface_pixdata_rgb888(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_rgb888(surface, (const rgb_t *)pixel_data, native_pixel_count);
  39. return true;
  40. }
  41. // Pixel colour conversion
  42. static bool qp_surface_palette_convert_rgb888(painter_device_t device, int16_t palette_size, qp_pixel_t *palette) {
  43. for (int16_t i = 0; i < palette_size; ++i) {
  44. palette[i].rgb888 = hsv_to_rgb_nocie(palette[i].hsv888);
  45. }
  46. return true;
  47. }
  48. // Append pixels to the target location, keyed by the pixel index
  49. static bool qp_surface_append_pixels_rgb888(painter_device_t device, uint8_t *target_buffer, qp_pixel_t *palette, uint32_t pixel_offset, uint32_t pixel_count, uint8_t *palette_indices) {
  50. rgb_t *buf = (rgb_t *)target_buffer;
  51. for (uint32_t i = 0; i < pixel_count; ++i) {
  52. buf[pixel_offset + i] = palette[palette_indices[i]].rgb888;
  53. }
  54. return true;
  55. }
  56. static bool rgb888_target_pixdata_transfer(painter_driver_t *surface_driver, painter_driver_t *target_driver, uint16_t x, uint16_t y, bool entire_surface) {
  57. surface_painter_device_t *surface_handle = (surface_painter_device_t *)surface_driver;
  58. uint16_t l = entire_surface ? 0 : surface_handle->dirty.l;
  59. uint16_t t = entire_surface ? 0 : surface_handle->dirty.t;
  60. uint16_t r = entire_surface ? (surface_handle->base.panel_width - 1) : surface_handle->dirty.r;
  61. uint16_t b = entire_surface ? (surface_handle->base.panel_height - 1) : surface_handle->dirty.b;
  62. // Set the target drawing area
  63. bool ok = qp_viewport((painter_device_t)target_driver, x + l, y + t, x + r, y + b);
  64. if (!ok) {
  65. qp_dprintf("rgb888_target_pixdata_transfer: fail (could not set target viewport)\n");
  66. return false;
  67. }
  68. // Housekeeping of the amount of pixels to transfer
  69. uint32_t total_pixel_count = (8 * QUANTUM_PAINTER_PIXDATA_BUFFER_SIZE) / surface_driver->native_bits_per_pixel;
  70. uint32_t pixel_counter = 0;
  71. rgb_t *target_buffer = (rgb_t *)qp_internal_global_pixdata_buffer;
  72. // Fill the global pixdata area so that we can start transferring to the panel
  73. for (uint16_t y = t; y <= b; ++y) {
  74. for (uint16_t x = l; x <= r; ++x) {
  75. // Update the target buffer
  76. target_buffer[pixel_counter++] = surface_handle->rgbbuffer[y * surface_handle->base.panel_width + x];
  77. // If we've accumulated enough data, send it
  78. if (pixel_counter == total_pixel_count) {
  79. ok = qp_pixdata((painter_device_t)target_driver, qp_internal_global_pixdata_buffer, pixel_counter);
  80. if (!ok) {
  81. qp_dprintf("rgb888_target_pixdata_transfer: fail (could not stream pixdata to target)\n");
  82. return false;
  83. }
  84. // Reset the counter
  85. pixel_counter = 0;
  86. }
  87. }
  88. }
  89. // If there's any leftover data, send it
  90. if (pixel_counter > 0) {
  91. ok = qp_pixdata((painter_device_t)target_driver, qp_internal_global_pixdata_buffer, pixel_counter);
  92. if (!ok) {
  93. qp_dprintf("rgb888_target_pixdata_transfer: fail (could not stream pixdata to target)\n");
  94. return false;
  95. }
  96. }
  97. return true;
  98. }
  99. static bool qp_surface_append_pixdata_rgb888(painter_device_t device, uint8_t *target_buffer, uint32_t pixdata_offset, uint8_t pixdata_byte) {
  100. target_buffer[pixdata_offset] = pixdata_byte;
  101. return true;
  102. }
  103. const surface_painter_driver_vtable_t rgb888_surface_driver_vtable = {
  104. .base =
  105. {
  106. .init = qp_surface_init,
  107. .power = qp_surface_power,
  108. .clear = qp_surface_clear,
  109. .flush = qp_surface_flush,
  110. .pixdata = qp_surface_pixdata_rgb888,
  111. .viewport = qp_surface_viewport,
  112. .palette_convert = qp_surface_palette_convert_rgb888,
  113. .append_pixels = qp_surface_append_pixels_rgb888,
  114. .append_pixdata = qp_surface_append_pixdata_rgb888,
  115. },
  116. .target_pixdata_transfer = rgb888_target_pixdata_transfer,
  117. };
  118. SURFACE_FACTORY_FUNCTION_IMPL(qp_make_rgb888_surface, rgb888_surface_driver_vtable, 24);
  119. #endif // QUANTUM_PAINTER_SURFACE_ENABLE