qp_draw_core.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294
  1. // Copyright 2021-2022 Nick Brassel (@tzarc)
  2. // Copyright 2021 Paul Cotter (@gr1mr3aver)
  3. // SPDX-License-Identifier: GPL-2.0-or-later
  4. #include "qp_internal.h"
  5. #include "qp_comms.h"
  6. #include "qp_draw.h"
  7. #include "qgf.h"
  8. _Static_assert((QUANTUM_PAINTER_PIXDATA_BUFFER_SIZE > 0) && (QUANTUM_PAINTER_PIXDATA_BUFFER_SIZE % 16) == 0, "QUANTUM_PAINTER_PIXDATA_BUFFER_SIZE needs to be a non-zero multiple of 16");
  9. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  10. // Global variables
  11. //
  12. // NOTE: The variables in this section are intentionally outside a stack frame. They are able to be defined with larger
  13. // sizes than the normal stack frames would allow, and as such need to be external.
  14. //
  15. // **** DO NOT refactor this and decide to place the variables inside the function calling them -- you will ****
  16. // **** very likely get artifacts rendered to the screen as a result. ****
  17. //
  18. // Buffer used for transmitting native pixel data to the downstream device.
  19. uint8_t qp_internal_global_pixdata_buffer[QUANTUM_PAINTER_PIXDATA_BUFFER_SIZE];
  20. // Static buffer to contain a generated color palette
  21. static bool generated_palette = false;
  22. static int16_t generated_steps = -1;
  23. static qp_pixel_t interpolated_fg_hsv888;
  24. static qp_pixel_t interpolated_bg_hsv888;
  25. #if QUANTUM_PAINTER_SUPPORTS_256_PALETTE
  26. qp_pixel_t qp_internal_global_pixel_lookup_table[256];
  27. #else
  28. qp_pixel_t qp_internal_global_pixel_lookup_table[16];
  29. #endif
  30. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  31. // Helpers
  32. uint32_t qp_internal_num_pixels_in_buffer(painter_device_t device) {
  33. struct painter_driver_t *driver = (struct painter_driver_t *)device;
  34. return ((QUANTUM_PAINTER_PIXDATA_BUFFER_SIZE * 8) / driver->native_bits_per_pixel);
  35. }
  36. // qp_setpixel internal implementation, but accepts a buffer with pre-converted native pixel. Only the first pixel is used.
  37. bool qp_internal_setpixel_impl(painter_device_t device, uint16_t x, uint16_t y) {
  38. struct painter_driver_t *driver = (struct painter_driver_t *)device;
  39. return driver->driver_vtable->viewport(device, x, y, x, y) && driver->driver_vtable->pixdata(device, qp_internal_global_pixdata_buffer, 1);
  40. }
  41. // Fills the global native pixel buffer with equivalent pixels matching the supplied HSV
  42. void qp_internal_fill_pixdata(painter_device_t device, uint32_t num_pixels, uint8_t hue, uint8_t sat, uint8_t val) {
  43. struct painter_driver_t *driver = (struct painter_driver_t *)device;
  44. uint32_t pixels_in_pixdata = qp_internal_num_pixels_in_buffer(device);
  45. num_pixels = QP_MIN(pixels_in_pixdata, num_pixels);
  46. // Convert the color to native pixel format
  47. qp_pixel_t color = {.hsv888 = {.h = hue, .s = sat, .v = val}};
  48. driver->driver_vtable->palette_convert(device, 1, &color);
  49. // Append the required number of pixels
  50. uint8_t palette_idx = 0;
  51. for (uint32_t i = 0; i < num_pixels; ++i) {
  52. driver->driver_vtable->append_pixels(device, qp_internal_global_pixdata_buffer, &color, i, 1, &palette_idx);
  53. }
  54. }
  55. // Resets the global palette so that it can be regenerated. Only needed if the colors are identical, but a different display is used with a different internal pixel format.
  56. void qp_internal_invalidate_palette(void) {
  57. generated_palette = false;
  58. generated_steps = -1;
  59. }
  60. // Interpolates between two colors to generate a palette
  61. bool qp_internal_interpolate_palette(qp_pixel_t fg_hsv888, qp_pixel_t bg_hsv888, int16_t steps) {
  62. // Check if we need to generate a new palette -- if the input parameters match then assume the palette can stay unchanged.
  63. // This may present a problem if using the same parameters but a different screen converts pixels -- use qp_internal_invalidate_palette() to reset.
  64. if (generated_palette == true && generated_steps == steps && memcmp(&interpolated_fg_hsv888, &fg_hsv888, sizeof(fg_hsv888)) == 0 && memcmp(&interpolated_bg_hsv888, &bg_hsv888, sizeof(bg_hsv888)) == 0) {
  65. // We already have the correct palette, no point regenerating it.
  66. return false;
  67. }
  68. // Save the parameters so we know whether we can skip generation
  69. generated_palette = true;
  70. generated_steps = steps;
  71. interpolated_fg_hsv888 = fg_hsv888;
  72. interpolated_bg_hsv888 = bg_hsv888;
  73. int16_t hue_fg = fg_hsv888.hsv888.h;
  74. int16_t hue_bg = bg_hsv888.hsv888.h;
  75. // Make sure we take the "shortest" route from one hue to the other
  76. if ((hue_fg - hue_bg) >= 128) {
  77. hue_bg += 256;
  78. } else if ((hue_fg - hue_bg) <= -128) {
  79. hue_bg -= 256;
  80. }
  81. // Interpolate each of the lookup table entries
  82. for (int16_t i = 0; i < steps; ++i) {
  83. qp_internal_global_pixel_lookup_table[i].hsv888.h = (uint8_t)((hue_fg - hue_bg) * i / (steps - 1) + hue_bg);
  84. qp_internal_global_pixel_lookup_table[i].hsv888.s = (uint8_t)((fg_hsv888.hsv888.s - bg_hsv888.hsv888.s) * i / (steps - 1) + bg_hsv888.hsv888.s);
  85. qp_internal_global_pixel_lookup_table[i].hsv888.v = (uint8_t)((fg_hsv888.hsv888.v - bg_hsv888.hsv888.v) * i / (steps - 1) + bg_hsv888.hsv888.v);
  86. qp_dprintf("qp_internal_interpolate_palette: %3d of %d -- H: %3d, S: %3d, V: %3d\n", (int)(i + 1), (int)steps, (int)qp_internal_global_pixel_lookup_table[i].hsv888.h, (int)qp_internal_global_pixel_lookup_table[i].hsv888.s, (int)qp_internal_global_pixel_lookup_table[i].hsv888.v);
  87. }
  88. return true;
  89. }
  90. // Helper shared between image and font rendering -- sets up the global palette to match the palette block specified in the asset. Expects the stream to be positioned at the start of the block header.
  91. bool qp_internal_load_qgf_palette(qp_stream_t *stream, uint8_t bpp) {
  92. qgf_palette_v1_t palette_descriptor;
  93. if (qp_stream_read(&palette_descriptor, sizeof(qgf_palette_v1_t), 1, stream) != 1) {
  94. qp_dprintf("Failed to read palette_descriptor, expected length was not %d\n", (int)sizeof(qgf_palette_v1_t));
  95. return false;
  96. }
  97. // BPP determines the number of palette entries, each entry is a HSV888 triplet.
  98. const uint16_t palette_entries = 1u << bpp;
  99. // Ensure we aren't reusing any palette
  100. qp_internal_invalidate_palette();
  101. // Read the palette entries
  102. for (uint16_t i = 0; i < palette_entries; ++i) {
  103. // Read the palette entry
  104. qgf_palette_entry_v1_t entry;
  105. if (qp_stream_read(&entry, sizeof(qgf_palette_entry_v1_t), 1, stream) != 1) {
  106. return false;
  107. }
  108. // Update the lookup table
  109. qp_internal_global_pixel_lookup_table[i].hsv888.h = entry.h;
  110. qp_internal_global_pixel_lookup_table[i].hsv888.s = entry.s;
  111. qp_internal_global_pixel_lookup_table[i].hsv888.v = entry.v;
  112. qp_dprintf("qp_internal_load_qgf_palette: %3d of %d -- H: %3d, S: %3d, V: %3d\n", (int)(i + 1), (int)palette_entries, (int)qp_internal_global_pixel_lookup_table[i].hsv888.h, (int)qp_internal_global_pixel_lookup_table[i].hsv888.s, (int)qp_internal_global_pixel_lookup_table[i].hsv888.v);
  113. }
  114. return true;
  115. }
  116. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  117. // Quantum Painter External API: qp_setpixel
  118. bool qp_setpixel(painter_device_t device, uint16_t x, uint16_t y, uint8_t hue, uint8_t sat, uint8_t val) {
  119. struct painter_driver_t *driver = (struct painter_driver_t *)device;
  120. if (!driver->validate_ok) {
  121. qp_dprintf("qp_setpixel: fail (validation_ok == false)\n");
  122. return false;
  123. }
  124. if (!qp_comms_start(device)) {
  125. qp_dprintf("Failed to start comms in qp_setpixel\n");
  126. return false;
  127. }
  128. qp_internal_fill_pixdata(device, 1, hue, sat, val);
  129. bool ret = qp_internal_setpixel_impl(device, x, y);
  130. qp_comms_stop(device);
  131. qp_dprintf("qp_setpixel: %s\n", ret ? "ok" : "fail");
  132. return ret;
  133. }
  134. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  135. // Quantum Painter External API: qp_line
  136. bool qp_line(painter_device_t device, uint16_t x0, uint16_t y0, uint16_t x1, uint16_t y1, uint8_t hue, uint8_t sat, uint8_t val) {
  137. if (x0 == x1 || y0 == y1) {
  138. qp_dprintf("qp_line(%d, %d, %d, %d): entry (deferring to qp_rect)\n", (int)x0, (int)y0, (int)x1, (int)y1);
  139. bool ret = qp_rect(device, x0, y0, x1, y1, hue, sat, val, true);
  140. qp_dprintf("qp_line(%d, %d, %d, %d): %s (deferred to qp_rect)\n", (int)x0, (int)y0, (int)x1, (int)y1, ret ? "ok" : "fail");
  141. return ret;
  142. }
  143. qp_dprintf("qp_line(%d, %d, %d, %d): entry\n", (int)x0, (int)y0, (int)x1, (int)y1);
  144. struct painter_driver_t *driver = (struct painter_driver_t *)device;
  145. if (!driver->validate_ok) {
  146. qp_dprintf("qp_line: fail (validation_ok == false)\n");
  147. return false;
  148. }
  149. if (!qp_comms_start(device)) {
  150. qp_dprintf("Failed to start comms in qp_line\n");
  151. return false;
  152. }
  153. qp_internal_fill_pixdata(device, 1, hue, sat, val);
  154. // draw angled line using Bresenham's algo
  155. int16_t x = ((int16_t)x0);
  156. int16_t y = ((int16_t)y0);
  157. int16_t slopex = ((int16_t)x0) < ((int16_t)x1) ? 1 : -1;
  158. int16_t slopey = ((int16_t)y0) < ((int16_t)y1) ? 1 : -1;
  159. int16_t dx = abs(((int16_t)x1) - ((int16_t)x0));
  160. int16_t dy = -abs(((int16_t)y1) - ((int16_t)y0));
  161. int16_t e = dx + dy;
  162. int16_t e2 = 2 * e;
  163. bool ret = true;
  164. while (x != x1 || y != y1) {
  165. if (!qp_internal_setpixel_impl(device, x, y)) {
  166. ret = false;
  167. break;
  168. }
  169. e2 = 2 * e;
  170. if (e2 >= dy) {
  171. e += dy;
  172. x += slopex;
  173. }
  174. if (e2 <= dx) {
  175. e += dx;
  176. y += slopey;
  177. }
  178. }
  179. // draw the last pixel
  180. if (!qp_internal_setpixel_impl(device, x, y)) {
  181. ret = false;
  182. }
  183. qp_comms_stop(device);
  184. qp_dprintf("qp_line(%d, %d, %d, %d): %s\n", (int)x0, (int)y0, (int)x1, (int)y1, ret ? "ok" : "fail");
  185. return ret;
  186. }
  187. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  188. // Quantum Painter External API: qp_rect
  189. bool qp_internal_fillrect_helper_impl(painter_device_t device, uint16_t left, uint16_t top, uint16_t right, uint16_t bottom) {
  190. uint32_t pixels_in_pixdata = qp_internal_num_pixels_in_buffer(device);
  191. struct painter_driver_t *driver = (struct painter_driver_t *)device;
  192. uint16_t l = QP_MIN(left, right);
  193. uint16_t r = QP_MAX(left, right);
  194. uint16_t t = QP_MIN(top, bottom);
  195. uint16_t b = QP_MAX(top, bottom);
  196. uint16_t w = r - l + 1;
  197. uint16_t h = b - t + 1;
  198. uint32_t remaining = w * h;
  199. driver->driver_vtable->viewport(device, l, t, r, b);
  200. while (remaining > 0) {
  201. uint32_t transmit = QP_MIN(remaining, pixels_in_pixdata);
  202. if (!driver->driver_vtable->pixdata(device, qp_internal_global_pixdata_buffer, transmit)) {
  203. return false;
  204. }
  205. remaining -= transmit;
  206. }
  207. return true;
  208. }
  209. bool qp_rect(painter_device_t device, uint16_t left, uint16_t top, uint16_t right, uint16_t bottom, uint8_t hue, uint8_t sat, uint8_t val, bool filled) {
  210. qp_dprintf("qp_rect(%d, %d, %d, %d): entry\n", (int)left, (int)top, (int)right, (int)bottom);
  211. struct painter_driver_t *driver = (struct painter_driver_t *)device;
  212. if (!driver->validate_ok) {
  213. qp_dprintf("qp_rect: fail (validation_ok == false)\n");
  214. return false;
  215. }
  216. // Cater for cases where people have submitted the coordinates backwards
  217. uint16_t l = QP_MIN(left, right);
  218. uint16_t r = QP_MAX(left, right);
  219. uint16_t t = QP_MIN(top, bottom);
  220. uint16_t b = QP_MAX(top, bottom);
  221. uint16_t w = r - l + 1;
  222. uint16_t h = b - t + 1;
  223. bool ret = true;
  224. if (!qp_comms_start(device)) {
  225. qp_dprintf("Failed to start comms in qp_rect\n");
  226. return false;
  227. }
  228. if (filled) {
  229. // Fill up the pixdata buffer with the required number of native pixels
  230. qp_internal_fill_pixdata(device, w * h, hue, sat, val);
  231. // Perform the draw
  232. ret = qp_internal_fillrect_helper_impl(device, l, t, r, b);
  233. } else {
  234. // Fill up the pixdata buffer with the required number of native pixels
  235. qp_internal_fill_pixdata(device, QP_MAX(w, h), hue, sat, val);
  236. // Draw 4x filled single-width rects to create an outline
  237. if (!qp_internal_fillrect_helper_impl(device, l, t, r, t) || !qp_internal_fillrect_helper_impl(device, l, b, r, b) || !qp_internal_fillrect_helper_impl(device, l, t + 1, l, b - 1) || !qp_internal_fillrect_helper_impl(device, r, t + 1, r, b - 1)) {
  238. ret = false;
  239. }
  240. }
  241. qp_comms_stop(device);
  242. qp_dprintf("qp_rect(%d, %d, %d, %d): %s\n", (int)l, (int)t, (int)r, (int)b, ret ? "ok" : "fail");
  243. return ret;
  244. }