xref: /freebsd/sys/dev/vt/vt_cpulogos.c (revision 81ad6265)
1 /*-
2  * Copyright (c) 2015 Conrad Meyer <cem@FreeBSD.org>
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  *
14  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24  * SUCH DAMAGE.
25  */
26 
27 #include <sys/cdefs.h>
28 __FBSDID("$FreeBSD$");
29 
30 #include <sys/param.h>
31 #include <sys/callout.h>
32 #include <sys/cons.h>
33 #include <sys/lock.h>
34 #include <sys/kernel.h>
35 #include <sys/mutex.h>
36 #include <sys/smp.h>
37 #include <sys/systm.h>
38 #include <sys/terminal.h>
39 
40 #include <dev/vt/vt.h>
41 
42 extern const unsigned char vt_beastie_vga16[];
43 extern const unsigned char vt_beastie2_vga16[];
44 extern const unsigned char vt_orb_vga16[];
45 
46 static struct timeout_task vt_splash_cpu_fini_task;
47 
48 static inline unsigned char
49 vt_vga2bsd(unsigned char vga)
50 {
51 	static const unsigned char lut[8] = {
52 		0,
53 		4,	/* 1 and 4 swap */
54 		2,
55 		6,	/* 3 and 6 swap */
56 		1,	/* 4 and 1 swap */
57 		5,
58 		3,	/* 6 and 3 swap */
59 		7,
60 	};
61 	unsigned int bright;
62 
63 	bright = (vga & 0x8);
64 	return (lut[vga & 0x7] | bright);
65 }
66 
67 static void
68 vt_draw_2_vga16_px(struct vt_device *vd, vt_axis_t x, vt_axis_t y,
69     unsigned char color)
70 {
71 
72 	vd->vd_driver->vd_setpixel(vd, x, y, vt_vga2bsd(color >> 4));
73 	vd->vd_driver->vd_setpixel(vd, x + 1, y, vt_vga2bsd(color & 0xf));
74 }
75 
76 static void
77 vt_draw_1_logo(struct vt_device *vd, vt_axis_t top, vt_axis_t left)
78 {
79 	const unsigned char rle_sent = 0x16, *data;
80 	unsigned int xy, run, runcolor, i;
81 
82 	switch (vt_splash_cpu_style) {
83 	case VT_LOGOS_DRAW_ALT_BEASTIE:
84 		data = vt_beastie2_vga16;
85 		break;
86 	case VT_LOGOS_DRAW_ORB:
87 		data = vt_orb_vga16;
88 		break;
89 	case VT_LOGOS_DRAW_BEASTIE:
90 		/* FALLTHROUGH */
91 	default:
92 		data = vt_beastie_vga16;
93 		break;
94 	}
95 
96 	/* Decode basic RLE (gets us to 30-40% of uncompressed data size): */
97 	for (i = 0, xy = 0; xy < vt_logo_sprite_height * vt_logo_sprite_width;) {
98 		if (data[i] == rle_sent) {
99 			runcolor = data[i + 1];
100 			run = data[i + 2];
101 
102 			for (; run; run--, xy += 2)
103 				vt_draw_2_vga16_px(vd,
104 				    left + (xy % vt_logo_sprite_width),
105 				    top + (xy / vt_logo_sprite_width),
106 				    runcolor);
107 
108 			i += 3;
109 		} else {
110 			vt_draw_2_vga16_px(vd, left + (xy % vt_logo_sprite_width),
111 			    top + (xy / vt_logo_sprite_width), data[i]);
112 
113 			i++;
114 			xy += 2;
115 		}
116 	}
117 }
118 
119 void
120 vtterm_draw_cpu_logos(struct vt_device *vd)
121 {
122 	unsigned int ncpu, i;
123 	vt_axis_t left;
124 	struct terminal *tm = vd->vd_curwindow->vw_terminal;
125 	const teken_attr_t *a;
126 
127 	if (vt_splash_ncpu)
128 		ncpu = vt_splash_ncpu;
129 	else {
130 		ncpu = mp_ncpus;
131 		if (ncpu < 1)
132 			ncpu = 1;
133 	}
134 
135 	a = teken_get_curattr(&tm->tm_emulator);
136 	if (vd->vd_driver->vd_drawrect)
137 		vd->vd_driver->vd_drawrect(vd, 0, 0, vd->vd_width - 1,
138 		    vt_logo_sprite_height - 1, 1, a->ta_bgcolor);
139 	/*
140 	 * Blank is okay because we only ever draw beasties on full screen
141 	 * refreshes.
142 	 */
143 	else if (vd->vd_driver->vd_blank)
144 		vd->vd_driver->vd_blank(vd, a->ta_bgcolor);
145 
146 	ncpu = MIN(ncpu, vd->vd_width / vt_logo_sprite_width);
147 	for (i = 0, left = 0; i < ncpu; left += vt_logo_sprite_width, i++)
148 		vt_draw_1_logo(vd, 0, left);
149 }
150 
151 static void
152 vt_fini_logos(void *dummy __unused, int pending __unused)
153 {
154 	struct vt_device *vd;
155 	struct vt_window *vw;
156 	struct terminal *tm;
157 	struct vt_font *vf;
158 	struct winsize wsz;
159 	term_pos_t size;
160 	unsigned int i;
161 
162 	if (!vt_draw_logo_cpus)
163 		return;
164 	if (!vty_enabled(VTY_VT))
165 		return;
166 	if (!vt_splash_cpu)
167 		return;
168 
169 	vd = &vt_consdev;
170 	VT_LOCK(vd);
171 	if ((vd->vd_flags & (VDF_DEAD | VDF_TEXTMODE)) != 0) {
172 		VT_UNLOCK(vd);
173 		return;
174 	}
175 	vt_draw_logo_cpus = 0;
176 	VT_UNLOCK(vd);
177 
178 	for (i = 0; i < VT_MAXWINDOWS; i++) {
179 		vw = vd->vd_windows[i];
180 		if (vw == NULL)
181 			continue;
182 		tm = vw->vw_terminal;
183 		vf = vw->vw_font;
184 		if (vf == NULL)
185 			continue;
186 
187 		vt_termsize(vd, vf, &size);
188 		vt_winsize(vd, vf, &wsz);
189 
190 		/* Resize screen buffer and terminal. */
191 		terminal_mute(tm, 1);
192 		vtbuf_grow(&vw->vw_buf, &size, vw->vw_buf.vb_history_size);
193 		terminal_set_winsize_blank(tm, &wsz, 0, NULL);
194 		terminal_set_cursor(tm, &vw->vw_buf.vb_cursor);
195 		terminal_mute(tm, 0);
196 
197 		VT_LOCK(vd);
198 		vt_compute_drawable_area(vw);
199 
200 		if (vd->vd_curwindow == vw) {
201 			vd->vd_flags |= VDF_INVALID;
202 			vt_resume_flush_timer(vw, 0);
203 		}
204 		VT_UNLOCK(vd);
205 	}
206 }
207 
208 static void
209 vt_init_logos(void *dummy)
210 {
211 	struct vt_device *vd;
212 	struct vt_window *vw;
213 	struct terminal *tm;
214 	struct vt_font *vf;
215 	struct winsize wsz;
216 	term_pos_t size;
217 
218 	if (!vty_enabled(VTY_VT))
219 		return;
220 	if (!vt_splash_cpu)
221 		return;
222 
223 	vd = &vt_consdev;
224 	if (vd == NULL)
225 		return;
226 	vw = vd->vd_curwindow;
227 	if (vw == NULL)
228 		return;
229 	tm = vw->vw_terminal;
230 	if (tm == NULL)
231 		return;
232 	vf = vw->vw_font;
233 	if (vf == NULL)
234 		return;
235 
236 	VT_LOCK(vd);
237 	if ((vd->vd_flags & VDF_INITIALIZED) == 0)
238 		goto out;
239 	if ((vd->vd_flags & (VDF_DEAD | VDF_TEXTMODE)) != 0)
240 		goto out;
241 	if (vd->vd_height <= vt_logo_sprite_height)
242 		goto out;
243 
244 	vt_draw_logo_cpus = 1;
245 	VT_UNLOCK(vd);
246 
247 	vt_termsize(vd, vf, &size);
248 	vt_winsize(vd, vf, &wsz);
249 
250 	/* Resize screen buffer and terminal. */
251 	terminal_mute(tm, 1);
252 	vtbuf_grow(&vw->vw_buf, &size, vw->vw_buf.vb_history_size);
253 	terminal_set_winsize_blank(tm, &wsz, 0, NULL);
254 	terminal_set_cursor(tm, &vw->vw_buf.vb_cursor);
255 	terminal_mute(tm, 0);
256 
257 	VT_LOCK(vd);
258 	vt_compute_drawable_area(vw);
259 
260 	if (vd->vd_curwindow == vw) {
261 		vd->vd_flags |= VDF_INVALID;
262 		vt_resume_flush_timer(vw, 0);
263 	}
264 
265 	TIMEOUT_TASK_INIT(taskqueue_thread, &vt_splash_cpu_fini_task, 0,
266 	    vt_fini_logos, NULL);
267 	taskqueue_enqueue_timeout(taskqueue_thread, &vt_splash_cpu_fini_task,
268 	    vt_splash_cpu_duration * hz);
269 
270 out:
271 	VT_UNLOCK(vd);
272 }
273 SYSINIT(vt_logos, SI_SUB_TASKQ, SI_ORDER_ANY, vt_init_logos, NULL);
274