20 #include <freerdp/config.h>
24 #include <winpr/crt.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"
33 #include "offscreen.h"
36 #define TAG FREERDP_TAG("cache.offscreen")
38 struct rdp_offscreen_cache
43 UINT32 currentSurface;
48 static void offscreen_cache_put(rdpOffscreenCache* offscreenCache, UINT32 index, rdpBitmap* bitmap);
49 static void offscreen_cache_delete(rdpOffscreenCache* offscreen, UINT32 index);
52 update_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);
96 static 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;
128 rdpBitmap* 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);
151 void 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;
165 void offscreen_cache_delete(rdpOffscreenCache* offscreenCache, UINT32 index)
167 rdpBitmap* prevBitmap = NULL;
169 WINPR_ASSERT(offscreenCache);
171 if (index >= offscreenCache->maxEntries)
173 WLog_ERR(TAG,
"invalid offscreen bitmap index (delete): 0x%08" PRIX32
"", index);
177 prevBitmap = offscreenCache->entries[index];
179 if (prevBitmap != NULL)
180 Bitmap_Free(offscreenCache->context, prevBitmap);
182 offscreenCache->entries[index] = NULL;
185 void offscreen_cache_register_callbacks(rdpUpdate* update)
187 WINPR_ASSERT(update);
188 WINPR_ASSERT(update->altsec);
190 update->altsec->CreateOffscreenBitmap = update_gdi_create_offscreen_bitmap;
191 update->altsec->SwitchSurface = update_gdi_switch_surface;
194 rdpOffscreenCache* offscreen_cache_new(rdpContext* context)
196 rdpOffscreenCache* offscreenCache = NULL;
197 rdpSettings* settings = NULL;
199 WINPR_ASSERT(context);
201 settings = context->settings;
202 WINPR_ASSERT(settings);
204 offscreenCache = (rdpOffscreenCache*)calloc(1,
sizeof(rdpOffscreenCache));
209 offscreenCache->context = context;
210 offscreenCache->currentSurface = SCREEN_BITMAP_SURFACE;
211 offscreenCache->maxSize = 7680;
212 offscreenCache->maxEntries = 2000;
216 offscreenCache->maxEntries))
218 offscreenCache->entries = (rdpBitmap**)calloc(offscreenCache->maxEntries,
sizeof(rdpBitmap*));
220 if (!offscreenCache->entries)
223 return offscreenCache;
225 WINPR_PRAGMA_DIAG_PUSH
226 WINPR_PRAGMA_DIAG_IGNORED_MISMATCHED_DEALLOC
227 offscreen_cache_free(offscreenCache);
228 WINPR_PRAGMA_DIAG_POP
232 void offscreen_cache_free(rdpOffscreenCache* offscreenCache)
236 if (offscreenCache->entries)
238 for (
size_t i = 0; i < offscreenCache->maxEntries; i++)
240 rdpBitmap* bitmap = offscreenCache->entries[i];
241 Bitmap_Free(offscreenCache->context, bitmap);
245 free((
void*)offscreenCache->entries);
246 free(offscreenCache);
FREERDP_API BOOL freerdp_settings_set_uint32(rdpSettings *settings, FreeRDP_Settings_Keys_UInt32 id, UINT32 param)
Sets a UINT32 settings value.