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