wsdisplay_glyphcache.c revision 1.6 1 1.6 macallan /* $NetBSD: wsdisplay_glyphcache.c,v 1.6 2014/01/13 19:30:33 macallan Exp $ */
2 1.1 macallan
3 1.1 macallan /*
4 1.1 macallan * Copyright (c) 2012 Michael Lorenz
5 1.1 macallan * All rights reserved.
6 1.1 macallan *
7 1.1 macallan * Redistribution and use in source and binary forms, with or without
8 1.1 macallan * modification, are permitted provided that the following conditions
9 1.1 macallan * are met:
10 1.1 macallan * 1. Redistributions of source code must retain the above copyright
11 1.1 macallan * notice, this list of conditions and the following disclaimer.
12 1.1 macallan * 2. Redistributions in binary form must reproduce the above copyright
13 1.1 macallan * notice, this list of conditions and the following disclaimer in the
14 1.1 macallan * documentation and/or other materials provided with the distribution.
15 1.1 macallan *
16 1.1 macallan * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
17 1.1 macallan * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
18 1.1 macallan * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
19 1.1 macallan * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
20 1.1 macallan * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
21 1.1 macallan * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
22 1.1 macallan * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
23 1.1 macallan * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24 1.1 macallan * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
25 1.1 macallan * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26 1.1 macallan */
27 1.1 macallan
28 1.1 macallan /*
29 1.1 macallan * a simple glyph cache in offscreen memory
30 1.1 macallan * For now it only caches glyphs with the default attribute ( assuming they're
31 1.1 macallan * the most commonly used glyphs ) but the API should at least not prevent
32 1.1 macallan * more sophisticated caching algorithms
33 1.1 macallan */
34 1.4 macallan
35 1.4 macallan #include <sys/systm.h>
36 1.1 macallan #include <sys/atomic.h>
37 1.1 macallan #include <sys/errno.h>
38 1.4 macallan #include <sys/kmem.h>
39 1.1 macallan #include <dev/wscons/wsdisplay_glyphcachevar.h>
40 1.4 macallan #include "opt_glyphcache.h"
41 1.4 macallan
42 1.4 macallan #ifdef GLYPHCACHE_DEBUG
43 1.4 macallan #define DPRINTF aprint_normal
44 1.4 macallan #else
45 1.4 macallan #define DPRINTF while (0) printf
46 1.4 macallan #endif
47 1.4 macallan
48 1.4 macallan static inline int
49 1.4 macallan attr2idx(long attr)
50 1.4 macallan {
51 1.4 macallan if ((attr & 0xf0f0fff8) != 0)
52 1.4 macallan return -1;
53 1.4 macallan
54 1.4 macallan return (((attr >> 16) & 0x0f) | ((attr >> 20) & 0xf0));
55 1.4 macallan }
56 1.1 macallan
57 1.1 macallan /* first line, lines, width, attr */
58 1.1 macallan int
59 1.1 macallan glyphcache_init(glyphcache *gc, int first, int lines, int width,
60 1.1 macallan int cellwidth, int cellheight, long attr)
61 1.1 macallan {
62 1.4 macallan int cache_lines, buckets, i, usedcells = 0, idx;
63 1.4 macallan gc_bucket *b;
64 1.1 macallan
65 1.4 macallan /* first the geometry stuff */
66 1.1 macallan gc->gc_cellwidth = cellwidth;
67 1.1 macallan gc->gc_cellheight = cellheight;
68 1.1 macallan gc->gc_firstline = first;
69 1.1 macallan gc->gc_cellsperline = width / cellwidth;
70 1.6 macallan gc->gc_buckets = NULL;
71 1.6 macallan gc->gc_numbuckets = 0;
72 1.3 macallan if (lines < 0) lines = 0;
73 1.1 macallan cache_lines = lines / cellheight;
74 1.1 macallan gc->gc_numcells = cache_lines * gc->gc_cellsperline;
75 1.4 macallan
76 1.4 macallan /* now allocate buckets */
77 1.4 macallan buckets = (gc->gc_numcells / 223);
78 1.4 macallan if ((buckets * 223) < gc->gc_numcells)
79 1.4 macallan buckets++;
80 1.5 macallan
81 1.5 macallan /*
82 1.5 macallan * if we don't have enough video memory to cache at least a few glyphs
83 1.5 macallan * we stop right here
84 1.5 macallan */
85 1.5 macallan if (buckets < 1)
86 1.5 macallan return ENOMEM;
87 1.5 macallan
88 1.4 macallan gc->gc_buckets = kmem_alloc(sizeof(gc_bucket) * buckets, KM_SLEEP);
89 1.4 macallan if (gc->gc_buckets == NULL) {
90 1.4 macallan aprint_error("%s: can't allocate memory\n", __func__);
91 1.4 macallan return ENOMEM;
92 1.4 macallan }
93 1.4 macallan gc->gc_numbuckets = buckets;
94 1.4 macallan
95 1.4 macallan DPRINTF("%s: using %d buckets\n", __func__, buckets);
96 1.4 macallan for (i = 0; i < buckets; i++) {
97 1.4 macallan b = &gc->gc_buckets[i];
98 1.4 macallan b->gb_firstcell = usedcells;
99 1.4 macallan b->gb_numcells = min(223, gc->gc_numcells - usedcells);
100 1.4 macallan usedcells += 223;
101 1.4 macallan b->gb_usedcells = 0;
102 1.4 macallan b->gb_index = -1;
103 1.4 macallan }
104 1.4 macallan
105 1.4 macallan /* initialize the attribute map... */
106 1.4 macallan for (i = 0; i < 256; i++) {
107 1.4 macallan gc->gc_attrmap[i] = -1;
108 1.4 macallan }
109 1.4 macallan
110 1.4 macallan /* first bucket goes to default attr */
111 1.4 macallan idx = attr2idx(attr);
112 1.4 macallan if (idx >= 0) {
113 1.4 macallan gc->gc_attrmap[idx] = 0;
114 1.4 macallan gc->gc_buckets[0].gb_index = idx;
115 1.4 macallan }
116 1.4 macallan
117 1.1 macallan glyphcache_wipe(gc);
118 1.4 macallan DPRINTF("%s: using %d cells total, from %d width %d\n", __func__,
119 1.4 macallan gc->gc_numcells, gc->gc_firstline, gc->gc_cellsperline);
120 1.1 macallan return 0;
121 1.1 macallan }
122 1.1 macallan
123 1.1 macallan void
124 1.1 macallan glyphcache_wipe(glyphcache *gc)
125 1.1 macallan {
126 1.4 macallan gc_bucket *b;
127 1.4 macallan int i, j, idx;
128 1.4 macallan
129 1.6 macallan if ((gc->gc_buckets == NULL) || (gc->gc_numbuckets < 1))
130 1.6 macallan return;
131 1.6 macallan
132 1.4 macallan idx = gc->gc_buckets[0].gb_index;
133 1.1 macallan
134 1.4 macallan /* empty all the buckets */
135 1.4 macallan for (i = 0; i < gc->gc_numbuckets; i++) {
136 1.4 macallan b = &gc->gc_buckets[i];
137 1.4 macallan b->gb_usedcells = 0;
138 1.4 macallan b->gb_index = -1;
139 1.4 macallan for (j = 0; j < b->gb_numcells; j++)
140 1.4 macallan b->gb_map[j] = -1;
141 1.4 macallan }
142 1.4 macallan
143 1.4 macallan for (i = 0; i < 256; i++) {
144 1.4 macallan gc->gc_attrmap[i] = -1;
145 1.4 macallan }
146 1.4 macallan
147 1.4 macallan /* now put the first bucket back where it was */
148 1.4 macallan gc->gc_attrmap[idx] = 0;
149 1.4 macallan gc->gc_buckets[0].gb_index = idx;
150 1.1 macallan }
151 1.1 macallan
152 1.1 macallan /*
153 1.1 macallan * add a glyph drawn at (x,y) to the cache as (c)
154 1.1 macallan * call this only if glyphcache_try() returned GC_ADD
155 1.1 macallan * caller or gc_bitblt must make sure the glyph is actually completely drawn
156 1.1 macallan */
157 1.1 macallan int
158 1.1 macallan glyphcache_add(glyphcache *gc, int c, int x, int y)
159 1.1 macallan {
160 1.4 macallan gc_bucket *b = gc->gc_next;
161 1.1 macallan int cell;
162 1.1 macallan int cx, cy;
163 1.1 macallan
164 1.4 macallan if (b->gb_usedcells >= b->gb_numcells)
165 1.1 macallan return ENOMEM;
166 1.4 macallan cell = atomic_add_int_nv(&b->gb_usedcells, 1) - 1;
167 1.4 macallan cell += b->gb_firstcell;
168 1.1 macallan cy = gc->gc_firstline +
169 1.1 macallan (cell / gc->gc_cellsperline) * gc->gc_cellheight;
170 1.1 macallan cx = (cell % gc->gc_cellsperline) * gc->gc_cellwidth;
171 1.4 macallan b->gb_map[c - 33] = (cx << 16) | cy;
172 1.1 macallan gc->gc_bitblt(gc->gc_blitcookie, x, y, cx, cy,
173 1.1 macallan gc->gc_cellwidth, gc->gc_cellheight, gc->gc_rop);
174 1.4 macallan if (gc->gc_underline & 1) {
175 1.4 macallan glyphcache_underline(gc, x, y, gc->gc_underline);
176 1.4 macallan }
177 1.1 macallan return 0;
178 1.1 macallan }
179 1.1 macallan
180 1.4 macallan void
181 1.4 macallan glyphcache_underline(glyphcache *gc, int x, int y, long attr)
182 1.4 macallan {
183 1.4 macallan if (gc->gc_rectfill == NULL)
184 1.4 macallan return;
185 1.4 macallan
186 1.4 macallan gc->gc_rectfill(gc->gc_blitcookie, x, y + gc->gc_cellheight - 2,
187 1.4 macallan gc->gc_cellwidth, 1, attr);
188 1.4 macallan }
189 1.1 macallan /*
190 1.1 macallan * check if (c) is in the cache, if so draw it at (x,y)
191 1.1 macallan * return:
192 1.1 macallan * - GC_OK when the glyph was found
193 1.1 macallan * - GC_ADD when the glyph wasn't found but can be added
194 1.1 macallan * - GC_NOPE when the glyph can't be cached
195 1.1 macallan */
196 1.1 macallan int
197 1.1 macallan glyphcache_try(glyphcache *gc, int c, int x, int y, long attr)
198 1.1 macallan {
199 1.4 macallan int cell, cx, cy, idx, bi;
200 1.4 macallan gc_bucket *b;
201 1.4 macallan
202 1.4 macallan idx = attr2idx(attr);
203 1.4 macallan /* see if we're in range */
204 1.4 macallan if ((c < 33) || (c > 255) || (idx < 0))
205 1.1 macallan return GC_NOPE;
206 1.4 macallan /* see if there's already a bucket for this attribute */
207 1.4 macallan bi = gc->gc_attrmap[idx];
208 1.4 macallan if (bi == -1) {
209 1.4 macallan /* nope, see if there's an empty one left */
210 1.4 macallan bi = 1;
211 1.4 macallan while ((bi < gc->gc_numbuckets) &&
212 1.4 macallan (gc->gc_buckets[bi].gb_index != -1)) {
213 1.4 macallan bi++;
214 1.4 macallan }
215 1.4 macallan if (bi < gc->gc_numbuckets) {
216 1.4 macallan /* found one -> grab it */
217 1.4 macallan gc->gc_attrmap[idx] = bi;
218 1.4 macallan b = &gc->gc_buckets[bi];
219 1.4 macallan b->gb_index = idx;
220 1.4 macallan b->gb_usedcells = 0;
221 1.4 macallan /* make sure this doesn't get evicted right away */
222 1.4 macallan b->gb_lastread = time_uptime;
223 1.4 macallan } else {
224 1.4 macallan /*
225 1.4 macallan * still nothing
226 1.4 macallan * steal the least recently read bucket
227 1.4 macallan */
228 1.4 macallan time_t moo = time_uptime;
229 1.4 macallan int i, oldest = 1;
230 1.4 macallan
231 1.4 macallan for (i = 1; i < gc->gc_numbuckets; i++) {
232 1.4 macallan if (gc->gc_buckets[i].gb_lastread < moo) {
233 1.4 macallan oldest = i;
234 1.4 macallan moo = gc->gc_buckets[i].gb_lastread;
235 1.4 macallan }
236 1.4 macallan }
237 1.4 macallan
238 1.4 macallan /* if we end up here all buckets must be in use */
239 1.4 macallan b = &gc->gc_buckets[oldest];
240 1.4 macallan gc->gc_attrmap[b->gb_index] = -1;
241 1.4 macallan b->gb_index = idx;
242 1.4 macallan b->gb_usedcells = 0;
243 1.4 macallan gc->gc_attrmap[idx] = oldest;
244 1.4 macallan /* now scrub it */
245 1.4 macallan for (i = 0; i < b->gb_numcells; i++)
246 1.4 macallan b->gb_map[i] = -1;
247 1.4 macallan /* and set the time stamp */
248 1.4 macallan b->gb_lastread = time_uptime;
249 1.4 macallan }
250 1.4 macallan } else {
251 1.4 macallan /* found one */
252 1.4 macallan b = &gc->gc_buckets[bi];
253 1.4 macallan }
254 1.4 macallan
255 1.4 macallan /* see if there's room in the bucket */
256 1.4 macallan if (b->gb_usedcells >= b->gb_numcells)
257 1.1 macallan return GC_NOPE;
258 1.4 macallan
259 1.4 macallan cell = b->gb_map[c - 33];
260 1.4 macallan if (cell == -1) {
261 1.4 macallan gc->gc_next = b;
262 1.4 macallan gc->gc_underline = attr;
263 1.1 macallan return GC_ADD;
264 1.4 macallan }
265 1.4 macallan
266 1.4 macallan /* it's in the cache - draw it */
267 1.2 macallan cy = cell & 0xffff;
268 1.2 macallan cx = (cell >> 16) & 0xffff;
269 1.1 macallan gc->gc_bitblt(gc->gc_blitcookie, cx, cy, x, y,
270 1.1 macallan gc->gc_cellwidth, gc->gc_cellheight, gc->gc_rop);
271 1.4 macallan /* and underline it if needed */
272 1.4 macallan if (attr & 1)
273 1.4 macallan glyphcache_underline(gc, x, y, attr);
274 1.4 macallan /* update bucket's time stamp */
275 1.4 macallan b->gb_lastread = time_uptime;
276 1.1 macallan return GC_OK;
277 1.1 macallan }
278