FreeRDP
Loading...
Searching...
No Matches
TestPrimitivesCopy.c
1/* test_copy.c
2 * vi:ts=4 sw=4
3 *
4 * (c) Copyright 2012 Hewlett-Packard Development Company, L.P.
5 * Licensed under the Apache License, Version 2.0 (the "License"); you may
6 * not use this file except in compliance with the License. You may obtain
7 * a copy of the License at http://www.apache.org/licenses/LICENSE-2.0.
8 * Unless required by applicable law or agreed to in writing, software
9 * distributed under the License is distributed on an "AS IS" BASIS,
10 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
11 * or implied. See the License for the specific language governing
12 * permissions and limitations under the License.
13 */
14
15#include <stdio.h>
16
17#include <freerdp/config.h>
18#include <winpr/crypto.h>
19
20#include <winpr/sysinfo.h>
21#include "prim_test.h"
22
23#define COPY_TESTSIZE (256 * 2 + 16 * 2 + 15 + 15)
24
25/* ------------------------------------------------------------------------- */
26static BOOL test_copy8u_func(void)
27{
28 primitives_t* prims = primitives_get();
29 BYTE data[COPY_TESTSIZE + 15] = WINPR_C_ARRAY_INIT;
30 if (winpr_RAND(data, sizeof(data)) < 0)
31 return FALSE;
32
33 for (int soff = 0; soff < 16; ++soff)
34 {
35 for (int doff = 0; doff < 16; ++doff)
36 {
37 for (int length = 1; length <= COPY_TESTSIZE - doff; ++length)
38 {
39 BYTE dest[COPY_TESTSIZE + 15] = WINPR_C_ARRAY_INIT;
40
41 if (prims->copy_8u(data + soff, dest + doff, length) != PRIMITIVES_SUCCESS)
42 return FALSE;
43
44 for (int i = 0; i < length; ++i)
45 {
46 if (dest[i + doff] != data[i + soff])
47 {
48 printf("COPY8U FAIL: off=%d len=%d, dest[%d]=0x%02" PRIx8 ""
49 "data[%d]=0x%02" PRIx8 "\n",
50 doff, length, i + doff, dest[i + doff], i + soff, data[i + soff]);
51 return FALSE;
52 }
53 }
54 }
55 }
56 }
57
58 return TRUE;
59}
60
61/* ------------------------------------------------------------------------- */
62static BOOL test_copy8u_speed(void)
63{
64 BYTE src[MAX_TEST_SIZE + 4] = WINPR_C_ARRAY_INIT;
65 BYTE dst[MAX_TEST_SIZE + 4] = WINPR_C_ARRAY_INIT;
66
67 if (!speed_test("copy_8u", "aligned", g_Iterations, (speed_test_fkt)generic->copy_8u,
68 (speed_test_fkt)optimized->copy_8u, src, dst, MAX_TEST_SIZE))
69 return FALSE;
70
71 if (!speed_test("copy_8u", "unaligned", g_Iterations, (speed_test_fkt)generic->copy_8u,
72 (speed_test_fkt)optimized->copy_8u, src + 1, dst + 1, MAX_TEST_SIZE))
73 return FALSE;
74
75 return TRUE;
76}
77
78static BYTE* rand_alloc(size_t w, size_t h, size_t bpp, size_t pad, BYTE** copy)
79{
80 const size_t s = w * bpp + pad;
81 BYTE* ptr = calloc(s, h);
82 if (!ptr)
83 return nullptr;
84
85 if (winpr_RAND(ptr, s * h) < 0)
86 {
87 free(ptr);
88 return nullptr;
89 }
90
91 if (copy)
92 {
93 BYTE* ptr2 = calloc(s, h);
94 if (!ptr2)
95 {
96 free(ptr);
97 return nullptr;
98 }
99 memcpy(ptr2, ptr, s * h);
100 *copy = ptr2;
101 }
102 return ptr;
103}
104
105static size_t runcount = 0;
106
107static BOOL test_copy_no_overlap_off(BOOL verbose, UINT32 srcFormat, UINT32 dstFormat, UINT32 flags,
108 UINT32 pad, UINT32 w, UINT32 h, UINT32 dxoff, UINT32 dyoff,
109 UINT32 sxoff, UINT32 syoff)
110{
111 BOOL rc = FALSE;
112 primitives_t* gen = primitives_get_generic();
113 primitives_t* prims = primitives_get();
114 if (!gen || !prims)
115 return FALSE;
116
117 runcount++;
118
119 WINPR_ASSERT(dxoff < w);
120 WINPR_ASSERT(sxoff < w);
121 WINPR_ASSERT(dyoff < h);
122 WINPR_ASSERT(syoff < h);
123
124 const UINT32 sbpp = FreeRDPGetBytesPerPixel(srcFormat);
125 const UINT32 dbpp = FreeRDPGetBytesPerPixel(dstFormat);
126
127 if (verbose)
128 {
129 (void)fprintf(stderr,
130 "run src: %s, dst: %s [flags 0x%08" PRIx32 "] %" PRIu32 "x%" PRIu32
131 ", soff=%" PRIu32 "x%" PRIu32 ", doff=%" PRIu32 "x%" PRIu32 ", pad=%" PRIu32
132 "\n",
133 FreeRDPGetColorFormatName(srcFormat), FreeRDPGetColorFormatName(dstFormat),
134 flags, w, h, sxoff, syoff, dxoff, dyoff, pad);
135 }
136
137 const UINT32 sstride = (w + sxoff) * sbpp + pad;
138 const UINT32 dstride = (w + dxoff) * dbpp + pad;
139 BYTE* dst2 = nullptr;
140 BYTE* src2 = nullptr;
141 BYTE* dst1 = rand_alloc(w + dxoff, h + dyoff, dbpp, pad, &dst2);
142 BYTE* src1 = rand_alloc(w + sxoff, h + syoff, sbpp, pad, &src2);
143 if (!dst1 || !dst2 || !src1 || !src2)
144 goto fail;
145
146 if (gen->copy_no_overlap(dst1, dstFormat, dstride, dxoff, dyoff, w, h, src1, srcFormat, sstride,
147 sxoff, syoff, nullptr, flags) != PRIMITIVES_SUCCESS)
148 goto fail;
149
150 if (memcmp(src1, src2, 1ULL * sstride * h) != 0)
151 goto fail;
152
153 if (prims->copy_no_overlap(dst2, dstFormat, dstride, dxoff, dyoff, w, h, src1, srcFormat,
154 sstride, sxoff, syoff, nullptr, flags) != PRIMITIVES_SUCCESS)
155 goto fail;
156
157 if (memcmp(src1, src2, 1ULL * sstride * h) != 0)
158 goto fail;
159
160 if (memcmp(dst1, dst2, 1ULL * dstride * h) != 0)
161 goto fail;
162
163 if (flags == FREERDP_KEEP_DST_ALPHA)
164 {
165 for (size_t y = 0; y < h; y++)
166 {
167 const BYTE* d1 = &dst1[(y + dyoff) * dstride];
168 const BYTE* d2 = &dst2[(y + dyoff) * dstride];
169 for (size_t x = 0; x < w; x++)
170 {
171 const UINT32 c1 = FreeRDPReadColor(&d1[(x + dxoff) * dbpp], dstFormat);
172 const UINT32 c2 = FreeRDPReadColor(&d2[(x + dxoff) * dbpp], dstFormat);
173 BYTE a1 = 0;
174 BYTE a2 = 0;
175 FreeRDPSplitColor(c1, dstFormat, nullptr, nullptr, nullptr, &a1, nullptr);
176 FreeRDPSplitColor(c2, dstFormat, nullptr, nullptr, nullptr, &a2, nullptr);
177 if (a1 != a2)
178 goto fail;
179 }
180 }
181 }
182 rc = TRUE;
183
184fail:
185 if (!rc)
186 {
187 (void)fprintf(stderr, "failed to compare copy_no_overlap(%s -> %s [0x%08" PRIx32 "])\n",
188 FreeRDPGetColorFormatName(srcFormat), FreeRDPGetColorFormatName(dstFormat),
189 flags);
190 }
191 free(dst1);
192 free(dst2);
193 free(src1);
194 free(src2);
195 return rc;
196}
197
198static BOOL test_copy_no_overlap(BOOL verbose, UINT32 srcFormat, UINT32 dstFormat, UINT32 flags,
199 UINT32 width, UINT32 height)
200{
201 BOOL rc = TRUE;
202 const UINT32 mw = 4;
203 const UINT32 mh = 4;
204 for (UINT32 dxoff = 0; dxoff < mw; dxoff++)
205 {
206 for (UINT32 dyoff = 0; dyoff <= mh; dyoff++)
207 {
208 for (UINT32 sxoff = 0; sxoff <= mw; sxoff++)
209 {
210 for (UINT32 syoff = 0; syoff <= mh; syoff++)
211 {
212 /* We need minimum alignment of 8 bytes.
213 * AVX2 can read 8 pixels (at most 8x4=32 bytes) per step
214 * if we have 24bpp input that is 24 bytes with 8 bytes read
215 * out of bound */
216 for (UINT32 pad = 8; pad <= 12; pad++)
217 {
218 if (!test_copy_no_overlap_off(verbose, srcFormat, dstFormat, flags, pad,
219 width, height, dxoff, dyoff, sxoff, syoff))
220 rc = FALSE;
221 }
222 }
223 }
224 }
225 }
226
227 return rc;
228}
229
230static BOOL test_copy_32_to_24_off(UINT32 srcFormat, UINT32 dstFormat, UINT32 flags, UINT32 w,
231 UINT32 h, UINT32 dxoff, UINT32 dyoff, UINT32 sxoff, UINT32 syoff)
232{
233 BOOL rc = FALSE;
234 const BYTE canary = 0xA5;
235 const UINT32 pad = 5;
236 const UINT32 dmargin = 2;
237 const UINT32 sbpp = FreeRDPGetBytesPerPixel(srcFormat);
238 const UINT32 dbpp = FreeRDPGetBytesPerPixel(dstFormat);
239 const UINT32 sstride = (w + sxoff) * sbpp + pad;
240 const UINT32 dstride = (w + dxoff + dmargin) * dbpp + pad;
241 const size_t ssize = 1ull * sstride * (h + syoff);
242 const size_t dsize = 1ull * dstride * (h + dyoff + dmargin);
243 const BOOL vflip = (flags & FREERDP_FLIP_VERTICAL) != 0;
244
245 primitives_t* gen = primitives_get_generic();
246 primitives_t* prims = primitives_get();
247 BYTE* src = calloc(1, ssize);
248 BYTE* expected = calloc(1, dsize);
249 BYTE* dst = calloc(1, dsize);
250 if (!gen || !prims || !src || !expected || !dst)
251 goto fail;
252
253 if (winpr_RAND(src, ssize) < 0)
254 goto fail;
255
256 memset(expected, canary, dsize);
257 for (size_t y = 0; y < h; y++)
258 {
259 /* A vertical flip reads the source rows bottom up */
260 const size_t sy = vflip ? (h - 1 - y - syoff) : (y + syoff);
261 const BYTE* sline = &src[sy * sstride];
262 BYTE* dline = &expected[(y + dyoff) * dstride];
263 for (size_t x = 0; x < w; x++)
264 {
265 const UINT32 color = FreeRDPReadColor(&sline[(x + sxoff) * sbpp], srcFormat);
266 const UINT32 dcolor = FreeRDPConvertColor(color, srcFormat, dstFormat, nullptr);
267 if (!FreeRDPWriteColor(&dline[(x + dxoff) * dbpp], dstFormat, dcolor))
268 goto fail;
269 }
270 }
271
272 for (size_t i = 0; i < 3; i++)
273 {
274 memset(dst, canary, dsize);
275 switch (i)
276 {
277 case 0:
278 if (!freerdp_image_copy_no_overlap(dst, dstFormat, dstride, dxoff, dyoff, w, h, src,
279 srcFormat, sstride, sxoff, syoff, nullptr,
280 flags))
281 goto fail;
282 break;
283 case 1:
284 if (gen->copy_no_overlap(dst, dstFormat, dstride, dxoff, dyoff, w, h, src,
285 srcFormat, sstride, sxoff, syoff, nullptr,
286 flags) != PRIMITIVES_SUCCESS)
287 goto fail;
288 break;
289 default:
290 if (prims->copy_no_overlap(dst, dstFormat, dstride, dxoff, dyoff, w, h, src,
291 srcFormat, sstride, sxoff, syoff, nullptr,
292 flags) != PRIMITIVES_SUCCESS)
293 goto fail;
294 break;
295 }
296
297 if (memcmp(dst, expected, dsize) != 0)
298 goto fail;
299 }
300
301 rc = TRUE;
302
303fail:
304 if (!rc)
305 {
306 (void)fprintf(stderr,
307 "failed copy_no_overlap(%s -> %s [0x%08" PRIx32 "]) %" PRIu32 "x%" PRIu32
308 ", soff=%" PRIu32 "x%" PRIu32 ", doff=%" PRIu32 "x%" PRIu32 "\n",
309 FreeRDPGetColorFormatName(srcFormat), FreeRDPGetColorFormatName(dstFormat),
310 flags, w, h, sxoff, syoff, dxoff, dyoff);
311 }
312 free(src);
313 free(expected);
314 free(dst);
315 return rc;
316}
317
318static BOOL test_copy_32_to_24(void)
319{
320 const UINT32 pairs[][2] = {
321 { PIXEL_FORMAT_BGRX32, PIXEL_FORMAT_RGB24 }, { PIXEL_FORMAT_BGRA32, PIXEL_FORMAT_RGB24 },
322 { PIXEL_FORMAT_RGBX32, PIXEL_FORMAT_BGR24 }, { PIXEL_FORMAT_RGBA32, PIXEL_FORMAT_BGR24 },
323 { PIXEL_FORMAT_BGRX32, PIXEL_FORMAT_BGR24 }, { PIXEL_FORMAT_BGRA32, PIXEL_FORMAT_BGR24 },
324 { PIXEL_FORMAT_RGBX32, PIXEL_FORMAT_RGB24 }, { PIXEL_FORMAT_RGBA32, PIXEL_FORMAT_RGB24 },
325 };
326 const UINT32 flags[] = { FREERDP_FLIP_NONE, FREERDP_FLIP_VERTICAL };
327 const UINT32 widths[] = { 1, 7, 8, 15, 17, 63 };
328 const UINT32 h = 5;
329 BOOL rc = TRUE;
330
331 for (size_t p = 0; p < ARRAYSIZE(pairs); p++)
332 {
333 for (size_t f = 0; f < ARRAYSIZE(flags); f++)
334 {
335 for (size_t i = 0; i < ARRAYSIZE(widths); i++)
336 {
337 for (UINT32 off = 0; off < 3; off++)
338 {
339 /* With a vertical flip the source rows are counted from the bottom of
340 * the copied rect, so only a zero source y offset stays in bounds */
341 const UINT32 syoff = (flags[f] & FREERDP_FLIP_VERTICAL) ? 0 : off + 1;
342 if (!test_copy_32_to_24_off(pairs[p][0], pairs[p][1], flags[f], widths[i], h,
343 off * 3, off + 2, off * 2 + 1, syoff))
344 rc = FALSE;
345 }
346 }
347 }
348 }
349 return rc;
350}
351
352int TestPrimitivesCopy(int argc, char* argv[])
353{
354 WINPR_UNUSED(argc);
355 WINPR_UNUSED(argv);
356
357 const BOOL verbose = argc > 1;
358
359 prim_test_setup(FALSE);
360
361 if (!test_copy8u_func())
362 return 1;
363
364 if (g_TestPrimitivesPerformance)
365 {
366 if (!test_copy8u_speed())
367 return 1;
368 }
369
370 const UINT32 flags[] = {
371 FREERDP_FLIP_NONE,
372 FREERDP_KEEP_DST_ALPHA,
373 FREERDP_FLIP_HORIZONTAL,
374 FREERDP_KEEP_DST_ALPHA | FREERDP_FLIP_HORIZONTAL,
375#if defined(TEST_ALL_FLAGS)
376 FREERDP_FLIP_VERTICAL,
377 FREERDP_FLIP_VERTICAL | FREERDP_FLIP_HORIZONTAL,
378 FREERDP_KEEP_DST_ALPHA | FREERDP_FLIP_VERTICAL,
379 FREERDP_KEEP_DST_ALPHA | FREERDP_FLIP_VERTICAL | FREERDP_FLIP_HORIZONTAL
380#endif
381 };
382 const UINT32 formats[] = { PIXEL_FORMAT_BGRA32,
383 PIXEL_FORMAT_BGRX32,
384 PIXEL_FORMAT_BGR24
385#if defined(TEST_ALL_FLAGS) /* Only the previous 3 have SIMD optimizations, so skip the rest */
386 ,
387 PIXEL_FORMAT_RGB24,
388 PIXEL_FORMAT_ABGR32,
389 PIXEL_FORMAT_ARGB32,
390 PIXEL_FORMAT_XBGR32,
391 PIXEL_FORMAT_XRGB32,
392 PIXEL_FORMAT_RGBA32,
393 PIXEL_FORMAT_RGBX32
394#endif
395 };
396
397 int rc = 0;
398 if (!test_copy_32_to_24())
399 rc = -1;
400
401 for (size_t z = 0; z < ARRAYSIZE(flags); z++)
402 {
403 const UINT32 flag = flags[z];
404 for (size_t x = 0; x < ARRAYSIZE(formats); x++)
405 {
406 const UINT32 sformat = formats[x];
407 for (size_t y = 0; y < ARRAYSIZE(formats); y++)
408 {
409 const UINT32 dformat = formats[y];
410
411 if (!test_copy_no_overlap(verbose, sformat, dformat, flag, 21, 17))
412 rc = -1;
413 }
414 }
415 }
416
417 if (verbose)
418 (void)fprintf(stderr, "runcount=%" PRIuz "\n", runcount);
419
420 return rc;
421}
WINPR_ATTR_NODISCARD fn_copy_no_overlap_t copy_no_overlap
Definition primitives.h:304