aboutsummaryrefslogtreecommitdiff
path: root/sys/dev/vt/vt_cpulogos.c
blob: c62b13a9097a4fa7882b27d15b63c1ca035f61f5 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
/*-
 * Copyright (c) 2015 Conrad Meyer <cem@FreeBSD.org>
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 * SUCH DAMAGE.
 */

#include <sys/cdefs.h>
__FBSDID("$FreeBSD$");

#include <sys/param.h>
#include <sys/callout.h>
#include <sys/cons.h>
#include <sys/lock.h>
#include <sys/kernel.h>
#include <sys/mutex.h>
#include <sys/smp.h>
#include <sys/systm.h>
#include <sys/taskqueue.h>
#include <sys/terminal.h>

#include <dev/vt/vt.h>

extern const unsigned char vt_beastie_vga16[];
extern const unsigned char vt_beastie2_vga16[];
extern const unsigned char vt_orb_vga16[];

static struct timeout_task vt_splash_cpu_fini_task;

static inline unsigned char
vt_vga2bsd(unsigned char vga)
{
	static const unsigned char lut[8] = {
		0,
		4,	/* 1 and 4 swap */
		2,
		6,	/* 3 and 6 swap */
		1,	/* 4 and 1 swap */
		5,
		3,	/* 6 and 3 swap */
		7,
	};
	unsigned int bright;

	bright = (vga & 0x8);
	return (lut[vga & 0x7] | bright);
}

static void
vt_draw_2_vga16_px(struct vt_device *vd, vt_axis_t x, vt_axis_t y,
    unsigned char color)
{

	vd->vd_driver->vd_setpixel(vd, x, y, vt_vga2bsd(color >> 4));
	vd->vd_driver->vd_setpixel(vd, x + 1, y, vt_vga2bsd(color & 0xf));
}

static void
vt_draw_1_logo(struct vt_device *vd, vt_axis_t top, vt_axis_t left)
{
	const unsigned char rle_sent = 0x16, *data;
	unsigned int xy, run, runcolor, i;

	switch (vt_splash_cpu_style) {
	case VT_LOGOS_DRAW_ALT_BEASTIE:
		data = vt_beastie2_vga16;
		break;
	case VT_LOGOS_DRAW_ORB:
		data = vt_orb_vga16;
		break;
	case VT_LOGOS_DRAW_BEASTIE:
		/* FALLTHROUGH */
	default:
		data = vt_beastie_vga16;
		break;
	}

	/* Decode basic RLE (gets us to 30-40% of uncompressed data size): */
	for (i = 0, xy = 0; xy < vt_logo_sprite_height * vt_logo_sprite_width;) {
		if (data[i] == rle_sent) {
			runcolor = data[i + 1];
			run = data[i + 2];

			for (; run; run--, xy += 2)
				vt_draw_2_vga16_px(vd,
				    left + (xy % vt_logo_sprite_width),
				    top + (xy / vt_logo_sprite_width),
				    runcolor);

			i += 3;
		} else {
			vt_draw_2_vga16_px(vd, left + (xy % vt_logo_sprite_width),
			    top + (xy / vt_logo_sprite_width), data[i]);

			i++;
			xy += 2;
		}
	}
}

void
vtterm_draw_cpu_logos(struct vt_device *vd)
{
	unsigned int ncpu, i;
	vt_axis_t left;
	struct terminal *tm = vd->vd_curwindow->vw_terminal;
	const teken_attr_t *a;

	if (vt_splash_ncpu)
		ncpu = vt_splash_ncpu;
	else {
		ncpu = mp_ncpus;
		if (ncpu < 1)
			ncpu = 1;
	}

	a = teken_get_curattr(&tm->tm_emulator);
	if (vd->vd_driver->vd_drawrect)
		vd->vd_driver->vd_drawrect(vd, 0, 0, vd->vd_width,
		    vt_logo_sprite_height, 1, a->ta_bgcolor);
	/*
	 * Blank is okay because we only ever draw beasties on full screen
	 * refreshes.
	 */
	else if (vd->vd_driver->vd_blank)
		vd->vd_driver->vd_blank(vd, a->ta_bgcolor);

	ncpu = MIN(ncpu, vd->vd_width / vt_logo_sprite_width);
	for (i = 0, left = 0; i < ncpu; left += vt_logo_sprite_width, i++)
		vt_draw_1_logo(vd, 0, left);
}

static void
vt_fini_logos(void *dummy __unused, int pending __unused)
{
	struct vt_device *vd;
	struct vt_window *vw;
	struct terminal *tm;
	struct vt_font *vf;
	struct winsize wsz;
	term_pos_t size;
	unsigned int i;

	if (!vt_draw_logo_cpus)
		return;
	if (!vty_enabled(VTY_VT))
		return;
	if (!vt_splash_cpu)
		return;

	vd = &vt_consdev;
	VT_LOCK(vd);
	if ((vd->vd_flags & (VDF_DEAD | VDF_TEXTMODE)) != 0) {
		VT_UNLOCK(vd);
		return;
	}
	vt_draw_logo_cpus = 0;
	VT_UNLOCK(vd);

	for (i = 0; i < VT_MAXWINDOWS; i++) {
		vw = vd->vd_windows[i];
		if (vw == NULL)
			continue;
		tm = vw->vw_terminal;
		vf = vw->vw_font;
		if (vf == NULL)
			continue;

		vt_termsize(vd, vf, &size);
		vt_winsize(vd, vf, &wsz);

		/* Resize screen buffer and terminal. */
		terminal_mute(tm, 1);
		vtbuf_grow(&vw->vw_buf, &size, vw->vw_buf.vb_history_size);
		terminal_set_winsize_blank(tm, &wsz, 0, NULL);
		terminal_set_cursor(tm, &vw->vw_buf.vb_cursor);
		terminal_mute(tm, 0);

		VT_LOCK(vd);
		vt_compute_drawable_area(vw);

		if (vd->vd_curwindow == vw) {
			vd->vd_flags |= VDF_INVALID;
			vt_resume_flush_timer(vw, 0);
		}
		VT_UNLOCK(vd);
	}
}

static void
vt_init_logos(void *dummy)
{
	struct vt_device *vd;
	struct vt_window *vw;
	struct terminal *tm;
	struct vt_font *vf;
	struct winsize wsz;
	term_pos_t size;

	if (!vty_enabled(VTY_VT))
		return;
	if (!vt_splash_cpu)
		return;

	tm = &vt_consterm;
	vw = tm->tm_softc;
	if (vw == NULL)
		return;
	vd = vw->vw_device;
	if (vd == NULL)
		return;
	vf = vw->vw_font;
	if (vf == NULL)
		return;

	VT_LOCK(vd);
	if ((vd->vd_flags & VDF_INITIALIZED) == 0)
		goto out;
	if ((vd->vd_flags & (VDF_DEAD | VDF_TEXTMODE)) != 0)
		goto out;
	if (vd->vd_height <= vt_logo_sprite_height)
		goto out;

	vt_draw_logo_cpus = 1;
	VT_UNLOCK(vd);

	vt_termsize(vd, vf, &size);
	vt_winsize(vd, vf, &wsz);

	/* Resize screen buffer and terminal. */
	terminal_mute(tm, 1);
	vtbuf_grow(&vw->vw_buf, &size, vw->vw_buf.vb_history_size);
	terminal_set_winsize_blank(tm, &wsz, 0, NULL);
	terminal_set_cursor(tm, &vw->vw_buf.vb_cursor);
	terminal_mute(tm, 0);

	VT_LOCK(vd);
	vt_compute_drawable_area(vw);

	if (vd->vd_curwindow == vw) {
		vd->vd_flags |= VDF_INVALID;
		vt_resume_flush_timer(vw, 0);
	}

	TIMEOUT_TASK_INIT(taskqueue_thread, &vt_splash_cpu_fini_task, 0,
	    vt_fini_logos, NULL);
	taskqueue_enqueue_timeout(taskqueue_thread, &vt_splash_cpu_fini_task,
	    vt_splash_cpu_duration * hz);

out:
	VT_UNLOCK(vd);
}
SYSINIT(vt_logos, SI_SUB_TASKQ, SI_ORDER_ANY, vt_init_logos, NULL);