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 
27 /*********************
28  *      DEFINES
29  *********************/
30 #ifndef LV_INV_BUF_SIZE
31 #define LV_INV_BUF_SIZE 32 /*Buffer size for invalid areas*/
32 #endif
33 
34 #ifndef LV_ATTRIBUTE_FLUSH_READY
35 #define LV_ATTRIBUTE_FLUSH_READY
36 #endif
37 
38 /**********************
39  *      TYPEDEFS
40  **********************/
41 
42 struct _lv_obj_t;
43 struct _lv_disp_t;
44 struct _lv_disp_drv_t;
45 struct _lv_theme_t;
46 
47 /**
48  * Structure for holding display buffer information.
49  */
50 typedef struct _lv_disp_draw_buf_t {
51     void * buf1; /**< First display buffer.*/
52     void * buf2; /**< Second display buffer.*/
53 
54     /*Internal, used by the library*/
55     void * buf_act;
56     uint32_t size; /*In pixel count*/
57     /*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)*/
58     volatile int flushing;
59     /*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)*/
60     volatile int flushing_last;
61     volatile uint32_t last_area         : 1; /*1: the last area is being rendered*/
62     volatile uint32_t last_part         : 1; /*1: the last part of the current area is being rendered*/
63 } lv_disp_draw_buf_t;
64 
65 typedef enum {
66     LV_DISP_ROT_NONE = 0,
67     LV_DISP_ROT_90,
68     LV_DISP_ROT_180,
69     LV_DISP_ROT_270
70 } lv_disp_rot_t;
71 
72 /**
73  * Display Driver structure to be registered by HAL.
74  * Only its pointer will be saved in `lv_disp_t` so it should be declared as
75  * `static lv_disp_drv_t my_drv` or allocated dynamically.
76  */
77 typedef struct _lv_disp_drv_t {
78 
79     lv_coord_t hor_res;         /**< Horizontal resolution.*/
80     lv_coord_t ver_res;         /**< Vertical resolution.*/
81 
82     lv_coord_t
83     physical_hor_res;     /**< Horizontal resolution of the full / physical display. Set to -1 for fullscreen mode.*/
84     lv_coord_t
85     physical_ver_res;     /**< Vertical resolution of the full / physical display. Set to -1 for fullscreen mode.*/
86     lv_coord_t
87     offset_x;             /**< Horizontal offset from the full / physical display. Set to 0 for fullscreen mode.*/
88     lv_coord_t offset_y;             /**< Vertical offset from the full / physical display. Set to 0 for fullscreen mode.*/
89 
90     /** Pointer to a buffer initialized with `lv_disp_draw_buf_init()`.
91      * LVGL will use this buffer(s) to draw the screens contents*/
92     lv_disp_draw_buf_t * draw_buf;
93 
94     uint32_t direct_mode : 1;        /**< 1: Use screen-sized buffers and draw to absolute coordinates*/
95     uint32_t full_refresh : 1;       /**< 1: Always make the whole screen redrawn*/
96     uint32_t sw_rotate : 1;          /**< 1: use software rotation (slower)*/
97     uint32_t antialiasing : 1;       /**< 1: anti-aliasing is enabled on this display.*/
98     uint32_t rotated : 2;            /**< 1: turn the display by 90 degree. @warning Does not update coordinates for you!*/
99     uint32_t screen_transp : 1;      /**Handle if the screen doesn't have a solid (opa == LV_OPA_COVER) background.
100                                        * Use only if required because it's slower.*/
101 
102     uint32_t dpi : 10;              /** DPI (dot per inch) of the display. Default value is `LV_DPI_DEF`.*/
103 
104     /** MANDATORY: Write the internal buffer (draw_buf) to the display. 'lv_disp_flush_ready()' has to be
105      * called when finished*/
106     void (*flush_cb)(struct _lv_disp_drv_t * disp_drv, const lv_area_t * area, lv_color_t * color_p);
107 
108     /** OPTIONAL: Extend the invalidated areas to match with the display drivers requirements
109      * E.g. round `y` to, 8, 16 ..) on a monochrome display*/
110     void (*rounder_cb)(struct _lv_disp_drv_t * disp_drv, lv_area_t * area);
111 
112     /** OPTIONAL: Set a pixel in a buffer according to the special requirements of the display
113      * Can be used for color format not supported in LittelvGL. E.g. 2 bit -> 4 gray scales
114      * @note Much slower then drawing with supported color formats.*/
115     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,
116                       lv_color_t color, lv_opa_t opa);
117 
118     void (*clear_cb)(struct _lv_disp_drv_t * disp_drv, uint8_t * buf, uint32_t size);
119 
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  :
177     1;          /**< 1: Draw previous screen over active screen*/
178 uint8_t del_prev  :
179     1;          /**< 1: Automatically delete the previous screen when the screen load animation is ready*/
180     uint8_t rendering_in_progress : 1; /**< 1: The current screen rendering is in progress*/
181 
182     lv_opa_t bg_opa;                /**<Opacity of the background color or wallpaper*/
183     lv_color_t bg_color;            /**< Default display color when screens are transparent*/
184     const void * bg_img;            /**< An image source to display as wallpaper*/
185 
186     /** Invalidated (marked to redraw) areas*/
187     lv_area_t inv_areas[LV_INV_BUF_SIZE];
188     uint8_t inv_area_joined[LV_INV_BUF_SIZE];
189     uint16_t inv_p;
190     int32_t inv_en_cnt;
191 
192     /*Miscellaneous data*/
193     uint32_t last_activity_time;        /**< Last time when there was activity on this display*/
194 } lv_disp_t;
195 
196 /**********************
197  * GLOBAL PROTOTYPES
198  **********************/
199 
200 /**
201  * Initialize a display driver with default values.
202  * It is used to have known values in the fields and not junk in memory.
203  * After it you can safely set only the fields you need.
204  * @param driver pointer to driver variable to initialize
205  */
206 void lv_disp_drv_init(lv_disp_drv_t * driver);
207 
208 /**
209  * Initialize a display buffer
210  * @param draw_buf pointer `lv_disp_draw_buf_t` variable to initialize
211  * @param buf1 A buffer to be used by LVGL to draw the image.
212  *             Always has to specified and can't be NULL.
213  *             Can be an array allocated by the user. E.g. `static lv_color_t disp_buf1[1024 * 10]`
214  *             Or a memory address e.g. in external SRAM
215  * @param buf2 Optionally specify a second buffer to make image rendering and image flushing
216  *             (sending to the display) parallel.
217  *             In the `disp_drv->flush` you should use DMA or similar hardware to send
218  *             the image to the display in the background.
219  *             It lets LVGL to render next frame into the other buffer while previous is being
220  * sent. Set to `NULL` if unused.
221  * @param size_in_px_cnt size of the `buf1` and `buf2` in pixel count.
222  */
223 void lv_disp_draw_buf_init(lv_disp_draw_buf_t * draw_buf, void * buf1, void * buf2, uint32_t size_in_px_cnt);
224 
225 /**
226  * Register an initialized display driver.
227  * Automatically set the first display as active.
228  * @param driver pointer to an initialized 'lv_disp_drv_t' variable. Only its pointer is saved!
229  * @return pointer to the new display or NULL on error
230  */
231 lv_disp_t * lv_disp_drv_register(lv_disp_drv_t * driver);
232 
233 /**
234  * Update the driver in run time.
235  * @param disp pointer to a display. (return value of `lv_disp_drv_register`)
236  * @param new_drv pointer to the new driver
237  */
238 void lv_disp_drv_update(lv_disp_t * disp, lv_disp_drv_t * new_drv);
239 
240 /**
241  * Remove a display
242  * @param disp pointer to display
243  */
244 void lv_disp_remove(lv_disp_t * disp);
245 
246 /**
247  * Set a default display. The new screens will be created on it by default.
248  * @param disp pointer to a display
249  */
250 void lv_disp_set_default(lv_disp_t * disp);
251 
252 /**
253  * Get the default display
254  * @return pointer to the default display
255  */
256 lv_disp_t * lv_disp_get_default(void);
257 
258 /**
259  * Get the horizontal resolution of a display
260  * @param disp pointer to a display (NULL to use the default display)
261  * @return the horizontal resolution of the display
262  */
263 lv_coord_t lv_disp_get_hor_res(lv_disp_t * disp);
264 
265 /**
266  * Get the vertical resolution of a display
267  * @param disp pointer to a display (NULL to use the default display)
268  * @return the vertical resolution of the display
269  */
270 lv_coord_t lv_disp_get_ver_res(lv_disp_t * disp);
271 
272 /**
273  * Get the full / physical horizontal resolution of a display
274  * @param disp pointer to a display (NULL to use the default display)
275  * @return the full / physical horizontal resolution of the display
276  */
277 lv_coord_t lv_disp_get_physical_hor_res(lv_disp_t * disp);
278 
279 /**
280  * Get the full / physical vertical resolution of a display
281  * @param disp pointer to a display (NULL to use the default display)
282  * @return the full / physical vertical resolution of the display
283  */
284 lv_coord_t lv_disp_get_physical_ver_res(lv_disp_t * disp);
285 
286 /**
287  * Get the horizontal offset from the full / physical display
288  * @param disp pointer to a display (NULL to use the default display)
289  * @return the horizontal offset from the full / physical display
290  */
291 lv_coord_t lv_disp_get_offset_x(lv_disp_t * disp);
292 
293 /**
294  * Get the vertical offset from the full / physical display
295  * @param disp pointer to a display (NULL to use the default display)
296  * @return the horizontal offset from the full / physical display
297  */
298 lv_coord_t lv_disp_get_offset_y(lv_disp_t * disp);
299 
300 /**
301  * Get if anti-aliasing is enabled for a display or not
302  * @param disp pointer to a display (NULL to use the default display)
303  * @return true: anti-aliasing is enabled; false: disabled
304  */
305 bool lv_disp_get_antialiasing(lv_disp_t * disp);
306 
307 /**
308  * Get the DPI of the display
309  * @param disp pointer to a display (NULL to use the default display)
310  * @return dpi of the display
311  */
312 lv_coord_t lv_disp_get_dpi(const lv_disp_t * disp);
313 
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 LV_ATTRIBUTE_FLUSH_READY void 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 LV_ATTRIBUTE_FLUSH_READY bool 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