rsdBcc.cpp revision bee28c6b56d0c399402014cd58cea426da2b98c1
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    p.lid = idx;
180    RsdHal * dc = (RsdHal *)mtls->rsc->mHal.drv;
181    uint32_t sig = mtls->sig;
182
183#if defined(ARCH_ARM_RS_USE_CACHED_SCANLINE_WRITE)
184    unsigned char buf[1024 * 8];
185#endif
186
187    outer_foreach_t fn = (outer_foreach_t) mtls->kernel;
188    while (1) {
189        uint32_t slice = (uint32_t)android_atomic_inc(&mtls->mSliceNum);
190        uint32_t yStart = mtls->yStart + slice * mtls->mSliceSize;
191        uint32_t yEnd = yStart + mtls->mSliceSize;
192        yEnd = rsMin(yEnd, mtls->yEnd);
193        if (yEnd <= yStart) {
194            return;
195        }
196
197        //ALOGE("usr idx %i, x %i,%i  y %i,%i", idx, mtls->xStart, mtls->xEnd, yStart, yEnd);
198        //ALOGE("usr ptr in %p,  out %p", mtls->fep.ptrIn, mtls->fep.ptrOut);
199
200#if defined(ARCH_ARM_RS_USE_CACHED_SCANLINE_WRITE)
201        if (mtls->fep.yStrideOut < sizeof(buf)) {
202            p.out = buf;
203            for (p.y = yStart; p.y < yEnd; p.y++) {
204                p.in = mtls->fep.ptrIn + (mtls->fep.yStrideIn * p.y);
205                fn(&p, mtls->xStart, mtls->xEnd, mtls->fep.eStrideIn, mtls->fep.eStrideOut);
206                memcpy(mtls->fep.ptrOut + (mtls->fep.yStrideOut * p.y), buf, mtls->fep.yStrideOut);
207            }
208        } else
209#endif
210            {
211            for (p.y = yStart; p.y < yEnd; p.y++) {
212                p.out = mtls->fep.ptrOut + (mtls->fep.yStrideOut * p.y) +
213                        (mtls->fep.eStrideOut * mtls->xStart);
214                p.in = mtls->fep.ptrIn + (mtls->fep.yStrideIn * p.y) +
215                       (mtls->fep.eStrideIn * mtls->xStart);
216                fn(&p, mtls->xStart, mtls->xEnd, mtls->fep.eStrideIn, mtls->fep.eStrideOut);
217            }
218        }
219    }
220}
221
222static void wc_x(void *usr, uint32_t idx) {
223    MTLaunchStruct *mtls = (MTLaunchStruct *)usr;
224    RsForEachStubParamStruct p;
225    memcpy(&p, &mtls->fep, sizeof(p));
226    p.lid = idx;
227    RsdHal * dc = (RsdHal *)mtls->rsc->mHal.drv;
228    uint32_t sig = mtls->sig;
229
230    outer_foreach_t fn = (outer_foreach_t) mtls->kernel;
231    while (1) {
232        uint32_t slice = (uint32_t)android_atomic_inc(&mtls->mSliceNum);
233        uint32_t xStart = mtls->xStart + slice * mtls->mSliceSize;
234        uint32_t xEnd = xStart + mtls->mSliceSize;
235        xEnd = rsMin(xEnd, mtls->xEnd);
236        if (xEnd <= xStart) {
237            return;
238        }
239
240        //ALOGE("usr slice %i idx %i, x %i,%i", slice, idx, xStart, xEnd);
241        //ALOGE("usr ptr in %p,  out %p", mtls->fep.ptrIn, mtls->fep.ptrOut);
242
243        p.out = mtls->fep.ptrOut + (mtls->fep.eStrideOut * xStart);
244        p.in = mtls->fep.ptrIn + (mtls->fep.eStrideIn * xStart);
245        fn(&p, xStart, xEnd, mtls->fep.eStrideIn, mtls->fep.eStrideOut);
246    }
247}
248
249void rsdScriptInvokeForEachMtlsSetup(const Context *rsc,
250                                     const Allocation * ain,
251                                     Allocation * aout,
252                                     const void * usr,
253                                     uint32_t usrLen,
254                                     const RsScriptCall *sc,
255                                     MTLaunchStruct *mtls) {
256
257    memset(mtls, 0, sizeof(MTLaunchStruct));
258
259    if (ain) {
260        mtls->fep.dimX = ain->getType()->getDimX();
261        mtls->fep.dimY = ain->getType()->getDimY();
262        mtls->fep.dimZ = ain->getType()->getDimZ();
263        //mtls->dimArray = ain->getType()->getDimArray();
264    } else if (aout) {
265        mtls->fep.dimX = aout->getType()->getDimX();
266        mtls->fep.dimY = aout->getType()->getDimY();
267        mtls->fep.dimZ = aout->getType()->getDimZ();
268        //mtls->dimArray = aout->getType()->getDimArray();
269    } else {
270        rsc->setError(RS_ERROR_BAD_SCRIPT, "rsForEach called with null allocations");
271        return;
272    }
273
274    if (!sc || (sc->xEnd == 0)) {
275        mtls->xEnd = mtls->fep.dimX;
276    } else {
277        rsAssert(sc->xStart < mtls->fep.dimX);
278        rsAssert(sc->xEnd <= mtls->fep.dimX);
279        rsAssert(sc->xStart < sc->xEnd);
280        mtls->xStart = rsMin(mtls->fep.dimX, sc->xStart);
281        mtls->xEnd = rsMin(mtls->fep.dimX, sc->xEnd);
282        if (mtls->xStart >= mtls->xEnd) return;
283    }
284
285    if (!sc || (sc->yEnd == 0)) {
286        mtls->yEnd = mtls->fep.dimY;
287    } else {
288        rsAssert(sc->yStart < mtls->fep.dimY);
289        rsAssert(sc->yEnd <= mtls->fep.dimY);
290        rsAssert(sc->yStart < sc->yEnd);
291        mtls->yStart = rsMin(mtls->fep.dimY, sc->yStart);
292        mtls->yEnd = rsMin(mtls->fep.dimY, sc->yEnd);
293        if (mtls->yStart >= mtls->yEnd) return;
294    }
295
296    mtls->xEnd = rsMax((uint32_t)1, mtls->xEnd);
297    mtls->yEnd = rsMax((uint32_t)1, mtls->yEnd);
298    mtls->zEnd = rsMax((uint32_t)1, mtls->zEnd);
299    mtls->arrayEnd = rsMax((uint32_t)1, mtls->arrayEnd);
300
301    rsAssert(!ain || (ain->getType()->getDimZ() == 0));
302
303    Context *mrsc = (Context *)rsc;
304    mtls->rsc = mrsc;
305    mtls->ain = ain;
306    mtls->aout = aout;
307    mtls->fep.usr = usr;
308    mtls->fep.usrLen = usrLen;
309    mtls->mSliceSize = 10;
310    mtls->mSliceNum = 0;
311
312    mtls->fep.ptrIn = NULL;
313    mtls->fep.eStrideIn = 0;
314
315    if (ain) {
316        DrvAllocation *aindrv = (DrvAllocation *)ain->mHal.drv;
317        mtls->fep.ptrIn = (const uint8_t *)aindrv->lod[0].mallocPtr;
318        mtls->fep.eStrideIn = ain->getType()->getElementSizeBytes();
319        mtls->fep.yStrideIn = aindrv->lod[0].stride;
320    }
321
322    mtls->fep.ptrOut = NULL;
323    mtls->fep.eStrideOut = 0;
324    if (aout) {
325        DrvAllocation *aoutdrv = (DrvAllocation *)aout->mHal.drv;
326        mtls->fep.ptrOut = (uint8_t *)aoutdrv->lod[0].mallocPtr;
327        mtls->fep.eStrideOut = aout->getType()->getElementSizeBytes();
328        mtls->fep.yStrideOut = aoutdrv->lod[0].stride;
329    }
330}
331
332void rsdScriptLaunchThreads(const Context *rsc,
333                            bool isThreadable,
334                            const Allocation * ain,
335                            Allocation * aout,
336                            const void * usr,
337                            uint32_t usrLen,
338                            const RsScriptCall *sc,
339                            MTLaunchStruct *mtls) {
340
341    Context *mrsc = (Context *)rsc;
342    RsdHal * dc = (RsdHal *)mtls->rsc->mHal.drv;
343
344    if ((dc->mWorkers.mCount > 1) && isThreadable && !dc->mInForEach) {
345        dc->mInForEach = true;
346        if (mtls->fep.dimY > 1) {
347            mtls->mSliceSize = mtls->fep.dimY / (dc->mWorkers.mCount * 4);
348            if(mtls->mSliceSize < 1) {
349                mtls->mSliceSize = 1;
350            }
351
352            rsdLaunchThreads(mrsc, wc_xy, mtls);
353        } else {
354            mtls->mSliceSize = mtls->fep.dimX / (dc->mWorkers.mCount * 4);
355            if(mtls->mSliceSize < 1) {
356                mtls->mSliceSize = 1;
357            }
358
359            rsdLaunchThreads(mrsc, wc_x, mtls);
360        }
361        dc->mInForEach = false;
362
363        //ALOGE("launch 1");
364    } else {
365        RsForEachStubParamStruct p;
366        memcpy(&p, &mtls->fep, sizeof(p));
367        uint32_t sig = mtls->sig;
368
369        //ALOGE("launch 3");
370        outer_foreach_t fn = (outer_foreach_t) mtls->kernel;
371        for (p.ar[0] = mtls->arrayStart; p.ar[0] < mtls->arrayEnd; p.ar[0]++) {
372            for (p.z = mtls->zStart; p.z < mtls->zEnd; p.z++) {
373                for (p.y = mtls->yStart; p.y < mtls->yEnd; p.y++) {
374                    uint32_t offset = mtls->fep.dimY * mtls->fep.dimZ * p.ar[0] +
375                                      mtls->fep.dimY * p.z + p.y;
376                    p.out = mtls->fep.ptrOut + (mtls->fep.yStrideOut * offset) +
377                            (mtls->fep.eStrideOut * mtls->xStart);
378                    p.in = mtls->fep.ptrIn + (mtls->fep.yStrideIn * offset) +
379                           (mtls->fep.eStrideIn * mtls->xStart);
380                    fn(&p, mtls->xStart, mtls->xEnd, mtls->fep.eStrideIn, mtls->fep.eStrideOut);
381                }
382            }
383        }
384    }
385}
386
387void rsdScriptInvokeForEach(const Context *rsc,
388                            Script *s,
389                            uint32_t slot,
390                            const Allocation * ain,
391                            Allocation * aout,
392                            const void * usr,
393                            uint32_t usrLen,
394                            const RsScriptCall *sc) {
395
396    RsdHal * dc = (RsdHal *)rsc->mHal.drv;
397
398    MTLaunchStruct mtls;
399    rsdScriptInvokeForEachMtlsSetup(rsc, ain, aout, usr, usrLen, sc, &mtls);
400    mtls.script = s;
401    mtls.fep.slot = slot;
402
403    DrvScript *drv = (DrvScript *)s->mHal.drv;
404    if (drv->mIntrinsicID) {
405        mtls.kernel = (void (*)())drv->mIntrinsicFuncs.root;
406        mtls.fep.usr = drv->mIntrinsicData;
407    } else {
408        rsAssert(slot < drv->mExecutable->getExportForeachFuncAddrs().size());
409        mtls.kernel = reinterpret_cast<ForEachFunc_t>(
410                          drv->mExecutable->getExportForeachFuncAddrs()[slot]);
411        rsAssert(mtls.kernel != NULL);
412        mtls.sig = drv->mExecutable->getInfo().getExportForeachFuncs()[slot].second;
413    }
414
415
416    Script * oldTLS = setTLS(s);
417    rsdScriptLaunchThreads(rsc, s->mHal.info.isThreadable, ain, aout, usr, usrLen, sc, &mtls);
418    setTLS(oldTLS);
419}
420
421
422int rsdScriptInvokeRoot(const Context *dc, Script *script) {
423    DrvScript *drv = (DrvScript *)script->mHal.drv;
424
425    Script * oldTLS = setTLS(script);
426    int ret = drv->mRoot();
427    setTLS(oldTLS);
428
429    return ret;
430}
431
432void rsdScriptInvokeInit(const Context *dc, Script *script) {
433    DrvScript *drv = (DrvScript *)script->mHal.drv;
434
435    if (drv->mInit) {
436        drv->mInit();
437    }
438}
439
440void rsdScriptInvokeFreeChildren(const Context *dc, Script *script) {
441    DrvScript *drv = (DrvScript *)script->mHal.drv;
442
443    if (drv->mFreeChildren) {
444        drv->mFreeChildren();
445    }
446}
447
448void rsdScriptInvokeFunction(const Context *dc, Script *script,
449                            uint32_t slot,
450                            const void *params,
451                            size_t paramLength) {
452    DrvScript *drv = (DrvScript *)script->mHal.drv;
453    //ALOGE("invoke %p %p %i %p %i", dc, script, slot, params, paramLength);
454
455    Script * oldTLS = setTLS(script);
456    reinterpret_cast<void (*)(const void *, uint32_t)>(
457        drv->mExecutable->getExportFuncAddrs()[slot])(params, paramLength);
458    setTLS(oldTLS);
459}
460
461void rsdScriptSetGlobalVar(const Context *dc, const Script *script,
462                           uint32_t slot, void *data, size_t dataLength) {
463    DrvScript *drv = (DrvScript *)script->mHal.drv;
464    //rsAssert(!script->mFieldIsObject[slot]);
465    //ALOGE("setGlobalVar %p %p %i %p %i", dc, script, slot, data, dataLength);
466
467    if (drv->mIntrinsicID) {
468        drv->mIntrinsicFuncs.setVar(dc, script, drv->mIntrinsicData, slot, data, dataLength);
469        return;
470    }
471
472    int32_t *destPtr = reinterpret_cast<int32_t *>(
473                          drv->mExecutable->getExportVarAddrs()[slot]);
474    if (!destPtr) {
475        //ALOGV("Calling setVar on slot = %i which is null", slot);
476        return;
477    }
478
479    memcpy(destPtr, data, dataLength);
480}
481
482void rsdScriptSetGlobalVarWithElemDims(
483        const android::renderscript::Context *dc,
484        const android::renderscript::Script *script,
485        uint32_t slot, void *data, size_t dataLength,
486        const android::renderscript::Element *elem,
487        const size_t *dims, size_t dimLength) {
488    DrvScript *drv = (DrvScript *)script->mHal.drv;
489
490    int32_t *destPtr = reinterpret_cast<int32_t *>(
491        drv->mExecutable->getExportVarAddrs()[slot]);
492    if (!destPtr) {
493        //ALOGV("Calling setVar on slot = %i which is null", slot);
494        return;
495    }
496
497    // We want to look at dimension in terms of integer components,
498    // but dimLength is given in terms of bytes.
499    dimLength /= sizeof(int);
500
501    // Only a single dimension is currently supported.
502    rsAssert(dimLength == 1);
503    if (dimLength == 1) {
504        // First do the increment loop.
505        size_t stride = elem->getSizeBytes();
506        char *cVal = reinterpret_cast<char *>(data);
507        for (size_t i = 0; i < dims[0]; i++) {
508            elem->incRefs(cVal);
509            cVal += stride;
510        }
511
512        // Decrement loop comes after (to prevent race conditions).
513        char *oldVal = reinterpret_cast<char *>(destPtr);
514        for (size_t i = 0; i < dims[0]; i++) {
515            elem->decRefs(oldVal);
516            oldVal += stride;
517        }
518    }
519
520    memcpy(destPtr, data, dataLength);
521}
522
523void rsdScriptSetGlobalBind(const Context *dc, const Script *script, uint32_t slot, Allocation *data) {
524    DrvScript *drv = (DrvScript *)script->mHal.drv;
525
526    //rsAssert(!script->mFieldIsObject[slot]);
527    //ALOGE("setGlobalBind %p %p %i %p", dc, script, slot, data);
528
529    rsAssert(!drv->mIntrinsicID);
530
531    int32_t *destPtr = reinterpret_cast<int32_t *>(
532                          drv->mExecutable->getExportVarAddrs()[slot]);
533    if (!destPtr) {
534        //ALOGV("Calling setVar on slot = %i which is null", slot);
535        return;
536    }
537
538    void *ptr = NULL;
539    drv->mBoundAllocs[slot] = data;
540    if(data) {
541        DrvAllocation *allocDrv = (DrvAllocation *)data->mHal.drv;
542        ptr = allocDrv->lod[0].mallocPtr;
543    }
544    memcpy(destPtr, &ptr, sizeof(void *));
545}
546
547void rsdScriptSetGlobalObj(const Context *dc, const Script *script, uint32_t slot, ObjectBase *data) {
548    DrvScript *drv = (DrvScript *)script->mHal.drv;
549    //rsAssert(script->mFieldIsObject[slot]);
550    //ALOGE("setGlobalObj %p %p %i %p", dc, script, slot, data);
551
552    if (drv->mIntrinsicID) {
553        drv->mIntrinsicFuncs.setVarObj(dc, script, drv->mIntrinsicData, slot,
554                                       static_cast<Allocation *>(data));
555        return;
556    }
557
558    int32_t *destPtr = reinterpret_cast<int32_t *>(
559                          drv->mExecutable->getExportVarAddrs()[slot]);
560    if (!destPtr) {
561        //ALOGV("Calling setVar on slot = %i which is null", slot);
562        return;
563    }
564
565    rsrSetObject(dc, script, (ObjectBase **)destPtr, data);
566}
567
568void rsdScriptDestroy(const Context *dc, Script *script) {
569    DrvScript *drv = (DrvScript *)script->mHal.drv;
570
571    if (drv == NULL) {
572        return;
573    }
574
575    if (drv->mExecutable) {
576        Vector<void *>::const_iterator var_addr_iter =
577            drv->mExecutable->getExportVarAddrs().begin();
578        Vector<void *>::const_iterator var_addr_end =
579            drv->mExecutable->getExportVarAddrs().end();
580
581        bcc::RSInfo::ObjectSlotListTy::const_iterator is_object_iter =
582            drv->mExecutable->getInfo().getObjectSlots().begin();
583        bcc::RSInfo::ObjectSlotListTy::const_iterator is_object_end =
584            drv->mExecutable->getInfo().getObjectSlots().end();
585
586        while ((var_addr_iter != var_addr_end) &&
587               (is_object_iter != is_object_end)) {
588            // The field address can be NULL if the script-side has optimized
589            // the corresponding global variable away.
590            ObjectBase **obj_addr =
591                reinterpret_cast<ObjectBase **>(*var_addr_iter);
592            if (*is_object_iter) {
593                if (*var_addr_iter != NULL) {
594                    rsrClearObject(dc, script, obj_addr);
595                }
596            }
597            var_addr_iter++;
598            is_object_iter++;
599        }
600    }
601
602    delete drv->mCompilerContext;
603    delete drv->mCompilerDriver;
604    delete drv->mExecutable;
605    delete[] drv->mBoundAllocs;
606    free(drv);
607    script->mHal.drv = NULL;
608}
609
610Allocation * rsdScriptGetAllocationForPointer(const android::renderscript::Context *dc,
611                                              const android::renderscript::Script *sc,
612                                              const void *ptr) {
613    DrvScript *drv = (DrvScript *)sc->mHal.drv;
614    if (!ptr) {
615        return NULL;
616    }
617
618    for (uint32_t ct=0; ct < sc->mHal.info.exportedVariableCount; ct++) {
619        Allocation *a = drv->mBoundAllocs[ct];
620        if (!a) continue;
621        DrvAllocation *adrv = (DrvAllocation *)a->mHal.drv;
622        if (adrv->lod[0].mallocPtr == ptr) {
623            return a;
624        }
625    }
626    ALOGE("rsGetAllocation, failed to find %p", ptr);
627    return NULL;
628}
629
630