lv_refr.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501
  1. /**
  2. * @file lv_refr.c
  3. *
  4. */
  5. /*********************
  6. * INCLUDES
  7. *********************/
  8. #include "../../lv_conf.h"
  9. #include <stddef.h>
  10. #include "lv_refr.h"
  11. #include "lv_vdb.h"
  12. #include "../lv_hal/lv_hal_tick.h"
  13. #include "../lv_misc/lv_task.h"
  14. #include "../lv_misc/lv_mem.h"
  15. /*********************
  16. * DEFINES
  17. *********************/
  18. /**********************
  19. * TYPEDEFS
  20. **********************/
  21. typedef struct
  22. {
  23. lv_area_t area;
  24. uint8_t joined;
  25. }lv_join_t;
  26. /**********************
  27. * STATIC PROTOTYPES
  28. **********************/
  29. static void lv_refr_task(void * param);
  30. static void lv_refr_join_area(void);
  31. static void lv_refr_areas(void);
  32. #if LV_VDB_SIZE == 0
  33. static void lv_refr_area_no_vdb(const lv_area_t * area_p);
  34. #else
  35. static void lv_refr_area_with_vdb(const lv_area_t * area_p);
  36. static void lv_refr_area_part_vdb(const lv_area_t * area_p);
  37. #endif
  38. static lv_obj_t * lv_refr_get_top_obj(const lv_area_t * area_p, lv_obj_t * obj);
  39. static void lv_refr_obj_and_children(lv_obj_t * top_p, const lv_area_t * mask_p);
  40. static void lv_refr_obj(lv_obj_t * obj, const lv_area_t * mask_ori_p);
  41. /**********************
  42. * STATIC VARIABLES
  43. **********************/
  44. static lv_join_t inv_buf[LV_INV_FIFO_SIZE];
  45. static uint16_t inv_buf_p;
  46. static void (*monitor_cb)(uint32_t, uint32_t); /*Monitor the rendering time*/
  47. static void (*round_cb)(lv_area_t*); /*If set then called to modify invalidated areas for special display controllers*/
  48. static uint32_t px_num;
  49. /**********************
  50. * MACROS
  51. **********************/
  52. /**********************
  53. * GLOBAL FUNCTIONS
  54. **********************/
  55. /**
  56. * Initialize the screen refresh subsystem
  57. */
  58. void lv_refr_init(void)
  59. {
  60. inv_buf_p = 0;
  61. memset(inv_buf, 0, sizeof(inv_buf));
  62. lv_task_t* task;
  63. task = lv_task_create(lv_refr_task, LV_REFR_PERIOD, LV_TASK_PRIO_MID, NULL);
  64. lv_task_ready(task); /*Be sure the screen will be refreshed immediately on start up*/
  65. }
  66. /**
  67. * Invalidate an area
  68. * @param area_p pointer to area which should be invalidated
  69. */
  70. void lv_inv_area(const lv_area_t * area_p)
  71. {
  72. /*Clear the invalidate buffer if the parameter is NULL*/
  73. if(area_p == NULL) {
  74. inv_buf_p = 0;
  75. return;
  76. }
  77. lv_area_t scr_area;
  78. scr_area.x1 = 0;
  79. scr_area.y1 = 0;
  80. scr_area.x2 = LV_HOR_RES - 1;
  81. scr_area.y2 = LV_VER_RES - 1;
  82. lv_area_t com_area;
  83. bool suc;
  84. suc = lv_area_union(&com_area, area_p, &scr_area);
  85. /*The area is truncated to the screen*/
  86. if(suc != false) {
  87. if(round_cb) round_cb(&com_area);
  88. /*Save only if this area is not in one of the saved areas*/
  89. uint16_t i;
  90. for(i = 0; i < inv_buf_p; i++) {
  91. if(lv_area_is_in(&com_area, &inv_buf[i].area) != false) return;
  92. }
  93. /*Save the area*/
  94. if(inv_buf_p < LV_INV_FIFO_SIZE) {
  95. lv_area_copy(&inv_buf[inv_buf_p].area,&com_area);
  96. } else {/*If no place for the area add the screen*/
  97. inv_buf_p = 0;
  98. lv_area_copy(&inv_buf[inv_buf_p].area,&scr_area);
  99. }
  100. inv_buf_p ++;
  101. }
  102. }
  103. /**
  104. * Set a function to call after every refresh to announce the refresh time and the number of refreshed pixels
  105. * @param cb pointer to a callback function (void my_refr_cb(uint32_t time_ms, uint32_t px_num))
  106. * time_ms: refresh time in [ms]
  107. * px_num: not the drawn pixels but the number of affected pixels of the screen
  108. * (more pixels are drawn because of overlapping objects)
  109. */
  110. void lv_refr_set_monitor_cb(void (*cb)(uint32_t, uint32_t))
  111. {
  112. monitor_cb = cb;
  113. }
  114. /**
  115. * Called when an area is invalidated to modify the coordinates of the area.
  116. * Special display controllers may require special coordinate rounding
  117. * @param cb pointer to the a function which will modify the area
  118. */
  119. void lv_refr_set_round_cb(void(*cb)(lv_area_t*))
  120. {
  121. round_cb = cb;
  122. }
  123. /**
  124. * Get the number of areas in the buffer
  125. * @return number of invalid areas
  126. */
  127. uint16_t lv_refr_get_buf_size(void)
  128. {
  129. return inv_buf_p;
  130. }
  131. /**
  132. * Pop (delete) the last 'num' invalidated areas from the buffer
  133. * @param num number of areas to delete
  134. */
  135. void lv_refr_pop_from_buf(uint16_t num)
  136. {
  137. if(inv_buf_p < num) inv_buf_p = 0;
  138. else inv_buf_p -= num;
  139. }
  140. /**********************
  141. * STATIC FUNCTIONS
  142. **********************/
  143. /**
  144. * Called periodically to handle the refreshing
  145. * @param param unused
  146. */
  147. static void lv_refr_task(void * param)
  148. {
  149. (void)param;
  150. uint32_t start = lv_tick_get();
  151. lv_refr_join_area();
  152. lv_refr_areas();
  153. bool refr_done = false;
  154. if(inv_buf_p != 0) refr_done = true;
  155. memset(inv_buf, 0, sizeof(inv_buf));
  156. inv_buf_p = 0;
  157. /* In the callback lv_obj_inv can occur
  158. * therefore be sure the inv_buf is cleared prior to it*/
  159. if(refr_done != false) {
  160. if(monitor_cb != NULL) {
  161. monitor_cb(lv_tick_elaps(start), px_num);
  162. }
  163. }
  164. }
  165. /**
  166. * Join the areas which has got common parts
  167. */
  168. static void lv_refr_join_area(void)
  169. {
  170. uint32_t join_from;
  171. uint32_t join_in;
  172. lv_area_t joined_area;
  173. for(join_in = 0; join_in < inv_buf_p; join_in++) {
  174. if(inv_buf[join_in].joined != 0) continue;
  175. /*Check all areas to join them in 'join_in'*/
  176. for(join_from = 0; join_from < inv_buf_p; join_from++) {
  177. /*Handle only unjoined areas and ignore itself*/
  178. if(inv_buf[join_from].joined != 0 || join_in == join_from) {
  179. continue;
  180. }
  181. /*Check if the areas are on each other*/
  182. if(lv_area_is_on(&inv_buf[join_in].area,
  183. &inv_buf[join_from].area) == false)
  184. {
  185. continue;
  186. }
  187. lv_area_join(&joined_area, &inv_buf[join_in].area,
  188. &inv_buf[join_from].area);
  189. /*Join two area only if the joined area size is smaller*/
  190. if(lv_area_get_size(&joined_area) <
  191. (lv_area_get_size(&inv_buf[join_in].area) + lv_area_get_size(&inv_buf[join_from].area))) {
  192. lv_area_copy(&inv_buf[join_in].area, &joined_area);
  193. /*Mark 'join_form' is joined into 'join_in'*/
  194. inv_buf[join_from].joined = 1;
  195. }
  196. }
  197. }
  198. }
  199. /**
  200. * Refresh the joined areas
  201. */
  202. static void lv_refr_areas(void)
  203. {
  204. px_num = 0;
  205. uint32_t i;
  206. for(i = 0; i < inv_buf_p; i++) {
  207. /*Refresh the unjoined areas*/
  208. if(inv_buf[i].joined == 0) {
  209. /*If there is no VDB do simple drawing*/
  210. #if LV_VDB_SIZE == 0
  211. lv_refr_area_no_vdb(&inv_buf[i].area);
  212. #else
  213. /*If VDB is used...*/
  214. lv_refr_area_with_vdb(&inv_buf[i].area);
  215. #endif
  216. if(monitor_cb != NULL) px_num += lv_area_get_size(&inv_buf[i].area);
  217. }
  218. }
  219. }
  220. #if LV_VDB_SIZE == 0
  221. /**
  222. * Refresh an area if there is no Virtual Display Buffer
  223. * @param area_p pointer to an area to refresh
  224. */
  225. static void lv_refr_area_no_vdb(const lv_area_t * area_p)
  226. {
  227. lv_obj_t * top_p;
  228. /*Get top object which is not covered by others*/
  229. top_p = lv_refr_get_top_obj(area_p, lv_scr_act());
  230. /*Do the refreshing*/
  231. lv_refr_obj_and_children(top_p, area_p);
  232. }
  233. #else
  234. /**
  235. * Refresh an area if there is Virtual Display Buffer
  236. * @param area_p pointer to an area to refresh
  237. */
  238. static void lv_refr_area_with_vdb(const lv_area_t * area_p)
  239. {
  240. /*Calculate the max row num*/
  241. lv_coord_t w = lv_area_get_width(area_p);
  242. lv_coord_t h = lv_area_get_height(area_p);
  243. lv_coord_t y2 = area_p->y2 >= LV_VER_RES ? y2 = LV_VER_RES - 1 : area_p->y2;
  244. uint32_t max_row = (uint32_t) LV_VDB_SIZE / w;
  245. if(max_row > h) max_row = h;
  246. /*Always use the full row*/
  247. uint32_t row;
  248. lv_coord_t row_last = 0;
  249. for(row = area_p->y1; row + max_row - 1 <= y2; row += max_row) {
  250. lv_vdb_t * vdb_p = lv_vdb_get();
  251. /*Calc. the next y coordinates of VDB*/
  252. vdb_p->area.x1 = area_p->x1;
  253. vdb_p->area.x2 = area_p->x2;
  254. vdb_p->area.y1 = row;
  255. vdb_p->area.y2 = row + max_row - 1;
  256. if(vdb_p->area.y2 > y2) vdb_p->area.y2 = y2;
  257. row_last = vdb_p->area.y2;
  258. lv_refr_area_part_vdb(area_p);
  259. }
  260. /*If the last y coordinates are not handled yet ...*/
  261. if(y2 != row_last) {
  262. lv_vdb_t * vdb_p = lv_vdb_get();
  263. /*Calc. the next y coordinates of VDB*/
  264. vdb_p->area.x1 = area_p->x1;
  265. vdb_p->area.x2 = area_p->x2;
  266. vdb_p->area.y1 = row;
  267. vdb_p->area.y2 = y2;
  268. /*Refresh this part too*/
  269. lv_refr_area_part_vdb(area_p);
  270. }
  271. }
  272. /**
  273. * Refresh a part of an area which is on the actual Virtual Display Buffer
  274. * @param area_p pointer to an area to refresh
  275. */
  276. static void lv_refr_area_part_vdb(const lv_area_t * area_p)
  277. {
  278. lv_vdb_t * vdb_p = lv_vdb_get();
  279. lv_obj_t * top_p;
  280. /*Get the new mask from the original area and the act. VDB
  281. It will be a part of 'area_p'*/
  282. lv_area_t start_mask;
  283. lv_area_union(&start_mask, area_p, &vdb_p->area);
  284. /*Get the most top object which is not covered by others*/
  285. top_p = lv_refr_get_top_obj(&start_mask, lv_scr_act());
  286. /*Do the refreshing from the top object*/
  287. lv_refr_obj_and_children(top_p, &start_mask);
  288. /*Also refresh top and sys layer unconditionally*/
  289. lv_refr_obj_and_children(lv_layer_top(), &start_mask);
  290. lv_refr_obj_and_children(lv_layer_sys(), &start_mask);
  291. /*Flush the content of the VDB*/
  292. lv_vdb_flush();
  293. }
  294. #endif /*LV_VDB_SIZE == 0*/
  295. /**
  296. * Search the most top object which fully covers an area
  297. * @param area_p pointer to an area
  298. * @param obj the first object to start the searching (typically a screen)
  299. * @return
  300. */
  301. static lv_obj_t * lv_refr_get_top_obj(const lv_area_t * area_p, lv_obj_t * obj)
  302. {
  303. lv_obj_t * i;
  304. lv_obj_t * found_p = NULL;
  305. /*If this object is fully cover the draw area check the children too */
  306. if(lv_area_is_in(area_p, &obj->coords) && obj->hidden == 0)
  307. {
  308. LL_READ(obj->child_ll, i) {
  309. found_p = lv_refr_get_top_obj(area_p, i);
  310. /*If a children is ok then break*/
  311. if(found_p != NULL) {
  312. break;
  313. }
  314. }
  315. /*If no better children check this object*/
  316. if(found_p == NULL) {
  317. lv_style_t * style = lv_obj_get_style(obj);
  318. if(style->body.opa == LV_OPA_COVER &&
  319. obj->design_func(obj, area_p, LV_DESIGN_COVER_CHK) != false) {
  320. found_p = obj;
  321. }
  322. }
  323. }
  324. return found_p;
  325. }
  326. /**
  327. * Make the refreshing from an object. Draw all its children and the youngers too.
  328. * @param top_p pointer to an objects. Start the drawing from it.
  329. * @param mask_p pointer to an area, the objects will be drawn only here
  330. */
  331. static void lv_refr_obj_and_children(lv_obj_t * top_p, const lv_area_t * mask_p)
  332. {
  333. /* Normally always will be a top_obj (at least the screen)
  334. * but in special cases (e.g. if the screen has alpha) it won't.
  335. * In this case use the screen directly */
  336. if(top_p == NULL) top_p = lv_scr_act();
  337. /*Refresh the top object and its children*/
  338. lv_refr_obj(top_p, mask_p);
  339. /*Draw the 'younger' sibling objects because they can be on top_obj */
  340. lv_obj_t * par;
  341. lv_obj_t * i;
  342. lv_obj_t * border_p = top_p;
  343. par = lv_obj_get_parent(top_p);
  344. /*Do until not reach the screen*/
  345. while(par != NULL) {
  346. /*object before border_p has to be redrawn*/
  347. i = lv_ll_get_prev(&(par->child_ll), border_p);
  348. while(i != NULL) {
  349. /*Refresh the objects*/
  350. lv_refr_obj(i, mask_p);
  351. i = lv_ll_get_prev(&(par->child_ll), i);
  352. }
  353. /*The new border will be there last parents,
  354. *so the 'younger' brothers of parent will be refreshed*/
  355. border_p = par;
  356. /*Go a level deeper*/
  357. par = lv_obj_get_parent(par);
  358. }
  359. /*Call the post draw design function of the parents of the to object*/
  360. par = lv_obj_get_parent(top_p);
  361. while(par != NULL) {
  362. par->design_func(par, mask_p, LV_DESIGN_DRAW_POST);
  363. par = lv_obj_get_parent(par);
  364. }
  365. }
  366. /**
  367. * Refresh an object an all of its children. (Called recursively)
  368. * @param obj pointer to an object to refresh
  369. * @param mask_ori_p pointer to an area, the objects will be drawn only here
  370. */
  371. static void lv_refr_obj(lv_obj_t * obj, const lv_area_t * mask_ori_p)
  372. {
  373. /*Do not refresh hidden objects*/
  374. if(obj->hidden != 0) return;
  375. bool union_ok; /* Store the return value of area_union */
  376. /* Truncate the original mask to the coordinates of the parent
  377. * because the parent and its children are visible only here */
  378. lv_area_t obj_mask;
  379. lv_area_t obj_ext_mask;
  380. lv_area_t obj_area;
  381. lv_coord_t ext_size = obj->ext_size;
  382. lv_obj_get_coords(obj, &obj_area);
  383. obj_area.x1 -= ext_size;
  384. obj_area.y1 -= ext_size;
  385. obj_area.x2 += ext_size;
  386. obj_area.y2 += ext_size;
  387. union_ok = lv_area_union(&obj_ext_mask, mask_ori_p, &obj_area);
  388. /*Draw the parent and its children only if they ore on 'mask_parent'*/
  389. if(union_ok != false) {
  390. /* Redraw the object */
  391. lv_style_t * style = lv_obj_get_style(obj);
  392. if(style->body.opa != LV_OPA_TRANSP) {
  393. obj->design_func(obj, &obj_ext_mask, LV_DESIGN_DRAW_MAIN);
  394. //tick_wait_ms(100); /*DEBUG: Wait after every object draw to see the order of drawing*/
  395. }
  396. /*Create a new 'obj_mask' without 'ext_size' because the children can't be visible there*/
  397. lv_obj_get_coords(obj, &obj_area);
  398. union_ok = lv_area_union(&obj_mask, mask_ori_p, &obj_area);
  399. if(union_ok != false) {
  400. lv_area_t mask_child; /*Mask from obj and its child*/
  401. lv_obj_t * child_p;
  402. lv_area_t child_area;
  403. LL_READ_BACK(obj->child_ll, child_p)
  404. {
  405. lv_obj_get_coords(child_p, &child_area);
  406. ext_size = child_p->ext_size;
  407. child_area.x1 -= ext_size;
  408. child_area.y1 -= ext_size;
  409. child_area.x2 += ext_size;
  410. child_area.y2 += ext_size;
  411. /* Get the union (common parts) of original mask (from obj)
  412. * and its child */
  413. union_ok = lv_area_union(&mask_child, &obj_mask, &child_area);
  414. /*If the parent and the child has common area then refresh the child */
  415. if(union_ok) {
  416. /*Refresh the next children*/
  417. lv_refr_obj(child_p, &mask_child);
  418. }
  419. }
  420. }
  421. /* If all the children are redrawn make 'post draw' design */
  422. if(style->body.opa != LV_OPA_TRANSP) {
  423. obj->design_func(obj, &obj_ext_mask, LV_DESIGN_DRAW_POST);
  424. }
  425. }
  426. }