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