blob: 772545b2a985794642f4ac7aa220550529db531c [file] [log] [blame]
Zonr Changc383a502010-10-12 01:52:08 +08001/*
Stephen Hinesd369cda2012-02-13 12:00:03 -08002 * Copyright 2010-2012, The Android Open Source Project
Zonr Changc383a502010-10-12 01:52:08 +08003 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
zonr6315f762010-10-05 15:35:14 +080017#include "slang_rs_reflection.h"
Shih-wei Liao462aefd2010-06-04 15:32:04 -070018
Stephen Hinese639eb52010-11-08 19:27:20 -080019#include <sys/stat.h>
20
Shih-wei Liao462aefd2010-06-04 15:32:04 -070021#include <cstdarg>
Shih-wei Liao9ef2f782010-10-01 12:31:37 -070022#include <cctype>
Stephen Hinese639eb52010-11-08 19:27:20 -080023
24#include <algorithm>
Stephen Hinesd369cda2012-02-13 12:00:03 -080025#include <sstream>
Stephen Hinese639eb52010-11-08 19:27:20 -080026#include <string>
27#include <utility>
Shih-wei Liao462aefd2010-06-04 15:32:04 -070028
zonr6315f762010-10-05 15:35:14 +080029#include "llvm/ADT/APFloat.h"
Zonr Chang89273bd2010-10-14 20:57:38 +080030#include "llvm/ADT/StringExtras.h"
zonr6315f762010-10-05 15:35:14 +080031
Raphael8d5a2f62011-02-08 00:15:05 -080032#include "os_sep.h"
zonr6315f762010-10-05 15:35:14 +080033#include "slang_rs_context.h"
34#include "slang_rs_export_var.h"
Stephen Hines593a8942011-05-10 15:29:50 -070035#include "slang_rs_export_foreach.h"
zonr6315f762010-10-05 15:35:14 +080036#include "slang_rs_export_func.h"
37#include "slang_rs_reflect_utils.h"
Stephen Hines4cc499d2011-08-24 19:06:17 -070038#include "slang_version.h"
Stephen Hinese639eb52010-11-08 19:27:20 -080039#include "slang_utils.h"
zonr6315f762010-10-05 15:35:14 +080040
Jason Sams192392f2012-03-13 16:22:12 -070041#include "slang_rs_reflection_base.h"
42
Shih-wei Liao9ef2f782010-10-01 12:31:37 -070043#define RS_SCRIPT_CLASS_NAME_PREFIX "ScriptC_"
44#define RS_SCRIPT_CLASS_SUPER_CLASS_NAME "ScriptC"
Shih-wei Liao8b1d0dd2010-07-15 18:56:55 -070045
Tim Murrayf69e1e52013-01-17 13:57:24 -080046#define RS_TYPE_CLASS_SUPER_CLASS_NAME ".Script.FieldBase"
Shih-wei Liao462aefd2010-06-04 15:32:04 -070047
Shih-wei Liao9ef2f782010-10-01 12:31:37 -070048#define RS_TYPE_ITEM_CLASS_NAME "Item"
Shih-wei Liao462aefd2010-06-04 15:32:04 -070049
Shih-wei Liao9ef2f782010-10-01 12:31:37 -070050#define RS_TYPE_ITEM_BUFFER_NAME "mItemArray"
51#define RS_TYPE_ITEM_BUFFER_PACKER_NAME "mIOBuffer"
Jason Sams381e95f2011-11-30 14:01:43 -080052#define RS_TYPE_ELEMENT_REF_NAME "mElementCache"
Shih-wei Liao462aefd2010-06-04 15:32:04 -070053
Shih-wei Liao9ef2f782010-10-01 12:31:37 -070054#define RS_EXPORT_VAR_INDEX_PREFIX "mExportVarIdx_"
55#define RS_EXPORT_VAR_PREFIX "mExportVar_"
Stephen Hinesa6b54142012-04-09 18:25:08 -070056#define RS_EXPORT_VAR_ELEM_PREFIX "mExportVarElem_"
57#define RS_EXPORT_VAR_DIM_PREFIX "mExportVarDim_"
Stephen Hines5d671782012-01-31 19:32:04 -080058#define RS_EXPORT_VAR_CONST_PREFIX "const_"
Shih-wei Liao462aefd2010-06-04 15:32:04 -070059
Stephen Hinesa6b54142012-04-09 18:25:08 -070060#define RS_ELEM_PREFIX "__"
61
Stephen Hines1f6c3312012-07-03 17:23:33 -070062#define RS_FP_PREFIX "__rs_fp_"
63
Stephen Hinesd2936932012-09-12 20:44:32 -070064#define RS_RESOURCE_NAME "__rs_resource_name"
65
Shih-wei Liao9ef2f782010-10-01 12:31:37 -070066#define RS_EXPORT_FUNC_INDEX_PREFIX "mExportFuncIdx_"
Stephen Hines593a8942011-05-10 15:29:50 -070067#define RS_EXPORT_FOREACH_INDEX_PREFIX "mExportForEachIdx_"
Shih-wei Liao462aefd2010-06-04 15:32:04 -070068
Shih-wei Liao9ef2f782010-10-01 12:31:37 -070069#define RS_EXPORT_VAR_ALLOCATION_PREFIX "mAlloction_"
70#define RS_EXPORT_VAR_DATA_STORAGE_PREFIX "mData_"
Shih-wei Liao462aefd2010-06-04 15:32:04 -070071
Stephen Hinese639eb52010-11-08 19:27:20 -080072namespace slang {
Shih-wei Liao462aefd2010-06-04 15:32:04 -070073
Shih-wei Liao9ef2f782010-10-01 12:31:37 -070074// Some utility function using internal in RSReflection
75static bool GetClassNameFromFileName(const std::string &FileName,
76 std::string &ClassName) {
77 ClassName.clear();
Shih-wei Liao462aefd2010-06-04 15:32:04 -070078
Shih-wei Liao9ef2f782010-10-01 12:31:37 -070079 if (FileName.empty() || (FileName == "-"))
Shih-wei Liao462aefd2010-06-04 15:32:04 -070080 return true;
Shih-wei Liao9ef2f782010-10-01 12:31:37 -070081
82 ClassName =
83 RSSlangReflectUtils::JavaClassNameFromRSFileName(FileName.c_str());
84
85 return true;
Shih-wei Liao462aefd2010-06-04 15:32:04 -070086}
87
Zonr Chang92b344a2010-10-05 20:39:03 +080088static const char *GetMatrixTypeName(const RSExportMatrixType *EMT) {
89 static const char *MatrixTypeJavaNameMap[] = {
90 /* 2x2 */ "Matrix2f",
91 /* 3x3 */ "Matrix3f",
92 /* 4x4 */ "Matrix4f",
93 };
94 unsigned Dim = EMT->getDim();
95
96 if ((Dim - 2) < (sizeof(MatrixTypeJavaNameMap) / sizeof(const char*)))
97 return MatrixTypeJavaNameMap[ EMT->getDim() - 2 ];
98
Stephen Hines6e6578a2011-02-07 18:05:48 -080099 slangAssert(false && "GetMatrixTypeName : Unsupported matrix dimension");
Zonr Chang92b344a2010-10-05 20:39:03 +0800100 return NULL;
101}
102
Stephen Hines6e6578a2011-02-07 18:05:48 -0800103static const char *GetVectorAccessor(unsigned Index) {
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700104 static const char *VectorAccessorMap[] = {
105 /* 0 */ "x",
106 /* 1 */ "y",
107 /* 2 */ "z",
108 /* 3 */ "w",
109 };
Shih-wei Liao462aefd2010-06-04 15:32:04 -0700110
Stephen Hines6e6578a2011-02-07 18:05:48 -0800111 slangAssert((Index < (sizeof(VectorAccessorMap) / sizeof(const char*))) &&
112 "Out-of-bound index to access vector member");
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700113
114 return VectorAccessorMap[Index];
Shih-wei Liao462aefd2010-06-04 15:32:04 -0700115}
116
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700117static const char *GetPackerAPIName(const RSExportPrimitiveType *EPT) {
zonr6315f762010-10-05 15:35:14 +0800118 static const char *PrimitiveTypePackerAPINameMap[] = {
Shih-wei Liao91a37832010-10-03 19:11:51 -0700119 "", // RSExportPrimitiveType::DataTypeFloat16
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700120 "addF32", // RSExportPrimitiveType::DataTypeFloat32
121 "addF64", // RSExportPrimitiveType::DataTypeFloat64
122 "addI8", // RSExportPrimitiveType::DataTypeSigned8
123 "addI16", // RSExportPrimitiveType::DataTypeSigned16
124 "addI32", // RSExportPrimitiveType::DataTypeSigned32
125 "addI64", // RSExportPrimitiveType::DataTypeSigned64
126 "addU8", // RSExportPrimitiveType::DataTypeUnsigned8
127 "addU16", // RSExportPrimitiveType::DataTypeUnsigned16
128 "addU32", // RSExportPrimitiveType::DataTypeUnsigned32
129 "addU64", // RSExportPrimitiveType::DataTypeUnsigned64
Shih-wei Liao91a37832010-10-03 19:11:51 -0700130 "addBoolean", // RSExportPrimitiveType::DataTypeBoolean
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700131
132 "addU16", // RSExportPrimitiveType::DataTypeUnsigned565
133 "addU16", // RSExportPrimitiveType::DataTypeUnsigned5551
134 "addU16", // RSExportPrimitiveType::DataTypeUnsigned4444
135
Alex Sakhartchoukaa180e92010-12-17 13:26:07 -0800136 "addMatrix", // RSExportPrimitiveType::DataTypeRSMatrix2x2
137 "addMatrix", // RSExportPrimitiveType::DataTypeRSMatrix3x3
138 "addMatrix", // RSExportPrimitiveType::DataTypeRSMatrix4x4
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700139
140 "addObj", // RSExportPrimitiveType::DataTypeRSElement
141 "addObj", // RSExportPrimitiveType::DataTypeRSType
142 "addObj", // RSExportPrimitiveType::DataTypeRSAllocation
143 "addObj", // RSExportPrimitiveType::DataTypeRSSampler
144 "addObj", // RSExportPrimitiveType::DataTypeRSScript
145 "addObj", // RSExportPrimitiveType::DataTypeRSMesh
Jason Samsdb6dfdf2011-11-07 18:11:10 -0800146 "addObj", // RSExportPrimitiveType::DataTypeRSPath
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700147 "addObj", // RSExportPrimitiveType::DataTypeRSProgramFragment
148 "addObj", // RSExportPrimitiveType::DataTypeRSProgramVertex
149 "addObj", // RSExportPrimitiveType::DataTypeRSProgramRaster
150 "addObj", // RSExportPrimitiveType::DataTypeRSProgramStore
151 "addObj", // RSExportPrimitiveType::DataTypeRSFont
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700152 };
153 unsigned TypeId = EPT->getType();
154
155 if (TypeId < (sizeof(PrimitiveTypePackerAPINameMap) / sizeof(const char*)))
156 return PrimitiveTypePackerAPINameMap[ EPT->getType() ];
157
Stephen Hines6e6578a2011-02-07 18:05:48 -0800158 slangAssert(false && "GetPackerAPIName : Unknown primitive data type");
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700159 return NULL;
160}
161
Stephen Hinesd369cda2012-02-13 12:00:03 -0800162static std::string GetTypeName(const RSExportType *ET, bool Brackets = true) {
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700163 switch (ET->getClass()) {
Zonr Chang2e1dba62010-10-05 22:20:11 +0800164 case RSExportType::ExportClassPrimitive: {
Stephen Hinesfdd1ba12012-03-08 17:26:32 -0800165 return RSExportPrimitiveType::getRSReflectionType(
166 static_cast<const RSExportPrimitiveType*>(ET))->java_name;
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700167 }
168 case RSExportType::ExportClassPointer: {
169 const RSExportType *PointeeType =
170 static_cast<const RSExportPointerType*>(ET)->getPointeeType();
171
172 if (PointeeType->getClass() != RSExportType::ExportClassRecord)
173 return "Allocation";
174 else
Stephen Hinesa6b54142012-04-09 18:25:08 -0700175 return PointeeType->getElementName();
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700176 }
177 case RSExportType::ExportClassVector: {
Stephen Hines47aca4e2012-03-08 20:07:28 -0800178 const RSExportVectorType *EVT =
179 static_cast<const RSExportVectorType*>(ET);
180 std::stringstream VecName;
181 VecName << EVT->getRSReflectionType(EVT)->rs_java_vector_prefix
182 << EVT->getNumElement();
183 return VecName.str();
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700184 }
Zonr Chang92b344a2010-10-05 20:39:03 +0800185 case RSExportType::ExportClassMatrix: {
186 return GetMatrixTypeName(static_cast<const RSExportMatrixType*>(ET));
Zonr Chang2e1dba62010-10-05 22:20:11 +0800187 }
188 case RSExportType::ExportClassConstantArray: {
189 const RSExportConstantArrayType* CAT =
190 static_cast<const RSExportConstantArrayType*>(ET);
191 std::string ElementTypeName = GetTypeName(CAT->getElementType());
Stephen Hinesd369cda2012-02-13 12:00:03 -0800192 if (Brackets) {
193 ElementTypeName.append("[]");
194 }
Zonr Chang2e1dba62010-10-05 22:20:11 +0800195 return ElementTypeName;
Zonr Chang92b344a2010-10-05 20:39:03 +0800196 }
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700197 case RSExportType::ExportClassRecord: {
Stephen Hinesa6b54142012-04-09 18:25:08 -0700198 return ET->getElementName() + "."RS_TYPE_ITEM_CLASS_NAME;
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700199 }
200 default: {
Stephen Hines6e6578a2011-02-07 18:05:48 -0800201 slangAssert(false && "Unknown class of type");
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700202 }
203 }
204
205 return "";
206}
207
Shih-wei Liaocf950c42010-10-06 03:44:40 -0700208static const char *GetTypeNullValue(const RSExportType *ET) {
209 switch (ET->getClass()) {
210 case RSExportType::ExportClassPrimitive: {
Zonr Chang1ab1a452010-10-06 18:28:30 +0800211 const RSExportPrimitiveType *EPT =
212 static_cast<const RSExportPrimitiveType*>(ET);
213 if (EPT->isRSObjectType())
Shih-wei Liaocf950c42010-10-06 03:44:40 -0700214 return "null";
Zonr Chang1ab1a452010-10-06 18:28:30 +0800215 else if (EPT->getType() == RSExportPrimitiveType::DataTypeBoolean)
216 return "false";
Shih-wei Liaocf950c42010-10-06 03:44:40 -0700217 else
218 return "0";
219 break;
220 }
221 case RSExportType::ExportClassPointer:
222 case RSExportType::ExportClassVector:
223 case RSExportType::ExportClassMatrix:
224 case RSExportType::ExportClassConstantArray:
225 case RSExportType::ExportClassRecord: {
226 return "null";
227 break;
228 }
229 default: {
Stephen Hines6e6578a2011-02-07 18:05:48 -0800230 slangAssert(false && "Unknown class of type");
Shih-wei Liaocf950c42010-10-06 03:44:40 -0700231 }
232 }
233 return "";
234}
235
Stephen Hines47aca4e2012-03-08 20:07:28 -0800236static std::string GetBuiltinElementConstruct(const RSExportType *ET) {
Zonr Chang2e1dba62010-10-05 22:20:11 +0800237 if (ET->getClass() == RSExportType::ExportClassPrimitive) {
Stephen Hinesa6b54142012-04-09 18:25:08 -0700238 return std::string("Element.") + ET->getElementName();
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700239 } else if (ET->getClass() == RSExportType::ExportClassVector) {
240 const RSExportVectorType *EVT = static_cast<const RSExportVectorType*>(ET);
Stephen Hines2b8fb642012-03-09 00:12:47 -0800241 if (EVT->getType() == RSExportPrimitiveType::DataTypeFloat32) {
242 if (EVT->getNumElement() == 2)
243 return "Element.F32_2";
244 else if (EVT->getNumElement() == 3)
245 return "Element.F32_3";
246 else if (EVT->getNumElement() == 4)
247 return "Element.F32_4";
248 } else if (EVT->getType() == RSExportPrimitiveType::DataTypeUnsigned8) {
249 if (EVT->getNumElement() == 4)
250 return "Element.U8_4";
Shih-wei Liao324c0472010-06-21 13:15:11 -0700251 }
Zonr Chang92b344a2010-10-05 20:39:03 +0800252 } else if (ET->getClass() == RSExportType::ExportClassMatrix) {
253 const RSExportMatrixType *EMT = static_cast<const RSExportMatrixType *>(ET);
254 switch (EMT->getDim()) {
Zonr Chang89273bd2010-10-14 20:57:38 +0800255 case 2: return "Element.MATRIX_2X2";
256 case 3: return "Element.MATRIX_3X3";
257 case 4: return "Element.MATRIX_4X4";
Stephen Hines6e6578a2011-02-07 18:05:48 -0800258 default: slangAssert(false && "Unsupported dimension of matrix");
Zonr Chang92b344a2010-10-05 20:39:03 +0800259 }
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700260 }
Stephen Hines47aca4e2012-03-08 20:07:28 -0800261 // RSExportType::ExportClassPointer can't be generated in a struct.
Shih-wei Liao324c0472010-06-21 13:15:11 -0700262
Stephen Hines47aca4e2012-03-08 20:07:28 -0800263 return "";
Stephen Hines48b72bf2011-06-10 15:37:27 -0700264}
265
Stephen Hines9d234b62013-03-26 11:20:52 -0700266// Replace all instances of "\" with "\\" in a single string to prevent
267// formatting errors due to unicode.
268static std::string SanitizeString(std::string s) {
269 size_t p = 0;
270 while ( ( p = s.find('\\', p)) != std::string::npos) {
271 s.replace(p, 1, "\\\\");
272 p+=2;
273 }
274 return s;
275}
276
Stephen Hines48b72bf2011-06-10 15:37:27 -0700277
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700278/********************** Methods to generate script class **********************/
279bool RSReflection::genScriptClass(Context &C,
280 const std::string &ClassName,
281 std::string &ErrorMsg) {
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700282 if (!C.startClass(Context::AM_Public,
283 false,
284 ClassName,
285 RS_SCRIPT_CLASS_SUPER_CLASS_NAME,
286 ErrorMsg))
287 return false;
288
289 genScriptClassConstructor(C);
290
291 // Reflect export variable
292 for (RSContext::const_export_var_iterator I = mRSContext->export_vars_begin(),
293 E = mRSContext->export_vars_end();
294 I != E;
295 I++)
296 genExportVariable(C, *I);
297
Stephen Hines4a4bf922011-08-18 17:20:33 -0700298 // Reflect export for each functions (only available on ICS+)
Stephen Hines4cc499d2011-08-24 19:06:17 -0700299 if (mRSContext->getTargetAPI() >= SLANG_ICS_TARGET_API) {
Stephen Hines4a4bf922011-08-18 17:20:33 -0700300 for (RSContext::const_export_foreach_iterator
301 I = mRSContext->export_foreach_begin(),
302 E = mRSContext->export_foreach_end();
303 I != E; I++)
304 genExportForEach(C, *I);
305 }
Stephen Hines593a8942011-05-10 15:29:50 -0700306
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700307 // Reflect export function
308 for (RSContext::const_export_func_iterator
309 I = mRSContext->export_funcs_begin(),
310 E = mRSContext->export_funcs_end();
311 I != E; I++)
312 genExportFunction(C, *I);
313
314 C.endClass();
315
316 return true;
317}
318
319void RSReflection::genScriptClassConstructor(Context &C) {
Stephen Hinesd2936932012-09-12 20:44:32 -0700320 // Provide a simple way to reference this object.
321 C.indent() << "private static final String " RS_RESOURCE_NAME " = \""
322 << C.getResourceId()
323 << "\";" << std::endl;
324
325 // Generate a simple constructor with only a single parameter (the rest
326 // can be inferred from information we already have).
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700327 C.indent() << "// Constructor" << std::endl;
328 C.startFunction(Context::AM_Public,
329 false,
330 NULL,
331 C.getClassName(),
Stephen Hinesd2936932012-09-12 20:44:32 -0700332 1,
333 "RenderScript", "rs");
334 // Call alternate constructor with required parameters.
335 // Look up the proper raw bitcode resource id via the context.
336 C.indent() << "this(rs," << std::endl;
337 C.indent() << " rs.getApplicationContext().getResources()," << std::endl;
338 C.indent() << " rs.getApplicationContext().getResources()."
339 "getIdentifier(" << std::endl;
340 C.indent() << " " RS_RESOURCE_NAME ", \"raw\"," << std::endl;
341 C.indent() << " rs.getApplicationContext().getPackageName()));"
342 << std::endl;
343 C.endFunction();
344
345 // Alternate constructor (legacy) with 3 original parameters.
346 C.startFunction(Context::AM_Public,
347 false,
348 NULL,
349 C.getClassName(),
Jason Samsb6902e22010-11-03 17:04:06 -0700350 3,
Zonr Changd42a4292010-10-17 02:38:43 +0800351 "RenderScript", "rs",
352 "Resources", "resources",
Jason Samsb6902e22010-11-03 17:04:06 -0700353 "int", "id");
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700354 // Call constructor of super class
Jason Samsb6902e22010-11-03 17:04:06 -0700355 C.indent() << "super(rs, resources, id);" << std::endl;
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700356
357 // If an exported variable has initial value, reflect it
358
359 for (RSContext::const_export_var_iterator I = mRSContext->export_vars_begin(),
360 E = mRSContext->export_vars_end();
361 I != E;
362 I++) {
363 const RSExportVar *EV = *I;
Stephen Hinesd369cda2012-02-13 12:00:03 -0800364 if (!EV->getInit().isUninit()) {
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700365 genInitExportVariable(C, EV->getType(), EV->getName(), EV->getInit());
Stephen Hinesd369cda2012-02-13 12:00:03 -0800366 } else if (EV->getArraySize()) {
367 // Always create an initial zero-init array object.
368 C.indent() << RS_EXPORT_VAR_PREFIX << EV->getName() << " = new "
369 << GetTypeName(EV->getType(), false) << "["
370 << EV->getArraySize() << "];" << std::endl;
371 size_t NumInits = EV->getNumInits();
372 const RSExportConstantArrayType *ECAT =
373 static_cast<const RSExportConstantArrayType*>(EV->getType());
374 const RSExportType *ET = ECAT->getElementType();
375 for (size_t i = 0; i < NumInits; i++) {
376 std::stringstream Name;
377 Name << EV->getName() << "[" << i << "]";
378 genInitExportVariable(C, ET, Name.str(), EV->getInitArray(i));
379 }
380 }
Stephen Hinesa6b54142012-04-09 18:25:08 -0700381 if (mRSContext->getTargetAPI() >= SLANG_JB_TARGET_API) {
382 genTypeInstance(C, EV->getType());
383 }
Stephen Hines1f6c3312012-07-03 17:23:33 -0700384 genFieldPackerInstance(C, EV->getType());
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700385 }
386
Stephen Hines48b72bf2011-06-10 15:37:27 -0700387 for (RSContext::const_export_foreach_iterator
388 I = mRSContext->export_foreach_begin(),
389 E = mRSContext->export_foreach_end();
390 I != E;
391 I++) {
392 const RSExportForEach *EF = *I;
393
394 const RSExportType *IET = EF->getInType();
395 if (IET) {
Stephen Hinesa6b54142012-04-09 18:25:08 -0700396 genTypeInstanceFromPointer(C, IET);
Stephen Hines48b72bf2011-06-10 15:37:27 -0700397 }
398 const RSExportType *OET = EF->getOutType();
399 if (OET) {
Stephen Hinesa6b54142012-04-09 18:25:08 -0700400 genTypeInstanceFromPointer(C, OET);
Stephen Hines48b72bf2011-06-10 15:37:27 -0700401 }
402 }
403
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700404 C.endFunction();
Jason Samsb6902e22010-11-03 17:04:06 -0700405
Stephen Hines48b72bf2011-06-10 15:37:27 -0700406 for (std::set<std::string>::iterator I = C.mTypesToCheck.begin(),
407 E = C.mTypesToCheck.end();
408 I != E;
409 I++) {
Stephen Hinesa6b54142012-04-09 18:25:08 -0700410 C.indent() << "private Element " RS_ELEM_PREFIX << *I << ";" << std::endl;
Stephen Hines48b72bf2011-06-10 15:37:27 -0700411 }
412
Stephen Hines1f6c3312012-07-03 17:23:33 -0700413 for (std::set<std::string>::iterator I = C.mFieldPackerTypes.begin(),
414 E = C.mFieldPackerTypes.end();
415 I != E;
416 I++) {
417 C.indent() << "private FieldPacker " RS_FP_PREFIX << *I << ";" << std::endl;
418 }
419
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700420 return;
421}
422
423void RSReflection::genInitBoolExportVariable(Context &C,
424 const std::string &VarName,
425 const clang::APValue &Val) {
Stephen Hines6e6578a2011-02-07 18:05:48 -0800426 slangAssert(!Val.isUninit() && "Not a valid initializer");
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700427
428 C.indent() << RS_EXPORT_VAR_PREFIX << VarName << " = ";
Stephen Hines6e6578a2011-02-07 18:05:48 -0800429 slangAssert((Val.getKind() == clang::APValue::Int) &&
430 "Bool type has wrong initial APValue");
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700431
Zonr Chang92b344a2010-10-05 20:39:03 +0800432 C.out() << ((Val.getInt().getSExtValue() == 0) ? "false" : "true")
433 << ";" << std::endl;
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700434
435 return;
436}
437
Stephen Hines5d671782012-01-31 19:32:04 -0800438void RSReflection::genInitPrimitiveExportVariable(
439 Context &C,
440 const std::string &VarName,
441 const clang::APValue &Val) {
442 slangAssert(!Val.isUninit() && "Not a valid initializer");
443
444 C.indent() << RS_EXPORT_VAR_PREFIX << VarName << " = ";
Jason Sams192392f2012-03-13 16:22:12 -0700445 C.out() << RSReflectionBase::genInitValue(Val);
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700446 C.out() << ";" << std::endl;
447
448 return;
Shih-wei Liao462aefd2010-06-04 15:32:04 -0700449}
450
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700451void RSReflection::genInitExportVariable(Context &C,
452 const RSExportType *ET,
453 const std::string &VarName,
454 const clang::APValue &Val) {
Stephen Hines6e6578a2011-02-07 18:05:48 -0800455 slangAssert(!Val.isUninit() && "Not a valid initializer");
Shih-wei Liao462aefd2010-06-04 15:32:04 -0700456
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700457 switch (ET->getClass()) {
Zonr Chang2e1dba62010-10-05 22:20:11 +0800458 case RSExportType::ExportClassPrimitive: {
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700459 const RSExportPrimitiveType *EPT =
460 static_cast<const RSExportPrimitiveType*>(ET);
Shih-wei Liao91a37832010-10-03 19:11:51 -0700461 if (EPT->getType() == RSExportPrimitiveType::DataTypeBoolean) {
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700462 genInitBoolExportVariable(C, VarName, Val);
463 } else {
464 genInitPrimitiveExportVariable(C, VarName, Val);
465 }
466 break;
Shih-wei Liao462aefd2010-06-04 15:32:04 -0700467 }
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700468 case RSExportType::ExportClassPointer: {
469 if (!Val.isInt() || Val.getInt().getSExtValue() != 0)
470 std::cout << "Initializer which is non-NULL to pointer type variable "
471 "will be ignored" << std::endl;
472 break;
Shih-wei Liao462aefd2010-06-04 15:32:04 -0700473 }
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700474 case RSExportType::ExportClassVector: {
475 const RSExportVectorType *EVT =
476 static_cast<const RSExportVectorType*>(ET);
477 switch (Val.getKind()) {
478 case clang::APValue::Int:
479 case clang::APValue::Float: {
Zonr Chang2e1dba62010-10-05 22:20:11 +0800480 for (unsigned i = 0; i < EVT->getNumElement(); i++) {
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700481 std::string Name = VarName + "." + GetVectorAccessor(i);
482 genInitPrimitiveExportVariable(C, Name, Val);
Shih-wei Liao1ebc0ca2010-09-14 10:57:21 -0700483 }
484 break;
485 }
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700486 case clang::APValue::Vector: {
Stephen Hines47aca4e2012-03-08 20:07:28 -0800487 std::stringstream VecName;
488 VecName << EVT->getRSReflectionType(EVT)->rs_java_vector_prefix
489 << EVT->getNumElement();
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700490 C.indent() << RS_EXPORT_VAR_PREFIX << VarName << " = new "
Stephen Hines47aca4e2012-03-08 20:07:28 -0800491 << VecName.str() << "();" << std::endl;
Shih-wei Liao462aefd2010-06-04 15:32:04 -0700492
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700493 unsigned NumElements =
494 std::min(static_cast<unsigned>(EVT->getNumElement()),
495 Val.getVectorLength());
496 for (unsigned i = 0; i < NumElements; i++) {
497 const clang::APValue &ElementVal = Val.getVectorElt(i);
498 std::string Name = VarName + "." + GetVectorAccessor(i);
499 genInitPrimitiveExportVariable(C, Name, ElementVal);
500 }
501 break;
Shih-wei Liao462aefd2010-06-04 15:32:04 -0700502 }
Logan Chienfa6ef562011-11-25 13:50:02 +0800503 case clang::APValue::MemberPointer:
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700504 case clang::APValue::Uninitialized:
505 case clang::APValue::ComplexInt:
506 case clang::APValue::ComplexFloat:
Stephen Hines4c622e02011-11-10 18:57:34 -0800507 case clang::APValue::LValue:
508 case clang::APValue::Array:
509 case clang::APValue::Struct:
Stephen Hines0444de02012-03-02 23:19:06 -0800510 case clang::APValue::Union:
511 case clang::APValue::AddrLabelDiff: {
Stephen Hines6e6578a2011-02-07 18:05:48 -0800512 slangAssert(false && "Unexpected type of value of initializer.");
Shih-wei Liao462aefd2010-06-04 15:32:04 -0700513 }
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700514 }
515 break;
Shih-wei Liao462aefd2010-06-04 15:32:04 -0700516 }
Zonr Chang92b344a2010-10-05 20:39:03 +0800517 // TODO(zonr): Resolving initializer of a record (and matrix) type variable
518 // is complex. It cannot obtain by just simply evaluating the initializer
519 // expression.
520 case RSExportType::ExportClassMatrix:
Zonr Chang2e1dba62010-10-05 22:20:11 +0800521 case RSExportType::ExportClassConstantArray:
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700522 case RSExportType::ExportClassRecord: {
523#if 0
524 unsigned InitIndex = 0;
zonr6315f762010-10-05 15:35:14 +0800525 const RSExportRecordType *ERT =
526 static_cast<const RSExportRecordType*>(ET);
Shih-wei Liao462aefd2010-06-04 15:32:04 -0700527
Stephen Hines6e6578a2011-02-07 18:05:48 -0800528 slangAssert((Val.getKind() == clang::APValue::Vector) &&
529 "Unexpected type of initializer for record type variable");
Shih-wei Liao9c631ff2010-09-17 11:57:29 -0700530
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700531 C.indent() << RS_EXPORT_VAR_PREFIX << VarName
Stephen Hinesa6b54142012-04-09 18:25:08 -0700532 << " = new " << ERT->getElementName()
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700533 << "."RS_TYPE_ITEM_CLASS_NAME"();" << std::endl;
Shih-wei Liao9c631ff2010-09-17 11:57:29 -0700534
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700535 for (RSExportRecordType::const_field_iterator I = ERT->fields_begin(),
536 E = ERT->fields_end();
537 I != E;
538 I++) {
539 const RSExportRecordType::Field *F = *I;
540 std::string FieldName = VarName + "." + F->getName();
Shih-wei Liao462aefd2010-06-04 15:32:04 -0700541
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700542 if (InitIndex > Val.getVectorLength())
543 break;
544
545 genInitPrimitiveExportVariable(C,
546 FieldName,
547 Val.getVectorElt(InitIndex++));
548 }
549#endif
Stephen Hines6e6578a2011-02-07 18:05:48 -0800550 slangAssert(false && "Unsupported initializer for record/matrix/constant "
551 "array type variable currently");
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700552 break;
Shih-wei Liao6de89272010-07-15 15:26:20 -0700553 }
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700554 default: {
Stephen Hines6e6578a2011-02-07 18:05:48 -0800555 slangAssert(false && "Unknown class of type");
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700556 }
557 }
558 return;
Shih-wei Liao462aefd2010-06-04 15:32:04 -0700559}
560
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700561void RSReflection::genExportVariable(Context &C, const RSExportVar *EV) {
562 const RSExportType *ET = EV->getType();
Shih-wei Liao462aefd2010-06-04 15:32:04 -0700563
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700564 C.indent() << "private final static int "RS_EXPORT_VAR_INDEX_PREFIX
565 << EV->getName() << " = " << C.getNextExportVarSlot() << ";"
566 << std::endl;
Shih-wei Liao462aefd2010-06-04 15:32:04 -0700567
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700568 switch (ET->getClass()) {
Zonr Chang2e1dba62010-10-05 22:20:11 +0800569 case RSExportType::ExportClassPrimitive: {
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700570 genPrimitiveTypeExportVariable(C, EV);
571 break;
Shih-wei Liaof8149d92010-08-22 05:32:02 -0700572 }
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700573 case RSExportType::ExportClassPointer: {
574 genPointerTypeExportVariable(C, EV);
575 break;
Shih-wei Liao462aefd2010-06-04 15:32:04 -0700576 }
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700577 case RSExportType::ExportClassVector: {
578 genVectorTypeExportVariable(C, EV);
579 break;
580 }
Zonr Chang92b344a2010-10-05 20:39:03 +0800581 case RSExportType::ExportClassMatrix: {
582 genMatrixTypeExportVariable(C, EV);
583 break;
584 }
Zonr Chang2e1dba62010-10-05 22:20:11 +0800585 case RSExportType::ExportClassConstantArray: {
586 genConstantArrayTypeExportVariable(C, EV);
587 break;
588 }
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700589 case RSExportType::ExportClassRecord: {
590 genRecordTypeExportVariable(C, EV);
591 break;
592 }
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700593 default: {
Stephen Hines6e6578a2011-02-07 18:05:48 -0800594 slangAssert(false && "Unknown class of type");
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700595 }
596 }
Shih-wei Liao462aefd2010-06-04 15:32:04 -0700597
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700598 return;
Shih-wei Liao462aefd2010-06-04 15:32:04 -0700599}
600
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700601void RSReflection::genExportFunction(Context &C, const RSExportFunc *EF) {
602 C.indent() << "private final static int "RS_EXPORT_FUNC_INDEX_PREFIX
603 << EF->getName() << " = " << C.getNextExportFuncSlot() << ";"
604 << std::endl;
Shih-wei Liao462aefd2010-06-04 15:32:04 -0700605
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700606 // invoke_*()
607 Context::ArgTy Args;
608
Zonr Chang0da0a7d2010-10-05 21:26:37 +0800609 if (EF->hasParam()) {
610 for (RSExportFunc::const_param_iterator I = EF->params_begin(),
611 E = EF->params_end();
612 I != E;
613 I++) {
614 Args.push_back(std::make_pair(GetTypeName((*I)->getType()),
615 (*I)->getName()));
616 }
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700617 }
618
619 C.startFunction(Context::AM_Public,
620 false,
621 "void",
Shih-wei Liao3fa286b2011-02-10 11:04:44 -0800622 "invoke_" + EF->getName(/*Mangle=*/ false),
623 // We are using un-mangled name since Java
624 // supports method overloading.
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700625 Args);
626
627 if (!EF->hasParam()) {
628 C.indent() << "invoke("RS_EXPORT_FUNC_INDEX_PREFIX << EF->getName() << ");"
629 << std::endl;
630 } else {
631 const RSExportRecordType *ERT = EF->getParamPacketType();
632 std::string FieldPackerName = EF->getName() + "_fp";
633
634 if (genCreateFieldPacker(C, ERT, FieldPackerName.c_str()))
635 genPackVarOfType(C, ERT, NULL, FieldPackerName.c_str());
636
637 C.indent() << "invoke("RS_EXPORT_FUNC_INDEX_PREFIX << EF->getName() << ", "
638 << FieldPackerName << ");" << std::endl;
639 }
640
641 C.endFunction();
642 return;
643}
644
Stephen Hines593a8942011-05-10 15:29:50 -0700645void RSReflection::genExportForEach(Context &C, const RSExportForEach *EF) {
Stephen Hinesc17e1982012-02-22 12:30:45 -0800646 if (EF->isDummyRoot()) {
647 // Skip reflection for dummy root() kernels. Note that we have to
648 // advance the next slot number for ForEach, however.
649 C.indent() << "//private final static int "RS_EXPORT_FOREACH_INDEX_PREFIX
650 << EF->getName() << " = " << C.getNextExportForEachSlot() << ";"
651 << std::endl;
652 return;
653 }
654
Stephen Hines593a8942011-05-10 15:29:50 -0700655 C.indent() << "private final static int "RS_EXPORT_FOREACH_INDEX_PREFIX
656 << EF->getName() << " = " << C.getNextExportForEachSlot() << ";"
657 << std::endl;
658
Stephen Hinesb5a89fb2011-05-17 14:48:02 -0700659 // forEach_*()
Stephen Hines593a8942011-05-10 15:29:50 -0700660 Context::ArgTy Args;
661
Stephen Hines9ca96e72012-09-13 16:57:06 -0700662 slangAssert(EF->getNumParameters() > 0 || EF->hasReturn());
Stephen Hines593a8942011-05-10 15:29:50 -0700663
Stephen Hinesb5a89fb2011-05-17 14:48:02 -0700664 if (EF->hasIn())
665 Args.push_back(std::make_pair("Allocation", "ain"));
Stephen Hines9ca96e72012-09-13 16:57:06 -0700666 if (EF->hasOut() || EF->hasReturn())
Stephen Hines593a8942011-05-10 15:29:50 -0700667 Args.push_back(std::make_pair("Allocation", "aout"));
Stephen Hinesb5a89fb2011-05-17 14:48:02 -0700668
669 const RSExportRecordType *ERT = EF->getParamPacketType();
670 if (ERT) {
671 for (RSExportForEach::const_param_iterator I = EF->params_begin(),
Stephen Hines593a8942011-05-10 15:29:50 -0700672 E = EF->params_end();
673 I != E;
674 I++) {
675 Args.push_back(std::make_pair(GetTypeName((*I)->getType()),
676 (*I)->getName()));
677 }
678 }
679
Tim Murrayb81a9932012-10-10 15:56:02 -0700680 const RSExportType *IET = EF->getInType();
681 const RSExportType *OET = EF->getOutType();
682
683 if (mRSContext->getTargetAPI() >= SLANG_JB_MR1_TARGET_API) {
684 int signature = 0;
685 C.startFunction(Context::AM_Public,
686 false,
687 "Script.KernelID",
688 "getKernelID_" + EF->getName(),
689 0);
690
691 if (IET)
692 signature |= 1;
693 if (OET)
694 signature |= 2;
695
696 //TODO: add element checking
697 C.indent() << "return createKernelID(" << RS_EXPORT_FOREACH_INDEX_PREFIX
698 << EF->getName() << ", " << signature << ", null, null);"
699 << std::endl;
700
701 C.endFunction();
702 }
703
Stephen Hines50974742013-02-12 22:11:08 -0800704 if (mRSContext->getTargetAPI() >= SLANG_JB_MR2_TARGET_API) {
705 C.startFunction(Context::AM_Public,
706 false,
707 "void",
708 "forEach_" + EF->getName(),
709 Args);
710
711 C.indent() << "forEach_" << EF->getName();
712 C.out() << "(";
713
714 if (EF->hasIn()) {
715 C.out() << "ain, ";
716 }
717
718 if (EF->hasOut() || EF->hasReturn()) {
719 C.out() << "aout, ";
720 }
721
722 if (EF->hasUsrData()) {
723 C.out() << Args.back().second << ", ";
724 }
725
726 // No clipped bounds to pass in.
727 C.out() << "null);" << std::endl;
728
729 C.endFunction();
730
731 // Add the clipped kernel parameters to the Args list.
732 Args.push_back(std::make_pair("Script.LaunchOptions", "sc"));
733 }
734
Stephen Hines593a8942011-05-10 15:29:50 -0700735 C.startFunction(Context::AM_Public,
736 false,
737 "void",
738 "forEach_" + EF->getName(),
739 Args);
740
Stephen Hinesb5a89fb2011-05-17 14:48:02 -0700741 if (IET) {
742 genTypeCheck(C, IET, "ain");
743 }
Stephen Hinesb5a89fb2011-05-17 14:48:02 -0700744 if (OET) {
745 genTypeCheck(C, OET, "aout");
746 }
747
Stephen Hines9ca96e72012-09-13 16:57:06 -0700748 if (EF->hasIn() && (EF->hasOut() || EF->hasReturn())) {
Stephen Hinesb5a89fb2011-05-17 14:48:02 -0700749 C.indent() << "// Verify dimensions" << std::endl;
750 C.indent() << "Type tIn = ain.getType();" << std::endl;
751 C.indent() << "Type tOut = aout.getType();" << std::endl;
752 C.indent() << "if ((tIn.getCount() != tOut.getCount()) ||" << std::endl;
753 C.indent() << " (tIn.getX() != tOut.getX()) ||" << std::endl;
754 C.indent() << " (tIn.getY() != tOut.getY()) ||" << std::endl;
755 C.indent() << " (tIn.getZ() != tOut.getZ()) ||" << std::endl;
756 C.indent() << " (tIn.hasFaces() != tOut.hasFaces()) ||" << std::endl;
757 C.indent() << " (tIn.hasMipmaps() != tOut.hasMipmaps())) {" << std::endl;
Stephen Hines48b72bf2011-06-10 15:37:27 -0700758 C.indent() << " throw new RSRuntimeException(\"Dimension mismatch "
759 << "between input and output parameters!\");";
Stephen Hinesb5a89fb2011-05-17 14:48:02 -0700760 C.out() << std::endl;
761 C.indent() << "}" << std::endl;
762 }
763
764 std::string FieldPackerName = EF->getName() + "_fp";
765 if (ERT) {
766 if (genCreateFieldPacker(C, ERT, FieldPackerName.c_str())) {
Stephen Hines593a8942011-05-10 15:29:50 -0700767 genPackVarOfType(C, ERT, NULL, FieldPackerName.c_str());
Stephen Hinesb5a89fb2011-05-17 14:48:02 -0700768 }
Stephen Hines593a8942011-05-10 15:29:50 -0700769 }
Stephen Hinesb5a89fb2011-05-17 14:48:02 -0700770 C.indent() << "forEach("RS_EXPORT_FOREACH_INDEX_PREFIX << EF->getName();
Stephen Hines593a8942011-05-10 15:29:50 -0700771
Stephen Hinesb5a89fb2011-05-17 14:48:02 -0700772 if (EF->hasIn())
773 C.out() << ", ain";
774 else
775 C.out() << ", null";
776
Stephen Hines9ca96e72012-09-13 16:57:06 -0700777 if (EF->hasOut() || EF->hasReturn())
Stephen Hinesb5a89fb2011-05-17 14:48:02 -0700778 C.out() << ", aout";
779 else
780 C.out() << ", null";
781
782 if (EF->hasUsrData())
783 C.out() << ", " << FieldPackerName;
784 else
785 C.out() << ", null";
786
Stephen Hines50974742013-02-12 22:11:08 -0800787 if (mRSContext->getTargetAPI() >= SLANG_JB_MR2_TARGET_API) {
788 C.out() << ", sc);" << std::endl;
789 } else {
790 C.out() << ");" << std::endl;
791 }
Stephen Hines593a8942011-05-10 15:29:50 -0700792
793 C.endFunction();
794 return;
795}
796
Stephen Hinesa6b54142012-04-09 18:25:08 -0700797void RSReflection::genTypeInstanceFromPointer(Context &C,
798 const RSExportType *ET) {
Stephen Hines48b72bf2011-06-10 15:37:27 -0700799 if (ET->getClass() == RSExportType::ExportClassPointer) {
Stephen Hines9ca96e72012-09-13 16:57:06 -0700800 // For pointer parameters to original forEach kernels.
Stephen Hines48b72bf2011-06-10 15:37:27 -0700801 const RSExportPointerType *EPT =
802 static_cast<const RSExportPointerType*>(ET);
Stephen Hinesa6b54142012-04-09 18:25:08 -0700803 genTypeInstance(C, EPT->getPointeeType());
Stephen Hines9ca96e72012-09-13 16:57:06 -0700804 } else {
805 // For handling pass-by-value kernel parameters.
806 genTypeInstance(C, ET);
Stephen Hinesa6b54142012-04-09 18:25:08 -0700807 }
808}
Stephen Hines48b72bf2011-06-10 15:37:27 -0700809
Stephen Hinesa6b54142012-04-09 18:25:08 -0700810void RSReflection::genTypeInstance(Context &C,
811 const RSExportType *ET) {
812 switch (ET->getClass()) {
813 case RSExportType::ExportClassPrimitive:
814 case RSExportType::ExportClassVector:
815 case RSExportType::ExportClassConstantArray: {
816 std::string TypeName = ET->getElementName();
Stephen Hines1f6c3312012-07-03 17:23:33 -0700817 if (C.addTypeNameForElement(TypeName)) {
Stephen Hinesa6b54142012-04-09 18:25:08 -0700818 C.indent() << RS_ELEM_PREFIX << TypeName << " = Element." << TypeName
819 << "(rs);" << std::endl;
Stephen Hines48b72bf2011-06-10 15:37:27 -0700820 }
Stephen Hinesa6b54142012-04-09 18:25:08 -0700821 break;
Stephen Hines48b72bf2011-06-10 15:37:27 -0700822 }
Stephen Hinesa6b54142012-04-09 18:25:08 -0700823
824 case RSExportType::ExportClassRecord: {
825 std::string ClassName = ET->getElementName();
Stephen Hines1f6c3312012-07-03 17:23:33 -0700826 if (C.addTypeNameForElement(ClassName)) {
Stephen Hinesa6b54142012-04-09 18:25:08 -0700827 C.indent() << RS_ELEM_PREFIX << ClassName << " = " << ClassName <<
828 ".createElement(rs);" << std::endl;
Stephen Hinesa6b54142012-04-09 18:25:08 -0700829 }
830 break;
831 }
832
833 default:
834 break;
Stephen Hines48b72bf2011-06-10 15:37:27 -0700835 }
836}
837
Stephen Hines1f6c3312012-07-03 17:23:33 -0700838void RSReflection::genFieldPackerInstance(Context &C,
839 const RSExportType *ET) {
840 switch (ET->getClass()) {
841 case RSExportType::ExportClassPrimitive:
842 case RSExportType::ExportClassVector:
843 case RSExportType::ExportClassConstantArray:
844 case RSExportType::ExportClassRecord: {
845 std::string TypeName = ET->getElementName();
846 C.addTypeNameForFieldPacker(TypeName);
847 break;
848 }
849
850 default:
851 break;
852 }
853}
854
Stephen Hinesb5a89fb2011-05-17 14:48:02 -0700855void RSReflection::genTypeCheck(Context &C,
856 const RSExportType *ET,
857 const char *VarName) {
858 C.indent() << "// check " << VarName << std::endl;
Stephen Hines48b72bf2011-06-10 15:37:27 -0700859
860 if (ET->getClass() == RSExportType::ExportClassPointer) {
861 const RSExportPointerType *EPT =
862 static_cast<const RSExportPointerType*>(ET);
863 ET = EPT->getPointeeType();
864 }
865
866 std::string TypeName;
867
868 switch (ET->getClass()) {
Stephen Hinesa6b54142012-04-09 18:25:08 -0700869 case RSExportType::ExportClassPrimitive:
870 case RSExportType::ExportClassVector:
Stephen Hines48b72bf2011-06-10 15:37:27 -0700871 case RSExportType::ExportClassRecord: {
Stephen Hinesa6b54142012-04-09 18:25:08 -0700872 TypeName = ET->getElementName();
Stephen Hines48b72bf2011-06-10 15:37:27 -0700873 break;
874 }
875
876 default:
877 break;
878 }
879
880 if (!TypeName.empty()) {
881 C.indent() << "if (!" << VarName
Stephen Hinesa6b54142012-04-09 18:25:08 -0700882 << ".getType().getElement().isCompatible(" RS_ELEM_PREFIX
Stephen Hines48b72bf2011-06-10 15:37:27 -0700883 << TypeName << ")) {" << std::endl;
884 C.indent() << " throw new RSRuntimeException(\"Type mismatch with "
885 << TypeName << "!\");" << std::endl;
886 C.indent() << "}" << std::endl;
887 }
888
Stephen Hinesb5a89fb2011-05-17 14:48:02 -0700889 return;
890}
891
892
Stephen Hinese639eb52010-11-08 19:27:20 -0800893void RSReflection::genPrimitiveTypeExportVariable(
894 Context &C,
895 const RSExportVar *EV) {
Stephen Hines6e6578a2011-02-07 18:05:48 -0800896 slangAssert((EV->getType()->getClass() == RSExportType::ExportClassPrimitive)
897 && "Variable should be type of primitive here");
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700898
899 const RSExportPrimitiveType *EPT =
900 static_cast<const RSExportPrimitiveType*>(EV->getType());
Stephen Hines0d26cef2012-05-01 19:23:01 -0700901 std::string TypeName = GetTypeName(EPT);
902 std::string VarName = EV->getName();
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700903
Stephen Hines0d26cef2012-05-01 19:23:01 -0700904 genPrivateExportVariable(C, TypeName, EV->getName());
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700905
Stephen Hines5d671782012-01-31 19:32:04 -0800906 if (EV->isConst()) {
907 C.indent() << "public final static " << TypeName
Stephen Hines0d26cef2012-05-01 19:23:01 -0700908 << " " RS_EXPORT_VAR_CONST_PREFIX << VarName << " = ";
Stephen Hines5d671782012-01-31 19:32:04 -0800909 const clang::APValue &Val = EV->getInit();
Jason Sams192392f2012-03-13 16:22:12 -0700910 C.out() << RSReflectionBase::genInitValue(Val, EPT->getType() ==
911 RSExportPrimitiveType::DataTypeBoolean) << ";" << std::endl;
Stephen Hines5d671782012-01-31 19:32:04 -0800912 } else {
913 // set_*()
Stephen Hines1f6c3312012-07-03 17:23:33 -0700914 // This must remain synchronized, since multiple Dalvik threads may
915 // be calling setters.
916 C.startFunction(Context::AM_PublicSynchronized,
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700917 false,
918 "void",
Stephen Hines0d26cef2012-05-01 19:23:01 -0700919 "set_" + VarName,
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700920 1,
Stephen Hines0d26cef2012-05-01 19:23:01 -0700921 TypeName.c_str(), "v");
Stephen Hinesbcae1fe2012-09-25 19:17:33 -0700922 if ((EPT->getSize() < 4) || EV->isUnsigned()) {
Stephen Hines1f6c3312012-07-03 17:23:33 -0700923 // We create/cache a per-type FieldPacker. This allows us to reuse the
924 // validation logic (for catching negative inputs from Dalvik, as well
925 // as inputs that are too large to be represented in the unsigned type).
Stephen Hinesbcae1fe2012-09-25 19:17:33 -0700926 // Sub-integer types are also handled specially here, so that we don't
927 // overwrite bytes accidentally.
Stephen Hines1f6c3312012-07-03 17:23:33 -0700928 std::string ElemName = EPT->getElementName();
929 std::string FPName;
930 FPName = RS_FP_PREFIX + ElemName;
931 C.indent() << "if (" << FPName << "!= null) {"
932 << std::endl;
933 C.incIndentLevel();
934 C.indent() << FPName << ".reset();" << std::endl;
935 C.decIndentLevel();
936 C.indent() << "} else {" << std::endl;
937 C.incIndentLevel();
938 C.indent() << FPName << " = new FieldPacker("
939 << EPT->getSize() << ");" << std::endl;
940 C.decIndentLevel();
941 C.indent() << "}" << std::endl;
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700942
Stephen Hines1f6c3312012-07-03 17:23:33 -0700943 genPackVarOfType(C, EPT, "v", FPName.c_str());
944 C.indent() << "setVar("RS_EXPORT_VAR_INDEX_PREFIX << VarName
945 << ", " << FPName << ");" << std::endl;
946 } else {
947 C.indent() << "setVar("RS_EXPORT_VAR_INDEX_PREFIX << VarName
948 << ", v);" << std::endl;
949 }
950
951 // Dalvik update comes last, since the input may be invalid (and hence
952 // throw an exception).
953 C.indent() << RS_EXPORT_VAR_PREFIX << VarName << " = v;" << std::endl;
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700954
Shih-wei Liao9e86e192010-06-18 20:42:28 -0700955 C.endFunction();
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700956 }
Shih-wei Liao9e86e192010-06-18 20:42:28 -0700957
Stephen Hines0d26cef2012-05-01 19:23:01 -0700958 genGetExportVariable(C, TypeName, VarName);
Stephen Hines28d60bc2012-10-15 15:54:16 -0700959 genGetFieldID(C, VarName);
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700960 return;
Shih-wei Liao462aefd2010-06-04 15:32:04 -0700961}
962
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700963void RSReflection::genPointerTypeExportVariable(Context &C,
964 const RSExportVar *EV) {
965 const RSExportType *ET = EV->getType();
966 const RSExportType *PointeeType;
Shih-wei Liao462aefd2010-06-04 15:32:04 -0700967
Stephen Hines6e6578a2011-02-07 18:05:48 -0800968 slangAssert((ET->getClass() == RSExportType::ExportClassPointer) &&
969 "Variable should be type of pointer here");
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700970
971 PointeeType = static_cast<const RSExportPointerType*>(ET)->getPointeeType();
Stephen Hines0d26cef2012-05-01 19:23:01 -0700972 std::string TypeName = GetTypeName(ET);
973 std::string VarName = EV->getName();
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700974
Stephen Hines0d26cef2012-05-01 19:23:01 -0700975 genPrivateExportVariable(C, TypeName, VarName);
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700976
Zonr Chang89273bd2010-10-14 20:57:38 +0800977 // bind_*()
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700978 C.startFunction(Context::AM_Public,
979 false,
980 "void",
Stephen Hines0d26cef2012-05-01 19:23:01 -0700981 "bind_" + VarName,
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700982 1,
Zonr Changd42a4292010-10-17 02:38:43 +0800983 TypeName.c_str(), "v");
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700984
Stephen Hines0d26cef2012-05-01 19:23:01 -0700985 C.indent() << RS_EXPORT_VAR_PREFIX << VarName << " = v;" << std::endl;
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700986 C.indent() << "if (v == null) bindAllocation(null, "RS_EXPORT_VAR_INDEX_PREFIX
Stephen Hines0d26cef2012-05-01 19:23:01 -0700987 << VarName << ");" << std::endl;
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700988
989 if (PointeeType->getClass() == RSExportType::ExportClassRecord)
990 C.indent() << "else bindAllocation(v.getAllocation(), "
Stephen Hines0d26cef2012-05-01 19:23:01 -0700991 RS_EXPORT_VAR_INDEX_PREFIX << VarName << ");"
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700992 << std::endl;
993 else
994 C.indent() << "else bindAllocation(v, "RS_EXPORT_VAR_INDEX_PREFIX
Stephen Hines0d26cef2012-05-01 19:23:01 -0700995 << VarName << ");" << std::endl;
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700996
997 C.endFunction();
998
Stephen Hines0d26cef2012-05-01 19:23:01 -0700999 genGetExportVariable(C, TypeName, VarName);
Shih-wei Liao9ef2f782010-10-01 12:31:37 -07001000 return;
1001}
1002
1003void RSReflection::genVectorTypeExportVariable(Context &C,
1004 const RSExportVar *EV) {
Stephen Hines6e6578a2011-02-07 18:05:48 -08001005 slangAssert((EV->getType()->getClass() == RSExportType::ExportClassVector) &&
1006 "Variable should be type of vector here");
Shih-wei Liao9ef2f782010-10-01 12:31:37 -07001007
Stephen Hines0d26cef2012-05-01 19:23:01 -07001008 std::string TypeName = GetTypeName(EV->getType());
1009 std::string VarName = EV->getName();
Shih-wei Liao9ef2f782010-10-01 12:31:37 -07001010
Stephen Hines0d26cef2012-05-01 19:23:01 -07001011 genPrivateExportVariable(C, TypeName, VarName);
1012 genSetExportVariable(C, TypeName, EV);
1013 genGetExportVariable(C, TypeName, VarName);
Stephen Hines28d60bc2012-10-15 15:54:16 -07001014 genGetFieldID(C, VarName);
Shih-wei Liao9ef2f782010-10-01 12:31:37 -07001015 return;
1016}
1017
Zonr Chang92b344a2010-10-05 20:39:03 +08001018void RSReflection::genMatrixTypeExportVariable(Context &C,
1019 const RSExportVar *EV) {
Stephen Hines6e6578a2011-02-07 18:05:48 -08001020 slangAssert((EV->getType()->getClass() == RSExportType::ExportClassMatrix) &&
1021 "Variable should be type of matrix here");
Zonr Chang92b344a2010-10-05 20:39:03 +08001022
Stephen Hines0d26cef2012-05-01 19:23:01 -07001023 const RSExportType *ET = EV->getType();
1024 std::string TypeName = GetTypeName(ET);
1025 std::string VarName = EV->getName();
Zonr Chang92b344a2010-10-05 20:39:03 +08001026
Stephen Hines0d26cef2012-05-01 19:23:01 -07001027 genPrivateExportVariable(C, TypeName, VarName);
Zonr Chang92b344a2010-10-05 20:39:03 +08001028
1029 // set_*()
1030 if (!EV->isConst()) {
Stephen Hines0d26cef2012-05-01 19:23:01 -07001031 const char *FieldPackerName = "fp";
Stephen Hines1f6c3312012-07-03 17:23:33 -07001032 C.startFunction(Context::AM_PublicSynchronized,
Zonr Chang92b344a2010-10-05 20:39:03 +08001033 false,
1034 "void",
Stephen Hines0d26cef2012-05-01 19:23:01 -07001035 "set_" + VarName,
Zonr Chang92b344a2010-10-05 20:39:03 +08001036 1,
Stephen Hines0d26cef2012-05-01 19:23:01 -07001037 TypeName.c_str(), "v");
1038 C.indent() << RS_EXPORT_VAR_PREFIX << VarName << " = v;" << std::endl;
Zonr Chang92b344a2010-10-05 20:39:03 +08001039
Stephen Hines0d26cef2012-05-01 19:23:01 -07001040 if (genCreateFieldPacker(C, ET, FieldPackerName))
1041 genPackVarOfType(C, ET, "v", FieldPackerName);
1042 C.indent() << "setVar("RS_EXPORT_VAR_INDEX_PREFIX << VarName << ", "
Zonr Chang92b344a2010-10-05 20:39:03 +08001043 << FieldPackerName << ");" << std::endl;
1044
1045 C.endFunction();
1046 }
1047
Stephen Hines0d26cef2012-05-01 19:23:01 -07001048 genGetExportVariable(C, TypeName, VarName);
Stephen Hines28d60bc2012-10-15 15:54:16 -07001049 genGetFieldID(C, VarName);
Zonr Chang92b344a2010-10-05 20:39:03 +08001050 return;
1051}
1052
Stephen Hinese639eb52010-11-08 19:27:20 -08001053void RSReflection::genConstantArrayTypeExportVariable(
1054 Context &C,
1055 const RSExportVar *EV) {
Stephen Hines6e6578a2011-02-07 18:05:48 -08001056 slangAssert((EV->getType()->getClass() ==
1057 RSExportType::ExportClassConstantArray) &&
1058 "Variable should be type of constant array here");
Zonr Chang2e1dba62010-10-05 22:20:11 +08001059
Stephen Hines0d26cef2012-05-01 19:23:01 -07001060 std::string TypeName = GetTypeName(EV->getType());
1061 std::string VarName = EV->getName();
Zonr Chang2e1dba62010-10-05 22:20:11 +08001062
Stephen Hines0d26cef2012-05-01 19:23:01 -07001063 genPrivateExportVariable(C, TypeName, VarName);
1064 genSetExportVariable(C, TypeName, EV);
1065 genGetExportVariable(C, TypeName, VarName);
Stephen Hines28d60bc2012-10-15 15:54:16 -07001066 genGetFieldID(C, VarName);
Zonr Chang2e1dba62010-10-05 22:20:11 +08001067 return;
1068}
1069
Shih-wei Liao9ef2f782010-10-01 12:31:37 -07001070void RSReflection::genRecordTypeExportVariable(Context &C,
1071 const RSExportVar *EV) {
Stephen Hines6e6578a2011-02-07 18:05:48 -08001072 slangAssert((EV->getType()->getClass() == RSExportType::ExportClassRecord) &&
1073 "Variable should be type of struct here");
Shih-wei Liao9ef2f782010-10-01 12:31:37 -07001074
Stephen Hines0d26cef2012-05-01 19:23:01 -07001075 std::string TypeName = GetTypeName(EV->getType());
1076 std::string VarName = EV->getName();
Shih-wei Liao9ef2f782010-10-01 12:31:37 -07001077
Stephen Hines0d26cef2012-05-01 19:23:01 -07001078 genPrivateExportVariable(C, TypeName, VarName);
1079 genSetExportVariable(C, TypeName, EV);
1080 genGetExportVariable(C, TypeName, VarName);
Stephen Hines28d60bc2012-10-15 15:54:16 -07001081 genGetFieldID(C, VarName);
Stephen Hines0d26cef2012-05-01 19:23:01 -07001082 return;
1083}
1084
1085void RSReflection::genPrivateExportVariable(Context &C,
1086 const std::string &TypeName,
1087 const std::string &VarName) {
Shih-wei Liao9ef2f782010-10-01 12:31:37 -07001088 C.indent() << "private " << TypeName << " "RS_EXPORT_VAR_PREFIX
Stephen Hines0d26cef2012-05-01 19:23:01 -07001089 << VarName << ";" << std::endl;
1090 return;
1091}
Shih-wei Liao9ef2f782010-10-01 12:31:37 -07001092
Stephen Hines0d26cef2012-05-01 19:23:01 -07001093void RSReflection::genSetExportVariable(Context &C,
1094 const std::string &TypeName,
1095 const RSExportVar *EV) {
Shih-wei Liao9ef2f782010-10-01 12:31:37 -07001096 if (!EV->isConst()) {
Stephen Hines0d26cef2012-05-01 19:23:01 -07001097 const char *FieldPackerName = "fp";
1098 std::string VarName = EV->getName();
1099 const RSExportType *ET = EV->getType();
Stephen Hines1f6c3312012-07-03 17:23:33 -07001100 C.startFunction(Context::AM_PublicSynchronized,
Shih-wei Liao9ef2f782010-10-01 12:31:37 -07001101 false,
1102 "void",
Stephen Hines0d26cef2012-05-01 19:23:01 -07001103 "set_" + VarName,
Shih-wei Liao9ef2f782010-10-01 12:31:37 -07001104 1,
Zonr Changd42a4292010-10-17 02:38:43 +08001105 TypeName.c_str(), "v");
Stephen Hines0d26cef2012-05-01 19:23:01 -07001106 C.indent() << RS_EXPORT_VAR_PREFIX << VarName << " = v;" << std::endl;
Shih-wei Liao9ef2f782010-10-01 12:31:37 -07001107
Stephen Hines0d26cef2012-05-01 19:23:01 -07001108 if (genCreateFieldPacker(C, ET, FieldPackerName))
1109 genPackVarOfType(C, ET, "v", FieldPackerName);
Stephen Hinesa6b54142012-04-09 18:25:08 -07001110
1111 if (mRSContext->getTargetAPI() < SLANG_JB_TARGET_API) {
1112 // Legacy apps must use the old setVar() without Element/dim components.
Stephen Hines0d26cef2012-05-01 19:23:01 -07001113 C.indent() << "setVar("RS_EXPORT_VAR_INDEX_PREFIX << VarName
Stephen Hinesa6b54142012-04-09 18:25:08 -07001114 << ", " << FieldPackerName << ");" << std::endl;
1115 } else {
1116 // We only have support for one-dimensional array reflection today,
1117 // but the entry point (i.e. setVar()) takes an array of dimensions.
1118 C.indent() << "int []__dimArr = new int[1];" << std::endl;
Stephen Hines0d26cef2012-05-01 19:23:01 -07001119 C.indent() << "__dimArr[0] = " << ET->getSize() << ";" << std::endl;
1120 C.indent() << "setVar("RS_EXPORT_VAR_INDEX_PREFIX << VarName << ", "
Stephen Hinesa6b54142012-04-09 18:25:08 -07001121 << FieldPackerName << ", " RS_ELEM_PREFIX
Stephen Hines0d26cef2012-05-01 19:23:01 -07001122 << ET->getElementName() << ", __dimArr);" << std::endl;
Stephen Hinesa6b54142012-04-09 18:25:08 -07001123 }
Shih-wei Liao9ef2f782010-10-01 12:31:37 -07001124
1125 C.endFunction();
1126 }
Shih-wei Liao9ef2f782010-10-01 12:31:37 -07001127 return;
1128}
1129
1130void RSReflection::genGetExportVariable(Context &C,
1131 const std::string &TypeName,
1132 const std::string &VarName) {
1133 C.startFunction(Context::AM_Public,
1134 false,
1135 TypeName.c_str(),
1136 "get_" + VarName,
1137 0);
1138
1139 C.indent() << "return "RS_EXPORT_VAR_PREFIX << VarName << ";" << std::endl;
1140
1141 C.endFunction();
Stephen Hines28d60bc2012-10-15 15:54:16 -07001142}
1143
1144void RSReflection::genGetFieldID(Context &C, const std::string &VarName) {
1145 // We only generate getFieldID_*() for non-Pointer (bind) types.
Tim Murrayb81a9932012-10-10 15:56:02 -07001146 if (mRSContext->getTargetAPI() >= SLANG_JB_MR1_TARGET_API) {
Stephen Hines28d60bc2012-10-15 15:54:16 -07001147 C.startFunction(Context::AM_Public,
1148 false,
1149 "Script.FieldID",
1150 "getFieldID_" + VarName,
1151 0);
Tim Murrayb81a9932012-10-10 15:56:02 -07001152
Stephen Hines28d60bc2012-10-15 15:54:16 -07001153 C.indent() << "return createFieldID(" << RS_EXPORT_VAR_INDEX_PREFIX
1154 << VarName << ", null);" << std::endl;
Tim Murrayb81a9932012-10-10 15:56:02 -07001155
Stephen Hines28d60bc2012-10-15 15:54:16 -07001156 C.endFunction();
Tim Murrayb81a9932012-10-10 15:56:02 -07001157 }
Shih-wei Liao9ef2f782010-10-01 12:31:37 -07001158}
1159
1160/******************* Methods to generate script class /end *******************/
1161
1162bool RSReflection::genCreateFieldPacker(Context &C,
1163 const RSExportType *ET,
1164 const char *FieldPackerName) {
1165 size_t AllocSize = RSExportType::GetTypeAllocSize(ET);
1166 if (AllocSize > 0)
1167 C.indent() << "FieldPacker " << FieldPackerName << " = new FieldPacker("
1168 << AllocSize << ");" << std::endl;
1169 else
1170 return false;
1171 return true;
1172}
1173
1174void RSReflection::genPackVarOfType(Context &C,
1175 const RSExportType *ET,
1176 const char *VarName,
1177 const char *FieldPackerName) {
1178 switch (ET->getClass()) {
1179 case RSExportType::ExportClassPrimitive:
1180 case RSExportType::ExportClassVector: {
1181 C.indent() << FieldPackerName << "."
1182 << GetPackerAPIName(
1183 static_cast<const RSExportPrimitiveType*>(ET))
1184 << "(" << VarName << ");" << std::endl;
1185 break;
1186 }
Shih-wei Liao9ef2f782010-10-01 12:31:37 -07001187 case RSExportType::ExportClassPointer: {
1188 // Must reflect as type Allocation in Java
1189 const RSExportType *PointeeType =
1190 static_cast<const RSExportPointerType*>(ET)->getPointeeType();
1191
1192 if (PointeeType->getClass() != RSExportType::ExportClassRecord)
1193 C.indent() << FieldPackerName << ".addI32(" << VarName
1194 << ".getPtr());" << std::endl;
1195 else
1196 C.indent() << FieldPackerName << ".addI32(" << VarName
1197 << ".getAllocation().getPtr());" << std::endl;
1198 break;
1199 }
Zonr Chang92b344a2010-10-05 20:39:03 +08001200 case RSExportType::ExportClassMatrix: {
Alex Sakhartchoukaa180e92010-12-17 13:26:07 -08001201 C.indent() << FieldPackerName << ".addMatrix(" << VarName << ");"
Zonr Chang92b344a2010-10-05 20:39:03 +08001202 << std::endl;
1203 break;
1204 }
Zonr Chang2e1dba62010-10-05 22:20:11 +08001205 case RSExportType::ExportClassConstantArray: {
1206 const RSExportConstantArrayType *ECAT =
1207 static_cast<const RSExportConstantArrayType *>(ET);
Zonr Chang89273bd2010-10-14 20:57:38 +08001208
1209 // TODO(zonr): more elegant way. Currently, we obtain the unique index
1210 // variable (this method involves recursive call which means
1211 // we may have more than one level loop, therefore we can't
1212 // always use the same index variable name here) name given
1213 // in the for-loop from counting the '.' in @VarName.
1214 unsigned Level = 0;
1215 size_t LastDotPos = 0;
1216 std::string ElementVarName(VarName);
1217
1218 while (LastDotPos != std::string::npos) {
1219 LastDotPos = ElementVarName.find_first_of('.', LastDotPos + 1);
1220 Level++;
1221 }
1222 std::string IndexVarName("ct");
1223 IndexVarName.append(llvm::utostr_32(Level));
1224
1225 C.indent() << "for (int " << IndexVarName << " = 0; " <<
1226 IndexVarName << " < " << ECAT->getSize() << "; " <<
1227 IndexVarName << "++)";
Zonr Chang2e1dba62010-10-05 22:20:11 +08001228 C.startBlock();
1229
Zonr Chang89273bd2010-10-14 20:57:38 +08001230 ElementVarName.append("[" + IndexVarName + "]");
Zonr Chang2e1dba62010-10-05 22:20:11 +08001231 genPackVarOfType(C, ECAT->getElementType(), ElementVarName.c_str(),
1232 FieldPackerName);
1233
1234 C.endBlock();
1235 break;
1236 }
Shih-wei Liao9ef2f782010-10-01 12:31:37 -07001237 case RSExportType::ExportClassRecord: {
zonr6315f762010-10-05 15:35:14 +08001238 const RSExportRecordType *ERT =
1239 static_cast<const RSExportRecordType*>(ET);
Shih-wei Liao9ef2f782010-10-01 12:31:37 -07001240 // Relative pos from now on in field packer
1241 unsigned Pos = 0;
1242
1243 for (RSExportRecordType::const_field_iterator I = ERT->fields_begin(),
1244 E = ERT->fields_end();
1245 I != E;
1246 I++) {
1247 const RSExportRecordType::Field *F = *I;
1248 std::string FieldName;
1249 size_t FieldOffset = F->getOffsetInParent();
1250 size_t FieldStoreSize = RSExportType::GetTypeStoreSize(F->getType());
1251 size_t FieldAllocSize = RSExportType::GetTypeAllocSize(F->getType());
1252
1253 if (VarName != NULL)
1254 FieldName = VarName + ("." + F->getName());
1255 else
1256 FieldName = F->getName();
1257
1258 if (FieldOffset > Pos)
1259 C.indent() << FieldPackerName << ".skip("
1260 << (FieldOffset - Pos) << ");" << std::endl;
1261
1262 genPackVarOfType(C, F->getType(), FieldName.c_str(), FieldPackerName);
1263
1264 // There is padding in the field type
1265 if (FieldAllocSize > FieldStoreSize)
1266 C.indent() << FieldPackerName << ".skip("
1267 << (FieldAllocSize - FieldStoreSize)
1268 << ");" << std::endl;
1269
Stephen Hines7dd6da22012-11-15 19:56:03 -08001270 Pos = FieldOffset + FieldAllocSize;
Shih-wei Liao9ef2f782010-10-01 12:31:37 -07001271 }
1272
1273 // There maybe some padding after the struct
Andrew Hsiehd22724a2011-07-20 18:03:56 -07001274 if (RSExportType::GetTypeAllocSize(ERT) > Pos)
Stephen Hines6b201eb2011-07-20 17:08:16 -07001275 C.indent() << FieldPackerName << ".skip("
Andrew Hsiehd22724a2011-07-20 18:03:56 -07001276 << RSExportType::GetTypeAllocSize(ERT) - Pos << ");"
Shih-wei Liao9ef2f782010-10-01 12:31:37 -07001277 << std::endl;
1278 break;
1279 }
1280 default: {
Stephen Hines6e6578a2011-02-07 18:05:48 -08001281 slangAssert(false && "Unknown class of type");
Shih-wei Liao9ef2f782010-10-01 12:31:37 -07001282 }
1283 }
1284
1285 return;
1286}
1287
Zonr Chang2e1dba62010-10-05 22:20:11 +08001288void RSReflection::genAllocateVarOfType(Context &C,
1289 const RSExportType *T,
1290 const std::string &VarName) {
1291 switch (T->getClass()) {
1292 case RSExportType::ExportClassPrimitive: {
1293 // Primitive type like int in Java has its own storage once it's declared.
1294 //
1295 // FIXME: Should we allocate storage for RS object?
1296 // if (static_cast<const RSExportPrimitiveType *>(T)->isRSObjectType())
1297 // C.indent() << VarName << " = new " << GetTypeName(T) << "();"
1298 // << std::endl;
1299 break;
1300 }
1301 case RSExportType::ExportClassPointer: {
1302 // Pointer type is an instance of Allocation or a TypeClass whose value is
1303 // expected to be assigned by programmer later in Java program. Therefore
1304 // we don't reflect things like [VarName] = new Allocation();
1305 C.indent() << VarName << " = null;" << std::endl;
1306 break;
1307 }
1308 case RSExportType::ExportClassConstantArray: {
1309 const RSExportConstantArrayType *ECAT =
1310 static_cast<const RSExportConstantArrayType *>(T);
1311 const RSExportType *ElementType = ECAT->getElementType();
1312
Zonr Chang2f1451c2010-10-14 02:58:28 +08001313 C.indent() << VarName << " = new " << GetTypeName(ElementType)
1314 << "[" << ECAT->getSize() << "];" << std::endl;
1315
Zonr Chang2e1dba62010-10-05 22:20:11 +08001316 // Primitive type element doesn't need allocation code.
1317 if (ElementType->getClass() != RSExportType::ExportClassPrimitive) {
Zonr Chang2e1dba62010-10-05 22:20:11 +08001318 C.indent() << "for (int $ct = 0; $ct < " << ECAT->getSize() << "; "
1319 "$ct++)";
1320 C.startBlock();
1321
1322 std::string ElementVarName(VarName);
1323 ElementVarName.append("[$ct]");
1324 genAllocateVarOfType(C, ElementType, ElementVarName);
1325
1326 C.endBlock();
1327 }
1328 break;
1329 }
1330 case RSExportType::ExportClassVector:
1331 case RSExportType::ExportClassMatrix:
1332 case RSExportType::ExportClassRecord: {
1333 C.indent() << VarName << " = new " << GetTypeName(T) << "();"
1334 << std::endl;
1335 break;
1336 }
1337 }
1338 return;
1339}
1340
Stephen Hinese639eb52010-11-08 19:27:20 -08001341void RSReflection::genNewItemBufferIfNull(Context &C,
1342 const char *Index) {
1343 C.indent() << "if (" RS_TYPE_ITEM_BUFFER_NAME " == null) "
1344 RS_TYPE_ITEM_BUFFER_NAME " = "
1345 "new " RS_TYPE_ITEM_CLASS_NAME
1346 "[getType().getX() /* count */];"
Shih-wei Liao9ef2f782010-10-01 12:31:37 -07001347 << std::endl;
1348 if (Index != NULL)
1349 C.indent() << "if ("RS_TYPE_ITEM_BUFFER_NAME"[" << Index << "] == null) "
Zonr Chang92b344a2010-10-05 20:39:03 +08001350 RS_TYPE_ITEM_BUFFER_NAME"[" << Index << "] = "
1351 "new "RS_TYPE_ITEM_CLASS_NAME"();" << std::endl;
Shih-wei Liao9ef2f782010-10-01 12:31:37 -07001352 return;
1353}
1354
1355void RSReflection::genNewItemBufferPackerIfNull(Context &C) {
Stephen Hinese639eb52010-11-08 19:27:20 -08001356 C.indent() << "if (" RS_TYPE_ITEM_BUFFER_PACKER_NAME " == null) "
1357 RS_TYPE_ITEM_BUFFER_PACKER_NAME " = "
1358 "new FieldPacker(" RS_TYPE_ITEM_CLASS_NAME
1359 ".sizeof * getType().getX()/* count */"
1360 ");" << std::endl;
Shih-wei Liao9ef2f782010-10-01 12:31:37 -07001361 return;
1362}
1363
1364/********************** Methods to generate type class **********************/
1365bool RSReflection::genTypeClass(Context &C,
1366 const RSExportRecordType *ERT,
1367 std::string &ErrorMsg) {
Stephen Hinesa6b54142012-04-09 18:25:08 -07001368 std::string ClassName = ERT->getElementName();
Tim Murrayf69e1e52013-01-17 13:57:24 -08001369 std::string superClassName = C.getRSPackageName();
1370 superClassName += RS_TYPE_CLASS_SUPER_CLASS_NAME;
Shih-wei Liao9ef2f782010-10-01 12:31:37 -07001371
Shih-wei Liao9ef2f782010-10-01 12:31:37 -07001372 if (!C.startClass(Context::AM_Public,
1373 false,
1374 ClassName,
Tim Murrayf69e1e52013-01-17 13:57:24 -08001375 superClassName.c_str(),
Shih-wei Liao9ef2f782010-10-01 12:31:37 -07001376 ErrorMsg))
1377 return false;
1378
Stephen Hines4cc67fc2011-01-31 16:48:57 -08001379 mGeneratedFileNames->push_back(ClassName);
1380
Zonr Chang2e1dba62010-10-05 22:20:11 +08001381 genTypeItemClass(C, ERT);
Shih-wei Liao9ef2f782010-10-01 12:31:37 -07001382
1383 // Declare item buffer and item buffer packer
1384 C.indent() << "private "RS_TYPE_ITEM_CLASS_NAME" "RS_TYPE_ITEM_BUFFER_NAME"[]"
1385 ";" << std::endl;
1386 C.indent() << "private FieldPacker "RS_TYPE_ITEM_BUFFER_PACKER_NAME";"
1387 << std::endl;
Stephen Hinese67239d2012-02-24 15:08:36 -08001388 C.indent() << "private static java.lang.ref.WeakReference<Element> "
1389 RS_TYPE_ELEMENT_REF_NAME
Jason Sams381e95f2011-11-30 14:01:43 -08001390 " = new java.lang.ref.WeakReference<Element>(null);" << std::endl;
Shih-wei Liao9ef2f782010-10-01 12:31:37 -07001391
1392 genTypeClassConstructor(C, ERT);
Alex Sakhartchouk38eca1a2011-08-25 10:47:52 -07001393 genTypeClassCopyToArrayLocal(C, ERT);
Shih-wei Liao9ef2f782010-10-01 12:31:37 -07001394 genTypeClassCopyToArray(C, ERT);
1395 genTypeClassItemSetter(C, ERT);
1396 genTypeClassItemGetter(C, ERT);
1397 genTypeClassComponentSetter(C, ERT);
1398 genTypeClassComponentGetter(C, ERT);
1399 genTypeClassCopyAll(C, ERT);
Stephen Hines82754d82013-01-18 19:46:06 -08001400 if (!mRSContext->isCompatLib()) {
1401 // Skip the resize method if we are targeting a compatibility library.
1402 genTypeClassResize(C);
1403 }
Shih-wei Liao9ef2f782010-10-01 12:31:37 -07001404
1405 C.endClass();
1406
Zonr Chang66aa2992010-10-05 15:56:31 +08001407 C.resetFieldIndex();
1408 C.clearFieldIndexMap();
1409
Shih-wei Liao9ef2f782010-10-01 12:31:37 -07001410 return true;
1411}
1412
Stephen Hinese639eb52010-11-08 19:27:20 -08001413void RSReflection::genTypeItemClass(Context &C,
1414 const RSExportRecordType *ERT) {
Shih-wei Liao9ef2f782010-10-01 12:31:37 -07001415 C.indent() << "static public class "RS_TYPE_ITEM_CLASS_NAME;
1416 C.startBlock();
1417
1418 C.indent() << "public static final int sizeof = "
1419 << RSExportType::GetTypeAllocSize(ERT) << ";" << std::endl;
1420
1421 // Member elements
1422 C.out() << std::endl;
1423 for (RSExportRecordType::const_field_iterator FI = ERT->fields_begin(),
1424 FE = ERT->fields_end();
1425 FI != FE;
1426 FI++) {
1427 C.indent() << GetTypeName((*FI)->getType()) << " " << (*FI)->getName()
1428 << ";" << std::endl;
1429 }
1430
1431 // Constructor
1432 C.out() << std::endl;
1433 C.indent() << RS_TYPE_ITEM_CLASS_NAME"()";
1434 C.startBlock();
1435
1436 for (RSExportRecordType::const_field_iterator FI = ERT->fields_begin(),
1437 FE = ERT->fields_end();
1438 FI != FE;
1439 FI++) {
1440 const RSExportRecordType::Field *F = *FI;
Zonr Chang2e1dba62010-10-05 22:20:11 +08001441 genAllocateVarOfType(C, F->getType(), F->getName());
Shih-wei Liao9ef2f782010-10-01 12:31:37 -07001442 }
1443
1444 // end Constructor
1445 C.endBlock();
1446
1447 // end Item class
1448 C.endBlock();
1449
Zonr Chang2e1dba62010-10-05 22:20:11 +08001450 return;
Shih-wei Liao9ef2f782010-10-01 12:31:37 -07001451}
1452
1453void RSReflection::genTypeClassConstructor(Context &C,
1454 const RSExportRecordType *ERT) {
1455 const char *RenderScriptVar = "rs";
1456
1457 C.startFunction(Context::AM_Public,
1458 true,
1459 "Element",
1460 "createElement",
1461 1,
Zonr Changd42a4292010-10-17 02:38:43 +08001462 "RenderScript", RenderScriptVar);
Jason Sams381e95f2011-11-30 14:01:43 -08001463
Stephen Hinese67239d2012-02-24 15:08:36 -08001464 // TODO(all): Fix weak-refs + multi-context issue.
1465 // C.indent() << "Element e = " << RS_TYPE_ELEMENT_REF_NAME
1466 // << ".get();" << std::endl;
1467 // C.indent() << "if (e != null) return e;" << std::endl;
Jason Sams381e95f2011-11-30 14:01:43 -08001468 genBuildElement(C, "eb", ERT, RenderScriptVar, /* IsInline = */true);
Stephen Hinesee35ab72011-12-07 20:30:33 -08001469 C.indent() << "return eb.create();" << std::endl;
Stephen Hinese67239d2012-02-24 15:08:36 -08001470 // C.indent() << "e = eb.create();" << std::endl;
1471 // C.indent() << RS_TYPE_ELEMENT_REF_NAME
1472 // << " = new java.lang.ref.WeakReference<Element>(e);"
1473 // << std::endl;
1474 // C.indent() << "return e;" << std::endl;
Shih-wei Liao9ef2f782010-10-01 12:31:37 -07001475 C.endFunction();
1476
Jason Sams381e95f2011-11-30 14:01:43 -08001477
1478 // private with element
1479 C.startFunction(Context::AM_Private,
1480 false,
1481 NULL,
1482 C.getClassName(),
1483 1,
1484 "RenderScript", RenderScriptVar);
1485 C.indent() << RS_TYPE_ITEM_BUFFER_NAME" = null;" << std::endl;
1486 C.indent() << RS_TYPE_ITEM_BUFFER_PACKER_NAME" = null;" << std::endl;
Stephen Hinese67239d2012-02-24 15:08:36 -08001487 C.indent() << "mElement = createElement(" << RenderScriptVar << ");"
1488 << std::endl;
Jason Sams381e95f2011-11-30 14:01:43 -08001489 C.endFunction();
1490
1491 // 1D without usage
Shih-wei Liao9ef2f782010-10-01 12:31:37 -07001492 C.startFunction(Context::AM_Public,
1493 false,
1494 NULL,
1495 C.getClassName(),
1496 2,
Zonr Changd42a4292010-10-17 02:38:43 +08001497 "RenderScript", RenderScriptVar,
1498 "int", "count");
Shih-wei Liao9ef2f782010-10-01 12:31:37 -07001499
1500 C.indent() << RS_TYPE_ITEM_BUFFER_NAME" = null;" << std::endl;
1501 C.indent() << RS_TYPE_ITEM_BUFFER_PACKER_NAME" = null;" << std::endl;
1502 C.indent() << "mElement = createElement(" << RenderScriptVar << ");"
1503 << std::endl;
1504 // Call init() in super class
1505 C.indent() << "init(" << RenderScriptVar << ", count);" << std::endl;
1506 C.endFunction();
1507
Jason Sams381e95f2011-11-30 14:01:43 -08001508 // 1D with usage
Jason Sams91fe83b2010-12-06 17:07:12 -08001509 C.startFunction(Context::AM_Public,
1510 false,
1511 NULL,
1512 C.getClassName(),
1513 3,
1514 "RenderScript", RenderScriptVar,
1515 "int", "count",
1516 "int", "usages");
1517
1518 C.indent() << RS_TYPE_ITEM_BUFFER_NAME" = null;" << std::endl;
1519 C.indent() << RS_TYPE_ITEM_BUFFER_PACKER_NAME" = null;" << std::endl;
1520 C.indent() << "mElement = createElement(" << RenderScriptVar << ");"
1521 << std::endl;
1522 // Call init() in super class
1523 C.indent() << "init(" << RenderScriptVar << ", count, usages);" << std::endl;
1524 C.endFunction();
1525
Jason Sams381e95f2011-11-30 14:01:43 -08001526
1527 // create1D with usage
1528 C.startFunction(Context::AM_Public,
1529 true,
1530 C.getClassName().c_str(),
1531 "create1D",
1532 3,
1533 "RenderScript", RenderScriptVar,
1534 "int", "dimX",
1535 "int", "usages");
1536 C.indent() << C.getClassName() << " obj = new " << C.getClassName() << "("
1537 << RenderScriptVar << ");" << std::endl;
Stephen Hinese67239d2012-02-24 15:08:36 -08001538 C.indent() << "obj.mAllocation = Allocation.createSized("
1539 "rs, obj.mElement, dimX, usages);" << std::endl;
Jason Sams381e95f2011-11-30 14:01:43 -08001540 C.indent() << "return obj;" << std::endl;
1541 C.endFunction();
1542
1543 // create1D without usage
1544 C.startFunction(Context::AM_Public,
1545 true,
1546 C.getClassName().c_str(),
1547 "create1D",
1548 2,
1549 "RenderScript", RenderScriptVar,
1550 "int", "dimX");
Stephen Hinese67239d2012-02-24 15:08:36 -08001551 C.indent() << "return create1D(" << RenderScriptVar
1552 << ", dimX, Allocation.USAGE_SCRIPT);" << std::endl;
Jason Sams381e95f2011-11-30 14:01:43 -08001553 C.endFunction();
1554
1555
1556 // create2D without usage
1557 C.startFunction(Context::AM_Public,
1558 true,
1559 C.getClassName().c_str(),
1560 "create2D",
1561 3,
1562 "RenderScript", RenderScriptVar,
1563 "int", "dimX",
1564 "int", "dimY");
Stephen Hinese67239d2012-02-24 15:08:36 -08001565 C.indent() << "return create2D(" << RenderScriptVar
1566 << ", dimX, dimY, Allocation.USAGE_SCRIPT);" << std::endl;
Jason Sams381e95f2011-11-30 14:01:43 -08001567 C.endFunction();
1568
1569 // create2D with usage
1570 C.startFunction(Context::AM_Public,
1571 true,
1572 C.getClassName().c_str(),
1573 "create2D",
1574 4,
1575 "RenderScript", RenderScriptVar,
1576 "int", "dimX",
1577 "int", "dimY",
1578 "int", "usages");
1579
1580 C.indent() << C.getClassName() << " obj = new " << C.getClassName() << "("
1581 << RenderScriptVar << ");" << std::endl;
Stephen Hinese67239d2012-02-24 15:08:36 -08001582 C.indent() << "Type.Builder b = new Type.Builder(rs, obj.mElement);"
1583 << std::endl;
Jason Sams381e95f2011-11-30 14:01:43 -08001584 C.indent() << "b.setX(dimX);" << std::endl;
1585 C.indent() << "b.setY(dimY);" << std::endl;
1586 C.indent() << "Type t = b.create();" << std::endl;
Stephen Hinese67239d2012-02-24 15:08:36 -08001587 C.indent() << "obj.mAllocation = Allocation.createTyped(rs, t, usages);"
1588 << std::endl;
Jason Sams381e95f2011-11-30 14:01:43 -08001589 C.indent() << "return obj;" << std::endl;
1590 C.endFunction();
1591
1592
1593 // createTypeBuilder
1594 C.startFunction(Context::AM_Public,
1595 true,
1596 "Type.Builder",
1597 "createTypeBuilder",
1598 1,
1599 "RenderScript", RenderScriptVar);
Stephen Hinese67239d2012-02-24 15:08:36 -08001600 C.indent() << "Element e = createElement(" << RenderScriptVar << ");"
1601 << std::endl;
Jason Sams381e95f2011-11-30 14:01:43 -08001602 C.indent() << "return new Type.Builder(rs, e);" << std::endl;
1603 C.endFunction();
1604
1605 // createCustom with usage
1606 C.startFunction(Context::AM_Public,
1607 true,
1608 C.getClassName().c_str(),
1609 "createCustom",
1610 3,
1611 "RenderScript", RenderScriptVar,
1612 "Type.Builder", "tb",
1613 "int", "usages");
1614 C.indent() << C.getClassName() << " obj = new " << C.getClassName() << "("
1615 << RenderScriptVar << ");" << std::endl;
1616 C.indent() << "Type t = tb.create();" << std::endl;
1617 C.indent() << "if (t.getElement() != obj.mElement) {" << std::endl;
Stephen Hinese67239d2012-02-24 15:08:36 -08001618 C.indent() << " throw new RSIllegalArgumentException("
1619 "\"Type.Builder did not match expected element type.\");"
Jason Sams381e95f2011-11-30 14:01:43 -08001620 << std::endl;
1621 C.indent() << "}" << std::endl;
Stephen Hinese67239d2012-02-24 15:08:36 -08001622 C.indent() << "obj.mAllocation = Allocation.createTyped(rs, t, usages);"
1623 << std::endl;
Jason Sams381e95f2011-11-30 14:01:43 -08001624 C.indent() << "return obj;" << std::endl;
1625 C.endFunction();
Shih-wei Liao9ef2f782010-10-01 12:31:37 -07001626}
1627
Jason Sams381e95f2011-11-30 14:01:43 -08001628
Shih-wei Liao9ef2f782010-10-01 12:31:37 -07001629void RSReflection::genTypeClassCopyToArray(Context &C,
1630 const RSExportRecordType *ERT) {
1631 C.startFunction(Context::AM_Private,
1632 false,
1633 "void",
1634 "copyToArray",
1635 2,
Zonr Changd42a4292010-10-17 02:38:43 +08001636 RS_TYPE_ITEM_CLASS_NAME, "i",
1637 "int", "index");
Shih-wei Liao9ef2f782010-10-01 12:31:37 -07001638
1639 genNewItemBufferPackerIfNull(C);
1640 C.indent() << RS_TYPE_ITEM_BUFFER_PACKER_NAME
Zonr Chang92b344a2010-10-05 20:39:03 +08001641 ".reset(index * "RS_TYPE_ITEM_CLASS_NAME".sizeof);"
1642 << std::endl;
Shih-wei Liao9ef2f782010-10-01 12:31:37 -07001643
Alex Sakhartchouk38eca1a2011-08-25 10:47:52 -07001644 C.indent() << "copyToArrayLocal(i, " RS_TYPE_ITEM_BUFFER_PACKER_NAME
1645 ");" << std::endl;
1646
1647 C.endFunction();
1648 return;
1649}
1650
1651void RSReflection::genTypeClassCopyToArrayLocal(Context &C,
1652 const RSExportRecordType *ERT) {
1653 C.startFunction(Context::AM_Private,
1654 false,
1655 "void",
1656 "copyToArrayLocal",
1657 2,
1658 RS_TYPE_ITEM_CLASS_NAME, "i",
1659 "FieldPacker", "fp");
1660
1661 genPackVarOfType(C, ERT, "i", "fp");
Shih-wei Liao9ef2f782010-10-01 12:31:37 -07001662
1663 C.endFunction();
1664 return;
1665}
1666
1667void RSReflection::genTypeClassItemSetter(Context &C,
zonr6315f762010-10-05 15:35:14 +08001668 const RSExportRecordType *ERT) {
Alex Sakhartchouk38eca1a2011-08-25 10:47:52 -07001669 C.startFunction(Context::AM_PublicSynchronized,
Shih-wei Liao9ef2f782010-10-01 12:31:37 -07001670 false,
1671 "void",
1672 "set",
1673 3,
Zonr Changd42a4292010-10-17 02:38:43 +08001674 RS_TYPE_ITEM_CLASS_NAME, "i",
1675 "int", "index",
1676 "boolean", "copyNow");
Shih-wei Liao9ef2f782010-10-01 12:31:37 -07001677 genNewItemBufferIfNull(C, NULL);
1678 C.indent() << RS_TYPE_ITEM_BUFFER_NAME"[index] = i;" << std::endl;
1679
1680 C.indent() << "if (copyNow) ";
1681 C.startBlock();
1682
1683 C.indent() << "copyToArray(i, index);" << std::endl;
Alex Sakhartchouk38eca1a2011-08-25 10:47:52 -07001684 C.indent() << "FieldPacker fp = new FieldPacker(" RS_TYPE_ITEM_CLASS_NAME
1685 ".sizeof);" << std::endl;
1686 C.indent() << "copyToArrayLocal(i, fp);" << std::endl;
1687 C.indent() << "mAllocation.setFromFieldPacker(index, fp);" << std::endl;
Shih-wei Liao9ef2f782010-10-01 12:31:37 -07001688
1689 // End of if (copyNow)
1690 C.endBlock();
1691
1692 C.endFunction();
1693 return;
1694}
1695
1696void RSReflection::genTypeClassItemGetter(Context &C,
1697 const RSExportRecordType *ERT) {
Alex Sakhartchouk38eca1a2011-08-25 10:47:52 -07001698 C.startFunction(Context::AM_PublicSynchronized,
Shih-wei Liao9ef2f782010-10-01 12:31:37 -07001699 false,
1700 RS_TYPE_ITEM_CLASS_NAME,
1701 "get",
1702 1,
Zonr Changd42a4292010-10-17 02:38:43 +08001703 "int", "index");
Shih-wei Liao9ef2f782010-10-01 12:31:37 -07001704 C.indent() << "if ("RS_TYPE_ITEM_BUFFER_NAME" == null) return null;"
1705 << std::endl;
1706 C.indent() << "return "RS_TYPE_ITEM_BUFFER_NAME"[index];" << std::endl;
1707 C.endFunction();
1708 return;
1709}
1710
1711void RSReflection::genTypeClassComponentSetter(Context &C,
1712 const RSExportRecordType *ERT) {
1713 for (RSExportRecordType::const_field_iterator FI = ERT->fields_begin(),
1714 FE = ERT->fields_end();
1715 FI != FE;
1716 FI++) {
1717 const RSExportRecordType::Field *F = *FI;
1718 size_t FieldOffset = F->getOffsetInParent();
1719 size_t FieldStoreSize = RSExportType::GetTypeStoreSize(F->getType());
1720 unsigned FieldIndex = C.getFieldIndex(F);
1721
Alex Sakhartchouk38eca1a2011-08-25 10:47:52 -07001722 C.startFunction(Context::AM_PublicSynchronized,
Shih-wei Liao9ef2f782010-10-01 12:31:37 -07001723 false,
1724 "void",
1725 "set_" + F->getName(), 3,
Zonr Changd42a4292010-10-17 02:38:43 +08001726 "int", "index",
1727 GetTypeName(F->getType()).c_str(), "v",
1728 "boolean", "copyNow");
Shih-wei Liao9c631ff2010-09-17 11:57:29 -07001729 genNewItemBufferPackerIfNull(C);
Shih-wei Liao9ef2f782010-10-01 12:31:37 -07001730 genNewItemBufferIfNull(C, "index");
1731 C.indent() << RS_TYPE_ITEM_BUFFER_NAME"[index]." << F->getName()
1732 << " = v;" << std::endl;
Shih-wei Liao462aefd2010-06-04 15:32:04 -07001733
1734 C.indent() << "if (copyNow) ";
1735 C.startBlock();
Shih-wei Liao2dd42ff2010-06-15 00:34:38 -07001736
Shih-wei Liao9ef2f782010-10-01 12:31:37 -07001737 if (FieldOffset > 0)
Zonr Chang92b344a2010-10-05 20:39:03 +08001738 C.indent() << RS_TYPE_ITEM_BUFFER_PACKER_NAME
1739 ".reset(index * "RS_TYPE_ITEM_CLASS_NAME".sizeof + "
1740 << FieldOffset << ");" << std::endl;
Shih-wei Liao9ef2f782010-10-01 12:31:37 -07001741 else
Zonr Chang92b344a2010-10-05 20:39:03 +08001742 C.indent() << RS_TYPE_ITEM_BUFFER_PACKER_NAME
1743 ".reset(index * "RS_TYPE_ITEM_CLASS_NAME".sizeof);"
1744 << std::endl;
Shih-wei Liao9ef2f782010-10-01 12:31:37 -07001745 genPackVarOfType(C, F->getType(), "v", RS_TYPE_ITEM_BUFFER_PACKER_NAME);
Shih-wei Liao462aefd2010-06-04 15:32:04 -07001746
Shih-wei Liao9ef2f782010-10-01 12:31:37 -07001747 C.indent() << "FieldPacker fp = new FieldPacker(" << FieldStoreSize << ");"
1748 << std::endl;
1749 genPackVarOfType(C, F->getType(), "v", "fp");
Jason Samsa036a8a2011-01-16 14:52:39 -08001750 C.indent() << "mAllocation.setFromFieldPacker(index, " << FieldIndex
Stephen Hines32917932011-01-11 20:51:53 -08001751 << ", fp);"
Zonr Chang66aa2992010-10-05 15:56:31 +08001752 << std::endl;
Shih-wei Liao9ef2f782010-10-01 12:31:37 -07001753
1754 // End of if (copyNow)
1755 C.endBlock();
Shih-wei Liao462aefd2010-06-04 15:32:04 -07001756
1757 C.endFunction();
Shih-wei Liao9ef2f782010-10-01 12:31:37 -07001758 }
1759 return;
Shih-wei Liao462aefd2010-06-04 15:32:04 -07001760}
1761
Shih-wei Liao9ef2f782010-10-01 12:31:37 -07001762void RSReflection::genTypeClassComponentGetter(Context &C,
1763 const RSExportRecordType *ERT) {
1764 for (RSExportRecordType::const_field_iterator FI = ERT->fields_begin(),
1765 FE = ERT->fields_end();
1766 FI != FE;
1767 FI++) {
1768 const RSExportRecordType::Field *F = *FI;
Alex Sakhartchouk38eca1a2011-08-25 10:47:52 -07001769 C.startFunction(Context::AM_PublicSynchronized,
Shih-wei Liao9ef2f782010-10-01 12:31:37 -07001770 false,
1771 GetTypeName(F->getType()).c_str(),
1772 "get_" + F->getName(),
1773 1,
Zonr Changd42a4292010-10-17 02:38:43 +08001774 "int", "index");
Shih-wei Liaocf950c42010-10-06 03:44:40 -07001775 C.indent() << "if ("RS_TYPE_ITEM_BUFFER_NAME" == null) return "
1776 << GetTypeNullValue(F->getType()) << ";" << std::endl;
zonr6315f762010-10-05 15:35:14 +08001777 C.indent() << "return "RS_TYPE_ITEM_BUFFER_NAME"[index]." << F->getName()
1778 << ";" << std::endl;
Shih-wei Liao9b1f50b2010-08-05 12:40:41 -07001779 C.endFunction();
Shih-wei Liao9ef2f782010-10-01 12:31:37 -07001780 }
1781 return;
Shih-wei Liao9c631ff2010-09-17 11:57:29 -07001782}
1783
Shih-wei Liao9ef2f782010-10-01 12:31:37 -07001784void RSReflection::genTypeClassCopyAll(Context &C,
1785 const RSExportRecordType *ERT) {
Alex Sakhartchouk38eca1a2011-08-25 10:47:52 -07001786 C.startFunction(Context::AM_PublicSynchronized, false, "void", "copyAll", 0);
Shih-wei Liao9ef2f782010-10-01 12:31:37 -07001787
Zonr Chang2e1dba62010-10-05 22:20:11 +08001788 C.indent() << "for (int ct = 0; ct < "RS_TYPE_ITEM_BUFFER_NAME".length; ct++)"
1789 " copyToArray("RS_TYPE_ITEM_BUFFER_NAME"[ct], ct);"
Shih-wei Liao9ef2f782010-10-01 12:31:37 -07001790 << std::endl;
Stephen Hines06617782011-01-17 12:24:01 -08001791 C.indent() << "mAllocation.setFromFieldPacker(0, "
1792 RS_TYPE_ITEM_BUFFER_PACKER_NAME");"
Shih-wei Liao9ef2f782010-10-01 12:31:37 -07001793 << std::endl;
1794
1795 C.endFunction();
1796 return;
Shih-wei Liao9c631ff2010-09-17 11:57:29 -07001797}
1798
Zonr Changd42a4292010-10-17 02:38:43 +08001799void RSReflection::genTypeClassResize(Context &C) {
Alex Sakhartchouk38eca1a2011-08-25 10:47:52 -07001800 C.startFunction(Context::AM_PublicSynchronized,
Zonr Changd42a4292010-10-17 02:38:43 +08001801 false,
1802 "void",
1803 "resize",
1804 1,
1805 "int", "newSize");
1806
Jason Sams3caea7d2010-10-25 18:15:53 -07001807 C.indent() << "if (mItemArray != null) ";
1808 C.startBlock();
Zonr Changd42a4292010-10-17 02:38:43 +08001809 C.indent() << "int oldSize = mItemArray.length;" << std::endl;
1810 C.indent() << "int copySize = Math.min(oldSize, newSize);" << std::endl;
Jason Sams3caea7d2010-10-25 18:15:53 -07001811 C.indent() << "if (newSize == oldSize) return;" << std::endl;
Zonr Changd42a4292010-10-17 02:38:43 +08001812 C.indent() << "Item ni[] = new Item[newSize];" << std::endl;
1813 C.indent() << "System.arraycopy(mItemArray, 0, ni, 0, copySize);"
1814 << std::endl;
1815 C.indent() << "mItemArray = ni;" << std::endl;
Jason Sams3caea7d2010-10-25 18:15:53 -07001816 C.endBlock();
Zonr Changd42a4292010-10-17 02:38:43 +08001817 C.indent() << "mAllocation.resize(newSize);" << std::endl;
1818
Jason Samscedffd92010-12-16 15:29:49 -08001819 C.indent() << "if (" RS_TYPE_ITEM_BUFFER_PACKER_NAME " != null) "
1820 RS_TYPE_ITEM_BUFFER_PACKER_NAME " = "
1821 "new FieldPacker(" RS_TYPE_ITEM_CLASS_NAME
1822 ".sizeof * getType().getX()/* count */"
1823 ");" << std::endl;
1824
Zonr Changd42a4292010-10-17 02:38:43 +08001825 C.endFunction();
1826 return;
1827}
1828
Shih-wei Liao9ef2f782010-10-01 12:31:37 -07001829/******************** Methods to generate type class /end ********************/
Shih-wei Liao462aefd2010-06-04 15:32:04 -07001830
Shih-wei Liao9ef2f782010-10-01 12:31:37 -07001831/********** Methods to create Element in Java of given record type ***********/
Zonr Chang89273bd2010-10-14 20:57:38 +08001832void RSReflection::genBuildElement(Context &C,
1833 const char *ElementBuilderName,
1834 const RSExportRecordType *ERT,
1835 const char *RenderScriptVar,
1836 bool IsInline) {
Shih-wei Liao9ef2f782010-10-01 12:31:37 -07001837 C.indent() << "Element.Builder " << ElementBuilderName << " = "
1838 "new Element.Builder(" << RenderScriptVar << ");" << std::endl;
1839
1840 // eb.add(...)
1841 genAddElementToElementBuilder(C,
1842 ERT,
1843 "",
1844 ElementBuilderName,
Zonr Chang89273bd2010-10-14 20:57:38 +08001845 RenderScriptVar,
1846 /* ArraySize = */0);
Shih-wei Liao9ef2f782010-10-01 12:31:37 -07001847
Zonr Chang89273bd2010-10-14 20:57:38 +08001848 if (!IsInline)
1849 C.indent() << "return " << ElementBuilderName << ".create();" << std::endl;
Shih-wei Liao9ef2f782010-10-01 12:31:37 -07001850 return;
Shih-wei Liao462aefd2010-06-04 15:32:04 -07001851}
1852
Zonr Chang89273bd2010-10-14 20:57:38 +08001853#define EB_ADD(x) do { \
Zonr Chang92b344a2010-10-05 20:39:03 +08001854 C.indent() << ElementBuilderName \
Zonr Chang89273bd2010-10-14 20:57:38 +08001855 << ".add(" << x << ", \"" << VarName << "\""; \
1856 if (ArraySize > 0) \
1857 C.out() << ", " << ArraySize; \
1858 C.out() << ");" << std::endl; \
1859 C.incFieldIndex(); \
1860} while (false)
Shih-wei Liao462aefd2010-06-04 15:32:04 -07001861
Shih-wei Liao9ef2f782010-10-01 12:31:37 -07001862void RSReflection::genAddElementToElementBuilder(Context &C,
1863 const RSExportType *ET,
1864 const std::string &VarName,
1865 const char *ElementBuilderName,
Zonr Chang89273bd2010-10-14 20:57:38 +08001866 const char *RenderScriptVar,
1867 unsigned ArraySize) {
Stephen Hines47aca4e2012-03-08 20:07:28 -08001868 std::string ElementConstruct = GetBuiltinElementConstruct(ET);
Shih-wei Liao462aefd2010-06-04 15:32:04 -07001869
Stephen Hines47aca4e2012-03-08 20:07:28 -08001870 if (ElementConstruct != "") {
Shih-wei Liao9ef2f782010-10-01 12:31:37 -07001871 EB_ADD(ElementConstruct << "(" << RenderScriptVar << ")");
1872 } else {
1873 if ((ET->getClass() == RSExportType::ExportClassPrimitive) ||
Zonr Chang2e1dba62010-10-05 22:20:11 +08001874 (ET->getClass() == RSExportType::ExportClassVector)) {
Shih-wei Liao9ef2f782010-10-01 12:31:37 -07001875 const RSExportPrimitiveType *EPT =
1876 static_cast<const RSExportPrimitiveType*>(ET);
Stephen Hines47aca4e2012-03-08 20:07:28 -08001877 const char *DataTypeName =
1878 RSExportPrimitiveType::getRSReflectionType(EPT)->rs_type;
Shih-wei Liao9ef2f782010-10-01 12:31:37 -07001879 int Size = (ET->getClass() == RSExportType::ExportClassVector) ?
1880 static_cast<const RSExportVectorType*>(ET)->getNumElement() :
1881 1;
Shih-wei Liao462aefd2010-06-04 15:32:04 -07001882
Stephen Hines47aca4e2012-03-08 20:07:28 -08001883 if (EPT->getClass() == RSExportType::ExportClassPrimitive) {
1884 // Element.createUser()
1885 EB_ADD("Element.createUser(" << RenderScriptVar
1886 << ", Element.DataType."
1887 << DataTypeName << ")");
1888 } else {
1889 slangAssert((ET->getClass() == RSExportType::ExportClassVector) &&
1890 "Unexpected type.");
1891 EB_ADD("Element.createVector(" << RenderScriptVar
1892 << ", Element.DataType."
1893 << DataTypeName << ", "
1894 << Size << ")");
Shih-wei Liaobd49c8f2010-06-17 09:51:36 -07001895 }
Zonr Chang92b344a2010-10-05 20:39:03 +08001896#ifndef NDEBUG
Shih-wei Liao9ef2f782010-10-01 12:31:37 -07001897 } else if (ET->getClass() == RSExportType::ExportClassPointer) {
1898 // Pointer type variable should be resolved in
1899 // GetBuiltinElementConstruct()
Stephen Hines6e6578a2011-02-07 18:05:48 -08001900 slangAssert(false && "??");
Zonr Chang92b344a2010-10-05 20:39:03 +08001901 } else if (ET->getClass() == RSExportType::ExportClassMatrix) {
1902 // Matrix type variable should be resolved
1903 // in GetBuiltinElementConstruct()
Stephen Hines6e6578a2011-02-07 18:05:48 -08001904 slangAssert(false && "??");
Zonr Chang92b344a2010-10-05 20:39:03 +08001905#endif
Zonr Chang2e1dba62010-10-05 22:20:11 +08001906 } else if (ET->getClass() == RSExportType::ExportClassConstantArray) {
1907 const RSExportConstantArrayType *ECAT =
1908 static_cast<const RSExportConstantArrayType *>(ET);
Zonr Chang2e1dba62010-10-05 22:20:11 +08001909
Zonr Chang89273bd2010-10-14 20:57:38 +08001910 const RSExportType *ElementType = ECAT->getElementType();
1911 if (ElementType->getClass() != RSExportType::ExportClassRecord) {
1912 genAddElementToElementBuilder(C,
1913 ECAT->getElementType(),
1914 VarName,
1915 ElementBuilderName,
1916 RenderScriptVar,
1917 ECAT->getSize());
1918 } else {
1919 std::string NewElementBuilderName(ElementBuilderName);
1920 NewElementBuilderName.append(1, '_');
Zonr Chang2e1dba62010-10-05 22:20:11 +08001921
Zonr Chang89273bd2010-10-14 20:57:38 +08001922 genBuildElement(C,
1923 NewElementBuilderName.c_str(),
1924 static_cast<const RSExportRecordType*>(ElementType),
1925 RenderScriptVar,
1926 /* IsInline = */true);
1927 ArraySize = ECAT->getSize();
1928 EB_ADD(NewElementBuilderName << ".create()");
1929 }
Shih-wei Liao9ef2f782010-10-01 12:31:37 -07001930 } else if (ET->getClass() == RSExportType::ExportClassRecord) {
1931 // Simalar to case of RSExportType::ExportClassRecord in genPackVarOfType.
1932 //
zonr6315f762010-10-05 15:35:14 +08001933 // TODO(zonr): Generalize these two function such that there's no
1934 // duplicated codes.
Shih-wei Liao9ef2f782010-10-01 12:31:37 -07001935 const RSExportRecordType *ERT =
1936 static_cast<const RSExportRecordType*>(ET);
1937 int Pos = 0; // relative pos from now on
1938
1939 for (RSExportRecordType::const_field_iterator I = ERT->fields_begin(),
1940 E = ERT->fields_end();
1941 I != E;
1942 I++) {
1943 const RSExportRecordType::Field *F = *I;
1944 std::string FieldName;
1945 int FieldOffset = F->getOffsetInParent();
1946 int FieldStoreSize = RSExportType::GetTypeStoreSize(F->getType());
1947 int FieldAllocSize = RSExportType::GetTypeAllocSize(F->getType());
1948
1949 if (!VarName.empty())
1950 FieldName = VarName + "." + F->getName();
1951 else
1952 FieldName = F->getName();
1953
1954 // Alignment
1955 genAddPaddingToElementBuiler(C,
1956 (FieldOffset - Pos),
1957 ElementBuilderName,
1958 RenderScriptVar);
1959
1960 // eb.add(...)
1961 C.addFieldIndexMapping(F);
Zonr Chang89273bd2010-10-14 20:57:38 +08001962 if (F->getType()->getClass() != RSExportType::ExportClassRecord) {
1963 genAddElementToElementBuilder(C,
1964 F->getType(),
1965 FieldName,
1966 ElementBuilderName,
1967 RenderScriptVar,
1968 0);
1969 } else {
1970 std::string NewElementBuilderName(ElementBuilderName);
1971 NewElementBuilderName.append(1, '_');
1972
1973 genBuildElement(C,
1974 NewElementBuilderName.c_str(),
1975 static_cast<const RSExportRecordType*>(F->getType()),
1976 RenderScriptVar,
1977 /* IsInline = */true);
1978
1979 const std::string &VarName = FieldName; // Hack for EB_ADD macro
1980 EB_ADD(NewElementBuilderName << ".create()");
1981 }
Shih-wei Liao9ef2f782010-10-01 12:31:37 -07001982
Stephen Hinesa9ae5ae2011-11-11 21:16:59 -08001983 if (mRSContext->getTargetAPI() < SLANG_ICS_TARGET_API) {
1984 // There is padding within the field type. This is only necessary
1985 // for HC-targeted APIs.
1986 genAddPaddingToElementBuiler(C,
1987 (FieldAllocSize - FieldStoreSize),
1988 ElementBuilderName,
1989 RenderScriptVar);
1990 }
Shih-wei Liao9ef2f782010-10-01 12:31:37 -07001991
1992 Pos = FieldOffset + FieldAllocSize;
1993 }
1994
1995 // There maybe some padding after the struct
Stephen Hines049229b2011-08-04 09:59:23 -07001996 size_t RecordAllocSize = RSExportType::GetTypeAllocSize(ERT);
Shih-wei Liao9ef2f782010-10-01 12:31:37 -07001997
1998 genAddPaddingToElementBuiler(C,
Stephen Hines049229b2011-08-04 09:59:23 -07001999 RecordAllocSize - Pos,
Shih-wei Liao9ef2f782010-10-01 12:31:37 -07002000 ElementBuilderName,
2001 RenderScriptVar);
2002 } else {
Stephen Hines6e6578a2011-02-07 18:05:48 -08002003 slangAssert(false && "Unknown class of type");
Shih-wei Liaob1a28e72010-06-16 16:31:32 -07002004 }
Shih-wei Liao9ef2f782010-10-01 12:31:37 -07002005 }
Shih-wei Liao462aefd2010-06-04 15:32:04 -07002006}
2007
Shih-wei Liao9ef2f782010-10-01 12:31:37 -07002008void RSReflection::genAddPaddingToElementBuiler(Context &C,
2009 int PaddingSize,
2010 const char *ElementBuilderName,
zonr6315f762010-10-05 15:35:14 +08002011 const char *RenderScriptVar) {
Zonr Chang89273bd2010-10-14 20:57:38 +08002012 unsigned ArraySize = 0; // Hack the EB_ADD macro
Shih-wei Liao9ef2f782010-10-01 12:31:37 -07002013 while (PaddingSize > 0) {
2014 const std::string &VarName = C.createPaddingField();
2015 if (PaddingSize >= 4) {
Zonr Chang89273bd2010-10-14 20:57:38 +08002016 EB_ADD("Element.U32(" << RenderScriptVar << ")");
Shih-wei Liao9ef2f782010-10-01 12:31:37 -07002017 PaddingSize -= 4;
2018 } else if (PaddingSize >= 2) {
Zonr Chang89273bd2010-10-14 20:57:38 +08002019 EB_ADD("Element.U16(" << RenderScriptVar << ")");
Shih-wei Liao9ef2f782010-10-01 12:31:37 -07002020 PaddingSize -= 2;
2021 } else if (PaddingSize >= 1) {
Zonr Chang89273bd2010-10-14 20:57:38 +08002022 EB_ADD("Element.U8(" << RenderScriptVar << ")");
Shih-wei Liao9ef2f782010-10-01 12:31:37 -07002023 PaddingSize -= 1;
Shih-wei Liao462aefd2010-06-04 15:32:04 -07002024 }
Shih-wei Liao9ef2f782010-10-01 12:31:37 -07002025 }
2026 return;
Shih-wei Liao462aefd2010-06-04 15:32:04 -07002027}
2028
2029#undef EB_ADD
Shih-wei Liao9ef2f782010-10-01 12:31:37 -07002030/******** Methods to create Element in Java of given record type /end ********/
Shih-wei Liao462aefd2010-06-04 15:32:04 -07002031
Shih-wei Liaob81c6a42010-10-10 14:15:00 -07002032bool RSReflection::reflect(const std::string &OutputPathBase,
2033 const std::string &OutputPackageName,
Stephen Hines0a813a32012-08-03 16:52:40 -07002034 const std::string &RSPackageName,
Shih-wei Liao9ef2f782010-10-01 12:31:37 -07002035 const std::string &InputFileName,
2036 const std::string &OutputBCFileName) {
2037 Context *C = NULL;
2038 std::string ResourceId = "";
Stephen Hinesa9ae5ae2011-11-11 21:16:59 -08002039 std::string PaddingPrefix = "";
2040
2041 if (mRSContext->getTargetAPI() < SLANG_ICS_TARGET_API) {
2042 PaddingPrefix = "#padding_";
2043 } else {
2044 PaddingPrefix = "#rs_padding_";
2045 }
Shih-wei Liao462aefd2010-06-04 15:32:04 -07002046
Shih-wei Liao9ef2f782010-10-01 12:31:37 -07002047 if (!GetClassNameFromFileName(OutputBCFileName, ResourceId))
2048 return false;
Shih-wei Liao462aefd2010-06-04 15:32:04 -07002049
Shih-wei Liao9ef2f782010-10-01 12:31:37 -07002050 if (ResourceId.empty())
2051 ResourceId = "<Resource ID>";
Shih-wei Liao462aefd2010-06-04 15:32:04 -07002052
Shih-wei Liaob81c6a42010-10-10 14:15:00 -07002053 if (OutputPackageName.empty() || OutputPackageName == "-")
2054 C = new Context(OutputPathBase, InputFileName, "<Package Name>",
Stephen Hines0a813a32012-08-03 16:52:40 -07002055 RSPackageName, ResourceId, PaddingPrefix, true);
Shih-wei Liao9ef2f782010-10-01 12:31:37 -07002056 else
Shih-wei Liaob81c6a42010-10-10 14:15:00 -07002057 C = new Context(OutputPathBase, InputFileName, OutputPackageName,
Stephen Hines0a813a32012-08-03 16:52:40 -07002058 RSPackageName, ResourceId, PaddingPrefix, false);
Shih-wei Liao462aefd2010-06-04 15:32:04 -07002059
Shih-wei Liao9ef2f782010-10-01 12:31:37 -07002060 if (C != NULL) {
2061 std::string ErrorMsg, ScriptClassName;
2062 // class ScriptC_<ScriptName>
2063 if (!GetClassNameFromFileName(InputFileName, ScriptClassName))
2064 return false;
Shih-wei Liao462aefd2010-06-04 15:32:04 -07002065
Shih-wei Liao9ef2f782010-10-01 12:31:37 -07002066 if (ScriptClassName.empty())
2067 ScriptClassName = "<Input Script Name>";
Shih-wei Liao462aefd2010-06-04 15:32:04 -07002068
Shih-wei Liao9ef2f782010-10-01 12:31:37 -07002069 ScriptClassName.insert(0, RS_SCRIPT_CLASS_NAME_PREFIX);
Shih-wei Liao462aefd2010-06-04 15:32:04 -07002070
Shih-wei Liao9ef2f782010-10-01 12:31:37 -07002071 if (mRSContext->getLicenseNote() != NULL) {
2072 C->setLicenseNote(*(mRSContext->getLicenseNote()));
Shih-wei Liao462aefd2010-06-04 15:32:04 -07002073 }
2074
Shih-wei Liao9ef2f782010-10-01 12:31:37 -07002075 if (!genScriptClass(*C, ScriptClassName, ErrorMsg)) {
2076 std::cerr << "Failed to generate class " << ScriptClassName << " ("
2077 << ErrorMsg << ")" << std::endl;
2078 return false;
2079 }
2080
Stephen Hines4cc67fc2011-01-31 16:48:57 -08002081 mGeneratedFileNames->push_back(ScriptClassName);
2082
Shih-wei Liao9ef2f782010-10-01 12:31:37 -07002083 // class ScriptField_<TypeName>
2084 for (RSContext::const_export_type_iterator TI =
2085 mRSContext->export_types_begin(),
2086 TE = mRSContext->export_types_end();
2087 TI != TE;
2088 TI++) {
2089 const RSExportType *ET = TI->getValue();
2090
2091 if (ET->getClass() == RSExportType::ExportClassRecord) {
2092 const RSExportRecordType *ERT =
2093 static_cast<const RSExportRecordType*>(ET);
2094
2095 if (!ERT->isArtificial() && !genTypeClass(*C, ERT, ErrorMsg)) {
2096 std::cerr << "Failed to generate type class for struct '"
2097 << ERT->getName() << "' (" << ErrorMsg << ")" << std::endl;
2098 return false;
2099 }
2100 }
2101 }
2102 }
2103
2104 return true;
Shih-wei Liao462aefd2010-06-04 15:32:04 -07002105}
2106
Shih-wei Liao9ef2f782010-10-01 12:31:37 -07002107/************************** RSReflection::Context **************************/
zonr6315f762010-10-05 15:35:14 +08002108const char *const RSReflection::Context::ApacheLicenseNote =
Shih-wei Liao462aefd2010-06-04 15:32:04 -07002109 "/*\n"
Stephen Hines9d234b62013-03-26 11:20:52 -07002110 " * Copyright (C) 2011-2013 The Android Open Source Project\n"
Shih-wei Liao9ef2f782010-10-01 12:31:37 -07002111 " *\n"
2112 " * Licensed under the Apache License, Version 2.0 (the \"License\");\n"
2113 " * you may not use this file except in compliance with the License.\n"
2114 " * You may obtain a copy of the License at\n"
2115 " *\n"
2116 " * http://www.apache.org/licenses/LICENSE-2.0\n"
2117 " *\n"
2118 " * Unless required by applicable law or agreed to in writing, software\n"
2119 " * distributed under the License is distributed on an \"AS IS\" BASIS,\n"
2120 " * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or "
2121 "implied.\n"
2122 " * See the License for the specific language governing permissions and\n"
2123 " * limitations under the License.\n"
2124 " */\n"
2125 "\n";
Shih-wei Liao462aefd2010-06-04 15:32:04 -07002126
Zonr Chang8c6d9b22010-10-07 18:01:19 +08002127bool RSReflection::Context::openClassFile(const std::string &ClassName,
2128 std::string &ErrorMsg) {
2129 if (!mUseStdout) {
2130 mOF.clear();
2131 std::string Path =
Shih-wei Liaob81c6a42010-10-10 14:15:00 -07002132 RSSlangReflectUtils::ComputePackagedPath(mOutputPathBase.c_str(),
Zonr Chang8c6d9b22010-10-07 18:01:19 +08002133 mPackageName.c_str());
2134
2135 if (!SlangUtils::CreateDirectoryWithParents(Path, &ErrorMsg))
2136 return false;
2137
Raphael8d5a2f62011-02-08 00:15:05 -08002138 std::string ClassFile = Path + OS_PATH_SEPARATOR_STR + ClassName + ".java";
Zonr Chang8c6d9b22010-10-07 18:01:19 +08002139
2140 mOF.open(ClassFile.c_str());
2141 if (!mOF.good()) {
2142 ErrorMsg = "failed to open file '" + ClassFile + "' for write";
2143 return false;
2144 }
2145 }
2146 return true;
2147}
2148
Shih-wei Liao9ef2f782010-10-01 12:31:37 -07002149const char *RSReflection::Context::AccessModifierStr(AccessModifier AM) {
2150 switch (AM) {
2151 case AM_Public: return "public"; break;
2152 case AM_Protected: return "protected"; break;
2153 case AM_Private: return "private"; break;
Alex Sakhartchouk38eca1a2011-08-25 10:47:52 -07002154 case AM_PublicSynchronized: return "public synchronized"; break;
Shih-wei Liao9ef2f782010-10-01 12:31:37 -07002155 default: return ""; break;
2156 }
Shih-wei Liao462aefd2010-06-04 15:32:04 -07002157}
2158
Shih-wei Liao9ef2f782010-10-01 12:31:37 -07002159bool RSReflection::Context::startClass(AccessModifier AM,
2160 bool IsStatic,
2161 const std::string &ClassName,
2162 const char *SuperClassName,
2163 std::string &ErrorMsg) {
2164 if (mVerbose)
2165 std::cout << "Generating " << ClassName << ".java ..." << std::endl;
Shih-wei Liao462aefd2010-06-04 15:32:04 -07002166
Zonr Chang8c6d9b22010-10-07 18:01:19 +08002167 // Open file for class
2168 if (!openClassFile(ClassName, ErrorMsg))
2169 return false;
2170
Shih-wei Liao9ef2f782010-10-01 12:31:37 -07002171 // License
2172 out() << mLicenseNote;
Shih-wei Liao462aefd2010-06-04 15:32:04 -07002173
Shih-wei Liao9ef2f782010-10-01 12:31:37 -07002174 // Notice of generated file
2175 out() << "/*" << std::endl;
2176 out() << " * This file is auto-generated. DO NOT MODIFY!" << std::endl;
Stephen Hines9d234b62013-03-26 11:20:52 -07002177 out() << " * The source Renderscript file: "
2178 << SanitizeString(mInputRSFile) << std::endl;
Shih-wei Liao9ef2f782010-10-01 12:31:37 -07002179 out() << " */" << std::endl;
Ying Wang4e348442010-08-18 10:29:12 -07002180
Shih-wei Liao9ef2f782010-10-01 12:31:37 -07002181 // Package
2182 if (!mPackageName.empty())
2183 out() << "package " << mPackageName << ";" << std::endl;
2184 out() << std::endl;
Shih-wei Liao462aefd2010-06-04 15:32:04 -07002185
Shih-wei Liao9ef2f782010-10-01 12:31:37 -07002186 // Imports
Stephen Hines0a813a32012-08-03 16:52:40 -07002187 out() << "import " << mRSPackageName << ".*;" << std::endl;
2188 out() << "import android.content.res.Resources;" << std::endl;
Shih-wei Liao9ef2f782010-10-01 12:31:37 -07002189 out() << std::endl;
Shih-wei Liao462aefd2010-06-04 15:32:04 -07002190
Shih-wei Liao9ef2f782010-10-01 12:31:37 -07002191 // All reflected classes should be annotated as hidden, so that they won't
2192 // be exposed in SDK.
2193 out() << "/**" << std::endl;
2194 out() << " * @hide" << std::endl;
2195 out() << " */" << std::endl;
Ying Wang4e348442010-08-18 10:29:12 -07002196
Shih-wei Liao9ef2f782010-10-01 12:31:37 -07002197 out() << AccessModifierStr(AM) << ((IsStatic) ? " static" : "") << " class "
2198 << ClassName;
2199 if (SuperClassName != NULL)
2200 out() << " extends " << SuperClassName;
Shih-wei Liao462aefd2010-06-04 15:32:04 -07002201
Shih-wei Liao9ef2f782010-10-01 12:31:37 -07002202 startBlock();
Shih-wei Liao462aefd2010-06-04 15:32:04 -07002203
Shih-wei Liao9ef2f782010-10-01 12:31:37 -07002204 mClassName = ClassName;
Shih-wei Liao462aefd2010-06-04 15:32:04 -07002205
Shih-wei Liao9ef2f782010-10-01 12:31:37 -07002206 return true;
Shih-wei Liao462aefd2010-06-04 15:32:04 -07002207}
2208
2209void RSReflection::Context::endClass() {
Shih-wei Liao9ef2f782010-10-01 12:31:37 -07002210 endBlock();
2211 if (!mUseStdout)
2212 mOF.close();
2213 clear();
2214 return;
Shih-wei Liao462aefd2010-06-04 15:32:04 -07002215}
2216
2217void RSReflection::Context::startBlock(bool ShouldIndent) {
Shih-wei Liao9ef2f782010-10-01 12:31:37 -07002218 if (ShouldIndent)
2219 indent() << "{" << std::endl;
2220 else
2221 out() << " {" << std::endl;
2222 incIndentLevel();
2223 return;
Shih-wei Liao462aefd2010-06-04 15:32:04 -07002224}
2225
2226void RSReflection::Context::endBlock() {
Shih-wei Liao9ef2f782010-10-01 12:31:37 -07002227 decIndentLevel();
2228 indent() << "}" << std::endl << std::endl;
2229 return;
Shih-wei Liao462aefd2010-06-04 15:32:04 -07002230}
2231
Shih-wei Liao9ef2f782010-10-01 12:31:37 -07002232void RSReflection::Context::startTypeClass(const std::string &ClassName) {
2233 indent() << "public static class " << ClassName;
2234 startBlock();
2235 return;
Shih-wei Liao462aefd2010-06-04 15:32:04 -07002236}
2237
2238void RSReflection::Context::endTypeClass() {
Shih-wei Liao9ef2f782010-10-01 12:31:37 -07002239 endBlock();
2240 return;
Shih-wei Liao462aefd2010-06-04 15:32:04 -07002241}
2242
Shih-wei Liao2dd42ff2010-06-15 00:34:38 -07002243void RSReflection::Context::startFunction(AccessModifier AM,
2244 bool IsStatic,
Shih-wei Liao9ef2f782010-10-01 12:31:37 -07002245 const char *ReturnType,
2246 const std::string &FunctionName,
2247 int Argc, ...) {
2248 ArgTy Args;
2249 va_list vl;
2250 va_start(vl, Argc);
2251
zonr6315f762010-10-05 15:35:14 +08002252 for (int i = 0; i < Argc; i++) {
Shih-wei Liao9ef2f782010-10-01 12:31:37 -07002253 const char *ArgType = va_arg(vl, const char*);
2254 const char *ArgName = va_arg(vl, const char*);
2255
zonr6315f762010-10-05 15:35:14 +08002256 Args.push_back(std::make_pair(ArgType, ArgName));
Shih-wei Liao9ef2f782010-10-01 12:31:37 -07002257 }
2258 va_end(vl);
2259
2260 startFunction(AM, IsStatic, ReturnType, FunctionName, Args);
2261
2262 return;
2263}
2264
2265void RSReflection::Context::startFunction(AccessModifier AM,
2266 bool IsStatic,
2267 const char *ReturnType,
2268 const std::string &FunctionName,
zonr6315f762010-10-05 15:35:14 +08002269 const ArgTy &Args) {
Shih-wei Liao9ef2f782010-10-01 12:31:37 -07002270 indent() << AccessModifierStr(AM) << ((IsStatic) ? " static " : " ")
2271 << ((ReturnType) ? ReturnType : "") << " " << FunctionName << "(";
Shih-wei Liao462aefd2010-06-04 15:32:04 -07002272
Shih-wei Liao9ef2f782010-10-01 12:31:37 -07002273 bool FirstArg = true;
2274 for (ArgTy::const_iterator I = Args.begin(), E = Args.end();
2275 I != E;
2276 I++) {
2277 if (!FirstArg)
2278 out() << ", ";
2279 else
2280 FirstArg = false;
Shih-wei Liao462aefd2010-06-04 15:32:04 -07002281
Shih-wei Liao9ef2f782010-10-01 12:31:37 -07002282 out() << I->first << " " << I->second;
2283 }
Shih-wei Liao462aefd2010-06-04 15:32:04 -07002284
Shih-wei Liao9ef2f782010-10-01 12:31:37 -07002285 out() << ")";
2286 startBlock();
Shih-wei Liao462aefd2010-06-04 15:32:04 -07002287
Shih-wei Liao9ef2f782010-10-01 12:31:37 -07002288 return;
Shih-wei Liao462aefd2010-06-04 15:32:04 -07002289}
2290
2291void RSReflection::Context::endFunction() {
Shih-wei Liao9ef2f782010-10-01 12:31:37 -07002292 endBlock();
2293 return;
Shih-wei Liao462aefd2010-06-04 15:32:04 -07002294}
Stephen Hinese639eb52010-11-08 19:27:20 -08002295
Stephen Hines1f6c3312012-07-03 17:23:33 -07002296bool RSReflection::Context::addTypeNameForElement(
2297 const std::string &TypeName) {
2298 if (mTypesToCheck.find(TypeName) == mTypesToCheck.end()) {
2299 mTypesToCheck.insert(TypeName);
2300 return true;
2301 } else {
2302 return false;
2303 }
2304}
2305
2306bool RSReflection::Context::addTypeNameForFieldPacker(
2307 const std::string &TypeName) {
2308 if (mFieldPackerTypes.find(TypeName) == mFieldPackerTypes.end()) {
2309 mFieldPackerTypes.insert(TypeName);
2310 return true;
2311 } else {
2312 return false;
2313 }
2314}
2315
Stephen Hinese639eb52010-11-08 19:27:20 -08002316} // namespace slang