16#include <freerdp/config.h>
19#include <freerdp/types.h>
20#include <freerdp/primitives.h>
21#include <freerdp/log.h>
23#include "prim_internal.h"
25#include "../codec/color.h"
27#include <freerdp/codec/color.h>
33static BOOL memory_regions_overlap_1d(
const BYTE* p1,
const BYTE* p2,
size_t bytes)
35 const ULONG_PTR p1m = (
const ULONG_PTR)p1;
36 const ULONG_PTR p2m = (
const ULONG_PTR)p2;
40 if (p1m + bytes > p2m)
45 if (p2m + bytes > p1m)
55static BOOL memory_regions_overlap_2d(
const BYTE* p1,
int p1Step,
int p1Size,
const BYTE* p2,
56 int p2Step,
int p2Size,
int width,
int height)
58 ULONG_PTR p1m = (ULONG_PTR)p1;
59 ULONG_PTR p2m = (ULONG_PTR)p2;
63 ULONG_PTR p1mEnd = p1m +
64 1ull * (WINPR_ASSERTING_INT_CAST(uint32_t, height - 1)) *
65 WINPR_ASSERTING_INT_CAST(uint32_t, p1Step) +
66 1ull * WINPR_ASSERTING_INT_CAST(uint32_t, width* p1Size);
73 ULONG_PTR p2mEnd = p2m +
74 1ull * (WINPR_ASSERTING_INT_CAST(uintptr_t, height - 1)) *
75 WINPR_ASSERTING_INT_CAST(uintptr_t, p2Step) +
76 1ull * WINPR_ASSERTING_INT_CAST(uintptr_t, width* p2Size);
87static pstatus_t general_copy_8u(
const BYTE* WINPR_RESTRICT pSrc, BYTE* WINPR_RESTRICT pDst,
90 if (memory_regions_overlap_1d(pSrc, pDst, (
size_t)len))
92 memmove((
void*)pDst, (
const void*)pSrc, (
size_t)len);
96 memcpy((
void*)pDst, (
const void*)pSrc, (
size_t)len);
99 return PRIMITIVES_SUCCESS;
107static pstatus_t general_copy_8u_AC4r(
const BYTE* WINPR_RESTRICT pSrc, INT32 srcStep,
108 BYTE* WINPR_RESTRICT pDst, INT32 dstStep, INT32 width,
111 const BYTE* src = pSrc;
113 const size_t rowbytes = WINPR_ASSERTING_INT_CAST(
size_t, width) *
sizeof(UINT32);
115 if ((width == 0) || (height == 0))
116 return PRIMITIVES_SUCCESS;
118 if (memory_regions_overlap_2d(pSrc, srcStep,
sizeof(UINT32), pDst, dstStep,
sizeof(UINT32),
124 generic->copy(src, dst, WINPR_ASSERTING_INT_CAST(int32_t, rowbytes));
125 if (rc != PRIMITIVES_SUCCESS)
140 memcpy(dst, src, rowbytes);
146 return PRIMITIVES_SUCCESS;
149static inline pstatus_t generic_image_copy_bgr24_bgrx32(BYTE* WINPR_RESTRICT pDstData,
150 UINT32 nDstStep, UINT32 nXDst, UINT32 nYDst,
151 UINT32 nWidth, UINT32 nHeight,
152 const BYTE* WINPR_RESTRICT pSrcData,
153 UINT32 nSrcStep, UINT32 nXSrc, UINT32 nYSrc,
154 int64_t srcVMultiplier, int64_t srcVOffset,
155 int64_t dstVMultiplier, int64_t dstVOffset)
158 const int64_t srcByte = 3;
159 const int64_t dstByte = 4;
161 const UINT32 width = nWidth - nWidth % 8;
163 for (int64_t y = 0; y < nHeight; y++)
165 const BYTE* WINPR_RESTRICT srcLine =
166 &pSrcData[srcVMultiplier * (y + nYSrc) * nSrcStep + srcVOffset];
167 BYTE* WINPR_RESTRICT dstLine =
168 &pDstData[dstVMultiplier * (y + nYDst) * nDstStep + dstVOffset];
171 WINPR_PRAGMA_UNROLL_LOOP
172 for (; x < width; x++)
174 dstLine[(x + nXDst) * dstByte + 0] = srcLine[(x + nXSrc) * srcByte + 0];
175 dstLine[(x + nXDst) * dstByte + 1] = srcLine[(x + nXSrc) * srcByte + 1];
176 dstLine[(x + nXDst) * dstByte + 2] = srcLine[(x + nXSrc) * srcByte + 2];
179 for (; x < nWidth; x++)
181 dstLine[(x + nXDst) * dstByte + 0] = srcLine[(x + nXSrc) * srcByte + 0];
182 dstLine[(x + nXDst) * dstByte + 1] = srcLine[(x + nXSrc) * srcByte + 1];
183 dstLine[(x + nXDst) * dstByte + 2] = srcLine[(x + nXSrc) * srcByte + 2];
187 return PRIMITIVES_SUCCESS;
190static inline pstatus_t
191generic_image_copy_bgrx32_bgrx32(BYTE* WINPR_RESTRICT pDstData, UINT32 nDstStep, UINT32 nXDst,
192 UINT32 nYDst, UINT32 nWidth, UINT32 nHeight,
193 const BYTE* WINPR_RESTRICT pSrcData, UINT32 nSrcStep, UINT32 nXSrc,
194 UINT32 nYSrc, int64_t srcVMultiplier, int64_t srcVOffset,
195 int64_t dstVMultiplier, int64_t dstVOffset)
198 const int64_t srcByte = 4;
199 const int64_t dstByte = 4;
201 const UINT32 width = nWidth - nWidth % 8;
203 for (int64_t y = 0; y < nHeight; y++)
205 const BYTE* WINPR_RESTRICT srcLine =
206 &pSrcData[srcVMultiplier * (y + nYSrc) * nSrcStep + srcVOffset];
207 BYTE* WINPR_RESTRICT dstLine =
208 &pDstData[dstVMultiplier * (y + nYDst) * nDstStep + dstVOffset];
211 WINPR_PRAGMA_UNROLL_LOOP
212 for (; x < width; x++)
214 dstLine[(x + nXDst) * dstByte + 0] = srcLine[(x + nXSrc) * srcByte + 0];
215 dstLine[(x + nXDst) * dstByte + 1] = srcLine[(x + nXSrc) * srcByte + 1];
216 dstLine[(x + nXDst) * dstByte + 2] = srcLine[(x + nXSrc) * srcByte + 2];
218 for (; x < nWidth; x++)
220 dstLine[(x + nXDst) * dstByte + 0] = srcLine[(x + nXSrc) * srcByte + 0];
221 dstLine[(x + nXDst) * dstByte + 1] = srcLine[(x + nXSrc) * srcByte + 1];
222 dstLine[(x + nXDst) * dstByte + 2] = srcLine[(x + nXSrc) * srcByte + 2];
226 return PRIMITIVES_SUCCESS;
229pstatus_t generic_image_copy_no_overlap_convert(
230 BYTE* WINPR_RESTRICT pDstData, DWORD DstFormat, UINT32 nDstStep, UINT32 nXDst, UINT32 nYDst,
231 UINT32 nWidth, UINT32 nHeight,
const BYTE* WINPR_RESTRICT pSrcData, DWORD SrcFormat,
232 UINT32 nSrcStep, UINT32 nXSrc, UINT32 nYSrc,
const gdiPalette* WINPR_RESTRICT palette,
233 int64_t srcVMultiplier, int64_t srcVOffset, int64_t dstVMultiplier, int64_t dstVOffset)
235 const int64_t srcByte = FreeRDPGetBytesPerPixel(SrcFormat);
236 const int64_t dstByte = FreeRDPGetBytesPerPixel(DstFormat);
238 const UINT32 width = nWidth - nWidth % 8;
239 for (int64_t y = 0; y < nHeight; y++)
241 const BYTE* WINPR_RESTRICT srcLine =
242 &pSrcData[srcVMultiplier * (y + nYSrc) * nSrcStep + srcVOffset];
243 BYTE* WINPR_RESTRICT dstLine =
244 &pDstData[dstVMultiplier * (y + nYDst) * nDstStep + dstVOffset];
248 for (; x < width; x++)
250 const UINT32 color = FreeRDPReadColor_int(&srcLine[(x + nXSrc) * srcByte], SrcFormat);
251 const UINT32 dstColor = FreeRDPConvertColor(color, SrcFormat, DstFormat, palette);
252 if (!FreeRDPWriteColor_int(&dstLine[(x + nXDst) * dstByte], DstFormat, dstColor))
255 for (; x < nWidth; x++)
257 const UINT32 color = FreeRDPReadColor_int(&srcLine[(x + nXSrc) * srcByte], SrcFormat);
258 const UINT32 dstColor = FreeRDPConvertColor(color, SrcFormat, DstFormat, palette);
259 if (!FreeRDPWriteColor_int(&dstLine[(x + nXDst) * dstByte], DstFormat, dstColor))
263 return PRIMITIVES_SUCCESS;
266pstatus_t generic_image_copy_no_overlap_memcpy(
267 BYTE* WINPR_RESTRICT pDstData, DWORD DstFormat, UINT32 nDstStep, UINT32 nXDst, UINT32 nYDst,
268 UINT32 nWidth, UINT32 nHeight,
const BYTE* WINPR_RESTRICT pSrcData, DWORD SrcFormat,
269 UINT32 nSrcStep, UINT32 nXSrc, UINT32 nYSrc,
270 WINPR_ATTR_UNUSED
const gdiPalette* WINPR_RESTRICT palette, int64_t srcVMultiplier,
271 int64_t srcVOffset, int64_t dstVMultiplier, int64_t dstVOffset, WINPR_ATTR_UNUSED UINT32 flags)
273 const int64_t dstByte = FreeRDPGetBytesPerPixel(DstFormat);
274 const int64_t srcByte = FreeRDPGetBytesPerPixel(SrcFormat);
275 const int64_t copyDstWidth = nWidth * dstByte;
276 const int64_t xSrcOffset = nXSrc * srcByte;
277 const int64_t xDstOffset = nXDst * dstByte;
279 for (int64_t y = 0; y < nHeight; y++)
281 const BYTE* WINPR_RESTRICT srcLine =
282 &pSrcData[srcVMultiplier * (y + nYSrc) * nSrcStep + srcVOffset];
283 BYTE* WINPR_RESTRICT dstLine =
284 &pDstData[dstVMultiplier * (y + nYDst) * nDstStep + dstVOffset];
285 memcpy(&dstLine[xDstOffset], &srcLine[xSrcOffset],
286 WINPR_ASSERTING_INT_CAST(
size_t, copyDstWidth));
289 return PRIMITIVES_SUCCESS;
292static inline pstatus_t generic_image_copy_no_overlap_dst_alpha(
293 BYTE* WINPR_RESTRICT pDstData, DWORD DstFormat, UINT32 nDstStep, UINT32 nXDst, UINT32 nYDst,
294 UINT32 nWidth, UINT32 nHeight,
const BYTE* WINPR_RESTRICT pSrcData, DWORD SrcFormat,
295 UINT32 nSrcStep, UINT32 nXSrc, UINT32 nYSrc,
const gdiPalette* WINPR_RESTRICT palette,
296 int64_t srcVMultiplier, int64_t srcVOffset, int64_t dstVMultiplier, int64_t dstVOffset)
298 WINPR_ASSERT(pDstData);
299 WINPR_ASSERT(pSrcData);
303 case PIXEL_FORMAT_BGR24:
306 case PIXEL_FORMAT_BGRX32:
307 case PIXEL_FORMAT_BGRA32:
308 return generic_image_copy_bgr24_bgrx32(
309 pDstData, nDstStep, nXDst, nYDst, nWidth, nHeight, pSrcData, nSrcStep,
310 nXSrc, nYSrc, srcVMultiplier, srcVOffset, dstVMultiplier, dstVOffset);
315 case PIXEL_FORMAT_BGRX32:
316 case PIXEL_FORMAT_BGRA32:
319 case PIXEL_FORMAT_BGRX32:
320 case PIXEL_FORMAT_BGRA32:
321 return generic_image_copy_bgrx32_bgrx32(
322 pDstData, nDstStep, nXDst, nYDst, nWidth, nHeight, pSrcData, nSrcStep,
323 nXSrc, nYSrc, srcVMultiplier, srcVOffset, dstVMultiplier, dstVOffset);
328 case PIXEL_FORMAT_RGBX32:
329 case PIXEL_FORMAT_RGBA32:
332 case PIXEL_FORMAT_RGBX32:
333 case PIXEL_FORMAT_RGBA32:
334 return generic_image_copy_bgrx32_bgrx32(
335 pDstData, nDstStep, nXDst, nYDst, nWidth, nHeight, pSrcData, nSrcStep,
336 nXSrc, nYSrc, srcVMultiplier, srcVOffset, dstVMultiplier, dstVOffset);
337 case PIXEL_FORMAT_RGB24:
338 return generic_image_copy_bgr24_bgrx32(
339 pDstData, nDstStep, nXDst, nYDst, nWidth, nHeight, pSrcData, nSrcStep,
340 nXSrc, nYSrc, srcVMultiplier, srcVOffset, dstVMultiplier, dstVOffset);
349 return generic_image_copy_no_overlap_convert(
350 pDstData, DstFormat, nDstStep, nXDst, nYDst, nWidth, nHeight, pSrcData, SrcFormat, nSrcStep,
351 nXSrc, nYSrc, palette, srcVMultiplier, srcVOffset, dstVMultiplier, dstVOffset);
354static inline pstatus_t generic_image_copy_no_overlap_no_alpha(
355 BYTE* WINPR_RESTRICT pDstData, DWORD DstFormat, UINT32 nDstStep, UINT32 nXDst, UINT32 nYDst,
356 UINT32 nWidth, UINT32 nHeight,
const BYTE* WINPR_RESTRICT pSrcData, DWORD SrcFormat,
357 UINT32 nSrcStep, UINT32 nXSrc, UINT32 nYSrc,
const gdiPalette* WINPR_RESTRICT palette,
358 int64_t srcVMultiplier, int64_t srcVOffset, int64_t dstVMultiplier, int64_t dstVOffset,
361 if (FreeRDPAreColorFormatsEqualNoAlpha(SrcFormat, DstFormat))
362 return generic_image_copy_no_overlap_memcpy(pDstData, DstFormat, nDstStep, nXDst, nYDst,
363 nWidth, nHeight, pSrcData, SrcFormat, nSrcStep,
364 nXSrc, nYSrc, palette, srcVMultiplier,
365 srcVOffset, dstVMultiplier, dstVOffset, flags);
367 return generic_image_copy_no_overlap_convert(pDstData, DstFormat, nDstStep, nXDst, nYDst,
368 nWidth, nHeight, pSrcData, SrcFormat, nSrcStep,
369 nXSrc, nYSrc, palette, srcVMultiplier,
370 srcVOffset, dstVMultiplier, dstVOffset);
373static pstatus_t generic_image_copy_no_overlap(BYTE* WINPR_RESTRICT pDstData, DWORD DstFormat,
374 UINT32 nDstStep, UINT32 nXDst, UINT32 nYDst,
375 UINT32 nWidth, UINT32 nHeight,
376 const BYTE* WINPR_RESTRICT pSrcData, DWORD SrcFormat,
377 UINT32 nSrcStep, UINT32 nXSrc, UINT32 nYSrc,
378 const gdiPalette* WINPR_RESTRICT palette,
381 const BOOL vSrcVFlip = (flags & FREERDP_FLIP_VERTICAL) != 0;
382 int64_t srcVOffset = 0;
383 int64_t srcVMultiplier = 1;
384 int64_t dstVOffset = 0;
385 int64_t dstVMultiplier = 1;
387 if ((nWidth == 0) || (nHeight == 0))
388 return PRIMITIVES_SUCCESS;
390 if ((nHeight > INT32_MAX) || (nWidth > INT32_MAX))
393 if (!pDstData || !pSrcData)
397 nDstStep = nWidth * FreeRDPGetBytesPerPixel(DstFormat);
400 nSrcStep = nWidth * FreeRDPGetBytesPerPixel(SrcFormat);
404 srcVOffset = (nHeight - 1ll) * nSrcStep;
408 if (((flags & FREERDP_KEEP_DST_ALPHA) != 0) && FreeRDPColorHasAlpha(DstFormat))
409 return generic_image_copy_no_overlap_dst_alpha(
410 pDstData, DstFormat, nDstStep, nXDst, nYDst, nWidth, nHeight, pSrcData, SrcFormat,
411 nSrcStep, nXSrc, nYSrc, palette, srcVMultiplier, srcVOffset, dstVMultiplier,
414 return generic_image_copy_no_overlap_no_alpha(
415 pDstData, DstFormat, nDstStep, nXDst, nYDst, nWidth, nHeight, pSrcData, SrcFormat,
416 nSrcStep, nXSrc, nYSrc, palette, srcVMultiplier, srcVOffset, dstVMultiplier, dstVOffset,
419 return PRIMITIVES_SUCCESS;
423void primitives_init_copy(
primitives_t* WINPR_RESTRICT prims)
426 prims->copy_8u = general_copy_8u;
427 prims->copy_8u_AC4r = general_copy_8u_AC4r;
428 prims->copy = WINPR_FUNC_PTR_CAST(prims->copy_8u, fn_copy_t);
429 prims->copy_no_overlap = generic_image_copy_no_overlap;
432void primitives_init_copy_opt(
primitives_t* WINPR_RESTRICT prims)
434 primitives_init_copy(prims);
435 primitives_init_copy_sse41(prims);
436#if defined(WITH_AVX2)
437 primitives_init_copy_avx2(prims);