/src/FreeRDP/libfreerdp/cache/offscreen.c
Line | Count | Source (jump to first uncovered line) |
1 | | /** |
2 | | * FreeRDP: A Remote Desktop Protocol Implementation |
3 | | * Offscreen Bitmap Cache |
4 | | * |
5 | | * Copyright 2011 Marc-Andre Moreau <marcandre.moreau@gmail.com> |
6 | | * |
7 | | * Licensed under the Apache License, Version 2.0 (the "License"); |
8 | | * you may not use this file except in compliance with the License. |
9 | | * You may obtain a copy of the License at |
10 | | * |
11 | | * http://www.apache.org/licenses/LICENSE-2.0 |
12 | | * |
13 | | * Unless required by applicable law or agreed to in writing, software |
14 | | * distributed under the License is distributed on an "AS IS" BASIS, |
15 | | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
16 | | * See the License for the specific language governing permissions and |
17 | | * limitations under the License. |
18 | | */ |
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 | | |
38 | | struct 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 | | |
48 | | static void offscreen_cache_put(rdpOffscreenCache* offscreenCache, UINT32 index, rdpBitmap* bitmap); |
49 | | static void offscreen_cache_delete(rdpOffscreenCache* offscreen, UINT32 index); |
50 | | |
51 | | static BOOL |
52 | | update_gdi_create_offscreen_bitmap(rdpContext* context, |
53 | | const CREATE_OFFSCREEN_BITMAP_ORDER* createOffscreenBitmap) |
54 | 0 | { |
55 | 0 | UINT16 index = 0; |
56 | 0 | rdpBitmap* bitmap = NULL; |
57 | 0 | rdpCache* cache = NULL; |
58 | |
|
59 | 0 | if (!context || !createOffscreenBitmap || !context->cache) |
60 | 0 | return FALSE; |
61 | | |
62 | 0 | cache = context->cache; |
63 | 0 | bitmap = Bitmap_Alloc(context); |
64 | |
|
65 | 0 | if (!bitmap) |
66 | 0 | return FALSE; |
67 | | |
68 | 0 | if (!Bitmap_SetDimensions(bitmap, WINPR_ASSERTING_INT_CAST(UINT16, createOffscreenBitmap->cx), |
69 | 0 | WINPR_ASSERTING_INT_CAST(UINT16, createOffscreenBitmap->cy))) |
70 | 0 | { |
71 | 0 | Bitmap_Free(context, bitmap); |
72 | 0 | return FALSE; |
73 | 0 | } |
74 | | |
75 | 0 | if (!bitmap->New(context, bitmap)) |
76 | 0 | { |
77 | 0 | Bitmap_Free(context, bitmap); |
78 | 0 | return FALSE; |
79 | 0 | } |
80 | | |
81 | 0 | offscreen_cache_delete(cache->offscreen, createOffscreenBitmap->id); |
82 | 0 | offscreen_cache_put(cache->offscreen, createOffscreenBitmap->id, bitmap); |
83 | |
|
84 | 0 | if (cache->offscreen->currentSurface == createOffscreenBitmap->id) |
85 | 0 | bitmap->SetSurface(context, bitmap, FALSE); |
86 | |
|
87 | 0 | for (UINT32 i = 0; i < createOffscreenBitmap->deleteList.cIndices; i++) |
88 | 0 | { |
89 | 0 | index = createOffscreenBitmap->deleteList.indices[i]; |
90 | 0 | offscreen_cache_delete(cache->offscreen, index); |
91 | 0 | } |
92 | |
|
93 | 0 | return TRUE; |
94 | 0 | } |
95 | | |
96 | | static BOOL update_gdi_switch_surface(rdpContext* context, |
97 | | const SWITCH_SURFACE_ORDER* switchSurface) |
98 | 0 | { |
99 | 0 | rdpCache* cache = NULL; |
100 | 0 | rdpBitmap* bitmap = NULL; |
101 | |
|
102 | 0 | if (!context || !context->cache || !switchSurface || !context->graphics) |
103 | 0 | return FALSE; |
104 | | |
105 | 0 | cache = context->cache; |
106 | 0 | bitmap = context->graphics->Bitmap_Prototype; |
107 | 0 | if (!bitmap) |
108 | 0 | return FALSE; |
109 | | |
110 | 0 | if (switchSurface->bitmapId == SCREEN_BITMAP_SURFACE) |
111 | 0 | { |
112 | 0 | bitmap->SetSurface(context, NULL, TRUE); |
113 | 0 | } |
114 | 0 | else |
115 | 0 | { |
116 | 0 | rdpBitmap* bmp = NULL; |
117 | 0 | bmp = offscreen_cache_get(cache->offscreen, switchSurface->bitmapId); |
118 | 0 | if (bmp == NULL) |
119 | 0 | return FALSE; |
120 | | |
121 | 0 | bitmap->SetSurface(context, bmp, FALSE); |
122 | 0 | } |
123 | | |
124 | 0 | cache->offscreen->currentSurface = switchSurface->bitmapId; |
125 | 0 | return TRUE; |
126 | 0 | } |
127 | | |
128 | | rdpBitmap* offscreen_cache_get(rdpOffscreenCache* offscreenCache, UINT32 index) |
129 | 0 | { |
130 | 0 | rdpBitmap* bitmap = NULL; |
131 | |
|
132 | 0 | WINPR_ASSERT(offscreenCache); |
133 | | |
134 | 0 | if (index >= offscreenCache->maxEntries) |
135 | 0 | { |
136 | 0 | WLog_ERR(TAG, "invalid offscreen bitmap index: 0x%08" PRIX32 "", index); |
137 | 0 | return NULL; |
138 | 0 | } |
139 | | |
140 | 0 | bitmap = offscreenCache->entries[index]; |
141 | |
|
142 | 0 | if (!bitmap) |
143 | 0 | { |
144 | 0 | WLog_ERR(TAG, "invalid offscreen bitmap at index: 0x%08" PRIX32 "", index); |
145 | 0 | return NULL; |
146 | 0 | } |
147 | | |
148 | 0 | return bitmap; |
149 | 0 | } |
150 | | |
151 | | void offscreen_cache_put(rdpOffscreenCache* offscreenCache, UINT32 index, rdpBitmap* bitmap) |
152 | 0 | { |
153 | 0 | WINPR_ASSERT(offscreenCache); |
154 | | |
155 | 0 | if (index >= offscreenCache->maxEntries) |
156 | 0 | { |
157 | 0 | WLog_ERR(TAG, "invalid offscreen bitmap index: 0x%08" PRIX32 "", index); |
158 | 0 | return; |
159 | 0 | } |
160 | | |
161 | 0 | offscreen_cache_delete(offscreenCache, index); |
162 | 0 | offscreenCache->entries[index] = bitmap; |
163 | 0 | } |
164 | | |
165 | | void offscreen_cache_delete(rdpOffscreenCache* offscreenCache, UINT32 index) |
166 | 0 | { |
167 | 0 | rdpBitmap* prevBitmap = NULL; |
168 | |
|
169 | 0 | WINPR_ASSERT(offscreenCache); |
170 | | |
171 | 0 | if (index >= offscreenCache->maxEntries) |
172 | 0 | { |
173 | 0 | WLog_ERR(TAG, "invalid offscreen bitmap index (delete): 0x%08" PRIX32 "", index); |
174 | 0 | return; |
175 | 0 | } |
176 | | |
177 | 0 | prevBitmap = offscreenCache->entries[index]; |
178 | |
|
179 | 0 | if (prevBitmap != NULL) |
180 | 0 | Bitmap_Free(offscreenCache->context, prevBitmap); |
181 | |
|
182 | 0 | offscreenCache->entries[index] = NULL; |
183 | 0 | } |
184 | | |
185 | | void offscreen_cache_register_callbacks(rdpUpdate* update) |
186 | 0 | { |
187 | 0 | WINPR_ASSERT(update); |
188 | 0 | WINPR_ASSERT(update->altsec); |
189 | | |
190 | 0 | update->altsec->CreateOffscreenBitmap = update_gdi_create_offscreen_bitmap; |
191 | 0 | update->altsec->SwitchSurface = update_gdi_switch_surface; |
192 | 0 | } |
193 | | |
194 | | rdpOffscreenCache* offscreen_cache_new(rdpContext* context) |
195 | 0 | { |
196 | 0 | rdpOffscreenCache* offscreenCache = NULL; |
197 | 0 | rdpSettings* settings = NULL; |
198 | |
|
199 | 0 | WINPR_ASSERT(context); |
200 | | |
201 | 0 | settings = context->settings; |
202 | 0 | WINPR_ASSERT(settings); |
203 | | |
204 | 0 | offscreenCache = (rdpOffscreenCache*)calloc(1, sizeof(rdpOffscreenCache)); |
205 | |
|
206 | 0 | if (!offscreenCache) |
207 | 0 | return NULL; |
208 | | |
209 | 0 | offscreenCache->context = context; |
210 | 0 | offscreenCache->currentSurface = SCREEN_BITMAP_SURFACE; |
211 | 0 | offscreenCache->maxSize = 7680; |
212 | 0 | offscreenCache->maxEntries = 2000; |
213 | 0 | if (!freerdp_settings_set_uint32(settings, FreeRDP_OffscreenCacheSize, offscreenCache->maxSize)) |
214 | 0 | goto fail; |
215 | 0 | if (!freerdp_settings_set_uint32(settings, FreeRDP_OffscreenCacheEntries, |
216 | 0 | offscreenCache->maxEntries)) |
217 | 0 | goto fail; |
218 | 0 | offscreenCache->entries = (rdpBitmap**)calloc(offscreenCache->maxEntries, sizeof(rdpBitmap*)); |
219 | |
|
220 | 0 | if (!offscreenCache->entries) |
221 | 0 | goto fail; |
222 | | |
223 | 0 | return offscreenCache; |
224 | 0 | fail: |
225 | 0 | WINPR_PRAGMA_DIAG_PUSH |
226 | 0 | WINPR_PRAGMA_DIAG_IGNORED_MISMATCHED_DEALLOC |
227 | 0 | offscreen_cache_free(offscreenCache); |
228 | 0 | WINPR_PRAGMA_DIAG_POP |
229 | 0 | return NULL; |
230 | 0 | } |
231 | | |
232 | | void offscreen_cache_free(rdpOffscreenCache* offscreenCache) |
233 | 0 | { |
234 | 0 | if (offscreenCache) |
235 | 0 | { |
236 | 0 | if (offscreenCache->entries) |
237 | 0 | { |
238 | 0 | for (size_t i = 0; i < offscreenCache->maxEntries; i++) |
239 | 0 | { |
240 | 0 | rdpBitmap* bitmap = offscreenCache->entries[i]; |
241 | 0 | Bitmap_Free(offscreenCache->context, bitmap); |
242 | 0 | } |
243 | 0 | } |
244 | |
|
245 | 0 | free((void*)offscreenCache->entries); |
246 | 0 | free(offscreenCache); |
247 | 0 | } |
248 | 0 | } |