1 /*
2  * Copyright (c) 2019 Jan Van Winkel <jan.van_winkel@dxplore.eu>
3  *
4  * SPDX-License-Identifier: Apache-2.0
5  */
6 
7 #include <zephyr/kernel.h>
8 #include <lvgl.h>
9 #include <string.h>
10 #include "lvgl_display.h"
11 
12 #define COLOR_PALETTE_HEADER_SIZE (8)
13 
14 static uint8_t *mono_conv_buf;
15 static uint32_t mono_conv_buf_size;
16 
set_px_at_pos(uint8_t * dst_buf,uint32_t x,uint32_t y,uint32_t width,const struct display_capabilities * caps)17 static ALWAYS_INLINE void set_px_at_pos(uint8_t *dst_buf, uint32_t x, uint32_t y, uint32_t width,
18 					const struct display_capabilities *caps)
19 {
20 	uint8_t bit;
21 	uint8_t *buf;
22 
23 	if (caps->screen_info & SCREEN_INFO_MONO_VTILED) {
24 		buf = dst_buf + x + y / 8 * width;
25 
26 		if (caps->screen_info & SCREEN_INFO_MONO_MSB_FIRST) {
27 			bit = 7 - y % 8;
28 		} else {
29 			bit = y % 8;
30 		}
31 	} else {
32 		buf = dst_buf + x / 8 + y * width / 8;
33 
34 		if (caps->screen_info & SCREEN_INFO_MONO_MSB_FIRST) {
35 			bit = 7 - x % 8;
36 		} else {
37 			bit = x % 8;
38 		}
39 	}
40 
41 	if (caps->current_pixel_format == PIXEL_FORMAT_MONO10) {
42 		*buf |= BIT(bit);
43 	} else {
44 		*buf &= ~BIT(bit);
45 	}
46 }
47 
lvgl_transform_buffer(uint8_t ** px_map,uint32_t width,uint32_t height,const struct display_capabilities * caps)48 static void lvgl_transform_buffer(uint8_t **px_map, uint32_t width, uint32_t height,
49 				  const struct display_capabilities *caps)
50 {
51 	uint8_t clear_color = caps->current_pixel_format == PIXEL_FORMAT_MONO10 ? 0x00 : 0xFF;
52 
53 	memset(mono_conv_buf, clear_color, mono_conv_buf_size);
54 
55 	/* Needed because LVGL reserves some bytes in the buffer for the color palette. */
56 	*px_map += COLOR_PALETTE_HEADER_SIZE;
57 
58 	uint8_t *src_buf = *px_map;
59 	uint32_t stride = (width + CONFIG_LV_DRAW_BUF_STRIDE_ALIGN - 1) &
60 			  ~(CONFIG_LV_DRAW_BUF_STRIDE_ALIGN - 1);
61 
62 	for (uint32_t y = 0; y < height; y++) {
63 		for (uint32_t x = 0; x < width; x++) {
64 			uint32_t bit_idx = x + y * stride;
65 			uint8_t src_bit = (src_buf[bit_idx / 8] >> (7 - (bit_idx % 8))) & 1;
66 
67 			if (src_bit) {
68 				set_px_at_pos(mono_conv_buf, x, y, width, caps);
69 			}
70 		}
71 	}
72 
73 	memcpy(src_buf, mono_conv_buf, mono_conv_buf_size - COLOR_PALETTE_HEADER_SIZE);
74 }
75 
lvgl_flush_cb_mono(lv_display_t * display,const lv_area_t * area,uint8_t * px_map)76 void lvgl_flush_cb_mono(lv_display_t *display, const lv_area_t *area, uint8_t *px_map)
77 {
78 	uint16_t w = area->x2 - area->x1 + 1;
79 	uint16_t h = area->y2 - area->y1 + 1;
80 	struct lvgl_disp_data *data = (struct lvgl_disp_data *)lv_display_get_user_data(display);
81 	const struct device *display_dev = data->display_dev;
82 	const bool is_epd = data->cap.screen_info & SCREEN_INFO_EPD;
83 	const bool is_last = lv_display_flush_is_last(display);
84 
85 	lvgl_transform_buffer(&px_map, w, h, &data->cap);
86 
87 	if (is_epd && !data->blanking_on && !is_last) {
88 		/*
89 		 * Turn on display blanking when using an EPD
90 		 * display. This prevents updates and the associated
91 		 * flicker if the screen is rendered in multiple
92 		 * steps.
93 		 */
94 		display_blanking_on(display_dev);
95 		data->blanking_on = true;
96 	}
97 
98 	struct display_buffer_descriptor desc = {
99 		.buf_size = (w * h) / 8U,
100 		.width = w,
101 		.pitch = w,
102 		.height = h,
103 		.frame_incomplete = !is_last,
104 	};
105 
106 	display_write(display_dev, area->x1, area->y1, &desc, (void *)px_map);
107 	if (data->cap.screen_info & SCREEN_INFO_DOUBLE_BUFFER) {
108 		display_write(display_dev, area->x1, area->y1, &desc, (void *)px_map);
109 	}
110 
111 	if (is_epd && is_last && data->blanking_on) {
112 		/*
113 		 * The entire screen has now been rendered. Update the
114 		 * display by disabling blanking.
115 		 */
116 		display_blanking_off(display_dev);
117 		data->blanking_on = false;
118 	}
119 
120 	lv_display_flush_ready(display);
121 }
122 
lvgl_rounder_cb_mono(lv_event_t * e)123 void lvgl_rounder_cb_mono(lv_event_t *e)
124 {
125 	lv_area_t *area = lv_event_get_param(e);
126 	lv_display_t *display = lv_event_get_user_data(e);
127 	struct lvgl_disp_data *data = (struct lvgl_disp_data *)lv_display_get_user_data(display);
128 
129 	if (data->cap.screen_info & SCREEN_INFO_X_ALIGNMENT_WIDTH) {
130 		area->x1 = 0;
131 		area->x2 = data->cap.x_resolution - 1;
132 	} else {
133 		area->x1 &= ~0x7;
134 		area->x2 |= 0x7;
135 		if (data->cap.screen_info & SCREEN_INFO_MONO_VTILED) {
136 			area->y1 &= ~0x7;
137 			area->y2 |= 0x7;
138 		}
139 	}
140 }
141 
lvgl_set_mono_conversion_buffer(uint8_t * buffer,uint32_t buffer_size)142 void lvgl_set_mono_conversion_buffer(uint8_t *buffer, uint32_t buffer_size)
143 {
144 	mono_conv_buf = buffer;
145 	mono_conv_buf_size = buffer_size;
146 }
147