rsCpuScript.cpp revision ef7481e2f0a4ad7b32bb626245e4207cabe171dc
1/*
2 * Copyright (C) 2011-2012 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
19#include "rsCpuCore.h"
20
21#include "rsCpuScript.h"
22//#include "rsdRuntime.h"
23//#include "rsdAllocation.h"
24//#include "rsCpuIntrinsics.h"
25
26#ifndef RS_SERVER
27#include "utils/Vector.h"
28#include "utils/Timers.h"
29#include "utils/StopWatch.h"
30#endif
31
32#ifdef RS_COMPATIBILITY_LIB
33    #include <dlfcn.h>
34    #include <stdio.h>
35    #include <string.h>
36#else
37    #include <bcc/BCCContext.h>
38    #include <bcc/Renderscript/RSCompilerDriver.h>
39    #include <bcc/Renderscript/RSExecutable.h>
40    #include <bcc/Renderscript/RSInfo.h>
41#endif
42
43namespace android {
44namespace renderscript {
45
46
47#ifdef RS_COMPATIBILITY_LIB
48#define MAXLINE 500
49#define MAKE_STR_HELPER(S) #S
50#define MAKE_STR(S) MAKE_STR_HELPER(S)
51#define EXPORT_VAR_STR "exportVarCount: "
52#define EXPORT_VAR_STR_LEN strlen(EXPORT_VAR_STR)
53#define EXPORT_FUNC_STR "exportFuncCount: "
54#define EXPORT_FUNC_STR_LEN strlen(EXPORT_FUNC_STR)
55#define EXPORT_FOREACH_STR "exportForEachCount: "
56#define EXPORT_FOREACH_STR_LEN strlen(EXPORT_FOREACH_STR)
57#define OBJECT_SLOT_STR "objectSlotCount: "
58#define OBJECT_SLOT_STR_LEN strlen(OBJECT_SLOT_STR)
59
60// Copy up to a newline or size chars from str -> s, updating str
61// Returns s when successful and NULL when '\0' is finally reached.
62static char* strgets(char *s, int size, const char **ppstr) {
63    if (!ppstr || !*ppstr || **ppstr == '\0' || size < 1) {
64        return NULL;
65    }
66
67    int i;
68    for (i = 0; i < (size - 1); i++) {
69        s[i] = **ppstr;
70        (*ppstr)++;
71        if (s[i] == '\0') {
72            return s;
73        } else if (s[i] == '\n') {
74            s[i+1] = '\0';
75            return s;
76        }
77    }
78
79    // size has been exceeded.
80    s[i] = '\0';
81
82    return s;
83}
84#endif
85
86RsdCpuScriptImpl::RsdCpuScriptImpl(RsdCpuReferenceImpl *ctx, const Script *s) {
87    mCtx = ctx;
88    mScript = s;
89
90#ifdef RS_COMPATIBILITY_LIB
91    mScriptSO = NULL;
92    mInvokeFunctions = NULL;
93    mForEachFunctions = NULL;
94    mFieldAddress = NULL;
95    mFieldIsObject = NULL;
96    mForEachSignatures = NULL;
97#else
98    mCompilerContext = NULL;
99    mCompilerDriver = NULL;
100    mExecutable = NULL;
101#endif
102
103    mRoot = NULL;
104    mRootExpand = NULL;
105    mInit = NULL;
106    mFreeChildren = NULL;
107
108
109    mBoundAllocs = NULL;
110    mIntrinsicData = NULL;
111    mIsThreadable = true;
112}
113
114
115bool RsdCpuScriptImpl::init(char const *resName, char const *cacheDir,
116                            uint8_t const *bitcode, size_t bitcodeSize,
117                            uint32_t flags) {
118    //ALOGE("rsdScriptCreate %p %p %p %p %i %i %p", rsc, resName, cacheDir, bitcode, bitcodeSize, flags, lookupFunc);
119    //ALOGE("rsdScriptInit %p %p", rsc, script);
120
121    mCtx->lockMutex();
122
123#ifndef RS_COMPATIBILITY_LIB
124    bcc::RSExecutable *exec;
125
126    mCompilerContext = NULL;
127    mCompilerDriver = NULL;
128    mExecutable = NULL;
129
130    mCompilerContext = new bcc::BCCContext();
131    if (mCompilerContext == NULL) {
132        ALOGE("bcc: FAILS to create compiler context (out of memory)");
133        mCtx->unlockMutex();
134        return false;
135    }
136
137    mCompilerDriver = new bcc::RSCompilerDriver();
138    if (mCompilerDriver == NULL) {
139        ALOGE("bcc: FAILS to create compiler driver (out of memory)");
140        mCtx->unlockMutex();
141        return false;
142    }
143
144    mCompilerDriver->setRSRuntimeLookupFunction(lookupRuntimeStub);
145    mCompilerDriver->setRSRuntimeLookupContext(this);
146
147    const char *core_lib = NULL;
148    RSSelectRTCallback selectRTCallback = mCtx->getSelectRTCallback();
149    if (selectRTCallback != NULL) {
150        core_lib = selectRTCallback((const char *)bitcode, bitcodeSize);
151    }
152    exec = mCompilerDriver->build(*mCompilerContext, cacheDir, resName,
153                                  (const char *)bitcode, bitcodeSize, core_lib,
154                                  mCtx->getLinkRuntimeCallback());
155
156    if (exec == NULL) {
157        ALOGE("bcc: FAILS to prepare executable for '%s'", resName);
158        mCtx->unlockMutex();
159        return false;
160    }
161
162    mExecutable = exec;
163
164    exec->setThreadable(mIsThreadable);
165    if (!exec->syncInfo()) {
166        ALOGW("bcc: FAILS to synchronize the RS info file to the disk");
167    }
168
169    mRoot = reinterpret_cast<int (*)()>(exec->getSymbolAddress("root"));
170    mRootExpand =
171        reinterpret_cast<int (*)()>(exec->getSymbolAddress("root.expand"));
172    mInit = reinterpret_cast<void (*)()>(exec->getSymbolAddress("init"));
173    mFreeChildren =
174        reinterpret_cast<void (*)()>(exec->getSymbolAddress(".rs.dtor"));
175
176
177    const bcc::RSInfo *info = &mExecutable->getInfo();
178    if (info->getExportVarNames().size()) {
179        mBoundAllocs = new Allocation *[info->getExportVarNames().size()];
180        memset(mBoundAllocs, 0, sizeof(void *) * info->getExportVarNames().size());
181    }
182
183#else
184
185#ifndef RS_SERVER
186    String8 scriptSOName(cacheDir);
187    scriptSOName = scriptSOName.getPathDir();
188    scriptSOName.appendPath("lib");
189    scriptSOName.append("/librs.");
190#else
191    String8 scriptSOName("lib");
192#endif
193    scriptSOName.append(resName);
194    scriptSOName.append(".so");
195
196    //script->mHal.drv = drv;
197
198    //ALOGV("Opening up shared object: %s", scriptSOName.string());
199    mScriptSO = dlopen(scriptSOName.string(), RTLD_NOW | RTLD_LOCAL);
200    if (mScriptSO == NULL) {
201        ALOGE("Unable to open shared library (%s): %s",
202              scriptSOName.string(), dlerror());
203
204        // One final attempt to find the library in "/system/lib".
205        // We do this to allow bundled applications to use the compatibility
206        // library fallback path. Those applications don't have a private
207        // library path, so they need to install to the system directly.
208        String8 scriptSONameSystem("/system/lib/librs.");
209        scriptSONameSystem.append(resName);
210        scriptSONameSystem.append(".so");
211        mScriptSO = dlopen(scriptSONameSystem.string(), RTLD_NOW | RTLD_LOCAL);
212        if (mScriptSO == NULL) {
213            ALOGE("Unable to open system shared library (%s): %s",
214                  scriptSONameSystem.string(), dlerror());
215            goto error;
216        }
217    }
218
219    if (mScriptSO) {
220        char line[MAXLINE];
221        mRoot = (RootFunc_t) dlsym(mScriptSO, "root");
222        if (mRoot) {
223            //ALOGE("Found root(): %p", mRoot);
224        }
225        mRootExpand = (RootFunc_t) dlsym(mScriptSO, "root.expand");
226        if (mRootExpand) {
227            //ALOGE("Found root.expand(): %p", mRootExpand);
228        }
229        mInit = (InvokeFunc_t) dlsym(mScriptSO, "init");
230        if (mInit) {
231            //ALOGE("Found init(): %p", mInit);
232        }
233        mFreeChildren = (InvokeFunc_t) dlsym(mScriptSO, ".rs.dtor");
234        if (mFreeChildren) {
235            //ALOGE("Found .rs.dtor(): %p", mFreeChildren);
236        }
237
238        const char *rsInfo = (const char *) dlsym(mScriptSO, ".rs.info");
239        if (rsInfo) {
240            //ALOGE("Found .rs.info(): %p - %s", rsInfo, rsInfo);
241        }
242
243        size_t varCount = 0;
244        if (strgets(line, MAXLINE, &rsInfo) == NULL) {
245            goto error;
246        }
247        if (sscanf(line, EXPORT_VAR_STR "%zu", &varCount) != 1) {
248            ALOGE("Invalid export var count!: %s", line);
249            goto error;
250        }
251
252        mExportedVariableCount = varCount;
253        //ALOGE("varCount: %zu", varCount);
254        if (varCount > 0) {
255            // Start by creating/zeroing this member, since we don't want to
256            // accidentally clean up invalid pointers later (if we error out).
257            mFieldIsObject = new bool[varCount];
258            if (mFieldIsObject == NULL) {
259                goto error;
260            }
261            memset(mFieldIsObject, 0, varCount * sizeof(*mFieldIsObject));
262            mFieldAddress = new void*[varCount];
263            if (mFieldAddress == NULL) {
264                goto error;
265            }
266            for (size_t i = 0; i < varCount; ++i) {
267                if (strgets(line, MAXLINE, &rsInfo) == NULL) {
268                    goto error;
269                }
270                char *c = strrchr(line, '\n');
271                if (c) {
272                    *c = '\0';
273                }
274                mFieldAddress[i] = dlsym(mScriptSO, line);
275                if (mFieldAddress[i] == NULL) {
276                    ALOGE("Failed to find variable address for %s: %s",
277                          line, dlerror());
278                    // Not a critical error if we don't find a global variable.
279                }
280                else {
281                    //ALOGE("Found variable %s at %p", line,
282                    //mFieldAddress[i]);
283                }
284            }
285        }
286
287        size_t funcCount = 0;
288        if (strgets(line, MAXLINE, &rsInfo) == NULL) {
289            goto error;
290        }
291        if (sscanf(line, EXPORT_FUNC_STR "%zu", &funcCount) != 1) {
292            ALOGE("Invalid export func count!: %s", line);
293            goto error;
294        }
295
296        mExportedFunctionCount = funcCount;
297        //ALOGE("funcCount: %zu", funcCount);
298
299        if (funcCount > 0) {
300            mInvokeFunctions = new InvokeFunc_t[funcCount];
301            if (mInvokeFunctions == NULL) {
302                goto error;
303            }
304            for (size_t i = 0; i < funcCount; ++i) {
305                if (strgets(line, MAXLINE, &rsInfo) == NULL) {
306                    goto error;
307                }
308                char *c = strrchr(line, '\n');
309                if (c) {
310                    *c = '\0';
311                }
312
313                mInvokeFunctions[i] = (InvokeFunc_t) dlsym(mScriptSO, line);
314                if (mInvokeFunctions[i] == NULL) {
315                    ALOGE("Failed to get function address for %s(): %s",
316                          line, dlerror());
317                    goto error;
318                }
319                else {
320                    //ALOGE("Found InvokeFunc_t %s at %p", line, mInvokeFunctions[i]);
321                }
322            }
323        }
324
325        size_t forEachCount = 0;
326        if (strgets(line, MAXLINE, &rsInfo) == NULL) {
327            goto error;
328        }
329        if (sscanf(line, EXPORT_FOREACH_STR "%zu", &forEachCount) != 1) {
330            ALOGE("Invalid export forEach count!: %s", line);
331            goto error;
332        }
333
334        if (forEachCount > 0) {
335
336            mForEachSignatures = new uint32_t[forEachCount];
337            if (mForEachSignatures == NULL) {
338                goto error;
339            }
340            mForEachFunctions = new ForEachFunc_t[forEachCount];
341            if (mForEachFunctions == NULL) {
342                goto error;
343            }
344            for (size_t i = 0; i < forEachCount; ++i) {
345                unsigned int tmpSig = 0;
346                char tmpName[MAXLINE];
347
348                if (strgets(line, MAXLINE, &rsInfo) == NULL) {
349                    goto error;
350                }
351                if (sscanf(line, "%u - %" MAKE_STR(MAXLINE) "s",
352                           &tmpSig, tmpName) != 2) {
353                    ALOGE("Invalid export forEach!: %s", line);
354                    goto error;
355                }
356
357                // Lookup the expanded ForEach kernel.
358                strncat(tmpName, ".expand", MAXLINE-1-strlen(tmpName));
359                mForEachSignatures[i] = tmpSig;
360                mForEachFunctions[i] =
361                        (ForEachFunc_t) dlsym(mScriptSO, tmpName);
362                if (i != 0 && mForEachFunctions[i] == NULL) {
363                    // Ignore missing root.expand functions.
364                    // root() is always specified at location 0.
365                    ALOGE("Failed to find forEach function address for %s: %s",
366                          tmpName, dlerror());
367                    goto error;
368                }
369                else {
370                    //ALOGE("Found forEach %s at %p", tmpName, mForEachFunctions[i]);
371                }
372            }
373        }
374
375        size_t objectSlotCount = 0;
376        if (strgets(line, MAXLINE, &rsInfo) == NULL) {
377            goto error;
378        }
379        if (sscanf(line, OBJECT_SLOT_STR "%zu", &objectSlotCount) != 1) {
380            ALOGE("Invalid object slot count!: %s", line);
381            goto error;
382        }
383
384        if (objectSlotCount > 0) {
385            rsAssert(varCount > 0);
386            for (size_t i = 0; i < objectSlotCount; ++i) {
387                uint32_t varNum = 0;
388                if (strgets(line, MAXLINE, &rsInfo) == NULL) {
389                    goto error;
390                }
391                if (sscanf(line, "%u", &varNum) != 1) {
392                    ALOGE("Invalid object slot!: %s", line);
393                    goto error;
394                }
395
396                if (varNum < varCount) {
397                    mFieldIsObject[varNum] = true;
398                }
399            }
400        }
401
402        if (varCount > 0) {
403            mBoundAllocs = new Allocation *[varCount];
404            memset(mBoundAllocs, 0, varCount * sizeof(*mBoundAllocs));
405        }
406
407        if (mScriptSO == (void*)1) {
408            //rsdLookupRuntimeStub(script, "acos");
409        }
410    }
411#endif
412
413    mCtx->unlockMutex();
414    return true;
415
416#ifdef RS_COMPATIBILITY_LIB
417error:
418
419    mCtx->unlockMutex();
420    delete[] mInvokeFunctions;
421    delete[] mForEachFunctions;
422    delete[] mFieldAddress;
423    delete[] mFieldIsObject;
424    delete[] mForEachSignatures;
425    delete[] mBoundAllocs;
426    if (mScriptSO) {
427        dlclose(mScriptSO);
428    }
429    return false;
430#endif
431}
432
433void RsdCpuScriptImpl::populateScript(Script *script) {
434#ifndef RS_COMPATIBILITY_LIB
435    const bcc::RSInfo *info = &mExecutable->getInfo();
436
437    // Copy info over to runtime
438    script->mHal.info.exportedFunctionCount = info->getExportFuncNames().size();
439    script->mHal.info.exportedVariableCount = info->getExportVarNames().size();
440    script->mHal.info.exportedPragmaCount = info->getPragmas().size();
441    script->mHal.info.exportedPragmaKeyList =
442        const_cast<const char**>(mExecutable->getPragmaKeys().array());
443    script->mHal.info.exportedPragmaValueList =
444        const_cast<const char**>(mExecutable->getPragmaValues().array());
445
446    if (mRootExpand) {
447        script->mHal.info.root = mRootExpand;
448    } else {
449        script->mHal.info.root = mRoot;
450    }
451#else
452    // Copy info over to runtime
453    script->mHal.info.exportedFunctionCount = mExportedFunctionCount;
454    script->mHal.info.exportedVariableCount = mExportedVariableCount;
455    script->mHal.info.exportedPragmaCount = 0;
456    script->mHal.info.exportedPragmaKeyList = 0;
457    script->mHal.info.exportedPragmaValueList = 0;
458
459    // Bug, need to stash in metadata
460    if (mRootExpand) {
461        script->mHal.info.root = mRootExpand;
462    } else {
463        script->mHal.info.root = mRoot;
464    }
465#endif
466}
467
468
469typedef void (*rs_t)(const void *, void *, const void *, uint32_t, uint32_t, uint32_t, uint32_t);
470
471void RsdCpuScriptImpl::forEachMtlsSetup(const Allocation * ain, Allocation * aout,
472                                        const void * usr, uint32_t usrLen,
473                                        const RsScriptCall *sc,
474                                        MTLaunchStruct *mtls) {
475
476    memset(mtls, 0, sizeof(MTLaunchStruct));
477
478    // possible for this to occur if IO_OUTPUT/IO_INPUT with no bound surface
479    if (ain && (const uint8_t *)ain->mHal.drvState.lod[0].mallocPtr == NULL) {
480        mCtx->getContext()->setError(RS_ERROR_BAD_SCRIPT, "rsForEach called with null allocations");
481        return;
482    }
483    if (aout && (const uint8_t *)aout->mHal.drvState.lod[0].mallocPtr == NULL) {
484        mCtx->getContext()->setError(RS_ERROR_BAD_SCRIPT, "rsForEach called with null allocations");
485        return;
486    }
487
488    if (ain) {
489        mtls->fep.dimX = ain->getType()->getDimX();
490        mtls->fep.dimY = ain->getType()->getDimY();
491        mtls->fep.dimZ = ain->getType()->getDimZ();
492        //mtls->dimArray = ain->getType()->getDimArray();
493    } else if (aout) {
494        mtls->fep.dimX = aout->getType()->getDimX();
495        mtls->fep.dimY = aout->getType()->getDimY();
496        mtls->fep.dimZ = aout->getType()->getDimZ();
497        //mtls->dimArray = aout->getType()->getDimArray();
498    } else {
499        mCtx->getContext()->setError(RS_ERROR_BAD_SCRIPT, "rsForEach called with null allocations");
500        return;
501    }
502
503    if (!sc || (sc->xEnd == 0)) {
504        mtls->xEnd = mtls->fep.dimX;
505    } else {
506        rsAssert(sc->xStart < mtls->fep.dimX);
507        rsAssert(sc->xEnd <= mtls->fep.dimX);
508        rsAssert(sc->xStart < sc->xEnd);
509        mtls->xStart = rsMin(mtls->fep.dimX, sc->xStart);
510        mtls->xEnd = rsMin(mtls->fep.dimX, sc->xEnd);
511        if (mtls->xStart >= mtls->xEnd) return;
512    }
513
514    if (!sc || (sc->yEnd == 0)) {
515        mtls->yEnd = mtls->fep.dimY;
516    } else {
517        rsAssert(sc->yStart < mtls->fep.dimY);
518        rsAssert(sc->yEnd <= mtls->fep.dimY);
519        rsAssert(sc->yStart < sc->yEnd);
520        mtls->yStart = rsMin(mtls->fep.dimY, sc->yStart);
521        mtls->yEnd = rsMin(mtls->fep.dimY, sc->yEnd);
522        if (mtls->yStart >= mtls->yEnd) return;
523    }
524
525    if (!sc || (sc->zEnd == 0)) {
526        mtls->zEnd = mtls->fep.dimZ;
527    } else {
528        rsAssert(sc->zStart < mtls->fep.dimZ);
529        rsAssert(sc->zEnd <= mtls->fep.dimZ);
530        rsAssert(sc->zStart < sc->zEnd);
531        mtls->zStart = rsMin(mtls->fep.dimZ, sc->zStart);
532        mtls->zEnd = rsMin(mtls->fep.dimZ, sc->zEnd);
533        if (mtls->zStart >= mtls->zEnd) return;
534    }
535
536    mtls->xEnd = rsMax((uint32_t)1, mtls->xEnd);
537    mtls->yEnd = rsMax((uint32_t)1, mtls->yEnd);
538    mtls->zEnd = rsMax((uint32_t)1, mtls->zEnd);
539    mtls->arrayEnd = rsMax((uint32_t)1, mtls->arrayEnd);
540
541    rsAssert(!ain || (ain->getType()->getDimZ() == 0));
542
543    mtls->rsc = mCtx;
544    mtls->ain = ain;
545    mtls->aout = aout;
546    mtls->fep.usr = usr;
547    mtls->fep.usrLen = usrLen;
548    mtls->mSliceSize = 1;
549    mtls->mSliceNum = 0;
550
551    mtls->fep.ptrIn = NULL;
552    mtls->fep.eStrideIn = 0;
553    mtls->isThreadable = mIsThreadable;
554
555    if (ain) {
556        mtls->fep.ptrIn = (const uint8_t *)ain->mHal.drvState.lod[0].mallocPtr;
557        mtls->fep.eStrideIn = ain->getType()->getElementSizeBytes();
558        mtls->fep.yStrideIn = ain->mHal.drvState.lod[0].stride;
559    }
560
561    mtls->fep.ptrOut = NULL;
562    mtls->fep.eStrideOut = 0;
563    if (aout) {
564        mtls->fep.ptrOut = (uint8_t *)aout->mHal.drvState.lod[0].mallocPtr;
565        mtls->fep.eStrideOut = aout->getType()->getElementSizeBytes();
566        mtls->fep.yStrideOut = aout->mHal.drvState.lod[0].stride;
567    }
568}
569
570
571void RsdCpuScriptImpl::invokeForEach(uint32_t slot,
572                                     const Allocation * ain,
573                                     Allocation * aout,
574                                     const void * usr,
575                                     uint32_t usrLen,
576                                     const RsScriptCall *sc) {
577
578    MTLaunchStruct mtls;
579    forEachMtlsSetup(ain, aout, usr, usrLen, sc, &mtls);
580    forEachKernelSetup(slot, &mtls);
581
582    RsdCpuScriptImpl * oldTLS = mCtx->setTLS(this);
583    mCtx->launchThreads(ain, aout, sc, &mtls);
584    mCtx->setTLS(oldTLS);
585}
586
587void RsdCpuScriptImpl::forEachKernelSetup(uint32_t slot, MTLaunchStruct *mtls) {
588    mtls->script = this;
589    mtls->fep.slot = slot;
590#ifndef RS_COMPATIBILITY_LIB
591    rsAssert(slot < mExecutable->getExportForeachFuncAddrs().size());
592    mtls->kernel = reinterpret_cast<ForEachFunc_t>(
593                      mExecutable->getExportForeachFuncAddrs()[slot]);
594    rsAssert(mtls->kernel != NULL);
595    mtls->sig = mExecutable->getInfo().getExportForeachFuncs()[slot].second;
596#else
597    mtls->kernel = reinterpret_cast<ForEachFunc_t>(mForEachFunctions[slot]);
598    rsAssert(mtls->kernel != NULL);
599    mtls->sig = mForEachSignatures[slot];
600#endif
601}
602
603int RsdCpuScriptImpl::invokeRoot() {
604    RsdCpuScriptImpl * oldTLS = mCtx->setTLS(this);
605    int ret = mRoot();
606    mCtx->setTLS(oldTLS);
607    return ret;
608}
609
610void RsdCpuScriptImpl::invokeInit() {
611    if (mInit) {
612        mInit();
613    }
614}
615
616void RsdCpuScriptImpl::invokeFreeChildren() {
617    if (mFreeChildren) {
618        mFreeChildren();
619    }
620}
621
622void RsdCpuScriptImpl::invokeFunction(uint32_t slot, const void *params,
623                                      size_t paramLength) {
624    //ALOGE("invoke %p %p %i %p %i", dc, script, slot, params, paramLength);
625
626    RsdCpuScriptImpl * oldTLS = mCtx->setTLS(this);
627    reinterpret_cast<void (*)(const void *, uint32_t)>(
628#ifndef RS_COMPATIBILITY_LIB
629        mExecutable->getExportFuncAddrs()[slot])(params, paramLength);
630#else
631        mInvokeFunctions[slot])(params, paramLength);
632#endif
633    mCtx->setTLS(oldTLS);
634}
635
636void RsdCpuScriptImpl::setGlobalVar(uint32_t slot, const void *data, size_t dataLength) {
637    //rsAssert(!script->mFieldIsObject[slot]);
638    //ALOGE("setGlobalVar %p %p %i %p %i", dc, script, slot, data, dataLength);
639
640    //if (mIntrinsicID) {
641        //mIntrinsicFuncs.setVar(dc, script, drv->mIntrinsicData, slot, data, dataLength);
642        //return;
643    //}
644
645#ifndef RS_COMPATIBILITY_LIB
646    int32_t *destPtr = reinterpret_cast<int32_t *>(
647                          mExecutable->getExportVarAddrs()[slot]);
648#else
649    int32_t *destPtr = reinterpret_cast<int32_t *>(mFieldAddress[slot]);
650#endif
651    if (!destPtr) {
652        //ALOGV("Calling setVar on slot = %i which is null", slot);
653        return;
654    }
655
656    memcpy(destPtr, data, dataLength);
657}
658
659void RsdCpuScriptImpl::setGlobalVarWithElemDims(uint32_t slot, const void *data, size_t dataLength,
660                                                const Element *elem,
661                                                const size_t *dims, size_t dimLength) {
662
663#ifndef RS_COMPATIBILITY_LIB
664    int32_t *destPtr = reinterpret_cast<int32_t *>(
665        mExecutable->getExportVarAddrs()[slot]);
666#else
667    int32_t *destPtr = reinterpret_cast<int32_t *>(mFieldAddress[slot]);
668#endif
669    if (!destPtr) {
670        //ALOGV("Calling setVar on slot = %i which is null", slot);
671        return;
672    }
673
674    // We want to look at dimension in terms of integer components,
675    // but dimLength is given in terms of bytes.
676    dimLength /= sizeof(int);
677
678    // Only a single dimension is currently supported.
679    rsAssert(dimLength == 1);
680    if (dimLength == 1) {
681        // First do the increment loop.
682        size_t stride = elem->getSizeBytes();
683        const char *cVal = reinterpret_cast<const char *>(data);
684        for (size_t i = 0; i < dims[0]; i++) {
685            elem->incRefs(cVal);
686            cVal += stride;
687        }
688
689        // Decrement loop comes after (to prevent race conditions).
690        char *oldVal = reinterpret_cast<char *>(destPtr);
691        for (size_t i = 0; i < dims[0]; i++) {
692            elem->decRefs(oldVal);
693            oldVal += stride;
694        }
695    }
696
697    memcpy(destPtr, data, dataLength);
698}
699
700void RsdCpuScriptImpl::setGlobalBind(uint32_t slot, Allocation *data) {
701
702    //rsAssert(!script->mFieldIsObject[slot]);
703    //ALOGE("setGlobalBind %p %p %i %p", dc, script, slot, data);
704
705#ifndef RS_COMPATIBILITY_LIB
706    int32_t *destPtr = reinterpret_cast<int32_t *>(
707                          mExecutable->getExportVarAddrs()[slot]);
708#else
709    int32_t *destPtr = reinterpret_cast<int32_t *>(mFieldAddress[slot]);
710#endif
711    if (!destPtr) {
712        //ALOGV("Calling setVar on slot = %i which is null", slot);
713        return;
714    }
715
716    void *ptr = NULL;
717    mBoundAllocs[slot] = data;
718    if(data) {
719        ptr = data->mHal.drvState.lod[0].mallocPtr;
720    }
721    memcpy(destPtr, &ptr, sizeof(void *));
722}
723
724void RsdCpuScriptImpl::setGlobalObj(uint32_t slot, ObjectBase *data) {
725
726    //rsAssert(script->mFieldIsObject[slot]);
727    //ALOGE("setGlobalObj %p %p %i %p", dc, script, slot, data);
728
729    //if (mIntrinsicID) {
730        //mIntrinsicFuncs.setVarObj(dc, script, drv->mIntrinsicData, slot, alloc);
731        //return;
732    //}
733
734#ifndef RS_COMPATIBILITY_LIB
735    int32_t *destPtr = reinterpret_cast<int32_t *>(
736                          mExecutable->getExportVarAddrs()[slot]);
737#else
738    int32_t *destPtr = reinterpret_cast<int32_t *>(mFieldAddress[slot]);
739#endif
740    if (!destPtr) {
741        //ALOGV("Calling setVar on slot = %i which is null", slot);
742        return;
743    }
744
745    rsrSetObject(mCtx->getContext(), (ObjectBase **)destPtr, data);
746}
747
748RsdCpuScriptImpl::~RsdCpuScriptImpl() {
749#ifndef RS_COMPATIBILITY_LIB
750    if (mExecutable) {
751        Vector<void *>::const_iterator var_addr_iter =
752            mExecutable->getExportVarAddrs().begin();
753        Vector<void *>::const_iterator var_addr_end =
754            mExecutable->getExportVarAddrs().end();
755
756        bcc::RSInfo::ObjectSlotListTy::const_iterator is_object_iter =
757            mExecutable->getInfo().getObjectSlots().begin();
758        bcc::RSInfo::ObjectSlotListTy::const_iterator is_object_end =
759            mExecutable->getInfo().getObjectSlots().end();
760
761        while ((var_addr_iter != var_addr_end) &&
762               (is_object_iter != is_object_end)) {
763            // The field address can be NULL if the script-side has optimized
764            // the corresponding global variable away.
765            ObjectBase **obj_addr =
766                reinterpret_cast<ObjectBase **>(*var_addr_iter);
767            if (*is_object_iter) {
768                if (*var_addr_iter != NULL) {
769                    rsrClearObject(mCtx->getContext(), obj_addr);
770                }
771            }
772            var_addr_iter++;
773            is_object_iter++;
774        }
775    }
776
777    if (mCompilerContext) {
778        delete mCompilerContext;
779    }
780    if (mCompilerDriver) {
781        delete mCompilerDriver;
782    }
783    if (mExecutable) {
784        delete mExecutable;
785    }
786    if (mBoundAllocs) {
787        delete[] mBoundAllocs;
788    }
789#else
790    if (mFieldIsObject) {
791        for (size_t i = 0; i < mExportedVariableCount; ++i) {
792            if (mFieldIsObject[i]) {
793                if (mFieldAddress[i] != NULL) {
794                    ObjectBase **obj_addr =
795                        reinterpret_cast<ObjectBase **>(mFieldAddress[i]);
796                    rsrClearObject(mCtx->getContext(), obj_addr);
797                }
798            }
799        }
800    }
801
802    if (mInvokeFunctions) delete[] mInvokeFunctions;
803    if (mForEachFunctions) delete[] mForEachFunctions;
804    if (mFieldAddress) delete[] mFieldAddress;
805    if (mFieldIsObject) delete[] mFieldIsObject;
806    if (mForEachSignatures) delete[] mForEachSignatures;
807    if (mBoundAllocs) delete[] mBoundAllocs;
808    if (mScriptSO) {
809        dlclose(mScriptSO);
810    }
811#endif
812}
813
814Allocation * RsdCpuScriptImpl::getAllocationForPointer(const void *ptr) const {
815    if (!ptr) {
816        return NULL;
817    }
818
819    for (uint32_t ct=0; ct < mScript->mHal.info.exportedVariableCount; ct++) {
820        Allocation *a = mBoundAllocs[ct];
821        if (!a) continue;
822        if (a->mHal.drvState.lod[0].mallocPtr == ptr) {
823            return a;
824        }
825    }
826    ALOGE("rsGetAllocation, failed to find %p", ptr);
827    return NULL;
828}
829
830
831}
832}
833