rsdBcc.cpp revision cbb0b8aceedb9146ba901cba1fbd6d1e9ca88f51
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
41    BCCScriptRef mBccScript;
42
43    bcinfo::MetadataExtractor *ME;
44
45    InvokeFunc_t *mInvokeFunctions;
46    void ** mFieldAddress;
47    bool * mFieldIsObject;
48
49    const uint8_t * mScriptText;
50    uint32_t mScriptTextLength;
51};
52
53
54static Script * setTLS(Script *sc) {
55    ScriptTLSStruct * tls = (ScriptTLSStruct *)pthread_getspecific(rsdgThreadTLSKey);
56    rsAssert(tls);
57    Script *old = tls->mScript;
58    tls->mScript = sc;
59    return old;
60}
61
62
63bool rsdScriptInit(const Context *rsc,
64                     ScriptC *script,
65                     char const *resName,
66                     char const *cacheDir,
67                     uint8_t const *bitcode,
68                     size_t bitcodeSize,
69                     uint32_t flags) {
70    //LOGE("rsdScriptCreate %p %p %p %p %i %i %p", rsc, resName, cacheDir, bitcode, bitcodeSize, flags, lookupFunc);
71
72    pthread_mutex_lock(&rsdgInitMutex);
73    char *cachePath = NULL;
74    size_t exportFuncCount = 0;
75    size_t exportVarCount = 0;
76    size_t objectSlotCount = 0;
77
78    DrvScript *drv = (DrvScript *)calloc(1, sizeof(DrvScript));
79    if (drv == NULL) {
80        goto error;
81    }
82    script->mHal.drv = drv;
83
84    drv->mBccScript = bccCreateScript();
85    script->mHal.info.isThreadable = true;
86    drv->mScriptText = bitcode;
87    drv->mScriptTextLength = bitcodeSize;
88
89
90    drv->ME = new bcinfo::MetadataExtractor((const char*)drv->mScriptText,
91                                            drv->mScriptTextLength);
92    if (!drv->ME->extract()) {
93      LOGE("bcinfo: failed to read script metadata");
94      goto error;
95    }
96
97    //LOGE("mBccScript %p", script->mBccScript);
98
99    if (bccRegisterSymbolCallback(drv->mBccScript, &rsdLookupRuntimeStub, script) != 0) {
100        LOGE("bcc: FAILS to register symbol callback");
101        goto error;
102    }
103
104    if (bccReadBC(drv->mBccScript,
105                  resName,
106                  (char const *)drv->mScriptText,
107                  drv->mScriptTextLength, 0) != 0) {
108        LOGE("bcc: FAILS to read bitcode");
109        goto error;
110    }
111
112    if (bccLinkFile(drv->mBccScript, "/system/lib/libclcore.bc", 0) != 0) {
113        LOGE("bcc: FAILS to link bitcode");
114        goto error;
115    }
116
117    if (bccPrepareExecutableEx(drv->mBccScript, cacheDir, resName, 0) != 0) {
118        LOGE("bcc: FAILS to prepare executable");
119        goto error;
120    }
121
122    free(cachePath);
123
124    drv->mRoot = reinterpret_cast<int (*)()>(bccGetFuncAddr(drv->mBccScript, "root"));
125    drv->mInit = reinterpret_cast<void (*)()>(bccGetFuncAddr(drv->mBccScript, "init"));
126
127    exportFuncCount = drv->ME->getExportFuncCount();
128    if (exportFuncCount > 0) {
129        drv->mInvokeFunctions = (InvokeFunc_t*) calloc(exportFuncCount,
130                                                       sizeof(InvokeFunc_t));
131        bccGetExportFuncList(drv->mBccScript, exportFuncCount,
132                             (void **) drv->mInvokeFunctions);
133    } else {
134        drv->mInvokeFunctions = NULL;
135    }
136
137    exportVarCount = drv->ME->getExportVarCount();
138    if (exportVarCount > 0) {
139        drv->mFieldAddress = (void **) calloc(exportVarCount, sizeof(void*));
140        drv->mFieldIsObject = (bool *) calloc(exportVarCount, sizeof(bool));
141        bccGetExportVarList(drv->mBccScript, exportVarCount,
142                            (void **) drv->mFieldAddress);
143    } else {
144        drv->mFieldAddress = NULL;
145        drv->mFieldIsObject = NULL;
146    }
147
148    objectSlotCount = drv->ME->getObjectSlotCount();
149    if (objectSlotCount > 0) {
150        const uint32_t *objectSlotList = drv->ME->getObjectSlotList();
151        for (uint32_t ct=0; ct < objectSlotCount; ct++) {
152            drv->mFieldIsObject[objectSlotList[ct]] = true;
153        }
154    }
155
156    // Copy info over to runtime
157    script->mHal.info.exportedFunctionCount = drv->ME->getExportFuncCount();
158    script->mHal.info.exportedVariableCount = drv->ME->getExportVarCount();
159    script->mHal.info.exportedPragmaCount = drv->ME->getPragmaCount();
160    script->mHal.info.exportedPragmaKeyList = drv->ME->getPragmaKeyList();
161    script->mHal.info.exportedPragmaValueList = drv->ME->getPragmaValueList();
162    script->mHal.info.root = drv->mRoot;
163
164    pthread_mutex_unlock(&rsdgInitMutex);
165    return true;
166
167error:
168
169    pthread_mutex_unlock(&rsdgInitMutex);
170    if (drv->ME) {
171        delete drv->ME;
172        drv->ME = NULL;
173    }
174    free(drv);
175    return false;
176
177}
178
179typedef struct {
180    Context *rsc;
181    Script *script;
182    const Allocation * ain;
183    Allocation * aout;
184    const void * usr;
185
186    uint32_t mSliceSize;
187    volatile int mSliceNum;
188
189    const uint8_t *ptrIn;
190    uint32_t eStrideIn;
191    uint8_t *ptrOut;
192    uint32_t eStrideOut;
193
194    uint32_t xStart;
195    uint32_t xEnd;
196    uint32_t yStart;
197    uint32_t yEnd;
198    uint32_t zStart;
199    uint32_t zEnd;
200    uint32_t arrayStart;
201    uint32_t arrayEnd;
202
203    uint32_t dimX;
204    uint32_t dimY;
205    uint32_t dimZ;
206    uint32_t dimArray;
207} MTLaunchStruct;
208typedef int (*rs_t)(const void *, void *, const void *, uint32_t, uint32_t, uint32_t, uint32_t);
209
210static void wc_xy(void *usr, uint32_t idx) {
211    MTLaunchStruct *mtls = (MTLaunchStruct *)usr;
212
213    while (1) {
214        uint32_t slice = (uint32_t)android_atomic_inc(&mtls->mSliceNum);
215        uint32_t yStart = mtls->yStart + slice * mtls->mSliceSize;
216        uint32_t yEnd = yStart + mtls->mSliceSize;
217        yEnd = rsMin(yEnd, mtls->yEnd);
218        if (yEnd <= yStart) {
219            return;
220        }
221
222        //LOGE("usr idx %i, x %i,%i  y %i,%i", idx, mtls->xStart, mtls->xEnd, yStart, yEnd);
223        //LOGE("usr ptr in %p,  out %p", mtls->ptrIn, mtls->ptrOut);
224        for (uint32_t y = yStart; y < yEnd; y++) {
225            uint32_t offset = mtls->dimX * y;
226            uint8_t *xPtrOut = mtls->ptrOut + (mtls->eStrideOut * offset);
227            const uint8_t *xPtrIn = mtls->ptrIn + (mtls->eStrideIn * offset);
228
229            for (uint32_t x = mtls->xStart; x < mtls->xEnd; x++) {
230                ((rs_t)mtls->script->mHal.info.root) (xPtrIn, xPtrOut, mtls->usr, x, y, 0, 0);
231                xPtrIn += mtls->eStrideIn;
232                xPtrOut += mtls->eStrideOut;
233            }
234        }
235    }
236}
237
238static void wc_x(void *usr, uint32_t idx) {
239    MTLaunchStruct *mtls = (MTLaunchStruct *)usr;
240
241    while (1) {
242        uint32_t slice = (uint32_t)android_atomic_inc(&mtls->mSliceNum);
243        uint32_t xStart = mtls->xStart + slice * mtls->mSliceSize;
244        uint32_t xEnd = xStart + mtls->mSliceSize;
245        xEnd = rsMin(xEnd, mtls->xEnd);
246        if (xEnd <= xStart) {
247            return;
248        }
249
250        //LOGE("usr idx %i, x %i,%i  y %i,%i", idx, mtls->xStart, mtls->xEnd, yStart, yEnd);
251        //LOGE("usr ptr in %p,  out %p", mtls->ptrIn, mtls->ptrOut);
252        uint8_t *xPtrOut = mtls->ptrOut + (mtls->eStrideOut * xStart);
253        const uint8_t *xPtrIn = mtls->ptrIn + (mtls->eStrideIn * xStart);
254        for (uint32_t x = xStart; x < xEnd; x++) {
255            ((rs_t)mtls->script->mHal.info.root) (xPtrIn, xPtrOut, mtls->usr, x, 0, 0, 0);
256            xPtrIn += mtls->eStrideIn;
257            xPtrOut += mtls->eStrideOut;
258        }
259    }
260}
261
262void rsdScriptInvokeForEach(const Context *rsc,
263                            Script *s,
264                            uint32_t slot,
265                            const Allocation * ain,
266                            Allocation * aout,
267                            const void * usr,
268                            uint32_t usrLen,
269                            const RsScriptCall *sc) {
270
271    RsdHal * dc = (RsdHal *)rsc->mHal.drv;
272
273    MTLaunchStruct mtls;
274    memset(&mtls, 0, sizeof(mtls));
275
276    if (ain) {
277        mtls.dimX = ain->getType()->getDimX();
278        mtls.dimY = ain->getType()->getDimY();
279        mtls.dimZ = ain->getType()->getDimZ();
280        //mtls.dimArray = ain->getType()->getDimArray();
281    } else if (aout) {
282        mtls.dimX = aout->getType()->getDimX();
283        mtls.dimY = aout->getType()->getDimY();
284        mtls.dimZ = aout->getType()->getDimZ();
285        //mtls.dimArray = aout->getType()->getDimArray();
286    } else {
287        rsc->setError(RS_ERROR_BAD_SCRIPT, "rsForEach called with null allocations");
288        return;
289    }
290
291    if (!sc || (sc->xEnd == 0)) {
292        mtls.xEnd = mtls.dimX;
293    } else {
294        rsAssert(sc->xStart < mtls.dimX);
295        rsAssert(sc->xEnd <= mtls.dimX);
296        rsAssert(sc->xStart < sc->xEnd);
297        mtls.xStart = rsMin(mtls.dimX, sc->xStart);
298        mtls.xEnd = rsMin(mtls.dimX, sc->xEnd);
299        if (mtls.xStart >= mtls.xEnd) return;
300    }
301
302    if (!sc || (sc->yEnd == 0)) {
303        mtls.yEnd = mtls.dimY;
304    } else {
305        rsAssert(sc->yStart < mtls.dimY);
306        rsAssert(sc->yEnd <= mtls.dimY);
307        rsAssert(sc->yStart < sc->yEnd);
308        mtls.yStart = rsMin(mtls.dimY, sc->yStart);
309        mtls.yEnd = rsMin(mtls.dimY, sc->yEnd);
310        if (mtls.yStart >= mtls.yEnd) return;
311    }
312
313    mtls.xEnd = rsMax((uint32_t)1, mtls.xEnd);
314    mtls.yEnd = rsMax((uint32_t)1, mtls.yEnd);
315    mtls.zEnd = rsMax((uint32_t)1, mtls.zEnd);
316    mtls.arrayEnd = rsMax((uint32_t)1, mtls.arrayEnd);
317
318    rsAssert(!ain || (ain->getType()->getDimZ() == 0));
319
320    Context *mrsc = (Context *)rsc;
321    Script * oldTLS = setTLS(s);
322
323    mtls.rsc = mrsc;
324    mtls.ain = ain;
325    mtls.aout = aout;
326    mtls.script = s;
327    mtls.usr = usr;
328    mtls.mSliceSize = 10;
329    mtls.mSliceNum = 0;
330
331    mtls.ptrIn = NULL;
332    mtls.eStrideIn = 0;
333    if (ain) {
334        mtls.ptrIn = (const uint8_t *)ain->getPtr();
335        mtls.eStrideIn = ain->getType()->getElementSizeBytes();
336    }
337
338    mtls.ptrOut = NULL;
339    mtls.eStrideOut = 0;
340    if (aout) {
341        mtls.ptrOut = (uint8_t *)aout->getPtr();
342        mtls.eStrideOut = aout->getType()->getElementSizeBytes();
343    }
344
345    if ((dc->mWorkers.mCount > 1) && s->mHal.info.isThreadable) {
346        if (mtls.dimY > 1) {
347            rsdLaunchThreads(mrsc, wc_xy, &mtls);
348        } else {
349            rsdLaunchThreads(mrsc, wc_x, &mtls);
350        }
351
352        //LOGE("launch 1");
353    } else {
354        //LOGE("launch 3");
355        for (uint32_t ar = mtls.arrayStart; ar < mtls.arrayEnd; ar++) {
356            for (uint32_t z = mtls.zStart; z < mtls.zEnd; z++) {
357                for (uint32_t y = mtls.yStart; y < mtls.yEnd; y++) {
358                    uint32_t offset = mtls.dimX * mtls.dimY * mtls.dimZ * ar +
359                                      mtls.dimX * mtls.dimY * z +
360                                      mtls.dimX * y;
361                    uint8_t *xPtrOut = mtls.ptrOut + (mtls.eStrideOut * offset);
362                    const uint8_t *xPtrIn = mtls.ptrIn + (mtls.eStrideIn * offset);
363
364                    for (uint32_t x = mtls.xStart; x < mtls.xEnd; x++) {
365                        ((rs_t)s->mHal.info.root) (xPtrIn, xPtrOut, usr, x, y, z, ar);
366                        xPtrIn += mtls.eStrideIn;
367                        xPtrOut += mtls.eStrideOut;
368                    }
369                }
370            }
371        }
372    }
373
374    setTLS(oldTLS);
375}
376
377
378int rsdScriptInvokeRoot(const Context *dc, Script *script) {
379    DrvScript *drv = (DrvScript *)script->mHal.drv;
380
381    Script * oldTLS = setTLS(script);
382    int ret = drv->mRoot();
383    setTLS(oldTLS);
384
385    return ret;
386}
387
388void rsdScriptInvokeInit(const Context *dc, Script *script) {
389    DrvScript *drv = (DrvScript *)script->mHal.drv;
390
391    if (drv->mInit) {
392        drv->mInit();
393    }
394}
395
396
397void rsdScriptInvokeFunction(const Context *dc, Script *script,
398                            uint32_t slot,
399                            const void *params,
400                            size_t paramLength) {
401    DrvScript *drv = (DrvScript *)script->mHal.drv;
402    //LOGE("invoke %p %p %i %p %i", dc, script, slot, params, paramLength);
403
404    Script * oldTLS = setTLS(script);
405    ((void (*)(const void *, uint32_t))
406        drv->mInvokeFunctions[slot])(params, paramLength);
407    setTLS(oldTLS);
408}
409
410void rsdScriptSetGlobalVar(const Context *dc, const Script *script,
411                           uint32_t slot, void *data, size_t dataLength) {
412    DrvScript *drv = (DrvScript *)script->mHal.drv;
413    //rsAssert(!script->mFieldIsObject[slot]);
414    //LOGE("setGlobalVar %p %p %i %p %i", dc, script, slot, data, dataLength);
415
416    int32_t *destPtr = ((int32_t **)drv->mFieldAddress)[slot];
417    if (!destPtr) {
418        //LOGV("Calling setVar on slot = %i which is null", slot);
419        return;
420    }
421
422    memcpy(destPtr, data, dataLength);
423}
424
425void rsdScriptSetGlobalBind(const Context *dc, const Script *script, uint32_t slot, void *data) {
426    DrvScript *drv = (DrvScript *)script->mHal.drv;
427    //rsAssert(!script->mFieldIsObject[slot]);
428    //LOGE("setGlobalBind %p %p %i %p", dc, script, slot, data);
429
430    int32_t *destPtr = ((int32_t **)drv->mFieldAddress)[slot];
431    if (!destPtr) {
432        //LOGV("Calling setVar on slot = %i which is null", slot);
433        return;
434    }
435
436    memcpy(destPtr, &data, sizeof(void *));
437}
438
439void rsdScriptSetGlobalObj(const Context *dc, const Script *script, uint32_t slot, ObjectBase *data) {
440    DrvScript *drv = (DrvScript *)script->mHal.drv;
441    //rsAssert(script->mFieldIsObject[slot]);
442    //LOGE("setGlobalObj %p %p %i %p", dc, script, slot, data);
443
444    int32_t *destPtr = ((int32_t **)drv->mFieldAddress)[slot];
445    if (!destPtr) {
446        //LOGV("Calling setVar on slot = %i which is null", slot);
447        return;
448    }
449
450    rsrSetObject(dc, script, (ObjectBase **)destPtr, data);
451}
452
453void rsdScriptDestroy(const Context *dc, Script *script) {
454    DrvScript *drv = (DrvScript *)script->mHal.drv;
455
456    if (drv->mFieldAddress) {
457        size_t exportVarCount = drv->ME->getExportVarCount();
458        for (size_t ct = 0; ct < exportVarCount; ct++) {
459            if (drv->mFieldIsObject[ct]) {
460                // The field address can be NULL if the script-side has
461                // optimized the corresponding global variable away.
462                if (drv->mFieldAddress[ct]) {
463                    rsrClearObject(dc, script, (ObjectBase **)drv->mFieldAddress[ct]);
464                }
465            }
466        }
467        free(drv->mFieldAddress);
468        drv->mFieldAddress = NULL;
469        free(drv->mFieldIsObject);
470        drv->mFieldIsObject = NULL;    }
471
472    if (drv->mInvokeFunctions) {
473        free(drv->mInvokeFunctions);
474        drv->mInvokeFunctions = NULL;
475    }
476
477    delete drv->ME;
478    drv->ME = NULL;
479
480    free(drv);
481    script->mHal.drv = NULL;
482
483}
484
485
486