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