qp_rgb565_surface.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277
  1. // Copyright 2022 Nick Brassel (@tzarc)
  2. // SPDX-License-Identifier: GPL-2.0-or-later
  3. #include "color.h"
  4. #include "qp_rgb565_surface.h"
  5. #include "qp_draw.h"
  6. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  7. // Common
  8. // Device definition
  9. typedef struct rgb565_surface_painter_device_t {
  10. struct painter_driver_t base; // must be first, so it can be cast to/from the painter_device_t* type
  11. // The target buffer
  12. uint16_t *buffer;
  13. // Manually manage the viewport for streaming pixel data to the display
  14. uint16_t viewport_l;
  15. uint16_t viewport_t;
  16. uint16_t viewport_r;
  17. uint16_t viewport_b;
  18. // Current write location to the display when streaming pixel data
  19. uint16_t pixdata_x;
  20. uint16_t pixdata_y;
  21. // Maintain a dirty region so we can stream only what we need
  22. bool is_dirty;
  23. uint16_t dirty_l;
  24. uint16_t dirty_t;
  25. uint16_t dirty_r;
  26. uint16_t dirty_b;
  27. } rgb565_surface_painter_device_t;
  28. // Driver storage
  29. rgb565_surface_painter_device_t surface_drivers[RGB565_SURFACE_NUM_DEVICES] = {0};
  30. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  31. // Helpers
  32. static inline void increment_pixdata_location(rgb565_surface_painter_device_t *surface) {
  33. // Increment the X-position
  34. surface->pixdata_x++;
  35. // If the x-coord has gone past the right-side edge, loop it back around and increment the y-coord
  36. if (surface->pixdata_x > surface->viewport_r) {
  37. surface->pixdata_x = surface->viewport_l;
  38. surface->pixdata_y++;
  39. }
  40. // If the y-coord has gone past the bottom, loop it back to the top
  41. if (surface->pixdata_y > surface->viewport_b) {
  42. surface->pixdata_y = surface->viewport_t;
  43. }
  44. }
  45. static inline void setpixel(rgb565_surface_painter_device_t *surface, uint16_t x, uint16_t y, uint16_t rgb565) {
  46. // Skip messing with the dirty info if the original value already matches
  47. if (surface->buffer[y * surface->base.panel_width + x] != rgb565) {
  48. // Maintain dirty region
  49. if (surface->dirty_l > x) {
  50. surface->dirty_l = x;
  51. }
  52. if (surface->dirty_r < x) {
  53. surface->dirty_r = x;
  54. }
  55. if (surface->dirty_t > y) {
  56. surface->dirty_t = y;
  57. }
  58. if (surface->dirty_b < y) {
  59. surface->dirty_b = y;
  60. }
  61. // Always dirty after a setpixel
  62. surface->is_dirty = true;
  63. // Update the pixel data in the buffer
  64. surface->buffer[y * surface->base.panel_width + x] = rgb565;
  65. }
  66. }
  67. static inline void append_pixel(rgb565_surface_painter_device_t *surface, uint16_t rgb565) {
  68. setpixel(surface, surface->pixdata_x, surface->pixdata_y, rgb565);
  69. increment_pixdata_location(surface);
  70. }
  71. static inline void stream_pixdata(rgb565_surface_painter_device_t *surface, const uint16_t *data, uint32_t native_pixel_count) {
  72. for (uint32_t pixel_counter = 0; pixel_counter < native_pixel_count; ++pixel_counter) {
  73. append_pixel(surface, data[pixel_counter]);
  74. }
  75. }
  76. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  77. // Driver vtable
  78. static bool qp_rgb565_surface_init(painter_device_t device, painter_rotation_t rotation) {
  79. struct painter_driver_t * driver = (struct painter_driver_t *)device;
  80. rgb565_surface_painter_device_t *surface = (rgb565_surface_painter_device_t *)driver;
  81. memset(surface->buffer, 0, driver->panel_width * driver->panel_height * driver->native_bits_per_pixel / 8);
  82. return true;
  83. }
  84. static bool qp_rgb565_surface_power(painter_device_t device, bool power_on) {
  85. // No-op.
  86. return true;
  87. }
  88. static bool qp_rgb565_surface_clear(painter_device_t device) {
  89. struct painter_driver_t *driver = (struct painter_driver_t *)device;
  90. driver->driver_vtable->init(device, driver->rotation); // Re-init the surface
  91. return true;
  92. }
  93. static bool qp_rgb565_surface_flush(painter_device_t device) {
  94. struct painter_driver_t * driver = (struct painter_driver_t *)device;
  95. rgb565_surface_painter_device_t *surface = (rgb565_surface_painter_device_t *)driver;
  96. surface->dirty_l = surface->dirty_t = UINT16_MAX;
  97. surface->dirty_r = surface->dirty_b = 0;
  98. surface->is_dirty = false;
  99. return true;
  100. }
  101. static bool qp_rgb565_surface_viewport(painter_device_t device, uint16_t left, uint16_t top, uint16_t right, uint16_t bottom) {
  102. struct painter_driver_t * driver = (struct painter_driver_t *)device;
  103. rgb565_surface_painter_device_t *surface = (rgb565_surface_painter_device_t *)driver;
  104. // Set the viewport locations
  105. surface->viewport_l = left;
  106. surface->viewport_t = top;
  107. surface->viewport_r = right;
  108. surface->viewport_b = bottom;
  109. // Reset the write location to the top left
  110. surface->pixdata_x = left;
  111. surface->pixdata_y = top;
  112. return true;
  113. }
  114. // Stream pixel data to the current write position in GRAM
  115. static bool qp_rgb565_surface_pixdata(painter_device_t device, const void *pixel_data, uint32_t native_pixel_count) {
  116. struct painter_driver_t * driver = (struct painter_driver_t *)device;
  117. rgb565_surface_painter_device_t *surface = (rgb565_surface_painter_device_t *)driver;
  118. stream_pixdata(surface, (const uint16_t *)pixel_data, native_pixel_count);
  119. return true;
  120. }
  121. // Pixel colour conversion
  122. static bool qp_rgb565_surface_palette_convert_rgb565_swapped(painter_device_t device, int16_t palette_size, qp_pixel_t *palette) {
  123. for (int16_t i = 0; i < palette_size; ++i) {
  124. RGB rgb = hsv_to_rgb_nocie((HSV){palette[i].hsv888.h, palette[i].hsv888.s, palette[i].hsv888.v});
  125. uint16_t rgb565 = (((uint16_t)rgb.r) >> 3) << 11 | (((uint16_t)rgb.g) >> 2) << 5 | (((uint16_t)rgb.b) >> 3);
  126. palette[i].rgb565 = __builtin_bswap16(rgb565);
  127. }
  128. return true;
  129. }
  130. // Append pixels to the target location, keyed by the pixel index
  131. static bool qp_rgb565_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) {
  132. uint16_t *buf = (uint16_t *)target_buffer;
  133. for (uint32_t i = 0; i < pixel_count; ++i) {
  134. buf[pixel_offset + i] = palette[palette_indices[i]].rgb565;
  135. }
  136. return true;
  137. }
  138. const struct painter_driver_vtable_t rgb565_surface_driver_vtable = {
  139. .init = qp_rgb565_surface_init,
  140. .power = qp_rgb565_surface_power,
  141. .clear = qp_rgb565_surface_clear,
  142. .flush = qp_rgb565_surface_flush,
  143. .pixdata = qp_rgb565_surface_pixdata,
  144. .viewport = qp_rgb565_surface_viewport,
  145. .palette_convert = qp_rgb565_surface_palette_convert_rgb565_swapped,
  146. .append_pixels = qp_rgb565_surface_append_pixels_rgb565,
  147. };
  148. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  149. // Comms vtable
  150. static bool qp_rgb565_surface_comms_init(painter_device_t device) {
  151. // No-op.
  152. return true;
  153. }
  154. static bool qp_rgb565_surface_comms_start(painter_device_t device) {
  155. // No-op.
  156. return true;
  157. }
  158. static void qp_rgb565_surface_comms_stop(painter_device_t device) {
  159. // No-op.
  160. }
  161. uint32_t qp_rgb565_surface_comms_send(painter_device_t device, const void *data, uint32_t byte_count) {
  162. // No-op.
  163. return byte_count;
  164. }
  165. struct painter_comms_vtable_t rgb565_surface_driver_comms_vtable = {
  166. // These are all effective no-op's because they're not actually needed.
  167. .comms_init = qp_rgb565_surface_comms_init,
  168. .comms_start = qp_rgb565_surface_comms_start,
  169. .comms_stop = qp_rgb565_surface_comms_stop,
  170. .comms_send = qp_rgb565_surface_comms_send};
  171. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  172. // Factory function for creating a handle to an rgb565 surface
  173. painter_device_t qp_rgb565_make_surface(uint16_t panel_width, uint16_t panel_height, void *buffer) {
  174. for (uint32_t i = 0; i < RGB565_SURFACE_NUM_DEVICES; ++i) {
  175. rgb565_surface_painter_device_t *driver = &surface_drivers[i];
  176. if (!driver->base.driver_vtable) {
  177. driver->base.driver_vtable = &rgb565_surface_driver_vtable;
  178. driver->base.comms_vtable = &rgb565_surface_driver_comms_vtable;
  179. driver->base.native_bits_per_pixel = 16; // RGB565
  180. driver->base.panel_width = panel_width;
  181. driver->base.panel_height = panel_height;
  182. driver->base.rotation = QP_ROTATION_0;
  183. driver->base.offset_x = 0;
  184. driver->base.offset_y = 0;
  185. driver->buffer = (uint16_t *)buffer;
  186. return (painter_device_t)driver;
  187. }
  188. }
  189. return NULL;
  190. }
  191. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  192. // Drawing routine to copy out the dirty region and send it to another device
  193. bool qp_rgb565_surface_draw(painter_device_t surface, painter_device_t display, uint16_t x, uint16_t y) {
  194. struct painter_driver_t * surface_driver = (struct painter_driver_t *)surface;
  195. rgb565_surface_painter_device_t *surface_handle = (rgb565_surface_painter_device_t *)surface_driver;
  196. // If we're not dirty... we're done.
  197. if (!surface_handle->is_dirty) {
  198. return true;
  199. }
  200. // Set the target drawing area
  201. bool ok = qp_viewport(display, x + surface_handle->dirty_l, y + surface_handle->dirty_t, x + surface_handle->dirty_r, y + surface_handle->dirty_b);
  202. if (!ok) {
  203. return false;
  204. }
  205. // Housekeeping of the amount of pixels to transfer
  206. uint32_t total_pixel_count = QUANTUM_PAINTER_PIXDATA_BUFFER_SIZE / sizeof(uint16_t);
  207. uint32_t pixel_counter = 0;
  208. uint16_t *target_buffer = (uint16_t *)qp_internal_global_pixdata_buffer;
  209. // Fill the global pixdata area so that we can start transferring to the panel
  210. for (uint16_t y = surface_handle->dirty_t; y <= surface_handle->dirty_b; ++y) {
  211. for (uint16_t x = surface_handle->dirty_l; x <= surface_handle->dirty_r; ++x) {
  212. // Update the target buffer
  213. target_buffer[pixel_counter++] = surface_handle->buffer[y * surface_handle->base.panel_width + x];
  214. // If we've accumulated enough data, send it
  215. if (pixel_counter == total_pixel_count) {
  216. ok = qp_pixdata(display, qp_internal_global_pixdata_buffer, pixel_counter);
  217. if (!ok) {
  218. return false;
  219. }
  220. // Reset the counter
  221. pixel_counter = 0;
  222. }
  223. }
  224. }
  225. // If there's any leftover data, send it
  226. if (pixel_counter > 0) {
  227. ok = qp_pixdata(display, qp_internal_global_pixdata_buffer, pixel_counter);
  228. if (!ok) {
  229. return false;
  230. }
  231. }
  232. // Clear the dirty info for the surface
  233. return qp_flush(surface);
  234. }