1 /**
2  * @file lv_hal_disp.h
3  *
4  * @description Display Driver HAL interface header file
5  *
6  */
7 
8 #ifndef LV_HAL_DISP_H
9 #define LV_HAL_DISP_H
10 
11 #ifdef __cplusplus
12 extern "C" {
13 #endif
14 
15 /*********************
16  *      INCLUDES
17  *********************/
18 #include <stdint.h>
19 #include <stdbool.h>
20 #include "lv_hal.h"
21 #include "../draw/lv_draw.h"
22 #include "../misc/lv_color.h"
23 #include "../misc/lv_area.h"
24 #include "../misc/lv_ll.h"
25 #include "../misc/lv_timer.h"
26 #include "../misc/lv_ll.h"
27 
28 /*********************
29  *      DEFINES
30  *********************/
31 #ifndef LV_INV_BUF_SIZE
32 #define LV_INV_BUF_SIZE 32 /*Buffer size for invalid areas*/
33 #endif
34 
35 #ifndef LV_ATTRIBUTE_FLUSH_READY
36 #define LV_ATTRIBUTE_FLUSH_READY
37 #endif
38 
39 /**********************
40  *      TYPEDEFS
41  **********************/
42 
43 struct _lv_obj_t;
44 struct _lv_disp_t;
45 struct _lv_disp_drv_t;
46 struct _lv_theme_t;
47 
48 /**
49  * Structure for holding display buffer information.
50  */
51 typedef struct _lv_disp_draw_buf_t {
52     void * buf1; /**< First display buffer.*/
53     void * buf2; /**< Second display buffer.*/
54 
55     /*Internal, used by the library*/
56     void * buf_act;
57     uint32_t size; /*In pixel count*/
58     /*1: flushing is in progress. (It can't be a bit field because when it's cleared from IRQ Read-Modify-Write issue might occur)*/
59     volatile int flushing;
60     /*1: It was the last chunk to flush. (It can't be a bit field because when it's cleared from IRQ Read-Modify-Write issue might occur)*/
61     volatile int flushing_last;
62     volatile uint32_t last_area         : 1; /*1: the last area is being rendered*/
63     volatile uint32_t last_part         : 1; /*1: the last part of the current area is being rendered*/
64 } lv_disp_draw_buf_t;
65 
66 typedef enum {
67     LV_DISP_ROT_NONE = 0,
68     LV_DISP_ROT_90,
69     LV_DISP_ROT_180,
70     LV_DISP_ROT_270
71 } lv_disp_rot_t;
72 
73 /**
74  * Display Driver structure to be registered by HAL.
75  * Only its pointer will be saved in `lv_disp_t` so it should be declared as
76  * `static lv_disp_drv_t my_drv` or allocated dynamically.
77  */
78 typedef struct _lv_disp_drv_t {
79 
80     lv_coord_t hor_res;         /**< Horizontal resolution.*/
81     lv_coord_t ver_res;         /**< Vertical resolution.*/
82 
83     lv_coord_t
84     physical_hor_res;     /**< Horizontal resolution of the full / physical display. Set to -1 for fullscreen mode.*/
85     lv_coord_t
86     physical_ver_res;     /**< Vertical resolution of the full / physical display. Set to -1 for fullscreen mode.*/
87     lv_coord_t
88     offset_x;             /**< Horizontal offset from the full / physical display. Set to 0 for fullscreen mode.*/
89     lv_coord_t offset_y;             /**< Vertical offset from the full / physical display. Set to 0 for fullscreen mode.*/
90 
91     /** Pointer to a buffer initialized with `lv_disp_draw_buf_init()`.
92      * LVGL will use this buffer(s) to draw the screens contents*/
93     lv_disp_draw_buf_t * draw_buf;
94 
95     uint32_t direct_mode : 1;        /**< 1: Use screen-sized buffers and draw to absolute coordinates*/
96     uint32_t full_refresh : 1;       /**< 1: Always make the whole screen redrawn*/
97     uint32_t sw_rotate : 1;          /**< 1: use software rotation (slower)*/
98     uint32_t antialiasing : 1;       /**< 1: anti-aliasing is enabled on this display.*/
99     uint32_t rotated : 2;            /**< 1: turn the display by 90 degree. @warning Does not update coordinates for you!*/
100     uint32_t screen_transp : 1;      /**Handle if the screen doesn't have a solid (opa == LV_OPA_COVER) background.
101                                        * Use only if required because it's slower.*/
102 
103     uint32_t dpi : 10;              /** DPI (dot per inch) of the display. Default value is `LV_DPI_DEF`.*/
104 
105     /** MANDATORY: Write the internal buffer (draw_buf) to the display. 'lv_disp_flush_ready()' has to be
106      * called when finished*/
107     void (*flush_cb)(struct _lv_disp_drv_t * disp_drv, const lv_area_t * area, lv_color_t * color_p);
108 
109     /** OPTIONAL: Extend the invalidated areas to match with the display drivers requirements
110      * E.g. round `y` to, 8, 16 ..) on a monochrome display*/
111     void (*rounder_cb)(struct _lv_disp_drv_t * disp_drv, lv_area_t * area);
112 
113     /** OPTIONAL: Set a pixel in a buffer according to the special requirements of the display
114      * Can be used for color format not supported in LittelvGL. E.g. 2 bit -> 4 gray scales
115      * @note Much slower then drawing with supported color formats.*/
116     void (*set_px_cb)(struct _lv_disp_drv_t * disp_drv, uint8_t * buf, lv_coord_t buf_w, lv_coord_t x, lv_coord_t y,
117                       lv_color_t color, lv_opa_t opa);
118 
119     void (*clear_cb)(struct _lv_disp_drv_t * disp_drv, uint8_t * buf, uint32_t size);
120 
121 
122     /** OPTIONAL: Called after every refresh cycle to tell the rendering and flushing time + the
123      * number of flushed pixels*/
124     void (*monitor_cb)(struct _lv_disp_drv_t * disp_drv, uint32_t time, uint32_t px);
125 
126     /** OPTIONAL: Called periodically while lvgl waits for operation to be completed.
127      * For example flushing or GPU
128      * User can execute very simple tasks here or yield the task*/
129     void (*wait_cb)(struct _lv_disp_drv_t * disp_drv);
130 
131     /** OPTIONAL: Called when lvgl needs any CPU cache that affects rendering to be cleaned*/
132     void (*clean_dcache_cb)(struct _lv_disp_drv_t * disp_drv);
133 
134     /** OPTIONAL: called when driver parameters are updated */
135     void (*drv_update_cb)(struct _lv_disp_drv_t * disp_drv);
136 
137     /** OPTIONAL: called when start rendering */
138     void (*render_start_cb)(struct _lv_disp_drv_t * disp_drv);
139 
140     /** On CHROMA_KEYED images this color will be transparent.
141      * `LV_COLOR_CHROMA_KEY` by default. (lv_conf.h)*/
142     lv_color_t color_chroma_key;
143 
144     lv_draw_ctx_t * draw_ctx;
145     void (*draw_ctx_init)(struct _lv_disp_drv_t * disp_drv, lv_draw_ctx_t * draw_ctx);
146     void (*draw_ctx_deinit)(struct _lv_disp_drv_t * disp_drv, lv_draw_ctx_t * draw_ctx);
147     size_t draw_ctx_size;
148 
149 #if LV_USE_USER_DATA
150     void * user_data; /**< Custom display driver user data*/
151 #endif
152 
153 } lv_disp_drv_t;
154 
155 /**
156  * Display structure.
157  * @note `lv_disp_drv_t` should be the first member of the structure.
158  */
159 typedef struct _lv_disp_t {
160     /**< Driver to the display*/
161     struct _lv_disp_drv_t * driver;
162 
163     /**< A timer which periodically checks the dirty areas and refreshes them*/
164     lv_timer_t * refr_timer;
165 
166     /**< The theme assigned to the screen*/
167     struct _lv_theme_t * theme;
168 
169     /** Screens of the display*/
170     struct _lv_obj_t ** screens;    /**< Array of screen objects.*/
171     struct _lv_obj_t * act_scr;     /**< Currently active screen on this display*/
172     struct _lv_obj_t * prev_scr;    /**< Previous screen. Used during screen animations*/
173     struct _lv_obj_t * scr_to_load; /**< The screen prepared to load in lv_scr_load_anim*/
174     struct _lv_obj_t * top_layer;   /**< @see lv_disp_get_layer_top*/
175     struct _lv_obj_t * sys_layer;   /**< @see lv_disp_get_layer_sys*/
176     uint32_t screen_cnt;
177     uint8_t draw_prev_over_act : 1; /**< 1: Draw previous screen over active screen*/
178     uint8_t del_prev : 1;           /**< 1: Automatically delete the previous screen when the screen load anim. is ready*/
179     uint8_t rendering_in_progress : 1; /**< 1: The current screen rendering is in progress*/
180 
181     lv_opa_t bg_opa;                /**<Opacity of the background color or wallpaper*/
182     lv_color_t bg_color;            /**< Default display color when screens are transparent*/
183     const void * bg_img;            /**< An image source to display as wallpaper*/
184 
185     /** Invalidated (marked to redraw) areas*/
186     lv_area_t inv_areas[LV_INV_BUF_SIZE];
187     uint8_t inv_area_joined[LV_INV_BUF_SIZE];
188     uint16_t inv_p;
189     int32_t inv_en_cnt;
190 
191     /** Double buffer sync areas */
192     lv_ll_t sync_areas;
193 
194     /*Miscellaneous data*/
195     uint32_t last_activity_time;        /**< Last time when there was activity on this display*/
196 } lv_disp_t;
197 
198 /**********************
199  * GLOBAL PROTOTYPES
200  **********************/
201 
202 /**
203  * Initialize a display driver with default values.
204  * It is used to have known values in the fields and not junk in memory.
205  * After it you can safely set only the fields you need.
206  * @param driver pointer to driver variable to initialize
207  */
208 void lv_disp_drv_init(lv_disp_drv_t * driver);
209 
210 /**
211  * Initialize a display buffer
212  * @param draw_buf pointer `lv_disp_draw_buf_t` variable to initialize
213  * @param buf1 A buffer to be used by LVGL to draw the image.
214  *             Always has to specified and can't be NULL.
215  *             Can be an array allocated by the user. E.g. `static lv_color_t disp_buf1[1024 * 10]`
216  *             Or a memory address e.g. in external SRAM
217  * @param buf2 Optionally specify a second buffer to make image rendering and image flushing
218  *             (sending to the display) parallel.
219  *             In the `disp_drv->flush` you should use DMA or similar hardware to send
220  *             the image to the display in the background.
221  *             It lets LVGL to render next frame into the other buffer while previous is being
222  * sent. Set to `NULL` if unused.
223  * @param size_in_px_cnt size of the `buf1` and `buf2` in pixel count.
224  */
225 void lv_disp_draw_buf_init(lv_disp_draw_buf_t * draw_buf, void * buf1, void * buf2, uint32_t size_in_px_cnt);
226 
227 /**
228  * Register an initialized display driver.
229  * Automatically set the first display as active.
230  * @param driver pointer to an initialized 'lv_disp_drv_t' variable. Only its pointer is saved!
231  * @return pointer to the new display or NULL on error
232  */
233 lv_disp_t * lv_disp_drv_register(lv_disp_drv_t * driver);
234 
235 /**
236  * Update the driver in run time.
237  * @param disp pointer to a display. (return value of `lv_disp_drv_register`)
238  * @param new_drv pointer to the new driver
239  */
240 void lv_disp_drv_update(lv_disp_t * disp, lv_disp_drv_t * new_drv);
241 
242 /**
243  * Remove a display
244  * @param disp pointer to display
245  */
246 void lv_disp_remove(lv_disp_t * disp);
247 
248 /**
249  * Set a default display. The new screens will be created on it by default.
250  * @param disp pointer to a display
251  */
252 void lv_disp_set_default(lv_disp_t * disp);
253 
254 /**
255  * Get the default display
256  * @return pointer to the default display
257  */
258 lv_disp_t * lv_disp_get_default(void);
259 
260 /**
261  * Get the horizontal resolution of a display
262  * @param disp pointer to a display (NULL to use the default display)
263  * @return the horizontal resolution of the display
264  */
265 lv_coord_t lv_disp_get_hor_res(lv_disp_t * disp);
266 
267 /**
268  * Get the vertical resolution of a display
269  * @param disp pointer to a display (NULL to use the default display)
270  * @return the vertical resolution of the display
271  */
272 lv_coord_t lv_disp_get_ver_res(lv_disp_t * disp);
273 
274 /**
275  * Get the full / physical horizontal resolution of a display
276  * @param disp pointer to a display (NULL to use the default display)
277  * @return the full / physical horizontal resolution of the display
278  */
279 lv_coord_t lv_disp_get_physical_hor_res(lv_disp_t * disp);
280 
281 /**
282  * Get the full / physical vertical resolution of a display
283  * @param disp pointer to a display (NULL to use the default display)
284  * @return the full / physical vertical resolution of the display
285  */
286 lv_coord_t lv_disp_get_physical_ver_res(lv_disp_t * disp);
287 
288 /**
289  * Get the horizontal offset from the full / physical display
290  * @param disp pointer to a display (NULL to use the default display)
291  * @return the horizontal offset from the full / physical display
292  */
293 lv_coord_t lv_disp_get_offset_x(lv_disp_t * disp);
294 
295 /**
296  * Get the vertical offset from the full / physical display
297  * @param disp pointer to a display (NULL to use the default display)
298  * @return the horizontal offset from the full / physical display
299  */
300 lv_coord_t lv_disp_get_offset_y(lv_disp_t * disp);
301 
302 /**
303  * Get if anti-aliasing is enabled for a display or not
304  * @param disp pointer to a display (NULL to use the default display)
305  * @return true: anti-aliasing is enabled; false: disabled
306  */
307 bool lv_disp_get_antialiasing(lv_disp_t * disp);
308 
309 /**
310  * Get the DPI of the display
311  * @param disp pointer to a display (NULL to use the default display)
312  * @return dpi of the display
313  */
314 lv_coord_t lv_disp_get_dpi(const lv_disp_t * disp);
315 
316 
317 /**
318  * Set the rotation of this display.
319  * @param disp pointer to a display (NULL to use the default display)
320  * @param rotation rotation angle
321  */
322 void lv_disp_set_rotation(lv_disp_t * disp, lv_disp_rot_t rotation);
323 
324 /**
325  * Get the current rotation of this display.
326  * @param disp pointer to a display (NULL to use the default display)
327  * @return rotation angle
328  */
329 lv_disp_rot_t lv_disp_get_rotation(lv_disp_t * disp);
330 
331 //! @cond Doxygen_Suppress
332 
333 /**
334  * Call in the display driver's `flush_cb` function when the flushing is finished
335  * @param disp_drv pointer to display driver in `flush_cb` where this function is called
336  */
337 void /* LV_ATTRIBUTE_FLUSH_READY */ lv_disp_flush_ready(lv_disp_drv_t * disp_drv);
338 
339 /**
340  * Tell if it's the last area of the refreshing process.
341  * Can be called from `flush_cb` to execute some special display refreshing if needed when all areas area flushed.
342  * @param disp_drv pointer to display driver
343  * @return true: it's the last area to flush; false: there are other areas too which will be refreshed soon
344  */
345 bool /* LV_ATTRIBUTE_FLUSH_READY */ lv_disp_flush_is_last(lv_disp_drv_t * disp_drv);
346 
347 //! @endcond
348 
349 /**
350  * Get the next display.
351  * @param disp pointer to the current display. NULL to initialize.
352  * @return the next display or NULL if no more. Give the first display when the parameter is NULL
353  */
354 lv_disp_t * lv_disp_get_next(lv_disp_t * disp);
355 
356 /**
357  * Get the internal buffer of a display
358  * @param disp pointer to a display
359  * @return pointer to the internal buffers
360  */
361 lv_disp_draw_buf_t * lv_disp_get_draw_buf(lv_disp_t * disp);
362 
363 void lv_disp_drv_use_generic_set_px_cb(lv_disp_drv_t * disp_drv, lv_img_cf_t cf);
364 
365 /**********************
366  *      MACROS
367  **********************/
368 
369 #ifdef __cplusplus
370 } /*extern "C"*/
371 #endif
372 
373 #endif
374