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     /** OPTIONAL: Called after every refresh cycle to tell the rendering and flushing time + the
122      * number of flushed pixels*/
123     void (*monitor_cb)(struct _lv_disp_drv_t * disp_drv, uint32_t time, uint32_t px);
124 
125     /** OPTIONAL: Called periodically while lvgl waits for operation to be completed.
126      * For example flushing or GPU
127      * User can execute very simple tasks here or yield the task*/
128     void (*wait_cb)(struct _lv_disp_drv_t * disp_drv);
129 
130     /** OPTIONAL: Called when lvgl needs any CPU cache that affects rendering to be cleaned*/
131     void (*clean_dcache_cb)(struct _lv_disp_drv_t * disp_drv);
132 
133     /** OPTIONAL: called when driver parameters are updated */
134     void (*drv_update_cb)(struct _lv_disp_drv_t * disp_drv);
135 
136     /** OPTIONAL: called when start rendering */
137     void (*render_start_cb)(struct _lv_disp_drv_t * disp_drv);
138 
139     /** On CHROMA_KEYED images this color will be transparent.
140      * `LV_COLOR_CHROMA_KEY` by default. (lv_conf.h)*/
141     lv_color_t color_chroma_key;
142 
143     lv_draw_ctx_t * draw_ctx;
144     void (*draw_ctx_init)(struct _lv_disp_drv_t * disp_drv, lv_draw_ctx_t * draw_ctx);
145     void (*draw_ctx_deinit)(struct _lv_disp_drv_t * disp_drv, lv_draw_ctx_t * draw_ctx);
146     size_t draw_ctx_size;
147 
148 #if LV_USE_USER_DATA
149     void * user_data; /**< Custom display driver user data*/
150 #endif
151 
152 } lv_disp_drv_t;
153 
154 /**
155  * Display structure.
156  * @note `lv_disp_drv_t` should be the first member of the structure.
157  */
158 typedef struct _lv_disp_t {
159     /**< Driver to the display*/
160     struct _lv_disp_drv_t * driver;
161 
162     /**< A timer which periodically checks the dirty areas and refreshes them*/
163     lv_timer_t * refr_timer;
164 
165     /**< The theme assigned to the screen*/
166     struct _lv_theme_t * theme;
167 
168     /** Screens of the display*/
169     struct _lv_obj_t ** screens;    /**< Array of screen objects.*/
170     struct _lv_obj_t * act_scr;     /**< Currently active screen on this display*/
171     struct _lv_obj_t * prev_scr;    /**< Previous screen. Used during screen animations*/
172     struct _lv_obj_t * scr_to_load; /**< The screen prepared to load in lv_scr_load_anim*/
173     struct _lv_obj_t * top_layer;   /**< @see lv_disp_get_layer_top*/
174     struct _lv_obj_t * sys_layer;   /**< @see lv_disp_get_layer_sys*/
175     uint32_t screen_cnt;
176     uint8_t draw_prev_over_act : 1; /**< 1: Draw previous screen over active screen*/
177     uint8_t del_prev : 1;           /**< 1: Automatically delete the previous screen when the screen load anim. is ready*/
178     uint8_t rendering_in_progress : 1; /**< 1: The current screen rendering is in progress*/
179 
180     lv_opa_t bg_opa;                /**<Opacity of the background color or wallpaper*/
181     lv_color_t bg_color;            /**< Default display color when screens are transparent*/
182     const void * bg_img;            /**< An image source to display as wallpaper*/
183 
184     /** Invalidated (marked to redraw) areas*/
185     lv_area_t inv_areas[LV_INV_BUF_SIZE];
186     uint8_t inv_area_joined[LV_INV_BUF_SIZE];
187     uint16_t inv_p;
188     int32_t inv_en_cnt;
189 
190     /** Double buffer sync areas */
191     lv_ll_t sync_areas;
192 
193     /*Miscellaneous data*/
194     uint32_t last_activity_time;        /**< Last time when there was activity on this display*/
195 } lv_disp_t;
196 
197 /**********************
198  * GLOBAL PROTOTYPES
199  **********************/
200 
201 /**
202  * Initialize a display driver with default values.
203  * It is used to have known values in the fields and not junk in memory.
204  * After it you can safely set only the fields you need.
205  * @param driver pointer to driver variable to initialize
206  */
207 void lv_disp_drv_init(lv_disp_drv_t * driver);
208 
209 /**
210  * Initialize a display buffer
211  * @param draw_buf pointer `lv_disp_draw_buf_t` variable to initialize
212  * @param buf1 A buffer to be used by LVGL to draw the image.
213  *             Always has to specified and can't be NULL.
214  *             Can be an array allocated by the user. E.g. `static lv_color_t disp_buf1[1024 * 10]`
215  *             Or a memory address e.g. in external SRAM
216  * @param buf2 Optionally specify a second buffer to make image rendering and image flushing
217  *             (sending to the display) parallel.
218  *             In the `disp_drv->flush` you should use DMA or similar hardware to send
219  *             the image to the display in the background.
220  *             It lets LVGL to render next frame into the other buffer while previous is being
221  * sent. Set to `NULL` if unused.
222  * @param size_in_px_cnt size of the `buf1` and `buf2` in pixel count.
223  */
224 void lv_disp_draw_buf_init(lv_disp_draw_buf_t * draw_buf, void * buf1, void * buf2, uint32_t size_in_px_cnt);
225 
226 /**
227  * Register an initialized display driver.
228  * Automatically set the first display as active.
229  * @param driver pointer to an initialized 'lv_disp_drv_t' variable. Only its pointer is saved!
230  * @return pointer to the new display or NULL on error
231  */
232 lv_disp_t * lv_disp_drv_register(lv_disp_drv_t * driver);
233 
234 /**
235  * Update the driver in run time.
236  * @param disp pointer to a display. (return value of `lv_disp_drv_register`)
237  * @param new_drv pointer to the new driver
238  */
239 void lv_disp_drv_update(lv_disp_t * disp, lv_disp_drv_t * new_drv);
240 
241 /**
242  * Remove a display
243  * @param disp pointer to display
244  */
245 void lv_disp_remove(lv_disp_t * disp);
246 
247 /**
248  * Set a default display. The new screens will be created on it by default.
249  * @param disp pointer to a display
250  */
251 void lv_disp_set_default(lv_disp_t * disp);
252 
253 /**
254  * Get the default display
255  * @return pointer to the default display
256  */
257 lv_disp_t * lv_disp_get_default(void);
258 
259 /**
260  * Get the horizontal resolution of a display
261  * @param disp pointer to a display (NULL to use the default display)
262  * @return the horizontal resolution of the display
263  */
264 lv_coord_t lv_disp_get_hor_res(lv_disp_t * disp);
265 
266 /**
267  * Get the vertical resolution of a display
268  * @param disp pointer to a display (NULL to use the default display)
269  * @return the vertical resolution of the display
270  */
271 lv_coord_t lv_disp_get_ver_res(lv_disp_t * disp);
272 
273 /**
274  * Get the full / physical horizontal resolution of a display
275  * @param disp pointer to a display (NULL to use the default display)
276  * @return the full / physical horizontal resolution of the display
277  */
278 lv_coord_t lv_disp_get_physical_hor_res(lv_disp_t * disp);
279 
280 /**
281  * Get the full / physical vertical resolution of a display
282  * @param disp pointer to a display (NULL to use the default display)
283  * @return the full / physical vertical resolution of the display
284  */
285 lv_coord_t lv_disp_get_physical_ver_res(lv_disp_t * disp);
286 
287 /**
288  * Get the horizontal offset from the full / physical display
289  * @param disp pointer to a display (NULL to use the default display)
290  * @return the horizontal offset from the full / physical display
291  */
292 lv_coord_t lv_disp_get_offset_x(lv_disp_t * disp);
293 
294 /**
295  * Get the vertical offset from the full / physical display
296  * @param disp pointer to a display (NULL to use the default display)
297  * @return the horizontal offset from the full / physical display
298  */
299 lv_coord_t lv_disp_get_offset_y(lv_disp_t * disp);
300 
301 /**
302  * Get if anti-aliasing is enabled for a display or not
303  * @param disp pointer to a display (NULL to use the default display)
304  * @return true: anti-aliasing is enabled; false: disabled
305  */
306 bool lv_disp_get_antialiasing(lv_disp_t * disp);
307 
308 /**
309  * Get the DPI of the display
310  * @param disp pointer to a display (NULL to use the default display)
311  * @return dpi of the display
312  */
313 lv_coord_t lv_disp_get_dpi(const lv_disp_t * disp);
314 
315 /**
316  * Set the rotation of this display.
317  * @param disp pointer to a display (NULL to use the default display)
318  * @param rotation rotation angle
319  */
320 void lv_disp_set_rotation(lv_disp_t * disp, lv_disp_rot_t rotation);
321 
322 /**
323  * Get the current rotation of this display.
324  * @param disp pointer to a display (NULL to use the default display)
325  * @return rotation angle
326  */
327 lv_disp_rot_t lv_disp_get_rotation(lv_disp_t * disp);
328 
329 //! @cond Doxygen_Suppress
330 
331 /**
332  * Call in the display driver's `flush_cb` function when the flushing is finished
333  * @param disp_drv pointer to display driver in `flush_cb` where this function is called
334  */
335 void /* LV_ATTRIBUTE_FLUSH_READY */ lv_disp_flush_ready(lv_disp_drv_t * disp_drv);
336 
337 /**
338  * Tell if it's the last area of the refreshing process.
339  * Can be called from `flush_cb` to execute some special display refreshing if needed when all areas area flushed.
340  * @param disp_drv pointer to display driver
341  * @return true: it's the last area to flush; false: there are other areas too which will be refreshed soon
342  */
343 bool /* LV_ATTRIBUTE_FLUSH_READY */ lv_disp_flush_is_last(lv_disp_drv_t * disp_drv);
344 
345 //! @endcond
346 
347 /**
348  * Get the next display.
349  * @param disp pointer to the current display. NULL to initialize.
350  * @return the next display or NULL if no more. Give the first display when the parameter is NULL
351  */
352 lv_disp_t * lv_disp_get_next(lv_disp_t * disp);
353 
354 /**
355  * Get the internal buffer of a display
356  * @param disp pointer to a display
357  * @return pointer to the internal buffers
358  */
359 lv_disp_draw_buf_t * lv_disp_get_draw_buf(lv_disp_t * disp);
360 
361 void lv_disp_drv_use_generic_set_px_cb(lv_disp_drv_t * disp_drv, lv_img_cf_t cf);
362 
363 /**********************
364  *      MACROS
365  **********************/
366 
367 #ifdef __cplusplus
368 } /*extern "C"*/
369 #endif
370 
371 #endif
372