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