FreeRDP
Loading...
Searching...
No Matches
offscreen.c
1
19
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
38struct 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
48WINPR_ATTR_NODISCARD
49static BOOL offscreen_cache_put(rdpOffscreenCache* offscreenCache, UINT32 index, rdpBitmap* bitmap);
50static void offscreen_cache_delete(rdpOffscreenCache* offscreen, UINT32 index);
51
52static BOOL
53update_gdi_create_offscreen_bitmap(rdpContext* context,
54 const CREATE_OFFSCREEN_BITMAP_ORDER* createOffscreenBitmap)
55{
56 if (!context || !createOffscreenBitmap || !context->cache)
57 return FALSE;
58
59 rdpCache* cache = context->cache;
60 rdpBitmap* bitmap = Bitmap_Alloc(context);
61
62 if (!bitmap)
63 return FALSE;
64
65 if (!Bitmap_SetDimensions(bitmap, WINPR_ASSERTING_INT_CAST(UINT16, createOffscreenBitmap->cx),
66 WINPR_ASSERTING_INT_CAST(UINT16, createOffscreenBitmap->cy)))
67 {
68 Bitmap_Free(context, bitmap);
69 return FALSE;
70 }
71
72 if (!bitmap->New(context, bitmap))
73 {
74 Bitmap_Free(context, bitmap);
75 return FALSE;
76 }
77
78 offscreen_cache_delete(cache->offscreen, createOffscreenBitmap->id);
79 if (!offscreen_cache_put(cache->offscreen, createOffscreenBitmap->id, bitmap))
80 {
81 Bitmap_Free(context, bitmap);
82 return FALSE;
83 }
84
85 if (cache->offscreen->currentSurface == createOffscreenBitmap->id)
86 {
87 if (!bitmap->SetSurface(context, bitmap, FALSE))
88 return FALSE;
89 }
90
91 for (UINT32 i = 0; i < createOffscreenBitmap->deleteList.cIndices; i++)
92 {
93 const UINT16 index = createOffscreenBitmap->deleteList.indices[i];
94 offscreen_cache_delete(cache->offscreen, index);
95 }
96
97 return TRUE;
98}
99
100static BOOL update_gdi_switch_surface(rdpContext* context,
101 const SWITCH_SURFACE_ORDER* switchSurface)
102{
103 if (!context || !context->cache || !switchSurface || !context->graphics)
104 return FALSE;
105
106 rdpCache* cache = context->cache;
107 rdpBitmap* bitmap = context->graphics->Bitmap_Prototype;
108 if (!bitmap)
109 return FALSE;
110
111 if (switchSurface->bitmapId == SCREEN_BITMAP_SURFACE)
112 {
113 if (!bitmap->SetSurface(context, nullptr, TRUE))
114 return FALSE;
115 }
116 else
117 {
118 rdpBitmap* bmp = nullptr;
119 bmp = offscreen_cache_get(cache->offscreen, switchSurface->bitmapId);
120 if (bmp == nullptr)
121 return FALSE;
122
123 if (!bitmap->SetSurface(context, bmp, FALSE))
124 return FALSE;
125 }
126
127 cache->offscreen->currentSurface = switchSurface->bitmapId;
128 return TRUE;
129}
130
131rdpBitmap* offscreen_cache_get(rdpOffscreenCache* offscreenCache, UINT32 index)
132{
133 rdpBitmap* bitmap = nullptr;
134
135 WINPR_ASSERT(offscreenCache);
136
137 if (index >= offscreenCache->maxEntries)
138 {
139 WLog_ERR(TAG, "invalid offscreen bitmap index: 0x%08" PRIX32 "", index);
140 return nullptr;
141 }
142
143 bitmap = offscreenCache->entries[index];
144
145 if (!bitmap)
146 {
147 WLog_ERR(TAG, "invalid offscreen bitmap at index: 0x%08" PRIX32 "", index);
148 return nullptr;
149 }
150
151 return bitmap;
152}
153
154BOOL offscreen_cache_put(rdpOffscreenCache* offscreenCache, UINT32 index, rdpBitmap* bitmap)
155{
156 WINPR_ASSERT(offscreenCache);
157
158 if (index >= offscreenCache->maxEntries)
159 {
160 WLog_ERR(TAG, "invalid offscreen bitmap index: 0x%08" PRIX32 "", index);
161 return FALSE;
162 }
163
164 offscreen_cache_delete(offscreenCache, index);
165 offscreenCache->entries[index] = bitmap;
166 return TRUE;
167}
168
169void offscreen_cache_delete(rdpOffscreenCache* offscreenCache, UINT32 index)
170{
171 WINPR_ASSERT(offscreenCache);
172
173 if (index >= offscreenCache->maxEntries)
174 {
175 WLog_ERR(TAG, "invalid offscreen bitmap index (delete): 0x%08" PRIX32 "", index);
176 return;
177 }
178
179 rdpBitmap* prevBitmap = offscreenCache->entries[index];
180
181 if (prevBitmap != nullptr)
182 {
183 WINPR_ASSERT(offscreenCache->context);
184
185 /* Ensure that the bitmap is no longer used in GDI */
186 if ((offscreenCache->currentSurface == index) && prevBitmap->SetSurface)
187 {
188 if (!prevBitmap->SetSurface(offscreenCache->context, nullptr, FALSE))
189 WLog_WARN(TAG, "prevBitmap->SetSurface failed");
190 }
191
192 Bitmap_Free(offscreenCache->context, prevBitmap);
193 }
194
195 offscreenCache->entries[index] = nullptr;
196}
197
198void offscreen_cache_register_callbacks(rdpUpdate* update)
199{
200 WINPR_ASSERT(update);
201 WINPR_ASSERT(update->altsec);
202
203 update->altsec->CreateOffscreenBitmap = update_gdi_create_offscreen_bitmap;
204 update->altsec->SwitchSurface = update_gdi_switch_surface;
205}
206
207rdpOffscreenCache* offscreen_cache_new(rdpContext* context)
208{
209 rdpOffscreenCache* offscreenCache = nullptr;
210 rdpSettings* settings = nullptr;
211
212 WINPR_ASSERT(context);
213
214 settings = context->settings;
215 WINPR_ASSERT(settings);
216
217 offscreenCache = (rdpOffscreenCache*)calloc(1, sizeof(rdpOffscreenCache));
218
219 if (!offscreenCache)
220 return nullptr;
221
222 offscreenCache->context = context;
223 offscreenCache->currentSurface = SCREEN_BITMAP_SURFACE;
224 offscreenCache->maxSize = 7680;
225 offscreenCache->maxEntries = 2000;
226 if (!freerdp_settings_set_uint32(settings, FreeRDP_OffscreenCacheSize, offscreenCache->maxSize))
227 goto fail;
228 if (!freerdp_settings_set_uint32(settings, FreeRDP_OffscreenCacheEntries,
229 offscreenCache->maxEntries))
230 goto fail;
231 offscreenCache->entries = (rdpBitmap**)calloc(offscreenCache->maxEntries, sizeof(rdpBitmap*));
232
233 if (!offscreenCache->entries)
234 goto fail;
235
236 return offscreenCache;
237fail:
238 WINPR_PRAGMA_DIAG_PUSH
239 WINPR_PRAGMA_DIAG_IGNORED_MISMATCHED_DEALLOC
240 offscreen_cache_free(offscreenCache);
241 WINPR_PRAGMA_DIAG_POP
242 return nullptr;
243}
244
245void offscreen_cache_free(rdpOffscreenCache* offscreenCache)
246{
247 if (offscreenCache)
248 {
249 if (offscreenCache->entries)
250 {
251 for (size_t i = 0; i < offscreenCache->maxEntries; i++)
252 {
253 rdpBitmap* bitmap = offscreenCache->entries[i];
254 Bitmap_Free(offscreenCache->context, bitmap);
255 }
256 }
257
258 free((void*)offscreenCache->entries);
259 free(offscreenCache);
260 }
261}
WINPR_ATTR_NODISCARD FREERDP_API BOOL freerdp_settings_set_uint32(rdpSettings *settings, FreeRDP_Settings_Keys_UInt32 id, UINT32 val)
Sets a UINT32 settings value.