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