/[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 10 by matty, Tue Aug 15 10:23:24 2000 UTC revision 738 by astrand, Mon Jul 12 21:07:39 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
# 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    
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    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 cache_get_bitmap(uint8 cache_id, uint16 cache_idx)  HBITMAP
64    cache_get_bitmap(uint8 cache_id, uint16 cache_idx)
65  {  {
66          HBITMAP bitmap;          HBITMAP *pbitmap;
67    
68          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])))  
69          {          {
70                  bitmap = 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);
85          return NULL;          return NULL;
86  }  }
87    
88  /* Store a bitmap in the cache */  /* Store a bitmap in the cache */
89  void cache_put_bitmap(uint8 cache_id, uint16 cache_idx, HBITMAP bitmap)  void
90    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(bmpcache))          if ((cache_id < NUM_ELEMENTS(g_bmpcache)) && (cache_idx < NUM_ELEMENTS(g_bmpcache[0])))
                         && (cache_idx < NUM_ELEMENTS(bmpcache[0])))  
95          {          {
96                  old = 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                  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          {          {
119                  ERROR("put bitmap %d:%d\n", cache_id, cache_idx);                  error("put bitmap %d:%d\n", cache_id, cache_idx);
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 fontcache[12][256];  static FONTGLYPH g_fontcache[12][256];
138    
139  /* Retrieve a glyph from the font cache */  /* Retrieve a glyph from the font cache */
140  FONTGLYPH *cache_get_font(uint8 font, uint16 character)  FONTGLYPH *
141    cache_get_font(uint8 font, uint16 character)
142  {  {
143          FONTGLYPH *glyph;          FONTGLYPH *glyph;
144    
145          if ((font < NUM_ELEMENTS(fontcache))          if ((font < NUM_ELEMENTS(g_fontcache)) && (character < NUM_ELEMENTS(g_fontcache[0])))
                         && (character < NUM_ELEMENTS(fontcache[0])))  
146          {          {
147                  glyph = &fontcache[font][character];                  glyph = &g_fontcache[font][character];
148                  if (glyph->pixmap != NULL)                  if (glyph->pixmap != NULL)
149                          return glyph;                          return glyph;
150          }          }
151    
152          ERROR("get font %d:%d\n", font, character);          error("get font %d:%d\n", font, character);
153          return NULL;          return NULL;
154  }  }
155    
156  /* Store a glyph in the font cache */  /* Store a glyph in the font cache */
157  void cache_put_font(uint8 font, uint32 character, uint16 baseline,  void
158                      uint16 width, uint16 height, HGLYPH pixmap)  cache_put_font(uint8 font, uint16 character, uint16 offset,
159                   uint16 baseline, uint16 width, uint16 height, HGLYPH pixmap)
160  {  {
161          FONTGLYPH *glyph;          FONTGLYPH *glyph;
162    
163          if ((font < NUM_ELEMENTS(fontcache))          if ((font < NUM_ELEMENTS(g_fontcache)) && (character < NUM_ELEMENTS(g_fontcache[0])))
                         && (character < NUM_ELEMENTS(fontcache[0])))  
164          {          {
165                  glyph = &fontcache[font][character];                  glyph = &g_fontcache[font][character];
166                  if (glyph->pixmap != NULL)                  if (glyph->pixmap != NULL)
167                          ui_destroy_glyph(glyph->pixmap);                          ui_destroy_glyph(glyph->pixmap);
168    
169                    glyph->offset = offset;
170                  glyph->baseline = baseline;                  glyph->baseline = baseline;
171                  glyph->width = width;                  glyph->width = width;
172                  glyph->height = height;                  glyph->height = height;
# Line 104  void cache_put_font(uint8 font, uint32 c Line 174  void cache_put_font(uint8 font, uint32 c
174          }          }
175          else          else
176          {          {
177                  ERROR("put font %d:%d\n", font, character);                  error("put font %d:%d\n", font, character);
178          }          }
179  }  }
180    
181    
182  /* TEXT CACHE */  /* TEXT CACHE */
183  static DATABLOB textcache[256];  static DATABLOB g_textcache[256];
184    
185  /* Retrieve a text item from the cache */  /* Retrieve a text item from the cache */
186  DATABLOB *cache_get_text(uint8 cache_id)  DATABLOB *
187    cache_get_text(uint8 cache_id)
188  {  {
189          DATABLOB *text;          DATABLOB *text;
190    
191          if (cache_id < NUM_ELEMENTS(textcache))          if (cache_id < NUM_ELEMENTS(g_textcache))
192          {          {
193                  text = &textcache[cache_id];                  text = &g_textcache[cache_id];
194                  if (text->data != NULL)                  if (text->data != NULL)
195                          return text;                          return text;
196          }          }
197    
198          ERROR("get text %d\n", cache_id);          error("get text %d\n", cache_id);
199          return NULL;          return NULL;
200  }  }
201    
202  /* Store a text item in the cache */  /* Store a text item in the cache */
203  void cache_put_text(uint8 cache_id, void *data, int length)  void
204    cache_put_text(uint8 cache_id, void *data, int length)
205  {  {
206          DATABLOB *text;          DATABLOB *text;
207    
208          if (cache_id < NUM_ELEMENTS(textcache))          if (cache_id < NUM_ELEMENTS(g_textcache))
209          {          {
210                  text = &textcache[cache_id];                  text = &g_textcache[cache_id];
211                  if (text->data != NULL)                  if (text->data != NULL)
212                          xfree(text->data);                          xfree(text->data);
213    
# Line 145  void cache_put_text(uint8 cache_id, void Line 217  void cache_put_text(uint8 cache_id, void
217          }          }
218          else          else
219          {          {
220                  ERROR("put text %d\n", cache_id);                  error("put text %d\n", cache_id);
221          }          }
222  }  }
223    
224    
225  /* DESKTOP CACHE */  /* DESKTOP CACHE */
226  static uint8 deskcache[0x38400];  static uint8 g_deskcache[0x38400 * 4];
227    
228  /* Retrieve desktop data from the cache */  /* Retrieve desktop data from the cache */
229  uint8 *cache_get_desktop(uint32 offset, uint32 length)  uint8 *
230    cache_get_desktop(uint32 offset, int cx, int cy, int bytes_per_pixel)
231  {  {
232          if ((offset + length) <= sizeof(deskcache))          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))
238          {          {
239                  return &deskcache[offset];                  return &g_deskcache[offset];
240          }          }
241    
242          ERROR("get desktop %d:%d\n", offset, length);          error("get desktop %d:%d\n", offset, length);
243          return NULL;          return NULL;
244  }  }
245    
246  /* Store desktop data in the cache */  /* Store desktop data in the cache */
247  void cache_put_desktop(uint32 offset, uint32 length, uint8 *data)  void
248    cache_put_desktop(uint32 offset, int cx, int cy, int scanline, int bytes_per_pixel, uint8 * data)
249  {  {
250          if ((offset + length) <= sizeof(deskcache))          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))
256          {          {
257                  memcpy(&deskcache[offset], data, length);                  cx *= bytes_per_pixel;
258                    while (cy--)
259                    {
260                            memcpy(&g_deskcache[offset], data, cx);
261                            data += scanline;
262                            offset += cx;
263                    }
264          }          }
265          else          else
266          {          {
267                  ERROR("put desktop %d:%d\n", offset, length);                  error("put desktop %d:%d\n", offset, length);
268          }          }
269  }  }
270    
271    
272    /* CURSOR CACHE */
273    static HCURSOR g_cursorcache[0x20];
274    
275    /* Retrieve cursor from cache */
276    HCURSOR
277    cache_get_cursor(uint16 cache_idx)
278    {
279            HCURSOR cursor;
280    
281            if (cache_idx < NUM_ELEMENTS(g_cursorcache))
282            {
283                    cursor = g_cursorcache[cache_idx];
284                    if (cursor != NULL)
285                            return cursor;
286            }
287    
288            error("get cursor %d\n", cache_idx);
289            return NULL;
290    }
291    
292    /* Store cursor in cache */
293    void
294    cache_put_cursor(uint16 cache_idx, HCURSOR cursor)
295    {
296            HCURSOR old;
297    
298            if (cache_idx < NUM_ELEMENTS(g_cursorcache))
299            {
300                    old = g_cursorcache[cache_idx];
301                    if (old != NULL)
302                            ui_destroy_cursor(old);
303    
304                    g_cursorcache[cache_idx] = cursor;
305            }
306            else
307            {
308                    error("put cursor %d\n", cache_idx);
309            }
310    }

Legend:
Removed from v.10  
changed lines
  Added in v.738

  ViewVC Help
Powered by ViewVC 1.1.26