2 * SPDX-License-Identifier: BSD-2-Clause
4 * Copyright 2020 Toomas Soome
5 * Copyright 2019 OmniOS Community Edition (OmniOSce) Association.
6 * Copyright 2020 RackTop Systems, Inc.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31 * The workhorse here is gfxfb_blt(). It is implemented to mimic UEFI
32 * GOP Blt, and allows us to fill the rectangle on screen, copy
33 * rectangle from video to buffer and buffer to video and video to video.
34 * Such implementation does allow us to have almost identical implementation
35 * for both BIOS VBE and UEFI.
37 * ALL pixel data is assumed to be 32-bit BGRA (byte order Blue, Green, Red,
38 * Alpha) format, this allows us to only handle RGB data and not to worry
39 * about mixing RGB with indexed colors.
40 * Data exchange between memory buffer and video will translate BGRA
41 * and native format as following:
43 * 32-bit to/from 32-bit is trivial case.
44 * 32-bit to/from 24-bit is also simple - we just drop the alpha channel.
45 * 32-bit to/from 16-bit is more complicated, because we nee to handle
46 * data loss from 32-bit to 16-bit. While reading/writing from/to video, we
47 * need to apply masks of 16-bit color components. This will preserve
48 * colors for terminal text. For 32-bit truecolor PMG images, we need to
49 * translate 32-bit colors to 15/16 bit colors and this means data loss.
50 * There are different algorithms how to perform such color space reduction,
51 * we are currently using bitwise right shift to reduce color space and so far
52 * this technique seems to be sufficient (see also gfx_fb_putimage(), the
54 * 32-bit to/from 8-bit is the most troublesome because 8-bit colors are
55 * indexed. From video, we do get color indexes, and we do translate
56 * color index values to RGB. To write to video, we again need to translate
57 * RGB to color index. Additionally, we need to translate between VGA and
60 * Our internal color data is represented using BGRA format. But the hardware
61 * used indexed colors for 8-bit colors (0-255) and for this mode we do
62 * need to perform translation to/from BGRA and index values.
64 * - paletteentry RGB <-> index -
65 * BGRA BUFFER <----/ \ - VIDEO
69 * To perform index to RGB translation, we use palette table generated
70 * from when we set up 8-bit mode video. We cannot read palette data from
71 * the hardware, because not all hardware supports reading it.
73 * BGRA to index is implemented in rgb_to_color_index() by searching
74 * palette array for closest match of RBG values.
76 * Note: In 8-bit mode, We do store first 16 colors to palette registers
77 * in VGA color order, this serves two purposes; firstly,
78 * if palette update is not supported, we still have correct 16 colors.
79 * Secondly, the kernel does get correct 16 colors when some other boot
80 * loader is used. However, the palette map for 8-bit colors is using
81 * console color ordering - this does allow us to skip translation
82 * from VGA colors to console colors, while we are reading RGB data.
85 #include <sys/param.h>
90 #include <sys/linker.h>
91 #include <sys/module.h>
92 #include <sys/stdint.h>
93 #include <sys/endian.h>
95 #include <bootstrap.h>
104 /* VGA text mode does use bold font. */
105 #if !defined(VGA_8X16_FONT)
106 #define VGA_8X16_FONT "/boot/fonts/8x16b.fnt"
108 #if !defined(DEFAULT_8X16_FONT)
109 #define DEFAULT_8X16_FONT "/boot/fonts/8x16.fnt"
113 * Must be sorted by font size in descending order
115 font_list_t fonts = STAILQ_HEAD_INITIALIZER(fonts);
117 #define DEFAULT_FONT_DATA font_data_8x16
118 extern vt_font_bitmap_data_t font_data_8x16;
119 teken_gfx_t gfx_state = { 0 };
122 unsigned char r; /* Red percentage value. */
123 unsigned char g; /* Green percentage value. */
124 unsigned char b; /* Blue percentage value. */
125 } color_def[NCOLORS] = {
126 {0, 0, 0}, /* black */
127 {50, 0, 0}, /* dark red */
128 {0, 50, 0}, /* dark green */
129 {77, 63, 0}, /* dark yellow */
130 {20, 40, 64}, /* dark blue */
131 {50, 0, 50}, /* dark magenta */
132 {0, 50, 50}, /* dark cyan */
133 {75, 75, 75}, /* light gray */
135 {18, 20, 21}, /* dark gray */
136 {100, 0, 0}, /* light red */
137 {0, 100, 0}, /* light green */
138 {100, 100, 0}, /* light yellow */
139 {45, 62, 81}, /* light blue */
140 {100, 0, 100}, /* light magenta */
141 {0, 100, 100}, /* light cyan */
142 {100, 100, 100}, /* white */
144 uint32_t cmap[NCMAP];
147 * Between console's palette and VGA's one:
148 * - blue and red are swapped (1 <-> 4)
149 * - yellow and cyan are swapped (3 <-> 6)
151 const int cons_to_vga_colors[NCOLORS] = {
152 0, 4, 2, 6, 1, 5, 3, 7,
153 8, 12, 10, 14, 9, 13, 11, 15
156 static const int vga_to_cons_colors[NCOLORS] = {
157 0, 1, 2, 3, 4, 5, 6, 7,
158 8, 9, 10, 11, 12, 13, 14, 15
161 struct text_pixel *screen_buffer;
163 static EFI_GRAPHICS_OUTPUT_BLT_PIXEL *GlyphBuffer;
165 static struct paletteentry *GlyphBuffer;
167 static size_t GlyphBufferSize;
169 static bool insert_font(char *, FONT_FLAGS);
170 static int font_set(struct env_var *, int, const void *);
171 static void * allocate_glyphbuffer(uint32_t, uint32_t);
172 static void gfx_fb_cursor_draw(teken_gfx_t *, const teken_pos_t *, bool);
175 * Initialize gfx framework.
178 gfx_framework_init(void)
181 * Setup font list to have builtin font.
183 (void) insert_font(NULL, FONT_BUILTIN);
184 gfx_interp_ref(); /* Draw in the gfx interpreter for this thing */
188 gfx_get_fb_address(void)
190 return (ptov((uint32_t)gfx_state.tg_fb.fb_addr));
194 * Utility function to parse gfx mode line strings.
197 gfx_parse_mode_str(char *str, int *x, int *y, int *depth)
203 *x = strtoul(p, &end, 0);
204 if (*x == 0 || errno != 0)
209 *y = strtoul(p, &end, 0);
210 if (*y == 0 || errno != 0)
213 *depth = -1; /* auto select */
216 *depth = strtoul(p, &end, 0);
217 if (*depth == 0 || errno != 0 || *end != '\0')
225 rgb_color_map(uint8_t index, uint32_t rmax, int roffset,
226 uint32_t gmax, int goffset, uint32_t bmax, int boffset)
228 uint32_t color, code, gray, level;
230 if (index < NCOLORS) {
231 #define CF(_f, _i) ((_f ## max * color_def[(_i)]._f / 100) << _f ## offset)
232 return (CF(r, index) | CF(g, index) | CF(b, index));
236 #define CF(_f, _c) ((_f ## max & _c) << _f ## offset)
237 /* 6x6x6 color cube */
238 if (index > 15 && index < 232) {
239 uint32_t red, green, blue;
241 for (red = 0; red < 6; red++) {
242 for (green = 0; green < 6; green++) {
243 for (blue = 0; blue < 6; blue++) {
244 code = 16 + (red * 36) +
248 red = red ? (red * 40 + 55) : 0;
249 green = green ? (green * 40 + 55) : 0;
250 blue = blue ? (blue * 40 + 55) : 0;
252 color |= CF(g, green);
253 color |= CF(b, blue);
260 /* colors 232-255 are a grayscale ramp */
261 for (gray = 0; gray < 24; gray++) {
262 level = (gray * 10) + 8;
267 return (CF(r, level) | CF(g, level) | CF(b, level));
272 * Support for color mapping.
273 * For 8, 24 and 32 bit depth, use mask size 8.
274 * 15/16 bit depth needs to use mask size from mode,
275 * or we will lose color information from 32-bit to 15/16 bit translation.
278 gfx_fb_color_map(uint8_t index)
280 int rmask, gmask, bmask;
281 int roff, goff, boff, bpp;
283 roff = ffs(gfx_state.tg_fb.fb_mask_red) - 1;
284 goff = ffs(gfx_state.tg_fb.fb_mask_green) - 1;
285 boff = ffs(gfx_state.tg_fb.fb_mask_blue) - 1;
286 bpp = roundup2(gfx_state.tg_fb.fb_bpp, 8) >> 3;
289 rmask = gfx_state.tg_fb.fb_mask_red >> roff;
294 gmask = gfx_state.tg_fb.fb_mask_green >> goff;
299 bmask = gfx_state.tg_fb.fb_mask_blue >> boff;
303 return (rgb_color_map(index, rmask, 16, gmask, 8, bmask, 0));
307 * Get indexed color from RGB. This function is used to write data to video
308 * memory when the adapter is set to use indexed colors.
309 * Since UEFI does only support 32-bit colors, we do not implement it for
310 * UEFI because there is no need for it and we do not have palette array
314 rgb_to_color_index(uint8_t r, uint8_t g, uint8_t b)
317 uint32_t color, best, dist, k;
321 best = 255 * 255 * 255;
322 for (k = 0; k < NCMAP; k++) {
323 diff = r - pe8[k].Red;
325 diff = g - pe8[k].Green;
327 diff = b - pe8[k].Blue;
330 /* Exact match, exit the loop */
351 generate_cons_palette(uint32_t *palette, int format,
352 uint32_t rmax, int roffset, uint32_t gmax, int goffset,
353 uint32_t bmax, int boffset)
358 case COLOR_FORMAT_VGA:
359 for (i = 0; i < NCOLORS; i++)
360 palette[i] = cons_to_vga_colors[i];
361 for (; i < NCMAP; i++)
364 case COLOR_FORMAT_RGB:
365 for (i = 0; i < NCMAP; i++)
366 palette[i] = rgb_color_map(i, rmax, roffset,
367 gmax, goffset, bmax, boffset);
377 gfx_mem_wr1(uint8_t *base, size_t size, uint32_t o, uint8_t v)
382 *(uint8_t *)(base + o) = v;
386 gfx_mem_wr2(uint8_t *base, size_t size, uint32_t o, uint16_t v)
391 *(uint16_t *)(base + o) = v;
395 gfx_mem_wr4(uint8_t *base, size_t size, uint32_t o, uint32_t v)
400 *(uint32_t *)(base + o) = v;
403 static int gfxfb_blt_fill(void *BltBuffer,
404 uint32_t DestinationX, uint32_t DestinationY,
405 uint32_t Width, uint32_t Height)
408 EFI_GRAPHICS_OUTPUT_BLT_PIXEL *p;
410 struct paletteentry *p;
412 uint32_t data, bpp, pitch, y, x;
413 int roff, goff, boff;
416 uint8_t *destination;
418 if (BltBuffer == NULL)
421 if (DestinationY + Height > gfx_state.tg_fb.fb_height)
424 if (DestinationX + Width > gfx_state.tg_fb.fb_width)
427 if (Width == 0 || Height == 0)
431 roff = ffs(gfx_state.tg_fb.fb_mask_red) - 1;
432 goff = ffs(gfx_state.tg_fb.fb_mask_green) - 1;
433 boff = ffs(gfx_state.tg_fb.fb_mask_blue) - 1;
435 if (gfx_state.tg_fb.fb_bpp == 8) {
436 data = rgb_to_color_index(p->Red, p->Green, p->Blue);
439 (gfx_state.tg_fb.fb_mask_red >> roff)) << roff;
441 (gfx_state.tg_fb.fb_mask_green >> goff)) << goff;
443 (gfx_state.tg_fb.fb_mask_blue >> boff)) << boff;
446 bpp = roundup2(gfx_state.tg_fb.fb_bpp, 8) >> 3;
447 pitch = gfx_state.tg_fb.fb_stride * bpp;
448 destination = gfx_get_fb_address();
449 size = gfx_state.tg_fb.fb_size;
451 for (y = DestinationY; y < Height + DestinationY; y++) {
452 off = y * pitch + DestinationX * bpp;
453 for (x = 0; x < Width; x++) {
456 gfx_mem_wr1(destination, size, off,
458 cons_to_vga_colors[data] : data);
461 gfx_mem_wr2(destination, size, off, data);
464 gfx_mem_wr1(destination, size, off,
465 (data >> 16) & 0xff);
466 gfx_mem_wr1(destination, size, off + 1,
468 gfx_mem_wr1(destination, size, off + 2,
472 gfx_mem_wr4(destination, size, off, data);
485 gfxfb_blt_video_to_buffer(void *BltBuffer, uint32_t SourceX, uint32_t SourceY,
486 uint32_t DestinationX, uint32_t DestinationY,
487 uint32_t Width, uint32_t Height, uint32_t Delta)
490 EFI_GRAPHICS_OUTPUT_BLT_PIXEL *p;
492 struct paletteentry *p;
495 uint32_t bpp, pitch, copybytes;
497 uint8_t *source, *destination, *sb;
498 uint8_t rm, rp, gm, gp, bm, bp;
501 if (BltBuffer == NULL)
504 if (SourceY + Height >
505 gfx_state.tg_fb.fb_height)
508 if (SourceX + Width > gfx_state.tg_fb.fb_width)
511 if (Width == 0 || Height == 0)
515 Delta = Width * sizeof (*p);
517 bpp = roundup2(gfx_state.tg_fb.fb_bpp, 8) >> 3;
518 pitch = gfx_state.tg_fb.fb_stride * bpp;
520 copybytes = Width * bpp;
522 rp = ffs(gfx_state.tg_fb.fb_mask_red) - 1;
523 gp = ffs(gfx_state.tg_fb.fb_mask_green) - 1;
524 bp = ffs(gfx_state.tg_fb.fb_mask_blue) - 1;
525 rm = gfx_state.tg_fb.fb_mask_red >> rp;
526 gm = gfx_state.tg_fb.fb_mask_green >> gp;
527 bm = gfx_state.tg_fb.fb_mask_blue >> bp;
529 /* If FB pixel format is BGRA, we can use direct copy. */
531 ffs(rm) - 1 == 8 && rp == 16 &&
532 ffs(gm) - 1 == 8 && gp == 8 &&
533 ffs(bm) - 1 == 8 && bp == 0;
535 for (sy = SourceY, dy = DestinationY; dy < Height + DestinationY;
537 off = sy * pitch + SourceX * bpp;
538 source = gfx_get_fb_address() + off;
539 destination = (uint8_t *)BltBuffer + dy * Delta +
540 DestinationX * sizeof (*p);
543 bcopy(source, destination, copybytes);
545 for (x = 0; x < Width; x++) {
548 p = (void *)(destination + x * sizeof (*p));
549 sb = source + x * bpp;
558 c = sb[0] << 16 | sb[1] << 8 | sb[2];
568 *(uint32_t *)p = gfx_fb_color_map(
570 vga_to_cons_colors[c] : c);
572 p->Red = (c >> rp) & rm;
573 p->Green = (c >> gp) & gm;
574 p->Blue = (c >> bp) & bm;
585 gfxfb_blt_buffer_to_video(void *BltBuffer, uint32_t SourceX, uint32_t SourceY,
586 uint32_t DestinationX, uint32_t DestinationY,
587 uint32_t Width, uint32_t Height, uint32_t Delta)
590 EFI_GRAPHICS_OUTPUT_BLT_PIXEL *p;
592 struct paletteentry *p;
595 uint32_t bpp, pitch, copybytes;
597 uint8_t *source, *destination;
598 uint8_t rm, rp, gm, gp, bm, bp;
601 if (BltBuffer == NULL)
604 if (DestinationY + Height >
605 gfx_state.tg_fb.fb_height)
608 if (DestinationX + Width > gfx_state.tg_fb.fb_width)
611 if (Width == 0 || Height == 0)
615 Delta = Width * sizeof (*p);
617 bpp = roundup2(gfx_state.tg_fb.fb_bpp, 8) >> 3;
618 pitch = gfx_state.tg_fb.fb_stride * bpp;
620 copybytes = Width * bpp;
622 rp = ffs(gfx_state.tg_fb.fb_mask_red) - 1;
623 gp = ffs(gfx_state.tg_fb.fb_mask_green) - 1;
624 bp = ffs(gfx_state.tg_fb.fb_mask_blue) - 1;
625 rm = gfx_state.tg_fb.fb_mask_red >> rp;
626 gm = gfx_state.tg_fb.fb_mask_green >> gp;
627 bm = gfx_state.tg_fb.fb_mask_blue >> bp;
629 /* If FB pixel format is BGRA, we can use direct copy. */
631 ffs(rm) - 1 == 8 && rp == 16 &&
632 ffs(gm) - 1 == 8 && gp == 8 &&
633 ffs(bm) - 1 == 8 && bp == 0;
635 for (sy = SourceY, dy = DestinationY; sy < Height + SourceY;
637 off = dy * pitch + DestinationX * bpp;
638 destination = gfx_get_fb_address() + off;
641 source = (uint8_t *)BltBuffer + sy * Delta +
642 SourceX * sizeof (*p);
643 bcopy(source, destination, copybytes);
645 for (x = 0; x < Width; x++) {
648 p = (void *)((uint8_t *)BltBuffer +
650 (SourceX + x) * sizeof (*p));
652 c = rgb_to_color_index(p->Red,
655 c = (p->Red & rm) << rp |
656 (p->Green & gm) << gp |
657 (p->Blue & bm) << bp;
662 gfx_mem_wr1(destination, copybytes,
664 cons_to_vga_colors[c] : c);
667 gfx_mem_wr2(destination, copybytes,
671 gfx_mem_wr1(destination, copybytes,
672 off, (c >> 16) & 0xff);
673 gfx_mem_wr1(destination, copybytes,
674 off + 1, (c >> 8) & 0xff);
675 gfx_mem_wr1(destination, copybytes,
679 gfx_mem_wr4(destination, copybytes,
693 gfxfb_blt_video_to_video(uint32_t SourceX, uint32_t SourceY,
694 uint32_t DestinationX, uint32_t DestinationY,
695 uint32_t Width, uint32_t Height)
697 uint32_t bpp, copybytes;
699 uint8_t *source, *destination;
702 if (SourceY + Height >
703 gfx_state.tg_fb.fb_height)
706 if (SourceX + Width > gfx_state.tg_fb.fb_width)
709 if (DestinationY + Height >
710 gfx_state.tg_fb.fb_height)
713 if (DestinationX + Width > gfx_state.tg_fb.fb_width)
716 if (Width == 0 || Height == 0)
719 bpp = roundup2(gfx_state.tg_fb.fb_bpp, 8) >> 3;
720 pitch = gfx_state.tg_fb.fb_stride * bpp;
722 copybytes = Width * bpp;
724 off = SourceY * pitch + SourceX * bpp;
725 source = gfx_get_fb_address() + off;
726 off = DestinationY * pitch + DestinationX * bpp;
727 destination = gfx_get_fb_address() + off;
729 if ((uintptr_t)destination > (uintptr_t)source) {
730 source += Height * pitch;
731 destination += Height * pitch;
735 while (Height-- > 0) {
736 bcopy(source, destination, copybytes);
738 destination += pitch;
745 gfxfb_shadow_fill(uint32_t *BltBuffer,
746 uint32_t DestinationX, uint32_t DestinationY,
747 uint32_t Width, uint32_t Height)
751 if (gfx_state.tg_shadow_fb == NULL)
754 fbX = gfx_state.tg_fb.fb_width;
755 fbY = gfx_state.tg_fb.fb_height;
757 if (BltBuffer == NULL)
760 if (DestinationX + Width > fbX)
761 Width = fbX - DestinationX;
763 if (DestinationY + Height > fbY)
764 Height = fbY - DestinationY;
766 uint32_t y2 = Height + DestinationY;
767 for (uint32_t y1 = DestinationY; y1 < y2; y1++) {
768 uint32_t off = y1 * fbX + DestinationX;
770 for (uint32_t x = 0; x < Width; x++) {
771 gfx_state.tg_shadow_fb[off + x] = *BltBuffer;
777 gfxfb_blt(void *BltBuffer, GFXFB_BLT_OPERATION BltOperation,
778 uint32_t SourceX, uint32_t SourceY,
779 uint32_t DestinationX, uint32_t DestinationY,
780 uint32_t Width, uint32_t Height, uint32_t Delta)
785 EFI_GRAPHICS_OUTPUT *gop = gfx_state.tg_private;
789 * We assume Blt() does work, if not, we will need to build exception
790 * list case by case. We only have boot services during part of our
791 * exectution. Once terminate boot services, these operations cannot be
792 * done as they are provided by protocols that disappear when exit
795 if (gop != NULL && boot_services_active) {
796 tpl = BS->RaiseTPL(TPL_NOTIFY);
797 switch (BltOperation) {
798 case GfxFbBltVideoFill:
799 gfxfb_shadow_fill(BltBuffer, DestinationX,
800 DestinationY, Width, Height);
801 status = gop->Blt(gop, BltBuffer, EfiBltVideoFill,
802 SourceX, SourceY, DestinationX, DestinationY,
803 Width, Height, Delta);
806 case GfxFbBltVideoToBltBuffer:
807 status = gop->Blt(gop, BltBuffer,
808 EfiBltVideoToBltBuffer,
809 SourceX, SourceY, DestinationX, DestinationY,
810 Width, Height, Delta);
813 case GfxFbBltBufferToVideo:
814 status = gop->Blt(gop, BltBuffer, EfiBltBufferToVideo,
815 SourceX, SourceY, DestinationX, DestinationY,
816 Width, Height, Delta);
819 case GfxFbBltVideoToVideo:
820 status = gop->Blt(gop, BltBuffer, EfiBltVideoToVideo,
821 SourceX, SourceY, DestinationX, DestinationY,
822 Width, Height, Delta);
826 status = EFI_INVALID_PARAMETER;
835 case EFI_INVALID_PARAMETER:
839 case EFI_DEVICE_ERROR:
850 switch (BltOperation) {
851 case GfxFbBltVideoFill:
852 gfxfb_shadow_fill(BltBuffer, DestinationX, DestinationY,
854 rv = gfxfb_blt_fill(BltBuffer, DestinationX, DestinationY,
858 case GfxFbBltVideoToBltBuffer:
859 rv = gfxfb_blt_video_to_buffer(BltBuffer, SourceX, SourceY,
860 DestinationX, DestinationY, Width, Height, Delta);
863 case GfxFbBltBufferToVideo:
864 rv = gfxfb_blt_buffer_to_video(BltBuffer, SourceX, SourceY,
865 DestinationX, DestinationY, Width, Height, Delta);
868 case GfxFbBltVideoToVideo:
869 rv = gfxfb_blt_video_to_video(SourceX, SourceY,
870 DestinationX, DestinationY, Width, Height);
881 gfx_bitblt_bitmap(teken_gfx_t *state, const uint8_t *glyph,
882 const teken_attr_t *a, uint32_t alpha, bool cursor)
884 uint32_t width, height;
885 uint32_t fgc, bgc, bpl, cc, o;
889 bpp = 4; /* We only generate BGRA */
890 width = state->tg_font.vf_width;
891 height = state->tg_font.vf_height;
892 bpl = (width + 7) / 8; /* Bytes per source line. */
896 if (a->ta_format & TF_BOLD)
898 if (a->ta_format & TF_BLINK)
901 fgc = gfx_fb_color_map(fgc);
902 bgc = gfx_fb_color_map(bgc);
904 if (a->ta_format & TF_REVERSE)
920 for (uint32_t y = 0; y < height; y++) {
921 for (uint32_t x = 0; x < width; x++) {
922 byte = y * bpl + x / 8;
923 bit = 0x80 >> (x % 8);
924 o = y * width * bpp + x * bpp;
925 cc = glyph[byte] & bit ? fgc : bgc;
927 gfx_mem_wr4(state->tg_glyph,
928 state->tg_glyph_size, o, cc);
934 * Draw prepared glyph on terminal point p.
937 gfx_fb_printchar(teken_gfx_t *state, const teken_pos_t *p)
939 unsigned x, y, width, height;
941 width = state->tg_font.vf_width;
942 height = state->tg_font.vf_height;
943 x = state->tg_origin.tp_col + p->tp_col * width;
944 y = state->tg_origin.tp_row + p->tp_row * height;
946 gfx_fb_cons_display(x, y, width, height, state->tg_glyph);
950 * Store char with its attribute to buffer and put it on screen.
953 gfx_fb_putchar(void *arg, const teken_pos_t *p, teken_char_t c,
954 const teken_attr_t *a)
956 teken_gfx_t *state = arg;
957 const uint8_t *glyph;
960 idx = p->tp_col + p->tp_row * state->tg_tp.tp_col;
961 if (idx >= state->tg_tp.tp_col * state->tg_tp.tp_row)
964 /* remove the cursor */
965 if (state->tg_cursor_visible)
966 gfx_fb_cursor_draw(state, &state->tg_cursor, false);
968 screen_buffer[idx].c = c;
969 screen_buffer[idx].a = *a;
971 glyph = font_lookup(&state->tg_font, c, a);
972 gfx_bitblt_bitmap(state, glyph, a, 0xff, false);
973 gfx_fb_printchar(state, p);
975 /* display the cursor */
976 if (state->tg_cursor_visible) {
977 const teken_pos_t *c;
979 c = teken_get_cursor(&state->tg_teken);
980 gfx_fb_cursor_draw(state, c, true);
985 gfx_fb_fill(void *arg, const teken_rect_t *r, teken_char_t c,
986 const teken_attr_t *a)
988 teken_gfx_t *state = arg;
989 const uint8_t *glyph;
991 struct text_pixel *row;
993 /* remove the cursor */
994 if (state->tg_cursor_visible)
995 gfx_fb_cursor_draw(state, &state->tg_cursor, false);
997 glyph = font_lookup(&state->tg_font, c, a);
998 gfx_bitblt_bitmap(state, glyph, a, 0xff, false);
1000 for (p.tp_row = r->tr_begin.tp_row; p.tp_row < r->tr_end.tp_row;
1002 row = &screen_buffer[p.tp_row * state->tg_tp.tp_col];
1003 for (p.tp_col = r->tr_begin.tp_col;
1004 p.tp_col < r->tr_end.tp_col; p.tp_col++) {
1005 row[p.tp_col].c = c;
1006 row[p.tp_col].a = *a;
1007 gfx_fb_printchar(state, &p);
1011 /* display the cursor */
1012 if (state->tg_cursor_visible) {
1013 const teken_pos_t *c;
1015 c = teken_get_cursor(&state->tg_teken);
1016 gfx_fb_cursor_draw(state, c, true);
1021 gfx_fb_cursor_draw(teken_gfx_t *state, const teken_pos_t *pos, bool on)
1023 const uint8_t *glyph;
1028 if (p.tp_col >= state->tg_tp.tp_col)
1029 p.tp_col = state->tg_tp.tp_col - 1;
1030 if (p.tp_row >= state->tg_tp.tp_row)
1031 p.tp_row = state->tg_tp.tp_row - 1;
1032 idx = p.tp_col + p.tp_row * state->tg_tp.tp_col;
1033 if (idx >= state->tg_tp.tp_col * state->tg_tp.tp_row)
1036 glyph = font_lookup(&state->tg_font, screen_buffer[idx].c,
1037 &screen_buffer[idx].a);
1038 gfx_bitblt_bitmap(state, glyph, &screen_buffer[idx].a, 0xff, on);
1039 gfx_fb_printchar(state, &p);
1041 state->tg_cursor = p;
1045 gfx_fb_cursor(void *arg, const teken_pos_t *p)
1047 teken_gfx_t *state = arg;
1049 /* Switch cursor off in old location and back on in new. */
1050 if (state->tg_cursor_visible) {
1051 gfx_fb_cursor_draw(state, &state->tg_cursor, false);
1052 gfx_fb_cursor_draw(state, p, true);
1057 gfx_fb_param(void *arg, int cmd, unsigned int value)
1059 teken_gfx_t *state = arg;
1060 const teken_pos_t *c;
1063 case TP_SETLOCALCURSOR:
1065 * 0 means normal (usually block), 1 means hidden, and
1066 * 2 means blinking (always block) for compatibility with
1067 * syscons. We don't support any changes except hiding,
1068 * so must map 2 to 0.
1070 value = (value == 1) ? 0 : 1;
1073 c = teken_get_cursor(&state->tg_teken);
1074 gfx_fb_cursor_draw(state, c, true);
1076 state->tg_cursor_visible = true;
1078 state->tg_cursor_visible = false;
1081 /* Not yet implemented */
1087 is_same_pixel(struct text_pixel *px1, struct text_pixel *px2)
1089 if (px1->c != px2->c)
1092 /* Is there image stored? */
1093 if ((px1->a.ta_format & TF_IMAGE) ||
1094 (px2->a.ta_format & TF_IMAGE))
1097 if (px1->a.ta_format != px2->a.ta_format)
1099 if (px1->a.ta_fgcolor != px2->a.ta_fgcolor)
1101 if (px1->a.ta_bgcolor != px2->a.ta_bgcolor)
1108 gfx_fb_copy_area(teken_gfx_t *state, const teken_rect_t *s,
1109 const teken_pos_t *d)
1111 uint32_t sx, sy, dx, dy, width, height;
1112 uint32_t pitch, bytes;
1115 width = state->tg_font.vf_width;
1116 height = state->tg_font.vf_height;
1118 sx = s->tr_begin.tp_col * width;
1119 sy = s->tr_begin.tp_row * height;
1120 dx = d->tp_col * width;
1121 dy = d->tp_row * height;
1123 width *= (s->tr_end.tp_col - s->tr_begin.tp_col + 1);
1126 * With no shadow fb, use video to video copy.
1128 if (state->tg_shadow_fb == NULL) {
1129 (void) gfxfb_blt(NULL, GfxFbBltVideoToVideo,
1130 sx + state->tg_origin.tp_col,
1131 sy + state->tg_origin.tp_row,
1132 dx + state->tg_origin.tp_col,
1133 dy + state->tg_origin.tp_row,
1139 * With shadow fb, we need to copy data on both shadow and video,
1140 * to preserve the consistency. We only read data from shadow fb.
1144 pitch = state->tg_fb.fb_width;
1145 bytes = width * sizeof (*state->tg_shadow_fb);
1148 * To handle overlapping areas, set up reverse copy here.
1150 if (dy * pitch + dx > sy * pitch + sx) {
1156 while (height-- > 0) {
1157 uint32_t *source = &state->tg_shadow_fb[sy * pitch + sx];
1158 uint32_t *destination = &state->tg_shadow_fb[dy * pitch + dx];
1160 bcopy(source, destination, bytes);
1161 (void) gfxfb_blt(destination, GfxFbBltBufferToVideo,
1162 0, 0, dx + state->tg_origin.tp_col,
1163 dy + state->tg_origin.tp_row, width, 1, 0);
1171 gfx_fb_copy_line(teken_gfx_t *state, int ncol, teken_pos_t *s, teken_pos_t *d)
1175 unsigned soffset, doffset;
1179 soffset = s->tp_col + s->tp_row * state->tg_tp.tp_col;
1180 doffset = d->tp_col + d->tp_row * state->tg_tp.tp_col;
1182 for (x = 0; x < ncol; x++) {
1183 if (is_same_pixel(&screen_buffer[soffset + x],
1184 &screen_buffer[doffset + x])) {
1186 gfx_fb_copy_area(state, &sr, &dp);
1190 screen_buffer[doffset + x] = screen_buffer[soffset + x];
1192 /* update end point */
1193 sr.tr_end.tp_col = s->tp_col + x;
1195 /* set up new rectangle */
1197 sr.tr_begin.tp_col = s->tp_col + x;
1198 sr.tr_begin.tp_row = s->tp_row;
1199 sr.tr_end.tp_col = s->tp_col + x;
1200 sr.tr_end.tp_row = s->tp_row;
1201 dp.tp_col = d->tp_col + x;
1202 dp.tp_row = d->tp_row;
1207 gfx_fb_copy_area(state, &sr, &dp);
1212 gfx_fb_copy(void *arg, const teken_rect_t *r, const teken_pos_t *p)
1214 teken_gfx_t *state = arg;
1215 unsigned doffset, soffset;
1217 int nrow, ncol, y; /* Has to be signed - >= 0 comparison */
1220 * Copying is a little tricky. We must make sure we do it in
1221 * correct order, to make sure we don't overwrite our own data.
1224 nrow = r->tr_end.tp_row - r->tr_begin.tp_row;
1225 ncol = r->tr_end.tp_col - r->tr_begin.tp_col;
1227 if (p->tp_row + nrow > state->tg_tp.tp_row ||
1228 p->tp_col + ncol > state->tg_tp.tp_col)
1231 soffset = r->tr_begin.tp_col + r->tr_begin.tp_row * state->tg_tp.tp_col;
1232 doffset = p->tp_col + p->tp_row * state->tg_tp.tp_col;
1234 /* remove the cursor */
1235 if (state->tg_cursor_visible)
1236 gfx_fb_cursor_draw(state, &state->tg_cursor, false);
1239 * Copy line by line.
1241 if (doffset <= soffset) {
1244 for (y = 0; y < nrow; y++) {
1245 s.tp_row = r->tr_begin.tp_row + y;
1246 d.tp_row = p->tp_row + y;
1248 gfx_fb_copy_line(state, ncol, &s, &d);
1251 for (y = nrow - 1; y >= 0; y--) {
1252 s.tp_row = r->tr_begin.tp_row + y;
1253 d.tp_row = p->tp_row + y;
1255 gfx_fb_copy_line(state, ncol, &s, &d);
1259 /* display the cursor */
1260 if (state->tg_cursor_visible) {
1261 const teken_pos_t *c;
1263 c = teken_get_cursor(&state->tg_teken);
1264 gfx_fb_cursor_draw(state, c, true);
1269 * Implements alpha blending for RGBA data, could use pixels for arguments,
1270 * but byte stream seems more generic.
1271 * The generic alpha blending is:
1272 * blend = alpha * fg + (1.0 - alpha) * bg.
1273 * Since our alpha is not from range [0..1], we scale appropriately.
1276 alpha_blend(uint8_t fg, uint8_t bg, uint8_t alpha)
1278 uint16_t blend, h, l;
1280 /* trivial corner cases */
1285 blend = (alpha * fg + (0xFF - alpha) * bg);
1286 /* Division by 0xFF */
1295 * Implements alpha blending for RGBA data, could use pixels for arguments,
1296 * but byte stream seems more generic.
1297 * The generic alpha blending is:
1298 * blend = alpha * fg + (1.0 - alpha) * bg.
1299 * Since our alpha is not from range [0..1], we scale appropriately.
1302 bitmap_cpy(void *dst, void *src, uint32_t size)
1305 EFI_GRAPHICS_OUTPUT_BLT_PIXEL *ps, *pd;
1307 struct paletteentry *ps, *pd;
1316 * we only implement alpha blending for depth 32.
1318 for (i = 0; i < size; i ++) {
1320 pd[i].Red = alpha_blend(ps[i].Red, pd[i].Red, a);
1321 pd[i].Green = alpha_blend(ps[i].Green, pd[i].Green, a);
1322 pd[i].Blue = alpha_blend(ps[i].Blue, pd[i].Blue, a);
1328 allocate_glyphbuffer(uint32_t width, uint32_t height)
1332 size = sizeof (*GlyphBuffer) * width * height;
1333 if (size != GlyphBufferSize) {
1335 GlyphBuffer = malloc(size);
1336 if (GlyphBuffer == NULL)
1338 GlyphBufferSize = size;
1340 return (GlyphBuffer);
1344 gfx_fb_cons_display(uint32_t x, uint32_t y, uint32_t width, uint32_t height,
1348 EFI_GRAPHICS_OUTPUT_BLT_PIXEL *buf, *p;
1350 struct paletteentry *buf, *p;
1355 * If we do have shadow fb, we will use shadow to render data,
1356 * and copy shadow to video.
1358 if (gfx_state.tg_shadow_fb != NULL) {
1359 uint32_t pitch = gfx_state.tg_fb.fb_width;
1361 /* Copy rectangle line by line. */
1363 for (uint32_t sy = 0; sy < height; sy++) {
1364 buf = (void *)(gfx_state.tg_shadow_fb +
1365 (y - gfx_state.tg_origin.tp_row) * pitch +
1366 x - gfx_state.tg_origin.tp_col);
1367 bitmap_cpy(buf, &p[sy * width], width);
1368 (void) gfxfb_blt(buf, GfxFbBltBufferToVideo,
1369 0, 0, x, y, width, 1, 0);
1376 * Common data to display is glyph, use preallocated
1379 if (gfx_state.tg_glyph_size != GlyphBufferSize)
1380 (void) allocate_glyphbuffer(width, height);
1382 size = width * height * sizeof(*buf);
1383 if (size == GlyphBufferSize)
1390 if (gfxfb_blt(buf, GfxFbBltVideoToBltBuffer, x, y, 0, 0,
1391 width, height, 0) == 0) {
1392 bitmap_cpy(buf, data, width * height);
1393 (void) gfxfb_blt(buf, GfxFbBltBufferToVideo, 0, 0, x, y,
1396 if (buf != GlyphBuffer)
1401 * Public graphics primitives.
1410 /* "bit" starts at the highest power of four <= the argument. */
1415 if (num >= res + bit) {
1417 res = (res >> 1) + bit;
1427 gfx_fb_getcolor(void)
1430 const teken_attr_t *ap;
1432 ap = teken_get_curattr(&gfx_state.tg_teken);
1433 if (ap->ta_format & TF_REVERSE) {
1435 if (ap->ta_format & TF_BLINK)
1439 if (ap->ta_format & TF_BOLD)
1443 return (gfx_fb_color_map(c));
1446 /* set pixel in framebuffer using gfx coordinates */
1448 gfx_fb_setpixel(uint32_t x, uint32_t y)
1452 if (gfx_state.tg_fb_type == FB_TEXT)
1455 c = gfx_fb_getcolor();
1457 if (x >= gfx_state.tg_fb.fb_width ||
1458 y >= gfx_state.tg_fb.fb_height)
1461 gfxfb_blt(&c, GfxFbBltVideoFill, 0, 0, x, y, 1, 1, 0);
1465 * draw rectangle in framebuffer using gfx coordinates.
1468 gfx_fb_drawrect(uint32_t x1, uint32_t y1, uint32_t x2, uint32_t y2,
1473 if (gfx_state.tg_fb_type == FB_TEXT)
1476 c = gfx_fb_getcolor();
1479 gfxfb_blt(&c, GfxFbBltVideoFill, 0, 0, x1, y1, x2 - x1,
1482 gfxfb_blt(&c, GfxFbBltVideoFill, 0, 0, x1, y1, x2 - x1, 1, 0);
1483 gfxfb_blt(&c, GfxFbBltVideoFill, 0, 0, x1, y2, x2 - x1, 1, 0);
1484 gfxfb_blt(&c, GfxFbBltVideoFill, 0, 0, x1, y1, 1, y2 - y1, 0);
1485 gfxfb_blt(&c, GfxFbBltVideoFill, 0, 0, x2, y1, 1, y2 - y1, 0);
1490 gfx_fb_line(uint32_t x0, uint32_t y0, uint32_t x1, uint32_t y1, uint32_t wd)
1493 int err, e2, x2, y2, ed, width;
1495 if (gfx_state.tg_fb_type == FB_TEXT)
1499 sx = x0 < x1? 1 : -1;
1500 sy = y0 < y1? 1 : -1;
1501 dx = x1 > x0? x1 - x0 : x0 - x1;
1502 dy = y1 > y0? y1 - y0 : y0 - y1;
1504 ed = dx + dy == 0 ? 1: isqrt(dx * dx + dy * dy);
1507 gfx_fb_setpixel(x0, y0);
1510 if ((e2 << 1) >= -dx) { /* x step */
1513 while (e2 < ed * width &&
1514 (y1 != (uint32_t)y2 || dx > dy)) {
1516 gfx_fb_setpixel(x0, y2);
1525 if ((e2 << 1) <= dy) { /* y step */
1527 while (e2 < ed * width &&
1528 (x1 != (uint32_t)x2 || dx < dy)) {
1530 gfx_fb_setpixel(x2, y0);
1542 * quadratic Bézier curve limited to gradients without sign change.
1545 gfx_fb_bezier(uint32_t x0, uint32_t y0, uint32_t x1, uint32_t y1, uint32_t x2,
1546 uint32_t y2, uint32_t wd)
1548 int sx, sy, xx, yy, xy, width;
1549 int dx, dy, err, curvature;
1552 if (gfx_state.tg_fb_type == FB_TEXT)
1560 curvature = xx*sy - yy*sx;
1562 if (sx*sx + sy*sy > xx*xx+yy*yy) {
1567 curvature = -curvature;
1569 if (curvature != 0) {
1571 sx = x0 < x2? 1 : -1;
1574 sy = y0 < y2? 1 : -1;
1579 if (curvature * sx * sy < 0) {
1583 curvature = -curvature;
1585 dx = 4 * sy * curvature * (x1 - x0) + xx - xy;
1586 dy = 4 * sx * curvature * (y0 - y1) + yy - xy;
1591 for (i = 0; i <= width; i++)
1592 gfx_fb_setpixel(x0 + i, y0);
1593 if (x0 == x2 && y0 == y2)
1594 return; /* last pixel -> curve finished */
1608 } while (dy < dx); /* gradient negates -> algorithm fails */
1610 gfx_fb_line(x0, y0, x2, y2, width);
1614 * draw rectangle using terminal coordinates and current foreground color.
1617 gfx_term_drawrect(uint32_t ux1, uint32_t uy1, uint32_t ux2, uint32_t uy2)
1622 uint32_t vf_width, vf_height;
1625 if (gfx_state.tg_fb_type == FB_TEXT)
1628 vf_width = gfx_state.tg_font.vf_width;
1629 vf_height = gfx_state.tg_font.vf_height;
1630 width = vf_width / 4; /* line width */
1631 xshift = (vf_width - width) / 2;
1632 yshift = (vf_height - width) / 2;
1634 /* Shift coordinates */
1642 /* mark area used in terminal */
1643 r.tr_begin.tp_col = ux1;
1644 r.tr_begin.tp_row = uy1;
1645 r.tr_end.tp_col = ux2 + 1;
1646 r.tr_end.tp_row = uy2 + 1;
1648 term_image_display(&gfx_state, &r);
1651 * Draw horizontal lines width points thick, shifted from outer edge.
1653 x1 = (ux1 + 1) * vf_width + gfx_state.tg_origin.tp_col;
1654 y1 = uy1 * vf_height + gfx_state.tg_origin.tp_row + yshift;
1655 x2 = ux2 * vf_width + gfx_state.tg_origin.tp_col;
1656 gfx_fb_drawrect(x1, y1, x2, y1 + width, 1);
1657 y2 = uy2 * vf_height + gfx_state.tg_origin.tp_row;
1658 y2 += vf_height - yshift - width;
1659 gfx_fb_drawrect(x1, y2, x2, y2 + width, 1);
1662 * Draw vertical lines width points thick, shifted from outer edge.
1664 x1 = ux1 * vf_width + gfx_state.tg_origin.tp_col + xshift;
1665 y1 = uy1 * vf_height + gfx_state.tg_origin.tp_row;
1667 y2 = uy2 * vf_height + gfx_state.tg_origin.tp_row;
1668 gfx_fb_drawrect(x1, y1, x1 + width, y2, 1);
1669 x1 = ux2 * vf_width + gfx_state.tg_origin.tp_col;
1670 x1 += vf_width - xshift - width;
1671 gfx_fb_drawrect(x1, y1, x1 + width, y2, 1);
1673 /* Draw upper left corner. */
1674 x1 = ux1 * vf_width + gfx_state.tg_origin.tp_col + xshift;
1675 y1 = uy1 * vf_height + gfx_state.tg_origin.tp_row;
1678 x2 = ux1 * vf_width + gfx_state.tg_origin.tp_col;
1680 y2 = uy1 * vf_height + gfx_state.tg_origin.tp_row + yshift;
1681 for (i = 0; i <= width; i++)
1682 gfx_fb_bezier(x1 + i, y1, x1 + i, y2 + i, x2, y2 + i, width-i);
1684 /* Draw lower left corner. */
1685 x1 = ux1 * vf_width + gfx_state.tg_origin.tp_col;
1687 y1 = uy2 * vf_height + gfx_state.tg_origin.tp_row;
1688 y1 += vf_height - yshift;
1689 x2 = ux1 * vf_width + gfx_state.tg_origin.tp_col + xshift;
1690 y2 = uy2 * vf_height + gfx_state.tg_origin.tp_row;
1691 for (i = 0; i <= width; i++)
1692 gfx_fb_bezier(x1, y1 - i, x2 + i, y1 - i, x2 + i, y2, width-i);
1694 /* Draw upper right corner. */
1695 x1 = ux2 * vf_width + gfx_state.tg_origin.tp_col;
1696 y1 = uy1 * vf_height + gfx_state.tg_origin.tp_row + yshift;
1697 x2 = ux2 * vf_width + gfx_state.tg_origin.tp_col;
1698 x2 += vf_width - xshift - width;
1699 y2 = uy1 * vf_height + gfx_state.tg_origin.tp_row;
1701 for (i = 0; i <= width; i++)
1702 gfx_fb_bezier(x1, y1 + i, x2 + i, y1 + i, x2 + i, y2, width-i);
1704 /* Draw lower right corner. */
1705 x1 = ux2 * vf_width + gfx_state.tg_origin.tp_col;
1706 y1 = uy2 * vf_height + gfx_state.tg_origin.tp_row;
1707 y1 += vf_height - yshift;
1708 x2 = ux2 * vf_width + gfx_state.tg_origin.tp_col;
1709 x2 += vf_width - xshift - width;
1710 y2 = uy2 * vf_height + gfx_state.tg_origin.tp_row;
1711 for (i = 0; i <= width; i++)
1712 gfx_fb_bezier(x1, y1 - i, x2 + i, y1 - i, x2 + i, y2, width-i);
1716 gfx_fb_putimage(png_t *png, uint32_t ux1, uint32_t uy1, uint32_t ux2,
1717 uint32_t uy2, uint32_t flags)
1720 EFI_GRAPHICS_OUTPUT_BLT_PIXEL *p;
1722 struct paletteentry *p;
1725 uint32_t i, j, x, y, fheight, fwidth;
1732 trace = (flags & FL_PUTIMAGE_DEBUG) != 0;
1734 if (gfx_state.tg_fb_type == FB_TEXT) {
1736 printf("Framebuffer not active.\n");
1740 if (png->color_type != PNG_TRUECOLOR_ALPHA) {
1742 printf("Not truecolor image.\n");
1746 if (ux1 > gfx_state.tg_fb.fb_width ||
1747 uy1 > gfx_state.tg_fb.fb_height) {
1749 printf("Top left coordinate off screen.\n");
1753 if (png->width > UINT16_MAX || png->height > UINT16_MAX) {
1755 printf("Image too large.\n");
1759 if (png->width < 1 || png->height < 1) {
1761 printf("Image too small.\n");
1766 * If 0 was passed for either ux2 or uy2, then calculate the missing
1767 * part of the bottom right coordinate.
1770 if (ux2 == 0 && uy2 == 0) {
1771 /* Both 0, use the native resolution of the image */
1772 ux2 = ux1 + png->width;
1773 uy2 = uy1 + png->height;
1775 } else if (ux2 == 0) {
1776 /* Set ux2 from uy2/uy1 to maintain aspect ratio */
1777 ux2 = ux1 + (png->width * (uy2 - uy1)) / png->height;
1778 } else if (uy2 == 0) {
1779 /* Set uy2 from ux2/ux1 to maintain aspect ratio */
1780 uy2 = uy1 + (png->height * (ux2 - ux1)) / png->width;
1783 if (ux2 > gfx_state.tg_fb.fb_width ||
1784 uy2 > gfx_state.tg_fb.fb_height) {
1786 printf("Bottom right coordinate off screen.\n");
1791 fheight = uy2 - uy1;
1794 * If the original image dimensions have been passed explicitly,
1797 if (fwidth == png->width && fheight == png->height)
1802 * No top left X co-ordinate (real coordinates start at 1),
1803 * place as far right as it will fit.
1805 ux2 = gfx_state.tg_fb.fb_width - gfx_state.tg_origin.tp_col;
1811 * No top left Y co-ordinate (real coordinates start at 1),
1812 * place as far down as it will fit.
1814 uy2 = gfx_state.tg_fb.fb_height - gfx_state.tg_origin.tp_row;
1815 uy1 = uy2 - fheight;
1818 if (ux1 >= ux2 || uy1 >= uy2) {
1820 printf("Image dimensions reversed.\n");
1824 if (fwidth < 2 || fheight < 2) {
1826 printf("Target area too small\n");
1831 printf("Image %ux%u -> %ux%u @%ux%u\n",
1832 png->width, png->height, fwidth, fheight, ux1, uy1);
1834 rect.tr_begin.tp_col = ux1 / gfx_state.tg_font.vf_width;
1835 rect.tr_begin.tp_row = uy1 / gfx_state.tg_font.vf_height;
1836 rect.tr_end.tp_col = (ux1 + fwidth) / gfx_state.tg_font.vf_width;
1837 rect.tr_end.tp_row = (uy1 + fheight) / gfx_state.tg_font.vf_height;
1840 * mark area used in terminal
1842 if (!(flags & FL_PUTIMAGE_NOSCROLL))
1843 term_image_display(&gfx_state, &rect);
1845 if ((flags & FL_PUTIMAGE_BORDER))
1846 gfx_fb_drawrect(ux1, uy1, ux2, uy2, 0);
1848 data = malloc(fwidth * fheight * sizeof(*p));
1852 printf("Out of memory.\n");
1857 * Build image for our framebuffer.
1860 /* Helper to calculate the pixel index from the source png */
1861 #define GETPIXEL(xx, yy) (((yy) * png->width + (xx)) * png->bpp)
1864 * For each of the x and y directions, calculate the number of pixels
1865 * in the source image that correspond to a single pixel in the target.
1866 * Use fixed-point arithmetic with 16-bits for each of the integer and
1869 const uint32_t wcstep = ((png->width - 1) << 16) / (fwidth - 1);
1870 const uint32_t hcstep = ((png->height - 1) << 16) / (fheight - 1);
1872 rs = 8 - (fls(gfx_state.tg_fb.fb_mask_red) -
1873 ffs(gfx_state.tg_fb.fb_mask_red) + 1);
1874 gs = 8 - (fls(gfx_state.tg_fb.fb_mask_green) -
1875 ffs(gfx_state.tg_fb.fb_mask_green) + 1);
1876 bs = 8 - (fls(gfx_state.tg_fb.fb_mask_blue) -
1877 ffs(gfx_state.tg_fb.fb_mask_blue) + 1);
1880 for (y = 0; y < fheight; y++) {
1881 uint32_t hc2 = (hc >> 9) & 0x7f;
1882 uint32_t hc1 = 0x80 - hc2;
1884 uint32_t offset_y = hc >> 16;
1885 uint32_t offset_y1 = offset_y + 1;
1888 for (x = 0; x < fwidth; x++) {
1889 uint32_t wc2 = (wc >> 9) & 0x7f;
1890 uint32_t wc1 = 0x80 - wc2;
1892 uint32_t offset_x = wc >> 16;
1893 uint32_t offset_x1 = offset_x + 1;
1895 /* Target pixel index */
1901 g = png->image[i + 1];
1902 b = png->image[i + 2];
1903 a = png->image[i + 3];
1907 uint32_t p00 = GETPIXEL(offset_x, offset_y);
1908 uint32_t p01 = GETPIXEL(offset_x, offset_y1);
1909 uint32_t p10 = GETPIXEL(offset_x1, offset_y);
1910 uint32_t p11 = GETPIXEL(offset_x1, offset_y1);
1913 * Given a 2x2 array of pixels in the source
1914 * image, combine them to produce a single
1915 * value for the pixel in the target image.
1916 * Each column of pixels is combined using
1917 * a weighted average where the top and bottom
1918 * pixels contribute hc1 and hc2 respectively.
1919 * The calculation for bottom pixel pB and
1921 * (pT * hc1 + pB * hc2) / (hc1 + hc2)
1922 * Once the values are determined for the two
1923 * columns of pixels, then the columns are
1924 * averaged together in the same way but using
1925 * wc1 and wc2 for the weightings.
1927 * Since hc1 and hc2 are chosen so that
1928 * hc1 + hc2 == 128 (and same for wc1 + wc2),
1929 * the >> 14 below is a quick way to divide by
1930 * (hc1 + hc2) * (wc1 + wc2)
1932 for (i = 0; i < 4; i++)
1934 (png->image[p00 + i] * hc1 +
1935 png->image[p01 + i] * hc2) * wc1 +
1936 (png->image[p10 + i] * hc1 +
1937 png->image[p11 + i] * hc2) * wc2)
1947 printf("r/g/b: %x/%x/%x\n", r, g, b);
1949 * Rough colorspace reduction for 15/16 bit colors.
1952 p[j].Green = g >> gs;
1953 p[j].Blue = b >> bs;
1961 gfx_fb_cons_display(ux1, uy1, fwidth, fheight, data);
1967 * Reset font flags to FONT_AUTO.
1970 reset_font_flags(void)
1972 struct fontlist *fl;
1974 STAILQ_FOREACH(fl, &fonts, font_next) {
1975 fl->font_flags = FONT_AUTO;
1979 /* Return w^2 + h^2 or 0, if the dimensions are unknown */
1981 edid_diagonal_squared(void)
1985 if (edid_info == NULL)
1988 w = edid_info->display.max_horizontal_image_size;
1989 h = edid_info->display.max_vertical_image_size;
1991 /* If either one is 0, we have aspect ratio, not size */
1992 if (w == 0 || h == 0)
1996 * some monitors encode the aspect ratio instead of the physical size.
1998 if ((w == 16 && h == 9) || (w == 16 && h == 10) ||
1999 (w == 4 && h == 3) || (w == 5 && h == 4))
2003 * translate cm to inch, note we scale by 100 here.
2008 /* Return w^2 + h^2 */
2009 return (w * w + h * h);
2013 * calculate pixels per inch.
2020 di = edid_diagonal_squared();
2024 dp = gfx_state.tg_fb.fb_width *
2025 gfx_state.tg_fb.fb_width +
2026 gfx_state.tg_fb.fb_height *
2027 gfx_state.tg_fb.fb_height;
2029 return (isqrt(dp / di));
2033 * Calculate font size from density independent pixels (dp):
2034 * ((16dp * ppi) / 160) * display_factor.
2035 * Here we are using fixed constants: 1dp == 160 ppi and
2038 * We are rounding font size up and are searching for font which is
2039 * not smaller than calculated size value.
2041 static vt_font_bitmap_data_t *
2045 vt_font_bitmap_data_t *font = NULL;
2046 struct fontlist *fl, *next;
2048 /* Text mode is not supported here. */
2049 if (gfx_state.tg_fb_type == FB_TEXT)
2052 ppi = gfx_get_ppi();
2057 * We will search for 16dp font.
2058 * We are using scale up by 10 for roundup.
2060 size = (16 * ppi * 10) / 160;
2061 /* Apply display factor 2. */
2062 size = roundup(size * 2, 10) / 10;
2064 STAILQ_FOREACH(fl, &fonts, font_next) {
2065 next = STAILQ_NEXT(fl, font_next);
2068 * If this is last font or, if next font is smaller,
2069 * we have our font. Make sure, it actually is loaded.
2071 if (next == NULL || next->font_data->vfbd_height < size) {
2072 font = fl->font_data;
2073 if (font->vfbd_font == NULL ||
2074 fl->font_flags == FONT_RELOAD) {
2075 if (fl->font_load != NULL &&
2076 fl->font_name != NULL)
2077 font = fl->font_load(fl->font_name);
2086 static vt_font_bitmap_data_t *
2087 set_font(teken_unit_t *rows, teken_unit_t *cols, teken_unit_t h, teken_unit_t w)
2089 vt_font_bitmap_data_t *font = NULL;
2090 struct fontlist *fl;
2091 unsigned height = h;
2095 * First check for manually loaded font.
2097 STAILQ_FOREACH(fl, &fonts, font_next) {
2098 if (fl->font_flags == FONT_MANUAL) {
2099 font = fl->font_data;
2100 if (font->vfbd_font == NULL && fl->font_load != NULL &&
2101 fl->font_name != NULL) {
2102 font = fl->font_load(fl->font_name);
2104 if (font == NULL || font->vfbd_font == NULL)
2111 font = gfx_get_font();
2114 *rows = height / font->vfbd_height;
2115 *cols = width / font->vfbd_width;
2120 * Find best font for these dimensions, or use default.
2121 * If height >= VT_FB_MAX_HEIGHT and width >= VT_FB_MAX_WIDTH,
2122 * do not use smaller font than our DEFAULT_FONT_DATA.
2124 STAILQ_FOREACH(fl, &fonts, font_next) {
2125 font = fl->font_data;
2126 if ((*rows * font->vfbd_height <= height &&
2127 *cols * font->vfbd_width <= width) ||
2128 (height >= VT_FB_MAX_HEIGHT &&
2129 width >= VT_FB_MAX_WIDTH &&
2130 font->vfbd_height == DEFAULT_FONT_DATA.vfbd_height &&
2131 font->vfbd_width == DEFAULT_FONT_DATA.vfbd_width)) {
2132 if (font->vfbd_font == NULL ||
2133 fl->font_flags == FONT_RELOAD) {
2134 if (fl->font_load != NULL &&
2135 fl->font_name != NULL) {
2136 font = fl->font_load(fl->font_name);
2141 *rows = height / font->vfbd_height;
2142 *cols = width / font->vfbd_width;
2150 * We have fonts sorted smallest last, try it before
2151 * falling back to builtin.
2153 fl = STAILQ_LAST(&fonts, fontlist, font_next);
2154 if (fl != NULL && fl->font_load != NULL &&
2155 fl->font_name != NULL) {
2156 font = fl->font_load(fl->font_name);
2159 font = &DEFAULT_FONT_DATA;
2161 *rows = height / font->vfbd_height;
2162 *cols = width / font->vfbd_width;
2171 char clear[] = { '\033', 'c' };
2173 /* Reset terminal */
2174 teken_input(&gfx_state.tg_teken, clear, sizeof(clear));
2175 gfx_state.tg_functions->tf_param(&gfx_state, TP_SHOWCURSOR, 0);
2179 setup_font(teken_gfx_t *state, teken_unit_t height, teken_unit_t width)
2181 vt_font_bitmap_data_t *font_data;
2182 teken_pos_t *tp = &state->tg_tp;
2187 * set_font() will select a appropriate sized font for
2188 * the number of rows and columns selected. If we don't
2189 * have a font that will fit, then it will use the
2190 * default builtin font and adjust the rows and columns
2191 * to fit on the screen.
2193 font_data = set_font(&tp->tp_row, &tp->tp_col, height, width);
2195 if (font_data == NULL)
2196 panic("out of memory");
2198 for (i = 0; i < VFNT_MAPS; i++) {
2199 state->tg_font.vf_map[i] =
2200 font_data->vfbd_font->vf_map[i];
2201 state->tg_font.vf_map_count[i] =
2202 font_data->vfbd_font->vf_map_count[i];
2205 state->tg_font.vf_bytes = font_data->vfbd_font->vf_bytes;
2206 state->tg_font.vf_height = font_data->vfbd_font->vf_height;
2207 state->tg_font.vf_width = font_data->vfbd_font->vf_width;
2209 snprintf(env, sizeof (env), "%ux%u",
2210 state->tg_font.vf_width, state->tg_font.vf_height);
2211 env_setenv("screen.font", EV_VOLATILE | EV_NOHOOK,
2212 env, font_set, env_nounset);
2215 /* Binary search for the glyph. Return 0 if not found. */
2217 font_bisearch(const vfnt_map_t *map, uint32_t len, teken_char_t src)
2219 unsigned min, mid, max;
2224 /* Empty font map. */
2227 /* Character below minimal entry. */
2228 if (src < map[0].vfm_src)
2230 /* Optimization: ASCII characters occur very often. */
2231 if (src <= map[0].vfm_src + map[0].vfm_len)
2232 return (src - map[0].vfm_src + map[0].vfm_dst);
2233 /* Character above maximum entry. */
2234 if (src > map[max].vfm_src + map[max].vfm_len)
2237 /* Binary search. */
2238 while (max >= min) {
2239 mid = (min + max) / 2;
2240 if (src < map[mid].vfm_src)
2242 else if (src > map[mid].vfm_src + map[mid].vfm_len)
2245 return (src - map[mid].vfm_src + map[mid].vfm_dst);
2252 * Return glyph bitmap. If glyph is not found, we will return bitmap
2253 * for the first (offset 0) glyph.
2256 font_lookup(const struct vt_font *vf, teken_char_t c, const teken_attr_t *a)
2261 /* Substitute bold with normal if not found. */
2262 if (a->ta_format & TF_BOLD) {
2263 dst = font_bisearch(vf->vf_map[VFNT_MAP_BOLD],
2264 vf->vf_map_count[VFNT_MAP_BOLD], c);
2268 dst = font_bisearch(vf->vf_map[VFNT_MAP_NORMAL],
2269 vf->vf_map_count[VFNT_MAP_NORMAL], c);
2272 stride = howmany(vf->vf_width, 8) * vf->vf_height;
2273 return (&vf->vf_bytes[dst * stride]);
2277 load_mapping(int fd, struct vt_font *fp, int n)
2283 if (fp->vf_map_count[n] == 0)
2286 size = fp->vf_map_count[n] * sizeof(*mp);
2292 rv = read(fd, mp, size);
2293 if (rv < 0 || (size_t)rv != size) {
2294 free(fp->vf_map[n]);
2295 fp->vf_map[n] = NULL;
2299 for (i = 0; i < fp->vf_map_count[n]; i++) {
2300 mp[i].vfm_src = be32toh(mp[i].vfm_src);
2301 mp[i].vfm_dst = be16toh(mp[i].vfm_dst);
2302 mp[i].vfm_len = be16toh(mp[i].vfm_len);
2308 builtin_mapping(struct vt_font *fp, int n)
2311 struct vfnt_map *mp;
2316 if (fp->vf_map_count[n] == 0)
2319 size = fp->vf_map_count[n] * sizeof(*mp);
2325 memcpy(mp, DEFAULT_FONT_DATA.vfbd_font->vf_map[n], size);
2330 * Load font from builtin or from file.
2331 * We do need special case for builtin because the builtin font glyphs
2332 * are compressed and we do need to uncompress them.
2333 * Having single load_font() for both cases will help us to simplify
2334 * font switch handling.
2336 static vt_font_bitmap_data_t *
2337 load_font(char *path)
2341 struct font_header fh;
2342 struct fontlist *fl;
2343 vt_font_bitmap_data_t *bp;
2348 /* Get our entry from the font list. */
2349 STAILQ_FOREACH(fl, &fonts, font_next) {
2350 if (strcmp(fl->font_name, path) == 0)
2354 return (NULL); /* Should not happen. */
2357 if (bp->vfbd_font != NULL && fl->font_flags != FONT_RELOAD)
2362 * Special case for builtin font.
2363 * Builtin font is the very first font we load, we do not have
2364 * previous loads to be released.
2366 if (fl->font_flags == FONT_BUILTIN) {
2367 if ((fp = calloc(1, sizeof(struct vt_font))) == NULL)
2370 fp->vf_width = DEFAULT_FONT_DATA.vfbd_width;
2371 fp->vf_height = DEFAULT_FONT_DATA.vfbd_height;
2373 fp->vf_bytes = malloc(DEFAULT_FONT_DATA.vfbd_uncompressed_size);
2374 if (fp->vf_bytes == NULL) {
2379 bp->vfbd_uncompressed_size =
2380 DEFAULT_FONT_DATA.vfbd_uncompressed_size;
2381 bp->vfbd_compressed_size =
2382 DEFAULT_FONT_DATA.vfbd_compressed_size;
2384 if (lz4_decompress(DEFAULT_FONT_DATA.vfbd_compressed_data,
2386 DEFAULT_FONT_DATA.vfbd_compressed_size,
2387 DEFAULT_FONT_DATA.vfbd_uncompressed_size, 0) != 0) {
2393 for (i = 0; i < VFNT_MAPS; i++) {
2394 fp->vf_map_count[i] =
2395 DEFAULT_FONT_DATA.vfbd_font->vf_map_count[i];
2396 if (builtin_mapping(fp, i) != 0)
2404 fd = open(path, O_RDONLY);
2409 rv = read(fd, &fh, size);
2410 if (rv < 0 || (size_t)rv != size) {
2414 if (memcmp(fh.fh_magic, FONT_HEADER_MAGIC, sizeof(fh.fh_magic)) != 0) {
2418 if ((fp = calloc(1, sizeof(struct vt_font))) == NULL) {
2422 for (i = 0; i < VFNT_MAPS; i++)
2423 fp->vf_map_count[i] = be32toh(fh.fh_map_count[i]);
2425 glyphs = be32toh(fh.fh_glyph_count);
2426 fp->vf_width = fh.fh_width;
2427 fp->vf_height = fh.fh_height;
2429 size = howmany(fp->vf_width, 8) * fp->vf_height * glyphs;
2430 bp->vfbd_uncompressed_size = size;
2431 if ((fp->vf_bytes = malloc(size)) == NULL)
2434 rv = read(fd, fp->vf_bytes, size);
2435 if (rv < 0 || (size_t)rv != size)
2437 for (i = 0; i < VFNT_MAPS; i++) {
2438 if (load_mapping(fd, fp, i) != 0)
2443 * Reset builtin flag now as we have full font loaded.
2445 if (fl->font_flags == FONT_BUILTIN)
2446 fl->font_flags = FONT_AUTO;
2449 * Release previously loaded entries. We can do this now, as
2450 * the new font is loaded. Note, there can be no console
2451 * output till the new font is in place and teken is notified.
2452 * We do need to keep fl->font_data for glyph dimensions.
2454 STAILQ_FOREACH(fl, &fonts, font_next) {
2455 if (fl->font_data->vfbd_font == NULL)
2458 for (i = 0; i < VFNT_MAPS; i++)
2459 free(fl->font_data->vfbd_font->vf_map[i]);
2460 free(fl->font_data->vfbd_font->vf_bytes);
2461 free(fl->font_data->vfbd_font);
2462 fl->font_data->vfbd_font = NULL;
2466 bp->vfbd_compressed_size = 0;
2474 for (i = 0; i < VFNT_MAPS; i++)
2475 free(fp->vf_map[i]);
2484 SLIST_ENTRY(name_entry) n_entry;
2487 SLIST_HEAD(name_list, name_entry);
2489 /* Read font names from index file. */
2490 static struct name_list *
2491 read_list(char *fonts)
2493 struct name_list *nl;
2494 struct name_entry *np;
2501 dir = strdup(fonts);
2505 ptr = strrchr(dir, '/');
2508 fd = open(fonts, O_RDONLY);
2512 nl = malloc(sizeof(*nl));
2519 while ((len = fgetstr(buf, sizeof (buf), fd)) >= 0) {
2520 if (*buf == '#' || *buf == '\0')
2523 if (bcmp(buf, "MENU", 4) == 0)
2526 if (bcmp(buf, "FONT", 4) == 0)
2529 ptr = strchr(buf, ':');
2535 np = malloc(sizeof(*np));
2538 return (nl); /* return what we have */
2540 if (asprintf(&np->n_name, "%s/%s", dir, buf) < 0) {
2543 return (nl); /* return what we have */
2545 SLIST_INSERT_HEAD(nl, np, n_entry);
2553 * Read the font properties and insert new entry into the list.
2554 * The font list is built in descending order.
2557 insert_font(char *name, FONT_FLAGS flags)
2559 struct font_header fh;
2560 struct fontlist *fp, *previous, *entry, *next;
2569 if (flags == FONT_BUILTIN) {
2571 * We only install builtin font once, while setting up
2572 * initial console. Since this will happen very early,
2573 * we assume asprintf will not fail. Once we have access to
2574 * files, the builtin font will be replaced by font loaded
2577 if (!STAILQ_EMPTY(&fonts))
2580 fh.fh_width = DEFAULT_FONT_DATA.vfbd_width;
2581 fh.fh_height = DEFAULT_FONT_DATA.vfbd_height;
2583 (void) asprintf(&font_name, "%dx%d",
2584 DEFAULT_FONT_DATA.vfbd_width,
2585 DEFAULT_FONT_DATA.vfbd_height);
2587 fd = open(name, O_RDONLY);
2590 rv = read(fd, &fh, sizeof(fh));
2592 if (rv < 0 || (size_t)rv != sizeof(fh))
2595 if (memcmp(fh.fh_magic, FONT_HEADER_MAGIC,
2596 sizeof(fh.fh_magic)) != 0)
2598 font_name = strdup(name);
2601 if (font_name == NULL)
2605 * If we have an entry with the same glyph dimensions, replace
2606 * the file name and mark us. We only support unique dimensions.
2608 STAILQ_FOREACH(entry, &fonts, font_next) {
2609 if (fh.fh_width == entry->font_data->vfbd_width &&
2610 fh.fh_height == entry->font_data->vfbd_height) {
2611 free(entry->font_name);
2612 entry->font_name = font_name;
2613 entry->font_flags = FONT_RELOAD;
2619 fp = calloc(sizeof(*fp), 1);
2624 fp->font_data = calloc(sizeof(*fp->font_data), 1);
2625 if (fp->font_data == NULL) {
2630 fp->font_name = font_name;
2631 fp->font_flags = flags;
2632 fp->font_load = load_font;
2633 fp->font_data->vfbd_width = fh.fh_width;
2634 fp->font_data->vfbd_height = fh.fh_height;
2636 if (STAILQ_EMPTY(&fonts)) {
2637 STAILQ_INSERT_HEAD(&fonts, fp, font_next);
2643 size = fp->font_data->vfbd_width * fp->font_data->vfbd_height;
2645 STAILQ_FOREACH(entry, &fonts, font_next) {
2646 vt_font_bitmap_data_t *bd;
2648 bd = entry->font_data;
2649 /* Should fp be inserted before the entry? */
2650 if (size > bd->vfbd_width * bd->vfbd_height) {
2651 if (previous == NULL) {
2652 STAILQ_INSERT_HEAD(&fonts, fp, font_next);
2654 STAILQ_INSERT_AFTER(&fonts, previous, fp,
2660 next = STAILQ_NEXT(entry, font_next);
2662 size > next->font_data->vfbd_width *
2663 next->font_data->vfbd_height) {
2664 STAILQ_INSERT_AFTER(&fonts, entry, fp, font_next);
2675 font_set(struct env_var *ev __unused, int flags __unused, const void *value)
2677 struct fontlist *fl;
2679 unsigned long x = 0, y = 0;
2682 * Attempt to extract values from "XxY" string. In case of error,
2683 * we have unmaching glyph dimensions and will just output the
2686 if (value != NULL) {
2687 x = strtoul(value, &eptr, 10);
2689 y = strtoul(eptr + 1, &eptr, 10);
2691 STAILQ_FOREACH(fl, &fonts, font_next) {
2692 if (fl->font_data->vfbd_width == x &&
2693 fl->font_data->vfbd_height == y)
2697 /* Reset any FONT_MANUAL flag. */
2700 /* Mark this font manually loaded */
2701 fl->font_flags = FONT_MANUAL;
2702 cons_update_mode(gfx_state.tg_fb_type != FB_TEXT);
2706 printf("Available fonts:\n");
2707 STAILQ_FOREACH(fl, &fonts, font_next) {
2708 printf(" %dx%d\n", fl->font_data->vfbd_width,
2709 fl->font_data->vfbd_height);
2715 bios_text_font(bool use_vga_font)
2718 (void) insert_font(VGA_8X16_FONT, FONT_MANUAL);
2720 (void) insert_font(DEFAULT_8X16_FONT, FONT_MANUAL);
2724 autoload_font(bool bios)
2726 struct name_list *nl;
2727 struct name_entry *np;
2731 nl = read_list("/boot/fonts/INDEX.fonts");
2735 while (!SLIST_EMPTY(nl)) {
2736 np = SLIST_FIRST(nl);
2737 SLIST_REMOVE_HEAD(nl, n_entry);
2738 if (insert_font(np->n_name, FONT_AUTO) == false)
2739 printf("failed to add font: %s\n", np->n_name);
2745 * If vga text mode was requested, load vga.font (8x16 bold) font.
2748 bios_text_font(true);
2751 (void) cons_update_mode(gfx_state.tg_fb_type != FB_TEXT);
2756 COMMAND_SET(load_font, "loadfont", "load console font from file", command_font);
2759 command_font(int argc, char *argv[])
2762 struct fontlist *fl;
2763 vt_font_bitmap_data_t *bd;
2771 while ((c = getopt(argc, argv, "l")) != -1) {
2785 if (argc > 1 || (list && argc != 0)) {
2786 printf("Usage: loadfont [-l] | [file.fnt]\n");
2791 STAILQ_FOREACH(fl, &fonts, font_next) {
2792 printf("font %s: %dx%d%s\n", fl->font_name,
2793 fl->font_data->vfbd_width,
2794 fl->font_data->vfbd_height,
2795 fl->font_data->vfbd_font == NULL? "" : " loaded");
2804 char *name = argv[0];
2806 if (insert_font(name, FONT_MANUAL) == false) {
2807 printf("loadfont error: failed to load: %s\n", name);
2811 (void) cons_update_mode(gfx_state.tg_fb_type != FB_TEXT);
2817 * Walk entire font list, release any loaded font, and set
2818 * autoload flag. The font list does have at least the builtin
2821 STAILQ_FOREACH(fl, &fonts, font_next) {
2822 if (fl->font_data->vfbd_font != NULL) {
2826 * Note the setup_font() is releasing
2829 for (i = 0; i < VFNT_MAPS; i++)
2830 free(bd->vfbd_font->vf_map[i]);
2831 free(fl->font_data->vfbd_font);
2832 fl->font_data->vfbd_font = NULL;
2833 fl->font_data->vfbd_uncompressed_size = 0;
2834 fl->font_flags = FONT_AUTO;
2837 (void) cons_update_mode(gfx_state.tg_fb_type != FB_TEXT);
2843 gfx_get_edid_resolution(struct vesa_edid_info *edid, edid_res_list_t *res)
2845 struct resolution *rp, *p;
2848 * Walk detailed timings tables (4).
2850 if ((edid->display.supported_features
2851 & EDID_FEATURE_PREFERRED_TIMING_MODE) != 0) {
2852 /* Walk detailed timing descriptors (4) */
2853 for (int i = 0; i < DET_TIMINGS; i++) {
2855 * Reserved value 0 is not used for display descriptor.
2857 if (edid->detailed_timings[i].pixel_clock == 0)
2859 if ((rp = malloc(sizeof(*rp))) == NULL)
2861 rp->width = GET_EDID_INFO_WIDTH(edid, i);
2862 rp->height = GET_EDID_INFO_HEIGHT(edid, i);
2863 if (rp->width > 0 && rp->width <= EDID_MAX_PIXELS &&
2864 rp->height > 0 && rp->height <= EDID_MAX_LINES)
2865 TAILQ_INSERT_TAIL(res, rp, next);
2872 * Walk standard timings list (8).
2874 for (int i = 0; i < STD_TIMINGS; i++) {
2875 /* Is this field unused? */
2876 if (edid->standard_timings[i] == 0x0101)
2879 if ((rp = malloc(sizeof(*rp))) == NULL)
2882 rp->width = HSIZE(edid->standard_timings[i]);
2883 switch (RATIO(edid->standard_timings[i])) {
2885 rp->height = HSIZE(edid->standard_timings[i]);
2886 if (edid->header.version > 1 ||
2887 edid->header.revision > 2) {
2888 rp->height = rp->height * 10 / 16;
2892 rp->height = HSIZE(edid->standard_timings[i]) * 3 / 4;
2895 rp->height = HSIZE(edid->standard_timings[i]) * 4 / 5;
2898 rp->height = HSIZE(edid->standard_timings[i]) * 9 / 16;
2903 * Create resolution list in decreasing order, except keep
2904 * first entry (preferred timing mode).
2906 TAILQ_FOREACH(p, res, next) {
2907 if (p->width * p->height < rp->width * rp->height) {
2908 /* Keep preferred mode first */
2909 if (TAILQ_FIRST(res) == p)
2910 TAILQ_INSERT_AFTER(res, p, rp, next);
2912 TAILQ_INSERT_BEFORE(p, rp, next);
2915 if (TAILQ_NEXT(p, next) == NULL) {
2916 TAILQ_INSERT_TAIL(res, rp, next);
2921 return (!TAILQ_EMPTY(res));
2925 build_font_module(vm_offset_t addr)
2927 vt_font_bitmap_data_t *bd;
2929 struct preloaded_file *fp;
2933 struct font_info fi;
2934 struct fontlist *fl;
2937 if (STAILQ_EMPTY(&fonts))
2940 /* We can't load first */
2941 if ((file_findfile(NULL, NULL)) == NULL) {
2942 printf("Can not load font module: %s\n",
2943 "the kernel is not loaded");
2947 /* helper pointers */
2949 STAILQ_FOREACH(fl, &fonts, font_next) {
2950 if (gfx_state.tg_font.vf_width == fl->font_data->vfbd_width &&
2951 gfx_state.tg_font.vf_height == fl->font_data->vfbd_height) {
2953 * Kernel does have better built in font.
2955 if (fl->font_flags == FONT_BUILTIN)
2966 fi.fi_width = fd->vf_width;
2967 checksum = fi.fi_width;
2968 fi.fi_height = fd->vf_height;
2969 checksum += fi.fi_height;
2970 fi.fi_bitmap_size = bd->vfbd_uncompressed_size;
2971 checksum += fi.fi_bitmap_size;
2973 size = roundup2(sizeof (struct font_info), 8);
2974 for (i = 0; i < VFNT_MAPS; i++) {
2975 fi.fi_map_count[i] = fd->vf_map_count[i];
2976 checksum += fi.fi_map_count[i];
2977 size += fd->vf_map_count[i] * sizeof (struct vfnt_map);
2978 size += roundup2(size, 8);
2980 size += bd->vfbd_uncompressed_size;
2982 fi.fi_checksum = -checksum;
2984 fp = file_findfile(NULL, "elf kernel");
2986 fp = file_findfile(NULL, "elf64 kernel");
2988 panic("can't find kernel file");
2991 addr += archsw.arch_copyin(&fi, addr, sizeof (struct font_info));
2992 addr = roundup2(addr, 8);
2995 for (i = 0; i < VFNT_MAPS; i++) {
2996 if (fd->vf_map_count[i] != 0) {
2997 addr += archsw.arch_copyin(fd->vf_map[i], addr,
2998 fd->vf_map_count[i] * sizeof (struct vfnt_map));
2999 addr = roundup2(addr, 8);
3003 /* Copy the bitmap. */
3004 addr += archsw.arch_copyin(fd->vf_bytes, addr, fi.fi_bitmap_size);
3006 /* Looks OK so far; populate control structure */
3007 file_addmetadata(fp, MODINFOMD_FONT, sizeof(fontp), &fontp);