1 |
/* |
/* |
2 |
rdesktop: A Remote Desktop Protocol client. |
rdesktop: A Remote Desktop Protocol client. |
3 |
Cache routines |
Cache routines |
4 |
Copyright (C) Matthew Chapman 1999-2001 |
Copyright (C) Matthew Chapman 1999-2002 |
5 |
|
|
6 |
This program is free software; you can redistribute it and/or modify |
This program is free software; you can redistribute it and/or modify |
7 |
it under the terms of the GNU General Public License as published by |
it under the terms of the GNU General Public License as published by |
21 |
#include "rdesktop.h" |
#include "rdesktop.h" |
22 |
|
|
23 |
#define NUM_ELEMENTS(array) (sizeof(array) / sizeof(array[0])) |
#define NUM_ELEMENTS(array) (sizeof(array) / sizeof(array[0])) |
24 |
|
#define TOUCH(id, idx) (g_bmpcache[id][idx].usage = ++g_stamp) |
25 |
|
#define IS_PERSISTENT(id) (g_pstcache_fd[id] > 0) |
26 |
|
|
27 |
|
extern int g_pstcache_fd[]; |
28 |
|
|
29 |
|
uint32 g_stamp; |
30 |
|
int g_num_bitmaps_in_memory[3]; |
31 |
|
|
32 |
|
|
33 |
/* BITMAP CACHE */ |
/* BITMAP CACHE */ |
34 |
static HBITMAP bmpcache[3][600]; |
static BMPCACHEENTRY g_bmpcache[3][0xa00]; |
35 |
|
|
36 |
|
/* Remove the least-recently used bitmap from the cache */ |
37 |
|
void |
38 |
|
cache_remove_lru_bitmap(uint8 cache_id) |
39 |
|
{ |
40 |
|
int i; |
41 |
|
uint16 cache_idx = 0; |
42 |
|
uint32 m = (uint32)-1; |
43 |
|
BMPCACHEENTRY *pbce; |
44 |
|
|
45 |
|
for (i = 0; i < NUM_ELEMENTS(g_bmpcache[cache_id]); i++) |
46 |
|
{ |
47 |
|
if (g_bmpcache[cache_id][i].bitmap && g_bmpcache[cache_id][i].usage < m) |
48 |
|
{ |
49 |
|
cache_idx = i; |
50 |
|
m = g_bmpcache[cache_id][i].usage; |
51 |
|
} |
52 |
|
} |
53 |
|
|
54 |
|
pbce = &g_bmpcache[cache_id][cache_idx]; |
55 |
|
ui_destroy_bitmap(pbce->bitmap); |
56 |
|
--g_num_bitmaps_in_memory[cache_id]; |
57 |
|
pbce->bitmap = 0; |
58 |
|
pbce->usage = 0; |
59 |
|
} |
60 |
|
|
61 |
/* Retrieve a bitmap from the cache */ |
/* Retrieve a bitmap from the cache */ |
62 |
HBITMAP |
HBITMAP |
63 |
cache_get_bitmap(uint8 cache_id, uint16 cache_idx) |
cache_get_bitmap(uint8 cache_id, uint16 cache_idx) |
64 |
{ |
{ |
65 |
HBITMAP bitmap; |
HBITMAP *pbitmap; |
66 |
|
|
67 |
if ((cache_id < NUM_ELEMENTS(bmpcache)) && (cache_idx < NUM_ELEMENTS(bmpcache[0]))) |
if ((cache_id < NUM_ELEMENTS(g_bmpcache)) && (cache_idx < NUM_ELEMENTS(g_bmpcache[0]))) |
68 |
{ |
{ |
69 |
bitmap = bmpcache[cache_id][cache_idx]; |
pbitmap = &g_bmpcache[cache_id][cache_idx].bitmap; |
70 |
if (bitmap != NULL) |
if ((*pbitmap != 0) || pstcache_load_bitmap(cache_id, cache_idx)) |
71 |
return bitmap; |
{ |
72 |
|
if (IS_PERSISTENT(cache_id)) |
73 |
|
TOUCH(cache_id, cache_idx); |
74 |
|
|
75 |
|
return *pbitmap; |
76 |
|
} |
77 |
} |
} |
78 |
|
|
79 |
error("get bitmap %d:%d\n", cache_id, cache_idx); |
error("get bitmap %d:%d\n", cache_id, cache_idx); |
82 |
|
|
83 |
/* Store a bitmap in the cache */ |
/* Store a bitmap in the cache */ |
84 |
void |
void |
85 |
cache_put_bitmap(uint8 cache_id, uint16 cache_idx, HBITMAP bitmap) |
cache_put_bitmap(uint8 cache_id, uint16 cache_idx, HBITMAP bitmap, uint32 stamp) |
86 |
{ |
{ |
87 |
HBITMAP old; |
HBITMAP old; |
88 |
|
|
89 |
if ((cache_id < NUM_ELEMENTS(bmpcache)) && (cache_idx < NUM_ELEMENTS(bmpcache[0]))) |
if ((cache_id < NUM_ELEMENTS(g_bmpcache)) && (cache_idx < NUM_ELEMENTS(g_bmpcache[0]))) |
90 |
{ |
{ |
91 |
old = bmpcache[cache_id][cache_idx]; |
old = g_bmpcache[cache_id][cache_idx].bitmap; |
92 |
if (old != NULL) |
if (old != NULL) |
93 |
|
{ |
94 |
ui_destroy_bitmap(old); |
ui_destroy_bitmap(old); |
95 |
|
} |
96 |
|
else |
97 |
|
{ |
98 |
|
if (++g_num_bitmaps_in_memory[cache_id] > BMPCACHE2_C2_CELLS) |
99 |
|
cache_remove_lru_bitmap(cache_id); |
100 |
|
} |
101 |
|
|
102 |
bmpcache[cache_id][cache_idx] = bitmap; |
g_bmpcache[cache_id][cache_idx].bitmap = bitmap; |
103 |
|
g_bmpcache[cache_id][cache_idx].usage = stamp; |
104 |
} |
} |
105 |
else |
else |
106 |
{ |
{ |
108 |
} |
} |
109 |
} |
} |
110 |
|
|
111 |
|
/* Updates the persistent bitmap cache MRU information on exit */ |
112 |
|
void |
113 |
|
cache_save_state(void) |
114 |
|
{ |
115 |
|
int id, idx; |
116 |
|
|
117 |
|
for (id = 0; id < NUM_ELEMENTS(g_bmpcache); id++) |
118 |
|
if (IS_PERSISTENT(id)) |
119 |
|
for (idx = 0; idx < NUM_ELEMENTS(g_bmpcache[id]); idx++) |
120 |
|
pstcache_touch_bitmap(id, idx, g_bmpcache[id][idx].usage); |
121 |
|
} |
122 |
|
|
123 |
|
|
124 |
/* FONT CACHE */ |
/* FONT CACHE */ |
125 |
static FONTGLYPH fontcache[12][256]; |
static FONTGLYPH g_fontcache[12][256]; |
126 |
|
|
127 |
/* Retrieve a glyph from the font cache */ |
/* Retrieve a glyph from the font cache */ |
128 |
FONTGLYPH * |
FONTGLYPH * |
130 |
{ |
{ |
131 |
FONTGLYPH *glyph; |
FONTGLYPH *glyph; |
132 |
|
|
133 |
if ((font < NUM_ELEMENTS(fontcache)) && (character < NUM_ELEMENTS(fontcache[0]))) |
if ((font < NUM_ELEMENTS(g_fontcache)) && (character < NUM_ELEMENTS(g_fontcache[0]))) |
134 |
{ |
{ |
135 |
glyph = &fontcache[font][character]; |
glyph = &g_fontcache[font][character]; |
136 |
if (glyph->pixmap != NULL) |
if (glyph->pixmap != NULL) |
137 |
return glyph; |
return glyph; |
138 |
} |
} |
148 |
{ |
{ |
149 |
FONTGLYPH *glyph; |
FONTGLYPH *glyph; |
150 |
|
|
151 |
if ((font < NUM_ELEMENTS(fontcache)) && (character < NUM_ELEMENTS(fontcache[0]))) |
if ((font < NUM_ELEMENTS(g_fontcache)) && (character < NUM_ELEMENTS(g_fontcache[0]))) |
152 |
{ |
{ |
153 |
glyph = &fontcache[font][character]; |
glyph = &g_fontcache[font][character]; |
154 |
if (glyph->pixmap != NULL) |
if (glyph->pixmap != NULL) |
155 |
ui_destroy_glyph(glyph->pixmap); |
ui_destroy_glyph(glyph->pixmap); |
156 |
|
|
168 |
|
|
169 |
|
|
170 |
/* TEXT CACHE */ |
/* TEXT CACHE */ |
171 |
static DATABLOB textcache[256]; |
static DATABLOB g_textcache[256]; |
172 |
|
|
173 |
/* Retrieve a text item from the cache */ |
/* Retrieve a text item from the cache */ |
174 |
DATABLOB * |
DATABLOB * |
176 |
{ |
{ |
177 |
DATABLOB *text; |
DATABLOB *text; |
178 |
|
|
179 |
if (cache_id < NUM_ELEMENTS(textcache)) |
if (cache_id < NUM_ELEMENTS(g_textcache)) |
180 |
{ |
{ |
181 |
text = &textcache[cache_id]; |
text = &g_textcache[cache_id]; |
182 |
if (text->data != NULL) |
if (text->data != NULL) |
183 |
return text; |
return text; |
184 |
} |
} |
193 |
{ |
{ |
194 |
DATABLOB *text; |
DATABLOB *text; |
195 |
|
|
196 |
if (cache_id < NUM_ELEMENTS(textcache)) |
if (cache_id < NUM_ELEMENTS(g_textcache)) |
197 |
{ |
{ |
198 |
text = &textcache[cache_id]; |
text = &g_textcache[cache_id]; |
199 |
if (text->data != NULL) |
if (text->data != NULL) |
200 |
xfree(text->data); |
xfree(text->data); |
201 |
|
|
211 |
|
|
212 |
|
|
213 |
/* DESKTOP CACHE */ |
/* DESKTOP CACHE */ |
214 |
static uint8 deskcache[0x38400 * 4]; |
static uint8 g_deskcache[0x38400 * 4]; |
215 |
|
|
216 |
/* Retrieve desktop data from the cache */ |
/* Retrieve desktop data from the cache */ |
217 |
uint8 * |
uint8 * |
219 |
{ |
{ |
220 |
int length = cx * cy * bytes_per_pixel; |
int length = cx * cy * bytes_per_pixel; |
221 |
|
|
222 |
if ((offset + length) <= sizeof(deskcache)) |
if (offset > sizeof(g_deskcache)) |
223 |
|
offset = 0; |
224 |
|
|
225 |
|
if ((offset + length) <= sizeof(g_deskcache)) |
226 |
{ |
{ |
227 |
return &deskcache[offset]; |
return &g_deskcache[offset]; |
228 |
} |
} |
229 |
|
|
230 |
error("get desktop %d:%d\n", offset, length); |
error("get desktop %d:%d\n", offset, length); |
237 |
{ |
{ |
238 |
int length = cx * cy * bytes_per_pixel; |
int length = cx * cy * bytes_per_pixel; |
239 |
|
|
240 |
if ((offset + length) <= sizeof(deskcache)) |
if (offset > sizeof(g_deskcache)) |
241 |
|
offset = 0; |
242 |
|
|
243 |
|
if ((offset + length) <= sizeof(g_deskcache)) |
244 |
{ |
{ |
245 |
cx *= bytes_per_pixel; |
cx *= bytes_per_pixel; |
246 |
while (cy--) |
while (cy--) |
247 |
{ |
{ |
248 |
memcpy(&deskcache[offset], data, cx); |
memcpy(&g_deskcache[offset], data, cx); |
249 |
data += scanline; |
data += scanline; |
250 |
offset += cx; |
offset += cx; |
251 |
} |
} |
258 |
|
|
259 |
|
|
260 |
/* CURSOR CACHE */ |
/* CURSOR CACHE */ |
261 |
static HCURSOR cursorcache[0x20]; |
static HCURSOR g_cursorcache[0x20]; |
262 |
|
|
263 |
/* Retrieve cursor from cache */ |
/* Retrieve cursor from cache */ |
264 |
HCURSOR |
HCURSOR |
266 |
{ |
{ |
267 |
HCURSOR cursor; |
HCURSOR cursor; |
268 |
|
|
269 |
if (cache_idx < NUM_ELEMENTS(cursorcache)) |
if (cache_idx < NUM_ELEMENTS(g_cursorcache)) |
270 |
{ |
{ |
271 |
cursor = cursorcache[cache_idx]; |
cursor = g_cursorcache[cache_idx]; |
272 |
if (cursor != NULL) |
if (cursor != NULL) |
273 |
return cursor; |
return cursor; |
274 |
} |
} |
283 |
{ |
{ |
284 |
HCURSOR old; |
HCURSOR old; |
285 |
|
|
286 |
if (cache_idx < NUM_ELEMENTS(cursorcache)) |
if (cache_idx < NUM_ELEMENTS(g_cursorcache)) |
287 |
{ |
{ |
288 |
old = cursorcache[cache_idx]; |
old = g_cursorcache[cache_idx]; |
289 |
if (old != NULL) |
if (old != NULL) |
290 |
ui_destroy_cursor(old); |
ui_destroy_cursor(old); |
291 |
|
|
292 |
cursorcache[cache_idx] = cursor; |
g_cursorcache[cache_idx] = cursor; |
293 |
} |
} |
294 |
else |
else |
295 |
{ |
{ |