wsdisplay_glyphcache.c revision 1.3 1 1.3 macallan /* $NetBSD: wsdisplay_glyphcache.c,v 1.3 2012/07/12 01:21:08 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.1 macallan
35 1.1 macallan #include <sys/atomic.h>
36 1.1 macallan #include <sys/errno.h>
37 1.1 macallan #include <dev/wscons/wsdisplay_glyphcachevar.h>
38 1.1 macallan
39 1.1 macallan /* first line, lines, width, attr */
40 1.1 macallan int
41 1.1 macallan glyphcache_init(glyphcache *gc, int first, int lines, int width,
42 1.1 macallan int cellwidth, int cellheight, long attr)
43 1.1 macallan {
44 1.1 macallan int cache_lines;
45 1.1 macallan
46 1.1 macallan gc->gc_cellwidth = cellwidth;
47 1.1 macallan gc->gc_cellheight = cellheight;
48 1.1 macallan gc->gc_firstline = first;
49 1.1 macallan gc->gc_cellsperline = width / cellwidth;
50 1.3 macallan if (lines < 0) lines = 0;
51 1.1 macallan cache_lines = lines / cellheight;
52 1.1 macallan gc->gc_numcells = cache_lines * gc->gc_cellsperline;
53 1.1 macallan if (gc->gc_numcells > 256)
54 1.1 macallan gc->gc_numcells = 256;
55 1.1 macallan gc->gc_attr = attr;
56 1.1 macallan glyphcache_wipe(gc);
57 1.1 macallan return 0;
58 1.1 macallan }
59 1.1 macallan
60 1.1 macallan void
61 1.1 macallan glyphcache_wipe(glyphcache *gc)
62 1.1 macallan {
63 1.1 macallan int i;
64 1.1 macallan
65 1.1 macallan gc->gc_usedcells = 0;
66 1.1 macallan for (i = 0; i < 256; i++)
67 1.1 macallan gc->gc_map[i] = -1;
68 1.1 macallan }
69 1.1 macallan
70 1.1 macallan /*
71 1.1 macallan * add a glyph drawn at (x,y) to the cache as (c)
72 1.1 macallan * call this only if glyphcache_try() returned GC_ADD
73 1.1 macallan * caller or gc_bitblt must make sure the glyph is actually completely drawn
74 1.1 macallan */
75 1.1 macallan int
76 1.1 macallan glyphcache_add(glyphcache *gc, int c, int x, int y)
77 1.1 macallan {
78 1.1 macallan int cell;
79 1.1 macallan int cx, cy;
80 1.1 macallan
81 1.1 macallan if (gc->gc_map[c] != -1)
82 1.1 macallan return EINVAL;
83 1.1 macallan if (gc->gc_usedcells >= gc->gc_numcells)
84 1.1 macallan return ENOMEM;
85 1.1 macallan cell = atomic_add_int_nv(&gc->gc_usedcells, 1) - 1;
86 1.1 macallan cy = gc->gc_firstline +
87 1.1 macallan (cell / gc->gc_cellsperline) * gc->gc_cellheight;
88 1.1 macallan cx = (cell % gc->gc_cellsperline) * gc->gc_cellwidth;
89 1.2 macallan gc->gc_map[c] = (cx << 16) | cy;
90 1.1 macallan gc->gc_bitblt(gc->gc_blitcookie, x, y, cx, cy,
91 1.1 macallan gc->gc_cellwidth, gc->gc_cellheight, gc->gc_rop);
92 1.1 macallan return 0;
93 1.1 macallan }
94 1.1 macallan
95 1.1 macallan /*
96 1.1 macallan * check if (c) is in the cache, if so draw it at (x,y)
97 1.1 macallan * return:
98 1.1 macallan * - GC_OK when the glyph was found
99 1.1 macallan * - GC_ADD when the glyph wasn't found but can be added
100 1.1 macallan * - GC_NOPE when the glyph can't be cached
101 1.1 macallan */
102 1.1 macallan int
103 1.1 macallan glyphcache_try(glyphcache *gc, int c, int x, int y, long attr)
104 1.1 macallan {
105 1.1 macallan int cell, cx, cy;
106 1.1 macallan if ((c < 0) || (c > 255) || (attr != gc->gc_attr))
107 1.1 macallan return GC_NOPE;
108 1.1 macallan if (gc->gc_usedcells >= gc->gc_numcells)
109 1.1 macallan return GC_NOPE;
110 1.1 macallan cell = gc->gc_map[c];
111 1.1 macallan if (cell == -1)
112 1.1 macallan return GC_ADD;
113 1.2 macallan cy = cell & 0xffff;
114 1.2 macallan cx = (cell >> 16) & 0xffff;
115 1.1 macallan gc->gc_bitblt(gc->gc_blitcookie, cx, cy, x, y,
116 1.1 macallan gc->gc_cellwidth, gc->gc_cellheight, gc->gc_rop);
117 1.1 macallan return GC_OK;
118 1.1 macallan }
119