rsdScriptGroup.cpp revision 78b050ea1e13108110ce3b8ead63252e5ebe2468
1/*
2 * Copyright (C) 2011 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 "rsdCore.h"
18
19#include <bcc/BCCContext.h>
20#include <bcc/Renderscript/RSCompilerDriver.h>
21#include <bcc/Renderscript/RSExecutable.h>
22#include <bcc/Renderscript/RSInfo.h>
23
24#include "rsScript.h"
25#include "rsScriptGroup.h"
26#include "rsdScriptGroup.h"
27#include "rsdBcc.h"
28
29using namespace android;
30using namespace android::renderscript;
31
32
33bool rsdScriptGroupInit(const android::renderscript::Context *rsc,
34                        const android::renderscript::ScriptGroup *sg) {
35    return true;
36}
37
38void rsdScriptGroupSetInput(const android::renderscript::Context *rsc,
39                            const android::renderscript::ScriptGroup *sg,
40                            const android::renderscript::ScriptKernelID *kid,
41                            android::renderscript::Allocation *) {
42}
43
44void rsdScriptGroupSetOutput(const android::renderscript::Context *rsc,
45                             const android::renderscript::ScriptGroup *sg,
46                             const android::renderscript::ScriptKernelID *kid,
47                             android::renderscript::Allocation *) {
48}
49
50void rsdScriptGroupExecute(const android::renderscript::Context *rsc,
51                           const android::renderscript::ScriptGroup *sg) {
52
53    Vector<Allocation *> ins;
54    Vector<Allocation *> outs;
55    Vector<const ScriptKernelID *> kernels;
56
57    for (size_t ct=0; ct < sg->mNodes.size(); ct++) {
58        ScriptGroup::Node *n = sg->mNodes[ct];
59        Script *s = n->mKernels[0]->mScript;
60
61        //ALOGE("node %i, order %i, in %i out %i", (int)ct, n->mOrder, (int)n->mInputs.size(), (int)n->mOutputs.size());
62
63        for (size_t ct2=0; ct2 < n->mInputs.size(); ct2++) {
64            if (n->mInputs[ct2]->mDstField.get() && n->mInputs[ct2]->mDstField->mScript) {
65                //ALOGE("field %p %zu", n->mInputs[ct2]->mDstField->mScript, n->mInputs[ct2]->mDstField->mSlot);
66                s->setVarObj(n->mInputs[ct2]->mDstField->mSlot, n->mInputs[ct2]->mAlloc.get());
67            }
68        }
69
70        for (size_t ct2=0; ct2 < n->mKernels.size(); ct2++) {
71            const ScriptKernelID *k = n->mKernels[ct2];
72            Allocation *ain = NULL;
73            Allocation *aout = NULL;
74
75            for (size_t ct3=0; ct3 < n->mInputs.size(); ct3++) {
76                if (n->mInputs[ct3]->mDstKernel.get() == k) {
77                    ain = n->mInputs[ct3]->mAlloc.get();
78                    //ALOGE(" link in %p", ain);
79                }
80            }
81            for (size_t ct3=0; ct3 < sg->mInputs.size(); ct3++) {
82                if (sg->mInputs[ct3]->mKernel == k) {
83                    ain = sg->mInputs[ct3]->mAlloc.get();
84                    //ALOGE(" io in %p", ain);
85                }
86            }
87
88            for (size_t ct3=0; ct3 < n->mOutputs.size(); ct3++) {
89                if (n->mOutputs[ct3]->mSource.get() == k) {
90                    aout = n->mOutputs[ct3]->mAlloc.get();
91                    //ALOGE(" link out %p", aout);
92                }
93            }
94            for (size_t ct3=0; ct3 < sg->mOutputs.size(); ct3++) {
95                if (sg->mOutputs[ct3]->mKernel == k) {
96                    aout = sg->mOutputs[ct3]->mAlloc.get();
97                    //ALOGE(" io out %p", aout);
98                }
99            }
100
101            if ((k->mHasKernelOutput == (aout != NULL)) &&
102                (k->mHasKernelInput == (ain != NULL))) {
103                ins.add(ain);
104                outs.add(aout);
105                kernels.add(k);
106            }
107        }
108
109    }
110
111    RsdHal * dc = (RsdHal *)rsc->mHal.drv;
112    MTLaunchStruct mtls;
113    for (size_t ct=0; ct < ins.size(); ct++) {
114
115        Script *s = kernels[ct]->mScript;
116        DrvScript *drv = (DrvScript *)s->mHal.drv;
117        uint32_t slot = kernels[ct]->mSlot;
118
119        rsdScriptInvokeForEachMtlsSetup(rsc, ins[ct], outs[ct], NULL, 0, NULL, &mtls);
120        mtls.script = s;
121        mtls.fep.slot = slot;
122
123        if (drv->mIntrinsicID) {
124            mtls.kernel = (void (*)())drv->mIntrinsicFuncs.root;
125            mtls.fep.usr = drv->mIntrinsicData;
126        } else {
127            mtls.kernel = reinterpret_cast<ForEachFunc_t>(
128                              drv->mExecutable->getExportForeachFuncAddrs()[slot]);
129            rsAssert(mtls.kernel != NULL);
130            mtls.sig = drv->mExecutable->getInfo().getExportForeachFuncs()[slot].second;
131        }
132
133//        typedef void (*outer_foreach_t)(
134  //          const android::renderscript::RsForEachStubParamStruct *,
135    //        uint32_t x1, uint32_t x2,
136      //      uint32_t instep, uint32_t outstep);
137        //outer_foreach_t fn = (outer_foreach_t) mtls->kernel;
138
139        rsdScriptLaunchThreads(rsc, s, slot, ins[ct], outs[ct], NULL, 0, NULL, &mtls);
140    }
141
142}
143
144void rsdScriptGroupDestroy(const android::renderscript::Context *rsc,
145                           const android::renderscript::ScriptGroup *sg) {
146}
147
148
149