qp_draw_image.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432
  1. // Copyright 2021 Nick Brassel (@tzarc)
  2. // SPDX-License-Identifier: GPL-2.0-or-later
  3. #include "qp_internal.h"
  4. #include "qp_draw.h"
  5. #include "qp_comms.h"
  6. #include "qgf.h"
  7. #include "deferred_exec.h"
  8. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  9. // QGF image handles
  10. typedef struct qgf_image_handle_t {
  11. painter_image_desc_t base;
  12. bool validate_ok;
  13. union {
  14. qp_stream_t stream;
  15. qp_memory_stream_t mem_stream;
  16. #ifdef QP_STREAM_HAS_FILE_IO
  17. qp_file_stream_t file_stream;
  18. #endif // QP_STREAM_HAS_FILE_IO
  19. };
  20. } qgf_image_handle_t;
  21. static qgf_image_handle_t image_descriptors[QUANTUM_PAINTER_NUM_IMAGES] = {0};
  22. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  23. // Helper: load image from stream
  24. static painter_image_handle_t qp_load_image_internal(bool (*stream_factory)(qgf_image_handle_t *image, void *arg), void *arg) {
  25. qp_dprintf("qp_load_image: entry\n");
  26. qgf_image_handle_t *image = NULL;
  27. // Find a free slot
  28. for (int i = 0; i < QUANTUM_PAINTER_NUM_IMAGES; ++i) {
  29. if (!image_descriptors[i].validate_ok) {
  30. image = &image_descriptors[i];
  31. break;
  32. }
  33. }
  34. // Drop out if not found
  35. if (!image) {
  36. qp_dprintf("qp_load_image: fail (no free slot)\n");
  37. return NULL;
  38. }
  39. if (!stream_factory(image, arg)) {
  40. qp_dprintf("qp_load_image: fail (could not create stream)\n");
  41. return NULL;
  42. }
  43. // Now that we know the length, validate the input data
  44. if (!qgf_validate_stream(&image->stream)) {
  45. qp_dprintf("qp_load_image: fail (failed validation)\n");
  46. return NULL;
  47. }
  48. // Fill out the QP image descriptor
  49. qgf_read_graphics_descriptor(&image->stream, &image->base.width, &image->base.height, &image->base.frame_count, NULL);
  50. // Validation success, we can return the handle
  51. image->validate_ok = true;
  52. qp_dprintf("qp_load_image: ok\n");
  53. return (painter_image_handle_t)image;
  54. }
  55. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  56. // Quantum Painter External API: qp_load_image_mem
  57. static inline bool image_mem_stream_factory(qgf_image_handle_t *image, void *arg) {
  58. void *buffer = arg;
  59. // Assume we can read the graphics descriptor
  60. image->mem_stream = qp_make_memory_stream((void *)buffer, sizeof(qgf_graphics_descriptor_v1_t));
  61. // Update the length of the stream to match, and rewind to the start
  62. image->mem_stream.length = qgf_get_total_size(&image->stream);
  63. image->mem_stream.position = 0;
  64. return true;
  65. }
  66. painter_image_handle_t qp_load_image_mem(const void *buffer) {
  67. return qp_load_image_internal(image_mem_stream_factory, (void *)buffer);
  68. }
  69. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  70. // Quantum Painter External API: qp_close_image
  71. bool qp_close_image(painter_image_handle_t image) {
  72. qgf_image_handle_t *qgf_image = (qgf_image_handle_t *)image;
  73. if (!qgf_image->validate_ok) {
  74. qp_dprintf("qp_close_image: fail (invalid image)\n");
  75. return false;
  76. }
  77. // Free up this image for use elsewhere.
  78. qgf_image->validate_ok = false;
  79. qp_stream_close(&qgf_image->stream);
  80. return true;
  81. }
  82. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  83. // Quantum Painter External API: qp_drawimage
  84. bool qp_drawimage(painter_device_t device, uint16_t x, uint16_t y, painter_image_handle_t image) {
  85. return qp_drawimage_recolor(device, x, y, image, 0, 0, 255, 0, 0, 0);
  86. }
  87. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  88. // Quantum Painter External API: qp_drawimage_recolor
  89. typedef struct qgf_frame_info_t {
  90. painter_compression_t compression_scheme;
  91. uint8_t bpp;
  92. bool has_palette;
  93. bool is_delta;
  94. uint16_t left;
  95. uint16_t top;
  96. uint16_t right;
  97. uint16_t bottom;
  98. uint16_t delay;
  99. } qgf_frame_info_t;
  100. static bool qp_drawimage_prepare_frame_for_stream_read(painter_device_t device, qgf_image_handle_t *qgf_image, uint16_t frame_number, qp_pixel_t fg_hsv888, qp_pixel_t bg_hsv888, qgf_frame_info_t *info) {
  101. struct painter_driver_t *driver = (struct painter_driver_t *)device;
  102. // Drop out if we can't actually place the data we read out anywhere
  103. if (!info) {
  104. qp_dprintf("Failed to prepare stream for read, output info buffer unavailable\n");
  105. return false;
  106. }
  107. // Seek to the frame
  108. qgf_seek_to_frame_descriptor(&qgf_image->stream, frame_number);
  109. // Read the frame descriptor
  110. qgf_frame_v1_t frame_descriptor;
  111. if (qp_stream_read(&frame_descriptor, sizeof(qgf_frame_v1_t), 1, &qgf_image->stream) != 1) {
  112. qp_dprintf("Failed to read frame_descriptor, expected length was not %d\n", (int)sizeof(qgf_frame_v1_t));
  113. return false;
  114. }
  115. // Parse out the frame info
  116. if (!qgf_parse_frame_descriptor(&frame_descriptor, &info->bpp, &info->has_palette, &info->is_delta, &info->compression_scheme, &info->delay)) {
  117. return false;
  118. }
  119. // Ensure we aren't reusing any palette
  120. qp_internal_invalidate_palette();
  121. if (!qp_internal_bpp_capable(info->bpp)) {
  122. qp_dprintf("qp_drawimage_recolor: fail (image bpp too high (%d), check QUANTUM_PAINTER_SUPPORTS_256_PALETTE or QUANTUM_PAINTER_SUPPORTS_NATIVE_COLORS)\n", (int)info->bpp);
  123. qp_comms_stop(device);
  124. return false;
  125. }
  126. // Handle palette if needed
  127. const uint16_t palette_entries = 1u << info->bpp;
  128. bool needs_pixconvert = false;
  129. if (info->has_palette) {
  130. // Load the palette from the stream
  131. if (!qp_internal_load_qgf_palette((qp_stream_t *)&qgf_image->stream, info->bpp)) {
  132. return false;
  133. }
  134. needs_pixconvert = true;
  135. } else {
  136. if (info->bpp <= 8) {
  137. // Interpolate from fg/bg
  138. needs_pixconvert = qp_internal_interpolate_palette(fg_hsv888, bg_hsv888, palette_entries);
  139. }
  140. }
  141. if (needs_pixconvert) {
  142. // Convert the palette to native format
  143. if (!driver->driver_vtable->palette_convert(device, palette_entries, qp_internal_global_pixel_lookup_table)) {
  144. qp_dprintf("qp_drawimage_recolor: fail (could not convert pixels to native)\n");
  145. qp_comms_stop(device);
  146. return false;
  147. }
  148. }
  149. // Handle delta if needed
  150. if (info->is_delta) {
  151. qgf_delta_v1_t delta_descriptor;
  152. if (qp_stream_read(&delta_descriptor, sizeof(qgf_delta_v1_t), 1, &qgf_image->stream) != 1) {
  153. qp_dprintf("Failed to read delta_descriptor, expected length was not %d\n", (int)sizeof(qgf_delta_v1_t));
  154. return false;
  155. }
  156. info->left = delta_descriptor.left;
  157. info->top = delta_descriptor.top;
  158. info->right = delta_descriptor.right;
  159. info->bottom = delta_descriptor.bottom;
  160. }
  161. // Read the data block
  162. qgf_data_v1_t data_descriptor;
  163. if (qp_stream_read(&data_descriptor, sizeof(qgf_data_v1_t), 1, &qgf_image->stream) != 1) {
  164. qp_dprintf("Failed to read data_descriptor, expected length was not %d\n", (int)sizeof(qgf_data_v1_t));
  165. return false;
  166. }
  167. // Stream is now at the point of being able to read pixdata
  168. return true;
  169. }
  170. static bool qp_drawimage_recolor_impl(painter_device_t device, uint16_t x, uint16_t y, painter_image_handle_t image, int frame_number, qgf_frame_info_t *frame_info, qp_pixel_t fg_hsv888, qp_pixel_t bg_hsv888) {
  171. qp_dprintf("qp_drawimage_recolor: entry\n");
  172. struct painter_driver_t *driver = (struct painter_driver_t *)device;
  173. if (!driver->validate_ok) {
  174. qp_dprintf("qp_drawimage_recolor: fail (validation_ok == false)\n");
  175. return false;
  176. }
  177. qgf_image_handle_t *qgf_image = (qgf_image_handle_t *)image;
  178. if (!qgf_image->validate_ok) {
  179. qp_dprintf("qp_drawimage_recolor: fail (invalid image)\n");
  180. return false;
  181. }
  182. // Read the frame info
  183. if (!qp_drawimage_prepare_frame_for_stream_read(device, qgf_image, frame_number, fg_hsv888, bg_hsv888, frame_info)) {
  184. qp_dprintf("qp_drawimage_recolor: fail (could not read frame %d)\n", frame_number);
  185. return false;
  186. }
  187. if (!qp_comms_start(device)) {
  188. qp_dprintf("qp_drawimage_recolor: fail (could not start comms)\n");
  189. return false;
  190. }
  191. uint16_t l, t, r, b;
  192. if (frame_info->is_delta) {
  193. l = x + frame_info->left;
  194. t = y + frame_info->top;
  195. r = x + frame_info->right - 1;
  196. b = y + frame_info->bottom - 1;
  197. } else {
  198. l = x;
  199. t = y;
  200. r = x + image->width - 1;
  201. b = y + image->height - 1;
  202. }
  203. uint32_t pixel_count = ((uint32_t)(r - l + 1)) * (b - t + 1);
  204. // Configure where we're going to be rendering to
  205. if (!driver->driver_vtable->viewport(device, l, t, r, b)) {
  206. qp_dprintf("qp_drawimage_recolor: fail (could not set viewport)\n");
  207. qp_comms_stop(device);
  208. return false;
  209. }
  210. // Set up the input state
  211. struct qp_internal_byte_input_state input_state = {.device = device, .src_stream = &qgf_image->stream};
  212. qp_internal_byte_input_callback input_callback = qp_internal_prepare_input_state(&input_state, frame_info->compression_scheme);
  213. if (input_callback == NULL) {
  214. qp_dprintf("qp_drawimage_recolor: fail (invalid image compression scheme)\n");
  215. qp_comms_stop(device);
  216. return false;
  217. }
  218. bool ret = false;
  219. if (frame_info->bpp <= 8) {
  220. // Set up the output state
  221. struct qp_internal_pixel_output_state output_state = {.device = device, .pixel_write_pos = 0, .max_pixels = qp_internal_num_pixels_in_buffer(device)};
  222. // Decode the pixel data and stream to the display
  223. ret = qp_internal_decode_palette(device, pixel_count, frame_info->bpp, input_callback, &input_state, qp_internal_global_pixel_lookup_table, qp_internal_pixel_appender, &output_state);
  224. // Any leftovers need transmission as well.
  225. if (ret && output_state.pixel_write_pos > 0) {
  226. ret &= driver->driver_vtable->pixdata(device, qp_internal_global_pixdata_buffer, output_state.pixel_write_pos);
  227. }
  228. } else if (frame_info->bpp != driver->native_bits_per_pixel) {
  229. // Prevent stuff like drawing 24bpp images on 16bpp displays
  230. qp_dprintf("Image's bpp doesn't match the target display's native_bits_per_pixel\n");
  231. return false;
  232. } else {
  233. // Set up the output state
  234. struct qp_internal_byte_output_state output_state = {.device = device, .byte_write_pos = 0, .max_bytes = qp_internal_num_pixels_in_buffer(device) * driver->native_bits_per_pixel / 8};
  235. // Stream the raw pixel data to the display
  236. uint32_t byte_count = pixel_count * frame_info->bpp / 8;
  237. ret = qp_internal_send_bytes(device, byte_count, input_callback, &input_state, qp_internal_byte_appender, &output_state);
  238. // Any leftovers need transmission as well.
  239. if (ret && output_state.byte_write_pos > 0) {
  240. ret &= driver->driver_vtable->pixdata(device, qp_internal_global_pixdata_buffer, output_state.byte_write_pos * 8 / driver->native_bits_per_pixel);
  241. }
  242. }
  243. qp_dprintf("qp_drawimage_recolor: %s\n", ret ? "ok" : "fail");
  244. qp_comms_stop(device);
  245. return ret;
  246. }
  247. bool qp_drawimage_recolor(painter_device_t device, uint16_t x, uint16_t y, painter_image_handle_t image, uint8_t hue_fg, uint8_t sat_fg, uint8_t val_fg, uint8_t hue_bg, uint8_t sat_bg, uint8_t val_bg) {
  248. qgf_frame_info_t frame_info = {0};
  249. qp_pixel_t fg_hsv888 = {.hsv888 = {.h = hue_fg, .s = sat_fg, .v = val_fg}};
  250. qp_pixel_t bg_hsv888 = {.hsv888 = {.h = hue_bg, .s = sat_bg, .v = val_bg}};
  251. return qp_drawimage_recolor_impl(device, x, y, image, 0, &frame_info, fg_hsv888, bg_hsv888);
  252. }
  253. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  254. // Quantum Painter External API: qp_animate
  255. deferred_token qp_animate(painter_device_t device, uint16_t x, uint16_t y, painter_image_handle_t image) {
  256. return qp_animate_recolor(device, x, y, image, 0, 0, 255, 0, 0, 0);
  257. }
  258. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  259. // Quantum Painter External API: qp_animate_recolor
  260. typedef struct animation_state_t {
  261. painter_device_t device;
  262. uint16_t x;
  263. uint16_t y;
  264. painter_image_handle_t image;
  265. qp_pixel_t fg_hsv888;
  266. qp_pixel_t bg_hsv888;
  267. uint16_t frame_number;
  268. deferred_token defer_token;
  269. } animation_state_t;
  270. static deferred_executor_t animation_executors[QUANTUM_PAINTER_CONCURRENT_ANIMATIONS] = {0};
  271. static animation_state_t animation_states[QUANTUM_PAINTER_CONCURRENT_ANIMATIONS] = {0};
  272. static deferred_token qp_render_animation_state(animation_state_t *state, uint16_t *delay_ms) {
  273. qgf_frame_info_t frame_info = {0};
  274. qp_dprintf("qp_render_animation_state: entry (frame #%d)\n", (int)state->frame_number);
  275. bool ret = qp_drawimage_recolor_impl(state->device, state->x, state->y, state->image, state->frame_number, &frame_info, state->fg_hsv888, state->bg_hsv888);
  276. if (ret) {
  277. ++state->frame_number;
  278. if (state->frame_number >= state->image->frame_count) {
  279. state->frame_number = 0;
  280. }
  281. *delay_ms = frame_info.delay;
  282. }
  283. qp_dprintf("qp_render_animation_state: %s (delay %dms)\n", ret ? "ok" : "fail", (int)(*delay_ms));
  284. return ret;
  285. }
  286. static uint32_t animation_callback(uint32_t trigger_time, void *cb_arg) {
  287. animation_state_t *state = (animation_state_t *)cb_arg;
  288. uint16_t delay_ms;
  289. bool ret = qp_render_animation_state(state, &delay_ms);
  290. if (!ret) {
  291. // Setting the device to NULL clears the animation slot
  292. state->device = NULL;
  293. }
  294. // If we're successful, keep animating -- returning 0 cancels the deferred execution
  295. return ret ? delay_ms : 0;
  296. }
  297. deferred_token qp_animate_recolor(painter_device_t device, uint16_t x, uint16_t y, painter_image_handle_t image, uint8_t hue_fg, uint8_t sat_fg, uint8_t val_fg, uint8_t hue_bg, uint8_t sat_bg, uint8_t val_bg) {
  298. qp_dprintf("qp_animate_recolor: entry\n");
  299. animation_state_t *anim_state = NULL;
  300. for (int i = 0; i < QUANTUM_PAINTER_CONCURRENT_ANIMATIONS; ++i) {
  301. if (animation_states[i].device == NULL) {
  302. anim_state = &animation_states[i];
  303. break;
  304. }
  305. }
  306. if (!anim_state) {
  307. qp_dprintf("qp_animate_recolor: fail (could not find free animation slot)\n");
  308. return INVALID_DEFERRED_TOKEN;
  309. }
  310. // Prepare the animation state
  311. anim_state->device = device;
  312. anim_state->x = x;
  313. anim_state->y = y;
  314. anim_state->image = image;
  315. anim_state->fg_hsv888 = (qp_pixel_t){.hsv888 = {.h = hue_fg, .s = sat_fg, .v = val_fg}};
  316. anim_state->bg_hsv888 = (qp_pixel_t){.hsv888 = {.h = hue_bg, .s = sat_bg, .v = val_bg}};
  317. anim_state->frame_number = 0;
  318. // Draw the first frame
  319. uint16_t delay_ms;
  320. if (!qp_render_animation_state(anim_state, &delay_ms)) {
  321. anim_state->device = NULL; // disregard the allocated animation slot
  322. qp_dprintf("qp_animate_recolor: fail (could not render first frame)\n");
  323. return INVALID_DEFERRED_TOKEN;
  324. }
  325. // Set up the timer
  326. anim_state->defer_token = defer_exec_advanced(animation_executors, QUANTUM_PAINTER_CONCURRENT_ANIMATIONS, delay_ms, animation_callback, anim_state);
  327. if (anim_state->defer_token == INVALID_DEFERRED_TOKEN) {
  328. anim_state->device = NULL; // disregard the allocated animation slot
  329. qp_dprintf("qp_animate_recolor: fail (could not set up animation executor)\n");
  330. return INVALID_DEFERRED_TOKEN;
  331. }
  332. qp_dprintf("qp_animate_recolor: ok (deferred token = %d)\n", (int)anim_state->defer_token);
  333. return anim_state->defer_token;
  334. }
  335. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  336. // Quantum Painter External API: qp_stop_animation
  337. void qp_stop_animation(deferred_token anim_token) {
  338. for (int i = 0; i < QUANTUM_PAINTER_CONCURRENT_ANIMATIONS; ++i) {
  339. if (animation_states[i].defer_token == anim_token) {
  340. cancel_deferred_exec_advanced(animation_executors, QUANTUM_PAINTER_CONCURRENT_ANIMATIONS, anim_token);
  341. animation_states[i].device = NULL;
  342. return;
  343. }
  344. }
  345. }
  346. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  347. // Quantum Painter Core API: qp_internal_animation_tick
  348. void qp_internal_animation_tick(void) {
  349. static uint32_t last_anim_exec = 0;
  350. deferred_exec_advanced_task(animation_executors, QUANTUM_PAINTER_CONCURRENT_ANIMATIONS, &last_anim_exec);
  351. }
  352. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  353. // Quantum Painter Core API: qp_internal_task
  354. void qp_internal_task(void) {
  355. qp_internal_animation_tick();
  356. #ifdef QUANTUM_PAINTER_LVGL_INTEGRATION_ENABLE
  357. // Run LVGL ticks
  358. void qp_lvgl_internal_tick(void);
  359. qp_lvgl_internal_tick();
  360. #endif
  361. }