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
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
|
#include "slang_rs_context.hpp"
#include "slang_rs_export_var.hpp"
#include "slang_rs_reflection.hpp"
#include "slang_rs_export_func.hpp"
#include "slang_rs_reflect_utils.hpp"
#include <ctype.h>
#include <utility>
#include <cstdarg>
#include "llvm/ADT/APFloat.h"
using std::make_pair;
using std::endl;
#define RS_SCRIPT_CLASS_NAME_PREFIX "ScriptC_"
#define RS_SCRIPT_CLASS_SUPER_CLASS_NAME "ScriptC"
#define RS_TYPE_CLASS_NAME_PREFIX "ScriptField_"
#define RS_TYPE_CLASS_SUPER_CLASS_NAME "android.renderscript.Script.FieldBase"
#define RS_TYPE_ITEM_CLASS_NAME "Item"
#define RS_TYPE_ITEM_BUFFER_NAME "mItemArray"
#define RS_TYPE_ITEM_BUFFER_PACKER_NAME "mIOBuffer"
#define RS_EXPORT_VAR_INDEX_PREFIX "mExportVarIdx_"
#define RS_EXPORT_VAR_PREFIX "mExportVar_"
#define RS_EXPORT_FUNC_INDEX_PREFIX "mExportFuncIdx_"
#define RS_EXPORT_VAR_ALLOCATION_PREFIX "mAlloction_"
#define RS_EXPORT_VAR_DATA_STORAGE_PREFIX "mData_"
namespace slang {
/* Some utility function using internal in RSReflection */
static bool GetClassNameFromFileName(const std::string& FileName, std::string& ClassName) {
ClassName.clear();
if(FileName.empty() || (FileName == "-"))
return true;
ClassName = RSSlangReflectUtils::JavaClassNameFromRSFileName(FileName.c_str());
return true;
}
static const char* GetPrimitiveTypeName(const RSExportPrimitiveType* EPT) {
static const char* PrimitiveTypeJavaNameMap[] = {
"",
"",
"float", /* RSExportPrimitiveType::DataTypeFloat32 */
"double", /* RSExportPrimitiveType::DataTypeFloat64 */
"byte", /* RSExportPrimitiveType::DataTypeSigned8 */
"short", /* RSExportPrimitiveType::DataTypeSigned16 */
"int", /* RSExportPrimitiveType::DataTypeSigned32 */
"long", /* RSExportPrimitiveType::DataTypeSigned64 */
"short", /* RSExportPrimitiveType::DataTypeUnsigned8 */
"int", /* RSExportPrimitiveType::DataTypeUnsigned16 */
"long", /* RSExportPrimitiveType::DataTypeUnsigned32 */
"long", /* RSExportPrimitiveType::DataTypeUnsigned64 */
"int", /* RSExportPrimitiveType::DataTypeUnsigned565 */
"int", /* RSExportPrimitiveType::DataTypeUnsigned5551 */
"int", /* RSExportPrimitiveType::DataTypeUnsigned4444 */
"boolean", /* RSExportPrimitiveType::DataTypeBool */
"Element", /* RSExportPrimitiveType::DataTypeRSElement */
"Type", /* RSExportPrimitiveType::DataTypeRSType */
"Allocation", /* RSExportPrimitiveType::DataTypeRSAllocation */
"Sampler", /* RSExportPrimitiveType::DataTypeRSSampler */
"Script", /* RSExportPrimitiveType::DataTypeRSScript */
"Mesh", /* RSExportPrimitiveType::DataTypeRSMesh */
"ProgramFragment", /* RSExportPrimitiveType::DataTypeRSProgramFragment */
"ProgramVertex", /* RSExportPrimitiveType::DataTypeRSProgramVertex */
"ProgramRaster", /* RSExportPrimitiveType::DataTypeRSProgramRaster */
"ProgramStore", /* RSExportPrimitiveType::DataTypeRSProgramStore */
"Font", /* RSExportPrimitiveType::DataTypeRSFont */
"Matrix2f", /* RSExportPrimitiveType::DataTypeRSMatrix2x2 */
"Matrix3f", /* RSExportPrimitiveType::DataTypeRSMatrix3x3 */
"Matrix4f", /* RSExportPrimitiveType::DataTypeRSMatrix4x4 */
};
unsigned TypeId = EPT->getType();
if(TypeId < (sizeof(PrimitiveTypeJavaNameMap) / sizeof(const char*))) {
// printf("Type %d\n", EPT->getType());
return PrimitiveTypeJavaNameMap[ EPT->getType() ];
}
assert(false && "GetPrimitiveTypeName : Unknown primitive data type");
return NULL;
}
static const char* GetVectorTypeName(const RSExportVectorType* EVT) {
static const char* VectorTypeJavaNameMap[][3] = {
/* 0 */ { "Byte2", "Byte3", "Byte4" },
/* 1 */ { "Short2", "Short3", "Short4" },
/* 2 */ { "Int2", "Int3", "Int4" },
/* 3 */ { "Long2", "Long3", "Long4" },
/* 4 */ { "Float2", "Float3", "Float4" },
};
const char** BaseElement = NULL;
switch(EVT->getType()) {
case RSExportPrimitiveType::DataTypeSigned8:
BaseElement = VectorTypeJavaNameMap[0];
break;
case RSExportPrimitiveType::DataTypeSigned16:
case RSExportPrimitiveType::DataTypeUnsigned8:
BaseElement = VectorTypeJavaNameMap[1];
break;
case RSExportPrimitiveType::DataTypeSigned32:
case RSExportPrimitiveType::DataTypeUnsigned16:
BaseElement = VectorTypeJavaNameMap[2];
break;
case RSExportPrimitiveType::DataTypeUnsigned32:
BaseElement = VectorTypeJavaNameMap[3];
break;
case RSExportPrimitiveType::DataTypeFloat32:
BaseElement = VectorTypeJavaNameMap[4];
break;
case RSExportPrimitiveType::DataTypeBool:
BaseElement = VectorTypeJavaNameMap[0];
break;
default:
assert(false && "RSReflection::genElementTypeName : Unsupported vector element data type");
break;
}
assert((EVT->getNumElement() > 1) && (EVT->getNumElement() <= 4) && "Number of element in vector type is invalid");
return BaseElement[EVT->getNumElement() - 2];
}
static const char* GetVectorAccessor(int Index) {
static const char* VectorAccessorMap[] = {
/* 0 */ "x",
/* 1 */ "y",
/* 2 */ "z",
/* 3 */ "w",
};
assert((Index >= 0) && (Index < (sizeof(VectorAccessorMap) / sizeof(const char*))) && "Out-of-bound index to access vector member");
return VectorAccessorMap[Index];
}
static const char* GetPackerAPIName(const RSExportPrimitiveType* EPT) {
static const char* PrimitiveTypePackerAPINameMap[] = {
"",
"",
"addF32", /* RSExportPrimitiveType::DataTypeFloat32 */
"addF64", /* RSExportPrimitiveType::DataTypeFloat64 */
"addI8", /* RSExportPrimitiveType::DataTypeSigned8 */
"addI16", /* RSExportPrimitiveType::DataTypeSigned16 */
"addI32", /* RSExportPrimitiveType::DataTypeSigned32 */
"addI64", /* RSExportPrimitiveType::DataTypeSigned64 */
"addU8", /* RSExportPrimitiveType::DataTypeUnsigned8 */
"addU16", /* RSExportPrimitiveType::DataTypeUnsigned16 */
"addU32", /* RSExportPrimitiveType::DataTypeUnsigned32 */
"addU64", /* RSExportPrimitiveType::DataTypeUnsigned64 */
"addU16", /* RSExportPrimitiveType::DataTypeUnsigned565 */
"addU16", /* RSExportPrimitiveType::DataTypeUnsigned5551 */
"addU16", /* RSExportPrimitiveType::DataTypeUnsigned4444 */
"addBoolean", /* RSExportPrimitiveType::DataTypeBool */
"addObj", /* RSExportPrimitiveType::DataTypeRSElement */
"addObj", /* RSExportPrimitiveType::DataTypeRSType */
"addObj", /* RSExportPrimitiveType::DataTypeRSAllocation */
"addObj", /* RSExportPrimitiveType::DataTypeRSSampler */
"addObj", /* RSExportPrimitiveType::DataTypeRSScript */
"addObj", /* RSExportPrimitiveType::DataTypeRSMesh */
"addObj", /* RSExportPrimitiveType::DataTypeRSProgramFragment */
"addObj", /* RSExportPrimitiveType::DataTypeRSProgramVertex */
"addObj", /* RSExportPrimitiveType::DataTypeRSProgramRaster */
"addObj", /* RSExportPrimitiveType::DataTypeRSProgramStore */
"addObj", /* RSExportPrimitiveType::DataTypeRSFont */
"addObj", /* RSExportPrimitiveType::DataTypeRSMatrix2x2 */
"addObj", /* RSExportPrimitiveType::DataTypeRSMatrix3x3 */
"addObj", /* RSExportPrimitiveType::DataTypeRSMatrix4x4 */
};
unsigned TypeId = EPT->getType();
if(TypeId < (sizeof(PrimitiveTypePackerAPINameMap) / sizeof(const char*)))
return PrimitiveTypePackerAPINameMap[ EPT->getType() ];
assert(false && "GetPackerAPIName : Unknown primitive data type");
return NULL;
}
static std::string GetTypeName(const RSExportType* ET) {
switch(ET->getClass()) {
case RSExportType::ExportClassPrimitive:
case RSExportType::ExportClassConstantArray:
return GetPrimitiveTypeName(static_cast<const RSExportPrimitiveType*>(ET));
break;
case RSExportType::ExportClassPointer:
{
const RSExportType* PointeeType = static_cast<const RSExportPointerType*>(ET)->getPointeeType();
if(PointeeType->getClass() != RSExportType::ExportClassRecord)
return "Allocation";
else
return RS_TYPE_CLASS_NAME_PREFIX + PointeeType->getName();
}
break;
case RSExportType::ExportClassVector:
return GetVectorTypeName(static_cast<const RSExportVectorType*>(ET));
break;
case RSExportType::ExportClassRecord:
return RS_TYPE_CLASS_NAME_PREFIX + ET->getName() + "."RS_TYPE_ITEM_CLASS_NAME;
break;
default:
assert(false && "Unknown class of type");
break;
}
return "";
}
static const char* GetBuiltinElementConstruct(const RSExportType* ET) {
if (ET->getClass() == RSExportType::ExportClassPrimitive || ET->getClass() == RSExportType::ExportClassConstantArray) {
const RSExportPrimitiveType* EPT = static_cast<const RSExportPrimitiveType*>(ET);
if (EPT->getKind() == RSExportPrimitiveType::DataKindUser) {
static const char* PrimitiveBuiltinElementConstructMap[] = {
NULL,
NULL,
"F32", /* RSExportPrimitiveType::DataTypeFloat32 */
"F64", /* RSExportPrimitiveType::DataTypeFloat64 */
"I8", /* RSExportPrimitiveType::DataTypeSigned8 */
NULL, /* RSExportPrimitiveType::DataTypeSigned16 */
"I32", /* RSExportPrimitiveType::DataTypeSigned32 */
NULL, /* RSExportPrimitiveType::DataTypeSigned64 */
"U8", /* RSExportPrimitiveType::DataTypeUnsigned8 */
NULL, /* RSExportPrimitiveType::DataTypeUnsigned16 */
"U32", /* RSExportPrimitiveType::DataTypeUnsigned32 */
NULL, /* RSExportPrimitiveType::DataTypeUnsigned64 */
NULL, /* RSExportPrimitiveType::DataTypeUnsigned565 */
NULL, /* RSExportPrimitiveType::DataTypeUnsigned5551 */
NULL, /* RSExportPrimitiveType::DataTypeUnsigned4444 */
"BOOLEAN", /* RSExportPrimitiveType::DataTypeBool */
"ELEMENT", /* RSExportPrimitiveType::DataTypeRSElement */
"TYPE", /* RSExportPrimitiveType::DataTypeRSType */
"ALLOCATION", /* RSExportPrimitiveType::DataTypeRSAllocation */
"SAMPLER", /* RSExportPrimitiveType::DataTypeRSSampler */
"SCRIPT", /* RSExportPrimitiveType::DataTypeRSScript */
"MESH", /* RSExportPrimitiveType::DataTypeRSMesh */
"PROGRAM_FRAGMENT", /* RSExportPrimitiveType::DataTypeRSProgramFragment */
"PROGRAM_VERTEX", /* RSExportPrimitiveType::DataTypeRSProgramVertex */
"PROGRAM_RASTER", /* RSExportPrimitiveType::DataTypeRSProgramRaster */
"PROGRAM_STORE", /* RSExportPrimitiveType::DataTypeRSProgramStore */
"FONT", /* RSExportPrimitiveType::DataTypeRSFont */
"MATRIX_2X2", /* RSExportPrimitiveType::DataTypeRSMatrix2x2 */
"MATRIX_3X3", /* RSExportPrimitiveType::DataTypeRSMatrix3x3 */
"MATRIX_4X4", /* RSExportPrimitiveType::DataTypeRSMatrix4x4 */
};
unsigned TypeId = EPT->getType();
if (TypeId < (sizeof(PrimitiveBuiltinElementConstructMap) / sizeof(const char*)))
return PrimitiveBuiltinElementConstructMap[ EPT->getType() ];
} else if (EPT->getKind() == RSExportPrimitiveType::DataKindPixelA) {
if (EPT->getType() == RSExportPrimitiveType::DataTypeUnsigned8)
return "A_8";
} else if (EPT->getKind() == RSExportPrimitiveType::DataKindPixelRGB) {
if (EPT->getType() == RSExportPrimitiveType::DataTypeUnsigned565)
return "RGB_565";
else if (EPT->getType() == RSExportPrimitiveType::DataTypeUnsigned8)
return "RGB_888";
} else if (EPT->getKind() == RSExportPrimitiveType::DataKindPixelRGBA) {
if (EPT->getType() == RSExportPrimitiveType::DataTypeUnsigned5551)
return "RGB_5551";
else if (EPT->getType() == RSExportPrimitiveType::DataTypeUnsigned4444)
return "RGB_4444";
else if (EPT->getType() == RSExportPrimitiveType::DataTypeUnsigned8)
return "RGB_8888";
} else if (EPT->getKind() == RSExportPrimitiveType::DataKindIndex) {
if (EPT->getType() == RSExportPrimitiveType::DataTypeUnsigned16)
return "INDEX_16";
}
} else if (ET->getClass() == RSExportType::ExportClassVector) {
const RSExportVectorType* EVT = static_cast<const RSExportVectorType*>(ET);
if (EVT->getKind() == RSExportPrimitiveType::DataKindPosition) {
if (EVT->getType() == RSExportPrimitiveType::DataTypeFloat32) {
if (EVT->getNumElement() == 2)
return "ATTRIB_POSITION_2";
else if (EVT->getNumElement() == 3)
return "ATTRIB_POSITION_3";
}
} else if (EVT->getKind() == RSExportPrimitiveType::DataKindTexture) {
if (EVT->getType() == RSExportPrimitiveType::DataTypeFloat32) {
if (EVT->getNumElement() == 2)
return "ATTRIB_TEXTURE_2";
}
} else if (EVT->getKind() == RSExportPrimitiveType::DataKindNormal) {
if (EVT->getType() == RSExportPrimitiveType::DataTypeFloat32) {
if (EVT->getNumElement() == 3)
return "ATTRIB_NORMAL_3";
}
} else if (EVT->getKind() == RSExportPrimitiveType::DataKindColor) {
if (EVT->getType() == RSExportPrimitiveType::DataTypeFloat32) {
if (EVT->getNumElement() == 4)
return "ATTRIB_COLOR_F32_4";
} else if (EVT->getType() == RSExportPrimitiveType::DataTypeUnsigned8) {
if (EVT->getNumElement() == 4)
return "ATTRIB_COLOR_U8_4";
}
}
} else if (ET->getClass() == RSExportType::ExportClassPointer) {
return "USER_I32"; /* tread pointer type variable as unsigned int (TODO: this is target dependent) */
}
return NULL;
}
static const char* GetElementDataKindName(RSExportPrimitiveType::DataKind DK) {
static const char* ElementDataKindNameMap[] = {
"Element.DataKind.USER", /* RSExportPrimitiveType::DataKindUser */
"Element.DataKind.COLOR", /* RSExportPrimitiveType::DataKindColor */
"Element.DataKind.POSITION", /* RSExportPrimitiveType::DataKindPosition */
"Element.DataKind.TEXTURE", /* RSExportPrimitiveType::DataKindTexture */
"Element.DataKind.NORMAL", /* RSExportPrimitiveType::DataKindNormal */
"Element.DataKind.INDEX", /* RSExportPrimitiveType::DataKindIndex */
"Element.DataKind.POINT_SIZE", /* RSExportPrimitiveType::DataKindPointSize */
"Element.DataKind.PIXEL_L", /* RSExportPrimitiveType::DataKindPixelL */
"Element.DataKind.PIXEL_A", /* RSExportPrimitiveType::DataKindPixelA */
"Element.DataKind.PIXEL_LA", /* RSExportPrimitiveType::DataKindPixelLA */
"Element.DataKind.PIXEL_RGB", /* RSExportPrimitiveType::DataKindPixelRGB */
"Element.DataKind.PIXEL_RGBA", /* RSExportPrimitiveType::DataKindPixelRGBA */
};
if(static_cast<unsigned>(DK) < (sizeof(ElementDataKindNameMap) / sizeof(const char*)))
return ElementDataKindNameMap[ DK ];
else
return NULL;
}
static const char* GetElementDataTypeName(RSExportPrimitiveType::DataType DT) {
static const char* ElementDataTypeNameMap[] = {
NULL,
NULL,
"Element.DataType.FLOAT_32", /* RSExportPrimitiveType::DataTypeFloat32 */
"Element.DataType.FLOAT_64", /* RSExportPrimitiveType::DataTypeFloat64 */
"Element.DataType.SIGNED_8", /* RSExportPrimitiveType::DataTypeSigned8 */
"Element.DataType.SIGNED_16", /* RSExportPrimitiveType::DataTypeSigned16 */
"Element.DataType.SIGNED_32", /* RSExportPrimitiveType::DataTypeSigned32 */
NULL, /* RSExportPrimitiveType::DataTypeSigned64 */
"Element.DataType.UNSIGNED_8", /* RSExportPrimitiveType::DataTypeUnsigned8 */
"Element.DataType.UNSIGNED_16", /* RSExportPrimitiveType::DataTypeUnsigned16 */
"Element.DataType.UNSIGNED_32", /* RSExportPrimitiveType::DataTypeUnsigned32 */
NULL, /* RSExportPrimitiveType::DataTypeUnsigned64 */
"Element.DataType.UNSIGNED_5_6_5", /* RSExportPrimitiveType::DataTypeUnsigned565 */
"Element.DataType.UNSIGNED_5_5_5_1", /* RSExportPrimitiveType::DataTypeUnsigned5551 */
"Element.DataType.UNSIGNED_4_4_4_4", /* RSExportPrimitiveType::DataTypeUnsigned4444 */
"Element.DataType.BOOLEAN", /* RSExportPrimitiveType::DataTypeBool */
"Element.DataType.RS_ELEMENT", /* RSExportPrimitiveType::DataTypeRSElement */
"Element.DataType.RS_TYPE", /* RSExportPrimitiveType::DataTypeRSType */
"Element.DataType.RS_ALLOCATION", /* RSExportPrimitiveType::DataTypeRSAllocation */
"Element.DataType.RS_SAMPLER", /* RSExportPrimitiveType::DataTypeRSSampler */
"Element.DataType.RS_SCRIPT", /* RSExportPrimitiveType::DataTypeRSScript */
"Element.DataType.RS_MESH", /* RSExportPrimitiveType::DataTypeRSMesh */
"Element.DataType.RS_PROGRAM_FRAGMENT", /* RSExportPrimitiveType::DataTypeRSProgramFragment */
"Element.DataType.RS_PROGRAM_VERTEX", /* RSExportPrimitiveType::DataTypeRSProgramVertex */
"Element.DataType.RS_PROGRAM_RASTER", /* RSExportPrimitiveType::DataTypeRSProgramRaster */
"Element.DataType.RS_PROGRAM_STORE", /* RSExportPrimitiveType::DataTypeRSProgramStore */
"Element.DataType.RS_FONT", /* RSExportPrimitiveType::DataTypeRSFont */
"Element.DataType.RS_MATRIX_2X2", /* RSExportPrimitiveType::DataTypeRSMatrix2x2 */
"Element.DataType.RS_MATRIX_3X3", /* RSExportPrimitiveType::DataTypeRSMatrix3x3 */
"Element.DataType.RS_MATRIX_4X4", /* RSExportPrimitiveType::DataTypeRSMatrix4x4 */
};
if(static_cast<unsigned>(DT) < (sizeof(ElementDataTypeNameMap) / sizeof(const char*)))
return ElementDataTypeNameMap[ DT ];
else
return NULL;
}
bool RSReflection::openScriptFile(Context&C, const std::string& ClassName, std::string& ErrorMsg) {
if(!C.mUseStdout) {
C.mOF.clear();
std::string _path = RSSlangReflectUtils::ComputePackagedPath(
mRSContext->getReflectJavaPathName().c_str(), C.getPackageName().c_str());
RSSlangReflectUtils::mkdir_p(_path.c_str());
C.mOF.open(( _path + "/" + ClassName + ".java" ).c_str());
if(!C.mOF.good()) {
ErrorMsg = "failed to open file '" + _path + "/" + ClassName + ".java' for write";
return false;
}
}
return true;
}
/****************************** Methods to generate script class ******************************/
bool RSReflection::genScriptClass(Context& C, const std::string& ClassName, std::string& ErrorMsg) {
/* Open the file */
if (!openScriptFile(C, ClassName, ErrorMsg)) {
return false;
}
if(!C.startClass(Context::AM_Public, false, ClassName, RS_SCRIPT_CLASS_SUPER_CLASS_NAME, ErrorMsg))
return false;
genScriptClassConstructor(C);
/* Reflect export variable */
for(RSContext::const_export_var_iterator I = mRSContext->export_vars_begin();
I != mRSContext->export_vars_end();
I++)
genExportVariable(C, *I);
/* Reflect export function */
for(RSContext::const_export_func_iterator I = mRSContext->export_funcs_begin();
I != mRSContext->export_funcs_end();
I++)
genExportFunction(C, *I);
C.endClass();
return true;
}
void RSReflection::genScriptClassConstructor(Context& C) {
C.indent() << "// Constructor" << endl;
C.startFunction(Context::AM_Public, false, NULL, C.getClassName(), 4, "RenderScript", "rs",
"Resources", "resources",
"int", "id",
"boolean", "isRoot");
/* Call constructor of super class */
C.indent() << "super(rs, resources, id, isRoot);" << endl;
/* If an exported variable has initial value, reflect it */
for(RSContext::const_export_var_iterator I = mRSContext->export_vars_begin();
I != mRSContext->export_vars_end();
I++)
{
const RSExportVar* EV = *I;
if(!EV->getInit().isUninit())
genInitExportVariable(C, EV->getType(), EV->getName(), EV->getInit());
}
C.endFunction();
return;
}
void RSReflection::genInitBoolExportVariable(Context& C, const std::string& VarName, const APValue& Val) {
assert(!Val.isUninit() && "Not a valid initializer");
C.indent() << RS_EXPORT_VAR_PREFIX << VarName << " = ";
assert((Val.getKind() == APValue::Int) && "Bool type has wrong initial APValue");
if (Val.getInt().getSExtValue() == 0) {
C.out() << "false";
} else {
C.out() << "true";
}
C.out() << ";" << endl;
return;
}
void RSReflection::genInitPrimitiveExportVariable(Context& C, const std::string& VarName, const APValue& Val) {
assert(!Val.isUninit() && "Not a valid initializer");
C.indent() << RS_EXPORT_VAR_PREFIX << VarName << " = ";
switch(Val.getKind()) {
case APValue::Int: C.out() << Val.getInt().getSExtValue(); break;
case APValue::Float: {
llvm::APFloat apf = Val.getFloat();
if (apf.semanticsPrecision(apf.getSemantics()) == 24 /*llvm::APFloat::IEEEsingle*/) {
C.out() << apf.convertToFloat() << "f";
} else {
C.out() << apf.convertToDouble();
}
break;
}
case APValue::ComplexInt:
case APValue::ComplexFloat:
case APValue::LValue:
case APValue::Vector:
assert(false && "Primitive type cannot have such kind of initializer");
break;
default:
assert(false && "Unknown kind of initializer");
break;
}
C.out() << ";" << endl;
return;
}
void RSReflection::genInitExportVariable(Context& C, const RSExportType* ET, const std::string& VarName, const APValue& Val) {
assert(!Val.isUninit() && "Not a valid initializer");
switch(ET->getClass()) {
case RSExportType::ExportClassPrimitive:
case RSExportType::ExportClassConstantArray:
{
const RSExportPrimitiveType* EPT = static_cast<const RSExportPrimitiveType*>(ET);
if (EPT->getType() == RSExportPrimitiveType::DataTypeBool) {
genInitBoolExportVariable(C, VarName, Val);
} else {
genInitPrimitiveExportVariable(C, VarName, Val);
}
break;
}
case RSExportType::ExportClassPointer:
if(!Val.isInt() || Val.getInt().getSExtValue() != 0)
std::cout << "Initializer which is non-NULL to pointer type variable will be ignored" << endl;
break;
case RSExportType::ExportClassVector:
{
const RSExportVectorType* EVT = static_cast<const RSExportVectorType*>(ET);
switch(Val.getKind()) {
case APValue::Int:
case APValue::Float:
for(int i=0;i<EVT->getNumElement();i++) {
std::string Name = VarName + "." + GetVectorAccessor(i);
genInitPrimitiveExportVariable(C, Name, Val);
}
break;
case APValue::Vector:
{
C.indent() << RS_EXPORT_VAR_PREFIX << VarName << " = new " << GetVectorTypeName(EVT) << "();" << endl;
unsigned NumElements = std::min(static_cast<unsigned>(EVT->getNumElement()), Val.getVectorLength());
for(unsigned i=0;i<NumElements;i++) {
const APValue& ElementVal = Val.getVectorElt(i);
std::string Name = VarName + "." + GetVectorAccessor(i);
genInitPrimitiveExportVariable(C, Name, ElementVal);
}
}
break;
case APValue::Uninitialized:
case APValue::ComplexInt:
case APValue::ComplexFloat:
case APValue::LValue:
assert(false && "Unexpected type of value of initializer.");
break;
}
}
break;
/* TODO: Resolving initializer of a record type variable is complex. It cannot obtain by just simply evaluating the initializer expression. */
case RSExportType::ExportClassRecord:
{
/*
unsigned InitIndex = 0;
const RSExportRecordType* ERT = static_cast<const RSExportRecordType*>(ET);
assert((Val.getKind() == APValue::Vector) && "Unexpected type of initializer for record type variable");
C.indent() << RS_EXPORT_VAR_PREFIX << VarName << " = new "RS_TYPE_CLASS_NAME_PREFIX << ERT->getName() << "."RS_TYPE_ITEM_CLASS_NAME"();" << endl;
for(RSExportRecordType::const_field_iterator I = ERT->fields_begin();
I != ERT->fields_end();
I++)
{
const RSExportRecordType::Field* F = *I;
std::string FieldName = VarName + "." + F->getName();
if(InitIndex > Val.getVectorLength())
break;
genInitPrimitiveExportVariable(C, FieldName, Val.getVectorElt(InitIndex++));
}
*/
assert(false && "Unsupported initializer for record type variable currently");
}
break;
default:
assert(false && "Unknown class of type");
break;
}
return;
}
void RSReflection::genExportVariable(Context& C, const RSExportVar* EV) {
const RSExportType* ET = EV->getType();
C.indent() << "private final static int "RS_EXPORT_VAR_INDEX_PREFIX << EV->getName() << " = " << C.getNextExportVarSlot() << ";" << endl;
switch(ET->getClass()) {
case RSExportType::ExportClassPrimitive:
case RSExportType::ExportClassConstantArray:
genPrimitiveTypeExportVariable(C, EV);
break;
case RSExportType::ExportClassPointer:
genPointerTypeExportVariable(C, EV);
break;
case RSExportType::ExportClassVector:
genVectorTypeExportVariable(C, EV);
break;
case RSExportType::ExportClassRecord:
genRecordTypeExportVariable(C, EV);
break;
default:
assert(false && "Unknown class of type");
break;
}
return;
}
void RSReflection::genExportFunction(Context& C, const RSExportFunc* EF) {
C.indent() << "private final static int "RS_EXPORT_FUNC_INDEX_PREFIX << EF->getName() << " = " << C.getNextExportFuncSlot() << ";" << endl;
/* invoke_*() */
Context::ArgTy Args;
for(RSExportFunc::const_param_iterator I = EF->params_begin();
I != EF->params_end();
I++)
{
const RSExportFunc::Parameter* P = *I;
Args.push_back( make_pair(GetTypeName(P->getType()), P->getName()) );
}
C.startFunction(Context::AM_Public, false, "void", "invoke_" + EF->getName(), Args);
if(!EF->hasParam()) {
C.indent() << "invoke("RS_EXPORT_FUNC_INDEX_PREFIX << EF->getName() << ");" << endl;
} else {
const RSExportRecordType* ERT = EF->getParamPacketType();
std::string FieldPackerName = EF->getName() + "_fp";
if(genCreateFieldPacker(C, ERT, FieldPackerName.c_str()))
genPackVarOfType(C, ERT, NULL, FieldPackerName.c_str());
C.indent() << "invoke("RS_EXPORT_FUNC_INDEX_PREFIX << EF->getName() << ", " << FieldPackerName << ");" << endl;
}
C.endFunction();
return;
}
void RSReflection::genPrimitiveTypeExportVariable(Context& C, const RSExportVar* EV) {
assert(( EV->getType()->getClass() == RSExportType::ExportClassPrimitive ||
EV->getType()->getClass() == RSExportType::ExportClassConstantArray
) && "Variable should be type of primitive here");
const RSExportPrimitiveType* EPT = static_cast<const RSExportPrimitiveType*>(EV->getType());
const char* TypeName = GetPrimitiveTypeName(EPT);
C.indent() << "private " << TypeName << " "RS_EXPORT_VAR_PREFIX << EV->getName() << ";" << endl;
/* set_*() */
if(!EV->isConst()) {
C.startFunction(Context::AM_Public, false, "void", "set_" + EV->getName(), 1, TypeName, "v");
C.indent() << RS_EXPORT_VAR_PREFIX << EV->getName() << " = v;" << endl;
if(EPT->isRSObjectType())
C.indent() << "setVar("RS_EXPORT_VAR_INDEX_PREFIX << EV->getName() << ", (v == null) ? 0 : v.getID());" << endl;
else
C.indent() << "setVar("RS_EXPORT_VAR_INDEX_PREFIX << EV->getName() << ", v);" << endl;
C.endFunction();
}
genGetExportVariable(C, TypeName, EV->getName());
return;
}
void RSReflection::genPointerTypeExportVariable(Context& C, const RSExportVar* EV) {
const RSExportType* ET = EV->getType();
const RSExportType* PointeeType;
std::string TypeName;
assert((ET->getClass() == RSExportType::ExportClassPointer) && "Variable should be type of pointer here");
PointeeType = static_cast<const RSExportPointerType*>(ET)->getPointeeType();
TypeName = GetTypeName(ET);
/* bind_*() */
C.indent() << "private " << TypeName << " "RS_EXPORT_VAR_PREFIX << EV->getName() << ";" << endl;
C.startFunction(Context::AM_Public, false, "void", "bind_" + EV->getName(), 1, TypeName.c_str(), "v");
C.indent() << RS_EXPORT_VAR_PREFIX << EV->getName() << " = v;" << endl;
C.indent() << "if(v == null) bindAllocation(null, "RS_EXPORT_VAR_INDEX_PREFIX << EV->getName() << ");" << endl;
if(PointeeType->getClass() == RSExportType::ExportClassRecord)
C.indent() << "else bindAllocation(v.getAllocation(), "RS_EXPORT_VAR_INDEX_PREFIX << EV->getName() << ");" << endl;
else
C.indent() << "else bindAllocation(v, "RS_EXPORT_VAR_INDEX_PREFIX << EV->getName() << ");" << endl;
C.endFunction();
genGetExportVariable(C, TypeName, EV->getName());
return;
}
void RSReflection::genVectorTypeExportVariable(Context& C, const RSExportVar* EV) {
assert((EV->getType()->getClass() == RSExportType::ExportClassVector) && "Variable should be type of vector here");
const RSExportVectorType* EVT = static_cast<const RSExportVectorType*>(EV->getType());
const char* TypeName = GetVectorTypeName(EVT);
const char* FieldPackerName = "fp";
C.indent() << "private " << TypeName << " "RS_EXPORT_VAR_PREFIX << EV->getName() << ";" << endl;
/* set_*() */
if(!EV->isConst()) {
C.startFunction(Context::AM_Public, false, "void", "set_" + EV->getName(), 1, TypeName, "v");
C.indent() << RS_EXPORT_VAR_PREFIX << EV->getName() << " = v;" << endl;
if(genCreateFieldPacker(C, EVT, FieldPackerName))
genPackVarOfType(C, EVT, "v", FieldPackerName);
C.indent() << "setVar("RS_EXPORT_VAR_INDEX_PREFIX << EV->getName() << ", " << FieldPackerName << ");" << endl;
C.endFunction();
}
genGetExportVariable(C, TypeName, EV->getName());
return;
}
void RSReflection::genRecordTypeExportVariable(Context& C, const RSExportVar* EV) {
assert((EV->getType()->getClass() == RSExportType::ExportClassRecord) && "Variable should be type of struct here");
const RSExportRecordType* ERT = static_cast<const RSExportRecordType*>(EV->getType());
std::string TypeName = RS_TYPE_CLASS_NAME_PREFIX + ERT->getName() + "."RS_TYPE_ITEM_CLASS_NAME;
const char* FieldPackerName = "fp";
C.indent() << "private " << TypeName << " "RS_EXPORT_VAR_PREFIX << EV->getName() << ";" << endl;
/* set_*() */
if(!EV->isConst()) {
C.startFunction(Context::AM_Public, false, "void", "set_" + EV->getName(), 1, TypeName.c_str(), "v");
C.indent() << RS_EXPORT_VAR_PREFIX << EV->getName() << " = v;" << endl;
if(genCreateFieldPacker(C, ERT, FieldPackerName))
genPackVarOfType(C, ERT, "v", FieldPackerName);
C.indent() << "setVar("RS_EXPORT_VAR_INDEX_PREFIX << EV->getName() << ", " << FieldPackerName << ");" << endl;
C.endFunction();
}
genGetExportVariable(C, TypeName.c_str(), EV->getName());
return;
}
void RSReflection::genGetExportVariable(Context& C, const std::string& TypeName, const std::string& VarName) {
C.startFunction(Context::AM_Public, false, TypeName.c_str(), "get_" + VarName, 0);
C.indent() << "return "RS_EXPORT_VAR_PREFIX << VarName << ";" << endl;
C.endFunction();
return;
}
/****************************** Methods to generate script class /end ******************************/
bool RSReflection::genCreateFieldPacker(Context& C, const RSExportType* ET, const char* FieldPackerName) {
size_t AllocSize = RSExportType::GetTypeAllocSize(ET);
if(AllocSize > 0)
C.indent() << "FieldPacker " << FieldPackerName << " = new FieldPacker(" << AllocSize << ");" << endl;
else
return false;
return true;
}
void RSReflection::genPackVarOfType(Context& C, const RSExportType* ET, const char* VarName, const char* FieldPackerName) {
switch(ET->getClass()) {
case RSExportType::ExportClassPrimitive:
case RSExportType::ExportClassVector:
C.indent() << FieldPackerName << "." << GetPackerAPIName(static_cast<const RSExportPrimitiveType*>(ET)) << "(" << VarName << ");" << endl;
break;
case RSExportType::ExportClassConstantArray: {
if (ET->getName().compare("addObj") == 0) {
C.indent() << FieldPackerName << "." << "addObj" << "(" << VarName << ");" << endl;
} else {
C.indent() << FieldPackerName << "." << GetPackerAPIName(static_cast<const RSExportPrimitiveType*>(ET)) << "(" << VarName << ");" << endl;
}
break;
}
case RSExportType::ExportClassPointer:
{
/* Must reflect as type Allocation in Java */
const RSExportType* PointeeType = static_cast<const RSExportPointerType*>(ET)->getPointeeType();
if(PointeeType->getClass() != RSExportType::ExportClassRecord)
C.indent() << FieldPackerName << ".addI32(" << VarName << ".getPtr());" << endl;
else
C.indent() << FieldPackerName << ".addI32(" << VarName << ".getAllocation().getPtr());" << endl;
}
break;
case RSExportType::ExportClassRecord:
{
const RSExportRecordType* ERT = static_cast<const RSExportRecordType*>(ET);
unsigned Pos = 0; /* relative pos from now on in field packer */
for(RSExportRecordType::const_field_iterator I = ERT->fields_begin();
I != ERT->fields_end();
I++)
{
const RSExportRecordType::Field* F = *I;
std::string FieldName;
size_t FieldOffset = F->getOffsetInParent();
size_t FieldStoreSize = RSExportType::GetTypeStoreSize(F->getType());
size_t FieldAllocSize = RSExportType::GetTypeAllocSize(F->getType());
if(VarName != NULL)
FieldName = VarName + ("." + F->getName());
else
FieldName = F->getName();
if(FieldOffset > Pos)
C.indent() << FieldPackerName << ".skip(" << (FieldOffset - Pos) << ");" << endl;
genPackVarOfType(C, F->getType(), FieldName.c_str(), FieldPackerName);
if(FieldAllocSize > FieldStoreSize) /* there's padding in the field type */
C.indent() << FieldPackerName << ".skip(" << (FieldAllocSize - FieldStoreSize) << ");" << endl;
Pos = FieldOffset + FieldAllocSize;
}
/* There maybe some padding after the struct */
size_t Padding = RSExportType::GetTypeAllocSize(ERT) - Pos;
if(Padding > 0)
C.indent() << FieldPackerName << ".skip(" << Padding << ");" << endl;
}
break;
default:
assert(false && "Unknown class of type");
break;
}
return;
}
void RSReflection::genNewItemBufferIfNull(Context& C, const char* Index) {
C.indent() << "if ("RS_TYPE_ITEM_BUFFER_NAME" == null) "
RS_TYPE_ITEM_BUFFER_NAME" = new "RS_TYPE_ITEM_CLASS_NAME"[mType.getX() /* count */];" << endl;
if (Index != NULL)
C.indent() << "if ("RS_TYPE_ITEM_BUFFER_NAME"[" << Index << "] == null) "
RS_TYPE_ITEM_BUFFER_NAME"[" << Index << "] = new "RS_TYPE_ITEM_CLASS_NAME"();" << endl;
return;
}
void RSReflection::genNewItemBufferPackerIfNull(Context& C) {
C.indent() << "if ("RS_TYPE_ITEM_BUFFER_PACKER_NAME" == null) "
RS_TYPE_ITEM_BUFFER_PACKER_NAME" = new FieldPacker("RS_TYPE_ITEM_CLASS_NAME".sizeof * mType.getX() /* count */);" << endl;
return;
}
/****************************** Methods to generate type class ******************************/
bool RSReflection::genTypeClass(Context& C, const RSExportRecordType* ERT, std::string& ErrorMsg) {
std::string ClassName = RS_TYPE_CLASS_NAME_PREFIX + ERT->getName();
/* Open the file */
if (!openScriptFile(C, ClassName, ErrorMsg)) {
return false;
}
if(!C.startClass(Context::AM_Public, false, ClassName, RS_TYPE_CLASS_SUPER_CLASS_NAME, ErrorMsg))
return false;
if(!genTypeItemClass(C, ERT, ErrorMsg))
return false;
/* Declare item buffer and item buffer packer */
C.indent() << "private "RS_TYPE_ITEM_CLASS_NAME" "RS_TYPE_ITEM_BUFFER_NAME"[];" << endl;
C.indent() << "private FieldPacker "RS_TYPE_ITEM_BUFFER_PACKER_NAME";" << endl;
genTypeClassConstructor(C, ERT);
genTypeClassCopyToArray(C, ERT);
genTypeClassItemSetter(C, ERT);
genTypeClassItemGetter(C, ERT);
genTypeClassComponentSetter(C, ERT);
genTypeClassComponentGetter(C, ERT);
genTypeClassCopyAll(C, ERT);
C.endClass();
return true;
}
bool RSReflection::genTypeItemClass(Context& C, const RSExportRecordType* ERT, std::string& ErrorMsg) {
C.indent() << "static public class "RS_TYPE_ITEM_CLASS_NAME;
C.startBlock();
C.indent() << "public static final int sizeof = " << RSExportType::GetTypeAllocSize(ERT) << ";" << endl;
/* Member elements */
C.out() << endl;
for(RSExportRecordType::const_field_iterator FI = ERT->fields_begin();
FI != ERT->fields_end();
FI++) {
C.indent() << GetTypeName((*FI)->getType()) << " " << (*FI)->getName() << ";" << endl;
}
/* Constructor */
C.out() << endl;
C.indent() << RS_TYPE_ITEM_CLASS_NAME"()";
C.startBlock();
for(RSExportRecordType::const_field_iterator FI = ERT->fields_begin();
FI != ERT->fields_end();
FI++)
{
const RSExportRecordType::Field* F = *FI;
if( (F->getType()->getClass() == RSExportType::ExportClassVector) ||
(F->getType()->getClass() == RSExportType::ExportClassRecord) ||
(F->getType()->getClass() == RSExportType::ExportClassConstantArray)
) {
C.indent() << F->getName() << " = new " << GetTypeName(F->getType()) << "();" << endl;
}
}
C.endBlock(); /* end Constructor */
C.endBlock(); /* end Item class */
return true;
}
void RSReflection::genTypeClassConstructor(Context& C, const RSExportRecordType* ERT) {
const char* RenderScriptVar = "rs";
C.startFunction(Context::AM_Public, true, "Element", "createElement", 1, "RenderScript", RenderScriptVar);
genBuildElement(C, ERT, RenderScriptVar);
C.endFunction();
C.startFunction(Context::AM_Public, false, NULL, C.getClassName(), 2, "RenderScript", RenderScriptVar,
"int", "count");
C.indent() << RS_TYPE_ITEM_BUFFER_NAME" = null;" << endl;
C.indent() << RS_TYPE_ITEM_BUFFER_PACKER_NAME" = null;" << endl;
C.indent() << "mElement = createElement(" << RenderScriptVar << ");" << endl;
/* Call init() in super class */
C.indent() << "init(" << RenderScriptVar << ", count);" << endl;
C.endFunction();
return;
}
void RSReflection::genTypeClassCopyToArray(Context& C, const RSExportRecordType* ERT) {
C.startFunction(Context::AM_Private, false, "void", "copyToArray", 2, RS_TYPE_ITEM_CLASS_NAME, "i",
"int", "index");
genNewItemBufferPackerIfNull(C);
C.indent() << RS_TYPE_ITEM_BUFFER_PACKER_NAME".reset(index * "RS_TYPE_ITEM_CLASS_NAME".sizeof);" << endl;
genPackVarOfType(C, ERT, "i", RS_TYPE_ITEM_BUFFER_PACKER_NAME);
C.endFunction();
return;
}
void RSReflection::genTypeClassItemSetter(Context& C, const RSExportRecordType* ERT) {
C.startFunction(Context::AM_Public, false, "void", "set", 3, RS_TYPE_ITEM_CLASS_NAME, "i",
"int", "index",
"boolean", "copyNow");
genNewItemBufferIfNull(C, NULL);
C.indent() << RS_TYPE_ITEM_BUFFER_NAME"[index] = i;" << endl;
C.indent() << "if (copyNow) ";
C.startBlock();
C.indent() << "copyToArray(i, index);" << endl;
C.indent() << "mAllocation.subData1D(index, 1, "RS_TYPE_ITEM_BUFFER_PACKER_NAME".getData());" << endl;
C.endBlock(); /* end if (copyNow) */
C.endFunction();
return;
}
void RSReflection::genTypeClassItemGetter(Context& C, const RSExportRecordType* ERT) {
C.startFunction(Context::AM_Public, false, RS_TYPE_ITEM_CLASS_NAME, "get", 1, "int", "index");
C.indent() << "if ("RS_TYPE_ITEM_BUFFER_NAME" == null) return null;" << endl;
C.indent() << "return "RS_TYPE_ITEM_BUFFER_NAME"[index];" << endl;
C.endFunction();
return;
}
void RSReflection::genTypeClassComponentSetter(Context& C, const RSExportRecordType* ERT) {
for(RSExportRecordType::const_field_iterator FI = ERT->fields_begin();
FI != ERT->fields_end();
FI++)
{
const RSExportRecordType::Field* F = *FI;
size_t FieldOffset = F->getOffsetInParent();
size_t FieldStoreSize = RSExportType::GetTypeStoreSize(F->getType());
unsigned FieldIndex = C.getFieldIndex(F);
C.startFunction(Context::AM_Public, false, "void", "set_" + F->getName(), 3, "int", "index",
GetTypeName(F->getType()).c_str(), "v",
"boolean", "copyNow");
genNewItemBufferPackerIfNull(C);
genNewItemBufferIfNull(C, "index");
C.indent() << RS_TYPE_ITEM_BUFFER_NAME"[index]." << F->getName() << " = v;" << endl;
C.indent() << "if (copyNow) ";
C.startBlock();
if(FieldOffset > 0)
C.indent() << RS_TYPE_ITEM_BUFFER_PACKER_NAME".reset(index * "RS_TYPE_ITEM_CLASS_NAME".sizeof + " << FieldOffset << ");" << endl;
else
C.indent() << RS_TYPE_ITEM_BUFFER_PACKER_NAME".reset(index * "RS_TYPE_ITEM_CLASS_NAME".sizeof);" << endl;
genPackVarOfType(C, F->getType(), "v", RS_TYPE_ITEM_BUFFER_PACKER_NAME);
C.indent() << "FieldPacker fp = new FieldPacker(" << FieldStoreSize << ");" << endl;
genPackVarOfType(C, F->getType(), "v", "fp");
C.indent() << "mAllocation.subElementData(index, " << 0/*FieldIndex*/ << ", fp);" << endl;
C.endBlock(); /* end if (copyNow) */
C.endFunction();
}
return;
}
void RSReflection::genTypeClassComponentGetter(Context& C, const RSExportRecordType* ERT) {
for(RSExportRecordType::const_field_iterator FI = ERT->fields_begin();
FI != ERT->fields_end();
FI++)
{
const RSExportRecordType::Field* F = *FI;
C.startFunction(Context::AM_Public, false, GetTypeName(F->getType()).c_str(), "get_" + F->getName(), 1, "int", "index");
//C.indent() << "if ("RS_TYPE_ITEM_BUFFER_NAME" == null) return null;" << endl;
C.indent() << "return "RS_TYPE_ITEM_BUFFER_NAME"[index]." << F->getName() << ";" << endl;
C.endFunction();
}
return;
}
void RSReflection::genTypeClassCopyAll(Context& C, const RSExportRecordType* ERT) {
C.startFunction(Context::AM_Public, false, "void", "copyAll", 0);
C.indent() << "for (int ct=0; ct < "RS_TYPE_ITEM_BUFFER_NAME".length; ct++) copyToArray("RS_TYPE_ITEM_BUFFER_NAME"[ct], ct);" << endl;
C.indent() << "mAllocation.data("RS_TYPE_ITEM_BUFFER_PACKER_NAME".getData());" << endl;
C.endFunction();
return;
}
/****************************** Methods to generate type class /end ******************************/
/******************** Methods to create Element in Java of given record type ********************/
void RSReflection::genBuildElement(Context& C, const RSExportRecordType* ERT, const char* RenderScriptVar) {
const char* ElementBuilderName = "eb";
/* Create element builder */
// C.startBlock(true);
C.indent() << "Element.Builder " << ElementBuilderName << " = new Element.Builder(" << RenderScriptVar << ");" << endl;
/* eb.add(...) */
genAddElementToElementBuilder(C, ERT, "", ElementBuilderName, RenderScriptVar);
C.indent() << "return " << ElementBuilderName << ".create();" << endl;
// C.endBlock();
return;
}
#define EB_ADD(x) \
C.indent() << ElementBuilderName << ".add(Element." << x << ", \"" << VarName << "\");" << endl; \
C.incFieldIndex()
void RSReflection::genAddElementToElementBuilder(Context& C, const RSExportType* ET, const std::string& VarName, const char* ElementBuilderName, const char* RenderScriptVar) {
const char* ElementConstruct = GetBuiltinElementConstruct(ET);
if(ElementConstruct != NULL) {
EB_ADD(ElementConstruct << "(" << RenderScriptVar << ")");
} else {
if ((ET->getClass() == RSExportType::ExportClassPrimitive) ||
(ET->getClass() == RSExportType::ExportClassVector) ||
(ET->getClass() == RSExportType::ExportClassConstantArray)
) {
const RSExportPrimitiveType* EPT = static_cast<const RSExportPrimitiveType*>(ET);
const char* DataKindName = GetElementDataKindName(EPT->getKind());
const char* DataTypeName = GetElementDataTypeName(EPT->getType());
int Size = (ET->getClass() == RSExportType::ExportClassVector) ? static_cast<const RSExportVectorType*>(ET)->getNumElement() : 1;
switch(EPT->getKind()) {
case RSExportPrimitiveType::DataKindColor:
case RSExportPrimitiveType::DataKindPosition:
case RSExportPrimitiveType::DataKindTexture:
case RSExportPrimitiveType::DataKindNormal:
case RSExportPrimitiveType::DataKindPointSize:
/* Element.createAttrib() */
EB_ADD("createAttrib(" << RenderScriptVar << ", " << DataTypeName << ", " << DataKindName << ", " << Size << ")");
break;
case RSExportPrimitiveType::DataKindIndex:
/* Element.createIndex() */
EB_ADD("createAttrib(" << RenderScriptVar << ")");
break;
case RSExportPrimitiveType::DataKindPixelL:
case RSExportPrimitiveType::DataKindPixelA:
case RSExportPrimitiveType::DataKindPixelLA:
case RSExportPrimitiveType::DataKindPixelRGB:
case RSExportPrimitiveType::DataKindPixelRGBA:
/* Element.createPixel() */
EB_ADD("createVector(" << RenderScriptVar << ", " << DataTypeName << ", " << DataKindName << ")");
break;
case RSExportPrimitiveType::DataKindUser:
default:
if (EPT->getClass() == RSExportType::ExportClassPrimitive || EPT->getClass() == RSExportType::ExportClassConstantArray) {
/* Element.createUser() */
EB_ADD("createUser(" << RenderScriptVar << ", " << DataTypeName << ")");
} else { /* (ET->getClass() == RSExportType::ExportClassVector) must hold here */
/* Element.createVector() */
EB_ADD("createVector(" << RenderScriptVar << ", " << DataTypeName << ", " << Size << ")");
}
break;
}
} else if(ET->getClass() == RSExportType::ExportClassPointer) {
/* Pointer type variable should be resolved in GetBuiltinElementConstruct() */
assert(false && "??");
} else if(ET->getClass() == RSExportType::ExportClassRecord) {
/*
* Simalar to case of RSExportType::ExportClassRecord in genPackVarOfType.
* TODO: Generalize these two function such that there's no duplicated codes.
*/
const RSExportRecordType* ERT = static_cast<const RSExportRecordType*>(ET);
int Pos = 0; /* relative pos from now on */
for(RSExportRecordType::const_field_iterator I = ERT->fields_begin();
I != ERT->fields_end();
I++)
{
const RSExportRecordType::Field* F = *I;
std::string FieldName;
size_t FieldOffset = F->getOffsetInParent();
size_t FieldStoreSize = RSExportType::GetTypeStoreSize(F->getType());
size_t FieldAllocSize = RSExportType::GetTypeAllocSize(F->getType());
if(!VarName.empty())
FieldName = VarName + "." + F->getName();
else
FieldName = F->getName();
/* alignment */
genAddPaddingToElementBuiler(C, (FieldOffset - Pos), ElementBuilderName, RenderScriptVar);
/* eb.add(...) */
C.addFieldIndexMapping(F);
genAddElementToElementBuilder(C, F->getType(), FieldName, ElementBuilderName, RenderScriptVar);
/* There is padding within the field type */
genAddPaddingToElementBuiler(C, (FieldAllocSize - FieldStoreSize), ElementBuilderName, RenderScriptVar);
Pos = FieldOffset + FieldAllocSize;
}
/* There maybe some padding after the struct */
//unsigned char align = RSExportType::GetTypeAlignment(ERT);
//size_t siz = RSExportType::GetTypeAllocSize(ERT);
size_t siz1 = RSExportType::GetTypeStoreSize(ERT);
genAddPaddingToElementBuiler(C, siz1 - Pos, ElementBuilderName, RenderScriptVar);
} else {
assert(false && "Unknown class of type");
}
}
}
void RSReflection::genAddPaddingToElementBuiler(Context& C, size_t PaddingSize, const char* ElementBuilderName, const char* RenderScriptVar) {
while(PaddingSize > 0) {
const std::string& VarName = C.createPaddingField();
if(PaddingSize >= 4) {
EB_ADD("U32(" << RenderScriptVar << ")");
PaddingSize -= 4;
} else if(PaddingSize >= 2) {
EB_ADD("U16(" << RenderScriptVar << ")");
PaddingSize -= 2;
} else if(PaddingSize >= 1) {
EB_ADD("U8(" << RenderScriptVar << ")");
PaddingSize -= 1;
}
}
return;
}
#undef EB_ADD
/******************** Methods to create Element in Java of given record type /end ********************/
bool RSReflection::reflect(const char* OutputPackageName, const std::string& InputFileName, const std::string& OutputBCFileName) {
Context *C = NULL;
std::string ResourceId = "";
if(!GetClassNameFromFileName(OutputBCFileName, ResourceId))
return false;
if(ResourceId.empty())
ResourceId = "<Resource ID>";
if((OutputPackageName == NULL) || (*OutputPackageName == '\0') || strcmp(OutputPackageName, "-") == 0)
C = new Context(InputFileName, "<Package Name>", ResourceId, true);
else
C = new Context(InputFileName, OutputPackageName, ResourceId, false);
if(C != NULL) {
std::string ErrorMsg, ScriptClassName;
/* class ScriptC_<ScriptName> */
if(!GetClassNameFromFileName(InputFileName, ScriptClassName))
return false;
if(ScriptClassName.empty())
ScriptClassName = "<Input Script Name>";
ScriptClassName.insert(0, RS_SCRIPT_CLASS_NAME_PREFIX);
if (mRSContext->getLicenseNote() != NULL) {
C->setLicenseNote(*(mRSContext->getLicenseNote()));
}
if(!genScriptClass(*C, ScriptClassName, ErrorMsg)) {
std::cerr << "Failed to generate class " << ScriptClassName << " (" << ErrorMsg << ")" << endl;
return false;
}
/* class ScriptField_<TypeName> */
for(RSContext::const_export_type_iterator TI = mRSContext->export_types_begin();
TI != mRSContext->export_types_end();
TI++)
{
const RSExportType* ET = TI->getValue();
if(ET->getClass() == RSExportType::ExportClassRecord) {
const RSExportRecordType* ERT = static_cast<const RSExportRecordType*>(ET);
if(!ERT->isArtificial() && !genTypeClass(*C, ERT, ErrorMsg)) {
std::cerr << "Failed to generate type class for struct '" << ERT->getName() << "' (" << ErrorMsg << ")" << endl;
return false;
}
}
}
}
return true;
}
/****************************** RSReflection::Context ******************************/
const char* const RSReflection::Context::ApacheLicenseNote =
"/*\n"
" * Copyright (C) 2010 The Android Open Source Project\n"
" *\n"
" * Licensed under the Apache License, Version 2.0 (the \"License\");\n"
" * you may not use this file except in compliance with the License.\n"
" * You may obtain a copy of the License at\n"
" *\n"
" * http://www.apache.org/licenses/LICENSE-2.0\n"
" *\n"
" * Unless required by applicable law or agreed to in writing, software\n"
" * distributed under the License is distributed on an \"AS IS\" BASIS,\n"
" * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n"
" * See the License for the specific language governing permissions and\n"
" * limitations under the License.\n"
" */\n"
"\n";
const char* const RSReflection::Context::Import[] = {
/* RenderScript java class */
"android.renderscript.*",
/* Import R */
"android.content.res.Resources",
/* Import for debugging */
"android.util.Log",
};
const char* RSReflection::Context::AccessModifierStr(AccessModifier AM) {
switch(AM) {
case AM_Public: return "public"; break;
case AM_Protected: return "protected"; break;
case AM_Private: return "private"; break;
default: return ""; break;
}
}
bool RSReflection::Context::startClass(AccessModifier AM, bool IsStatic, const std::string& ClassName, const char* SuperClassName, std::string& ErrorMsg) {
if(mVerbose)
std::cout << "Generating " << ClassName << ".java ..." << endl;
/* License */
out() << mLicenseNote;
/* Notice of generated file */
out() << "/*" << endl;
out() << " * This file is auto-generated. DO NOT MODIFY!" << endl;
out() << " * The source RenderScript file: " << mInputRSFile << endl;
out() << " */" << endl;
/* Package */
if(!mPackageName.empty())
out() << "package " << mPackageName << ";" << endl;
out() << endl;
/* Imports */
for(unsigned i=0;i<(sizeof(Import)/sizeof(const char*));i++)
out() << "import " << Import[i] << ";" << endl;
out() << endl;
/* All reflected classes should be annotated as hidden, so that they won't be exposed in SDK. */
out() << "/**" << endl;
out() << " * @hide" << endl;
out() << " */" << endl;
out() << AccessModifierStr(AM) << ((IsStatic) ? " static" : "") << " class " << ClassName;
if(SuperClassName != NULL)
out() << " extends " << SuperClassName;
startBlock();
mClassName = ClassName;
return true;
}
void RSReflection::Context::endClass() {
endBlock();
if(!mUseStdout)
mOF.close();
clear();
return;
}
void RSReflection::Context::startBlock(bool ShouldIndent) {
if(ShouldIndent)
indent() << "{" << endl;
else
out() << " {" << endl;
incIndentLevel();
return;
}
void RSReflection::Context::endBlock() {
decIndentLevel();
indent() << "}" << endl << endl;
return;
}
void RSReflection::Context::startTypeClass(const std::string& ClassName) {
indent() << "public static class " << ClassName;
startBlock();
return;
}
void RSReflection::Context::endTypeClass() {
endBlock();
return;
}
void RSReflection::Context::startFunction(AccessModifier AM, bool IsStatic, const char* ReturnType, const std::string& FunctionName, int Argc, ...) {
ArgTy Args;
va_list vl;
va_start(vl, Argc);
for(int i=0;i<Argc;i++) {
const char* ArgType = va_arg(vl, const char*);
const char* ArgName = va_arg(vl, const char*);
Args.push_back( make_pair(ArgType, ArgName) );
}
va_end(vl);
startFunction(AM, IsStatic, ReturnType, FunctionName, Args);
return;
}
void RSReflection::Context::startFunction(AccessModifier AM,
bool IsStatic,
const char* ReturnType,
const std::string& FunctionName,
const ArgTy& Args)
{
indent() << AccessModifierStr(AM) << ((IsStatic) ? " static " : " ") << ((ReturnType) ? ReturnType : "") << " " << FunctionName << "(";
bool FirstArg = true;
for(ArgTy::const_iterator I = Args.begin();
I != Args.end();
I++)
{
if(!FirstArg)
out() << ", ";
else
FirstArg = false;
out() << I->first << " " << I->second;
}
out() << ")";
startBlock();
return;
}
void RSReflection::Context::endFunction() {
endBlock();
return;
}
} /* namespace slang */
|