rsScriptC.cpp revision 1884278a974c62718aacd9b6c0da8d6871631bd8
1/*
2 * Copyright (C) 2009 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 "rsContext.h"
18#include "rsScriptC.h"
19#include "rsMatrix.h"
20#include "../../compile/libbcc/include/bcc/bcc.h"
21#include "utils/Timers.h"
22#include "utils/StopWatch.h"
23extern "C" {
24#include "libdex/ZipArchive.h"
25}
26
27#include <GLES/gl.h>
28#include <GLES/glext.h>
29
30using namespace android;
31using namespace android::renderscript;
32
33#define GET_TLS()  Context::ScriptTLSStruct * tls = \
34    (Context::ScriptTLSStruct *)pthread_getspecific(Context::gThreadTLSKey); \
35    Context * rsc = tls->mContext; \
36    ScriptC * sc = (ScriptC *) tls->mScript
37
38
39ScriptC::ScriptC(Context *rsc) : Script(rsc) {
40    LOGD(">>>> ScriptC ctor called, obj=%p", this);
41    mBccScript = NULL;
42    memset(&mProgram, 0, sizeof(mProgram));
43}
44
45ScriptC::~ScriptC() {
46    LOGD(">>>> ~ScriptC() mBccScript = %p", mBccScript);
47    if (mBccScript) {
48        bccDeleteScript(mBccScript);
49        LOGD(">>>> ~ScriptC(mBCCScript)");
50    }
51    free(mEnviroment.mScriptText);
52    mEnviroment.mScriptText = NULL;
53}
54
55void ScriptC::setupScript(Context *rsc) {
56    mEnviroment.mStartTimeMillis
57                = nanoseconds_to_milliseconds(systemTime(SYSTEM_TIME_MONOTONIC));
58
59    for (uint32_t ct=0; ct < mEnviroment.mFieldCount; ct++) {
60        if (mSlots[ct].get() && !mTypes[ct].get()) {
61            mTypes[ct].set(mSlots[ct]->getType());
62        }
63
64        if (!mTypes[ct].get())
65            continue;
66        void *ptr = NULL;
67        if (mSlots[ct].get()) {
68            ptr = mSlots[ct]->getPtr();
69        }
70        void **dest = ((void ***)mEnviroment.mFieldAddress)[ct];
71
72        if (rsc->props.mLogScripts) {
73            if (mSlots[ct].get() != NULL) {
74                LOGV("%p ScriptC::setupScript slot=%i  dst=%p  src=%p  type=%p", rsc, ct, dest, ptr, mSlots[ct]->getType());
75            } else {
76                LOGV("%p ScriptC::setupScript slot=%i  dst=%p  src=%p  type=null", rsc, ct, dest, ptr);
77            }
78        }
79
80        if (dest) {
81            *dest = ptr;
82        }
83    }
84}
85
86const Allocation *ScriptC::ptrToAllocation(const void *ptr) const {
87    if (!ptr) {
88        return NULL;
89    }
90    for (uint32_t ct=0; ct < mEnviroment.mFieldCount; ct++) {
91        if (!mSlots[ct].get())
92            continue;
93        if (mSlots[ct]->getPtr() == ptr) {
94            return mSlots[ct].get();
95        }
96    }
97    LOGE("ScriptC::ptrToAllocation, failed to find %p", ptr);
98    return NULL;
99}
100
101Script * ScriptC::setTLS(Script *sc) {
102    Context::ScriptTLSStruct * tls = (Context::ScriptTLSStruct *)
103                                  pthread_getspecific(Context::gThreadTLSKey);
104    rsAssert(tls);
105    Script *old = tls->mScript;
106    tls->mScript = sc;
107    return old;
108}
109
110void ScriptC::setupGLState(Context *rsc) {
111    if (mEnviroment.mFragmentStore.get()) {
112        rsc->setProgramStore(mEnviroment.mFragmentStore.get());
113    }
114    if (mEnviroment.mFragment.get()) {
115        rsc->setProgramFragment(mEnviroment.mFragment.get());
116    }
117    if (mEnviroment.mVertex.get()) {
118        rsc->setProgramVertex(mEnviroment.mVertex.get());
119    }
120    if (mEnviroment.mRaster.get()) {
121        rsc->setProgramRaster(mEnviroment.mRaster.get());
122    }
123}
124
125uint32_t ScriptC::run(Context *rsc) {
126    if (mProgram.mRoot == NULL) {
127        rsc->setError(RS_ERROR_BAD_SCRIPT, "Attempted to run bad script");
128        return 0;
129    }
130
131    setupGLState(rsc);
132    setupScript(rsc);
133
134    uint32_t ret = 0;
135    Script * oldTLS = setTLS(this);
136
137    if (rsc->props.mLogScripts) {
138        LOGV("%p ScriptC::run invoking root,  ptr %p", rsc, mProgram.mRoot);
139    }
140
141    ret = mProgram.mRoot();
142
143    if (rsc->props.mLogScripts) {
144        LOGV("%p ScriptC::run invoking complete, ret=%i", rsc, ret);
145    }
146
147    setTLS(oldTLS);
148    return ret;
149}
150
151typedef struct {
152    Context *rsc;
153    ScriptC *script;
154    const Allocation * ain;
155    Allocation * aout;
156    const void * usr;
157
158    uint32_t mSliceSize;
159    volatile int mSliceNum;
160
161    const uint8_t *ptrIn;
162    uint32_t eStrideIn;
163    uint8_t *ptrOut;
164    uint32_t eStrideOut;
165
166    uint32_t xStart;
167    uint32_t xEnd;
168    uint32_t yStart;
169    uint32_t yEnd;
170    uint32_t zStart;
171    uint32_t zEnd;
172    uint32_t arrayStart;
173    uint32_t arrayEnd;
174
175    uint32_t dimX;
176    uint32_t dimY;
177    uint32_t dimZ;
178    uint32_t dimArray;
179} MTLaunchStruct;
180typedef int (*rs_t)(const void *, void *, const void *, uint32_t, uint32_t, uint32_t, uint32_t);
181
182static void wc_xy(void *usr, uint32_t idx) {
183    MTLaunchStruct *mtls = (MTLaunchStruct *)usr;
184
185    while (1) {
186        uint32_t slice = (uint32_t)android_atomic_inc(&mtls->mSliceNum);
187        uint32_t yStart = mtls->yStart + slice * mtls->mSliceSize;
188        uint32_t yEnd = yStart + mtls->mSliceSize;
189        yEnd = rsMin(yEnd, mtls->yEnd);
190        if (yEnd <= yStart) {
191            return;
192        }
193
194        //LOGE("usr idx %i, x %i,%i  y %i,%i", idx, mtls->xStart, mtls->xEnd, yStart, yEnd);
195        //LOGE("usr ptr in %p,  out %p", mtls->ptrIn, mtls->ptrOut);
196        for (uint32_t y = yStart; y < yEnd; y++) {
197            uint32_t offset = mtls->dimX * y;
198            uint8_t *xPtrOut = mtls->ptrOut + (mtls->eStrideOut * offset);
199            const uint8_t *xPtrIn = mtls->ptrIn + (mtls->eStrideIn * offset);
200
201            for (uint32_t x = mtls->xStart; x < mtls->xEnd; x++) {
202                ((rs_t)mtls->script->mProgram.mRoot) (xPtrIn, xPtrOut, mtls->usr, x, y, 0, 0);
203                xPtrIn += mtls->eStrideIn;
204                xPtrOut += mtls->eStrideOut;
205            }
206        }
207    }
208}
209
210static void wc_x(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 xStart = mtls->xStart + slice * mtls->mSliceSize;
216        uint32_t xEnd = xStart + mtls->mSliceSize;
217        xEnd = rsMin(xEnd, mtls->xEnd);
218        if (xEnd <= xStart) {
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        uint8_t *xPtrOut = mtls->ptrOut + (mtls->eStrideOut * xStart);
225        const uint8_t *xPtrIn = mtls->ptrIn + (mtls->eStrideIn * xStart);
226        for (uint32_t x = xStart; x < xEnd; x++) {
227            ((rs_t)mtls->script->mProgram.mRoot) (xPtrIn, xPtrOut, mtls->usr, x, 0, 0, 0);
228            xPtrIn += mtls->eStrideIn;
229            xPtrOut += mtls->eStrideOut;
230        }
231    }
232}
233
234void ScriptC::runForEach(Context *rsc,
235                         const Allocation * ain,
236                         Allocation * aout,
237                         const void * usr,
238                         const RsScriptCall *sc) {
239    MTLaunchStruct mtls;
240    memset(&mtls, 0, sizeof(mtls));
241    Context::PushState ps(rsc);
242
243    if (ain) {
244        mtls.dimX = ain->getType()->getDimX();
245        mtls.dimY = ain->getType()->getDimY();
246        mtls.dimZ = ain->getType()->getDimZ();
247        //mtls.dimArray = ain->getType()->getDimArray();
248    } else if (aout) {
249        mtls.dimX = aout->getType()->getDimX();
250        mtls.dimY = aout->getType()->getDimY();
251        mtls.dimZ = aout->getType()->getDimZ();
252        //mtls.dimArray = aout->getType()->getDimArray();
253    } else {
254        rsc->setError(RS_ERROR_BAD_SCRIPT, "rsForEach called with null allocations");
255        return;
256    }
257
258    if (!sc || (sc->xEnd == 0)) {
259        mtls.xEnd = mtls.dimX;
260    } else {
261        rsAssert(sc->xStart < mtls.dimX);
262        rsAssert(sc->xEnd <= mtls.dimX);
263        rsAssert(sc->xStart < sc->xEnd);
264        mtls.xStart = rsMin(mtls.dimX, sc->xStart);
265        mtls.xEnd = rsMin(mtls.dimX, sc->xEnd);
266        if (mtls.xStart >= mtls.xEnd) return;
267    }
268
269    if (!sc || (sc->yEnd == 0)) {
270        mtls.yEnd = mtls.dimY;
271    } else {
272        rsAssert(sc->yStart < mtls.dimY);
273        rsAssert(sc->yEnd <= mtls.dimY);
274        rsAssert(sc->yStart < sc->yEnd);
275        mtls.yStart = rsMin(mtls.dimY, sc->yStart);
276        mtls.yEnd = rsMin(mtls.dimY, sc->yEnd);
277        if (mtls.yStart >= mtls.yEnd) return;
278    }
279
280    mtls.xEnd = rsMax((uint32_t)1, mtls.xEnd);
281    mtls.yEnd = rsMax((uint32_t)1, mtls.yEnd);
282    mtls.zEnd = rsMax((uint32_t)1, mtls.zEnd);
283    mtls.arrayEnd = rsMax((uint32_t)1, mtls.arrayEnd);
284
285    rsAssert(ain->getType()->getDimZ() == 0);
286
287    setupGLState(rsc);
288    setupScript(rsc);
289    Script * oldTLS = setTLS(this);
290
291    mtls.rsc = rsc;
292    mtls.ain = ain;
293    mtls.aout = aout;
294    mtls.script = this;
295    mtls.usr = usr;
296    mtls.mSliceSize = 10;
297    mtls.mSliceNum = 0;
298
299    mtls.ptrIn = NULL;
300    mtls.eStrideIn = 0;
301    if (ain) {
302        mtls.ptrIn = (const uint8_t *)ain->getPtr();
303        mtls.eStrideIn = ain->getType()->getElementSizeBytes();
304    }
305
306    mtls.ptrOut = NULL;
307    mtls.eStrideOut = 0;
308    if (aout) {
309        mtls.ptrOut = (uint8_t *)aout->getPtr();
310        mtls.eStrideOut = aout->getType()->getElementSizeBytes();
311    }
312
313    if ((rsc->getWorkerPoolSize() > 1) && mEnviroment.mIsThreadable) {
314        if (mtls.dimY > 1) {
315            rsc->launchThreads(wc_xy, &mtls);
316        } else {
317            rsc->launchThreads(wc_x, &mtls);
318        }
319
320        //LOGE("launch 1");
321    } else {
322        //LOGE("launch 3");
323        for (uint32_t ar = mtls.arrayStart; ar < mtls.arrayEnd; ar++) {
324            for (uint32_t z = mtls.zStart; z < mtls.zEnd; z++) {
325                for (uint32_t y = mtls.yStart; y < mtls.yEnd; y++) {
326                    uint32_t offset = mtls.dimX * mtls.dimY * mtls.dimZ * ar +
327                                      mtls.dimX * mtls.dimY * z +
328                                      mtls.dimX * y;
329                    uint8_t *xPtrOut = mtls.ptrOut + (mtls.eStrideOut * offset);
330                    const uint8_t *xPtrIn = mtls.ptrIn + (mtls.eStrideIn * offset);
331
332                    for (uint32_t x = mtls.xStart; x < mtls.xEnd; x++) {
333                        ((rs_t)mProgram.mRoot) (xPtrIn, xPtrOut, usr, x, y, z, ar);
334                        xPtrIn += mtls.eStrideIn;
335                        xPtrOut += mtls.eStrideOut;
336                    }
337                }
338            }
339        }
340    }
341
342    setTLS(oldTLS);
343}
344
345void ScriptC::Invoke(Context *rsc, uint32_t slot, const void *data, uint32_t len) {
346    if ((slot >= mEnviroment.mInvokeFunctionCount) ||
347        (mEnviroment.mInvokeFunctions[slot] == NULL)) {
348        rsc->setError(RS_ERROR_BAD_SCRIPT, "Calling invoke on bad script");
349        return;
350    }
351    setupScript(rsc);
352    Script * oldTLS = setTLS(this);
353
354    if (rsc->props.mLogScripts) {
355        LOGV("%p ScriptC::Invoke invoking slot %i,  ptr %p", rsc, slot, mEnviroment.mInvokeFunctions[slot]);
356    }
357    ((void (*)(const void *, uint32_t))
358        mEnviroment.mInvokeFunctions[slot])(data, len);
359    if (rsc->props.mLogScripts) {
360        LOGV("%p ScriptC::Invoke complete", rsc);
361    }
362
363    setTLS(oldTLS);
364}
365
366ScriptCState::ScriptCState() {
367    mScript.clear();
368}
369
370ScriptCState::~ScriptCState() {
371    mScript.clear();
372}
373
374void ScriptCState::init(Context *rsc) {
375    clear(rsc);
376}
377
378void ScriptCState::clear(Context *rsc) {
379    rsAssert(rsc);
380    mScript.clear();
381    mScript.set(new ScriptC(rsc));
382}
383
384static BCCvoid* symbolLookup(BCCvoid* pContext, const BCCchar* name) {
385    const ScriptCState::SymbolTable_t *sym;
386    ScriptC *s = (ScriptC *)pContext;
387    if (!strcmp(name, "__isThreadable")) {
388      return (BCCvoid*) s->mEnviroment.mIsThreadable;
389    } else if (!strcmp(name, "__clearThreadable")) {
390      s->mEnviroment.mIsThreadable = false;
391      return NULL;
392    }
393    sym = ScriptCState::lookupSymbol(name);
394    if (!sym) {
395        sym = ScriptCState::lookupSymbolCL(name);
396    }
397    if (!sym) {
398        sym = ScriptCState::lookupSymbolGL(name);
399    }
400    if (sym) {
401        s->mEnviroment.mIsThreadable &= sym->threadable;
402        return sym->mPtr;
403    }
404    LOGE("ScriptC sym lookup failed for %s", name);
405    return NULL;
406}
407
408extern const char rs_runtime_lib_bc[];
409extern unsigned rs_runtime_lib_bc_size;
410
411void ScriptCState::runCompiler(Context *rsc,
412                               ScriptC *s,
413                               const char *resName,
414                               const char *cacheDir) {
415    {
416        s->mBccScript = bccCreateScript();
417
418        s->mEnviroment.mIsThreadable = true;
419
420        bccRegisterSymbolCallback(s->mBccScript, symbolLookup, s);
421
422        if (bccReadBC(s->mBccScript,
423                      s->mEnviroment.mScriptText,
424                      s->mEnviroment.mScriptTextLength,
425                      /*deprecated*/ 0, /*deprecated*/ 0,
426                      resName, cacheDir) != 0) {
427            LOGE("bcc: FAILS to read bitcode");
428            // Handle Fatal Error
429        }
430
431#if 0
432        if (bccLinkBC(s->mBccScript,
433                      rs_runtime_lib_bc,
434                      rs_runtime_lib_bc_size) != 0) {
435            LOGE("bcc: FAILS to link bitcode");
436            // Handle Fatal Error
437        }
438#endif
439
440        if (bccPrepareExecutable(s->mBccScript) != 0) {
441            LOGE("bcc: FAILS to prepare executable");
442            // Handle Fatal Error
443        }
444
445        bccGetScriptLabel(s->mBccScript, "root", (BCCvoid**) &s->mProgram.mRoot);
446        bccGetScriptLabel(s->mBccScript, "init", (BCCvoid**) &s->mProgram.mInit);
447    }
448    LOGV("%p ScriptCState::runCompiler root %p,  init %p", rsc, s->mProgram.mRoot, s->mProgram.mInit);
449
450    if (s->mProgram.mInit) {
451        s->mProgram.mInit();
452    }
453
454    bccGetExportFuncs(s->mBccScript, (BCCsizei*) &s->mEnviroment.mInvokeFunctionCount, 0, NULL);
455    if (s->mEnviroment.mInvokeFunctionCount <= 0)
456        s->mEnviroment.mInvokeFunctions = NULL;
457    else {
458        s->mEnviroment.mInvokeFunctions = (Script::InvokeFunc_t*) calloc(s->mEnviroment.mInvokeFunctionCount, sizeof(Script::InvokeFunc_t));
459        bccGetExportFuncs(s->mBccScript, NULL, s->mEnviroment.mInvokeFunctionCount, (BCCvoid **) s->mEnviroment.mInvokeFunctions);
460    }
461
462    bccGetExportVars(s->mBccScript, (BCCsizei*) &s->mEnviroment.mFieldCount, 0, NULL);
463    if (s->mEnviroment.mFieldCount <= 0)
464        s->mEnviroment.mFieldAddress = NULL;
465    else {
466        s->mEnviroment.mFieldAddress = (void **) calloc(s->mEnviroment.mFieldCount, sizeof(void *));
467        bccGetExportVars(s->mBccScript, NULL, s->mEnviroment.mFieldCount, (BCCvoid **) s->mEnviroment.mFieldAddress);
468        s->initSlots();
469    }
470
471    s->mEnviroment.mFragment.set(rsc->getDefaultProgramFragment());
472    s->mEnviroment.mVertex.set(rsc->getDefaultProgramVertex());
473    s->mEnviroment.mFragmentStore.set(rsc->getDefaultProgramStore());
474    s->mEnviroment.mRaster.set(rsc->getDefaultProgramRaster());
475
476    if (s->mProgram.mRoot) {
477        const static int pragmaMax = 16;
478        BCCsizei pragmaCount;
479        BCCchar * str[pragmaMax];
480        bccGetPragmas(s->mBccScript, &pragmaCount, pragmaMax, &str[0]);
481
482        for (int ct=0; ct < pragmaCount; ct+=2) {
483            //LOGE("pragme %s %s", str[ct], str[ct+1]);
484            if (!strcmp(str[ct], "version")) {
485                continue;
486            }
487
488            if (!strcmp(str[ct], "stateVertex")) {
489                if (!strcmp(str[ct+1], "default")) {
490                    continue;
491                }
492                if (!strcmp(str[ct+1], "parent")) {
493                    s->mEnviroment.mVertex.clear();
494                    continue;
495                }
496                LOGE("Unreconized value %s passed to stateVertex", str[ct+1]);
497            }
498
499            if (!strcmp(str[ct], "stateRaster")) {
500                if (!strcmp(str[ct+1], "default")) {
501                    continue;
502                }
503                if (!strcmp(str[ct+1], "parent")) {
504                    s->mEnviroment.mRaster.clear();
505                    continue;
506                }
507                LOGE("Unreconized value %s passed to stateRaster", str[ct+1]);
508            }
509
510            if (!strcmp(str[ct], "stateFragment")) {
511                if (!strcmp(str[ct+1], "default")) {
512                    continue;
513                }
514                if (!strcmp(str[ct+1], "parent")) {
515                    s->mEnviroment.mFragment.clear();
516                    continue;
517                }
518                LOGE("Unreconized value %s passed to stateFragment", str[ct+1]);
519            }
520
521            if (!strcmp(str[ct], "stateStore")) {
522                if (!strcmp(str[ct+1], "default")) {
523                    continue;
524                }
525                if (!strcmp(str[ct+1], "parent")) {
526                    s->mEnviroment.mFragmentStore.clear();
527                    continue;
528                }
529                LOGE("Unreconized value %s passed to stateStore", str[ct+1]);
530            }
531
532        }
533
534
535    } else {
536        LOGE("bcc: FAILS to prepare executable");
537        // Handle Fatal Error
538    }
539}
540
541namespace android {
542namespace renderscript {
543
544void rsi_ScriptCBegin(Context * rsc) {
545    ScriptCState *ss = &rsc->mScriptC;
546    ss->clear(rsc);
547}
548
549void rsi_ScriptCSetText(Context *rsc, const char *text, uint32_t len) {
550    ScriptCState *ss = &rsc->mScriptC;
551
552    char *t = (char *)malloc(len + 1);
553    memcpy(t, text, len);
554    t[len] = 0;
555    ss->mScript->mEnviroment.mScriptText = t;
556    ss->mScript->mEnviroment.mScriptTextLength = len;
557}
558
559
560RsScript rsi_ScriptCCreate(Context *rsc,
561                           const char *packageName /* deprecated */,
562                           const char *resName,
563                           const char *cacheDir)
564{
565    ScriptCState *ss = &rsc->mScriptC;
566
567    ObjectBaseRef<ScriptC> s(ss->mScript);
568    ss->mScript.clear();
569    s->incUserRef();
570
571    ss->runCompiler(rsc, s.get(), resName, cacheDir);
572    ss->clear(rsc);
573    return s.get();
574}
575
576}
577}
578