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