1 
2 /**
3  * @file hal_disp.c
4  *
5  * @description HAL layer for display driver
6  *
7  */
8 
9 /*********************
10  *      INCLUDES
11  *********************/
12 #include <stdint.h>
13 #include <stddef.h>
14 #include "lv_hal.h"
15 #include "../lv_misc/lv_mem.h"
16 #include "../lv_misc/lv_gc.h"
17 #include "../lv_misc/lv_debug.h"
18 #include "../lv_core/lv_obj.h"
19 #include "../lv_core/lv_refr.h"
20 #include "../lv_themes/lv_theme.h"
21 
22 #if defined(LV_GC_INCLUDE)
23     #include LV_GC_INCLUDE
24 #endif /* LV_ENABLE_GC */
25 
26 /*********************
27  *      DEFINES
28  *********************/
29 
30 /**********************
31  *      TYPEDEFS
32  **********************/
33 
34 /**********************
35  *  STATIC PROTOTYPES
36  **********************/
37 
38 /**********************
39  *  STATIC VARIABLES
40  **********************/
41 static lv_disp_t * disp_def;
42 
43 /**********************
44  *      MACROS
45  **********************/
46 
47 /**********************
48  *   GLOBAL FUNCTIONS
49  **********************/
50 
51 /**
52  * Initialize a display driver with default values.
53  * It is used to surly have known values in the fields ant not memory junk.
54  * After it you can set the fields.
55  * @param driver pointer to driver variable to initialize
56  */
lv_disp_drv_init(lv_disp_drv_t * driver)57 void lv_disp_drv_init(lv_disp_drv_t * driver)
58 {
59     _lv_memset_00(driver, sizeof(lv_disp_drv_t));
60 
61     driver->flush_cb         = NULL;
62     driver->hor_res          = LV_HOR_RES_MAX;
63     driver->ver_res          = LV_VER_RES_MAX;
64     driver->buffer           = NULL;
65     driver->rotated          = 0;
66     driver->color_chroma_key = LV_COLOR_TRANSP;
67     driver->dpi = LV_DPI;
68 
69 #if LV_ANTIALIAS
70     driver->antialiasing = true;
71 #endif
72 
73 #if LV_COLOR_SCREEN_TRANSP
74     driver->screen_transp = 1;
75 #endif
76 
77 #if LV_USE_GPU
78     driver->gpu_blend_cb = NULL;
79     driver->gpu_fill_cb  = NULL;
80 #endif
81 
82 #if LV_USE_USER_DATA
83     driver->user_data = NULL;
84 #endif
85 
86     driver->set_px_cb = NULL;
87 }
88 
89 /**
90  * Initialize a display buffer
91  * @param disp_buf pointer `lv_disp_buf_t` variable to initialize
92  * @param buf1 A buffer to be used by LVGL to draw the image.
93  *             Always has to specified and can't be NULL.
94  *             Can be an array allocated by the user. E.g. `static lv_color_t disp_buf1[1024 * 10]`
95  *             Or a memory address e.g. in external SRAM
96  * @param buf2 Optionally specify a second buffer to make image rendering and image flushing
97  *             (sending to the display) parallel.
98  *             In the `disp_drv->flush` you should use DMA or similar hardware to send
99  *             the image to the display in the background.
100  *             It lets LVGL to render next frame into the other buffer while previous is being
101  * sent. Set to `NULL` if unused.
102  * @param size_in_px_cnt size of the `buf1` and `buf2` in pixel count.
103  */
lv_disp_buf_init(lv_disp_buf_t * disp_buf,void * buf1,void * buf2,uint32_t size_in_px_cnt)104 void lv_disp_buf_init(lv_disp_buf_t * disp_buf, void * buf1, void * buf2, uint32_t size_in_px_cnt)
105 {
106     _lv_memset_00(disp_buf, sizeof(lv_disp_buf_t));
107 
108     disp_buf->buf1    = buf1;
109     disp_buf->buf2    = buf2;
110     disp_buf->buf_act = disp_buf->buf1;
111     disp_buf->size    = size_in_px_cnt;
112 }
113 
114 /**
115  * Register an initialized display driver.
116  * Automatically set the first display as active.
117  * @param driver pointer to an initialized 'lv_disp_drv_t' variable (can be local variable)
118  * @return pointer to the new display or NULL on error
119  */
lv_disp_drv_register(lv_disp_drv_t * driver)120 lv_disp_t * lv_disp_drv_register(lv_disp_drv_t * driver)
121 {
122     lv_disp_t * disp = _lv_ll_ins_head(&LV_GC_ROOT(_lv_disp_ll));
123     if(!disp) {
124         LV_ASSERT_MEM(disp);
125         return NULL;
126     }
127 
128     _lv_memset_00(disp, sizeof(lv_disp_t));
129     _lv_memcpy(&disp->driver, driver, sizeof(lv_disp_drv_t));
130 
131     _lv_ll_init(&disp->scr_ll, sizeof(lv_obj_t));
132     disp->last_activity_time = 0;
133 
134     if(disp_def == NULL) disp_def = disp;
135 
136     lv_disp_t * disp_def_tmp = disp_def;
137     disp_def                 = disp; /*Temporarily change the default screen to create the default screens on the
138                                         new display*/
139     /*Create a refresh task*/
140     disp->refr_task = lv_task_create(_lv_disp_refr_task, LV_DISP_DEF_REFR_PERIOD, LV_REFR_TASK_PRIO, disp);
141     LV_ASSERT_MEM(disp->refr_task);
142     if(disp->refr_task == NULL) return NULL;
143 
144     disp->inv_p = 0;
145     disp->last_activity_time = 0;
146 
147     disp->bg_color = LV_COLOR_WHITE;
148     disp->bg_img = NULL;
149 #if LV_COLOR_SCREEN_TRANSP
150     disp->bg_opa = LV_OPA_TRANSP;
151 #else
152     disp->bg_opa = LV_OPA_COVER;
153 #endif
154 
155     disp->prev_scr  = NULL;
156     disp->act_scr   = lv_obj_create(NULL, NULL); /*Create a default screen on the display*/
157     disp->top_layer = lv_obj_create(NULL, NULL); /*Create top layer on the display*/
158     disp->sys_layer = lv_obj_create(NULL, NULL); /*Create sys layer on the display*/
159     lv_obj_reset_style_list(disp->top_layer, LV_OBJ_PART_MAIN);
160     lv_obj_reset_style_list(disp->sys_layer, LV_OBJ_PART_MAIN);
161     lv_obj_set_click(disp->top_layer, false);
162     lv_obj_set_click(disp->sys_layer, false);
163 
164     lv_obj_invalidate(disp->act_scr);
165 
166     disp_def = disp_def_tmp; /*Revert the default display*/
167 
168     lv_task_ready(disp->refr_task); /*Be sure the screen will be refreshed immediately on start up*/
169 
170     /*Can't handle this case later so add an error*/
171     if(lv_disp_is_true_double_buf(disp) && disp->driver.set_px_cb) {
172         LV_LOG_ERROR("Can't handle 2 screen sized buffers with set_px_cb. Display will not be refreshed.");
173     }
174 
175     return disp;
176 }
177 
178 /**
179  * Update the driver in run time.
180  * @param disp pointer to a display. (return value of `lv_disp_drv_register`)
181  * @param new_drv pointer to the new driver
182  */
lv_disp_drv_update(lv_disp_t * disp,lv_disp_drv_t * new_drv)183 void lv_disp_drv_update(lv_disp_t * disp, lv_disp_drv_t * new_drv)
184 {
185     memcpy(&disp->driver, new_drv, sizeof(lv_disp_drv_t));
186 
187     lv_obj_t * scr;
188     _LV_LL_READ(disp->scr_ll, scr) {
189         lv_obj_set_size(scr, lv_disp_get_hor_res(disp), lv_disp_get_ver_res(disp));
190     }
191 }
192 
193 /**
194  * Remove a display
195  * @param disp pointer to display
196  */
lv_disp_remove(lv_disp_t * disp)197 void lv_disp_remove(lv_disp_t * disp)
198 {
199     bool was_default = false;
200     if(disp == lv_disp_get_default()) was_default = true;
201 
202     /*Detach the input devices */
203     lv_indev_t * indev;
204     indev = lv_indev_get_next(NULL);
205     while(indev) {
206         if(indev->driver.disp == disp) {
207             indev->driver.disp = NULL;
208         }
209         indev = lv_indev_get_next(indev);
210     }
211 
212     _lv_ll_remove(&LV_GC_ROOT(_lv_disp_ll), disp);
213     lv_mem_free(disp);
214 
215     if(was_default) lv_disp_set_default(_lv_ll_get_head(&LV_GC_ROOT(_lv_disp_ll)));
216 }
217 
218 /**
219  * Set a default screen. The new screens will be created on it by default.
220  * @param disp pointer to a display
221  */
lv_disp_set_default(lv_disp_t * disp)222 void lv_disp_set_default(lv_disp_t * disp)
223 {
224     disp_def = disp;
225 }
226 
227 /**
228  * Get the default display
229  * @return pointer to the default display
230  */
lv_disp_get_default(void)231 lv_disp_t * lv_disp_get_default(void)
232 {
233     return disp_def;
234 }
235 
236 /**
237  * Get the horizontal resolution of a display
238  * @param disp pointer to a display (NULL to use the default display)
239  * @return the horizontal resolution of the display
240  */
lv_disp_get_hor_res(lv_disp_t * disp)241 lv_coord_t lv_disp_get_hor_res(lv_disp_t * disp)
242 {
243     if(disp == NULL) disp = lv_disp_get_default();
244 
245     if(disp == NULL)
246         return LV_HOR_RES_MAX;
247     else
248         return disp->driver.rotated == 0 ? disp->driver.hor_res : disp->driver.ver_res;
249 }
250 
251 /**
252  * Get the vertical resolution of a display
253  * @param disp pointer to a display (NULL to use the default display)
254  * @return the vertical resolution of the display
255  */
lv_disp_get_ver_res(lv_disp_t * disp)256 lv_coord_t lv_disp_get_ver_res(lv_disp_t * disp)
257 {
258     if(disp == NULL) disp = lv_disp_get_default();
259 
260     if(disp == NULL)
261         return LV_VER_RES_MAX;
262     else
263         return disp->driver.rotated == 0 ? disp->driver.ver_res : disp->driver.hor_res;
264 }
265 
266 /**
267  * Get if anti-aliasing is enabled for a display or not
268  * @param disp pointer to a display (NULL to use the default display)
269  * @return true: anti-aliasing is enabled; false: disabled
270  */
lv_disp_get_antialiasing(lv_disp_t * disp)271 bool lv_disp_get_antialiasing(lv_disp_t * disp)
272 {
273 #if LV_ANTIALIAS == 0
274     LV_UNUSED(disp);
275     return false;
276 #else
277     if(disp == NULL) disp = lv_disp_get_default();
278     if(disp == NULL) return false;
279 
280     return disp->driver.antialiasing ? true : false;
281 #endif
282 }
283 
284 /**
285  * Get the DPI of the display
286  * @param disp pointer to a display (NULL to use the default display)
287  * @return dpi of the display
288  */
lv_disp_get_dpi(lv_disp_t * disp)289 lv_coord_t lv_disp_get_dpi(lv_disp_t * disp)
290 {
291     if(disp == NULL) disp = lv_disp_get_default();
292     if(disp == NULL) return LV_DPI;  /*Do not return 0 because it might be a divider*/
293     return disp->driver.dpi;
294 }
295 
296 /**
297  * Get the size category of the display based on it's hor. res. and dpi.
298  * @param disp pointer to a display (NULL to use the default display)
299  * @return LV_DISP_SIZE_SMALL/MEDIUM/LARGE/EXTRA_LARGE
300  */
lv_disp_get_size_category(lv_disp_t * disp)301 lv_disp_size_t lv_disp_get_size_category(lv_disp_t * disp)
302 {
303     if(disp == NULL) disp = lv_disp_get_default();
304 
305     uint32_t w;
306     if(disp == NULL) w = LV_HOR_RES_MAX;
307     else w = lv_disp_get_hor_res(disp);
308 
309     uint32_t dpi = lv_disp_get_dpi(disp);
310 
311     w = w * 10 / dpi;
312 
313     if(w < LV_DISP_SMALL_LIMIT) return LV_DISP_SIZE_SMALL;
314     if(w < LV_DISP_MEDIUM_LIMIT) return LV_DISP_SIZE_MEDIUM;
315     if(w < LV_DISP_LARGE_LIMIT) return LV_DISP_SIZE_LARGE;
316     else return LV_DISP_SIZE_EXTRA_LARGE;
317 }
318 
319 /**
320  * Call in the display driver's `flush_cb` function when the flushing is finished
321  * @param disp_drv pointer to display driver in `flush_cb` where this function is called
322  */
lv_disp_flush_ready(lv_disp_drv_t * disp_drv)323 LV_ATTRIBUTE_FLUSH_READY void lv_disp_flush_ready(lv_disp_drv_t * disp_drv)
324 {
325     /*If the screen is transparent initialize it when the flushing is ready*/
326 #if LV_COLOR_SCREEN_TRANSP
327     if(disp_drv->screen_transp) {
328         _lv_memset_00(disp_drv->buffer->buf_act, disp_drv->buffer->size * sizeof(lv_color32_t));
329     }
330 #endif
331 
332     disp_drv->buffer->flushing = 0;
333     disp_drv->buffer->flushing_last = 0;
334 }
335 
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  */
lv_disp_flush_is_last(lv_disp_drv_t * disp_drv)343 LV_ATTRIBUTE_FLUSH_READY bool lv_disp_flush_is_last(lv_disp_drv_t * disp_drv)
344 {
345     return disp_drv->buffer->flushing_last;
346 }
347 
348 /**
349  * Get the next display.
350  * @param disp pointer to the current display. NULL to initialize.
351  * @return the next display or NULL if no more. Give the first display when the parameter is NULL
352  */
lv_disp_get_next(lv_disp_t * disp)353 lv_disp_t * lv_disp_get_next(lv_disp_t * disp)
354 {
355     if(disp == NULL)
356         return _lv_ll_get_head(&LV_GC_ROOT(_lv_disp_ll));
357     else
358         return _lv_ll_get_next(&LV_GC_ROOT(_lv_disp_ll), disp);
359 }
360 
361 /**
362  * Get the internal buffer of a display
363  * @param disp pointer to a display
364  * @return pointer to the internal buffers
365  */
lv_disp_get_buf(lv_disp_t * disp)366 lv_disp_buf_t * lv_disp_get_buf(lv_disp_t * disp)
367 {
368     return disp->driver.buffer;
369 }
370 
371 /**
372  * Get the number of areas in the buffer
373  * @return number of invalid areas
374  */
lv_disp_get_inv_buf_size(lv_disp_t * disp)375 uint16_t lv_disp_get_inv_buf_size(lv_disp_t * disp)
376 {
377     return disp->inv_p;
378 }
379 
380 /**
381  * Pop (delete) the last 'num' invalidated areas from the buffer
382  * @param num number of areas to delete
383  */
_lv_disp_pop_from_inv_buf(lv_disp_t * disp,uint16_t num)384 void _lv_disp_pop_from_inv_buf(lv_disp_t * disp, uint16_t num)
385 {
386 
387     if(disp->inv_p < num)
388         disp->inv_p = 0;
389     else
390         disp->inv_p -= num;
391 }
392 
393 /**
394  * Check the driver configuration if it's double buffered (both `buf1` and `buf2` are set)
395  * @param disp pointer to to display to check
396  * @return true: double buffered; false: not double buffered
397  */
lv_disp_is_double_buf(lv_disp_t * disp)398 bool lv_disp_is_double_buf(lv_disp_t * disp)
399 {
400     if(disp->driver.buffer->buf1 && disp->driver.buffer->buf2)
401         return true;
402     else
403         return false;
404 }
405 
406 /**
407  * Check the driver configuration if it's TRUE double buffered (both `buf1` and `buf2` are set and
408  * `size` is screen sized)
409  * @param disp pointer to to display to check
410  * @return true: double buffered; false: not double buffered
411  */
lv_disp_is_true_double_buf(lv_disp_t * disp)412 bool lv_disp_is_true_double_buf(lv_disp_t * disp)
413 {
414     uint32_t scr_size = disp->driver.hor_res * disp->driver.ver_res;
415 
416     if(lv_disp_is_double_buf(disp) && disp->driver.buffer->size == scr_size) {
417         return true;
418     }
419     else {
420         return false;
421     }
422 }
423 
424 /**********************
425  *   STATIC FUNCTIONS
426  **********************/
427