vglite_util.h 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298
  1. /*
  2. * Copyright (c) 2020 Actions Technology Co., Ltd
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. */
  6. #ifndef VG_LITE_VGLITE_UTIL_H_
  7. #define VG_LITE_VGLITE_UTIL_H_
  8. #ifdef CONFIG_VG_LITE
  9. /*********************
  10. * INCLUDES
  11. *********************/
  12. #include <stdbool.h>
  13. #include <display/graphic_buffer.h>
  14. #include "vg_lite.h"
  15. /**********************
  16. * TYPEDEFS
  17. **********************/
  18. #define RAD(d) ((float)(d) * 3.141592654/180.0)
  19. #define RAD10(d) ((float)(d) * 3.141592654/1800.0)
  20. /**********************
  21. * TYPEDEFS
  22. **********************/
  23. /*
  24. * Euler angle rotation order around model's dynamic axis
  25. */
  26. typedef enum rotate_order {
  27. ROT_ZYX = 0,
  28. ROT_ZXY,
  29. ROT_YZX,
  30. ROT_YXZ,
  31. ROT_XYZ,
  32. ROT_XZY,
  33. NUM_ROT_ORDERS,
  34. } rotate_order_e;
  35. typedef struct vertex_rec {
  36. float x;
  37. float y;
  38. float z;
  39. } vertex_t;
  40. typedef struct normal_rec {
  41. float x;
  42. float y;
  43. float z;
  44. } normal_t;
  45. /**********************
  46. * GLOBAL PROTOTYPES
  47. **********************/
  48. /**********************
  49. * Buffer functions
  50. **********************/
  51. /**
  52. * @brief Convert display hal pixel format to vglite buffer format
  53. *
  54. * @param hal_format display hal pixel format
  55. * @param bits_per_pixel optional address to store the bits_per_pixel of format.
  56. *
  57. * @retval vglite buffer format
  58. */
  59. int vglite_buf_format_from_hal(uint32_t hal_format, uint8_t *bits_per_pixel);
  60. /**
  61. * @brief Convert vglite buffer format to display hal pixel format
  62. *
  63. * @param format vglite buffer cf.
  64. * @param bits_per_pixel optional address to store the bits_per_pixel of format.
  65. *
  66. * @retval display hal pixel format
  67. */
  68. uint32_t vglite_buf_format_to_hal(vg_lite_buffer_format_t format, uint8_t *bits_per_pixel);
  69. /*
  70. * Query vglite buffer is valid or not
  71. *
  72. * @param vgbuf pointer to structure vg_lite_buffer
  73. *
  74. * @retval query result
  75. */
  76. bool vglite_buf_is_valid(vg_lite_buffer_t *vgbuf);
  77. /*
  78. * Map a vglite buffer
  79. *
  80. * @param vgbuf pointer to structure vg_lite_buffer
  81. * @param data buffer address to map
  82. * @param width width of buffer in pixels
  83. * @param height height of buffer in pixels
  84. * @param stride number of bytes to move from one line in the buffer to the next line
  85. * @param format buffer format
  86. *
  87. * @retval 0 on success else negative code
  88. */
  89. int vglite_buf_map(vg_lite_buffer_t *vgbuf, void *data, uint16_t width, uint16_t height,
  90. uint16_t stride, vg_lite_buffer_format_t format);
  91. /*
  92. * Map a vglite buffer from graphic buffer
  93. *
  94. * @param vgbuf pointer to structure vg_lite_buffer
  95. * @param gbuf pointer to structure graphic_buffer_t
  96. *
  97. * @retval 0 on success else negative code
  98. */
  99. int vglite_buf_map_graphic(vg_lite_buffer_t *vgbuf, const graphic_buffer_t *gbuf);
  100. /*
  101. * Unmap a vglite buffer
  102. *
  103. * @param vgbuf pointer to structure vg_lite_buffer
  104. *
  105. * @retval 0 on success else negative code
  106. */
  107. int vglite_buf_unmap(vg_lite_buffer_t *vgbuf);
  108. /**********************
  109. * Matrix functions
  110. **********************/
  111. /*
  112. * Compute rotate matrix around x-axis to the world coordinate
  113. *
  114. * @param rx Euler angle in radian around X axis
  115. * @param matrix store the rotate matrix result
  116. *
  117. * @retval N/A
  118. */
  119. void matrix_rotate_x(float rx, vg_lite_matrix_t *matrix);
  120. /*
  121. * Compute rotate matrix around x-axis to the world coordinate
  122. *
  123. * @param ry Euler angle in radian around Y axis
  124. * @param matrix store the rotate matrix result
  125. *
  126. * @retval N/A
  127. */
  128. void matrix_rotate_y(float ry, vg_lite_matrix_t *matrix);
  129. /*
  130. * Compute rotate matrix around x-axis to the world coordinate
  131. *
  132. * @param rz Euler angle in radian around Z axis
  133. * @param matrix store the rotate matrix result
  134. *
  135. * @retval N/A
  136. */
  137. void matrix_rotate_z(float rz, vg_lite_matrix_t *matrix);
  138. /*
  139. * Compute rotate matrix to the world coordinate
  140. *
  141. * The rotation from model coordinate to world coordinate around model's dynamic axis.
  142. *
  143. * @param rx Euler angle in radian around X axis
  144. * @param ry Euler angle in radian around Y axis
  145. * @param rz Euler angle in radian around Z axis
  146. * @param order Rotation order, see enum rotate_order
  147. * @param matrix store the rotate matrix result
  148. *
  149. * @retval N/A
  150. */
  151. void matrix_rotate(float rx, float ry, float rz, uint8_t order, vg_lite_matrix_t *matrix);
  152. /*
  153. * Compute matrix multiplication
  154. *
  155. * @param matrix_left pointer to the left multiplication matrix
  156. * @param matrix_right pointer to the right multiplication matrix
  157. * @param result store the multiplication result
  158. *
  159. * @retval N/A
  160. */
  161. void matrix_multiply(const vg_lite_matrix_t *matrix_left, const vg_lite_matrix_t *matrix_right, vg_lite_matrix_t *result);
  162. /*
  163. * Compute left multiply translate matrix
  164. *
  165. * @param tx X translate
  166. * @param ty Y translate
  167. * @param matrix_right right multiplied matrix
  168. * @param matrix store the matrix result
  169. *
  170. * @retval N/A
  171. */
  172. void matrix_translate_left(float tx, float ty,
  173. const vg_lite_matrix_t *matrix_right, vg_lite_matrix_t *matrix);
  174. /*
  175. * Compute affine transformation matrix for blit
  176. *
  177. * @param w image width
  178. * @param h image height
  179. * @param v0 1st vertex in counterclock wise
  180. * @param v1 2nd vertex in counterclock wise
  181. * @param v2 3rd vertex in counterclock wise
  182. * @param v3 4th vertex in counterclock wise
  183. * @param matrix store the result matrix
  184. *
  185. * @retval N/A
  186. */
  187. void matrix_affine_blit(int w, int h, vertex_t *v0, vertex_t *v1, vertex_t *v2, vertex_t *v3, vg_lite_matrix_t *matrix);
  188. /*
  189. * Compute perspective transformation matrix for blit
  190. *
  191. * @param w image width
  192. * @param h image height
  193. * @param v0 1st vertex in counterclock wise
  194. * @param v1 2nd vertex in counterclock wise
  195. * @param v2 3rd vertex in counterclock wise
  196. * @param v3 4th vertex in counterclock wise
  197. * @param matrix store the result matrix
  198. *
  199. * @retval N/A
  200. */
  201. void matrix_perspective_blit(int w, int h, vertex_t *v0, vertex_t *v1, vertex_t *v2, vertex_t *v3, vg_lite_matrix_t *matrix);
  202. /*
  203. * Compute full transformation matrix for blit
  204. *
  205. * @param rect image bounding rectangle
  206. * @param pivots image pivot coordinate in pixels relative to top-left corner of the image
  207. * @param angles image Euler angles around X/Y/Z axis respectively in radian in ZYX order
  208. * @param scale scaling factor
  209. * @param camera camera position in display coordinates, [0] x; [1] y; [2] distance (>= 0)
  210. * @param result store the result vertices
  211. * @param matrix store the result matrix
  212. *
  213. * @retval N/A
  214. */
  215. void matrix_transform_rect(vg_lite_rectangle_t *rect,
  216. float pivots[3], float angles[3], float scale, float camera[3],
  217. vertex_t result[4], vg_lite_matrix_t *matrix);
  218. /*
  219. * Compute full transformation matrix of given rotate order for blit
  220. *
  221. * @param rect image bounding rectangle
  222. * @param pivots image pivot coordinate in pixels relative to top-left corner of the image
  223. * @param angles image Euler angles around X/Y/Z axis respectively in radian
  224. * @param rotate_order axis rotation order, see @rotate_order_e
  225. * @param scale scaling factor
  226. * @param camera camera position in display coordinates, [0] x; [1] y; [2] distance (>= 0)
  227. * @param result store the result vertices
  228. * @param matrix store the result matrix
  229. *
  230. * @retval N/A
  231. */
  232. void matrix_transform_rect2(vg_lite_rectangle_t *rect,
  233. float pivots[3], float angles[3], uint8_t rotate_order,
  234. float scale, float camera[3],
  235. vertex_t result[4], vg_lite_matrix_t *matrix);
  236. /*
  237. * Compute rotated vertex
  238. *
  239. * @param result store the rotated vertex result
  240. * @param rotate rotate matrix
  241. * @param vertex the vertex to rotate
  242. * @param scale scale factor
  243. * @param translate translate factor
  244. *
  245. * @retval N/A
  246. */
  247. void transfrom_rotate(vertex_t *result, vg_lite_matrix_t *rotate, const vertex_t *vertex, const vertex_t *scale, const vertex_t *translate);
  248. /*
  249. * Compute perspective vertex
  250. *
  251. * @param vertex vertex to do perspective transformation
  252. * @param camera_x camera x coordinate
  253. * @param camera_y camera y coordinate
  254. * @param camera_distance camera distance
  255. *
  256. * @retval N/A
  257. */
  258. void transfrom_perspective(vertex_t *vertex, float camera_x, float camera_y, float camera_distance);
  259. /*
  260. * Compute rotated normal value
  261. *
  262. * @param rotate rotate matrix
  263. * @param nVec normal
  264. *
  265. * @retval the result
  266. */
  267. float transfrom_normal_z(vg_lite_matrix_t *rotate, const normal_t *normal);
  268. #endif /* CONFIG_VG_LITE */
  269. #endif /* VG_LITE_VGLITE_UTIL_H_ */