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