summaryrefslogtreecommitdiffstats
path: root/firmware/font_cache.c
blob: 4624b1866ec17d92f3fabc555d021291e0958c96 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
/***************************************************************************
 *             __________               __   ___.
 *   Open      \______   \ ____   ____ |  | _\_ |__   _______  ___
 *   Source     |       _//  _ \_/ ___\|  |/ /| __ \ /  _ \  \/  /
 *   Jukebox    |    |   (  <_> )  \___|    < | \_\ (  <_> > <  <
 *   Firmware   |____|_  /\____/ \___  >__|_ \|___  /\____/__/\_ \
 *                     \/            \/     \/    \/            \/
 *
 * Copyright (C) 2003 Tat Tang
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
 * KIND, either express or implied.
 *
 ****************************************************************************/

#include <string.h>
#include "font_cache.h"
#include "debug.h"

/*******************************************************************************
 * font_cache_lru_init
 ******************************************************************************/
static void font_cache_lru_init(void* data)
{
    struct font_cache_entry* p = data;
    p->_char_code = 0xffff;   /* assume invalid char */
}

/*******************************************************************************
 * font_cache_create
 ******************************************************************************/
void font_cache_create(
    struct font_cache* fcache,
    void *buf,
    int buf_size,
    int bitmap_bytes_size)
{
    int font_cache_entry_size =
        sizeof(struct font_cache_entry) + bitmap_bytes_size;

    /* make sure font cache entries are a multiple of 16 bits */
    if (font_cache_entry_size % 2 != 0)
        font_cache_entry_size++;

    int cache_size = buf_size /
        (font_cache_entry_size + LRU_SLOT_OVERHEAD + sizeof(short));

    fcache->_size = 1;
    fcache->_capacity = cache_size;
    fcache->_prev_result = 0;
    fcache->_prev_char_code = 0;

    /* set up index */
    fcache->_index = buf;

    /* set up lru list */
    unsigned char* lru_buf = buf;
    lru_buf += sizeof(short) * cache_size;
    lru_create(&fcache->_lru, lru_buf, cache_size, font_cache_entry_size);

    /* initialise cache */
    lru_traverse(&fcache->_lru, font_cache_lru_init);
    short i;
    for (i = 0; i < cache_size; i++)
        fcache->_index[i] = i; /* small cheat here */
}

/*************************************************************************
 * Binary search that attempts a primary lucky guess that succeeds
 * when there are consecutive codes in the cache between previous 
 * search and new search. Returns a negative of insertion point if 
 * not found.
 ************************************************************************/
static int search(struct font_cache* fcache,
                  unsigned short char_code,
                  int *p_insertion_point )
{
    struct font_cache_entry *p;
    int left, right, mid=-1, c;
    left = 0;
    right = fcache->_size - 1;

    /* go for a lucky guess */
    mid = char_code + 
        fcache->_prev_result - fcache->_prev_char_code;
            
    /* check bounds */        
    if ( mid < 0 || mid > right )
            mid = ( left + right ) / 2;

    do
    {
        p = lru_data(&fcache->_lru, fcache->_index[mid]);
        c = p->_char_code - char_code;

        if (c == 0)
        {
            fcache->_prev_result = mid;
            fcache->_prev_char_code = char_code;
            *p_insertion_point = mid;
            return 1;
        }
        if (c < 0)
            left = mid + 1;
        else
            right = mid - 1;

        mid = (left + right) / 2;
    }
    while (left <= right);
    
    /* not found */
    *p_insertion_point = mid;
    return 0;
}
/*******************************************************************************
 * font_cache_get
 ******************************************************************************/
struct font_cache_entry* font_cache_get(
    struct font_cache* fcache,
    unsigned short char_code,
    void (*callback) (struct font_cache_entry* p, void *callback_data),
    void *callback_data)
{
    struct font_cache_entry* p;
    int insertion_point;
    int index_to_replace;
    
    if( search(fcache, char_code, &insertion_point))
    {
        short lru_handle = fcache->_index[insertion_point];
        p = lru_data(&fcache->_lru, lru_handle);
        if (p->_char_code == char_code)
        {
            lru_touch(&fcache->_lru, lru_handle);
            return lru_data(&fcache->_lru, lru_handle);
        }
    }
    else  /* not found */
    {
        p = lru_data(&fcache->_lru, 
                     fcache->_index[insertion_point+1]);
        if ( char_code > p->_char_code )
            insertion_point++;
    }

    /* find index to replace */
    short lru_handle_to_replace = fcache->_lru._head;
    p = lru_data(&fcache->_lru, lru_handle_to_replace);
    search(fcache, p->_char_code, &index_to_replace);
        
    if (insertion_point < index_to_replace)
    {
        /* shift memory up */
        memmove(fcache->_index + insertion_point + 2,
                fcache->_index + insertion_point + 1,
                (index_to_replace - insertion_point - 1) * sizeof(short));

        /* add to index */
        fcache->_index[insertion_point + 1] = lru_handle_to_replace;
    }
    else if (insertion_point > index_to_replace)
    {
        /* shift memory down */
        memmove(fcache->_index + index_to_replace,
                fcache->_index + index_to_replace + 1,
                (insertion_point - index_to_replace) * sizeof(short));

        /* add to index */
        fcache->_index[insertion_point] = lru_handle_to_replace;
    }

    /* load new entry into cache */
    lru_touch(&fcache->_lru, lru_handle_to_replace);

    if (fcache->_size < fcache->_capacity)
        fcache->_size++;

    p->_char_code = char_code;
    /* fill bitmap */
    callback(p, callback_data);
    return p;
}