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