rsScript.cpp 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297
  1. /*
  2. * Copyright (C) 2009-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 "rsContext.h"
  17. #include <time.h>
  18. namespace android {
  19. namespace renderscript {
  20. Script::Script(Context *rsc) : ObjectBase(rsc) {
  21. memset(&mEnviroment, 0, sizeof(mEnviroment));
  22. memset(&mHal, 0, sizeof(mHal));
  23. mSlots = nullptr;
  24. mTypes = nullptr;
  25. mInitialized = false;
  26. mHasObjectSlots = false;
  27. mApiLevel = 0;
  28. }
  29. Script::~Script() {
  30. if (mSlots) {
  31. delete [] mSlots;
  32. mSlots = nullptr;
  33. }
  34. if (mTypes) {
  35. delete [] mTypes;
  36. mTypes = nullptr;
  37. }
  38. }
  39. void Script::setSlot(uint32_t slot, Allocation *a) {
  40. //ALOGE("setSlot %i %p", slot, a);
  41. if (slot >= mHal.info.exportedVariableCount) {
  42. ALOGE("Script::setSlot unable to set allocation, invalid slot index");
  43. return;
  44. }
  45. if (mRSC->hadFatalError()) return;
  46. mSlots[slot].set(a);
  47. mHasObjectSlots = true;
  48. mRSC->mHal.funcs.script.setGlobalBind(mRSC, this, slot, a);
  49. }
  50. void Script::setVar(uint32_t slot, const void *val, size_t len) {
  51. //ALOGE("setVar %i %p %i", slot, val, len);
  52. if (slot >= mHal.info.exportedVariableCount) {
  53. ALOGE("Script::setVar unable to set allocation, invalid slot index");
  54. return;
  55. }
  56. if (mRSC->hadFatalError()) return;
  57. mRSC->mHal.funcs.script.setGlobalVar(mRSC, this, slot, (void *)val, len);
  58. }
  59. void Script::getVar(uint32_t slot, const void *val, size_t len) {
  60. //ALOGE("getVar %i %p %i", slot, val, len);
  61. if (slot >= mHal.info.exportedVariableCount) {
  62. ALOGE("Script::getVar unable to set allocation, invalid slot index: "
  63. "%u >= %zu", slot, mHal.info.exportedVariableCount);
  64. return;
  65. }
  66. if (mRSC->hadFatalError()) return;
  67. mRSC->mHal.funcs.script.getGlobalVar(mRSC, this, slot, (void *)val, len);
  68. }
  69. void Script::setVar(uint32_t slot, const void *val, size_t len, Element *e,
  70. const uint32_t *dims, size_t dimLen) {
  71. if (slot >= mHal.info.exportedVariableCount) {
  72. ALOGE("Script::setVar unable to set allocation, invalid slot index: "
  73. "%u >= %zu", slot, mHal.info.exportedVariableCount);
  74. return;
  75. }
  76. if (mRSC->hadFatalError()) return;
  77. mRSC->mHal.funcs.script.setGlobalVarWithElemDims(mRSC, this, slot,
  78. (void *)val, len, e, dims, dimLen);
  79. }
  80. void Script::setVarObj(uint32_t slot, ObjectBase *val) {
  81. //ALOGE("setVarObj %i %p", slot, val);
  82. if (slot >= mHal.info.exportedVariableCount) {
  83. ALOGE("Script::setVarObj unable to set allocation, invalid slot index: "
  84. "%u >= %zu", slot, mHal.info.exportedVariableCount);
  85. return;
  86. }
  87. if (mRSC->hadFatalError()) return;
  88. mHasObjectSlots = true;
  89. mRSC->mHal.funcs.script.setGlobalObj(mRSC, this, slot, val);
  90. }
  91. void Script::callUpdateCacheObject(const Context *rsc, void *dstObj) const {
  92. if (rsc->mHal.funcs.script.updateCachedObject != nullptr) {
  93. rsc->mHal.funcs.script.updateCachedObject(rsc, this, (rs_script *)dstObj);
  94. } else {
  95. *((const void **)dstObj) = this;
  96. }
  97. }
  98. bool Script::freeChildren() {
  99. incSysRef();
  100. mRSC->mHal.funcs.script.invokeFreeChildren(mRSC, this);
  101. return decSysRef();
  102. }
  103. ScriptKernelID::ScriptKernelID(Context *rsc, Script *s, int slot, int sig)
  104. : IDBase(rsc, s, slot) {
  105. mHasKernelInput = (sig & 1) != 0;
  106. mHasKernelOutput = (sig & 2) != 0;
  107. }
  108. RsA3DClassID ScriptKernelID::getClassId() const {
  109. return RS_A3D_CLASS_ID_SCRIPT_KERNEL_ID;
  110. }
  111. ScriptInvokeID::ScriptInvokeID(Context *rsc, Script *s, int slot)
  112. : IDBase(rsc, s, slot) {
  113. }
  114. RsA3DClassID ScriptInvokeID::getClassId() const {
  115. return RS_A3D_CLASS_ID_SCRIPT_INVOKE_ID;
  116. }
  117. ScriptFieldID::ScriptFieldID(Context *rsc, Script *s, int slot) :
  118. IDBase(rsc, s, slot) {
  119. }
  120. RsA3DClassID ScriptFieldID::getClassId() const {
  121. return RS_A3D_CLASS_ID_SCRIPT_FIELD_ID;
  122. }
  123. RsScriptKernelID rsi_ScriptKernelIDCreate(Context *rsc, RsScript vs, int slot, int sig) {
  124. ScriptKernelID *kid = new ScriptKernelID(rsc, (Script *)vs, slot, sig);
  125. kid->incUserRef();
  126. return kid;
  127. }
  128. RsScriptInvokeID rsi_ScriptInvokeIDCreate(Context *rsc, RsScript vs, uint32_t slot) {
  129. ScriptInvokeID *iid = new ScriptInvokeID(rsc, (Script *)vs, slot);
  130. iid->incUserRef();
  131. return iid;
  132. }
  133. RsScriptFieldID rsi_ScriptFieldIDCreate(Context *rsc, RsScript vs, int slot) {
  134. ScriptFieldID *fid = new ScriptFieldID(rsc, (Script *)vs, slot);
  135. fid->incUserRef();
  136. return fid;
  137. }
  138. void rsi_ScriptBindAllocation(Context * rsc, RsScript vs, RsAllocation va, uint32_t slot) {
  139. Script *s = static_cast<Script *>(vs);
  140. Allocation *a = static_cast<Allocation *>(va);
  141. s->setSlot(slot, a);
  142. }
  143. void rsi_ScriptSetTimeZone(Context * rsc, RsScript vs, const char * timeZone, size_t length) {
  144. // We unfortunately need to make a new copy of the string, since it is
  145. // not nullptr-terminated. We then use setenv(), which properly handles
  146. // freeing/duplicating the actual string for the environment.
  147. char *tz = (char *) malloc(length + 1);
  148. if (!tz) {
  149. ALOGE("Couldn't allocate memory for timezone buffer");
  150. return;
  151. }
  152. strncpy(tz, timeZone, length);
  153. tz[length] = '\0';
  154. if (setenv("TZ", tz, 1) == 0) {
  155. tzset();
  156. } else {
  157. ALOGE("Error setting timezone");
  158. }
  159. free(tz);
  160. }
  161. void rsi_ScriptForEachMulti(Context *rsc, RsScript vs, uint32_t slot,
  162. RsAllocation *vains, size_t inLen,
  163. RsAllocation vaout, const void *params,
  164. size_t paramLen, const RsScriptCall *sc,
  165. size_t scLen) {
  166. Script *s = static_cast<Script *>(vs);
  167. Allocation **ains = (Allocation**)(vains);
  168. s->runForEach(rsc, slot,
  169. const_cast<const Allocation **>(ains), inLen,
  170. static_cast<Allocation *>(vaout), params, paramLen, sc);
  171. }
  172. void rsi_ScriptForEach(Context *rsc, RsScript vs, uint32_t slot,
  173. RsAllocation vain, RsAllocation vaout,
  174. const void *params, size_t paramLen,
  175. const RsScriptCall *sc, size_t scLen) {
  176. if (vain == nullptr) {
  177. rsi_ScriptForEachMulti(rsc, vs, slot, nullptr, 0, vaout, params, paramLen,
  178. sc, scLen);
  179. } else {
  180. RsAllocation ains[1] = {vain};
  181. rsi_ScriptForEachMulti(rsc, vs, slot, ains,
  182. sizeof(ains) / sizeof(RsAllocation), vaout,
  183. params, paramLen, sc, scLen);
  184. }
  185. }
  186. void rsi_ScriptReduce(Context *rsc, RsScript vs, uint32_t slot,
  187. RsAllocation *vains, size_t inLen,
  188. RsAllocation vaout, const RsScriptCall *sc,
  189. size_t scLen) {
  190. Script *s = static_cast<Script *>(vs);
  191. Allocation **ains = (Allocation**)(vains);
  192. s->runReduce(rsc, slot,
  193. const_cast<const Allocation **>(ains), inLen,
  194. static_cast<Allocation *>(vaout), sc);
  195. }
  196. void rsi_ScriptInvoke(Context *rsc, RsScript vs, uint32_t slot) {
  197. Script *s = static_cast<Script *>(vs);
  198. s->Invoke(rsc, slot, nullptr, 0);
  199. }
  200. void rsi_ScriptInvokeData(Context *rsc, RsScript vs, uint32_t slot, void *data) {
  201. Script *s = static_cast<Script *>(vs);
  202. s->Invoke(rsc, slot, nullptr, 0);
  203. }
  204. void rsi_ScriptInvokeV(Context *rsc, RsScript vs, uint32_t slot, const void *data, size_t len) {
  205. Script *s = static_cast<Script *>(vs);
  206. s->Invoke(rsc, slot, data, len);
  207. }
  208. void rsi_ScriptSetVarI(Context *rsc, RsScript vs, uint32_t slot, int value) {
  209. Script *s = static_cast<Script *>(vs);
  210. s->setVar(slot, &value, sizeof(value));
  211. }
  212. void rsi_ScriptSetVarObj(Context *rsc, RsScript vs, uint32_t slot, RsObjectBase value) {
  213. Script *s = static_cast<Script *>(vs);
  214. ObjectBase *o = static_cast<ObjectBase *>(value);
  215. s->setVarObj(slot, o);
  216. }
  217. void rsi_ScriptSetVarJ(Context *rsc, RsScript vs, uint32_t slot, int64_t value) {
  218. Script *s = static_cast<Script *>(vs);
  219. s->setVar(slot, &value, sizeof(value));
  220. }
  221. void rsi_ScriptSetVarF(Context *rsc, RsScript vs, uint32_t slot, float value) {
  222. Script *s = static_cast<Script *>(vs);
  223. s->setVar(slot, &value, sizeof(value));
  224. }
  225. void rsi_ScriptSetVarD(Context *rsc, RsScript vs, uint32_t slot, double value) {
  226. Script *s = static_cast<Script *>(vs);
  227. s->setVar(slot, &value, sizeof(value));
  228. }
  229. void rsi_ScriptSetVarV(Context *rsc, RsScript vs, uint32_t slot, const void *data, size_t len) {
  230. Script *s = static_cast<Script *>(vs);
  231. s->setVar(slot, data, len);
  232. }
  233. void rsi_ScriptGetVarV(Context *rsc, RsScript vs, uint32_t slot, void *data, size_t len) {
  234. Script *s = static_cast<Script *>(vs);
  235. s->getVar(slot, data, len);
  236. }
  237. void rsi_ScriptSetVarVE(Context *rsc, RsScript vs, uint32_t slot,
  238. const void *data, size_t len, RsElement ve,
  239. const uint32_t *dims, size_t dimLen) {
  240. Script *s = static_cast<Script *>(vs);
  241. Element *e = static_cast<Element *>(ve);
  242. s->setVar(slot, data, len, e, dims, dimLen);
  243. }
  244. } // namespace renderscript
  245. } // namespace android