rsdBcc.cpp revision 78b050ea1e13108110ce3b8ead63252e5ebe2468
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#include "rsdCore.h"
18
19#include <bcc/BCCContext.h>
20#include <bcc/Renderscript/RSCompilerDriver.h>
21#include <bcc/Renderscript/RSExecutable.h>
22#include <bcc/Renderscript/RSInfo.h>
23
24#include "rsdBcc.h"
25#include "rsdRuntime.h"
26#include "rsdAllocation.h"
27#include "rsdIntrinsics.h"
28
29#include "rsContext.h"
30#include "rsElement.h"
31#include "rsScriptC.h"
32
33#include "utils/Vector.h"
34#include "utils/Timers.h"
35#include "utils/StopWatch.h"
36
37using namespace android;
38using namespace android::renderscript;
39
40
41static Script * setTLS(Script *sc) {
42    ScriptTLSStruct * tls = (ScriptTLSStruct *)pthread_getspecific(rsdgThreadTLSKey);
43    rsAssert(tls);
44    Script *old = tls->mScript;
45    tls->mScript = sc;
46    return old;
47}
48
49
50bool rsdScriptInit(const Context *rsc,
51                     ScriptC *script,
52                     char const *resName,
53                     char const *cacheDir,
54                     uint8_t const *bitcode,
55                     size_t bitcodeSize,
56                     uint32_t flags) {
57    //ALOGE("rsdScriptCreate %p %p %p %p %i %i %p", rsc, resName, cacheDir, bitcode, bitcodeSize, flags, lookupFunc);
58    //ALOGE("rsdScriptInit %p %p", rsc, script);
59
60    pthread_mutex_lock(&rsdgInitMutex);
61
62    bcc::RSExecutable *exec;
63    const bcc::RSInfo *info;
64    DrvScript *drv = (DrvScript *)calloc(1, sizeof(DrvScript));
65    if (drv == NULL) {
66        goto error;
67    }
68    script->mHal.drv = drv;
69
70    drv->mCompilerContext = NULL;
71    drv->mCompilerDriver = NULL;
72    drv->mExecutable = NULL;
73
74    drv->mCompilerContext = new bcc::BCCContext();
75    if (drv->mCompilerContext == NULL) {
76        ALOGE("bcc: FAILS to create compiler context (out of memory)");
77        goto error;
78    }
79
80    drv->mCompilerDriver = new bcc::RSCompilerDriver();
81    if (drv->mCompilerDriver == NULL) {
82        ALOGE("bcc: FAILS to create compiler driver (out of memory)");
83        goto error;
84    }
85
86    script->mHal.info.isThreadable = true;
87
88    drv->mCompilerDriver->setRSRuntimeLookupFunction(rsdLookupRuntimeStub);
89    drv->mCompilerDriver->setRSRuntimeLookupContext(script);
90
91    exec = drv->mCompilerDriver->build(*drv->mCompilerContext,
92                                       cacheDir, resName,
93                                       (const char *)bitcode, bitcodeSize);
94
95    if (exec == NULL) {
96        ALOGE("bcc: FAILS to prepare executable for '%s'", resName);
97        goto error;
98    }
99
100    drv->mExecutable = exec;
101
102    exec->setThreadable(script->mHal.info.isThreadable);
103    if (!exec->syncInfo()) {
104        ALOGW("bcc: FAILS to synchronize the RS info file to the disk");
105    }
106
107    drv->mRoot = reinterpret_cast<int (*)()>(exec->getSymbolAddress("root"));
108    drv->mRootExpand =
109        reinterpret_cast<int (*)()>(exec->getSymbolAddress("root.expand"));
110    drv->mInit = reinterpret_cast<void (*)()>(exec->getSymbolAddress("init"));
111    drv->mFreeChildren =
112        reinterpret_cast<void (*)()>(exec->getSymbolAddress(".rs.dtor"));
113
114    info = &drv->mExecutable->getInfo();
115    // Copy info over to runtime
116    script->mHal.info.exportedFunctionCount = info->getExportFuncNames().size();
117    script->mHal.info.exportedVariableCount = info->getExportVarNames().size();
118    script->mHal.info.exportedPragmaCount = info->getPragmas().size();
119    script->mHal.info.exportedPragmaKeyList =
120        const_cast<const char**>(exec->getPragmaKeys().array());
121    script->mHal.info.exportedPragmaValueList =
122        const_cast<const char**>(exec->getPragmaValues().array());
123
124    if (drv->mRootExpand) {
125        script->mHal.info.root = drv->mRootExpand;
126    } else {
127        script->mHal.info.root = drv->mRoot;
128    }
129
130    if (script->mHal.info.exportedVariableCount) {
131        drv->mBoundAllocs = new Allocation *[script->mHal.info.exportedVariableCount];
132        memset(drv->mBoundAllocs, 0, sizeof(void *) * script->mHal.info.exportedVariableCount);
133    }
134
135    pthread_mutex_unlock(&rsdgInitMutex);
136    return true;
137
138error:
139
140    pthread_mutex_unlock(&rsdgInitMutex);
141    if (drv) {
142        delete drv->mCompilerContext;
143        delete drv->mCompilerDriver;
144        delete drv->mExecutable;
145        delete[] drv->mBoundAllocs;
146        free(drv);
147    }
148    script->mHal.drv = NULL;
149    return false;
150
151}
152
153bool rsdInitIntrinsic(const Context *rsc, Script *s, RsScriptIntrinsicID iid, Element *e) {
154    pthread_mutex_lock(&rsdgInitMutex);
155
156    DrvScript *drv = (DrvScript *)calloc(1, sizeof(DrvScript));
157    if (drv == NULL) {
158        goto error;
159    }
160    s->mHal.drv = drv;
161    drv->mIntrinsicID = iid;
162    drv->mIntrinsicData = rsdIntrinsic_Init(rsc, s, iid, &drv->mIntrinsicFuncs);
163    s->mHal.info.isThreadable = true;
164
165    pthread_mutex_unlock(&rsdgInitMutex);
166    return true;
167
168error:
169    pthread_mutex_unlock(&rsdgInitMutex);
170    return false;
171}
172
173typedef void (*rs_t)(const void *, void *, const void *, uint32_t, uint32_t, uint32_t, uint32_t);
174
175static void wc_xy(void *usr, uint32_t idx) {
176    MTLaunchStruct *mtls = (MTLaunchStruct *)usr;
177    RsForEachStubParamStruct p;
178    memcpy(&p, &mtls->fep, sizeof(p));
179    RsdHal * dc = (RsdHal *)mtls->rsc->mHal.drv;
180    uint32_t sig = mtls->sig;
181
182#if defined(ARCH_ARM_RS_USE_CACHED_SCANLINE_WRITE)
183    unsigned char buf[1024 * 8];
184#endif
185
186    outer_foreach_t fn = (outer_foreach_t) mtls->kernel;
187    while (1) {
188        uint32_t slice = (uint32_t)android_atomic_inc(&mtls->mSliceNum);
189        uint32_t yStart = mtls->yStart + slice * mtls->mSliceSize;
190        uint32_t yEnd = yStart + mtls->mSliceSize;
191        yEnd = rsMin(yEnd, mtls->yEnd);
192        if (yEnd <= yStart) {
193            return;
194        }
195
196        //ALOGE("usr idx %i, x %i,%i  y %i,%i", idx, mtls->xStart, mtls->xEnd, yStart, yEnd);
197        //ALOGE("usr ptr in %p,  out %p", mtls->ptrIn, mtls->ptrOut);
198
199#if defined(ARCH_ARM_RS_USE_CACHED_SCANLINE_WRITE)
200        if (mtls->fep.yStrideOut < sizeof(buf)) {
201            p.out = buf;
202            for (p.y = yStart; p.y < yEnd; p.y++) {
203                p.in = mtls->fep.ptrIn + (mtls->fep.yStrideIn * p.y);
204                fn(&p, mtls->xStart, mtls->xEnd, mtls->fep.eStrideIn, mtls->fep.eStrideOut);
205                memcpy(mtls->fep.ptrOut + (mtls->fep.yStrideOut * p.y), buf, mtls->fep.yStrideOut);
206            }
207        } else
208#endif
209            {
210            for (p.y = yStart; p.y < yEnd; p.y++) {
211                p.out = mtls->fep.ptrOut + (mtls->fep.yStrideOut * p.y);
212                p.in = mtls->fep.ptrIn + (mtls->fep.yStrideIn * p.y);
213                fn(&p, mtls->xStart, mtls->xEnd, mtls->fep.eStrideIn, mtls->fep.eStrideOut);
214            }
215        }
216    }
217}
218
219static void wc_x(void *usr, uint32_t idx) {
220    MTLaunchStruct *mtls = (MTLaunchStruct *)usr;
221    RsForEachStubParamStruct p;
222    memcpy(&p, &mtls->fep, sizeof(p));
223    RsdHal * dc = (RsdHal *)mtls->rsc->mHal.drv;
224    uint32_t sig = mtls->sig;
225
226    outer_foreach_t fn = (outer_foreach_t) mtls->kernel;
227    while (1) {
228        uint32_t slice = (uint32_t)android_atomic_inc(&mtls->mSliceNum);
229        uint32_t xStart = mtls->xStart + slice * mtls->mSliceSize;
230        uint32_t xEnd = xStart + mtls->mSliceSize;
231        xEnd = rsMin(xEnd, mtls->xEnd);
232        if (xEnd <= xStart) {
233            return;
234        }
235
236        //ALOGE("usr slice %i idx %i, x %i,%i", slice, idx, xStart, xEnd);
237        //ALOGE("usr ptr in %p,  out %p", mtls->ptrIn, mtls->ptrOut);
238
239        p.out = mtls->fep.ptrOut + (mtls->fep.eStrideOut * xStart);
240        p.in = mtls->fep.ptrIn + (mtls->fep.eStrideIn * xStart);
241        fn(&p, xStart, xEnd, mtls->fep.eStrideIn, mtls->fep.eStrideOut);
242    }
243}
244
245void rsdScriptInvokeForEachMtlsSetup(const Context *rsc,
246                                     const Allocation * ain,
247                                     Allocation * aout,
248                                     const void * usr,
249                                     uint32_t usrLen,
250                                     const RsScriptCall *sc,
251                                     MTLaunchStruct *mtls) {
252
253    memset(mtls, 0, sizeof(MTLaunchStruct));
254
255    if (ain) {
256        mtls->fep.dimX = ain->getType()->getDimX();
257        mtls->fep.dimY = ain->getType()->getDimY();
258        mtls->fep.dimZ = ain->getType()->getDimZ();
259        //mtls->dimArray = ain->getType()->getDimArray();
260    } else if (aout) {
261        mtls->fep.dimX = aout->getType()->getDimX();
262        mtls->fep.dimY = aout->getType()->getDimY();
263        mtls->fep.dimZ = aout->getType()->getDimZ();
264        //mtls->dimArray = aout->getType()->getDimArray();
265    } else {
266        rsc->setError(RS_ERROR_BAD_SCRIPT, "rsForEach called with null allocations");
267        return;
268    }
269
270    if (!sc || (sc->xEnd == 0)) {
271        mtls->xEnd = mtls->fep.dimX;
272    } else {
273        rsAssert(sc->xStart < mtls->fep.dimX);
274        rsAssert(sc->xEnd <= mtls->fep.dimX);
275        rsAssert(sc->xStart < sc->xEnd);
276        mtls->xStart = rsMin(mtls->fep.dimX, sc->xStart);
277        mtls->xEnd = rsMin(mtls->fep.dimX, sc->xEnd);
278        if (mtls->xStart >= mtls->xEnd) return;
279    }
280
281    if (!sc || (sc->yEnd == 0)) {
282        mtls->yEnd = mtls->fep.dimY;
283    } else {
284        rsAssert(sc->yStart < mtls->fep.dimY);
285        rsAssert(sc->yEnd <= mtls->fep.dimY);
286        rsAssert(sc->yStart < sc->yEnd);
287        mtls->yStart = rsMin(mtls->fep.dimY, sc->yStart);
288        mtls->yEnd = rsMin(mtls->fep.dimY, sc->yEnd);
289        if (mtls->yStart >= mtls->yEnd) return;
290    }
291
292    mtls->xEnd = rsMax((uint32_t)1, mtls->xEnd);
293    mtls->yEnd = rsMax((uint32_t)1, mtls->yEnd);
294    mtls->zEnd = rsMax((uint32_t)1, mtls->zEnd);
295    mtls->arrayEnd = rsMax((uint32_t)1, mtls->arrayEnd);
296
297    rsAssert(!ain || (ain->getType()->getDimZ() == 0));
298
299    Context *mrsc = (Context *)rsc;
300    mtls->rsc = mrsc;
301    mtls->ain = ain;
302    mtls->aout = aout;
303    mtls->fep.usr = usr;
304    mtls->fep.usrLen = usrLen;
305    mtls->mSliceSize = 10;
306    mtls->mSliceNum = 0;
307
308    mtls->fep.ptrIn = NULL;
309    mtls->fep.eStrideIn = 0;
310
311    if (ain) {
312        DrvAllocation *aindrv = (DrvAllocation *)ain->mHal.drv;
313        mtls->fep.ptrIn = (const uint8_t *)aindrv->lod[0].mallocPtr;
314        mtls->fep.eStrideIn = ain->getType()->getElementSizeBytes();
315        mtls->fep.yStrideIn = aindrv->lod[0].stride;
316    }
317
318    mtls->fep.ptrOut = NULL;
319    mtls->fep.eStrideOut = 0;
320    if (aout) {
321        DrvAllocation *aoutdrv = (DrvAllocation *)aout->mHal.drv;
322        mtls->fep.ptrOut = (uint8_t *)aoutdrv->lod[0].mallocPtr;
323        mtls->fep.eStrideOut = aout->getType()->getElementSizeBytes();
324        mtls->fep.yStrideOut = aoutdrv->lod[0].stride;
325    }
326}
327
328void rsdScriptLaunchThreads(const Context *rsc,
329                            Script *s,
330                            uint32_t slot,
331                            const Allocation * ain,
332                            Allocation * aout,
333                            const void * usr,
334                            uint32_t usrLen,
335                            const RsScriptCall *sc,
336                            MTLaunchStruct *mtls) {
337
338    Script * oldTLS = setTLS(s);
339    Context *mrsc = (Context *)rsc;
340    RsdHal * dc = (RsdHal *)mtls->rsc->mHal.drv;
341
342    if ((dc->mWorkers.mCount > 1) && s->mHal.info.isThreadable && !dc->mInForEach) {
343        dc->mInForEach = true;
344        if (mtls->fep.dimY > 1) {
345            mtls->mSliceSize = mtls->fep.dimY / (dc->mWorkers.mCount * 4);
346            if(mtls->mSliceSize < 1) {
347                mtls->mSliceSize = 1;
348            }
349
350            rsdLaunchThreads(mrsc, wc_xy, mtls);
351        } else {
352            mtls->mSliceSize = mtls->fep.dimX / (dc->mWorkers.mCount * 4);
353            if(mtls->mSliceSize < 1) {
354                mtls->mSliceSize = 1;
355            }
356
357            rsdLaunchThreads(mrsc, wc_x, mtls);
358        }
359        dc->mInForEach = false;
360
361        //ALOGE("launch 1");
362    } else {
363        RsForEachStubParamStruct p;
364        memcpy(&p, &mtls->fep, sizeof(p));
365        uint32_t sig = mtls->sig;
366
367        //ALOGE("launch 3");
368        outer_foreach_t fn = (outer_foreach_t) mtls->kernel;
369        for (p.ar[0] = mtls->arrayStart; p.ar[0] < mtls->arrayEnd; p.ar[0]++) {
370            for (p.z = mtls->zStart; p.z < mtls->zEnd; p.z++) {
371                for (p.y = mtls->yStart; p.y < mtls->yEnd; p.y++) {
372                    uint32_t offset = mtls->fep.dimY * mtls->fep.dimZ * p.ar[0] +
373                                      mtls->fep.dimY * p.z + p.y;
374                    p.out = mtls->fep.ptrOut + (mtls->fep.yStrideOut * offset);
375                    p.in = mtls->fep.ptrIn + (mtls->fep.yStrideIn * offset);
376                    fn(&p, mtls->xStart, mtls->xEnd, mtls->fep.eStrideIn, mtls->fep.eStrideOut);
377                }
378            }
379        }
380    }
381
382    setTLS(oldTLS);
383}
384
385void rsdScriptInvokeForEach(const Context *rsc,
386                            Script *s,
387                            uint32_t slot,
388                            const Allocation * ain,
389                            Allocation * aout,
390                            const void * usr,
391                            uint32_t usrLen,
392                            const RsScriptCall *sc) {
393
394    RsdHal * dc = (RsdHal *)rsc->mHal.drv;
395
396    MTLaunchStruct mtls;
397    rsdScriptInvokeForEachMtlsSetup(rsc, ain, aout, usr, usrLen, sc, &mtls);
398    mtls.script = s;
399    mtls.fep.slot = slot;
400
401    DrvScript *drv = (DrvScript *)s->mHal.drv;
402    if (drv->mIntrinsicID) {
403        mtls.kernel = (void (*)())drv->mIntrinsicFuncs.root;
404        mtls.fep.usr = drv->mIntrinsicData;
405    } else {
406        rsAssert(slot < drv->mExecutable->getExportForeachFuncAddrs().size());
407        mtls.kernel = reinterpret_cast<ForEachFunc_t>(
408                          drv->mExecutable->getExportForeachFuncAddrs()[slot]);
409        rsAssert(mtls.kernel != NULL);
410        mtls.sig = drv->mExecutable->getInfo().getExportForeachFuncs()[slot].second;
411    }
412
413
414    rsdScriptLaunchThreads(rsc, s, slot, ain, aout, usr, usrLen, sc, &mtls);
415}
416
417
418int rsdScriptInvokeRoot(const Context *dc, Script *script) {
419    DrvScript *drv = (DrvScript *)script->mHal.drv;
420
421    Script * oldTLS = setTLS(script);
422    int ret = drv->mRoot();
423    setTLS(oldTLS);
424
425    return ret;
426}
427
428void rsdScriptInvokeInit(const Context *dc, Script *script) {
429    DrvScript *drv = (DrvScript *)script->mHal.drv;
430
431    if (drv->mInit) {
432        drv->mInit();
433    }
434}
435
436void rsdScriptInvokeFreeChildren(const Context *dc, Script *script) {
437    DrvScript *drv = (DrvScript *)script->mHal.drv;
438
439    if (drv->mFreeChildren) {
440        drv->mFreeChildren();
441    }
442}
443
444void rsdScriptInvokeFunction(const Context *dc, Script *script,
445                            uint32_t slot,
446                            const void *params,
447                            size_t paramLength) {
448    DrvScript *drv = (DrvScript *)script->mHal.drv;
449    //ALOGE("invoke %p %p %i %p %i", dc, script, slot, params, paramLength);
450
451    Script * oldTLS = setTLS(script);
452    reinterpret_cast<void (*)(const void *, uint32_t)>(
453        drv->mExecutable->getExportFuncAddrs()[slot])(params, paramLength);
454    setTLS(oldTLS);
455}
456
457void rsdScriptSetGlobalVar(const Context *dc, const Script *script,
458                           uint32_t slot, void *data, size_t dataLength) {
459    DrvScript *drv = (DrvScript *)script->mHal.drv;
460    //rsAssert(!script->mFieldIsObject[slot]);
461    //ALOGE("setGlobalVar %p %p %i %p %i", dc, script, slot, data, dataLength);
462
463    if (drv->mIntrinsicID) {
464        drv->mIntrinsicFuncs.setVar(dc, script, drv->mIntrinsicData, slot, data, dataLength);
465        return;
466    }
467
468    int32_t *destPtr = reinterpret_cast<int32_t *>(
469                          drv->mExecutable->getExportVarAddrs()[slot]);
470    if (!destPtr) {
471        //ALOGV("Calling setVar on slot = %i which is null", slot);
472        return;
473    }
474
475    memcpy(destPtr, data, dataLength);
476}
477
478void rsdScriptSetGlobalVarWithElemDims(
479        const android::renderscript::Context *dc,
480        const android::renderscript::Script *script,
481        uint32_t slot, void *data, size_t dataLength,
482        const android::renderscript::Element *elem,
483        const size_t *dims, size_t dimLength) {
484    DrvScript *drv = (DrvScript *)script->mHal.drv;
485
486    int32_t *destPtr = reinterpret_cast<int32_t *>(
487        drv->mExecutable->getExportVarAddrs()[slot]);
488    if (!destPtr) {
489        //ALOGV("Calling setVar on slot = %i which is null", slot);
490        return;
491    }
492
493    // We want to look at dimension in terms of integer components,
494    // but dimLength is given in terms of bytes.
495    dimLength /= sizeof(int);
496
497    // Only a single dimension is currently supported.
498    rsAssert(dimLength == 1);
499    if (dimLength == 1) {
500        // First do the increment loop.
501        size_t stride = elem->getSizeBytes();
502        char *cVal = reinterpret_cast<char *>(data);
503        for (size_t i = 0; i < dims[0]; i++) {
504            elem->incRefs(cVal);
505            cVal += stride;
506        }
507
508        // Decrement loop comes after (to prevent race conditions).
509        char *oldVal = reinterpret_cast<char *>(destPtr);
510        for (size_t i = 0; i < dims[0]; i++) {
511            elem->decRefs(oldVal);
512            oldVal += stride;
513        }
514    }
515
516    memcpy(destPtr, data, dataLength);
517}
518
519void rsdScriptSetGlobalBind(const Context *dc, const Script *script, uint32_t slot, Allocation *data) {
520    DrvScript *drv = (DrvScript *)script->mHal.drv;
521
522    //rsAssert(!script->mFieldIsObject[slot]);
523    //ALOGE("setGlobalBind %p %p %i %p", dc, script, slot, data);
524
525    rsAssert(!drv->mIntrinsicID);
526
527    int32_t *destPtr = reinterpret_cast<int32_t *>(
528                          drv->mExecutable->getExportVarAddrs()[slot]);
529    if (!destPtr) {
530        //ALOGV("Calling setVar on slot = %i which is null", slot);
531        return;
532    }
533
534    void *ptr = NULL;
535    drv->mBoundAllocs[slot] = data;
536    if(data) {
537        DrvAllocation *allocDrv = (DrvAllocation *)data->mHal.drv;
538        ptr = allocDrv->lod[0].mallocPtr;
539    }
540    memcpy(destPtr, &ptr, sizeof(void *));
541}
542
543void rsdScriptSetGlobalObj(const Context *dc, const Script *script, uint32_t slot, ObjectBase *data) {
544    DrvScript *drv = (DrvScript *)script->mHal.drv;
545    //rsAssert(script->mFieldIsObject[slot]);
546    //ALOGE("setGlobalObj %p %p %i %p", dc, script, slot, data);
547
548    if (drv->mIntrinsicID) {
549        drv->mIntrinsicFuncs.setVarObj(dc, script, drv->mIntrinsicData, slot,
550                                       static_cast<Allocation *>(data));
551        return;
552    }
553
554    int32_t *destPtr = reinterpret_cast<int32_t *>(
555                          drv->mExecutable->getExportVarAddrs()[slot]);
556    if (!destPtr) {
557        //ALOGV("Calling setVar on slot = %i which is null", slot);
558        return;
559    }
560
561    rsrSetObject(dc, script, (ObjectBase **)destPtr, data);
562}
563
564void rsdScriptDestroy(const Context *dc, Script *script) {
565    DrvScript *drv = (DrvScript *)script->mHal.drv;
566
567    if (drv == NULL) {
568        return;
569    }
570
571    if (drv->mExecutable) {
572        Vector<void *>::const_iterator var_addr_iter =
573            drv->mExecutable->getExportVarAddrs().begin();
574        Vector<void *>::const_iterator var_addr_end =
575            drv->mExecutable->getExportVarAddrs().end();
576
577        bcc::RSInfo::ObjectSlotListTy::const_iterator is_object_iter =
578            drv->mExecutable->getInfo().getObjectSlots().begin();
579        bcc::RSInfo::ObjectSlotListTy::const_iterator is_object_end =
580            drv->mExecutable->getInfo().getObjectSlots().end();
581
582        while ((var_addr_iter != var_addr_end) &&
583               (is_object_iter != is_object_end)) {
584            // The field address can be NULL if the script-side has optimized
585            // the corresponding global variable away.
586            ObjectBase **obj_addr =
587                reinterpret_cast<ObjectBase **>(*var_addr_iter);
588            if (*is_object_iter) {
589                if (*var_addr_iter != NULL) {
590                    rsrClearObject(dc, script, obj_addr);
591                }
592            }
593            var_addr_iter++;
594            is_object_iter++;
595        }
596    }
597
598    delete drv->mCompilerContext;
599    delete drv->mCompilerDriver;
600    delete drv->mExecutable;
601    delete[] drv->mBoundAllocs;
602    free(drv);
603    script->mHal.drv = NULL;
604}
605
606Allocation * rsdScriptGetAllocationForPointer(const android::renderscript::Context *dc,
607                                              const android::renderscript::Script *sc,
608                                              const void *ptr) {
609    DrvScript *drv = (DrvScript *)sc->mHal.drv;
610    if (!ptr) {
611        return NULL;
612    }
613
614    for (uint32_t ct=0; ct < sc->mHal.info.exportedVariableCount; ct++) {
615        Allocation *a = drv->mBoundAllocs[ct];
616        if (!a) continue;
617        DrvAllocation *adrv = (DrvAllocation *)a->mHal.drv;
618        if (adrv->lod[0].mallocPtr == ptr) {
619            return a;
620        }
621    }
622    ALOGE("rsGetAllocation, failed to find %p", ptr);
623    return NULL;
624}
625
626