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