blob: dac79fd844e25e672d0e2565ef46ab1c9aa11912 [file] [log] [blame]
Yang Ni1ffd86b2015-01-07 09:16:40 -08001#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
8namespace android {
9namespace renderscript {
10
11RsClosure rsi_ClosureCreate(Context* context, RsScriptKernelID kernelID,
12 RsAllocation returnValue,
13 RsScriptFieldID* fieldIDs, size_t fieldIDs_length,
Yang Nifef0cd42015-11-11 15:08:16 -080014 const int64_t* values, size_t values_length,
15 const int* sizes, size_t sizes_length,
Yang Ni1ffd86b2015-01-07 09:16:40 -080016 RsClosure* depClosures, size_t depClosures_length,
17 RsScriptFieldID* depFieldIDs,
18 size_t depFieldIDs_length) {
Yang Niff2bb542015-02-02 14:33:47 -080019 rsAssert(fieldIDs_length == values_length && values_length == sizes_length &&
20 sizes_length == depClosures_length &&
21 depClosures_length == depFieldIDs_length);
Yang Ni1ffd86b2015-01-07 09:16:40 -080022
Yang Nibd0af2d2015-03-23 17:14:58 -070023 Closure* c = new Closure(
Yang Niff2bb542015-02-02 14:33:47 -080024 context, (const ScriptKernelID*)kernelID, (Allocation*)returnValue,
Yang Nifef0cd42015-11-11 15:08:16 -080025 fieldIDs_length, (const ScriptFieldID**)fieldIDs, values,
Yang Niff2bb542015-02-02 14:33:47 -080026 sizes, (const Closure**)depClosures,
Yang Nibd0af2d2015-03-23 17:14:58 -070027 (const ScriptFieldID**)depFieldIDs);
28 c->incUserRef();
29 return static_cast<RsClosure>(c);
Yang Ni1ffd86b2015-01-07 09:16:40 -080030}
31
Yang Nieb9aa672015-01-27 14:32:25 -080032RsClosure rsi_InvokeClosureCreate(Context* context, RsScriptInvokeID invokeID,
33 const void* params, const size_t paramLength,
34 const RsScriptFieldID* fieldIDs, const size_t fieldIDs_length,
Yang Nifef0cd42015-11-11 15:08:16 -080035 const int64_t* values, const size_t values_length,
Yang Nibd0af2d2015-03-23 17:14:58 -070036 const int* sizes, const size_t sizes_length) {
Yang Nieb9aa672015-01-27 14:32:25 -080037 rsAssert(fieldIDs_length == values_length && values_length == sizes_length);
Yang Nibd0af2d2015-03-23 17:14:58 -070038 Closure* c = new Closure(
Yang Nieb9aa672015-01-27 14:32:25 -080039 context, (const ScriptInvokeID*)invokeID, params, paramLength,
Yang Nifef0cd42015-11-11 15:08:16 -080040 fieldIDs_length, (const ScriptFieldID**)fieldIDs, values,
Yang Nibd0af2d2015-03-23 17:14:58 -070041 sizes);
42 c->incUserRef();
43 return static_cast<RsClosure>(c);
Yang Nieb9aa672015-01-27 14:32:25 -080044}
45
Yang Ni1ffd86b2015-01-07 09:16:40 -080046void rsi_ClosureSetArg(Context* rsc, RsClosure closure, uint32_t index,
Yang Nifef0cd42015-11-11 15:08:16 -080047 uintptr_t value, int size) {
Yang Nieb9aa672015-01-27 14:32:25 -080048 ((Closure*)closure)->setArg(index, (const void*)value, size);
Yang Ni1ffd86b2015-01-07 09:16:40 -080049}
50
51void rsi_ClosureSetGlobal(Context* rsc, RsClosure closure,
Yang Nifef0cd42015-11-11 15:08:16 -080052 RsScriptFieldID fieldID, int64_t value,
53 int size) {
Yang Nieb9aa672015-01-27 14:32:25 -080054 ((Closure*)closure)->setGlobal((const ScriptFieldID*)fieldID,
Yang Nifef0cd42015-11-11 15:08:16 -080055 value, size);
Yang Ni1ffd86b2015-01-07 09:16:40 -080056}
57
58Closure::Closure(Context* context,
59 const ScriptKernelID* kernelID,
60 Allocation* returnValue,
61 const int numValues,
62 const ScriptFieldID** fieldIDs,
Yang Nifef0cd42015-11-11 15:08:16 -080063 const int64_t* values,
Yang Nibd0af2d2015-03-23 17:14:58 -070064 const int* sizes,
Yang Ni1ffd86b2015-01-07 09:16:40 -080065 const Closure** depClosures,
66 const ScriptFieldID** depFieldIDs) :
Yang Ni062c2872015-02-20 15:20:00 -080067 ObjectBase(context), mContext(context), mFunctionID((IDBase*)kernelID),
68 mIsKernel(true), mReturnValue(returnValue), mParams(nullptr),
Yang Nieb9aa672015-01-27 14:32:25 -080069 mParamLength(0) {
Yang Niff2bb542015-02-02 14:33:47 -080070 size_t i;
Yang Ni1ffd86b2015-01-07 09:16:40 -080071
Yang Niff2bb542015-02-02 14:33:47 -080072 for (i = 0; i < (size_t)numValues && fieldIDs[i] == nullptr; i++);
Yang Ni1ffd86b2015-01-07 09:16:40 -080073
Yang Niff2bb542015-02-02 14:33:47 -080074 mNumArg = i;
75 mArgs = new const void*[mNumArg];
Yang Nifef0cd42015-11-11 15:08:16 -080076 for (size_t j = 0; j < mNumArg; j++) {
77 mArgs[j] = (const void*)values[j];
78 }
Yang Ni1ffd86b2015-01-07 09:16:40 -080079
Yang Niff2bb542015-02-02 14:33:47 -080080 for (; i < (size_t)numValues; i++) {
81 rsAssert(fieldIDs[i] != nullptr);
82 mGlobals[fieldIDs[i]] = make_pair(values[i], sizes[i]);
Yang Ni1ffd86b2015-01-07 09:16:40 -080083 }
Yang Niff2bb542015-02-02 14:33:47 -080084
Yang Niff2bb542015-02-02 14:33:47 -080085 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 Nibd0af2d2015-03-23 17:14:58 -070090 mapping = new Map<int, ObjectBaseRef<ScriptFieldID>>();
Yang Niff2bb542015-02-02 14:33:47 -080091 mArgDeps[dep] = mapping;
92 }
Yang Nibd0af2d2015-03-23 17:14:58 -070093 (*mapping)[i].set(const_cast<ScriptFieldID*>(depFieldIDs[i]));
Yang Niff2bb542015-02-02 14:33:47 -080094 }
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 Nibd0af2d2015-03-23 17:14:58 -0700102 mapping = new Map<const ScriptFieldID*,
103 ObjectBaseRef<ScriptFieldID>>();
Yang Niff2bb542015-02-02 14:33:47 -0800104 mGlobalDeps[dep] = mapping;
105 }
Yang Nibd0af2d2015-03-23 17:14:58 -0700106 fieldIDs[i]->incSysRef();
107 (*mapping)[fieldIDs[i]].set(const_cast<ScriptFieldID*>(depFieldIDs[i]));
Yang Niff2bb542015-02-02 14:33:47 -0800108 }
109 }
Yang Ni1ffd86b2015-01-07 09:16:40 -0800110}
111
Yang Nieb9aa672015-01-27 14:32:25 -0800112Closure::Closure(Context* context, const ScriptInvokeID* invokeID,
113 const void* params, const size_t paramLength,
114 const size_t numValues, const ScriptFieldID** fieldIDs,
Yang Nifef0cd42015-11-11 15:08:16 -0800115 const int64_t* values, const int* sizes) :
Yang Ni062c2872015-02-20 15:20:00 -0800116 ObjectBase(context), mContext(context), mFunctionID((IDBase*)invokeID), mIsKernel(false),
Yang Nibd0af2d2015-03-23 17:14:58 -0700117 mArgs(nullptr), mNumArg(0),
Yang Ni99bd4a42015-05-11 19:40:38 -0700118 mReturnValue(nullptr), mParamLength(paramLength) {
119 mParams = new uint8_t[mParamLength];
120 memcpy(mParams, params, mParamLength);
Yang Nieb9aa672015-01-27 14:32:25 -0800121 for (size_t i = 0; i < numValues; i++) {
Yang Niff2bb542015-02-02 14:33:47 -0800122 mGlobals[fieldIDs[i]] = make_pair(values[i], sizes[i]);
Yang Ni1ffd86b2015-01-07 09:16:40 -0800123 }
Yang Nieb9aa672015-01-27 14:32:25 -0800124}
Yang Ni1ffd86b2015-01-07 09:16:40 -0800125
Yang Nieb9aa672015-01-27 14:32:25 -0800126Closure::~Closure() {
127 for (const auto& p : mArgDeps) {
Yang Nieb9aa672015-01-27 14:32:25 -0800128 delete p.second;
Yang Ni1ffd86b2015-01-07 09:16:40 -0800129 }
Yang Nieb9aa672015-01-27 14:32:25 -0800130
131 for (const auto& p : mGlobalDeps) {
132 auto map = p.second;
133 for (const auto& p1 : *map) {
Yang Nibd0af2d2015-03-23 17:14:58 -0700134 p1.first->decSysRef();
Yang Nieb9aa672015-01-27 14:32:25 -0800135 }
136 delete p.second;
137 }
Yang Ni1ffd86b2015-01-07 09:16:40 -0800138
Yang Niff2bb542015-02-02 14:33:47 -0800139 delete[] mArgs;
Yang Ni99bd4a42015-05-11 19:40:38 -0700140 delete[] mParams;
Yang Ni1ffd86b2015-01-07 09:16:40 -0800141}
142
Yang Nifef0cd42015-11-11 15:08:16 -0800143void Closure::setArg(const uint32_t index, const void* value, const int size) {
Yang Nieb9aa672015-01-27 14:32:25 -0800144 mArgs[index] = value;
Yang Ni1ffd86b2015-01-07 09:16:40 -0800145}
146
Yang Nifef0cd42015-11-11 15:08:16 -0800147void Closure::setGlobal(const ScriptFieldID* fieldID, const int64_t value,
Yang Nibd0af2d2015-03-23 17:14:58 -0700148 const int size) {
Yang Niff2bb542015-02-02 14:33:47 -0800149 mGlobals[fieldID] = make_pair(value, size);
Yang Ni1ffd86b2015-01-07 09:16:40 -0800150}
151
152} // namespace renderscript
153} // namespace android