/[rdesktop]/jpeg/rdesktop/trunk/cache.c
This is repository of my old source code which isn't updated any more. Go to git.rot13.org for current projects!
ViewVC logotype

Annotation of /jpeg/rdesktop/trunk/cache.c

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1483 - (hide annotations)
Sat Nov 1 02:37:10 2008 UTC (15 years, 6 months ago) by jsorg71
Original Path: sourceforge.net/trunk/rdesktop/cache.c
File MIME type: text/plain
File size: 10204 byte(s)
brush cache for > 2 color brushes

1 astrand 963 /* -*- c-basic-offset: 8 -*-
2 matty 9 rdesktop: A Remote Desktop Protocol client.
3     Cache routines
4 jsorg71 1477 Copyright (C) Matthew Chapman 1999-2008
5 jdmeijer 830 Copyright (C) Jeroen Meijer 2005
6 jsorg71 774
7 matty 9 This program is free software; you can redistribute it and/or modify
8     it under the terms of the GNU General Public License as published by
9     the Free Software Foundation; either version 2 of the License, or
10     (at your option) any later version.
11 jsorg71 774
12 matty 9 This program is distributed in the hope that it will be useful,
13     but WITHOUT ANY WARRANTY; without even the implied warranty of
14     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15     GNU General Public License for more details.
16 jsorg71 774
17 matty 9 You should have received a copy of the GNU General Public License
18     along with this program; if not, write to the Free Software
19     Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
20     */
21    
22 matty 10 #include "rdesktop.h"
23 matty 9
24 jdmeijer 830 /* BITMAP CACHE */
25     extern int g_pstcache_fd[];
26    
27 matty 10 #define NUM_ELEMENTS(array) (sizeof(array) / sizeof(array[0]))
28 jsorg71 725 #define IS_PERSISTENT(id) (g_pstcache_fd[id] > 0)
29 jdmeijer 830 #define TO_TOP -1
30     #define NOT_SET -1
31     #define IS_SET(idx) (idx >= 0)
32 matty 10
33 jdmeijer 830 /*
34     * TODO: Test for optimal value of BUMP_COUNT. TO_TOP gives lowest cpu utilisation but using
35     * a positive value will hopefully result in less frequently used bitmaps having a greater chance
36     * of being evicted from the cache, and therby reducing the need to load bitmaps from disk.
37     * (Jeroen)
38     */
39     #define BUMP_COUNT 40
40 matty 10
41 jdmeijer 830 struct bmpcache_entry
42     {
43 jsorg71 1364 RD_HBITMAP bitmap;
44 jdmeijer 830 sint16 previous;
45     sint16 next;
46     };
47 jsorg71 725
48 jdmeijer 830 static struct bmpcache_entry g_bmpcache[3][0xa00];
49 jsorg71 1364 static RD_HBITMAP g_volatile_bc[3];
50 matty 10
51 jdmeijer 830 static int g_bmpcache_lru[3] = { NOT_SET, NOT_SET, NOT_SET };
52     static int g_bmpcache_mru[3] = { NOT_SET, NOT_SET, NOT_SET };
53     static int g_bmpcache_count[3];
54    
55     /* Setup the bitmap cache lru/mru linked list */
56 jsorg71 725 void
57 jdmeijer 830 cache_rebuild_bmpcache_linked_list(uint8 id, sint16 * idx, int count)
58 jsorg71 725 {
59 jdmeijer 830 int n = count, c = 0;
60     sint16 n_idx;
61 jsorg71 725
62 jdmeijer 830 /* find top, skip evicted bitmaps */
63     while (--n >= 0 && g_bmpcache[id][idx[n]].bitmap == NULL);
64     if (n < 0)
65 jsorg71 725 {
66 jdmeijer 830 g_bmpcache_mru[id] = g_bmpcache_lru[id] = NOT_SET;
67     return;
68     }
69    
70     g_bmpcache_mru[id] = idx[n];
71     g_bmpcache[id][idx[n]].next = NOT_SET;
72     n_idx = idx[n];
73     c++;
74    
75     /* link list */
76     while (n >= 0)
77     {
78     /* skip evicted bitmaps */
79     while (--n >= 0 && g_bmpcache[id][idx[n]].bitmap == NULL);
80    
81     if (n < 0)
82     break;
83    
84     g_bmpcache[id][n_idx].previous = idx[n];
85     g_bmpcache[id][idx[n]].next = n_idx;
86     n_idx = idx[n];
87     c++;
88     }
89    
90     g_bmpcache[id][n_idx].previous = NOT_SET;
91     g_bmpcache_lru[id] = n_idx;
92    
93     if (c != g_bmpcache_count[id])
94     {
95     error("Oops. %d in bitmap cache linked list, %d in ui cache...\n", c,
96     g_bmpcache_count[id]);
97     exit(1);
98     }
99     }
100    
101     /* Move a bitmap to a new position in the linked list. */
102     void
103     cache_bump_bitmap(uint8 id, uint16 idx, int bump)
104     {
105     int p_idx, n_idx, n;
106    
107     if (!IS_PERSISTENT(id))
108     return;
109    
110     if (g_bmpcache_mru[id] == idx)
111     return;
112    
113     DEBUG_RDP5(("bump bitmap: id=%d, idx=%d, bump=%d\n", id, idx, bump));
114    
115     n_idx = g_bmpcache[id][idx].next;
116     p_idx = g_bmpcache[id][idx].previous;
117    
118     if (IS_SET(n_idx))
119     {
120     /* remove */
121     --g_bmpcache_count[id];
122     if (IS_SET(p_idx))
123     g_bmpcache[id][p_idx].next = n_idx;
124     else
125     g_bmpcache_lru[id] = n_idx;
126     if (IS_SET(n_idx))
127     g_bmpcache[id][n_idx].previous = p_idx;
128     else
129     g_bmpcache_mru[id] = p_idx;
130     }
131     else
132     {
133     p_idx = NOT_SET;
134     n_idx = g_bmpcache_lru[id];
135     }
136    
137     if (bump >= 0)
138     {
139     for (n = 0; n < bump && IS_SET(n_idx); n++)
140 jsorg71 725 {
141 jdmeijer 830 p_idx = n_idx;
142     n_idx = g_bmpcache[id][p_idx].next;
143 jsorg71 725 }
144     }
145 jdmeijer 830 else
146     {
147     p_idx = g_bmpcache_mru[id];
148     n_idx = NOT_SET;
149     }
150 jsorg71 725
151 jdmeijer 830 /* insert */
152     ++g_bmpcache_count[id];
153     g_bmpcache[id][idx].previous = p_idx;
154     g_bmpcache[id][idx].next = n_idx;
155    
156     if (p_idx >= 0)
157     g_bmpcache[id][p_idx].next = idx;
158     else
159     g_bmpcache_lru[id] = idx;
160    
161     if (n_idx >= 0)
162     g_bmpcache[id][n_idx].previous = idx;
163     else
164     g_bmpcache_mru[id] = idx;
165 jsorg71 725 }
166    
167 jdmeijer 830 /* Evict the least-recently used bitmap from the cache */
168     void
169     cache_evict_bitmap(uint8 id)
170     {
171     uint16 idx;
172     int n_idx;
173    
174     if (!IS_PERSISTENT(id))
175     return;
176    
177     idx = g_bmpcache_lru[id];
178     n_idx = g_bmpcache[id][idx].next;
179     DEBUG_RDP5(("evict bitmap: id=%d idx=%d n_idx=%d bmp=0x%x\n", id, idx, n_idx,
180     g_bmpcache[id][idx].bitmap));
181    
182     ui_destroy_bitmap(g_bmpcache[id][idx].bitmap);
183     --g_bmpcache_count[id];
184     g_bmpcache[id][idx].bitmap = 0;
185    
186     g_bmpcache_lru[id] = n_idx;
187     g_bmpcache[id][n_idx].previous = NOT_SET;
188    
189     pstcache_touch_bitmap(id, idx, 0);
190     }
191    
192 matty 10 /* Retrieve a bitmap from the cache */
193 jsorg71 1364 RD_HBITMAP
194 jdmeijer 830 cache_get_bitmap(uint8 id, uint16 idx)
195 matty 9 {
196 jdmeijer 830 if ((id < NUM_ELEMENTS(g_bmpcache)) && (idx < NUM_ELEMENTS(g_bmpcache[0])))
197 matty 9 {
198 jdmeijer 830 if (g_bmpcache[id][idx].bitmap || pstcache_load_bitmap(id, idx))
199 jsorg71 725 {
200 jdmeijer 830 if (IS_PERSISTENT(id))
201     cache_bump_bitmap(id, idx, BUMP_COUNT);
202 jsorg71 725
203 jdmeijer 830 return g_bmpcache[id][idx].bitmap;
204 jsorg71 725 }
205 matty 9 }
206 jdmeijer 830 else if ((id < NUM_ELEMENTS(g_volatile_bc)) && (idx == 0x7fff))
207 jsorg71 730 {
208 jdmeijer 830 return g_volatile_bc[id];
209 jsorg71 730 }
210 matty 9
211 jdmeijer 830 error("get bitmap %d:%d\n", id, idx);
212 matty 9 return NULL;
213     }
214    
215 matty 10 /* Store a bitmap in the cache */
216 matty 25 void
217 jsorg71 1364 cache_put_bitmap(uint8 id, uint16 idx, RD_HBITMAP bitmap)
218 matty 9 {
219 jsorg71 1364 RD_HBITMAP old;
220 matty 9
221 jdmeijer 830 if ((id < NUM_ELEMENTS(g_bmpcache)) && (idx < NUM_ELEMENTS(g_bmpcache[0])))
222 matty 9 {
223 jdmeijer 830 old = g_bmpcache[id][idx].bitmap;
224 matty 9 if (old != NULL)
225 matty 10 ui_destroy_bitmap(old);
226 jdmeijer 830 g_bmpcache[id][idx].bitmap = bitmap;
227    
228     if (IS_PERSISTENT(id))
229 jsorg71 725 {
230 jdmeijer 830 if (old == NULL)
231     g_bmpcache[id][idx].previous = g_bmpcache[id][idx].next = NOT_SET;
232    
233     cache_bump_bitmap(id, idx, TO_TOP);
234     if (g_bmpcache_count[id] > BMPCACHE2_C2_CELLS)
235     cache_evict_bitmap(id);
236 jsorg71 725 }
237 matty 9 }
238 jdmeijer 830 else if ((id < NUM_ELEMENTS(g_volatile_bc)) && (idx == 0x7fff))
239 jsorg71 730 {
240 jdmeijer 830 old = g_volatile_bc[id];
241 jsorg71 730 if (old != NULL)
242     ui_destroy_bitmap(old);
243 jdmeijer 830 g_volatile_bc[id] = bitmap;
244 jsorg71 730 }
245 matty 9 else
246     {
247 jdmeijer 830 error("put bitmap %d:%d\n", id, idx);
248 matty 9 }
249     }
250    
251 jsorg71 725 /* Updates the persistent bitmap cache MRU information on exit */
252     void
253     cache_save_state(void)
254     {
255 jdmeijer 830 uint32 id = 0, t = 0;
256     int idx;
257 matty 10
258 jsorg71 725 for (id = 0; id < NUM_ELEMENTS(g_bmpcache); id++)
259     if (IS_PERSISTENT(id))
260 jdmeijer 830 {
261     DEBUG_RDP5(("Saving cache state for bitmap cache %d...", id));
262     idx = g_bmpcache_lru[id];
263     while (idx >= 0)
264     {
265     pstcache_touch_bitmap(id, idx, ++t);
266     idx = g_bmpcache[id][idx].next;
267     }
268     DEBUG_RDP5((" %d stamps written.\n", t));
269     }
270 jsorg71 725 }
271    
272    
273 matty 10 /* FONT CACHE */
274 jsorg71 379 static FONTGLYPH g_fontcache[12][256];
275 matty 10
276     /* Retrieve a glyph from the font cache */
277 matty 25 FONTGLYPH *
278     cache_get_font(uint8 font, uint16 character)
279 matty 9 {
280 matty 10 FONTGLYPH *glyph;
281 matty 9
282 jsorg71 379 if ((font < NUM_ELEMENTS(g_fontcache)) && (character < NUM_ELEMENTS(g_fontcache[0])))
283 matty 9 {
284 jsorg71 379 glyph = &g_fontcache[font][character];
285 astrand 1294 if (glyph->pixmap != NULL)
286 matty 9 return glyph;
287     }
288    
289 matty 30 error("get font %d:%d\n", font, character);
290 matty 9 return NULL;
291     }
292    
293 matty 10 /* Store a glyph in the font cache */
294 matty 25 void
295     cache_put_font(uint8 font, uint16 character, uint16 offset,
296 jsorg71 1364 uint16 baseline, uint16 width, uint16 height, RD_HGLYPH pixmap)
297 matty 9 {
298 matty 10 FONTGLYPH *glyph;
299 matty 9
300 jsorg71 379 if ((font < NUM_ELEMENTS(g_fontcache)) && (character < NUM_ELEMENTS(g_fontcache[0])))
301 matty 9 {
302 jsorg71 379 glyph = &g_fontcache[font][character];
303 matty 9 if (glyph->pixmap != NULL)
304 matty 10 ui_destroy_glyph(glyph->pixmap);
305 matty 9
306 matty 20 glyph->offset = offset;
307 matty 9 glyph->baseline = baseline;
308     glyph->width = width;
309     glyph->height = height;
310     glyph->pixmap = pixmap;
311     }
312     else
313     {
314 matty 30 error("put font %d:%d\n", font, character);
315 matty 9 }
316     }
317    
318 matty 10
319     /* TEXT CACHE */
320 jsorg71 379 static DATABLOB g_textcache[256];
321 matty 10
322     /* Retrieve a text item from the cache */
323 matty 25 DATABLOB *
324     cache_get_text(uint8 cache_id)
325 matty 9 {
326 matty 10 DATABLOB *text;
327 matty 9
328 jsorg71 789 text = &g_textcache[cache_id];
329     return text;
330 matty 9 }
331    
332 matty 10 /* Store a text item in the cache */
333 matty 25 void
334     cache_put_text(uint8 cache_id, void *data, int length)
335 matty 9 {
336 matty 10 DATABLOB *text;
337 matty 9
338 jsorg71 789 text = &g_textcache[cache_id];
339     if (text->data != NULL)
340     xfree(text->data);
341     text->data = xmalloc(length);
342     text->size = length;
343     memcpy(text->data, data, length);
344 matty 9 }
345 matty 10
346    
347     /* DESKTOP CACHE */
348 jsorg71 379 static uint8 g_deskcache[0x38400 * 4];
349 matty 10
350     /* Retrieve desktop data from the cache */
351 matty 25 uint8 *
352 matty 28 cache_get_desktop(uint32 offset, int cx, int cy, int bytes_per_pixel)
353 matty 10 {
354 matty 28 int length = cx * cy * bytes_per_pixel;
355 matty 16
356 jsorg71 563 if (offset > sizeof(g_deskcache))
357     offset = 0;
358    
359 jsorg71 379 if ((offset + length) <= sizeof(g_deskcache))
360 matty 10 {
361 jsorg71 379 return &g_deskcache[offset];
362 matty 10 }
363    
364 matty 30 error("get desktop %d:%d\n", offset, length);
365 matty 10 return NULL;
366     }
367    
368     /* Store desktop data in the cache */
369 matty 25 void
370 astrand 82 cache_put_desktop(uint32 offset, int cx, int cy, int scanline, int bytes_per_pixel, uint8 * data)
371 matty 10 {
372 matty 28 int length = cx * cy * bytes_per_pixel;
373 matty 16
374 jsorg71 563 if (offset > sizeof(g_deskcache))
375     offset = 0;
376    
377 jsorg71 379 if ((offset + length) <= sizeof(g_deskcache))
378 matty 10 {
379 matty 28 cx *= bytes_per_pixel;
380 matty 16 while (cy--)
381     {
382 jsorg71 379 memcpy(&g_deskcache[offset], data, cx);
383 matty 16 data += scanline;
384     offset += cx;
385     }
386 matty 10 }
387     else
388     {
389 matty 30 error("put desktop %d:%d\n", offset, length);
390 matty 10 }
391     }
392 matty 28
393    
394     /* CURSOR CACHE */
395 jsorg71 1364 static RD_HCURSOR g_cursorcache[0x20];
396 matty 28
397     /* Retrieve cursor from cache */
398 jsorg71 1364 RD_HCURSOR
399 astrand 64 cache_get_cursor(uint16 cache_idx)
400 matty 28 {
401 jsorg71 1364 RD_HCURSOR cursor;
402 matty 28
403 jsorg71 379 if (cache_idx < NUM_ELEMENTS(g_cursorcache))
404 matty 28 {
405 jsorg71 379 cursor = g_cursorcache[cache_idx];
406 matty 28 if (cursor != NULL)
407     return cursor;
408     }
409    
410 matty 30 error("get cursor %d\n", cache_idx);
411 matty 28 return NULL;
412     }
413    
414     /* Store cursor in cache */
415     void
416 jsorg71 1364 cache_put_cursor(uint16 cache_idx, RD_HCURSOR cursor)
417 matty 28 {
418 jsorg71 1364 RD_HCURSOR old;
419 matty 28
420 jsorg71 379 if (cache_idx < NUM_ELEMENTS(g_cursorcache))
421 matty 28 {
422 jsorg71 379 old = g_cursorcache[cache_idx];
423 matty 28 if (old != NULL)
424     ui_destroy_cursor(old);
425    
426 jsorg71 379 g_cursorcache[cache_idx] = cursor;
427 matty 28 }
428     else
429     {
430 matty 30 error("put cursor %d\n", cache_idx);
431 matty 28 }
432     }
433 jsorg71 1474
434     /* BRUSH CACHE */
435 jsorg71 1483 /* index 0 is 2 colour brush, index 1 is muti colour brush */
436     static BRUSHDATA g_brushcache[2][64];
437 jsorg71 1474
438     /* Retrieve brush from cache */
439     BRUSHDATA *
440 jsorg71 1483 cache_get_brush_data(uint8 colour_code, uint8 idx)
441 jsorg71 1474 {
442 jsorg71 1483 colour_code = colour_code == 1 ? 0 : 1;
443     if (idx < NUM_ELEMENTS(g_brushcache[0]))
444 jsorg71 1474 {
445 jsorg71 1483 return &g_brushcache[colour_code][idx];
446 jsorg71 1474 }
447 jsorg71 1483 error("get brush %d %d\n", colour_code, idx);
448 jsorg71 1474 return NULL;
449     }
450    
451     /* Store brush in cache */
452 jsorg71 1483 /* this function takes over the data pointer in struct, eg, caller gives it up */
453 jsorg71 1474 void
454 jsorg71 1483 cache_put_brush_data(uint8 colour_code, uint8 idx, BRUSHDATA * brush_data)
455 jsorg71 1474 {
456 jsorg71 1483 BRUSHDATA * bd;
457    
458     colour_code = colour_code == 1 ? 0 : 1;
459     if (idx < NUM_ELEMENTS(g_brushcache[0]))
460 jsorg71 1474 {
461 jsorg71 1483 bd = &g_brushcache[colour_code][idx];
462     if (bd->data != 0)
463     {
464     xfree(bd->data);
465     }
466     memcpy(bd, brush_data, sizeof(BRUSHDATA));
467 jsorg71 1474 }
468     else
469     {
470 jsorg71 1483 error("put brush %d %d\n", colour_code, idx);
471 jsorg71 1474 }
472     }

  ViewVC Help
Powered by ViewVC 1.1.26