FreeRDP
Loading...
Searching...
No Matches
prim_internal.h
1/* prim_internal.h
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. Algorithms used by
13 * this code may be covered by patents by HP, Microsoft, or other parties.
14 *
15 */
16
17#ifndef FREERDP_LIB_PRIM_INTERNAL_H
18#define FREERDP_LIB_PRIM_INTERNAL_H
19
20#include <winpr/platform.h>
21#include <freerdp/config.h>
22
23#include <freerdp/primitives.h>
24#include <freerdp/api.h>
25
26#include "../core/simd.h"
27
28#ifdef __GNUC__
29#define PRIM_ALIGN_128 __attribute__((aligned(16)))
30#else
31#ifdef _WIN32
32#define PRIM_ALIGN_128 __declspec(align(16))
33#endif
34#endif
35
36#if defined(SSE_AVX_INTRINSICS_ENABLED) || defined(NEON_INTRINSICS_ENABLED) || defined(WITH_OPENCL)
37#define HAVE_OPTIMIZED_PRIMITIVES 1
38#endif
39
40#if defined(SSE_AVX_INTRINSICS_ENABLED) || defined(NEON_INTRINSICS_ENABLED)
41#define HAVE_CPU_OPTIMIZED_PRIMITIVES 1
42#endif
43
44static INLINE BYTE* writePixelBGRA(BYTE* dst, DWORD formatSize, UINT32 format, BYTE R, BYTE G,
45 BYTE B, BYTE A)
46{
47 WINPR_UNUSED(formatSize);
48 WINPR_UNUSED(format);
49
50 *dst++ = B;
51 *dst++ = G;
52 *dst++ = R;
53 *dst++ = A;
54 return dst;
55}
56
57static INLINE BYTE* writePixelBGRX(BYTE* dst, DWORD formatSize, UINT32 format, BYTE R, BYTE G,
58 BYTE B, BYTE A)
59{
60 WINPR_UNUSED(formatSize);
61 WINPR_UNUSED(format);
62 WINPR_UNUSED(A);
63
64 *dst++ = B;
65 *dst++ = G;
66 *dst++ = R;
67 dst++; /* Do not touch alpha */
68
69 return dst;
70}
71
72static INLINE BYTE* writePixelRGBA(BYTE* dst, DWORD formatSize, UINT32 format, BYTE R, BYTE G,
73 BYTE B, BYTE A)
74{
75 WINPR_UNUSED(formatSize);
76 WINPR_UNUSED(format);
77
78 *dst++ = R;
79 *dst++ = G;
80 *dst++ = B;
81 *dst++ = A;
82 return dst;
83}
84
85static INLINE BYTE* writePixelRGBX(BYTE* dst, DWORD formatSize, UINT32 format, BYTE R, BYTE G,
86 BYTE B, BYTE A)
87{
88 WINPR_UNUSED(formatSize);
89 WINPR_UNUSED(format);
90 WINPR_UNUSED(A);
91
92 *dst++ = R;
93 *dst++ = G;
94 *dst++ = B;
95 dst++; /* Do not touch alpha */
96
97 return dst;
98}
99
100static INLINE BYTE* writePixelABGR(BYTE* dst, DWORD formatSize, UINT32 format, BYTE R, BYTE G,
101 BYTE B, BYTE A)
102{
103 WINPR_UNUSED(formatSize);
104 WINPR_UNUSED(format);
105
106 *dst++ = A;
107 *dst++ = B;
108 *dst++ = G;
109 *dst++ = R;
110 return dst;
111}
112
113static INLINE BYTE* writePixelXBGR(BYTE* dst, DWORD formatSize, UINT32 format, BYTE R, BYTE G,
114 BYTE B, BYTE A)
115{
116 WINPR_UNUSED(formatSize);
117 WINPR_UNUSED(format);
118 WINPR_UNUSED(A);
119
120 dst++; /* Do not touch alpha */
121 *dst++ = B;
122 *dst++ = G;
123 *dst++ = R;
124 return dst;
125}
126
127static INLINE BYTE* writePixelARGB(BYTE* dst, DWORD formatSize, UINT32 format, BYTE R, BYTE G,
128 BYTE B, BYTE A)
129{
130 WINPR_UNUSED(formatSize);
131 WINPR_UNUSED(format);
132
133 *dst++ = A;
134 *dst++ = R;
135 *dst++ = G;
136 *dst++ = B;
137 return dst;
138}
139
140static INLINE BYTE* writePixelXRGB(BYTE* dst, DWORD formatSize, UINT32 format, BYTE R, BYTE G,
141 BYTE B, BYTE A)
142{
143 WINPR_UNUSED(formatSize);
144 WINPR_UNUSED(format);
145 WINPR_UNUSED(A);
146
147 dst++; /* Do not touch alpha */
148 *dst++ = R;
149 *dst++ = G;
150 *dst++ = B;
151 return dst;
152}
153
154static INLINE BYTE* writePixelGenericAlpha(BYTE* dst, DWORD formatSize, UINT32 format, BYTE R,
155 BYTE G, BYTE B, BYTE A)
156{
157 UINT32 color = FreeRDPGetColor(format, R, G, B, A);
158 FreeRDPWriteColor(dst, format, color);
159 return dst + formatSize;
160}
161
162static INLINE BYTE* writePixelGeneric(BYTE* dst, DWORD formatSize, UINT32 format, BYTE R, BYTE G,
163 BYTE B, BYTE A)
164{
165 UINT32 color = FreeRDPGetColor(format, R, G, B, A);
166 FreeRDPWriteColorIgnoreAlpha(dst, format, color);
167 return dst + formatSize;
168}
169
170typedef BYTE* (*fkt_writePixel)(BYTE*, DWORD, UINT32, BYTE, BYTE, BYTE, BYTE);
171
172static INLINE fkt_writePixel getPixelWriteFunction(DWORD format, BOOL useAlpha)
173{
174 switch (format)
175 {
176 case PIXEL_FORMAT_ARGB32:
177 case PIXEL_FORMAT_XRGB32:
178 return useAlpha ? writePixelARGB : writePixelXRGB;
179
180 case PIXEL_FORMAT_ABGR32:
181 case PIXEL_FORMAT_XBGR32:
182 return useAlpha ? writePixelABGR : writePixelXBGR;
183
184 case PIXEL_FORMAT_RGBA32:
185 case PIXEL_FORMAT_RGBX32:
186 return useAlpha ? writePixelRGBA : writePixelRGBX;
187
188 case PIXEL_FORMAT_BGRA32:
189 case PIXEL_FORMAT_BGRX32:
190 return useAlpha ? writePixelBGRA : writePixelBGRX;
191
192 default:
193 return useAlpha ? writePixelGenericAlpha : writePixelGeneric;
194 }
195}
196
197static INLINE BYTE CLIP(INT64 X)
198{
199 if (X > 255L)
200 return 255L;
201
202 if (X < 0L)
203 return 0L;
204
205 return (BYTE)X;
206}
207
208static INLINE BYTE CONDITIONAL_CLIP(INT32 in, BYTE original)
209{
210 BYTE out = CLIP(in);
211 BYTE diff = 0;
212 if (out > original)
213 diff = out - original;
214 else
215 diff = original - out;
216 if (diff < 30)
217 return original;
218 return out;
219}
220
226static INLINE INT32 C(INT32 Y)
227{
228 return (Y)-0;
229}
230
231static INLINE INT32 D(INT32 U)
232{
233 return (U)-128;
234}
235
236static INLINE INT32 E(INT32 V)
237{
238 return (V)-128;
239}
240
241static INLINE BYTE YUV2R(INT32 Y, INT32 U, INT32 V)
242{
243 const INT32 r = (256 * C(Y) + 0 * D(U) + 403 * E(V));
244 const INT32 r8 = r >> 8;
245 return CLIP(r8);
246}
247
248static INLINE BYTE YUV2G(INT32 Y, INT32 U, INT32 V)
249{
250 const INT32 g = (256 * C(Y) - 48 * D(U) - 120 * E(V));
251 const INT32 g8 = g >> 8;
252 return CLIP(g8);
253}
254
255static INLINE BYTE YUV2B(INT32 Y, INT32 U, INT32 V)
256{
257 const INT32 b = (256 * C(Y) + 475 * D(U) + 0 * E(V));
258 const INT32 b8 = b >> 8;
259 return CLIP(b8);
260}
261
267static INLINE BYTE RGB2Y(INT32 R, INT32 G, INT32 B)
268{
269 const INT32 val = ((54 * R + 183 * G + 18 * B) >> 8);
270 return WINPR_ASSERTING_INT_CAST(BYTE, val);
271}
272
273static INLINE BYTE RGB2U(INT32 R, INT32 G, INT32 B)
274{
275 const INT32 val = (((-29 * R - 99 * G + 128 * B) >> 8) + 128);
276 return WINPR_ASSERTING_INT_CAST(BYTE, val);
277}
278
279static INLINE BYTE RGB2V(INT32 R, INT32 G, INT32 B)
280{
281 const INT32 val = (((128 * R - 116 * G - 12 * B) >> 8) + 128);
282 return WINPR_ASSERTING_INT_CAST(BYTE, val);
283}
284
285static inline BYTE* writeYUVPixel(BYTE* dst, UINT32 DstFormat, INT32 y, INT32 u, INT32 v,
286 fkt_writePixel fkt)
287{
288 WINPR_ASSERT(fkt);
289 const BYTE r = YUV2R(y, u, v);
290 const BYTE g = YUV2G(y, u, v);
291 const BYTE b = YUV2B(y, u, v);
292 const DWORD formatSize = FreeRDPGetBytesPerPixel(DstFormat);
293 return fkt(dst, formatSize, DstFormat, r, g, b, 0);
294}
295
296FREERDP_LOCAL void general_RGBToAVC444YUV_BGRX_DOUBLE_ROW(
297 size_t offset, const BYTE* WINPR_RESTRICT srcEven, const BYTE* WINPR_RESTRICT srcOdd,
298 BYTE* WINPR_RESTRICT b1Even, BYTE* WINPR_RESTRICT b1Odd, BYTE* WINPR_RESTRICT b2,
299 BYTE* WINPR_RESTRICT b3, BYTE* WINPR_RESTRICT b4, BYTE* WINPR_RESTRICT b5,
300 BYTE* WINPR_RESTRICT b6, BYTE* WINPR_RESTRICT b7, UINT32 width);
301
302FREERDP_LOCAL void general_RGBToAVC444YUVv2_BGRX_DOUBLE_ROW(
303 size_t offset, const BYTE* WINPR_RESTRICT pSrcEven, const BYTE* WINPR_RESTRICT pSrcOdd,
304 BYTE* WINPR_RESTRICT yLumaDstEven, BYTE* WINPR_RESTRICT yLumaDstOdd,
305 BYTE* WINPR_RESTRICT uLumaDst, BYTE* WINPR_RESTRICT vLumaDst,
306 BYTE* WINPR_RESTRICT yEvenChromaDst1, BYTE* WINPR_RESTRICT yEvenChromaDst2,
307 BYTE* WINPR_RESTRICT yOddChromaDst1, BYTE* WINPR_RESTRICT yOddChromaDst2,
308 BYTE* WINPR_RESTRICT uChromaDst1, BYTE* WINPR_RESTRICT uChromaDst2,
309 BYTE* WINPR_RESTRICT vChromaDst1, BYTE* WINPR_RESTRICT vChromaDst2, UINT32 width);
310
311/* Function prototypes for all the init/deinit routines. */
312FREERDP_LOCAL void primitives_init_copy(primitives_t* WINPR_RESTRICT prims);
313FREERDP_LOCAL void primitives_init_set(primitives_t* WINPR_RESTRICT prims);
314FREERDP_LOCAL void primitives_init_add(primitives_t* WINPR_RESTRICT prims);
315FREERDP_LOCAL void primitives_init_andor(primitives_t* WINPR_RESTRICT prims);
316FREERDP_LOCAL void primitives_init_shift(primitives_t* WINPR_RESTRICT prims);
317FREERDP_LOCAL void primitives_init_sign(primitives_t* WINPR_RESTRICT prims);
318FREERDP_LOCAL void primitives_init_alphaComp(primitives_t* WINPR_RESTRICT prims);
319FREERDP_LOCAL void primitives_init_colors(primitives_t* WINPR_RESTRICT prims);
320FREERDP_LOCAL void primitives_init_YCoCg(primitives_t* WINPR_RESTRICT prims);
321FREERDP_LOCAL void primitives_init_YUV(primitives_t* WINPR_RESTRICT prims);
322
323FREERDP_LOCAL void primitives_init_copy_opt(primitives_t* WINPR_RESTRICT prims);
324FREERDP_LOCAL void primitives_init_set_opt(primitives_t* WINPR_RESTRICT prims);
325FREERDP_LOCAL void primitives_init_add_opt(primitives_t* WINPR_RESTRICT prims);
326FREERDP_LOCAL void primitives_init_andor_opt(primitives_t* WINPR_RESTRICT prims);
327FREERDP_LOCAL void primitives_init_shift_opt(primitives_t* WINPR_RESTRICT prims);
328FREERDP_LOCAL void primitives_init_sign_opt(primitives_t* WINPR_RESTRICT prims);
329FREERDP_LOCAL void primitives_init_alphaComp_opt(primitives_t* WINPR_RESTRICT prims);
330FREERDP_LOCAL void primitives_init_colors_opt(primitives_t* WINPR_RESTRICT prims);
331FREERDP_LOCAL void primitives_init_YCoCg_opt(primitives_t* WINPR_RESTRICT prims);
332FREERDP_LOCAL void primitives_init_YUV_opt(primitives_t* WINPR_RESTRICT prims);
333
334#if defined(WITH_OPENCL)
335FREERDP_LOCAL BOOL primitives_init_opencl(primitives_t* WINPR_RESTRICT prims);
336#endif
337
338#endif /* FREERDP_LIB_PRIM_INTERNAL_H */