rsdBcc.cpp revision 4ee16ffbd9d1d72e1757c9b26715597fdc044117
1/*
2 * Copyright (C) 2011 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 *      http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17
18#include "rsdCore.h"
19#include "rsdBcc.h"
20#include "rsdRuntime.h"
21
22#include <bcinfo/MetadataExtractor.h>
23
24#include "rsContext.h"
25#include "rsScriptC.h"
26
27#include "utils/Timers.h"
28#include "utils/StopWatch.h"
29extern "C" {
30#include "libdex/ZipArchive.h"
31}
32
33
34using namespace android;
35using namespace android::renderscript;
36
37struct DrvScript {
38    int (*mRoot)();
39    void (*mInit)();
40    void (*mFreeChildren)();
41
42    BCCScriptRef mBccScript;
43
44    bcinfo::MetadataExtractor *ME;
45
46    InvokeFunc_t *mInvokeFunctions;
47    void ** mFieldAddress;
48    bool * mFieldIsObject;
49    const uint32_t *mExportForEachSignatureList;
50
51    const uint8_t * mScriptText;
52    uint32_t mScriptTextLength;
53};
54
55
56static Script * setTLS(Script *sc) {
57    ScriptTLSStruct * tls = (ScriptTLSStruct *)pthread_getspecific(rsdgThreadTLSKey);
58    rsAssert(tls);
59    Script *old = tls->mScript;
60    tls->mScript = sc;
61    return old;
62}
63
64
65bool rsdScriptInit(const Context *rsc,
66                     ScriptC *script,
67                     char const *resName,
68                     char const *cacheDir,
69                     uint8_t const *bitcode,
70                     size_t bitcodeSize,
71                     uint32_t flags) {
72    //LOGE("rsdScriptCreate %p %p %p %p %i %i %p", rsc, resName, cacheDir, bitcode, bitcodeSize, flags, lookupFunc);
73
74    pthread_mutex_lock(&rsdgInitMutex);
75    char *cachePath = NULL;
76    size_t exportFuncCount = 0;
77    size_t exportVarCount = 0;
78    size_t objectSlotCount = 0;
79    size_t exportForEachSignatureCount = 0;
80
81    DrvScript *drv = (DrvScript *)calloc(1, sizeof(DrvScript));
82    if (drv == NULL) {
83        goto error;
84    }
85    script->mHal.drv = drv;
86
87    drv->mBccScript = bccCreateScript();
88    script->mHal.info.isThreadable = true;
89    drv->mScriptText = bitcode;
90    drv->mScriptTextLength = bitcodeSize;
91
92
93    drv->ME = new bcinfo::MetadataExtractor((const char*)drv->mScriptText,
94                                            drv->mScriptTextLength);
95    if (!drv->ME->extract()) {
96      LOGE("bcinfo: failed to read script metadata");
97      goto error;
98    }
99
100    //LOGE("mBccScript %p", script->mBccScript);
101
102    if (bccRegisterSymbolCallback(drv->mBccScript, &rsdLookupRuntimeStub, script) != 0) {
103        LOGE("bcc: FAILS to register symbol callback");
104        goto error;
105    }
106
107    if (bccReadBC(drv->mBccScript,
108                  resName,
109                  (char const *)drv->mScriptText,
110                  drv->mScriptTextLength, 0) != 0) {
111        LOGE("bcc: FAILS to read bitcode");
112        goto error;
113    }
114
115    if (bccLinkFile(drv->mBccScript, "/system/lib/libclcore.bc", 0) != 0) {
116        LOGE("bcc: FAILS to link bitcode");
117        goto error;
118    }
119
120    if (bccPrepareExecutable(drv->mBccScript, cacheDir, resName, 0) != 0) {
121        LOGE("bcc: FAILS to prepare executable");
122        goto error;
123    }
124
125    free(cachePath);
126
127    drv->mRoot = reinterpret_cast<int (*)()>(bccGetFuncAddr(drv->mBccScript, "root"));
128    drv->mInit = reinterpret_cast<void (*)()>(bccGetFuncAddr(drv->mBccScript, "init"));
129    drv->mFreeChildren = reinterpret_cast<void (*)()>(bccGetFuncAddr(drv->mBccScript, ".rs.dtor"));
130
131    exportFuncCount = drv->ME->getExportFuncCount();
132    if (exportFuncCount > 0) {
133        drv->mInvokeFunctions = (InvokeFunc_t*) calloc(exportFuncCount,
134                                                       sizeof(InvokeFunc_t));
135        bccGetExportFuncList(drv->mBccScript, exportFuncCount,
136                             (void **) drv->mInvokeFunctions);
137    } else {
138        drv->mInvokeFunctions = NULL;
139    }
140
141    exportVarCount = drv->ME->getExportVarCount();
142    if (exportVarCount > 0) {
143        drv->mFieldAddress = (void **) calloc(exportVarCount, sizeof(void*));
144        drv->mFieldIsObject = (bool *) calloc(exportVarCount, sizeof(bool));
145        bccGetExportVarList(drv->mBccScript, exportVarCount,
146                            (void **) drv->mFieldAddress);
147    } else {
148        drv->mFieldAddress = NULL;
149        drv->mFieldIsObject = NULL;
150    }
151
152    objectSlotCount = drv->ME->getObjectSlotCount();
153    if (objectSlotCount > 0) {
154        const uint32_t *objectSlotList = drv->ME->getObjectSlotList();
155        for (uint32_t ct=0; ct < objectSlotCount; ct++) {
156            drv->mFieldIsObject[objectSlotList[ct]] = true;
157        }
158    }
159
160    exportForEachSignatureCount = drv->ME->getExportForEachSignatureCount();
161    rsAssert(exportForEachSignatureCount <= 1);
162    drv->mExportForEachSignatureList = drv->ME->getExportForEachSignatureList();
163
164    // Copy info over to runtime
165    script->mHal.info.exportedFunctionCount = drv->ME->getExportFuncCount();
166    script->mHal.info.exportedVariableCount = drv->ME->getExportVarCount();
167    script->mHal.info.exportedPragmaCount = drv->ME->getPragmaCount();
168    script->mHal.info.exportedPragmaKeyList = drv->ME->getPragmaKeyList();
169    script->mHal.info.exportedPragmaValueList = drv->ME->getPragmaValueList();
170    script->mHal.info.root = drv->mRoot;
171
172    pthread_mutex_unlock(&rsdgInitMutex);
173    return true;
174
175error:
176
177    pthread_mutex_unlock(&rsdgInitMutex);
178    if (drv->ME) {
179        delete drv->ME;
180        drv->ME = NULL;
181    }
182    free(drv);
183    return false;
184
185}
186
187typedef struct {
188    Context *rsc;
189    Script *script;
190    uint32_t sig;
191    const Allocation * ain;
192    Allocation * aout;
193    const void * usr;
194    size_t usrLen;
195
196    uint32_t mSliceSize;
197    volatile int mSliceNum;
198
199    const uint8_t *ptrIn;
200    uint32_t eStrideIn;
201    uint8_t *ptrOut;
202    uint32_t eStrideOut;
203
204    uint32_t xStart;
205    uint32_t xEnd;
206    uint32_t yStart;
207    uint32_t yEnd;
208    uint32_t zStart;
209    uint32_t zEnd;
210    uint32_t arrayStart;
211    uint32_t arrayEnd;
212
213    uint32_t dimX;
214    uint32_t dimY;
215    uint32_t dimZ;
216    uint32_t dimArray;
217} MTLaunchStruct;
218typedef void (*rs_t)(const void *, void *, const void *, uint32_t, uint32_t, uint32_t, uint32_t);
219
220static void wc_xy(void *usr, uint32_t idx) {
221    MTLaunchStruct *mtls = (MTLaunchStruct *)usr;
222    RsForEachStubParamStruct p;
223    memset(&p, 0, sizeof(p));
224    p.usr = mtls->usr;
225    p.usr_len = mtls->usrLen;
226    RsdHal * dc = (RsdHal *)mtls->rsc->mHal.drv;
227    uint32_t sig = mtls->sig;
228
229    while (1) {
230        uint32_t slice = (uint32_t)android_atomic_inc(&mtls->mSliceNum);
231        uint32_t yStart = mtls->yStart + slice * mtls->mSliceSize;
232        uint32_t yEnd = yStart + mtls->mSliceSize;
233        yEnd = rsMin(yEnd, mtls->yEnd);
234        if (yEnd <= yStart) {
235            return;
236        }
237
238        //LOGE("usr idx %i, x %i,%i  y %i,%i", idx, mtls->xStart, mtls->xEnd, yStart, yEnd);
239        //LOGE("usr ptr in %p,  out %p", mtls->ptrIn, mtls->ptrOut);
240        for (p.y = yStart; p.y < yEnd; p.y++) {
241            uint32_t offset = mtls->dimX * p.y;
242            uint8_t *xPtrOut = mtls->ptrOut + (mtls->eStrideOut * offset);
243            const uint8_t *xPtrIn = mtls->ptrIn + (mtls->eStrideIn * offset);
244
245            for (p.x = mtls->xStart; p.x < mtls->xEnd; p.x++) {
246                p.in = xPtrIn;
247                p.out = xPtrOut;
248                dc->mForEachLaunch[sig](&mtls->script->mHal.info.root, &p);
249                xPtrIn += mtls->eStrideIn;
250                xPtrOut += mtls->eStrideOut;
251            }
252        }
253    }
254}
255
256static void wc_x(void *usr, uint32_t idx) {
257    MTLaunchStruct *mtls = (MTLaunchStruct *)usr;
258    RsForEachStubParamStruct p;
259    memset(&p, 0, sizeof(p));
260    p.usr = mtls->usr;
261    p.usr_len = mtls->usrLen;
262    RsdHal * dc = (RsdHal *)mtls->rsc->mHal.drv;
263    uint32_t sig = mtls->sig;
264
265    while (1) {
266        uint32_t slice = (uint32_t)android_atomic_inc(&mtls->mSliceNum);
267        uint32_t xStart = mtls->xStart + slice * mtls->mSliceSize;
268        uint32_t xEnd = xStart + mtls->mSliceSize;
269        xEnd = rsMin(xEnd, mtls->xEnd);
270        if (xEnd <= xStart) {
271            return;
272        }
273
274        //LOGE("usr idx %i, x %i,%i  y %i,%i", idx, mtls->xStart, mtls->xEnd, yStart, yEnd);
275        //LOGE("usr ptr in %p,  out %p", mtls->ptrIn, mtls->ptrOut);
276        uint8_t *xPtrOut = mtls->ptrOut + (mtls->eStrideOut * xStart);
277        const uint8_t *xPtrIn = mtls->ptrIn + (mtls->eStrideIn * xStart);
278        for (p.x = xStart; p.x < xEnd; p.x++) {
279            p.in = xPtrIn;
280            p.out = xPtrOut;
281            dc->mForEachLaunch[sig](&mtls->script->mHal.info.root, &p);
282            xPtrIn += mtls->eStrideIn;
283            xPtrOut += mtls->eStrideOut;
284        }
285    }
286}
287
288void rsdScriptInvokeForEach(const Context *rsc,
289                            Script *s,
290                            uint32_t slot,
291                            const Allocation * ain,
292                            Allocation * aout,
293                            const void * usr,
294                            uint32_t usrLen,
295                            const RsScriptCall *sc) {
296
297    RsdHal * dc = (RsdHal *)rsc->mHal.drv;
298
299    MTLaunchStruct mtls;
300    memset(&mtls, 0, sizeof(mtls));
301
302    DrvScript *drv = (DrvScript *)s->mHal.drv;
303    // We only support slot 0 (root) at this point in time.
304    rsAssert(slot == 0);
305    mtls.sig = drv->mExportForEachSignatureList[slot];
306    if (ain) {
307        mtls.dimX = ain->getType()->getDimX();
308        mtls.dimY = ain->getType()->getDimY();
309        mtls.dimZ = ain->getType()->getDimZ();
310        //mtls.dimArray = ain->getType()->getDimArray();
311    } else if (aout) {
312        mtls.dimX = aout->getType()->getDimX();
313        mtls.dimY = aout->getType()->getDimY();
314        mtls.dimZ = aout->getType()->getDimZ();
315        //mtls.dimArray = aout->getType()->getDimArray();
316    } else {
317        rsc->setError(RS_ERROR_BAD_SCRIPT, "rsForEach called with null allocations");
318        return;
319    }
320
321    if (!sc || (sc->xEnd == 0)) {
322        mtls.xEnd = mtls.dimX;
323    } else {
324        rsAssert(sc->xStart < mtls.dimX);
325        rsAssert(sc->xEnd <= mtls.dimX);
326        rsAssert(sc->xStart < sc->xEnd);
327        mtls.xStart = rsMin(mtls.dimX, sc->xStart);
328        mtls.xEnd = rsMin(mtls.dimX, sc->xEnd);
329        if (mtls.xStart >= mtls.xEnd) return;
330    }
331
332    if (!sc || (sc->yEnd == 0)) {
333        mtls.yEnd = mtls.dimY;
334    } else {
335        rsAssert(sc->yStart < mtls.dimY);
336        rsAssert(sc->yEnd <= mtls.dimY);
337        rsAssert(sc->yStart < sc->yEnd);
338        mtls.yStart = rsMin(mtls.dimY, sc->yStart);
339        mtls.yEnd = rsMin(mtls.dimY, sc->yEnd);
340        if (mtls.yStart >= mtls.yEnd) return;
341    }
342
343    mtls.xEnd = rsMax((uint32_t)1, mtls.xEnd);
344    mtls.yEnd = rsMax((uint32_t)1, mtls.yEnd);
345    mtls.zEnd = rsMax((uint32_t)1, mtls.zEnd);
346    mtls.arrayEnd = rsMax((uint32_t)1, mtls.arrayEnd);
347
348    rsAssert(!ain || (ain->getType()->getDimZ() == 0));
349
350    Context *mrsc = (Context *)rsc;
351    Script * oldTLS = setTLS(s);
352
353    mtls.rsc = mrsc;
354    mtls.ain = ain;
355    mtls.aout = aout;
356    mtls.script = s;
357    mtls.usr = usr;
358    mtls.usrLen = usrLen;
359    mtls.mSliceSize = 10;
360    mtls.mSliceNum = 0;
361
362    mtls.ptrIn = NULL;
363    mtls.eStrideIn = 0;
364    if (ain) {
365        mtls.ptrIn = (const uint8_t *)ain->getPtr();
366        mtls.eStrideIn = ain->getType()->getElementSizeBytes();
367    }
368
369    mtls.ptrOut = NULL;
370    mtls.eStrideOut = 0;
371    if (aout) {
372        mtls.ptrOut = (uint8_t *)aout->getPtr();
373        mtls.eStrideOut = aout->getType()->getElementSizeBytes();
374    }
375
376    if ((dc->mWorkers.mCount > 1) && s->mHal.info.isThreadable) {
377        if (mtls.dimY > 1) {
378            rsdLaunchThreads(mrsc, wc_xy, &mtls);
379        } else {
380            rsdLaunchThreads(mrsc, wc_x, &mtls);
381        }
382
383        //LOGE("launch 1");
384    } else {
385        RsForEachStubParamStruct p;
386        memset(&p, 0, sizeof(p));
387        p.usr = mtls.usr;
388        p.usr_len = mtls.usrLen;
389        uint32_t sig = mtls.sig;
390
391        //LOGE("launch 3");
392        for (p.ar[0] = mtls.arrayStart; p.ar[0] < mtls.arrayEnd; p.ar[0]++) {
393            for (p.z = mtls.zStart; p.z < mtls.zEnd; p.z++) {
394                for (p.y = mtls.yStart; p.y < mtls.yEnd; p.y++) {
395                    uint32_t offset = mtls.dimX * mtls.dimY * mtls.dimZ * p.ar[0] +
396                                      mtls.dimX * mtls.dimY * p.z +
397                                      mtls.dimX * p.y;
398                    uint8_t *xPtrOut = mtls.ptrOut + (mtls.eStrideOut * offset);
399                    const uint8_t *xPtrIn = mtls.ptrIn + (mtls.eStrideIn * offset);
400
401                    for (p.x = mtls.xStart; p.x < mtls.xEnd; p.x++) {
402                        p.in = xPtrIn;
403                        p.out = xPtrOut;
404                        dc->mForEachLaunch[sig](&s->mHal.info.root, &p);
405                        xPtrIn += mtls.eStrideIn;
406                        xPtrOut += mtls.eStrideOut;
407                    }
408                }
409            }
410        }
411    }
412
413    setTLS(oldTLS);
414}
415
416
417int rsdScriptInvokeRoot(const Context *dc, Script *script) {
418    DrvScript *drv = (DrvScript *)script->mHal.drv;
419
420    Script * oldTLS = setTLS(script);
421    int ret = drv->mRoot();
422    setTLS(oldTLS);
423
424    return ret;
425}
426
427void rsdScriptInvokeInit(const Context *dc, Script *script) {
428    DrvScript *drv = (DrvScript *)script->mHal.drv;
429
430    if (drv->mInit) {
431        drv->mInit();
432    }
433}
434
435void rsdScriptInvokeFreeChildren(const Context *dc, Script *script) {
436    DrvScript *drv = (DrvScript *)script->mHal.drv;
437
438    if (drv->mFreeChildren) {
439        drv->mFreeChildren();
440    }
441}
442
443void rsdScriptInvokeFunction(const Context *dc, Script *script,
444                            uint32_t slot,
445                            const void *params,
446                            size_t paramLength) {
447    DrvScript *drv = (DrvScript *)script->mHal.drv;
448    //LOGE("invoke %p %p %i %p %i", dc, script, slot, params, paramLength);
449
450    Script * oldTLS = setTLS(script);
451    ((void (*)(const void *, uint32_t))
452        drv->mInvokeFunctions[slot])(params, paramLength);
453    setTLS(oldTLS);
454}
455
456void rsdScriptSetGlobalVar(const Context *dc, const Script *script,
457                           uint32_t slot, void *data, size_t dataLength) {
458    DrvScript *drv = (DrvScript *)script->mHal.drv;
459    //rsAssert(!script->mFieldIsObject[slot]);
460    //LOGE("setGlobalVar %p %p %i %p %i", dc, script, slot, data, dataLength);
461
462    int32_t *destPtr = ((int32_t **)drv->mFieldAddress)[slot];
463    if (!destPtr) {
464        //LOGV("Calling setVar on slot = %i which is null", slot);
465        return;
466    }
467
468    memcpy(destPtr, data, dataLength);
469}
470
471void rsdScriptSetGlobalBind(const Context *dc, const Script *script, uint32_t slot, void *data) {
472    DrvScript *drv = (DrvScript *)script->mHal.drv;
473    //rsAssert(!script->mFieldIsObject[slot]);
474    //LOGE("setGlobalBind %p %p %i %p", dc, script, slot, data);
475
476    int32_t *destPtr = ((int32_t **)drv->mFieldAddress)[slot];
477    if (!destPtr) {
478        //LOGV("Calling setVar on slot = %i which is null", slot);
479        return;
480    }
481
482    memcpy(destPtr, &data, sizeof(void *));
483}
484
485void rsdScriptSetGlobalObj(const Context *dc, const Script *script, uint32_t slot, ObjectBase *data) {
486    DrvScript *drv = (DrvScript *)script->mHal.drv;
487    //rsAssert(script->mFieldIsObject[slot]);
488    //LOGE("setGlobalObj %p %p %i %p", dc, script, slot, data);
489
490    int32_t *destPtr = ((int32_t **)drv->mFieldAddress)[slot];
491    if (!destPtr) {
492        //LOGV("Calling setVar on slot = %i which is null", slot);
493        return;
494    }
495
496    rsrSetObject(dc, script, (ObjectBase **)destPtr, data);
497}
498
499void rsdScriptDestroy(const Context *dc, Script *script) {
500    DrvScript *drv = (DrvScript *)script->mHal.drv;
501
502    if (drv->mFieldAddress) {
503        size_t exportVarCount = drv->ME->getExportVarCount();
504        for (size_t ct = 0; ct < exportVarCount; ct++) {
505            if (drv->mFieldIsObject[ct]) {
506                // The field address can be NULL if the script-side has
507                // optimized the corresponding global variable away.
508                if (drv->mFieldAddress[ct]) {
509                    rsrClearObject(dc, script, (ObjectBase **)drv->mFieldAddress[ct]);
510                }
511            }
512        }
513        free(drv->mFieldAddress);
514        drv->mFieldAddress = NULL;
515        free(drv->mFieldIsObject);
516        drv->mFieldIsObject = NULL;    }
517
518    if (drv->mInvokeFunctions) {
519        free(drv->mInvokeFunctions);
520        drv->mInvokeFunctions = NULL;
521    }
522
523    delete drv->ME;
524    drv->ME = NULL;
525
526    free(drv);
527    script->mHal.drv = NULL;
528
529}
530
531
532