blob: e667a946267f05f39d0915cfa4a02791980c374d [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
Shih-wei Liao9ef2f782010-10-01 12:31:37 -070046#define RS_TYPE_CLASS_SUPER_CLASS_NAME "android.renderscript.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
266
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700267/********************** Methods to generate script class **********************/
268bool RSReflection::genScriptClass(Context &C,
269 const std::string &ClassName,
270 std::string &ErrorMsg) {
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700271 if (!C.startClass(Context::AM_Public,
272 false,
273 ClassName,
274 RS_SCRIPT_CLASS_SUPER_CLASS_NAME,
275 ErrorMsg))
276 return false;
277
278 genScriptClassConstructor(C);
279
280 // Reflect export variable
281 for (RSContext::const_export_var_iterator I = mRSContext->export_vars_begin(),
282 E = mRSContext->export_vars_end();
283 I != E;
284 I++)
285 genExportVariable(C, *I);
286
Stephen Hines4a4bf922011-08-18 17:20:33 -0700287 // Reflect export for each functions (only available on ICS+)
Stephen Hines4cc499d2011-08-24 19:06:17 -0700288 if (mRSContext->getTargetAPI() >= SLANG_ICS_TARGET_API) {
Stephen Hines4a4bf922011-08-18 17:20:33 -0700289 for (RSContext::const_export_foreach_iterator
290 I = mRSContext->export_foreach_begin(),
291 E = mRSContext->export_foreach_end();
292 I != E; I++)
293 genExportForEach(C, *I);
294 }
Stephen Hines593a8942011-05-10 15:29:50 -0700295
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700296 // Reflect export function
297 for (RSContext::const_export_func_iterator
298 I = mRSContext->export_funcs_begin(),
299 E = mRSContext->export_funcs_end();
300 I != E; I++)
301 genExportFunction(C, *I);
302
303 C.endClass();
304
305 return true;
306}
307
308void RSReflection::genScriptClassConstructor(Context &C) {
Stephen Hinesd2936932012-09-12 20:44:32 -0700309 // Provide a simple way to reference this object.
310 C.indent() << "private static final String " RS_RESOURCE_NAME " = \""
311 << C.getResourceId()
312 << "\";" << std::endl;
313
314 // Generate a simple constructor with only a single parameter (the rest
315 // can be inferred from information we already have).
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700316 C.indent() << "// Constructor" << std::endl;
317 C.startFunction(Context::AM_Public,
318 false,
319 NULL,
320 C.getClassName(),
Stephen Hinesd2936932012-09-12 20:44:32 -0700321 1,
322 "RenderScript", "rs");
323 // Call alternate constructor with required parameters.
324 // Look up the proper raw bitcode resource id via the context.
325 C.indent() << "this(rs," << std::endl;
326 C.indent() << " rs.getApplicationContext().getResources()," << std::endl;
327 C.indent() << " rs.getApplicationContext().getResources()."
328 "getIdentifier(" << std::endl;
329 C.indent() << " " RS_RESOURCE_NAME ", \"raw\"," << std::endl;
330 C.indent() << " rs.getApplicationContext().getPackageName()));"
331 << std::endl;
332 C.endFunction();
333
334 // Alternate constructor (legacy) with 3 original parameters.
335 C.startFunction(Context::AM_Public,
336 false,
337 NULL,
338 C.getClassName(),
Jason Samsb6902e22010-11-03 17:04:06 -0700339 3,
Zonr Changd42a4292010-10-17 02:38:43 +0800340 "RenderScript", "rs",
341 "Resources", "resources",
Jason Samsb6902e22010-11-03 17:04:06 -0700342 "int", "id");
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700343 // Call constructor of super class
Jason Samsb6902e22010-11-03 17:04:06 -0700344 C.indent() << "super(rs, resources, id);" << std::endl;
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700345
346 // If an exported variable has initial value, reflect it
347
348 for (RSContext::const_export_var_iterator I = mRSContext->export_vars_begin(),
349 E = mRSContext->export_vars_end();
350 I != E;
351 I++) {
352 const RSExportVar *EV = *I;
Stephen Hinesd369cda2012-02-13 12:00:03 -0800353 if (!EV->getInit().isUninit()) {
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700354 genInitExportVariable(C, EV->getType(), EV->getName(), EV->getInit());
Stephen Hinesd369cda2012-02-13 12:00:03 -0800355 } else if (EV->getArraySize()) {
356 // Always create an initial zero-init array object.
357 C.indent() << RS_EXPORT_VAR_PREFIX << EV->getName() << " = new "
358 << GetTypeName(EV->getType(), false) << "["
359 << EV->getArraySize() << "];" << std::endl;
360 size_t NumInits = EV->getNumInits();
361 const RSExportConstantArrayType *ECAT =
362 static_cast<const RSExportConstantArrayType*>(EV->getType());
363 const RSExportType *ET = ECAT->getElementType();
364 for (size_t i = 0; i < NumInits; i++) {
365 std::stringstream Name;
366 Name << EV->getName() << "[" << i << "]";
367 genInitExportVariable(C, ET, Name.str(), EV->getInitArray(i));
368 }
369 }
Stephen Hinesa6b54142012-04-09 18:25:08 -0700370 if (mRSContext->getTargetAPI() >= SLANG_JB_TARGET_API) {
371 genTypeInstance(C, EV->getType());
372 }
Stephen Hines1f6c3312012-07-03 17:23:33 -0700373 genFieldPackerInstance(C, EV->getType());
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700374 }
375
Stephen Hines48b72bf2011-06-10 15:37:27 -0700376 for (RSContext::const_export_foreach_iterator
377 I = mRSContext->export_foreach_begin(),
378 E = mRSContext->export_foreach_end();
379 I != E;
380 I++) {
381 const RSExportForEach *EF = *I;
382
383 const RSExportType *IET = EF->getInType();
384 if (IET) {
Stephen Hinesa6b54142012-04-09 18:25:08 -0700385 genTypeInstanceFromPointer(C, IET);
Stephen Hines48b72bf2011-06-10 15:37:27 -0700386 }
387 const RSExportType *OET = EF->getOutType();
388 if (OET) {
Stephen Hinesa6b54142012-04-09 18:25:08 -0700389 genTypeInstanceFromPointer(C, OET);
Stephen Hines48b72bf2011-06-10 15:37:27 -0700390 }
391 }
392
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700393 C.endFunction();
Jason Samsb6902e22010-11-03 17:04:06 -0700394
Stephen Hines48b72bf2011-06-10 15:37:27 -0700395 for (std::set<std::string>::iterator I = C.mTypesToCheck.begin(),
396 E = C.mTypesToCheck.end();
397 I != E;
398 I++) {
Stephen Hinesa6b54142012-04-09 18:25:08 -0700399 C.indent() << "private Element " RS_ELEM_PREFIX << *I << ";" << std::endl;
Stephen Hines48b72bf2011-06-10 15:37:27 -0700400 }
401
Stephen Hines1f6c3312012-07-03 17:23:33 -0700402 for (std::set<std::string>::iterator I = C.mFieldPackerTypes.begin(),
403 E = C.mFieldPackerTypes.end();
404 I != E;
405 I++) {
406 C.indent() << "private FieldPacker " RS_FP_PREFIX << *I << ";" << std::endl;
407 }
408
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700409 return;
410}
411
412void RSReflection::genInitBoolExportVariable(Context &C,
413 const std::string &VarName,
414 const clang::APValue &Val) {
Stephen Hines6e6578a2011-02-07 18:05:48 -0800415 slangAssert(!Val.isUninit() && "Not a valid initializer");
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700416
417 C.indent() << RS_EXPORT_VAR_PREFIX << VarName << " = ";
Stephen Hines6e6578a2011-02-07 18:05:48 -0800418 slangAssert((Val.getKind() == clang::APValue::Int) &&
419 "Bool type has wrong initial APValue");
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700420
Zonr Chang92b344a2010-10-05 20:39:03 +0800421 C.out() << ((Val.getInt().getSExtValue() == 0) ? "false" : "true")
422 << ";" << std::endl;
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700423
424 return;
425}
426
Stephen Hines5d671782012-01-31 19:32:04 -0800427void RSReflection::genInitPrimitiveExportVariable(
428 Context &C,
429 const std::string &VarName,
430 const clang::APValue &Val) {
431 slangAssert(!Val.isUninit() && "Not a valid initializer");
432
433 C.indent() << RS_EXPORT_VAR_PREFIX << VarName << " = ";
Jason Sams192392f2012-03-13 16:22:12 -0700434 C.out() << RSReflectionBase::genInitValue(Val);
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700435 C.out() << ";" << std::endl;
436
437 return;
Shih-wei Liao462aefd2010-06-04 15:32:04 -0700438}
439
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700440void RSReflection::genInitExportVariable(Context &C,
441 const RSExportType *ET,
442 const std::string &VarName,
443 const clang::APValue &Val) {
Stephen Hines6e6578a2011-02-07 18:05:48 -0800444 slangAssert(!Val.isUninit() && "Not a valid initializer");
Shih-wei Liao462aefd2010-06-04 15:32:04 -0700445
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700446 switch (ET->getClass()) {
Zonr Chang2e1dba62010-10-05 22:20:11 +0800447 case RSExportType::ExportClassPrimitive: {
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700448 const RSExportPrimitiveType *EPT =
449 static_cast<const RSExportPrimitiveType*>(ET);
Shih-wei Liao91a37832010-10-03 19:11:51 -0700450 if (EPT->getType() == RSExportPrimitiveType::DataTypeBoolean) {
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700451 genInitBoolExportVariable(C, VarName, Val);
452 } else {
453 genInitPrimitiveExportVariable(C, VarName, Val);
454 }
455 break;
Shih-wei Liao462aefd2010-06-04 15:32:04 -0700456 }
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700457 case RSExportType::ExportClassPointer: {
458 if (!Val.isInt() || Val.getInt().getSExtValue() != 0)
459 std::cout << "Initializer which is non-NULL to pointer type variable "
460 "will be ignored" << std::endl;
461 break;
Shih-wei Liao462aefd2010-06-04 15:32:04 -0700462 }
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700463 case RSExportType::ExportClassVector: {
464 const RSExportVectorType *EVT =
465 static_cast<const RSExportVectorType*>(ET);
466 switch (Val.getKind()) {
467 case clang::APValue::Int:
468 case clang::APValue::Float: {
Zonr Chang2e1dba62010-10-05 22:20:11 +0800469 for (unsigned i = 0; i < EVT->getNumElement(); i++) {
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700470 std::string Name = VarName + "." + GetVectorAccessor(i);
471 genInitPrimitiveExportVariable(C, Name, Val);
Shih-wei Liao1ebc0ca2010-09-14 10:57:21 -0700472 }
473 break;
474 }
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700475 case clang::APValue::Vector: {
Stephen Hines47aca4e2012-03-08 20:07:28 -0800476 std::stringstream VecName;
477 VecName << EVT->getRSReflectionType(EVT)->rs_java_vector_prefix
478 << EVT->getNumElement();
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700479 C.indent() << RS_EXPORT_VAR_PREFIX << VarName << " = new "
Stephen Hines47aca4e2012-03-08 20:07:28 -0800480 << VecName.str() << "();" << std::endl;
Shih-wei Liao462aefd2010-06-04 15:32:04 -0700481
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700482 unsigned NumElements =
483 std::min(static_cast<unsigned>(EVT->getNumElement()),
484 Val.getVectorLength());
485 for (unsigned i = 0; i < NumElements; i++) {
486 const clang::APValue &ElementVal = Val.getVectorElt(i);
487 std::string Name = VarName + "." + GetVectorAccessor(i);
488 genInitPrimitiveExportVariable(C, Name, ElementVal);
489 }
490 break;
Shih-wei Liao462aefd2010-06-04 15:32:04 -0700491 }
Logan Chienfa6ef562011-11-25 13:50:02 +0800492 case clang::APValue::MemberPointer:
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700493 case clang::APValue::Uninitialized:
494 case clang::APValue::ComplexInt:
495 case clang::APValue::ComplexFloat:
Stephen Hines4c622e02011-11-10 18:57:34 -0800496 case clang::APValue::LValue:
497 case clang::APValue::Array:
498 case clang::APValue::Struct:
Stephen Hines0444de02012-03-02 23:19:06 -0800499 case clang::APValue::Union:
500 case clang::APValue::AddrLabelDiff: {
Stephen Hines6e6578a2011-02-07 18:05:48 -0800501 slangAssert(false && "Unexpected type of value of initializer.");
Shih-wei Liao462aefd2010-06-04 15:32:04 -0700502 }
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700503 }
504 break;
Shih-wei Liao462aefd2010-06-04 15:32:04 -0700505 }
Zonr Chang92b344a2010-10-05 20:39:03 +0800506 // TODO(zonr): Resolving initializer of a record (and matrix) type variable
507 // is complex. It cannot obtain by just simply evaluating the initializer
508 // expression.
509 case RSExportType::ExportClassMatrix:
Zonr Chang2e1dba62010-10-05 22:20:11 +0800510 case RSExportType::ExportClassConstantArray:
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700511 case RSExportType::ExportClassRecord: {
512#if 0
513 unsigned InitIndex = 0;
zonr6315f762010-10-05 15:35:14 +0800514 const RSExportRecordType *ERT =
515 static_cast<const RSExportRecordType*>(ET);
Shih-wei Liao462aefd2010-06-04 15:32:04 -0700516
Stephen Hines6e6578a2011-02-07 18:05:48 -0800517 slangAssert((Val.getKind() == clang::APValue::Vector) &&
518 "Unexpected type of initializer for record type variable");
Shih-wei Liao9c631ff2010-09-17 11:57:29 -0700519
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700520 C.indent() << RS_EXPORT_VAR_PREFIX << VarName
Stephen Hinesa6b54142012-04-09 18:25:08 -0700521 << " = new " << ERT->getElementName()
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700522 << "."RS_TYPE_ITEM_CLASS_NAME"();" << std::endl;
Shih-wei Liao9c631ff2010-09-17 11:57:29 -0700523
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700524 for (RSExportRecordType::const_field_iterator I = ERT->fields_begin(),
525 E = ERT->fields_end();
526 I != E;
527 I++) {
528 const RSExportRecordType::Field *F = *I;
529 std::string FieldName = VarName + "." + F->getName();
Shih-wei Liao462aefd2010-06-04 15:32:04 -0700530
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700531 if (InitIndex > Val.getVectorLength())
532 break;
533
534 genInitPrimitiveExportVariable(C,
535 FieldName,
536 Val.getVectorElt(InitIndex++));
537 }
538#endif
Stephen Hines6e6578a2011-02-07 18:05:48 -0800539 slangAssert(false && "Unsupported initializer for record/matrix/constant "
540 "array type variable currently");
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700541 break;
Shih-wei Liao6de89272010-07-15 15:26:20 -0700542 }
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700543 default: {
Stephen Hines6e6578a2011-02-07 18:05:48 -0800544 slangAssert(false && "Unknown class of type");
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700545 }
546 }
547 return;
Shih-wei Liao462aefd2010-06-04 15:32:04 -0700548}
549
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700550void RSReflection::genExportVariable(Context &C, const RSExportVar *EV) {
551 const RSExportType *ET = EV->getType();
Shih-wei Liao462aefd2010-06-04 15:32:04 -0700552
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700553 C.indent() << "private final static int "RS_EXPORT_VAR_INDEX_PREFIX
554 << EV->getName() << " = " << C.getNextExportVarSlot() << ";"
555 << std::endl;
Shih-wei Liao462aefd2010-06-04 15:32:04 -0700556
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700557 switch (ET->getClass()) {
Zonr Chang2e1dba62010-10-05 22:20:11 +0800558 case RSExportType::ExportClassPrimitive: {
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700559 genPrimitiveTypeExportVariable(C, EV);
560 break;
Shih-wei Liaof8149d92010-08-22 05:32:02 -0700561 }
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700562 case RSExportType::ExportClassPointer: {
563 genPointerTypeExportVariable(C, EV);
564 break;
Shih-wei Liao462aefd2010-06-04 15:32:04 -0700565 }
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700566 case RSExportType::ExportClassVector: {
567 genVectorTypeExportVariable(C, EV);
568 break;
569 }
Zonr Chang92b344a2010-10-05 20:39:03 +0800570 case RSExportType::ExportClassMatrix: {
571 genMatrixTypeExportVariable(C, EV);
572 break;
573 }
Zonr Chang2e1dba62010-10-05 22:20:11 +0800574 case RSExportType::ExportClassConstantArray: {
575 genConstantArrayTypeExportVariable(C, EV);
576 break;
577 }
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700578 case RSExportType::ExportClassRecord: {
579 genRecordTypeExportVariable(C, EV);
580 break;
581 }
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700582 default: {
Stephen Hines6e6578a2011-02-07 18:05:48 -0800583 slangAssert(false && "Unknown class of type");
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700584 }
585 }
Shih-wei Liao462aefd2010-06-04 15:32:04 -0700586
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700587 return;
Shih-wei Liao462aefd2010-06-04 15:32:04 -0700588}
589
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700590void RSReflection::genExportFunction(Context &C, const RSExportFunc *EF) {
591 C.indent() << "private final static int "RS_EXPORT_FUNC_INDEX_PREFIX
592 << EF->getName() << " = " << C.getNextExportFuncSlot() << ";"
593 << std::endl;
Shih-wei Liao462aefd2010-06-04 15:32:04 -0700594
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700595 // invoke_*()
596 Context::ArgTy Args;
597
Zonr Chang0da0a7d2010-10-05 21:26:37 +0800598 if (EF->hasParam()) {
599 for (RSExportFunc::const_param_iterator I = EF->params_begin(),
600 E = EF->params_end();
601 I != E;
602 I++) {
603 Args.push_back(std::make_pair(GetTypeName((*I)->getType()),
604 (*I)->getName()));
605 }
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700606 }
607
608 C.startFunction(Context::AM_Public,
609 false,
610 "void",
Shih-wei Liao3fa286b2011-02-10 11:04:44 -0800611 "invoke_" + EF->getName(/*Mangle=*/ false),
612 // We are using un-mangled name since Java
613 // supports method overloading.
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700614 Args);
615
616 if (!EF->hasParam()) {
617 C.indent() << "invoke("RS_EXPORT_FUNC_INDEX_PREFIX << EF->getName() << ");"
618 << std::endl;
619 } else {
620 const RSExportRecordType *ERT = EF->getParamPacketType();
621 std::string FieldPackerName = EF->getName() + "_fp";
622
623 if (genCreateFieldPacker(C, ERT, FieldPackerName.c_str()))
624 genPackVarOfType(C, ERT, NULL, FieldPackerName.c_str());
625
626 C.indent() << "invoke("RS_EXPORT_FUNC_INDEX_PREFIX << EF->getName() << ", "
627 << FieldPackerName << ");" << std::endl;
628 }
629
630 C.endFunction();
631 return;
632}
633
Stephen Hines593a8942011-05-10 15:29:50 -0700634void RSReflection::genExportForEach(Context &C, const RSExportForEach *EF) {
Stephen Hinesc17e1982012-02-22 12:30:45 -0800635 if (EF->isDummyRoot()) {
636 // Skip reflection for dummy root() kernels. Note that we have to
637 // advance the next slot number for ForEach, however.
638 C.indent() << "//private final static int "RS_EXPORT_FOREACH_INDEX_PREFIX
639 << EF->getName() << " = " << C.getNextExportForEachSlot() << ";"
640 << std::endl;
641 return;
642 }
643
Stephen Hines593a8942011-05-10 15:29:50 -0700644 C.indent() << "private final static int "RS_EXPORT_FOREACH_INDEX_PREFIX
645 << EF->getName() << " = " << C.getNextExportForEachSlot() << ";"
646 << std::endl;
647
Stephen Hinesb5a89fb2011-05-17 14:48:02 -0700648 // forEach_*()
Stephen Hines593a8942011-05-10 15:29:50 -0700649 Context::ArgTy Args;
650
Stephen Hines9ca96e72012-09-13 16:57:06 -0700651 slangAssert(EF->getNumParameters() > 0 || EF->hasReturn());
Stephen Hines593a8942011-05-10 15:29:50 -0700652
Stephen Hinesb5a89fb2011-05-17 14:48:02 -0700653 if (EF->hasIn())
654 Args.push_back(std::make_pair("Allocation", "ain"));
Stephen Hines9ca96e72012-09-13 16:57:06 -0700655 if (EF->hasOut() || EF->hasReturn())
Stephen Hines593a8942011-05-10 15:29:50 -0700656 Args.push_back(std::make_pair("Allocation", "aout"));
Stephen Hinesb5a89fb2011-05-17 14:48:02 -0700657
658 const RSExportRecordType *ERT = EF->getParamPacketType();
659 if (ERT) {
660 for (RSExportForEach::const_param_iterator I = EF->params_begin(),
Stephen Hines593a8942011-05-10 15:29:50 -0700661 E = EF->params_end();
662 I != E;
663 I++) {
664 Args.push_back(std::make_pair(GetTypeName((*I)->getType()),
665 (*I)->getName()));
666 }
667 }
668
Tim Murrayb81a9932012-10-10 15:56:02 -0700669 const RSExportType *IET = EF->getInType();
670 const RSExportType *OET = EF->getOutType();
671
672 if (mRSContext->getTargetAPI() >= SLANG_JB_MR1_TARGET_API) {
673 int signature = 0;
674 C.startFunction(Context::AM_Public,
675 false,
676 "Script.KernelID",
677 "getKernelID_" + EF->getName(),
678 0);
679
680 if (IET)
681 signature |= 1;
682 if (OET)
683 signature |= 2;
684
685 //TODO: add element checking
686 C.indent() << "return createKernelID(" << RS_EXPORT_FOREACH_INDEX_PREFIX
687 << EF->getName() << ", " << signature << ", null, null);"
688 << std::endl;
689
690 C.endFunction();
691 }
692
Stephen Hines593a8942011-05-10 15:29:50 -0700693 C.startFunction(Context::AM_Public,
694 false,
695 "void",
696 "forEach_" + EF->getName(),
697 Args);
698
Stephen Hinesb5a89fb2011-05-17 14:48:02 -0700699 if (IET) {
700 genTypeCheck(C, IET, "ain");
701 }
Stephen Hinesb5a89fb2011-05-17 14:48:02 -0700702 if (OET) {
703 genTypeCheck(C, OET, "aout");
704 }
705
Stephen Hines9ca96e72012-09-13 16:57:06 -0700706 if (EF->hasIn() && (EF->hasOut() || EF->hasReturn())) {
Stephen Hinesb5a89fb2011-05-17 14:48:02 -0700707 C.indent() << "// Verify dimensions" << std::endl;
708 C.indent() << "Type tIn = ain.getType();" << std::endl;
709 C.indent() << "Type tOut = aout.getType();" << std::endl;
710 C.indent() << "if ((tIn.getCount() != tOut.getCount()) ||" << std::endl;
711 C.indent() << " (tIn.getX() != tOut.getX()) ||" << std::endl;
712 C.indent() << " (tIn.getY() != tOut.getY()) ||" << std::endl;
713 C.indent() << " (tIn.getZ() != tOut.getZ()) ||" << std::endl;
714 C.indent() << " (tIn.hasFaces() != tOut.hasFaces()) ||" << std::endl;
715 C.indent() << " (tIn.hasMipmaps() != tOut.hasMipmaps())) {" << std::endl;
Stephen Hines48b72bf2011-06-10 15:37:27 -0700716 C.indent() << " throw new RSRuntimeException(\"Dimension mismatch "
717 << "between input and output parameters!\");";
Stephen Hinesb5a89fb2011-05-17 14:48:02 -0700718 C.out() << std::endl;
719 C.indent() << "}" << std::endl;
720 }
721
722 std::string FieldPackerName = EF->getName() + "_fp";
723 if (ERT) {
724 if (genCreateFieldPacker(C, ERT, FieldPackerName.c_str())) {
Stephen Hines593a8942011-05-10 15:29:50 -0700725 genPackVarOfType(C, ERT, NULL, FieldPackerName.c_str());
Stephen Hinesb5a89fb2011-05-17 14:48:02 -0700726 }
Stephen Hines593a8942011-05-10 15:29:50 -0700727 }
Stephen Hinesb5a89fb2011-05-17 14:48:02 -0700728 C.indent() << "forEach("RS_EXPORT_FOREACH_INDEX_PREFIX << EF->getName();
Stephen Hines593a8942011-05-10 15:29:50 -0700729
Stephen Hinesb5a89fb2011-05-17 14:48:02 -0700730 if (EF->hasIn())
731 C.out() << ", ain";
732 else
733 C.out() << ", null";
734
Stephen Hines9ca96e72012-09-13 16:57:06 -0700735 if (EF->hasOut() || EF->hasReturn())
Stephen Hinesb5a89fb2011-05-17 14:48:02 -0700736 C.out() << ", aout";
737 else
738 C.out() << ", null";
739
740 if (EF->hasUsrData())
741 C.out() << ", " << FieldPackerName;
742 else
743 C.out() << ", null";
744
745 C.out() << ");" << std::endl;
Stephen Hines593a8942011-05-10 15:29:50 -0700746
747 C.endFunction();
748 return;
749}
750
Stephen Hinesa6b54142012-04-09 18:25:08 -0700751void RSReflection::genTypeInstanceFromPointer(Context &C,
752 const RSExportType *ET) {
Stephen Hines48b72bf2011-06-10 15:37:27 -0700753 if (ET->getClass() == RSExportType::ExportClassPointer) {
Stephen Hines9ca96e72012-09-13 16:57:06 -0700754 // For pointer parameters to original forEach kernels.
Stephen Hines48b72bf2011-06-10 15:37:27 -0700755 const RSExportPointerType *EPT =
756 static_cast<const RSExportPointerType*>(ET);
Stephen Hinesa6b54142012-04-09 18:25:08 -0700757 genTypeInstance(C, EPT->getPointeeType());
Stephen Hines9ca96e72012-09-13 16:57:06 -0700758 } else {
759 // For handling pass-by-value kernel parameters.
760 genTypeInstance(C, ET);
Stephen Hinesa6b54142012-04-09 18:25:08 -0700761 }
762}
Stephen Hines48b72bf2011-06-10 15:37:27 -0700763
Stephen Hinesa6b54142012-04-09 18:25:08 -0700764void RSReflection::genTypeInstance(Context &C,
765 const RSExportType *ET) {
766 switch (ET->getClass()) {
767 case RSExportType::ExportClassPrimitive:
768 case RSExportType::ExportClassVector:
769 case RSExportType::ExportClassConstantArray: {
770 std::string TypeName = ET->getElementName();
Stephen Hines1f6c3312012-07-03 17:23:33 -0700771 if (C.addTypeNameForElement(TypeName)) {
Stephen Hinesa6b54142012-04-09 18:25:08 -0700772 C.indent() << RS_ELEM_PREFIX << TypeName << " = Element." << TypeName
773 << "(rs);" << std::endl;
Stephen Hines48b72bf2011-06-10 15:37:27 -0700774 }
Stephen Hinesa6b54142012-04-09 18:25:08 -0700775 break;
Stephen Hines48b72bf2011-06-10 15:37:27 -0700776 }
Stephen Hinesa6b54142012-04-09 18:25:08 -0700777
778 case RSExportType::ExportClassRecord: {
779 std::string ClassName = ET->getElementName();
Stephen Hines1f6c3312012-07-03 17:23:33 -0700780 if (C.addTypeNameForElement(ClassName)) {
Stephen Hinesa6b54142012-04-09 18:25:08 -0700781 C.indent() << RS_ELEM_PREFIX << ClassName << " = " << ClassName <<
782 ".createElement(rs);" << std::endl;
Stephen Hinesa6b54142012-04-09 18:25:08 -0700783 }
784 break;
785 }
786
787 default:
788 break;
Stephen Hines48b72bf2011-06-10 15:37:27 -0700789 }
790}
791
Stephen Hines1f6c3312012-07-03 17:23:33 -0700792void RSReflection::genFieldPackerInstance(Context &C,
793 const RSExportType *ET) {
794 switch (ET->getClass()) {
795 case RSExportType::ExportClassPrimitive:
796 case RSExportType::ExportClassVector:
797 case RSExportType::ExportClassConstantArray:
798 case RSExportType::ExportClassRecord: {
799 std::string TypeName = ET->getElementName();
800 C.addTypeNameForFieldPacker(TypeName);
801 break;
802 }
803
804 default:
805 break;
806 }
807}
808
Stephen Hinesb5a89fb2011-05-17 14:48:02 -0700809void RSReflection::genTypeCheck(Context &C,
810 const RSExportType *ET,
811 const char *VarName) {
812 C.indent() << "// check " << VarName << std::endl;
Stephen Hines48b72bf2011-06-10 15:37:27 -0700813
814 if (ET->getClass() == RSExportType::ExportClassPointer) {
815 const RSExportPointerType *EPT =
816 static_cast<const RSExportPointerType*>(ET);
817 ET = EPT->getPointeeType();
818 }
819
820 std::string TypeName;
821
822 switch (ET->getClass()) {
Stephen Hinesa6b54142012-04-09 18:25:08 -0700823 case RSExportType::ExportClassPrimitive:
824 case RSExportType::ExportClassVector:
Stephen Hines48b72bf2011-06-10 15:37:27 -0700825 case RSExportType::ExportClassRecord: {
Stephen Hinesa6b54142012-04-09 18:25:08 -0700826 TypeName = ET->getElementName();
Stephen Hines48b72bf2011-06-10 15:37:27 -0700827 break;
828 }
829
830 default:
831 break;
832 }
833
834 if (!TypeName.empty()) {
835 C.indent() << "if (!" << VarName
Stephen Hinesa6b54142012-04-09 18:25:08 -0700836 << ".getType().getElement().isCompatible(" RS_ELEM_PREFIX
Stephen Hines48b72bf2011-06-10 15:37:27 -0700837 << TypeName << ")) {" << std::endl;
838 C.indent() << " throw new RSRuntimeException(\"Type mismatch with "
839 << TypeName << "!\");" << std::endl;
840 C.indent() << "}" << std::endl;
841 }
842
Stephen Hinesb5a89fb2011-05-17 14:48:02 -0700843 return;
844}
845
846
Stephen Hinese639eb52010-11-08 19:27:20 -0800847void RSReflection::genPrimitiveTypeExportVariable(
848 Context &C,
849 const RSExportVar *EV) {
Stephen Hines6e6578a2011-02-07 18:05:48 -0800850 slangAssert((EV->getType()->getClass() == RSExportType::ExportClassPrimitive)
851 && "Variable should be type of primitive here");
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700852
853 const RSExportPrimitiveType *EPT =
854 static_cast<const RSExportPrimitiveType*>(EV->getType());
Stephen Hines0d26cef2012-05-01 19:23:01 -0700855 std::string TypeName = GetTypeName(EPT);
856 std::string VarName = EV->getName();
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700857
Stephen Hines0d26cef2012-05-01 19:23:01 -0700858 genPrivateExportVariable(C, TypeName, EV->getName());
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700859
Stephen Hines5d671782012-01-31 19:32:04 -0800860 if (EV->isConst()) {
861 C.indent() << "public final static " << TypeName
Stephen Hines0d26cef2012-05-01 19:23:01 -0700862 << " " RS_EXPORT_VAR_CONST_PREFIX << VarName << " = ";
Stephen Hines5d671782012-01-31 19:32:04 -0800863 const clang::APValue &Val = EV->getInit();
Jason Sams192392f2012-03-13 16:22:12 -0700864 C.out() << RSReflectionBase::genInitValue(Val, EPT->getType() ==
865 RSExportPrimitiveType::DataTypeBoolean) << ";" << std::endl;
Stephen Hines5d671782012-01-31 19:32:04 -0800866 } else {
867 // set_*()
Stephen Hines1f6c3312012-07-03 17:23:33 -0700868 // This must remain synchronized, since multiple Dalvik threads may
869 // be calling setters.
870 C.startFunction(Context::AM_PublicSynchronized,
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700871 false,
872 "void",
Stephen Hines0d26cef2012-05-01 19:23:01 -0700873 "set_" + VarName,
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700874 1,
Stephen Hines0d26cef2012-05-01 19:23:01 -0700875 TypeName.c_str(), "v");
Stephen Hinesbcae1fe2012-09-25 19:17:33 -0700876 if ((EPT->getSize() < 4) || EV->isUnsigned()) {
Stephen Hines1f6c3312012-07-03 17:23:33 -0700877 // We create/cache a per-type FieldPacker. This allows us to reuse the
878 // validation logic (for catching negative inputs from Dalvik, as well
879 // as inputs that are too large to be represented in the unsigned type).
Stephen Hinesbcae1fe2012-09-25 19:17:33 -0700880 // Sub-integer types are also handled specially here, so that we don't
881 // overwrite bytes accidentally.
Stephen Hines1f6c3312012-07-03 17:23:33 -0700882 std::string ElemName = EPT->getElementName();
883 std::string FPName;
884 FPName = RS_FP_PREFIX + ElemName;
885 C.indent() << "if (" << FPName << "!= null) {"
886 << std::endl;
887 C.incIndentLevel();
888 C.indent() << FPName << ".reset();" << std::endl;
889 C.decIndentLevel();
890 C.indent() << "} else {" << std::endl;
891 C.incIndentLevel();
892 C.indent() << FPName << " = new FieldPacker("
893 << EPT->getSize() << ");" << std::endl;
894 C.decIndentLevel();
895 C.indent() << "}" << std::endl;
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700896
Stephen Hines1f6c3312012-07-03 17:23:33 -0700897 genPackVarOfType(C, EPT, "v", FPName.c_str());
898 C.indent() << "setVar("RS_EXPORT_VAR_INDEX_PREFIX << VarName
899 << ", " << FPName << ");" << std::endl;
900 } else {
901 C.indent() << "setVar("RS_EXPORT_VAR_INDEX_PREFIX << VarName
902 << ", v);" << std::endl;
903 }
904
905 // Dalvik update comes last, since the input may be invalid (and hence
906 // throw an exception).
907 C.indent() << RS_EXPORT_VAR_PREFIX << VarName << " = v;" << std::endl;
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700908
Shih-wei Liao9e86e192010-06-18 20:42:28 -0700909 C.endFunction();
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700910 }
Shih-wei Liao9e86e192010-06-18 20:42:28 -0700911
Stephen Hines0d26cef2012-05-01 19:23:01 -0700912 genGetExportVariable(C, TypeName, VarName);
Stephen Hines28d60bc2012-10-15 15:54:16 -0700913 genGetFieldID(C, VarName);
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700914 return;
Shih-wei Liao462aefd2010-06-04 15:32:04 -0700915}
916
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700917void RSReflection::genPointerTypeExportVariable(Context &C,
918 const RSExportVar *EV) {
919 const RSExportType *ET = EV->getType();
920 const RSExportType *PointeeType;
Shih-wei Liao462aefd2010-06-04 15:32:04 -0700921
Stephen Hines6e6578a2011-02-07 18:05:48 -0800922 slangAssert((ET->getClass() == RSExportType::ExportClassPointer) &&
923 "Variable should be type of pointer here");
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700924
925 PointeeType = static_cast<const RSExportPointerType*>(ET)->getPointeeType();
Stephen Hines0d26cef2012-05-01 19:23:01 -0700926 std::string TypeName = GetTypeName(ET);
927 std::string VarName = EV->getName();
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700928
Stephen Hines0d26cef2012-05-01 19:23:01 -0700929 genPrivateExportVariable(C, TypeName, VarName);
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700930
Zonr Chang89273bd2010-10-14 20:57:38 +0800931 // bind_*()
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700932 C.startFunction(Context::AM_Public,
933 false,
934 "void",
Stephen Hines0d26cef2012-05-01 19:23:01 -0700935 "bind_" + VarName,
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700936 1,
Zonr Changd42a4292010-10-17 02:38:43 +0800937 TypeName.c_str(), "v");
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700938
Stephen Hines0d26cef2012-05-01 19:23:01 -0700939 C.indent() << RS_EXPORT_VAR_PREFIX << VarName << " = v;" << std::endl;
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700940 C.indent() << "if (v == null) bindAllocation(null, "RS_EXPORT_VAR_INDEX_PREFIX
Stephen Hines0d26cef2012-05-01 19:23:01 -0700941 << VarName << ");" << std::endl;
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700942
943 if (PointeeType->getClass() == RSExportType::ExportClassRecord)
944 C.indent() << "else bindAllocation(v.getAllocation(), "
Stephen Hines0d26cef2012-05-01 19:23:01 -0700945 RS_EXPORT_VAR_INDEX_PREFIX << VarName << ");"
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700946 << std::endl;
947 else
948 C.indent() << "else bindAllocation(v, "RS_EXPORT_VAR_INDEX_PREFIX
Stephen Hines0d26cef2012-05-01 19:23:01 -0700949 << VarName << ");" << std::endl;
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700950
951 C.endFunction();
952
Stephen Hines0d26cef2012-05-01 19:23:01 -0700953 genGetExportVariable(C, TypeName, VarName);
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700954 return;
955}
956
957void RSReflection::genVectorTypeExportVariable(Context &C,
958 const RSExportVar *EV) {
Stephen Hines6e6578a2011-02-07 18:05:48 -0800959 slangAssert((EV->getType()->getClass() == RSExportType::ExportClassVector) &&
960 "Variable should be type of vector here");
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700961
Stephen Hines0d26cef2012-05-01 19:23:01 -0700962 std::string TypeName = GetTypeName(EV->getType());
963 std::string VarName = EV->getName();
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700964
Stephen Hines0d26cef2012-05-01 19:23:01 -0700965 genPrivateExportVariable(C, TypeName, VarName);
966 genSetExportVariable(C, TypeName, EV);
967 genGetExportVariable(C, TypeName, VarName);
Stephen Hines28d60bc2012-10-15 15:54:16 -0700968 genGetFieldID(C, VarName);
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700969 return;
970}
971
Zonr Chang92b344a2010-10-05 20:39:03 +0800972void RSReflection::genMatrixTypeExportVariable(Context &C,
973 const RSExportVar *EV) {
Stephen Hines6e6578a2011-02-07 18:05:48 -0800974 slangAssert((EV->getType()->getClass() == RSExportType::ExportClassMatrix) &&
975 "Variable should be type of matrix here");
Zonr Chang92b344a2010-10-05 20:39:03 +0800976
Stephen Hines0d26cef2012-05-01 19:23:01 -0700977 const RSExportType *ET = EV->getType();
978 std::string TypeName = GetTypeName(ET);
979 std::string VarName = EV->getName();
Zonr Chang92b344a2010-10-05 20:39:03 +0800980
Stephen Hines0d26cef2012-05-01 19:23:01 -0700981 genPrivateExportVariable(C, TypeName, VarName);
Zonr Chang92b344a2010-10-05 20:39:03 +0800982
983 // set_*()
984 if (!EV->isConst()) {
Stephen Hines0d26cef2012-05-01 19:23:01 -0700985 const char *FieldPackerName = "fp";
Stephen Hines1f6c3312012-07-03 17:23:33 -0700986 C.startFunction(Context::AM_PublicSynchronized,
Zonr Chang92b344a2010-10-05 20:39:03 +0800987 false,
988 "void",
Stephen Hines0d26cef2012-05-01 19:23:01 -0700989 "set_" + VarName,
Zonr Chang92b344a2010-10-05 20:39:03 +0800990 1,
Stephen Hines0d26cef2012-05-01 19:23:01 -0700991 TypeName.c_str(), "v");
992 C.indent() << RS_EXPORT_VAR_PREFIX << VarName << " = v;" << std::endl;
Zonr Chang92b344a2010-10-05 20:39:03 +0800993
Stephen Hines0d26cef2012-05-01 19:23:01 -0700994 if (genCreateFieldPacker(C, ET, FieldPackerName))
995 genPackVarOfType(C, ET, "v", FieldPackerName);
996 C.indent() << "setVar("RS_EXPORT_VAR_INDEX_PREFIX << VarName << ", "
Zonr Chang92b344a2010-10-05 20:39:03 +0800997 << FieldPackerName << ");" << std::endl;
998
999 C.endFunction();
1000 }
1001
Stephen Hines0d26cef2012-05-01 19:23:01 -07001002 genGetExportVariable(C, TypeName, VarName);
Stephen Hines28d60bc2012-10-15 15:54:16 -07001003 genGetFieldID(C, VarName);
Zonr Chang92b344a2010-10-05 20:39:03 +08001004 return;
1005}
1006
Stephen Hinese639eb52010-11-08 19:27:20 -08001007void RSReflection::genConstantArrayTypeExportVariable(
1008 Context &C,
1009 const RSExportVar *EV) {
Stephen Hines6e6578a2011-02-07 18:05:48 -08001010 slangAssert((EV->getType()->getClass() ==
1011 RSExportType::ExportClassConstantArray) &&
1012 "Variable should be type of constant array here");
Zonr Chang2e1dba62010-10-05 22:20:11 +08001013
Stephen Hines0d26cef2012-05-01 19:23:01 -07001014 std::string TypeName = GetTypeName(EV->getType());
1015 std::string VarName = EV->getName();
Zonr Chang2e1dba62010-10-05 22:20:11 +08001016
Stephen Hines0d26cef2012-05-01 19:23:01 -07001017 genPrivateExportVariable(C, TypeName, VarName);
1018 genSetExportVariable(C, TypeName, EV);
1019 genGetExportVariable(C, TypeName, VarName);
Stephen Hines28d60bc2012-10-15 15:54:16 -07001020 genGetFieldID(C, VarName);
Zonr Chang2e1dba62010-10-05 22:20:11 +08001021 return;
1022}
1023
Shih-wei Liao9ef2f782010-10-01 12:31:37 -07001024void RSReflection::genRecordTypeExportVariable(Context &C,
1025 const RSExportVar *EV) {
Stephen Hines6e6578a2011-02-07 18:05:48 -08001026 slangAssert((EV->getType()->getClass() == RSExportType::ExportClassRecord) &&
1027 "Variable should be type of struct here");
Shih-wei Liao9ef2f782010-10-01 12:31:37 -07001028
Stephen Hines0d26cef2012-05-01 19:23:01 -07001029 std::string TypeName = GetTypeName(EV->getType());
1030 std::string VarName = EV->getName();
Shih-wei Liao9ef2f782010-10-01 12:31:37 -07001031
Stephen Hines0d26cef2012-05-01 19:23:01 -07001032 genPrivateExportVariable(C, TypeName, VarName);
1033 genSetExportVariable(C, TypeName, EV);
1034 genGetExportVariable(C, TypeName, VarName);
Stephen Hines28d60bc2012-10-15 15:54:16 -07001035 genGetFieldID(C, VarName);
Stephen Hines0d26cef2012-05-01 19:23:01 -07001036 return;
1037}
1038
1039void RSReflection::genPrivateExportVariable(Context &C,
1040 const std::string &TypeName,
1041 const std::string &VarName) {
Shih-wei Liao9ef2f782010-10-01 12:31:37 -07001042 C.indent() << "private " << TypeName << " "RS_EXPORT_VAR_PREFIX
Stephen Hines0d26cef2012-05-01 19:23:01 -07001043 << VarName << ";" << std::endl;
1044 return;
1045}
Shih-wei Liao9ef2f782010-10-01 12:31:37 -07001046
Stephen Hines0d26cef2012-05-01 19:23:01 -07001047void RSReflection::genSetExportVariable(Context &C,
1048 const std::string &TypeName,
1049 const RSExportVar *EV) {
Shih-wei Liao9ef2f782010-10-01 12:31:37 -07001050 if (!EV->isConst()) {
Stephen Hines0d26cef2012-05-01 19:23:01 -07001051 const char *FieldPackerName = "fp";
1052 std::string VarName = EV->getName();
1053 const RSExportType *ET = EV->getType();
Stephen Hines1f6c3312012-07-03 17:23:33 -07001054 C.startFunction(Context::AM_PublicSynchronized,
Shih-wei Liao9ef2f782010-10-01 12:31:37 -07001055 false,
1056 "void",
Stephen Hines0d26cef2012-05-01 19:23:01 -07001057 "set_" + VarName,
Shih-wei Liao9ef2f782010-10-01 12:31:37 -07001058 1,
Zonr Changd42a4292010-10-17 02:38:43 +08001059 TypeName.c_str(), "v");
Stephen Hines0d26cef2012-05-01 19:23:01 -07001060 C.indent() << RS_EXPORT_VAR_PREFIX << VarName << " = v;" << std::endl;
Shih-wei Liao9ef2f782010-10-01 12:31:37 -07001061
Stephen Hines0d26cef2012-05-01 19:23:01 -07001062 if (genCreateFieldPacker(C, ET, FieldPackerName))
1063 genPackVarOfType(C, ET, "v", FieldPackerName);
Stephen Hinesa6b54142012-04-09 18:25:08 -07001064
1065 if (mRSContext->getTargetAPI() < SLANG_JB_TARGET_API) {
1066 // Legacy apps must use the old setVar() without Element/dim components.
Stephen Hines0d26cef2012-05-01 19:23:01 -07001067 C.indent() << "setVar("RS_EXPORT_VAR_INDEX_PREFIX << VarName
Stephen Hinesa6b54142012-04-09 18:25:08 -07001068 << ", " << FieldPackerName << ");" << std::endl;
1069 } else {
1070 // We only have support for one-dimensional array reflection today,
1071 // but the entry point (i.e. setVar()) takes an array of dimensions.
1072 C.indent() << "int []__dimArr = new int[1];" << std::endl;
Stephen Hines0d26cef2012-05-01 19:23:01 -07001073 C.indent() << "__dimArr[0] = " << ET->getSize() << ";" << std::endl;
1074 C.indent() << "setVar("RS_EXPORT_VAR_INDEX_PREFIX << VarName << ", "
Stephen Hinesa6b54142012-04-09 18:25:08 -07001075 << FieldPackerName << ", " RS_ELEM_PREFIX
Stephen Hines0d26cef2012-05-01 19:23:01 -07001076 << ET->getElementName() << ", __dimArr);" << std::endl;
Stephen Hinesa6b54142012-04-09 18:25:08 -07001077 }
Shih-wei Liao9ef2f782010-10-01 12:31:37 -07001078
1079 C.endFunction();
1080 }
Shih-wei Liao9ef2f782010-10-01 12:31:37 -07001081 return;
1082}
1083
1084void RSReflection::genGetExportVariable(Context &C,
1085 const std::string &TypeName,
1086 const std::string &VarName) {
1087 C.startFunction(Context::AM_Public,
1088 false,
1089 TypeName.c_str(),
1090 "get_" + VarName,
1091 0);
1092
1093 C.indent() << "return "RS_EXPORT_VAR_PREFIX << VarName << ";" << std::endl;
1094
1095 C.endFunction();
Stephen Hines28d60bc2012-10-15 15:54:16 -07001096}
1097
1098void RSReflection::genGetFieldID(Context &C, const std::string &VarName) {
1099 // We only generate getFieldID_*() for non-Pointer (bind) types.
Tim Murrayb81a9932012-10-10 15:56:02 -07001100 if (mRSContext->getTargetAPI() >= SLANG_JB_MR1_TARGET_API) {
Stephen Hines28d60bc2012-10-15 15:54:16 -07001101 C.startFunction(Context::AM_Public,
1102 false,
1103 "Script.FieldID",
1104 "getFieldID_" + VarName,
1105 0);
Tim Murrayb81a9932012-10-10 15:56:02 -07001106
Stephen Hines28d60bc2012-10-15 15:54:16 -07001107 C.indent() << "return createFieldID(" << RS_EXPORT_VAR_INDEX_PREFIX
1108 << VarName << ", null);" << std::endl;
Tim Murrayb81a9932012-10-10 15:56:02 -07001109
Stephen Hines28d60bc2012-10-15 15:54:16 -07001110 C.endFunction();
Tim Murrayb81a9932012-10-10 15:56:02 -07001111 }
Shih-wei Liao9ef2f782010-10-01 12:31:37 -07001112}
1113
1114/******************* Methods to generate script class /end *******************/
1115
1116bool RSReflection::genCreateFieldPacker(Context &C,
1117 const RSExportType *ET,
1118 const char *FieldPackerName) {
1119 size_t AllocSize = RSExportType::GetTypeAllocSize(ET);
1120 if (AllocSize > 0)
1121 C.indent() << "FieldPacker " << FieldPackerName << " = new FieldPacker("
1122 << AllocSize << ");" << std::endl;
1123 else
1124 return false;
1125 return true;
1126}
1127
1128void RSReflection::genPackVarOfType(Context &C,
1129 const RSExportType *ET,
1130 const char *VarName,
1131 const char *FieldPackerName) {
1132 switch (ET->getClass()) {
1133 case RSExportType::ExportClassPrimitive:
1134 case RSExportType::ExportClassVector: {
1135 C.indent() << FieldPackerName << "."
1136 << GetPackerAPIName(
1137 static_cast<const RSExportPrimitiveType*>(ET))
1138 << "(" << VarName << ");" << std::endl;
1139 break;
1140 }
Shih-wei Liao9ef2f782010-10-01 12:31:37 -07001141 case RSExportType::ExportClassPointer: {
1142 // Must reflect as type Allocation in Java
1143 const RSExportType *PointeeType =
1144 static_cast<const RSExportPointerType*>(ET)->getPointeeType();
1145
1146 if (PointeeType->getClass() != RSExportType::ExportClassRecord)
1147 C.indent() << FieldPackerName << ".addI32(" << VarName
1148 << ".getPtr());" << std::endl;
1149 else
1150 C.indent() << FieldPackerName << ".addI32(" << VarName
1151 << ".getAllocation().getPtr());" << std::endl;
1152 break;
1153 }
Zonr Chang92b344a2010-10-05 20:39:03 +08001154 case RSExportType::ExportClassMatrix: {
Alex Sakhartchoukaa180e92010-12-17 13:26:07 -08001155 C.indent() << FieldPackerName << ".addMatrix(" << VarName << ");"
Zonr Chang92b344a2010-10-05 20:39:03 +08001156 << std::endl;
1157 break;
1158 }
Zonr Chang2e1dba62010-10-05 22:20:11 +08001159 case RSExportType::ExportClassConstantArray: {
1160 const RSExportConstantArrayType *ECAT =
1161 static_cast<const RSExportConstantArrayType *>(ET);
Zonr Chang89273bd2010-10-14 20:57:38 +08001162
1163 // TODO(zonr): more elegant way. Currently, we obtain the unique index
1164 // variable (this method involves recursive call which means
1165 // we may have more than one level loop, therefore we can't
1166 // always use the same index variable name here) name given
1167 // in the for-loop from counting the '.' in @VarName.
1168 unsigned Level = 0;
1169 size_t LastDotPos = 0;
1170 std::string ElementVarName(VarName);
1171
1172 while (LastDotPos != std::string::npos) {
1173 LastDotPos = ElementVarName.find_first_of('.', LastDotPos + 1);
1174 Level++;
1175 }
1176 std::string IndexVarName("ct");
1177 IndexVarName.append(llvm::utostr_32(Level));
1178
1179 C.indent() << "for (int " << IndexVarName << " = 0; " <<
1180 IndexVarName << " < " << ECAT->getSize() << "; " <<
1181 IndexVarName << "++)";
Zonr Chang2e1dba62010-10-05 22:20:11 +08001182 C.startBlock();
1183
Zonr Chang89273bd2010-10-14 20:57:38 +08001184 ElementVarName.append("[" + IndexVarName + "]");
Zonr Chang2e1dba62010-10-05 22:20:11 +08001185 genPackVarOfType(C, ECAT->getElementType(), ElementVarName.c_str(),
1186 FieldPackerName);
1187
1188 C.endBlock();
1189 break;
1190 }
Shih-wei Liao9ef2f782010-10-01 12:31:37 -07001191 case RSExportType::ExportClassRecord: {
zonr6315f762010-10-05 15:35:14 +08001192 const RSExportRecordType *ERT =
1193 static_cast<const RSExportRecordType*>(ET);
Shih-wei Liao9ef2f782010-10-01 12:31:37 -07001194 // Relative pos from now on in field packer
1195 unsigned Pos = 0;
1196
1197 for (RSExportRecordType::const_field_iterator I = ERT->fields_begin(),
1198 E = ERT->fields_end();
1199 I != E;
1200 I++) {
1201 const RSExportRecordType::Field *F = *I;
1202 std::string FieldName;
1203 size_t FieldOffset = F->getOffsetInParent();
1204 size_t FieldStoreSize = RSExportType::GetTypeStoreSize(F->getType());
1205 size_t FieldAllocSize = RSExportType::GetTypeAllocSize(F->getType());
1206
1207 if (VarName != NULL)
1208 FieldName = VarName + ("." + F->getName());
1209 else
1210 FieldName = F->getName();
1211
1212 if (FieldOffset > Pos)
1213 C.indent() << FieldPackerName << ".skip("
1214 << (FieldOffset - Pos) << ");" << std::endl;
1215
1216 genPackVarOfType(C, F->getType(), FieldName.c_str(), FieldPackerName);
1217
1218 // There is padding in the field type
1219 if (FieldAllocSize > FieldStoreSize)
1220 C.indent() << FieldPackerName << ".skip("
1221 << (FieldAllocSize - FieldStoreSize)
1222 << ");" << std::endl;
1223
1224 Pos = FieldOffset + FieldAllocSize;
1225 }
1226
1227 // There maybe some padding after the struct
Andrew Hsiehd22724a2011-07-20 18:03:56 -07001228 if (RSExportType::GetTypeAllocSize(ERT) > Pos)
Stephen Hines6b201eb2011-07-20 17:08:16 -07001229 C.indent() << FieldPackerName << ".skip("
Andrew Hsiehd22724a2011-07-20 18:03:56 -07001230 << RSExportType::GetTypeAllocSize(ERT) - Pos << ");"
Shih-wei Liao9ef2f782010-10-01 12:31:37 -07001231 << std::endl;
1232 break;
1233 }
1234 default: {
Stephen Hines6e6578a2011-02-07 18:05:48 -08001235 slangAssert(false && "Unknown class of type");
Shih-wei Liao9ef2f782010-10-01 12:31:37 -07001236 }
1237 }
1238
1239 return;
1240}
1241
Zonr Chang2e1dba62010-10-05 22:20:11 +08001242void RSReflection::genAllocateVarOfType(Context &C,
1243 const RSExportType *T,
1244 const std::string &VarName) {
1245 switch (T->getClass()) {
1246 case RSExportType::ExportClassPrimitive: {
1247 // Primitive type like int in Java has its own storage once it's declared.
1248 //
1249 // FIXME: Should we allocate storage for RS object?
1250 // if (static_cast<const RSExportPrimitiveType *>(T)->isRSObjectType())
1251 // C.indent() << VarName << " = new " << GetTypeName(T) << "();"
1252 // << std::endl;
1253 break;
1254 }
1255 case RSExportType::ExportClassPointer: {
1256 // Pointer type is an instance of Allocation or a TypeClass whose value is
1257 // expected to be assigned by programmer later in Java program. Therefore
1258 // we don't reflect things like [VarName] = new Allocation();
1259 C.indent() << VarName << " = null;" << std::endl;
1260 break;
1261 }
1262 case RSExportType::ExportClassConstantArray: {
1263 const RSExportConstantArrayType *ECAT =
1264 static_cast<const RSExportConstantArrayType *>(T);
1265 const RSExportType *ElementType = ECAT->getElementType();
1266
Zonr Chang2f1451c2010-10-14 02:58:28 +08001267 C.indent() << VarName << " = new " << GetTypeName(ElementType)
1268 << "[" << ECAT->getSize() << "];" << std::endl;
1269
Zonr Chang2e1dba62010-10-05 22:20:11 +08001270 // Primitive type element doesn't need allocation code.
1271 if (ElementType->getClass() != RSExportType::ExportClassPrimitive) {
Zonr Chang2e1dba62010-10-05 22:20:11 +08001272 C.indent() << "for (int $ct = 0; $ct < " << ECAT->getSize() << "; "
1273 "$ct++)";
1274 C.startBlock();
1275
1276 std::string ElementVarName(VarName);
1277 ElementVarName.append("[$ct]");
1278 genAllocateVarOfType(C, ElementType, ElementVarName);
1279
1280 C.endBlock();
1281 }
1282 break;
1283 }
1284 case RSExportType::ExportClassVector:
1285 case RSExportType::ExportClassMatrix:
1286 case RSExportType::ExportClassRecord: {
1287 C.indent() << VarName << " = new " << GetTypeName(T) << "();"
1288 << std::endl;
1289 break;
1290 }
1291 }
1292 return;
1293}
1294
Stephen Hinese639eb52010-11-08 19:27:20 -08001295void RSReflection::genNewItemBufferIfNull(Context &C,
1296 const char *Index) {
1297 C.indent() << "if (" RS_TYPE_ITEM_BUFFER_NAME " == null) "
1298 RS_TYPE_ITEM_BUFFER_NAME " = "
1299 "new " RS_TYPE_ITEM_CLASS_NAME
1300 "[getType().getX() /* count */];"
Shih-wei Liao9ef2f782010-10-01 12:31:37 -07001301 << std::endl;
1302 if (Index != NULL)
1303 C.indent() << "if ("RS_TYPE_ITEM_BUFFER_NAME"[" << Index << "] == null) "
Zonr Chang92b344a2010-10-05 20:39:03 +08001304 RS_TYPE_ITEM_BUFFER_NAME"[" << Index << "] = "
1305 "new "RS_TYPE_ITEM_CLASS_NAME"();" << std::endl;
Shih-wei Liao9ef2f782010-10-01 12:31:37 -07001306 return;
1307}
1308
1309void RSReflection::genNewItemBufferPackerIfNull(Context &C) {
Stephen Hinese639eb52010-11-08 19:27:20 -08001310 C.indent() << "if (" RS_TYPE_ITEM_BUFFER_PACKER_NAME " == null) "
1311 RS_TYPE_ITEM_BUFFER_PACKER_NAME " = "
1312 "new FieldPacker(" RS_TYPE_ITEM_CLASS_NAME
1313 ".sizeof * getType().getX()/* count */"
1314 ");" << std::endl;
Shih-wei Liao9ef2f782010-10-01 12:31:37 -07001315 return;
1316}
1317
1318/********************** Methods to generate type class **********************/
1319bool RSReflection::genTypeClass(Context &C,
1320 const RSExportRecordType *ERT,
1321 std::string &ErrorMsg) {
Stephen Hinesa6b54142012-04-09 18:25:08 -07001322 std::string ClassName = ERT->getElementName();
Shih-wei Liao9ef2f782010-10-01 12:31:37 -07001323
Shih-wei Liao9ef2f782010-10-01 12:31:37 -07001324 if (!C.startClass(Context::AM_Public,
1325 false,
1326 ClassName,
1327 RS_TYPE_CLASS_SUPER_CLASS_NAME,
1328 ErrorMsg))
1329 return false;
1330
Stephen Hines4cc67fc2011-01-31 16:48:57 -08001331 mGeneratedFileNames->push_back(ClassName);
1332
Zonr Chang2e1dba62010-10-05 22:20:11 +08001333 genTypeItemClass(C, ERT);
Shih-wei Liao9ef2f782010-10-01 12:31:37 -07001334
1335 // Declare item buffer and item buffer packer
1336 C.indent() << "private "RS_TYPE_ITEM_CLASS_NAME" "RS_TYPE_ITEM_BUFFER_NAME"[]"
1337 ";" << std::endl;
1338 C.indent() << "private FieldPacker "RS_TYPE_ITEM_BUFFER_PACKER_NAME";"
1339 << std::endl;
Stephen Hinese67239d2012-02-24 15:08:36 -08001340 C.indent() << "private static java.lang.ref.WeakReference<Element> "
1341 RS_TYPE_ELEMENT_REF_NAME
Jason Sams381e95f2011-11-30 14:01:43 -08001342 " = new java.lang.ref.WeakReference<Element>(null);" << std::endl;
Shih-wei Liao9ef2f782010-10-01 12:31:37 -07001343
1344 genTypeClassConstructor(C, ERT);
Alex Sakhartchouk38eca1a2011-08-25 10:47:52 -07001345 genTypeClassCopyToArrayLocal(C, ERT);
Shih-wei Liao9ef2f782010-10-01 12:31:37 -07001346 genTypeClassCopyToArray(C, ERT);
1347 genTypeClassItemSetter(C, ERT);
1348 genTypeClassItemGetter(C, ERT);
1349 genTypeClassComponentSetter(C, ERT);
1350 genTypeClassComponentGetter(C, ERT);
1351 genTypeClassCopyAll(C, ERT);
Zonr Changd42a4292010-10-17 02:38:43 +08001352 genTypeClassResize(C);
Shih-wei Liao9ef2f782010-10-01 12:31:37 -07001353
1354 C.endClass();
1355
Zonr Chang66aa2992010-10-05 15:56:31 +08001356 C.resetFieldIndex();
1357 C.clearFieldIndexMap();
1358
Shih-wei Liao9ef2f782010-10-01 12:31:37 -07001359 return true;
1360}
1361
Stephen Hinese639eb52010-11-08 19:27:20 -08001362void RSReflection::genTypeItemClass(Context &C,
1363 const RSExportRecordType *ERT) {
Shih-wei Liao9ef2f782010-10-01 12:31:37 -07001364 C.indent() << "static public class "RS_TYPE_ITEM_CLASS_NAME;
1365 C.startBlock();
1366
1367 C.indent() << "public static final int sizeof = "
1368 << RSExportType::GetTypeAllocSize(ERT) << ";" << std::endl;
1369
1370 // Member elements
1371 C.out() << std::endl;
1372 for (RSExportRecordType::const_field_iterator FI = ERT->fields_begin(),
1373 FE = ERT->fields_end();
1374 FI != FE;
1375 FI++) {
1376 C.indent() << GetTypeName((*FI)->getType()) << " " << (*FI)->getName()
1377 << ";" << std::endl;
1378 }
1379
1380 // Constructor
1381 C.out() << std::endl;
1382 C.indent() << RS_TYPE_ITEM_CLASS_NAME"()";
1383 C.startBlock();
1384
1385 for (RSExportRecordType::const_field_iterator FI = ERT->fields_begin(),
1386 FE = ERT->fields_end();
1387 FI != FE;
1388 FI++) {
1389 const RSExportRecordType::Field *F = *FI;
Zonr Chang2e1dba62010-10-05 22:20:11 +08001390 genAllocateVarOfType(C, F->getType(), F->getName());
Shih-wei Liao9ef2f782010-10-01 12:31:37 -07001391 }
1392
1393 // end Constructor
1394 C.endBlock();
1395
1396 // end Item class
1397 C.endBlock();
1398
Zonr Chang2e1dba62010-10-05 22:20:11 +08001399 return;
Shih-wei Liao9ef2f782010-10-01 12:31:37 -07001400}
1401
1402void RSReflection::genTypeClassConstructor(Context &C,
1403 const RSExportRecordType *ERT) {
1404 const char *RenderScriptVar = "rs";
1405
1406 C.startFunction(Context::AM_Public,
1407 true,
1408 "Element",
1409 "createElement",
1410 1,
Zonr Changd42a4292010-10-17 02:38:43 +08001411 "RenderScript", RenderScriptVar);
Jason Sams381e95f2011-11-30 14:01:43 -08001412
Stephen Hinese67239d2012-02-24 15:08:36 -08001413 // TODO(all): Fix weak-refs + multi-context issue.
1414 // C.indent() << "Element e = " << RS_TYPE_ELEMENT_REF_NAME
1415 // << ".get();" << std::endl;
1416 // C.indent() << "if (e != null) return e;" << std::endl;
Jason Sams381e95f2011-11-30 14:01:43 -08001417 genBuildElement(C, "eb", ERT, RenderScriptVar, /* IsInline = */true);
Stephen Hinesee35ab72011-12-07 20:30:33 -08001418 C.indent() << "return eb.create();" << std::endl;
Stephen Hinese67239d2012-02-24 15:08:36 -08001419 // C.indent() << "e = eb.create();" << std::endl;
1420 // C.indent() << RS_TYPE_ELEMENT_REF_NAME
1421 // << " = new java.lang.ref.WeakReference<Element>(e);"
1422 // << std::endl;
1423 // C.indent() << "return e;" << std::endl;
Shih-wei Liao9ef2f782010-10-01 12:31:37 -07001424 C.endFunction();
1425
Jason Sams381e95f2011-11-30 14:01:43 -08001426
1427 // private with element
1428 C.startFunction(Context::AM_Private,
1429 false,
1430 NULL,
1431 C.getClassName(),
1432 1,
1433 "RenderScript", RenderScriptVar);
1434 C.indent() << RS_TYPE_ITEM_BUFFER_NAME" = null;" << std::endl;
1435 C.indent() << RS_TYPE_ITEM_BUFFER_PACKER_NAME" = null;" << std::endl;
Stephen Hinese67239d2012-02-24 15:08:36 -08001436 C.indent() << "mElement = createElement(" << RenderScriptVar << ");"
1437 << std::endl;
Jason Sams381e95f2011-11-30 14:01:43 -08001438 C.endFunction();
1439
1440 // 1D without usage
Shih-wei Liao9ef2f782010-10-01 12:31:37 -07001441 C.startFunction(Context::AM_Public,
1442 false,
1443 NULL,
1444 C.getClassName(),
1445 2,
Zonr Changd42a4292010-10-17 02:38:43 +08001446 "RenderScript", RenderScriptVar,
1447 "int", "count");
Shih-wei Liao9ef2f782010-10-01 12:31:37 -07001448
1449 C.indent() << RS_TYPE_ITEM_BUFFER_NAME" = null;" << std::endl;
1450 C.indent() << RS_TYPE_ITEM_BUFFER_PACKER_NAME" = null;" << std::endl;
1451 C.indent() << "mElement = createElement(" << RenderScriptVar << ");"
1452 << std::endl;
1453 // Call init() in super class
1454 C.indent() << "init(" << RenderScriptVar << ", count);" << std::endl;
1455 C.endFunction();
1456
Jason Sams381e95f2011-11-30 14:01:43 -08001457 // 1D with usage
Jason Sams91fe83b2010-12-06 17:07:12 -08001458 C.startFunction(Context::AM_Public,
1459 false,
1460 NULL,
1461 C.getClassName(),
1462 3,
1463 "RenderScript", RenderScriptVar,
1464 "int", "count",
1465 "int", "usages");
1466
1467 C.indent() << RS_TYPE_ITEM_BUFFER_NAME" = null;" << std::endl;
1468 C.indent() << RS_TYPE_ITEM_BUFFER_PACKER_NAME" = null;" << std::endl;
1469 C.indent() << "mElement = createElement(" << RenderScriptVar << ");"
1470 << std::endl;
1471 // Call init() in super class
1472 C.indent() << "init(" << RenderScriptVar << ", count, usages);" << std::endl;
1473 C.endFunction();
1474
Jason Sams381e95f2011-11-30 14:01:43 -08001475
1476 // create1D with usage
1477 C.startFunction(Context::AM_Public,
1478 true,
1479 C.getClassName().c_str(),
1480 "create1D",
1481 3,
1482 "RenderScript", RenderScriptVar,
1483 "int", "dimX",
1484 "int", "usages");
1485 C.indent() << C.getClassName() << " obj = new " << C.getClassName() << "("
1486 << RenderScriptVar << ");" << std::endl;
Stephen Hinese67239d2012-02-24 15:08:36 -08001487 C.indent() << "obj.mAllocation = Allocation.createSized("
1488 "rs, obj.mElement, dimX, usages);" << std::endl;
Jason Sams381e95f2011-11-30 14:01:43 -08001489 C.indent() << "return obj;" << std::endl;
1490 C.endFunction();
1491
1492 // create1D without usage
1493 C.startFunction(Context::AM_Public,
1494 true,
1495 C.getClassName().c_str(),
1496 "create1D",
1497 2,
1498 "RenderScript", RenderScriptVar,
1499 "int", "dimX");
Stephen Hinese67239d2012-02-24 15:08:36 -08001500 C.indent() << "return create1D(" << RenderScriptVar
1501 << ", dimX, Allocation.USAGE_SCRIPT);" << std::endl;
Jason Sams381e95f2011-11-30 14:01:43 -08001502 C.endFunction();
1503
1504
1505 // create2D without usage
1506 C.startFunction(Context::AM_Public,
1507 true,
1508 C.getClassName().c_str(),
1509 "create2D",
1510 3,
1511 "RenderScript", RenderScriptVar,
1512 "int", "dimX",
1513 "int", "dimY");
Stephen Hinese67239d2012-02-24 15:08:36 -08001514 C.indent() << "return create2D(" << RenderScriptVar
1515 << ", dimX, dimY, Allocation.USAGE_SCRIPT);" << std::endl;
Jason Sams381e95f2011-11-30 14:01:43 -08001516 C.endFunction();
1517
1518 // create2D with usage
1519 C.startFunction(Context::AM_Public,
1520 true,
1521 C.getClassName().c_str(),
1522 "create2D",
1523 4,
1524 "RenderScript", RenderScriptVar,
1525 "int", "dimX",
1526 "int", "dimY",
1527 "int", "usages");
1528
1529 C.indent() << C.getClassName() << " obj = new " << C.getClassName() << "("
1530 << RenderScriptVar << ");" << std::endl;
Stephen Hinese67239d2012-02-24 15:08:36 -08001531 C.indent() << "Type.Builder b = new Type.Builder(rs, obj.mElement);"
1532 << std::endl;
Jason Sams381e95f2011-11-30 14:01:43 -08001533 C.indent() << "b.setX(dimX);" << std::endl;
1534 C.indent() << "b.setY(dimY);" << std::endl;
1535 C.indent() << "Type t = b.create();" << std::endl;
Stephen Hinese67239d2012-02-24 15:08:36 -08001536 C.indent() << "obj.mAllocation = Allocation.createTyped(rs, t, usages);"
1537 << std::endl;
Jason Sams381e95f2011-11-30 14:01:43 -08001538 C.indent() << "return obj;" << std::endl;
1539 C.endFunction();
1540
1541
1542 // createTypeBuilder
1543 C.startFunction(Context::AM_Public,
1544 true,
1545 "Type.Builder",
1546 "createTypeBuilder",
1547 1,
1548 "RenderScript", RenderScriptVar);
Stephen Hinese67239d2012-02-24 15:08:36 -08001549 C.indent() << "Element e = createElement(" << RenderScriptVar << ");"
1550 << std::endl;
Jason Sams381e95f2011-11-30 14:01:43 -08001551 C.indent() << "return new Type.Builder(rs, e);" << std::endl;
1552 C.endFunction();
1553
1554 // createCustom with usage
1555 C.startFunction(Context::AM_Public,
1556 true,
1557 C.getClassName().c_str(),
1558 "createCustom",
1559 3,
1560 "RenderScript", RenderScriptVar,
1561 "Type.Builder", "tb",
1562 "int", "usages");
1563 C.indent() << C.getClassName() << " obj = new " << C.getClassName() << "("
1564 << RenderScriptVar << ");" << std::endl;
1565 C.indent() << "Type t = tb.create();" << std::endl;
1566 C.indent() << "if (t.getElement() != obj.mElement) {" << std::endl;
Stephen Hinese67239d2012-02-24 15:08:36 -08001567 C.indent() << " throw new RSIllegalArgumentException("
1568 "\"Type.Builder did not match expected element type.\");"
Jason Sams381e95f2011-11-30 14:01:43 -08001569 << std::endl;
1570 C.indent() << "}" << std::endl;
Stephen Hinese67239d2012-02-24 15:08:36 -08001571 C.indent() << "obj.mAllocation = Allocation.createTyped(rs, t, usages);"
1572 << std::endl;
Jason Sams381e95f2011-11-30 14:01:43 -08001573 C.indent() << "return obj;" << std::endl;
1574 C.endFunction();
Shih-wei Liao9ef2f782010-10-01 12:31:37 -07001575}
1576
Jason Sams381e95f2011-11-30 14:01:43 -08001577
Shih-wei Liao9ef2f782010-10-01 12:31:37 -07001578void RSReflection::genTypeClassCopyToArray(Context &C,
1579 const RSExportRecordType *ERT) {
1580 C.startFunction(Context::AM_Private,
1581 false,
1582 "void",
1583 "copyToArray",
1584 2,
Zonr Changd42a4292010-10-17 02:38:43 +08001585 RS_TYPE_ITEM_CLASS_NAME, "i",
1586 "int", "index");
Shih-wei Liao9ef2f782010-10-01 12:31:37 -07001587
1588 genNewItemBufferPackerIfNull(C);
1589 C.indent() << RS_TYPE_ITEM_BUFFER_PACKER_NAME
Zonr Chang92b344a2010-10-05 20:39:03 +08001590 ".reset(index * "RS_TYPE_ITEM_CLASS_NAME".sizeof);"
1591 << std::endl;
Shih-wei Liao9ef2f782010-10-01 12:31:37 -07001592
Alex Sakhartchouk38eca1a2011-08-25 10:47:52 -07001593 C.indent() << "copyToArrayLocal(i, " RS_TYPE_ITEM_BUFFER_PACKER_NAME
1594 ");" << std::endl;
1595
1596 C.endFunction();
1597 return;
1598}
1599
1600void RSReflection::genTypeClassCopyToArrayLocal(Context &C,
1601 const RSExportRecordType *ERT) {
1602 C.startFunction(Context::AM_Private,
1603 false,
1604 "void",
1605 "copyToArrayLocal",
1606 2,
1607 RS_TYPE_ITEM_CLASS_NAME, "i",
1608 "FieldPacker", "fp");
1609
1610 genPackVarOfType(C, ERT, "i", "fp");
Shih-wei Liao9ef2f782010-10-01 12:31:37 -07001611
1612 C.endFunction();
1613 return;
1614}
1615
1616void RSReflection::genTypeClassItemSetter(Context &C,
zonr6315f762010-10-05 15:35:14 +08001617 const RSExportRecordType *ERT) {
Alex Sakhartchouk38eca1a2011-08-25 10:47:52 -07001618 C.startFunction(Context::AM_PublicSynchronized,
Shih-wei Liao9ef2f782010-10-01 12:31:37 -07001619 false,
1620 "void",
1621 "set",
1622 3,
Zonr Changd42a4292010-10-17 02:38:43 +08001623 RS_TYPE_ITEM_CLASS_NAME, "i",
1624 "int", "index",
1625 "boolean", "copyNow");
Shih-wei Liao9ef2f782010-10-01 12:31:37 -07001626 genNewItemBufferIfNull(C, NULL);
1627 C.indent() << RS_TYPE_ITEM_BUFFER_NAME"[index] = i;" << std::endl;
1628
1629 C.indent() << "if (copyNow) ";
1630 C.startBlock();
1631
1632 C.indent() << "copyToArray(i, index);" << std::endl;
Alex Sakhartchouk38eca1a2011-08-25 10:47:52 -07001633 C.indent() << "FieldPacker fp = new FieldPacker(" RS_TYPE_ITEM_CLASS_NAME
1634 ".sizeof);" << std::endl;
1635 C.indent() << "copyToArrayLocal(i, fp);" << std::endl;
1636 C.indent() << "mAllocation.setFromFieldPacker(index, fp);" << std::endl;
Shih-wei Liao9ef2f782010-10-01 12:31:37 -07001637
1638 // End of if (copyNow)
1639 C.endBlock();
1640
1641 C.endFunction();
1642 return;
1643}
1644
1645void RSReflection::genTypeClassItemGetter(Context &C,
1646 const RSExportRecordType *ERT) {
Alex Sakhartchouk38eca1a2011-08-25 10:47:52 -07001647 C.startFunction(Context::AM_PublicSynchronized,
Shih-wei Liao9ef2f782010-10-01 12:31:37 -07001648 false,
1649 RS_TYPE_ITEM_CLASS_NAME,
1650 "get",
1651 1,
Zonr Changd42a4292010-10-17 02:38:43 +08001652 "int", "index");
Shih-wei Liao9ef2f782010-10-01 12:31:37 -07001653 C.indent() << "if ("RS_TYPE_ITEM_BUFFER_NAME" == null) return null;"
1654 << std::endl;
1655 C.indent() << "return "RS_TYPE_ITEM_BUFFER_NAME"[index];" << std::endl;
1656 C.endFunction();
1657 return;
1658}
1659
1660void RSReflection::genTypeClassComponentSetter(Context &C,
1661 const RSExportRecordType *ERT) {
1662 for (RSExportRecordType::const_field_iterator FI = ERT->fields_begin(),
1663 FE = ERT->fields_end();
1664 FI != FE;
1665 FI++) {
1666 const RSExportRecordType::Field *F = *FI;
1667 size_t FieldOffset = F->getOffsetInParent();
1668 size_t FieldStoreSize = RSExportType::GetTypeStoreSize(F->getType());
1669 unsigned FieldIndex = C.getFieldIndex(F);
1670
Alex Sakhartchouk38eca1a2011-08-25 10:47:52 -07001671 C.startFunction(Context::AM_PublicSynchronized,
Shih-wei Liao9ef2f782010-10-01 12:31:37 -07001672 false,
1673 "void",
1674 "set_" + F->getName(), 3,
Zonr Changd42a4292010-10-17 02:38:43 +08001675 "int", "index",
1676 GetTypeName(F->getType()).c_str(), "v",
1677 "boolean", "copyNow");
Shih-wei Liao9c631ff2010-09-17 11:57:29 -07001678 genNewItemBufferPackerIfNull(C);
Shih-wei Liao9ef2f782010-10-01 12:31:37 -07001679 genNewItemBufferIfNull(C, "index");
1680 C.indent() << RS_TYPE_ITEM_BUFFER_NAME"[index]." << F->getName()
1681 << " = v;" << std::endl;
Shih-wei Liao462aefd2010-06-04 15:32:04 -07001682
1683 C.indent() << "if (copyNow) ";
1684 C.startBlock();
Shih-wei Liao2dd42ff2010-06-15 00:34:38 -07001685
Shih-wei Liao9ef2f782010-10-01 12:31:37 -07001686 if (FieldOffset > 0)
Zonr Chang92b344a2010-10-05 20:39:03 +08001687 C.indent() << RS_TYPE_ITEM_BUFFER_PACKER_NAME
1688 ".reset(index * "RS_TYPE_ITEM_CLASS_NAME".sizeof + "
1689 << FieldOffset << ");" << std::endl;
Shih-wei Liao9ef2f782010-10-01 12:31:37 -07001690 else
Zonr Chang92b344a2010-10-05 20:39:03 +08001691 C.indent() << RS_TYPE_ITEM_BUFFER_PACKER_NAME
1692 ".reset(index * "RS_TYPE_ITEM_CLASS_NAME".sizeof);"
1693 << std::endl;
Shih-wei Liao9ef2f782010-10-01 12:31:37 -07001694 genPackVarOfType(C, F->getType(), "v", RS_TYPE_ITEM_BUFFER_PACKER_NAME);
Shih-wei Liao462aefd2010-06-04 15:32:04 -07001695
Shih-wei Liao9ef2f782010-10-01 12:31:37 -07001696 C.indent() << "FieldPacker fp = new FieldPacker(" << FieldStoreSize << ");"
1697 << std::endl;
1698 genPackVarOfType(C, F->getType(), "v", "fp");
Jason Samsa036a8a2011-01-16 14:52:39 -08001699 C.indent() << "mAllocation.setFromFieldPacker(index, " << FieldIndex
Stephen Hines32917932011-01-11 20:51:53 -08001700 << ", fp);"
Zonr Chang66aa2992010-10-05 15:56:31 +08001701 << std::endl;
Shih-wei Liao9ef2f782010-10-01 12:31:37 -07001702
1703 // End of if (copyNow)
1704 C.endBlock();
Shih-wei Liao462aefd2010-06-04 15:32:04 -07001705
1706 C.endFunction();
Shih-wei Liao9ef2f782010-10-01 12:31:37 -07001707 }
1708 return;
Shih-wei Liao462aefd2010-06-04 15:32:04 -07001709}
1710
Shih-wei Liao9ef2f782010-10-01 12:31:37 -07001711void RSReflection::genTypeClassComponentGetter(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;
Alex Sakhartchouk38eca1a2011-08-25 10:47:52 -07001718 C.startFunction(Context::AM_PublicSynchronized,
Shih-wei Liao9ef2f782010-10-01 12:31:37 -07001719 false,
1720 GetTypeName(F->getType()).c_str(),
1721 "get_" + F->getName(),
1722 1,
Zonr Changd42a4292010-10-17 02:38:43 +08001723 "int", "index");
Shih-wei Liaocf950c42010-10-06 03:44:40 -07001724 C.indent() << "if ("RS_TYPE_ITEM_BUFFER_NAME" == null) return "
1725 << GetTypeNullValue(F->getType()) << ";" << std::endl;
zonr6315f762010-10-05 15:35:14 +08001726 C.indent() << "return "RS_TYPE_ITEM_BUFFER_NAME"[index]." << F->getName()
1727 << ";" << std::endl;
Shih-wei Liao9b1f50b2010-08-05 12:40:41 -07001728 C.endFunction();
Shih-wei Liao9ef2f782010-10-01 12:31:37 -07001729 }
1730 return;
Shih-wei Liao9c631ff2010-09-17 11:57:29 -07001731}
1732
Shih-wei Liao9ef2f782010-10-01 12:31:37 -07001733void RSReflection::genTypeClassCopyAll(Context &C,
1734 const RSExportRecordType *ERT) {
Alex Sakhartchouk38eca1a2011-08-25 10:47:52 -07001735 C.startFunction(Context::AM_PublicSynchronized, false, "void", "copyAll", 0);
Shih-wei Liao9ef2f782010-10-01 12:31:37 -07001736
Zonr Chang2e1dba62010-10-05 22:20:11 +08001737 C.indent() << "for (int ct = 0; ct < "RS_TYPE_ITEM_BUFFER_NAME".length; ct++)"
1738 " copyToArray("RS_TYPE_ITEM_BUFFER_NAME"[ct], ct);"
Shih-wei Liao9ef2f782010-10-01 12:31:37 -07001739 << std::endl;
Stephen Hines06617782011-01-17 12:24:01 -08001740 C.indent() << "mAllocation.setFromFieldPacker(0, "
1741 RS_TYPE_ITEM_BUFFER_PACKER_NAME");"
Shih-wei Liao9ef2f782010-10-01 12:31:37 -07001742 << std::endl;
1743
1744 C.endFunction();
1745 return;
Shih-wei Liao9c631ff2010-09-17 11:57:29 -07001746}
1747
Zonr Changd42a4292010-10-17 02:38:43 +08001748void RSReflection::genTypeClassResize(Context &C) {
Alex Sakhartchouk38eca1a2011-08-25 10:47:52 -07001749 C.startFunction(Context::AM_PublicSynchronized,
Zonr Changd42a4292010-10-17 02:38:43 +08001750 false,
1751 "void",
1752 "resize",
1753 1,
1754 "int", "newSize");
1755
Jason Sams3caea7d2010-10-25 18:15:53 -07001756 C.indent() << "if (mItemArray != null) ";
1757 C.startBlock();
Zonr Changd42a4292010-10-17 02:38:43 +08001758 C.indent() << "int oldSize = mItemArray.length;" << std::endl;
1759 C.indent() << "int copySize = Math.min(oldSize, newSize);" << std::endl;
Jason Sams3caea7d2010-10-25 18:15:53 -07001760 C.indent() << "if (newSize == oldSize) return;" << std::endl;
Zonr Changd42a4292010-10-17 02:38:43 +08001761 C.indent() << "Item ni[] = new Item[newSize];" << std::endl;
1762 C.indent() << "System.arraycopy(mItemArray, 0, ni, 0, copySize);"
1763 << std::endl;
1764 C.indent() << "mItemArray = ni;" << std::endl;
Jason Sams3caea7d2010-10-25 18:15:53 -07001765 C.endBlock();
Zonr Changd42a4292010-10-17 02:38:43 +08001766 C.indent() << "mAllocation.resize(newSize);" << std::endl;
1767
Jason Samscedffd92010-12-16 15:29:49 -08001768 C.indent() << "if (" RS_TYPE_ITEM_BUFFER_PACKER_NAME " != null) "
1769 RS_TYPE_ITEM_BUFFER_PACKER_NAME " = "
1770 "new FieldPacker(" RS_TYPE_ITEM_CLASS_NAME
1771 ".sizeof * getType().getX()/* count */"
1772 ");" << std::endl;
1773
Zonr Changd42a4292010-10-17 02:38:43 +08001774 C.endFunction();
1775 return;
1776}
1777
Shih-wei Liao9ef2f782010-10-01 12:31:37 -07001778/******************** Methods to generate type class /end ********************/
Shih-wei Liao462aefd2010-06-04 15:32:04 -07001779
Shih-wei Liao9ef2f782010-10-01 12:31:37 -07001780/********** Methods to create Element in Java of given record type ***********/
Zonr Chang89273bd2010-10-14 20:57:38 +08001781void RSReflection::genBuildElement(Context &C,
1782 const char *ElementBuilderName,
1783 const RSExportRecordType *ERT,
1784 const char *RenderScriptVar,
1785 bool IsInline) {
Shih-wei Liao9ef2f782010-10-01 12:31:37 -07001786 C.indent() << "Element.Builder " << ElementBuilderName << " = "
1787 "new Element.Builder(" << RenderScriptVar << ");" << std::endl;
1788
1789 // eb.add(...)
1790 genAddElementToElementBuilder(C,
1791 ERT,
1792 "",
1793 ElementBuilderName,
Zonr Chang89273bd2010-10-14 20:57:38 +08001794 RenderScriptVar,
1795 /* ArraySize = */0);
Shih-wei Liao9ef2f782010-10-01 12:31:37 -07001796
Zonr Chang89273bd2010-10-14 20:57:38 +08001797 if (!IsInline)
1798 C.indent() << "return " << ElementBuilderName << ".create();" << std::endl;
Shih-wei Liao9ef2f782010-10-01 12:31:37 -07001799 return;
Shih-wei Liao462aefd2010-06-04 15:32:04 -07001800}
1801
Zonr Chang89273bd2010-10-14 20:57:38 +08001802#define EB_ADD(x) do { \
Zonr Chang92b344a2010-10-05 20:39:03 +08001803 C.indent() << ElementBuilderName \
Zonr Chang89273bd2010-10-14 20:57:38 +08001804 << ".add(" << x << ", \"" << VarName << "\""; \
1805 if (ArraySize > 0) \
1806 C.out() << ", " << ArraySize; \
1807 C.out() << ");" << std::endl; \
1808 C.incFieldIndex(); \
1809} while (false)
Shih-wei Liao462aefd2010-06-04 15:32:04 -07001810
Shih-wei Liao9ef2f782010-10-01 12:31:37 -07001811void RSReflection::genAddElementToElementBuilder(Context &C,
1812 const RSExportType *ET,
1813 const std::string &VarName,
1814 const char *ElementBuilderName,
Zonr Chang89273bd2010-10-14 20:57:38 +08001815 const char *RenderScriptVar,
1816 unsigned ArraySize) {
Stephen Hines47aca4e2012-03-08 20:07:28 -08001817 std::string ElementConstruct = GetBuiltinElementConstruct(ET);
Shih-wei Liao462aefd2010-06-04 15:32:04 -07001818
Stephen Hines47aca4e2012-03-08 20:07:28 -08001819 if (ElementConstruct != "") {
Shih-wei Liao9ef2f782010-10-01 12:31:37 -07001820 EB_ADD(ElementConstruct << "(" << RenderScriptVar << ")");
1821 } else {
1822 if ((ET->getClass() == RSExportType::ExportClassPrimitive) ||
Zonr Chang2e1dba62010-10-05 22:20:11 +08001823 (ET->getClass() == RSExportType::ExportClassVector)) {
Shih-wei Liao9ef2f782010-10-01 12:31:37 -07001824 const RSExportPrimitiveType *EPT =
1825 static_cast<const RSExportPrimitiveType*>(ET);
Stephen Hines47aca4e2012-03-08 20:07:28 -08001826 const char *DataTypeName =
1827 RSExportPrimitiveType::getRSReflectionType(EPT)->rs_type;
Shih-wei Liao9ef2f782010-10-01 12:31:37 -07001828 int Size = (ET->getClass() == RSExportType::ExportClassVector) ?
1829 static_cast<const RSExportVectorType*>(ET)->getNumElement() :
1830 1;
Shih-wei Liao462aefd2010-06-04 15:32:04 -07001831
Stephen Hines47aca4e2012-03-08 20:07:28 -08001832 if (EPT->getClass() == RSExportType::ExportClassPrimitive) {
1833 // Element.createUser()
1834 EB_ADD("Element.createUser(" << RenderScriptVar
1835 << ", Element.DataType."
1836 << DataTypeName << ")");
1837 } else {
1838 slangAssert((ET->getClass() == RSExportType::ExportClassVector) &&
1839 "Unexpected type.");
1840 EB_ADD("Element.createVector(" << RenderScriptVar
1841 << ", Element.DataType."
1842 << DataTypeName << ", "
1843 << Size << ")");
Shih-wei Liaobd49c8f2010-06-17 09:51:36 -07001844 }
Zonr Chang92b344a2010-10-05 20:39:03 +08001845#ifndef NDEBUG
Shih-wei Liao9ef2f782010-10-01 12:31:37 -07001846 } else if (ET->getClass() == RSExportType::ExportClassPointer) {
1847 // Pointer type variable should be resolved in
1848 // GetBuiltinElementConstruct()
Stephen Hines6e6578a2011-02-07 18:05:48 -08001849 slangAssert(false && "??");
Zonr Chang92b344a2010-10-05 20:39:03 +08001850 } else if (ET->getClass() == RSExportType::ExportClassMatrix) {
1851 // Matrix type variable should be resolved
1852 // in GetBuiltinElementConstruct()
Stephen Hines6e6578a2011-02-07 18:05:48 -08001853 slangAssert(false && "??");
Zonr Chang92b344a2010-10-05 20:39:03 +08001854#endif
Zonr Chang2e1dba62010-10-05 22:20:11 +08001855 } else if (ET->getClass() == RSExportType::ExportClassConstantArray) {
1856 const RSExportConstantArrayType *ECAT =
1857 static_cast<const RSExportConstantArrayType *>(ET);
Zonr Chang2e1dba62010-10-05 22:20:11 +08001858
Zonr Chang89273bd2010-10-14 20:57:38 +08001859 const RSExportType *ElementType = ECAT->getElementType();
1860 if (ElementType->getClass() != RSExportType::ExportClassRecord) {
1861 genAddElementToElementBuilder(C,
1862 ECAT->getElementType(),
1863 VarName,
1864 ElementBuilderName,
1865 RenderScriptVar,
1866 ECAT->getSize());
1867 } else {
1868 std::string NewElementBuilderName(ElementBuilderName);
1869 NewElementBuilderName.append(1, '_');
Zonr Chang2e1dba62010-10-05 22:20:11 +08001870
Zonr Chang89273bd2010-10-14 20:57:38 +08001871 genBuildElement(C,
1872 NewElementBuilderName.c_str(),
1873 static_cast<const RSExportRecordType*>(ElementType),
1874 RenderScriptVar,
1875 /* IsInline = */true);
1876 ArraySize = ECAT->getSize();
1877 EB_ADD(NewElementBuilderName << ".create()");
1878 }
Shih-wei Liao9ef2f782010-10-01 12:31:37 -07001879 } else if (ET->getClass() == RSExportType::ExportClassRecord) {
1880 // Simalar to case of RSExportType::ExportClassRecord in genPackVarOfType.
1881 //
zonr6315f762010-10-05 15:35:14 +08001882 // TODO(zonr): Generalize these two function such that there's no
1883 // duplicated codes.
Shih-wei Liao9ef2f782010-10-01 12:31:37 -07001884 const RSExportRecordType *ERT =
1885 static_cast<const RSExportRecordType*>(ET);
1886 int Pos = 0; // relative pos from now on
1887
1888 for (RSExportRecordType::const_field_iterator I = ERT->fields_begin(),
1889 E = ERT->fields_end();
1890 I != E;
1891 I++) {
1892 const RSExportRecordType::Field *F = *I;
1893 std::string FieldName;
1894 int FieldOffset = F->getOffsetInParent();
1895 int FieldStoreSize = RSExportType::GetTypeStoreSize(F->getType());
1896 int FieldAllocSize = RSExportType::GetTypeAllocSize(F->getType());
1897
1898 if (!VarName.empty())
1899 FieldName = VarName + "." + F->getName();
1900 else
1901 FieldName = F->getName();
1902
1903 // Alignment
1904 genAddPaddingToElementBuiler(C,
1905 (FieldOffset - Pos),
1906 ElementBuilderName,
1907 RenderScriptVar);
1908
1909 // eb.add(...)
1910 C.addFieldIndexMapping(F);
Zonr Chang89273bd2010-10-14 20:57:38 +08001911 if (F->getType()->getClass() != RSExportType::ExportClassRecord) {
1912 genAddElementToElementBuilder(C,
1913 F->getType(),
1914 FieldName,
1915 ElementBuilderName,
1916 RenderScriptVar,
1917 0);
1918 } else {
1919 std::string NewElementBuilderName(ElementBuilderName);
1920 NewElementBuilderName.append(1, '_');
1921
1922 genBuildElement(C,
1923 NewElementBuilderName.c_str(),
1924 static_cast<const RSExportRecordType*>(F->getType()),
1925 RenderScriptVar,
1926 /* IsInline = */true);
1927
1928 const std::string &VarName = FieldName; // Hack for EB_ADD macro
1929 EB_ADD(NewElementBuilderName << ".create()");
1930 }
Shih-wei Liao9ef2f782010-10-01 12:31:37 -07001931
Stephen Hinesa9ae5ae2011-11-11 21:16:59 -08001932 if (mRSContext->getTargetAPI() < SLANG_ICS_TARGET_API) {
1933 // There is padding within the field type. This is only necessary
1934 // for HC-targeted APIs.
1935 genAddPaddingToElementBuiler(C,
1936 (FieldAllocSize - FieldStoreSize),
1937 ElementBuilderName,
1938 RenderScriptVar);
1939 }
Shih-wei Liao9ef2f782010-10-01 12:31:37 -07001940
1941 Pos = FieldOffset + FieldAllocSize;
1942 }
1943
1944 // There maybe some padding after the struct
Stephen Hines049229b2011-08-04 09:59:23 -07001945 size_t RecordAllocSize = RSExportType::GetTypeAllocSize(ERT);
Shih-wei Liao9ef2f782010-10-01 12:31:37 -07001946
1947 genAddPaddingToElementBuiler(C,
Stephen Hines049229b2011-08-04 09:59:23 -07001948 RecordAllocSize - Pos,
Shih-wei Liao9ef2f782010-10-01 12:31:37 -07001949 ElementBuilderName,
1950 RenderScriptVar);
1951 } else {
Stephen Hines6e6578a2011-02-07 18:05:48 -08001952 slangAssert(false && "Unknown class of type");
Shih-wei Liaob1a28e72010-06-16 16:31:32 -07001953 }
Shih-wei Liao9ef2f782010-10-01 12:31:37 -07001954 }
Shih-wei Liao462aefd2010-06-04 15:32:04 -07001955}
1956
Shih-wei Liao9ef2f782010-10-01 12:31:37 -07001957void RSReflection::genAddPaddingToElementBuiler(Context &C,
1958 int PaddingSize,
1959 const char *ElementBuilderName,
zonr6315f762010-10-05 15:35:14 +08001960 const char *RenderScriptVar) {
Zonr Chang89273bd2010-10-14 20:57:38 +08001961 unsigned ArraySize = 0; // Hack the EB_ADD macro
Shih-wei Liao9ef2f782010-10-01 12:31:37 -07001962 while (PaddingSize > 0) {
1963 const std::string &VarName = C.createPaddingField();
1964 if (PaddingSize >= 4) {
Zonr Chang89273bd2010-10-14 20:57:38 +08001965 EB_ADD("Element.U32(" << RenderScriptVar << ")");
Shih-wei Liao9ef2f782010-10-01 12:31:37 -07001966 PaddingSize -= 4;
1967 } else if (PaddingSize >= 2) {
Zonr Chang89273bd2010-10-14 20:57:38 +08001968 EB_ADD("Element.U16(" << RenderScriptVar << ")");
Shih-wei Liao9ef2f782010-10-01 12:31:37 -07001969 PaddingSize -= 2;
1970 } else if (PaddingSize >= 1) {
Zonr Chang89273bd2010-10-14 20:57:38 +08001971 EB_ADD("Element.U8(" << RenderScriptVar << ")");
Shih-wei Liao9ef2f782010-10-01 12:31:37 -07001972 PaddingSize -= 1;
Shih-wei Liao462aefd2010-06-04 15:32:04 -07001973 }
Shih-wei Liao9ef2f782010-10-01 12:31:37 -07001974 }
1975 return;
Shih-wei Liao462aefd2010-06-04 15:32:04 -07001976}
1977
1978#undef EB_ADD
Shih-wei Liao9ef2f782010-10-01 12:31:37 -07001979/******** Methods to create Element in Java of given record type /end ********/
Shih-wei Liao462aefd2010-06-04 15:32:04 -07001980
Shih-wei Liaob81c6a42010-10-10 14:15:00 -07001981bool RSReflection::reflect(const std::string &OutputPathBase,
1982 const std::string &OutputPackageName,
Stephen Hines0a813a32012-08-03 16:52:40 -07001983 const std::string &RSPackageName,
Shih-wei Liao9ef2f782010-10-01 12:31:37 -07001984 const std::string &InputFileName,
1985 const std::string &OutputBCFileName) {
1986 Context *C = NULL;
1987 std::string ResourceId = "";
Stephen Hinesa9ae5ae2011-11-11 21:16:59 -08001988 std::string PaddingPrefix = "";
1989
1990 if (mRSContext->getTargetAPI() < SLANG_ICS_TARGET_API) {
1991 PaddingPrefix = "#padding_";
1992 } else {
1993 PaddingPrefix = "#rs_padding_";
1994 }
Shih-wei Liao462aefd2010-06-04 15:32:04 -07001995
Shih-wei Liao9ef2f782010-10-01 12:31:37 -07001996 if (!GetClassNameFromFileName(OutputBCFileName, ResourceId))
1997 return false;
Shih-wei Liao462aefd2010-06-04 15:32:04 -07001998
Shih-wei Liao9ef2f782010-10-01 12:31:37 -07001999 if (ResourceId.empty())
2000 ResourceId = "<Resource ID>";
Shih-wei Liao462aefd2010-06-04 15:32:04 -07002001
Shih-wei Liaob81c6a42010-10-10 14:15:00 -07002002 if (OutputPackageName.empty() || OutputPackageName == "-")
2003 C = new Context(OutputPathBase, InputFileName, "<Package Name>",
Stephen Hines0a813a32012-08-03 16:52:40 -07002004 RSPackageName, ResourceId, PaddingPrefix, true);
Shih-wei Liao9ef2f782010-10-01 12:31:37 -07002005 else
Shih-wei Liaob81c6a42010-10-10 14:15:00 -07002006 C = new Context(OutputPathBase, InputFileName, OutputPackageName,
Stephen Hines0a813a32012-08-03 16:52:40 -07002007 RSPackageName, ResourceId, PaddingPrefix, false);
Shih-wei Liao462aefd2010-06-04 15:32:04 -07002008
Shih-wei Liao9ef2f782010-10-01 12:31:37 -07002009 if (C != NULL) {
2010 std::string ErrorMsg, ScriptClassName;
2011 // class ScriptC_<ScriptName>
2012 if (!GetClassNameFromFileName(InputFileName, ScriptClassName))
2013 return false;
Shih-wei Liao462aefd2010-06-04 15:32:04 -07002014
Shih-wei Liao9ef2f782010-10-01 12:31:37 -07002015 if (ScriptClassName.empty())
2016 ScriptClassName = "<Input Script Name>";
Shih-wei Liao462aefd2010-06-04 15:32:04 -07002017
Shih-wei Liao9ef2f782010-10-01 12:31:37 -07002018 ScriptClassName.insert(0, RS_SCRIPT_CLASS_NAME_PREFIX);
Shih-wei Liao462aefd2010-06-04 15:32:04 -07002019
Shih-wei Liao9ef2f782010-10-01 12:31:37 -07002020 if (mRSContext->getLicenseNote() != NULL) {
2021 C->setLicenseNote(*(mRSContext->getLicenseNote()));
Shih-wei Liao462aefd2010-06-04 15:32:04 -07002022 }
2023
Shih-wei Liao9ef2f782010-10-01 12:31:37 -07002024 if (!genScriptClass(*C, ScriptClassName, ErrorMsg)) {
2025 std::cerr << "Failed to generate class " << ScriptClassName << " ("
2026 << ErrorMsg << ")" << std::endl;
2027 return false;
2028 }
2029
Stephen Hines4cc67fc2011-01-31 16:48:57 -08002030 mGeneratedFileNames->push_back(ScriptClassName);
2031
Shih-wei Liao9ef2f782010-10-01 12:31:37 -07002032 // class ScriptField_<TypeName>
2033 for (RSContext::const_export_type_iterator TI =
2034 mRSContext->export_types_begin(),
2035 TE = mRSContext->export_types_end();
2036 TI != TE;
2037 TI++) {
2038 const RSExportType *ET = TI->getValue();
2039
2040 if (ET->getClass() == RSExportType::ExportClassRecord) {
2041 const RSExportRecordType *ERT =
2042 static_cast<const RSExportRecordType*>(ET);
2043
2044 if (!ERT->isArtificial() && !genTypeClass(*C, ERT, ErrorMsg)) {
2045 std::cerr << "Failed to generate type class for struct '"
2046 << ERT->getName() << "' (" << ErrorMsg << ")" << std::endl;
2047 return false;
2048 }
2049 }
2050 }
2051 }
2052
2053 return true;
Shih-wei Liao462aefd2010-06-04 15:32:04 -07002054}
2055
Shih-wei Liao9ef2f782010-10-01 12:31:37 -07002056/************************** RSReflection::Context **************************/
zonr6315f762010-10-05 15:35:14 +08002057const char *const RSReflection::Context::ApacheLicenseNote =
Shih-wei Liao462aefd2010-06-04 15:32:04 -07002058 "/*\n"
Stephen Hines41cc6102012-02-16 00:42:23 -08002059 " * Copyright (C) 2011-2012 The Android Open Source Project\n"
Shih-wei Liao9ef2f782010-10-01 12:31:37 -07002060 " *\n"
2061 " * Licensed under the Apache License, Version 2.0 (the \"License\");\n"
2062 " * you may not use this file except in compliance with the License.\n"
2063 " * You may obtain a copy of the License at\n"
2064 " *\n"
2065 " * http://www.apache.org/licenses/LICENSE-2.0\n"
2066 " *\n"
2067 " * Unless required by applicable law or agreed to in writing, software\n"
2068 " * distributed under the License is distributed on an \"AS IS\" BASIS,\n"
2069 " * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or "
2070 "implied.\n"
2071 " * See the License for the specific language governing permissions and\n"
2072 " * limitations under the License.\n"
2073 " */\n"
2074 "\n";
Shih-wei Liao462aefd2010-06-04 15:32:04 -07002075
Zonr Chang8c6d9b22010-10-07 18:01:19 +08002076bool RSReflection::Context::openClassFile(const std::string &ClassName,
2077 std::string &ErrorMsg) {
2078 if (!mUseStdout) {
2079 mOF.clear();
2080 std::string Path =
Shih-wei Liaob81c6a42010-10-10 14:15:00 -07002081 RSSlangReflectUtils::ComputePackagedPath(mOutputPathBase.c_str(),
Zonr Chang8c6d9b22010-10-07 18:01:19 +08002082 mPackageName.c_str());
2083
2084 if (!SlangUtils::CreateDirectoryWithParents(Path, &ErrorMsg))
2085 return false;
2086
Raphael8d5a2f62011-02-08 00:15:05 -08002087 std::string ClassFile = Path + OS_PATH_SEPARATOR_STR + ClassName + ".java";
Zonr Chang8c6d9b22010-10-07 18:01:19 +08002088
2089 mOF.open(ClassFile.c_str());
2090 if (!mOF.good()) {
2091 ErrorMsg = "failed to open file '" + ClassFile + "' for write";
2092 return false;
2093 }
2094 }
2095 return true;
2096}
2097
Shih-wei Liao9ef2f782010-10-01 12:31:37 -07002098const char *RSReflection::Context::AccessModifierStr(AccessModifier AM) {
2099 switch (AM) {
2100 case AM_Public: return "public"; break;
2101 case AM_Protected: return "protected"; break;
2102 case AM_Private: return "private"; break;
Alex Sakhartchouk38eca1a2011-08-25 10:47:52 -07002103 case AM_PublicSynchronized: return "public synchronized"; break;
Shih-wei Liao9ef2f782010-10-01 12:31:37 -07002104 default: return ""; break;
2105 }
Shih-wei Liao462aefd2010-06-04 15:32:04 -07002106}
2107
Shih-wei Liao9ef2f782010-10-01 12:31:37 -07002108bool RSReflection::Context::startClass(AccessModifier AM,
2109 bool IsStatic,
2110 const std::string &ClassName,
2111 const char *SuperClassName,
2112 std::string &ErrorMsg) {
2113 if (mVerbose)
2114 std::cout << "Generating " << ClassName << ".java ..." << std::endl;
Shih-wei Liao462aefd2010-06-04 15:32:04 -07002115
Zonr Chang8c6d9b22010-10-07 18:01:19 +08002116 // Open file for class
2117 if (!openClassFile(ClassName, ErrorMsg))
2118 return false;
2119
Shih-wei Liao9ef2f782010-10-01 12:31:37 -07002120 // License
2121 out() << mLicenseNote;
Shih-wei Liao462aefd2010-06-04 15:32:04 -07002122
Shih-wei Liao9ef2f782010-10-01 12:31:37 -07002123 // Notice of generated file
2124 out() << "/*" << std::endl;
2125 out() << " * This file is auto-generated. DO NOT MODIFY!" << std::endl;
Stephen Hinesb7d12692011-09-02 18:16:19 -07002126 out() << " * The source Renderscript file: " << mInputRSFile << std::endl;
Shih-wei Liao9ef2f782010-10-01 12:31:37 -07002127 out() << " */" << std::endl;
Ying Wang4e348442010-08-18 10:29:12 -07002128
Shih-wei Liao9ef2f782010-10-01 12:31:37 -07002129 // Package
2130 if (!mPackageName.empty())
2131 out() << "package " << mPackageName << ";" << std::endl;
2132 out() << std::endl;
Shih-wei Liao462aefd2010-06-04 15:32:04 -07002133
Shih-wei Liao9ef2f782010-10-01 12:31:37 -07002134 // Imports
Stephen Hines0a813a32012-08-03 16:52:40 -07002135 out() << "import " << mRSPackageName << ".*;" << std::endl;
2136 out() << "import android.content.res.Resources;" << std::endl;
Shih-wei Liao9ef2f782010-10-01 12:31:37 -07002137 out() << std::endl;
Shih-wei Liao462aefd2010-06-04 15:32:04 -07002138
Shih-wei Liao9ef2f782010-10-01 12:31:37 -07002139 // All reflected classes should be annotated as hidden, so that they won't
2140 // be exposed in SDK.
2141 out() << "/**" << std::endl;
2142 out() << " * @hide" << std::endl;
2143 out() << " */" << std::endl;
Ying Wang4e348442010-08-18 10:29:12 -07002144
Shih-wei Liao9ef2f782010-10-01 12:31:37 -07002145 out() << AccessModifierStr(AM) << ((IsStatic) ? " static" : "") << " class "
2146 << ClassName;
2147 if (SuperClassName != NULL)
2148 out() << " extends " << SuperClassName;
Shih-wei Liao462aefd2010-06-04 15:32:04 -07002149
Shih-wei Liao9ef2f782010-10-01 12:31:37 -07002150 startBlock();
Shih-wei Liao462aefd2010-06-04 15:32:04 -07002151
Shih-wei Liao9ef2f782010-10-01 12:31:37 -07002152 mClassName = ClassName;
Shih-wei Liao462aefd2010-06-04 15:32:04 -07002153
Shih-wei Liao9ef2f782010-10-01 12:31:37 -07002154 return true;
Shih-wei Liao462aefd2010-06-04 15:32:04 -07002155}
2156
2157void RSReflection::Context::endClass() {
Shih-wei Liao9ef2f782010-10-01 12:31:37 -07002158 endBlock();
2159 if (!mUseStdout)
2160 mOF.close();
2161 clear();
2162 return;
Shih-wei Liao462aefd2010-06-04 15:32:04 -07002163}
2164
2165void RSReflection::Context::startBlock(bool ShouldIndent) {
Shih-wei Liao9ef2f782010-10-01 12:31:37 -07002166 if (ShouldIndent)
2167 indent() << "{" << std::endl;
2168 else
2169 out() << " {" << std::endl;
2170 incIndentLevel();
2171 return;
Shih-wei Liao462aefd2010-06-04 15:32:04 -07002172}
2173
2174void RSReflection::Context::endBlock() {
Shih-wei Liao9ef2f782010-10-01 12:31:37 -07002175 decIndentLevel();
2176 indent() << "}" << std::endl << std::endl;
2177 return;
Shih-wei Liao462aefd2010-06-04 15:32:04 -07002178}
2179
Shih-wei Liao9ef2f782010-10-01 12:31:37 -07002180void RSReflection::Context::startTypeClass(const std::string &ClassName) {
2181 indent() << "public static class " << ClassName;
2182 startBlock();
2183 return;
Shih-wei Liao462aefd2010-06-04 15:32:04 -07002184}
2185
2186void RSReflection::Context::endTypeClass() {
Shih-wei Liao9ef2f782010-10-01 12:31:37 -07002187 endBlock();
2188 return;
Shih-wei Liao462aefd2010-06-04 15:32:04 -07002189}
2190
Shih-wei Liao2dd42ff2010-06-15 00:34:38 -07002191void RSReflection::Context::startFunction(AccessModifier AM,
2192 bool IsStatic,
Shih-wei Liao9ef2f782010-10-01 12:31:37 -07002193 const char *ReturnType,
2194 const std::string &FunctionName,
2195 int Argc, ...) {
2196 ArgTy Args;
2197 va_list vl;
2198 va_start(vl, Argc);
2199
zonr6315f762010-10-05 15:35:14 +08002200 for (int i = 0; i < Argc; i++) {
Shih-wei Liao9ef2f782010-10-01 12:31:37 -07002201 const char *ArgType = va_arg(vl, const char*);
2202 const char *ArgName = va_arg(vl, const char*);
2203
zonr6315f762010-10-05 15:35:14 +08002204 Args.push_back(std::make_pair(ArgType, ArgName));
Shih-wei Liao9ef2f782010-10-01 12:31:37 -07002205 }
2206 va_end(vl);
2207
2208 startFunction(AM, IsStatic, ReturnType, FunctionName, Args);
2209
2210 return;
2211}
2212
2213void RSReflection::Context::startFunction(AccessModifier AM,
2214 bool IsStatic,
2215 const char *ReturnType,
2216 const std::string &FunctionName,
zonr6315f762010-10-05 15:35:14 +08002217 const ArgTy &Args) {
Shih-wei Liao9ef2f782010-10-01 12:31:37 -07002218 indent() << AccessModifierStr(AM) << ((IsStatic) ? " static " : " ")
2219 << ((ReturnType) ? ReturnType : "") << " " << FunctionName << "(";
Shih-wei Liao462aefd2010-06-04 15:32:04 -07002220
Shih-wei Liao9ef2f782010-10-01 12:31:37 -07002221 bool FirstArg = true;
2222 for (ArgTy::const_iterator I = Args.begin(), E = Args.end();
2223 I != E;
2224 I++) {
2225 if (!FirstArg)
2226 out() << ", ";
2227 else
2228 FirstArg = false;
Shih-wei Liao462aefd2010-06-04 15:32:04 -07002229
Shih-wei Liao9ef2f782010-10-01 12:31:37 -07002230 out() << I->first << " " << I->second;
2231 }
Shih-wei Liao462aefd2010-06-04 15:32:04 -07002232
Shih-wei Liao9ef2f782010-10-01 12:31:37 -07002233 out() << ")";
2234 startBlock();
Shih-wei Liao462aefd2010-06-04 15:32:04 -07002235
Shih-wei Liao9ef2f782010-10-01 12:31:37 -07002236 return;
Shih-wei Liao462aefd2010-06-04 15:32:04 -07002237}
2238
2239void RSReflection::Context::endFunction() {
Shih-wei Liao9ef2f782010-10-01 12:31:37 -07002240 endBlock();
2241 return;
Shih-wei Liao462aefd2010-06-04 15:32:04 -07002242}
Stephen Hinese639eb52010-11-08 19:27:20 -08002243
Stephen Hines1f6c3312012-07-03 17:23:33 -07002244bool RSReflection::Context::addTypeNameForElement(
2245 const std::string &TypeName) {
2246 if (mTypesToCheck.find(TypeName) == mTypesToCheck.end()) {
2247 mTypesToCheck.insert(TypeName);
2248 return true;
2249 } else {
2250 return false;
2251 }
2252}
2253
2254bool RSReflection::Context::addTypeNameForFieldPacker(
2255 const std::string &TypeName) {
2256 if (mFieldPackerTypes.find(TypeName) == mFieldPackerTypes.end()) {
2257 mFieldPackerTypes.insert(TypeName);
2258 return true;
2259 } else {
2260 return false;
2261 }
2262}
2263
Stephen Hinese639eb52010-11-08 19:27:20 -08002264} // namespace slang