FreeRDP
Loading...
Searching...
No Matches
prim_copy.c
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
29static primitives_t* generic = NULL;
30
31/* ------------------------------------------------------------------------- */
32/*static inline BOOL memory_regions_overlap_1d(*/
33static BOOL memory_regions_overlap_1d(const BYTE* p1, const BYTE* p2, size_t bytes)
34{
35 const ULONG_PTR p1m = (const ULONG_PTR)p1;
36 const ULONG_PTR p2m = (const ULONG_PTR)p2;
37
38 if (p1m <= p2m)
39 {
40 if (p1m + bytes > p2m)
41 return TRUE;
42 }
43 else
44 {
45 if (p2m + bytes > p1m)
46 return TRUE;
47 }
48
49 /* else */
50 return FALSE;
51}
52
53/* ------------------------------------------------------------------------- */
54/*static inline BOOL memory_regions_overlap_2d( */
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)
57{
58 ULONG_PTR p1m = (ULONG_PTR)p1;
59 ULONG_PTR p2m = (ULONG_PTR)p2;
60
61 if (p1m <= p2m)
62 {
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);
67
68 if (p1mEnd > p2m)
69 return TRUE;
70 }
71 else
72 {
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);
77
78 if (p2mEnd > p1m)
79 return TRUE;
80 }
81
82 /* else */
83 return FALSE;
84}
85
86/* ------------------------------------------------------------------------- */
87static pstatus_t general_copy_8u(const BYTE* WINPR_RESTRICT pSrc, BYTE* WINPR_RESTRICT pDst,
88 INT32 len)
89{
90 if (memory_regions_overlap_1d(pSrc, pDst, (size_t)len))
91 {
92 memmove((void*)pDst, (const void*)pSrc, (size_t)len);
93 }
94 else
95 {
96 memcpy((void*)pDst, (const void*)pSrc, (size_t)len);
97 }
98
99 return PRIMITIVES_SUCCESS;
100}
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 */
107static 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{
111 const BYTE* src = pSrc;
112 BYTE* dst = pDst;
113 const size_t rowbytes = WINPR_ASSERTING_INT_CAST(size_t, width) * sizeof(UINT32);
114
115 if ((width == 0) || (height == 0))
116 return PRIMITIVES_SUCCESS;
117
118 if (memory_regions_overlap_2d(pSrc, srcStep, sizeof(UINT32), pDst, dstStep, sizeof(UINT32),
119 width, height))
120 {
121 do
122 {
123 generic->copy(src, dst, WINPR_ASSERTING_INT_CAST(int32_t, rowbytes));
124 src += srcStep;
125 dst += dstStep;
126 } while (--height);
127 }
128 else
129 {
130 /* TODO: do it in one operation when the rowdata is adjacent. */
131 do
132 {
133 /* If we find a replacement for memcpy that is consistently
134 * faster, this could be replaced with that.
135 */
136 memcpy(dst, src, rowbytes);
137 src += srcStep;
138 dst += dstStep;
139 } while (--height);
140 }
141
142 return PRIMITIVES_SUCCESS;
143}
144
145static 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{
153
154 const int64_t srcByte = 3;
155 const int64_t dstByte = 4;
156
157 const UINT32 width = nWidth - nWidth % 8;
158
159 for (int64_t y = 0; y < nHeight; y++)
160 {
161 const BYTE* WINPR_RESTRICT srcLine =
162 &pSrcData[srcVMultiplier * (y + nYSrc) * nSrcStep + srcVOffset];
163 BYTE* WINPR_RESTRICT dstLine =
164 &pDstData[dstVMultiplier * (y + nYDst) * nDstStep + dstVOffset];
165
166 int64_t x = 0;
167 WINPR_PRAGMA_UNROLL_LOOP
168 for (; x < width; x++)
169 {
170 dstLine[(x + nXDst) * dstByte + 0] = srcLine[(x + nXSrc) * srcByte + 0];
171 dstLine[(x + nXDst) * dstByte + 1] = srcLine[(x + nXSrc) * srcByte + 1];
172 dstLine[(x + nXDst) * dstByte + 2] = srcLine[(x + nXSrc) * srcByte + 2];
173 }
174
175 for (; x < nWidth; x++)
176 {
177 dstLine[(x + nXDst) * dstByte + 0] = srcLine[(x + nXSrc) * srcByte + 0];
178 dstLine[(x + nXDst) * dstByte + 1] = srcLine[(x + nXSrc) * srcByte + 1];
179 dstLine[(x + nXDst) * dstByte + 2] = srcLine[(x + nXSrc) * srcByte + 2];
180 }
181 }
182
183 return PRIMITIVES_SUCCESS;
184}
185
186static INLINE pstatus_t generic_image_copy_bgrx32_bgrx32(
187 BYTE* WINPR_RESTRICT pDstData, UINT32 nDstStep, UINT32 nXDst, UINT32 nYDst, UINT32 nWidth,
188 UINT32 nHeight, const BYTE* WINPR_RESTRICT pSrcData, UINT32 nSrcStep, UINT32 nXSrc,
189 UINT32 nYSrc, int64_t srcVMultiplier, int64_t srcVOffset, int64_t dstVMultiplier,
190 int64_t dstVOffset)
191{
192
193 const int64_t srcByte = 4;
194 const int64_t dstByte = 4;
195
196 const UINT32 width = nWidth - nWidth % 8;
197
198 for (int64_t y = 0; y < nHeight; y++)
199 {
200 const BYTE* WINPR_RESTRICT srcLine =
201 &pSrcData[srcVMultiplier * (y + nYSrc) * nSrcStep + srcVOffset];
202 BYTE* WINPR_RESTRICT dstLine =
203 &pDstData[dstVMultiplier * (y + nYDst) * nDstStep + dstVOffset];
204
205 int64_t x = 0;
206 WINPR_PRAGMA_UNROLL_LOOP
207 for (; x < width; x++)
208 {
209 dstLine[(x + nXDst) * dstByte + 0] = srcLine[(x + nXSrc) * srcByte + 0];
210 dstLine[(x + nXDst) * dstByte + 1] = srcLine[(x + nXSrc) * srcByte + 1];
211 dstLine[(x + nXDst) * dstByte + 2] = srcLine[(x + nXSrc) * srcByte + 2];
212 }
213 for (; x < nWidth; x++)
214 {
215 dstLine[(x + nXDst) * dstByte + 0] = srcLine[(x + nXSrc) * srcByte + 0];
216 dstLine[(x + nXDst) * dstByte + 1] = srcLine[(x + nXSrc) * srcByte + 1];
217 dstLine[(x + nXDst) * dstByte + 2] = srcLine[(x + nXSrc) * srcByte + 2];
218 }
219 }
220
221 return PRIMITIVES_SUCCESS;
222}
223
224pstatus_t generic_image_copy_no_overlap_convert(
225 BYTE* WINPR_RESTRICT pDstData, DWORD DstFormat, UINT32 nDstStep, UINT32 nXDst, UINT32 nYDst,
226 UINT32 nWidth, UINT32 nHeight, const BYTE* WINPR_RESTRICT pSrcData, DWORD SrcFormat,
227 UINT32 nSrcStep, UINT32 nXSrc, UINT32 nYSrc, const gdiPalette* WINPR_RESTRICT palette,
228 int64_t srcVMultiplier, int64_t srcVOffset, int64_t dstVMultiplier, int64_t dstVOffset)
229{
230 const int64_t srcByte = FreeRDPGetBytesPerPixel(SrcFormat);
231 const int64_t dstByte = FreeRDPGetBytesPerPixel(DstFormat);
232
233 const UINT32 width = nWidth - nWidth % 8;
234 for (int64_t y = 0; y < nHeight; y++)
235 {
236 const BYTE* WINPR_RESTRICT srcLine =
237 &pSrcData[srcVMultiplier * (y + nYSrc) * nSrcStep + srcVOffset];
238 BYTE* WINPR_RESTRICT dstLine =
239 &pDstData[dstVMultiplier * (y + nYDst) * nDstStep + dstVOffset];
240
241 int64_t x = 0;
242 // WINPR_PRAGMA_UNROLL_LOOP
243 for (; x < width; x++)
244 {
245 const UINT32 color = FreeRDPReadColor_int(&srcLine[(x + nXSrc) * srcByte], SrcFormat);
246 const UINT32 dstColor = FreeRDPConvertColor(color, SrcFormat, DstFormat, palette);
247 FreeRDPWriteColor_int(&dstLine[(x + nXDst) * dstByte], DstFormat, dstColor);
248 }
249 for (; x < nWidth; x++)
250 {
251 const UINT32 color = FreeRDPReadColor_int(&srcLine[(x + nXSrc) * srcByte], SrcFormat);
252 const UINT32 dstColor = FreeRDPConvertColor(color, SrcFormat, DstFormat, palette);
253 FreeRDPWriteColor_int(&dstLine[(x + nXDst) * dstByte], DstFormat, dstColor);
254 }
255 }
256 return PRIMITIVES_SUCCESS;
257}
258
259pstatus_t generic_image_copy_no_overlap_memcpy(
260 BYTE* WINPR_RESTRICT pDstData, DWORD DstFormat, UINT32 nDstStep, UINT32 nXDst, UINT32 nYDst,
261 UINT32 nWidth, UINT32 nHeight, const BYTE* WINPR_RESTRICT pSrcData, DWORD SrcFormat,
262 UINT32 nSrcStep, UINT32 nXSrc, UINT32 nYSrc,
263 WINPR_ATTR_UNUSED const gdiPalette* WINPR_RESTRICT palette, int64_t srcVMultiplier,
264 int64_t srcVOffset, int64_t dstVMultiplier, int64_t dstVOffset, WINPR_ATTR_UNUSED UINT32 flags)
265{
266 const int64_t dstByte = FreeRDPGetBytesPerPixel(DstFormat);
267 const int64_t srcByte = FreeRDPGetBytesPerPixel(SrcFormat);
268 const int64_t copyDstWidth = nWidth * dstByte;
269 const int64_t xSrcOffset = nXSrc * srcByte;
270 const int64_t xDstOffset = nXDst * dstByte;
271
272 for (int64_t y = 0; y < nHeight; y++)
273 {
274 const BYTE* WINPR_RESTRICT srcLine =
275 &pSrcData[srcVMultiplier * (y + nYSrc) * nSrcStep + srcVOffset];
276 BYTE* WINPR_RESTRICT dstLine =
277 &pDstData[dstVMultiplier * (y + nYDst) * nDstStep + dstVOffset];
278 memcpy(&dstLine[xDstOffset], &srcLine[xSrcOffset],
279 WINPR_ASSERTING_INT_CAST(size_t, copyDstWidth));
280 }
281
282 return PRIMITIVES_SUCCESS;
283}
284
285static INLINE pstatus_t generic_image_copy_no_overlap_dst_alpha(
286 BYTE* WINPR_RESTRICT pDstData, DWORD DstFormat, UINT32 nDstStep, UINT32 nXDst, UINT32 nYDst,
287 UINT32 nWidth, UINT32 nHeight, const BYTE* WINPR_RESTRICT pSrcData, DWORD SrcFormat,
288 UINT32 nSrcStep, UINT32 nXSrc, UINT32 nYSrc, const gdiPalette* WINPR_RESTRICT palette,
289 int64_t srcVMultiplier, int64_t srcVOffset, int64_t dstVMultiplier, int64_t dstVOffset)
290{
291 WINPR_ASSERT(pDstData);
292 WINPR_ASSERT(pSrcData);
293
294 switch (SrcFormat)
295 {
296 case PIXEL_FORMAT_BGR24:
297 switch (DstFormat)
298 {
299 case PIXEL_FORMAT_BGRX32:
300 case PIXEL_FORMAT_BGRA32:
301 return generic_image_copy_bgr24_bgrx32(
302 pDstData, nDstStep, nXDst, nYDst, nWidth, nHeight, pSrcData, nSrcStep,
303 nXSrc, nYSrc, srcVMultiplier, srcVOffset, dstVMultiplier, dstVOffset);
304 default:
305 break;
306 }
307 break;
308 case PIXEL_FORMAT_BGRX32:
309 case PIXEL_FORMAT_BGRA32:
310 switch (DstFormat)
311 {
312 case PIXEL_FORMAT_BGRX32:
313 case PIXEL_FORMAT_BGRA32:
314 return generic_image_copy_bgrx32_bgrx32(
315 pDstData, nDstStep, nXDst, nYDst, nWidth, nHeight, pSrcData, nSrcStep,
316 nXSrc, nYSrc, srcVMultiplier, srcVOffset, dstVMultiplier, dstVOffset);
317 default:
318 break;
319 }
320 break;
321 case PIXEL_FORMAT_RGBX32:
322 case PIXEL_FORMAT_RGBA32:
323 switch (DstFormat)
324 {
325 case PIXEL_FORMAT_RGBX32:
326 case PIXEL_FORMAT_RGBA32:
327 return generic_image_copy_bgrx32_bgrx32(
328 pDstData, nDstStep, nXDst, nYDst, nWidth, nHeight, pSrcData, nSrcStep,
329 nXSrc, nYSrc, srcVMultiplier, srcVOffset, dstVMultiplier, dstVOffset);
330 case PIXEL_FORMAT_RGB24:
331 return generic_image_copy_bgr24_bgrx32(
332 pDstData, nDstStep, nXDst, nYDst, nWidth, nHeight, pSrcData, nSrcStep,
333 nXSrc, nYSrc, srcVMultiplier, srcVOffset, dstVMultiplier, dstVOffset);
334 default:
335 break;
336 }
337 break;
338 default:
339 break;
340 }
341
342 return generic_image_copy_no_overlap_convert(
343 pDstData, DstFormat, nDstStep, nXDst, nYDst, nWidth, nHeight, pSrcData, SrcFormat, nSrcStep,
344 nXSrc, nYSrc, palette, srcVMultiplier, srcVOffset, dstVMultiplier, dstVOffset);
345}
346
347static INLINE pstatus_t generic_image_copy_no_overlap_no_alpha(
348 BYTE* WINPR_RESTRICT pDstData, DWORD DstFormat, UINT32 nDstStep, UINT32 nXDst, UINT32 nYDst,
349 UINT32 nWidth, UINT32 nHeight, const BYTE* WINPR_RESTRICT pSrcData, DWORD SrcFormat,
350 UINT32 nSrcStep, UINT32 nXSrc, UINT32 nYSrc, const gdiPalette* WINPR_RESTRICT palette,
351 int64_t srcVMultiplier, int64_t srcVOffset, int64_t dstVMultiplier, int64_t dstVOffset,
352 UINT32 flags)
353{
354 if (FreeRDPAreColorFormatsEqualNoAlpha(SrcFormat, DstFormat))
355 return generic_image_copy_no_overlap_memcpy(pDstData, DstFormat, nDstStep, nXDst, nYDst,
356 nWidth, nHeight, pSrcData, SrcFormat, nSrcStep,
357 nXSrc, nYSrc, palette, srcVMultiplier,
358 srcVOffset, dstVMultiplier, dstVOffset, flags);
359 else
360 return generic_image_copy_no_overlap_convert(pDstData, DstFormat, nDstStep, nXDst, nYDst,
361 nWidth, nHeight, pSrcData, SrcFormat, nSrcStep,
362 nXSrc, nYSrc, palette, srcVMultiplier,
363 srcVOffset, dstVMultiplier, dstVOffset);
364}
365
366static pstatus_t generic_image_copy_no_overlap(BYTE* WINPR_RESTRICT pDstData, DWORD DstFormat,
367 UINT32 nDstStep, UINT32 nXDst, UINT32 nYDst,
368 UINT32 nWidth, UINT32 nHeight,
369 const BYTE* WINPR_RESTRICT pSrcData, DWORD SrcFormat,
370 UINT32 nSrcStep, UINT32 nXSrc, UINT32 nYSrc,
371 const gdiPalette* WINPR_RESTRICT palette,
372 UINT32 flags)
373{
374 const BOOL vSrcVFlip = (flags & FREERDP_FLIP_VERTICAL) ? TRUE : FALSE;
375 int64_t srcVOffset = 0;
376 int64_t srcVMultiplier = 1;
377 int64_t dstVOffset = 0;
378 int64_t dstVMultiplier = 1;
379
380 if ((nWidth == 0) || (nHeight == 0))
381 return PRIMITIVES_SUCCESS;
382
383 if ((nHeight > INT32_MAX) || (nWidth > INT32_MAX))
384 return -1;
385
386 if (!pDstData || !pSrcData)
387 return -1;
388
389 if (nDstStep == 0)
390 nDstStep = nWidth * FreeRDPGetBytesPerPixel(DstFormat);
391
392 if (nSrcStep == 0)
393 nSrcStep = nWidth * FreeRDPGetBytesPerPixel(SrcFormat);
394
395 if (vSrcVFlip)
396 {
397 srcVOffset = (nHeight - 1ll) * nSrcStep;
398 srcVMultiplier = -1;
399 }
400
401 if (((flags & FREERDP_KEEP_DST_ALPHA) != 0) && FreeRDPColorHasAlpha(DstFormat))
402 return generic_image_copy_no_overlap_dst_alpha(
403 pDstData, DstFormat, nDstStep, nXDst, nYDst, nWidth, nHeight, pSrcData, SrcFormat,
404 nSrcStep, nXSrc, nYSrc, palette, srcVMultiplier, srcVOffset, dstVMultiplier,
405 dstVOffset);
406 else
407 return generic_image_copy_no_overlap_no_alpha(
408 pDstData, DstFormat, nDstStep, nXDst, nYDst, nWidth, nHeight, pSrcData, SrcFormat,
409 nSrcStep, nXSrc, nYSrc, palette, srcVMultiplier, srcVOffset, dstVMultiplier, dstVOffset,
410 flags);
411
412 return PRIMITIVES_SUCCESS;
413}
414
415/* ------------------------------------------------------------------------- */
416void primitives_init_copy(primitives_t* WINPR_RESTRICT prims)
417{
418 /* Start with the default. */
419 prims->copy_8u = general_copy_8u;
420 prims->copy_8u_AC4r = general_copy_8u_AC4r;
421 prims->copy = WINPR_FUNC_PTR_CAST(prims->copy_8u, fn_copy_t);
422 prims->copy_no_overlap = generic_image_copy_no_overlap;
423}
424
425void primitives_init_copy_opt(primitives_t* WINPR_RESTRICT prims)
426{
427 primitives_init_copy(prims);
428 primitives_init_copy_sse41(prims);
429#if defined(WITH_AVX2)
430 primitives_init_copy_avx2(prims);
431#endif
432}