blob: 26ab6a8dc51b5783d70bc48ce0598efd092109cf [file] [log] [blame]
Jason Samsdbe66d62012-09-17 13:54:41 -07001/*
2 * Copyright (C) 2012 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
Yang Ni1ffd86b2015-01-07 09:16:40 -080017#include "rsScriptGroup.h"
Chris Wailes93d6bc82014-07-28 16:54:38 -070018
Jason Samsdbe66d62012-09-17 13:54:41 -070019#include "rsContext.h"
Yang Nib8353c52015-02-14 18:00:59 -080020// TODO: Is this header needed here?
Yang Ni1ffd86b2015-01-07 09:16:40 -080021#include "rsScriptGroup2.h"
22
23#include <algorithm>
Jason Samsdbe66d62012-09-17 13:54:41 -070024#include <time.h>
25
26using namespace android;
27using namespace android::renderscript;
28
Yang Ni1ffd86b2015-01-07 09:16:40 -080029ScriptGroup::ScriptGroup(Context *rsc) : ScriptGroupBase(rsc) {
Jason Samsdbe66d62012-09-17 13:54:41 -070030}
31
32ScriptGroup::~ScriptGroup() {
33 if (mRSC->mHal.funcs.scriptgroup.destroy) {
34 mRSC->mHal.funcs.scriptgroup.destroy(mRSC, this);
35 }
36
Yang Nib8353c52015-02-14 18:00:59 -080037 for (size_t ct=0; ct < mLinks.size(); ct++) {
38 delete mLinks[ct];
Jason Samsdbe66d62012-09-17 13:54:41 -070039 }
Yong Chen61d5ed52015-01-16 09:05:18 +080040
41 for (auto input : mInputs) {
42 input->mAlloc.clear();
43 }
44
45 for (auto output : mOutputs) {
46 output->mAlloc.clear();
47 }
Jason Samsdbe66d62012-09-17 13:54:41 -070048}
49
50ScriptGroup::IO::IO(const ScriptKernelID *kid) {
51 mKernel = kid;
52}
53
54ScriptGroup::Node::Node(Script *s) {
55 mScript = s;
56 mSeen = false;
57 mOrder = 0;
58}
59
60ScriptGroup::Node * ScriptGroup::findNode(Script *s) const {
Yang Nib8353c52015-02-14 18:00:59 -080061 //ALOGE("find %p %i", s, (int)mNodes.size());
62 for (size_t ct=0; ct < mNodes.size(); ct++) {
63 Node *n = mNodes[ct];
64 for (size_t ct2=0; ct2 < n->mKernels.size(); ct2++) {
65 if (n->mKernels[ct2]->mScript == s) {
66 return n;
Jason Samsdbe66d62012-09-17 13:54:41 -070067 }
68 }
69 }
Chris Wailes93d6bc82014-07-28 16:54:38 -070070
Chris Wailes44bef6f2014-08-12 13:51:10 -070071 return nullptr;
Jason Samsdbe66d62012-09-17 13:54:41 -070072}
73
Yang Nib8353c52015-02-14 18:00:59 -080074bool ScriptGroup::calcOrderRecurse(Node *n, int depth) {
75 n->mSeen = true;
76 if (n->mOrder < depth) {
77 n->mOrder = depth;
Jason Samsdbe66d62012-09-17 13:54:41 -070078 }
79 bool ret = true;
Chris Wailes93d6bc82014-07-28 16:54:38 -070080
Yang Nib8353c52015-02-14 18:00:59 -080081 for (size_t ct=0; ct < n->mOutputs.size(); ct++) {
82 const Link *l = n->mOutputs[ct];
83 Node *nt = NULL;
84 if (l->mDstField.get()) {
85 nt = findNode(l->mDstField->mScript);
Jason Samsdbe66d62012-09-17 13:54:41 -070086 } else {
Yang Nib8353c52015-02-14 18:00:59 -080087 nt = findNode(l->mDstKernel->mScript);
Jason Samsdbe66d62012-09-17 13:54:41 -070088 }
Yang Nib8353c52015-02-14 18:00:59 -080089 if (nt->mSeen) {
Jason Samsdbe66d62012-09-17 13:54:41 -070090 return false;
91 }
Yang Nib8353c52015-02-14 18:00:59 -080092 ret &= calcOrderRecurse(nt, n->mOrder + 1);
Jason Samsdbe66d62012-09-17 13:54:41 -070093 }
94 return ret;
95}
96
Yang Nib8353c52015-02-14 18:00:59 -080097#if !defined(RS_SERVER) && !defined(RS_COMPATIBILITY_LIB)
98static int CompareNodeForSort(ScriptGroup::Node *const* lhs,
99 ScriptGroup::Node *const* rhs) {
100 if (lhs[0]->mOrder > rhs[0]->mOrder) {
101 return 1;
102 }
103 return 0;
104}
105#else
106class NodeCompare {
107public:
108 bool operator() (const ScriptGroup::Node* lhs,
109 const ScriptGroup::Node* rhs) {
110 if (lhs->mOrder > rhs->mOrder) {
111 return true;
112 }
113 return false;
114 }
115};
116#endif
117
Jason Samsdbe66d62012-09-17 13:54:41 -0700118bool ScriptGroup::calcOrder() {
119 // Make nodes
Chris Wailes93d6bc82014-07-28 16:54:38 -0700120
Yang Nib8353c52015-02-14 18:00:59 -0800121 for (size_t ct=0; ct < mKernels.size(); ct++) {
122 const ScriptKernelID *k = mKernels[ct].get();
123 //ALOGE(" kernel %i, %p s=%p", (int)ct, k, mKernels[ct]->mScript);
124 Node *n = findNode(k->mScript);
125 //ALOGE(" n = %p", n);
126 if (n == NULL) {
127 n = new Node(k->mScript);
128 mNodes.add(n);
Jason Samsdbe66d62012-09-17 13:54:41 -0700129 }
Yang Nib8353c52015-02-14 18:00:59 -0800130 n->mKernels.add(k);
Jason Samsdbe66d62012-09-17 13:54:41 -0700131 }
132
133 // add links
Yang Nib8353c52015-02-14 18:00:59 -0800134 //ALOGE("link count %i", (int)mLinks.size());
135 for (size_t ct=0; ct < mLinks.size(); ct++) {
136 Link *l = mLinks[ct];
137 //ALOGE("link %i %p", (int)ct, l);
138 Node *n = findNode(l->mSource->mScript);
139 //ALOGE("link n %p", n);
140 n->mOutputs.add(l);
Jason Samsdbe66d62012-09-17 13:54:41 -0700141
Yang Nib8353c52015-02-14 18:00:59 -0800142 if (l->mDstKernel.get()) {
143 //ALOGE("l->mDstKernel.get() %p", l->mDstKernel.get());
144 n = findNode(l->mDstKernel->mScript);
145 //ALOGE(" n1 %p", n);
146 n->mInputs.add(l);
Jason Samsdbe66d62012-09-17 13:54:41 -0700147 } else {
Yang Nib8353c52015-02-14 18:00:59 -0800148 n = findNode(l->mDstField->mScript);
149 //ALOGE(" n2 %p", n);
150 n->mInputs.add(l);
Jason Samsdbe66d62012-09-17 13:54:41 -0700151 }
152 }
153
Yang Nib8353c52015-02-14 18:00:59 -0800154 //ALOGE("node count %i", (int)mNodes.size());
Jason Samsdbe66d62012-09-17 13:54:41 -0700155 // Order nodes
156 bool ret = true;
Yang Nib8353c52015-02-14 18:00:59 -0800157 for (size_t ct=0; ct < mNodes.size(); ct++) {
158 Node *n = mNodes[ct];
159 if (n->mInputs.size() == 0) {
160 for (size_t ct2=0; ct2 < mNodes.size(); ct2++) {
161 mNodes[ct2]->mSeen = false;
Jason Samsdbe66d62012-09-17 13:54:41 -0700162 }
Yang Nib8353c52015-02-14 18:00:59 -0800163 ret &= calcOrderRecurse(n, 0);
Jason Samsdbe66d62012-09-17 13:54:41 -0700164 }
165 }
166
Yang Nib8353c52015-02-14 18:00:59 -0800167 for (size_t ct=0; ct < mKernels.size(); ct++) {
168 const ScriptKernelID *k = mKernels[ct].get();
169 const Node *n = findNode(k->mScript);
Jason Samsdbe66d62012-09-17 13:54:41 -0700170
Yang Nib8353c52015-02-14 18:00:59 -0800171 if (k->mHasKernelOutput) {
Jason Samsdbe66d62012-09-17 13:54:41 -0700172 bool found = false;
Yang Nib8353c52015-02-14 18:00:59 -0800173 for (size_t ct2=0; ct2 < n->mOutputs.size(); ct2++) {
174 if (n->mOutputs[ct2]->mSource.get() == k) {
Jason Samsdbe66d62012-09-17 13:54:41 -0700175 found = true;
176 break;
177 }
178 }
179 if (!found) {
Yang Nib8353c52015-02-14 18:00:59 -0800180 //ALOGE("add io out %p", k);
181 mOutputs.add(new IO(k));
Jason Samsdbe66d62012-09-17 13:54:41 -0700182 }
183 }
184
Yang Nib8353c52015-02-14 18:00:59 -0800185 if (k->mHasKernelInput) {
Jason Samsdbe66d62012-09-17 13:54:41 -0700186 bool found = false;
Yang Nib8353c52015-02-14 18:00:59 -0800187 for (size_t ct2=0; ct2 < n->mInputs.size(); ct2++) {
188 if (n->mInputs[ct2]->mDstKernel.get() == k) {
Jason Samsdbe66d62012-09-17 13:54:41 -0700189 found = true;
190 break;
191 }
192 }
193 if (!found) {
Yang Nib8353c52015-02-14 18:00:59 -0800194 //ALOGE("add io in %p", k);
195 mInputs.add(new IO(k));
Jason Samsdbe66d62012-09-17 13:54:41 -0700196 }
197 }
198 }
199
200 // sort
Yang Nib8353c52015-02-14 18:00:59 -0800201#if !defined(RS_SERVER) && !defined(RS_COMPATIBILITY_LIB)
202 mNodes.sort(&CompareNodeForSort);
203#else
204 std::sort(mNodes.begin(), mNodes.end(), NodeCompare());
205#endif
Jason Samsdbe66d62012-09-17 13:54:41 -0700206
207 return ret;
208}
209
210ScriptGroup * ScriptGroup::create(Context *rsc,
211 ScriptKernelID ** kernels, size_t kernelsSize,
212 ScriptKernelID ** src, size_t srcSize,
213 ScriptKernelID ** dstK, size_t dstKSize,
214 ScriptFieldID ** dstF, size_t dstFSize,
215 const Type ** type, size_t typeSize) {
216
217 size_t kernelCount = kernelsSize / sizeof(ScriptKernelID *);
218 size_t linkCount = typeSize / sizeof(Type *);
219
220 //ALOGE("ScriptGroup::create kernels=%i links=%i", (int)kernelCount, (int)linkCount);
221
222
223 // Start by counting unique kernel sources
224
225 ScriptGroup *sg = new ScriptGroup(rsc);
226
227 sg->mKernels.reserve(kernelCount);
228 for (size_t ct=0; ct < kernelCount; ct++) {
Yang Nib8353c52015-02-14 18:00:59 -0800229 sg->mKernels.add(kernels[ct]);
Jason Samsdbe66d62012-09-17 13:54:41 -0700230 }
231
232 sg->mLinks.reserve(linkCount);
233 for (size_t ct=0; ct < linkCount; ct++) {
234 Link *l = new Link();
235 l->mType = type[ct];
236 l->mSource = src[ct];
237 l->mDstField = dstF[ct];
238 l->mDstKernel = dstK[ct];
Yang Nib8353c52015-02-14 18:00:59 -0800239 sg->mLinks.add(l);
Jason Samsdbe66d62012-09-17 13:54:41 -0700240 }
241
242 sg->calcOrder();
243
244 // allocate links
245 for (size_t ct=0; ct < sg->mNodes.size(); ct++) {
246 const Node *n = sg->mNodes[ct];
247 for (size_t ct2=0; ct2 < n->mOutputs.size(); ct2++) {
248 Link *l = n->mOutputs[ct2];
249 if (l->mAlloc.get()) {
250 continue;
251 }
252 const ScriptKernelID *k = l->mSource.get();
253
254 Allocation * alloc = Allocation::createAllocation(rsc,
255 l->mType.get(), RS_ALLOCATION_USAGE_SCRIPT);
256 l->mAlloc = alloc;
257
258 for (size_t ct3=ct2+1; ct3 < n->mOutputs.size(); ct3++) {
259 if (n->mOutputs[ct3]->mSource.get() == l->mSource.get()) {
260 n->mOutputs[ct3]->mAlloc = alloc;
261 }
262 }
263 }
264 }
265
266 if (rsc->mHal.funcs.scriptgroup.init) {
267 rsc->mHal.funcs.scriptgroup.init(rsc, sg);
268 }
Stephen Hines61c86952013-04-09 17:34:43 -0700269 sg->incUserRef();
Jason Samsdbe66d62012-09-17 13:54:41 -0700270 return sg;
271}
272
273void ScriptGroup::setInput(Context *rsc, ScriptKernelID *kid, Allocation *a) {
Yang Nib8353c52015-02-14 18:00:59 -0800274 for (size_t ct=0; ct < mInputs.size(); ct++) {
275 if (mInputs[ct]->mKernel == kid) {
276 mInputs[ct]->mAlloc = a;
Jason Samsdbe66d62012-09-17 13:54:41 -0700277
278 if (rsc->mHal.funcs.scriptgroup.setInput) {
279 rsc->mHal.funcs.scriptgroup.setInput(rsc, this, kid, a);
280 }
281 return;
282 }
283 }
284 rsAssert(!"ScriptGroup:setInput kid not found");
285}
286
287void ScriptGroup::setOutput(Context *rsc, ScriptKernelID *kid, Allocation *a) {
Yang Nib8353c52015-02-14 18:00:59 -0800288 for (size_t ct=0; ct < mOutputs.size(); ct++) {
289 if (mOutputs[ct]->mKernel == kid) {
290 mOutputs[ct]->mAlloc = a;
Jason Samsdbe66d62012-09-17 13:54:41 -0700291
292 if (rsc->mHal.funcs.scriptgroup.setOutput) {
293 rsc->mHal.funcs.scriptgroup.setOutput(rsc, this, kid, a);
294 }
295 return;
296 }
297 }
298 rsAssert(!"ScriptGroup:setOutput kid not found");
299}
300
Yang Ni5f6f16f2014-07-25 13:51:09 -0700301bool ScriptGroup::validateInputAndOutput(Context *rsc) {
302 for(size_t i = 0; i < mInputs.size(); i++) {
Chris Wailes44bef6f2014-08-12 13:51:10 -0700303 if (mInputs[i]->mAlloc.get() == nullptr) {
Yang Ni5f6f16f2014-07-25 13:51:09 -0700304 rsc->setError(RS_ERROR_BAD_VALUE, "ScriptGroup missing input.");
305 return false;
306 }
307 }
308
309 for(size_t i = 0; i < mOutputs.size(); i++) {
Chris Wailes44bef6f2014-08-12 13:51:10 -0700310 if (mOutputs[i]->mAlloc.get() == nullptr) {
Yang Ni5f6f16f2014-07-25 13:51:09 -0700311 rsc->setError(RS_ERROR_BAD_VALUE, "ScriptGroup missing output.");
312 return false;
313 }
314 }
315
316 return true;
317}
318
Jason Samsdbe66d62012-09-17 13:54:41 -0700319void ScriptGroup::execute(Context *rsc) {
Yang Ni5f6f16f2014-07-25 13:51:09 -0700320 if (!validateInputAndOutput(rsc)) {
321 return;
322 }
323
Jason Samsdbe66d62012-09-17 13:54:41 -0700324 if (rsc->mHal.funcs.scriptgroup.execute) {
325 rsc->mHal.funcs.scriptgroup.execute(rsc, this);
326 return;
327 }
328
Yang Nib8353c52015-02-14 18:00:59 -0800329 for (size_t ct=0; ct < mNodes.size(); ct++) {
330 Node *n = mNodes[ct];
331 //ALOGE("node %i, order %i, in %i out %i", (int)ct, n->mOrder, (int)n->mInputs.size(), (int)n->mOutputs.size());
Jason Samsdbe66d62012-09-17 13:54:41 -0700332
Yang Nib8353c52015-02-14 18:00:59 -0800333 for (size_t ct2=0; ct2 < n->mKernels.size(); ct2++) {
334 const ScriptKernelID *k = n->mKernels[ct2];
335 Allocation *ain = NULL;
336 Allocation *aout = NULL;
337
338 for (size_t ct3=0; ct3 < n->mInputs.size(); ct3++) {
339 if (n->mInputs[ct3]->mDstKernel.get() == k) {
340 ain = n->mInputs[ct3]->mAlloc.get();
341 //ALOGE(" link in %p", ain);
342 }
343 }
344 for (size_t ct3=0; ct3 < mInputs.size(); ct3++) {
345 if (mInputs[ct3]->mKernel == k) {
346 ain = mInputs[ct3]->mAlloc.get();
347 //ALOGE(" io in %p", ain);
Jason Samsdbe66d62012-09-17 13:54:41 -0700348 }
349 }
350
Yang Nib8353c52015-02-14 18:00:59 -0800351 for (size_t ct3=0; ct3 < n->mOutputs.size(); ct3++) {
352 if (n->mOutputs[ct3]->mSource.get() == k) {
353 aout = n->mOutputs[ct3]->mAlloc.get();
354 //ALOGE(" link out %p", aout);
355 }
356 }
357 for (size_t ct3=0; ct3 < mOutputs.size(); ct3++) {
358 if (mOutputs[ct3]->mKernel == k) {
359 aout = mOutputs[ct3]->mAlloc.get();
360 //ALOGE(" io out %p", aout);
Jason Samsdbe66d62012-09-17 13:54:41 -0700361 }
362 }
Chris Wailes93d6bc82014-07-28 16:54:38 -0700363
Yang Nib8353c52015-02-14 18:00:59 -0800364 if (ain == NULL) {
365 n->mScript->runForEach(rsc, k->mSlot, NULL, 0, aout, NULL, 0);
Chris Wailes93d6bc82014-07-28 16:54:38 -0700366
Chris Wailesf3712132014-07-16 15:18:30 -0700367 } else {
368 const Allocation *ains[1] = {ain};
Yang Nib8353c52015-02-14 18:00:59 -0800369 n->mScript->runForEach(rsc, k->mSlot, ains,
370 sizeof(ains) / sizeof(RsAllocation),
371 aout, NULL, 0);
Chris Wailesf3712132014-07-16 15:18:30 -0700372 }
Jason Samsdbe66d62012-09-17 13:54:41 -0700373 }
Yang Nib8353c52015-02-14 18:00:59 -0800374
Jason Samsdbe66d62012-09-17 13:54:41 -0700375 }
376
377}
378
Jason Samsdbe66d62012-09-17 13:54:41 -0700379ScriptGroup::Link::Link() {
380}
381
382ScriptGroup::Link::~Link() {
383}
384
385namespace android {
386namespace renderscript {
387
388
389RsScriptGroup rsi_ScriptGroupCreate(Context *rsc,
390 RsScriptKernelID * kernels, size_t kernelsSize,
391 RsScriptKernelID * src, size_t srcSize,
392 RsScriptKernelID * dstK, size_t dstKSize,
393 RsScriptFieldID * dstF, size_t dstFSize,
394 const RsType * type, size_t typeSize) {
395
396
397 return ScriptGroup::create(rsc,
398 (ScriptKernelID **) kernels, kernelsSize,
399 (ScriptKernelID **) src, srcSize,
400 (ScriptKernelID **) dstK, dstKSize,
401 (ScriptFieldID **) dstF, dstFSize,
402 (const Type **) type, typeSize);
403}
404
405
406void rsi_ScriptGroupSetInput(Context *rsc, RsScriptGroup sg, RsScriptKernelID kid,
407 RsAllocation alloc) {
Yang Nib8353c52015-02-14 18:00:59 -0800408 //ALOGE("rsi_ScriptGroupSetInput");
Jason Samsdbe66d62012-09-17 13:54:41 -0700409 ScriptGroup *s = (ScriptGroup *)sg;
410 s->setInput(rsc, (ScriptKernelID *)kid, (Allocation *)alloc);
411}
412
413void rsi_ScriptGroupSetOutput(Context *rsc, RsScriptGroup sg, RsScriptKernelID kid,
414 RsAllocation alloc) {
Yang Nib8353c52015-02-14 18:00:59 -0800415 //ALOGE("rsi_ScriptGroupSetOutput");
Jason Samsdbe66d62012-09-17 13:54:41 -0700416 ScriptGroup *s = (ScriptGroup *)sg;
417 s->setOutput(rsc, (ScriptKernelID *)kid, (Allocation *)alloc);
418}
419
420void rsi_ScriptGroupExecute(Context *rsc, RsScriptGroup sg) {
Yang Ni1ffd86b2015-01-07 09:16:40 -0800421 ScriptGroupBase *s = (ScriptGroupBase *)sg;
Jason Samsdbe66d62012-09-17 13:54:41 -0700422 s->execute(rsc);
423}
424
425}
426}