Home | History | Annotate | Line # | Download | only in wscons
wsdisplay_glyphcache.c revision 1.14
      1  1.14  macallan /*	$NetBSD: wsdisplay_glyphcache.c,v 1.14 2024/12/06 11:46:11 macallan Exp $	*/
      2   1.1  macallan 
      3   1.1  macallan /*
      4   1.1  macallan  * Copyright (c) 2012 Michael Lorenz
      5   1.1  macallan  * All rights reserved.
      6   1.1  macallan  *
      7   1.1  macallan  * Redistribution and use in source and binary forms, with or without
      8   1.1  macallan  * modification, are permitted provided that the following conditions
      9   1.1  macallan  * are met:
     10   1.1  macallan  * 1. Redistributions of source code must retain the above copyright
     11   1.1  macallan  *    notice, this list of conditions and the following disclaimer.
     12   1.1  macallan  * 2. Redistributions in binary form must reproduce the above copyright
     13   1.1  macallan  *    notice, this list of conditions and the following disclaimer in the
     14   1.1  macallan  *    documentation and/or other materials provided with the distribution.
     15   1.1  macallan  *
     16   1.1  macallan  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     17   1.1  macallan  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     18   1.1  macallan  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     19   1.1  macallan  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
     20   1.1  macallan  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
     21   1.1  macallan  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
     22   1.1  macallan  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
     23   1.1  macallan  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     24   1.1  macallan  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
     25   1.1  macallan  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     26   1.1  macallan  */
     27   1.1  macallan 
     28   1.1  macallan /*
     29   1.1  macallan  * a simple glyph cache in offscreen memory
     30   1.1  macallan  */
     31   1.7     pooka 
     32   1.7     pooka #ifdef _KERNEL_OPT
     33   1.7     pooka #include "opt_glyphcache.h"
     34   1.7     pooka #endif
     35   1.4  macallan 
     36   1.4  macallan #include <sys/systm.h>
     37   1.1  macallan #include <sys/atomic.h>
     38   1.1  macallan #include <sys/errno.h>
     39   1.4  macallan #include <sys/kmem.h>
     40   1.9  macallan #include <dev/wscons/wsdisplayvar.h>
     41   1.9  macallan #include <dev/rasops/rasops.h>
     42   1.9  macallan #include <dev/wscons/wsdisplay_vconsvar.h>
     43   1.1  macallan #include <dev/wscons/wsdisplay_glyphcachevar.h>
     44   1.4  macallan 
     45   1.4  macallan #ifdef GLYPHCACHE_DEBUG
     46   1.4  macallan #define DPRINTF aprint_normal
     47   1.4  macallan #else
     48   1.4  macallan #define DPRINTF while (0) printf
     49   1.4  macallan #endif
     50   1.4  macallan 
     51   1.9  macallan #define NBUCKETS 32
     52   1.9  macallan 
     53   1.4  macallan static inline int
     54   1.4  macallan attr2idx(long attr)
     55   1.4  macallan {
     56   1.4  macallan 	return (((attr >> 16) & 0x0f) | ((attr >> 20) & 0xf0));
     57   1.4  macallan }
     58   1.1  macallan 
     59   1.1  macallan int
     60   1.1  macallan glyphcache_init(glyphcache *gc, int first, int lines, int width,
     61   1.1  macallan     int cellwidth, int cellheight, long attr)
     62   1.1  macallan {
     63  1.12  macallan 	return glyphcache_init_align(gc, first, lines, width, cellwidth, cellheight,
     64  1.12  macallan 	    attr, 0);
     65  1.12  macallan }
     66  1.12  macallan 
     67  1.12  macallan int
     68  1.12  macallan glyphcache_init_align(glyphcache *gc, int first, int lines, int width,
     69  1.12  macallan     int cellwidth, int cellheight, long attr, int alignment)
     70  1.12  macallan {
     71   1.9  macallan 
     72   1.9  macallan 	/* first the geometry stuff */
     73   1.9  macallan 	if (lines < 0) lines = 0;
     74   1.9  macallan 	gc->gc_width = width;
     75   1.9  macallan 	gc->gc_cellwidth = -1;
     76   1.9  macallan 	gc->gc_cellheight = -1;
     77   1.9  macallan 	gc->gc_firstline = first;
     78  1.14  macallan 	gc->gc_firstcol = 0;
     79   1.9  macallan 	gc->gc_lines = lines;
     80  1.12  macallan 	gc->gc_cellalign = alignment;
     81   1.9  macallan 	gc->gc_buckets = NULL;
     82   1.9  macallan 	gc->gc_numbuckets = 0;
     83  1.10  christos 	// XXX: Never free?
     84  1.10  christos 	gc->gc_buckets = kmem_alloc(sizeof(*gc->gc_buckets) * NBUCKETS,
     85  1.10  christos 	    KM_SLEEP);
     86   1.9  macallan 	gc->gc_nbuckets = NBUCKETS;
     87   1.9  macallan 	return glyphcache_reconfig(gc, cellwidth, cellheight, attr);
     88   1.9  macallan 
     89   1.9  macallan }
     90   1.9  macallan 
     91   1.9  macallan int
     92  1.14  macallan glyphcache_init_x(glyphcache *gc, int x, int y, int lines, int width,
     93  1.14  macallan     int cellwidth, int cellheight, long attr)
     94  1.14  macallan {
     95  1.14  macallan 
     96  1.14  macallan 	/* first the geometry stuff */
     97  1.14  macallan 	if (lines < 0) lines = 0;
     98  1.14  macallan 	gc->gc_width = width;
     99  1.14  macallan 	gc->gc_cellwidth = -1;
    100  1.14  macallan 	gc->gc_cellheight = -1;
    101  1.14  macallan 	gc->gc_firstline = y;
    102  1.14  macallan 	gc->gc_firstcol = x;
    103  1.14  macallan 	gc->gc_lines = lines;
    104  1.14  macallan 	gc->gc_cellalign = 0;
    105  1.14  macallan 	gc->gc_buckets = NULL;
    106  1.14  macallan 	gc->gc_numbuckets = 0;
    107  1.14  macallan 	// XXX: Never free?
    108  1.14  macallan 	gc->gc_buckets = kmem_alloc(sizeof(*gc->gc_buckets) * NBUCKETS,
    109  1.14  macallan 	    KM_SLEEP);
    110  1.14  macallan 	gc->gc_nbuckets = NBUCKETS;
    111  1.14  macallan 	return glyphcache_reconfig(gc, cellwidth, cellheight, attr);
    112  1.14  macallan 
    113  1.14  macallan }
    114  1.14  macallan 
    115  1.14  macallan int
    116   1.9  macallan glyphcache_reconfig(glyphcache *gc, int cellwidth, int cellheight, long attr)
    117   1.9  macallan {
    118   1.4  macallan 	int cache_lines, buckets, i, usedcells = 0, idx;
    119   1.4  macallan 	gc_bucket *b;
    120   1.1  macallan 
    121   1.9  macallan 	/* see if we actually need to reconfigure anything */
    122   1.9  macallan 	if ((gc->gc_cellwidth == cellwidth) &&
    123   1.9  macallan 	    (gc->gc_cellheight == cellheight) &&
    124   1.9  macallan 	    ((gc->gc_buckets != NULL) &&
    125   1.9  macallan 	     (gc->gc_buckets[0].gb_index == attr2idx(attr)))) {
    126   1.9  macallan 		return 0;
    127   1.9  macallan 	}
    128   1.9  macallan 
    129   1.1  macallan 	gc->gc_cellwidth = cellwidth;
    130  1.12  macallan 	if (gc->gc_cellalign != 0) {
    131  1.12  macallan 		/* alignment in bytes */
    132  1.12  macallan 		gc->gc_cellstride =
    133  1.12  macallan 		    (gc->gc_cellwidth + gc->gc_cellalign - 1) &
    134  1.12  macallan 		    ~(gc->gc_cellalign - 1);
    135  1.12  macallan 	} else
    136  1.12  macallan 		gc->gc_cellstride = cellwidth;
    137   1.1  macallan 	gc->gc_cellheight = cellheight;
    138   1.9  macallan 
    139  1.12  macallan 	gc->gc_cellsperline = gc->gc_width / gc->gc_cellstride;
    140   1.9  macallan 
    141   1.9  macallan 	cache_lines = gc->gc_lines / cellheight;
    142   1.1  macallan 	gc->gc_numcells = cache_lines * gc->gc_cellsperline;
    143   1.4  macallan 
    144   1.4  macallan 	/* now allocate buckets */
    145   1.4  macallan 	buckets = (gc->gc_numcells / 223);
    146   1.4  macallan 	if ((buckets * 223) < gc->gc_numcells)
    147   1.4  macallan 		buckets++;
    148   1.5  macallan 
    149   1.5  macallan 	/*
    150   1.5  macallan 	 * if we don't have enough video memory to cache at least a few glyphs
    151   1.5  macallan 	 * we stop right here
    152   1.5  macallan 	 */
    153   1.5  macallan 	if (buckets < 1)
    154   1.5  macallan 		return ENOMEM;
    155   1.5  macallan 
    156  1.11  riastrad 	buckets = uimin(buckets, gc->gc_nbuckets);
    157   1.4  macallan 	gc->gc_numbuckets = buckets;
    158   1.4  macallan 
    159   1.4  macallan 	DPRINTF("%s: using %d buckets\n", __func__, buckets);
    160   1.4  macallan 	for (i = 0; i < buckets; i++) {
    161   1.4  macallan 		b = &gc->gc_buckets[i];
    162   1.4  macallan 		b->gb_firstcell = usedcells;
    163  1.11  riastrad 		b->gb_numcells = uimin(223, gc->gc_numcells - usedcells);
    164   1.4  macallan 		usedcells += 223;
    165   1.4  macallan 		b->gb_usedcells = 0;
    166   1.4  macallan 		b->gb_index = -1;
    167   1.4  macallan 	}
    168   1.4  macallan 
    169   1.4  macallan 	/* initialize the attribute map... */
    170   1.4  macallan 	for (i = 0; i < 256; i++) {
    171   1.4  macallan 		gc->gc_attrmap[i] = -1;
    172   1.4  macallan 	}
    173   1.4  macallan 
    174   1.4  macallan 	/* first bucket goes to default attr */
    175   1.4  macallan 	idx = attr2idx(attr);
    176   1.4  macallan 	if (idx >= 0) {
    177   1.4  macallan 		gc->gc_attrmap[idx] = 0;
    178   1.4  macallan 		gc->gc_buckets[0].gb_index = idx;
    179   1.4  macallan 	}
    180   1.4  macallan 
    181   1.1  macallan 	glyphcache_wipe(gc);
    182   1.4  macallan 	DPRINTF("%s: using %d cells total, from %d width %d\n", __func__,
    183   1.4  macallan 	    gc->gc_numcells, gc->gc_firstline, gc->gc_cellsperline);
    184  1.12  macallan 	DPRINTF("%s: cell size %d x %d, stride %d\n", __func__,
    185  1.12  macallan 	    gc->gc_cellwidth, gc->gc_cellheight, gc->gc_cellstride);
    186   1.1  macallan 	return 0;
    187   1.1  macallan }
    188   1.1  macallan 
    189   1.1  macallan void
    190   1.9  macallan glyphcache_adapt(struct vcons_screen *scr, void *cookie)
    191   1.9  macallan {
    192   1.9  macallan 	glyphcache *gc = cookie;
    193   1.9  macallan 	struct rasops_info *ri = &scr->scr_ri;
    194   1.9  macallan 
    195   1.9  macallan 	if (ri->ri_wsfcookie != gc->gc_fontcookie) {
    196   1.9  macallan 		glyphcache_wipe(gc);
    197   1.9  macallan 		gc->gc_fontcookie = ri->ri_wsfcookie;
    198   1.9  macallan 	}
    199   1.9  macallan 
    200   1.9  macallan 	glyphcache_reconfig(gc, ri->ri_font->fontwidth,
    201   1.9  macallan 			        ri->ri_font->fontheight, scr->scr_defattr);
    202   1.9  macallan }
    203   1.9  macallan 
    204   1.9  macallan void
    205   1.1  macallan glyphcache_wipe(glyphcache *gc)
    206   1.1  macallan {
    207   1.4  macallan 	gc_bucket *b;
    208   1.4  macallan 	int i, j, idx;
    209   1.4  macallan 
    210   1.6  macallan 	if ((gc->gc_buckets == NULL) || (gc->gc_numbuckets < 1))
    211   1.6  macallan 		return;
    212   1.6  macallan 
    213   1.4  macallan 	idx = gc->gc_buckets[0].gb_index;
    214   1.1  macallan 
    215   1.4  macallan 	/* empty all the buckets */
    216   1.4  macallan 	for (i = 0; i < gc->gc_numbuckets; i++) {
    217   1.4  macallan 		b = &gc->gc_buckets[i];
    218   1.4  macallan 		b->gb_usedcells = 0;
    219   1.4  macallan 		b->gb_index = -1;
    220   1.4  macallan 		for (j = 0; j < b->gb_numcells; j++)
    221   1.4  macallan 			b->gb_map[j] = -1;
    222   1.4  macallan 	}
    223   1.4  macallan 
    224   1.4  macallan 	for (i = 0; i < 256; i++) {
    225   1.4  macallan 		gc->gc_attrmap[i] = -1;
    226   1.4  macallan 	}
    227   1.4  macallan 
    228   1.4  macallan 	/* now put the first bucket back where it was */
    229   1.4  macallan 	gc->gc_attrmap[idx] = 0;
    230   1.4  macallan 	gc->gc_buckets[0].gb_index = idx;
    231   1.1  macallan }
    232   1.1  macallan 
    233   1.1  macallan /*
    234   1.1  macallan  * add a glyph drawn at (x,y) to the cache as (c)
    235   1.1  macallan  * call this only if glyphcache_try() returned GC_ADD
    236   1.1  macallan  * caller or gc_bitblt must make sure the glyph is actually completely drawn
    237   1.1  macallan  */
    238   1.1  macallan int
    239   1.1  macallan glyphcache_add(glyphcache *gc, int c, int x, int y)
    240   1.1  macallan {
    241   1.4  macallan 	gc_bucket *b = gc->gc_next;
    242   1.1  macallan 	int cell;
    243   1.1  macallan 	int cx, cy;
    244   1.1  macallan 
    245   1.4  macallan 	if (b->gb_usedcells >= b->gb_numcells)
    246   1.1  macallan 		return ENOMEM;
    247   1.4  macallan 	cell = atomic_add_int_nv(&b->gb_usedcells, 1) - 1;
    248   1.4  macallan 	cell += b->gb_firstcell;
    249   1.1  macallan 	cy = gc->gc_firstline +
    250   1.1  macallan 	    (cell / gc->gc_cellsperline) * gc->gc_cellheight;
    251  1.14  macallan 	cx = gc->gc_firstcol +
    252  1.14  macallan 	    (cell % gc->gc_cellsperline) * gc->gc_cellstride;
    253   1.4  macallan 	b->gb_map[c - 33] = (cx << 16) | cy;
    254   1.1  macallan 	gc->gc_bitblt(gc->gc_blitcookie, x, y, cx, cy,
    255   1.1  macallan 	    gc->gc_cellwidth, gc->gc_cellheight, gc->gc_rop);
    256   1.4  macallan 	if (gc->gc_underline & 1) {
    257   1.4  macallan 		glyphcache_underline(gc, x, y, gc->gc_underline);
    258   1.4  macallan 	}
    259   1.1  macallan 	return 0;
    260   1.1  macallan }
    261   1.1  macallan 
    262   1.4  macallan void
    263   1.4  macallan glyphcache_underline(glyphcache *gc, int x, int y, long attr)
    264   1.4  macallan {
    265   1.4  macallan 	if (gc->gc_rectfill == NULL)
    266   1.4  macallan 		return;
    267   1.4  macallan 
    268   1.4  macallan 	gc->gc_rectfill(gc->gc_blitcookie, x, y + gc->gc_cellheight - 2,
    269   1.4  macallan 	    gc->gc_cellwidth, 1, attr);
    270   1.4  macallan }
    271   1.1  macallan /*
    272   1.1  macallan  * check if (c) is in the cache, if so draw it at (x,y)
    273   1.1  macallan  * return:
    274   1.1  macallan  * - GC_OK when the glyph was found
    275   1.1  macallan  * - GC_ADD when the glyph wasn't found but can be added
    276   1.1  macallan  * - GC_NOPE when the glyph can't be cached
    277   1.1  macallan  */
    278   1.1  macallan int
    279   1.1  macallan glyphcache_try(glyphcache *gc, int c, int x, int y, long attr)
    280   1.1  macallan {
    281   1.4  macallan 	int cell, cx, cy, idx, bi;
    282   1.4  macallan 	gc_bucket *b;
    283   1.4  macallan 
    284   1.4  macallan 	idx = attr2idx(attr);
    285   1.4  macallan 	/* see if we're in range */
    286   1.4  macallan 	if ((c < 33) || (c > 255) || (idx < 0))
    287   1.1  macallan 		return GC_NOPE;
    288   1.4  macallan 	/* see if there's already a bucket for this attribute */
    289   1.4  macallan 	bi = gc->gc_attrmap[idx];
    290   1.4  macallan 	if (bi == -1) {
    291   1.4  macallan 		/* nope, see if there's an empty one left */
    292   1.4  macallan 		bi = 1;
    293   1.4  macallan 		while ((bi < gc->gc_numbuckets) &&
    294   1.4  macallan 		       (gc->gc_buckets[bi].gb_index != -1)) {
    295   1.4  macallan 			bi++;
    296   1.4  macallan 		}
    297   1.4  macallan 		if (bi < gc->gc_numbuckets) {
    298   1.4  macallan 			/* found one -> grab it */
    299   1.4  macallan 			gc->gc_attrmap[idx] = bi;
    300   1.4  macallan 			b = &gc->gc_buckets[bi];
    301   1.4  macallan 			b->gb_index = idx;
    302   1.4  macallan 			b->gb_usedcells = 0;
    303   1.4  macallan 			/* make sure this doesn't get evicted right away */
    304   1.4  macallan 			b->gb_lastread = time_uptime;
    305   1.4  macallan 		} else {
    306   1.4  macallan 			/*
    307   1.4  macallan 			 * still nothing
    308   1.4  macallan 			 * steal the least recently read bucket
    309   1.4  macallan 			 */
    310   1.4  macallan 			time_t moo = time_uptime;
    311   1.4  macallan 			int i, oldest = 1;
    312   1.4  macallan 
    313   1.4  macallan 			for (i = 1; i < gc->gc_numbuckets; i++) {
    314   1.4  macallan 				if (gc->gc_buckets[i].gb_lastread < moo) {
    315   1.4  macallan 					oldest = i;
    316   1.4  macallan 					moo = gc->gc_buckets[i].gb_lastread;
    317   1.4  macallan 				}
    318   1.4  macallan 			}
    319   1.4  macallan 
    320   1.4  macallan 			/* if we end up here all buckets must be in use */
    321   1.4  macallan 			b = &gc->gc_buckets[oldest];
    322   1.4  macallan 			gc->gc_attrmap[b->gb_index] = -1;
    323   1.4  macallan 			b->gb_index = idx;
    324   1.4  macallan 			b->gb_usedcells = 0;
    325   1.4  macallan 			gc->gc_attrmap[idx] = oldest;
    326   1.4  macallan 			/* now scrub it */
    327   1.4  macallan 			for (i = 0; i < b->gb_numcells; i++)
    328   1.4  macallan 				b->gb_map[i] = -1;
    329   1.4  macallan 			/* and set the time stamp */
    330   1.4  macallan 			b->gb_lastread = time_uptime;
    331   1.4  macallan 		}
    332   1.4  macallan 	} else {
    333   1.4  macallan 		/* found one */
    334   1.4  macallan 		b = &gc->gc_buckets[bi];
    335   1.4  macallan 	}
    336   1.4  macallan 
    337   1.4  macallan 	/* see if there's room in the bucket */
    338   1.4  macallan 	if (b->gb_usedcells >= b->gb_numcells)
    339   1.1  macallan 		return GC_NOPE;
    340   1.4  macallan 
    341   1.4  macallan 	cell = b->gb_map[c - 33];
    342   1.4  macallan 	if (cell == -1) {
    343   1.4  macallan 		gc->gc_next = b;
    344   1.4  macallan 		gc->gc_underline = attr;
    345   1.1  macallan 		return GC_ADD;
    346   1.4  macallan 	}
    347   1.4  macallan 
    348   1.4  macallan 	/* it's in the cache - draw it */
    349   1.2  macallan 	cy = cell & 0xffff;
    350   1.2  macallan 	cx = (cell >> 16) & 0xffff;
    351   1.1  macallan 	gc->gc_bitblt(gc->gc_blitcookie, cx, cy, x, y,
    352   1.1  macallan 	    gc->gc_cellwidth, gc->gc_cellheight, gc->gc_rop);
    353   1.4  macallan 	/* and underline it if needed */
    354   1.4  macallan 	if (attr & 1)
    355   1.4  macallan 		glyphcache_underline(gc, x, y, attr);
    356   1.4  macallan 	/* update bucket's time stamp */
    357   1.4  macallan 	b->gb_lastread = time_uptime;
    358   1.1  macallan 	return GC_OK;
    359   1.1  macallan }
    360