FreeRDP
Loading...
Searching...
No Matches
TestPrimitivesAlphaComp.c
1/* test_alphaComp.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 <freerdp/config.h>
16
17#include <winpr/sysinfo.h>
18
19#include "prim_test.h"
20
21#define MAX_BLOCK_SIZE 256
22#define SIZE_SQUARED (MAX_BLOCK_SIZE * MAX_BLOCK_SIZE)
23
24/* ========================================================================= */
25#define ALF(_c_) (((_c_)&0xFF000000U) >> 24)
26#define RED(_c_) (((_c_)&0x00FF0000U) >> 16)
27#define GRN(_c_) (((_c_)&0x0000FF00U) >> 8)
28#define BLU(_c_) ((_c_)&0x000000FFU)
29#define TOLERANCE 1
30static inline const UINT32* PIXEL(const BYTE* _addr_, UINT32 _bytes_, UINT32 _x_, UINT32 _y_)
31{
32 const BYTE* addr = _addr_ + 1ULL * _x_ * sizeof(UINT32) + 1ULL * _y_ * _bytes_;
33 return (const UINT32*)addr;
34}
35
36#define SRC1_WIDTH 6
37#define SRC1_HEIGHT 6
38#define SRC2_WIDTH 7
39#define SRC2_HEIGHT 7
40#define DST_WIDTH 9
41#define DST_HEIGHT 9
42#define TEST_WIDTH 4
43#define TEST_HEIGHT 5
44
45/* ------------------------------------------------------------------------- */
46static UINT32 alpha_add(UINT32 c1, UINT32 c2)
47{
48 UINT32 a1 = ALF(c1);
49 UINT32 r1 = RED(c1);
50 UINT32 g1 = GRN(c1);
51 UINT32 b1 = BLU(c1);
52 UINT32 a2 = ALF(c2);
53 UINT32 r2 = RED(c2);
54 UINT32 g2 = GRN(c2);
55 UINT32 b2 = BLU(c2);
56 UINT32 a3 = ((a1 * a1 + (255 - a1) * a2) / 255) & 0xff;
57 UINT32 r3 = ((a1 * r1 + (255 - a1) * r2) / 255) & 0xff;
58 UINT32 g3 = ((a1 * g1 + (255 - a1) * g2) / 255) & 0xff;
59 UINT32 b3 = ((a1 * b1 + (255 - a1) * b2) / 255) & 0xff;
60 return (a3 << 24) | (r3 << 16) | (g3 << 8) | b3;
61}
62
63/* ------------------------------------------------------------------------- */
64static UINT32 colordist(UINT32 c1, UINT32 c2)
65{
66 int d = 0;
67 int maxd = 0;
68 d = ABS((INT32)(ALF(c1) - ALF(c2)));
69
70 if (d > maxd)
71 maxd = d;
72
73 d = ABS((INT32)(RED(c1) - RED(c2)));
74
75 if (d > maxd)
76 maxd = d;
77
78 d = ABS((INT32)(GRN(c1) - GRN(c2)));
79
80 if (d > maxd)
81 maxd = d;
82
83 d = ABS((INT32)(BLU(c1) - BLU(c2)));
84
85 if (d > maxd)
86 maxd = d;
87
88 return maxd;
89}
90
91/* ------------------------------------------------------------------------- */
92static BOOL check(const BYTE* pSrc1, UINT32 src1Step, const BYTE* pSrc2, UINT32 src2Step,
93 BYTE* pDst, UINT32 dstStep, UINT32 width, UINT32 height)
94{
95 for (UINT32 y = 0; y < height; ++y)
96 {
97 for (UINT32 x = 0; x < width; ++x)
98 {
99 UINT32 s1 = *PIXEL(pSrc1, src1Step, x, y);
100 UINT32 s2 = *PIXEL(pSrc2, src2Step, x, y);
101 UINT32 c0 = alpha_add(s1, s2);
102 UINT32 c1 = *PIXEL(pDst, dstStep, x, y);
103
104 if (colordist(c0, c1) > TOLERANCE)
105 {
106 printf("alphaComp-general: [%" PRIu32 ",%" PRIu32 "] 0x%08" PRIx32 "+0x%08" PRIx32
107 "=0x%08" PRIx32 ", got 0x%08" PRIx32 "\n",
108 x, y, s1, s2, c0, c1);
109 return FALSE;
110 }
111 }
112 }
113
114 return TRUE;
115}
116
117static BOOL test_alphaComp_func(void)
118{
119 pstatus_t status = 0;
120 BYTE src1[SRC1_WIDTH * SRC1_HEIGHT * 4] = WINPR_C_ARRAY_INIT;
121 BYTE src2[SRC2_WIDTH * SRC2_HEIGHT * 4] = WINPR_C_ARRAY_INIT;
122 BYTE dst1[DST_WIDTH * DST_HEIGHT * 4] = WINPR_C_ARRAY_INIT;
123 UINT32* ptr = nullptr;
124 if (winpr_RAND(src1, sizeof(src1)) < 0)
125 return FALSE;
126 /* Special-case the first two values */
127 src1[0] &= 0x00FFFFFFU;
128 src1[1] |= 0xFF000000U;
129 if (winpr_RAND(src2, sizeof(src2)) < 0)
130 return FALSE;
131 /* Set the second operand to fully-opaque. */
132 ptr = (UINT32*)src2;
133
134 for (UINT32 i = 0; i < sizeof(src2) / 4; ++i)
135 *ptr++ |= 0xFF000000U;
136
137 status = generic->alphaComp_argb(src1, 4 * SRC1_WIDTH, src2, 4 * SRC2_WIDTH, dst1,
138 4 * DST_WIDTH, TEST_WIDTH, TEST_HEIGHT);
139
140 if (status != PRIMITIVES_SUCCESS)
141 return FALSE;
142
143 if (!check(src1, 4 * SRC1_WIDTH, src2, 4 * SRC2_WIDTH, dst1, 4 * DST_WIDTH, TEST_WIDTH,
144 TEST_HEIGHT))
145 return FALSE;
146
147 status = optimized->alphaComp_argb((const BYTE*)src1, 4 * SRC1_WIDTH, (const BYTE*)src2,
148 4 * SRC2_WIDTH, (BYTE*)dst1, 4 * DST_WIDTH, TEST_WIDTH,
149 TEST_HEIGHT);
150
151 if (status != PRIMITIVES_SUCCESS)
152 return FALSE;
153
154 if (!check(src1, 4 * SRC1_WIDTH, src2, 4 * SRC2_WIDTH, dst1, 4 * DST_WIDTH, TEST_WIDTH,
155 TEST_HEIGHT))
156 return FALSE;
157
158 return TRUE;
159}
160
161static int test_alphaComp_speed(void)
162{
163 BYTE src1[SRC1_WIDTH * SRC1_HEIGHT] = WINPR_C_ARRAY_INIT;
164 BYTE src2[SRC2_WIDTH * SRC2_HEIGHT] = WINPR_C_ARRAY_INIT;
165 BYTE dst1[DST_WIDTH * DST_HEIGHT] = WINPR_C_ARRAY_INIT;
166 UINT32* ptr = nullptr;
167
168 if (winpr_RAND(src1, sizeof(src1)) < 0)
169 return -1;
170 /* Special-case the first two values */
171 src1[0] &= 0x00FFFFFFU;
172 src1[1] |= 0xFF000000U;
173 if (winpr_RAND(src2, sizeof(src2)) < 0)
174 return -1;
175 /* Set the second operand to fully-opaque. */
176 ptr = (UINT32*)src2;
177
178 for (UINT32 i = 0; i < sizeof(src2) / 4; ++i)
179 *ptr++ |= 0xFF000000U;
180
181 return (speed_test("add16s", "aligned", g_Iterations, (speed_test_fkt)generic->alphaComp_argb,
182 (speed_test_fkt)optimized->alphaComp_argb, src1, 4 * SRC1_WIDTH, src2,
183 4 * SRC2_WIDTH, dst1, 4 * DST_WIDTH, TEST_WIDTH, TEST_HEIGHT));
184}
185
186int TestPrimitivesAlphaComp(int argc, char* argv[])
187{
188 WINPR_UNUSED(argc);
189 WINPR_UNUSED(argv);
190
191 prim_test_setup(FALSE);
192
193 if (!test_alphaComp_func())
194 return -1;
195
196 if (g_TestPrimitivesPerformance)
197 {
198 if (!test_alphaComp_speed())
199 return -1;
200 }
201
202 return 0;
203}