20#include <freerdp/config.h>
25#include <winpr/assert.h>
26#include <winpr/cast.h>
27#include <winpr/stream.h>
29#include <freerdp/log.h>
31#include "../core/graphics.h"
36#define TAG FREERDP_TAG("cache.offscreen")
38struct rdp_offscreen_cache
43 UINT32 currentSurface;
48static void offscreen_cache_put(rdpOffscreenCache* offscreenCache, UINT32 index, rdpBitmap* bitmap);
49static void offscreen_cache_delete(rdpOffscreenCache* offscreen, UINT32 index);
52update_gdi_create_offscreen_bitmap(rdpContext* context,
56 rdpBitmap* bitmap = NULL;
57 rdpCache* cache = NULL;
59 if (!context || !createOffscreenBitmap || !context->cache)
62 cache = context->cache;
63 bitmap = Bitmap_Alloc(context);
68 if (!Bitmap_SetDimensions(bitmap, WINPR_ASSERTING_INT_CAST(UINT16, createOffscreenBitmap->cx),
69 WINPR_ASSERTING_INT_CAST(UINT16, createOffscreenBitmap->cy)))
71 Bitmap_Free(context, bitmap);
75 if (!bitmap->New(context, bitmap))
77 Bitmap_Free(context, bitmap);
81 offscreen_cache_delete(cache->offscreen, createOffscreenBitmap->id);
82 offscreen_cache_put(cache->offscreen, createOffscreenBitmap->id, bitmap);
84 if (cache->offscreen->currentSurface == createOffscreenBitmap->id)
85 bitmap->SetSurface(context, bitmap, FALSE);
87 for (UINT32 i = 0; i < createOffscreenBitmap->deleteList.cIndices; i++)
89 index = createOffscreenBitmap->deleteList.indices[i];
90 offscreen_cache_delete(cache->offscreen, index);
96static BOOL update_gdi_switch_surface(rdpContext* context,
99 rdpCache* cache = NULL;
100 rdpBitmap* bitmap = NULL;
102 if (!context || !context->cache || !switchSurface || !context->graphics)
105 cache = context->cache;
106 bitmap = context->graphics->Bitmap_Prototype;
110 if (switchSurface->bitmapId == SCREEN_BITMAP_SURFACE)
112 bitmap->SetSurface(context, NULL, TRUE);
116 rdpBitmap* bmp = NULL;
117 bmp = offscreen_cache_get(cache->offscreen, switchSurface->bitmapId);
121 bitmap->SetSurface(context, bmp, FALSE);
124 cache->offscreen->currentSurface = switchSurface->bitmapId;
128rdpBitmap* offscreen_cache_get(rdpOffscreenCache* offscreenCache, UINT32 index)
130 rdpBitmap* bitmap = NULL;
132 WINPR_ASSERT(offscreenCache);
134 if (index >= offscreenCache->maxEntries)
136 WLog_ERR(TAG,
"invalid offscreen bitmap index: 0x%08" PRIX32
"", index);
140 bitmap = offscreenCache->entries[index];
144 WLog_ERR(TAG,
"invalid offscreen bitmap at index: 0x%08" PRIX32
"", index);
151void offscreen_cache_put(rdpOffscreenCache* offscreenCache, UINT32 index, rdpBitmap* bitmap)
153 WINPR_ASSERT(offscreenCache);
155 if (index >= offscreenCache->maxEntries)
157 WLog_ERR(TAG,
"invalid offscreen bitmap index: 0x%08" PRIX32
"", index);
161 offscreen_cache_delete(offscreenCache, index);
162 offscreenCache->entries[index] = bitmap;
165void offscreen_cache_delete(rdpOffscreenCache* offscreenCache, UINT32 index)
167 WINPR_ASSERT(offscreenCache);
169 if (index >= offscreenCache->maxEntries)
171 WLog_ERR(TAG,
"invalid offscreen bitmap index (delete): 0x%08" PRIX32
"", index);
175 rdpBitmap* prevBitmap = offscreenCache->entries[index];
177 if (prevBitmap != NULL)
179 WINPR_ASSERT(offscreenCache->context);
182 IFCALL(prevBitmap->SetSurface, offscreenCache->context, NULL, FALSE);
183 Bitmap_Free(offscreenCache->context, prevBitmap);
186 offscreenCache->entries[index] = NULL;
189void offscreen_cache_register_callbacks(rdpUpdate* update)
191 WINPR_ASSERT(update);
192 WINPR_ASSERT(update->altsec);
194 update->altsec->CreateOffscreenBitmap = update_gdi_create_offscreen_bitmap;
195 update->altsec->SwitchSurface = update_gdi_switch_surface;
198rdpOffscreenCache* offscreen_cache_new(rdpContext* context)
200 rdpOffscreenCache* offscreenCache = NULL;
201 rdpSettings* settings = NULL;
203 WINPR_ASSERT(context);
205 settings = context->settings;
206 WINPR_ASSERT(settings);
208 offscreenCache = (rdpOffscreenCache*)calloc(1,
sizeof(rdpOffscreenCache));
213 offscreenCache->context = context;
214 offscreenCache->currentSurface = SCREEN_BITMAP_SURFACE;
215 offscreenCache->maxSize = 7680;
216 offscreenCache->maxEntries = 2000;
220 offscreenCache->maxEntries))
222 offscreenCache->entries = (rdpBitmap**)calloc(offscreenCache->maxEntries,
sizeof(rdpBitmap*));
224 if (!offscreenCache->entries)
227 return offscreenCache;
229 WINPR_PRAGMA_DIAG_PUSH
230 WINPR_PRAGMA_DIAG_IGNORED_MISMATCHED_DEALLOC
231 offscreen_cache_free(offscreenCache);
232 WINPR_PRAGMA_DIAG_POP
236void offscreen_cache_free(rdpOffscreenCache* offscreenCache)
240 if (offscreenCache->entries)
242 for (
size_t i = 0; i < offscreenCache->maxEntries; i++)
244 rdpBitmap* bitmap = offscreenCache->entries[i];
245 Bitmap_Free(offscreenCache->context, bitmap);
249 free((
void*)offscreenCache->entries);
250 free(offscreenCache);
FREERDP_API BOOL freerdp_settings_set_uint32(rdpSettings *settings, FreeRDP_Settings_Keys_UInt32 id, UINT32 param)
Sets a UINT32 settings value.