From 3f66398ef8ce13a05f8a0d5127de0ae761727614 Mon Sep 17 00:00:00 2001 From: Daisuke Nojiri Date: Wed, 29 Jul 2015 16:03:52 -0700 Subject: cbgfx: coreboot graphics library This change introduces cbgfx, a graphics library, which provides APIs for drawing basic shapes, texts, graphic data, etc. on a screen. BUG=chrome-os-partner:43444 BRANCH=tot TEST=Drew boxes by draw command of depthcharge cli on Samus Change-Id: I6019e5998e65dca3ab4785a90669b5db02463d2e Signed-off-by: Patrick Georgi Original-Commit-Id: 5b3ebce8eae91be742e4f977d3407d24e1537580 Original-Reviewed-on: https://chromium-review.googlesource.com/290301 Original-Reviewed-by: Stefan Reinauer Original-Change-Id: I10db27715cb907bdc451a33ed99d257e3af241b7 Original-Signed-off-by: Daisuke Nojiri Original-Reviewed-on: https://chromium-review.googlesource.com/291065 Original-Reviewed-by: Aaron Durbin Reviewed-on: http://review.coreboot.org/11408 Tested-by: build bot (Jenkins) Reviewed-by: Patrick Georgi --- payloads/libpayload/drivers/Makefile.inc | 3 + payloads/libpayload/drivers/video/graphics.c | 148 +++++++++++++++++++++++++++ 2 files changed, 151 insertions(+) create mode 100644 payloads/libpayload/drivers/video/graphics.c (limited to 'payloads/libpayload/drivers') diff --git a/payloads/libpayload/drivers/Makefile.inc b/payloads/libpayload/drivers/Makefile.inc index 25ff4fea20..083f122ab8 100644 --- a/payloads/libpayload/drivers/Makefile.inc +++ b/payloads/libpayload/drivers/Makefile.inc @@ -68,6 +68,9 @@ libc-$(CONFIG_LP_GEODELX_VIDEO_CONSOLE) += video/font8x16.c libc-$(CONFIG_LP_COREBOOT_VIDEO_CONSOLE) += video/corebootfb.c libc-$(CONFIG_LP_COREBOOT_VIDEO_CONSOLE) += video/font8x16.c +# cbgfx: coreboot graphics library +libc-y += video/graphics.c + # AHCI/ATAPI driver libc-$(CONFIG_LP_STORAGE) += storage/storage.c libc-$(CONFIG_LP_STORAGE_AHCI) += storage/ahci.c diff --git a/payloads/libpayload/drivers/video/graphics.c b/payloads/libpayload/drivers/video/graphics.c new file mode 100644 index 0000000000..d9270a5a7f --- /dev/null +++ b/payloads/libpayload/drivers/video/graphics.c @@ -0,0 +1,148 @@ +/* + * This file is part of the libpayload project. + * + * Copyright (C) 2015 Google, Inc. + */ + +#include +#include + +/* + * 'canvas' is the drawing area located in the center of the screen. It's a + * square area, stretching vertically to the edges of the screen, leaving + * non-drawing areas on the left and right. The screen is assumed to be + * landscape. + */ +static struct vector canvas; +static uint32_t canvas_offset; /* horizontal position of canvas */ + +/* + * Framebuffer is assumed to assign a higher coordinate (larger x, y) to + * a higher address + */ +static struct cb_framebuffer *fbinfo; +static uint8_t *fbaddr; + +static char initialized = 0; +#define LOG(x...) printf("CBGFX: " x) + +static void add_vectors(struct vector *out, + const struct vector *v1, const struct vector *v2) +{ + out->x = v1->x + v2->x; + out->y = v1->y + v2->y; +} + +static void to_canvas(const struct vector *relative, struct vector *absolute) +{ + absolute->x = canvas.width * relative->x / CANVAS_SCALE; + absolute->y = canvas.height * relative->y / CANVAS_SCALE; +} + +static int within_canvas(const struct vector *v) +{ + return v->x < canvas.width && v->y < canvas.height; +} + +static inline uint32_t calculate_color(const struct rgb_color *rgb) +{ + uint32_t color = 0; + color |= (rgb->red >> (8 - fbinfo->red_mask_size)) + << fbinfo->red_mask_pos; + color |= (rgb->green >> (8 - fbinfo->green_mask_size)) + << fbinfo->green_mask_pos; + color |= (rgb->blue >> (8 - fbinfo->blue_mask_size)) + << fbinfo->blue_mask_pos; + return color; +} + +/* + * Plot a pixel in a framebuffer. This is called from tight loops. Keep it slim + * and do the validation at callers' site. + */ +static inline void set_pixel(struct vector *coord, uint32_t color) +{ + const int bpp = fbinfo->bits_per_pixel; + int i; + uint8_t * const pixel = fbaddr + (coord->x + canvas_offset + + coord->y * fbinfo->x_resolution) * bpp / 8; + for (i = 0; i < bpp / 8; i++) + pixel[i] = (color >> (i * 8)); +} + +/* + * Initializes the library. Automatically called by APIs. It sets up + * the canvas and the framebuffer. + */ +static int cbgfx_init(void) +{ + if (initialized) + return 0; + + fbinfo = lib_sysinfo.framebuffer; + if (!fbinfo) + return -1; + + fbaddr = phys_to_virt((uint8_t *)(uintptr_t)(fbinfo->physical_address)); + if (!fbaddr) + return -1; + + /* calculate canvas size, assuming the screen is landscape */ + canvas.height = fbinfo->y_resolution; + canvas.width = canvas.height; + canvas_offset = (fbinfo->x_resolution - canvas.width) / 2; + if (canvas_offset < 0) { + LOG("Portrait screens are not supported\n"); + return -1; + } + + initialized = 1; + LOG("cbgfx initialized: canvas width=%d, height=%d, offset=%d\n", + canvas.width, canvas.height, canvas_offset); + + return 0; +} + +int draw_box(const struct vector *top_left_rel, + const struct vector *size_rel, + const struct rgb_color *rgb) +{ + struct vector top_left; + struct vector size; + struct vector p, t; + const uint32_t color = calculate_color(rgb); + + if (cbgfx_init()) + return CBGFX_ERROR_INIT; + + to_canvas(top_left_rel, &top_left); + to_canvas(size_rel, &size); + add_vectors(&t, &top_left, &size); + if (!within_canvas(&t)) { + LOG("Box 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(struct rgb_color *rgb) +{ + const struct vector coord = { + .x = 0, + .y = 0, + }; + const struct vector size = { + .width = CANVAS_SCALE, + .height = CANVAS_SCALE, + }; + + if (cbgfx_init()) + return CBGFX_ERROR_INIT; + + return draw_box(&coord, &size, rgb); +} -- cgit v1.2.3