Home | History | Annotate | Line # | Download | only in hpc
video_subr.c revision 1.2
      1  1.2  uch /*	$NetBSD: video_subr.c,v 1.2 2001/06/04 18:59:32 uch Exp $	*/
      2  1.1  uch 
      3  1.1  uch /*-
      4  1.1  uch  * Copyright (c) 2000 The NetBSD Foundation, Inc.
      5  1.1  uch  * All rights reserved.
      6  1.1  uch  *
      7  1.1  uch  * This code is derived from software contributed to The NetBSD Foundation
      8  1.1  uch  * by UCHIYAMA Yasushi.
      9  1.1  uch  *
     10  1.1  uch  * Redistribution and use in source and binary forms, with or without
     11  1.1  uch  * modification, are permitted provided that the following conditions
     12  1.1  uch  * are met:
     13  1.1  uch  * 1. Redistributions of source code must retain the above copyright
     14  1.1  uch  *    notice, this list of conditions and the following disclaimer.
     15  1.1  uch  * 2. Redistributions in binary form must reproduce the above copyright
     16  1.1  uch  *    notice, this list of conditions and the following disclaimer in the
     17  1.1  uch  *    documentation and/or other materials provided with the distribution.
     18  1.1  uch  * 3. All advertising materials mentioning features or use of this software
     19  1.1  uch  *    must display the following acknowledgement:
     20  1.1  uch  *        This product includes software developed by the NetBSD
     21  1.1  uch  *        Foundation, Inc. and its contributors.
     22  1.1  uch  * 4. Neither the name of The NetBSD Foundation nor the names of its
     23  1.1  uch  *    contributors may be used to endorse or promote products derived
     24  1.1  uch  *    from this software without specific prior written permission.
     25  1.1  uch  *
     26  1.1  uch  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     27  1.1  uch  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     28  1.1  uch  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     29  1.1  uch  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     30  1.1  uch  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     31  1.1  uch  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     32  1.1  uch  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     33  1.1  uch  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     34  1.1  uch  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     35  1.1  uch  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     36  1.1  uch  * POSSIBILITY OF SUCH DAMAGE.
     37  1.1  uch  */
     38  1.1  uch 
     39  1.1  uch #include <sys/param.h>
     40  1.1  uch #include <sys/systm.h>
     41  1.1  uch #include <sys/malloc.h>
     42  1.1  uch 
     43  1.1  uch #include <machine/bootinfo.h>
     44  1.1  uch 
     45  1.1  uch #include <dev/hpc/video_subr.h>
     46  1.1  uch 
     47  1.1  uch #define BPP2 ({								\
     48  1.1  uch 	u_int8_t bitmap;						\
     49  1.1  uch 	bitmap = *(volatile u_int8_t*)addr;				\
     50  1.1  uch 	*(volatile u_int8_t*)addr =					\
     51  1.1  uch 		(bitmap & ~(0x3 << ((3 - (x % 4)) * 2)));		\
     52  1.1  uch })
     53  1.1  uch 
     54  1.1  uch #define BPP4 ({								\
     55  1.1  uch 	u_int8_t bitmap;						\
     56  1.1  uch 	bitmap = *(volatile u_int8_t*)addr;				\
     57  1.1  uch 	*(volatile u_int8_t*)addr =					\
     58  1.1  uch 		(bitmap & ~(0xf << ((1 - (x % 2)) * 4)));		\
     59  1.1  uch })
     60  1.1  uch 
     61  1.1  uch #define BPP8 ({								\
     62  1.1  uch 	*(volatile u_int8_t*)addr = 0xff;				\
     63  1.1  uch })
     64  1.1  uch 
     65  1.1  uch #define BRESENHAM(a, b, c, d, func) ({					\
     66  1.1  uch 	u_int32_t fbaddr = vc->vc_fbvaddr;				\
     67  1.1  uch 	u_int32_t fbwidth = vc->vc_fbwidth;				\
     68  1.1  uch 	u_int32_t fbdepth = vc->vc_fbdepth;				\
     69  1.1  uch 	len = a, step = b -1;						\
     70  1.1  uch 	if (step == 0)							\
     71  1.1  uch 		return;							\
     72  1.1  uch 	kstep = len == 0 ? 0 : 1;					\
     73  1.1  uch 	for (i = k = 0, j = step / 2; i <= step; i++) {			\
     74  1.1  uch 		x = xbase c;						\
     75  1.1  uch 		y = ybase d;						\
     76  1.1  uch 		addr = fbaddr + (((y * fbwidth + x) * fbdepth) >> 3);	\
     77  1.1  uch 		func;							\
     78  1.1  uch 		j -= len;						\
     79  1.1  uch 		while (j < 0) {						\
     80  1.1  uch 			j += step;					\
     81  1.1  uch 			k += kstep;					\
     82  1.1  uch 		}							\
     83  1.1  uch 	}								\
     84  1.1  uch })
     85  1.1  uch 
     86  1.1  uch #define DRAWLINE(func) ({						\
     87  1.1  uch 	if (x < 0) {							\
     88  1.1  uch 		if (y < 0) {						\
     89  1.1  uch 			if (_y < _x) {					\
     90  1.1  uch 				BRESENHAM(_y, _x, -i, -k, func);	\
     91  1.1  uch 			} else {					\
     92  1.1  uch 				BRESENHAM(_x, _y, -k, -i, func);	\
     93  1.1  uch 			}						\
     94  1.1  uch 		} else {						\
     95  1.1  uch 			if (_y < _x) {					\
     96  1.1  uch 				BRESENHAM(_y, _x, -i, +k, func);	\
     97  1.1  uch 			} else {					\
     98  1.1  uch 				BRESENHAM(_x, _y, -k, +i, func);	\
     99  1.1  uch 			}						\
    100  1.1  uch 		}							\
    101  1.1  uch 	} else {							\
    102  1.1  uch 		if (y < 0) {						\
    103  1.1  uch 			if (_y < _x) {					\
    104  1.1  uch 				BRESENHAM(_y, _x, +i, -k, func);	\
    105  1.1  uch 			} else {					\
    106  1.1  uch 				BRESENHAM(_x, _y, +k, -i, func);	\
    107  1.1  uch 			}						\
    108  1.1  uch 		} else {						\
    109  1.1  uch 			if (_y < _x) {					\
    110  1.1  uch 				BRESENHAM(_y, _x, +i, +k, func);	\
    111  1.1  uch 			} else {					\
    112  1.1  uch 				BRESENHAM(_x, _y, +k, +i, func);	\
    113  1.1  uch 			}						\
    114  1.1  uch 		}							\
    115  1.1  uch 	}								\
    116  1.1  uch })
    117  1.1  uch 
    118  1.1  uch #define LINEFUNC(b)							\
    119  1.1  uch static void linebpp##b (struct video_chip *, int, int, int, int);	\
    120  1.1  uch static void								\
    121  1.1  uch linebpp##b##(vc, x0, y0, x1, y1)					\
    122  1.1  uch 	struct video_chip *vc;						\
    123  1.1  uch 	int x0, y0, x1, y1;						\
    124  1.1  uch {									\
    125  1.1  uch 	u_int32_t addr;							\
    126  1.1  uch 	int i, j, k, len, step, kstep;					\
    127  1.1  uch 	int x, _x, y, _y;						\
    128  1.1  uch 	int xbase, ybase;						\
    129  1.1  uch 	x = x1 - x0;							\
    130  1.1  uch 	y = y1 - y0;							\
    131  1.1  uch 	_x = abs(x);							\
    132  1.1  uch 	_y = abs(y);							\
    133  1.1  uch 	xbase = x0;							\
    134  1.1  uch 	ybase = y0;							\
    135  1.1  uch 	DRAWLINE(BPP##b##);						\
    136  1.1  uch }
    137  1.1  uch 
    138  1.1  uch #define DOTFUNC(b)							\
    139  1.1  uch static void dotbpp##b (struct video_chip *, int, int);			\
    140  1.1  uch static void								\
    141  1.1  uch dotbpp##b##(vc, x, y)							\
    142  1.1  uch 	struct video_chip *vc;						\
    143  1.1  uch 	int x, y;							\
    144  1.1  uch {									\
    145  1.1  uch 	u_int32_t addr;							\
    146  1.1  uch 	addr = vc->vc_fbvaddr + (((y * vc->vc_fbwidth + x) *		\
    147  1.1  uch 				 vc->vc_fbdepth) >> 3);			\
    148  1.1  uch 	BPP##b;								\
    149  1.1  uch }
    150  1.1  uch 
    151  1.1  uch LINEFUNC(2)
    152  1.1  uch LINEFUNC(4)
    153  1.1  uch LINEFUNC(8)
    154  1.1  uch DOTFUNC(2)
    155  1.1  uch DOTFUNC(4)
    156  1.1  uch DOTFUNC(8)
    157  1.1  uch static void linebpp_unimpl(struct video_chip *, int, int, int, int);
    158  1.1  uch static void dotbpp_unimpl(struct video_chip *, int, int);
    159  1.1  uch 
    160  1.1  uch int
    161  1.1  uch cmap_work_alloc(u_int8_t **r, u_int8_t **g, u_int8_t **b, u_int32_t **rgb,
    162  1.2  uch     int cnt)
    163  1.1  uch {
    164  1.1  uch 	KASSERT(r && g && b && rgb && LEGAL_CLUT_INDEX(cnt - 1));
    165  1.1  uch 
    166  1.1  uch 	*r = malloc(cnt * sizeof(u_int8_t), M_DEVBUF, M_WAITOK);
    167  1.1  uch 	*g = malloc(cnt * sizeof(u_int8_t), M_DEVBUF, M_WAITOK);
    168  1.1  uch 	*b = malloc(cnt * sizeof(u_int8_t), M_DEVBUF, M_WAITOK);
    169  1.1  uch 	*rgb = malloc(cnt * sizeof(u_int32_t), M_DEVBUF, M_WAITOK);
    170  1.1  uch 
    171  1.1  uch 	return (!(*r && *g && *b && *rgb));
    172  1.1  uch }
    173  1.1  uch 
    174  1.1  uch void
    175  1.1  uch cmap_work_free(u_int8_t *r, u_int8_t *g, u_int8_t *b, u_int32_t *rgb)
    176  1.1  uch {
    177  1.1  uch 	if (r)
    178  1.1  uch 		free(r, M_DEVBUF);
    179  1.1  uch 	if (g)
    180  1.1  uch 		free(g, M_DEVBUF);
    181  1.1  uch 	if (b)
    182  1.1  uch 		free(b, M_DEVBUF);
    183  1.1  uch 	if (rgb)
    184  1.1  uch 		free(rgb, M_DEVBUF);
    185  1.1  uch }
    186  1.1  uch 
    187  1.1  uch void
    188  1.1  uch rgb24_compose(u_int32_t *rgb24, u_int8_t *r, u_int8_t *g, u_int8_t *b, int cnt)
    189  1.1  uch {
    190  1.1  uch 	int i;
    191  1.1  uch 	KASSERT(rgb24 && r && g && b && LEGAL_CLUT_INDEX(cnt - 1));
    192  1.1  uch 
    193  1.1  uch 	for (i = 0; i < cnt; i++) {
    194  1.1  uch 		*rgb24++ = RGB24(r[i], g[i], b[i]);
    195  1.1  uch 	}
    196  1.1  uch }
    197  1.1  uch 
    198  1.1  uch void
    199  1.1  uch rgb24_decompose(u_int32_t *rgb24, u_int8_t *r, u_int8_t *g, u_int8_t *b,
    200  1.2  uch     int cnt)
    201  1.1  uch {
    202  1.1  uch 	int i;
    203  1.1  uch 	KASSERT(rgb24 && r && g && b && LEGAL_CLUT_INDEX(cnt - 1));
    204  1.1  uch 
    205  1.1  uch 	for (i = 0; i < cnt; i++) {
    206  1.1  uch 		u_int32_t rgb = *rgb24++;
    207  1.1  uch 		*r++ = (rgb >> 16) & 0xff;
    208  1.1  uch 		*g++ = (rgb >> 8) & 0xff;
    209  1.1  uch 		*b++ = rgb & 0xff;
    210  1.1  uch 	}
    211  1.1  uch }
    212  1.1  uch 
    213  1.1  uch /*
    214  1.1  uch  * Debug routines.
    215  1.1  uch  */
    216  1.1  uch void
    217  1.1  uch video_calibration_pattern(struct video_chip *vc)
    218  1.1  uch {
    219  1.1  uch 	int x, y;
    220  1.1  uch 
    221  1.1  uch 	x = vc->vc_fbwidth - 40;
    222  1.1  uch 	y = vc->vc_fbheight - 40;
    223  1.1  uch 	video_line(vc, 40, 40, x , 40);
    224  1.1  uch 	video_line(vc, x , 40, x , y );
    225  1.1  uch 	video_line(vc, x , y , 40, y );
    226  1.1  uch 	video_line(vc, 40, y , 40, 40);
    227  1.1  uch 	video_line(vc, 40, 40, x , y );
    228  1.1  uch 	video_line(vc, x,  40, 40, y );
    229  1.1  uch }
    230  1.1  uch 
    231  1.1  uch static void
    232  1.1  uch linebpp_unimpl(struct video_chip *vc, int x0, int y0, int x1, int y1)
    233  1.1  uch {
    234  1.1  uch 	return;
    235  1.1  uch }
    236  1.1  uch 
    237  1.1  uch static void
    238  1.1  uch dotbpp_unimpl(struct video_chip *vc, int x, int y)
    239  1.1  uch {
    240  1.1  uch 	return;
    241  1.1  uch }
    242  1.1  uch 
    243  1.1  uch void
    244  1.1  uch video_attach_drawfunc(struct video_chip *vc)
    245  1.1  uch {
    246  1.1  uch 	switch (vc->vc_fbdepth) {
    247  1.1  uch 	default:
    248  1.1  uch 		vc->vc_drawline = linebpp_unimpl;
    249  1.1  uch 		vc->vc_drawdot = dotbpp_unimpl;
    250  1.1  uch 		break;
    251  1.1  uch 	case 8:
    252  1.1  uch 		vc->vc_drawline = linebpp8;
    253  1.1  uch 		vc->vc_drawdot = dotbpp8;
    254  1.1  uch 		break;
    255  1.1  uch 	case 4:
    256  1.1  uch 		vc->vc_drawline = linebpp4;
    257  1.1  uch 		vc->vc_drawdot = dotbpp4;
    258  1.1  uch 		break;
    259  1.1  uch 	case 2:
    260  1.1  uch 		vc->vc_drawline = linebpp2;
    261  1.1  uch 		vc->vc_drawdot = dotbpp2;
    262  1.1  uch 		break;
    263  1.1  uch 	}
    264  1.1  uch }
    265  1.1  uch 
    266  1.1  uch void
    267  1.1  uch video_line(struct video_chip *vc, int x0, int y0, int x1, int y1)
    268  1.1  uch {
    269  1.1  uch 	if (vc->vc_drawline)
    270  1.1  uch 		vc->vc_drawline(vc, x0, y0, x1, y1);
    271  1.1  uch }
    272  1.1  uch 
    273  1.1  uch void
    274  1.1  uch video_dot(struct video_chip *vc, int x, int y)
    275  1.1  uch {
    276  1.1  uch 	if (vc->vc_drawdot)
    277  1.1  uch 		vc->vc_drawdot(vc, x, y);
    278  1.1  uch }
    279  1.1  uch 
    280  1.1  uch int
    281  1.1  uch video_reverse_color()
    282  1.1  uch {
    283  1.1  uch 	struct {
    284  1.1  uch 		int reverse, normal;
    285  1.1  uch 	} ctype[] = {
    286  1.1  uch 		{ BIFB_D2_M2L_3,	BIFB_D2_M2L_0	},
    287  1.1  uch 		{ BIFB_D2_M2L_3x2,	BIFB_D2_M2L_0x2	},
    288  1.1  uch 		{ BIFB_D8_FF,		BIFB_D8_00	},
    289  1.1  uch 		{ BIFB_D16_FFFF,	BIFB_D16_0000,	},
    290  1.1  uch 		{ -1, -1 } /* terminator */
    291  1.1  uch 	}, *ctypep;
    292  1.1  uch 	u_int16_t fbtype;
    293  1.1  uch 
    294  1.1  uch 	/* check reverse color */
    295  1.1  uch 	fbtype = bootinfo->fb_type;
    296  1.1  uch 	for (ctypep = ctype; ctypep->normal != -1 ;  ctypep++) {
    297  1.1  uch 		if (fbtype == ctypep->normal) {
    298  1.1  uch 			return (0);
    299  1.1  uch 		} else if (fbtype == ctypep->reverse) {
    300  1.1  uch 			return (1);
    301  1.1  uch 		}
    302  1.1  uch 	}
    303  1.1  uch 	printf(": WARNING unknown frame buffer type 0x%04x.\n", fbtype);
    304  1.1  uch 	return (0);
    305  1.1  uch }
    306