Home | History | Annotate | Line # | Download | only in rasops
rasops.h revision 1.47
      1 /* 	$NetBSD: rasops.h,v 1.47 2019/08/10 01:24:17 rin Exp $ */
      2 
      3 /*-
      4  * Copyright (c) 1999 The NetBSD Foundation, Inc.
      5  * All rights reserved.
      6  *
      7  * This code is derived from software contributed to The NetBSD Foundation
      8  * by Andrew Doran.
      9  *
     10  * Redistribution and use in source and binary forms, with or without
     11  * modification, are permitted provided that the following conditions
     12  * are met:
     13  * 1. Redistributions of source code must retain the above copyright
     14  *    notice, this list of conditions and the following disclaimer.
     15  * 2. Redistributions in binary form must reproduce the above copyright
     16  *    notice, this list of conditions and the following disclaimer in the
     17  *    documentation and/or other materials provided with the distribution.
     18  *
     19  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     20  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     21  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     22  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     23  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     24  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     25  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     26  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     27  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     28  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     29  * POSSIBILITY OF SUCH DAMAGE.
     30  */
     31 
     32 #ifndef _RASOPS_H_
     33 #define _RASOPS_H_ 1
     34 
     35 #include <sys/param.h>
     36 
     37 #include <dev/wscons/wsconsio.h>
     38 #include <dev/wsfont/wsfont.h>
     39 
     40 /* For rasops_info::ri_flg */
     41 #define RI_FULLCLEAR	0x01	/* eraserows() hack to clear full screen */
     42 #define RI_FORCEMONO	0x02	/* monochrome output even if we can do color */
     43 #define RI_BSWAP	0x04	/* framebuffer endianness doesn't match CPU */
     44 #define RI_CURSOR	0x08	/* cursor is switched on */
     45 #define RI_CLEAR	0x10	/* clear display on startup */
     46 #define RI_CENTER	0x20	/* center onscreen output */
     47 #define RI_CURSORCLIP	0x40	/* cursor is currently clipped */
     48 #define RI_CFGDONE	0x80	/* rasops_reconfig() completed successfully */
     49 #define RI_ROTATE_CW	0x100	/* display is rotated, quarter clockwise */
     50 #define RI_ROTATE_CCW	0x200	/* display is rotated, quarter counter-clockwise */
     51 #define RI_ROTATE_UD	0x400	/* display is rotated, upside-down */
     52 #define RI_ROTATE_MASK	0x700
     53 /*
     54  * if you call rasops_init() or rasops_reconfig() in a context where it is not
     55  * safe to call kmem_alloc(), like early on during kernel startup, you MUST set
     56  * RI_NO_AUTO to keep rasops from trying to allocate memory for autogenerated
     57  * box drawing characters
     58  */
     59 #define	RI_NO_AUTO	0x800	/* do not generate box drawing characters */
     60 /*
     61  * Set this if your driver's putchar() method supports anti-aliased fonts in
     62  * the given video mode. Without this flag rasops_init() will only ever pick
     63  * monochrome bitmap fonts.
     64  */
     65 #define RI_ENABLE_ALPHA	0x1000
     66 /* set this in order to use r3g3b2 'true' colour in 8 bit */
     67 #define RI_8BIT_IS_RGB	0x2000
     68 /*
     69  * drivers can set this to tell the font selection code that they'd rather
     70  * use alpha fonts
     71  */
     72 #define RI_PREFER_ALPHA	0x4000
     73 
     74 struct rasops_info {
     75 	/* These must be filled in by the caller */
     76 	int	ri_depth;	/* depth in bits */
     77 	uint8_t	*ri_bits;	/* ptr to bits */
     78 	int	ri_width;	/* width (pels) */
     79 	int	ri_height;	/* height (pels) */
     80 	int	ri_stride;	/* stride in bytes */
     81 
     82 	/*
     83 	 * If you want shadow framebuffer support, point ri_hwbits
     84 	 * to the real framebuffer, and ri_bits to the shadow framebuffer
     85 	 */
     86 	uint8_t	*ri_hwbits;
     87 
     88 	/*
     89 	 * These can optionally be left zeroed out. If you fill ri_font,
     90 	 * but aren't using wsfont, set ri_wsfcookie to -1.
     91 	 */
     92 	struct	wsdisplay_font *ri_font;
     93 	struct	wsdisplay_font ri_optfont;
     94 	int	ri_wsfcookie;	/* wsfont cookie */
     95 	void	*ri_hw;		/* driver private data; ignored by rasops */
     96 	int	ri_crow;	/* cursor row */
     97 	int	ri_ccol;	/* cursor column */
     98 	int	ri_flg;		/* various operational flags */
     99 
    100 	/*
    101 	 * These are optional and will default if zero. Meaningless
    102 	 * on depths other than 15, 16, 24 and 32 bits per pel. On
    103 	 * 24 bit displays, ri_{r,g,b}num must be 8.
    104 	 */
    105 	uint8_t	ri_rnum;	/* number of bits for red */
    106 	uint8_t	ri_gnum;	/* number of bits for green */
    107 	uint8_t	ri_bnum;	/* number of bits for blue */
    108 	uint8_t	ri_rpos;	/* which bit red starts at */
    109 	uint8_t	ri_gpos;	/* which bit green starts at */
    110 	uint8_t	ri_bpos;	/* which bit blue starts at */
    111 
    112 	/* These are filled in by rasops_init() */
    113 	int	ri_emuwidth;	/* width we actually care about */
    114 	int	ri_emuheight;	/* height we actually care about */
    115 	int	ri_emustride;	/* bytes per row we actually care about */
    116 	int	ri_rows;	/* number of rows (characters, not pels) */
    117 	int	ri_cols;	/* number of columns (characters, not pels) */
    118 #if __NetBSD_Prereq__(9, 99, 1)
    119 	struct {
    120 		int	off;	/* offset of underline from bottom */
    121 		int	height;	/* height of underline */
    122 	} ri_ul;
    123 #else
    124 	/*
    125 	 * XXX
    126 	 * hack to keep ABI compatibility for netbsd-9, -8, and -7.
    127 	 */
    128 	// int	ri_delta;	/* obsoleted */
    129 	struct {
    130 		short	off;
    131 		short	height;
    132 	} __packed ri_ul;
    133 #endif
    134 	int	ri_pelbytes;	/* bytes per pel (may be zero) */
    135 	int	ri_fontscale;	/* fontheight * fontstride */
    136 	int	ri_xscale;	/* fontwidth * pelbytes */
    137 	int	ri_yscale;	/* fontheight * stride */
    138 	uint8_t	*ri_origbits;	/* where screen bits actually start */
    139 	uint8_t *ri_hworigbits;	/* where hw bits actually start */
    140 	int	ri_xorigin;	/* where ri_bits begins (x) */
    141 	int	ri_yorigin;	/* where ri_bits begins (y) */
    142 	uint32_t
    143 		ri_devcmap[16]; /* color -> framebuffer data */
    144 
    145 	/* The emulops you need to use, and the screen caps for wscons */
    146 	struct	wsdisplay_emulops ri_ops;
    147 	int	ri_caps;
    148 
    149 	/* Callbacks so we can share some code */
    150 	void	(*ri_do_cursor)(struct rasops_info *);
    151 
    152 #if NRASOPS_ROTATION > 0
    153 	/* Used to intercept putchar to permit display rotation */
    154 	struct	wsdisplay_emulops ri_real_ops;
    155 #endif
    156 };
    157 
    158 #define CHAR_IN_FONT(c, font)						\
    159 	((c) >= (font)->firstchar &&					\
    160 	    (c) - (font)->firstchar < (font)->numchars)
    161 
    162 #define PICK_FONT(ri, c)						\
    163 	((((c) & WSFONT_FLAGS_MASK) == WSFONT_FLAG_OPT &&		\
    164 	    (ri)->ri_optfont.data != NULL) ?				\
    165 		&(ri)->ri_optfont : (ri)->ri_font)
    166 
    167 /*
    168  * rasops_init().
    169  *
    170  * Integer parameters are the number of rows and columns we'd *like*.
    171  *
    172  * In terms of optimization, fonts that are a multiple of 8 pixels wide
    173  * work the best.
    174  *
    175  * rasops_init() takes care of rasops_reconfig(). The parameters to both
    176  * are the same. If calling rasops_reconfig() to change the font and
    177  * ri_wsfcookie >= 0, you must call wsfont_unlock() on it, and reset it
    178  * to -1 (or a new, valid cookie).
    179  */
    180 
    181 /* rasops.c */
    182 int	rasops_init(struct rasops_info *, int, int);
    183 int	rasops_reconfig(struct rasops_info *, int, int);
    184 void	rasops_unpack_attr(long, int *, int *, int *);
    185 void	rasops_eraserows(void *, int, int, long);
    186 void	rasops_erasecols(void *, int, int, int, long);
    187 int	rasops_get_cmap(struct rasops_info *, uint8_t *, size_t);
    188 
    189 extern const uint8_t	rasops_cmap[256 * 3];
    190 
    191 #ifdef _RASOPS_PRIVATE
    192 /*
    193  * Per-depth initialization functions.
    194  */
    195 void	rasops1_init(struct rasops_info *);
    196 void	rasops2_init(struct rasops_info *);
    197 void	rasops4_init(struct rasops_info *);
    198 void	rasops8_init(struct rasops_info *);
    199 void	rasops15_init(struct rasops_info *);
    200 void	rasops24_init(struct rasops_info *);
    201 void	rasops32_init(struct rasops_info *);
    202 
    203 #define	ATTR_BG(ri, attr) ((ri)->ri_devcmap[((uint32_t)(attr) >> 16) & 0xf])
    204 #define	ATTR_FG(ri, attr) ((ri)->ri_devcmap[((uint32_t)(attr) >> 24) & 0xf])
    205 
    206 #define	DELTA(p, d, cast) ((p) = (cast)((uint8_t *)(p) + (d)))
    207 
    208 #define	FBOFFSET(ri, row, col)						\
    209 	((row) * (ri)->ri_yscale + (col) * (ri)->ri_xscale)
    210 
    211 #define	FONT_GLYPH(uc, font, ri)					\
    212 	((uint8_t *)(font)->data + ((uc) - ((font)->firstchar)) *	\
    213 	    (ri)->ri_fontscale)
    214 
    215 static __inline void
    216 rasops_memset32(void *p, uint32_t val, size_t bytes)
    217 {
    218 	int slop1, slop2, full;
    219 	uint8_t *dp = (uint8_t *)p;
    220 
    221 	if (bytes == 1) {
    222 		*dp = val;
    223 		return;
    224 	}
    225 
    226 	slop1 = (4 - ((uintptr_t)dp & 3)) & 3;
    227 	slop2 = (bytes - slop1) & 3;
    228 	full = (bytes - slop1 /* - slop2 */) >> 2;
    229 
    230 	if (slop1 & 1)
    231 		*dp++ = val;
    232 
    233 	if (slop1 & 2) {
    234 		*(uint16_t *)dp = val;
    235 		dp += 2;
    236 	}
    237 
    238 	for (; full; full--) {
    239 		*(uint32_t *)dp = val;
    240 		dp += 4;
    241 	}
    242 
    243 	if (slop2 & 2) {
    244 		*(uint16_t *)dp = val;
    245 		dp += 2;
    246 	}
    247 
    248 	if (slop2 & 1)
    249 		*dp = val;
    250 
    251 	return;
    252 }
    253 
    254 static __inline uint32_t
    255 rasops_be32uatoh(uint8_t *p)
    256 {
    257 	uint32_t u;
    258 
    259 	u  = p[0]; u <<= 8;
    260 	u |= p[1]; u <<= 8;
    261 	u |= p[2]; u <<= 8;
    262 	u |= p[3];
    263 	return u;
    264 }
    265 #endif /* _RASOPS_PRIVATE */
    266 
    267 #endif /* _RASOPS_H_ */
    268