FreeRDP
offscreen.c
1 
20 #include <freerdp/config.h>
21 
22 #include <stdio.h>
23 
24 #include <winpr/crt.h>
25 #include <winpr/assert.h>
26 #include <winpr/cast.h>
27 #include <winpr/stream.h>
28 
29 #include <freerdp/log.h>
30 
31 #include "../core/graphics.h"
32 
33 #include "offscreen.h"
34 #include "cache.h"
35 
36 #define TAG FREERDP_TAG("cache.offscreen")
37 
38 struct rdp_offscreen_cache
39 {
40  UINT32 maxSize; /* 0 */
41  UINT32 maxEntries; /* 1 */
42  rdpBitmap** entries; /* 2 */
43  UINT32 currentSurface; /* 3 */
44 
45  rdpContext* context;
46 };
47 
48 static void offscreen_cache_put(rdpOffscreenCache* offscreenCache, UINT32 index, rdpBitmap* bitmap);
49 static void offscreen_cache_delete(rdpOffscreenCache* offscreen, UINT32 index);
50 
51 static BOOL
52 update_gdi_create_offscreen_bitmap(rdpContext* context,
53  const CREATE_OFFSCREEN_BITMAP_ORDER* createOffscreenBitmap)
54 {
55  UINT16 index = 0;
56  rdpBitmap* bitmap = NULL;
57  rdpCache* cache = NULL;
58 
59  if (!context || !createOffscreenBitmap || !context->cache)
60  return FALSE;
61 
62  cache = context->cache;
63  bitmap = Bitmap_Alloc(context);
64 
65  if (!bitmap)
66  return FALSE;
67 
68  if (!Bitmap_SetDimensions(bitmap, WINPR_ASSERTING_INT_CAST(UINT16, createOffscreenBitmap->cx),
69  WINPR_ASSERTING_INT_CAST(UINT16, createOffscreenBitmap->cy)))
70  {
71  Bitmap_Free(context, bitmap);
72  return FALSE;
73  }
74 
75  if (!bitmap->New(context, bitmap))
76  {
77  Bitmap_Free(context, bitmap);
78  return FALSE;
79  }
80 
81  offscreen_cache_delete(cache->offscreen, createOffscreenBitmap->id);
82  offscreen_cache_put(cache->offscreen, createOffscreenBitmap->id, bitmap);
83 
84  if (cache->offscreen->currentSurface == createOffscreenBitmap->id)
85  bitmap->SetSurface(context, bitmap, FALSE);
86 
87  for (UINT32 i = 0; i < createOffscreenBitmap->deleteList.cIndices; i++)
88  {
89  index = createOffscreenBitmap->deleteList.indices[i];
90  offscreen_cache_delete(cache->offscreen, index);
91  }
92 
93  return TRUE;
94 }
95 
96 static BOOL update_gdi_switch_surface(rdpContext* context,
97  const SWITCH_SURFACE_ORDER* switchSurface)
98 {
99  rdpCache* cache = NULL;
100  rdpBitmap* bitmap = NULL;
101 
102  if (!context || !context->cache || !switchSurface || !context->graphics)
103  return FALSE;
104 
105  cache = context->cache;
106  bitmap = context->graphics->Bitmap_Prototype;
107  if (!bitmap)
108  return FALSE;
109 
110  if (switchSurface->bitmapId == SCREEN_BITMAP_SURFACE)
111  {
112  bitmap->SetSurface(context, NULL, TRUE);
113  }
114  else
115  {
116  rdpBitmap* bmp = NULL;
117  bmp = offscreen_cache_get(cache->offscreen, switchSurface->bitmapId);
118  if (bmp == NULL)
119  return FALSE;
120 
121  bitmap->SetSurface(context, bmp, FALSE);
122  }
123 
124  cache->offscreen->currentSurface = switchSurface->bitmapId;
125  return TRUE;
126 }
127 
128 rdpBitmap* offscreen_cache_get(rdpOffscreenCache* offscreenCache, UINT32 index)
129 {
130  rdpBitmap* bitmap = NULL;
131 
132  WINPR_ASSERT(offscreenCache);
133 
134  if (index >= offscreenCache->maxEntries)
135  {
136  WLog_ERR(TAG, "invalid offscreen bitmap index: 0x%08" PRIX32 "", index);
137  return NULL;
138  }
139 
140  bitmap = offscreenCache->entries[index];
141 
142  if (!bitmap)
143  {
144  WLog_ERR(TAG, "invalid offscreen bitmap at index: 0x%08" PRIX32 "", index);
145  return NULL;
146  }
147 
148  return bitmap;
149 }
150 
151 void offscreen_cache_put(rdpOffscreenCache* offscreenCache, UINT32 index, rdpBitmap* bitmap)
152 {
153  WINPR_ASSERT(offscreenCache);
154 
155  if (index >= offscreenCache->maxEntries)
156  {
157  WLog_ERR(TAG, "invalid offscreen bitmap index: 0x%08" PRIX32 "", index);
158  return;
159  }
160 
161  offscreen_cache_delete(offscreenCache, index);
162  offscreenCache->entries[index] = bitmap;
163 }
164 
165 void offscreen_cache_delete(rdpOffscreenCache* offscreenCache, UINT32 index)
166 {
167  rdpBitmap* prevBitmap = NULL;
168 
169  WINPR_ASSERT(offscreenCache);
170 
171  if (index >= offscreenCache->maxEntries)
172  {
173  WLog_ERR(TAG, "invalid offscreen bitmap index (delete): 0x%08" PRIX32 "", index);
174  return;
175  }
176 
177  prevBitmap = offscreenCache->entries[index];
178 
179  if (prevBitmap != NULL)
180  Bitmap_Free(offscreenCache->context, prevBitmap);
181 
182  offscreenCache->entries[index] = NULL;
183 }
184 
185 void offscreen_cache_register_callbacks(rdpUpdate* update)
186 {
187  WINPR_ASSERT(update);
188  WINPR_ASSERT(update->altsec);
189 
190  update->altsec->CreateOffscreenBitmap = update_gdi_create_offscreen_bitmap;
191  update->altsec->SwitchSurface = update_gdi_switch_surface;
192 }
193 
194 rdpOffscreenCache* offscreen_cache_new(rdpContext* context)
195 {
196  rdpOffscreenCache* offscreenCache = NULL;
197  rdpSettings* settings = NULL;
198 
199  WINPR_ASSERT(context);
200 
201  settings = context->settings;
202  WINPR_ASSERT(settings);
203 
204  offscreenCache = (rdpOffscreenCache*)calloc(1, sizeof(rdpOffscreenCache));
205 
206  if (!offscreenCache)
207  return NULL;
208 
209  offscreenCache->context = context;
210  offscreenCache->currentSurface = SCREEN_BITMAP_SURFACE;
211  offscreenCache->maxSize = 7680;
212  offscreenCache->maxEntries = 2000;
213  if (!freerdp_settings_set_uint32(settings, FreeRDP_OffscreenCacheSize, offscreenCache->maxSize))
214  goto fail;
215  if (!freerdp_settings_set_uint32(settings, FreeRDP_OffscreenCacheEntries,
216  offscreenCache->maxEntries))
217  goto fail;
218  offscreenCache->entries = (rdpBitmap**)calloc(offscreenCache->maxEntries, sizeof(rdpBitmap*));
219 
220  if (!offscreenCache->entries)
221  goto fail;
222 
223  return offscreenCache;
224 fail:
225  WINPR_PRAGMA_DIAG_PUSH
226  WINPR_PRAGMA_DIAG_IGNORED_MISMATCHED_DEALLOC
227  offscreen_cache_free(offscreenCache);
228  WINPR_PRAGMA_DIAG_POP
229  return NULL;
230 }
231 
232 void offscreen_cache_free(rdpOffscreenCache* offscreenCache)
233 {
234  if (offscreenCache)
235  {
236  if (offscreenCache->entries)
237  {
238  for (size_t i = 0; i < offscreenCache->maxEntries; i++)
239  {
240  rdpBitmap* bitmap = offscreenCache->entries[i];
241  Bitmap_Free(offscreenCache->context, bitmap);
242  }
243  }
244 
245  free((void*)offscreenCache->entries);
246  free(offscreenCache);
247  }
248 }
FREERDP_API BOOL freerdp_settings_set_uint32(rdpSettings *settings, FreeRDP_Settings_Keys_UInt32 id, UINT32 param)
Sets a UINT32 settings value.