Coverage Report

Created: 2025-10-10 06:46

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/FreeRDP/libfreerdp/primitives/prim_copy.c
Line
Count
Source
1
/* FreeRDP: A Remote Desktop Protocol Client
2
 * Copy operations.
3
 * vi:ts=4 sw=4:
4
 *
5
 * (c) Copyright 2012 Hewlett-Packard Development Company, L.P.
6
 * Licensed under the Apache License, Version 2.0 (the "License"); you may
7
 * not use this file except in compliance with the License. You may obtain
8
 * a copy of the License at http://www.apache.org/licenses/LICENSE-2.0.
9
 * Unless required by applicable law or agreed to in writing, software
10
 * distributed under the License is distributed on an "AS IS" BASIS,
11
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
12
 * or implied. See the License for the specific language governing
13
 * permissions and limitations under the License.
14
 */
15
16
#include <freerdp/config.h>
17
18
#include <string.h>
19
#include <freerdp/types.h>
20
#include <freerdp/primitives.h>
21
#include <freerdp/log.h>
22
23
#include "prim_internal.h"
24
#include "prim_copy.h"
25
#include "../codec/color.h"
26
27
#include <freerdp/codec/color.h>
28
29
static primitives_t* generic = NULL;
30
31
/* ------------------------------------------------------------------------- */
32
/*static inline BOOL memory_regions_overlap_1d(*/
33
static BOOL memory_regions_overlap_1d(const BYTE* p1, const BYTE* p2, size_t bytes)
34
0
{
35
0
  const ULONG_PTR p1m = (const ULONG_PTR)p1;
36
0
  const ULONG_PTR p2m = (const ULONG_PTR)p2;
37
38
0
  if (p1m <= p2m)
39
0
  {
40
0
    if (p1m + bytes > p2m)
41
0
      return TRUE;
42
0
  }
43
0
  else
44
0
  {
45
0
    if (p2m + bytes > p1m)
46
0
      return TRUE;
47
0
  }
48
49
  /* else */
50
0
  return FALSE;
51
0
}
52
53
/* ------------------------------------------------------------------------- */
54
/*static inline BOOL memory_regions_overlap_2d( */
55
static BOOL memory_regions_overlap_2d(const BYTE* p1, int p1Step, int p1Size, const BYTE* p2,
56
                                      int p2Step, int p2Size, int width, int height)
57
0
{
58
0
  ULONG_PTR p1m = (ULONG_PTR)p1;
59
0
  ULONG_PTR p2m = (ULONG_PTR)p2;
60
61
0
  if (p1m <= p2m)
62
0
  {
63
0
    ULONG_PTR p1mEnd = p1m +
64
0
                       1ull * (WINPR_ASSERTING_INT_CAST(uint32_t, height - 1)) *
65
0
                           WINPR_ASSERTING_INT_CAST(uint32_t, p1Step) +
66
0
                       1ull * WINPR_ASSERTING_INT_CAST(uint32_t, width* p1Size);
67
68
0
    if (p1mEnd > p2m)
69
0
      return TRUE;
70
0
  }
71
0
  else
72
0
  {
73
0
    ULONG_PTR p2mEnd = p2m +
74
0
                       1ull * (WINPR_ASSERTING_INT_CAST(uintptr_t, height - 1)) *
75
0
                           WINPR_ASSERTING_INT_CAST(uintptr_t, p2Step) +
76
0
                       1ull * WINPR_ASSERTING_INT_CAST(uintptr_t, width* p2Size);
77
78
0
    if (p2mEnd > p1m)
79
0
      return TRUE;
80
0
  }
81
82
  /* else */
83
0
  return FALSE;
84
0
}
85
86
/* ------------------------------------------------------------------------- */
87
static pstatus_t general_copy_8u(const BYTE* WINPR_RESTRICT pSrc, BYTE* WINPR_RESTRICT pDst,
88
                                 INT32 len)
89
0
{
90
0
  if (memory_regions_overlap_1d(pSrc, pDst, (size_t)len))
91
0
  {
92
0
    memmove((void*)pDst, (const void*)pSrc, (size_t)len);
93
0
  }
94
0
  else
95
0
  {
96
0
    memcpy((void*)pDst, (const void*)pSrc, (size_t)len);
97
0
  }
98
99
0
  return PRIMITIVES_SUCCESS;
100
0
}
101
102
/* ------------------------------------------------------------------------- */
103
/* Copy a block of pixels from one buffer to another.
104
 * The addresses are assumed to have been already offset to the upper-left
105
 * corners of the source and destination region of interest.
106
 */
107
static pstatus_t general_copy_8u_AC4r(const BYTE* WINPR_RESTRICT pSrc, INT32 srcStep,
108
                                      BYTE* WINPR_RESTRICT pDst, INT32 dstStep, INT32 width,
109
                                      INT32 height)
110
0
{
111
0
  const BYTE* src = pSrc;
112
0
  BYTE* dst = pDst;
113
0
  const size_t rowbytes = WINPR_ASSERTING_INT_CAST(size_t, width) * sizeof(UINT32);
114
115
0
  if ((width == 0) || (height == 0))
116
0
    return PRIMITIVES_SUCCESS;
117
118
0
  if (memory_regions_overlap_2d(pSrc, srcStep, sizeof(UINT32), pDst, dstStep, sizeof(UINT32),
119
0
                                width, height))
120
0
  {
121
0
    do
122
0
    {
123
0
      generic->copy(src, dst, WINPR_ASSERTING_INT_CAST(int32_t, rowbytes));
124
0
      src += srcStep;
125
0
      dst += dstStep;
126
0
    } while (--height);
127
0
  }
128
0
  else
129
0
  {
130
    /* TODO: do it in one operation when the rowdata is adjacent. */
131
0
    do
132
0
    {
133
      /* If we find a replacement for memcpy that is consistently
134
       * faster, this could be replaced with that.
135
       */
136
0
      memcpy(dst, src, rowbytes);
137
0
      src += srcStep;
138
0
      dst += dstStep;
139
0
    } while (--height);
140
0
  }
141
142
0
  return PRIMITIVES_SUCCESS;
143
0
}
144
145
static inline pstatus_t generic_image_copy_bgr24_bgrx32(BYTE* WINPR_RESTRICT pDstData,
146
                                                        UINT32 nDstStep, UINT32 nXDst, UINT32 nYDst,
147
                                                        UINT32 nWidth, UINT32 nHeight,
148
                                                        const BYTE* WINPR_RESTRICT pSrcData,
149
                                                        UINT32 nSrcStep, UINT32 nXSrc, UINT32 nYSrc,
150
                                                        int64_t srcVMultiplier, int64_t srcVOffset,
151
                                                        int64_t dstVMultiplier, int64_t dstVOffset)
152
0
{
153
154
0
  const int64_t srcByte = 3;
155
0
  const int64_t dstByte = 4;
156
157
0
  const UINT32 width = nWidth - nWidth % 8;
158
159
0
  for (int64_t y = 0; y < nHeight; y++)
160
0
  {
161
0
    const BYTE* WINPR_RESTRICT srcLine =
162
0
        &pSrcData[srcVMultiplier * (y + nYSrc) * nSrcStep + srcVOffset];
163
0
    BYTE* WINPR_RESTRICT dstLine =
164
0
        &pDstData[dstVMultiplier * (y + nYDst) * nDstStep + dstVOffset];
165
166
0
    int64_t x = 0;
167
0
    WINPR_PRAGMA_UNROLL_LOOP
168
0
    for (; x < width; x++)
169
0
    {
170
0
      dstLine[(x + nXDst) * dstByte + 0] = srcLine[(x + nXSrc) * srcByte + 0];
171
0
      dstLine[(x + nXDst) * dstByte + 1] = srcLine[(x + nXSrc) * srcByte + 1];
172
0
      dstLine[(x + nXDst) * dstByte + 2] = srcLine[(x + nXSrc) * srcByte + 2];
173
0
    }
174
175
0
    for (; x < nWidth; x++)
176
0
    {
177
0
      dstLine[(x + nXDst) * dstByte + 0] = srcLine[(x + nXSrc) * srcByte + 0];
178
0
      dstLine[(x + nXDst) * dstByte + 1] = srcLine[(x + nXSrc) * srcByte + 1];
179
0
      dstLine[(x + nXDst) * dstByte + 2] = srcLine[(x + nXSrc) * srcByte + 2];
180
0
    }
181
0
  }
182
183
0
  return PRIMITIVES_SUCCESS;
184
0
}
185
186
static inline pstatus_t
187
generic_image_copy_bgrx32_bgrx32(BYTE* WINPR_RESTRICT pDstData, UINT32 nDstStep, UINT32 nXDst,
188
                                 UINT32 nYDst, UINT32 nWidth, UINT32 nHeight,
189
                                 const BYTE* WINPR_RESTRICT pSrcData, UINT32 nSrcStep, UINT32 nXSrc,
190
                                 UINT32 nYSrc, int64_t srcVMultiplier, int64_t srcVOffset,
191
                                 int64_t dstVMultiplier, int64_t dstVOffset)
192
0
{
193
194
0
  const int64_t srcByte = 4;
195
0
  const int64_t dstByte = 4;
196
197
0
  const UINT32 width = nWidth - nWidth % 8;
198
199
0
  for (int64_t y = 0; y < nHeight; y++)
200
0
  {
201
0
    const BYTE* WINPR_RESTRICT srcLine =
202
0
        &pSrcData[srcVMultiplier * (y + nYSrc) * nSrcStep + srcVOffset];
203
0
    BYTE* WINPR_RESTRICT dstLine =
204
0
        &pDstData[dstVMultiplier * (y + nYDst) * nDstStep + dstVOffset];
205
206
0
    int64_t x = 0;
207
0
    WINPR_PRAGMA_UNROLL_LOOP
208
0
    for (; x < width; x++)
209
0
    {
210
0
      dstLine[(x + nXDst) * dstByte + 0] = srcLine[(x + nXSrc) * srcByte + 0];
211
0
      dstLine[(x + nXDst) * dstByte + 1] = srcLine[(x + nXSrc) * srcByte + 1];
212
0
      dstLine[(x + nXDst) * dstByte + 2] = srcLine[(x + nXSrc) * srcByte + 2];
213
0
    }
214
0
    for (; x < nWidth; x++)
215
0
    {
216
0
      dstLine[(x + nXDst) * dstByte + 0] = srcLine[(x + nXSrc) * srcByte + 0];
217
0
      dstLine[(x + nXDst) * dstByte + 1] = srcLine[(x + nXSrc) * srcByte + 1];
218
0
      dstLine[(x + nXDst) * dstByte + 2] = srcLine[(x + nXSrc) * srcByte + 2];
219
0
    }
220
0
  }
221
222
0
  return PRIMITIVES_SUCCESS;
223
0
}
224
225
pstatus_t generic_image_copy_no_overlap_convert(
226
    BYTE* WINPR_RESTRICT pDstData, DWORD DstFormat, UINT32 nDstStep, UINT32 nXDst, UINT32 nYDst,
227
    UINT32 nWidth, UINT32 nHeight, const BYTE* WINPR_RESTRICT pSrcData, DWORD SrcFormat,
228
    UINT32 nSrcStep, UINT32 nXSrc, UINT32 nYSrc, const gdiPalette* WINPR_RESTRICT palette,
229
    int64_t srcVMultiplier, int64_t srcVOffset, int64_t dstVMultiplier, int64_t dstVOffset)
230
5.48k
{
231
5.48k
  const int64_t srcByte = FreeRDPGetBytesPerPixel(SrcFormat);
232
5.48k
  const int64_t dstByte = FreeRDPGetBytesPerPixel(DstFormat);
233
234
5.48k
  const UINT32 width = nWidth - nWidth % 8;
235
323k
  for (int64_t y = 0; y < nHeight; y++)
236
317k
  {
237
317k
    const BYTE* WINPR_RESTRICT srcLine =
238
317k
        &pSrcData[srcVMultiplier * (y + nYSrc) * nSrcStep + srcVOffset];
239
317k
    BYTE* WINPR_RESTRICT dstLine =
240
317k
        &pDstData[dstVMultiplier * (y + nYDst) * nDstStep + dstVOffset];
241
242
317k
    int64_t x = 0;
243
    // WINPR_PRAGMA_UNROLL_LOOP
244
20.0M
    for (; x < width; x++)
245
19.7M
    {
246
19.7M
      const UINT32 color = FreeRDPReadColor_int(&srcLine[(x + nXSrc) * srcByte], SrcFormat);
247
19.7M
      const UINT32 dstColor = FreeRDPConvertColor(color, SrcFormat, DstFormat, palette);
248
19.7M
      FreeRDPWriteColor_int(&dstLine[(x + nXDst) * dstByte], DstFormat, dstColor);
249
19.7M
    }
250
324k
    for (; x < nWidth; x++)
251
6.32k
    {
252
6.32k
      const UINT32 color = FreeRDPReadColor_int(&srcLine[(x + nXSrc) * srcByte], SrcFormat);
253
6.32k
      const UINT32 dstColor = FreeRDPConvertColor(color, SrcFormat, DstFormat, palette);
254
6.32k
      FreeRDPWriteColor_int(&dstLine[(x + nXDst) * dstByte], DstFormat, dstColor);
255
6.32k
    }
256
317k
  }
257
5.48k
  return PRIMITIVES_SUCCESS;
258
5.48k
}
259
260
pstatus_t generic_image_copy_no_overlap_memcpy(
261
    BYTE* WINPR_RESTRICT pDstData, DWORD DstFormat, UINT32 nDstStep, UINT32 nXDst, UINT32 nYDst,
262
    UINT32 nWidth, UINT32 nHeight, const BYTE* WINPR_RESTRICT pSrcData, DWORD SrcFormat,
263
    UINT32 nSrcStep, UINT32 nXSrc, UINT32 nYSrc,
264
    WINPR_ATTR_UNUSED const gdiPalette* WINPR_RESTRICT palette, int64_t srcVMultiplier,
265
    int64_t srcVOffset, int64_t dstVMultiplier, int64_t dstVOffset, WINPR_ATTR_UNUSED UINT32 flags)
266
144
{
267
144
  const int64_t dstByte = FreeRDPGetBytesPerPixel(DstFormat);
268
144
  const int64_t srcByte = FreeRDPGetBytesPerPixel(SrcFormat);
269
144
  const int64_t copyDstWidth = nWidth * dstByte;
270
144
  const int64_t xSrcOffset = nXSrc * srcByte;
271
144
  const int64_t xDstOffset = nXDst * dstByte;
272
273
2.84k
  for (int64_t y = 0; y < nHeight; y++)
274
2.69k
  {
275
2.69k
    const BYTE* WINPR_RESTRICT srcLine =
276
2.69k
        &pSrcData[srcVMultiplier * (y + nYSrc) * nSrcStep + srcVOffset];
277
2.69k
    BYTE* WINPR_RESTRICT dstLine =
278
2.69k
        &pDstData[dstVMultiplier * (y + nYDst) * nDstStep + dstVOffset];
279
2.69k
    memcpy(&dstLine[xDstOffset], &srcLine[xSrcOffset],
280
5.39k
           WINPR_ASSERTING_INT_CAST(size_t, copyDstWidth));
281
5.39k
  }
282
283
144
  return PRIMITIVES_SUCCESS;
284
144
}
285
286
static inline pstatus_t generic_image_copy_no_overlap_dst_alpha(
287
    BYTE* WINPR_RESTRICT pDstData, DWORD DstFormat, UINT32 nDstStep, UINT32 nXDst, UINT32 nYDst,
288
    UINT32 nWidth, UINT32 nHeight, const BYTE* WINPR_RESTRICT pSrcData, DWORD SrcFormat,
289
    UINT32 nSrcStep, UINT32 nXSrc, UINT32 nYSrc, const gdiPalette* WINPR_RESTRICT palette,
290
    int64_t srcVMultiplier, int64_t srcVOffset, int64_t dstVMultiplier, int64_t dstVOffset)
291
0
{
292
0
  WINPR_ASSERT(pDstData);
293
0
  WINPR_ASSERT(pSrcData);
294
295
0
  switch (SrcFormat)
296
0
  {
297
0
    case PIXEL_FORMAT_BGR24:
298
0
      switch (DstFormat)
299
0
      {
300
0
        case PIXEL_FORMAT_BGRX32:
301
0
        case PIXEL_FORMAT_BGRA32:
302
0
          return generic_image_copy_bgr24_bgrx32(
303
0
              pDstData, nDstStep, nXDst, nYDst, nWidth, nHeight, pSrcData, nSrcStep,
304
0
              nXSrc, nYSrc, srcVMultiplier, srcVOffset, dstVMultiplier, dstVOffset);
305
0
        default:
306
0
          break;
307
0
      }
308
0
      break;
309
0
    case PIXEL_FORMAT_BGRX32:
310
0
    case PIXEL_FORMAT_BGRA32:
311
0
      switch (DstFormat)
312
0
      {
313
0
        case PIXEL_FORMAT_BGRX32:
314
0
        case PIXEL_FORMAT_BGRA32:
315
0
          return generic_image_copy_bgrx32_bgrx32(
316
0
              pDstData, nDstStep, nXDst, nYDst, nWidth, nHeight, pSrcData, nSrcStep,
317
0
              nXSrc, nYSrc, srcVMultiplier, srcVOffset, dstVMultiplier, dstVOffset);
318
0
        default:
319
0
          break;
320
0
      }
321
0
      break;
322
0
    case PIXEL_FORMAT_RGBX32:
323
0
    case PIXEL_FORMAT_RGBA32:
324
0
      switch (DstFormat)
325
0
      {
326
0
        case PIXEL_FORMAT_RGBX32:
327
0
        case PIXEL_FORMAT_RGBA32:
328
0
          return generic_image_copy_bgrx32_bgrx32(
329
0
              pDstData, nDstStep, nXDst, nYDst, nWidth, nHeight, pSrcData, nSrcStep,
330
0
              nXSrc, nYSrc, srcVMultiplier, srcVOffset, dstVMultiplier, dstVOffset);
331
0
        case PIXEL_FORMAT_RGB24:
332
0
          return generic_image_copy_bgr24_bgrx32(
333
0
              pDstData, nDstStep, nXDst, nYDst, nWidth, nHeight, pSrcData, nSrcStep,
334
0
              nXSrc, nYSrc, srcVMultiplier, srcVOffset, dstVMultiplier, dstVOffset);
335
0
        default:
336
0
          break;
337
0
      }
338
0
      break;
339
0
    default:
340
0
      break;
341
0
  }
342
343
0
  return generic_image_copy_no_overlap_convert(
344
0
      pDstData, DstFormat, nDstStep, nXDst, nYDst, nWidth, nHeight, pSrcData, SrcFormat, nSrcStep,
345
0
      nXSrc, nYSrc, palette, srcVMultiplier, srcVOffset, dstVMultiplier, dstVOffset);
346
0
}
347
348
static inline pstatus_t generic_image_copy_no_overlap_no_alpha(
349
    BYTE* WINPR_RESTRICT pDstData, DWORD DstFormat, UINT32 nDstStep, UINT32 nXDst, UINT32 nYDst,
350
    UINT32 nWidth, UINT32 nHeight, const BYTE* WINPR_RESTRICT pSrcData, DWORD SrcFormat,
351
    UINT32 nSrcStep, UINT32 nXSrc, UINT32 nYSrc, const gdiPalette* WINPR_RESTRICT palette,
352
    int64_t srcVMultiplier, int64_t srcVOffset, int64_t dstVMultiplier, int64_t dstVOffset,
353
    UINT32 flags)
354
5.48k
{
355
5.48k
  if (FreeRDPAreColorFormatsEqualNoAlpha(SrcFormat, DstFormat))
356
0
    return generic_image_copy_no_overlap_memcpy(pDstData, DstFormat, nDstStep, nXDst, nYDst,
357
0
                                                nWidth, nHeight, pSrcData, SrcFormat, nSrcStep,
358
0
                                                nXSrc, nYSrc, palette, srcVMultiplier,
359
0
                                                srcVOffset, dstVMultiplier, dstVOffset, flags);
360
5.48k
  else
361
5.48k
    return generic_image_copy_no_overlap_convert(pDstData, DstFormat, nDstStep, nXDst, nYDst,
362
5.48k
                                                 nWidth, nHeight, pSrcData, SrcFormat, nSrcStep,
363
5.48k
                                                 nXSrc, nYSrc, palette, srcVMultiplier,
364
5.48k
                                                 srcVOffset, dstVMultiplier, dstVOffset);
365
5.48k
}
366
367
static pstatus_t generic_image_copy_no_overlap(BYTE* WINPR_RESTRICT pDstData, DWORD DstFormat,
368
                                               UINT32 nDstStep, UINT32 nXDst, UINT32 nYDst,
369
                                               UINT32 nWidth, UINT32 nHeight,
370
                                               const BYTE* WINPR_RESTRICT pSrcData, DWORD SrcFormat,
371
                                               UINT32 nSrcStep, UINT32 nXSrc, UINT32 nYSrc,
372
                                               const gdiPalette* WINPR_RESTRICT palette,
373
                                               UINT32 flags)
374
5.48k
{
375
5.48k
  const BOOL vSrcVFlip = (flags & FREERDP_FLIP_VERTICAL) ? TRUE : FALSE;
376
5.48k
  int64_t srcVOffset = 0;
377
5.48k
  int64_t srcVMultiplier = 1;
378
5.48k
  int64_t dstVOffset = 0;
379
5.48k
  int64_t dstVMultiplier = 1;
380
381
5.48k
  if ((nWidth == 0) || (nHeight == 0))
382
0
    return PRIMITIVES_SUCCESS;
383
384
5.48k
  if ((nHeight > INT32_MAX) || (nWidth > INT32_MAX))
385
0
    return -1;
386
387
5.48k
  if (!pDstData || !pSrcData)
388
0
    return -1;
389
390
5.48k
  if (nDstStep == 0)
391
749
    nDstStep = nWidth * FreeRDPGetBytesPerPixel(DstFormat);
392
393
5.48k
  if (nSrcStep == 0)
394
0
    nSrcStep = nWidth * FreeRDPGetBytesPerPixel(SrcFormat);
395
396
5.48k
  if (vSrcVFlip)
397
4.42k
  {
398
4.42k
    srcVOffset = (nHeight - 1ll) * nSrcStep;
399
4.42k
    srcVMultiplier = -1;
400
4.42k
  }
401
402
5.48k
  if (((flags & FREERDP_KEEP_DST_ALPHA) != 0) && FreeRDPColorHasAlpha(DstFormat))
403
0
    return generic_image_copy_no_overlap_dst_alpha(
404
0
        pDstData, DstFormat, nDstStep, nXDst, nYDst, nWidth, nHeight, pSrcData, SrcFormat,
405
0
        nSrcStep, nXSrc, nYSrc, palette, srcVMultiplier, srcVOffset, dstVMultiplier,
406
0
        dstVOffset);
407
5.48k
  else
408
5.48k
    return generic_image_copy_no_overlap_no_alpha(
409
5.48k
        pDstData, DstFormat, nDstStep, nXDst, nYDst, nWidth, nHeight, pSrcData, SrcFormat,
410
5.48k
        nSrcStep, nXSrc, nYSrc, palette, srcVMultiplier, srcVOffset, dstVMultiplier, dstVOffset,
411
5.48k
        flags);
412
413
0
  return PRIMITIVES_SUCCESS;
414
5.48k
}
415
416
/* ------------------------------------------------------------------------- */
417
void primitives_init_copy(primitives_t* WINPR_RESTRICT prims)
418
3
{
419
  /* Start with the default. */
420
3
  prims->copy_8u = general_copy_8u;
421
3
  prims->copy_8u_AC4r = general_copy_8u_AC4r;
422
3
  prims->copy = WINPR_FUNC_PTR_CAST(prims->copy_8u, fn_copy_t);
423
3
  prims->copy_no_overlap = generic_image_copy_no_overlap;
424
3
}
425
426
void primitives_init_copy_opt(primitives_t* WINPR_RESTRICT prims)
427
1
{
428
1
  primitives_init_copy(prims);
429
1
  primitives_init_copy_sse41(prims);
430
1
#if defined(WITH_AVX2)
431
1
  primitives_init_copy_avx2(prims);
432
1
#endif
433
1
}