1 /* $NetBSD: wsdisplay_glyphcache.c,v 1.12 2023/06/08 05:48:41 macallan 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 if ((attr & 0xf0f00ff8) != 0) 57 return -1; 58 59 return (((attr >> 16) & 0x0f) | ((attr >> 20) & 0xf0)); 60 } 61 62 int 63 glyphcache_init(glyphcache *gc, int first, int lines, int width, 64 int cellwidth, int cellheight, long attr) 65 { 66 return glyphcache_init_align(gc, first, lines, width, cellwidth, cellheight, 67 attr, 0); 68 } 69 70 int 71 glyphcache_init_align(glyphcache *gc, int first, int lines, int width, 72 int cellwidth, int cellheight, long attr, int alignment) 73 { 74 75 /* first the geometry stuff */ 76 if (lines < 0) lines = 0; 77 gc->gc_width = width; 78 gc->gc_cellwidth = -1; 79 gc->gc_cellheight = -1; 80 gc->gc_firstline = first; 81 gc->gc_lines = lines; 82 gc->gc_cellalign = alignment; 83 gc->gc_buckets = NULL; 84 gc->gc_numbuckets = 0; 85 // XXX: Never free? 86 gc->gc_buckets = kmem_alloc(sizeof(*gc->gc_buckets) * NBUCKETS, 87 KM_SLEEP); 88 gc->gc_nbuckets = NBUCKETS; 89 return glyphcache_reconfig(gc, cellwidth, cellheight, attr); 90 91 } 92 93 int 94 glyphcache_reconfig(glyphcache *gc, int cellwidth, int cellheight, long attr) 95 { 96 int cache_lines, buckets, i, usedcells = 0, idx; 97 gc_bucket *b; 98 99 /* see if we actually need to reconfigure anything */ 100 if ((gc->gc_cellwidth == cellwidth) && 101 (gc->gc_cellheight == cellheight) && 102 ((gc->gc_buckets != NULL) && 103 (gc->gc_buckets[0].gb_index == attr2idx(attr)))) { 104 return 0; 105 } 106 107 gc->gc_cellwidth = cellwidth; 108 if (gc->gc_cellalign != 0) { 109 /* alignment in bytes */ 110 gc->gc_cellstride = 111 (gc->gc_cellwidth + gc->gc_cellalign - 1) & 112 ~(gc->gc_cellalign - 1); 113 } else 114 gc->gc_cellstride = cellwidth; 115 gc->gc_cellheight = cellheight; 116 117 gc->gc_cellsperline = gc->gc_width / gc->gc_cellstride; 118 119 cache_lines = gc->gc_lines / cellheight; 120 gc->gc_numcells = cache_lines * gc->gc_cellsperline; 121 122 /* now allocate buckets */ 123 buckets = (gc->gc_numcells / 223); 124 if ((buckets * 223) < gc->gc_numcells) 125 buckets++; 126 127 /* 128 * if we don't have enough video memory to cache at least a few glyphs 129 * we stop right here 130 */ 131 if (buckets < 1) 132 return ENOMEM; 133 134 buckets = uimin(buckets, gc->gc_nbuckets); 135 gc->gc_numbuckets = buckets; 136 137 DPRINTF("%s: using %d buckets\n", __func__, buckets); 138 for (i = 0; i < buckets; i++) { 139 b = &gc->gc_buckets[i]; 140 b->gb_firstcell = usedcells; 141 b->gb_numcells = uimin(223, gc->gc_numcells - usedcells); 142 usedcells += 223; 143 b->gb_usedcells = 0; 144 b->gb_index = -1; 145 } 146 147 /* initialize the attribute map... */ 148 for (i = 0; i < 256; i++) { 149 gc->gc_attrmap[i] = -1; 150 } 151 152 /* first bucket goes to default attr */ 153 idx = attr2idx(attr); 154 if (idx >= 0) { 155 gc->gc_attrmap[idx] = 0; 156 gc->gc_buckets[0].gb_index = idx; 157 } 158 159 glyphcache_wipe(gc); 160 DPRINTF("%s: using %d cells total, from %d width %d\n", __func__, 161 gc->gc_numcells, gc->gc_firstline, gc->gc_cellsperline); 162 DPRINTF("%s: cell size %d x %d, stride %d\n", __func__, 163 gc->gc_cellwidth, gc->gc_cellheight, gc->gc_cellstride); 164 return 0; 165 } 166 167 void 168 glyphcache_adapt(struct vcons_screen *scr, void *cookie) 169 { 170 glyphcache *gc = cookie; 171 struct rasops_info *ri = &scr->scr_ri; 172 173 if (ri->ri_wsfcookie != gc->gc_fontcookie) { 174 glyphcache_wipe(gc); 175 gc->gc_fontcookie = ri->ri_wsfcookie; 176 } 177 178 glyphcache_reconfig(gc, ri->ri_font->fontwidth, 179 ri->ri_font->fontheight, scr->scr_defattr); 180 } 181 182 void 183 glyphcache_wipe(glyphcache *gc) 184 { 185 gc_bucket *b; 186 int i, j, idx; 187 188 if ((gc->gc_buckets == NULL) || (gc->gc_numbuckets < 1)) 189 return; 190 191 idx = gc->gc_buckets[0].gb_index; 192 193 /* empty all the buckets */ 194 for (i = 0; i < gc->gc_numbuckets; i++) { 195 b = &gc->gc_buckets[i]; 196 b->gb_usedcells = 0; 197 b->gb_index = -1; 198 for (j = 0; j < b->gb_numcells; j++) 199 b->gb_map[j] = -1; 200 } 201 202 for (i = 0; i < 256; i++) { 203 gc->gc_attrmap[i] = -1; 204 } 205 206 /* now put the first bucket back where it was */ 207 gc->gc_attrmap[idx] = 0; 208 gc->gc_buckets[0].gb_index = idx; 209 } 210 211 /* 212 * add a glyph drawn at (x,y) to the cache as (c) 213 * call this only if glyphcache_try() returned GC_ADD 214 * caller or gc_bitblt must make sure the glyph is actually completely drawn 215 */ 216 int 217 glyphcache_add(glyphcache *gc, int c, int x, int y) 218 { 219 gc_bucket *b = gc->gc_next; 220 int cell; 221 int cx, cy; 222 223 if (b->gb_usedcells >= b->gb_numcells) 224 return ENOMEM; 225 cell = atomic_add_int_nv(&b->gb_usedcells, 1) - 1; 226 cell += b->gb_firstcell; 227 cy = gc->gc_firstline + 228 (cell / gc->gc_cellsperline) * gc->gc_cellheight; 229 cx = (cell % gc->gc_cellsperline) * gc->gc_cellstride; 230 b->gb_map[c - 33] = (cx << 16) | cy; 231 gc->gc_bitblt(gc->gc_blitcookie, x, y, cx, cy, 232 gc->gc_cellwidth, gc->gc_cellheight, gc->gc_rop); 233 if (gc->gc_underline & 1) { 234 glyphcache_underline(gc, x, y, gc->gc_underline); 235 } 236 return 0; 237 } 238 239 void 240 glyphcache_underline(glyphcache *gc, int x, int y, long attr) 241 { 242 if (gc->gc_rectfill == NULL) 243 return; 244 245 gc->gc_rectfill(gc->gc_blitcookie, x, y + gc->gc_cellheight - 2, 246 gc->gc_cellwidth, 1, attr); 247 } 248 /* 249 * check if (c) is in the cache, if so draw it at (x,y) 250 * return: 251 * - GC_OK when the glyph was found 252 * - GC_ADD when the glyph wasn't found but can be added 253 * - GC_NOPE when the glyph can't be cached 254 */ 255 int 256 glyphcache_try(glyphcache *gc, int c, int x, int y, long attr) 257 { 258 int cell, cx, cy, idx, bi; 259 gc_bucket *b; 260 261 idx = attr2idx(attr); 262 /* see if we're in range */ 263 if ((c < 33) || (c > 255) || (idx < 0)) 264 return GC_NOPE; 265 /* see if there's already a bucket for this attribute */ 266 bi = gc->gc_attrmap[idx]; 267 if (bi == -1) { 268 /* nope, see if there's an empty one left */ 269 bi = 1; 270 while ((bi < gc->gc_numbuckets) && 271 (gc->gc_buckets[bi].gb_index != -1)) { 272 bi++; 273 } 274 if (bi < gc->gc_numbuckets) { 275 /* found one -> grab it */ 276 gc->gc_attrmap[idx] = bi; 277 b = &gc->gc_buckets[bi]; 278 b->gb_index = idx; 279 b->gb_usedcells = 0; 280 /* make sure this doesn't get evicted right away */ 281 b->gb_lastread = time_uptime; 282 } else { 283 /* 284 * still nothing 285 * steal the least recently read bucket 286 */ 287 time_t moo = time_uptime; 288 int i, oldest = 1; 289 290 for (i = 1; i < gc->gc_numbuckets; i++) { 291 if (gc->gc_buckets[i].gb_lastread < moo) { 292 oldest = i; 293 moo = gc->gc_buckets[i].gb_lastread; 294 } 295 } 296 297 /* if we end up here all buckets must be in use */ 298 b = &gc->gc_buckets[oldest]; 299 gc->gc_attrmap[b->gb_index] = -1; 300 b->gb_index = idx; 301 b->gb_usedcells = 0; 302 gc->gc_attrmap[idx] = oldest; 303 /* now scrub it */ 304 for (i = 0; i < b->gb_numcells; i++) 305 b->gb_map[i] = -1; 306 /* and set the time stamp */ 307 b->gb_lastread = time_uptime; 308 } 309 } else { 310 /* found one */ 311 b = &gc->gc_buckets[bi]; 312 } 313 314 /* see if there's room in the bucket */ 315 if (b->gb_usedcells >= b->gb_numcells) 316 return GC_NOPE; 317 318 cell = b->gb_map[c - 33]; 319 if (cell == -1) { 320 gc->gc_next = b; 321 gc->gc_underline = attr; 322 return GC_ADD; 323 } 324 325 /* it's in the cache - draw it */ 326 cy = cell & 0xffff; 327 cx = (cell >> 16) & 0xffff; 328 gc->gc_bitblt(gc->gc_blitcookie, cx, cy, x, y, 329 gc->gc_cellwidth, gc->gc_cellheight, gc->gc_rop); 330 /* and underline it if needed */ 331 if (attr & 1) 332 glyphcache_underline(gc, x, y, attr); 333 /* update bucket's time stamp */ 334 b->gb_lastread = time_uptime; 335 return GC_OK; 336 } 337