rsClosure.cpp 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. #include "rsClosure.h"
  2. #include "cpu_ref/rsCpuCore.h"
  3. #include "rsContext.h" // XXX: necessary to avoid compiler error on rsScript.h below
  4. #include "rsScript.h"
  5. #include "rsType.h"
  6. namespace android {
  7. namespace renderscript {
  8. RsClosure rsi_ClosureCreate(Context* context, RsScriptKernelID kernelID,
  9. RsAllocation returnValue,
  10. RsScriptFieldID* fieldIDs, size_t fieldIDs_length,
  11. const int64_t* values, size_t values_length,
  12. const int* sizes, size_t sizes_length,
  13. RsClosure* depClosures, size_t depClosures_length,
  14. RsScriptFieldID* depFieldIDs,
  15. size_t depFieldIDs_length) {
  16. rsAssert(fieldIDs_length == values_length && values_length == sizes_length &&
  17. sizes_length == depClosures_length &&
  18. depClosures_length == depFieldIDs_length);
  19. Closure* c = new Closure(
  20. context, (const ScriptKernelID*)kernelID, (Allocation*)returnValue,
  21. fieldIDs_length, (const ScriptFieldID**)fieldIDs, values,
  22. sizes, (const Closure**)depClosures,
  23. (const ScriptFieldID**)depFieldIDs);
  24. c->incUserRef();
  25. return static_cast<RsClosure>(c);
  26. }
  27. RsClosure rsi_InvokeClosureCreate(Context* context, RsScriptInvokeID invokeID,
  28. const void* params, const size_t paramLength,
  29. const RsScriptFieldID* fieldIDs, const size_t fieldIDs_length,
  30. const int64_t* values, const size_t values_length,
  31. const int* sizes, const size_t sizes_length) {
  32. rsAssert(fieldIDs_length == values_length && values_length == sizes_length);
  33. Closure* c = new Closure(
  34. context, (const ScriptInvokeID*)invokeID, params, paramLength,
  35. fieldIDs_length, (const ScriptFieldID**)fieldIDs, values,
  36. sizes);
  37. c->incUserRef();
  38. return static_cast<RsClosure>(c);
  39. }
  40. void rsi_ClosureSetArg(Context* rsc, RsClosure closure, uint32_t index,
  41. uintptr_t value, int size) {
  42. ((Closure*)closure)->setArg(index, (const void*)value, size);
  43. }
  44. void rsi_ClosureSetGlobal(Context* rsc, RsClosure closure,
  45. RsScriptFieldID fieldID, int64_t value,
  46. int size) {
  47. ((Closure*)closure)->setGlobal((const ScriptFieldID*)fieldID,
  48. value, size);
  49. }
  50. Closure::Closure(Context* context,
  51. const ScriptKernelID* kernelID,
  52. Allocation* returnValue,
  53. const int numValues,
  54. const ScriptFieldID** fieldIDs,
  55. const int64_t* values,
  56. const int* sizes,
  57. const Closure** depClosures,
  58. const ScriptFieldID** depFieldIDs) :
  59. ObjectBase(context), mContext(context), mFunctionID((IDBase*)kernelID),
  60. mIsKernel(true), mReturnValue(returnValue), mParams(nullptr),
  61. mParamLength(0) {
  62. size_t i;
  63. for (i = 0; i < (size_t)numValues && fieldIDs[i] == nullptr; i++);
  64. mNumArg = i;
  65. mArgs = new const void*[mNumArg];
  66. for (size_t j = 0; j < mNumArg; j++) {
  67. mArgs[j] = (const void*)values[j];
  68. }
  69. for (; i < (size_t)numValues; i++) {
  70. rsAssert(fieldIDs[i] != nullptr);
  71. mGlobals[fieldIDs[i]] = make_pair(values[i], sizes[i]);
  72. }
  73. for (i = 0; i < mNumArg; i++) {
  74. const Closure* dep = depClosures[i];
  75. if (dep != nullptr) {
  76. auto mapping = mArgDeps[dep];
  77. if (mapping == nullptr) {
  78. mapping = new Map<int, ObjectBaseRef<ScriptFieldID>>();
  79. mArgDeps[dep] = mapping;
  80. }
  81. (*mapping)[i].set(const_cast<ScriptFieldID*>(depFieldIDs[i]));
  82. }
  83. }
  84. for (; i < (size_t)numValues; i++) {
  85. const Closure* dep = depClosures[i];
  86. if (dep != nullptr) {
  87. auto mapping = mGlobalDeps[dep];
  88. if (mapping == nullptr) {
  89. mapping = new Map<const ScriptFieldID*,
  90. ObjectBaseRef<ScriptFieldID>>();
  91. mGlobalDeps[dep] = mapping;
  92. }
  93. fieldIDs[i]->incSysRef();
  94. (*mapping)[fieldIDs[i]].set(const_cast<ScriptFieldID*>(depFieldIDs[i]));
  95. }
  96. }
  97. }
  98. Closure::Closure(Context* context, const ScriptInvokeID* invokeID,
  99. const void* params, const size_t paramLength,
  100. const size_t numValues, const ScriptFieldID** fieldIDs,
  101. const int64_t* values, const int* sizes) :
  102. ObjectBase(context), mContext(context), mFunctionID((IDBase*)invokeID), mIsKernel(false),
  103. mArgs(nullptr), mNumArg(0),
  104. mReturnValue(nullptr), mParamLength(paramLength) {
  105. mParams = new uint8_t[mParamLength];
  106. memcpy(mParams, params, mParamLength);
  107. for (size_t i = 0; i < numValues; i++) {
  108. mGlobals[fieldIDs[i]] = make_pair(values[i], sizes[i]);
  109. }
  110. }
  111. Closure::~Closure() {
  112. for (const auto& p : mArgDeps) {
  113. delete p.second;
  114. }
  115. for (const auto& p : mGlobalDeps) {
  116. auto map = p.second;
  117. for (const auto& p1 : *map) {
  118. p1.first->decSysRef();
  119. }
  120. delete p.second;
  121. }
  122. delete[] mArgs;
  123. delete[] mParams;
  124. }
  125. void Closure::setArg(const uint32_t index, const void* value, const int size) {
  126. mArgs[index] = value;
  127. }
  128. void Closure::setGlobal(const ScriptFieldID* fieldID, const int64_t value,
  129. const int size) {
  130. mGlobals[fieldID] = make_pair(value, size);
  131. }
  132. } // namespace renderscript
  133. } // namespace android