blob: 1d26f59db882f2270c4223187d7e38918d2428ef [file] [log] [blame]
Jason Sams709a0972012-11-15 18:18:04 -08001/*
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 "rsCpuCore.h"
18#include "rsCpuScript.h"
Jason Sams709a0972012-11-15 18:18:04 -080019#include "rsScriptGroup.h"
20#include "rsCpuScriptGroup.h"
21//#include "rsdBcc.h"
22//#include "rsdAllocation.h"
23
24using namespace android;
25using namespace android::renderscript;
26
27CpuScriptGroupImpl::CpuScriptGroupImpl(RsdCpuReferenceImpl *ctx, const ScriptGroup *sg) {
28 mCtx = ctx;
29 mSG = sg;
30}
31
32CpuScriptGroupImpl::~CpuScriptGroupImpl() {
33
34}
35
36bool CpuScriptGroupImpl::init() {
37 return true;
38}
39
40void CpuScriptGroupImpl::setInput(const ScriptKernelID *kid, Allocation *a) {
41}
42
43void CpuScriptGroupImpl::setOutput(const ScriptKernelID *kid, Allocation *a) {
44}
45
46
Chris Wailes80ef6932014-07-08 11:22:18 -070047typedef void (*ScriptGroupRootFunc_t)(const RsExpandKernelParams *kparams,
Jason Sams709a0972012-11-15 18:18:04 -080048 uint32_t xstart, uint32_t xend,
Chris Wailes9ed79102014-07-25 15:53:28 -070049 uint32_t outstep);
Jason Sams709a0972012-11-15 18:18:04 -080050
Chris Wailes80ef6932014-07-08 11:22:18 -070051void CpuScriptGroupImpl::scriptGroupRoot(const RsExpandKernelParams *kparams,
Jason Sams709a0972012-11-15 18:18:04 -080052 uint32_t xstart, uint32_t xend,
Chris Wailes9ed79102014-07-25 15:53:28 -070053 uint32_t outstep) {
Jason Sams709a0972012-11-15 18:18:04 -080054
55
Chris Wailesf3712132014-07-16 15:18:30 -070056 const ScriptList *sl = (const ScriptList *)kparams->usr;
Chris Wailes80ef6932014-07-08 11:22:18 -070057 RsExpandKernelParams *mkparams = (RsExpandKernelParams *)kparams;
Jason Sams709a0972012-11-15 18:18:04 -080058
Chris Wailesf3712132014-07-16 15:18:30 -070059 const void **oldIns = mkparams->ins;
60 uint32_t *oldStrides = mkparams->inEStrides;
61
62 void *localIns[1];
63 uint32_t localStride[1];
64
65 mkparams->ins = (const void**)localIns;
66 mkparams->inEStrides = localStride;
67
Chris Wailes80ef6932014-07-08 11:22:18 -070068 for (size_t ct = 0; ct < sl->count; ct++) {
Jason Sams709a0972012-11-15 18:18:04 -080069 ScriptGroupRootFunc_t func;
Chris Wailes80ef6932014-07-08 11:22:18 -070070 func = (ScriptGroupRootFunc_t)sl->fnPtrs[ct];
71 mkparams->usr = sl->usrPtrs[ct];
Jason Sams709a0972012-11-15 18:18:04 -080072
Stephen Hines4b2bea32014-08-13 17:32:10 +000073 if (sl->ins[ct]) {
Chris Wailesf3712132014-07-16 15:18:30 -070074 localIns[0] = sl->ins[ct]->mHal.drvState.lod[0].mallocPtr;
Stephen Hines4b2bea32014-08-13 17:32:10 +000075
Chris Wailesf3712132014-07-16 15:18:30 -070076 localStride[0] = sl->ins[ct]->mHal.state.elementSizeBytes;
Chris Wailes80ef6932014-07-08 11:22:18 -070077
Jason Sams709a0972012-11-15 18:18:04 -080078 if (sl->inExts[ct]) {
Chris Wailesf3712132014-07-16 15:18:30 -070079 localIns[0] = (void*)
80 ((const uint8_t *)localIns[0] +
81 sl->ins[ct]->mHal.drvState.lod[0].stride * kparams->y);
Chris Wailes80ef6932014-07-08 11:22:18 -070082
83 } else if (sl->ins[ct]->mHal.drvState.lod[0].dimY > kparams->lid) {
Chris Wailesf3712132014-07-16 15:18:30 -070084 localIns[0] = (void*)
85 ((const uint8_t *)localIns[0] +
86 sl->ins[ct]->mHal.drvState.lod[0].stride * kparams->lid);
Jason Sams709a0972012-11-15 18:18:04 -080087 }
Chris Wailesf3712132014-07-16 15:18:30 -070088
89 } else {
90 localIns[0] = NULL;
91 localStride[0] = 0;
Jason Sams709a0972012-11-15 18:18:04 -080092 }
93
Chris Wailesf3712132014-07-16 15:18:30 -070094 uint32_t ostep;
Jason Sams709a0972012-11-15 18:18:04 -080095 if (sl->outs[ct]) {
Chris Wailes80ef6932014-07-08 11:22:18 -070096 mkparams->out =
97 (uint8_t *)sl->outs[ct]->mHal.drvState.lod[0].mallocPtr;
98
Jason Sams17e3cdc2013-09-09 17:32:16 -070099 ostep = sl->outs[ct]->mHal.state.elementSizeBytes;
Chris Wailes80ef6932014-07-08 11:22:18 -0700100
Jason Sams709a0972012-11-15 18:18:04 -0800101 if (sl->outExts[ct]) {
Chris Wailes80ef6932014-07-08 11:22:18 -0700102 mkparams->out =
103 (uint8_t *)mkparams->out +
104 sl->outs[ct]->mHal.drvState.lod[0].stride * kparams->y;
105
106 } else if (sl->outs[ct]->mHal.drvState.lod[0].dimY > kparams->lid) {
107 mkparams->out =
108 (uint8_t *)mkparams->out +
109 sl->outs[ct]->mHal.drvState.lod[0].stride * kparams->lid;
Jason Sams709a0972012-11-15 18:18:04 -0800110 }
Chris Wailesf3712132014-07-16 15:18:30 -0700111 } else {
112 mkparams->out = NULL;
113 ostep = 0;
Jason Sams709a0972012-11-15 18:18:04 -0800114 }
115
116 //ALOGE("kernel %i %p,%p %p,%p", ct, mp->ptrIn, mp->in, mp->ptrOut, mp->out);
Chris Wailes9ed79102014-07-25 15:53:28 -0700117 func(kparams, xstart, xend, ostep);
Jason Sams709a0972012-11-15 18:18:04 -0800118 }
119 //ALOGE("script group root");
120
Chris Wailesf3712132014-07-16 15:18:30 -0700121 mkparams->ins = oldIns;
122 mkparams->inEStrides = oldStrides;
123 mkparams->usr = sl;
Jason Sams709a0972012-11-15 18:18:04 -0800124}
125
126
127
128void CpuScriptGroupImpl::execute() {
Chris Wailes93d6bc82014-07-28 16:54:38 -0700129 std::vector<Allocation *> ins;
130 std::vector<char> inExts;
131 std::vector<Allocation *> outs;
132 std::vector<char> outExts;
133 std::vector<const ScriptKernelID *> kernels;
Jason Sams709a0972012-11-15 18:18:04 -0800134 bool fieldDep = false;
135
136 for (size_t ct=0; ct < mSG->mNodes.size(); ct++) {
137 ScriptGroup::Node *n = mSG->mNodes[ct];
138 Script *s = n->mKernels[0]->mScript;
Stephen Hinesc78839b2013-09-10 17:40:41 -0700139 if (s->hasObjectSlots()) {
140 // Disable the ScriptGroup optimization if we have global RS
141 // objects that might interfere between kernels.
142 fieldDep = true;
143 }
Jason Sams709a0972012-11-15 18:18:04 -0800144
145 //ALOGE("node %i, order %i, in %i out %i", (int)ct, n->mOrder, (int)n->mInputs.size(), (int)n->mOutputs.size());
146
147 for (size_t ct2=0; ct2 < n->mInputs.size(); ct2++) {
148 if (n->mInputs[ct2]->mDstField.get() && n->mInputs[ct2]->mDstField->mScript) {
149 //ALOGE("field %p %zu", n->mInputs[ct2]->mDstField->mScript, n->mInputs[ct2]->mDstField->mSlot);
150 s->setVarObj(n->mInputs[ct2]->mDstField->mSlot, n->mInputs[ct2]->mAlloc.get());
151 }
152 }
153
154 for (size_t ct2=0; ct2 < n->mKernels.size(); ct2++) {
155 const ScriptKernelID *k = n->mKernels[ct2];
156 Allocation *ain = NULL;
157 Allocation *aout = NULL;
158 bool inExt = false;
159 bool outExt = false;
160
161 for (size_t ct3=0; ct3 < n->mInputs.size(); ct3++) {
162 if (n->mInputs[ct3]->mDstKernel.get() == k) {
163 ain = n->mInputs[ct3]->mAlloc.get();
Yang Ni5f6f16f2014-07-25 13:51:09 -0700164 break;
Jason Sams709a0972012-11-15 18:18:04 -0800165 }
166 }
Yang Ni5f6f16f2014-07-25 13:51:09 -0700167 if (ain == NULL) {
168 for (size_t ct3=0; ct3 < mSG->mInputs.size(); ct3++) {
169 if (mSG->mInputs[ct3]->mKernel == k) {
170 ain = mSG->mInputs[ct3]->mAlloc.get();
171 inExt = true;
172 break;
173 }
Jason Sams709a0972012-11-15 18:18:04 -0800174 }
175 }
176
177 for (size_t ct3=0; ct3 < n->mOutputs.size(); ct3++) {
178 if (n->mOutputs[ct3]->mSource.get() == k) {
179 aout = n->mOutputs[ct3]->mAlloc.get();
180 if(n->mOutputs[ct3]->mDstField.get() != NULL) {
181 fieldDep = true;
182 }
Yang Ni5f6f16f2014-07-25 13:51:09 -0700183 break;
Jason Sams709a0972012-11-15 18:18:04 -0800184 }
185 }
Yang Ni5f6f16f2014-07-25 13:51:09 -0700186 if (aout == NULL) {
187 for (size_t ct3=0; ct3 < mSG->mOutputs.size(); ct3++) {
188 if (mSG->mOutputs[ct3]->mKernel == k) {
189 aout = mSG->mOutputs[ct3]->mAlloc.get();
190 outExt = true;
191 break;
192 }
Jason Sams709a0972012-11-15 18:18:04 -0800193 }
194 }
195
Yang Ni5f6f16f2014-07-25 13:51:09 -0700196 rsAssert((k->mHasKernelOutput == (aout != NULL)) &&
197 (k->mHasKernelInput == (ain != NULL)));
198
Chris Wailes93d6bc82014-07-28 16:54:38 -0700199 ins.push_back(ain);
200 inExts.push_back(inExt);
201 outs.push_back(aout);
202 outExts.push_back(outExt);
203 kernels.push_back(k);
Jason Sams709a0972012-11-15 18:18:04 -0800204 }
205
206 }
207
208 MTLaunchStruct mtls;
209
Chris Wailesf3712132014-07-16 15:18:30 -0700210 if (fieldDep) {
Jason Sams709a0972012-11-15 18:18:04 -0800211 for (size_t ct=0; ct < ins.size(); ct++) {
212 Script *s = kernels[ct]->mScript;
213 RsdCpuScriptImpl *si = (RsdCpuScriptImpl *)mCtx->lookupScript(s);
214 uint32_t slot = kernels[ct]->mSlot;
215
Chris Wailesf3712132014-07-16 15:18:30 -0700216 uint32_t inLen;
217 const Allocation **ains;
218
219 if (ins[ct] == NULL) {
220 inLen = 0;
221 ains = NULL;
222
223 } else {
224 inLen = 1;
225 ains = const_cast<const Allocation**>(&ins[ct]);
226 }
227
228 si->forEachMtlsSetup(ains, inLen, outs[ct], NULL, 0, NULL, &mtls);
229
Jason Sams709a0972012-11-15 18:18:04 -0800230 si->forEachKernelSetup(slot, &mtls);
Chris Wailesf3712132014-07-16 15:18:30 -0700231 si->preLaunch(slot, ains, inLen, outs[ct], mtls.fep.usr,
232 mtls.fep.usrLen, NULL);
233
234 mCtx->launchThreads(ains, inLen, outs[ct], NULL, &mtls);
235
236 si->postLaunch(slot, ains, inLen, outs[ct], NULL, 0, NULL);
Jason Sams709a0972012-11-15 18:18:04 -0800237 }
238 } else {
239 ScriptList sl;
Chris Wailes93d6bc82014-07-28 16:54:38 -0700240
241 /*
242 * TODO: This is a hacky way of doing this and should be replaced by a
243 * call to std::vector's data() member once we have a C++11
244 * version of the STL.
245 */
246 sl.ins = &ins.front();
247 sl.outs = &outs.front();
248 sl.kernels = &kernels.front();
249 sl.count = kernels.size();
Jason Sams709a0972012-11-15 18:18:04 -0800250
Chris Wailesf3712132014-07-16 15:18:30 -0700251 uint32_t inLen;
252 const Allocation **ains;
253
254 if (ins[0] == NULL) {
255 inLen = 0;
256 ains = NULL;
257
258 } else {
259 inLen = 1;
260 ains = const_cast<const Allocation**>(&ins[0]);
261 }
262
Chris Wailes93d6bc82014-07-28 16:54:38 -0700263 std::vector<const void *> usrPtrs;
264 std::vector<const void *> fnPtrs;
265 std::vector<uint32_t> sigs;
Jason Sams709a0972012-11-15 18:18:04 -0800266 for (size_t ct=0; ct < kernels.size(); ct++) {
267 Script *s = kernels[ct]->mScript;
268 RsdCpuScriptImpl *si = (RsdCpuScriptImpl *)mCtx->lookupScript(s);
269
270 si->forEachKernelSetup(kernels[ct]->mSlot, &mtls);
Chris Wailes93d6bc82014-07-28 16:54:38 -0700271 fnPtrs.push_back((void *)mtls.kernel);
272 usrPtrs.push_back(mtls.fep.usr);
273 sigs.push_back(mtls.fep.usrLen);
Chris Wailesf3712132014-07-16 15:18:30 -0700274 si->preLaunch(kernels[ct]->mSlot, ains, inLen, outs[ct],
275 mtls.fep.usr, mtls.fep.usrLen, NULL);
Jason Sams709a0972012-11-15 18:18:04 -0800276 }
Chris Wailes93d6bc82014-07-28 16:54:38 -0700277
278 sl.sigs = &sigs.front();
279 sl.usrPtrs = &usrPtrs.front();
280 sl.fnPtrs = &fnPtrs.front();
281
282 sl.inExts = (bool*)&inExts.front();
283 sl.outExts = (bool*)&outExts.front();
Jason Sams709a0972012-11-15 18:18:04 -0800284
285 Script *s = kernels[0]->mScript;
286 RsdCpuScriptImpl *si = (RsdCpuScriptImpl *)mCtx->lookupScript(s);
Chris Wailesf3712132014-07-16 15:18:30 -0700287
288 si->forEachMtlsSetup(ains, inLen, outs[0], NULL, 0, NULL, &mtls);
289
Jason Sams709a0972012-11-15 18:18:04 -0800290 mtls.script = NULL;
291 mtls.kernel = (void (*)())&scriptGroupRoot;
292 mtls.fep.usr = &sl;
Chris Wailesf3712132014-07-16 15:18:30 -0700293
294 mCtx->launchThreads(ains, inLen, outs[0], NULL, &mtls);
Jason Sams17e3cdc2013-09-09 17:32:16 -0700295
296 for (size_t ct=0; ct < kernels.size(); ct++) {
297 Script *s = kernels[ct]->mScript;
298 RsdCpuScriptImpl *si = (RsdCpuScriptImpl *)mCtx->lookupScript(s);
Chris Wailesf3712132014-07-16 15:18:30 -0700299 si->postLaunch(kernels[ct]->mSlot, ains, inLen, outs[ct], NULL, 0,
300 NULL);
Jason Sams17e3cdc2013-09-09 17:32:16 -0700301 }
Jason Sams709a0972012-11-15 18:18:04 -0800302 }
303}