UEFIStarter
a simple UEFI framework
|
Functions for creating and showing graphics. More...
#include <Uefi.h>
#include <Library/UefiLib.h>
#include <Library/UefiBootServicesTableLib.h>
#include <Library/BaseMemoryLib.h>
#include <UEFIStarter/graphics.h>
#include <UEFIStarter/core/memory.h>
#include <UEFIStarter/core/logger.h>
#include <UEFIStarter/core/cmdline.h>
#include <UEFIStarter/core/timestamp.h>
#include <UEFIStarter/core/string.h>
Macros | |
#define | MIX_CHANNEL(CH) rv.CH = corners[0].CH*sa + corners[1].CH*sb + corners[row_width].CH*sc + corners[row_width+1].CH*sd |
shortcut macro for weighted mixing of 4 pixels' values More... | |
Typedefs | |
typedef void | netpbm_pixel_parser_f(char *in, EFI_GRAPHICS_OUTPUT_BLT_PIXEL *out, unsigned int pixels, unsigned int width) |
internal: data type for netpbm parser functions | |
Functions | |
void | set_graphics_sin_func (trig_func *f) |
Sets the sin() function pointer to use in graphics operations. More... | |
void | set_graphics_cos_func (trig_func *f) |
Sets the cos() function pointer to use in graphics operations. More... | |
static BOOLEAN | _validate_vsync (cmdline_value_t v) |
Validates the "vsync mode" command-line parameter. More... | |
static BOOLEAN | _validate_fps (cmdline_value_t v) |
Validates the "framerate limit" command-line parameter. More... | |
static BOOLEAN | _validate_display_handle (cmdline_value_t v) |
Validates the "display handle" command-line parameter. More... | |
static void | _parse_ppm_pixel_data (char *in, EFI_GRAPHICS_OUTPUT_BLT_PIXEL *out, unsigned int pixels, unsigned int width) |
internal: parses PPM pixel data More... | |
static void | _parse_pgm_pixel_data (char *in, EFI_GRAPHICS_OUTPUT_BLT_PIXEL *out, unsigned int pixels, unsigned int width) |
internal: parses PGM pixel data More... | |
static void | _parse_pbm_pixel_data (char *in, EFI_GRAPHICS_OUTPUT_BLT_PIXEL *out, unsigned int pixels, unsigned int width) |
internal: parses PBM pixel data More... | |
image_t * | create_image (INTN width, INTN height) |
Allocates and initializes an image. More... | |
static image_t * | _parse_netpbm_image_data (file_contents_t *contents, netpbm_pixel_parser_f *pixel_parser, char magic_digit, char has_maxval_row) |
internal: parses netpbm file contents into an image More... | |
image_t * | parse_ppm_image_data (file_contents_t *contents) |
Parses a PPM (color) image. More... | |
image_t * | parse_pgm_image_data (file_contents_t *contents) |
Parses a PGM (grayscale) image. More... | |
image_t * | parse_pbm_image_data (file_contents_t *contents) |
Parses a PBM (black/white bitmap) image. More... | |
COLOR | interpolate_4px (COLOR *corners, UINTN row_width, float x, float y) |
Performs bilinear interpolation of a pixel value within a square of 4 surrounding pixels. More... | |
COLOR | interpolate_2px (COLOR *colors, float ratio) |
Performs linear interpolation of a pixel value between 2 pixels. More... | |
void | rotate_image (SPRITE original, SPRITE rotated, INTN radius, float theta) |
Rotates an image by an arbitrary angle. More... | |
static image_t * | _load_netpbm_file (CHAR16 *filename, image_t *parse_f(file_contents_t *)) |
internal: loads a netpbm image file. More... | |
image_t * | load_ppm_file (CHAR16 *filename) |
Reads a netpbm PPM (color) file. More... | |
image_t * | load_pgm_file (CHAR16 *filename) |
Reads a netpbm PGM (grayscale) file. More... | |
image_t * | load_pbm_file (CHAR16 *filename) |
Reads a netpbm PBM (black/white bitmap) file. More... | |
image_t * | load_netpbm_file (CHAR16 *filename) |
Reads a netpbm file, determines the image format by the file's extension. More... | |
void | free_image (image_t *image) |
Frees an image resource. More... | |
void | load_image_assets (UINTN count, image_asset_t *assets) |
Loads a list of image assets. More... | |
void | free_image_assets (UINTN count, image_asset_t *assets) |
Frees a list of image assets. More... | |
void | print_graphics_modes (EFI_GRAPHICS_OUTPUT_PROTOCOL *gop) |
Prints the list of available graphics modes. More... | |
EFI_STATUS | draw_filled_rect (EFI_GRAPHICS_OUTPUT_PROTOCOL *gop, UINTN x, UINTN y, UINTN width, UINTN height, COLOR *color) |
Draws an unbuffered rectangle to the screen. More... | |
EFI_GRAPHICS_OUTPUT_PROTOCOL * | get_graphics_protocol () |
Fetches the UEFI graphics output protocol handle. More... | |
EFI_STATUS | set_graphics_mode (EFI_GRAPHICS_OUTPUT_PROTOCOL *gop, int mode) |
Sets the graphics mode. More... | |
EFI_STATUS | query_current_mode (EFI_GRAPHICS_OUTPUT_PROTOCOL *gop, EFI_GRAPHICS_OUTPUT_MODE_INFORMATION **info) |
Fetches information about the current graphics mode. More... | |
static void | _parse_glyph_data (UINT8 *data, image_t *image, unsigned int left, unsigned int top) |
internal: parses a character from a font sprite sheet into glyph data. More... | |
glyph_list_t * | parse_glyphs (image_t *image, CHAR16 *text) |
Parses a font sprite sheet. More... | |
glyph_list_t * | load_font () |
Parses the font file. More... | |
void | draw_glyph (SPRITE start, UINTN buffer_width, glyph_t *glyph, COLOR color) |
Draws a single glyph to a sprite or output buffer. More... | |
void | draw_text (SPRITE buffer, UINTN buffer_width, glyph_list_t *glyphs, UINT32 x, UINT32 y, COLOR color, CHAR16 *text) |
Draws text to a sprite or output buffer. More... | |
void | free_glyphs (glyph_list_t *glyphs) |
Frees a previously allocated list of glyphs. More... | |
void | wait_vsync () |
Waits for the graphics card's vertical synchronisation event. More... | |
BOOLEAN | limit_framerate (UINT64 *previous, UINT64 minimum_frame_ticks) |
Limits graphics animation framerates. More... | |
GFX_BUFFER | create_graphics_fs_buffer () |
Creates a full-screen graphics buffer. More... | |
void | free_graphics_fs_buffer (void *addr) |
Frees a full-screen graphics buffer. More... | |
EFI_STATUS | graphics_fs_blt (GFX_BUFFER buffer) |
Outputs a full-screen graphics buffer to the graphics display. More... | |
EFI_STATUS | init_graphics () |
Initializes the graphics output. More... | |
void | shutdown_graphics () |
Shuts down the graphics features. More... | |
Variables | |
EFI_GRAPHICS_OUTPUT_PROTOCOL * | graphics_protocol |
UEFI's graphics output protocol. | |
EFI_GRAPHICS_OUTPUT_MODE_INFORMATION * | graphics_info |
UEFI's graphics info. | |
UINTN | graphics_fs_width |
screen width, in pixels | |
UINTN | graphics_fs_height |
screen height, in pixels | |
EFI_GRAPHICS_OUTPUT_BLT_PIXEL * | graphics_fs_buffer |
full-screen output buffer | |
UINTN | graphics_fs_pages |
number of pages for output buffer | |
UINTN | graphics_fs_pixel_count |
number of pixels in output buffer | |
static trig_func * | _sin =NULL |
pointer to sin() function | |
static trig_func * | _cos =NULL |
pointer to cos() function | |
cmdline_argument_t | graphics_argument_list [] |
list of graphics-related command-line arguments More... | |
cmdline_argument_group_t | graphics_arguments ={ L"Graphics options" ,sizeof( graphics_argument_list )/sizeof(cmdline_argument_t), graphics_argument_list } |
group for graphics-related arguments | |
Functions for creating and showing graphics.
#define MIX_CHANNEL | ( | CH | ) | rv.CH = corners[0].CH*sa + corners[1].CH*sb + corners[row_width].CH*sc + corners[row_width+1].CH*sd |
shortcut macro for weighted mixing of 4 pixels' values
CH | the color channel's name |
void set_graphics_sin_func | ( | trig_func * | f | ) |
Sets the sin() function pointer to use in graphics operations.
f | pointer to the function to use |
void set_graphics_cos_func | ( | trig_func * | f | ) |
Sets the cos() function pointer to use in graphics operations.
f | pointer to the function to use |
|
static |
Validates the "vsync mode" command-line parameter.
v | the input to check |
|
static |
Validates the "framerate limit" command-line parameter.
v | the input to check |
|
static |
Validates the "display handle" command-line parameter.
v | the input to check |
|
static |
internal: parses PPM pixel data
in | the pixel data to parse, as bytes |
out | the output sprite to write to |
pixels | the number of pixels in the image |
width | (unused) the image's width, in pixels |
|
static |
internal: parses PGM pixel data
in | the pixel data to parse, as bytes |
out | the output sprite to write to |
pixels | the number of pixels in the image |
width | (unused) the image's width, in pixels |
|
static |
internal: parses PBM pixel data
in | the pixel data to parse, as bytes |
out | the output sprite to write to |
pixels | the number of pixels in the image |
width | the image's width, in pixels |
image_t* create_image | ( | INTN | width, |
INTN | height | ||
) |
Allocates and initializes an image.
width | the image's width |
height | the image's height |
|
static |
internal: parses netpbm file contents into an image
contents | the file contents to parse |
pixel_parser | the pixel parser function to use |
magic_digit | the file's expected netpbm magic digit (indicates pixel format) |
has_maxval_row | whether the file has a row indicating pixels' maximum values |
image_t* parse_ppm_image_data | ( | file_contents_t * | contents | ) |
Parses a PPM (color) image.
contents | the file contents to parse |
image_t* parse_pgm_image_data | ( | file_contents_t * | contents | ) |
Parses a PGM (grayscale) image.
contents | the file contents to parse |
image_t* parse_pbm_image_data | ( | file_contents_t * | contents | ) |
Parses a PBM (black/white bitmap) image.
contents | the file contents to parse |
Performs bilinear interpolation of a pixel value within a square of 4 surrounding pixels.
This illustrates the required parameters to interpolate a new pixel P:
corners[0] | | x v--| -0 1 px px y| - P 2 3 px px |------------------------------| row_width
corners | pointer to the pixel data to interpolate between, starting at top left pixel's offset |
row_width | the source image's row width, in pixels |
x | the horizontal position to interpolate at, within [0..1], 0 being left |
y | the vertical position to interpolate at, within [0..1], 0 being top |
Performs linear interpolation of a pixel value between 2 pixels.
Works like interpolate_2px(), only with 1 dimension.
colors | pointer to 2 pixels to interpolate between |
ratio | the position to interpolate at, within [0..1], 0.0 being colors[0] and 1.0 being colors[1] |
Rotates an image by an arbitrary angle.
Make sure you set trigonometry function pointers with set_graphics_sin_func() and set_graphics_cos_func() before using this. This is necessary to avoid having to link StdLib into all graphical applications.
original | the source image to rotate, must be square with 2*radius+1 pixels width and height |
rotated | the output image to write the rotated image to |
radius | the inner circle's radius, should be (square's side length-1)/2 |
theta | the clockwise angle to rotate by, in radians |
internal: loads a netpbm image file.
filename | the filename to read the image from |
parse_f | the parser function to use |
image_t* load_ppm_file | ( | CHAR16 * | filename | ) |
Reads a netpbm PPM (color) file.
filename | the image's filename |
image_t* load_pgm_file | ( | CHAR16 * | filename | ) |
Reads a netpbm PGM (grayscale) file.
filename | the image's filename |
image_t* load_pbm_file | ( | CHAR16 * | filename | ) |
Reads a netpbm PBM (black/white bitmap) file.
filename | the image's filename |
image_t* load_netpbm_file | ( | CHAR16 * | filename | ) |
Reads a netpbm file, determines the image format by the file's extension.
filename | the image's filename |
void free_image | ( | image_t * | image | ) |
Frees an image resource.
image | the image to free |
void load_image_assets | ( | UINTN | count, |
image_asset_t * | assets | ||
) |
Loads a list of image assets.
Image assets are a handy way of loading e.g. sprites in a graphical application.
count | the number of assets to load |
assets | the list of assets to load |
void free_image_assets | ( | UINTN | count, |
image_asset_t * | assets | ||
) |
Frees a list of image assets.
count | the number of assets to free |
assets | the list of assets to free |
void print_graphics_modes | ( | EFI_GRAPHICS_OUTPUT_PROTOCOL * | gop | ) |
Prints the list of available graphics modes.
gop | the UEFI graphics output protocol handle |
EFI_STATUS draw_filled_rect | ( | EFI_GRAPHICS_OUTPUT_PROTOCOL * | gop, |
UINTN | x, | ||
UINTN | y, | ||
UINTN | width, | ||
UINTN | height, | ||
COLOR * | color | ||
) |
Draws an unbuffered rectangle to the screen.
gop | the UEFI graphics output protocol handle |
x | the rectangle's left x coordinate |
y | the rectangle's top y coordinate |
width | the rectangle's width, in pixels |
height | the rectangle's height, in pixels |
color | the rectangle's color |
EFI_GRAPHICS_OUTPUT_PROTOCOL* get_graphics_protocol | ( | ) |
Fetches the UEFI graphics output protocol handle.
This is the main access point for UEFI graphics functions.
EFI_STATUS set_graphics_mode | ( | EFI_GRAPHICS_OUTPUT_PROTOCOL * | gop, |
int | mode | ||
) |
Sets the graphics mode.
gop | the UEFI graphics output protocol handle |
mode | the graphics mode to set |
EFI_STATUS query_current_mode | ( | EFI_GRAPHICS_OUTPUT_PROTOCOL * | gop, |
EFI_GRAPHICS_OUTPUT_MODE_INFORMATION ** | info | ||
) |
Fetches information about the current graphics mode.
gop | the UEFI graphics output protocol handle |
info | the graphics mode info structure to write to |
|
static |
internal: parses a character from a font sprite sheet into glyph data.
data | the glyph data to write to |
image | the sprite sheet to read from |
left | the left offset to start reading from |
top | the top offset to start reading from |
glyph_list_t* parse_glyphs | ( | image_t * | image, |
CHAR16 * | text | ||
) |
Parses a font sprite sheet.
image | the sprite sheet to read from |
text | the text printed in the sprite sheet, use \n to indicate line breaks |
glyph_list_t* load_font | ( | ) |
Parses the font file.
There's currently just one font file with a hardcoded text. The font file is a netpbm image with known text printed in it.
Draws a single glyph to a sprite or output buffer.
start | the first pixel to write to |
buffer_width | the width of the output buffer, in pixels |
glyph | the glyph to draw |
color | the color to draw with |
void draw_text | ( | SPRITE | buffer, |
UINTN | buffer_width, | ||
glyph_list_t * | glyphs, | ||
UINT32 | x, | ||
UINT32 | y, | ||
COLOR | color, | ||
CHAR16 * | text | ||
) |
Draws text to a sprite or output buffer.
buffer | the output image to write to |
buffer_width | the output image's width, in pixels |
glyphs | the glyph list (font) to use |
x | the x coordinate to start writing at |
y | the y coordinate to start writing at |
color | the color to draw the text with |
text | the text to write, as UTF-16 |
void free_glyphs | ( | glyph_list_t * | glyphs | ) |
Frees a previously allocated list of glyphs.
glyphs | the list to free |
void wait_vsync | ( | ) |
Waits for the graphics card's vertical synchronisation event.
How and if this works depends on your hardware. If you're running the application in a virtual environment it might not work at all.
BOOLEAN limit_framerate | ( | UINT64 * | previous, |
UINT64 | minimum_frame_ticks | ||
) |
Limits graphics animation framerates.
This works by keeping track of timestamps: a given number of ticks needs to have elapsed between frames. If vsync is enabled this function additionally waits for the next vsync event.
previous | the previous timestamp value, will be updated when done waiting |
minimum_frame_ticks | the number of ticks that must have elapsed since the previous timestamp |
GFX_BUFFER create_graphics_fs_buffer | ( | ) |
Creates a full-screen graphics buffer.
Make sure you have set the target graphics mode first.
void free_graphics_fs_buffer | ( | void * | addr | ) |
Frees a full-screen graphics buffer.
addr | the buffer to free |
EFI_STATUS graphics_fs_blt | ( | GFX_BUFFER | buffer | ) |
Outputs a full-screen graphics buffer to the graphics display.
buffer | the graphics buffer to output |
EFI_STATUS init_graphics | ( | ) |
Initializes the graphics output.
Call this function at the start of graphical applications.
void shutdown_graphics | ( | ) |
Shuts down the graphics features.
Call this at the end of graphical applications.
cmdline_argument_t graphics_argument_list[] |
list of graphics-related command-line arguments