xref: /netbsd-src/sys/dev/wscons/wsdisplay_glyphcache.c (revision b7b7574d3bf8eeb51a1fa3977b59142ec6434a55)
1 /*	$NetBSD: wsdisplay_glyphcache.c,v 1.6 2014/01/13 19:30:33 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  * For now it only caches glyphs with the default attribute ( assuming they're
31  * the most commonly used glyphs ) but the API should at least not prevent
32  * more sophisticated caching algorithms
33  */
34 
35 #include <sys/systm.h>
36 #include <sys/atomic.h>
37 #include <sys/errno.h>
38 #include <sys/kmem.h>
39 #include <dev/wscons/wsdisplay_glyphcachevar.h>
40 #include "opt_glyphcache.h"
41 
42 #ifdef GLYPHCACHE_DEBUG
43 #define DPRINTF aprint_normal
44 #else
45 #define DPRINTF while (0) printf
46 #endif
47 
48 static inline int
49 attr2idx(long attr)
50 {
51 	if ((attr & 0xf0f0fff8) != 0)
52 		return -1;
53 
54 	return (((attr >> 16) & 0x0f) | ((attr >> 20) & 0xf0));
55 }
56 
57 /* first line, lines, width, attr */
58 int
59 glyphcache_init(glyphcache *gc, int first, int lines, int width,
60     int cellwidth, int cellheight, long attr)
61 {
62 	int cache_lines, buckets, i, usedcells = 0, idx;
63 	gc_bucket *b;
64 
65 	/* first the geometry stuff */
66 	gc->gc_cellwidth = cellwidth;
67 	gc->gc_cellheight = cellheight;
68 	gc->gc_firstline = first;
69 	gc->gc_cellsperline = width / cellwidth;
70 	gc->gc_buckets = NULL;
71 	gc->gc_numbuckets = 0;
72 	if (lines < 0) lines = 0;
73 	cache_lines = lines / cellheight;
74 	gc->gc_numcells = cache_lines * gc->gc_cellsperline;
75 
76 	/* now allocate buckets */
77 	buckets = (gc->gc_numcells / 223);
78 	if ((buckets * 223) < gc->gc_numcells)
79 		buckets++;
80 
81 	/*
82 	 * if we don't have enough video memory to cache at least a few glyphs
83 	 * we stop right here
84 	 */
85 	if (buckets < 1)
86 		return ENOMEM;
87 
88 	gc->gc_buckets = kmem_alloc(sizeof(gc_bucket) * buckets, KM_SLEEP);
89 	if (gc->gc_buckets == NULL) {
90 		aprint_error("%s: can't allocate memory\n", __func__);
91 		return ENOMEM;
92 	}
93 	gc->gc_numbuckets = buckets;
94 
95 	DPRINTF("%s: using %d buckets\n", __func__, buckets);
96 	for (i = 0; i < buckets; i++) {
97 		b = &gc->gc_buckets[i];
98 		b->gb_firstcell = usedcells;
99 		b->gb_numcells = min(223, gc->gc_numcells - usedcells);
100 		usedcells += 223;
101 		b->gb_usedcells = 0;
102 		b->gb_index = -1;
103 	}
104 
105 	/* initialize the attribute map... */
106 	for (i = 0; i < 256; i++) {
107 		gc->gc_attrmap[i] = -1;
108 	}
109 
110 	/* first bucket goes to default attr */
111 	idx = attr2idx(attr);
112 	if (idx >= 0) {
113 		gc->gc_attrmap[idx] = 0;
114 		gc->gc_buckets[0].gb_index = idx;
115 	}
116 
117 	glyphcache_wipe(gc);
118 	DPRINTF("%s: using %d cells total, from %d width %d\n", __func__,
119 	    gc->gc_numcells, gc->gc_firstline, gc->gc_cellsperline);
120 	return 0;
121 }
122 
123 void
124 glyphcache_wipe(glyphcache *gc)
125 {
126 	gc_bucket *b;
127 	int i, j, idx;
128 
129 	if ((gc->gc_buckets == NULL) || (gc->gc_numbuckets < 1))
130 		return;
131 
132 	idx = gc->gc_buckets[0].gb_index;
133 
134 	/* empty all the buckets */
135 	for (i = 0; i < gc->gc_numbuckets; i++) {
136 		b = &gc->gc_buckets[i];
137 		b->gb_usedcells = 0;
138 		b->gb_index = -1;
139 		for (j = 0; j < b->gb_numcells; j++)
140 			b->gb_map[j] = -1;
141 	}
142 
143 	for (i = 0; i < 256; i++) {
144 		gc->gc_attrmap[i] = -1;
145 	}
146 
147 	/* now put the first bucket back where it was */
148 	gc->gc_attrmap[idx] = 0;
149 	gc->gc_buckets[0].gb_index = idx;
150 }
151 
152 /*
153  * add a glyph drawn at (x,y) to the cache as (c)
154  * call this only if glyphcache_try() returned GC_ADD
155  * caller or gc_bitblt must make sure the glyph is actually completely drawn
156  */
157 int
158 glyphcache_add(glyphcache *gc, int c, int x, int y)
159 {
160 	gc_bucket *b = gc->gc_next;
161 	int cell;
162 	int cx, cy;
163 
164 	if (b->gb_usedcells >= b->gb_numcells)
165 		return ENOMEM;
166 	cell = atomic_add_int_nv(&b->gb_usedcells, 1) - 1;
167 	cell += b->gb_firstcell;
168 	cy = gc->gc_firstline +
169 	    (cell / gc->gc_cellsperline) * gc->gc_cellheight;
170 	cx = (cell % gc->gc_cellsperline) * gc->gc_cellwidth;
171 	b->gb_map[c - 33] = (cx << 16) | cy;
172 	gc->gc_bitblt(gc->gc_blitcookie, x, y, cx, cy,
173 	    gc->gc_cellwidth, gc->gc_cellheight, gc->gc_rop);
174 	if (gc->gc_underline & 1) {
175 		glyphcache_underline(gc, x, y, gc->gc_underline);
176 	}
177 	return 0;
178 }
179 
180 void
181 glyphcache_underline(glyphcache *gc, int x, int y, long attr)
182 {
183 	if (gc->gc_rectfill == NULL)
184 		return;
185 
186 	gc->gc_rectfill(gc->gc_blitcookie, x, y + gc->gc_cellheight - 2,
187 	    gc->gc_cellwidth, 1, attr);
188 }
189 /*
190  * check if (c) is in the cache, if so draw it at (x,y)
191  * return:
192  * - GC_OK when the glyph was found
193  * - GC_ADD when the glyph wasn't found but can be added
194  * - GC_NOPE when the glyph can't be cached
195  */
196 int
197 glyphcache_try(glyphcache *gc, int c, int x, int y, long attr)
198 {
199 	int cell, cx, cy, idx, bi;
200 	gc_bucket *b;
201 
202 	idx = attr2idx(attr);
203 	/* see if we're in range */
204 	if ((c < 33) || (c > 255) || (idx < 0))
205 		return GC_NOPE;
206 	/* see if there's already a bucket for this attribute */
207 	bi = gc->gc_attrmap[idx];
208 	if (bi == -1) {
209 		/* nope, see if there's an empty one left */
210 		bi = 1;
211 		while ((bi < gc->gc_numbuckets) &&
212 		       (gc->gc_buckets[bi].gb_index != -1)) {
213 			bi++;
214 		}
215 		if (bi < gc->gc_numbuckets) {
216 			/* found one -> grab it */
217 			gc->gc_attrmap[idx] = bi;
218 			b = &gc->gc_buckets[bi];
219 			b->gb_index = idx;
220 			b->gb_usedcells = 0;
221 			/* make sure this doesn't get evicted right away */
222 			b->gb_lastread = time_uptime;
223 		} else {
224 			/*
225 			 * still nothing
226 			 * steal the least recently read bucket
227 			 */
228 			time_t moo = time_uptime;
229 			int i, oldest = 1;
230 
231 			for (i = 1; i < gc->gc_numbuckets; i++) {
232 				if (gc->gc_buckets[i].gb_lastread < moo) {
233 					oldest = i;
234 					moo = gc->gc_buckets[i].gb_lastread;
235 				}
236 			}
237 
238 			/* if we end up here all buckets must be in use */
239 			b = &gc->gc_buckets[oldest];
240 			gc->gc_attrmap[b->gb_index] = -1;
241 			b->gb_index = idx;
242 			b->gb_usedcells = 0;
243 			gc->gc_attrmap[idx] = oldest;
244 			/* now scrub it */
245 			for (i = 0; i < b->gb_numcells; i++)
246 				b->gb_map[i] = -1;
247 			/* and set the time stamp */
248 			b->gb_lastread = time_uptime;
249 		}
250 	} else {
251 		/* found one */
252 		b = &gc->gc_buckets[bi];
253 	}
254 
255 	/* see if there's room in the bucket */
256 	if (b->gb_usedcells >= b->gb_numcells)
257 		return GC_NOPE;
258 
259 	cell = b->gb_map[c - 33];
260 	if (cell == -1) {
261 		gc->gc_next = b;
262 		gc->gc_underline = attr;
263 		return GC_ADD;
264 	}
265 
266 	/* it's in the cache - draw it */
267 	cy = cell & 0xffff;
268 	cx = (cell >> 16) & 0xffff;
269 	gc->gc_bitblt(gc->gc_blitcookie, cx, cy, x, y,
270 	    gc->gc_cellwidth, gc->gc_cellheight, gc->gc_rop);
271 	/* and underline it if needed */
272 	if (attr & 1)
273 		glyphcache_underline(gc, x, y, attr);
274 	/* update bucket's time stamp */
275 	b->gb_lastread = time_uptime;
276 	return GC_OK;
277 }
278