FreeRDP
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
30 static 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 /* ------------------------------------------------------------------------- */
46 static 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 /* ------------------------------------------------------------------------- */
64 static 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 /* ------------------------------------------------------------------------- */
92 static 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 
117 static BOOL test_alphaComp_func(void)
118 {
119  pstatus_t status = 0;
120  BYTE ALIGN(src1[SRC1_WIDTH * SRC1_HEIGHT * 4]) = { 0 };
121  BYTE ALIGN(src2[SRC2_WIDTH * SRC2_HEIGHT * 4]) = { 0 };
122  BYTE ALIGN(dst1[DST_WIDTH * DST_HEIGHT * 4]) = { 0 };
123  UINT32* ptr = NULL;
124  winpr_RAND(src1, sizeof(src1));
125  /* Special-case the first two values */
126  src1[0] &= 0x00FFFFFFU;
127  src1[1] |= 0xFF000000U;
128  winpr_RAND(src2, sizeof(src2));
129  /* Set the second operand to fully-opaque. */
130  ptr = (UINT32*)src2;
131 
132  for (UINT32 i = 0; i < sizeof(src2) / 4; ++i)
133  *ptr++ |= 0xFF000000U;
134 
135  status = generic->alphaComp_argb(src1, 4 * SRC1_WIDTH, src2, 4 * SRC2_WIDTH, dst1,
136  4 * DST_WIDTH, TEST_WIDTH, TEST_HEIGHT);
137 
138  if (status != PRIMITIVES_SUCCESS)
139  return FALSE;
140 
141  if (!check(src1, 4 * SRC1_WIDTH, src2, 4 * SRC2_WIDTH, dst1, 4 * DST_WIDTH, TEST_WIDTH,
142  TEST_HEIGHT))
143  return FALSE;
144 
145  status = optimized->alphaComp_argb((const BYTE*)src1, 4 * SRC1_WIDTH, (const BYTE*)src2,
146  4 * SRC2_WIDTH, (BYTE*)dst1, 4 * DST_WIDTH, TEST_WIDTH,
147  TEST_HEIGHT);
148 
149  if (status != PRIMITIVES_SUCCESS)
150  return FALSE;
151 
152  if (!check(src1, 4 * SRC1_WIDTH, src2, 4 * SRC2_WIDTH, dst1, 4 * DST_WIDTH, TEST_WIDTH,
153  TEST_HEIGHT))
154  return FALSE;
155 
156  return TRUE;
157 }
158 
159 static int test_alphaComp_speed(void)
160 {
161  BYTE ALIGN(src1[SRC1_WIDTH * SRC1_HEIGHT]) = { 0 };
162  BYTE ALIGN(src2[SRC2_WIDTH * SRC2_HEIGHT]) = { 0 };
163  BYTE ALIGN(dst1[DST_WIDTH * DST_HEIGHT]) = { 0 };
164  UINT32* ptr = NULL;
165 
166  winpr_RAND(src1, sizeof(src1));
167  /* Special-case the first two values */
168  src1[0] &= 0x00FFFFFFU;
169  src1[1] |= 0xFF000000U;
170  winpr_RAND(src2, sizeof(src2));
171  /* Set the second operand to fully-opaque. */
172  ptr = (UINT32*)src2;
173 
174  for (UINT32 i = 0; i < sizeof(src2) / 4; ++i)
175  *ptr++ |= 0xFF000000U;
176 
177  if (!speed_test("add16s", "aligned", g_Iterations, (speed_test_fkt)generic->alphaComp_argb,
178  (speed_test_fkt)optimized->alphaComp_argb, src1, 4 * SRC1_WIDTH, src2,
179  4 * SRC2_WIDTH, dst1, 4 * DST_WIDTH, TEST_WIDTH, TEST_HEIGHT))
180  return FALSE;
181 
182  return TRUE;
183 }
184 
185 int TestPrimitivesAlphaComp(int argc, char* argv[])
186 {
187  WINPR_UNUSED(argc);
188  WINPR_UNUSED(argv);
189 
190  prim_test_setup(FALSE);
191 
192  if (!test_alphaComp_func())
193  return -1;
194 
195  if (g_TestPrimitivesPerformance)
196  {
197  if (!test_alphaComp_speed())
198  return -1;
199  }
200 
201  return 0;
202 }