xref: /freebsd/stand/common/gfx_fb.h (revision 315ee00f)
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause
3  *
4  * Copyright 2020 Toomas Soome
5  * Copyright 2020 RackTop Systems, Inc.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  *
16  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26  * SUCH DAMAGE.
27  */
28 
29 #ifndef _GFX_FB_H
30 #define	_GFX_FB_H
31 
32 #include <sys/font.h>
33 #include <teken.h>
34 #include <stdbool.h>
35 #include <machine/metadata.h>
36 #include <pnglite.h>
37 
38 #ifdef __cplusplus
39 extern "C" {
40 #endif
41 
42 #define	EDID_MAGIC	{ 0x00, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x00 }
43 
44 struct edid_header {
45 	uint8_t header[8];	/* fixed header pattern */
46 	uint16_t manufacturer_id;
47 	uint16_t product_code;
48 	uint32_t serial_number;
49 	uint8_t week_of_manufacture;
50 	uint8_t year_of_manufacture;
51 	uint8_t version;
52 	uint8_t revision;
53 };
54 
55 struct edid_basic_display_parameters {
56 	uint8_t video_input_parameters;
57 	uint8_t max_horizontal_image_size;
58 	uint8_t max_vertical_image_size;
59 	uint8_t display_gamma;
60 	uint8_t supported_features;
61 };
62 
63 struct edid_chromaticity_coordinates {
64 	uint8_t red_green_lo;
65 	uint8_t blue_white_lo;
66 	uint8_t red_x_hi;
67 	uint8_t red_y_hi;
68 	uint8_t green_x_hi;
69 	uint8_t green_y_hi;
70 	uint8_t blue_x_hi;
71 	uint8_t blue_y_hi;
72 	uint8_t white_x_hi;
73 	uint8_t white_y_hi;
74 };
75 
76 struct edid_detailed_timings {
77 	uint16_t pixel_clock;
78 	uint8_t horizontal_active_lo;
79 	uint8_t horizontal_blanking_lo;
80 	uint8_t horizontal_hi;
81 	uint8_t vertical_active_lo;
82 	uint8_t vertical_blanking_lo;
83 	uint8_t vertical_hi;
84 	uint8_t horizontal_sync_offset_lo;
85 	uint8_t horizontal_sync_pulse_width_lo;
86 	uint8_t vertical_sync_lo;
87 	uint8_t sync_hi;
88 	uint8_t horizontal_image_size_lo;
89 	uint8_t vertical_image_size_lo;
90 	uint8_t image_size_hi;
91 	uint8_t horizontal_border;
92 	uint8_t vertical_border;
93 	uint8_t features;
94 };
95 
96 struct vesa_edid_info {
97 	struct edid_header header;
98 	struct edid_basic_display_parameters display;
99 #define	EDID_FEATURE_PREFERRED_TIMING_MODE	(1 << 1)
100 	struct edid_chromaticity_coordinates chromaticity;
101 	uint8_t established_timings_1;
102 	uint8_t established_timings_2;
103 	uint8_t manufacturer_reserved_timings;
104 	uint16_t standard_timings[8];
105 	struct edid_detailed_timings detailed_timings[4];
106 	uint8_t number_of_extensions;
107 	uint8_t checksum;
108 } __packed;
109 
110 extern struct vesa_edid_info *edid_info;
111 
112 #define	STD_TIMINGS	8
113 #define	DET_TIMINGS	4
114 
115 #define	HSIZE(x)	(((x & 0xff) + 31) * 8)
116 #define	RATIO(x)	((x & 0xC000) >> 14)
117 #define	RATIO1_1	0
118 /* EDID Ver. 1.3 redefined this */
119 #define	RATIO16_10	RATIO1_1
120 #define	RATIO4_3	1
121 #define	RATIO5_4	2
122 #define	RATIO16_9	3
123 
124 /*
125  * Number of pixels and lines is 12-bit int, valid values 0-4095.
126  */
127 #define	EDID_MAX_PIXELS	4095
128 #define	EDID_MAX_LINES	4095
129 
130 #define	GET_EDID_INFO_WIDTH(edid_info, timings_num) \
131     ((edid_info)->detailed_timings[(timings_num)].horizontal_active_lo | \
132     (((uint32_t)(edid_info)->detailed_timings[(timings_num)].horizontal_hi & \
133     0xf0) << 4))
134 
135 #define	GET_EDID_INFO_HEIGHT(edid_info, timings_num) \
136     ((edid_info)->detailed_timings[(timings_num)].vertical_active_lo | \
137     (((uint32_t)(edid_info)->detailed_timings[(timings_num)].vertical_hi & \
138     0xf0) << 4))
139 
140 struct resolution {
141 	uint32_t width;
142 	uint32_t height;
143 	TAILQ_ENTRY(resolution) next;
144 };
145 
146 typedef TAILQ_HEAD(edid_resolution, resolution) edid_res_list_t;
147 
148 struct vesa_flat_panel_info {
149 	uint16_t HSize;			/* Horizontal Size in Pixels */
150 	uint16_t VSize;			/* Vertical Size in Lines */
151 	uint16_t FPType;		/* Flat Panel Type */
152 	uint8_t RedBPP;			/* Red Bits Per Primary */
153 	uint8_t GreenBPP;		/* Green Bits Per Primary */
154 	uint8_t BlueBPP;		/* Blue Bits Per Primary */
155 	uint8_t ReservedBPP;		/* Reserved Bits Per Primary */
156 	uint32_t RsvdOffScrnMemSize;	/* Size in KB of Offscreen Memory */
157 	uint32_t RsvdOffScrnMemPtr; /* Pointer to reserved offscreen memory */
158 	uint8_t Reserved[14];		/* remainder of FPInfo */
159 } __packed;
160 
161 #define	COLOR_FORMAT_VGA 0
162 #define	COLOR_FORMAT_RGB 1
163 #define	NCOLORS	16
164 #define	NCMAP	256
165 extern uint32_t cmap[NCMAP];
166 
167 /*
168  * VT_FB_MAX_WIDTH and VT_FB_MAX_HEIGHT are dimensions from where
169  * we will not auto select smaller font than 8x16.
170  * See also sys/dev/vt/vt.h
171  */
172 #ifndef VT_FB_MAX_WIDTH
173 #define	VT_FB_MAX_WIDTH		4096
174 #endif
175 #ifndef VT_FB_MAX_HEIGHT
176 #define	VT_FB_MAX_HEIGHT	2400
177 #endif
178 
179 enum FB_TYPE {
180 	FB_TEXT = -1,
181 	FB_GOP,
182 	FB_UGA,
183 	FB_VBE
184 };
185 
186 enum COLOR_TYPE {
187 	CT_INDEXED,
188 	CT_RGB
189 };
190 
191 struct gen_fb {
192 	uint64_t	fb_addr;
193 	uint64_t	fb_size;
194 	uint32_t	fb_height;
195 	uint32_t	fb_width;
196 	uint32_t	fb_stride;
197 	uint32_t	fb_mask_red;
198 	uint32_t	fb_mask_green;
199 	uint32_t	fb_mask_blue;
200 	uint32_t	fb_mask_reserved;
201 	uint32_t	fb_bpp;
202 };
203 
204 typedef struct teken_gfx {
205 	enum FB_TYPE	tg_fb_type;
206 	enum COLOR_TYPE tg_ctype;
207 	unsigned	tg_mode;
208 	teken_t		tg_teken;		/* Teken core */
209 	teken_pos_t	tg_cursor;		/* Where cursor was drawn */
210 	bool		tg_cursor_visible;
211 	teken_pos_t	tg_tp;			/* Terminal dimensions */
212 	teken_pos_t	tg_origin;		/* Point of origin in pixels */
213 	uint8_t		*tg_glyph;		/* Memory for glyph */
214 	size_t		tg_glyph_size;
215 	struct vt_font	tg_font;
216 	struct gen_fb	tg_fb;
217 	uint32_t	*tg_shadow_fb;		/* units of 4 bytes */
218 	size_t		tg_shadow_sz;		/* units of pages */
219 	teken_funcs_t	*tg_functions;
220 	void		*tg_private;
221 } teken_gfx_t;
222 
223 extern font_list_t fonts;
224 extern teken_gfx_t gfx_state;
225 
226 typedef enum {
227 	GfxFbBltVideoFill,
228 	GfxFbBltVideoToBltBuffer,
229 	GfxFbBltBufferToVideo,
230 	GfxFbBltVideoToVideo,
231 	GfxFbBltOperationMax,
232 } GFXFB_BLT_OPERATION;
233 
234 int gfxfb_blt(void *, GFXFB_BLT_OPERATION, uint32_t, uint32_t,
235     uint32_t, uint32_t, uint32_t, uint32_t, uint32_t);
236 
237 int generate_cons_palette(uint32_t *, int, uint32_t, int, uint32_t, int,
238     uint32_t, int);
239 bool console_update_mode(bool);
240 void setup_font(teken_gfx_t *, teken_unit_t, teken_unit_t);
241 uint8_t *font_lookup(const struct vt_font *, teken_char_t,
242     const teken_attr_t *);
243 void bios_text_font(bool);
244 
245 /* teken callbacks. */
246 tf_cursor_t gfx_fb_cursor;
247 tf_putchar_t gfx_fb_putchar;
248 tf_fill_t gfx_fb_fill;
249 tf_copy_t gfx_fb_copy;
250 tf_param_t gfx_fb_param;
251 
252 /* Screen buffer element */
253 struct text_pixel {
254 	teken_char_t c;
255 	teken_attr_t a;
256 };
257 
258 extern const int cons_to_vga_colors[NCOLORS];
259 
260 /* Screen buffer to track changes on the terminal screen. */
261 extern struct text_pixel *screen_buffer;
262 bool is_same_pixel(struct text_pixel *, struct text_pixel *);
263 
264 bool gfx_get_edid_resolution(struct vesa_edid_info *, edid_res_list_t *);
265 void gfx_framework_init(void);
266 void gfx_fb_cons_display(uint32_t, uint32_t, uint32_t, uint32_t, void *);
267 void gfx_fb_setpixel(uint32_t, uint32_t);
268 void gfx_fb_drawrect(uint32_t, uint32_t, uint32_t, uint32_t, uint32_t);
269 void gfx_term_drawrect(uint32_t, uint32_t, uint32_t, uint32_t);
270 void gfx_fb_line(uint32_t, uint32_t, uint32_t, uint32_t, uint32_t);
271 void gfx_fb_bezier(uint32_t, uint32_t, uint32_t, uint32_t, uint32_t, uint32_t,
272 	uint32_t);
273 
274 #define	FL_PUTIMAGE_BORDER	0x1
275 #define	FL_PUTIMAGE_NOSCROLL	0x2
276 #define	FL_PUTIMAGE_DEBUG	0x80
277 
278 int gfx_fb_putimage(png_t *, uint32_t, uint32_t, uint32_t, uint32_t, uint32_t);
279 bool gfx_parse_mode_str(char *, int *, int *, int *);
280 void term_image_display(teken_gfx_t *, const teken_rect_t *);
281 
282 void reset_font_flags(void);
283 
284 #ifdef __cplusplus
285 }
286 #endif
287 
288 #endif /* _GFX_FB_H */
289