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