xref: /freebsd/stand/common/gfx_fb.h (revision 6faf55c86d9b86f40de8d19970e42cb42a892c47)
13630506bSToomas Soome /*-
23630506bSToomas Soome  * SPDX-License-Identifier: BSD-2-Clause
33630506bSToomas Soome  *
43630506bSToomas Soome  * Copyright 2020 Toomas Soome
53630506bSToomas Soome  * Copyright 2020 RackTop Systems, Inc.
63630506bSToomas Soome  *
73630506bSToomas Soome  * Redistribution and use in source and binary forms, with or without
83630506bSToomas Soome  * modification, are permitted provided that the following conditions
93630506bSToomas Soome  * are met:
103630506bSToomas Soome  * 1. Redistributions of source code must retain the above copyright
113630506bSToomas Soome  *    notice, this list of conditions and the following disclaimer.
123630506bSToomas Soome  * 2. Redistributions in binary form must reproduce the above copyright
133630506bSToomas Soome  *    notice, this list of conditions and the following disclaimer in the
143630506bSToomas Soome  *    documentation and/or other materials provided with the distribution.
153630506bSToomas Soome  *
163630506bSToomas Soome  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
173630506bSToomas Soome  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
183630506bSToomas Soome  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
193630506bSToomas Soome  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
203630506bSToomas Soome  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
213630506bSToomas Soome  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
223630506bSToomas Soome  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
233630506bSToomas Soome  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
243630506bSToomas Soome  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
253630506bSToomas Soome  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
263630506bSToomas Soome  * SUCH DAMAGE.
273630506bSToomas Soome  */
283630506bSToomas Soome 
293630506bSToomas Soome #ifndef _GFX_FB_H
303630506bSToomas Soome #define	_GFX_FB_H
313630506bSToomas Soome 
323630506bSToomas Soome #include <sys/font.h>
333630506bSToomas Soome #include <teken.h>
343630506bSToomas Soome #include <stdbool.h>
353630506bSToomas Soome #include <machine/metadata.h>
363630506bSToomas Soome #include <pnglite.h>
373630506bSToomas Soome 
383630506bSToomas Soome #ifdef __cplusplus
393630506bSToomas Soome extern "C" {
403630506bSToomas Soome #endif
413630506bSToomas Soome 
423630506bSToomas Soome #define	EDID_MAGIC	{ 0x00, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x00 }
433630506bSToomas Soome 
443630506bSToomas Soome struct edid_header {
453630506bSToomas Soome 	uint8_t header[8];	/* fixed header pattern */
463630506bSToomas Soome 	uint16_t manufacturer_id;
473630506bSToomas Soome 	uint16_t product_code;
483630506bSToomas Soome 	uint32_t serial_number;
493630506bSToomas Soome 	uint8_t week_of_manufacture;
503630506bSToomas Soome 	uint8_t year_of_manufacture;
513630506bSToomas Soome 	uint8_t version;
523630506bSToomas Soome 	uint8_t revision;
533630506bSToomas Soome };
543630506bSToomas Soome 
553630506bSToomas Soome struct edid_basic_display_parameters {
563630506bSToomas Soome 	uint8_t video_input_parameters;
573630506bSToomas Soome 	uint8_t max_horizontal_image_size;
583630506bSToomas Soome 	uint8_t max_vertical_image_size;
593630506bSToomas Soome 	uint8_t display_gamma;
603630506bSToomas Soome 	uint8_t supported_features;
613630506bSToomas Soome };
623630506bSToomas Soome 
633630506bSToomas Soome struct edid_chromaticity_coordinates {
643630506bSToomas Soome 	uint8_t red_green_lo;
653630506bSToomas Soome 	uint8_t blue_white_lo;
663630506bSToomas Soome 	uint8_t red_x_hi;
673630506bSToomas Soome 	uint8_t red_y_hi;
683630506bSToomas Soome 	uint8_t green_x_hi;
693630506bSToomas Soome 	uint8_t green_y_hi;
703630506bSToomas Soome 	uint8_t blue_x_hi;
713630506bSToomas Soome 	uint8_t blue_y_hi;
723630506bSToomas Soome 	uint8_t white_x_hi;
733630506bSToomas Soome 	uint8_t white_y_hi;
743630506bSToomas Soome };
753630506bSToomas Soome 
763630506bSToomas Soome struct edid_detailed_timings {
773630506bSToomas Soome 	uint16_t pixel_clock;
783630506bSToomas Soome 	uint8_t horizontal_active_lo;
793630506bSToomas Soome 	uint8_t horizontal_blanking_lo;
803630506bSToomas Soome 	uint8_t horizontal_hi;
813630506bSToomas Soome 	uint8_t vertical_active_lo;
823630506bSToomas Soome 	uint8_t vertical_blanking_lo;
833630506bSToomas Soome 	uint8_t vertical_hi;
843630506bSToomas Soome 	uint8_t horizontal_sync_offset_lo;
853630506bSToomas Soome 	uint8_t horizontal_sync_pulse_width_lo;
863630506bSToomas Soome 	uint8_t vertical_sync_lo;
873630506bSToomas Soome 	uint8_t sync_hi;
883630506bSToomas Soome 	uint8_t horizontal_image_size_lo;
893630506bSToomas Soome 	uint8_t vertical_image_size_lo;
903630506bSToomas Soome 	uint8_t image_size_hi;
913630506bSToomas Soome 	uint8_t horizontal_border;
923630506bSToomas Soome 	uint8_t vertical_border;
933630506bSToomas Soome 	uint8_t features;
943630506bSToomas Soome };
953630506bSToomas Soome 
963630506bSToomas Soome struct vesa_edid_info {
973630506bSToomas Soome 	struct edid_header header;
983630506bSToomas Soome 	struct edid_basic_display_parameters display;
993630506bSToomas Soome #define	EDID_FEATURE_PREFERRED_TIMING_MODE	(1 << 1)
1003630506bSToomas Soome 	struct edid_chromaticity_coordinates chromaticity;
1013630506bSToomas Soome 	uint8_t established_timings_1;
1023630506bSToomas Soome 	uint8_t established_timings_2;
1033630506bSToomas Soome 	uint8_t manufacturer_reserved_timings;
1043630506bSToomas Soome 	uint16_t standard_timings[8];
1053630506bSToomas Soome 	struct edid_detailed_timings detailed_timings[4];
1063630506bSToomas Soome 	uint8_t number_of_extensions;
1073630506bSToomas Soome 	uint8_t checksum;
1083630506bSToomas Soome } __packed;
1093630506bSToomas Soome 
110becaac39SToomas Soome extern struct vesa_edid_info *edid_info;
111becaac39SToomas Soome 
1123630506bSToomas Soome #define	STD_TIMINGS	8
1133630506bSToomas Soome #define	DET_TIMINGS	4
1143630506bSToomas Soome 
1153630506bSToomas Soome #define	HSIZE(x)	(((x & 0xff) + 31) * 8)
1163630506bSToomas Soome #define	RATIO(x)	((x & 0xC000) >> 14)
1173630506bSToomas Soome #define	RATIO1_1	0
1183630506bSToomas Soome /* EDID Ver. 1.3 redefined this */
1193630506bSToomas Soome #define	RATIO16_10	RATIO1_1
1203630506bSToomas Soome #define	RATIO4_3	1
1213630506bSToomas Soome #define	RATIO5_4	2
1223630506bSToomas Soome #define	RATIO16_9	3
1233630506bSToomas Soome 
1243630506bSToomas Soome /*
1253630506bSToomas Soome  * Number of pixels and lines is 12-bit int, valid values 0-4095.
1263630506bSToomas Soome  */
1273630506bSToomas Soome #define	EDID_MAX_PIXELS	4095
1283630506bSToomas Soome #define	EDID_MAX_LINES	4095
1293630506bSToomas Soome 
1303630506bSToomas Soome #define	GET_EDID_INFO_WIDTH(edid_info, timings_num) \
1313630506bSToomas Soome     ((edid_info)->detailed_timings[(timings_num)].horizontal_active_lo | \
1323630506bSToomas Soome     (((uint32_t)(edid_info)->detailed_timings[(timings_num)].horizontal_hi & \
1333630506bSToomas Soome     0xf0) << 4))
1343630506bSToomas Soome 
1353630506bSToomas Soome #define	GET_EDID_INFO_HEIGHT(edid_info, timings_num) \
1363630506bSToomas Soome     ((edid_info)->detailed_timings[(timings_num)].vertical_active_lo | \
1373630506bSToomas Soome     (((uint32_t)(edid_info)->detailed_timings[(timings_num)].vertical_hi & \
1383630506bSToomas Soome     0xf0) << 4))
1393630506bSToomas Soome 
1403630506bSToomas Soome struct resolution {
1413630506bSToomas Soome 	uint32_t width;
1423630506bSToomas Soome 	uint32_t height;
1433630506bSToomas Soome 	TAILQ_ENTRY(resolution) next;
1443630506bSToomas Soome };
1453630506bSToomas Soome 
1463630506bSToomas Soome typedef TAILQ_HEAD(edid_resolution, resolution) edid_res_list_t;
1473630506bSToomas Soome 
1483630506bSToomas Soome struct vesa_flat_panel_info {
1493630506bSToomas Soome 	uint16_t HSize;			/* Horizontal Size in Pixels */
1503630506bSToomas Soome 	uint16_t VSize;			/* Vertical Size in Lines */
1513630506bSToomas Soome 	uint16_t FPType;		/* Flat Panel Type */
1523630506bSToomas Soome 	uint8_t RedBPP;			/* Red Bits Per Primary */
1533630506bSToomas Soome 	uint8_t GreenBPP;		/* Green Bits Per Primary */
1543630506bSToomas Soome 	uint8_t BlueBPP;		/* Blue Bits Per Primary */
1553630506bSToomas Soome 	uint8_t ReservedBPP;		/* Reserved Bits Per Primary */
1563630506bSToomas Soome 	uint32_t RsvdOffScrnMemSize;	/* Size in KB of Offscreen Memory */
1573630506bSToomas Soome 	uint32_t RsvdOffScrnMemPtr; /* Pointer to reserved offscreen memory */
1583630506bSToomas Soome 	uint8_t Reserved[14];		/* remainder of FPInfo */
1593630506bSToomas Soome } __packed;
1603630506bSToomas Soome 
1613630506bSToomas Soome #define	COLOR_FORMAT_VGA 0
1623630506bSToomas Soome #define	COLOR_FORMAT_RGB 1
1633630506bSToomas Soome #define	NCOLORS	16
1643630506bSToomas Soome #define	NCMAP	256
1653630506bSToomas Soome extern uint32_t cmap[NCMAP];
1663630506bSToomas Soome 
167a26f7358SToomas Soome /*
168a26f7358SToomas Soome  * VT_FB_MAX_WIDTH and VT_FB_MAX_HEIGHT are dimensions from where
169a26f7358SToomas Soome  * we will not auto select smaller font than 8x16.
170a26f7358SToomas Soome  * See also sys/dev/vt/vt.h
171a26f7358SToomas Soome  */
172a26f7358SToomas Soome #ifndef VT_FB_MAX_WIDTH
173a26f7358SToomas Soome #define	VT_FB_MAX_WIDTH		4096
174a26f7358SToomas Soome #endif
175a26f7358SToomas Soome #ifndef VT_FB_MAX_HEIGHT
176a26f7358SToomas Soome #define	VT_FB_MAX_HEIGHT	2400
177a26f7358SToomas Soome #endif
178a26f7358SToomas Soome 
1793630506bSToomas Soome enum FB_TYPE {
1803630506bSToomas Soome 	FB_TEXT = -1,
1813630506bSToomas Soome 	FB_GOP,
1823630506bSToomas Soome 	FB_UGA,
1833630506bSToomas Soome 	FB_VBE
1843630506bSToomas Soome };
1853630506bSToomas Soome 
1863630506bSToomas Soome enum COLOR_TYPE {
1873630506bSToomas Soome 	CT_INDEXED,
1883630506bSToomas Soome 	CT_RGB
1893630506bSToomas Soome };
1903630506bSToomas Soome 
1913630506bSToomas Soome struct gen_fb {
1923630506bSToomas Soome 	uint64_t	fb_addr;
1933630506bSToomas Soome 	uint64_t	fb_size;
1943630506bSToomas Soome 	uint32_t	fb_height;
1953630506bSToomas Soome 	uint32_t	fb_width;
1963630506bSToomas Soome 	uint32_t	fb_stride;
1973630506bSToomas Soome 	uint32_t	fb_mask_red;
1983630506bSToomas Soome 	uint32_t	fb_mask_green;
1993630506bSToomas Soome 	uint32_t	fb_mask_blue;
2003630506bSToomas Soome 	uint32_t	fb_mask_reserved;
2013630506bSToomas Soome 	uint32_t	fb_bpp;
2023630506bSToomas Soome };
2033630506bSToomas Soome 
2043630506bSToomas Soome typedef struct teken_gfx {
2053630506bSToomas Soome 	enum FB_TYPE	tg_fb_type;
2063630506bSToomas Soome 	enum COLOR_TYPE tg_ctype;
2073630506bSToomas Soome 	unsigned	tg_mode;
2083630506bSToomas Soome 	teken_t		tg_teken;		/* Teken core */
2093630506bSToomas Soome 	teken_pos_t	tg_cursor;		/* Where cursor was drawn */
2103630506bSToomas Soome 	bool		tg_cursor_visible;
2113630506bSToomas Soome 	teken_pos_t	tg_tp;			/* Terminal dimensions */
2123630506bSToomas Soome 	teken_pos_t	tg_origin;		/* Point of origin in pixels */
2133630506bSToomas Soome 	uint8_t		*tg_glyph;		/* Memory for glyph */
2143630506bSToomas Soome 	size_t		tg_glyph_size;
2153630506bSToomas Soome 	struct vt_font	tg_font;
2163630506bSToomas Soome 	struct gen_fb	tg_fb;
2176102f43cSToomas Soome 	uint32_t	*tg_shadow_fb;		/* units of 4 bytes */
218221376dbSToomas Soome 	size_t		tg_shadow_sz;		/* units of pages */
2193630506bSToomas Soome 	teken_funcs_t	*tg_functions;
2203630506bSToomas Soome 	void		*tg_private;
2213630506bSToomas Soome } teken_gfx_t;
2223630506bSToomas Soome 
2233630506bSToomas Soome extern font_list_t fonts;
2243630506bSToomas Soome extern teken_gfx_t gfx_state;
2253630506bSToomas Soome 
2263630506bSToomas Soome typedef enum {
2273630506bSToomas Soome 	GfxFbBltVideoFill,
2283630506bSToomas Soome 	GfxFbBltVideoToBltBuffer,
2293630506bSToomas Soome 	GfxFbBltBufferToVideo,
2303630506bSToomas Soome 	GfxFbBltVideoToVideo,
2313630506bSToomas Soome 	GfxFbBltOperationMax,
2323630506bSToomas Soome } GFXFB_BLT_OPERATION;
2333630506bSToomas Soome 
2343630506bSToomas Soome int gfxfb_blt(void *, GFXFB_BLT_OPERATION, uint32_t, uint32_t,
2353630506bSToomas Soome     uint32_t, uint32_t, uint32_t, uint32_t, uint32_t);
2363630506bSToomas Soome 
2373630506bSToomas Soome int generate_cons_palette(uint32_t *, int, uint32_t, int, uint32_t, int,
2383630506bSToomas Soome     uint32_t, int);
2393630506bSToomas Soome bool console_update_mode(bool);
2403630506bSToomas Soome void setup_font(teken_gfx_t *, teken_unit_t, teken_unit_t);
2413630506bSToomas Soome uint8_t *font_lookup(const struct vt_font *, teken_char_t,
2423630506bSToomas Soome     const teken_attr_t *);
2433630506bSToomas Soome void bios_text_font(bool);
2443630506bSToomas Soome 
2453630506bSToomas Soome /* teken callbacks. */
2463630506bSToomas Soome tf_cursor_t gfx_fb_cursor;
2473630506bSToomas Soome tf_putchar_t gfx_fb_putchar;
2483630506bSToomas Soome tf_fill_t gfx_fb_fill;
2493630506bSToomas Soome tf_copy_t gfx_fb_copy;
2503630506bSToomas Soome tf_param_t gfx_fb_param;
2513630506bSToomas Soome 
2523630506bSToomas Soome /* Screen buffer element */
2533630506bSToomas Soome struct text_pixel {
2543630506bSToomas Soome 	teken_char_t c;
2553630506bSToomas Soome 	teken_attr_t a;
2563630506bSToomas Soome };
2573630506bSToomas Soome 
2583630506bSToomas Soome extern const int cons_to_vga_colors[NCOLORS];
2593630506bSToomas Soome 
2603630506bSToomas Soome /* Screen buffer to track changes on the terminal screen. */
2613630506bSToomas Soome extern struct text_pixel *screen_buffer;
2623630506bSToomas Soome bool is_same_pixel(struct text_pixel *, struct text_pixel *);
2633630506bSToomas Soome 
2643630506bSToomas Soome bool gfx_get_edid_resolution(struct vesa_edid_info *, edid_res_list_t *);
2653630506bSToomas Soome void gfx_framework_init(void);
2663630506bSToomas Soome void gfx_fb_cons_display(uint32_t, uint32_t, uint32_t, uint32_t, void *);
2673630506bSToomas Soome void gfx_fb_setpixel(uint32_t, uint32_t);
2683630506bSToomas Soome void gfx_fb_drawrect(uint32_t, uint32_t, uint32_t, uint32_t, uint32_t);
2693630506bSToomas Soome void gfx_term_drawrect(uint32_t, uint32_t, uint32_t, uint32_t);
2703630506bSToomas Soome void gfx_fb_line(uint32_t, uint32_t, uint32_t, uint32_t, uint32_t);
2713630506bSToomas Soome void gfx_fb_bezier(uint32_t, uint32_t, uint32_t, uint32_t, uint32_t, uint32_t,
2723630506bSToomas Soome 	uint32_t);
2733630506bSToomas Soome 
2743630506bSToomas Soome #define	FL_PUTIMAGE_BORDER	0x1
2753630506bSToomas Soome #define	FL_PUTIMAGE_NOSCROLL	0x2
2763630506bSToomas Soome #define	FL_PUTIMAGE_DEBUG	0x80
2773630506bSToomas Soome 
2783630506bSToomas Soome int gfx_fb_putimage(png_t *, uint32_t, uint32_t, uint32_t, uint32_t, uint32_t);
2793630506bSToomas Soome bool gfx_parse_mode_str(char *, int *, int *, int *);
2803630506bSToomas Soome void term_image_display(teken_gfx_t *, const teken_rect_t *);
2813630506bSToomas Soome 
2823630506bSToomas Soome void reset_font_flags(void);
2833630506bSToomas Soome 
284*6faf55c8SWarner Losh void gfx_interp_ref(void);
28560e199d9SWarner Losh 
2863630506bSToomas Soome #ifdef __cplusplus
2873630506bSToomas Soome }
2883630506bSToomas Soome #endif
2893630506bSToomas Soome 
2903630506bSToomas Soome #endif /* _GFX_FB_H */
291