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