Home | History | Annotate | Line # | Download | only in wscons
wsdisplay_glyphcache.c revision 1.1
      1  1.1  macallan /*	$NetBSD: wsdisplay_glyphcache.c,v 1.1 2012/02/16 17:29:21 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.1  macallan 	cache_lines = lines / cellheight;
     51  1.1  macallan 	gc->gc_numcells = cache_lines * gc->gc_cellsperline;
     52  1.1  macallan 	if (gc->gc_numcells > 256)
     53  1.1  macallan 		gc->gc_numcells = 256;
     54  1.1  macallan 	gc->gc_attr = attr;
     55  1.1  macallan 	glyphcache_wipe(gc);
     56  1.1  macallan 	return 0;
     57  1.1  macallan }
     58  1.1  macallan 
     59  1.1  macallan void
     60  1.1  macallan glyphcache_wipe(glyphcache *gc)
     61  1.1  macallan {
     62  1.1  macallan 	int i;
     63  1.1  macallan 
     64  1.1  macallan 	gc->gc_usedcells = 0;
     65  1.1  macallan 	for (i = 0; i < 256; i++)
     66  1.1  macallan 		gc->gc_map[i] = -1;
     67  1.1  macallan }
     68  1.1  macallan 
     69  1.1  macallan /*
     70  1.1  macallan  * add a glyph drawn at (x,y) to the cache as (c)
     71  1.1  macallan  * call this only if glyphcache_try() returned GC_ADD
     72  1.1  macallan  * caller or gc_bitblt must make sure the glyph is actually completely drawn
     73  1.1  macallan  */
     74  1.1  macallan int
     75  1.1  macallan glyphcache_add(glyphcache *gc, int c, int x, int y)
     76  1.1  macallan {
     77  1.1  macallan 	int cell;
     78  1.1  macallan 	int cx, cy;
     79  1.1  macallan 
     80  1.1  macallan 	if (gc->gc_map[c] != -1)
     81  1.1  macallan 		return EINVAL;
     82  1.1  macallan 	if (gc->gc_usedcells >= gc->gc_numcells)
     83  1.1  macallan 		return ENOMEM;
     84  1.1  macallan 	cell = atomic_add_int_nv(&gc->gc_usedcells, 1) - 1;
     85  1.1  macallan 	gc->gc_map[c] = cell;
     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.1  macallan 	gc->gc_bitblt(gc->gc_blitcookie, x, y, cx, cy,
     90  1.1  macallan 	    gc->gc_cellwidth, gc->gc_cellheight, gc->gc_rop);
     91  1.1  macallan 	return 0;
     92  1.1  macallan }
     93  1.1  macallan 
     94  1.1  macallan /*
     95  1.1  macallan  * check if (c) is in the cache, if so draw it at (x,y)
     96  1.1  macallan  * return:
     97  1.1  macallan  * - GC_OK when the glyph was found
     98  1.1  macallan  * - GC_ADD when the glyph wasn't found but can be added
     99  1.1  macallan  * - GC_NOPE when the glyph can't be cached
    100  1.1  macallan  */
    101  1.1  macallan int
    102  1.1  macallan glyphcache_try(glyphcache *gc, int c, int x, int y, long attr)
    103  1.1  macallan {
    104  1.1  macallan 	int cell, cx, cy;
    105  1.1  macallan 	if ((c < 0) || (c > 255) || (attr != gc->gc_attr))
    106  1.1  macallan 		return GC_NOPE;
    107  1.1  macallan 	if (gc->gc_usedcells >= gc->gc_numcells)
    108  1.1  macallan 		return GC_NOPE;
    109  1.1  macallan 	cell = gc->gc_map[c];
    110  1.1  macallan 	if (cell == -1)
    111  1.1  macallan 		return GC_ADD;
    112  1.1  macallan 	cy = gc->gc_firstline +
    113  1.1  macallan 	    (cell / gc->gc_cellsperline) * gc->gc_cellheight;
    114  1.1  macallan 	cx = (cell % gc->gc_cellsperline) * gc->gc_cellwidth;
    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