logo

qmk_firmware

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

qp_draw_core.c (13222B)


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