qp_rgb565_surface.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284
  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. // Append data to the target location
  139. static bool qp_rgb565_surface_append_pixdata(painter_device_t device, uint8_t *target_buffer, uint32_t pixdata_offset, uint8_t pixdata_byte) {
  140. target_buffer[pixdata_offset] = pixdata_byte;
  141. return true;
  142. }
  143. const struct painter_driver_vtable_t rgb565_surface_driver_vtable = {
  144. .init = qp_rgb565_surface_init,
  145. .power = qp_rgb565_surface_power,
  146. .clear = qp_rgb565_surface_clear,
  147. .flush = qp_rgb565_surface_flush,
  148. .pixdata = qp_rgb565_surface_pixdata,
  149. .viewport = qp_rgb565_surface_viewport,
  150. .palette_convert = qp_rgb565_surface_palette_convert_rgb565_swapped,
  151. .append_pixels = qp_rgb565_surface_append_pixels_rgb565,
  152. .append_pixdata = qp_rgb565_surface_append_pixdata,
  153. };
  154. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  155. // Comms vtable
  156. static bool qp_rgb565_surface_comms_init(painter_device_t device) {
  157. // No-op.
  158. return true;
  159. }
  160. static bool qp_rgb565_surface_comms_start(painter_device_t device) {
  161. // No-op.
  162. return true;
  163. }
  164. static void qp_rgb565_surface_comms_stop(painter_device_t device) {
  165. // No-op.
  166. }
  167. uint32_t qp_rgb565_surface_comms_send(painter_device_t device, const void *data, uint32_t byte_count) {
  168. // No-op.
  169. return byte_count;
  170. }
  171. struct painter_comms_vtable_t rgb565_surface_driver_comms_vtable = {
  172. // These are all effective no-op's because they're not actually needed.
  173. .comms_init = qp_rgb565_surface_comms_init,
  174. .comms_start = qp_rgb565_surface_comms_start,
  175. .comms_stop = qp_rgb565_surface_comms_stop,
  176. .comms_send = qp_rgb565_surface_comms_send};
  177. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  178. // Factory function for creating a handle to an rgb565 surface
  179. painter_device_t qp_rgb565_make_surface(uint16_t panel_width, uint16_t panel_height, void *buffer) {
  180. for (uint32_t i = 0; i < RGB565_SURFACE_NUM_DEVICES; ++i) {
  181. rgb565_surface_painter_device_t *driver = &surface_drivers[i];
  182. if (!driver->base.driver_vtable) {
  183. driver->base.driver_vtable = &rgb565_surface_driver_vtable;
  184. driver->base.comms_vtable = &rgb565_surface_driver_comms_vtable;
  185. driver->base.native_bits_per_pixel = 16; // RGB565
  186. driver->base.panel_width = panel_width;
  187. driver->base.panel_height = panel_height;
  188. driver->base.rotation = QP_ROTATION_0;
  189. driver->base.offset_x = 0;
  190. driver->base.offset_y = 0;
  191. driver->buffer = (uint16_t *)buffer;
  192. return (painter_device_t)driver;
  193. }
  194. }
  195. return NULL;
  196. }
  197. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  198. // Drawing routine to copy out the dirty region and send it to another device
  199. bool qp_rgb565_surface_draw(painter_device_t surface, painter_device_t display, uint16_t x, uint16_t y) {
  200. struct painter_driver_t * surface_driver = (struct painter_driver_t *)surface;
  201. rgb565_surface_painter_device_t *surface_handle = (rgb565_surface_painter_device_t *)surface_driver;
  202. // If we're not dirty... we're done.
  203. if (!surface_handle->is_dirty) {
  204. return true;
  205. }
  206. // Set the target drawing area
  207. 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);
  208. if (!ok) {
  209. return false;
  210. }
  211. // Housekeeping of the amount of pixels to transfer
  212. uint32_t total_pixel_count = QUANTUM_PAINTER_PIXDATA_BUFFER_SIZE / sizeof(uint16_t);
  213. uint32_t pixel_counter = 0;
  214. uint16_t *target_buffer = (uint16_t *)qp_internal_global_pixdata_buffer;
  215. // Fill the global pixdata area so that we can start transferring to the panel
  216. for (uint16_t y = surface_handle->dirty_t; y <= surface_handle->dirty_b; ++y) {
  217. for (uint16_t x = surface_handle->dirty_l; x <= surface_handle->dirty_r; ++x) {
  218. // Update the target buffer
  219. target_buffer[pixel_counter++] = surface_handle->buffer[y * surface_handle->base.panel_width + x];
  220. // If we've accumulated enough data, send it
  221. if (pixel_counter == total_pixel_count) {
  222. ok = qp_pixdata(display, qp_internal_global_pixdata_buffer, pixel_counter);
  223. if (!ok) {
  224. return false;
  225. }
  226. // Reset the counter
  227. pixel_counter = 0;
  228. }
  229. }
  230. }
  231. // If there's any leftover data, send it
  232. if (pixel_counter > 0) {
  233. ok = qp_pixdata(display, qp_internal_global_pixdata_buffer, pixel_counter);
  234. if (!ok) {
  235. return false;
  236. }
  237. }
  238. // Clear the dirty info for the surface
  239. return qp_flush(surface);
  240. }