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 g_bmpcache[3][600]; |
static BMPCACHEENTRY g_bmpcache[3][0xa00]; |
35 |
|
static HBITMAP g_volatile_bc[3]; |
36 |
|
|
37 |
|
/* Remove the least-recently used bitmap from the cache */ |
38 |
|
void |
39 |
|
cache_remove_lru_bitmap(uint8 cache_id) |
40 |
|
{ |
41 |
|
int i; |
42 |
|
uint16 cache_idx = 0; |
43 |
|
uint32 m = (uint32)-1; |
44 |
|
BMPCACHEENTRY *pbce; |
45 |
|
|
46 |
|
for (i = 0; i < NUM_ELEMENTS(g_bmpcache[cache_id]); i++) |
47 |
|
{ |
48 |
|
if (g_bmpcache[cache_id][i].bitmap && g_bmpcache[cache_id][i].usage < m) |
49 |
|
{ |
50 |
|
cache_idx = i; |
51 |
|
m = g_bmpcache[cache_id][i].usage; |
52 |
|
} |
53 |
|
} |
54 |
|
|
55 |
|
pbce = &g_bmpcache[cache_id][cache_idx]; |
56 |
|
ui_destroy_bitmap(pbce->bitmap); |
57 |
|
--g_num_bitmaps_in_memory[cache_id]; |
58 |
|
pbce->bitmap = 0; |
59 |
|
pbce->usage = 0; |
60 |
|
} |
61 |
|
|
62 |
/* Retrieve a bitmap from the cache */ |
/* Retrieve a bitmap from the cache */ |
63 |
HBITMAP |
HBITMAP |
64 |
cache_get_bitmap(uint8 cache_id, uint16 cache_idx) |
cache_get_bitmap(uint8 cache_id, uint16 cache_idx) |
65 |
{ |
{ |
66 |
HBITMAP bitmap; |
HBITMAP *pbitmap; |
67 |
|
|
68 |
if ((cache_id < NUM_ELEMENTS(g_bmpcache)) && (cache_idx < NUM_ELEMENTS(g_bmpcache[0]))) |
if ((cache_id < NUM_ELEMENTS(g_bmpcache)) && (cache_idx < NUM_ELEMENTS(g_bmpcache[0]))) |
69 |
{ |
{ |
70 |
bitmap = g_bmpcache[cache_id][cache_idx]; |
pbitmap = &g_bmpcache[cache_id][cache_idx].bitmap; |
71 |
if (bitmap != NULL) |
if ((*pbitmap != 0) || pstcache_load_bitmap(cache_id, cache_idx)) |
72 |
return bitmap; |
{ |
73 |
|
if (IS_PERSISTENT(cache_id)) |
74 |
|
TOUCH(cache_id, cache_idx); |
75 |
|
|
76 |
|
return *pbitmap; |
77 |
|
} |
78 |
|
} |
79 |
|
else if ((cache_id < NUM_ELEMENTS(g_volatile_bc)) && (cache_idx == 0x7fff)) |
80 |
|
{ |
81 |
|
return g_volatile_bc[cache_id]; |
82 |
} |
} |
83 |
|
|
84 |
error("get bitmap %d:%d\n", cache_id, cache_idx); |
error("get bitmap %d:%d\n", cache_id, cache_idx); |
87 |
|
|
88 |
/* Store a bitmap in the cache */ |
/* Store a bitmap in the cache */ |
89 |
void |
void |
90 |
cache_put_bitmap(uint8 cache_id, uint16 cache_idx, HBITMAP bitmap) |
cache_put_bitmap(uint8 cache_id, uint16 cache_idx, HBITMAP bitmap, uint32 stamp) |
91 |
{ |
{ |
92 |
HBITMAP old; |
HBITMAP old; |
93 |
|
|
94 |
if ((cache_id < NUM_ELEMENTS(g_bmpcache)) && (cache_idx < NUM_ELEMENTS(g_bmpcache[0]))) |
if ((cache_id < NUM_ELEMENTS(g_bmpcache)) && (cache_idx < NUM_ELEMENTS(g_bmpcache[0]))) |
95 |
{ |
{ |
96 |
old = g_bmpcache[cache_id][cache_idx]; |
old = g_bmpcache[cache_id][cache_idx].bitmap; |
97 |
if (old != NULL) |
if (old != NULL) |
98 |
|
{ |
99 |
ui_destroy_bitmap(old); |
ui_destroy_bitmap(old); |
100 |
|
} |
101 |
|
else |
102 |
|
{ |
103 |
|
if (++g_num_bitmaps_in_memory[cache_id] > BMPCACHE2_C2_CELLS) |
104 |
|
cache_remove_lru_bitmap(cache_id); |
105 |
|
} |
106 |
|
|
107 |
g_bmpcache[cache_id][cache_idx] = bitmap; |
g_bmpcache[cache_id][cache_idx].bitmap = bitmap; |
108 |
|
g_bmpcache[cache_id][cache_idx].usage = stamp; |
109 |
|
} |
110 |
|
else if ((cache_id < NUM_ELEMENTS(g_volatile_bc)) && (cache_idx == 0x7fff)) |
111 |
|
{ |
112 |
|
old = g_volatile_bc[cache_id]; |
113 |
|
if (old != NULL) |
114 |
|
ui_destroy_bitmap(old); |
115 |
|
g_volatile_bc[cache_id] = bitmap; |
116 |
} |
} |
117 |
else |
else |
118 |
{ |
{ |
120 |
} |
} |
121 |
} |
} |
122 |
|
|
123 |
|
/* Updates the persistent bitmap cache MRU information on exit */ |
124 |
|
void |
125 |
|
cache_save_state(void) |
126 |
|
{ |
127 |
|
int id, idx; |
128 |
|
|
129 |
|
for (id = 0; id < NUM_ELEMENTS(g_bmpcache); id++) |
130 |
|
if (IS_PERSISTENT(id)) |
131 |
|
for (idx = 0; idx < NUM_ELEMENTS(g_bmpcache[id]); idx++) |
132 |
|
pstcache_touch_bitmap(id, idx, g_bmpcache[id][idx].usage); |
133 |
|
} |
134 |
|
|
135 |
|
|
136 |
/* FONT CACHE */ |
/* FONT CACHE */ |
137 |
static FONTGLYPH g_fontcache[12][256]; |
static FONTGLYPH g_fontcache[12][256]; |
231 |
{ |
{ |
232 |
int length = cx * cy * bytes_per_pixel; |
int length = cx * cy * bytes_per_pixel; |
233 |
|
|
234 |
|
if (offset > sizeof(g_deskcache)) |
235 |
|
offset = 0; |
236 |
|
|
237 |
if ((offset + length) <= sizeof(g_deskcache)) |
if ((offset + length) <= sizeof(g_deskcache)) |
238 |
{ |
{ |
239 |
return &g_deskcache[offset]; |
return &g_deskcache[offset]; |
249 |
{ |
{ |
250 |
int length = cx * cy * bytes_per_pixel; |
int length = cx * cy * bytes_per_pixel; |
251 |
|
|
252 |
|
if (offset > sizeof(g_deskcache)) |
253 |
|
offset = 0; |
254 |
|
|
255 |
if ((offset + length) <= sizeof(g_deskcache)) |
if ((offset + length) <= sizeof(g_deskcache)) |
256 |
{ |
{ |
257 |
cx *= bytes_per_pixel; |
cx *= bytes_per_pixel; |