/[rdesktop]/sourceforge.net/trunk/rdesktop/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

Diff of /sourceforge.net/trunk/rdesktop/cache.c

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

revision 28 by matty, Wed Jun 20 13:54:48 2001 UTC revision 775 by jsorg71, Sat Oct 2 01:01:57 2004 UTC
# Line 1  Line 1 
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-2000     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
8     the Free Software Foundation; either version 2 of the License, or     the Free Software Foundation; either version 2 of the License, or
9     (at your option) any later version.     (at your option) any later version.
10      
11     This program is distributed in the hope that it will be useful,     This program is distributed in the hope that it will be useful,
12     but WITHOUT ANY WARRANTY; without even the implied warranty of     but WITHOUT ANY WARRANTY; without even the implied warranty of
13     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14     GNU General Public License for more details.     GNU General Public License for more details.
15      
16     You should have received a copy of the GNU General Public License     You should have received a copy of the GNU General Public License
17     along with this program; if not, write to the Free Software     along with this program; if not, write to the Free Software
18     Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.     Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
# Line 21  Line 21 
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    extern BOOL g_use_rdp5;
29    
30    uint32 g_stamp;
31    
32    static int g_num_bitmaps_in_memory[3];
33    
34  /* BITMAP CACHE */  /* BITMAP CACHE */
35  static HBITMAP bmpcache[3][600];  static BMPCACHEENTRY g_bmpcache[3][0xa00];
36    static HBITMAP g_volatile_bc[3];
37    
38    /* Remove the least-recently used bitmap from the cache */
39    void
40    cache_remove_lru_bitmap(uint8 cache_id)
41    {
42            int i;
43            uint16 cache_idx = 0;
44            uint32 m = 0xffffffff;
45            BMPCACHEENTRY *pbce;
46    
47            for (i = 0; i < NUM_ELEMENTS(g_bmpcache[cache_id]); i++)
48            {
49                    if (g_bmpcache[cache_id][i].bitmap && g_bmpcache[cache_id][i].usage < m)
50                    {
51                            cache_idx = i;
52                            m = g_bmpcache[cache_id][i].usage;
53                    }
54            }
55    
56            pbce = &g_bmpcache[cache_id][cache_idx];
57            ui_destroy_bitmap(pbce->bitmap);
58            --g_num_bitmaps_in_memory[cache_id];
59            pbce->bitmap = 0;
60            pbce->usage = 0;
61    }
62    
63  /* Retrieve a bitmap from the cache */  /* Retrieve a bitmap from the cache */
64  HBITMAP  HBITMAP
65  cache_get_bitmap(uint8 cache_id, uint16 cache_idx)  cache_get_bitmap(uint8 cache_id, uint16 cache_idx)
66  {  {
67          HBITMAP bitmap;          HBITMAP *pbitmap;
68    
69          if ((cache_id < NUM_ELEMENTS(bmpcache))          if ((cache_id < NUM_ELEMENTS(g_bmpcache)) && (cache_idx < NUM_ELEMENTS(g_bmpcache[0])))
             && (cache_idx < NUM_ELEMENTS(bmpcache[0])))  
70          {          {
71                  bitmap = bmpcache[cache_id][cache_idx];                  pbitmap = &g_bmpcache[cache_id][cache_idx].bitmap;
72                  if (bitmap != NULL)                  if ((*pbitmap != 0) || pstcache_load_bitmap(cache_id, cache_idx))
73                          return bitmap;                  {
74                            if (IS_PERSISTENT(cache_id))
75                                    TOUCH(cache_id, cache_idx);
76    
77                            return *pbitmap;
78                    }
79            }
80            else if ((cache_id < NUM_ELEMENTS(g_volatile_bc)) && (cache_idx == 0x7fff))
81            {
82                    return g_volatile_bc[cache_id];
83          }          }
84    
85          ERROR("get bitmap %d:%d\n", cache_id, cache_idx);          error("get bitmap %d:%d\n", cache_id, cache_idx);
86          return NULL;          return NULL;
87  }  }
88    
89  /* Store a bitmap in the cache */  /* Store a bitmap in the cache */
90  void  void
91  cache_put_bitmap(uint8 cache_id, uint16 cache_idx, HBITMAP bitmap)  cache_put_bitmap(uint8 cache_id, uint16 cache_idx, HBITMAP bitmap, uint32 stamp)
92  {  {
93          HBITMAP old;          HBITMAP old;
94    
95          if ((cache_id < NUM_ELEMENTS(bmpcache))          if ((cache_id < NUM_ELEMENTS(g_bmpcache)) && (cache_idx < NUM_ELEMENTS(g_bmpcache[0])))
             && (cache_idx < NUM_ELEMENTS(bmpcache[0])))  
96          {          {
97                  old = bmpcache[cache_id][cache_idx];                  old = g_bmpcache[cache_id][cache_idx].bitmap;
98                  if (old != NULL)                  if (old != NULL)
99                    {
100                          ui_destroy_bitmap(old);                          ui_destroy_bitmap(old);
101                    }
102                    else if (g_use_rdp5)
103                    {
104                            if (++g_num_bitmaps_in_memory[cache_id] > BMPCACHE2_C2_CELLS)
105                                    cache_remove_lru_bitmap(cache_id);
106                    }
107    
108                  bmpcache[cache_id][cache_idx] = bitmap;                  g_bmpcache[cache_id][cache_idx].bitmap = bitmap;
109                    g_bmpcache[cache_id][cache_idx].usage = stamp;
110            }
111            else if ((cache_id < NUM_ELEMENTS(g_volatile_bc)) && (cache_idx == 0x7fff))
112            {
113                    old = g_volatile_bc[cache_id];
114                    if (old != NULL)
115                            ui_destroy_bitmap(old);
116                    g_volatile_bc[cache_id] = bitmap;
117          }          }
118          else          else
119          {          {
120                  ERROR("put bitmap %d:%d\n", cache_id, cache_idx);                  error("put bitmap %d:%d\n", cache_id, cache_idx);
121          }          }
122  }  }
123    
124    /* Updates the persistent bitmap cache MRU information on exit */
125    void
126    cache_save_state(void)
127    {
128            int id, idx;
129    
130            for (id = 0; id < NUM_ELEMENTS(g_bmpcache); id++)
131                    if (IS_PERSISTENT(id))
132                            for (idx = 0; idx < NUM_ELEMENTS(g_bmpcache[id]); idx++)
133                                    pstcache_touch_bitmap(id, idx, g_bmpcache[id][idx].usage);
134    }
135    
136    
137  /* FONT CACHE */  /* FONT CACHE */
138  static FONTGLYPH fontcache[12][256];  static FONTGLYPH g_fontcache[12][256];
139    
140  /* Retrieve a glyph from the font cache */  /* Retrieve a glyph from the font cache */
141  FONTGLYPH *  FONTGLYPH *
# Line 75  cache_get_font(uint8 font, uint16 charac Line 143  cache_get_font(uint8 font, uint16 charac
143  {  {
144          FONTGLYPH *glyph;          FONTGLYPH *glyph;
145    
146          if ((font < NUM_ELEMENTS(fontcache))          if ((font < NUM_ELEMENTS(g_fontcache)) && (character < NUM_ELEMENTS(g_fontcache[0])))
             && (character < NUM_ELEMENTS(fontcache[0])))  
147          {          {
148                  glyph = &fontcache[font][character];                  glyph = &g_fontcache[font][character];
149                  if (glyph->pixmap != NULL)                  if (glyph->pixmap != NULL)
150                          return glyph;                          return glyph;
151          }          }
152    
153          ERROR("get font %d:%d\n", font, character);          error("get font %d:%d\n", font, character);
154          return NULL;          return NULL;
155  }  }
156    
# Line 94  cache_put_font(uint8 font, uint16 charac Line 161  cache_put_font(uint8 font, uint16 charac
161  {  {
162          FONTGLYPH *glyph;          FONTGLYPH *glyph;
163    
164          if ((font < NUM_ELEMENTS(fontcache))          if ((font < NUM_ELEMENTS(g_fontcache)) && (character < NUM_ELEMENTS(g_fontcache[0])))
             && (character < NUM_ELEMENTS(fontcache[0])))  
165          {          {
166                  glyph = &fontcache[font][character];                  glyph = &g_fontcache[font][character];
167                  if (glyph->pixmap != NULL)                  if (glyph->pixmap != NULL)
168                          ui_destroy_glyph(glyph->pixmap);                          ui_destroy_glyph(glyph->pixmap);
169    
# Line 109  cache_put_font(uint8 font, uint16 charac Line 175  cache_put_font(uint8 font, uint16 charac
175          }          }
176          else          else
177          {          {
178                  ERROR("put font %d:%d\n", font, character);                  error("put font %d:%d\n", font, character);
179          }          }
180  }  }
181    
182    
183  /* TEXT CACHE */  /* TEXT CACHE */
184  static DATABLOB textcache[256];  static DATABLOB g_textcache[256];
185    
186  /* Retrieve a text item from the cache */  /* Retrieve a text item from the cache */
187  DATABLOB *  DATABLOB *
# Line 123  cache_get_text(uint8 cache_id) Line 189  cache_get_text(uint8 cache_id)
189  {  {
190          DATABLOB *text;          DATABLOB *text;
191    
192          if (cache_id < NUM_ELEMENTS(textcache))          if (cache_id < NUM_ELEMENTS(g_textcache))
193          {          {
194                  text = &textcache[cache_id];                  text = &g_textcache[cache_id];
195                  if (text->data != NULL)                  if (text->data != NULL)
196                          return text;                          return text;
197          }          }
198    
199          ERROR("get text %d\n", cache_id);          error("get text %d\n", cache_id);
200          return NULL;          return NULL;
201  }  }
202    
# Line 140  cache_put_text(uint8 cache_id, void *dat Line 206  cache_put_text(uint8 cache_id, void *dat
206  {  {
207          DATABLOB *text;          DATABLOB *text;
208    
209          if (cache_id < NUM_ELEMENTS(textcache))          if (cache_id < NUM_ELEMENTS(g_textcache))
210          {          {
211                  text = &textcache[cache_id];                  text = &g_textcache[cache_id];
212                  if (text->data != NULL)                  if (text->data != NULL)
213                          xfree(text->data);                          xfree(text->data);
214    
# Line 152  cache_put_text(uint8 cache_id, void *dat Line 218  cache_put_text(uint8 cache_id, void *dat
218          }          }
219          else          else
220          {          {
221                  ERROR("put text %d\n", cache_id);                  error("put text %d\n", cache_id);
222          }          }
223  }  }
224    
225    
226  /* DESKTOP CACHE */  /* DESKTOP CACHE */
227  static uint8 deskcache[0x38400 * 4];  static uint8 g_deskcache[0x38400 * 4];
228    
229  /* Retrieve desktop data from the cache */  /* Retrieve desktop data from the cache */
230  uint8 *  uint8 *
# Line 166  cache_get_desktop(uint32 offset, int cx, Line 232  cache_get_desktop(uint32 offset, int cx,
232  {  {
233          int length = cx * cy * bytes_per_pixel;          int length = cx * cy * bytes_per_pixel;
234    
235          if ((offset + length) <= sizeof(deskcache))          if (offset > sizeof(g_deskcache))
236                    offset = 0;
237    
238            if ((offset + length) <= sizeof(g_deskcache))
239          {          {
240                  return &deskcache[offset];                  return &g_deskcache[offset];
241          }          }
242    
243          ERROR("get desktop %d:%d\n", offset, length);          error("get desktop %d:%d\n", offset, length);
244          return NULL;          return NULL;
245  }  }
246    
247  /* Store desktop data in the cache */  /* Store desktop data in the cache */
248  void  void
249  cache_put_desktop(uint32 offset, int cx, int cy, int scanline,  cache_put_desktop(uint32 offset, int cx, int cy, int scanline, int bytes_per_pixel, uint8 * data)
                   int bytes_per_pixel, uint8 *data)  
250  {  {
251          int length = cx * cy * bytes_per_pixel;          int length = cx * cy * bytes_per_pixel;
252    
253          if ((offset + length) <= sizeof(deskcache))          if (offset > sizeof(g_deskcache))
254                    offset = 0;
255    
256            if ((offset + length) <= sizeof(g_deskcache))
257          {          {
258                  cx *= bytes_per_pixel;                  cx *= bytes_per_pixel;
259                  while (cy--)                  while (cy--)
260                  {                  {
261                          memcpy(&deskcache[offset], data, cx);                          memcpy(&g_deskcache[offset], data, cx);
262                          data += scanline;                          data += scanline;
263                          offset += cx;                          offset += cx;
264                  }                  }
265          }          }
266          else          else
267          {          {
268                  ERROR("put desktop %d:%d\n", offset, length);                  error("put desktop %d:%d\n", offset, length);
269          }          }
270  }  }
271    
272    
273  /* CURSOR CACHE */  /* CURSOR CACHE */
274  static HCURSOR cursorcache[0x20];  static HCURSOR g_cursorcache[0x20];
275    
276  /* Retrieve cursor from cache */  /* Retrieve cursor from cache */
277  HCURSOR cache_get_cursor(uint16 cache_idx)  HCURSOR
278    cache_get_cursor(uint16 cache_idx)
279  {  {
280          HCURSOR cursor;          HCURSOR cursor;
281    
282          if (cache_idx < NUM_ELEMENTS(cursorcache))          if (cache_idx < NUM_ELEMENTS(g_cursorcache))
283          {          {
284                  cursor = cursorcache[cache_idx];                  cursor = g_cursorcache[cache_idx];
285                  if (cursor != NULL)                  if (cursor != NULL)
286                          return cursor;                          return cursor;
287          }          }
288    
289          ERROR("get cursor %d\n", cache_idx);          error("get cursor %d\n", cache_idx);
290          return NULL;          return NULL;
291  }  }
292    
# Line 224  cache_put_cursor(uint16 cache_idx, HCURS Line 296  cache_put_cursor(uint16 cache_idx, HCURS
296  {  {
297          HCURSOR old;          HCURSOR old;
298    
299          if (cache_idx < NUM_ELEMENTS(cursorcache))          if (cache_idx < NUM_ELEMENTS(g_cursorcache))
300          {          {
301                  old = cursorcache[cache_idx];                  old = g_cursorcache[cache_idx];
302                  if (old != NULL)                  if (old != NULL)
303                          ui_destroy_cursor(old);                          ui_destroy_cursor(old);
304    
305                  cursorcache[cache_idx] = cursor;                  g_cursorcache[cache_idx] = cursor;
306          }          }
307          else          else
308          {          {
309                  ERROR("put cursor %d\n", cache_idx);                  error("put cursor %d\n", cache_idx);
310          }          }
311  }  }

Legend:
Removed from v.28  
changed lines
  Added in v.775

  ViewVC Help
Powered by ViewVC 1.1.26