summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorDaisuke Nojiri <dnojiri@chromium.org>2015-08-03 10:51:38 -0700
committerPatrick Georgi <pgeorgi@google.com>2015-10-27 15:21:42 +0100
commit09ad206cdad1b427c331a8f04a63c198e41b5ae9 (patch)
tree914e02b148bf0cbfa014580d606e2ca8620be480
parent3f66398ef8ce13a05f8a0d5127de0ae761727614 (diff)
downloadcoreboot-09ad206cdad1b427c331a8f04a63c198e41b5ae9.tar.gz
coreboot-09ad206cdad1b427c331a8f04a63c198e41b5ae9.tar.bz2
coreboot-09ad206cdad1b427c331a8f04a63c198e41b5ae9.zip
cbgfx: add draw_bitmap
draw_bitmap renders a bitmap image on screen with position and sizes scaled relative to the screen. images are scaled up or down by nearest neighbor interpolation. BUG=chrome-os-partner:43444 BRANCH=tot TEST=drew bitmap images on Samus Change-Id: Ib599acc85b25626a6aed1fa9884ecd8e169bb860 Signed-off-by: Patrick Georgi <pgeorgi@chromium.org> Original-Commit-Id: c910c9cdb7efc53aace067bd081aeefc07556811 Original-Reviewed-on: https://chromium-review.googlesource.com/290302 Original-Reviewed-by: Aaron Durbin <adurbin@chromium.org> Original-Change-Id: Ib599acc85b25626a6aed1fa9884ecd8e169bb860 Original-Signed-off-by: Daisuke Nojiri <dnojiri@chromium.org> Original-Reviewed-on: https://chromium-review.googlesource.com/295532 Reviewed-on: http://review.coreboot.org/11584 Tested-by: build bot (Jenkins) Reviewed-by: Patrick Georgi <pgeorgi@google.com>
-rw-r--r--payloads/libpayload/drivers/video/bitmap.h36
-rw-r--r--payloads/libpayload/drivers/video/graphics.c236
-rw-r--r--payloads/libpayload/include/cbgfx.h26
3 files changed, 295 insertions, 3 deletions
diff --git a/payloads/libpayload/drivers/video/bitmap.h b/payloads/libpayload/drivers/video/bitmap.h
new file mode 100644
index 000000000000..10d60a38a6f4
--- /dev/null
+++ b/payloads/libpayload/drivers/video/bitmap.h
@@ -0,0 +1,36 @@
+#ifndef __BITMAP_H__
+#define __BITMAP_H__
+
+#include <stdint.h>
+
+struct bitmap_file_header {
+ uint8_t signature[2];
+ uint32_t file_size;
+ uint16_t reserved[2];
+ uint32_t bitmap_offset;
+} __attribute__ ((__packed__));
+
+/* Bitmap version 3 */
+
+struct bitmap_header_v3 {
+ uint32_t header_size;
+ int32_t width;
+ int32_t height;
+ uint16_t planes;
+ uint16_t bits_per_pixel;
+ uint32_t compression;
+ uint32_t size;
+ int32_t h_res;
+ int32_t v_res;
+ uint32_t colors_used;
+ uint32_t colors_important;
+} __attribute__ ((__packed__));
+
+struct bitmap_palette_element_v3 {
+ uint8_t blue;
+ uint8_t green;
+ uint8_t red;
+ uint8_t reserved;
+} __attribute__ ((__packed__));
+
+#endif /* __BITMAP_H__ */
diff --git a/payloads/libpayload/drivers/video/graphics.c b/payloads/libpayload/drivers/video/graphics.c
index d9270a5a7fcd..5f1dab730844 100644
--- a/payloads/libpayload/drivers/video/graphics.c
+++ b/payloads/libpayload/drivers/video/graphics.c
@@ -6,6 +6,7 @@
#include <libpayload.h>
#include <sysinfo.h>
+#include "bitmap.h"
/*
* 'canvas' is the drawing area located in the center of the screen. It's a
@@ -26,6 +27,16 @@ static uint8_t *fbaddr;
static char initialized = 0;
#define LOG(x...) printf("CBGFX: " x)
+/*
+ * This is the range used internally to scale bitmap images. (e.g. 128 = 50%,
+ * 512 = 200%). We choose 256 so that division and multiplication become bit
+ * shift operation.
+ */
+#define BITMAP_SCALE_BASE 256
+
+#define ROUNDUP(x, y) ((x) + ((y) - ((x) % (y))))
+#define ABS(x) ((x) < 0 ? -(x) : (x))
+
static void add_vectors(struct vector *out,
const struct vector *v1, const struct vector *v2)
{
@@ -33,15 +44,30 @@ static void add_vectors(struct vector *out,
out->y = v1->y + v2->y;
}
+static void scale_vector(struct vector *out, const struct vector *in,
+ size_t scale, size_t base)
+{
+ out->x = in->x * scale / base;
+ out->y = in->y * scale / base;
+}
+
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;
}
+/*
+ * Returns 1 if exclusively within canvas, or 0 if inclusively within canvas.
+ */
static int within_canvas(const struct vector *v)
{
- return v->x < canvas.width && v->y < canvas.height;
+ if (v->x < canvas.width && v->y < canvas.height)
+ return 1;
+ else if (v->x <= canvas.width && v->y <= canvas.height)
+ return 0;
+ else
+ return -1;
}
static inline uint32_t calculate_color(const struct rgb_color *rgb)
@@ -118,7 +144,7 @@ int draw_box(const struct vector *top_left_rel,
to_canvas(top_left_rel, &top_left);
to_canvas(size_rel, &size);
add_vectors(&t, &top_left, &size);
- if (!within_canvas(&t)) {
+ if (within_canvas(&t) < 0) {
LOG("Box exceeds canvas boundary\n");
return CBGFX_ERROR_BOUNDARY;
}
@@ -146,3 +172,209 @@ int clear_canvas(struct rgb_color *rgb)
return draw_box(&coord, &size, rgb);
}
+
+static int draw_bitmap_v3(const struct vector *top_left,
+ size_t scale,
+ const struct vector *image,
+ const struct bitmap_header_v3 *header,
+ const struct bitmap_palette_element_v3 *palette,
+ const uint8_t *pixel_array)
+{
+ const int bpp = header->bits_per_pixel;
+ int32_t dir;
+ struct vector p;
+
+ if (header->compression) {
+ LOG("Compressed bitmaps are not supported\n");
+ return CBGFX_ERROR_BITMAP_FORMAT;
+ }
+ if (bpp >= 16) {
+ LOG("Non-palette bitmaps are not supported\n");
+ return CBGFX_ERROR_BITMAP_FORMAT;
+ }
+ if (bpp != 8) {
+ LOG("Unsupported bits per pixel: %d\n", bpp);
+ return CBGFX_ERROR_BITMAP_FORMAT;
+ }
+ if (scale == 0) {
+ LOG("Scaling out of range\n");
+ return CBGFX_ERROR_SCALE_OUT_OF_RANGE;
+ }
+
+ const int32_t y_stride = ROUNDUP(header->width * bpp / 8, 4);
+ /*
+ * header->height can be positive or negative.
+ *
+ * If it's negative, pixel data is stored from top to bottom. We render
+ * image from the lowest row to the highest row.
+ *
+ * If it's positive, pixel data is stored from bottom to top. We render
+ * image from the highest row to the lowest row.
+ */
+ p.y = top_left->y;
+ if (header->height < 0) {
+ dir = 1;
+ } else {
+ p.y += image->height - 1;
+ dir = -1;
+ }
+ /*
+ * Plot pixels scaled by the nearest neighbor interpolation. We scan
+ * over the image on canvas (using d) and find the corresponding pixel
+ * in the bitmap data (using s).
+ */
+ struct vector s, d;
+ for (d.y = 0; d.y < image->height; d.y++, p.y += dir) {
+ s.y = d.y * BITMAP_SCALE_BASE / scale;
+ const uint8_t *data = pixel_array + s.y * y_stride;
+ p.x = top_left->x;
+ for (d.x = 0; d.x < image->width; d.x++, p.x++) {
+ s.x = d.x * BITMAP_SCALE_BASE / scale;
+ if (s.y * y_stride + s.x > header->size)
+ /*
+ * Because we're handling integers rounded by
+ * divisions, we might get here legitimately
+ * when rendering the last row of a sane image.
+ */
+ return CBGFX_SUCCESS;
+ uint8_t index = data[s.x];
+ if (index >= header->colors_used) {
+ LOG("Color index exceeds palette boundary\n");
+ return CBGFX_ERROR_BITMAP_DATA;
+ }
+ const struct rgb_color rgb = {
+ .red = palette[index].red,
+ .green = palette[index].green,
+ .blue = palette[index].blue,
+ };
+ set_pixel(&p, calculate_color(&rgb));
+ }
+ }
+
+ return CBGFX_SUCCESS;
+}
+
+static int get_bitmap_file_header(const void *bitmap, size_t size,
+ struct bitmap_file_header *file_header)
+{
+ const struct bitmap_file_header *fh;
+
+ if (sizeof(*file_header) > size) {
+ LOG("Invalid bitmap data\n");
+ return CBGFX_ERROR_BITMAP_DATA;
+ }
+ fh = (struct bitmap_file_header *)bitmap;
+ if (fh->signature[0] != 'B' || fh->signature[1] != 'M') {
+ LOG("Bitmap signature mismatch\n");
+ return CBGFX_ERROR_BITMAP_SIGNATURE;
+ }
+ file_header->file_size = le32toh(fh->file_size);
+ if (file_header->file_size != size) {
+ LOG("Bitmap file size does not match cbfs file size\n");
+ return CBGFX_ERROR_BITMAP_DATA;
+ }
+ file_header->bitmap_offset = le32toh(fh->bitmap_offset);
+
+ return CBGFX_SUCCESS;
+}
+
+static int parse_bitmap_header_v3(const uint8_t *bitmap,
+ const struct bitmap_file_header *file_header,
+ /* ^--- IN / OUT ---v */
+ struct bitmap_header_v3 *header,
+ const struct bitmap_palette_element_v3 **palette,
+ const uint8_t **pixel_array)
+{
+ struct bitmap_header_v3 *h;
+ size_t header_offset = sizeof(struct bitmap_file_header);
+ size_t header_size = sizeof(struct bitmap_header_v3);
+ size_t palette_offset = header_offset + header_size;
+ size_t file_size = file_header->file_size;
+
+ h = (struct bitmap_header_v3 *)(bitmap + header_offset);
+ header->header_size = le32toh(h->header_size);
+ if (header->header_size != header_size) {
+ LOG("Unsupported bitmap format\n");
+ return CBGFX_ERROR_BITMAP_FORMAT;
+ }
+ header->width = le32toh(h->width);
+ header->height = le32toh(h->height);
+ header->bits_per_pixel = le16toh(h->bits_per_pixel);
+ header->compression = le32toh(h->compression);
+ header->size = le32toh(h->size);
+ header->colors_used = le32toh(h->colors_used);
+ size_t palette_size = header->colors_used
+ * sizeof(struct bitmap_palette_element_v3);
+ size_t pixel_offset = file_header->bitmap_offset;
+ if (pixel_offset > file_size) {
+ LOG("Bitmap pixel data exceeds buffer boundary\n");
+ return CBGFX_ERROR_BITMAP_DATA;
+ }
+ if (palette_offset + palette_size > pixel_offset) {
+ LOG("Bitmap palette data exceeds palette boundary\n");
+ return CBGFX_ERROR_BITMAP_DATA;
+ }
+ *palette = (struct bitmap_palette_element_v3 *)(bitmap +
+ palette_offset);
+
+ size_t pixel_size = header->size;
+ if (pixel_size != header->height *
+ ROUNDUP(header->width * header->bits_per_pixel / 8, 4)) {
+ LOG("Bitmap pixel array size does not match expected size\n");
+ return CBGFX_ERROR_BITMAP_DATA;
+ }
+ if (pixel_offset + pixel_size > file_size) {
+ LOG("Bitmap pixel array exceeds buffer boundary\n");
+ return CBGFX_ERROR_BITMAP_DATA;
+ }
+ *pixel_array = bitmap + pixel_offset;
+
+ return CBGFX_SUCCESS;
+}
+
+int draw_bitmap(const struct vector *top_left_rel,
+ size_t scale_rel, const void *bitmap, size_t size)
+{
+ struct bitmap_file_header file_header;
+ struct bitmap_header_v3 header;
+ const struct bitmap_palette_element_v3 *palette;
+ const uint8_t *pixel_array;
+ struct vector top_left, t, image;
+ size_t scale;
+ int rv;
+
+ if (cbgfx_init())
+ return CBGFX_ERROR_INIT;
+
+ rv = get_bitmap_file_header(bitmap, size, &file_header);
+ if (rv)
+ return rv;
+
+ /* only v3 is supported now */
+ rv = parse_bitmap_header_v3(bitmap, &file_header,
+ &header, &palette, &pixel_array);
+ if (rv)
+ return rv;
+
+ /* convert relative coordinate to canvas coordinate */
+ to_canvas(top_left_rel, &top_left);
+
+ /* convert canvas scale to self scale (relative to image width) */
+ scale = scale_rel * canvas.width * BITMAP_SCALE_BASE /
+ (CANVAS_SCALE * header.width);
+
+ /* calculate height and width of the image on canvas */
+ image.width = header.width;
+ image.height = ABS(header.height);
+ scale_vector(&image, &image, scale, BITMAP_SCALE_BASE);
+
+ /* check whether right bottom corner exceeds canvas boundaries or not */
+ add_vectors(&t, &image, &top_left);
+ if (within_canvas(&t) < 0) {
+ LOG("Bitmap image exceeds canvas boundary\n");
+ return CBGFX_ERROR_BOUNDARY;
+ }
+
+ return draw_bitmap_v3(&top_left, scale, &image,
+ &header, palette, pixel_array);
+}
diff --git a/payloads/libpayload/include/cbgfx.h b/payloads/libpayload/include/cbgfx.h
index 1f3e0b48d2cc..074a42d40836 100644
--- a/payloads/libpayload/include/cbgfx.h
+++ b/payloads/libpayload/include/cbgfx.h
@@ -4,7 +4,9 @@
* Copyright (C) 2015 Google, Inc.
*/
-#include <stdint.h>
+#include <libpayload.h>
+#include <arch/types.h>
+#include <stddef.h>
/*
* API error codes
@@ -16,6 +18,14 @@
#define CBGFX_ERROR_INIT 2
/* drawing beyond canvas boundary */
#define CBGFX_ERROR_BOUNDARY 3
+/* bitmap error: signature mismatch */
+#define CBGFX_ERROR_BITMAP_SIGNATURE 0x10
+/* bitmap error: unsupported format */
+#define CBGFX_ERROR_BITMAP_FORMAT 0x11
+/* bitmap error: invalid data */
+#define CBGFX_ERROR_BITMAP_DATA 0x12
+/* bitmap error: scaling out of range */
+#define CBGFX_ERROR_SCALE_OUT_OF_RANGE 0x13
struct vector {
union {
@@ -64,3 +74,17 @@ int draw_box(const struct vector *top_left_rel,
* Clear the canvas
*/
int clear_canvas(struct rgb_color *rgb);
+
+/*
+ * Draw a bitmap image on screen.
+ *
+ * top_left_rel: coordinate of the top left corner of the image relative to the
+ * canvas (0 - CANVAS_SCALE).
+ * scale_rel: scale factor relative to the canvas width (0 - CANVAS_SCALE).
+ * bitmap: pointer to the bitmap data, starting from the file header.
+ * size: size of the bitmap data
+ *
+ * return: CBGFX_* error codes
+ */
+int draw_bitmap(const struct vector *top_left_rel,
+ size_t scale_rel, const void *bitmap, size_t size);