vg_lite_context.h 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307
  1. /****************************************************************************
  2. *
  3. * Copyright 2012 - 2023 Vivante Corporation, Santa Clara, California.
  4. * All Rights Reserved.
  5. *
  6. * Permission is hereby granted, free of charge, to any person obtaining
  7. * a copy of this software and associated documentation files (the
  8. * 'Software'), to deal in the Software without restriction, including
  9. * without limitation the rights to use, copy, modify, merge, publish,
  10. * distribute, sub license, and/or sell copies of the Software, and to
  11. * permit persons to whom the Software is furnished to do so, subject
  12. * to the following conditions:
  13. *
  14. * The above copyright notice and this permission notice (including the
  15. * next paragraph) shall be included in all copies or substantial
  16. * portions of the Software.
  17. *
  18. * THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND,
  19. * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  20. * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.
  21. * IN NO EVENT SHALL VIVANTE AND/OR ITS SUPPLIERS BE LIABLE FOR ANY
  22. * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
  23. * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
  24. * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  25. *
  26. *****************************************************************************/
  27. #include <stdio.h>
  28. #include <stdlib.h>
  29. #include <string.h>
  30. #include <stdint.h>
  31. #include <stddef.h>
  32. #include <math.h>
  33. #include "vg_lite.h"
  34. #include "vg_lite_kernel.h"
  35. #include "vg_lite_options.h"
  36. #define DUMP_CAPTURE 0
  37. #define DUMP_COMMAND_CAPTURE 0
  38. #define DUMP_INIT_COMMAND 0
  39. #define DUMP_API 0
  40. #define DUMP_LAST_CAPTURE 0
  41. #if DUMP_API
  42. #include "dumpAPI.h"
  43. #endif
  44. #define VGLITE_LOG printf
  45. /*** Global Context Access ***/
  46. #define GET_CONTEXT() &s_context
  47. /*** Default command buffer size is 32KB. Double command buffer is used.
  48. App can call vg_lite_set_command_buffer_size(size) before vg_lite_init()
  49. to overwrite the default command buffer size.
  50. ***/
  51. #define VG_LITE_COMMAND_BUFFER_SIZE (32 << 10)
  52. #define VG_LITE_SINGLE_COMMAND_BUFFER_SIZE (64 << 10) /* For only using one command buffer. */
  53. #define CMDBUF_BUFFER(context) (context).command_buffer[(context).command_buffer_current]
  54. #define CMDBUF_INDEX(context) (context).command_buffer_current
  55. #define CMDBUF_SIZE(context) (context).command_buffer_size
  56. #define CMDBUF_OFFSET(context) (context).command_offset[(context).command_buffer_current]
  57. #define CMDBUF_SWAP(context) (context).command_buffer_current = \
  58. ((context).command_buffer_current + 1) % CMDBUF_COUNT
  59. /*** Command macros ***/
  60. #define VG_LITE_END(interrupt) (0x00000000 | interrupt)
  61. #define VG_LITE_SEMAPHORE(id) (0x10000000 | id)
  62. #define VG_LITE_STALL(id) (0x20000000 | id)
  63. #define VG_LITE_STATE(address) (0x30010000 | address)
  64. #define VG_LITE_STATES(count, address) (0x30000000 | ((count) << 16) | address)
  65. #define VG_LITE_DATA(count) (0x40000000 | count)
  66. #define VG_LITE_CALL(count) (0x60000000 | count)
  67. #define VG_LITE_RETURN() (0x70000000)
  68. #define VG_LITE_NOP() (0x80000000)
  69. #define FC_BURST_BYTES 64
  70. #define FC_BIT_TO_BYTES 64
  71. #define STATES_COUNT 208
  72. #define MIN_TS_SIZE 8 << 10
  73. #define VG_LITE_RETURN_ERROR(func) \
  74. if ((error = func) != VG_LITE_SUCCESS) \
  75. return error
  76. #define VG_LITE_BREAK_ERROR(func) \
  77. if ((error = func) != VG_LITE_SUCCESS) \
  78. break
  79. #define VG_LITE_ERROR_HANDLER(func) \
  80. if ((error = func) != VG_LITE_SUCCESS) \
  81. goto ErrorHandler
  82. /*** Shortcuts. ***/
  83. #define A(color) (color) >> 24
  84. #define R(color) ((color) & 0x00ff0000) >> 16
  85. #define G(color) ((color) & 0x0000ff00) >> 8
  86. #define B(color) ((color) & 0xff)
  87. #define ARGB(a, r, g, b) ((a) << 24) | ((r) << 16) | ((g) << 8 ) | (b)
  88. #define ARGB4(a, r, g, b) (((a) & 0xf0) << 8) | (((r) & 0xf0) << 4) | (((g) & 0xf0)) | ((b) >> 4)
  89. #define MIN(a, b) (a) > (b) ? (b) : (a)
  90. #define MAX(a, b) (a) > (b) ? (a) : (b)
  91. #define LERP(v1, v2, w) ((v1) * (w) + (v2) * (1.0f - (w)))
  92. #define CLAMP(x, min, max) (((x) < (min)) ? (min) : ((x) > (max)) ? (max) : (x))
  93. #define COLOR_FROM_RAMP(ColorRamp) (((vg_lite_float_t *) ColorRamp) + 1)
  94. #define MATRIX_ROWS 3
  95. #define GET_MATRIX_VALUES(Pointer) ((float *) (Pointer))
  96. #define MAT(Matrix, Row, Column) (GET_MATRIX_VALUES(Matrix)[Row * MATRIX_ROWS + Column])
  97. #define PI 3.141592653589793238462643383279502f
  98. #if !gcFEATURE_VG_MATH_PRECISION_FIX && (CHIPID == 0x555)
  99. #define VG_SW_BLIT_PRECISION_OPT 1
  100. #else
  101. #define VG_SW_BLIT_PRECISION_OPT 0
  102. #endif
  103. /* Driver implementation internal structures.
  104. */
  105. typedef struct vg_lite_states {
  106. uint32_t state;
  107. uint8_t init;
  108. } vg_lite_states_t;
  109. typedef struct vg_lite_hardware {
  110. vg_lite_states_t hw_states[STATES_COUNT];
  111. } vg_lite_hardware_t;
  112. /* Tessellation buffer information. */
  113. typedef struct vg_lite_tess_buffer
  114. {
  115. vg_lite_uint32_t physical_addr; /*! Physical address for tessellation buffer. */
  116. vg_lite_uint8_t *logical_addr; /*! Logical address for tessellation buffer. */
  117. vg_lite_uint32_t tessbuf_size; /*! Buffer size for tessellation buffer */
  118. vg_lite_uint32_t countbuf_size; /*! Buffer size for VG count buffer */
  119. vg_lite_uint32_t tess_w_h; /*! Combination of buffer width and height. */
  120. /* gc355 Specific fields below */
  121. vg_lite_uint32_t L1_phyaddr; /*! L1 physical address. */
  122. vg_lite_uint32_t L2_phyaddr; /*! L2 physical address. */
  123. vg_lite_uint8_t *L1_logical; /*! L1 Logical address. */
  124. vg_lite_uint8_t *L2_logical; /*! L2 Logical address. */
  125. vg_lite_uint32_t L1_size; /*! L1 size for tessellation buffer */
  126. vg_lite_uint32_t L2_size; /*! L2 size for tessellation buffer */
  127. vg_lite_uint32_t tess_stride; /*! Stride for tessellation buffer */
  128. } vg_lite_tess_buffer_t;
  129. typedef struct vg_lite_context {
  130. vg_lite_kernel_context_t context;
  131. vg_lite_hardware_t hw;
  132. vg_lite_capabilities_t capabilities;
  133. uint8_t *command_buffer[CMDBUF_COUNT];
  134. uint32_t command_buffer_size;
  135. uint32_t command_offset[CMDBUF_COUNT];
  136. uint32_t command_buffer_current;
  137. vg_lite_memory_pool_t command_buffer_pool;
  138. vg_lite_tess_buffer_t tessbuf;
  139. vg_lite_memory_pool_t tess_buffer_pool;
  140. vg_lite_buffer_t *rtbuffer; /* DDRLess: this is used as composing buffer. */
  141. vg_lite_memory_pool_t render_buffer_pool;
  142. vg_lite_float_t path_lastX;
  143. vg_lite_float_t path_lastY;
  144. uint32_t scissor_set;
  145. uint32_t scissor_enable;
  146. uint32_t scissor_dirty;
  147. int32_t scissor[4]; /* Scissor area: x, y, right, bottom. */
  148. vg_lite_buffer_t *scissor_layer;
  149. uint32_t src_alpha_mode;
  150. uint32_t src_alpha_value;
  151. uint32_t dst_alpha_mode;
  152. uint32_t dst_alpha_value;
  153. vg_lite_blend_t blend_mode;
  154. uint32_t sbi_mode;
  155. uint32_t sync_mode;
  156. uint32_t flexa_mode;
  157. uint32_t stream_id;
  158. uint32_t segment_address;
  159. uint32_t segment_count;
  160. uint32_t segment_size;
  161. uint32_t stop_flag;
  162. uint8_t flexa_dirty;
  163. uint32_t start_flag;
  164. uint32_t reset_flag;
  165. uint8_t custom_cmdbuf;
  166. uint8_t custom_tessbuf;
  167. uint32_t enable_mask;
  168. uint32_t matrix_enable;
  169. uint32_t tess_width;
  170. uint32_t tess_height;
  171. uint32_t target_width;
  172. uint32_t target_height;
  173. uint8_t enable_scissor;
  174. uint32_t mirror_orient;
  175. uint32_t mirror_dirty;
  176. uint32_t gamma_value;
  177. uint32_t gamma_dirty;
  178. uint32_t gamma_src;
  179. uint32_t gamma_dst;
  180. uint32_t gamma_stencil;
  181. uint32_t color_transform;
  182. uint32_t path_counter;
  183. vg_lite_filter_t filter;
  184. void* last_command_buffer_logical;
  185. size_t Physical;
  186. uint32_t last_command_size;
  187. vg_lite_frame_flag_t frame_flag;
  188. } vg_lite_context_t;
  189. typedef struct vg_lite_ftable {
  190. uint32_t ftable[gcFEATURE_COUNT];
  191. } vg_lite_ftable_t;
  192. extern vg_lite_context_t s_context;
  193. extern vg_lite_ftable_t s_ftable;
  194. extern vg_lite_error_t set_render_target(vg_lite_buffer_t* target);
  195. extern vg_lite_error_t push_state(vg_lite_context_t* context, uint32_t address, uint32_t data);
  196. extern vg_lite_error_t push_state_ptr(vg_lite_context_t* context, uint32_t address, void* data_ptr);
  197. extern vg_lite_error_t push_call(vg_lite_context_t* context, uint32_t address, uint32_t bytes);
  198. extern vg_lite_error_t push_data(vg_lite_context_t* context, uint32_t size, void* data);
  199. extern vg_lite_error_t push_clut(vg_lite_context_t* context, uint32_t address, uint32_t count, uint32_t* data);
  200. extern vg_lite_error_t push_stall(vg_lite_context_t* context, uint32_t module);
  201. extern void* vg_lite_os_malloc(size_t size);
  202. extern void vg_lite_os_free(void* memory);
  203. extern vg_lite_void set_gamma_dest_only(vg_lite_buffer_t *target, vg_lite_int32_t stencil);
  204. extern vg_lite_void save_st_gamma_src_dest(vg_lite_buffer_t* source, vg_lite_buffer_t* target);
  205. extern vg_lite_void get_st_gamma_src_dest(vg_lite_buffer_t* source, vg_lite_buffer_t* target);
  206. extern vg_lite_void setup_lvgl_image(vg_lite_buffer_t* dst, vg_lite_buffer_t* src, vg_lite_buffer_t* temp, vg_lite_blend_t operation);
  207. #if defined(__ZEPHYR__)
  208. extern void * vg_lite_os_fopen(const char *__restrict path, const char *__restrict mode);
  209. extern int vg_lite_os_fclose(void * fp);
  210. extern size_t vg_lite_os_fread(void *__restrict ptr, size_t size, size_t nmemb, void *__restrict fp);
  211. extern size_t vg_lite_os_fwrite(const void *__restrict ptr, size_t size, size_t nmemb, void * fp);
  212. extern int vg_lite_os_fseek(void * fp, long offset, int whence);
  213. extern int vg_lite_os_fflush(void *fp);
  214. extern int vg_lite_os_fprintf(void *__restrict fp, const char *__restrict format, ...);
  215. extern int vg_lite_os_getpid(void);
  216. #else
  217. extern int vg_lite_os_fseek(FILE* Stream, long Offset, int Origin);
  218. extern FILE* vg_lite_os_fopen(char const* FileName, char const* Mode);
  219. extern long vg_lite_os_ftell(FILE* Stream);
  220. extern size_t vg_lite_os_fread(void* Buffer, size_t ElementSize, size_t ElementCount, FILE* Stream);
  221. extern size_t vg_lite_os_fwrite(void const* Buffer, size_t ElementSize, size_t ElementCount, FILE* Stream);
  222. extern int vg_lite_os_close(FILE* Stream);
  223. extern int vg_lite_os_fflush(FILE* fp);
  224. #endif
  225. /**************************** Dump command, image ********************************************/
  226. #define DUMP_COMMAND 0
  227. #define DUMP_IMAGE 0
  228. /* Enable FC buffer dump if SOC supports fast clear */
  229. #define VG_TARGET_FC_DUMP 0
  230. #if DUMP_COMMAND || DUMP_IMAGE
  231. #ifdef __linux__
  232. #include <unistd.h>
  233. #endif
  234. FILE* fp;
  235. char filename[30];
  236. #endif
  237. /**************************** Dump Capture ****************************************************/
  238. #ifndef vgliteDUMP_PATH
  239. # define vgliteDUMP_PATH "./"
  240. #endif
  241. #ifndef vgliteDUMP_KEY
  242. # define vgliteDUMP_KEY "process"
  243. #endif
  244. #if DUMP_LAST_CAPTURE
  245. void _SetDumpFileInfo();
  246. vg_lite_error_t vglitefDumpBuffer_single(char* Tag, size_t Physical, void* Logical, size_t Offset, size_t Bytes);
  247. #define vglitemDUMP_single vglitefDump
  248. #define vglitemDUMP_BUFFER_single vglitefDumpBuffer_single
  249. #endif
  250. #if DUMP_CAPTURE
  251. void _SetDumpFileInfo();
  252. vg_lite_error_t vglitefDump(char* String, ...);
  253. vg_lite_error_t vglitefDumpBuffer(char* Tag, size_t Physical, void* Logical, size_t Offset, size_t Bytes);
  254. #define vglitemDUMP vglitefDump
  255. #define vglitemDUMP_BUFFER vglitefDumpBuffer
  256. #else
  257. static inline void __dummy_dump(char* Message, ...) {}
  258. static inline void __dummy_dump_buffer(char* Tag, size_t Physical, void* Logical, size_t Offset, size_t Bytes) {}
  259. #define vglitemDUMP __dummy_dump
  260. #define vglitemDUMP_BUFFER __dummy_dump_buffer
  261. #endif
  262. /**********************************************************************************************/