qp_draw_image.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382
  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. // Quantum Painter External API: qp_load_image_mem
  24. painter_image_handle_t qp_load_image_mem(const void *buffer) {
  25. qp_dprintf("qp_load_image_mem: 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_mem: fail (no free slot)\n");
  37. return NULL;
  38. }
  39. // Assume we can read the graphics descriptor
  40. image->mem_stream = qp_make_memory_stream((void *)buffer, sizeof(qgf_graphics_descriptor_v1_t));
  41. // Update the length of the stream to match, and rewind to the start
  42. image->mem_stream.length = qgf_get_total_size(&image->stream);
  43. image->mem_stream.position = 0;
  44. // Now that we know the length, validate the input data
  45. if (!qgf_validate_stream(&image->stream)) {
  46. qp_dprintf("qp_load_image_mem: fail (failed validation)\n");
  47. return NULL;
  48. }
  49. // Fill out the QP image descriptor
  50. qgf_read_graphics_descriptor(&image->stream, &image->base.width, &image->base.height, &image->base.frame_count, NULL);
  51. // Validation success, we can return the handle
  52. image->validate_ok = true;
  53. qp_dprintf("qp_load_image_mem: ok\n");
  54. return (painter_image_handle_t)image;
  55. }
  56. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  57. // Quantum Painter External API: qp_close_image
  58. bool qp_close_image(painter_image_handle_t image) {
  59. qgf_image_handle_t *qgf_image = (qgf_image_handle_t *)image;
  60. if (!qgf_image->validate_ok) {
  61. qp_dprintf("qp_close_image: fail (invalid image)\n");
  62. return false;
  63. }
  64. // Free up this image for use elsewhere.
  65. qgf_image->validate_ok = false;
  66. return true;
  67. }
  68. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  69. // Quantum Painter External API: qp_drawimage
  70. bool qp_drawimage(painter_device_t device, uint16_t x, uint16_t y, painter_image_handle_t image) {
  71. return qp_drawimage_recolor(device, x, y, image, 0, 0, 255, 0, 0, 0);
  72. }
  73. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  74. // Quantum Painter External API: qp_drawimage_recolor
  75. typedef struct qgf_frame_info_t {
  76. painter_compression_t compression_scheme;
  77. uint8_t bpp;
  78. bool has_palette;
  79. bool is_delta;
  80. uint16_t left;
  81. uint16_t top;
  82. uint16_t right;
  83. uint16_t bottom;
  84. uint16_t delay;
  85. } qgf_frame_info_t;
  86. 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) {
  87. struct painter_driver_t *driver = (struct painter_driver_t *)device;
  88. // Drop out if we can't actually place the data we read out anywhere
  89. if (!info) {
  90. qp_dprintf("Failed to prepare stream for read, output info buffer unavailable\n");
  91. return false;
  92. }
  93. // Seek to the frame
  94. qgf_seek_to_frame_descriptor(&qgf_image->stream, frame_number);
  95. // Read the frame descriptor
  96. qgf_frame_v1_t frame_descriptor;
  97. if (qp_stream_read(&frame_descriptor, sizeof(qgf_frame_v1_t), 1, &qgf_image->stream) != 1) {
  98. qp_dprintf("Failed to read frame_descriptor, expected length was not %d\n", (int)sizeof(qgf_frame_v1_t));
  99. return false;
  100. }
  101. // Parse out the frame info
  102. if (!qgf_parse_frame_descriptor(&frame_descriptor, &info->bpp, &info->has_palette, &info->is_delta, &info->compression_scheme, &info->delay)) {
  103. return false;
  104. }
  105. // Ensure we aren't reusing any palette
  106. qp_internal_invalidate_palette();
  107. // Handle palette if needed
  108. const uint16_t palette_entries = 1u << info->bpp;
  109. bool needs_pixconvert = false;
  110. if (info->has_palette) {
  111. // Load the palette from the stream
  112. if (!qp_internal_load_qgf_palette((qp_stream_t *)&qgf_image->stream, info->bpp)) {
  113. return false;
  114. }
  115. needs_pixconvert = true;
  116. } else {
  117. // Interpolate from fg/bg
  118. needs_pixconvert = qp_internal_interpolate_palette(fg_hsv888, bg_hsv888, palette_entries);
  119. }
  120. if (!qp_internal_bpp_capable(info->bpp)) {
  121. qp_dprintf("qp_drawimage_recolor: fail (image bpp too high (%d), check QUANTUM_PAINTER_SUPPORTS_256_PALETTE)\n", (int)info->bpp);
  122. qp_comms_stop(device);
  123. return false;
  124. }
  125. if (needs_pixconvert) {
  126. // Convert the palette to native format
  127. if (!driver->driver_vtable->palette_convert(device, palette_entries, qp_internal_global_pixel_lookup_table)) {
  128. qp_dprintf("qp_drawimage_recolor: fail (could not convert pixels to native)\n");
  129. qp_comms_stop(device);
  130. return false;
  131. }
  132. }
  133. // Handle delta if needed
  134. if (info->is_delta) {
  135. qgf_delta_v1_t delta_descriptor;
  136. if (qp_stream_read(&delta_descriptor, sizeof(qgf_delta_v1_t), 1, &qgf_image->stream) != 1) {
  137. qp_dprintf("Failed to read delta_descriptor, expected length was not %d\n", (int)sizeof(qgf_delta_v1_t));
  138. return false;
  139. }
  140. info->left = delta_descriptor.left;
  141. info->top = delta_descriptor.top;
  142. info->right = delta_descriptor.right;
  143. info->bottom = delta_descriptor.bottom;
  144. }
  145. // Read the data block
  146. qgf_data_v1_t data_descriptor;
  147. if (qp_stream_read(&data_descriptor, sizeof(qgf_data_v1_t), 1, &qgf_image->stream) != 1) {
  148. qp_dprintf("Failed to read data_descriptor, expected length was not %d\n", (int)sizeof(qgf_data_v1_t));
  149. return false;
  150. }
  151. // Stream is now at the point of being able to read pixdata
  152. return true;
  153. }
  154. 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) {
  155. qp_dprintf("qp_drawimage_recolor: entry\n");
  156. struct painter_driver_t *driver = (struct painter_driver_t *)device;
  157. if (!driver->validate_ok) {
  158. qp_dprintf("qp_drawimage_recolor: fail (validation_ok == false)\n");
  159. return false;
  160. }
  161. qgf_image_handle_t *qgf_image = (qgf_image_handle_t *)image;
  162. if (!qgf_image->validate_ok) {
  163. qp_dprintf("qp_drawimage_recolor: fail (invalid image)\n");
  164. return false;
  165. }
  166. // Read the frame info
  167. if (!qp_drawimage_prepare_frame_for_stream_read(device, qgf_image, frame_number, fg_hsv888, bg_hsv888, frame_info)) {
  168. qp_dprintf("qp_drawimage_recolor: fail (could not read frame %d)\n", frame_number);
  169. return false;
  170. }
  171. if (!qp_comms_start(device)) {
  172. qp_dprintf("qp_drawimage_recolor: fail (could not start comms)\n");
  173. return false;
  174. }
  175. uint16_t l, t, r, b;
  176. if (frame_info->is_delta) {
  177. l = x + frame_info->left;
  178. t = y + frame_info->top;
  179. r = x + frame_info->right - 1;
  180. b = y + frame_info->bottom - 1;
  181. } else {
  182. l = x;
  183. t = y;
  184. r = x + image->width - 1;
  185. b = y + image->height - 1;
  186. }
  187. uint32_t pixel_count = ((uint32_t)(r - l + 1)) * (b - t + 1);
  188. // Configure where we're going to be rendering to
  189. if (!driver->driver_vtable->viewport(device, l, t, r, b)) {
  190. qp_dprintf("qp_drawimage_recolor: fail (could not set viewport)\n");
  191. qp_comms_stop(device);
  192. return false;
  193. }
  194. // Set up the input state
  195. struct qp_internal_byte_input_state input_state = {.device = device, .src_stream = &qgf_image->stream};
  196. qp_internal_byte_input_callback input_callback = qp_internal_prepare_input_state(&input_state, frame_info->compression_scheme);
  197. if (input_callback == NULL) {
  198. qp_dprintf("qp_drawimage_recolor: fail (invalid image compression scheme)\n");
  199. qp_comms_stop(device);
  200. return false;
  201. }
  202. // Set up the output state
  203. struct qp_internal_pixel_output_state output_state = {.device = device, .pixel_write_pos = 0, .max_pixels = qp_internal_num_pixels_in_buffer(device)};
  204. // Decode the pixel data and stream to the display
  205. bool 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);
  206. // Any leftovers need transmission as well.
  207. if (ret && output_state.pixel_write_pos > 0) {
  208. ret &= driver->driver_vtable->pixdata(device, qp_internal_global_pixdata_buffer, output_state.pixel_write_pos);
  209. }
  210. qp_dprintf("qp_drawimage_recolor: %s\n", ret ? "ok" : "fail");
  211. qp_comms_stop(device);
  212. return ret;
  213. }
  214. 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) {
  215. qgf_frame_info_t frame_info = {0};
  216. qp_pixel_t fg_hsv888 = {.hsv888 = {.h = hue_fg, .s = sat_fg, .v = val_fg}};
  217. qp_pixel_t bg_hsv888 = {.hsv888 = {.h = hue_bg, .s = sat_bg, .v = val_bg}};
  218. return qp_drawimage_recolor_impl(device, x, y, image, 0, &frame_info, fg_hsv888, bg_hsv888);
  219. }
  220. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  221. // Quantum Painter External API: qp_animate
  222. deferred_token qp_animate(painter_device_t device, uint16_t x, uint16_t y, painter_image_handle_t image) {
  223. return qp_animate_recolor(device, x, y, image, 0, 0, 255, 0, 0, 0);
  224. }
  225. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  226. // Quantum Painter External API: qp_animate_recolor
  227. typedef struct animation_state_t {
  228. painter_device_t device;
  229. uint16_t x;
  230. uint16_t y;
  231. painter_image_handle_t image;
  232. qp_pixel_t fg_hsv888;
  233. qp_pixel_t bg_hsv888;
  234. uint16_t frame_number;
  235. deferred_token defer_token;
  236. } animation_state_t;
  237. static deferred_executor_t animation_executors[QUANTUM_PAINTER_CONCURRENT_ANIMATIONS] = {0};
  238. static animation_state_t animation_states[QUANTUM_PAINTER_CONCURRENT_ANIMATIONS] = {0};
  239. static deferred_token qp_render_animation_state(animation_state_t *state, uint16_t *delay_ms) {
  240. qgf_frame_info_t frame_info = {0};
  241. qp_dprintf("qp_render_animation_state: entry (frame #%d)\n", (int)state->frame_number);
  242. 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);
  243. if (ret) {
  244. ++state->frame_number;
  245. if (state->frame_number >= state->image->frame_count) {
  246. state->frame_number = 0;
  247. }
  248. *delay_ms = frame_info.delay;
  249. }
  250. qp_dprintf("qp_render_animation_state: %s (delay %dms)\n", ret ? "ok" : "fail", (int)(*delay_ms));
  251. return ret;
  252. }
  253. static uint32_t animation_callback(uint32_t trigger_time, void *cb_arg) {
  254. animation_state_t *state = (animation_state_t *)cb_arg;
  255. uint16_t delay_ms;
  256. bool ret = qp_render_animation_state(state, &delay_ms);
  257. if (!ret) {
  258. // Setting the device to NULL clears the animation slot
  259. state->device = NULL;
  260. }
  261. // If we're successful, keep animating -- returning 0 cancels the deferred execution
  262. return ret ? delay_ms : 0;
  263. }
  264. 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) {
  265. qp_dprintf("qp_animate_recolor: entry\n");
  266. animation_state_t *anim_state = NULL;
  267. for (int i = 0; i < QUANTUM_PAINTER_CONCURRENT_ANIMATIONS; ++i) {
  268. if (animation_states[i].device == NULL) {
  269. anim_state = &animation_states[i];
  270. break;
  271. }
  272. }
  273. if (!anim_state) {
  274. qp_dprintf("qp_animate_recolor: fail (could not find free animation slot)\n");
  275. return INVALID_DEFERRED_TOKEN;
  276. }
  277. // Prepare the animation state
  278. anim_state->device = device;
  279. anim_state->x = x;
  280. anim_state->y = y;
  281. anim_state->image = image;
  282. anim_state->fg_hsv888 = (qp_pixel_t){.hsv888 = {.h = hue_fg, .s = sat_fg, .v = val_fg}};
  283. anim_state->bg_hsv888 = (qp_pixel_t){.hsv888 = {.h = hue_bg, .s = sat_bg, .v = val_bg}};
  284. anim_state->frame_number = 0;
  285. // Draw the first frame
  286. uint16_t delay_ms;
  287. if (!qp_render_animation_state(anim_state, &delay_ms)) {
  288. anim_state->device = NULL; // disregard the allocated animation slot
  289. qp_dprintf("qp_animate_recolor: fail (could not render first frame)\n");
  290. return INVALID_DEFERRED_TOKEN;
  291. }
  292. // Set up the timer
  293. anim_state->defer_token = defer_exec_advanced(animation_executors, QUANTUM_PAINTER_CONCURRENT_ANIMATIONS, delay_ms, animation_callback, anim_state);
  294. if (anim_state->defer_token == INVALID_DEFERRED_TOKEN) {
  295. anim_state->device = NULL; // disregard the allocated animation slot
  296. qp_dprintf("qp_animate_recolor: fail (could not set up animation executor)\n");
  297. return INVALID_DEFERRED_TOKEN;
  298. }
  299. qp_dprintf("qp_animate_recolor: ok (deferred token = %d)\n", (int)anim_state->defer_token);
  300. return anim_state->defer_token;
  301. }
  302. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  303. // Quantum Painter External API: qp_stop_animation
  304. void qp_stop_animation(deferred_token anim_token) {
  305. for (int i = 0; i < QUANTUM_PAINTER_CONCURRENT_ANIMATIONS; ++i) {
  306. if (animation_states[i].defer_token == anim_token) {
  307. cancel_deferred_exec_advanced(animation_executors, QUANTUM_PAINTER_CONCURRENT_ANIMATIONS, anim_token);
  308. animation_states[i].device = NULL;
  309. return;
  310. }
  311. }
  312. }
  313. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  314. // Quantum Painter Core API: qp_internal_animation_tick
  315. void qp_internal_animation_tick(void) {
  316. static uint32_t last_anim_exec = 0;
  317. deferred_exec_advanced_task(animation_executors, QUANTUM_PAINTER_CONCURRENT_ANIMATIONS, &last_anim_exec);
  318. }