Coverage Report

Created: 2024-05-20 06:11

/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/stream.h>
27
28
#include <freerdp/log.h>
29
30
#include "../core/graphics.h"
31
32
#include "offscreen.h"
33
#include "cache.h"
34
35
#define TAG FREERDP_TAG("cache.offscreen")
36
37
struct rdp_offscreen_cache
38
{
39
  UINT32 maxSize;        /* 0 */
40
  UINT32 maxEntries;     /* 1 */
41
  rdpBitmap** entries;   /* 2 */
42
  UINT32 currentSurface; /* 3 */
43
44
  rdpContext* context;
45
};
46
47
static void offscreen_cache_put(rdpOffscreenCache* offscreen_cache, UINT32 index,
48
                                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
  Bitmap_SetDimensions(bitmap, createOffscreenBitmap->cx, createOffscreenBitmap->cy);
69
70
0
  if (!bitmap->New(context, bitmap))
71
0
  {
72
0
    Bitmap_Free(context, bitmap);
73
0
    return FALSE;
74
0
  }
75
76
0
  offscreen_cache_delete(cache->offscreen, createOffscreenBitmap->id);
77
0
  offscreen_cache_put(cache->offscreen, createOffscreenBitmap->id, bitmap);
78
79
0
  if (cache->offscreen->currentSurface == createOffscreenBitmap->id)
80
0
    bitmap->SetSurface(context, bitmap, FALSE);
81
82
0
  for (UINT32 i = 0; i < createOffscreenBitmap->deleteList.cIndices; i++)
83
0
  {
84
0
    index = createOffscreenBitmap->deleteList.indices[i];
85
0
    offscreen_cache_delete(cache->offscreen, index);
86
0
  }
87
88
0
  return TRUE;
89
0
}
90
91
static BOOL update_gdi_switch_surface(rdpContext* context,
92
                                      const SWITCH_SURFACE_ORDER* switchSurface)
93
0
{
94
0
  rdpCache* cache = NULL;
95
0
  rdpBitmap* bitmap = NULL;
96
97
0
  if (!context || !context->cache || !switchSurface || !context->graphics)
98
0
    return FALSE;
99
100
0
  cache = context->cache;
101
0
  bitmap = context->graphics->Bitmap_Prototype;
102
0
  if (!bitmap)
103
0
    return FALSE;
104
105
0
  if (switchSurface->bitmapId == SCREEN_BITMAP_SURFACE)
106
0
  {
107
0
    bitmap->SetSurface(context, NULL, TRUE);
108
0
  }
109
0
  else
110
0
  {
111
0
    rdpBitmap* bmp = NULL;
112
0
    bmp = offscreen_cache_get(cache->offscreen, switchSurface->bitmapId);
113
0
    if (bmp == NULL)
114
0
      return FALSE;
115
116
0
    bitmap->SetSurface(context, bmp, FALSE);
117
0
  }
118
119
0
  cache->offscreen->currentSurface = switchSurface->bitmapId;
120
0
  return TRUE;
121
0
}
122
123
rdpBitmap* offscreen_cache_get(rdpOffscreenCache* offscreenCache, UINT32 index)
124
0
{
125
0
  rdpBitmap* bitmap = NULL;
126
127
0
  WINPR_ASSERT(offscreenCache);
128
129
0
  if (index >= offscreenCache->maxEntries)
130
0
  {
131
0
    WLog_ERR(TAG, "invalid offscreen bitmap index: 0x%08" PRIX32 "", index);
132
0
    return NULL;
133
0
  }
134
135
0
  bitmap = offscreenCache->entries[index];
136
137
0
  if (!bitmap)
138
0
  {
139
0
    WLog_ERR(TAG, "invalid offscreen bitmap at index: 0x%08" PRIX32 "", index);
140
0
    return NULL;
141
0
  }
142
143
0
  return bitmap;
144
0
}
145
146
void offscreen_cache_put(rdpOffscreenCache* offscreenCache, UINT32 index, rdpBitmap* bitmap)
147
0
{
148
0
  WINPR_ASSERT(offscreenCache);
149
150
0
  if (index >= offscreenCache->maxEntries)
151
0
  {
152
0
    WLog_ERR(TAG, "invalid offscreen bitmap index: 0x%08" PRIX32 "", index);
153
0
    return;
154
0
  }
155
156
0
  offscreen_cache_delete(offscreenCache, index);
157
0
  offscreenCache->entries[index] = bitmap;
158
0
}
159
160
void offscreen_cache_delete(rdpOffscreenCache* offscreenCache, UINT32 index)
161
0
{
162
0
  rdpBitmap* prevBitmap = NULL;
163
164
0
  WINPR_ASSERT(offscreenCache);
165
166
0
  if (index >= offscreenCache->maxEntries)
167
0
  {
168
0
    WLog_ERR(TAG, "invalid offscreen bitmap index (delete): 0x%08" PRIX32 "", index);
169
0
    return;
170
0
  }
171
172
0
  prevBitmap = offscreenCache->entries[index];
173
174
0
  if (prevBitmap != NULL)
175
0
    Bitmap_Free(offscreenCache->context, prevBitmap);
176
177
0
  offscreenCache->entries[index] = NULL;
178
0
}
179
180
void offscreen_cache_register_callbacks(rdpUpdate* update)
181
0
{
182
0
  WINPR_ASSERT(update);
183
0
  WINPR_ASSERT(update->altsec);
184
185
0
  update->altsec->CreateOffscreenBitmap = update_gdi_create_offscreen_bitmap;
186
0
  update->altsec->SwitchSurface = update_gdi_switch_surface;
187
0
}
188
189
rdpOffscreenCache* offscreen_cache_new(rdpContext* context)
190
0
{
191
0
  rdpOffscreenCache* offscreenCache = NULL;
192
0
  rdpSettings* settings = NULL;
193
194
0
  WINPR_ASSERT(context);
195
196
0
  settings = context->settings;
197
0
  WINPR_ASSERT(settings);
198
199
0
  offscreenCache = (rdpOffscreenCache*)calloc(1, sizeof(rdpOffscreenCache));
200
201
0
  if (!offscreenCache)
202
0
    return NULL;
203
204
0
  offscreenCache->context = context;
205
0
  offscreenCache->currentSurface = SCREEN_BITMAP_SURFACE;
206
0
  offscreenCache->maxSize = 7680;
207
0
  offscreenCache->maxEntries = 2000;
208
0
  if (!freerdp_settings_set_uint32(settings, FreeRDP_OffscreenCacheSize, offscreenCache->maxSize))
209
0
    goto fail;
210
0
  if (!freerdp_settings_set_uint32(settings, FreeRDP_OffscreenCacheEntries,
211
0
                                   offscreenCache->maxEntries))
212
0
    goto fail;
213
0
  offscreenCache->entries = (rdpBitmap**)calloc(offscreenCache->maxEntries, sizeof(rdpBitmap*));
214
215
0
  if (!offscreenCache->entries)
216
0
    goto fail;
217
218
0
  return offscreenCache;
219
0
fail:
220
0
  WINPR_PRAGMA_DIAG_PUSH
221
0
  WINPR_PRAGMA_DIAG_IGNORED_MISMATCHED_DEALLOC
222
0
  offscreen_cache_free(offscreenCache);
223
0
  WINPR_PRAGMA_DIAG_POP
224
0
  return NULL;
225
0
}
226
227
void offscreen_cache_free(rdpOffscreenCache* offscreenCache)
228
0
{
229
0
  if (offscreenCache)
230
0
  {
231
0
    if (offscreenCache->entries)
232
0
    {
233
0
      for (size_t i = 0; i < offscreenCache->maxEntries; i++)
234
0
      {
235
0
        rdpBitmap* bitmap = offscreenCache->entries[i];
236
0
        Bitmap_Free(offscreenCache->context, bitmap);
237
0
      }
238
0
    }
239
240
0
    free(offscreenCache->entries);
241
0
    free(offscreenCache);
242
0
  }
243
0
}