rsScriptGroup.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403
  1. /*
  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. #include "rsScriptGroup.h"
  17. #include "rsContext.h"
  18. // TODO: Is this header needed here?
  19. #include "rsScriptGroup2.h"
  20. #include <algorithm>
  21. #include <time.h>
  22. namespace android {
  23. namespace renderscript {
  24. ScriptGroup::ScriptGroup(Context *rsc) : ScriptGroupBase(rsc) {
  25. }
  26. ScriptGroup::~ScriptGroup() {
  27. if (mRSC->mHal.funcs.scriptgroup.destroy) {
  28. mRSC->mHal.funcs.scriptgroup.destroy(mRSC, this);
  29. }
  30. for (size_t ct=0; ct < mLinks.size(); ct++) {
  31. delete mLinks[ct];
  32. }
  33. for (auto input : mInputs) {
  34. input->mAlloc.clear();
  35. }
  36. for (auto output : mOutputs) {
  37. output->mAlloc.clear();
  38. }
  39. }
  40. ScriptGroup::IO::IO(const ScriptKernelID *kid) {
  41. mKernel = kid;
  42. }
  43. ScriptGroup::Node::Node(Script *s) {
  44. mScript = s;
  45. mSeen = false;
  46. mOrder = 0;
  47. }
  48. ScriptGroup::Node * ScriptGroup::findNode(Script *s) const {
  49. //ALOGE("find %p %i", s, (int)mNodes.size());
  50. for (size_t ct=0; ct < mNodes.size(); ct++) {
  51. Node *n = mNodes[ct];
  52. for (size_t ct2=0; ct2 < n->mKernels.size(); ct2++) {
  53. if (n->mKernels[ct2]->mScript == s) {
  54. return n;
  55. }
  56. }
  57. }
  58. return nullptr;
  59. }
  60. bool ScriptGroup::calcOrderRecurse(Node *n, int depth) {
  61. n->mSeen = true;
  62. if (n->mOrder < depth) {
  63. n->mOrder = depth;
  64. }
  65. bool ret = true;
  66. for (size_t ct=0; ct < n->mOutputs.size(); ct++) {
  67. const Link *l = n->mOutputs[ct];
  68. Node *nt = NULL;
  69. if (l->mDstField.get()) {
  70. nt = findNode(l->mDstField->mScript);
  71. } else {
  72. nt = findNode(l->mDstKernel->mScript);
  73. }
  74. if (nt->mSeen) {
  75. return false;
  76. }
  77. ret &= calcOrderRecurse(nt, n->mOrder + 1);
  78. }
  79. return ret;
  80. }
  81. class NodeCompare {
  82. public:
  83. bool operator() (const ScriptGroup::Node* lhs,
  84. const ScriptGroup::Node* rhs) {
  85. return (lhs->mOrder < rhs->mOrder);
  86. }
  87. };
  88. bool ScriptGroup::calcOrder() {
  89. // Make nodes
  90. for (size_t ct=0; ct < mKernels.size(); ct++) {
  91. const ScriptKernelID *k = mKernels[ct].get();
  92. //ALOGE(" kernel %i, %p s=%p", (int)ct, k, mKernels[ct]->mScript);
  93. Node *n = findNode(k->mScript);
  94. //ALOGE(" n = %p", n);
  95. if (n == NULL) {
  96. n = new Node(k->mScript);
  97. mNodes.push_back(n);
  98. }
  99. n->mKernels.push_back(k);
  100. }
  101. // add links
  102. //ALOGE("link count %i", (int)mLinks.size());
  103. for (size_t ct=0; ct < mLinks.size(); ct++) {
  104. Link *l = mLinks[ct];
  105. //ALOGE("link %i %p", (int)ct, l);
  106. Node *n = findNode(l->mSource->mScript);
  107. //ALOGE("link n %p", n);
  108. n->mOutputs.push_back(l);
  109. if (l->mDstKernel.get()) {
  110. //ALOGE("l->mDstKernel.get() %p", l->mDstKernel.get());
  111. n = findNode(l->mDstKernel->mScript);
  112. //ALOGE(" n1 %p", n);
  113. n->mInputs.push_back(l);
  114. } else {
  115. n = findNode(l->mDstField->mScript);
  116. //ALOGE(" n2 %p", n);
  117. n->mInputs.push_back(l);
  118. }
  119. }
  120. //ALOGE("node count %i", (int)mNodes.size());
  121. // Order nodes
  122. bool ret = true;
  123. for (size_t ct=0; ct < mNodes.size(); ct++) {
  124. Node *n = mNodes[ct];
  125. if (n->mInputs.size() == 0) {
  126. for (size_t ct2=0; ct2 < mNodes.size(); ct2++) {
  127. mNodes[ct2]->mSeen = false;
  128. }
  129. ret &= calcOrderRecurse(n, 0);
  130. }
  131. }
  132. for (size_t ct=0; ct < mKernels.size(); ct++) {
  133. const ScriptKernelID *k = mKernels[ct].get();
  134. const Node *n = findNode(k->mScript);
  135. if (k->mHasKernelOutput) {
  136. bool found = false;
  137. for (size_t ct2=0; ct2 < n->mOutputs.size(); ct2++) {
  138. if (n->mOutputs[ct2]->mSource.get() == k) {
  139. found = true;
  140. break;
  141. }
  142. }
  143. if (!found) {
  144. //ALOGE("add io out %p", k);
  145. mOutputs.push_back(new IO(k));
  146. }
  147. }
  148. if (k->mHasKernelInput) {
  149. bool found = false;
  150. for (size_t ct2=0; ct2 < n->mInputs.size(); ct2++) {
  151. if (n->mInputs[ct2]->mDstKernel.get() == k) {
  152. found = true;
  153. break;
  154. }
  155. }
  156. if (!found) {
  157. //ALOGE("add io in %p", k);
  158. mInputs.push_back(new IO(k));
  159. }
  160. }
  161. }
  162. // Sort mNodes in the increasing order.
  163. std::sort(mNodes.begin(), mNodes.end(), NodeCompare());
  164. return ret;
  165. }
  166. ScriptGroup * ScriptGroup::create(Context *rsc,
  167. ScriptKernelID ** kernels, size_t kernelsSize,
  168. ScriptKernelID ** src, size_t srcSize,
  169. ScriptKernelID ** dstK, size_t dstKSize,
  170. ScriptFieldID ** dstF, size_t dstFSize,
  171. const Type ** type, size_t typeSize) {
  172. size_t kernelCount = kernelsSize / sizeof(ScriptKernelID *);
  173. size_t linkCount = typeSize / sizeof(Type *);
  174. //ALOGE("ScriptGroup::create kernels=%i links=%i", (int)kernelCount, (int)linkCount);
  175. // Start by counting unique kernel sources
  176. ScriptGroup *sg = new ScriptGroup(rsc);
  177. sg->mKernels.reserve(kernelCount);
  178. for (size_t ct=0; ct < kernelCount; ct++) {
  179. sg->mKernels.push_back(kernels[ct]);
  180. }
  181. sg->mLinks.reserve(linkCount);
  182. for (size_t ct=0; ct < linkCount; ct++) {
  183. Link *l = new Link();
  184. l->mType = type[ct];
  185. l->mSource = src[ct];
  186. l->mDstField = dstF[ct];
  187. l->mDstKernel = dstK[ct];
  188. sg->mLinks.push_back(l);
  189. }
  190. sg->calcOrder();
  191. // allocate links
  192. for (size_t ct=0; ct < sg->mNodes.size(); ct++) {
  193. const Node *n = sg->mNodes[ct];
  194. for (size_t ct2=0; ct2 < n->mOutputs.size(); ct2++) {
  195. Link *l = n->mOutputs[ct2];
  196. if (l->mAlloc.get()) {
  197. continue;
  198. }
  199. Allocation * alloc = Allocation::createAllocation(rsc,
  200. l->mType.get(), RS_ALLOCATION_USAGE_SCRIPT);
  201. l->mAlloc = alloc;
  202. for (size_t ct3=ct2+1; ct3 < n->mOutputs.size(); ct3++) {
  203. if (n->mOutputs[ct3]->mSource.get() == l->mSource.get()) {
  204. n->mOutputs[ct3]->mAlloc = alloc;
  205. }
  206. }
  207. }
  208. }
  209. if (rsc->mHal.funcs.scriptgroup.init) {
  210. rsc->mHal.funcs.scriptgroup.init(rsc, sg);
  211. }
  212. sg->incUserRef();
  213. return sg;
  214. }
  215. void ScriptGroup::setInput(Context *rsc, ScriptKernelID *kid, Allocation *a) {
  216. for (size_t ct=0; ct < mInputs.size(); ct++) {
  217. if (mInputs[ct]->mKernel == kid) {
  218. mInputs[ct]->mAlloc = a;
  219. if (rsc->mHal.funcs.scriptgroup.setInput) {
  220. rsc->mHal.funcs.scriptgroup.setInput(rsc, this, kid, a);
  221. }
  222. return;
  223. }
  224. }
  225. rsAssert(!"ScriptGroup:setInput kid not found");
  226. }
  227. void ScriptGroup::setOutput(Context *rsc, ScriptKernelID *kid, Allocation *a) {
  228. for (size_t ct=0; ct < mOutputs.size(); ct++) {
  229. if (mOutputs[ct]->mKernel == kid) {
  230. mOutputs[ct]->mAlloc = a;
  231. if (rsc->mHal.funcs.scriptgroup.setOutput) {
  232. rsc->mHal.funcs.scriptgroup.setOutput(rsc, this, kid, a);
  233. }
  234. return;
  235. }
  236. }
  237. rsAssert(!"ScriptGroup:setOutput kid not found");
  238. }
  239. bool ScriptGroup::validateInputAndOutput(Context *rsc) {
  240. for(size_t i = 0; i < mInputs.size(); i++) {
  241. if (mInputs[i]->mAlloc.get() == nullptr) {
  242. rsc->setError(RS_ERROR_BAD_VALUE, "ScriptGroup missing input.");
  243. return false;
  244. }
  245. }
  246. for(size_t i = 0; i < mOutputs.size(); i++) {
  247. if (mOutputs[i]->mAlloc.get() == nullptr) {
  248. rsc->setError(RS_ERROR_BAD_VALUE, "ScriptGroup missing output.");
  249. return false;
  250. }
  251. }
  252. return true;
  253. }
  254. void ScriptGroup::execute(Context *rsc) {
  255. if (!validateInputAndOutput(rsc)) {
  256. return;
  257. }
  258. if (rsc->mHal.funcs.scriptgroup.execute) {
  259. rsc->mHal.funcs.scriptgroup.execute(rsc, this);
  260. return;
  261. }
  262. for (size_t ct=0; ct < mNodes.size(); ct++) {
  263. Node *n = mNodes[ct];
  264. //ALOGE("node %i, order %i, in %i out %i", (int)ct, n->mOrder, (int)n->mInputs.size(), (int)n->mOutputs.size());
  265. for (size_t ct2=0; ct2 < n->mKernels.size(); ct2++) {
  266. const ScriptKernelID *k = n->mKernels[ct2];
  267. Allocation *ain = NULL;
  268. Allocation *aout = NULL;
  269. for (size_t ct3=0; ct3 < n->mInputs.size(); ct3++) {
  270. if (n->mInputs[ct3]->mDstKernel.get() == k) {
  271. ain = n->mInputs[ct3]->mAlloc.get();
  272. //ALOGE(" link in %p", ain);
  273. }
  274. }
  275. for (size_t ct3=0; ct3 < mInputs.size(); ct3++) {
  276. if (mInputs[ct3]->mKernel == k) {
  277. ain = mInputs[ct3]->mAlloc.get();
  278. //ALOGE(" io in %p", ain);
  279. }
  280. }
  281. for (size_t ct3=0; ct3 < n->mOutputs.size(); ct3++) {
  282. if (n->mOutputs[ct3]->mSource.get() == k) {
  283. aout = n->mOutputs[ct3]->mAlloc.get();
  284. //ALOGE(" link out %p", aout);
  285. }
  286. }
  287. for (size_t ct3=0; ct3 < mOutputs.size(); ct3++) {
  288. if (mOutputs[ct3]->mKernel == k) {
  289. aout = mOutputs[ct3]->mAlloc.get();
  290. //ALOGE(" io out %p", aout);
  291. }
  292. }
  293. if (ain == NULL) {
  294. n->mScript->runForEach(rsc, k->mSlot, NULL, 0, aout, NULL, 0);
  295. } else {
  296. const Allocation *ains[1] = {ain};
  297. n->mScript->runForEach(rsc, k->mSlot, ains,
  298. sizeof(ains) / sizeof(RsAllocation),
  299. aout, NULL, 0);
  300. }
  301. }
  302. }
  303. }
  304. ScriptGroup::Link::Link() {
  305. }
  306. ScriptGroup::Link::~Link() {
  307. }
  308. RsScriptGroup rsi_ScriptGroupCreate(Context *rsc,
  309. RsScriptKernelID * kernels, size_t kernelsSize,
  310. RsScriptKernelID * src, size_t srcSize,
  311. RsScriptKernelID * dstK, size_t dstKSize,
  312. RsScriptFieldID * dstF, size_t dstFSize,
  313. const RsType * type, size_t typeSize) {
  314. return ScriptGroup::create(rsc,
  315. (ScriptKernelID **) kernels, kernelsSize,
  316. (ScriptKernelID **) src, srcSize,
  317. (ScriptKernelID **) dstK, dstKSize,
  318. (ScriptFieldID **) dstF, dstFSize,
  319. (const Type **) type, typeSize);
  320. }
  321. void rsi_ScriptGroupSetInput(Context *rsc, RsScriptGroup sg, RsScriptKernelID kid,
  322. RsAllocation alloc) {
  323. //ALOGE("rsi_ScriptGroupSetInput");
  324. ScriptGroup *s = (ScriptGroup *)sg;
  325. s->setInput(rsc, (ScriptKernelID *)kid, (Allocation *)alloc);
  326. }
  327. void rsi_ScriptGroupSetOutput(Context *rsc, RsScriptGroup sg, RsScriptKernelID kid,
  328. RsAllocation alloc) {
  329. //ALOGE("rsi_ScriptGroupSetOutput");
  330. ScriptGroup *s = (ScriptGroup *)sg;
  331. s->setOutput(rsc, (ScriptKernelID *)kid, (Allocation *)alloc);
  332. }
  333. void rsi_ScriptGroupExecute(Context *rsc, RsScriptGroup sg) {
  334. ScriptGroupBase *s = (ScriptGroupBase *)sg;
  335. s->execute(rsc);
  336. }
  337. } // namespace renderscript
  338. } // namespace android