display_composer.c 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962
  1. /*
  2. * Copyright (c) 2020 Actions Technology Co., Ltd
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. */
  6. #define LOG_MODULE_CUSTOMER
  7. /*********************
  8. * INCLUDES
  9. *********************/
  10. #include <assert.h>
  11. #include <string.h>
  12. #include <sys/slist.h>
  13. #include <zephyr.h>
  14. #include <drivers/display/display_engine.h>
  15. #ifdef CONFIG_TRACING
  16. # include <tracing/tracing.h>
  17. #endif
  18. #include <display/display_composer.h>
  19. #include <board_cfg.h>
  20. #include <logging/log.h>
  21. LOG_MODULE_REGISTER(composer, LOG_LEVEL_INF);
  22. /*********************
  23. * DEFINES
  24. *********************/
  25. #define CONFIG_COMPOSER_POST_NO_WAIT
  26. #ifndef CONFIG_PANEL_ROUND_SHAPE
  27. # define CONFIG_PANEL_ROUND_SHAPE 0
  28. #endif
  29. #ifndef CONFIG_PANEL_NUM_SCREEN_AREAS
  30. # define CONFIG_PANEL_NUM_SCREEN_AREAS 1
  31. #endif
  32. #ifdef CONFIG_PANEL_FULL_SCREEN_OPT_AREA
  33. static const ui_region_t g_screen_areas[] = CONFIG_PANEL_FULL_SCREEN_OPT_AREA;
  34. # define NUM_SCREEN_AREAS ARRAY_SIZE(g_screen_areas)
  35. # define HAS_SCREEN_AREAS (1)
  36. #elif CONFIG_PANEL_ROUND_SHAPE
  37. # if CONFIG_PANEL_NUM_SCREEN_AREAS >= 7
  38. # define NUM_SCREEN_AREAS (7)
  39. # elif CONFIG_PANEL_NUM_SCREEN_AREAS >= 3
  40. # define NUM_SCREEN_AREAS (3)
  41. # else
  42. # define NUM_SCREEN_AREAS (1)
  43. # endif
  44. # if NUM_SCREEN_AREAS > 1
  45. # define HAS_SCREEN_AREAS (1)
  46. static ui_region_t g_screen_areas[NUM_SCREEN_AREAS];
  47. # endif
  48. #else
  49. # define NUM_SCREEN_AREAS (1)
  50. # define HAS_SCREEN_AREAS (0)
  51. #endif /* CONFIG_PANEL_FULL_SCREEN_OPT_AREA */
  52. /* 3 frames */
  53. #if defined(CONFIG_UI_SERVICE) && defined(CONFIG_SURFACE_ZERO_BUFFER) && defined(CONFIG_LVGL)
  54. # define NUM_POST_ENTRIES (NUM_SCREEN_AREAS * CONFIG_LV_VDB_NUM)
  55. #else
  56. # define NUM_POST_ENTRIES (NUM_SCREEN_AREAS * 3)
  57. #endif
  58. /**********************
  59. * TYPEDEFS
  60. **********************/
  61. typedef struct post_entry {
  62. uint16_t flags;
  63. uint8_t n_ovls;
  64. display_layer_t ovls[NUM_POST_LAYERS];
  65. display_buffer_t bufs[NUM_POST_LAYERS];
  66. graphic_buffer_t *graphic_bufs[NUM_POST_LAYERS];
  67. display_composer_post_cleanup_t cleanup_cb[NUM_POST_LAYERS];
  68. void *cleanup_data[NUM_POST_LAYERS];
  69. } post_entry_t;
  70. typedef struct display_composer {
  71. /* display (panel) device */
  72. const struct device *disp_dev;
  73. /* display device callback */
  74. struct display_callback disp_cb;
  75. /* display write pixel format */
  76. struct display_capabilities disp_cap;
  77. /* display engine device */
  78. const struct device *de_dev;
  79. int de_inst;
  80. /* user display callback */
  81. const struct display_callback *user_cb;
  82. /* post entries */
  83. post_entry_t post_entries[NUM_POST_ENTRIES];
  84. uint8_t cplt_idx;
  85. uint8_t free_idx;
  86. uint8_t post_idx;
  87. /* post state */
  88. uint8_t post_cnt; /* number of posts transferring and waiting to progress */
  89. uint8_t post_inprog; /* any post is transferring */
  90. /* control post frame rate */
  91. uint8_t post_frame_period;
  92. uint8_t post_frame_cnt;
  93. /* display state */
  94. uint8_t max_layers : 3; /* supported maximum layers */
  95. uint8_t disp_has_vsync : 1; /* test really has vsyn signal */
  96. uint8_t disp_active : 1; /* has power or not */
  97. uint8_t disp_lowpower : 1; /* in low power state or not */
  98. uint8_t first_frame : 1; /* first frame after resume */
  99. /* test frame crossing vsync or not */
  100. uint8_t vsync_counter;
  101. uint8_t frame_start_vsync_cnt;
  102. uint32_t frame_start_cycle;
  103. #ifdef CONFIG_DISPLAY_COMPOSER_DEBUG_FPS
  104. uint32_t frame_timestamp;
  105. uint16_t frame_cnt;
  106. #endif
  107. #ifdef CONFIG_DISPLAY_COMPOSER_DEBUG_VSYNC
  108. uint32_t vsync_timestamp; /* measure in cycles */
  109. uint32_t vsync_print_timestamp; /* measure in cycles */
  110. #endif
  111. struct k_spinlock post_lock;
  112. #ifndef CONFIG_COMPOSER_POST_NO_WAIT
  113. struct k_sem post_sem;
  114. #endif
  115. } display_composer_t;
  116. /**********************
  117. * STATIC PROTOTYPES
  118. **********************/
  119. static void _composer_display_vsync_handler(const struct display_callback *callback, uint32_t timestamp);
  120. static void _composer_display_complete_handler(const struct display_callback *callback);
  121. static void _composer_display_pm_notify_handler(const struct display_callback *callback, uint32_t pm_action);
  122. static uint8_t _composer_num_free_entries_get(display_composer_t *composer);
  123. static post_entry_t *_composer_find_free_entry(display_composer_t *composer);
  124. static int _composer_post_top_entry(display_composer_t *composer, bool require_not_first);
  125. static void _composer_cleanup_entry(display_composer_t *composer, post_entry_t *entry);
  126. static void _composer_dump_entry(post_entry_t *entry);
  127. static int _composer_post_entry_noram(display_composer_t *composer);
  128. static void _composer_de_complete_handler(int status, uint16_t cmd_seq, void *user_data);
  129. #if !defined(CONFIG_PANEL_FULL_SCREEN_OPT_AREA) && HAS_SCREEN_AREAS
  130. static void _compute_round_screen_areas(uint16_t screen_size, ui_region_t areas[], uint8_t n_areas);
  131. #endif
  132. /**********************
  133. * STATIC VARIABLES
  134. **********************/
  135. static display_composer_t display_composer __in_section_unique(ram.noinit.display_composer);
  136. /**********************
  137. * INLINE FUNCTIONS
  138. **********************/
  139. static inline display_composer_t *_composer_get(void)
  140. {
  141. return &display_composer;
  142. }
  143. static inline bool _composer_has_gram(display_composer_t *composer)
  144. {
  145. return (composer->disp_cap.screen_info & SCREEN_INFO_ZERO_BUFFER) ? false : true;
  146. }
  147. /**********************
  148. * GLOBAL FUNCTIONS
  149. **********************/
  150. int display_composer_init(void)
  151. {
  152. display_composer_t *composer = _composer_get();
  153. memset(composer, 0, sizeof(*composer));
  154. composer->disp_active = 1;
  155. composer->first_frame = 1;
  156. composer->disp_dev = device_get_binding(CONFIG_LCD_DISPLAY_DEV_NAME);
  157. if (composer->disp_dev == NULL) {
  158. SYS_LOG_ERR("cannot find display " CONFIG_LCD_DISPLAY_DEV_NAME);
  159. return -ENODEV;
  160. }
  161. composer->de_dev = device_get_binding(CONFIG_DISPLAY_ENGINE_DEV_NAME);
  162. if (composer->de_dev) {
  163. composer->de_inst = display_engine_open(composer->de_dev,
  164. DISPLAY_ENGINE_FLAG_HIGH_PRIO | DISPLAY_ENGINE_FLAG_POST);
  165. } else {
  166. composer->de_inst = -1;
  167. }
  168. if (composer->de_inst >= 0) {
  169. struct display_engine_capabilities de_cap;
  170. display_engine_get_capabilities(composer->de_dev, &de_cap);
  171. composer->max_layers = MIN(de_cap.num_overlays, NUM_POST_LAYERS);
  172. } else {
  173. composer->max_layers = 1;
  174. }
  175. SYS_LOG_INF("supported layer num %d", composer->max_layers);
  176. #ifndef CONFIG_COMPOSER_POST_NO_WAIT
  177. k_sem_init(&composer->post_sem, NUM_POST_ENTRIES, NUM_POST_ENTRIES);
  178. #endif
  179. display_get_capabilities(composer->disp_dev, &composer->disp_cap);
  180. composer->post_frame_period = 1;
  181. if (composer->disp_cap.screen_info & SCREEN_INFO_ZERO_BUFFER) {
  182. SYS_LOG_WRN("panel no ram\n");
  183. assert(composer->de_inst >= 0);
  184. display_engine_register_callback(composer->de_dev, composer->de_inst,
  185. _composer_de_complete_handler, composer);
  186. }
  187. composer->disp_cb.vsync = _composer_display_vsync_handler;
  188. composer->disp_cb.complete = _composer_display_complete_handler;
  189. composer->disp_cb.pm_notify = _composer_display_pm_notify_handler;
  190. display_register_callback(composer->disp_dev, &composer->disp_cb);
  191. #ifdef CONFIG_PANEL_FULL_SCREEN_OPT_AREA
  192. if (g_screen_areas[0].y1 != 0 ||
  193. g_screen_areas[NUM_SCREEN_AREAS - 1].y2 != composer->disp_cap.y_resolution - 1) {
  194. SYS_LOG_ERR("composer clip areas invalid\n");
  195. }
  196. #elif HAS_SCREEN_AREAS
  197. _compute_round_screen_areas(composer->disp_cap.x_resolution, g_screen_areas, NUM_SCREEN_AREAS);
  198. #endif
  199. SYS_LOG_INF("composer initialized\n");
  200. return 0;
  201. }
  202. void display_composer_destroy(void)
  203. {
  204. SYS_LOG_INF("composer finalized\n");
  205. }
  206. void display_composer_register_callback(const struct display_callback *callback)
  207. {
  208. display_composer_t *composer = _composer_get();
  209. composer->user_cb = callback;
  210. }
  211. void display_composer_set_post_period(uint8_t multiple)
  212. {
  213. display_composer_t *composer = _composer_get();
  214. unsigned int key = os_irq_lock();
  215. composer->post_frame_period = multiple;
  216. composer->post_frame_cnt = 0;
  217. os_irq_unlock(key);
  218. }
  219. uint8_t display_composer_get_refresh_rate(void)
  220. {
  221. display_composer_t *composer = _composer_get();
  222. return composer->disp_cap.refresh_rate;
  223. }
  224. int display_composer_get_geometry(uint16_t *width, uint16_t *height,
  225. uint32_t *pixel_format, uint8_t *round_screen)
  226. {
  227. display_composer_t *composer = _composer_get();
  228. if (width) {
  229. *width = composer->disp_cap.x_resolution;
  230. }
  231. if (height) {
  232. *height = composer->disp_cap.y_resolution;
  233. }
  234. if (pixel_format) {
  235. *pixel_format = composer->disp_cap.current_pixel_format;
  236. }
  237. if (round_screen) {
  238. *round_screen = (composer->disp_cap.screen_info & SCREEN_INFO_ROUND_SHAPE) ? 1 : 0;
  239. }
  240. return 0;
  241. }
  242. uint16_t display_composer_get_orientation(void)
  243. {
  244. display_composer_t *composer = _composer_get();
  245. return (uint16_t)composer->disp_cap.current_orientation * 90;
  246. }
  247. uint8_t display_composer_get_num_layers(void)
  248. {
  249. display_composer_t *composer = _composer_get();
  250. return composer->max_layers;
  251. }
  252. int display_composer_set_brightness(uint8_t brightness)
  253. {
  254. display_composer_t *composer = _composer_get();
  255. if (composer->disp_dev == NULL) {
  256. return -ENODEV;
  257. }
  258. return display_set_brightness(composer->disp_dev, brightness);
  259. }
  260. void display_composer_round(ui_region_t *region)
  261. {
  262. display_composer_t *composer = _composer_get();
  263. struct display_capabilities *cap = &composer->disp_cap;
  264. /* Some LCD driver IC require even pixel alignment, so set even area if possible */
  265. if (cap->screen_info & SCREEN_INFO_X_ALIGNMENT_WIDTH) {
  266. region->x1 = 0;
  267. region->x2 = cap->x_resolution - 1;
  268. } else if (!(cap->x_resolution & 0x1)) {
  269. region->x1 &= ~0x1;
  270. region->x2 |= 0x1;
  271. }
  272. if (cap->screen_info & SCREEN_INFO_Y_ALIGNMENT_HEIGHT) {
  273. region->y1 = 0;
  274. region->y2 = cap->y_resolution - 1;
  275. } else if (!(cap->y_resolution & 0x1)) {
  276. region->y1 &= ~0x1;
  277. region->y2 |= 0x1;
  278. }
  279. }
  280. /**********************
  281. * STATIC FUNCTIONS
  282. **********************/
  283. #if !defined(CONFIG_PANEL_FULL_SCREEN_OPT_AREA) && HAS_SCREEN_AREAS
  284. /**
  285. * Get the square root of a number
  286. * @param x integer which square root should be calculated
  287. * @param mask optional to skip some iterations if the magnitude of the root is known.
  288. * Set to 0x8000 by default.
  289. * If root < 16: mask = 0x80
  290. * If root < 256: mask = 0x800
  291. * Else: mask = 0x8000
  292. */
  293. static uint32_t _sqrt_internal(uint32_t x, uint32_t mask)
  294. {
  295. x = x << 8; /*To get 4 bit precision. (sqrt(256) = 16 = 4 bit)*/
  296. uint32_t root = 0;
  297. uint32_t trial;
  298. // http://ww1.microchip.com/...en/AppNotes/91040a.pdf
  299. do {
  300. trial = root + mask;
  301. if (trial * trial <= x) root = trial;
  302. mask = mask >> 1;
  303. } while (mask);
  304. return root >> 4;
  305. }
  306. static void _compute_round_screen_areas(uint16_t screen_size, ui_region_t areas[], uint8_t n_areas)
  307. {
  308. const uint16_t align = (screen_size & 0x1) ? 1 : 2;
  309. uint16_t radius = (screen_size + 1) / 2;
  310. uint16_t x1 = (uint16_t)(radius * 0.29289f) & ~(align - 1);
  311. uint16_t y1 = x1;
  312. uint16_t x2 = 0;
  313. uint16_t y2 = 0;
  314. uint32_t area2 = 0;
  315. if (n_areas == 3) {
  316. areas[0] = (ui_region_t) { x1, 0, screen_size - x1 - 1, y1 - 1 };
  317. areas[1] = (ui_region_t) { 0, y1, screen_size - 1, screen_size - y1 - 1 };
  318. areas[2] = (ui_region_t) { x1, screen_size - y1, screen_size - x1 - 1, screen_size - 1 };
  319. } else {
  320. for (uint32_t y = align; y < y1; y += align) {
  321. uint16_t x = (radius - _sqrt_internal((2 * radius - y) * y, 0x800) - 1) & ~(align - 1);
  322. uint32_t area = (x - x1) * y;
  323. if (area > area2) {
  324. area2 = area;
  325. x2 = x;
  326. y2 = y;
  327. }
  328. }
  329. areas[0] = (ui_region_t) { x2, 0, screen_size - x2 - 1, y2 - 1 };
  330. areas[1] = (ui_region_t) { x1, y2, screen_size - x1 - 1, y1 - 1 };
  331. areas[2] = (ui_region_t) { y2, y1, screen_size - y2 - 1, x2 - 1 };
  332. areas[3] = (ui_region_t) { 0, y1 + x2 - x1, screen_size - - 1, screen_size - (y1 + x2 - x1) - 1 };
  333. areas[4] = (ui_region_t) { y2, screen_size - (y1 + x2 - x1), screen_size - y2 - 1, screen_size - y1 - 1 };
  334. areas[5] = (ui_region_t) { x1, screen_size - y1, screen_size - x1 - 1, screen_size - y2 - 1 };
  335. areas[6] = (ui_region_t) { x2, screen_size - y2, screen_size - x2 - 1, screen_size - 1 };
  336. }
  337. for (int i = 0; i < n_areas; i++) {
  338. #ifdef CONFIG_PANEL_OFFSET_X
  339. areas[i].x1 -= CONFIG_PANEL_OFFSET_X;
  340. areas[i].x2 -= CONFIG_PANEL_OFFSET_X;
  341. #endif
  342. #ifdef CONFIG_PANEL_OFFSET_Y
  343. areas[i].y1 -= CONFIG_PANEL_OFFSET_Y;
  344. areas[i].y2 -= CONFIG_PANEL_OFFSET_Y;
  345. #endif
  346. SYS_LOG_INF("screen_area[%d]: %d %d %d %d", i, areas[i].x1, areas[i].y1, areas[i].x2, areas[i].y2);
  347. }
  348. }
  349. #endif /* !defined(CONFIG_PANEL_FULL_SCREEN_OPT_AREA) && HAS_SCREEN_AREAS */
  350. static void _composer_display_vsync_handler(const struct display_callback *callback, uint32_t timestamp)
  351. {
  352. display_composer_t *composer = CONTAINER_OF(callback, display_composer_t, disp_cb);
  353. if (!composer->disp_has_vsync) {
  354. composer->disp_has_vsync = 1;
  355. LOG_INF("has vsync\n");
  356. }
  357. composer->vsync_counter++;
  358. if (++composer->post_frame_cnt >= composer->post_frame_period) {
  359. composer->post_frame_cnt = 0;
  360. if (_composer_has_gram(composer) && composer->post_cnt > 0 && !composer->post_inprog) {
  361. _composer_post_top_entry(composer, false);
  362. }
  363. }
  364. if (composer->user_cb && composer->user_cb->vsync) {
  365. composer->user_cb->vsync(composer->user_cb, timestamp);
  366. }
  367. #ifdef CONFIG_DISPLAY_COMPOSER_DEBUG_VSYNC
  368. if (timestamp - composer->vsync_print_timestamp >= sys_clock_hw_cycles_per_sec() * 8u) {
  369. LOG_INF("vsync %u us\n", k_cyc_to_us_near32(timestamp - composer->vsync_timestamp));
  370. composer->vsync_print_timestamp = timestamp;
  371. }
  372. composer->vsync_timestamp = timestamp;
  373. #endif
  374. }
  375. static void _composer_complete_one_entry(display_composer_t *composer)
  376. {
  377. post_entry_t *entry = &composer->post_entries[composer->cplt_idx];
  378. _composer_cleanup_entry(composer, entry);
  379. if (++composer->cplt_idx >= NUM_POST_ENTRIES) {
  380. composer->cplt_idx = 0;
  381. }
  382. --composer->post_cnt;
  383. }
  384. static void _composer_display_complete_handler(const struct display_callback *callback)
  385. {
  386. display_composer_t *composer = CONTAINER_OF(callback, display_composer_t, disp_cb);
  387. post_entry_t *entry = &composer->post_entries[composer->cplt_idx];
  388. composer->post_inprog = 0;
  389. if (entry->flags & LAST_POST_IN_FRAME) {
  390. if (composer->vsync_counter != composer->frame_start_vsync_cnt) {
  391. uint32_t frame_cycles = k_cycle_get_32() - composer->frame_start_cycle;
  392. SYS_LOG_WRN("frame refresh over vsync: %u us\n", k_cyc_to_us_floor32(frame_cycles));
  393. sys_trace_void(SYS_TRACE_ID_COMPOSER_OVERVSYNC);
  394. }
  395. }
  396. if (--composer->post_cnt > 0) {
  397. _composer_post_top_entry(composer, true);
  398. }
  399. _composer_cleanup_entry(composer, entry);
  400. if (++composer->cplt_idx >= NUM_POST_ENTRIES) {
  401. composer->cplt_idx = 0;
  402. }
  403. if (composer->post_cnt == 0 && composer->user_cb && composer->user_cb->complete) {
  404. composer->user_cb->complete(composer->user_cb);
  405. }
  406. }
  407. static void _composer_de_complete_handler(int status, uint16_t cmd_seq, void *user_data)
  408. {
  409. display_composer_t *composer = user_data;
  410. /* TODO: recovery the display */
  411. assert(status == 0);
  412. _composer_complete_one_entry(composer);
  413. if (composer->post_cnt == 0 && composer->user_cb && composer->user_cb->complete) {
  414. composer->user_cb->complete(composer->user_cb);
  415. }
  416. }
  417. static void _composer_drop_all_entries(display_composer_t *composer)
  418. {
  419. k_spinlock_key_t key = k_spin_lock(&composer->post_lock);
  420. while (composer->post_cnt > 0) {
  421. _composer_complete_one_entry(composer);
  422. }
  423. composer->post_inprog = 0;
  424. composer->post_idx = composer->free_idx;
  425. composer->cplt_idx =composer->free_idx;
  426. if (composer->user_cb && composer->user_cb->complete) {
  427. composer->user_cb->complete(composer->user_cb);
  428. }
  429. k_spin_unlock(&composer->post_lock, key);
  430. }
  431. static void _composer_display_pm_notify_handler(const struct display_callback *callback, uint32_t pm_action)
  432. {
  433. display_composer_t *composer = CONTAINER_OF(callback, display_composer_t, disp_cb);
  434. if (pm_action == PM_DEVICE_ACTION_EARLY_SUSPEND ||
  435. pm_action == PM_DEVICE_ACTION_LOW_POWER) {
  436. uint8_t wait_post_cnt = _composer_has_gram(composer) ? 0 : 1;
  437. int try_cnt = 500;
  438. composer->disp_active = 0; /* lock the post temporarily */
  439. if (composer->user_cb && composer->user_cb->pm_notify) {
  440. composer->user_cb->pm_notify(composer->user_cb, pm_action);
  441. }
  442. while (composer->post_cnt > wait_post_cnt && try_cnt-- > 0) {
  443. SYS_LOG_INF("post cnt %d", composer->post_cnt);
  444. os_sleep(2);
  445. }
  446. if (_composer_has_gram(composer) && composer->post_cnt > 0) {
  447. SYS_LOG_ERR("wait timeout, drop all post entries");
  448. _composer_drop_all_entries(composer);
  449. }
  450. composer->disp_active = (pm_action != PM_DEVICE_ACTION_EARLY_SUSPEND);
  451. composer->disp_lowpower = (pm_action != PM_DEVICE_ACTION_EARLY_SUSPEND);
  452. } else if (composer->disp_lowpower) {
  453. composer->disp_lowpower = 0;
  454. } else {
  455. composer->disp_active = 1;
  456. composer->first_frame = 1;
  457. if (composer->user_cb && composer->user_cb->pm_notify) {
  458. composer->user_cb->pm_notify(composer->user_cb, pm_action);
  459. }
  460. }
  461. }
  462. static uint8_t _composer_num_free_entries_get(display_composer_t *composer)
  463. {
  464. return NUM_POST_ENTRIES - composer->post_cnt;
  465. }
  466. static post_entry_t *_composer_find_free_entry(display_composer_t *composer)
  467. {
  468. post_entry_t *entry;
  469. #ifdef CONFIG_COMPOSER_POST_NO_WAIT
  470. if (composer->post_cnt >= NUM_POST_ENTRIES)
  471. return NULL;
  472. #else
  473. if (k_sem_take(&composer->post_sem, k_is_in_isr() ? K_NO_WAIT : K_FOREVER))
  474. return NULL;
  475. #endif
  476. entry = &composer->post_entries[composer->free_idx];
  477. if (++composer->free_idx >= NUM_POST_ENTRIES)
  478. composer->free_idx = 0;
  479. return entry;
  480. }
  481. static int _composer_post_top_entry(display_composer_t *composer, bool require_not_first)
  482. {
  483. post_entry_t *entry = &composer->post_entries[composer->post_idx];
  484. display_layer_t *ovls = entry->ovls;
  485. int res;
  486. if (require_not_first && (entry->flags & FIRST_POST_IN_FRAME)) {
  487. return -EINVAL;
  488. }
  489. composer->post_inprog = 1;
  490. if (entry->flags & FIRST_POST_IN_FRAME) {
  491. composer->frame_start_vsync_cnt = composer->vsync_counter;
  492. composer->frame_start_cycle = k_cycle_get_32();
  493. }
  494. if (entry->flags & POST_PATH_BY_DE) {
  495. res = display_engine_compose(composer->de_dev, composer->de_inst,
  496. NULL, ovls, entry->n_ovls);
  497. } else {
  498. res = display_write(composer->disp_dev, ovls[0].frame.x, ovls[0].frame.y,
  499. &ovls[0].buffer->desc, (void *)ovls[0].buffer->addr);
  500. }
  501. if (res < 0) {
  502. SYS_LOG_ERR("post failed\n");
  503. _composer_dump_entry(entry);
  504. assert(0);
  505. return res;
  506. }
  507. if (++composer->post_idx >= NUM_POST_ENTRIES)
  508. composer->post_idx = 0;
  509. return 0;
  510. }
  511. static int _composer_post_entry_noram(display_composer_t *composer)
  512. {
  513. post_entry_t *entry = &composer->post_entries[composer->post_idx];
  514. display_layer_t *ovls = entry->ovls;
  515. uint8_t num_layers = ovls[1].buffer ? 2 : 1;
  516. int res;
  517. res = display_engine_compose(composer->de_dev, composer->de_inst,
  518. NULL, ovls, num_layers);
  519. assert(res >= 0);
  520. if (++composer->post_idx >= NUM_POST_ENTRIES)
  521. composer->post_idx = 0;
  522. return res;
  523. }
  524. static void _composer_cleanup_entry(display_composer_t *composer, post_entry_t *entry)
  525. {
  526. for (int i = 0; i < ARRAY_SIZE(entry->graphic_bufs); i++) {
  527. if (entry->graphic_bufs[i]) {
  528. graphic_buffer_unref(entry->graphic_bufs[i]);
  529. }
  530. if (entry->cleanup_cb[i]) {
  531. entry->cleanup_cb[i](entry->cleanup_data[i]);
  532. }
  533. }
  534. memset(entry, 0, sizeof(*entry));
  535. #ifndef CONFIG_COMPOSER_POST_NO_WAIT
  536. k_sem_give(&composer->post_sem);
  537. #endif
  538. }
  539. static void _composer_dump_entry(post_entry_t *entry)
  540. {
  541. display_layer_t *ovls = entry->ovls;
  542. uint8_t num_layers = ovls[1].buffer ? 2 : 1;
  543. uint8_t i;
  544. for (i = 0; i < num_layers; i++) {
  545. SYS_LOG_INF("L-%d: ptr=0x%08x fmt=0x%02x stride=%u color=0x%08x blend=0x%x frame=(%d,%d-%d,%d)%s",
  546. i, entry->bufs[i].addr, entry->bufs[i].desc.pixel_format, entry->bufs[i].desc.pitch,
  547. ovls[i].color.full, ovls[i].blending, ovls[i].frame.x,
  548. ovls[i].frame.y, ovls[i].frame.w, ovls[i].frame.h,
  549. i == num_layers - 1 ? "\n" : "");
  550. }
  551. }
  552. static int _composer_post_inner(const ui_layer_t *layers, int num_layers, uint32_t post_flags)
  553. {
  554. display_composer_t *composer = _composer_get();
  555. post_entry_t *entry = NULL;
  556. int res = -EINVAL;
  557. int i;
  558. /* Get the free entry */
  559. entry = _composer_find_free_entry(composer);
  560. if (entry == NULL) {
  561. goto fail_cleanup_cb;
  562. }
  563. entry->flags = (uint16_t)post_flags;
  564. entry->n_ovls = (uint8_t)num_layers;
  565. /* Validate the buffer */
  566. for (i = 0; i < num_layers; i++) {
  567. entry->graphic_bufs[i] = layers[i].buffer;
  568. if (layers[i].buffer) {
  569. graphic_buffer_ref(layers[i].buffer);
  570. }
  571. if (layers[i].buffer) {
  572. entry->ovls[i].buffer = &entry->bufs[i];
  573. entry->bufs[i].addr = (uint32_t)graphic_buffer_get_bufptr(
  574. layers[i].buffer, layers[i].crop.x1, layers[i].crop.y1);
  575. entry->bufs[i].desc.pixel_format =
  576. graphic_buffer_get_pixel_format(layers[i].buffer);
  577. entry->bufs[i].desc.pitch = graphic_buffer_get_stride(layers[i].buffer) *
  578. display_format_get_bits_per_pixel(entry->bufs[i].desc.pixel_format) / 8;
  579. entry->bufs[i].desc.buf_size = entry->bufs[i].desc.pitch * entry->bufs[i].desc.height;
  580. } else {
  581. entry->ovls[i].buffer = NULL;
  582. entry->bufs[i].addr = 0;
  583. entry->bufs[i].desc.pitch = 0;
  584. entry->bufs[i].desc.pixel_format = 0;
  585. }
  586. entry->bufs[i].desc.width = ui_region_get_width(&layers[i].frame);
  587. entry->bufs[i].desc.height = ui_region_get_height(&layers[i].frame);
  588. entry->ovls[i].frame.x = layers[i].frame.x1;
  589. entry->ovls[i].frame.y = layers[i].frame.y1;
  590. entry->ovls[i].frame.w = entry->bufs[i].desc.width;
  591. entry->ovls[i].frame.h = entry->bufs[i].desc.height;
  592. entry->ovls[i].color.a = layers[i].plane_alpha;
  593. entry->ovls[i].blending = layers[i].blending;
  594. assert(layers[i].frame.x1 >= 0 && layers[i].frame.y1 >= 0);
  595. assert(entry->ovls[i].frame.w > 0 && layers[i].frame.x2 < composer->disp_cap.x_resolution);
  596. assert(entry->ovls[i].frame.h > 0 && layers[i].frame.y2 < composer->disp_cap.y_resolution);
  597. entry->cleanup_cb[i] = layers[i].cleanup_cb;
  598. entry->cleanup_data[i] = layers[i].cleanup_data;
  599. }
  600. k_spinlock_key_t key = k_spin_lock(&composer->post_lock);
  601. composer->post_cnt++;
  602. if (!_composer_has_gram(composer)) {
  603. _composer_post_entry_noram(composer);
  604. } else if (!composer->post_inprog) {
  605. _composer_post_top_entry(composer, composer->disp_lowpower ? false : true);
  606. }
  607. k_spin_unlock(&composer->post_lock, key);
  608. return 0;
  609. fail_cleanup_cb:
  610. for (i = 0; i < num_layers; i++) {
  611. if (layers[i].cleanup_cb)
  612. layers[i].cleanup_cb(layers[i].cleanup_data);
  613. }
  614. return res;
  615. }
  616. #if HAS_SCREEN_AREAS
  617. static int _composer_post_areas(const ui_layer_t *layers, int num_layers, uint32_t post_flags)
  618. {
  619. const uint32_t las_post_flag = post_flags & LAST_POST_IN_FRAME;
  620. ui_layer_t tmp_layers[NUM_POST_LAYERS];
  621. int8_t max_clip_idx[NUM_POST_LAYERS];
  622. int8_t max_post_idx = -1;
  623. int8_t i, j;
  624. for (j = num_layers - 1; j >= 0; j--) {
  625. max_clip_idx[j] = -1;
  626. for (i = NUM_SCREEN_AREAS - 1; i >= 0; i--) {
  627. if (ui_region_is_on(&layers[j].frame, &g_screen_areas[i])) {
  628. max_clip_idx[j] = i;
  629. break;
  630. }
  631. }
  632. if (max_clip_idx[j] < 0) {
  633. if (layers[j].cleanup_cb)
  634. layers[j].cleanup_cb(layers[j].cleanup_data);
  635. } else if (max_clip_idx[j] > max_post_idx) {
  636. max_post_idx = max_clip_idx[j];
  637. }
  638. }
  639. if (max_post_idx < 0)
  640. return -EINVAL;
  641. post_flags &= ~LAST_POST_IN_FRAME;
  642. for (i = 0; i <= max_post_idx; i++) {
  643. int8_t num = 0;
  644. for (j = 0; j < num_layers; j++) {
  645. if (max_clip_idx[j] < 0)
  646. continue;
  647. if (!ui_region_intersect(&tmp_layers[num].frame, &layers[j].frame, &g_screen_areas[i]))
  648. continue;
  649. if (i == max_clip_idx[j]) {
  650. tmp_layers[num].cleanup_cb = layers[j].cleanup_cb;
  651. tmp_layers[num].cleanup_data = layers[j].cleanup_data;
  652. } else {
  653. tmp_layers[num].cleanup_cb = NULL;
  654. }
  655. tmp_layers[num].blending = layers[j].blending;
  656. tmp_layers[num].plane_alpha = layers[j].plane_alpha;
  657. tmp_layers[num].buffer = layers[j].buffer;
  658. if (tmp_layers[num].buffer) {
  659. tmp_layers[num].crop.x1 = layers[j].crop.x1 +
  660. tmp_layers[num].frame.x1 - layers[j].frame.x1;
  661. tmp_layers[num].crop.y1 = layers[j].crop.y1 +
  662. tmp_layers[num].frame.y1 - layers[j].frame.y1;
  663. }
  664. num++;
  665. }
  666. if (num > 0) {
  667. if (i == max_post_idx)
  668. post_flags |= las_post_flag;
  669. _composer_post_inner(tmp_layers, num, post_flags);
  670. post_flags &= ~FIRST_POST_IN_FRAME;
  671. }
  672. }
  673. return 0;
  674. }
  675. #endif /* HAS_SCREEN_AREAS */
  676. int display_composer_post(const ui_layer_t *layers, int num_layers, uint32_t post_flags)
  677. {
  678. display_composer_t *composer = _composer_get();
  679. uint8_t num_free_entries;
  680. int res = 0, i;
  681. #ifdef CONFIG_DISPLAY_COMPOSER_DEBUG_FPS
  682. if (post_flags & LAST_POST_IN_FRAME) {
  683. uint32_t timestamp = k_cycle_get_32();
  684. ++composer->frame_cnt;
  685. if ((timestamp - composer->frame_timestamp) >= sys_clock_hw_cycles_per_sec()) {
  686. LOG_INF("post fps %u\n", composer->frame_cnt);
  687. composer->frame_cnt = 0;
  688. composer->frame_timestamp = timestamp;
  689. }
  690. }
  691. #endif
  692. if (composer->disp_dev == NULL) {
  693. SYS_LOG_ERR("composer not initialized");
  694. goto fail_cleanup_cb;
  695. }
  696. /* vsync-check is not required any longer due to panel ESD */
  697. if (/*composer->disp_has_vsync == 0 || */composer->disp_active == 0) {
  698. SYS_LOG_DBG("display active %d, has_vsync %d",
  699. composer->disp_active, composer->disp_has_vsync);
  700. goto fail_cleanup_cb;
  701. }
  702. if (num_layers <= 0 || num_layers > composer->max_layers) {
  703. SYS_LOG_ERR("invalid layer num %d", num_layers);
  704. goto fail_cleanup_cb;
  705. }
  706. if (num_layers > 1 || layers[0].buffer == NULL ||
  707. !(layers[0].buffer->pixel_format & composer->disp_cap.supported_pixel_formats)) {
  708. post_flags |= POST_PATH_BY_DE;
  709. if (composer->de_inst < 0) {
  710. SYS_LOG_ERR("DE path not unavailable");
  711. goto fail_cleanup_cb;
  712. }
  713. }
  714. num_free_entries = _composer_num_free_entries_get(composer);
  715. #ifdef CONFIG_COMPOSER_POST_NO_WAIT
  716. if (num_free_entries < NUM_SCREEN_AREAS)
  717. #else
  718. if (k_is_in_isr() && num_free_entries < NUM_SCREEN_AREAS)
  719. #endif
  720. {
  721. SYS_LOG_WRN("drop 1 frame (%d)", num_free_entries);
  722. sys_trace_void(SYS_TRACE_ID_COMPOSER_OVERFLOW);
  723. goto fail_cleanup_cb;
  724. }
  725. #if HAS_SCREEN_AREAS
  726. if (NUM_SCREEN_AREAS > 1 && !composer->first_frame) {
  727. res = _composer_post_areas(layers, num_layers, post_flags);
  728. } else {
  729. res = _composer_post_inner(layers, num_layers, post_flags);
  730. }
  731. #else
  732. res = _composer_post_inner(layers, num_layers, post_flags);
  733. #endif
  734. if (res == 0)
  735. composer->first_frame = 0;
  736. return res;
  737. fail_cleanup_cb:
  738. for (i = 0; i < num_layers; i++) {
  739. if (layers[i].cleanup_cb)
  740. layers[i].cleanup_cb(layers[i].cleanup_data);
  741. }
  742. return -EINVAL;
  743. }
  744. int display_composer_flush(unsigned int timeout)
  745. {
  746. display_composer_t *composer = _composer_get();
  747. int8_t wait_post_cnt = _composer_has_gram(composer) ? 0 : 1;
  748. int n_frames;
  749. uint32_t uptime;
  750. if (composer->disp_dev == NULL) {
  751. return -ENODEV;
  752. }
  753. n_frames = (int)composer->post_cnt - wait_post_cnt;
  754. if (n_frames <= 0) {
  755. return 0;
  756. }
  757. uptime = os_uptime_get_32();
  758. do {
  759. if (!_composer_has_gram(composer) || composer->post_inprog ||
  760. (composer->disp_active && !composer->disp_lowpower)) {
  761. k_msleep(1);
  762. } else {
  763. k_spinlock_key_t key = k_spin_lock(&composer->post_lock);
  764. if (!composer->post_inprog)
  765. composer->disp_cb.vsync(&composer->disp_cb, os_cycle_get_32());
  766. k_spin_unlock(&composer->post_lock, key);
  767. }
  768. if (composer->post_cnt <= wait_post_cnt) {
  769. return n_frames;
  770. }
  771. } while (composer->post_inprog || (os_uptime_get_32() - uptime < timeout));
  772. return -ETIME;
  773. }