blob: 6a4642778d92f11e6ebc98e3efbdc988eef6affc [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
Miao Wang82e135c2017-02-27 23:35:35 -080022#include <vector>
23
Chih-Hung Hsieh462de212016-11-16 11:33:57 -080024namespace android {
25namespace renderscript {
Jason Sams709a0972012-11-15 18:18:04 -080026
Yang Ni1ffd86b2015-01-07 09:16:40 -080027CpuScriptGroupImpl::CpuScriptGroupImpl(RsdCpuReferenceImpl *ctx, const ScriptGroupBase *sg) {
Jason Sams709a0972012-11-15 18:18:04 -080028 mCtx = ctx;
Yang Ni1ffd86b2015-01-07 09:16:40 -080029 mSG = (ScriptGroup*)sg;
Jason Sams709a0972012-11-15 18:18:04 -080030}
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
David Grossb0abb142015-03-12 15:23:03 -070047typedef void (*ScriptGroupRootFunc_t)(const RsExpandKernelDriverInfo *kinfo,
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
David Grossb0abb142015-03-12 15:23:03 -070051void CpuScriptGroupImpl::scriptGroupRoot(const RsExpandKernelDriverInfo *kinfo,
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
David Grossb0abb142015-03-12 15:23:03 -070056 const ScriptList *sl = (const ScriptList *)kinfo->usr;
57 RsExpandKernelDriverInfo *mkinfo = const_cast<RsExpandKernelDriverInfo *>(kinfo);
Jason Sams709a0972012-11-15 18:18:04 -080058
David Grossb0abb142015-03-12 15:23:03 -070059 const uint32_t oldInStride = mkinfo->inStride[0];
Chris Wailesf3712132014-07-16 15:18:30 -070060
Chris Wailes80ef6932014-07-08 11:22:18 -070061 for (size_t ct = 0; ct < sl->count; ct++) {
Jason Sams709a0972012-11-15 18:18:04 -080062 ScriptGroupRootFunc_t func;
Chris Wailes80ef6932014-07-08 11:22:18 -070063 func = (ScriptGroupRootFunc_t)sl->fnPtrs[ct];
David Grossb0abb142015-03-12 15:23:03 -070064 mkinfo->usr = sl->usrPtrs[ct];
Jason Sams709a0972012-11-15 18:18:04 -080065
Stephen Hines4b2bea32014-08-13 17:32:10 +000066 if (sl->ins[ct]) {
David Grossb0abb142015-03-12 15:23:03 -070067 rsAssert(kinfo->inLen == 1);
Stephen Hines4b2bea32014-08-13 17:32:10 +000068
David Grossb0abb142015-03-12 15:23:03 -070069 mkinfo->inPtr[0] = (const uint8_t *)sl->ins[ct]->mHal.drvState.lod[0].mallocPtr;
70
71 mkinfo->inStride[0] = sl->ins[ct]->mHal.state.elementSizeBytes;
Chris Wailes80ef6932014-07-08 11:22:18 -070072
Jason Sams709a0972012-11-15 18:18:04 -080073 if (sl->inExts[ct]) {
David Grossb0abb142015-03-12 15:23:03 -070074 mkinfo->inPtr[0] =
75 (mkinfo->inPtr[0] +
76 sl->ins[ct]->mHal.drvState.lod[0].stride * kinfo->current.y);
Chris Wailes80ef6932014-07-08 11:22:18 -070077
David Grossb0abb142015-03-12 15:23:03 -070078 } else if (sl->ins[ct]->mHal.drvState.lod[0].dimY > kinfo->lid) {
79 mkinfo->inPtr[0] =
80 (mkinfo->inPtr[0] +
81 sl->ins[ct]->mHal.drvState.lod[0].stride * kinfo->lid);
Jason Sams709a0972012-11-15 18:18:04 -080082 }
Chris Wailesf3712132014-07-16 15:18:30 -070083
84 } else {
David Grossb0abb142015-03-12 15:23:03 -070085 rsAssert(kinfo->inLen == 0);
86
87 mkinfo->inPtr[0] = nullptr;
88 mkinfo->inStride[0] = 0;
Jason Sams709a0972012-11-15 18:18:04 -080089 }
90
Chris Wailesf3712132014-07-16 15:18:30 -070091 uint32_t ostep;
Jason Sams709a0972012-11-15 18:18:04 -080092 if (sl->outs[ct]) {
David Grossb0abb142015-03-12 15:23:03 -070093 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 mkinfo->outPtr[0] = nullptr;
110 ostep = 0;
Jason Sams709a0972012-11-15 18:18:04 -0800111 }
112
113 //ALOGE("kernel %i %p,%p %p,%p", ct, mp->ptrIn, mp->in, mp->ptrOut, mp->out);
David Grossb0abb142015-03-12 15:23:03 -0700114 func(kinfo, xstart, xend, ostep);
Jason Sams709a0972012-11-15 18:18:04 -0800115 }
116 //ALOGE("script group root");
117
David Grossb0abb142015-03-12 15:23:03 -0700118 mkinfo->inStride[0] = oldInStride;
119 mkinfo->usr = sl;
Jason Sams709a0972012-11-15 18:18:04 -0800120}
121
122
123
124void CpuScriptGroupImpl::execute() {
Miao Wang82e135c2017-02-27 23:35:35 -0800125 std::vector<Allocation *> ins;
126 std::vector<uint8_t> inExts;
127 std::vector<Allocation *> outs;
128 std::vector<uint8_t> outExts;
129 std::vector<const ScriptKernelID *> kernels;
Jason Sams709a0972012-11-15 18:18:04 -0800130 bool fieldDep = false;
131
132 for (size_t ct=0; ct < mSG->mNodes.size(); ct++) {
133 ScriptGroup::Node *n = mSG->mNodes[ct];
134 Script *s = n->mKernels[0]->mScript;
Stephen Hinesc78839b2013-09-10 17:40:41 -0700135 if (s->hasObjectSlots()) {
136 // Disable the ScriptGroup optimization if we have global RS
137 // objects that might interfere between kernels.
138 fieldDep = true;
139 }
Jason Sams709a0972012-11-15 18:18:04 -0800140
141 //ALOGE("node %i, order %i, in %i out %i", (int)ct, n->mOrder, (int)n->mInputs.size(), (int)n->mOutputs.size());
142
143 for (size_t ct2=0; ct2 < n->mInputs.size(); ct2++) {
144 if (n->mInputs[ct2]->mDstField.get() && n->mInputs[ct2]->mDstField->mScript) {
145 //ALOGE("field %p %zu", n->mInputs[ct2]->mDstField->mScript, n->mInputs[ct2]->mDstField->mSlot);
146 s->setVarObj(n->mInputs[ct2]->mDstField->mSlot, n->mInputs[ct2]->mAlloc.get());
147 }
148 }
149
150 for (size_t ct2=0; ct2 < n->mKernels.size(); ct2++) {
151 const ScriptKernelID *k = n->mKernels[ct2];
Chris Wailes44bef6f2014-08-12 13:51:10 -0700152 Allocation *ain = nullptr;
153 Allocation *aout = nullptr;
Jason Sams709a0972012-11-15 18:18:04 -0800154 bool inExt = false;
155 bool outExt = false;
156
157 for (size_t ct3=0; ct3 < n->mInputs.size(); ct3++) {
158 if (n->mInputs[ct3]->mDstKernel.get() == k) {
159 ain = n->mInputs[ct3]->mAlloc.get();
Yang Ni5f6f16f2014-07-25 13:51:09 -0700160 break;
Jason Sams709a0972012-11-15 18:18:04 -0800161 }
162 }
Chris Wailes44bef6f2014-08-12 13:51:10 -0700163 if (ain == nullptr) {
Yang Ni5f6f16f2014-07-25 13:51:09 -0700164 for (size_t ct3=0; ct3 < mSG->mInputs.size(); ct3++) {
165 if (mSG->mInputs[ct3]->mKernel == k) {
166 ain = mSG->mInputs[ct3]->mAlloc.get();
167 inExt = true;
168 break;
169 }
Jason Sams709a0972012-11-15 18:18:04 -0800170 }
171 }
172
173 for (size_t ct3=0; ct3 < n->mOutputs.size(); ct3++) {
174 if (n->mOutputs[ct3]->mSource.get() == k) {
175 aout = n->mOutputs[ct3]->mAlloc.get();
Chris Wailes44bef6f2014-08-12 13:51:10 -0700176 if(n->mOutputs[ct3]->mDstField.get() != nullptr) {
Jason Sams709a0972012-11-15 18:18:04 -0800177 fieldDep = true;
178 }
Yang Ni5f6f16f2014-07-25 13:51:09 -0700179 break;
Jason Sams709a0972012-11-15 18:18:04 -0800180 }
181 }
Chris Wailes44bef6f2014-08-12 13:51:10 -0700182 if (aout == nullptr) {
Yang Ni5f6f16f2014-07-25 13:51:09 -0700183 for (size_t ct3=0; ct3 < mSG->mOutputs.size(); ct3++) {
184 if (mSG->mOutputs[ct3]->mKernel == k) {
185 aout = mSG->mOutputs[ct3]->mAlloc.get();
186 outExt = true;
187 break;
188 }
Jason Sams709a0972012-11-15 18:18:04 -0800189 }
190 }
191
Chris Wailes44bef6f2014-08-12 13:51:10 -0700192 rsAssert((k->mHasKernelOutput == (aout != nullptr)) &&
193 (k->mHasKernelInput == (ain != nullptr)));
Yang Ni5f6f16f2014-07-25 13:51:09 -0700194
Miao Wang82e135c2017-02-27 23:35:35 -0800195 ins.push_back(ain);
196 inExts.push_back(inExt);
197 outs.push_back(aout);
198 outExts.push_back(outExt);
199 kernels.push_back(k);
Jason Sams709a0972012-11-15 18:18:04 -0800200 }
201
202 }
203
Matt Wala14ce0072015-07-30 17:30:25 -0700204 MTLaunchStructForEach mtls;
Jason Sams709a0972012-11-15 18:18:04 -0800205
Chris Wailesf3712132014-07-16 15:18:30 -0700206 if (fieldDep) {
Jason Sams709a0972012-11-15 18:18:04 -0800207 for (size_t ct=0; ct < ins.size(); ct++) {
208 Script *s = kernels[ct]->mScript;
209 RsdCpuScriptImpl *si = (RsdCpuScriptImpl *)mCtx->lookupScript(s);
210 uint32_t slot = kernels[ct]->mSlot;
211
Chris Wailesf3712132014-07-16 15:18:30 -0700212 uint32_t inLen;
213 const Allocation **ains;
214
Chris Wailes44bef6f2014-08-12 13:51:10 -0700215 if (ins[ct] == nullptr) {
Chris Wailesf3712132014-07-16 15:18:30 -0700216 inLen = 0;
Chris Wailes44bef6f2014-08-12 13:51:10 -0700217 ains = nullptr;
Chris Wailesf3712132014-07-16 15:18:30 -0700218
219 } else {
220 inLen = 1;
221 ains = const_cast<const Allocation**>(&ins[ct]);
222 }
223
Jason Samsbf2111d2015-01-26 18:13:41 -0800224 bool launchOK = si->forEachMtlsSetup(ains, inLen, outs[ct], nullptr, 0, nullptr, &mtls);
Chris Wailesf3712132014-07-16 15:18:30 -0700225
Jason Sams709a0972012-11-15 18:18:04 -0800226 si->forEachKernelSetup(slot, &mtls);
Chris Wailesf3712132014-07-16 15:18:30 -0700227 si->preLaunch(slot, ains, inLen, outs[ct], mtls.fep.usr,
Chris Wailes44bef6f2014-08-12 13:51:10 -0700228 mtls.fep.usrLen, nullptr);
Chris Wailesf3712132014-07-16 15:18:30 -0700229
Jason Samsbf2111d2015-01-26 18:13:41 -0800230 if (launchOK) {
Matt Wala14ce0072015-07-30 17:30:25 -0700231 mCtx->launchForEach(ains, inLen, outs[ct], nullptr, &mtls);
Jason Samsbf2111d2015-01-26 18:13:41 -0800232 }
Chris Wailesf3712132014-07-16 15:18:30 -0700233
Chris Wailes44bef6f2014-08-12 13:51:10 -0700234 si->postLaunch(slot, ains, inLen, outs[ct], nullptr, 0, nullptr);
Jason Sams709a0972012-11-15 18:18:04 -0800235 }
236 } else {
237 ScriptList sl;
Miao Wang82e135c2017-02-27 23:35:35 -0800238 sl.ins = ins.data();
239 sl.outs = outs.data();
240 sl.kernels = kernels.data();
Yang Nib8353c52015-02-14 18:00:59 -0800241 sl.count = kernels.size();
Jason Sams709a0972012-11-15 18:18:04 -0800242
Chris Wailesf3712132014-07-16 15:18:30 -0700243 uint32_t inLen;
244 const Allocation **ains;
245
Chris Wailes44bef6f2014-08-12 13:51:10 -0700246 if (ins[0] == nullptr) {
Chris Wailesf3712132014-07-16 15:18:30 -0700247 inLen = 0;
Chris Wailes44bef6f2014-08-12 13:51:10 -0700248 ains = nullptr;
Chris Wailesf3712132014-07-16 15:18:30 -0700249
250 } else {
251 inLen = 1;
252 ains = const_cast<const Allocation**>(&ins[0]);
253 }
254
Miao Wang82e135c2017-02-27 23:35:35 -0800255 std::vector<const void *> usrPtrs;
256 std::vector<const void *> fnPtrs;
257 std::vector<uint32_t> sigs;
Jason Sams709a0972012-11-15 18:18:04 -0800258 for (size_t ct=0; ct < kernels.size(); ct++) {
259 Script *s = kernels[ct]->mScript;
260 RsdCpuScriptImpl *si = (RsdCpuScriptImpl *)mCtx->lookupScript(s);
261
262 si->forEachKernelSetup(kernels[ct]->mSlot, &mtls);
Miao Wang82e135c2017-02-27 23:35:35 -0800263 fnPtrs.push_back((void *)mtls.kernel);
264 usrPtrs.push_back(mtls.fep.usr);
265 sigs.push_back(mtls.fep.usrLen);
Chris Wailesf3712132014-07-16 15:18:30 -0700266 si->preLaunch(kernels[ct]->mSlot, ains, inLen, outs[ct],
Chris Wailes44bef6f2014-08-12 13:51:10 -0700267 mtls.fep.usr, mtls.fep.usrLen, nullptr);
Jason Sams709a0972012-11-15 18:18:04 -0800268 }
Miao Wang82e135c2017-02-27 23:35:35 -0800269 sl.sigs = sigs.data();
270 sl.usrPtrs = usrPtrs.data();
271 sl.fnPtrs = fnPtrs.data();
272 sl.inExts = inExts.data();
273 sl.outExts = outExts.data();
Jason Sams709a0972012-11-15 18:18:04 -0800274
275 Script *s = kernels[0]->mScript;
276 RsdCpuScriptImpl *si = (RsdCpuScriptImpl *)mCtx->lookupScript(s);
Chris Wailesf3712132014-07-16 15:18:30 -0700277
Jason Samsbf2111d2015-01-26 18:13:41 -0800278 if (si->forEachMtlsSetup(ains, inLen, outs[0], nullptr, 0, nullptr, &mtls)) {
Chris Wailesf3712132014-07-16 15:18:30 -0700279
Jason Samsbf2111d2015-01-26 18:13:41 -0800280 mtls.script = nullptr;
Matt Wala14ce0072015-07-30 17:30:25 -0700281 mtls.kernel = &scriptGroupRoot;
Jason Samsbf2111d2015-01-26 18:13:41 -0800282 mtls.fep.usr = &sl;
Chris Wailesf3712132014-07-16 15:18:30 -0700283
Matt Wala14ce0072015-07-30 17:30:25 -0700284 mCtx->launchForEach(ains, inLen, outs[0], nullptr, &mtls);
Jason Samsbf2111d2015-01-26 18:13:41 -0800285 }
Jason Sams17e3cdc2013-09-09 17:32:16 -0700286
287 for (size_t ct=0; ct < kernels.size(); ct++) {
288 Script *s = kernels[ct]->mScript;
289 RsdCpuScriptImpl *si = (RsdCpuScriptImpl *)mCtx->lookupScript(s);
Chris Wailes44bef6f2014-08-12 13:51:10 -0700290 si->postLaunch(kernels[ct]->mSlot, ains, inLen, outs[ct], nullptr, 0,
291 nullptr);
Jason Sams17e3cdc2013-09-09 17:32:16 -0700292 }
Jason Sams709a0972012-11-15 18:18:04 -0800293 }
294}
Chih-Hung Hsieh462de212016-11-16 11:33:57 -0800295
296} // namespace renderscript
297} // namespace android