1 /*
2  * Copyright (c) 2021 HPMicro
3  *
4  * SPDX-License-Identifier: BSD-3-Clause
5  *
6  */
7 
8 #ifndef HPM_DISPLAY_COMMON_H
9 #define HPM_DISPLAY_COMMON_H
10 
11 #include "hpm_common.h"
12 
13 /**
14  * @brief Display_common driver APIs
15  * @defgroup Display_common_interface Display_common driver APIs
16  * @ingroup io_interfaces
17  * @{
18  */
19 
20 /**
21  * @brief display alphablend mode
22  */
23 typedef enum display_alphablend_mode {
24     display_alphablend_mode_clear = 0,
25     display_alphablend_mode_src = 1,
26     display_alphablend_mode_dst = 2,
27     display_alphablend_mode_src_over = 3,
28     display_alphablend_mode_dst_over = 4,
29     display_alphablend_mode_src_in = 5,
30     display_alphablend_mode_dst_in = 6,
31     display_alphablend_mode_src_out = 7,
32     display_alphablend_mode_dst_out = 8,
33     display_alphablend_mode_src_at_top = 9,
34     display_alphablend_mode_dst_at_top = 10,
35     display_alphablend_mode_xor = 11,
36     display_alphablend_mode_plus = 12,
37     display_alphablend_mode_modulate = 13,
38     display_alphablend_mode_src_org = 14,
39     display_alphablend_mode_dst_org = 15,
40 } display_alphablend_mode_t;
41 
42 /**
43  * @brief display pixel format
44  */
45 typedef enum display_pixel_format {
46     display_pixel_format_argb8888,
47     display_pixel_format_rgb565,
48     display_pixel_format_rgb555,
49     display_pixel_format_rgb444,
50     display_pixel_format_gbr422,
51     display_pixel_format_yuv422,
52     display_pixel_format_ycbcr422,
53     display_pixel_format_y8,
54     display_pixel_format_raw8,
55 } display_pixel_format_t;
56 
57 /**
58  * @brief display data byte order
59  */
60 typedef enum display_byteorder {
61     display_byteorder_a3a2a1a0 = 0,
62     display_byteorder_a2a3a0a1 = 1,
63     display_byteorder_a1a0a3a2 = 2,
64     display_byteorder_a0a1a2a3 = 3,
65 } display_byteorder_t;
66 
67 /**
68  * @brief display yuv format
69  */
70 typedef enum display_yuv_format {
71     display_yuv_mode_422_u1y1v1y2 = 0,
72     display_yuv_mode_422_v1y1u1y2,
73     display_yuv_mode_422_y1u1y2v1,
74     display_yuv_mode_422_y1v1y2u1,
75 } display_yuv_format_t;
76 
77 /**
78  * @brief display data 32 bits argb
79  */
80 typedef union display_color_32b {
81     uint32_t u;
82     struct {
83         uint8_t b;
84         uint8_t g;
85         uint8_t r;
86         uint8_t alpha;
87     };
88 } display_color_32b_t;
89 
90 /**
91  * @brief display data alpha value usage option
92  */
93 typedef enum display_alpha_op {
94     display_alpha_op_invalid = 0,
95     display_alpha_op_override = 1,
96     display_alpha_op_scale = 2,
97 } display_alpha_op_t;
98 
99 /**
100  * @brief display data alphablend option
101  */
102 typedef struct dispaly_alphablend_option {
103     uint8_t dst_alpha;
104     uint8_t src_alpha;
105     display_alpha_op_t dst_alpha_op;
106     display_alpha_op_t src_alpha_op;
107     display_alphablend_mode_t mode;
108 } display_alphablend_option_t;
109 
110 /**
111  * @brief display yuv to rgb format conversion coefficient
112  */
113 typedef struct dispaly_yuv2rgb_coef {
114     uint16_t c0;
115     uint16_t c1;
116     uint16_t c2;
117     uint16_t c3;
118     uint16_t c4;
119     uint16_t uv_offset;
120     uint16_t y_offset;
121 } display_yuv2rgb_coef_t;
122 
123 /**
124  * @brief display yuv to rgb format conversion config
125  */
126 typedef struct display_yuv2rgb_config {
127     bool enable;
128     bool ycbcr_mode;
129     display_yuv2rgb_coef_t yuv2rgb_coef;
130 } display_yuv2rgb_config_t;
131 
132 /**
133  * @brief display rgb to yuv format conversion config
134  */
135 typedef struct display_rgb2yuv_config {
136     bool enable;
137     bool ycbcr_mode;
138     uint16_t c0;
139     uint16_t c1;
140     uint16_t c2;
141     uint16_t c3;
142     uint16_t c4;
143     uint16_t c5;
144     uint16_t c6;
145     uint16_t c7;
146     uint16_t c8;
147     uint16_t uv_offset;
148     uint16_t y_offset;
149 } display_rgb2yuv_config_t;
150 
151 typedef enum display_buf_format {
152     display_buf_format_argb8888, /*!< memory layout in byte(low->high): b0[7:0], g0[7:0], r0[7:0], a0[7:0], b1[7:1], g1[7:1], r1[7:0], a1[7:0], ... */
153     display_buf_format_bgra8888, /*!< memory layout in byte(low->high): a0[7:0], r0[7:0], g0[7:0], b0[7:0], a1[7:1], r1[7:1], g1[7:0], b1[7:0], ... */
154     display_buf_format_rgb565, /*!< memory layout in byte(low->high): g0[2:0]:b0[4:0], r0[4:0]:g0[5:3], g1[2:0]:b1[4:0], r1[4:0]:g1[5:3], ... */
155     display_buf_format_rgb565_swap, /*!< memory layout in byte(low->high): r0[4:0]:g0[5:3], g0[2:0]:b0[4:0], r2[4:0]:g2[5:3], g1[2:0]:b1[4:0],  ... */
156     display_buf_format_yuyv, /*!< memory layout in byte(low->high): y0, u0, y1, v0, y2, u2, y3, v2, ... */
157     display_buf_format_uyvy, /*!< memory layout in byte(low->high): u0, y0, v0, y1, u2, y2, v2, y3, ... */
158     display_buf_format_y8, /*!< memory layout in byte(low->high): y0, y1, y2, y3, y4, y5, ... */
159     display_buf_format_max,
160 } display_buf_format_t;
161 
162 typedef struct display_buf {
163     void *buf; /*!< point pixel memory */
164     uint16_t width; /*!< width in pixel */
165     uint16_t height; /*!< height in pixel */
166     uint32_t stride; /*!< stride each line, in byte */
167     display_buf_format_t format;
168     struct {
169         display_alpha_op_t op;
170         uint8_t val;
171     } alpha;
172 } display_buf_t;
173 
174 #ifdef __cplusplus
175 extern "C" {
176 #endif
177 
178 /**
179  * @brief Display get pixel size in bit
180  *
181  * @param [in] format display_pixel_format_t
182  *
183  * @retval pixel size in bit
184  */
185 static inline
display_get_pixel_size_in_bit(display_pixel_format_t format)186     uint8_t display_get_pixel_size_in_bit(display_pixel_format_t format)
187 {
188     switch (format) {
189     case display_pixel_format_argb8888:
190         return 32;
191     case display_pixel_format_rgb565:
192         return 16;
193     case display_pixel_format_yuv422:
194         return 16;
195     case display_pixel_format_ycbcr422:
196         return 16;
197     case display_pixel_format_y8:
198         return 8;
199     case display_pixel_format_raw8:
200         return 8;
201     default:
202         return 0;
203     }
204 }
205 
206 /**
207  * @brief Check whether the pixel data is yuv format
208  *
209  * @param [in] format display_pixel_format_t
210  *
211  * @retval bool: true or false
212  */
display_pixel_format_is_yuv_format(display_pixel_format_t format)213 static inline bool display_pixel_format_is_yuv_format(display_pixel_format_t format)
214 {
215     switch (format) {
216     case display_pixel_format_yuv422:
217         return true;
218     case display_pixel_format_ycbcr422:
219         return true;
220     default:
221         return false;
222     }
223 }
224 
225 /**
226  * @brief Display get pixel size in byte
227  *
228  * @param [in] format display_pixel_format_t
229  *
230  * @retval pixel size in byte
231  */
232 static inline
display_get_pixel_size_in_byte(display_pixel_format_t format)233     uint8_t display_get_pixel_size_in_byte(display_pixel_format_t format)
234 {
235     return display_get_pixel_size_in_bit(format) >> 3;
236 }
237 
238 /**
239  * @brief Display get pitch length in byte
240  *
241  * @param [in] format display_pixel_format_t
242  * @param [in] width_in_pixel pixel width
243  *
244  * @retval pitch length in byte
245  */
246 static inline
display_get_pitch_length_in_byte(display_pixel_format_t format,uint32_t width_in_pixel)247     uint32_t display_get_pitch_length_in_byte(display_pixel_format_t format,
248                                             uint32_t width_in_pixel)
249 {
250     return width_in_pixel * (display_get_pixel_size_in_bit(format) >> 3);
251 }
252 
253 /**
254  * @}
255  *
256  */
257 
258 #ifdef __cplusplus
259 }
260 #endif
261 #endif /* HPM_DISPLAY_COMMON_H */
262