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