qp_draw_image.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401
  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)\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. // Interpolate from fg/bg
  137. needs_pixconvert = qp_internal_interpolate_palette(fg_hsv888, bg_hsv888, palette_entries);
  138. }
  139. if (needs_pixconvert) {
  140. // Convert the palette to native format
  141. if (!driver->driver_vtable->palette_convert(device, palette_entries, qp_internal_global_pixel_lookup_table)) {
  142. qp_dprintf("qp_drawimage_recolor: fail (could not convert pixels to native)\n");
  143. qp_comms_stop(device);
  144. return false;
  145. }
  146. }
  147. // Handle delta if needed
  148. if (info->is_delta) {
  149. qgf_delta_v1_t delta_descriptor;
  150. if (qp_stream_read(&delta_descriptor, sizeof(qgf_delta_v1_t), 1, &qgf_image->stream) != 1) {
  151. qp_dprintf("Failed to read delta_descriptor, expected length was not %d\n", (int)sizeof(qgf_delta_v1_t));
  152. return false;
  153. }
  154. info->left = delta_descriptor.left;
  155. info->top = delta_descriptor.top;
  156. info->right = delta_descriptor.right;
  157. info->bottom = delta_descriptor.bottom;
  158. }
  159. // Read the data block
  160. qgf_data_v1_t data_descriptor;
  161. if (qp_stream_read(&data_descriptor, sizeof(qgf_data_v1_t), 1, &qgf_image->stream) != 1) {
  162. qp_dprintf("Failed to read data_descriptor, expected length was not %d\n", (int)sizeof(qgf_data_v1_t));
  163. return false;
  164. }
  165. // Stream is now at the point of being able to read pixdata
  166. return true;
  167. }
  168. 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) {
  169. qp_dprintf("qp_drawimage_recolor: entry\n");
  170. struct painter_driver_t *driver = (struct painter_driver_t *)device;
  171. if (!driver->validate_ok) {
  172. qp_dprintf("qp_drawimage_recolor: fail (validation_ok == false)\n");
  173. return false;
  174. }
  175. qgf_image_handle_t *qgf_image = (qgf_image_handle_t *)image;
  176. if (!qgf_image->validate_ok) {
  177. qp_dprintf("qp_drawimage_recolor: fail (invalid image)\n");
  178. return false;
  179. }
  180. // Read the frame info
  181. if (!qp_drawimage_prepare_frame_for_stream_read(device, qgf_image, frame_number, fg_hsv888, bg_hsv888, frame_info)) {
  182. qp_dprintf("qp_drawimage_recolor: fail (could not read frame %d)\n", frame_number);
  183. return false;
  184. }
  185. if (!qp_comms_start(device)) {
  186. qp_dprintf("qp_drawimage_recolor: fail (could not start comms)\n");
  187. return false;
  188. }
  189. uint16_t l, t, r, b;
  190. if (frame_info->is_delta) {
  191. l = x + frame_info->left;
  192. t = y + frame_info->top;
  193. r = x + frame_info->right - 1;
  194. b = y + frame_info->bottom - 1;
  195. } else {
  196. l = x;
  197. t = y;
  198. r = x + image->width - 1;
  199. b = y + image->height - 1;
  200. }
  201. uint32_t pixel_count = ((uint32_t)(r - l + 1)) * (b - t + 1);
  202. // Configure where we're going to be rendering to
  203. if (!driver->driver_vtable->viewport(device, l, t, r, b)) {
  204. qp_dprintf("qp_drawimage_recolor: fail (could not set viewport)\n");
  205. qp_comms_stop(device);
  206. return false;
  207. }
  208. // Set up the input state
  209. struct qp_internal_byte_input_state input_state = {.device = device, .src_stream = &qgf_image->stream};
  210. qp_internal_byte_input_callback input_callback = qp_internal_prepare_input_state(&input_state, frame_info->compression_scheme);
  211. if (input_callback == NULL) {
  212. qp_dprintf("qp_drawimage_recolor: fail (invalid image compression scheme)\n");
  213. qp_comms_stop(device);
  214. return false;
  215. }
  216. // Set up the output state
  217. struct qp_internal_pixel_output_state output_state = {.device = device, .pixel_write_pos = 0, .max_pixels = qp_internal_num_pixels_in_buffer(device)};
  218. // Decode the pixel data and stream to the display
  219. 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);
  220. // Any leftovers need transmission as well.
  221. if (ret && output_state.pixel_write_pos > 0) {
  222. ret &= driver->driver_vtable->pixdata(device, qp_internal_global_pixdata_buffer, output_state.pixel_write_pos);
  223. }
  224. qp_dprintf("qp_drawimage_recolor: %s\n", ret ? "ok" : "fail");
  225. qp_comms_stop(device);
  226. return ret;
  227. }
  228. 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) {
  229. qgf_frame_info_t frame_info = {0};
  230. qp_pixel_t fg_hsv888 = {.hsv888 = {.h = hue_fg, .s = sat_fg, .v = val_fg}};
  231. qp_pixel_t bg_hsv888 = {.hsv888 = {.h = hue_bg, .s = sat_bg, .v = val_bg}};
  232. return qp_drawimage_recolor_impl(device, x, y, image, 0, &frame_info, fg_hsv888, bg_hsv888);
  233. }
  234. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  235. // Quantum Painter External API: qp_animate
  236. deferred_token qp_animate(painter_device_t device, uint16_t x, uint16_t y, painter_image_handle_t image) {
  237. return qp_animate_recolor(device, x, y, image, 0, 0, 255, 0, 0, 0);
  238. }
  239. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  240. // Quantum Painter External API: qp_animate_recolor
  241. typedef struct animation_state_t {
  242. painter_device_t device;
  243. uint16_t x;
  244. uint16_t y;
  245. painter_image_handle_t image;
  246. qp_pixel_t fg_hsv888;
  247. qp_pixel_t bg_hsv888;
  248. uint16_t frame_number;
  249. deferred_token defer_token;
  250. } animation_state_t;
  251. static deferred_executor_t animation_executors[QUANTUM_PAINTER_CONCURRENT_ANIMATIONS] = {0};
  252. static animation_state_t animation_states[QUANTUM_PAINTER_CONCURRENT_ANIMATIONS] = {0};
  253. static deferred_token qp_render_animation_state(animation_state_t *state, uint16_t *delay_ms) {
  254. qgf_frame_info_t frame_info = {0};
  255. qp_dprintf("qp_render_animation_state: entry (frame #%d)\n", (int)state->frame_number);
  256. 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);
  257. if (ret) {
  258. ++state->frame_number;
  259. if (state->frame_number >= state->image->frame_count) {
  260. state->frame_number = 0;
  261. }
  262. *delay_ms = frame_info.delay;
  263. }
  264. qp_dprintf("qp_render_animation_state: %s (delay %dms)\n", ret ? "ok" : "fail", (int)(*delay_ms));
  265. return ret;
  266. }
  267. static uint32_t animation_callback(uint32_t trigger_time, void *cb_arg) {
  268. animation_state_t *state = (animation_state_t *)cb_arg;
  269. uint16_t delay_ms;
  270. bool ret = qp_render_animation_state(state, &delay_ms);
  271. if (!ret) {
  272. // Setting the device to NULL clears the animation slot
  273. state->device = NULL;
  274. }
  275. // If we're successful, keep animating -- returning 0 cancels the deferred execution
  276. return ret ? delay_ms : 0;
  277. }
  278. 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) {
  279. qp_dprintf("qp_animate_recolor: entry\n");
  280. animation_state_t *anim_state = NULL;
  281. for (int i = 0; i < QUANTUM_PAINTER_CONCURRENT_ANIMATIONS; ++i) {
  282. if (animation_states[i].device == NULL) {
  283. anim_state = &animation_states[i];
  284. break;
  285. }
  286. }
  287. if (!anim_state) {
  288. qp_dprintf("qp_animate_recolor: fail (could not find free animation slot)\n");
  289. return INVALID_DEFERRED_TOKEN;
  290. }
  291. // Prepare the animation state
  292. anim_state->device = device;
  293. anim_state->x = x;
  294. anim_state->y = y;
  295. anim_state->image = image;
  296. anim_state->fg_hsv888 = (qp_pixel_t){.hsv888 = {.h = hue_fg, .s = sat_fg, .v = val_fg}};
  297. anim_state->bg_hsv888 = (qp_pixel_t){.hsv888 = {.h = hue_bg, .s = sat_bg, .v = val_bg}};
  298. anim_state->frame_number = 0;
  299. // Draw the first frame
  300. uint16_t delay_ms;
  301. if (!qp_render_animation_state(anim_state, &delay_ms)) {
  302. anim_state->device = NULL; // disregard the allocated animation slot
  303. qp_dprintf("qp_animate_recolor: fail (could not render first frame)\n");
  304. return INVALID_DEFERRED_TOKEN;
  305. }
  306. // Set up the timer
  307. anim_state->defer_token = defer_exec_advanced(animation_executors, QUANTUM_PAINTER_CONCURRENT_ANIMATIONS, delay_ms, animation_callback, anim_state);
  308. if (anim_state->defer_token == INVALID_DEFERRED_TOKEN) {
  309. anim_state->device = NULL; // disregard the allocated animation slot
  310. qp_dprintf("qp_animate_recolor: fail (could not set up animation executor)\n");
  311. return INVALID_DEFERRED_TOKEN;
  312. }
  313. qp_dprintf("qp_animate_recolor: ok (deferred token = %d)\n", (int)anim_state->defer_token);
  314. return anim_state->defer_token;
  315. }
  316. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  317. // Quantum Painter External API: qp_stop_animation
  318. void qp_stop_animation(deferred_token anim_token) {
  319. for (int i = 0; i < QUANTUM_PAINTER_CONCURRENT_ANIMATIONS; ++i) {
  320. if (animation_states[i].defer_token == anim_token) {
  321. cancel_deferred_exec_advanced(animation_executors, QUANTUM_PAINTER_CONCURRENT_ANIMATIONS, anim_token);
  322. animation_states[i].device = NULL;
  323. return;
  324. }
  325. }
  326. }
  327. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  328. // Quantum Painter Core API: qp_internal_animation_tick
  329. void qp_internal_animation_tick(void) {
  330. static uint32_t last_anim_exec = 0;
  331. deferred_exec_advanced_task(animation_executors, QUANTUM_PAINTER_CONCURRENT_ANIMATIONS, &last_anim_exec);
  332. }