blob: 5fd5c35168c7fe30262b219450fb660d3c198cb9 [file] [log] [blame]
Jason Samse4a06c52011-03-16 16:29:28 -07001/*
2 * Copyright (C) 2011 The Android Open Source Project
3 *
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)();
39 void (*mInit)();
Stephen Hines514f9792011-08-31 17:41:39 -070040 void (*mFreeChildren)();
Jason Samse4a06c52011-03-16 16:29:28 -070041
42 BCCScriptRef mBccScript;
43
Stephen Hines4382467a2011-08-01 15:02:34 -070044 bcinfo::MetadataExtractor *ME;
Stephen Hinesc27d1872011-07-18 17:24:11 -070045
Jason Samse4a06c52011-03-16 16:29:28 -070046 InvokeFunc_t *mInvokeFunctions;
Jason Samse4a06c52011-03-16 16:29:28 -070047 void ** mFieldAddress;
48 bool * mFieldIsObject;
Stephen Hines12223932011-08-18 19:33:01 -070049 const uint32_t *mExportForEachSignatureList;
Jason Samse4a06c52011-03-16 16:29:28 -070050
51 const uint8_t * mScriptText;
52 uint32_t mScriptTextLength;
Jason Samse4a06c52011-03-16 16:29:28 -070053};
54
Jason Samsbe8ac6a2011-04-21 11:46:50 -070055
Jason Sams55d2a252011-03-17 16:12:47 -070056static Script * setTLS(Script *sc) {
Jason Samsbe8ac6a2011-04-21 11:46:50 -070057 ScriptTLSStruct * tls = (ScriptTLSStruct *)pthread_getspecific(rsdgThreadTLSKey);
Jason Sams55d2a252011-03-17 16:12:47 -070058 rsAssert(tls);
59 Script *old = tls->mScript;
60 tls->mScript = sc;
61 return old;
62}
63
64
Jason Samse4a06c52011-03-16 16:29:28 -070065bool rsdScriptInit(const Context *rsc,
66 ScriptC *script,
67 char const *resName,
68 char const *cacheDir,
69 uint8_t const *bitcode,
70 size_t bitcodeSize,
Jason Samsfcf72312011-04-20 15:09:01 -070071 uint32_t flags) {
Jason Samse4a06c52011-03-16 16:29:28 -070072 //LOGE("rsdScriptCreate %p %p %p %p %i %i %p", rsc, resName, cacheDir, bitcode, bitcodeSize, flags, lookupFunc);
73
Jason Samsbe8ac6a2011-04-21 11:46:50 -070074 pthread_mutex_lock(&rsdgInitMutex);
Jason Samse4a06c52011-03-16 16:29:28 -070075 char *cachePath = NULL;
Stephen Hines4382467a2011-08-01 15:02:34 -070076 size_t exportFuncCount = 0;
77 size_t exportVarCount = 0;
78 size_t objectSlotCount = 0;
Stephen Hines12223932011-08-18 19:33:01 -070079 size_t exportForEachSignatureCount = 0;
Jason Samse4a06c52011-03-16 16:29:28 -070080
81 DrvScript *drv = (DrvScript *)calloc(1, sizeof(DrvScript));
82 if (drv == NULL) {
Jason Samsbe8ac6a2011-04-21 11:46:50 -070083 goto error;
Jason Samse4a06c52011-03-16 16:29:28 -070084 }
85 script->mHal.drv = drv;
86
87 drv->mBccScript = bccCreateScript();
88 script->mHal.info.isThreadable = true;
89 drv->mScriptText = bitcode;
90 drv->mScriptTextLength = bitcodeSize;
91
Stephen Hines4382467a2011-08-01 15:02:34 -070092
93 drv->ME = new bcinfo::MetadataExtractor((const char*)drv->mScriptText,
94 drv->mScriptTextLength);
95 if (!drv->ME->extract()) {
Stephen Hinesc27d1872011-07-18 17:24:11 -070096 LOGE("bcinfo: failed to read script metadata");
97 goto error;
98 }
Stephen Hinesc27d1872011-07-18 17:24:11 -070099
Jason Samse4a06c52011-03-16 16:29:28 -0700100 //LOGE("mBccScript %p", script->mBccScript);
101
Jason Samsfcf72312011-04-20 15:09:01 -0700102 if (bccRegisterSymbolCallback(drv->mBccScript, &rsdLookupRuntimeStub, script) != 0) {
Jason Samse4a06c52011-03-16 16:29:28 -0700103 LOGE("bcc: FAILS to register symbol callback");
104 goto error;
105 }
106
107 if (bccReadBC(drv->mBccScript,
108 resName,
109 (char const *)drv->mScriptText,
110 drv->mScriptTextLength, 0) != 0) {
111 LOGE("bcc: FAILS to read bitcode");
Jason Samsbe8ac6a2011-04-21 11:46:50 -0700112 goto error;
Jason Samse4a06c52011-03-16 16:29:28 -0700113 }
114
Jason Samse4a06c52011-03-16 16:29:28 -0700115 if (bccLinkFile(drv->mBccScript, "/system/lib/libclcore.bc", 0) != 0) {
116 LOGE("bcc: FAILS to link bitcode");
Jason Samsbe8ac6a2011-04-21 11:46:50 -0700117 goto error;
Jason Samse4a06c52011-03-16 16:29:28 -0700118 }
Jason Samse4a06c52011-03-16 16:29:28 -0700119
Logan Chien8ce8a1a2011-07-30 16:53:16 +0800120 if (bccPrepareExecutable(drv->mBccScript, cacheDir, resName, 0) != 0) {
Jason Samse4a06c52011-03-16 16:29:28 -0700121 LOGE("bcc: FAILS to prepare executable");
Jason Samsbe8ac6a2011-04-21 11:46:50 -0700122 goto error;
Jason Samse4a06c52011-03-16 16:29:28 -0700123 }
124
125 free(cachePath);
126
127 drv->mRoot = reinterpret_cast<int (*)()>(bccGetFuncAddr(drv->mBccScript, "root"));
128 drv->mInit = reinterpret_cast<void (*)()>(bccGetFuncAddr(drv->mBccScript, "init"));
Stephen Hines514f9792011-08-31 17:41:39 -0700129 drv->mFreeChildren = reinterpret_cast<void (*)()>(bccGetFuncAddr(drv->mBccScript, ".rs.dtor"));
Jason Samse4a06c52011-03-16 16:29:28 -0700130
Stephen Hines4382467a2011-08-01 15:02:34 -0700131 exportFuncCount = drv->ME->getExportFuncCount();
132 if (exportFuncCount > 0) {
133 drv->mInvokeFunctions = (InvokeFunc_t*) calloc(exportFuncCount,
Stephen Hinesc27d1872011-07-18 17:24:11 -0700134 sizeof(InvokeFunc_t));
Stephen Hines4382467a2011-08-01 15:02:34 -0700135 bccGetExportFuncList(drv->mBccScript, exportFuncCount,
Stephen Hinesc27d1872011-07-18 17:24:11 -0700136 (void **) drv->mInvokeFunctions);
137 } else {
Jason Samse4a06c52011-03-16 16:29:28 -0700138 drv->mInvokeFunctions = NULL;
Jason Samse4a06c52011-03-16 16:29:28 -0700139 }
140
Stephen Hines4382467a2011-08-01 15:02:34 -0700141 exportVarCount = drv->ME->getExportVarCount();
142 if (exportVarCount > 0) {
143 drv->mFieldAddress = (void **) calloc(exportVarCount, sizeof(void*));
144 drv->mFieldIsObject = (bool *) calloc(exportVarCount, sizeof(bool));
145 bccGetExportVarList(drv->mBccScript, exportVarCount,
Stephen Hinesc27d1872011-07-18 17:24:11 -0700146 (void **) drv->mFieldAddress);
147 } else {
Jason Samse4a06c52011-03-16 16:29:28 -0700148 drv->mFieldAddress = NULL;
149 drv->mFieldIsObject = NULL;
Jason Samse4a06c52011-03-16 16:29:28 -0700150 }
151
Stephen Hines4382467a2011-08-01 15:02:34 -0700152 objectSlotCount = drv->ME->getObjectSlotCount();
153 if (objectSlotCount > 0) {
154 const uint32_t *objectSlotList = drv->ME->getObjectSlotList();
155 for (uint32_t ct=0; ct < objectSlotCount; ct++) {
156 drv->mFieldIsObject[objectSlotList[ct]] = true;
Jason Samse4a06c52011-03-16 16:29:28 -0700157 }
Jason Samse4a06c52011-03-16 16:29:28 -0700158 }
159
Stephen Hines12223932011-08-18 19:33:01 -0700160 exportForEachSignatureCount = drv->ME->getExportForEachSignatureCount();
161 rsAssert(exportForEachSignatureCount <= 1);
162 drv->mExportForEachSignatureList = drv->ME->getExportForEachSignatureList();
163
Jason Samse4a06c52011-03-16 16:29:28 -0700164 // Copy info over to runtime
Stephen Hines4382467a2011-08-01 15:02:34 -0700165 script->mHal.info.exportedFunctionCount = drv->ME->getExportFuncCount();
166 script->mHal.info.exportedVariableCount = drv->ME->getExportVarCount();
167 script->mHal.info.exportedPragmaCount = drv->ME->getPragmaCount();
168 script->mHal.info.exportedPragmaKeyList = drv->ME->getPragmaKeyList();
169 script->mHal.info.exportedPragmaValueList = drv->ME->getPragmaValueList();
Jason Samse4a06c52011-03-16 16:29:28 -0700170 script->mHal.info.root = drv->mRoot;
171
Jason Samsbe8ac6a2011-04-21 11:46:50 -0700172 pthread_mutex_unlock(&rsdgInitMutex);
Jason Samse4a06c52011-03-16 16:29:28 -0700173 return true;
174
175error:
176
Jason Samsbe8ac6a2011-04-21 11:46:50 -0700177 pthread_mutex_unlock(&rsdgInitMutex);
Stephen Hines4382467a2011-08-01 15:02:34 -0700178 if (drv->ME) {
179 delete drv->ME;
180 drv->ME = NULL;
181 }
Jason Samse4a06c52011-03-16 16:29:28 -0700182 free(drv);
183 return false;
184
185}
186
Jason Sams55d2a252011-03-17 16:12:47 -0700187typedef struct {
188 Context *rsc;
189 Script *script;
Stephen Hines12223932011-08-18 19:33:01 -0700190 uint32_t sig;
Jason Sams55d2a252011-03-17 16:12:47 -0700191 const Allocation * ain;
192 Allocation * aout;
193 const void * usr;
Jason Samsc1b4c1f2011-08-17 13:46:46 -0700194 size_t usrLen;
Jason Samse4a06c52011-03-16 16:29:28 -0700195
Jason Sams55d2a252011-03-17 16:12:47 -0700196 uint32_t mSliceSize;
197 volatile int mSliceNum;
198
199 const uint8_t *ptrIn;
200 uint32_t eStrideIn;
201 uint8_t *ptrOut;
202 uint32_t eStrideOut;
203
204 uint32_t xStart;
205 uint32_t xEnd;
206 uint32_t yStart;
207 uint32_t yEnd;
208 uint32_t zStart;
209 uint32_t zEnd;
210 uint32_t arrayStart;
211 uint32_t arrayEnd;
212
213 uint32_t dimX;
214 uint32_t dimY;
215 uint32_t dimZ;
216 uint32_t dimArray;
217} MTLaunchStruct;
Stephen Hines12223932011-08-18 19:33:01 -0700218typedef void (*rs_t)(const void *, void *, const void *, uint32_t, uint32_t, uint32_t, uint32_t);
Jason Sams55d2a252011-03-17 16:12:47 -0700219
220static void wc_xy(void *usr, uint32_t idx) {
221 MTLaunchStruct *mtls = (MTLaunchStruct *)usr;
Jason Samsc1b4c1f2011-08-17 13:46:46 -0700222 RsForEachStubParamStruct p;
223 memset(&p, 0, sizeof(p));
224 p.usr = mtls->usr;
225 p.usr_len = mtls->usrLen;
Stephen Hines12223932011-08-18 19:33:01 -0700226 RsdHal * dc = (RsdHal *)mtls->rsc->mHal.drv;
227 uint32_t sig = mtls->sig;
Jason Sams55d2a252011-03-17 16:12:47 -0700228
229 while (1) {
230 uint32_t slice = (uint32_t)android_atomic_inc(&mtls->mSliceNum);
231 uint32_t yStart = mtls->yStart + slice * mtls->mSliceSize;
232 uint32_t yEnd = yStart + mtls->mSliceSize;
233 yEnd = rsMin(yEnd, mtls->yEnd);
234 if (yEnd <= yStart) {
235 return;
236 }
237
238 //LOGE("usr idx %i, x %i,%i y %i,%i", idx, mtls->xStart, mtls->xEnd, yStart, yEnd);
239 //LOGE("usr ptr in %p, out %p", mtls->ptrIn, mtls->ptrOut);
Jason Samsc1b4c1f2011-08-17 13:46:46 -0700240 for (p.y = yStart; p.y < yEnd; p.y++) {
241 uint32_t offset = mtls->dimX * p.y;
Jason Sams55d2a252011-03-17 16:12:47 -0700242 uint8_t *xPtrOut = mtls->ptrOut + (mtls->eStrideOut * offset);
243 const uint8_t *xPtrIn = mtls->ptrIn + (mtls->eStrideIn * offset);
244
Jason Samsc1b4c1f2011-08-17 13:46:46 -0700245 for (p.x = mtls->xStart; p.x < mtls->xEnd; p.x++) {
246 p.in = xPtrIn;
247 p.out = xPtrOut;
Stephen Hines12223932011-08-18 19:33:01 -0700248 dc->mForEachLaunch[sig](&mtls->script->mHal.info.root, &p);
Jason Sams55d2a252011-03-17 16:12:47 -0700249 xPtrIn += mtls->eStrideIn;
250 xPtrOut += mtls->eStrideOut;
251 }
252 }
253 }
Jason Samse4a06c52011-03-16 16:29:28 -0700254}
255
Jason Sams55d2a252011-03-17 16:12:47 -0700256static void wc_x(void *usr, uint32_t idx) {
257 MTLaunchStruct *mtls = (MTLaunchStruct *)usr;
Jason Samsc1b4c1f2011-08-17 13:46:46 -0700258 RsForEachStubParamStruct p;
259 memset(&p, 0, sizeof(p));
260 p.usr = mtls->usr;
261 p.usr_len = mtls->usrLen;
Stephen Hines12223932011-08-18 19:33:01 -0700262 RsdHal * dc = (RsdHal *)mtls->rsc->mHal.drv;
263 uint32_t sig = mtls->sig;
Jason Sams55d2a252011-03-17 16:12:47 -0700264
265 while (1) {
266 uint32_t slice = (uint32_t)android_atomic_inc(&mtls->mSliceNum);
267 uint32_t xStart = mtls->xStart + slice * mtls->mSliceSize;
268 uint32_t xEnd = xStart + mtls->mSliceSize;
269 xEnd = rsMin(xEnd, mtls->xEnd);
270 if (xEnd <= xStart) {
271 return;
272 }
273
274 //LOGE("usr idx %i, x %i,%i y %i,%i", idx, mtls->xStart, mtls->xEnd, yStart, yEnd);
275 //LOGE("usr ptr in %p, out %p", mtls->ptrIn, mtls->ptrOut);
276 uint8_t *xPtrOut = mtls->ptrOut + (mtls->eStrideOut * xStart);
277 const uint8_t *xPtrIn = mtls->ptrIn + (mtls->eStrideIn * xStart);
Jason Samsc1b4c1f2011-08-17 13:46:46 -0700278 for (p.x = xStart; p.x < xEnd; p.x++) {
279 p.in = xPtrIn;
280 p.out = xPtrOut;
Stephen Hines12223932011-08-18 19:33:01 -0700281 dc->mForEachLaunch[sig](&mtls->script->mHal.info.root, &p);
Jason Sams55d2a252011-03-17 16:12:47 -0700282 xPtrIn += mtls->eStrideIn;
283 xPtrOut += mtls->eStrideOut;
284 }
285 }
286}
287
288void rsdScriptInvokeForEach(const Context *rsc,
289 Script *s,
Jason Sams92b0eab2011-07-13 11:26:26 -0700290 uint32_t slot,
Jason Sams55d2a252011-03-17 16:12:47 -0700291 const Allocation * ain,
292 Allocation * aout,
293 const void * usr,
294 uint32_t usrLen,
295 const RsScriptCall *sc) {
296
Jason Samsfcf72312011-04-20 15:09:01 -0700297 RsdHal * dc = (RsdHal *)rsc->mHal.drv;
Jason Sams55d2a252011-03-17 16:12:47 -0700298
299 MTLaunchStruct mtls;
300 memset(&mtls, 0, sizeof(mtls));
301
Stephen Hines12223932011-08-18 19:33:01 -0700302 DrvScript *drv = (DrvScript *)s->mHal.drv;
303 // We only support slot 0 (root) at this point in time.
304 rsAssert(slot == 0);
Jason Sams795072e2011-10-04 15:46:57 -0700305 mtls.sig = 0x1f; // temp fix for old apps, full table in slang_rs_export_foreach.cpp
306 if (drv->mExportForEachSignatureList) {
307 mtls.sig = drv->mExportForEachSignatureList[slot];
308 }
Jason Sams55d2a252011-03-17 16:12:47 -0700309 if (ain) {
310 mtls.dimX = ain->getType()->getDimX();
311 mtls.dimY = ain->getType()->getDimY();
312 mtls.dimZ = ain->getType()->getDimZ();
313 //mtls.dimArray = ain->getType()->getDimArray();
314 } else if (aout) {
315 mtls.dimX = aout->getType()->getDimX();
316 mtls.dimY = aout->getType()->getDimY();
317 mtls.dimZ = aout->getType()->getDimZ();
318 //mtls.dimArray = aout->getType()->getDimArray();
319 } else {
320 rsc->setError(RS_ERROR_BAD_SCRIPT, "rsForEach called with null allocations");
321 return;
322 }
323
324 if (!sc || (sc->xEnd == 0)) {
325 mtls.xEnd = mtls.dimX;
326 } else {
327 rsAssert(sc->xStart < mtls.dimX);
328 rsAssert(sc->xEnd <= mtls.dimX);
329 rsAssert(sc->xStart < sc->xEnd);
330 mtls.xStart = rsMin(mtls.dimX, sc->xStart);
331 mtls.xEnd = rsMin(mtls.dimX, sc->xEnd);
332 if (mtls.xStart >= mtls.xEnd) return;
333 }
334
335 if (!sc || (sc->yEnd == 0)) {
336 mtls.yEnd = mtls.dimY;
337 } else {
338 rsAssert(sc->yStart < mtls.dimY);
339 rsAssert(sc->yEnd <= mtls.dimY);
340 rsAssert(sc->yStart < sc->yEnd);
341 mtls.yStart = rsMin(mtls.dimY, sc->yStart);
342 mtls.yEnd = rsMin(mtls.dimY, sc->yEnd);
343 if (mtls.yStart >= mtls.yEnd) return;
344 }
345
346 mtls.xEnd = rsMax((uint32_t)1, mtls.xEnd);
347 mtls.yEnd = rsMax((uint32_t)1, mtls.yEnd);
348 mtls.zEnd = rsMax((uint32_t)1, mtls.zEnd);
349 mtls.arrayEnd = rsMax((uint32_t)1, mtls.arrayEnd);
350
Stephen Hines97837c92011-04-11 14:02:22 -0700351 rsAssert(!ain || (ain->getType()->getDimZ() == 0));
Jason Sams55d2a252011-03-17 16:12:47 -0700352
353 Context *mrsc = (Context *)rsc;
354 Script * oldTLS = setTLS(s);
355
356 mtls.rsc = mrsc;
357 mtls.ain = ain;
358 mtls.aout = aout;
359 mtls.script = s;
360 mtls.usr = usr;
Jason Samsc1b4c1f2011-08-17 13:46:46 -0700361 mtls.usrLen = usrLen;
Jason Sams55d2a252011-03-17 16:12:47 -0700362 mtls.mSliceSize = 10;
363 mtls.mSliceNum = 0;
364
365 mtls.ptrIn = NULL;
366 mtls.eStrideIn = 0;
367 if (ain) {
368 mtls.ptrIn = (const uint8_t *)ain->getPtr();
369 mtls.eStrideIn = ain->getType()->getElementSizeBytes();
370 }
371
372 mtls.ptrOut = NULL;
373 mtls.eStrideOut = 0;
374 if (aout) {
375 mtls.ptrOut = (uint8_t *)aout->getPtr();
376 mtls.eStrideOut = aout->getType()->getElementSizeBytes();
377 }
378
379 if ((dc->mWorkers.mCount > 1) && s->mHal.info.isThreadable) {
380 if (mtls.dimY > 1) {
381 rsdLaunchThreads(mrsc, wc_xy, &mtls);
382 } else {
383 rsdLaunchThreads(mrsc, wc_x, &mtls);
384 }
385
386 //LOGE("launch 1");
387 } else {
Jason Samsc1b4c1f2011-08-17 13:46:46 -0700388 RsForEachStubParamStruct p;
389 memset(&p, 0, sizeof(p));
390 p.usr = mtls.usr;
391 p.usr_len = mtls.usrLen;
Stephen Hines12223932011-08-18 19:33:01 -0700392 uint32_t sig = mtls.sig;
Jason Samsc1b4c1f2011-08-17 13:46:46 -0700393
Jason Sams55d2a252011-03-17 16:12:47 -0700394 //LOGE("launch 3");
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 Sams55d2a252011-03-17 16:12:47 -0700401 uint8_t *xPtrOut = mtls.ptrOut + (mtls.eStrideOut * offset);
402 const uint8_t *xPtrIn = mtls.ptrIn + (mtls.eStrideIn * offset);
403
Jason Samsc1b4c1f2011-08-17 13:46:46 -0700404 for (p.x = mtls.xStart; p.x < mtls.xEnd; p.x++) {
405 p.in = xPtrIn;
406 p.out = xPtrOut;
Stephen Hines12223932011-08-18 19:33:01 -0700407 dc->mForEachLaunch[sig](&s->mHal.info.root, &p);
Jason Sams55d2a252011-03-17 16:12:47 -0700408 xPtrIn += mtls.eStrideIn;
409 xPtrOut += mtls.eStrideOut;
410 }
411 }
412 }
413 }
414 }
415
416 setTLS(oldTLS);
417}
418
419
420int rsdScriptInvokeRoot(const Context *dc, Script *script) {
421 DrvScript *drv = (DrvScript *)script->mHal.drv;
422
423 Script * oldTLS = setTLS(script);
424 int ret = drv->mRoot();
425 setTLS(oldTLS);
426
427 return ret;
428}
429
430void rsdScriptInvokeInit(const Context *dc, Script *script) {
Jason Samse4a06c52011-03-16 16:29:28 -0700431 DrvScript *drv = (DrvScript *)script->mHal.drv;
432
433 if (drv->mInit) {
434 drv->mInit();
435 }
436}
437
Stephen Hines514f9792011-08-31 17:41:39 -0700438void rsdScriptInvokeFreeChildren(const Context *dc, Script *script) {
439 DrvScript *drv = (DrvScript *)script->mHal.drv;
440
441 if (drv->mFreeChildren) {
442 drv->mFreeChildren();
443 }
444}
Jason Samse4a06c52011-03-16 16:29:28 -0700445
Jason Sams55d2a252011-03-17 16:12:47 -0700446void rsdScriptInvokeFunction(const Context *dc, Script *script,
Jason Samse4a06c52011-03-16 16:29:28 -0700447 uint32_t slot,
448 const void *params,
449 size_t paramLength) {
450 DrvScript *drv = (DrvScript *)script->mHal.drv;
451 //LOGE("invoke %p %p %i %p %i", dc, script, slot, params, paramLength);
452
Jason Sams55d2a252011-03-17 16:12:47 -0700453 Script * oldTLS = setTLS(script);
Jason Samse4a06c52011-03-16 16:29:28 -0700454 ((void (*)(const void *, uint32_t))
455 drv->mInvokeFunctions[slot])(params, paramLength);
Jason Sams55d2a252011-03-17 16:12:47 -0700456 setTLS(oldTLS);
Jason Samse4a06c52011-03-16 16:29:28 -0700457}
458
459void rsdScriptSetGlobalVar(const Context *dc, const Script *script,
460 uint32_t slot, void *data, size_t dataLength) {
461 DrvScript *drv = (DrvScript *)script->mHal.drv;
462 //rsAssert(!script->mFieldIsObject[slot]);
463 //LOGE("setGlobalVar %p %p %i %p %i", dc, script, slot, data, dataLength);
464
465 int32_t *destPtr = ((int32_t **)drv->mFieldAddress)[slot];
466 if (!destPtr) {
467 //LOGV("Calling setVar on slot = %i which is null", slot);
468 return;
469 }
470
471 memcpy(destPtr, data, dataLength);
472}
473
474void rsdScriptSetGlobalBind(const Context *dc, const Script *script, uint32_t slot, void *data) {
475 DrvScript *drv = (DrvScript *)script->mHal.drv;
476 //rsAssert(!script->mFieldIsObject[slot]);
477 //LOGE("setGlobalBind %p %p %i %p", dc, script, slot, data);
478
479 int32_t *destPtr = ((int32_t **)drv->mFieldAddress)[slot];
480 if (!destPtr) {
481 //LOGV("Calling setVar on slot = %i which is null", slot);
482 return;
483 }
484
485 memcpy(destPtr, &data, sizeof(void *));
486}
487
488void rsdScriptSetGlobalObj(const Context *dc, const Script *script, uint32_t slot, ObjectBase *data) {
489 DrvScript *drv = (DrvScript *)script->mHal.drv;
490 //rsAssert(script->mFieldIsObject[slot]);
491 //LOGE("setGlobalObj %p %p %i %p", dc, script, slot, data);
492
493 int32_t *destPtr = ((int32_t **)drv->mFieldAddress)[slot];
494 if (!destPtr) {
495 //LOGV("Calling setVar on slot = %i which is null", slot);
496 return;
497 }
498
Jason Samsfcf72312011-04-20 15:09:01 -0700499 rsrSetObject(dc, script, (ObjectBase **)destPtr, data);
Jason Samse4a06c52011-03-16 16:29:28 -0700500}
501
502void rsdScriptDestroy(const Context *dc, Script *script) {
503 DrvScript *drv = (DrvScript *)script->mHal.drv;
504
505 if (drv->mFieldAddress) {
Stephen Hines4382467a2011-08-01 15:02:34 -0700506 size_t exportVarCount = drv->ME->getExportVarCount();
507 for (size_t ct = 0; ct < exportVarCount; ct++) {
Jason Samse4a06c52011-03-16 16:29:28 -0700508 if (drv->mFieldIsObject[ct]) {
Stephen Hines623cb952011-03-24 20:03:49 -0700509 // The field address can be NULL if the script-side has
510 // optimized the corresponding global variable away.
511 if (drv->mFieldAddress[ct]) {
Jason Samsfcf72312011-04-20 15:09:01 -0700512 rsrClearObject(dc, script, (ObjectBase **)drv->mFieldAddress[ct]);
Stephen Hines623cb952011-03-24 20:03:49 -0700513 }
Jason Samse4a06c52011-03-16 16:29:28 -0700514 }
515 }
Stephen Hinesc27d1872011-07-18 17:24:11 -0700516 free(drv->mFieldAddress);
Jason Samse4a06c52011-03-16 16:29:28 -0700517 drv->mFieldAddress = NULL;
Stephen Hinesc27d1872011-07-18 17:24:11 -0700518 free(drv->mFieldIsObject);
519 drv->mFieldIsObject = NULL; }
Jason Samse4a06c52011-03-16 16:29:28 -0700520
521 if (drv->mInvokeFunctions) {
Stephen Hinesc27d1872011-07-18 17:24:11 -0700522 free(drv->mInvokeFunctions);
Jason Samse4a06c52011-03-16 16:29:28 -0700523 drv->mInvokeFunctions = NULL;
Jason Samse4a06c52011-03-16 16:29:28 -0700524 }
Stephen Hinesc27d1872011-07-18 17:24:11 -0700525
Stephen Hines4382467a2011-08-01 15:02:34 -0700526 delete drv->ME;
527 drv->ME = NULL;
Stephen Hinesc27d1872011-07-18 17:24:11 -0700528
Jason Samse4a06c52011-03-16 16:29:28 -0700529 free(drv);
530 script->mHal.drv = NULL;
531
532}
533
534