graphic_buffer.h 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264
  1. /*
  2. * Copyright (c) 2020 Actions Technology Co., Ltd
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. */
  6. /**
  7. * @file
  8. * @brief graphic buffer interface
  9. */
  10. #ifndef FRAMEWORK_PORTING_INCLUDE_DISPLAY_GRAPHIC_BUFFER_H_
  11. #define FRAMEWORK_PORTING_INCLUDE_DISPLAY_GRAPHIC_BUFFER_H_
  12. /**
  13. * @defgroup graphic_buffer_apis Graphic Buffer APIs
  14. * @ingroup system_apis
  15. * @{
  16. */
  17. #include <os_common_api.h>
  18. #include <sys/atomic.h>
  19. #include <sys/util.h>
  20. #include <display/display_hal.h>
  21. #include "ui_region.h"
  22. #ifdef __cplusplus
  23. extern "C" {
  24. #endif
  25. /**
  26. * @enum graphic_buffer_usage
  27. * @brief Enumeration with possible graphic buffer usage
  28. *
  29. */
  30. enum graphic_buffer_usage {
  31. GRAPHIC_BUFFER_SW_READ = BIT(0),
  32. GRAPHIC_BUFFER_SW_WRITE = BIT(1),
  33. GRAPHIC_BUFFER_SW_MASK = GRAPHIC_BUFFER_SW_READ | GRAPHIC_BUFFER_SW_WRITE,
  34. GRAPHIC_BUFFER_HW_TEXTURE = BIT(2),
  35. GRAPHIC_BUFFER_HW_RENDER = BIT(3),
  36. GRAPHIC_BUFFER_HW_COMPOSER = BIT(4),
  37. GRAPHIC_BUFFER_HW_MASK = GRAPHIC_BUFFER_HW_TEXTURE | GRAPHIC_BUFFER_HW_RENDER | GRAPHIC_BUFFER_HW_COMPOSER,
  38. GRAPHIC_BUFFER_READ_MASK = GRAPHIC_BUFFER_SW_READ | GRAPHIC_BUFFER_HW_TEXTURE | GRAPHIC_BUFFER_HW_COMPOSER,
  39. GRAPHIC_BUFFER_WRITE_MASK = GRAPHIC_BUFFER_SW_WRITE | GRAPHIC_BUFFER_HW_RENDER,
  40. };
  41. /**
  42. * @struct graphic_buffer
  43. * @brief Structure holding graphic buffer
  44. *
  45. */
  46. typedef struct graphic_buffer {
  47. uint8_t *data; /* data address */
  48. uint32_t pixel_format; /* pixel format */
  49. uint16_t width; /* width in pixels */
  50. uint16_t height; /* height in pixels */
  51. uint16_t stride; /* number of pixels per row */
  52. uint8_t bits_per_pixel; /* bits per pixel */
  53. uint8_t usage; /* usage */
  54. uint8_t lock_usage; /* lock usage */
  55. ui_region_t lock_wrect; /* lock write area */
  56. atomic_t refcount; /* reference count */
  57. void (*destroy_cb)(struct graphic_buffer *buffer);
  58. } graphic_buffer_t;
  59. /**
  60. * @brief Initialize a graphic buffer
  61. *
  62. * After initialization, the refcount will be 1.
  63. * And destroy_cb can be set to free buffer structure and buffer data when
  64. * buffer destroyed by graphic_buffer_set_destroy_cb().
  65. *
  66. * @param buffer pointer to graphic_buffer structure
  67. * @param w width in pixels
  68. * @param h height in pixels
  69. * @param pixel_format display pixel format, see enum display_pixel_format
  70. * @param buffer usage
  71. * @param stride number of pixels per row
  72. * @param data pre-allocated data address
  73. *
  74. * @retval 0 on success else negative errno code.
  75. */
  76. int graphic_buffer_init(graphic_buffer_t *buffer, uint16_t w, uint16_t h,
  77. uint32_t pixel_format, uint32_t usage, uint16_t stride, void *data);
  78. /**
  79. * @brief Helper function to set destroy_cb() for a graphic buffer
  80. *
  81. * destroy_cb() will be invoked when buffer is destroyed (refcount becomes 0),
  82. * where user can free the buffer structure and buffer data if necessary.
  83. *
  84. * Must be invoked after graphic_buffer_init()
  85. *
  86. * @param destroy_cb pointer to graphic_buffer structure
  87. *
  88. * @retval N/A
  89. */
  90. static inline void graphic_buffer_set_destroy_cb(graphic_buffer_t *buffer,
  91. void (*destroy_cb)(struct graphic_buffer *))
  92. {
  93. buffer->destroy_cb = destroy_cb;
  94. }
  95. /**
  96. * @brief Get width of graphic buffer
  97. *
  98. * @param buffer pointer to graphic buffer structure
  99. *
  100. * @return width in pixels.
  101. */
  102. static inline uint16_t graphic_buffer_get_width(const graphic_buffer_t *buffer)
  103. {
  104. return buffer->width;
  105. }
  106. /**
  107. * @brief Get height of graphic buffer
  108. *
  109. * @param buffer pointer to graphic buffer structure
  110. *
  111. * @return height in pixels.
  112. */
  113. static inline uint16_t graphic_buffer_get_height(const graphic_buffer_t *buffer)
  114. {
  115. return buffer->height;
  116. }
  117. /**
  118. * @brief Get stride of graphic buffer
  119. *
  120. * @param buffer pointer to graphic buffer structure
  121. *
  122. * @return stride in pixels.
  123. */
  124. static inline uint16_t graphic_buffer_get_stride(const graphic_buffer_t *buffer)
  125. {
  126. return buffer->stride;
  127. }
  128. /**
  129. * @brief Get bits per pixel of graphic buffer
  130. *
  131. * @param buffer pointer to graphic buffer structure
  132. *
  133. * @return bits per pixel.
  134. */
  135. static inline uint16_t graphic_buffer_get_bits_per_pixel(const graphic_buffer_t *buffer)
  136. {
  137. return buffer->bits_per_pixel;
  138. }
  139. /**
  140. * @brief Get bytes per line of graphic buffer
  141. *
  142. * @param buffer pointer to graphic buffer structure
  143. *
  144. * @return bytes per line.
  145. */
  146. static inline uint16_t graphic_buffer_get_bytes_per_line(const graphic_buffer_t *buffer)
  147. {
  148. return buffer->stride * buffer->bits_per_pixel / 8;
  149. }
  150. /**
  151. * @brief Get pixel format of graphic buffer
  152. *
  153. * @param buffer pointer to graphic buffer structure
  154. *
  155. * @return pixel format.
  156. */
  157. static inline uint32_t graphic_buffer_get_pixel_format(const graphic_buffer_t *buffer)
  158. {
  159. return buffer->pixel_format;
  160. }
  161. /**
  162. * @brief Increase reference of graphic buffer
  163. *
  164. * @param buffer pointer to graphic buffer structure
  165. *
  166. * @retval 0 on success else negative errno code.
  167. */
  168. static inline int graphic_buffer_get_refcnt(const graphic_buffer_t *buffer)
  169. {
  170. return atomic_get(&buffer->refcount);
  171. }
  172. /**
  173. * @brief Increase reference of graphic buffer
  174. *
  175. * @param buffer pointer to graphic buffer structure
  176. *
  177. * @retval ref count before the call.
  178. */
  179. int graphic_buffer_ref(graphic_buffer_t *buffer);
  180. /**
  181. * @brief Decrease reference of graphic buffer
  182. *
  183. * @param buffer pointer to graphic buffer structure
  184. *
  185. * @retval ref count before the call.
  186. */
  187. int graphic_buffer_unref(graphic_buffer_t *buffer);
  188. /**
  189. * @brief Lock graphic buffer
  190. *
  191. * @param buffer pointer to graphic buffer structure
  192. * @param usage usage, see enum graphic_buffer_usage
  193. * @param rect the locked area
  194. * @param vaddr store the address of locked area
  195. *
  196. * @return 0 on success else negative errno code.
  197. */
  198. int graphic_buffer_lock(graphic_buffer_t *buffer, uint32_t usage,
  199. const ui_region_t *rect, void **vaddr);
  200. /**
  201. * @brief Unlock graphic buffer
  202. *
  203. * @param buffer pointer to graphic buffer structure
  204. *
  205. * @retval 0 on success else negative errno code.
  206. */
  207. int graphic_buffer_unlock(graphic_buffer_t *buffer);
  208. /**
  209. * @brief Get graphic buffer native buffer ptr at pixel (x, y)
  210. *
  211. * This routine don't increases the ref of buffer, and do any cache ops. It mainly
  212. * cooperates with graphic_buffer_ref(), like for display composer.
  213. *
  214. * @param buffer pointer to graphic buffer structure
  215. * @param x x-coord of pixel
  216. * @param y y-coord of pixel
  217. *
  218. * @return native buffer ptr.
  219. */
  220. const void *graphic_buffer_get_bufptr(graphic_buffer_t *buffer, int16_t x, int16_t y);
  221. /**
  222. * @brief Destroy graphic buffer
  223. *
  224. * @param buffer pointer to graphic buffer structure
  225. *
  226. * @retval N/A.
  227. */
  228. void graphic_buffer_destroy(graphic_buffer_t *buffer);
  229. #ifdef __cplusplus
  230. }
  231. #endif
  232. /**
  233. * @}
  234. */
  235. #endif /* FRAMEWORK_PORTING_INCLUDE_DISPLAY_GRAPHIC_BUFFER_H_ */