rsdBcc.cpp revision 585f554211bd9945ef84878d89bfc9b4b1c0bd94
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 "rsContext.h"
23#include "rsScriptC.h"
24
25#include "utils/Timers.h"
26#include "utils/StopWatch.h"
27extern "C" {
28#include "libdex/ZipArchive.h"
29}
30
31
32using namespace android;
33using namespace android::renderscript;
34
35struct DrvScript {
36    int (*mRoot)();
37    void (*mInit)();
38
39    BCCScriptRef mBccScript;
40
41    uint32_t mInvokeFunctionCount;
42    InvokeFunc_t *mInvokeFunctions;
43    uint32_t mFieldCount;
44    void ** mFieldAddress;
45    bool * mFieldIsObject;
46
47    const uint8_t * mScriptText;
48    uint32_t mScriptTextLength;
49
50    //uint32_t * mObjectSlots;
51    //uint32_t mObjectSlotCount;
52
53    uint32_t mPragmaCount;
54    const char ** mPragmaKeys;
55    const char ** mPragmaValues;
56
57};
58
59
60static Script * setTLS(Script *sc) {
61    ScriptTLSStruct * tls = (ScriptTLSStruct *)pthread_getspecific(rsdgThreadTLSKey);
62    rsAssert(tls);
63    Script *old = tls->mScript;
64    tls->mScript = sc;
65    return old;
66}
67
68
69// Input: cacheDir
70// Input: resName
71// Input: extName
72//
73// Note: cacheFile = resName + extName
74//
75// Output: Returns cachePath == cacheDir + cacheFile
76static char *genCacheFileName(const char *cacheDir,
77                              const char *resName,
78                              const char *extName) {
79    char cachePath[512];
80    char cacheFile[sizeof(cachePath)];
81    const size_t kBufLen = sizeof(cachePath) - 1;
82
83    cacheFile[0] = '\0';
84    // Note: resName today is usually something like
85    //       "/com.android.fountain:raw/fountain"
86    if (resName[0] != '/') {
87        // Get the absolute path of the raw/***.bc file.
88
89        // Generate the absolute path.  This doesn't do everything it
90        // should, e.g. if resName is "./out/whatever" it doesn't crunch
91        // the leading "./" out because this if-block is not triggered,
92        // but it'll make do.
93        //
94        if (getcwd(cacheFile, kBufLen) == NULL) {
95            LOGE("Can't get CWD while opening raw/***.bc file\n");
96            return NULL;
97        }
98        // Append "/" at the end of cacheFile so far.
99        strncat(cacheFile, "/", kBufLen);
100    }
101
102    // cacheFile = resName + extName
103    //
104    strncat(cacheFile, resName, kBufLen);
105    if (extName != NULL) {
106        // TODO(srhines): strncat() is a bit dangerous
107        strncat(cacheFile, extName, kBufLen);
108    }
109
110    // Turn the path into a flat filename by replacing
111    // any slashes after the first one with '@' characters.
112    char *cp = cacheFile + 1;
113    while (*cp != '\0') {
114        if (*cp == '/') {
115            *cp = '@';
116        }
117        cp++;
118    }
119
120    // Tack on the file name for the actual cache file path.
121    strncpy(cachePath, cacheDir, kBufLen);
122    strncat(cachePath, cacheFile, kBufLen);
123
124    LOGV("Cache file for '%s' '%s' is '%s'\n", resName, extName, cachePath);
125    return strdup(cachePath);
126}
127
128bool rsdScriptInit(const Context *rsc,
129                     ScriptC *script,
130                     char const *resName,
131                     char const *cacheDir,
132                     uint8_t const *bitcode,
133                     size_t bitcodeSize,
134                     uint32_t flags) {
135    //LOGE("rsdScriptCreate %p %p %p %p %i %i %p", rsc, resName, cacheDir, bitcode, bitcodeSize, flags, lookupFunc);
136
137    pthread_mutex_lock(&rsdgInitMutex);
138    char *cachePath = NULL;
139    uint32_t objectSlotCount = 0;
140
141    DrvScript *drv = (DrvScript *)calloc(1, sizeof(DrvScript));
142    if (drv == NULL) {
143        goto error;
144    }
145    script->mHal.drv = drv;
146
147    drv->mBccScript = bccCreateScript();
148    script->mHal.info.isThreadable = true;
149    drv->mScriptText = bitcode;
150    drv->mScriptTextLength = bitcodeSize;
151
152    //LOGE("mBccScript %p", script->mBccScript);
153
154    if (bccRegisterSymbolCallback(drv->mBccScript, &rsdLookupRuntimeStub, script) != 0) {
155        LOGE("bcc: FAILS to register symbol callback");
156        goto error;
157    }
158
159    if (bccReadBC(drv->mBccScript,
160                  resName,
161                  (char const *)drv->mScriptText,
162                  drv->mScriptTextLength, 0) != 0) {
163        LOGE("bcc: FAILS to read bitcode");
164        goto error;
165    }
166
167#if 1
168    if (bccLinkFile(drv->mBccScript, "/system/lib/libclcore.bc", 0) != 0) {
169        LOGE("bcc: FAILS to link bitcode");
170        goto error;
171    }
172#endif
173    cachePath = genCacheFileName(cacheDir, resName, ".oBCC");
174
175    if (bccPrepareExecutable(drv->mBccScript, cachePath, 0) != 0) {
176        LOGE("bcc: FAILS to prepare executable");
177        goto error;
178    }
179
180    free(cachePath);
181
182    drv->mRoot = reinterpret_cast<int (*)()>(bccGetFuncAddr(drv->mBccScript, "root"));
183    drv->mInit = reinterpret_cast<void (*)()>(bccGetFuncAddr(drv->mBccScript, "init"));
184
185    drv->mInvokeFunctionCount = bccGetExportFuncCount(drv->mBccScript);
186    if (drv->mInvokeFunctionCount <= 0)
187        drv->mInvokeFunctions = NULL;
188    else {
189        drv->mInvokeFunctions = (InvokeFunc_t*) calloc(drv->mInvokeFunctionCount, sizeof(InvokeFunc_t));
190        bccGetExportFuncList(drv->mBccScript, drv->mInvokeFunctionCount, (void **) drv->mInvokeFunctions);
191    }
192
193    drv->mFieldCount = bccGetExportVarCount(drv->mBccScript);
194    if (drv->mFieldCount <= 0) {
195        drv->mFieldAddress = NULL;
196        drv->mFieldIsObject = NULL;
197    } else {
198        drv->mFieldAddress = (void **) calloc(drv->mFieldCount, sizeof(void *));
199        drv->mFieldIsObject = (bool *) calloc(drv->mFieldCount, sizeof(bool));
200        bccGetExportVarList(drv->mBccScript, drv->mFieldCount, (void **) drv->mFieldAddress);
201    }
202
203    objectSlotCount = bccGetObjectSlotCount(drv->mBccScript);
204    if (objectSlotCount) {
205        uint32_t * slots = new uint32_t[objectSlotCount];
206        bccGetObjectSlotList(drv->mBccScript, objectSlotCount, slots);
207        for (uint32_t ct=0; ct < objectSlotCount; ct++) {
208            drv->mFieldIsObject[slots[ct]] = true;
209        }
210        delete [] slots;
211    }
212
213    uint32_t mPragmaCount;
214    const char ** mPragmaKeys;
215    const char ** mPragmaValues;
216
217    drv->mPragmaCount = bccGetPragmaCount(drv->mBccScript);
218    if (drv->mPragmaCount <= 0) {
219        drv->mPragmaKeys = NULL;
220        drv->mPragmaValues = NULL;
221    } else {
222        drv->mPragmaKeys = (const char **) calloc(drv->mPragmaCount, sizeof(const char *));
223        drv->mPragmaValues = (const char **) calloc(drv->mPragmaCount, sizeof(const char *));
224        bccGetPragmaList(drv->mBccScript, drv->mPragmaCount, drv->mPragmaKeys, drv->mPragmaValues);
225    }
226
227
228
229    // Copy info over to runtime
230    script->mHal.info.exportedFunctionCount = drv->mInvokeFunctionCount;
231    script->mHal.info.exportedVariableCount = drv->mFieldCount;
232    script->mHal.info.exportedPragmaCount = drv->mPragmaCount;
233    script->mHal.info.exportedPragmaKeyList = drv->mPragmaKeys;
234    script->mHal.info.exportedPragmaValueList = drv->mPragmaValues;
235    script->mHal.info.root = drv->mRoot;
236
237
238    pthread_mutex_unlock(&rsdgInitMutex);
239    return true;
240
241error:
242
243    pthread_mutex_unlock(&rsdgInitMutex);
244    free(drv);
245    return false;
246
247}
248
249typedef struct {
250    Context *rsc;
251    Script *script;
252    const Allocation * ain;
253    Allocation * aout;
254    const void * usr;
255
256    uint32_t mSliceSize;
257    volatile int mSliceNum;
258
259    const uint8_t *ptrIn;
260    uint32_t eStrideIn;
261    uint8_t *ptrOut;
262    uint32_t eStrideOut;
263
264    uint32_t xStart;
265    uint32_t xEnd;
266    uint32_t yStart;
267    uint32_t yEnd;
268    uint32_t zStart;
269    uint32_t zEnd;
270    uint32_t arrayStart;
271    uint32_t arrayEnd;
272
273    uint32_t dimX;
274    uint32_t dimY;
275    uint32_t dimZ;
276    uint32_t dimArray;
277} MTLaunchStruct;
278typedef int (*rs_t)(const void *, void *, const void *, uint32_t, uint32_t, uint32_t, uint32_t);
279
280static void wc_xy(void *usr, uint32_t idx) {
281    MTLaunchStruct *mtls = (MTLaunchStruct *)usr;
282
283    while (1) {
284        uint32_t slice = (uint32_t)android_atomic_inc(&mtls->mSliceNum);
285        uint32_t yStart = mtls->yStart + slice * mtls->mSliceSize;
286        uint32_t yEnd = yStart + mtls->mSliceSize;
287        yEnd = rsMin(yEnd, mtls->yEnd);
288        if (yEnd <= yStart) {
289            return;
290        }
291
292        //LOGE("usr idx %i, x %i,%i  y %i,%i", idx, mtls->xStart, mtls->xEnd, yStart, yEnd);
293        //LOGE("usr ptr in %p,  out %p", mtls->ptrIn, mtls->ptrOut);
294        for (uint32_t y = yStart; y < yEnd; y++) {
295            uint32_t offset = mtls->dimX * y;
296            uint8_t *xPtrOut = mtls->ptrOut + (mtls->eStrideOut * offset);
297            const uint8_t *xPtrIn = mtls->ptrIn + (mtls->eStrideIn * offset);
298
299            for (uint32_t x = mtls->xStart; x < mtls->xEnd; x++) {
300                ((rs_t)mtls->script->mHal.info.root) (xPtrIn, xPtrOut, mtls->usr, x, y, 0, 0);
301                xPtrIn += mtls->eStrideIn;
302                xPtrOut += mtls->eStrideOut;
303            }
304        }
305    }
306}
307
308static void wc_x(void *usr, uint32_t idx) {
309    MTLaunchStruct *mtls = (MTLaunchStruct *)usr;
310
311    while (1) {
312        uint32_t slice = (uint32_t)android_atomic_inc(&mtls->mSliceNum);
313        uint32_t xStart = mtls->xStart + slice * mtls->mSliceSize;
314        uint32_t xEnd = xStart + mtls->mSliceSize;
315        xEnd = rsMin(xEnd, mtls->xEnd);
316        if (xEnd <= xStart) {
317            return;
318        }
319
320        //LOGE("usr idx %i, x %i,%i  y %i,%i", idx, mtls->xStart, mtls->xEnd, yStart, yEnd);
321        //LOGE("usr ptr in %p,  out %p", mtls->ptrIn, mtls->ptrOut);
322        uint8_t *xPtrOut = mtls->ptrOut + (mtls->eStrideOut * xStart);
323        const uint8_t *xPtrIn = mtls->ptrIn + (mtls->eStrideIn * xStart);
324        for (uint32_t x = xStart; x < xEnd; x++) {
325            ((rs_t)mtls->script->mHal.info.root) (xPtrIn, xPtrOut, mtls->usr, x, 0, 0, 0);
326            xPtrIn += mtls->eStrideIn;
327            xPtrOut += mtls->eStrideOut;
328        }
329    }
330}
331
332void rsdScriptInvokeForEach(const Context *rsc,
333                            Script *s,
334                            const Allocation * ain,
335                            Allocation * aout,
336                            const void * usr,
337                            uint32_t usrLen,
338                            const RsScriptCall *sc) {
339
340    RsdHal * dc = (RsdHal *)rsc->mHal.drv;
341
342    MTLaunchStruct mtls;
343    memset(&mtls, 0, sizeof(mtls));
344
345    if (ain) {
346        mtls.dimX = ain->getType()->getDimX();
347        mtls.dimY = ain->getType()->getDimY();
348        mtls.dimZ = ain->getType()->getDimZ();
349        //mtls.dimArray = ain->getType()->getDimArray();
350    } else if (aout) {
351        mtls.dimX = aout->getType()->getDimX();
352        mtls.dimY = aout->getType()->getDimY();
353        mtls.dimZ = aout->getType()->getDimZ();
354        //mtls.dimArray = aout->getType()->getDimArray();
355    } else {
356        rsc->setError(RS_ERROR_BAD_SCRIPT, "rsForEach called with null allocations");
357        return;
358    }
359
360    if (!sc || (sc->xEnd == 0)) {
361        mtls.xEnd = mtls.dimX;
362    } else {
363        rsAssert(sc->xStart < mtls.dimX);
364        rsAssert(sc->xEnd <= mtls.dimX);
365        rsAssert(sc->xStart < sc->xEnd);
366        mtls.xStart = rsMin(mtls.dimX, sc->xStart);
367        mtls.xEnd = rsMin(mtls.dimX, sc->xEnd);
368        if (mtls.xStart >= mtls.xEnd) return;
369    }
370
371    if (!sc || (sc->yEnd == 0)) {
372        mtls.yEnd = mtls.dimY;
373    } else {
374        rsAssert(sc->yStart < mtls.dimY);
375        rsAssert(sc->yEnd <= mtls.dimY);
376        rsAssert(sc->yStart < sc->yEnd);
377        mtls.yStart = rsMin(mtls.dimY, sc->yStart);
378        mtls.yEnd = rsMin(mtls.dimY, sc->yEnd);
379        if (mtls.yStart >= mtls.yEnd) return;
380    }
381
382    mtls.xEnd = rsMax((uint32_t)1, mtls.xEnd);
383    mtls.yEnd = rsMax((uint32_t)1, mtls.yEnd);
384    mtls.zEnd = rsMax((uint32_t)1, mtls.zEnd);
385    mtls.arrayEnd = rsMax((uint32_t)1, mtls.arrayEnd);
386
387    rsAssert(!ain || (ain->getType()->getDimZ() == 0));
388
389    Context *mrsc = (Context *)rsc;
390    Script * oldTLS = setTLS(s);
391
392    mtls.rsc = mrsc;
393    mtls.ain = ain;
394    mtls.aout = aout;
395    mtls.script = s;
396    mtls.usr = usr;
397    mtls.mSliceSize = 10;
398    mtls.mSliceNum = 0;
399
400    mtls.ptrIn = NULL;
401    mtls.eStrideIn = 0;
402    if (ain) {
403        mtls.ptrIn = (const uint8_t *)ain->getPtr();
404        mtls.eStrideIn = ain->getType()->getElementSizeBytes();
405    }
406
407    mtls.ptrOut = NULL;
408    mtls.eStrideOut = 0;
409    if (aout) {
410        mtls.ptrOut = (uint8_t *)aout->getPtr();
411        mtls.eStrideOut = aout->getType()->getElementSizeBytes();
412    }
413
414    if ((dc->mWorkers.mCount > 1) && s->mHal.info.isThreadable) {
415        if (mtls.dimY > 1) {
416            rsdLaunchThreads(mrsc, wc_xy, &mtls);
417        } else {
418            rsdLaunchThreads(mrsc, wc_x, &mtls);
419        }
420
421        //LOGE("launch 1");
422    } else {
423        //LOGE("launch 3");
424        for (uint32_t ar = mtls.arrayStart; ar < mtls.arrayEnd; ar++) {
425            for (uint32_t z = mtls.zStart; z < mtls.zEnd; z++) {
426                for (uint32_t y = mtls.yStart; y < mtls.yEnd; y++) {
427                    uint32_t offset = mtls.dimX * mtls.dimY * mtls.dimZ * ar +
428                                      mtls.dimX * mtls.dimY * z +
429                                      mtls.dimX * y;
430                    uint8_t *xPtrOut = mtls.ptrOut + (mtls.eStrideOut * offset);
431                    const uint8_t *xPtrIn = mtls.ptrIn + (mtls.eStrideIn * offset);
432
433                    for (uint32_t x = mtls.xStart; x < mtls.xEnd; x++) {
434                        ((rs_t)s->mHal.info.root) (xPtrIn, xPtrOut, usr, x, y, z, ar);
435                        xPtrIn += mtls.eStrideIn;
436                        xPtrOut += mtls.eStrideOut;
437                    }
438                }
439            }
440        }
441    }
442
443    setTLS(oldTLS);
444}
445
446
447int rsdScriptInvokeRoot(const Context *dc, Script *script) {
448    DrvScript *drv = (DrvScript *)script->mHal.drv;
449
450    Script * oldTLS = setTLS(script);
451    int ret = drv->mRoot();
452    setTLS(oldTLS);
453
454    return ret;
455}
456
457void rsdScriptInvokeInit(const Context *dc, Script *script) {
458    DrvScript *drv = (DrvScript *)script->mHal.drv;
459
460    if (drv->mInit) {
461        drv->mInit();
462    }
463}
464
465
466void rsdScriptInvokeFunction(const Context *dc, Script *script,
467                            uint32_t slot,
468                            const void *params,
469                            size_t paramLength) {
470    DrvScript *drv = (DrvScript *)script->mHal.drv;
471    //LOGE("invoke %p %p %i %p %i", dc, script, slot, params, paramLength);
472
473    Script * oldTLS = setTLS(script);
474    ((void (*)(const void *, uint32_t))
475        drv->mInvokeFunctions[slot])(params, paramLength);
476    setTLS(oldTLS);
477}
478
479void rsdScriptSetGlobalVar(const Context *dc, const Script *script,
480                           uint32_t slot, void *data, size_t dataLength) {
481    DrvScript *drv = (DrvScript *)script->mHal.drv;
482    //rsAssert(!script->mFieldIsObject[slot]);
483    //LOGE("setGlobalVar %p %p %i %p %i", dc, script, slot, data, dataLength);
484
485    int32_t *destPtr = ((int32_t **)drv->mFieldAddress)[slot];
486    if (!destPtr) {
487        //LOGV("Calling setVar on slot = %i which is null", slot);
488        return;
489    }
490
491    memcpy(destPtr, data, dataLength);
492}
493
494void rsdScriptSetGlobalBind(const Context *dc, const Script *script, uint32_t slot, void *data) {
495    DrvScript *drv = (DrvScript *)script->mHal.drv;
496    //rsAssert(!script->mFieldIsObject[slot]);
497    //LOGE("setGlobalBind %p %p %i %p", dc, script, slot, data);
498
499    int32_t *destPtr = ((int32_t **)drv->mFieldAddress)[slot];
500    if (!destPtr) {
501        //LOGV("Calling setVar on slot = %i which is null", slot);
502        return;
503    }
504
505    memcpy(destPtr, &data, sizeof(void *));
506}
507
508void rsdScriptSetGlobalObj(const Context *dc, const Script *script, uint32_t slot, ObjectBase *data) {
509    DrvScript *drv = (DrvScript *)script->mHal.drv;
510    //rsAssert(script->mFieldIsObject[slot]);
511    //LOGE("setGlobalObj %p %p %i %p", dc, script, slot, data);
512
513    int32_t *destPtr = ((int32_t **)drv->mFieldAddress)[slot];
514    if (!destPtr) {
515        //LOGV("Calling setVar on slot = %i which is null", slot);
516        return;
517    }
518
519    rsrSetObject(dc, script, (ObjectBase **)destPtr, data);
520}
521
522void rsdScriptDestroy(const Context *dc, Script *script) {
523    DrvScript *drv = (DrvScript *)script->mHal.drv;
524
525    if (drv->mFieldAddress) {
526        for (size_t ct=0; ct < drv->mFieldCount; ct++) {
527            if (drv->mFieldIsObject[ct]) {
528                // The field address can be NULL if the script-side has
529                // optimized the corresponding global variable away.
530                if (drv->mFieldAddress[ct]) {
531                    rsrClearObject(dc, script, (ObjectBase **)drv->mFieldAddress[ct]);
532                }
533            }
534        }
535        delete [] drv->mFieldAddress;
536        delete [] drv->mFieldIsObject;
537        drv->mFieldAddress = NULL;
538        drv->mFieldIsObject = NULL;
539        drv->mFieldCount = 0;
540    }
541
542    if (drv->mInvokeFunctions) {
543        delete [] drv->mInvokeFunctions;
544        drv->mInvokeFunctions = NULL;
545        drv->mInvokeFunctionCount = 0;
546    }
547    free(drv);
548    script->mHal.drv = NULL;
549
550}
551
552
553