xref: /freebsd/sys/dev/vt/vt_cpulogos.c (revision 22cf89c938886d14f5796fc49f9f020c23ea8eaf)
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 #include <sys/param.h>
29 #include <sys/callout.h>
30 #include <sys/cons.h>
31 #include <sys/lock.h>
32 #include <sys/kernel.h>
33 #include <sys/mutex.h>
34 #include <sys/smp.h>
35 #include <sys/systm.h>
36 #include <sys/terminal.h>
37 
38 #include <dev/vt/vt.h>
39 
40 extern const unsigned char vt_beastie_vga16[];
41 extern const unsigned char vt_beastie2_vga16[];
42 extern const unsigned char vt_orb_vga16[];
43 
44 static struct timeout_task vt_splash_cpu_fini_task;
45 
46 static inline unsigned char
47 vt_vga2bsd(unsigned char vga)
48 {
49 	static const unsigned char lut[8] = {
50 		0,
51 		4,	/* 1 and 4 swap */
52 		2,
53 		6,	/* 3 and 6 swap */
54 		1,	/* 4 and 1 swap */
55 		5,
56 		3,	/* 6 and 3 swap */
57 		7,
58 	};
59 	unsigned int bright;
60 
61 	bright = (vga & 0x8);
62 	return (lut[vga & 0x7] | bright);
63 }
64 
65 static void
66 vt_draw_2_vga16_px(struct vt_device *vd, vt_axis_t x, vt_axis_t y,
67     unsigned char color)
68 {
69 
70 	vd->vd_driver->vd_setpixel(vd, x, y, vt_vga2bsd(color >> 4));
71 	vd->vd_driver->vd_setpixel(vd, x + 1, y, vt_vga2bsd(color & 0xf));
72 }
73 
74 static void
75 vt_draw_1_logo(struct vt_device *vd, vt_axis_t top, vt_axis_t left)
76 {
77 	const unsigned char rle_sent = 0x16, *data;
78 	unsigned int xy, run, runcolor, i;
79 
80 	switch (vt_splash_cpu_style) {
81 	case VT_LOGOS_DRAW_ALT_BEASTIE:
82 		data = vt_beastie2_vga16;
83 		break;
84 	case VT_LOGOS_DRAW_ORB:
85 		data = vt_orb_vga16;
86 		break;
87 	case VT_LOGOS_DRAW_BEASTIE:
88 		/* FALLTHROUGH */
89 	default:
90 		data = vt_beastie_vga16;
91 		break;
92 	}
93 
94 	/* Decode basic RLE (gets us to 30-40% of uncompressed data size): */
95 	for (i = 0, xy = 0; xy < vt_logo_sprite_height * vt_logo_sprite_width;) {
96 		if (data[i] == rle_sent) {
97 			runcolor = data[i + 1];
98 			run = data[i + 2];
99 
100 			for (; run; run--, xy += 2)
101 				vt_draw_2_vga16_px(vd,
102 				    left + (xy % vt_logo_sprite_width),
103 				    top + (xy / vt_logo_sprite_width),
104 				    runcolor);
105 
106 			i += 3;
107 		} else {
108 			vt_draw_2_vga16_px(vd, left + (xy % vt_logo_sprite_width),
109 			    top + (xy / vt_logo_sprite_width), data[i]);
110 
111 			i++;
112 			xy += 2;
113 		}
114 	}
115 }
116 
117 void
118 vtterm_draw_cpu_logos(struct vt_device *vd)
119 {
120 	unsigned int ncpu, i;
121 	vt_axis_t left;
122 	struct terminal *tm = vd->vd_curwindow->vw_terminal;
123 	const teken_attr_t *a;
124 
125 	if (vt_splash_ncpu)
126 		ncpu = vt_splash_ncpu;
127 	else {
128 		ncpu = mp_ncpus;
129 		if (ncpu < 1)
130 			ncpu = 1;
131 	}
132 
133 	a = teken_get_curattr(&tm->tm_emulator);
134 	if (vd->vd_driver->vd_drawrect)
135 		vd->vd_driver->vd_drawrect(vd, 0, 0, vd->vd_width - 1,
136 		    vt_logo_sprite_height - 1, 1, a->ta_bgcolor);
137 	/*
138 	 * Blank is okay because we only ever draw beasties on full screen
139 	 * refreshes.
140 	 */
141 	else if (vd->vd_driver->vd_blank)
142 		vd->vd_driver->vd_blank(vd, a->ta_bgcolor);
143 
144 	ncpu = MIN(ncpu, vd->vd_width / vt_logo_sprite_width);
145 	for (i = 0, left = 0; i < ncpu; left += vt_logo_sprite_width, i++)
146 		vt_draw_1_logo(vd, 0, left);
147 }
148 
149 static void
150 vt_fini_logos(void *dummy __unused, int pending __unused)
151 {
152 	struct vt_device *vd;
153 	struct vt_window *vw;
154 	struct terminal *tm;
155 	struct vt_font *vf;
156 	struct winsize wsz;
157 	term_pos_t size;
158 	unsigned int i;
159 
160 	if (!vt_draw_logo_cpus)
161 		return;
162 	if (!vty_enabled(VTY_VT))
163 		return;
164 	if (!vt_splash_cpu)
165 		return;
166 
167 	vd = &vt_consdev;
168 	VT_LOCK(vd);
169 	if ((vd->vd_flags & (VDF_DEAD | VDF_TEXTMODE)) != 0) {
170 		VT_UNLOCK(vd);
171 		return;
172 	}
173 	vt_draw_logo_cpus = 0;
174 	VT_UNLOCK(vd);
175 
176 	for (i = 0; i < VT_MAXWINDOWS; i++) {
177 		vw = vd->vd_windows[i];
178 		if (vw == NULL)
179 			continue;
180 		tm = vw->vw_terminal;
181 		vf = vw->vw_font;
182 		if (vf == NULL)
183 			continue;
184 
185 		vt_termsize(vd, vf, &size);
186 		vt_winsize(vd, vf, &wsz);
187 
188 		/* Resize screen buffer and terminal. */
189 		terminal_mute(tm, 1);
190 		vtbuf_grow(&vw->vw_buf, &size, vw->vw_buf.vb_history_size);
191 		terminal_set_winsize_blank(tm, &wsz, 0, NULL);
192 		terminal_set_cursor(tm, &vw->vw_buf.vb_cursor);
193 		terminal_mute(tm, 0);
194 
195 		VT_LOCK(vd);
196 		vt_compute_drawable_area(vw);
197 
198 		if (vd->vd_curwindow == vw) {
199 			vd->vd_flags |= VDF_INVALID;
200 			vt_resume_flush_timer(vw, 0);
201 		}
202 		VT_UNLOCK(vd);
203 	}
204 }
205 
206 static void
207 vt_init_logos(void *dummy)
208 {
209 	struct vt_device *vd;
210 	struct vt_window *vw;
211 	struct terminal *tm;
212 	struct vt_font *vf;
213 	struct winsize wsz;
214 	term_pos_t size;
215 
216 	if (!vty_enabled(VTY_VT))
217 		return;
218 	if (!vt_splash_cpu)
219 		return;
220 
221 	vd = &vt_consdev;
222 	if (vd == NULL)
223 		return;
224 	vw = vd->vd_curwindow;
225 	if (vw == NULL)
226 		return;
227 	tm = vw->vw_terminal;
228 	if (tm == NULL)
229 		return;
230 	vf = vw->vw_font;
231 	if (vf == NULL)
232 		return;
233 
234 	VT_LOCK(vd);
235 	if ((vd->vd_flags & VDF_INITIALIZED) == 0)
236 		goto out;
237 	if ((vd->vd_flags & (VDF_DEAD | VDF_TEXTMODE)) != 0)
238 		goto out;
239 	if (vd->vd_height <= vt_logo_sprite_height)
240 		goto out;
241 
242 	vt_draw_logo_cpus = 1;
243 	VT_UNLOCK(vd);
244 
245 	vt_termsize(vd, vf, &size);
246 	vt_winsize(vd, vf, &wsz);
247 
248 	/* Resize screen buffer and terminal. */
249 	terminal_mute(tm, 1);
250 	vtbuf_grow(&vw->vw_buf, &size, vw->vw_buf.vb_history_size);
251 	terminal_set_winsize_blank(tm, &wsz, 0, NULL);
252 	terminal_set_cursor(tm, &vw->vw_buf.vb_cursor);
253 	terminal_mute(tm, 0);
254 
255 	VT_LOCK(vd);
256 	vt_compute_drawable_area(vw);
257 
258 	if (vd->vd_curwindow == vw) {
259 		vd->vd_flags |= VDF_INVALID;
260 		vt_resume_flush_timer(vw, 0);
261 	}
262 
263 	TIMEOUT_TASK_INIT(taskqueue_thread, &vt_splash_cpu_fini_task, 0,
264 	    vt_fini_logos, NULL);
265 	taskqueue_enqueue_timeout(taskqueue_thread, &vt_splash_cpu_fini_task,
266 	    vt_splash_cpu_duration * hz);
267 
268 out:
269 	VT_UNLOCK(vd);
270 }
271 SYSINIT(vt_logos, SI_SUB_TASKQ, SI_ORDER_ANY, vt_init_logos, NULL);
272