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;
49static BOOL offscreen_cache_put(rdpOffscreenCache* offscreenCache, UINT32 index, rdpBitmap* bitmap);
50static void offscreen_cache_delete(rdpOffscreenCache* offscreen, UINT32 index);
53update_gdi_create_offscreen_bitmap(rdpContext* context,
56 if (!context || !createOffscreenBitmap || !context->cache)
59 rdpCache* cache = context->cache;
60 rdpBitmap* bitmap = Bitmap_Alloc(context);
65 if (!Bitmap_SetDimensions(bitmap, WINPR_ASSERTING_INT_CAST(UINT16, createOffscreenBitmap->cx),
66 WINPR_ASSERTING_INT_CAST(UINT16, createOffscreenBitmap->cy)))
68 Bitmap_Free(context, bitmap);
72 if (!bitmap->New(context, bitmap))
74 Bitmap_Free(context, bitmap);
78 offscreen_cache_delete(cache->offscreen, createOffscreenBitmap->id);
79 if (!offscreen_cache_put(cache->offscreen, createOffscreenBitmap->id, bitmap))
81 Bitmap_Free(context, bitmap);
85 if (cache->offscreen->currentSurface == createOffscreenBitmap->id)
87 if (!bitmap->SetSurface(context, bitmap, FALSE))
91 for (UINT32 i = 0; i < createOffscreenBitmap->deleteList.cIndices; i++)
93 const UINT16 index = createOffscreenBitmap->deleteList.indices[i];
94 offscreen_cache_delete(cache->offscreen, index);
100static BOOL update_gdi_switch_surface(rdpContext* context,
103 if (!context || !context->cache || !switchSurface || !context->graphics)
106 rdpCache* cache = context->cache;
107 rdpBitmap* bitmap = context->graphics->Bitmap_Prototype;
111 if (switchSurface->bitmapId == SCREEN_BITMAP_SURFACE)
113 if (!bitmap->SetSurface(context,
nullptr, TRUE))
118 rdpBitmap* bmp =
nullptr;
119 bmp = offscreen_cache_get(cache->offscreen, switchSurface->bitmapId);
123 if (!bitmap->SetSurface(context, bmp, FALSE))
127 cache->offscreen->currentSurface = switchSurface->bitmapId;
131rdpBitmap* offscreen_cache_get(rdpOffscreenCache* offscreenCache, UINT32 index)
133 rdpBitmap* bitmap =
nullptr;
135 WINPR_ASSERT(offscreenCache);
137 if (index >= offscreenCache->maxEntries)
139 WLog_ERR(TAG,
"invalid offscreen bitmap index: 0x%08" PRIX32
"", index);
143 bitmap = offscreenCache->entries[index];
147 WLog_ERR(TAG,
"invalid offscreen bitmap at index: 0x%08" PRIX32
"", index);
154BOOL offscreen_cache_put(rdpOffscreenCache* offscreenCache, UINT32 index, rdpBitmap* bitmap)
156 WINPR_ASSERT(offscreenCache);
158 if (index >= offscreenCache->maxEntries)
160 WLog_ERR(TAG,
"invalid offscreen bitmap index: 0x%08" PRIX32
"", index);
164 offscreen_cache_delete(offscreenCache, index);
165 offscreenCache->entries[index] = bitmap;
169void offscreen_cache_delete(rdpOffscreenCache* offscreenCache, UINT32 index)
171 WINPR_ASSERT(offscreenCache);
173 if (index >= offscreenCache->maxEntries)
175 WLog_ERR(TAG,
"invalid offscreen bitmap index (delete): 0x%08" PRIX32
"", index);
179 rdpBitmap* prevBitmap = offscreenCache->entries[index];
181 if (prevBitmap !=
nullptr)
183 WINPR_ASSERT(offscreenCache->context);
186 if ((offscreenCache->currentSurface == index) && prevBitmap->SetSurface)
188 if (!prevBitmap->SetSurface(offscreenCache->context,
nullptr, FALSE))
189 WLog_WARN(TAG,
"prevBitmap->SetSurface failed");
192 Bitmap_Free(offscreenCache->context, prevBitmap);
195 offscreenCache->entries[index] =
nullptr;
198void offscreen_cache_register_callbacks(rdpUpdate* update)
200 WINPR_ASSERT(update);
201 WINPR_ASSERT(update->altsec);
203 update->altsec->CreateOffscreenBitmap = update_gdi_create_offscreen_bitmap;
204 update->altsec->SwitchSurface = update_gdi_switch_surface;
207rdpOffscreenCache* offscreen_cache_new(rdpContext* context)
209 rdpOffscreenCache* offscreenCache =
nullptr;
210 rdpSettings* settings =
nullptr;
212 WINPR_ASSERT(context);
214 settings = context->settings;
215 WINPR_ASSERT(settings);
217 offscreenCache = (rdpOffscreenCache*)calloc(1,
sizeof(rdpOffscreenCache));
222 offscreenCache->context = context;
223 offscreenCache->currentSurface = SCREEN_BITMAP_SURFACE;
224 offscreenCache->maxSize = 7680;
225 offscreenCache->maxEntries = 2000;
229 offscreenCache->maxEntries))
231 offscreenCache->entries = (rdpBitmap**)calloc(offscreenCache->maxEntries,
sizeof(rdpBitmap*));
233 if (!offscreenCache->entries)
236 return offscreenCache;
238 WINPR_PRAGMA_DIAG_PUSH
239 WINPR_PRAGMA_DIAG_IGNORED_MISMATCHED_DEALLOC
240 offscreen_cache_free(offscreenCache);
241 WINPR_PRAGMA_DIAG_POP
245void offscreen_cache_free(rdpOffscreenCache* offscreenCache)
249 if (offscreenCache->entries)
251 for (
size_t i = 0; i < offscreenCache->maxEntries; i++)
253 rdpBitmap* bitmap = offscreenCache->entries[i];
254 Bitmap_Free(offscreenCache->context, bitmap);
258 free((
void*)offscreenCache->entries);
259 free(offscreenCache);
WINPR_ATTR_NODISCARD FREERDP_API BOOL freerdp_settings_set_uint32(rdpSettings *settings, FreeRDP_Settings_Keys_UInt32 id, UINT32 val)
Sets a UINT32 settings value.