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,
55 if (!context || !createOffscreenBitmap || !context->cache)
58 rdpCache* cache = context->cache;
59 rdpBitmap* bitmap = Bitmap_Alloc(context);
64 if (!Bitmap_SetDimensions(bitmap, WINPR_ASSERTING_INT_CAST(UINT16, createOffscreenBitmap->cx),
65 WINPR_ASSERTING_INT_CAST(UINT16, createOffscreenBitmap->cy)))
67 Bitmap_Free(context, bitmap);
71 if (!bitmap->New(context, bitmap))
73 Bitmap_Free(context, bitmap);
77 offscreen_cache_delete(cache->offscreen, createOffscreenBitmap->id);
78 offscreen_cache_put(cache->offscreen, createOffscreenBitmap->id, bitmap);
80 if (cache->offscreen->currentSurface == createOffscreenBitmap->id)
82 if (!bitmap->SetSurface(context, bitmap, FALSE))
86 for (UINT32 i = 0; i < createOffscreenBitmap->deleteList.cIndices; i++)
88 const UINT16 index = createOffscreenBitmap->deleteList.indices[i];
89 offscreen_cache_delete(cache->offscreen, index);
95static BOOL update_gdi_switch_surface(rdpContext* context,
98 if (!context || !context->cache || !switchSurface || !context->graphics)
101 rdpCache* cache = context->cache;
102 rdpBitmap* bitmap = context->graphics->Bitmap_Prototype;
106 if (switchSurface->bitmapId == SCREEN_BITMAP_SURFACE)
108 if (!bitmap->SetSurface(context,
nullptr, TRUE))
113 rdpBitmap* bmp =
nullptr;
114 bmp = offscreen_cache_get(cache->offscreen, switchSurface->bitmapId);
118 if (!bitmap->SetSurface(context, bmp, FALSE))
122 cache->offscreen->currentSurface = switchSurface->bitmapId;
126rdpBitmap* offscreen_cache_get(rdpOffscreenCache* offscreenCache, UINT32 index)
128 rdpBitmap* bitmap =
nullptr;
130 WINPR_ASSERT(offscreenCache);
132 if (index >= offscreenCache->maxEntries)
134 WLog_ERR(TAG,
"invalid offscreen bitmap index: 0x%08" PRIX32
"", index);
138 bitmap = offscreenCache->entries[index];
142 WLog_ERR(TAG,
"invalid offscreen bitmap at index: 0x%08" PRIX32
"", index);
149void offscreen_cache_put(rdpOffscreenCache* offscreenCache, UINT32 index, rdpBitmap* bitmap)
151 WINPR_ASSERT(offscreenCache);
153 if (index >= offscreenCache->maxEntries)
155 WLog_ERR(TAG,
"invalid offscreen bitmap index: 0x%08" PRIX32
"", index);
159 offscreen_cache_delete(offscreenCache, index);
160 offscreenCache->entries[index] = bitmap;
163void offscreen_cache_delete(rdpOffscreenCache* offscreenCache, UINT32 index)
165 WINPR_ASSERT(offscreenCache);
167 if (index >= offscreenCache->maxEntries)
169 WLog_ERR(TAG,
"invalid offscreen bitmap index (delete): 0x%08" PRIX32
"", index);
173 rdpBitmap* prevBitmap = offscreenCache->entries[index];
175 if (prevBitmap !=
nullptr)
177 WINPR_ASSERT(offscreenCache->context);
180 if (prevBitmap->SetSurface)
182 if (!prevBitmap->SetSurface(offscreenCache->context,
nullptr, FALSE))
183 WLog_WARN(TAG,
"prevBitmap->SetSurface failed");
186 Bitmap_Free(offscreenCache->context, prevBitmap);
189 offscreenCache->entries[index] =
nullptr;
192void offscreen_cache_register_callbacks(rdpUpdate* update)
194 WINPR_ASSERT(update);
195 WINPR_ASSERT(update->altsec);
197 update->altsec->CreateOffscreenBitmap = update_gdi_create_offscreen_bitmap;
198 update->altsec->SwitchSurface = update_gdi_switch_surface;
201rdpOffscreenCache* offscreen_cache_new(rdpContext* context)
203 rdpOffscreenCache* offscreenCache =
nullptr;
204 rdpSettings* settings =
nullptr;
206 WINPR_ASSERT(context);
208 settings = context->settings;
209 WINPR_ASSERT(settings);
211 offscreenCache = (rdpOffscreenCache*)calloc(1,
sizeof(rdpOffscreenCache));
216 offscreenCache->context = context;
217 offscreenCache->currentSurface = SCREEN_BITMAP_SURFACE;
218 offscreenCache->maxSize = 7680;
219 offscreenCache->maxEntries = 2000;
223 offscreenCache->maxEntries))
225 offscreenCache->entries = (rdpBitmap**)calloc(offscreenCache->maxEntries,
sizeof(rdpBitmap*));
227 if (!offscreenCache->entries)
230 return offscreenCache;
232 WINPR_PRAGMA_DIAG_PUSH
233 WINPR_PRAGMA_DIAG_IGNORED_MISMATCHED_DEALLOC
234 offscreen_cache_free(offscreenCache);
235 WINPR_PRAGMA_DIAG_POP
239void offscreen_cache_free(rdpOffscreenCache* offscreenCache)
243 if (offscreenCache->entries)
245 for (
size_t i = 0; i < offscreenCache->maxEntries; i++)
247 rdpBitmap* bitmap = offscreenCache->entries[i];
248 Bitmap_Free(offscreenCache->context, bitmap);
252 free((
void*)offscreenCache->entries);
253 free(offscreenCache);
FREERDP_API BOOL freerdp_settings_set_uint32(rdpSettings *settings, FreeRDP_Settings_Keys_UInt32 id, UINT32 val)
Sets a UINT32 settings value.