Coverage Report

Created: 2024-09-08 06:20

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