Coverage Report

Created: 2026-02-26 06:54

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