blob: 82208db9b55100d85cfc2da40c2d312f245b2b7c [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"
Jason Sams709a0972012-11-15 18:18:04 -080021
22using namespace android;
23using namespace android::renderscript;
24
Yang Ni1ffd86b2015-01-07 09:16:40 -080025CpuScriptGroupImpl::CpuScriptGroupImpl(RsdCpuReferenceImpl *ctx, const ScriptGroupBase *sg) {
Jason Sams709a0972012-11-15 18:18:04 -080026 mCtx = ctx;
Yang Ni1ffd86b2015-01-07 09:16:40 -080027 mSG = (ScriptGroup*)sg;
Jason Sams709a0972012-11-15 18:18:04 -080028}
29
30CpuScriptGroupImpl::~CpuScriptGroupImpl() {
31
32}
33
34bool CpuScriptGroupImpl::init() {
35 return true;
36}
37
38void CpuScriptGroupImpl::setInput(const ScriptKernelID *kid, Allocation *a) {
39}
40
41void CpuScriptGroupImpl::setOutput(const ScriptKernelID *kid, Allocation *a) {
42}
43
44
David Grossb0abb142015-03-12 15:23:03 -070045typedef void (*ScriptGroupRootFunc_t)(const RsExpandKernelDriverInfo *kinfo,
Jason Sams709a0972012-11-15 18:18:04 -080046 uint32_t xstart, uint32_t xend,
Chris Wailes9ed79102014-07-25 15:53:28 -070047 uint32_t outstep);
Jason Sams709a0972012-11-15 18:18:04 -080048
David Grossb0abb142015-03-12 15:23:03 -070049void CpuScriptGroupImpl::scriptGroupRoot(const RsExpandKernelDriverInfo *kinfo,
Jason Sams709a0972012-11-15 18:18:04 -080050 uint32_t xstart, uint32_t xend,
Chris Wailes9ed79102014-07-25 15:53:28 -070051 uint32_t outstep) {
Jason Sams709a0972012-11-15 18:18:04 -080052
53
David Grossb0abb142015-03-12 15:23:03 -070054 const ScriptList *sl = (const ScriptList *)kinfo->usr;
55 RsExpandKernelDriverInfo *mkinfo = const_cast<RsExpandKernelDriverInfo *>(kinfo);
Jason Sams709a0972012-11-15 18:18:04 -080056
David Grossb0abb142015-03-12 15:23:03 -070057 const uint32_t oldInStride = mkinfo->inStride[0];
Chris Wailesf3712132014-07-16 15:18:30 -070058
Chris Wailes80ef6932014-07-08 11:22:18 -070059 for (size_t ct = 0; ct < sl->count; ct++) {
Jason Sams709a0972012-11-15 18:18:04 -080060 ScriptGroupRootFunc_t func;
Chris Wailes80ef6932014-07-08 11:22:18 -070061 func = (ScriptGroupRootFunc_t)sl->fnPtrs[ct];
David Grossb0abb142015-03-12 15:23:03 -070062 mkinfo->usr = sl->usrPtrs[ct];
Jason Sams709a0972012-11-15 18:18:04 -080063
Stephen Hines4b2bea32014-08-13 17:32:10 +000064 if (sl->ins[ct]) {
David Grossb0abb142015-03-12 15:23:03 -070065 rsAssert(kinfo->inLen == 1);
Stephen Hines4b2bea32014-08-13 17:32:10 +000066
David Grossb0abb142015-03-12 15:23:03 -070067 mkinfo->inPtr[0] = (const uint8_t *)sl->ins[ct]->mHal.drvState.lod[0].mallocPtr;
68
69 mkinfo->inStride[0] = sl->ins[ct]->mHal.state.elementSizeBytes;
Chris Wailes80ef6932014-07-08 11:22:18 -070070
Jason Sams709a0972012-11-15 18:18:04 -080071 if (sl->inExts[ct]) {
David Grossb0abb142015-03-12 15:23:03 -070072 mkinfo->inPtr[0] =
73 (mkinfo->inPtr[0] +
74 sl->ins[ct]->mHal.drvState.lod[0].stride * kinfo->current.y);
Chris Wailes80ef6932014-07-08 11:22:18 -070075
David Grossb0abb142015-03-12 15:23:03 -070076 } else if (sl->ins[ct]->mHal.drvState.lod[0].dimY > kinfo->lid) {
77 mkinfo->inPtr[0] =
78 (mkinfo->inPtr[0] +
79 sl->ins[ct]->mHal.drvState.lod[0].stride * kinfo->lid);
Jason Sams709a0972012-11-15 18:18:04 -080080 }
Chris Wailesf3712132014-07-16 15:18:30 -070081
82 } else {
David Grossb0abb142015-03-12 15:23:03 -070083 rsAssert(kinfo->inLen == 0);
84
85 mkinfo->inPtr[0] = nullptr;
86 mkinfo->inStride[0] = 0;
Jason Sams709a0972012-11-15 18:18:04 -080087 }
88
Chris Wailesf3712132014-07-16 15:18:30 -070089 uint32_t ostep;
Jason Sams709a0972012-11-15 18:18:04 -080090 if (sl->outs[ct]) {
David Grossb0abb142015-03-12 15:23:03 -070091 rsAssert(kinfo->outLen == 1);
92
93 mkinfo->outPtr[0] =
Chris Wailes80ef6932014-07-08 11:22:18 -070094 (uint8_t *)sl->outs[ct]->mHal.drvState.lod[0].mallocPtr;
95
Jason Sams17e3cdc2013-09-09 17:32:16 -070096 ostep = sl->outs[ct]->mHal.state.elementSizeBytes;
Chris Wailes80ef6932014-07-08 11:22:18 -070097
Jason Sams709a0972012-11-15 18:18:04 -080098 if (sl->outExts[ct]) {
David Grossb0abb142015-03-12 15:23:03 -070099 mkinfo->outPtr[0] =
100 mkinfo->outPtr[0] +
101 sl->outs[ct]->mHal.drvState.lod[0].stride * kinfo->current.y;
Chris Wailes80ef6932014-07-08 11:22:18 -0700102
David Grossb0abb142015-03-12 15:23:03 -0700103 } else if (sl->outs[ct]->mHal.drvState.lod[0].dimY > kinfo->lid) {
104 mkinfo->outPtr[0] =
105 mkinfo->outPtr[0] +
106 sl->outs[ct]->mHal.drvState.lod[0].stride * kinfo->lid;
Jason Sams709a0972012-11-15 18:18:04 -0800107 }
Chris Wailesf3712132014-07-16 15:18:30 -0700108 } else {
David Grossb0abb142015-03-12 15:23:03 -0700109 rsAssert(kinfo->outLen == 0);
110
111 mkinfo->outPtr[0] = nullptr;
112 ostep = 0;
Jason Sams709a0972012-11-15 18:18:04 -0800113 }
114
115 //ALOGE("kernel %i %p,%p %p,%p", ct, mp->ptrIn, mp->in, mp->ptrOut, mp->out);
David Grossb0abb142015-03-12 15:23:03 -0700116 func(kinfo, xstart, xend, ostep);
Jason Sams709a0972012-11-15 18:18:04 -0800117 }
118 //ALOGE("script group root");
119
David Grossb0abb142015-03-12 15:23:03 -0700120 mkinfo->inStride[0] = oldInStride;
121 mkinfo->usr = sl;
Jason Sams709a0972012-11-15 18:18:04 -0800122}
123
124
125
126void CpuScriptGroupImpl::execute() {
Yang Nib8353c52015-02-14 18:00:59 -0800127 Vector<Allocation *> ins;
128 Vector<bool> inExts;
129 Vector<Allocation *> outs;
130 Vector<bool> outExts;
131 Vector<const ScriptKernelID *> kernels;
Jason Sams709a0972012-11-15 18:18:04 -0800132 bool fieldDep = false;
133
134 for (size_t ct=0; ct < mSG->mNodes.size(); ct++) {
135 ScriptGroup::Node *n = mSG->mNodes[ct];
136 Script *s = n->mKernels[0]->mScript;
Stephen Hinesc78839b2013-09-10 17:40:41 -0700137 if (s->hasObjectSlots()) {
138 // Disable the ScriptGroup optimization if we have global RS
139 // objects that might interfere between kernels.
140 fieldDep = true;
141 }
Jason Sams709a0972012-11-15 18:18:04 -0800142
143 //ALOGE("node %i, order %i, in %i out %i", (int)ct, n->mOrder, (int)n->mInputs.size(), (int)n->mOutputs.size());
144
145 for (size_t ct2=0; ct2 < n->mInputs.size(); ct2++) {
146 if (n->mInputs[ct2]->mDstField.get() && n->mInputs[ct2]->mDstField->mScript) {
147 //ALOGE("field %p %zu", n->mInputs[ct2]->mDstField->mScript, n->mInputs[ct2]->mDstField->mSlot);
148 s->setVarObj(n->mInputs[ct2]->mDstField->mSlot, n->mInputs[ct2]->mAlloc.get());
149 }
150 }
151
152 for (size_t ct2=0; ct2 < n->mKernels.size(); ct2++) {
153 const ScriptKernelID *k = n->mKernels[ct2];
Chris Wailes44bef6f2014-08-12 13:51:10 -0700154 Allocation *ain = nullptr;
155 Allocation *aout = nullptr;
Jason Sams709a0972012-11-15 18:18:04 -0800156 bool inExt = false;
157 bool outExt = false;
158
159 for (size_t ct3=0; ct3 < n->mInputs.size(); ct3++) {
160 if (n->mInputs[ct3]->mDstKernel.get() == k) {
161 ain = n->mInputs[ct3]->mAlloc.get();
Yang Ni5f6f16f2014-07-25 13:51:09 -0700162 break;
Jason Sams709a0972012-11-15 18:18:04 -0800163 }
164 }
Chris Wailes44bef6f2014-08-12 13:51:10 -0700165 if (ain == nullptr) {
Yang Ni5f6f16f2014-07-25 13:51:09 -0700166 for (size_t ct3=0; ct3 < mSG->mInputs.size(); ct3++) {
167 if (mSG->mInputs[ct3]->mKernel == k) {
168 ain = mSG->mInputs[ct3]->mAlloc.get();
169 inExt = true;
170 break;
171 }
Jason Sams709a0972012-11-15 18:18:04 -0800172 }
173 }
174
175 for (size_t ct3=0; ct3 < n->mOutputs.size(); ct3++) {
176 if (n->mOutputs[ct3]->mSource.get() == k) {
177 aout = n->mOutputs[ct3]->mAlloc.get();
Chris Wailes44bef6f2014-08-12 13:51:10 -0700178 if(n->mOutputs[ct3]->mDstField.get() != nullptr) {
Jason Sams709a0972012-11-15 18:18:04 -0800179 fieldDep = true;
180 }
Yang Ni5f6f16f2014-07-25 13:51:09 -0700181 break;
Jason Sams709a0972012-11-15 18:18:04 -0800182 }
183 }
Chris Wailes44bef6f2014-08-12 13:51:10 -0700184 if (aout == nullptr) {
Yang Ni5f6f16f2014-07-25 13:51:09 -0700185 for (size_t ct3=0; ct3 < mSG->mOutputs.size(); ct3++) {
186 if (mSG->mOutputs[ct3]->mKernel == k) {
187 aout = mSG->mOutputs[ct3]->mAlloc.get();
188 outExt = true;
189 break;
190 }
Jason Sams709a0972012-11-15 18:18:04 -0800191 }
192 }
193
Chris Wailes44bef6f2014-08-12 13:51:10 -0700194 rsAssert((k->mHasKernelOutput == (aout != nullptr)) &&
195 (k->mHasKernelInput == (ain != nullptr)));
Yang Ni5f6f16f2014-07-25 13:51:09 -0700196
Yang Nib8353c52015-02-14 18:00:59 -0800197 ins.add(ain);
198 inExts.add(inExt);
199 outs.add(aout);
200 outExts.add(outExt);
201 kernels.add(k);
Jason Sams709a0972012-11-15 18:18:04 -0800202 }
203
204 }
205
206 MTLaunchStruct mtls;
207
Chris Wailesf3712132014-07-16 15:18:30 -0700208 if (fieldDep) {
Jason Sams709a0972012-11-15 18:18:04 -0800209 for (size_t ct=0; ct < ins.size(); ct++) {
210 Script *s = kernels[ct]->mScript;
211 RsdCpuScriptImpl *si = (RsdCpuScriptImpl *)mCtx->lookupScript(s);
212 uint32_t slot = kernels[ct]->mSlot;
213
Chris Wailesf3712132014-07-16 15:18:30 -0700214 uint32_t inLen;
215 const Allocation **ains;
216
Chris Wailes44bef6f2014-08-12 13:51:10 -0700217 if (ins[ct] == nullptr) {
Chris Wailesf3712132014-07-16 15:18:30 -0700218 inLen = 0;
Chris Wailes44bef6f2014-08-12 13:51:10 -0700219 ains = nullptr;
Chris Wailesf3712132014-07-16 15:18:30 -0700220
221 } else {
222 inLen = 1;
223 ains = const_cast<const Allocation**>(&ins[ct]);
224 }
225
Jason Samsbf2111d2015-01-26 18:13:41 -0800226 bool launchOK = si->forEachMtlsSetup(ains, inLen, outs[ct], nullptr, 0, nullptr, &mtls);
Chris Wailesf3712132014-07-16 15:18:30 -0700227
Jason Sams709a0972012-11-15 18:18:04 -0800228 si->forEachKernelSetup(slot, &mtls);
Chris Wailesf3712132014-07-16 15:18:30 -0700229 si->preLaunch(slot, ains, inLen, outs[ct], mtls.fep.usr,
Chris Wailes44bef6f2014-08-12 13:51:10 -0700230 mtls.fep.usrLen, nullptr);
Chris Wailesf3712132014-07-16 15:18:30 -0700231
Jason Samsbf2111d2015-01-26 18:13:41 -0800232 if (launchOK) {
233 mCtx->launchThreads(ains, inLen, outs[ct], nullptr, &mtls);
234 }
Chris Wailesf3712132014-07-16 15:18:30 -0700235
Chris Wailes44bef6f2014-08-12 13:51:10 -0700236 si->postLaunch(slot, ains, inLen, outs[ct], nullptr, 0, nullptr);
Jason Sams709a0972012-11-15 18:18:04 -0800237 }
238 } else {
239 ScriptList sl;
Yang Nib8353c52015-02-14 18:00:59 -0800240 sl.ins = ins.array();
241 sl.outs = outs.array();
242 sl.kernels = kernels.array();
243 sl.count = kernels.size();
Jason Sams709a0972012-11-15 18:18:04 -0800244
Chris Wailesf3712132014-07-16 15:18:30 -0700245 uint32_t inLen;
246 const Allocation **ains;
247
Chris Wailes44bef6f2014-08-12 13:51:10 -0700248 if (ins[0] == nullptr) {
Chris Wailesf3712132014-07-16 15:18:30 -0700249 inLen = 0;
Chris Wailes44bef6f2014-08-12 13:51:10 -0700250 ains = nullptr;
Chris Wailesf3712132014-07-16 15:18:30 -0700251
252 } else {
253 inLen = 1;
254 ains = const_cast<const Allocation**>(&ins[0]);
255 }
256
Yang Nib8353c52015-02-14 18:00:59 -0800257 Vector<const void *> usrPtrs;
258 Vector<const void *> fnPtrs;
259 Vector<uint32_t> sigs;
Jason Sams709a0972012-11-15 18:18:04 -0800260 for (size_t ct=0; ct < kernels.size(); ct++) {
261 Script *s = kernels[ct]->mScript;
262 RsdCpuScriptImpl *si = (RsdCpuScriptImpl *)mCtx->lookupScript(s);
263
264 si->forEachKernelSetup(kernels[ct]->mSlot, &mtls);
Yang Nib8353c52015-02-14 18:00:59 -0800265 fnPtrs.add((void *)mtls.kernel);
266 usrPtrs.add(mtls.fep.usr);
267 sigs.add(mtls.fep.usrLen);
Chris Wailesf3712132014-07-16 15:18:30 -0700268 si->preLaunch(kernels[ct]->mSlot, ains, inLen, outs[ct],
Chris Wailes44bef6f2014-08-12 13:51:10 -0700269 mtls.fep.usr, mtls.fep.usrLen, nullptr);
Jason Sams709a0972012-11-15 18:18:04 -0800270 }
Yang Nib8353c52015-02-14 18:00:59 -0800271 sl.sigs = sigs.array();
272 sl.usrPtrs = usrPtrs.array();
273 sl.fnPtrs = fnPtrs.array();
274 sl.inExts = inExts.array();
275 sl.outExts = outExts.array();
Jason Sams709a0972012-11-15 18:18:04 -0800276
277 Script *s = kernels[0]->mScript;
278 RsdCpuScriptImpl *si = (RsdCpuScriptImpl *)mCtx->lookupScript(s);
Chris Wailesf3712132014-07-16 15:18:30 -0700279
Jason Samsbf2111d2015-01-26 18:13:41 -0800280 if (si->forEachMtlsSetup(ains, inLen, outs[0], nullptr, 0, nullptr, &mtls)) {
Chris Wailesf3712132014-07-16 15:18:30 -0700281
Jason Samsbf2111d2015-01-26 18:13:41 -0800282 mtls.script = nullptr;
283 mtls.kernel = (void (*)())&scriptGroupRoot;
284 mtls.fep.usr = &sl;
Chris Wailesf3712132014-07-16 15:18:30 -0700285
Jason Samsbf2111d2015-01-26 18:13:41 -0800286 mCtx->launchThreads(ains, inLen, outs[0], nullptr, &mtls);
287 }
Jason Sams17e3cdc2013-09-09 17:32:16 -0700288
289 for (size_t ct=0; ct < kernels.size(); ct++) {
290 Script *s = kernels[ct]->mScript;
291 RsdCpuScriptImpl *si = (RsdCpuScriptImpl *)mCtx->lookupScript(s);
Chris Wailes44bef6f2014-08-12 13:51:10 -0700292 si->postLaunch(kernels[ct]->mSlot, ains, inLen, outs[ct], nullptr, 0,
293 nullptr);
Jason Sams17e3cdc2013-09-09 17:32:16 -0700294 }
Jason Sams709a0972012-11-15 18:18:04 -0800295 }
296}