1 #include "rs_core.rsh"
2 #include "rs_structs.h"
3 
4 // Opaque Allocation type operations
5 extern uint32_t __attribute__((overloadable))
rsAllocationGetDimX(rs_allocation a)6     rsAllocationGetDimX(rs_allocation a) {
7     Allocation_t *alloc = (Allocation_t *)a.p;
8     return alloc->mHal.drvState.lod[0].dimX;
9 }
10 
11 extern uint32_t __attribute__((overloadable))
rsAllocationGetDimY(rs_allocation a)12         rsAllocationGetDimY(rs_allocation a) {
13     Allocation_t *alloc = (Allocation_t *)a.p;
14     return alloc->mHal.drvState.lod[0].dimY;
15 }
16 
17 extern uint32_t __attribute__((overloadable))
rsAllocationGetDimZ(rs_allocation a)18         rsAllocationGetDimZ(rs_allocation a) {
19     Allocation_t *alloc = (Allocation_t *)a.p;
20     return alloc->mHal.drvState.lod[0].dimZ;
21 }
22 
23 extern uint32_t __attribute__((overloadable))
rsAllocationGetDimLOD(rs_allocation a)24         rsAllocationGetDimLOD(rs_allocation a) {
25     Allocation_t *alloc = (Allocation_t *)a.p;
26     return alloc->mHal.state.hasMipmaps;
27 }
28 
29 extern uint32_t __attribute__((overloadable))
rsAllocationGetDimFaces(rs_allocation a)30         rsAllocationGetDimFaces(rs_allocation a) {
31     Allocation_t *alloc = (Allocation_t *)a.p;
32     return alloc->mHal.state.hasFaces;
33 }
34 
35 
36 extern rs_element __attribute__((overloadable))
rsAllocationGetElement(rs_allocation a)37         rsAllocationGetElement(rs_allocation a) {
38     Allocation_t *alloc = (Allocation_t *)a.p;
39     if (alloc == NULL) {
40         rs_element nullElem = {0};
41         return nullElem;
42     }
43     Type_t *type = (Type_t *)alloc->mHal.state.type;
44     rs_element returnElem = {type->mHal.state.element};
45     return returnElem;
46 }
47 
48 // TODO: this needs to be optimized, obviously
memcpy(void * dst,void * src,size_t size)49 static void memcpy(void* dst, void* src, size_t size) {
50     char* dst_c = (char*) dst, *src_c = (char*) src;
51     for (; size > 0; size--) {
52         *dst_c++ = *src_c++;
53     }
54 }
55 
56 #ifdef RS_DEBUG_RUNTIME
57 #define ELEMENT_AT(T)                                                   \
58     extern void __attribute__((overloadable))                           \
59         rsSetElementAt_##T(rs_allocation a, const T *val, uint32_t x);  \
60     extern void __attribute__((overloadable))                           \
61         rsSetElementAt_##T(rs_allocation a, const T *val, uint32_t x, uint32_t y); \
62     extern void __attribute__((overloadable))                           \
63         rsSetElementAt_##T(rs_allocation a, const T *val, uint32_t x, uint32_t y, uint32_t z); \
64     extern void __attribute__((overloadable))                           \
65         rsGetElementAt_##T(rs_allocation a, T *val, uint32_t x);  \
66     extern void __attribute__((overloadable))                           \
67         rsGetElementAt_##T(rs_allocation a, T *val, uint32_t x, uint32_t y); \
68     extern void __attribute__((overloadable))                           \
69         rsGetElementAt_##T(rs_allocation a, T *val, uint32_t x, uint32_t y, uint32_t z); \
70                                                                         \
71     extern void __attribute__((overloadable))                           \
72     rsSetElementAt_##T(rs_allocation a, T val, uint32_t x) {            \
73         rsSetElementAt_##T(a, &val, x);                                 \
74     }                                                                   \
75     extern void __attribute__((overloadable))                           \
76     rsSetElementAt_##T(rs_allocation a, T val, uint32_t x, uint32_t y) { \
77         rsSetElementAt_##T(a, &val, x, y);                              \
78     }                                                                   \
79     extern void __attribute__((overloadable))                           \
80     rsSetElementAt_##T(rs_allocation a, T val, uint32_t x, uint32_t y, uint32_t z) { \
81         rsSetElementAt_##T(a, &val, x, y, z);                           \
82     }                                                                   \
83     extern T __attribute__((overloadable))                              \
84     rsGetElementAt_##T(rs_allocation a, uint32_t x) {                   \
85         T tmp;                                                          \
86         rsGetElementAt_##T(a, &tmp, x);                                 \
87         return tmp;                                                     \
88     }                                                                   \
89     extern T __attribute__((overloadable))                              \
90     rsGetElementAt_##T(rs_allocation a, uint32_t x, uint32_t y) {       \
91         T tmp;                                                          \
92         rsGetElementAt_##T(a, &tmp, x, y);                              \
93         return tmp;                                                     \
94     }                                                                   \
95     extern T __attribute__((overloadable))                              \
96     rsGetElementAt_##T(rs_allocation a, uint32_t x, uint32_t y, uint32_t z) { \
97         T tmp;                                                          \
98         rsGetElementAt_##T(a, &tmp, x, y, z);                           \
99         return tmp;                                                     \
100     }
101 #else
102 
103 uint8_t*
rsOffset(rs_allocation a,uint32_t sizeOf,uint32_t x,uint32_t y,uint32_t z)104 rsOffset(rs_allocation a, uint32_t sizeOf, uint32_t x, uint32_t y,
105          uint32_t z) {
106     Allocation_t *alloc = (Allocation_t *)a.p;
107     //#ifdef __LP64__
108     //    uint8_t *p = (uint8_t *)a.r;
109     //#else
110     uint8_t *p = (uint8_t *)alloc->mHal.drvState.lod[0].mallocPtr;
111     //#endif
112     const uint32_t stride = (uint32_t)alloc->mHal.drvState.lod[0].stride;
113     const uint32_t dimY = alloc->mHal.drvState.lod[0].dimY;
114     uint8_t *dp = &p[(sizeOf * x) + (y * stride) +
115                      (z * stride * dimY)];
116     return dp;
117 }
118 
119 uint8_t*
rsOffsetNs(rs_allocation a,uint32_t x,uint32_t y,uint32_t z)120 rsOffsetNs(rs_allocation a, uint32_t x, uint32_t y, uint32_t z) {
121     Allocation_t *alloc = (Allocation_t *)a.p;
122     //#ifdef __LP64__
123     //    uint8_t *p = (uint8_t *)a.r;
124     //#else
125     uint8_t *p = (uint8_t *)alloc->mHal.drvState.lod[0].mallocPtr;
126     //#endif
127     const uint32_t stride = alloc->mHal.drvState.lod[0].stride;
128     const uint32_t dimY = alloc->mHal.drvState.lod[0].dimY;
129     const uint32_t sizeOf = alloc->mHal.state.elementSizeBytes;;
130     uint8_t *dp = &p[(sizeOf * x) + (y * stride) +
131                      (z * stride * dimY)];
132     return dp;
133 }
134 
135 #define SET_ELEMENT_AT_TYPE(T, typename)                                    \
136                                                                         \
137     void                                                                \
138     rsSetElementAtImpl_##typename(rs_allocation a, typename val, uint32_t x,   \
139                                   uint32_t y, uint32_t z);              \
140                                                                         \
141     extern void __attribute__((overloadable))                           \
142     rsSetElementAt_##typename(rs_allocation a, T val, uint32_t x) {     \
143         rsSetElementAtImpl_##typename(a, (typename)val, x, 0, 0);              \
144     }                                                                   \
145                                                                         \
146     extern void __attribute__((overloadable))                           \
147     rsSetElementAt_##typename(rs_allocation a, T val, uint32_t x,       \
148                               uint32_t y) {                             \
149         rsSetElementAtImpl_##typename(a, (typename)val, x, y, 0);              \
150     }                                                                   \
151                                                                         \
152     extern void __attribute__((overloadable))                           \
153     rsSetElementAt_##typename(rs_allocation a, T val, uint32_t x, uint32_t y, \
154                               uint32_t z) {                             \
155         rsSetElementAtImpl_##typename(a, (typename)val, x, y, z);              \
156     }                                                                   \
157 
158 
159 
160 #define GET_ELEMENT_AT_TYPE(T, typename)                                \
161     typename                                                            \
162     rsGetElementAtImpl_##typename(rs_allocation a, uint32_t x, uint32_t y, \
163                                   uint32_t z);                          \
164                                                                         \
165     extern typename __attribute__((overloadable))                       \
166     rsGetElementAt_##typename(rs_allocation a, uint32_t x) {            \
167         return (typename)rsGetElementAtImpl_##typename(a, x, 0, 0);     \
168     }                                                                   \
169                                                                         \
170     extern typename __attribute__((overloadable))                       \
171     rsGetElementAt_##typename(rs_allocation a, uint32_t x, uint32_t y) { \
172         return (typename)rsGetElementAtImpl_##typename(a, x, y, 0);     \
173     }                                                                   \
174                                                                         \
175     extern typename __attribute__((overloadable))                       \
176     rsGetElementAt_##typename(rs_allocation a, uint32_t x, uint32_t y,  \
177                               uint32_t z) {                             \
178         return (typename)rsGetElementAtImpl_##typename(a, x, y, z);     \
179     }
180 
181 #define SET_ELEMENT_AT(T) SET_ELEMENT_AT_TYPE(T, T)
182 #define GET_ELEMENT_AT(T) GET_ELEMENT_AT_TYPE(T, T)
183 
184 #define ELEMENT_AT(T)                           \
185     SET_ELEMENT_AT(T)                           \
186     GET_ELEMENT_AT(T)
187 
188 
189 extern const void * __attribute__((overloadable))
rsGetElementAt(rs_allocation a,uint32_t x)190         rsGetElementAt(rs_allocation a, uint32_t x) {
191     Allocation_t *alloc = (Allocation_t *)a.p;
192     const uint8_t *p = (const uint8_t *)alloc->mHal.drvState.lod[0].mallocPtr;
193     const uint32_t eSize = alloc->mHal.state.elementSizeBytes;
194     return &p[eSize * x];
195 }
196 
197 extern const void * __attribute__((overloadable))
rsGetElementAt(rs_allocation a,uint32_t x,uint32_t y)198         rsGetElementAt(rs_allocation a, uint32_t x, uint32_t y) {
199     Allocation_t *alloc = (Allocation_t *)a.p;
200     const uint8_t *p = (const uint8_t *)alloc->mHal.drvState.lod[0].mallocPtr;
201     const uint32_t eSize = alloc->mHal.state.elementSizeBytes;
202     const uint32_t stride = alloc->mHal.drvState.lod[0].stride;
203     return &p[(eSize * x) + (y * stride)];
204 }
205 
206 extern const void * __attribute__((overloadable))
rsGetElementAt(rs_allocation a,uint32_t x,uint32_t y,uint32_t z)207         rsGetElementAt(rs_allocation a, uint32_t x, uint32_t y, uint32_t z) {
208     Allocation_t *alloc = (Allocation_t *)a.p;
209     const uint8_t *p = (const uint8_t *)alloc->mHal.drvState.lod[0].mallocPtr;
210     const uint32_t eSize = alloc->mHal.state.elementSizeBytes;
211     const uint32_t stride = alloc->mHal.drvState.lod[0].stride;
212     const uint32_t dimY = alloc->mHal.drvState.lod[0].dimY;
213     return &p[(eSize * x) + (y * stride) + (z * stride * dimY)];
214 }
215 extern void __attribute__((overloadable))
rsSetElementAt(rs_allocation a,void * ptr,uint32_t x)216         rsSetElementAt(rs_allocation a, void* ptr, uint32_t x) {
217     Allocation_t *alloc = (Allocation_t *)a.p;
218     const uint8_t *p = (const uint8_t *)alloc->mHal.drvState.lod[0].mallocPtr;
219     const uint32_t eSize = alloc->mHal.state.elementSizeBytes;
220     memcpy((void*)&p[eSize * x], ptr, eSize);
221 }
222 
223 extern void __attribute__((overloadable))
rsSetElementAt(rs_allocation a,void * ptr,uint32_t x,uint32_t y)224         rsSetElementAt(rs_allocation a, void* ptr, uint32_t x, uint32_t y) {
225     Allocation_t *alloc = (Allocation_t *)a.p;
226     const uint8_t *p = (const uint8_t *)alloc->mHal.drvState.lod[0].mallocPtr;
227     const uint32_t eSize = alloc->mHal.state.elementSizeBytes;
228     const uint32_t stride = alloc->mHal.drvState.lod[0].stride;
229     memcpy((void*)&p[(eSize * x) + (y * stride)], ptr, eSize);
230 }
231 
232 extern void __attribute__((overloadable))
rsSetElementAt(rs_allocation a,void * ptr,uint32_t x,uint32_t y,uint32_t z)233         rsSetElementAt(rs_allocation a, void* ptr, uint32_t x, uint32_t y, uint32_t z) {
234     Allocation_t *alloc = (Allocation_t *)a.p;
235     const uint8_t *p = (const uint8_t *)alloc->mHal.drvState.lod[0].mallocPtr;
236     const uint32_t eSize = alloc->mHal.state.elementSizeBytes;
237     const uint32_t stride = alloc->mHal.drvState.lod[0].stride;
238     const uint32_t dimY = alloc->mHal.drvState.lod[0].dimY;
239     memcpy((void*)&p[(eSize * x) + (y * stride) + (z * stride * dimY)], ptr, eSize);
240 }
241 #endif // RS_DEBUG_RUNTIME
242 
243 ELEMENT_AT(char)
244 ELEMENT_AT(char2)
245 ELEMENT_AT(char3)
246 ELEMENT_AT(char4)
247 ELEMENT_AT(uchar)
248 ELEMENT_AT(uchar2)
249 ELEMENT_AT(uchar3)
250 ELEMENT_AT(uchar4)
251 ELEMENT_AT(short)
252 ELEMENT_AT(short2)
253 ELEMENT_AT(short3)
254 ELEMENT_AT(short4)
255 ELEMENT_AT(ushort)
256 ELEMENT_AT(ushort2)
257 ELEMENT_AT(ushort3)
258 ELEMENT_AT(ushort4)
259 ELEMENT_AT(int)
260 ELEMENT_AT(int2)
261 ELEMENT_AT(int3)
262 ELEMENT_AT(int4)
263 ELEMENT_AT(uint)
264 ELEMENT_AT(uint2)
265 ELEMENT_AT(uint3)
266 ELEMENT_AT(uint4)
267 ELEMENT_AT(long)
268 ELEMENT_AT(long2)
269 ELEMENT_AT(long3)
270 ELEMENT_AT(long4)
271 ELEMENT_AT(ulong)
272 ELEMENT_AT(ulong2)
273 ELEMENT_AT(ulong3)
274 ELEMENT_AT(ulong4)
275 ELEMENT_AT(half)
276 ELEMENT_AT(half2)
277 ELEMENT_AT(half3)
278 ELEMENT_AT(half4)
279 ELEMENT_AT(float)
280 ELEMENT_AT(float2)
281 ELEMENT_AT(float3)
282 ELEMENT_AT(float4)
283 ELEMENT_AT(double)
284 ELEMENT_AT(double2)
285 ELEMENT_AT(double3)
286 ELEMENT_AT(double4)
287 
288 typedef unsigned long long ull;
289 typedef unsigned long long ull2 __attribute__((ext_vector_type(2)));
290 typedef unsigned long long ull3 __attribute__((ext_vector_type(3)));
291 typedef unsigned long long ull4 __attribute__((ext_vector_type(4)));
292 
293 #ifndef RS_DEBUG_RUNTIME
SET_ELEMENT_AT_TYPE(ull,ulong)294 SET_ELEMENT_AT_TYPE(ull, ulong)
295 SET_ELEMENT_AT_TYPE(ull2, ulong2)
296 SET_ELEMENT_AT_TYPE(ull3, ulong3)
297 SET_ELEMENT_AT_TYPE(ull4, ulong4)
298 
299 #undef SET_ELEMENT_AT_TYPE
300 #undef GET_ELEMENT_AT_TYPE
301 #undef ELEMENT_AT_TYPE
302 #endif
303 
304 #undef ELEMENT_AT
305 
306 
307 extern uchar __attribute__((overloadable))
308         rsGetElementAtYuv_uchar_Y(rs_allocation a, uint32_t x, uint32_t y) {
309     return rsGetElementAt_uchar(a, x, y);
310 }
311 
312 extern uchar __attribute__((overloadable))
rsGetElementAtYuv_uchar_U(rs_allocation a,uint32_t x,uint32_t y)313         rsGetElementAtYuv_uchar_U(rs_allocation a, uint32_t x, uint32_t y) {
314 
315     Allocation_t *alloc = (Allocation_t *)a.p;
316 
317     const size_t cstep = alloc->mHal.drvState.yuv.step;
318     const size_t shift = alloc->mHal.drvState.yuv.shift;
319     const size_t stride = alloc->mHal.drvState.lod[1].stride;
320 
321     const uchar *pin = (const uchar *)alloc->mHal.drvState.lod[1].mallocPtr;
322 
323     return pin[((x >> shift) * cstep) + ((y >> shift) * stride)];
324 }
325 
326 extern uchar __attribute__((overloadable))
rsGetElementAtYuv_uchar_V(rs_allocation a,uint32_t x,uint32_t y)327         rsGetElementAtYuv_uchar_V(rs_allocation a, uint32_t x, uint32_t y) {
328 
329     Allocation_t *alloc = (Allocation_t *)a.p;
330 
331     const size_t cstep = alloc->mHal.drvState.yuv.step;
332     const size_t shift = alloc->mHal.drvState.yuv.shift;
333     const size_t stride = alloc->mHal.drvState.lod[2].stride;
334 
335     const uchar *pin = (const uchar *)alloc->mHal.drvState.lod[2].mallocPtr;
336 
337     return pin[((x >> shift) * cstep) + ((y >> shift) * stride)];
338 }
339 
340 
341 #define VOP(T)                                                          \
342     extern void __rsAllocationVStoreXImpl_##T(rs_allocation a, const T val, uint32_t x, uint32_t y, uint32_t z); \
343     extern T __rsAllocationVLoadXImpl_##T(rs_allocation a, uint32_t x, uint32_t y, uint32_t z); \
344                                                                         \
345     extern void __attribute__((overloadable))                           \
346     rsAllocationVStoreX_##T(rs_allocation a, T val, uint32_t x) {       \
347         __rsAllocationVStoreXImpl_##T(a, val, x, 0, 0);                 \
348     }                                                                   \
349     extern void __attribute__((overloadable))                           \
350     rsAllocationVStoreX_##T(rs_allocation a, T val, uint32_t x, uint32_t y) { \
351         __rsAllocationVStoreXImpl_##T(a, val, x, y, 0);                 \
352     }                                                                   \
353     extern void __attribute__((overloadable))                           \
354     rsAllocationVStoreX_##T(rs_allocation a, T val, uint32_t x, uint32_t y, uint32_t z) { \
355         __rsAllocationVStoreXImpl_##T(a, val, x, y, z);                 \
356     }                                                                   \
357     extern T __attribute__((overloadable))                              \
358     rsAllocationVLoadX_##T(rs_allocation a, uint32_t x) {               \
359         return __rsAllocationVLoadXImpl_##T(a, x, 0, 0);                \
360     }                                                                   \
361     extern T __attribute__((overloadable))                              \
362     rsAllocationVLoadX_##T(rs_allocation a, uint32_t x, uint32_t y) {   \
363         return __rsAllocationVLoadXImpl_##T(a, x, y, 0);                \
364     }                                                                   \
365     extern T __attribute__((overloadable))                              \
366     rsAllocationVLoadX_##T(rs_allocation a, uint32_t x, uint32_t y, uint32_t z) { \
367         return __rsAllocationVLoadXImpl_##T(a, x, y, z);                \
368     }
369 
370 VOP(char2)
371 VOP(char3)
372 VOP(char4)
373 VOP(uchar2)
374 VOP(uchar3)
375 VOP(uchar4)
376 VOP(short2)
377 VOP(short3)
378 VOP(short4)
379 VOP(ushort2)
380 VOP(ushort3)
381 VOP(ushort4)
382 VOP(int2)
383 VOP(int3)
384 VOP(int4)
385 VOP(uint2)
386 VOP(uint3)
387 VOP(uint4)
388 VOP(long2)
389 VOP(long3)
390 VOP(long4)
391 VOP(ulong2)
392 VOP(ulong3)
393 VOP(ulong4)
394 VOP(float2)
395 VOP(float3)
396 VOP(float4)
397 VOP(double2)
398 VOP(double3)
399 VOP(double4)
400 
401 #undef VOP
402