summaryrefslogtreecommitdiff
path: root/payloads/libpayload
diff options
context:
space:
mode:
Diffstat (limited to 'payloads/libpayload')
-rw-r--r--payloads/libpayload/drivers/Makefile.inc3
-rw-r--r--payloads/libpayload/drivers/video/graphics.c148
-rw-r--r--payloads/libpayload/include/cbgfx.h66
-rw-r--r--payloads/libpayload/include/libpayload.h1
4 files changed, 218 insertions, 0 deletions
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 <libpayload.h>
+#include <sysinfo.h>
+
+/*
+ * '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);
+}
diff --git a/payloads/libpayload/include/cbgfx.h b/payloads/libpayload/include/cbgfx.h
new file mode 100644
index 0000000000..1f3e0b48d2
--- /dev/null
+++ b/payloads/libpayload/include/cbgfx.h
@@ -0,0 +1,66 @@
+/*
+ * This file is part of the libpayload project.
+ *
+ * Copyright (C) 2015 Google, Inc.
+ */
+
+#include <stdint.h>
+
+/*
+ * API error codes
+ */
+#define CBGFX_SUCCESS 0
+/* unknown error */
+#define CBGFX_ERROR_UNKNOWN 1
+/* failed to initialize cbgfx library */
+#define CBGFX_ERROR_INIT 2
+/* drawing beyond canvas boundary */
+#define CBGFX_ERROR_BOUNDARY 3
+
+struct vector {
+ union {
+ uint32_t x;
+ uint32_t width;
+ };
+ union {
+ uint32_t y;
+ uint32_t height;
+ };
+};
+
+struct rgb_color {
+ uint8_t red;
+ uint8_t green;
+ uint8_t blue;
+};
+
+/*
+ * Resolution of scale parameters used to describe height, width, coordinate,
+ * etc. relative to the canvas. For example, if it's 100, scales range from 0 to
+ * 100%.
+ */
+#define CANVAS_SCALE 100
+
+/*
+ * The coordinate system is expected to have (0, 0) at top left corner with
+ * y values increasing towards bottom of screen.
+ */
+
+/*
+ * draw a box filled with a color on screen
+ *
+ * top_left_rel: coordinate of top left corner of the box, relative to canvas.
+ * (0 - CANVAS_SCALE).
+ * size_rel: width and height of the box, relative to canvas. (0 - CANVAS_SCALE)
+ * rgb: RGB color of the box.
+ *
+ * return: CBGFX_* error codes
+ */
+int draw_box(const struct vector *top_left_rel,
+ const struct vector *size_rel,
+ const struct rgb_color *rgb);
+
+/*
+ * Clear the canvas
+ */
+int clear_canvas(struct rgb_color *rgb);
diff --git a/payloads/libpayload/include/libpayload.h b/payloads/libpayload/include/libpayload.h
index 470aafab5a..a1dbb0143b 100644
--- a/payloads/libpayload/include/libpayload.h
+++ b/payloads/libpayload/include/libpayload.h
@@ -44,6 +44,7 @@
#define _LIBPAYLOAD_H
#include <libpayload-config.h>
+#include <cbgfx.h>
#include <ctype.h>
#include <die.h>
#include <endian.h>