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