You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

339 lines
10 KiB

  1. /*
  2. * libcaca Colour ASCII-Art library
  3. * Copyright (c) 2002-2009 Sam Hocevar <sam@hocevar.net>
  4. * All Rights Reserved
  5. *
  6. * $Id$
  7. *
  8. * This library is free software. It comes without any warranty, to
  9. * the extent permitted by applicable law. You can redistribute it
  10. * and/or modify it under the terms of the Do What The Fuck You Want
  11. * To Public License, Version 2, as published by Sam Hocevar. See
  12. * http://sam.zoy.org/wtfpl/COPYING for more details.
  13. */
  14. /*
  15. * This file contains the dirty rectangle handling functions.
  16. *
  17. *
  18. * About dirty rectangles:
  19. *
  20. * * Dirty rectangles MUST NOT be larger than the canvas. If the user
  21. * provides a large rectangle through caca_add_dirty_rect(), or if the
  22. * canvas changes size to become smaller, all dirty rectangles MUST
  23. * immediately be clipped to the canvas size.
  24. */
  25. #include "config.h"
  26. #if !defined(__KERNEL__)
  27. # include <stdio.h>
  28. # include <string.h>
  29. #endif
  30. #include "caca.h"
  31. #include "caca_internals.h"
  32. static void merge_new_rect(caca_canvas_t *cv, int n);
  33. /** \brief Get the number of dirty rectangles in the canvas.
  34. *
  35. * Get the number of dirty rectangles in a canvas. Dirty rectangles are
  36. * areas that contain cells that have changed since the last reset.
  37. *
  38. * The dirty rectangles are used internally by display drivers to optimise
  39. * rendering by avoiding to redraw the whole screen. Once the display driver
  40. * has rendered the canvas, it resets the dirty rectangle list.
  41. *
  42. * Dirty rectangles are guaranteed not to overlap.
  43. *
  44. * This function never fails.
  45. *
  46. * \param cv A libcaca canvas.
  47. * \return The number of dirty rectangles in the given canvas.
  48. */
  49. int caca_get_dirty_rect_count(caca_canvas_t *cv)
  50. {
  51. return cv->ndirty;
  52. }
  53. /** \brief Get a canvas's dirty rectangle.
  54. *
  55. * Get the canvas's given dirty rectangle coordinates. The index must be
  56. * within the dirty rectangle count. See caca_get_dirty_rect_count()
  57. * for how to compute this count.
  58. *
  59. * If an error occurs, no coordinates are written in the pointer arguments,
  60. * -1 is returned and \b errno is set accordingly:
  61. * - \c EINVAL Specified rectangle index is out of bounds.
  62. *
  63. * \param cv A libcaca canvas.
  64. * \param r The requested rectangle index.
  65. * \param x A pointer to an integer where the leftmost edge of the
  66. * dirty rectangle will be stored.
  67. * \param y A pointer to an integer where the topmost edge of the
  68. * dirty rectangle will be stored.
  69. * \param width A pointer to an integer where the width of the
  70. * dirty rectangle will be stored.
  71. * \param height A pointer to an integer where the height of the
  72. * dirty rectangle will be stored.
  73. * \return 0 in case of success, -1 if an error occurred.
  74. */
  75. int caca_get_dirty_rect(caca_canvas_t *cv, int r,
  76. int *x, int *y, int *width, int *height)
  77. {
  78. if(r < 0 || r >= cv->ndirty)
  79. {
  80. seterrno(EINVAL);
  81. return -1;
  82. }
  83. *x = cv->dirty[r].xmin;
  84. *y = cv->dirty[r].ymin;
  85. *width = cv->dirty[r].xmax - cv->dirty[r].xmin + 1;
  86. *height = cv->dirty[r].ymax - cv->dirty[r].ymin + 1;
  87. debug("dirty #%i: %ix%i at (%i,%i)", r, *width, *height, *x, *y);
  88. return 0;
  89. }
  90. /** \brief Add an area to the canvas's dirty rectangle list.
  91. *
  92. * Add an invalidating zone to the canvas's dirty rectangle list. For more
  93. * information about the dirty rectangles, see caca_get_dirty_rect().
  94. *
  95. * This function may be useful to force refresh of a given zone of the
  96. * canvas even if the dirty rectangle tracking indicates that it is
  97. * unchanged. This may happen if the canvas contents were somewhat
  98. * directly modified.
  99. *
  100. * If an error occurs, -1 is returned and \b errno is set accordingly:
  101. * - \c EINVAL Specified rectangle coordinates are out of bounds.
  102. *
  103. * \param cv A libcaca canvas.
  104. * \param x The leftmost edge of the additional dirty rectangle.
  105. * \param y The topmost edge of the additional dirty rectangle.
  106. * \param width The width of the additional dirty rectangle.
  107. * \param height The height of the additional dirty rectangle.
  108. * \return 0 in case of success, -1 if an error occurred.
  109. */
  110. int caca_add_dirty_rect(caca_canvas_t *cv, int x, int y, int width, int height)
  111. {
  112. debug("new dirty: %ix%i at (%i,%i)", width, height, x, y);
  113. /* Clip arguments to canvas */
  114. if(x < 0) { width += x; x = 0; }
  115. if(x + width > cv->width)
  116. width = cv->width - x;
  117. if(y < 0) { height += y; y = 0; }
  118. if(y + height > cv->height)
  119. height = cv->height - y;
  120. /* Ignore empty and out-of-canvas rectangles */
  121. if(width <= 0 || height <= 0)
  122. {
  123. seterrno(EINVAL);
  124. return -1;
  125. }
  126. /* Add the new rectangle to the list; it works even if cv->ndirty
  127. * is MAX_DIRTY_COUNT because there's an extra cell in the array. */
  128. cv->dirty[cv->ndirty].xmin = x;
  129. cv->dirty[cv->ndirty].ymin = y;
  130. cv->dirty[cv->ndirty].xmax = x + width - 1;
  131. cv->dirty[cv->ndirty].ymax = y + height - 1;
  132. cv->ndirty++;
  133. /* Try to merge the new rectangle with existing ones. This also ensures
  134. * that cv->ndirty is brought back below MAX_DIRTY_COUNT. */
  135. merge_new_rect(cv, cv->ndirty - 1);
  136. return 0;
  137. }
  138. /** \brief Remove an area from the dirty rectangle list.
  139. *
  140. * Mark a cell area in the canvas as not dirty. For more information about
  141. * the dirty rectangles, see caca_get_dirty_rect().
  142. *
  143. * Values such that \b xmin > \b xmax or \b ymin > \b ymax indicate that
  144. * the dirty rectangle is empty. They will be silently ignored.
  145. *
  146. * If an error occurs, -1 is returned and \b errno is set accordingly:
  147. * - \c EINVAL Specified rectangle coordinates are out of bounds.
  148. *
  149. * \param cv A libcaca canvas.
  150. * \param x The leftmost edge of the clean rectangle.
  151. * \param y The topmost edge of the clean rectangle.
  152. * \param width The width of the clean rectangle.
  153. * \param height The height of the clean rectangle.
  154. * \return 0 in case of success, -1 if an error occurred.
  155. */
  156. int caca_remove_dirty_rect(caca_canvas_t *cv, int x, int y,
  157. int width, int height)
  158. {
  159. /* Clip arguments to canvas size */
  160. if(x < 0) { width += x; x = 0; }
  161. if(x + width > cv->width)
  162. width = cv->width - x;
  163. if(y < 0) { height += y; y = 0; }
  164. if(y + height > cv->height)
  165. height = cv->height - y;
  166. /* Ignore empty and out-of-canvas rectangles */
  167. if(width <= 0 || height <= 0)
  168. {
  169. seterrno(EINVAL);
  170. return -1;
  171. }
  172. /* FIXME: implement this function. It's OK to have it do nothing,
  173. * since we take a conservative approach in dirty rectangle handling,
  174. * but we ought to help the rendering eventually. */
  175. return 0;
  176. }
  177. /** \brief Clear a canvas's dirty rectangle list.
  178. *
  179. * Empty the canvas's dirty rectangle list.
  180. *
  181. * This function never fails.
  182. *
  183. * \param cv A libcaca canvas.
  184. * \return This function always returns 0.
  185. */
  186. int caca_clear_dirty_rect_list(caca_canvas_t *cv)
  187. {
  188. cv->ndirty = 0;
  189. return 0;
  190. }
  191. /*
  192. * XXX: the following functions are local.
  193. */
  194. static inline int int_min(int a, int b) { return a < b ? a : b; }
  195. static inline int int_max(int a, int b) { return a > b ? a : b; }
  196. /* Merge a newly added rectangle, if necessary. */
  197. static void merge_new_rect(caca_canvas_t *cv, int n)
  198. {
  199. int wasted[MAX_DIRTY_COUNT + 1];
  200. int i, sn, best, best_score;
  201. best = -1;
  202. best_score = cv->width * cv->height;
  203. sn = (cv->dirty[n].xmax - cv->dirty[n].xmin + 1)
  204. * (cv->dirty[n].ymax - cv->dirty[n].ymin + 1);
  205. /* Check whether the new rectangle can be merged with an existing one. */
  206. for(i = 0; i < cv->ndirty; i++)
  207. {
  208. int si, sf, xmin, ymin, xmax, ymax;
  209. if(i == n)
  210. continue;
  211. xmin = int_min(cv->dirty[i].xmin, cv->dirty[n].xmin);
  212. ymin = int_min(cv->dirty[i].ymin, cv->dirty[n].ymin);
  213. xmax = int_max(cv->dirty[i].xmax, cv->dirty[n].xmax);
  214. ymax = int_max(cv->dirty[i].ymax, cv->dirty[n].ymax);
  215. sf = (xmax - xmin + 1) * (ymax - ymin + 1);
  216. /* Shortcut: if the current rectangle is inside the new rectangle,
  217. * we remove the current rectangle and continue trying merges. */
  218. if(sf == sn)
  219. {
  220. memmove(&cv->dirty[i], &cv->dirty[i + 1],
  221. (cv->ndirty - i) * sizeof(cv->dirty[0]));
  222. cv->ndirty--;
  223. if(i < n)
  224. n--;
  225. else
  226. i--;
  227. continue;
  228. }
  229. si = (cv->dirty[i].xmax - cv->dirty[i].xmin + 1)
  230. * (cv->dirty[i].ymax - cv->dirty[i].ymin + 1);
  231. /* Shortcut: if the new rectangle is inside the current rectangle,
  232. * we get rid of the new rectangle and bail out. */
  233. if(sf == si)
  234. {
  235. cv->ndirty--;
  236. memmove(&cv->dirty[n], &cv->dirty[n + 1],
  237. (cv->ndirty - n) * sizeof(cv->dirty[0]));
  238. return;
  239. }
  240. /* We store approximately how many bytes were wasted. FIXME: this is
  241. * not an exact computation, we need to be more precise. */
  242. wasted[i] = sf - si - sn;
  243. if(wasted[i] < best_score)
  244. {
  245. best = i;
  246. best_score = wasted[i];
  247. }
  248. }
  249. /* FIXME: we only try to merge the current rectangle, ignoring
  250. * potentially better merges. */
  251. /* If no acceptable score was found and the dirty rectangle list is
  252. * not full, we bail out. */
  253. if(best_score > 0 && cv->ndirty < MAX_DIRTY_COUNT)
  254. return;
  255. /* Otherwise, merge the rectangle with the best candidate */
  256. cv->dirty[best].xmin = int_min(cv->dirty[best].xmin, cv->dirty[n].xmin);
  257. cv->dirty[best].ymin = int_min(cv->dirty[best].ymin, cv->dirty[n].ymin);
  258. cv->dirty[best].xmax = int_max(cv->dirty[best].xmax, cv->dirty[n].xmax);
  259. cv->dirty[best].ymax = int_max(cv->dirty[best].ymax, cv->dirty[n].ymax);
  260. memmove(&cv->dirty[n], &cv->dirty[n + 1],
  261. (cv->ndirty - n) * sizeof(cv->dirty[0]));
  262. cv->ndirty--;
  263. if(best < n)
  264. merge_new_rect(cv, best);
  265. else
  266. merge_new_rect(cv, best - 1);
  267. }
  268. /* Clip all dirty rectangles in case they're larger than the canvas */
  269. void _caca_clip_dirty_rect_list(caca_canvas_t *cv)
  270. {
  271. int i;
  272. for(i = 0; i < cv->ndirty; i++)
  273. {
  274. if(cv->dirty[i].xmin < 0)
  275. cv->dirty[i].xmin = 0;
  276. if(cv->dirty[i].ymin < 0)
  277. cv->dirty[i].ymin = 0;
  278. if(cv->dirty[i].xmax >= cv->width)
  279. cv->dirty[i].xmax = cv->width - 1;
  280. if(cv->dirty[i].ymax >= cv->height)
  281. cv->dirty[i].ymax = cv->height - 1;
  282. }
  283. }