rs_allocation.c revision 5a47020542c52af3e879c1cd67674ca979ff0a18
1#include "rs_core.rsh"
2#include "rs_graphics.rsh"
3#include "rs_structs.h"
4
5// Opaque Allocation type operations
6extern uint32_t __attribute__((overloadable))
7    rsAllocationGetDimX(rs_allocation a) {
8    Allocation_t *alloc = (Allocation_t *)a.p;
9    return alloc->mHal.drvState.lod[0].dimX;
10}
11
12extern uint32_t __attribute__((overloadable))
13        rsAllocationGetDimY(rs_allocation a) {
14    Allocation_t *alloc = (Allocation_t *)a.p;
15    return alloc->mHal.drvState.lod[0].dimY;
16}
17
18extern uint32_t __attribute__((overloadable))
19        rsAllocationGetDimZ(rs_allocation a) {
20    Allocation_t *alloc = (Allocation_t *)a.p;
21    return alloc->mHal.drvState.lod[0].dimZ;
22}
23
24extern uint32_t __attribute__((overloadable))
25        rsAllocationGetDimLOD(rs_allocation a) {
26    Allocation_t *alloc = (Allocation_t *)a.p;
27    return alloc->mHal.state.hasMipmaps;
28}
29
30extern uint32_t __attribute__((overloadable))
31        rsAllocationGetDimFaces(rs_allocation a) {
32    Allocation_t *alloc = (Allocation_t *)a.p;
33    return alloc->mHal.state.hasFaces;
34}
35
36
37extern rs_element __attribute__((overloadable))
38        rsAllocationGetElement(rs_allocation a) {
39    Allocation_t *alloc = (Allocation_t *)a.p;
40    if (alloc == NULL) {
41        rs_element nullElem = {0};
42        return nullElem;
43    }
44    Type_t *type = (Type_t *)alloc->mHal.state.type;
45    rs_element returnElem = {type->mHal.state.element};
46    return returnElem;
47}
48
49// TODO: this needs to be optimized, obviously
50static void memcpy(void* dst, void* src, size_t size) {
51    char* dst_c = (char*) dst, *src_c = (char*) src;
52    for (; size > 0; size--) {
53        *dst_c++ = *src_c++;
54    }
55}
56
57#ifdef RS_DEBUG_RUNTIME
58#define ELEMENT_AT(T)                                                   \
59    extern void __attribute__((overloadable))                           \
60        rsSetElementAt_##T(rs_allocation a, const T *val, uint32_t x);  \
61    extern void __attribute__((overloadable))                           \
62        rsSetElementAt_##T(rs_allocation a, const T *val, uint32_t x, uint32_t y); \
63    extern void __attribute__((overloadable))                           \
64        rsSetElementAt_##T(rs_allocation a, const T *val, uint32_t x, uint32_t y, uint32_t z); \
65    extern void __attribute__((overloadable))                           \
66        rsGetElementAt_##T(rs_allocation a, T *val, uint32_t x);  \
67    extern void __attribute__((overloadable))                           \
68        rsGetElementAt_##T(rs_allocation a, T *val, uint32_t x, uint32_t y); \
69    extern void __attribute__((overloadable))                           \
70        rsGetElementAt_##T(rs_allocation a, T *val, uint32_t x, uint32_t y, uint32_t z); \
71                                                                        \
72    extern void __attribute__((overloadable))                           \
73    rsSetElementAt_##T(rs_allocation a, T val, uint32_t x) {            \
74        rsSetElementAt_##T(a, &val, x);                                 \
75    }                                                                   \
76    extern void __attribute__((overloadable))                           \
77    rsSetElementAt_##T(rs_allocation a, T val, uint32_t x, uint32_t y) { \
78        rsSetElementAt_##T(a, &val, x, y);                              \
79    }                                                                   \
80    extern void __attribute__((overloadable))                           \
81    rsSetElementAt_##T(rs_allocation a, T val, uint32_t x, uint32_t y, uint32_t z) { \
82        rsSetElementAt_##T(a, &val, x, y, z);                           \
83    }                                                                   \
84    extern T __attribute__((overloadable))                              \
85    rsGetElementAt_##T(rs_allocation a, uint32_t x) {                   \
86        T tmp;                                                          \
87        rsGetElementAt_##T(a, &tmp, x);                                 \
88        return tmp;                                                     \
89    }                                                                   \
90    extern T __attribute__((overloadable))                              \
91    rsGetElementAt_##T(rs_allocation a, uint32_t x, uint32_t y) {       \
92        T tmp;                                                          \
93        rsGetElementAt_##T(a, &tmp, x, y);                              \
94        return tmp;                                                     \
95    }                                                                   \
96    extern T __attribute__((overloadable))                              \
97    rsGetElementAt_##T(rs_allocation a, uint32_t x, uint32_t y, uint32_t z) { \
98        T tmp;                                                          \
99        rsGetElementAt_##T(a, &tmp, x, y, z);                           \
100        return tmp;                                                     \
101    }
102
103#else
104#define ELEMENT_AT(T)                                                   \
105    extern void __attribute__((overloadable))                           \
106    rsSetElementAt_##T(rs_allocation a, T val, uint32_t x) {            \
107        Allocation_t *alloc = (Allocation_t *)a.p;                      \
108        uint8_t *p = (uint8_t *)alloc->mHal.drvState.lod[0].mallocPtr; \
109        const uint32_t eSize = sizeof(T);                               \
110        *((T*)&p[(eSize * x)]) = val;                                   \
111    }                                                                   \
112    extern void __attribute__((overloadable))                           \
113    rsSetElementAt_##T(rs_allocation a, T val, uint32_t x, uint32_t y) { \
114        Allocation_t *alloc = (Allocation_t *)a.p;                      \
115        uint8_t *p = (uint8_t *)alloc->mHal.drvState.lod[0].mallocPtr; \
116        const uint32_t eSize = sizeof(T);                               \
117        const uint32_t stride = alloc->mHal.drvState.lod[0].stride;     \
118        *((T*)&p[(eSize * x) + (y * stride)]) = val;                    \
119    }                                                                   \
120    extern void __attribute__((overloadable))                           \
121    rsSetElementAt_##T(rs_allocation a, T val, uint32_t x, uint32_t y, uint32_t z) { \
122        Allocation_t *alloc = (Allocation_t *)a.p;                      \
123        uint8_t *p = (uint8_t *)alloc->mHal.drvState.lod[0].mallocPtr; \
124        const uint32_t stride = alloc->mHal.drvState.lod[0].stride;     \
125        const uint32_t dimY = alloc->mHal.drvState.lod[0].dimY;         \
126        uint8_t *dp = &p[(sizeof(T) * x) + (y * stride) + (z * stride * dimY)]; \
127        ((T*)dp)[0] = val;                                        \
128    }                                                                   \
129    extern T __attribute__((overloadable))                              \
130    rsGetElementAt_##T(rs_allocation a, uint32_t x) {                   \
131        Allocation_t *alloc = (Allocation_t *)a.p;                      \
132        const uint8_t *p = (const uint8_t *)alloc->mHal.drvState.lod[0].mallocPtr; \
133        return *((T*)&p[(sizeof(T) * x)]);                              \
134    }                                                                   \
135    extern T __attribute__((overloadable))                              \
136    rsGetElementAt_##T(rs_allocation a, uint32_t x, uint32_t y) {       \
137        Allocation_t *alloc = (Allocation_t *)a.p;                      \
138        const uint8_t *p = (const uint8_t *)alloc->mHal.drvState.lod[0].mallocPtr; \
139        const uint32_t stride = alloc->mHal.drvState.lod[0].stride;     \
140        return *((T*)&p[(sizeof(T) * x) + (y * stride)]);               \
141    }                                                                   \
142    extern T __attribute__((overloadable))                              \
143    rsGetElementAt_##T(rs_allocation a, uint32_t x, uint32_t y, uint32_t z) { \
144        Allocation_t *alloc = (Allocation_t *)a.p;                      \
145        const uint8_t *p = (const uint8_t *)alloc->mHal.drvState.lod[0].mallocPtr; \
146        const uint32_t stride = alloc->mHal.drvState.lod[0].stride;     \
147        const uint32_t dimY = alloc->mHal.drvState.lod[0].dimY;         \
148        const uint8_t *dp = &p[(sizeof(T) * x) + (y * stride) + (z * stride * dimY)]; \
149        return ((const T*)dp)[0];                                       \
150    }
151
152
153
154extern const void * __attribute__((overloadable))
155        rsGetElementAt(rs_allocation a, uint32_t x) {
156    Allocation_t *alloc = (Allocation_t *)a.p;
157    const uint8_t *p = (const uint8_t *)alloc->mHal.drvState.lod[0].mallocPtr;
158    const uint32_t eSize = alloc->mHal.state.elementSizeBytes;
159    return &p[eSize * x];
160}
161
162extern const void * __attribute__((overloadable))
163        rsGetElementAt(rs_allocation a, uint32_t x, uint32_t y) {
164    Allocation_t *alloc = (Allocation_t *)a.p;
165    const uint8_t *p = (const uint8_t *)alloc->mHal.drvState.lod[0].mallocPtr;
166    const uint32_t eSize = alloc->mHal.state.elementSizeBytes;
167    const uint32_t stride = alloc->mHal.drvState.lod[0].stride;
168    return &p[(eSize * x) + (y * stride)];
169}
170
171extern const void * __attribute__((overloadable))
172        rsGetElementAt(rs_allocation a, uint32_t x, uint32_t y, uint32_t z) {
173    Allocation_t *alloc = (Allocation_t *)a.p;
174    const uint8_t *p = (const uint8_t *)alloc->mHal.drvState.lod[0].mallocPtr;
175    const uint32_t eSize = alloc->mHal.state.elementSizeBytes;
176    const uint32_t stride = alloc->mHal.drvState.lod[0].stride;
177    const uint32_t dimY = alloc->mHal.drvState.lod[0].dimY;
178    return &p[(eSize * x) + (y * stride) + (z * stride * dimY)];
179}
180extern void __attribute__((overloadable))
181        rsSetElementAt(rs_allocation a, void* ptr, uint32_t x) {
182    Allocation_t *alloc = (Allocation_t *)a.p;
183    const uint8_t *p = (const uint8_t *)alloc->mHal.drvState.lod[0].mallocPtr;
184    const uint32_t eSize = alloc->mHal.state.elementSizeBytes;
185    memcpy((void*)&p[eSize * x], ptr, eSize);
186}
187
188extern void __attribute__((overloadable))
189        rsSetElementAt(rs_allocation a, void* ptr, uint32_t x, uint32_t y) {
190    Allocation_t *alloc = (Allocation_t *)a.p;
191    const uint8_t *p = (const uint8_t *)alloc->mHal.drvState.lod[0].mallocPtr;
192    const uint32_t eSize = alloc->mHal.state.elementSizeBytes;
193    const uint32_t stride = alloc->mHal.drvState.lod[0].stride;
194    memcpy((void*)&p[(eSize * x) + (y * stride)], ptr, eSize);
195}
196
197extern void __attribute__((overloadable))
198        rsSetElementAt(rs_allocation a, void* ptr, uint32_t x, uint32_t y, uint32_t z) {
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    const uint32_t dimY = alloc->mHal.drvState.lod[0].dimY;
204    memcpy((void*)&p[(eSize * x) + (y * stride) + (z * stride * dimY)], ptr, eSize);
205}
206#endif
207
208ELEMENT_AT(char)
209ELEMENT_AT(char2)
210ELEMENT_AT(char3)
211ELEMENT_AT(char4)
212ELEMENT_AT(uchar)
213ELEMENT_AT(uchar2)
214ELEMENT_AT(uchar3)
215ELEMENT_AT(uchar4)
216ELEMENT_AT(short)
217ELEMENT_AT(short2)
218ELEMENT_AT(short3)
219ELEMENT_AT(short4)
220ELEMENT_AT(ushort)
221ELEMENT_AT(ushort2)
222ELEMENT_AT(ushort3)
223ELEMENT_AT(ushort4)
224ELEMENT_AT(int)
225ELEMENT_AT(int2)
226ELEMENT_AT(int3)
227ELEMENT_AT(int4)
228ELEMENT_AT(uint)
229ELEMENT_AT(uint2)
230ELEMENT_AT(uint3)
231ELEMENT_AT(uint4)
232ELEMENT_AT(long)
233ELEMENT_AT(long2)
234ELEMENT_AT(long3)
235ELEMENT_AT(long4)
236ELEMENT_AT(ulong)
237ELEMENT_AT(ulong2)
238ELEMENT_AT(ulong3)
239ELEMENT_AT(ulong4)
240ELEMENT_AT(float)
241ELEMENT_AT(float2)
242ELEMENT_AT(float3)
243ELEMENT_AT(float4)
244ELEMENT_AT(double)
245ELEMENT_AT(double2)
246ELEMENT_AT(double3)
247ELEMENT_AT(double4)
248
249#undef ELEMENT_AT
250
251
252extern const uchar __attribute__((overloadable))
253        rsGetElementAtYuv_uchar_Y(rs_allocation a, uint32_t x, uint32_t y) {
254    return rsGetElementAt_uchar(a, x, y);
255}
256
257extern const uchar __attribute__((overloadable))
258        rsGetElementAtYuv_uchar_U(rs_allocation a, uint32_t x, uint32_t y) {
259
260    Allocation_t *alloc = (Allocation_t *)a.p;
261    const uint32_t yuvID = alloc->mHal.state.yuv;
262    const uint8_t *p = (const uint8_t *)alloc->mHal.drvState.lod[1].mallocPtr;
263    const uint32_t stride = alloc->mHal.drvState.lod[1].stride;
264
265    switch(yuvID) {
266    case 0x32315659: //HAL_PIXEL_FORMAT_YV12:
267        x >>= 1;
268        y >>= 1;
269        return p[x + (y * stride)];
270    case 11: //HAL_PIXEL_FORMAT_YCrCb_420_SP:  // NV21
271        x >>= 1;
272        y >>= 1;
273        return p[(x<<1) + (y * stride)];
274    default:
275        break;
276    }
277
278    return 0;
279}
280
281extern const uchar __attribute__((overloadable))
282        rsGetElementAtYuv_uchar_V(rs_allocation a, uint32_t x, uint32_t y) {
283
284    Allocation_t *alloc = (Allocation_t *)a.p;
285    const uint32_t yuvID = alloc->mHal.state.yuv;
286
287    switch(yuvID) {
288    case 0x32315659: //HAL_PIXEL_FORMAT_YV12:
289        {
290        const uint8_t *p = (const uint8_t *)alloc->mHal.drvState.lod[2].mallocPtr;
291        const uint32_t stride = alloc->mHal.drvState.lod[2].stride;
292        x >>= 1;
293        y >>= 1;
294        return p[x + (y * stride)];
295        }
296    case 11: //HAL_PIXEL_FORMAT_YCrCb_420_SP:  // NV21
297        {
298        const uint8_t *p = (const uint8_t *)alloc->mHal.drvState.lod[1].mallocPtr;
299        const uint32_t stride = alloc->mHal.drvState.lod[1].stride;
300        x >>= 1;
301        y >>= 1;
302        return p[(x<<1) + (y * stride) + 1];
303        }
304    default:
305            break;
306    }
307
308    return 0;
309}
310
311