summaryrefslogtreecommitdiff
path: root/driver/rsdRuntimeStubs.cpp
blob: c728ca715fec9f308cb80e630f312dcbcef98b7b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
/*
 * Copyright (C) 2011-2012 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#include "rsContext.h"
#include "rsScriptC.h"
#include "rsMatrix4x4.h"
#include "rsMatrix3x3.h"
#include "rsMatrix2x2.h"
#include "rsRuntime.h"

#include "rsdCore.h"
#include "rsdBcc.h"

#include "rsdAllocation.h"
#include "rsdShaderCache.h"
#include "rsdVertexArray.h"

#include <time.h>

using namespace android;
using namespace android::renderscript;

typedef float float2 __attribute__((ext_vector_type(2)));
typedef float float3 __attribute__((ext_vector_type(3)));
typedef float float4 __attribute__((ext_vector_type(4)));
typedef double double2 __attribute__((ext_vector_type(2)));
typedef double double3 __attribute__((ext_vector_type(3)));
typedef double double4 __attribute__((ext_vector_type(4)));
typedef char char2 __attribute__((ext_vector_type(2)));
typedef char char3 __attribute__((ext_vector_type(3)));
typedef char char4 __attribute__((ext_vector_type(4)));
typedef unsigned char uchar2 __attribute__((ext_vector_type(2)));
typedef unsigned char uchar3 __attribute__((ext_vector_type(3)));
typedef unsigned char uchar4 __attribute__((ext_vector_type(4)));
typedef int16_t short2 __attribute__((ext_vector_type(2)));
typedef int16_t short3 __attribute__((ext_vector_type(3)));
typedef int16_t short4 __attribute__((ext_vector_type(4)));
typedef uint16_t ushort2 __attribute__((ext_vector_type(2)));
typedef uint16_t ushort3 __attribute__((ext_vector_type(3)));
typedef uint16_t ushort4 __attribute__((ext_vector_type(4)));
typedef int32_t int2 __attribute__((ext_vector_type(2)));
typedef int32_t int3 __attribute__((ext_vector_type(3)));
typedef int32_t int4 __attribute__((ext_vector_type(4)));
typedef uint32_t uint2 __attribute__((ext_vector_type(2)));
typedef uint32_t uint3 __attribute__((ext_vector_type(3)));
typedef uint32_t uint4 __attribute__((ext_vector_type(4)));
typedef int64_t long2 __attribute__((ext_vector_type(2)));
typedef int64_t long3 __attribute__((ext_vector_type(3)));
typedef int64_t long4 __attribute__((ext_vector_type(4)));
typedef uint64_t ulong2 __attribute__((ext_vector_type(2)));
typedef uint64_t ulong3 __attribute__((ext_vector_type(3)));
typedef uint64_t ulong4 __attribute__((ext_vector_type(4)));

typedef uint8_t uchar;
typedef uint16_t ushort;
typedef uint32_t uint;
#ifndef RS_SERVER
typedef uint64_t ulong;
#endif

#ifndef __LP64__
#define OPAQUETYPE(t) \
    typedef struct { const int* const p; } __attribute__((packed, aligned(4))) t;
#else
#define OPAQUETYPE(t) \
    typedef struct { const void* p; const void* r; const void* v1; const void* v2; } t;
#endif

OPAQUETYPE(rs_element)
OPAQUETYPE(rs_type)
OPAQUETYPE(rs_allocation)
OPAQUETYPE(rs_sampler)
OPAQUETYPE(rs_script)
OPAQUETYPE(rs_script_call)

OPAQUETYPE(rs_program_fragment);
OPAQUETYPE(rs_program_store);
OPAQUETYPE(rs_program_vertex);
OPAQUETYPE(rs_program_raster);
OPAQUETYPE(rs_mesh);
OPAQUETYPE(rs_font);

#undef OPAQUETYPE

typedef enum {
    // Empty to avoid conflicting definitions with RsAllocationCubemapFace
} rs_allocation_cubemap_face;

typedef struct { unsigned int val; } rs_allocation_usage_type;

typedef struct {
    int tm_sec;     ///< seconds
    int tm_min;     ///< minutes
    int tm_hour;    ///< hours
    int tm_mday;    ///< day of the month
    int tm_mon;     ///< month
    int tm_year;    ///< year
    int tm_wday;    ///< day of the week
    int tm_yday;    ///< day of the year
    int tm_isdst;   ///< daylight savings time
} rs_tm;

// Some RS functions are not threadsafe but can be called from an invoke
// function.  Instead of summarily marking scripts that call these functions as
// not-threadable we detect calls to them in the driver and sends a fatal error
// message.
static bool failIfInKernel(Context *rsc, const char *funcName) {
    RsdHal *dc = (RsdHal *)rsc->mHal.drv;
    RsdCpuReference *impl = (RsdCpuReference *) dc->mCpuRef;

    if (impl->getInForEach()) {
        char buf[256];
        sprintf(buf, "Error: Call to unsupported function %s "
                         "in kernel", funcName);
        rsc->setError(RS_ERROR_FATAL_DRIVER, buf);
        return true;
    }
    return false;
}

//////////////////////////////////////////////////////////////////////////////
// Allocation routines
//////////////////////////////////////////////////////////////////////////////
#ifdef __i386__
// i386 has different struct return passing to ARM; emulate with a pointer
const Allocation * rsGetAllocation(const void *ptr) {
    Context *rsc = RsdCpuReference::getTlsContext();
    const Script *sc = RsdCpuReference::getTlsScript();
    Allocation* alloc = rsdScriptGetAllocationForPointer(rsc, sc, ptr);
    android::renderscript::rs_allocation obj = {0};
    alloc->callUpdateCacheObject(rsc, &obj);
    return (Allocation *)obj.p;
}
#else
const android::renderscript::rs_allocation rsGetAllocation(const void *ptr) {
    Context *rsc = RsdCpuReference::getTlsContext();
    const Script *sc = RsdCpuReference::getTlsScript();
    Allocation* alloc = rsdScriptGetAllocationForPointer(rsc, sc, ptr);

#ifndef __LP64__ // ARMv7/MIPS
    android::renderscript::rs_allocation obj = {0};
#else // AArch64/x86_64/MIPS64
    android::renderscript::rs_allocation obj = {0, 0, 0, 0};
#endif
    alloc->callUpdateCacheObject(rsc, &obj);
    return obj;
}
#endif

void __attribute__((overloadable)) rsAllocationIoSend(::rs_allocation a) {
    Context *rsc = RsdCpuReference::getTlsContext();
    if (failIfInKernel(rsc, "rsAllocationIoSend"))
        return;
    rsrAllocationIoSend(rsc, (Allocation *)a.p);
}

void __attribute__((overloadable)) rsAllocationIoReceive(::rs_allocation a) {
    Context *rsc = RsdCpuReference::getTlsContext();
    if (failIfInKernel(rsc, "rsAllocationIoReceive"))
        return;
    rsrAllocationIoReceive(rsc, (Allocation *)a.p);
}

void __attribute__((overloadable)) rsAllocationCopy1DRange(
        ::rs_allocation dstAlloc,
        uint32_t dstOff, uint32_t dstMip, uint32_t count,
        ::rs_allocation srcAlloc,
        uint32_t srcOff, uint32_t srcMip) {
    Context *rsc = RsdCpuReference::getTlsContext();
    if (failIfInKernel(rsc, "rsAllocationCopy1DRange"))
        return;
    rsrAllocationCopy1DRange(rsc, (Allocation *)dstAlloc.p, dstOff, dstMip,
                             count, (Allocation *)srcAlloc.p, srcOff, srcMip);
}

void __attribute__((overloadable)) rsAllocationCopy2DRange(
        ::rs_allocation dstAlloc,
        uint32_t dstXoff, uint32_t dstYoff,
        uint32_t dstMip, rs_allocation_cubemap_face dstFace,
        uint32_t width, uint32_t height,
        ::rs_allocation srcAlloc,
        uint32_t srcXoff, uint32_t srcYoff,
        uint32_t srcMip, rs_allocation_cubemap_face srcFace) {
    Context *rsc = RsdCpuReference::getTlsContext();
    if (failIfInKernel(rsc, "rsAllocationCopy2DRange"))
        return;
    rsrAllocationCopy2DRange(rsc, (Allocation *)dstAlloc.p,
                             dstXoff, dstYoff, dstMip, dstFace,
                             width, height, (Allocation *)srcAlloc.p,
                             srcXoff, srcYoff, srcMip, srcFace);
}

//////////////////////////////////////////////////////////////////////////////
// Object routines
//////////////////////////////////////////////////////////////////////////////
#define IS_CLEAR_SET_OBJ(t) \
    bool rsIsObject(t src) { \
        return src.p != nullptr; \
    } \
    void __attribute__((overloadable)) rsClearObject(t *dst) { \
        Context *rsc = RsdCpuReference::getTlsContext(); \
        rsrClearObject(rsc, reinterpret_cast<rs_object_base *>(dst)); \
    } \
    void __attribute__((overloadable)) rsSetObject(t *dst, t src) { \
        Context *rsc = RsdCpuReference::getTlsContext(); \
        rsrSetObject(rsc, reinterpret_cast<rs_object_base *>(dst), (ObjectBase*)src.p); \
    }

IS_CLEAR_SET_OBJ(::rs_element)
IS_CLEAR_SET_OBJ(::rs_type)
IS_CLEAR_SET_OBJ(::rs_allocation)
IS_CLEAR_SET_OBJ(::rs_sampler)
IS_CLEAR_SET_OBJ(::rs_script)

IS_CLEAR_SET_OBJ(::rs_mesh)
IS_CLEAR_SET_OBJ(::rs_program_fragment)
IS_CLEAR_SET_OBJ(::rs_program_vertex)
IS_CLEAR_SET_OBJ(::rs_program_raster)
IS_CLEAR_SET_OBJ(::rs_program_store)
IS_CLEAR_SET_OBJ(::rs_font)

#undef IS_CLEAR_SET_OBJ

//////////////////////////////////////////////////////////////////////////////
// Element routines
//////////////////////////////////////////////////////////////////////////////
static void * ElementAt(Allocation *a, RsDataType dt, uint32_t vecSize,
                        uint32_t x, uint32_t y, uint32_t z) {
    Context *rsc = RsdCpuReference::getTlsContext();
    const Type *t = a->getType();
    const Element *e = t->getElement();

    char buf[256];
    if (x && (x >= t->getLODDimX(0))) {
        sprintf(buf, "Out range ElementAt X %i of %i", x, t->getLODDimX(0));
        rsc->setError(RS_ERROR_FATAL_DEBUG, buf);
        return nullptr;
    }

    if (y && (y >= t->getLODDimY(0))) {
        sprintf(buf, "Out range ElementAt Y %i of %i", y, t->getLODDimY(0));
        rsc->setError(RS_ERROR_FATAL_DEBUG, buf);
        return nullptr;
    }

    if (z && (z >= t->getLODDimZ(0))) {
        sprintf(buf, "Out range ElementAt Z %i of %i", z, t->getLODDimZ(0));
        rsc->setError(RS_ERROR_FATAL_DEBUG, buf);
        return nullptr;
    }

    if (vecSize > 0) {
        if (vecSize != e->getVectorSize()) {
            sprintf(buf, "Vector size mismatch for ElementAt %i of %i", vecSize, e->getVectorSize());
            rsc->setError(RS_ERROR_FATAL_DEBUG, buf);
            return nullptr;
        }

        if (dt != e->getType()) {
            sprintf(buf, "Data type mismatch for ElementAt %i of %i", dt, e->getType());
            rsc->setError(RS_ERROR_FATAL_DEBUG, buf);
            return nullptr;
        }
    }

    uint8_t *p = (uint8_t *)a->mHal.drvState.lod[0].mallocPtr;
    const uint32_t eSize = e->getSizeBytes();
    const uint32_t stride = a->mHal.drvState.lod[0].stride;
    const uint32_t dimY = a->mHal.drvState.lod[0].dimY;
    return &p[(x * eSize) + (y * stride) + (z * stride * dimY)];
}

void rsSetElementAt(::rs_allocation a, const void *ptr, uint32_t x, uint32_t y, uint32_t z) {
    const Type *t = const_cast<Allocation*>((Allocation*)a.p)->getType();
    const Element *e = t->getElement();
    void *tmp = ElementAt((Allocation *)a.p, RS_TYPE_UNSIGNED_8, 0, x, y, z);
    if (tmp != nullptr)
        memcpy(tmp, ptr, e->getSizeBytes());
}

void rsSetElementAt(::rs_allocation a, const void *ptr, uint32_t x, uint32_t y) {
    rsSetElementAt(a, ptr, x, y, 0);
}

void rsSetElementAt(::rs_allocation a, const void *ptr, uint32_t x) {
    rsSetElementAt(a, ptr, x, 0, 0);
}

const void *rsGetElementAt(::rs_allocation a, uint32_t x, uint32_t y, uint32_t z) {
    return ElementAt((Allocation *)a.p, RS_TYPE_UNSIGNED_8, 0, x, y, z);
}

const void *rsGetElementAt(::rs_allocation a, uint32_t x, uint32_t y) {
    return rsGetElementAt(a, x, y ,0);
}

const void *rsGetElementAt(::rs_allocation a, uint32_t x) {
    return rsGetElementAt(a, x, 0, 0);
}

#define ELEMENT_AT(T, DT, VS) \
    void rsSetElementAt_##T(::rs_allocation a, const T *val, uint32_t x, uint32_t y, uint32_t z) { \
        void *r = ElementAt((Allocation *)a.p, DT, VS, x, y, z); \
        if (r != nullptr) ((T *)r)[0] = *val; \
        else ALOGE("Error from %s", __PRETTY_FUNCTION__); \
    } \
    void rsSetElementAt_##T(::rs_allocation a, const T *val, uint32_t x, uint32_t y) { \
        rsSetElementAt_##T(a, val, x, y, 0); \
    } \
    void rsSetElementAt_##T(::rs_allocation a, const T *val, uint32_t x) { \
        rsSetElementAt_##T(a, val, x, 0, 0); \
    } \
    void rsGetElementAt_##T(::rs_allocation a, T *val, uint32_t x, uint32_t y, uint32_t z) { \
        void *r = ElementAt((Allocation *)a.p, DT, VS, x, y, z); \
        if (r != nullptr) *val = ((T *)r)[0]; \
        else ALOGE("Error from %s", __PRETTY_FUNCTION__); \
    } \
    void rsGetElementAt_##T(::rs_allocation a, T *val, uint32_t x, uint32_t y) { \
        rsGetElementAt_##T(a, val, x, y, 0); \
    } \
    void rsGetElementAt_##T(::rs_allocation a, T *val, uint32_t x) { \
        rsGetElementAt_##T(a, val, x, 0, 0); \
    }

ELEMENT_AT(char, RS_TYPE_SIGNED_8, 1)
ELEMENT_AT(char2, RS_TYPE_SIGNED_8, 2)
ELEMENT_AT(char3, RS_TYPE_SIGNED_8, 3)
ELEMENT_AT(char4, RS_TYPE_SIGNED_8, 4)
ELEMENT_AT(uchar, RS_TYPE_UNSIGNED_8, 1)
ELEMENT_AT(uchar2, RS_TYPE_UNSIGNED_8, 2)
ELEMENT_AT(uchar3, RS_TYPE_UNSIGNED_8, 3)
ELEMENT_AT(uchar4, RS_TYPE_UNSIGNED_8, 4)
ELEMENT_AT(short, RS_TYPE_SIGNED_16, 1)
ELEMENT_AT(short2, RS_TYPE_SIGNED_16, 2)
ELEMENT_AT(short3, RS_TYPE_SIGNED_16, 3)
ELEMENT_AT(short4, RS_TYPE_SIGNED_16, 4)
ELEMENT_AT(ushort, RS_TYPE_UNSIGNED_16, 1)
ELEMENT_AT(ushort2, RS_TYPE_UNSIGNED_16, 2)
ELEMENT_AT(ushort3, RS_TYPE_UNSIGNED_16, 3)
ELEMENT_AT(ushort4, RS_TYPE_UNSIGNED_16, 4)
ELEMENT_AT(int, RS_TYPE_SIGNED_32, 1)
ELEMENT_AT(int2, RS_TYPE_SIGNED_32, 2)
ELEMENT_AT(int3, RS_TYPE_SIGNED_32, 3)
ELEMENT_AT(int4, RS_TYPE_SIGNED_32, 4)
ELEMENT_AT(uint, RS_TYPE_UNSIGNED_32, 1)
ELEMENT_AT(uint2, RS_TYPE_UNSIGNED_32, 2)
ELEMENT_AT(uint3, RS_TYPE_UNSIGNED_32, 3)
ELEMENT_AT(uint4, RS_TYPE_UNSIGNED_32, 4)
ELEMENT_AT(long, RS_TYPE_SIGNED_64, 1)
ELEMENT_AT(long2, RS_TYPE_SIGNED_64, 2)
ELEMENT_AT(long3, RS_TYPE_SIGNED_64, 3)
ELEMENT_AT(long4, RS_TYPE_SIGNED_64, 4)
ELEMENT_AT(ulong, RS_TYPE_UNSIGNED_64, 1)
ELEMENT_AT(ulong2, RS_TYPE_UNSIGNED_64, 2)
ELEMENT_AT(ulong3, RS_TYPE_UNSIGNED_64, 3)
ELEMENT_AT(ulong4, RS_TYPE_UNSIGNED_64, 4)
ELEMENT_AT(float, RS_TYPE_FLOAT_32, 1)
ELEMENT_AT(float2, RS_TYPE_FLOAT_32, 2)
ELEMENT_AT(float3, RS_TYPE_FLOAT_32, 3)
ELEMENT_AT(float4, RS_TYPE_FLOAT_32, 4)
ELEMENT_AT(double, RS_TYPE_FLOAT_64, 1)
ELEMENT_AT(double2, RS_TYPE_FLOAT_64, 2)
ELEMENT_AT(double3, RS_TYPE_FLOAT_64, 3)
ELEMENT_AT(double4, RS_TYPE_FLOAT_64, 4)

#undef ELEMENT_AT

#ifndef __LP64__
/*
 * We miss some symbols for rs{Get,Set}Element_long,ulong variants because 64
 * bit integer values are 'long' in RS-land but might be 'long long' in the
 * driver.  Define native_long* and native_ulong* types to be vectors of
 * 'long' as seen by the driver and define overloaded versions of
 * rsSetElementAt_* and rsGetElementAt_*.  This should get us the correct
 * mangled names in the driver.
 */

typedef long native_long2 __attribute__((ext_vector_type(2)));
typedef long native_long3 __attribute__((ext_vector_type(3)));
typedef long native_long4 __attribute__((ext_vector_type(4)));
typedef unsigned long native_ulong2 __attribute__((ext_vector_type(2)));
typedef unsigned long native_ulong3 __attribute__((ext_vector_type(3)));
typedef unsigned long native_ulong4 __attribute__((ext_vector_type(4)));

#define ELEMENT_AT_OVERLOADS(T, U) \
    void rsSetElementAt_##T(::rs_allocation a, const U *val, uint32_t x, uint32_t y, uint32_t z) { \
        rsSetElementAt_##T(a, (T *) val, x, y, z); \
    } \
    void rsSetElementAt_##T(::rs_allocation a, const U *val, uint32_t x, uint32_t y) { \
        rsSetElementAt_##T(a, (T *) val, x, y, 0); \
    } \
    void rsSetElementAt_##T(::rs_allocation a, const U *val, uint32_t x) { \
        rsSetElementAt_##T(a, (T *) val, x, 0, 0); \
    } \
    void rsGetElementAt_##T(::rs_allocation a, U *val, uint32_t x, uint32_t y, uint32_t z) { \
        rsGetElementAt_##T(a, (T *) val, x, y, z); \
    } \
    void rsGetElementAt_##T(::rs_allocation a, U *val, uint32_t x, uint32_t y) { \
        rsGetElementAt_##T(a, (T *) val, x, y, 0); \
    } \
    void rsGetElementAt_##T(::rs_allocation a, U *val, uint32_t x) { \
        rsGetElementAt_##T(a, (T *) val, x, 0, 0); \
    } \

ELEMENT_AT_OVERLOADS(long2, native_long2)
ELEMENT_AT_OVERLOADS(long3, native_long3)
ELEMENT_AT_OVERLOADS(long4, native_long4)
ELEMENT_AT_OVERLOADS(ulong, unsigned long)
ELEMENT_AT_OVERLOADS(ulong2, native_ulong2)
ELEMENT_AT_OVERLOADS(ulong3, native_ulong3)
ELEMENT_AT_OVERLOADS(ulong4, native_ulong4)

// We also need variants of rs{Get,Set}ElementAt_long that take 'long long *' as
// we might have this overloaded variant in old APKs.
ELEMENT_AT_OVERLOADS(long, long long)

#undef ELEMENT_AT_OVERLOADS
#endif

//////////////////////////////////////////////////////////////////////////////
// ForEach routines
//////////////////////////////////////////////////////////////////////////////
void __attribute__((overloadable)) rsForEach(::rs_script script,
                                             ::rs_allocation in,
                                             ::rs_allocation out,
                                             const void *usr,
                                             const rs_script_call *call) {
    Context *rsc = RsdCpuReference::getTlsContext();
    rsrForEach(rsc, (Script *)script.p, (Allocation *)in.p,
               (Allocation *)out.p, usr, 0, (RsScriptCall *)call);
}

void __attribute__((overloadable)) rsForEach(::rs_script script,
                                             ::rs_allocation in,
                                             ::rs_allocation out,
                                             const void *usr) {
    Context *rsc = RsdCpuReference::getTlsContext();
    rsrForEach(rsc, (Script *)script.p, (Allocation *)in.p, (Allocation *)out.p,
               usr, 0, nullptr);
}

void __attribute__((overloadable)) rsForEach(::rs_script script,
                                             ::rs_allocation in,
                                             ::rs_allocation out) {
    Context *rsc = RsdCpuReference::getTlsContext();
    rsrForEach(rsc, (Script *)script.p, (Allocation *)in.p, (Allocation *)out.p,
               nullptr, 0, nullptr);
}

// These functions are only supported in 32-bit.
#ifndef __LP64__
void __attribute__((overloadable)) rsForEach(::rs_script script,
                                             ::rs_allocation in,
                                             ::rs_allocation out,
                                             const void *usr,
                                             uint32_t usrLen) {
    Context *rsc = RsdCpuReference::getTlsContext();
    rsrForEach(rsc, (Script *)script.p, (Allocation *)in.p, (Allocation *)out.p,
               usr, usrLen, nullptr);
}

void __attribute__((overloadable)) rsForEach(::rs_script script,
                                             ::rs_allocation in,
                                             ::rs_allocation out,
                                             const void *usr,
                                             uint32_t usrLen,
                                             const rs_script_call *call) {
    Context *rsc = RsdCpuReference::getTlsContext();
    rsrForEach(rsc, (Script *)script.p, (Allocation *)in.p, (Allocation *)out.p,
               usr, usrLen, (RsScriptCall *)call);
}
#endif

//////////////////////////////////////////////////////////////////////////////
// Message routines
//////////////////////////////////////////////////////////////////////////////
uint32_t rsSendToClient(int cmdID) {
    Context *rsc = RsdCpuReference::getTlsContext();
    return rsrToClient(rsc, cmdID, (const void *)nullptr, 0);
}

uint32_t rsSendToClient(int cmdID, const void *data, uint32_t len) {
    Context *rsc = RsdCpuReference::getTlsContext();
    return rsrToClient(rsc, cmdID, data, len);
}

uint32_t rsSendToClientBlocking(int cmdID) {
    Context *rsc = RsdCpuReference::getTlsContext();
    return rsrToClientBlocking(rsc, cmdID, (const void *)nullptr, 0);
}

uint32_t rsSendToClientBlocking(int cmdID, const void *data, uint32_t len) {
    Context *rsc = RsdCpuReference::getTlsContext();
    return rsrToClientBlocking(rsc, cmdID, data, len);
}

//////////////////////////////////////////////////////////////////////////////
// Time routines
//////////////////////////////////////////////////////////////////////////////

// time_t is int in 32-bit RenderScript.  time_t is long in bionic.  rsTime and
// rsLocaltime are set to explicitly take 'const int *' so we generate the
// correct mangled names.
#ifndef __LP64__
int rsTime(int *timer) {
#else
time_t rsTime(time_t * timer) {
#endif
    Context *rsc = RsdCpuReference::getTlsContext();
    return rsrTime(rsc, (time_t *)timer);
}

#ifndef __LP64__
rs_tm* rsLocaltime(rs_tm* local, const int *timer) {
#else
rs_tm* rsLocaltime(rs_tm* local, const time_t *timer) {
#endif
    Context *rsc = RsdCpuReference::getTlsContext();
    return (rs_tm*)rsrLocalTime(rsc, (tm*)local, (time_t *)timer);
}

int64_t rsUptimeMillis() {
    Context *rsc = RsdCpuReference::getTlsContext();
    return rsrUptimeMillis(rsc);
}

int64_t rsUptimeNanos() {
    Context *rsc = RsdCpuReference::getTlsContext();
    return rsrUptimeNanos(rsc);
}

float rsGetDt() {
    Context *rsc = RsdCpuReference::getTlsContext();
    const Script *sc = RsdCpuReference::getTlsScript();
    return rsrGetDt(rsc, sc);
}

//////////////////////////////////////////////////////////////////////////////
// Graphics routines
//////////////////////////////////////////////////////////////////////////////
#ifndef RS_COMPATIBILITY_LIB
static void SC_DrawQuadTexCoords(float x1, float y1, float z1, float u1, float v1,
                                 float x2, float y2, float z2, float u2, float v2,
                                 float x3, float y3, float z3, float u3, float v3,
                                 float x4, float y4, float z4, float u4, float v4) {
    Context *rsc = RsdCpuReference::getTlsContext();

    if (!rsc->setupCheck()) {
        return;
    }

    RsdHal *dc = (RsdHal *)rsc->mHal.drv;
    if (!dc->gl.shaderCache->setup(rsc)) {
        return;
    }

    //ALOGE("Quad");
    //ALOGE("%4.2f, %4.2f, %4.2f", x1, y1, z1);
    //ALOGE("%4.2f, %4.2f, %4.2f", x2, y2, z2);
    //ALOGE("%4.2f, %4.2f, %4.2f", x3, y3, z3);
    //ALOGE("%4.2f, %4.2f, %4.2f", x4, y4, z4);

    float vtx[] = {x1,y1,z1, x2,y2,z2, x3,y3,z3, x4,y4,z4};
    const float tex[] = {u1,v1, u2,v2, u3,v3, u4,v4};

    RsdVertexArray::Attrib attribs[2];
    attribs[0].set(GL_FLOAT, 3, 12, false, (size_t)vtx, "ATTRIB_position");
    attribs[1].set(GL_FLOAT, 2, 8, false, (size_t)tex, "ATTRIB_texture0");

    RsdVertexArray va(attribs, 2);
    va.setup(rsc);

    RSD_CALL_GL(glDrawArrays, GL_TRIANGLE_FAN, 0, 4);
}

static void SC_DrawQuad(float x1, float y1, float z1,
                        float x2, float y2, float z2,
                        float x3, float y3, float z3,
                        float x4, float y4, float z4) {
    SC_DrawQuadTexCoords(x1, y1, z1, 0, 1,
                         x2, y2, z2, 1, 1,
                         x3, y3, z3, 1, 0,
                         x4, y4, z4, 0, 0);
}

static void SC_DrawSpriteScreenspace(float x, float y, float z, float w, float h) {
    Context *rsc = RsdCpuReference::getTlsContext();

    ObjectBaseRef<const ProgramVertex> tmp(rsc->getProgramVertex());
    rsc->setProgramVertex(rsc->getDefaultProgramVertex());
    //rsc->setupCheck();

    //GLint crop[4] = {0, h, w, -h};

    float sh = rsc->getHeight();

    SC_DrawQuad(x,   sh - y,     z,
                x+w, sh - y,     z,
                x+w, sh - (y+h), z,
                x,   sh - (y+h), z);
    rsc->setProgramVertex((ProgramVertex *)tmp.get());
}

void rsAllocationMarkDirty(::rs_allocation a) {
    Context *rsc = RsdCpuReference::getTlsContext();
    rsrAllocationSyncAll(rsc, (Allocation *)a.p, RS_ALLOCATION_USAGE_SCRIPT);
}

void rsgAllocationSyncAll(::rs_allocation a) {
    Context *rsc = RsdCpuReference::getTlsContext();
    rsrAllocationSyncAll(rsc, (Allocation *)a.p, RS_ALLOCATION_USAGE_SCRIPT);
}

void rsgAllocationSyncAll(::rs_allocation a,
                          unsigned int usage) {
    Context *rsc = RsdCpuReference::getTlsContext();
    rsrAllocationSyncAll(rsc, (Allocation *)a.p, (RsAllocationUsageType)usage);
}


void rsgAllocationSyncAll(::rs_allocation a,
                          rs_allocation_usage_type source) {
    Context *rsc = RsdCpuReference::getTlsContext();
    rsrAllocationSyncAll(rsc, (Allocation *)a.p, (RsAllocationUsageType)source.val);
}

void rsgBindProgramFragment(::rs_program_fragment pf) {
    Context *rsc = RsdCpuReference::getTlsContext();
    rsrBindProgramFragment(rsc, (ProgramFragment *)pf.p);
}

void rsgBindProgramStore(::rs_program_store ps) {
    Context *rsc = RsdCpuReference::getTlsContext();
    rsrBindProgramStore(rsc, (ProgramStore *)ps.p);
}

void rsgBindProgramVertex(::rs_program_vertex pv) {
    Context *rsc = RsdCpuReference::getTlsContext();
    rsrBindProgramVertex(rsc, (ProgramVertex *)pv.p);
}

void rsgBindProgramRaster(::rs_program_raster pr) {
    Context *rsc = RsdCpuReference::getTlsContext();
    rsrBindProgramRaster(rsc, (ProgramRaster *)pr.p);
}

void rsgBindSampler(::rs_program_fragment pf,
                    uint32_t slot, ::rs_sampler s) {
    Context *rsc = RsdCpuReference::getTlsContext();
    rsrBindSampler(rsc, (ProgramFragment *)pf.p, slot, (Sampler *)s.p);
}

void rsgBindTexture(::rs_program_fragment pf,
                    uint32_t slot, ::rs_allocation a) {
    Context *rsc = RsdCpuReference::getTlsContext();
    rsrBindTexture(rsc, (ProgramFragment *)pf.p, slot, (Allocation *)a.p);
}

void rsgBindConstant(::rs_program_fragment pf,
                     uint32_t slot, ::rs_allocation a) {
    Context *rsc = RsdCpuReference::getTlsContext();
    rsrBindConstant(rsc, (ProgramFragment *)pf.p, slot, (Allocation *)a.p);
}

void rsgBindConstant(::rs_program_vertex pv,
                     uint32_t slot, ::rs_allocation a) {
    Context *rsc = RsdCpuReference::getTlsContext();
    rsrBindConstant(rsc, (ProgramVertex *)pv.p, slot, (Allocation *)a.p);
}

void rsgProgramVertexLoadProjectionMatrix(const rs_matrix4x4 *m) {
    Context *rsc = RsdCpuReference::getTlsContext();
    rsrVpLoadProjectionMatrix(rsc, (const rsc_Matrix *)m);
}

void rsgProgramVertexLoadModelMatrix(const rs_matrix4x4 *m) {
    Context *rsc = RsdCpuReference::getTlsContext();
    rsrVpLoadModelMatrix(rsc, (const rsc_Matrix *)m);
}

void rsgProgramVertexLoadTextureMatrix(const rs_matrix4x4 *m) {
    Context *rsc = RsdCpuReference::getTlsContext();
    rsrVpLoadTextureMatrix(rsc, (const rsc_Matrix *)m);
}

void rsgProgramVertexGetProjectionMatrix(rs_matrix4x4 *m) {
    Context *rsc = RsdCpuReference::getTlsContext();
    rsrVpGetProjectionMatrix(rsc, (rsc_Matrix *)m);
}

void rsgProgramFragmentConstantColor(::rs_program_fragment pf,
                                     float r, float g, float b, float a) {
    Context *rsc = RsdCpuReference::getTlsContext();
    rsrPfConstantColor(rsc, (ProgramFragment *)pf.p, r, g, b, a);
}

uint32_t rsgGetWidth(void) {
    Context *rsc = RsdCpuReference::getTlsContext();
    return rsrGetWidth(rsc);
}

uint32_t rsgGetHeight(void) {
    Context *rsc = RsdCpuReference::getTlsContext();
    return rsrGetHeight(rsc);
}

void rsgDrawRect(float x1, float y1, float x2, float y2, float z) {
    SC_DrawQuad(x1, y2, z,
                x2, y2, z,
                x2, y1, z,
                x1, y1, z);
}

void rsgDrawQuad(float x1, float y1, float z1,
                 float x2, float y2, float z2,
                 float x3, float y3, float z3,
                 float x4, float y4, float z4) {
    SC_DrawQuad(x1, y1, z1,
                x2, y2, z2,
                x3, y3, z3,
                x4, y4, z4);
}

void rsgDrawQuadTexCoords(float x1, float y1, float z1, float u1, float v1,
                          float x2, float y2, float z2, float u2, float v2,
                          float x3, float y3, float z3, float u3, float v3,
                          float x4, float y4, float z4, float u4, float v4) {
    SC_DrawQuadTexCoords(x1, y1, z1, u1, v1,
                         x2, y2, z2, u2, v2,
                         x3, y3, z3, u3, v3,
                         x4, y4, z4, u4, v4);
}

void rsgDrawSpriteScreenspace(float x, float y, float z, float w, float h) {
    SC_DrawSpriteScreenspace(x, y, z, w, h);
}

void rsgDrawMesh(::rs_mesh ism) {
    Context *rsc = RsdCpuReference::getTlsContext();
    rsrDrawMesh(rsc, (Mesh *)ism.p);
}

void rsgDrawMesh(::rs_mesh ism, uint primitiveIndex) {
    Context *rsc = RsdCpuReference::getTlsContext();
    rsrDrawMeshPrimitive(rsc, (Mesh *)ism.p, primitiveIndex);
}

void rsgDrawMesh(::rs_mesh ism, uint primitiveIndex, uint start, uint len) {
    Context *rsc = RsdCpuReference::getTlsContext();
    rsrDrawMeshPrimitiveRange(rsc, (Mesh *)ism.p, primitiveIndex, start, len);
}

void  rsgMeshComputeBoundingBox(::rs_mesh mesh,
                                float *minX, float *minY, float *minZ,
                                float *maxX, float *maxY, float *maxZ) {
    Context *rsc = RsdCpuReference::getTlsContext();
    rsrMeshComputeBoundingBox(rsc, (Mesh *)mesh.p, minX, minY, minZ, maxX, maxY, maxZ);
}

void rsgClearColor(float r, float g, float b, float a) {
    Context *rsc = RsdCpuReference::getTlsContext();
    rsrPrepareClear(rsc);
    rsdGLClearColor(rsc, r, g, b, a);
}

void rsgClearDepth(float value) {
    Context *rsc = RsdCpuReference::getTlsContext();
    rsrPrepareClear(rsc);
    rsdGLClearDepth(rsc, value);
}

void rsgDrawText(const char *text, int x, int y) {
    Context *rsc = RsdCpuReference::getTlsContext();
    rsrDrawText(rsc, text, x, y);
}

void rsgDrawText(::rs_allocation a, int x, int y) {
    Context *rsc = RsdCpuReference::getTlsContext();
    rsrDrawTextAlloc(rsc, (Allocation *)a.p, x, y);
}

void rsgMeasureText(const char *text, int *left, int *right,
                    int *top, int *bottom) {
    Context *rsc = RsdCpuReference::getTlsContext();
    rsrMeasureText(rsc, text, left, right, top, bottom);
}

void rsgMeasureText(::rs_allocation a, int *left, int *right,
                    int *top, int *bottom) {
    Context *rsc = RsdCpuReference::getTlsContext();
    rsrMeasureTextAlloc(rsc, (Allocation *)a.p, left, right, top, bottom);
}

void rsgBindFont(::rs_font font) {
    Context *rsc = RsdCpuReference::getTlsContext();
    rsrBindFont(rsc, (Font *)font.p);
}

void rsgFontColor(float r, float g, float b, float a) {
    Context *rsc = RsdCpuReference::getTlsContext();
    rsrFontColor(rsc, r, g, b, a);
}

void rsgBindColorTarget(::rs_allocation a, uint slot) {
    Context *rsc = RsdCpuReference::getTlsContext();
    rsrBindFrameBufferObjectColorTarget(rsc, (Allocation *)a.p, slot);
}

void rsgBindDepthTarget(::rs_allocation a) {
    Context *rsc = RsdCpuReference::getTlsContext();
    rsrBindFrameBufferObjectDepthTarget(rsc, (Allocation *)a.p);
}

void rsgClearColorTarget(uint slot) {
    Context *rsc = RsdCpuReference::getTlsContext();
    rsrClearFrameBufferObjectColorTarget(rsc, slot);
}

void rsgClearDepthTarget(void) {
    Context *rsc = RsdCpuReference::getTlsContext();
    rsrClearFrameBufferObjectDepthTarget(rsc);
}

void rsgClearAllRenderTargets(void) {
    Context *rsc = RsdCpuReference::getTlsContext();
    rsrClearFrameBufferObjectTargets(rsc);
}

void color(float r, float g, float b, float a) {
    Context *rsc = RsdCpuReference::getTlsContext();
    rsrColor(rsc, r, g, b, a);
}

void rsgFinish(void) {
    Context *rsc = RsdCpuReference::getTlsContext();
    rsdGLFinish(rsc);
}
#endif

//////////////////////////////////////////////////////////////////////////////
// Debug routines
//////////////////////////////////////////////////////////////////////////////
void rsDebug(const char *s, float f) {
    ALOGD("%s %f, 0x%08x", s, f, *((int *) (&f)));
}

void rsDebug(const char *s, float f1, float f2) {
    ALOGD("%s {%f, %f}", s, f1, f2);
}

void rsDebug(const char *s, float f1, float f2, float f3) {
    ALOGD("%s {%f, %f, %f}", s, f1, f2, f3);
}

void rsDebug(const char *s, float f1, float f2, float f3, float f4) {
    ALOGD("%s {%f, %f, %f, %f}", s, f1, f2, f3, f4);
}

void rsDebug(const char *s, const float2 *f2) {
    float2 f = *f2;
    ALOGD("%s {%f, %f}", s, f.x, f.y);
}

void rsDebug(const char *s, const float3 *f3) {
    float3 f = *f3;
    ALOGD("%s {%f, %f, %f}", s, f.x, f.y, f.z);
}

void rsDebug(const char *s, const float4 *f4) {
    float4 f = *f4;
    ALOGD("%s {%f, %f, %f, %f}", s, f.x, f.y, f.z, f.w);
}

void rsDebug(const char *s, double d) {
    ALOGD("%s %f, 0x%08llx", s, d, *((long long *) (&d)));
}

void rsDebug(const char *s, const double2 *d2) {
    double2 d = *d2;
    ALOGD("%s {%f, %f}", s, d.x, d.y);
}

void rsDebug(const char *s, const double3 *d3) {
    double3 d = *d3;
    ALOGD("%s {%f, %f, %f}", s, d.x, d.y, d.z);
}

void rsDebug(const char *s, const double4 *d4) {
    double4 d = *d4;
    ALOGD("%s {%f, %f, %f, %f}", s, d.x, d.y, d.z, d.w);
}

void rsDebug(const char *s, const rs_matrix4x4 *m) {
    float *f = (float *)m;
    ALOGD("%s {%f, %f, %f, %f", s, f[0], f[4], f[8], f[12]);
    ALOGD("%s  %f, %f, %f, %f", s, f[1], f[5], f[9], f[13]);
    ALOGD("%s  %f, %f, %f, %f", s, f[2], f[6], f[10], f[14]);
    ALOGD("%s  %f, %f, %f, %f}", s, f[3], f[7], f[11], f[15]);
}

void rsDebug(const char *s, const rs_matrix3x3 *m) {
    float *f = (float *)m;
    ALOGD("%s {%f, %f, %f", s, f[0], f[3], f[6]);
    ALOGD("%s  %f, %f, %f", s, f[1], f[4], f[7]);
    ALOGD("%s  %f, %f, %f}",s, f[2], f[5], f[8]);
}

void rsDebug(const char *s, const rs_matrix2x2 *m) {
    float *f = (float *)m;
    ALOGD("%s {%f, %f", s, f[0], f[2]);
    ALOGD("%s  %f, %f}",s, f[1], f[3]);
}

void rsDebug(const char *s, char c) {
    ALOGD("%s %hhd  0x%hhx", s, c, (unsigned char)c);
}

void rsDebug(const char *s, const char2 *c2) {
    char2 c = *c2;
    ALOGD("%s {%hhd, %hhd}  0x%hhx 0x%hhx", s, c.x, c.y, (unsigned char)c.x, (unsigned char)c.y);
}

void rsDebug(const char *s, const char3 *c3) {
    char3 c = *c3;
    ALOGD("%s {%hhd, %hhd, %hhd}  0x%hhx 0x%hhx 0x%hhx", s, c.x, c.y, c.z, (unsigned char)c.x, (unsigned char)c.y, (unsigned char)c.z);
}

void rsDebug(const char *s, const char4 *c4) {
    char4 c = *c4;
    ALOGD("%s {%hhd, %hhd, %hhd, %hhd}  0x%hhx 0x%hhx 0x%hhx 0x%hhx", s, c.x, c.y, c.z, c.w, (unsigned char)c.x, (unsigned char)c.y, (unsigned char)c.z, (unsigned char)c.w);
}

void rsDebug(const char *s, unsigned char c) {
    ALOGD("%s %hhu  0x%hhx", s, c, c);
}

void rsDebug(const char *s, const uchar2 *c2) {
    uchar2 c = *c2;
    ALOGD("%s {%hhu, %hhu}  0x%hhx 0x%hhx", s, c.x, c.y, c.x, c.y);
}

void rsDebug(const char *s, const uchar3 *c3) {
    uchar3 c = *c3;
    ALOGD("%s {%hhu, %hhu, %hhu}  0x%hhx 0x%hhx 0x%hhx", s, c.x, c.y, c.z, c.x, c.y, c.z);
}

void rsDebug(const char *s, const uchar4 *c4) {
    uchar4 c = *c4;
    ALOGD("%s {%hhu, %hhu, %hhu, %hhu}  0x%hhx 0x%hhx 0x%hhx 0x%hhx", s, c.x, c.y, c.z, c.w, c.x, c.y, c.z, c.w);
}

void rsDebug(const char *s, short c) {
    ALOGD("%s %hd  0x%hx", s, c, c);
}

void rsDebug(const char *s, const short2 *c2) {
    short2 c = *c2;
    ALOGD("%s {%hd, %hd}  0x%hx 0x%hx", s, c.x, c.y, c.x, c.y);
}

void rsDebug(const char *s, const short3 *c3) {
    short3 c = *c3;
    ALOGD("%s {%hd, %hd, %hd}  0x%hx 0x%hx 0x%hx", s, c.x, c.y, c.z, c.x, c.y, c.z);
}

void rsDebug(const char *s, const short4 *c4) {
    short4 c = *c4;
    ALOGD("%s {%hd, %hd, %hd, %hd}  0x%hx 0x%hx 0x%hx 0x%hx", s, c.x, c.y, c.z, c.w, c.x, c.y, c.z, c.w);
}

void rsDebug(const char *s, unsigned short c) {
    ALOGD("%s %hu  0x%hx", s, c, c);
}

void rsDebug(const char *s, const ushort2 *c2) {
    ushort2 c = *c2;
    ALOGD("%s {%hu, %hu}  0x%hx 0x%hx", s, c.x, c.y, c.x, c.y);
}

void rsDebug(const char *s, const ushort3 *c3) {
    ushort3 c = *c3;
    ALOGD("%s {%hu, %hu, %hu}  0x%hx 0x%hx 0x%hx", s, c.x, c.y, c.z, c.x, c.y, c.z);
}

void rsDebug(const char *s, const ushort4 *c4) {
    ushort4 c = *c4;
    ALOGD("%s {%hu, %hu, %hu, %hu}  0x%hx 0x%hx 0x%hx 0x%hx", s, c.x, c.y, c.z, c.w, c.x, c.y, c.z, c.w);
}

void rsDebug(const char *s, int i) {
    ALOGD("%s %d  0x%x", s, i, i);
}

void rsDebug(const char *s, const int2 *i2) {
    int2 i = *i2;
    ALOGD("%s {%d, %d}  0x%x 0x%x", s, i.x, i.y, i.x, i.y);
}

void rsDebug(const char *s, const int3 *i3) {
    int3 i = *i3;
    ALOGD("%s {%d, %d, %d}  0x%x 0x%x 0x%x", s, i.x, i.y, i.z, i.x, i.y, i.z);
}

void rsDebug(const char *s, const int4 *i4) {
    int4 i = *i4;
    ALOGD("%s {%d, %d, %d, %d}  0x%x 0x%x 0x%x 0x%x", s, i.x, i.y, i.z, i.w, i.x, i.y, i.z, i.w);
}

void rsDebug(const char *s, unsigned int i) {
    ALOGD("%s %u  0x%x", s, i, i);
}

void rsDebug(const char *s, const uint2 *i2) {
    uint2 i = *i2;
    ALOGD("%s {%u, %u}  0x%x 0x%x", s, i.x, i.y, i.x, i.y);
}

void rsDebug(const char *s, const uint3 *i3) {
    uint3 i = *i3;
    ALOGD("%s {%u, %u, %u}  0x%x 0x%x 0x%x", s, i.x, i.y, i.z, i.x, i.y, i.z);
}

void rsDebug(const char *s, const uint4 *i4) {
    uint4 i = *i4;
    ALOGD("%s {%u, %u, %u, %u}  0x%x 0x%x 0x%x 0x%x", s, i.x, i.y, i.z, i.w, i.x, i.y, i.z, i.w);
}

template <typename T>
static inline long long LL(const T &x) {
    return static_cast<long long>(x);
}

template <typename T>
static inline unsigned long long LLu(const T &x) {
    return static_cast<unsigned long long>(x);
}

void rsDebug(const char *s, long l) {
    ALOGD("%s %lld  0x%llx", s, LL(l), LL(l));
}

void rsDebug(const char *s, long long ll) {
    ALOGD("%s %lld  0x%llx", s, LL(ll), LL(ll));
}

void rsDebug(const char *s, const long2 *c) {
    long2 ll = *c;
    ALOGD("%s {%lld, %lld}  0x%llx 0x%llx", s, LL(ll.x), LL(ll.y), LL(ll.x), LL(ll.y));
}

void rsDebug(const char *s, const long3 *c) {
    long3 ll = *c;
    ALOGD("%s {%lld, %lld, %lld}  0x%llx 0x%llx 0x%llx", s, LL(ll.x), LL(ll.y), LL(ll.z), LL(ll.x), LL(ll.y), LL(ll.z));
}

void rsDebug(const char *s, const long4 *c) {
    long4 ll = *c;
    ALOGD("%s {%lld, %lld, %lld, %lld}  0x%llx 0x%llx 0x%llx 0x%llx", s, LL(ll.x), LL(ll.y), LL(ll.z), LL(ll.w), LL(ll.x), LL(ll.y), LL(ll.z), LL(ll.w));
}

void rsDebug(const char *s, unsigned long l) {
    unsigned long long ll = l;
    ALOGD("%s %llu  0x%llx", s, ll, ll);
}

void rsDebug(const char *s, unsigned long long ll) {
    ALOGD("%s %llu  0x%llx", s, ll, ll);
}

void rsDebug(const char *s, const ulong2 *c) {
    ulong2 ll = *c;
    ALOGD("%s {%llu, %llu}  0x%llx 0x%llx", s, LLu(ll.x), LLu(ll.y), LLu(ll.x), LLu(ll.y));
}

void rsDebug(const char *s, const ulong3 *c) {
    ulong3 ll = *c;
    ALOGD("%s {%llu, %llu, %llu}  0x%llx 0x%llx 0x%llx", s, LLu(ll.x), LLu(ll.y), LLu(ll.z), LLu(ll.x), LLu(ll.y), LLu(ll.z));
}

void rsDebug(const char *s, const ulong4 *c) {
    ulong4 ll = *c;
    ALOGD("%s {%llu, %llu, %llu, %llu}  0x%llx 0x%llx 0x%llx 0x%llx", s, LLu(ll.x), LLu(ll.y), LLu(ll.z), LLu(ll.w), LLu(ll.x), LLu(ll.y), LLu(ll.z), LLu(ll.w));
}

// FIXME: We need to export these function signatures for the compatibility
// library. The C++ name mangling that LLVM uses for ext_vector_type requires
// different versions for "long" vs. "long long". Note that the called
// functions are still using the appropriate 64-bit sizes.

#ifndef __LP64__
typedef long l2 __attribute__((ext_vector_type(2)));
typedef long l3 __attribute__((ext_vector_type(3)));
typedef long l4 __attribute__((ext_vector_type(4)));
typedef unsigned long ul2 __attribute__((ext_vector_type(2)));
typedef unsigned long ul3 __attribute__((ext_vector_type(3)));
typedef unsigned long ul4 __attribute__((ext_vector_type(4)));

void rsDebug(const char *s, const l2 *c) {
    long2 ll = *(const long2 *)c;
    ALOGD("%s {%lld, %lld}  0x%llx 0x%llx", s, LL(ll.x), LL(ll.y), LL(ll.x), LL(ll.y));
}

void rsDebug(const char *s, const l3 *c) {
    long3 ll = *(const long3 *)c;
    ALOGD("%s {%lld, %lld, %lld}  0x%llx 0x%llx 0x%llx", s, LL(ll.x), LL(ll.y), LL(ll.z), LL(ll.x), LL(ll.y), LL(ll.z));
}

void rsDebug(const char *s, const l4 *c) {
    long4 ll = *(const long4 *)c;
    ALOGD("%s {%lld, %lld, %lld, %lld}  0x%llx 0x%llx 0x%llx 0x%llx", s, LL(ll.x), LL(ll.y), LL(ll.z), LL(ll.w), LL(ll.x), LL(ll.y), LL(ll.z), LL(ll.w));
}

void rsDebug(const char *s, const ul2 *c) {
    ulong2 ll = *(const ulong2 *)c;
    ALOGD("%s {%llu, %llu}  0x%llx 0x%llx", s, LLu(ll.x), LLu(ll.y), LLu(ll.x), LLu(ll.y));
}

void rsDebug(const char *s, const ul3 *c) {
    ulong3 ll = *(const ulong3 *)c;
    ALOGD("%s {%llu, %llu, %llu}  0x%llx 0x%llx 0x%llx", s, LLu(ll.x), LLu(ll.y), LLu(ll.z), LLu(ll.x), LLu(ll.y), LLu(ll.z));
}

void rsDebug(const char *s, const ul4 *c) {
    ulong4 ll = *(const ulong4 *)c;
    ALOGD("%s {%llu, %llu, %llu, %llu}  0x%llx 0x%llx 0x%llx 0x%llx", s, LLu(ll.x), LLu(ll.y), LLu(ll.z), LLu(ll.w), LLu(ll.x), LLu(ll.y), LLu(ll.z), LLu(ll.w));
}
#endif

void rsDebug(const char *s, const long2 ll) {
    ALOGD("%s {%lld, %lld}  0x%llx 0x%llx", s, LL(ll.x), LL(ll.y), LL(ll.x), LL(ll.y));
}

void rsDebug(const char *s, const long3 ll) {
    ALOGD("%s {%lld, %lld, %lld}  0x%llx 0x%llx 0x%llx", s, LL(ll.x), LL(ll.y), LL(ll.z), LL(ll.x), LL(ll.y), LL(ll.z));
}

void rsDebug(const char *s, const long4 ll) {
    ALOGD("%s {%lld, %lld, %lld, %lld}  0x%llx 0x%llx 0x%llx 0x%llx", s, LL(ll.x), LL(ll.y), LL(ll.z), LL(ll.w), LL(ll.x), LL(ll.y), LL(ll.z), LL(ll.w));
}

void rsDebug(const char *s, const ulong2 ll) {
    ALOGD("%s {%llu, %llu}  0x%llx 0x%llx", s, LLu(ll.x), LLu(ll.y), LLu(ll.x), LLu(ll.y));
}

void rsDebug(const char *s, const ulong3 ll) {
    ALOGD("%s {%llu, %llu, %llu}  0x%llx 0x%llx 0x%llx", s, LLu(ll.x), LLu(ll.y), LLu(ll.z), LLu(ll.x), LLu(ll.y), LLu(ll.z));
}

void rsDebug(const char *s, const ulong4 ll) {
    ALOGD("%s {%llu, %llu, %llu, %llu}  0x%llx 0x%llx 0x%llx 0x%llx", s, LLu(ll.x), LLu(ll.y), LLu(ll.z), LLu(ll.w), LLu(ll.x), LLu(ll.y), LLu(ll.z), LLu(ll.w));
}

void rsDebug(const char *s, const void *p) {
    ALOGD("%s %p", s, p);
}

extern const RsdCpuReference::CpuSymbol * rsdLookupRuntimeStub(Context * pContext, char const* name) {
// TODO: remove
    return nullptr;
}