blob: bec6ffffce018c42550cc057415762ccf05aafb2 [file] [log] [blame]
Jason Samse4a06c52011-03-16 16:29:28 -07001/*
Stephen Hines6b4cf072012-01-12 18:56:23 -08002 * Copyright (C) 2011-2012 The Android Open Source Project
Jason Samse4a06c52011-03-16 16:29:28 -07003 *
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
17
18#include "rsdCore.h"
19#include "rsdBcc.h"
Jason Samsfcf72312011-04-20 15:09:01 -070020#include "rsdRuntime.h"
Jason Samse4a06c52011-03-16 16:29:28 -070021
Stephen Hines4382467a2011-08-01 15:02:34 -070022#include <bcinfo/MetadataExtractor.h>
Stephen Hinesc27d1872011-07-18 17:24:11 -070023
Jason Samse4a06c52011-03-16 16:29:28 -070024#include "rsContext.h"
25#include "rsScriptC.h"
26
27#include "utils/Timers.h"
28#include "utils/StopWatch.h"
29extern "C" {
30#include "libdex/ZipArchive.h"
31}
32
33
34using namespace android;
35using namespace android::renderscript;
36
37struct DrvScript {
38 int (*mRoot)();
Stephen Hines6b4cf072012-01-12 18:56:23 -080039 int (*mRootExpand)();
Jason Samse4a06c52011-03-16 16:29:28 -070040 void (*mInit)();
Stephen Hines514f9792011-08-31 17:41:39 -070041 void (*mFreeChildren)();
Jason Samse4a06c52011-03-16 16:29:28 -070042
43 BCCScriptRef mBccScript;
44
Stephen Hines4382467a2011-08-01 15:02:34 -070045 bcinfo::MetadataExtractor *ME;
Stephen Hinesc27d1872011-07-18 17:24:11 -070046
Jason Samse4a06c52011-03-16 16:29:28 -070047 InvokeFunc_t *mInvokeFunctions;
Jason Samse4a06c52011-03-16 16:29:28 -070048 void ** mFieldAddress;
49 bool * mFieldIsObject;
Stephen Hines12223932011-08-18 19:33:01 -070050 const uint32_t *mExportForEachSignatureList;
Jason Samse4a06c52011-03-16 16:29:28 -070051
52 const uint8_t * mScriptText;
53 uint32_t mScriptTextLength;
Jason Samse4a06c52011-03-16 16:29:28 -070054};
55
Stephen Hines6b4cf072012-01-12 18:56:23 -080056typedef void (*outer_foreach_t)(
57 const android::renderscript::RsForEachStubParamStruct *,
58 uint32_t x1, uint32_t x2,
59 uint32_t instep, uint32_t outstep);
Jason Samsbe8ac6a2011-04-21 11:46:50 -070060
Jason Sams55d2a252011-03-17 16:12:47 -070061static Script * setTLS(Script *sc) {
Jason Samsbe8ac6a2011-04-21 11:46:50 -070062 ScriptTLSStruct * tls = (ScriptTLSStruct *)pthread_getspecific(rsdgThreadTLSKey);
Jason Sams55d2a252011-03-17 16:12:47 -070063 rsAssert(tls);
64 Script *old = tls->mScript;
65 tls->mScript = sc;
66 return old;
67}
68
69
Jason Samse4a06c52011-03-16 16:29:28 -070070bool rsdScriptInit(const Context *rsc,
71 ScriptC *script,
72 char const *resName,
73 char const *cacheDir,
74 uint8_t const *bitcode,
75 size_t bitcodeSize,
Jason Samsfcf72312011-04-20 15:09:01 -070076 uint32_t flags) {
Steve Block3762c312012-01-06 19:20:56 +000077 //ALOGE("rsdScriptCreate %p %p %p %p %i %i %p", rsc, resName, cacheDir, bitcode, bitcodeSize, flags, lookupFunc);
Jason Samse4a06c52011-03-16 16:29:28 -070078
Jason Samsbe8ac6a2011-04-21 11:46:50 -070079 pthread_mutex_lock(&rsdgInitMutex);
Logan Chien12be3ff2011-11-27 08:41:23 +080080
Stephen Hines4382467a2011-08-01 15:02:34 -070081 size_t exportFuncCount = 0;
82 size_t exportVarCount = 0;
83 size_t objectSlotCount = 0;
Stephen Hines12223932011-08-18 19:33:01 -070084 size_t exportForEachSignatureCount = 0;
Jason Samse4a06c52011-03-16 16:29:28 -070085
86 DrvScript *drv = (DrvScript *)calloc(1, sizeof(DrvScript));
87 if (drv == NULL) {
Jason Samsbe8ac6a2011-04-21 11:46:50 -070088 goto error;
Jason Samse4a06c52011-03-16 16:29:28 -070089 }
90 script->mHal.drv = drv;
91
92 drv->mBccScript = bccCreateScript();
93 script->mHal.info.isThreadable = true;
94 drv->mScriptText = bitcode;
95 drv->mScriptTextLength = bitcodeSize;
96
Stephen Hines4382467a2011-08-01 15:02:34 -070097
98 drv->ME = new bcinfo::MetadataExtractor((const char*)drv->mScriptText,
99 drv->mScriptTextLength);
100 if (!drv->ME->extract()) {
Steve Block3762c312012-01-06 19:20:56 +0000101 ALOGE("bcinfo: failed to read script metadata");
Stephen Hinesc27d1872011-07-18 17:24:11 -0700102 goto error;
103 }
Stephen Hinesc27d1872011-07-18 17:24:11 -0700104
Steve Block3762c312012-01-06 19:20:56 +0000105 //ALOGE("mBccScript %p", script->mBccScript);
Jason Samse4a06c52011-03-16 16:29:28 -0700106
Jason Samsfcf72312011-04-20 15:09:01 -0700107 if (bccRegisterSymbolCallback(drv->mBccScript, &rsdLookupRuntimeStub, script) != 0) {
Steve Block3762c312012-01-06 19:20:56 +0000108 ALOGE("bcc: FAILS to register symbol callback");
Jason Samse4a06c52011-03-16 16:29:28 -0700109 goto error;
110 }
111
112 if (bccReadBC(drv->mBccScript,
113 resName,
114 (char const *)drv->mScriptText,
115 drv->mScriptTextLength, 0) != 0) {
Steve Block3762c312012-01-06 19:20:56 +0000116 ALOGE("bcc: FAILS to read bitcode");
Jason Samsbe8ac6a2011-04-21 11:46:50 -0700117 goto error;
Jason Samse4a06c52011-03-16 16:29:28 -0700118 }
119
Jason Samse4a06c52011-03-16 16:29:28 -0700120 if (bccLinkFile(drv->mBccScript, "/system/lib/libclcore.bc", 0) != 0) {
Steve Block3762c312012-01-06 19:20:56 +0000121 ALOGE("bcc: FAILS to link bitcode");
Jason Samsbe8ac6a2011-04-21 11:46:50 -0700122 goto error;
Jason Samse4a06c52011-03-16 16:29:28 -0700123 }
Jason Samse4a06c52011-03-16 16:29:28 -0700124
Logan Chien8ce8a1a2011-07-30 16:53:16 +0800125 if (bccPrepareExecutable(drv->mBccScript, cacheDir, resName, 0) != 0) {
Steve Block3762c312012-01-06 19:20:56 +0000126 ALOGE("bcc: FAILS to prepare executable");
Jason Samsbe8ac6a2011-04-21 11:46:50 -0700127 goto error;
Jason Samse4a06c52011-03-16 16:29:28 -0700128 }
129
Jason Samse4a06c52011-03-16 16:29:28 -0700130 drv->mRoot = reinterpret_cast<int (*)()>(bccGetFuncAddr(drv->mBccScript, "root"));
Stephen Hines6b4cf072012-01-12 18:56:23 -0800131 drv->mRootExpand = reinterpret_cast<int (*)()>(bccGetFuncAddr(drv->mBccScript, "root.expand"));
Jason Samse4a06c52011-03-16 16:29:28 -0700132 drv->mInit = reinterpret_cast<void (*)()>(bccGetFuncAddr(drv->mBccScript, "init"));
Stephen Hines514f9792011-08-31 17:41:39 -0700133 drv->mFreeChildren = reinterpret_cast<void (*)()>(bccGetFuncAddr(drv->mBccScript, ".rs.dtor"));
Jason Samse4a06c52011-03-16 16:29:28 -0700134
Stephen Hines4382467a2011-08-01 15:02:34 -0700135 exportFuncCount = drv->ME->getExportFuncCount();
136 if (exportFuncCount > 0) {
137 drv->mInvokeFunctions = (InvokeFunc_t*) calloc(exportFuncCount,
Stephen Hinesc27d1872011-07-18 17:24:11 -0700138 sizeof(InvokeFunc_t));
Stephen Hines4382467a2011-08-01 15:02:34 -0700139 bccGetExportFuncList(drv->mBccScript, exportFuncCount,
Stephen Hinesc27d1872011-07-18 17:24:11 -0700140 (void **) drv->mInvokeFunctions);
141 } else {
Jason Samse4a06c52011-03-16 16:29:28 -0700142 drv->mInvokeFunctions = NULL;
Jason Samse4a06c52011-03-16 16:29:28 -0700143 }
144
Stephen Hines4382467a2011-08-01 15:02:34 -0700145 exportVarCount = drv->ME->getExportVarCount();
146 if (exportVarCount > 0) {
147 drv->mFieldAddress = (void **) calloc(exportVarCount, sizeof(void*));
148 drv->mFieldIsObject = (bool *) calloc(exportVarCount, sizeof(bool));
149 bccGetExportVarList(drv->mBccScript, exportVarCount,
Stephen Hinesc27d1872011-07-18 17:24:11 -0700150 (void **) drv->mFieldAddress);
151 } else {
Jason Samse4a06c52011-03-16 16:29:28 -0700152 drv->mFieldAddress = NULL;
153 drv->mFieldIsObject = NULL;
Jason Samse4a06c52011-03-16 16:29:28 -0700154 }
155
Stephen Hines4382467a2011-08-01 15:02:34 -0700156 objectSlotCount = drv->ME->getObjectSlotCount();
157 if (objectSlotCount > 0) {
158 const uint32_t *objectSlotList = drv->ME->getObjectSlotList();
159 for (uint32_t ct=0; ct < objectSlotCount; ct++) {
160 drv->mFieldIsObject[objectSlotList[ct]] = true;
Jason Samse4a06c52011-03-16 16:29:28 -0700161 }
Jason Samse4a06c52011-03-16 16:29:28 -0700162 }
163
Stephen Hines12223932011-08-18 19:33:01 -0700164 exportForEachSignatureCount = drv->ME->getExportForEachSignatureCount();
165 rsAssert(exportForEachSignatureCount <= 1);
166 drv->mExportForEachSignatureList = drv->ME->getExportForEachSignatureList();
167
Jason Samse4a06c52011-03-16 16:29:28 -0700168 // Copy info over to runtime
Stephen Hines4382467a2011-08-01 15:02:34 -0700169 script->mHal.info.exportedFunctionCount = drv->ME->getExportFuncCount();
170 script->mHal.info.exportedVariableCount = drv->ME->getExportVarCount();
171 script->mHal.info.exportedPragmaCount = drv->ME->getPragmaCount();
172 script->mHal.info.exportedPragmaKeyList = drv->ME->getPragmaKeyList();
173 script->mHal.info.exportedPragmaValueList = drv->ME->getPragmaValueList();
Stephen Hines6b4cf072012-01-12 18:56:23 -0800174
175 if (drv->mRootExpand) {
176 script->mHal.info.root = drv->mRootExpand;
177 } else {
178 script->mHal.info.root = drv->mRoot;
179 }
Jason Samse4a06c52011-03-16 16:29:28 -0700180
Jason Samsbe8ac6a2011-04-21 11:46:50 -0700181 pthread_mutex_unlock(&rsdgInitMutex);
Jason Samse4a06c52011-03-16 16:29:28 -0700182 return true;
183
184error:
185
Jason Samsbe8ac6a2011-04-21 11:46:50 -0700186 pthread_mutex_unlock(&rsdgInitMutex);
Stephen Hines4382467a2011-08-01 15:02:34 -0700187 if (drv->ME) {
188 delete drv->ME;
189 drv->ME = NULL;
190 }
Jason Samse4a06c52011-03-16 16:29:28 -0700191 free(drv);
192 return false;
193
194}
195
Jason Sams55d2a252011-03-17 16:12:47 -0700196typedef struct {
197 Context *rsc;
198 Script *script;
Stephen Hines12223932011-08-18 19:33:01 -0700199 uint32_t sig;
Jason Sams55d2a252011-03-17 16:12:47 -0700200 const Allocation * ain;
201 Allocation * aout;
202 const void * usr;
Jason Samsc1b4c1f2011-08-17 13:46:46 -0700203 size_t usrLen;
Jason Samse4a06c52011-03-16 16:29:28 -0700204
Jason Sams55d2a252011-03-17 16:12:47 -0700205 uint32_t mSliceSize;
206 volatile int mSliceNum;
207
208 const uint8_t *ptrIn;
209 uint32_t eStrideIn;
210 uint8_t *ptrOut;
211 uint32_t eStrideOut;
212
213 uint32_t xStart;
214 uint32_t xEnd;
215 uint32_t yStart;
216 uint32_t yEnd;
217 uint32_t zStart;
218 uint32_t zEnd;
219 uint32_t arrayStart;
220 uint32_t arrayEnd;
221
222 uint32_t dimX;
223 uint32_t dimY;
224 uint32_t dimZ;
225 uint32_t dimArray;
226} MTLaunchStruct;
Stephen Hines12223932011-08-18 19:33:01 -0700227typedef void (*rs_t)(const void *, void *, const void *, uint32_t, uint32_t, uint32_t, uint32_t);
Jason Sams55d2a252011-03-17 16:12:47 -0700228
229static void wc_xy(void *usr, uint32_t idx) {
230 MTLaunchStruct *mtls = (MTLaunchStruct *)usr;
Jason Samsc1b4c1f2011-08-17 13:46:46 -0700231 RsForEachStubParamStruct p;
232 memset(&p, 0, sizeof(p));
233 p.usr = mtls->usr;
234 p.usr_len = mtls->usrLen;
Stephen Hines12223932011-08-18 19:33:01 -0700235 RsdHal * dc = (RsdHal *)mtls->rsc->mHal.drv;
236 uint32_t sig = mtls->sig;
Jason Sams55d2a252011-03-17 16:12:47 -0700237
Stephen Hines6b4cf072012-01-12 18:56:23 -0800238 outer_foreach_t fn = (outer_foreach_t) mtls->script->mHal.info.root;
Jason Sams55d2a252011-03-17 16:12:47 -0700239 while (1) {
240 uint32_t slice = (uint32_t)android_atomic_inc(&mtls->mSliceNum);
241 uint32_t yStart = mtls->yStart + slice * mtls->mSliceSize;
242 uint32_t yEnd = yStart + mtls->mSliceSize;
243 yEnd = rsMin(yEnd, mtls->yEnd);
244 if (yEnd <= yStart) {
245 return;
246 }
247
Steve Block3762c312012-01-06 19:20:56 +0000248 //ALOGE("usr idx %i, x %i,%i y %i,%i", idx, mtls->xStart, mtls->xEnd, yStart, yEnd);
249 //ALOGE("usr ptr in %p, out %p", mtls->ptrIn, mtls->ptrOut);
Jason Samsc1b4c1f2011-08-17 13:46:46 -0700250 for (p.y = yStart; p.y < yEnd; p.y++) {
251 uint32_t offset = mtls->dimX * p.y;
Jason Samsaac24a22011-10-11 18:44:05 -0700252 p.out = mtls->ptrOut + (mtls->eStrideOut * offset);
253 p.in = mtls->ptrIn + (mtls->eStrideIn * offset);
Stephen Hines6b4cf072012-01-12 18:56:23 -0800254 fn(&p, mtls->xStart, mtls->xEnd, mtls->eStrideIn, mtls->eStrideOut);
Jason Sams55d2a252011-03-17 16:12:47 -0700255 }
256 }
Jason Samse4a06c52011-03-16 16:29:28 -0700257}
258
Jason Sams55d2a252011-03-17 16:12:47 -0700259static void wc_x(void *usr, uint32_t idx) {
260 MTLaunchStruct *mtls = (MTLaunchStruct *)usr;
Jason Samsc1b4c1f2011-08-17 13:46:46 -0700261 RsForEachStubParamStruct p;
262 memset(&p, 0, sizeof(p));
263 p.usr = mtls->usr;
264 p.usr_len = mtls->usrLen;
Stephen Hines12223932011-08-18 19:33:01 -0700265 RsdHal * dc = (RsdHal *)mtls->rsc->mHal.drv;
266 uint32_t sig = mtls->sig;
Jason Sams55d2a252011-03-17 16:12:47 -0700267
Stephen Hines6b4cf072012-01-12 18:56:23 -0800268 outer_foreach_t fn = (outer_foreach_t) mtls->script->mHal.info.root;
Jason Sams55d2a252011-03-17 16:12:47 -0700269 while (1) {
270 uint32_t slice = (uint32_t)android_atomic_inc(&mtls->mSliceNum);
271 uint32_t xStart = mtls->xStart + slice * mtls->mSliceSize;
272 uint32_t xEnd = xStart + mtls->mSliceSize;
273 xEnd = rsMin(xEnd, mtls->xEnd);
274 if (xEnd <= xStart) {
275 return;
276 }
277
Steve Block3762c312012-01-06 19:20:56 +0000278 //ALOGE("usr slice %i idx %i, x %i,%i", slice, idx, xStart, xEnd);
279 //ALOGE("usr ptr in %p, out %p", mtls->ptrIn, mtls->ptrOut);
Jason Sams2802dae2011-10-12 18:33:01 -0700280
Jason Samsaac24a22011-10-11 18:44:05 -0700281 p.out = mtls->ptrOut + (mtls->eStrideOut * xStart);
282 p.in = mtls->ptrIn + (mtls->eStrideIn * xStart);
Stephen Hines6b4cf072012-01-12 18:56:23 -0800283 fn(&p, xStart, xEnd, mtls->eStrideIn, mtls->eStrideOut);
Jason Sams55d2a252011-03-17 16:12:47 -0700284 }
285}
286
287void rsdScriptInvokeForEach(const Context *rsc,
288 Script *s,
Jason Sams92b0eab2011-07-13 11:26:26 -0700289 uint32_t slot,
Jason Sams55d2a252011-03-17 16:12:47 -0700290 const Allocation * ain,
291 Allocation * aout,
292 const void * usr,
293 uint32_t usrLen,
294 const RsScriptCall *sc) {
295
Jason Samsfcf72312011-04-20 15:09:01 -0700296 RsdHal * dc = (RsdHal *)rsc->mHal.drv;
Jason Sams55d2a252011-03-17 16:12:47 -0700297
298 MTLaunchStruct mtls;
299 memset(&mtls, 0, sizeof(mtls));
300
Stephen Hines12223932011-08-18 19:33:01 -0700301 DrvScript *drv = (DrvScript *)s->mHal.drv;
302 // We only support slot 0 (root) at this point in time.
303 rsAssert(slot == 0);
Jason Sams795072e2011-10-04 15:46:57 -0700304 mtls.sig = 0x1f; // temp fix for old apps, full table in slang_rs_export_foreach.cpp
305 if (drv->mExportForEachSignatureList) {
306 mtls.sig = drv->mExportForEachSignatureList[slot];
307 }
Jason Sams55d2a252011-03-17 16:12:47 -0700308 if (ain) {
309 mtls.dimX = ain->getType()->getDimX();
310 mtls.dimY = ain->getType()->getDimY();
311 mtls.dimZ = ain->getType()->getDimZ();
312 //mtls.dimArray = ain->getType()->getDimArray();
313 } else if (aout) {
314 mtls.dimX = aout->getType()->getDimX();
315 mtls.dimY = aout->getType()->getDimY();
316 mtls.dimZ = aout->getType()->getDimZ();
317 //mtls.dimArray = aout->getType()->getDimArray();
318 } else {
319 rsc->setError(RS_ERROR_BAD_SCRIPT, "rsForEach called with null allocations");
320 return;
321 }
322
323 if (!sc || (sc->xEnd == 0)) {
324 mtls.xEnd = mtls.dimX;
325 } else {
326 rsAssert(sc->xStart < mtls.dimX);
327 rsAssert(sc->xEnd <= mtls.dimX);
328 rsAssert(sc->xStart < sc->xEnd);
329 mtls.xStart = rsMin(mtls.dimX, sc->xStart);
330 mtls.xEnd = rsMin(mtls.dimX, sc->xEnd);
331 if (mtls.xStart >= mtls.xEnd) return;
332 }
333
334 if (!sc || (sc->yEnd == 0)) {
335 mtls.yEnd = mtls.dimY;
336 } else {
337 rsAssert(sc->yStart < mtls.dimY);
338 rsAssert(sc->yEnd <= mtls.dimY);
339 rsAssert(sc->yStart < sc->yEnd);
340 mtls.yStart = rsMin(mtls.dimY, sc->yStart);
341 mtls.yEnd = rsMin(mtls.dimY, sc->yEnd);
342 if (mtls.yStart >= mtls.yEnd) return;
343 }
344
345 mtls.xEnd = rsMax((uint32_t)1, mtls.xEnd);
346 mtls.yEnd = rsMax((uint32_t)1, mtls.yEnd);
347 mtls.zEnd = rsMax((uint32_t)1, mtls.zEnd);
348 mtls.arrayEnd = rsMax((uint32_t)1, mtls.arrayEnd);
349
Stephen Hines97837c92011-04-11 14:02:22 -0700350 rsAssert(!ain || (ain->getType()->getDimZ() == 0));
Jason Sams55d2a252011-03-17 16:12:47 -0700351
352 Context *mrsc = (Context *)rsc;
353 Script * oldTLS = setTLS(s);
354
355 mtls.rsc = mrsc;
356 mtls.ain = ain;
357 mtls.aout = aout;
358 mtls.script = s;
359 mtls.usr = usr;
Jason Samsc1b4c1f2011-08-17 13:46:46 -0700360 mtls.usrLen = usrLen;
Jason Sams55d2a252011-03-17 16:12:47 -0700361 mtls.mSliceSize = 10;
362 mtls.mSliceNum = 0;
363
364 mtls.ptrIn = NULL;
365 mtls.eStrideIn = 0;
366 if (ain) {
367 mtls.ptrIn = (const uint8_t *)ain->getPtr();
368 mtls.eStrideIn = ain->getType()->getElementSizeBytes();
369 }
370
371 mtls.ptrOut = NULL;
372 mtls.eStrideOut = 0;
373 if (aout) {
374 mtls.ptrOut = (uint8_t *)aout->getPtr();
375 mtls.eStrideOut = aout->getType()->getElementSizeBytes();
376 }
377
378 if ((dc->mWorkers.mCount > 1) && s->mHal.info.isThreadable) {
379 if (mtls.dimY > 1) {
380 rsdLaunchThreads(mrsc, wc_xy, &mtls);
381 } else {
382 rsdLaunchThreads(mrsc, wc_x, &mtls);
383 }
384
Steve Block3762c312012-01-06 19:20:56 +0000385 //ALOGE("launch 1");
Jason Sams55d2a252011-03-17 16:12:47 -0700386 } else {
Jason Samsc1b4c1f2011-08-17 13:46:46 -0700387 RsForEachStubParamStruct p;
388 memset(&p, 0, sizeof(p));
389 p.usr = mtls.usr;
390 p.usr_len = mtls.usrLen;
Stephen Hines12223932011-08-18 19:33:01 -0700391 uint32_t sig = mtls.sig;
Jason Samsc1b4c1f2011-08-17 13:46:46 -0700392
Steve Block3762c312012-01-06 19:20:56 +0000393 //ALOGE("launch 3");
Stephen Hines6b4cf072012-01-12 18:56:23 -0800394 outer_foreach_t fn = (outer_foreach_t) mtls.script->mHal.info.root;
Jason Samsc1b4c1f2011-08-17 13:46:46 -0700395 for (p.ar[0] = mtls.arrayStart; p.ar[0] < mtls.arrayEnd; p.ar[0]++) {
396 for (p.z = mtls.zStart; p.z < mtls.zEnd; p.z++) {
397 for (p.y = mtls.yStart; p.y < mtls.yEnd; p.y++) {
398 uint32_t offset = mtls.dimX * mtls.dimY * mtls.dimZ * p.ar[0] +
399 mtls.dimX * mtls.dimY * p.z +
400 mtls.dimX * p.y;
Jason Samsaac24a22011-10-11 18:44:05 -0700401 p.out = mtls.ptrOut + (mtls.eStrideOut * offset);
402 p.in = mtls.ptrIn + (mtls.eStrideIn * offset);
Stephen Hines6b4cf072012-01-12 18:56:23 -0800403 fn(&p, mtls.xStart, mtls.xEnd, mtls.eStrideIn,
404 mtls.eStrideOut);
Jason Sams55d2a252011-03-17 16:12:47 -0700405 }
406 }
407 }
408 }
409
410 setTLS(oldTLS);
411}
412
413
414int rsdScriptInvokeRoot(const Context *dc, Script *script) {
415 DrvScript *drv = (DrvScript *)script->mHal.drv;
416
417 Script * oldTLS = setTLS(script);
418 int ret = drv->mRoot();
419 setTLS(oldTLS);
420
421 return ret;
422}
423
424void rsdScriptInvokeInit(const Context *dc, Script *script) {
Jason Samse4a06c52011-03-16 16:29:28 -0700425 DrvScript *drv = (DrvScript *)script->mHal.drv;
426
427 if (drv->mInit) {
428 drv->mInit();
429 }
430}
431
Stephen Hines514f9792011-08-31 17:41:39 -0700432void rsdScriptInvokeFreeChildren(const Context *dc, Script *script) {
433 DrvScript *drv = (DrvScript *)script->mHal.drv;
434
435 if (drv->mFreeChildren) {
436 drv->mFreeChildren();
437 }
438}
Jason Samse4a06c52011-03-16 16:29:28 -0700439
Jason Sams55d2a252011-03-17 16:12:47 -0700440void rsdScriptInvokeFunction(const Context *dc, Script *script,
Jason Samse4a06c52011-03-16 16:29:28 -0700441 uint32_t slot,
442 const void *params,
443 size_t paramLength) {
444 DrvScript *drv = (DrvScript *)script->mHal.drv;
Steve Block3762c312012-01-06 19:20:56 +0000445 //ALOGE("invoke %p %p %i %p %i", dc, script, slot, params, paramLength);
Jason Samse4a06c52011-03-16 16:29:28 -0700446
Jason Sams55d2a252011-03-17 16:12:47 -0700447 Script * oldTLS = setTLS(script);
Jason Samse4a06c52011-03-16 16:29:28 -0700448 ((void (*)(const void *, uint32_t))
449 drv->mInvokeFunctions[slot])(params, paramLength);
Jason Sams55d2a252011-03-17 16:12:47 -0700450 setTLS(oldTLS);
Jason Samse4a06c52011-03-16 16:29:28 -0700451}
452
453void rsdScriptSetGlobalVar(const Context *dc, const Script *script,
454 uint32_t slot, void *data, size_t dataLength) {
455 DrvScript *drv = (DrvScript *)script->mHal.drv;
456 //rsAssert(!script->mFieldIsObject[slot]);
Steve Block3762c312012-01-06 19:20:56 +0000457 //ALOGE("setGlobalVar %p %p %i %p %i", dc, script, slot, data, dataLength);
Jason Samse4a06c52011-03-16 16:29:28 -0700458
459 int32_t *destPtr = ((int32_t **)drv->mFieldAddress)[slot];
460 if (!destPtr) {
Steve Block71f2cf12011-10-20 11:56:00 +0100461 //ALOGV("Calling setVar on slot = %i which is null", slot);
Jason Samse4a06c52011-03-16 16:29:28 -0700462 return;
463 }
464
465 memcpy(destPtr, data, dataLength);
466}
467
468void rsdScriptSetGlobalBind(const Context *dc, const Script *script, uint32_t slot, void *data) {
469 DrvScript *drv = (DrvScript *)script->mHal.drv;
470 //rsAssert(!script->mFieldIsObject[slot]);
Steve Block3762c312012-01-06 19:20:56 +0000471 //ALOGE("setGlobalBind %p %p %i %p", dc, script, slot, data);
Jason Samse4a06c52011-03-16 16:29:28 -0700472
473 int32_t *destPtr = ((int32_t **)drv->mFieldAddress)[slot];
474 if (!destPtr) {
Steve Block71f2cf12011-10-20 11:56:00 +0100475 //ALOGV("Calling setVar on slot = %i which is null", slot);
Jason Samse4a06c52011-03-16 16:29:28 -0700476 return;
477 }
478
479 memcpy(destPtr, &data, sizeof(void *));
480}
481
482void rsdScriptSetGlobalObj(const Context *dc, const Script *script, uint32_t slot, ObjectBase *data) {
483 DrvScript *drv = (DrvScript *)script->mHal.drv;
484 //rsAssert(script->mFieldIsObject[slot]);
Steve Block3762c312012-01-06 19:20:56 +0000485 //ALOGE("setGlobalObj %p %p %i %p", dc, script, slot, data);
Jason Samse4a06c52011-03-16 16:29:28 -0700486
487 int32_t *destPtr = ((int32_t **)drv->mFieldAddress)[slot];
488 if (!destPtr) {
Steve Block71f2cf12011-10-20 11:56:00 +0100489 //ALOGV("Calling setVar on slot = %i which is null", slot);
Jason Samse4a06c52011-03-16 16:29:28 -0700490 return;
491 }
492
Jason Samsfcf72312011-04-20 15:09:01 -0700493 rsrSetObject(dc, script, (ObjectBase **)destPtr, data);
Jason Samse4a06c52011-03-16 16:29:28 -0700494}
495
496void rsdScriptDestroy(const Context *dc, Script *script) {
497 DrvScript *drv = (DrvScript *)script->mHal.drv;
498
499 if (drv->mFieldAddress) {
Stephen Hines4382467a2011-08-01 15:02:34 -0700500 size_t exportVarCount = drv->ME->getExportVarCount();
501 for (size_t ct = 0; ct < exportVarCount; ct++) {
Jason Samse4a06c52011-03-16 16:29:28 -0700502 if (drv->mFieldIsObject[ct]) {
Stephen Hines623cb952011-03-24 20:03:49 -0700503 // The field address can be NULL if the script-side has
504 // optimized the corresponding global variable away.
505 if (drv->mFieldAddress[ct]) {
Jason Samsfcf72312011-04-20 15:09:01 -0700506 rsrClearObject(dc, script, (ObjectBase **)drv->mFieldAddress[ct]);
Stephen Hines623cb952011-03-24 20:03:49 -0700507 }
Jason Samse4a06c52011-03-16 16:29:28 -0700508 }
509 }
Stephen Hinesc27d1872011-07-18 17:24:11 -0700510 free(drv->mFieldAddress);
Jason Samse4a06c52011-03-16 16:29:28 -0700511 drv->mFieldAddress = NULL;
Stephen Hinesc27d1872011-07-18 17:24:11 -0700512 free(drv->mFieldIsObject);
513 drv->mFieldIsObject = NULL; }
Jason Samse4a06c52011-03-16 16:29:28 -0700514
515 if (drv->mInvokeFunctions) {
Stephen Hinesc27d1872011-07-18 17:24:11 -0700516 free(drv->mInvokeFunctions);
Jason Samse4a06c52011-03-16 16:29:28 -0700517 drv->mInvokeFunctions = NULL;
Jason Samse4a06c52011-03-16 16:29:28 -0700518 }
Stephen Hinesc27d1872011-07-18 17:24:11 -0700519
Stephen Hines4382467a2011-08-01 15:02:34 -0700520 delete drv->ME;
521 drv->ME = NULL;
Stephen Hinesc27d1872011-07-18 17:24:11 -0700522
Jason Samse4a06c52011-03-16 16:29:28 -0700523 free(drv);
524 script->mHal.drv = NULL;
525
526}
527
528