1
0
mirror of https://github.com/lvgl/lvgl.git synced 2025-01-28 07:03:00 +08:00
lvgl/src/lv_draw/lv_draw_line.c

336 lines
12 KiB
C
Raw Normal View History

/**
* @file lv_draw_line.c
*
*/
/*********************
* INCLUDES
*********************/
#include <stdio.h>
#include <stdbool.h>
#include "lv_draw.h"
2019-08-21 15:44:35 +02:00
#include "lv_mask.h"
#include "lv_blend.h"
#include "../lv_core/lv_refr.h"
#include "../lv_misc/lv_math.h"
/*********************
* DEFINES
*********************/
/**********************
* TYPEDEFS
**********************/
/**********************
* STATIC PROTOTYPES
**********************/
2019-08-21 15:44:35 +02:00
static void draw_line_skew(const lv_point_t * point1, const lv_point_t * point2, const lv_area_t * clip,
const lv_style_t * style, lv_opa_t opa_scale);
static void draw_line_hor(const lv_point_t * point1, const lv_point_t * point2, const lv_area_t * clip,
const lv_style_t * style, lv_opa_t opa_scale);
static void draw_line_ver(const lv_point_t * point1, const lv_point_t * point2, const lv_area_t * clip,
const lv_style_t * style, lv_opa_t opa_scale);
/**********************
* STATIC VARIABLES
**********************/
/**********************
* MACROS
**********************/
/**********************
* GLOBAL FUNCTIONS
**********************/
/**
* Draw a line
2018-06-14 13:08:19 +02:00
* @param point1 first point of the line
* @param point2 second point of the line
* @param mask the line will be drawn only on this area
* @param style pointer to a line's style
* @param opa_scale scale down all opacities by the factor
*/
2019-08-21 15:44:35 +02:00
void lv_draw_line(const lv_point_t * point1, const lv_point_t * point2, const lv_area_t * clip,
const lv_style_t * style, lv_opa_t opa_scale)
{
if(style->line.width == 0) return;
if(point1->x == point2->x && point1->y == point2->y) return;
2019-08-21 15:44:35 +02:00
if(point1->y == point2->y) draw_line_hor(point1, point2, clip, style, opa_scale);
else if(point1->x == point2->x) draw_line_ver(point1, point2, clip, style, opa_scale);
else draw_line_skew(point1, point2, clip, style, opa_scale);
}
/**********************
* STATIC FUNCTIONS
**********************/
2019-08-21 15:44:35 +02:00
static void draw_line_hor(const lv_point_t * point1, const lv_point_t * point2, const lv_area_t * clip,
const lv_style_t * style, lv_opa_t opa_scale)
{
2019-08-21 15:44:35 +02:00
lv_opa_t opa = style->body.opa;
2018-09-23 21:54:55 +02:00
2019-08-24 16:35:25 +02:00
lv_disp_t * disp = lv_refr_get_disp_refreshing();
lv_disp_buf_t * vdb = lv_disp_get_buf(disp);
2018-06-19 09:49:58 +02:00
2019-08-24 16:35:25 +02:00
const lv_area_t * disp_area = &vdb->area;
lv_color_t * disp_buf = vdb->buf_act;
2019-08-21 15:44:35 +02:00
lv_coord_t w = style->line.width - 1;
lv_coord_t w_half0 = w >> 1;
lv_coord_t w_half1 = w_half0 + (w & 0x1); /*Compensate rounding error*/
2019-08-24 16:35:25 +02:00
int16_t other_mask_cnt = lv_mask_get_cnt();
2019-08-21 15:44:35 +02:00
2019-08-24 16:35:25 +02:00
lv_area_t draw_area;
draw_area.x1 = LV_MATH_MIN(point1->x, point2->x);
draw_area.x2 = LV_MATH_MAX(point1->x, point2->x) - 1;
draw_area.y1 = point1->y - w_half1;
draw_area.y2 = point1->y + w_half0;
2019-08-21 15:44:35 +02:00
2019-08-24 16:35:25 +02:00
/*If there is no mask then simply draw a rectangle*/
2019-08-26 05:06:49 +02:00
if(other_mask_cnt == 0) {
2019-08-24 16:35:25 +02:00
lv_blend_fill(disp_area, clip, &draw_area,
disp_buf, LV_IMG_CF_TRUE_COLOR, style->line.color,
NULL, LV_MASK_RES_FULL_COVER, style->line.opa, LV_BLIT_MODE_NORMAL);
2018-06-19 09:49:58 +02:00
}
2019-08-24 16:35:25 +02:00
/*If there other mask apply it*/
else {
/* Get clipped fill area which is the real draw area.
* It is always the same or inside `fill_area` */
bool is_common;
is_common = lv_area_intersect(&draw_area, clip, &draw_area);
if(!is_common) return;
/* Now `draw_area` has absolute coordinates.
* Make it relative to `disp_area` to simplify draw to `disp_buf`*/
draw_area.x1 -= vdb->area.x1;
draw_area.y1 -= vdb->area.y1;
draw_area.x2 -= vdb->area.x1;
draw_area.y2 -= vdb->area.y1;
lv_coord_t draw_area_w = lv_area_get_width(&draw_area);
lv_area_t fill_area;
fill_area.x1 = draw_area.x1 + disp_area->x1;
fill_area.x2 = draw_area.x2 + disp_area->x1;
fill_area.y1 = draw_area.y1 + disp_area->y1;
fill_area.y2 = fill_area.y1;
lv_opa_t mask_buf[LV_HOR_RES_MAX];
lv_coord_t h;
lv_mask_res_t mask_res;
for(h = draw_area.y1; h <= draw_area.y2; h++) {
memset(mask_buf, LV_OPA_COVER, draw_area_w);
mask_res = lv_mask_apply(mask_buf, vdb->area.x1 + draw_area.x1, vdb->area.y1 + h, draw_area_w);
lv_blend_fill(disp_area, clip, &fill_area,
disp_buf, LV_IMG_CF_TRUE_COLOR, style->line.color,
mask_buf, mask_res, style->line.opa, LV_BLIT_MODE_NORMAL);
fill_area.y1++;
fill_area.y2++;
}
}
2019-08-21 15:44:35 +02:00
}
2019-08-21 15:44:35 +02:00
static void draw_line_ver(const lv_point_t * point1, const lv_point_t * point2, const lv_area_t * clip,
const lv_style_t * style, lv_opa_t opa_scale)
{
lv_opa_t opa = style->body.opa;
2019-08-24 16:35:25 +02:00
lv_disp_t * disp = lv_refr_get_disp_refreshing();
lv_disp_buf_t * vdb = lv_disp_get_buf(disp);
const lv_area_t * disp_area = &vdb->area;
lv_color_t * disp_buf = vdb->buf_act;
2019-08-21 15:44:35 +02:00
lv_coord_t w = style->line.width - 1;
lv_coord_t w_half0 = w >> 1;
lv_coord_t w_half1 = w_half0 + (w & 0x1); /*Compensate rounding error*/
2019-08-24 16:35:25 +02:00
int16_t other_mask_cnt = lv_mask_get_cnt();
2019-08-21 15:44:35 +02:00
2019-08-24 16:35:25 +02:00
lv_area_t draw_area;
draw_area.x1 = point1->x - w_half1;
draw_area.x2 = point1->x + w_half0;
draw_area.y1 = LV_MATH_MIN(point1->y, point2->y);
draw_area.y2 = LV_MATH_MAX(point1->y, point2->y) - 1;
2019-08-21 15:44:35 +02:00
2019-08-24 16:35:25 +02:00
/*If there is no mask then simply draw a rectangle*/
if(other_mask_cnt == 0) {
2019-08-21 15:44:35 +02:00
2019-08-24 16:35:25 +02:00
lv_blend_fill(disp_area, clip, &draw_area,
disp_buf, LV_IMG_CF_TRUE_COLOR, style->line.color,
NULL, LV_MASK_RES_FULL_COVER, style->line.opa, LV_BLIT_MODE_NORMAL);
}
/*If there other mask apply it*/
else {
/* Get clipped fill area which is the real draw area.
* It is always the same or inside `fill_area` */
bool is_common;
is_common = lv_area_intersect(&draw_area, clip, &draw_area);
if(!is_common) return;
/* Now `draw_area` has absolute coordinates.
* Make it relative to `disp_area` to simplify draw to `disp_buf`*/
draw_area.x1 -= vdb->area.x1;
draw_area.y1 -= vdb->area.y1;
draw_area.x2 -= vdb->area.x1;
draw_area.y2 -= vdb->area.y1;
lv_coord_t draw_area_w = lv_area_get_width(&draw_area);
lv_area_t fill_area;
fill_area.x1 = draw_area.x1 + disp_area->x1;
fill_area.x2 = draw_area.x2 + disp_area->x1;
fill_area.y1 = draw_area.y1 + disp_area->y1;
fill_area.y2 = fill_area.y1;
lv_opa_t mask_buf[LV_HOR_RES_MAX];
lv_coord_t h;
lv_mask_res_t mask_res;
for(h = draw_area.y1; h <= draw_area.y2; h++) {
memset(mask_buf, LV_OPA_COVER, draw_area_w);
mask_res = lv_mask_apply(mask_buf, vdb->area.x1 + draw_area.x1, vdb->area.y1 + h, draw_area_w);
lv_blend_fill(disp_area, clip, &fill_area,
disp_buf, LV_IMG_CF_TRUE_COLOR, style->line.color,
mask_buf, mask_res, style->line.opa, LV_BLIT_MODE_NORMAL);
fill_area.y1++;
fill_area.y2++;
}
2018-06-19 09:49:58 +02:00
}
2019-08-21 15:44:35 +02:00
}
2019-08-21 15:44:35 +02:00
static void draw_line_skew(const lv_point_t * point1, const lv_point_t * point2, const lv_area_t * clip,
const lv_style_t * style, lv_opa_t opa_scale)
{
lv_opa_t opa = style->body.opa;
2019-08-21 15:44:35 +02:00
/*Keep the great y in p1*/
lv_point_t p1;
lv_point_t p2;
if(point1->y < point2->y) {
p1.y = point1->y;
p2.y = point2->y;
p1.x = point1->x;
p2.x = point2->x;
} else {
p1.y = point2->y;
p2.y = point1->y;
p1.x = point2->x;
p2.x = point1->x;
2018-06-19 09:49:58 +02:00
}
2019-08-21 15:44:35 +02:00
lv_coord_t xdiff = p2.x - p1.x;
lv_coord_t ydiff = p2.y - p1.y;
bool flat = LV_MATH_ABS(xdiff) > LV_MATH_ABS(ydiff) ? true : false;
static const uint8_t wcorr[] = {
128, 128, 128, 129, 129, 130, 130, 131,
132, 133, 134, 135, 137, 138, 140, 141,
143, 145, 147, 149, 151, 153, 155, 158,
160, 162, 165, 167, 170, 173, 175, 178,
181,
};
lv_coord_t w = style->line.width;
lv_coord_t wcorr_i = 0;
if(flat) wcorr_i = (LV_MATH_ABS(ydiff) << 5) / LV_MATH_ABS(xdiff);
else wcorr_i = (LV_MATH_ABS(xdiff) << 5) / LV_MATH_ABS(ydiff);
w = (w * wcorr[wcorr_i]) >> 7;
lv_coord_t w_half0 = w >> 1;
lv_coord_t w_half1 = w_half0 + (w & 0x1); /*Compensate rounding error*/
2019-08-24 16:35:25 +02:00
lv_area_t draw_area;
draw_area.x1 = LV_MATH_MIN(p1.x, p2.x) - w;
draw_area.x2 = LV_MATH_MAX(p1.x, p2.x) + w;
draw_area.y1 = LV_MATH_MIN(p1.y, p2.y) - w;
draw_area.y2 = LV_MATH_MAX(p1.y, p2.y) + w;
2019-08-21 15:44:35 +02:00
/* Get the union of `coords` and `clip`*/
/* `clip` is already truncated to the `vdb` size
* in 'lv_refr_area' function */
2019-08-24 16:35:25 +02:00
bool is_common = lv_area_intersect(&draw_area, &draw_area, clip);
if(is_common == false) return;
2019-08-21 15:44:35 +02:00
lv_mask_param_t mask_left_param;
lv_mask_param_t mask_right_param;
lv_mask_param_t mask_top_param;
lv_mask_param_t mask_bottom_param;
if(flat) {
if(xdiff > 0) {
lv_mask_line_points_init(&mask_left_param, p1.x, p1.y - w_half0, p2.x, p2.y - w_half0, LV_LINE_MASK_SIDE_LEFT);
lv_mask_line_points_init(&mask_right_param, p1.x, p1.y + w_half1, p2.x, p2.y + w_half1, LV_LINE_MASK_SIDE_RIGHT);
} else {
lv_mask_line_points_init(&mask_left_param, p1.x, p1.y + w_half0, p2.x, p2.y + w_half0, LV_LINE_MASK_SIDE_LEFT);
lv_mask_line_points_init(&mask_right_param, p1.x, p1.y - w_half1, p2.x, p2.y - w_half1, LV_LINE_MASK_SIDE_RIGHT);
}
2019-08-21 15:44:35 +02:00
} else {
lv_mask_line_points_init(&mask_left_param, p1.x + w_half0, p1.y, p2.x + w_half0, p2.y, LV_LINE_MASK_SIDE_LEFT);
lv_mask_line_points_init(&mask_right_param, p1.x - w_half1, p1.y, p2.x - w_half1, p2.y, LV_LINE_MASK_SIDE_RIGHT);
2018-06-19 09:49:58 +02:00
}
2019-08-21 15:44:35 +02:00
/*Use the normal vector for the endings*/
lv_mask_line_points_init(&mask_top_param, p1.x, p1.y, p1.x - ydiff, p1.y + xdiff, LV_LINE_MASK_SIDE_BOTTOM);
lv_mask_line_points_init(&mask_bottom_param, p2.x, p2.y,p2.x - ydiff, p2.y + xdiff, LV_LINE_MASK_SIDE_TOP);
int16_t mask_left_id = lv_mask_add(lv_mask_line, &mask_left_param, NULL);
int16_t mask_right_id = lv_mask_add(lv_mask_line, &mask_right_param, NULL);
int16_t mask_top_id = lv_mask_add(lv_mask_line, &mask_top_param, NULL);
int16_t mask_bottom_id = lv_mask_add(lv_mask_line, &mask_bottom_param, NULL);
lv_disp_t * disp = lv_refr_get_disp_refreshing();
lv_disp_buf_t * vdb = lv_disp_get_buf(disp);
2019-08-24 16:35:25 +02:00
const lv_area_t * disp_area = &vdb->area;
lv_color_t * disp_buf = vdb->buf_act;
2019-08-21 15:44:35 +02:00
2019-08-24 16:35:25 +02:00
/*Store the coordinates of the `draw_a` relative to the VDB */
draw_area.x1 -= disp_area->x1;
draw_area.y1 -= disp_area->y1;
draw_area.x2 -= disp_area->x1;
draw_area.y2 -= disp_area->y1;
2019-08-21 15:44:35 +02:00
2019-08-24 16:35:25 +02:00
lv_coord_t draw_area_w = lv_area_get_width(&draw_area);
2019-08-21 15:44:35 +02:00
/*Draw the background line by line*/
lv_coord_t h;
lv_mask_res_t mask_res;
2019-08-24 16:35:25 +02:00
lv_opa_t mask_buf[LV_HOR_RES_MAX];
lv_area_t fill_area;
fill_area.x1 = draw_area.x1 + disp_area->x1;
fill_area.x2 = draw_area.x2 + disp_area->x1;
fill_area.y1 = draw_area.y1 + disp_area->y1;
fill_area.y2 = fill_area.y1;
2019-08-21 15:44:35 +02:00
/*Fill the first row with 'color'*/
2019-08-24 16:35:25 +02:00
for(h = draw_area.y1; h <= draw_area.y2; h++) {
memset(mask_buf, LV_OPA_COVER, draw_area_w);
mask_res = lv_mask_apply(mask_buf, vdb->area.x1 + draw_area.x1, vdb->area.y1 + h, draw_area_w);
lv_blend_fill(disp_area, clip, &fill_area,
disp_buf, LV_IMG_CF_TRUE_COLOR, style->line.color,
mask_buf, mask_res, style->line.opa, LV_BLIT_MODE_NORMAL);
fill_area.y1++;
fill_area.y2++;
2018-06-19 09:49:58 +02:00
}
2019-08-21 15:44:35 +02:00
lv_mask_remove_id(mask_left_id);
lv_mask_remove_id(mask_right_id);
lv_mask_remove_id(mask_top_id);
lv_mask_remove_id(mask_bottom_id);
}