/[rdesktop]/jpeg/rdesktop/trunk/pstcache.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/pstcache.c

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1507 - (hide annotations)
Mon Jul 20 16:45:11 2009 UTC (14 years, 10 months ago) by dpavlin
File MIME type: text/plain
File size: 5384 byte(s)
branch for integration of Daniel Jarboe <daniel.jarboe(at)gmail.com>
patches for jpeg
1 astrand 963 /* -*- c-basic-offset: 8 -*-
2 jsorg71 725 rdesktop: A Remote Desktop Protocol client.
3     Persistent Bitmap Cache routines
4 jsorg71 1475 Copyright (C) Jeroen Meijer 2004-2008
5 jsorg71 725
6     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
8     the Free Software Foundation; either version 2 of the License, or
9     (at your option) any later version.
10    
11     This program is distributed in the hope that it will be useful,
12     but WITHOUT ANY WARRANTY; without even the implied warranty of
13     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14     GNU General Public License for more details.
15    
16     You should have received a copy of the GNU General Public License
17     along with this program; if not, write to the Free Software
18     Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
19     */
20    
21     #include "rdesktop.h"
22    
23 astrand 738 #define MAX_CELL_SIZE 0x1000 /* pixels */
24 jsorg71 725
25 jdmeijer 808 #define IS_PERSISTENT(id) (id < 8 && g_pstcache_fd[id] > 0)
26 jsorg71 725
27 astrand 1042 extern int g_server_depth;
28 jsorg71 1372 extern RD_BOOL g_bitmap_cache;
29     extern RD_BOOL g_bitmap_cache_persist_enable;
30     extern RD_BOOL g_bitmap_cache_precache;
31 jsorg71 725
32     int g_pstcache_fd[8];
33     int g_pstcache_Bpp;
34 jsorg71 1372 RD_BOOL g_pstcache_enumerated = False;
35 jdmeijer 830 uint8 zero_key[] = { 0, 0, 0, 0, 0, 0, 0, 0 };
36 jsorg71 725
37    
38 jdmeijer 830 /* Update mru stamp/index for a bitmap */
39 jsorg71 725 void
40     pstcache_touch_bitmap(uint8 cache_id, uint16 cache_idx, uint32 stamp)
41     {
42     int fd;
43    
44 jdmeijer 808 if (!IS_PERSISTENT(cache_id) || cache_idx >= BMPCACHE2_NUM_PSTCELLS)
45 jsorg71 725 return;
46    
47     fd = g_pstcache_fd[cache_id];
48     rd_lseek_file(fd, 12 + cache_idx * (g_pstcache_Bpp * MAX_CELL_SIZE + sizeof(CELLHEADER)));
49     rd_write_file(fd, &stamp, sizeof(stamp));
50     }
51    
52     /* Load a bitmap from the persistent cache */
53 jsorg71 1372 RD_BOOL
54 jsorg71 725 pstcache_load_bitmap(uint8 cache_id, uint16 cache_idx)
55     {
56     uint8 *celldata;
57     int fd;
58     CELLHEADER cellhdr;
59 jsorg71 1364 RD_HBITMAP bitmap;
60 jsorg71 725
61 jdmeijer 808 if (!g_bitmap_cache_persist_enable)
62 jsorg71 725 return False;
63    
64 jdmeijer 808 if (!IS_PERSISTENT(cache_id) || cache_idx >= BMPCACHE2_NUM_PSTCELLS)
65     return False;
66    
67 jsorg71 725 fd = g_pstcache_fd[cache_id];
68     rd_lseek_file(fd, cache_idx * (g_pstcache_Bpp * MAX_CELL_SIZE + sizeof(CELLHEADER)));
69     rd_read_file(fd, &cellhdr, sizeof(CELLHEADER));
70 astrand 738 celldata = (uint8 *) xmalloc(cellhdr.length);
71 jsorg71 725 rd_read_file(fd, celldata, cellhdr.length);
72    
73     bitmap = ui_create_bitmap(cellhdr.width, cellhdr.height, celldata);
74 astrand 1478 DEBUG(("Load bitmap from disk: id=%d, idx=%d, bmp=0x%x)\n", cache_id, cache_idx,
75     (unsigned int) bitmap));
76 jdmeijer 830 cache_put_bitmap(cache_id, cache_idx, bitmap);
77 jsorg71 725
78     xfree(celldata);
79     return True;
80     }
81    
82     /* Store a bitmap in the persistent cache */
83 jsorg71 1372 RD_BOOL
84 jdmeijer 830 pstcache_save_bitmap(uint8 cache_id, uint16 cache_idx, uint8 * key,
85 jsorg71 1221 uint8 width, uint8 height, uint16 length, uint8 * data)
86 jsorg71 725 {
87     int fd;
88     CELLHEADER cellhdr;
89    
90 jdmeijer 808 if (!IS_PERSISTENT(cache_id) || cache_idx >= BMPCACHE2_NUM_PSTCELLS)
91 jsorg71 725 return False;
92    
93 jdmeijer 830 memcpy(cellhdr.key, key, sizeof(HASH_KEY));
94 jsorg71 725 cellhdr.width = width;
95     cellhdr.height = height;
96     cellhdr.length = length;
97     cellhdr.stamp = 0;
98    
99     fd = g_pstcache_fd[cache_id];
100     rd_lseek_file(fd, cache_idx * (g_pstcache_Bpp * MAX_CELL_SIZE + sizeof(CELLHEADER)));
101     rd_write_file(fd, &cellhdr, sizeof(CELLHEADER));
102     rd_write_file(fd, data, length);
103    
104     return True;
105     }
106    
107 jdmeijer 830 /* List the bitmap keys from the persistent cache file */
108 jsorg71 725 int
109 jdmeijer 830 pstcache_enumerate(uint8 id, HASH_KEY * keylist)
110 jsorg71 725 {
111 jsorg71 1222 int fd, n;
112     uint16 idx;
113 jdmeijer 830 sint16 mru_idx[0xa00];
114     uint32 mru_stamp[0xa00];
115 jsorg71 725 CELLHEADER cellhdr;
116    
117 jdmeijer 830 if (!(g_bitmap_cache && g_bitmap_cache_persist_enable && IS_PERSISTENT(id)))
118 jsorg71 725 return 0;
119    
120     /* The server disconnects if the bitmap cache content is sent more than once */
121     if (g_pstcache_enumerated)
122     return 0;
123    
124 jdmeijer 830 DEBUG_RDP5(("Persistent bitmap cache enumeration... "));
125     for (idx = 0; idx < BMPCACHE2_NUM_PSTCELLS; idx++)
126 jsorg71 725 {
127 jdmeijer 830 fd = g_pstcache_fd[id];
128     rd_lseek_file(fd, idx * (g_pstcache_Bpp * MAX_CELL_SIZE + sizeof(CELLHEADER)));
129 jsorg71 725 if (rd_read_file(fd, &cellhdr, sizeof(CELLHEADER)) <= 0)
130     break;
131    
132 jdmeijer 830 if (memcmp(cellhdr.key, zero_key, sizeof(HASH_KEY)) != 0)
133 jsorg71 725 {
134 jdmeijer 830 memcpy(keylist[idx], cellhdr.key, sizeof(HASH_KEY));
135 jsorg71 725
136 astrand 1042 /* 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 jdmeijer 830 pstcache_load_bitmap(id, idx);
139    
140     /* Sort by stamp */
141     for (n = idx; n > 0 && cellhdr.stamp < mru_stamp[n - 1]; n--)
142 jsorg71 725 {
143 jdmeijer 830 mru_idx[n] = mru_idx[n - 1];
144     mru_stamp[n] = mru_stamp[n - 1];
145     }
146 jsorg71 725
147 jdmeijer 830 mru_idx[n] = idx;
148     mru_stamp[n] = cellhdr.stamp;
149 jsorg71 725 }
150     else
151     {
152     break;
153     }
154     }
155    
156 jdmeijer 830 DEBUG_RDP5(("%d cached bitmaps.\n", idx));
157    
158     cache_rebuild_bmpcache_linked_list(id, mru_idx, idx);
159 jsorg71 725 g_pstcache_enumerated = True;
160 jdmeijer 830 return idx;
161 jsorg71 725 }
162    
163     /* initialise the persistent bitmap cache */
164 jsorg71 1372 RD_BOOL
165 jsorg71 725 pstcache_init(uint8 cache_id)
166     {
167     int fd;
168     char filename[256];
169    
170     if (g_pstcache_enumerated)
171     return True;
172    
173     g_pstcache_fd[cache_id] = 0;
174    
175     if (!(g_bitmap_cache && g_bitmap_cache_persist_enable))
176     return False;
177    
178     if (!rd_pstcache_mkdir())
179     {
180     DEBUG(("failed to get/make cache directory!\n"));
181     return False;
182     }
183    
184 astrand 1042 g_pstcache_Bpp = (g_server_depth + 7) / 8;
185 jsorg71 725 sprintf(filename, "cache/pstcache_%d_%d", cache_id, g_pstcache_Bpp);
186     DEBUG(("persistent bitmap cache file: %s\n", filename));
187    
188     fd = rd_open_file(filename);
189     if (fd == -1)
190     return False;
191    
192     if (!rd_lock_file(fd, 0, 0))
193     {
194     warning("Persistent bitmap caching is disabled. (The file is already in use)\n");
195     rd_close_file(fd);
196     return False;
197     }
198    
199     g_pstcache_fd[cache_id] = fd;
200     return True;
201     }

  ViewVC Help
Powered by ViewVC 1.1.26