summaryrefslogtreecommitdiff
path: root/payloads/libpayload/drivers
diff options
context:
space:
mode:
authorYu-Ping Wu <yupingso@chromium.org>2020-07-15 15:37:00 +0800
committerJoel Kitching <kitching@google.com>2020-07-23 05:09:31 +0000
commitb086728094d70b3455ddc66c513571e319516efe (patch)
tree476d35fb0fad75ef082bf6ce44040cc5b4d0b374 /payloads/libpayload/drivers
parent918073d9bf7620694b94d24b369fb2e08acec624 (diff)
downloadcoreboot-b086728094d70b3455ddc66c513571e319516efe.tar.xz
libpayload: cbgfx: Add draw_line()
Add a function draw_line() to draw either a horizontal or vertical line segment. Theoretically a horizontal line can also be drawn by calling draw_rounded_box() with dim_rel.x being the line length and dim_rel.y being the line width. However, due to the truncation in integer division when converting relative coordinates to absolute ones, this will potentially produce inconsistent line widths, depending on the value of pos_rel.y. It is guaranteed that draw_line() will produce consistent line widths, regardless of the position of the line. Also, when the thickness argument is zero, this function is able to draw a line with 1-pixel width, which is not achievable by draw_rounded_box(). BRANCH=puff BUG=b:146399181, b:161424726 TEST=emerge-puff libpayload Change-Id: I2d50414c4bfed343516197da9bb50791c89ba4c2 Signed-off-by: Yu-Ping Wu <yupingso@chromium.org> Reviewed-on: https://review.coreboot.org/c/coreboot/+/43508 Tested-by: build bot (Jenkins) <no-reply@coreboot.org> Reviewed-by: Joel Kitching <kitching@google.com>
Diffstat (limited to 'payloads/libpayload/drivers')
-rw-r--r--payloads/libpayload/drivers/video/graphics.c86
1 files changed, 79 insertions, 7 deletions
diff --git a/payloads/libpayload/drivers/video/graphics.c b/payloads/libpayload/drivers/video/graphics.c
index 54d3dfa2b2..b52bd99103 100644
--- a/payloads/libpayload/drivers/video/graphics.c
+++ b/payloads/libpayload/drivers/video/graphics.c
@@ -99,6 +99,11 @@ static void add_vectors(struct vector *out,
out->y = v1->y + v2->y;
}
+static int fraction_equal(const struct fraction *f1, const struct fraction *f2)
+{
+ return (int64_t)f1->n * f2->d == (int64_t)f2->n * f1->d;
+}
+
static int is_valid_fraction(const struct fraction *f)
{
return f->d != 0;
@@ -109,19 +114,33 @@ static int is_valid_scale(const struct scale *s)
return is_valid_fraction(&s->x) && is_valid_fraction(&s->y);
}
-static void add_fractions(struct fraction *out,
- const struct fraction *f1, const struct fraction *f2)
+static void reduce_fraction(struct fraction *out, int64_t n, int64_t d)
{
- int64_t n, d;
- int shift;
- n = (int64_t)f1->n * f2->d + (int64_t)f2->n * f1->d;
- d = (int64_t)f1->d * f2->d;
/* Simplest way to reduce the fraction until fitting in int32_t */
- shift = log2(MAX(ABS(n), ABS(d)) >> 31) + 1;
+ int shift = log2(MAX(ABS(n), ABS(d)) >> 31) + 1;
out->n = n >> shift;
out->d = d >> shift;
}
+/* out = f1 + f2 */
+static void add_fractions(struct fraction *out,
+ const struct fraction *f1, const struct fraction *f2)
+{
+ reduce_fraction(out,
+ (int64_t)f1->n * f2->d + (int64_t)f2->n * f1->d,
+ (int64_t)f1->d * f2->d);
+}
+
+/* out = f1 - f2 */
+static void subtract_fractions(struct fraction *out,
+ const struct fraction *f1,
+ const struct fraction *f2)
+{
+ reduce_fraction(out,
+ (int64_t)f1->n * f2->d - (int64_t)f2->n * f1->d,
+ (int64_t)f1->d * f2->d);
+}
+
static void add_scales(struct scale *out,
const struct scale *s1, const struct scale *s2)
{
@@ -477,6 +496,59 @@ int draw_rounded_box(const struct scale *pos_rel, const struct scale *dim_rel,
return CBGFX_SUCCESS;
}
+int draw_line(const struct scale *pos1, const struct scale *pos2,
+ const struct fraction *thickness, const struct rgb_color *rgb)
+{
+ struct fraction len;
+ struct vector top_left;
+ struct vector size;
+ struct vector p, t;
+
+ if (cbgfx_init())
+ return CBGFX_ERROR_INIT;
+
+ const uint32_t color = calculate_color(rgb, 0);
+
+ if (!is_valid_fraction(thickness))
+ return CBGFX_ERROR_INVALID_PARAMETER;
+
+ transform_vector(&top_left, &canvas.size, pos1, &canvas.offset);
+ if (fraction_equal(&pos1->y, &pos2->y)) {
+ /* Horizontal line */
+ subtract_fractions(&len, &pos2->x, &pos1->x);
+ struct scale dim = {
+ .x = { .n = len.n, .d = len.d },
+ .y = { .n = thickness->n, .d = thickness->d },
+ };
+ transform_vector(&size, &canvas.size, &dim, &vzero);
+ size.y = MAX(size.y, 1);
+ } else if (fraction_equal(&pos1->x, &pos2->x)) {
+ /* Vertical line */
+ subtract_fractions(&len, &pos2->y, &pos1->y);
+ struct scale dim = {
+ .x = { .n = thickness->n, .d = thickness->d },
+ .y = { .n = len.n, .d = len.d },
+ };
+ transform_vector(&size, &canvas.size, &dim, &vzero);
+ size.x = MAX(size.x, 1);
+ } else {
+ LOG("Only support horizontal and vertical lines\n");
+ return CBGFX_ERROR_INVALID_PARAMETER;
+ }
+
+ add_vectors(&t, &top_left, &size);
+ if (within_box(&t, &canvas) < 0) {
+ LOG("Line exceeds canvas boundary\n");
+ return CBGFX_ERROR_BOUNDARY;
+ }
+
+ for (p.y = top_left.y; p.y < t.y; p.y++)
+ for (p.x = top_left.x; p.x < t.x; p.x++)
+ set_pixel(&p, color);
+
+ return CBGFX_SUCCESS;
+}
+
int clear_canvas(const struct rgb_color *rgb)
{
const struct rect box = {