1 |
/* |
/* -*- c-basic-offset: 8 -*- |
2 |
rdesktop: A Remote Desktop Protocol client. |
rdesktop: A Remote Desktop Protocol client. |
3 |
Persistent Bitmap Cache routines |
Persistent Bitmap Cache routines |
4 |
Copyright (C) Jeroen Meijer 2004 |
Copyright (C) Jeroen Meijer 2004-2008 |
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 |
20 |
|
|
21 |
#include "rdesktop.h" |
#include "rdesktop.h" |
22 |
|
|
23 |
#define MAX_CELL_SIZE 0x1000 /* pixels */ |
#define MAX_CELL_SIZE 0x1000 /* pixels */ |
24 |
|
|
25 |
#define IS_PERSISTENT(id) (g_pstcache_fd[id] > 0) |
#define IS_PERSISTENT(id) (id < 8 && g_pstcache_fd[id] > 0) |
26 |
|
|
27 |
extern int g_server_bpp; |
extern int g_server_depth; |
28 |
extern uint32 g_stamp; |
extern RD_BOOL g_bitmap_cache; |
29 |
extern BOOL g_bitmap_cache; |
extern RD_BOOL g_bitmap_cache_persist_enable; |
30 |
extern BOOL g_bitmap_cache_persist_enable; |
extern RD_BOOL g_bitmap_cache_precache; |
|
extern BOOL g_bitmap_cache_precache; |
|
31 |
|
|
32 |
int g_pstcache_fd[8]; |
int g_pstcache_fd[8]; |
33 |
int g_pstcache_Bpp; |
int g_pstcache_Bpp; |
34 |
BOOL g_pstcache_enumerated = False; |
RD_BOOL g_pstcache_enumerated = False; |
35 |
uint8 zero_id[] = {0, 0, 0, 0, 0, 0, 0, 0}; |
uint8 zero_key[] = { 0, 0, 0, 0, 0, 0, 0, 0 }; |
36 |
|
|
37 |
|
|
38 |
/* Update usage info for a bitmap */ |
/* Update mru stamp/index for a bitmap */ |
39 |
void |
void |
40 |
pstcache_touch_bitmap(uint8 cache_id, uint16 cache_idx, uint32 stamp) |
pstcache_touch_bitmap(uint8 cache_id, uint16 cache_idx, uint32 stamp) |
41 |
{ |
{ |
42 |
int fd; |
int fd; |
43 |
|
|
44 |
if (!IS_PERSISTENT(cache_id)) |
if (!IS_PERSISTENT(cache_id) || cache_idx >= BMPCACHE2_NUM_PSTCELLS) |
45 |
return; |
return; |
46 |
|
|
47 |
fd = g_pstcache_fd[cache_id]; |
fd = g_pstcache_fd[cache_id]; |
50 |
} |
} |
51 |
|
|
52 |
/* Load a bitmap from the persistent cache */ |
/* Load a bitmap from the persistent cache */ |
53 |
BOOL |
RD_BOOL |
54 |
pstcache_load_bitmap(uint8 cache_id, uint16 cache_idx) |
pstcache_load_bitmap(uint8 cache_id, uint16 cache_idx) |
55 |
{ |
{ |
56 |
uint8 *celldata; |
uint8 *celldata; |
57 |
int fd; |
int fd; |
58 |
CELLHEADER cellhdr; |
CELLHEADER cellhdr; |
59 |
HBITMAP bitmap; |
RD_HBITMAP bitmap; |
60 |
|
|
61 |
if (!(g_bitmap_cache_persist_enable && IS_PERSISTENT(cache_id))) |
if (!g_bitmap_cache_persist_enable) |
62 |
|
return False; |
63 |
|
|
64 |
|
if (!IS_PERSISTENT(cache_id) || cache_idx >= BMPCACHE2_NUM_PSTCELLS) |
65 |
return False; |
return False; |
66 |
|
|
67 |
fd = g_pstcache_fd[cache_id]; |
fd = g_pstcache_fd[cache_id]; |
68 |
rd_lseek_file(fd, cache_idx * (g_pstcache_Bpp * MAX_CELL_SIZE + sizeof(CELLHEADER))); |
rd_lseek_file(fd, cache_idx * (g_pstcache_Bpp * MAX_CELL_SIZE + sizeof(CELLHEADER))); |
69 |
rd_read_file(fd, &cellhdr, sizeof(CELLHEADER)); |
rd_read_file(fd, &cellhdr, sizeof(CELLHEADER)); |
70 |
celldata = (uint8 *)xmalloc(cellhdr.length); |
celldata = (uint8 *) xmalloc(cellhdr.length); |
71 |
rd_read_file(fd, celldata, cellhdr.length); |
rd_read_file(fd, celldata, cellhdr.length); |
72 |
|
|
|
DEBUG(("Loading bitmap from disk (%d:%d)\n", cache_id, cache_idx)); |
|
|
|
|
73 |
bitmap = ui_create_bitmap(cellhdr.width, cellhdr.height, celldata); |
bitmap = ui_create_bitmap(cellhdr.width, cellhdr.height, celldata); |
74 |
cache_put_bitmap(cache_id, cache_idx, bitmap, cellhdr.stamp); |
DEBUG(("Load bitmap from disk: id=%d, idx=%d, bmp=0x%x)\n", cache_id, cache_idx, |
75 |
|
(unsigned int) bitmap)); |
76 |
|
cache_put_bitmap(cache_id, cache_idx, bitmap); |
77 |
|
|
78 |
xfree(celldata); |
xfree(celldata); |
79 |
return True; |
return True; |
80 |
} |
} |
81 |
|
|
82 |
/* Store a bitmap in the persistent cache */ |
/* Store a bitmap in the persistent cache */ |
83 |
BOOL |
RD_BOOL |
84 |
pstcache_put_bitmap(uint8 cache_id, uint16 cache_idx, uint8 *bitmap_id, |
pstcache_save_bitmap(uint8 cache_id, uint16 cache_idx, uint8 * key, |
85 |
uint16 width, uint16 height, uint16 length, uint8 *data) |
uint8 width, uint8 height, uint16 length, uint8 * data) |
86 |
{ |
{ |
87 |
int fd; |
int fd; |
88 |
CELLHEADER cellhdr; |
CELLHEADER cellhdr; |
89 |
|
|
90 |
if (!IS_PERSISTENT(cache_id)) |
if (!IS_PERSISTENT(cache_id) || cache_idx >= BMPCACHE2_NUM_PSTCELLS) |
91 |
return False; |
return False; |
92 |
|
|
93 |
memcpy(cellhdr.bitmap_id, bitmap_id, sizeof(BITMAP_ID)); |
memcpy(cellhdr.key, key, sizeof(HASH_KEY)); |
94 |
cellhdr.width = width; |
cellhdr.width = width; |
95 |
cellhdr.height = height; |
cellhdr.height = height; |
96 |
cellhdr.length = length; |
cellhdr.length = length; |
104 |
return True; |
return True; |
105 |
} |
} |
106 |
|
|
107 |
/* list the bitmaps from the persistent cache file */ |
/* List the bitmap keys from the persistent cache file */ |
108 |
int |
int |
109 |
pstcache_enumerate(uint8 cache_id, uint8 *idlist) |
pstcache_enumerate(uint8 id, HASH_KEY * keylist) |
110 |
{ |
{ |
111 |
int fd, n, c = 0; |
int fd, n; |
112 |
|
uint16 idx; |
113 |
|
sint16 mru_idx[0xa00]; |
114 |
|
uint32 mru_stamp[0xa00]; |
115 |
CELLHEADER cellhdr; |
CELLHEADER cellhdr; |
116 |
|
|
117 |
if (!(g_bitmap_cache && g_bitmap_cache_persist_enable && IS_PERSISTENT(cache_id))) |
if (!(g_bitmap_cache && g_bitmap_cache_persist_enable && IS_PERSISTENT(id))) |
118 |
return 0; |
return 0; |
119 |
|
|
120 |
/* The server disconnects if the bitmap cache content is sent more than once */ |
/* The server disconnects if the bitmap cache content is sent more than once */ |
121 |
if (g_pstcache_enumerated) |
if (g_pstcache_enumerated) |
122 |
return 0; |
return 0; |
123 |
|
|
124 |
DEBUG(("pstcache enumeration... ")); |
DEBUG_RDP5(("Persistent bitmap cache enumeration... ")); |
125 |
for (n = 0; n < BMPCACHE2_NUM_PSTCELLS; n++) |
for (idx = 0; idx < BMPCACHE2_NUM_PSTCELLS; idx++) |
126 |
{ |
{ |
127 |
fd = g_pstcache_fd[cache_id]; |
fd = g_pstcache_fd[id]; |
128 |
rd_lseek_file(fd, n * (g_pstcache_Bpp * MAX_CELL_SIZE + sizeof(CELLHEADER))); |
rd_lseek_file(fd, idx * (g_pstcache_Bpp * MAX_CELL_SIZE + sizeof(CELLHEADER))); |
129 |
if (rd_read_file(fd, &cellhdr, sizeof(CELLHEADER)) <= 0) |
if (rd_read_file(fd, &cellhdr, sizeof(CELLHEADER)) <= 0) |
130 |
break; |
break; |
131 |
|
|
132 |
if (memcmp(cellhdr.bitmap_id, zero_id, sizeof(BITMAP_ID)) != 0) |
if (memcmp(cellhdr.key, zero_key, sizeof(HASH_KEY)) != 0) |
133 |
{ |
{ |
134 |
memcpy(idlist + n * sizeof(BITMAP_ID), cellhdr.bitmap_id, |
memcpy(keylist[idx], cellhdr.key, sizeof(HASH_KEY)); |
|
sizeof(BITMAP_ID)); |
|
135 |
|
|
136 |
if (cellhdr.stamp) |
/* Pre-cache (not possible for 8 bit colour depth cause it needs a colourmap) */ |
137 |
{ |
if (g_bitmap_cache_precache && cellhdr.stamp && g_server_depth > 8) |
138 |
/* Pre-caching is not possible with 8bpp because a colourmap |
pstcache_load_bitmap(id, idx); |
|
* is needed to load them */ |
|
|
if (g_bitmap_cache_precache && (g_server_bpp > 8)) |
|
|
{ |
|
|
if (pstcache_load_bitmap(cache_id, n)) |
|
|
c++; |
|
|
} |
|
139 |
|
|
140 |
g_stamp = MAX(g_stamp, cellhdr.stamp); |
/* Sort by stamp */ |
141 |
|
for (n = idx; n > 0 && cellhdr.stamp < mru_stamp[n - 1]; n--) |
142 |
|
{ |
143 |
|
mru_idx[n] = mru_idx[n - 1]; |
144 |
|
mru_stamp[n] = mru_stamp[n - 1]; |
145 |
} |
} |
146 |
|
|
147 |
|
mru_idx[n] = idx; |
148 |
|
mru_stamp[n] = cellhdr.stamp; |
149 |
} |
} |
150 |
else |
else |
151 |
{ |
{ |
153 |
} |
} |
154 |
} |
} |
155 |
|
|
156 |
DEBUG(("%d bitmaps in persistent cache, %d bitmaps loaded in memory\n", n, c)); |
DEBUG_RDP5(("%d cached bitmaps.\n", idx)); |
157 |
|
|
158 |
|
cache_rebuild_bmpcache_linked_list(id, mru_idx, idx); |
159 |
g_pstcache_enumerated = True; |
g_pstcache_enumerated = True; |
160 |
return n; |
return idx; |
161 |
} |
} |
162 |
|
|
163 |
/* initialise the persistent bitmap cache */ |
/* initialise the persistent bitmap cache */ |
164 |
BOOL |
RD_BOOL |
165 |
pstcache_init(uint8 cache_id) |
pstcache_init(uint8 cache_id) |
166 |
{ |
{ |
167 |
int fd; |
int fd; |
181 |
return False; |
return False; |
182 |
} |
} |
183 |
|
|
184 |
g_pstcache_Bpp = (g_server_bpp + 7) / 8; |
g_pstcache_Bpp = (g_server_depth + 7) / 8; |
185 |
sprintf(filename, "cache/pstcache_%d_%d", cache_id, g_pstcache_Bpp); |
sprintf(filename, "cache/pstcache_%d_%d", cache_id, g_pstcache_Bpp); |
186 |
DEBUG(("persistent bitmap cache file: %s\n", filename)); |
DEBUG(("persistent bitmap cache file: %s\n", filename)); |
187 |
|
|