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