MFH (r293034, r29304): plasma screensaver

This commit is contained in:
des 2016-01-26 08:55:31 +00:00
parent dd0e351e29
commit 89be55aaa9
6 changed files with 500 additions and 1 deletions

View File

@ -26,7 +26,7 @@
.\"
.\" $FreeBSD$
.\"
.Dd November 29, 2010
.Dd December 31, 2015
.Dt SPLASH 4
.Os
.Sh NAME
@ -130,6 +130,8 @@ the screen will also be powered off.
Animated graphical
.Fx
logo.
.It Pa plasma_saver.ko
Draws an animated interference pattern.
.It Pa rain_saver.ko
Draws a shower on the screen.
.It Pa snake_saver.ko
@ -282,6 +284,14 @@ based on the
code, with some additional inspiration from the
.Pa daemon_saver
code.
The
.Pa logo_saver ,
.Pa plasma_saver ,
.Pa rain_saver
and
.Pa warp_saver
modules were written by
.An Dag-Erling Sm\(/orgrav Aq Mt des@FreeBSD.org .
.Sh CAVEATS
Both the splash screen and the screen saver work with
.Xr syscons 4

View File

@ -0,0 +1,155 @@
/*-
* Copyright (c) 2015 Dag-Erling Smørgrav
* 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.
*
* $FreeBSD$
*/
#ifdef _KERNEL
#include <sys/libkern.h>
#else
#include <stdio.h>
#include <strings.h>
#endif
#include "fp16.h"
/*
* Compute the quare root of x, using Newton's method with 2^(log2(x)/2)
* as the initial estimate.
*/
fp16_t
fp16_sqrt(fp16_t x)
{
fp16_t y, delta;
signed int log2x;
/* special case */
if (x == 0)
return (0);
/* shift toward 0 by half the logarithm */
log2x = flsl(x) - 1;
if (log2x >= 16) {
y = x >> (log2x - 16) / 2;
} else {
#if 0
y = x << (16 - log2x) / 2;
#else
/* XXX for now, return 0 for anything < 1 */
return (0);
#endif
}
while (y > 0) {
/* delta = y^2 / 2y */
delta = fp16_div(fp16_sub(fp16_mul(y, y), x), y * 2);
if (delta == 0)
break;
y = fp16_sub(y, delta);
}
return (y);
}
static fp16_t fp16_sin_table[256] = {
0, 402, 804, 1206, 1608, 2010, 2412, 2814,
3215, 3617, 4018, 4420, 4821, 5222, 5622, 6023,
6423, 6823, 7223, 7623, 8022, 8421, 8819, 9218,
9616, 10013, 10410, 10807, 11204, 11600, 11995, 12390,
12785, 13179, 13573, 13966, 14359, 14751, 15142, 15533,
15923, 16313, 16702, 17091, 17479, 17866, 18253, 18638,
19024, 19408, 19792, 20175, 20557, 20938, 21319, 21699,
22078, 22456, 22833, 23210, 23586, 23960, 24334, 24707,
25079, 25450, 25820, 26189, 26557, 26925, 27291, 27656,
28020, 28383, 28745, 29105, 29465, 29824, 30181, 30538,
30893, 31247, 31600, 31952, 32302, 32651, 32999, 33346,
33692, 34036, 34379, 34721, 35061, 35400, 35738, 36074,
36409, 36743, 37075, 37406, 37736, 38064, 38390, 38716,
39039, 39362, 39682, 40002, 40319, 40636, 40950, 41263,
41575, 41885, 42194, 42501, 42806, 43110, 43412, 43712,
44011, 44308, 44603, 44897, 45189, 45480, 45768, 46055,
46340, 46624, 46906, 47186, 47464, 47740, 48015, 48288,
48558, 48828, 49095, 49360, 49624, 49886, 50146, 50403,
50660, 50914, 51166, 51416, 51665, 51911, 52155, 52398,
52639, 52877, 53114, 53348, 53581, 53811, 54040, 54266,
54491, 54713, 54933, 55152, 55368, 55582, 55794, 56004,
56212, 56417, 56621, 56822, 57022, 57219, 57414, 57606,
57797, 57986, 58172, 58356, 58538, 58718, 58895, 59070,
59243, 59414, 59583, 59749, 59913, 60075, 60235, 60392,
60547, 60700, 60850, 60998, 61144, 61288, 61429, 61568,
61705, 61839, 61971, 62100, 62228, 62353, 62475, 62596,
62714, 62829, 62942, 63053, 63162, 63268, 63371, 63473,
63571, 63668, 63762, 63854, 63943, 64030, 64115, 64197,
64276, 64353, 64428, 64501, 64571, 64638, 64703, 64766,
64826, 64884, 64939, 64992, 65043, 65091, 65136, 65179,
65220, 65258, 65294, 65327, 65358, 65386, 65412, 65436,
65457, 65475, 65491, 65505, 65516, 65524, 65531, 65534,
};
/*
* Compute the sine of theta.
*/
fp16_t
fp16_sin(fp16_t theta)
{
unsigned int i;
i = 1024 * (theta % FP16_2PI) / FP16_2PI;
switch (i / 256) {
case 0:
return (fp16_sin_table[i % 256]);
case 1:
return (fp16_sin_table[255 - i % 256]);
case 2:
return (-fp16_sin_table[i % 256]);
case 3:
return (-fp16_sin_table[255 - i % 256]);
default:
/* inconceivable! */
return (0);
}
}
/*
* Compute the cosine of theta.
*/
fp16_t
fp16_cos(fp16_t theta)
{
unsigned int i;
i = 1024 * (theta % FP16_2PI) / FP16_2PI;
switch (i / 256) {
case 0:
return (fp16_sin_table[255 - i % 256]);
case 1:
return (-fp16_sin_table[i % 256]);
case 2:
return (-fp16_sin_table[255 - i % 256]);
case 3:
return (fp16_sin_table[i % 256]);
default:
/* inconceivable! */
return (0);
}
}

View File

@ -0,0 +1,85 @@
/*-
* Copyright (c) 2015 Dag-Erling Smørgrav
* 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.
*
* $FreeBSD$
*/
#ifndef FP16_H_INCLUDED
#define FP16_H_INCLUDED
typedef signed long long fp16_t;
#define ItoFP16(n) ((signed long long)(n) << 16)
#define FP16toI(n) ((signed long long)(n) >> 16)
#ifndef _KERNEL
#define FP16toF(n) ((n) / 65536.0)
#endif
/* add a and b */
static inline fp16_t
fp16_add(fp16_t a, fp16_t b)
{
return (a + b);
}
/* subtract b from a */
static inline fp16_t
fp16_sub(fp16_t a, fp16_t b)
{
return (a - b);
}
/* multiply a by b */
static inline fp16_t
fp16_mul(fp16_t a, fp16_t b)
{
return (a * b >> 16);
}
/* divide a by b */
static inline fp16_t
fp16_div(fp16_t a, fp16_t b)
{
return ((a << 16) / b);
}
/* square root */
fp16_t fp16_sqrt(fp16_t);
#define FP16_2PI 411774
#define FP16_PI 205887
#define FP16_PI_2 102943
#define FP16_PI_4 51471
/* sine and cosine */
fp16_t fp16_sin(fp16_t);
fp16_t fp16_cos(fp16_t);
#endif

View File

@ -0,0 +1,239 @@
/*-
* Copyright (c) 2015 Dag-Erling Smørgrav
* 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.
*
* $FreeBSD$
*
* To CJA, in appreciation of Nighthawk brunches past and future.
*/
#include <sys/param.h>
#include <sys/systm.h>
#include <sys/kernel.h>
#include <sys/module.h>
#include <sys/syslog.h>
#include <sys/consio.h>
#include <sys/fbio.h>
#include <dev/fb/fbreg.h>
#include <dev/fb/splashreg.h>
#include <dev/syscons/syscons.h>
#define SAVER_NAME "plasma_saver"
#include "fp16.h"
/*
* Preferred video modes
*/
static int modes[] = {
M_VGA_CG640,
M_PC98_PEGC640x480,
M_PC98_PEGC640x400,
M_VGA_CG320,
-1
};
/*
* Display parameters
*/
static unsigned char *vid;
static unsigned int banksize, scrmode, scrw, scrh;
static unsigned int blanked;
/*
* List of foci
*/
#define FOCI 3
static struct {
int x, y; /* coordinates */
int vx, vy; /* velocity */
} plasma_foci[FOCI];
/*
* Palette
*/
static struct {
unsigned char r, g, b;
} plasma_pal[256];
/*
* Draw a new frame
*/
static void
plasma_update(video_adapter_t *adp)
{
unsigned int x, y; /* coordinates */
signed int dx, dy; /* horizontal / vertical distance */
fp16_t sqd, d; /* square of distance and distance */
fp16_t m; /* magnitude */
unsigned int org, off; /* origin and offset */
unsigned int i; /* loop index */
/* switch to bank 0 */
vidd_set_win_org(adp, 0);
/* for each scan line */
for (y = org = off = 0; y < scrh; ++y) {
/* for each pixel on scan line */
for (x = 0; x < scrw; ++x, ++off) {
/* for each focus */
for (i = m = 0; i < FOCI; ++i) {
dx = x - plasma_foci[i].x;
dy = y - plasma_foci[i].y;
sqd = ItoFP16(dx * dx + dy * dy);
d = fp16_sqrt(sqd);
/* divide by 4 to stretch out the pattern */
m = fp16_sub(m, fp16_cos(d / 4));
}
/*
* m is now in the range +/- FOCI, but we need a
* value between 0 and 255. We scale to +/- 127
* and add 127, which moves it into the range [0,
* 254].
*/
m = fp16_mul(m, ItoFP16(127));
m = fp16_div(m, ItoFP16(FOCI));
m = fp16_add(m, ItoFP16(127));
/* switch banks if necessary */
if (off > banksize) {
off -= banksize;
org += banksize;
vidd_set_win_org(adp, org);
}
/* plot */
vid[off] = FP16toI(m);
}
}
/* now move the foci */
for (i = 0; i < FOCI; ++i) {
plasma_foci[i].x += plasma_foci[i].vx;
if (plasma_foci[i].x < 0) {
/* bounce against left wall */
plasma_foci[i].vx = -plasma_foci[i].vx;
plasma_foci[i].x = -plasma_foci[i].x;
} else if (plasma_foci[i].x >= scrw) {
/* bounce against right wall */
plasma_foci[i].vx = -plasma_foci[i].vx;
plasma_foci[i].x = scrw - (plasma_foci[i].x - scrw);
}
plasma_foci[i].y += plasma_foci[i].vy;
if (plasma_foci[i].y < 0) {
/* bounce against ceiling */
plasma_foci[i].vy = -plasma_foci[i].vy;
plasma_foci[i].y = -plasma_foci[i].y;
} else if (plasma_foci[i].y >= scrh) {
/* bounce against floor */
plasma_foci[i].vy = -plasma_foci[i].vy;
plasma_foci[i].y = scrh - (plasma_foci[i].y - scrh);
}
}
}
/*
* Start or stop the screensaver
*/
static int
plasma_saver(video_adapter_t *adp, int blank)
{
int pl;
if (blank) {
/* switch to graphics mode */
if (blanked <= 0) {
pl = splhigh();
vidd_set_mode(adp, scrmode);
vidd_load_palette(adp, (unsigned char *)plasma_pal);
vidd_set_border(adp, 0);
blanked++;
vid = (unsigned char *)adp->va_window;
banksize = adp->va_window_size;
splx(pl);
vidd_clear(adp);
}
/* update display */
plasma_update(adp);
} else {
blanked = 0;
}
return (0);
}
/*
* Initialize on module load
*/
static int
plasma_init(video_adapter_t *adp)
{
video_info_t info;
int i;
/* select video mode */
for (i = 0; modes[i] >= 0; ++i)
if (vidd_get_info(adp, modes[i], &info) == 0)
break;
if (modes[i] < 0) {
log(LOG_NOTICE, "%s: no supported video modes\n", SAVER_NAME);
return (ENODEV);
}
scrmode = modes[i];
scrw = info.vi_width;
scrh = info.vi_height;
/* initialize the palette */
for (i = 0; i < 256; ++i)
plasma_pal[i].r = plasma_pal[i].g = plasma_pal[i].b = i;
/* randomize the foci */
for (i = 0; i < FOCI; i++) {
plasma_foci[i].x = random() % scrw;
plasma_foci[i].y = random() % scrh;
plasma_foci[i].vx = random() % 5 - 2;
plasma_foci[i].vy = random() % 5 - 2;
}
return (0);
}
/*
* Clean up before module unload
*/
static int
plasma_term(video_adapter_t *adp)
{
return (0);
}
/*
* Boilerplate
*/
static scrn_saver_t plasma_module = {
SAVER_NAME,
plasma_init,
plasma_term,
plasma_saver,
NULL
};
SAVER_MODULE(plasma_saver, plasma_module);

View File

@ -9,6 +9,7 @@ SUBDIR= ${_apm} \
${_fire} \
green \
${_logo} \
${_plasma} \
${_rain} \
${_snake} \
${_star} \
@ -25,6 +26,7 @@ _daemon= daemon
_dragon= dragon
_fire= fire
_logo= logo
_plasma= plasma
_rain= rain
_snake= snake
_star= star

View File

@ -0,0 +1,8 @@
# $FreeBSD$
.PATH: ${.CURDIR}/../../../dev/syscons/plasma
KMOD= plasma_saver
SRCS= fp16.c plasma_saver.c
.include <bsd.kmod.mk>