Coverage Report

Created: 2023-09-25 06:56

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