blob: cb611afcb12ac73da56db2337d97ebbe5757a49b [file] [log] [blame]
Jason Sams326e0dd2009-05-22 14:03:28 -07001/*
Stephen Hines44199772012-02-21 20:13:12 -08002 * Copyright (C) 2009-2012 The Android Open Source Project
Jason Sams326e0dd2009-05-22 14:03:28 -07003 *
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
17#include "rsContext.h"
Stephen Hinesd2432b92011-11-09 18:02:20 -080018#include <time.h>
Jason Sams326e0dd2009-05-22 14:03:28 -070019
20using namespace android;
21using namespace android::renderscript;
22
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -080023Script::Script(Context *rsc) : ObjectBase(rsc) {
Jason Sams928b7342009-06-08 18:50:13 -070024 memset(&mEnviroment, 0, sizeof(mEnviroment));
Jason Samsbad80742011-03-16 16:29:28 -070025 memset(&mHal, 0, sizeof(mHal));
Alex Sakhartchouk700ba382010-10-08 15:00:05 -070026
Chris Wailes44bef6f2014-08-12 13:51:10 -070027 mSlots = nullptr;
28 mTypes = nullptr;
Jason Sams77020c52011-11-22 12:49:11 -080029 mInitialized = false;
Stephen Hinesc78839b2013-09-10 17:40:41 -070030 mHasObjectSlots = false;
Jason Sams326e0dd2009-05-22 14:03:28 -070031}
32
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -080033Script::~Script() {
34 if (mSlots) {
Alex Sakhartchouk700ba382010-10-08 15:00:05 -070035 delete [] mSlots;
Chris Wailes44bef6f2014-08-12 13:51:10 -070036 mSlots = nullptr;
Alex Sakhartchouk700ba382010-10-08 15:00:05 -070037 }
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -080038 if (mTypes) {
Alex Sakhartchouk700ba382010-10-08 15:00:05 -070039 delete [] mTypes;
Chris Wailes44bef6f2014-08-12 13:51:10 -070040 mTypes = nullptr;
Alex Sakhartchouk700ba382010-10-08 15:00:05 -070041 }
42}
43
Alex Sakhartchouk700ba382010-10-08 15:00:05 -070044void Script::setSlot(uint32_t slot, Allocation *a) {
Steve Blockaf12ac62012-01-06 19:20:56 +000045 //ALOGE("setSlot %i %p", slot, a);
Jason Samsbad80742011-03-16 16:29:28 -070046 if (slot >= mHal.info.exportedVariableCount) {
Steve Blockaf12ac62012-01-06 19:20:56 +000047 ALOGE("Script::setSlot unable to set allocation, invalid slot index");
Alex Sakhartchouk700ba382010-10-08 15:00:05 -070048 return;
49 }
50
Jason Sams4efe3d32015-03-18 18:28:06 -070051 if (mRSC->hadFatalError()) return;
52
Alex Sakhartchouk700ba382010-10-08 15:00:05 -070053 mSlots[slot].set(a);
Stephen Hinesc78839b2013-09-10 17:40:41 -070054 mHasObjectSlots = true;
Jason Sams807fdc42012-07-25 17:55:39 -070055 mRSC->mHal.funcs.script.setGlobalBind(mRSC, this, slot, a);
Jason Sams326e0dd2009-05-22 14:03:28 -070056}
57
Alex Sakhartchoukb81a0eb2011-06-03 10:18:01 -070058void Script::setVar(uint32_t slot, const void *val, size_t len) {
Steve Blockaf12ac62012-01-06 19:20:56 +000059 //ALOGE("setVar %i %p %i", slot, val, len);
Jason Samsbad80742011-03-16 16:29:28 -070060 if (slot >= mHal.info.exportedVariableCount) {
Steve Blockaf12ac62012-01-06 19:20:56 +000061 ALOGE("Script::setVar unable to set allocation, invalid slot index");
Jason Samsbad80742011-03-16 16:29:28 -070062 return;
Jason Samsbe36bf32010-05-11 14:03:58 -070063 }
Jason Sams4efe3d32015-03-18 18:28:06 -070064 if (mRSC->hadFatalError()) return;
65
Jason Samsbad80742011-03-16 16:29:28 -070066 mRSC->mHal.funcs.script.setGlobalVar(mRSC, this, slot, (void *)val, len);
Jason Samsbe36bf32010-05-11 14:03:58 -070067}
68
Tim Murray9c642392013-04-11 13:29:59 -070069void Script::getVar(uint32_t slot, const void *val, size_t len) {
70 //ALOGE("getVar %i %p %i", slot, val, len);
71 if (slot >= mHal.info.exportedVariableCount) {
Stephen Hinescadee382013-12-12 13:21:00 -080072 ALOGE("Script::getVar unable to set allocation, invalid slot index: "
Nick Kralevich02759d72013-12-15 21:04:00 -080073 "%u >= %zu", slot, mHal.info.exportedVariableCount);
Tim Murray9c642392013-04-11 13:29:59 -070074 return;
75 }
Jason Sams4efe3d32015-03-18 18:28:06 -070076 if (mRSC->hadFatalError()) return;
77
Tim Murray9c642392013-04-11 13:29:59 -070078 mRSC->mHal.funcs.script.getGlobalVar(mRSC, this, slot, (void *)val, len);
79}
80
Stephen Hines2980f072012-04-09 18:26:29 -070081void Script::setVar(uint32_t slot, const void *val, size_t len, Element *e,
Stephen Hinesf0a50782014-06-25 13:41:10 -070082 const uint32_t *dims, size_t dimLen) {
Stephen Hines2980f072012-04-09 18:26:29 -070083 if (slot >= mHal.info.exportedVariableCount) {
Stephen Hinescadee382013-12-12 13:21:00 -080084 ALOGE("Script::setVar unable to set allocation, invalid slot index: "
Nick Kralevich02759d72013-12-15 21:04:00 -080085 "%u >= %zu", slot, mHal.info.exportedVariableCount);
Stephen Hines2980f072012-04-09 18:26:29 -070086 return;
87 }
Jason Sams4efe3d32015-03-18 18:28:06 -070088 if (mRSC->hadFatalError()) return;
89
Stephen Hines2980f072012-04-09 18:26:29 -070090 mRSC->mHal.funcs.script.setGlobalVarWithElemDims(mRSC, this, slot,
91 (void *)val, len, e, dims, dimLen);
92}
93
Jason Samsa5eb6e12010-11-16 17:37:02 -080094void Script::setVarObj(uint32_t slot, ObjectBase *val) {
Steve Blockaf12ac62012-01-06 19:20:56 +000095 //ALOGE("setVarObj %i %p", slot, val);
Jason Samsbad80742011-03-16 16:29:28 -070096 if (slot >= mHal.info.exportedVariableCount) {
Stephen Hinescadee382013-12-12 13:21:00 -080097 ALOGE("Script::setVarObj unable to set allocation, invalid slot index: "
Nick Kralevich02759d72013-12-15 21:04:00 -080098 "%u >= %zu", slot, mHal.info.exportedVariableCount);
Jason Samsbad80742011-03-16 16:29:28 -070099 return;
Jason Samsa5eb6e12010-11-16 17:37:02 -0800100 }
Jason Sams4efe3d32015-03-18 18:28:06 -0700101 if (mRSC->hadFatalError()) return;
102
Stephen Hinesc78839b2013-09-10 17:40:41 -0700103 mHasObjectSlots = true;
Jason Samsbad80742011-03-16 16:29:28 -0700104 mRSC->mHal.funcs.script.setGlobalObj(mRSC, this, slot, val);
Jason Samsa5eb6e12010-11-16 17:37:02 -0800105}
106
Jason Samsa36c50a2014-06-17 12:06:06 -0700107void Script::callUpdateCacheObject(const Context *rsc, void *dstObj) const {
Chris Wailes44bef6f2014-08-12 13:51:10 -0700108 if (rsc->mHal.funcs.script.updateCachedObject != nullptr) {
Jason Samsa36c50a2014-06-17 12:06:06 -0700109 rsc->mHal.funcs.script.updateCachedObject(rsc, this, (rs_script *)dstObj);
110 } else {
111 *((const void **)dstObj) = this;
112 }
113}
114
Stephen Hines4ee16ff2011-08-31 17:41:39 -0700115bool Script::freeChildren() {
116 incSysRef();
117 mRSC->mHal.funcs.script.invokeFreeChildren(mRSC, this);
118 return decSysRef();
119}
120
Jason Samsdbe66d62012-09-17 13:54:41 -0700121ScriptKernelID::ScriptKernelID(Context *rsc, Script *s, int slot, int sig)
Yang Nieb9aa672015-01-27 14:32:25 -0800122 : IDBase(rsc, s, slot) {
Jason Samsdbe66d62012-09-17 13:54:41 -0700123 mHasKernelInput = (sig & 1) != 0;
124 mHasKernelOutput = (sig & 2) != 0;
125}
126
Jason Samsdbe66d62012-09-17 13:54:41 -0700127RsA3DClassID ScriptKernelID::getClassId() const {
128 return RS_A3D_CLASS_ID_SCRIPT_KERNEL_ID;
129}
130
Yang Nieb9aa672015-01-27 14:32:25 -0800131ScriptInvokeID::ScriptInvokeID(Context *rsc, Script *s, int slot)
132 : IDBase(rsc, s, slot) {
Jason Samsdbe66d62012-09-17 13:54:41 -0700133}
134
Yang Nieb9aa672015-01-27 14:32:25 -0800135RsA3DClassID ScriptInvokeID::getClassId() const {
136 return RS_A3D_CLASS_ID_SCRIPT_INVOKE_ID;
Jason Samsdbe66d62012-09-17 13:54:41 -0700137}
138
Yang Nieb9aa672015-01-27 14:32:25 -0800139ScriptFieldID::ScriptFieldID(Context *rsc, Script *s, int slot) :
140 IDBase(rsc, s, slot) {
Jason Samsdbe66d62012-09-17 13:54:41 -0700141}
142
143RsA3DClassID ScriptFieldID::getClassId() const {
144 return RS_A3D_CLASS_ID_SCRIPT_FIELD_ID;
145}
146
147
Jason Sams326e0dd2009-05-22 14:03:28 -0700148namespace android {
149namespace renderscript {
150
Jason Samsdbe66d62012-09-17 13:54:41 -0700151RsScriptKernelID rsi_ScriptKernelIDCreate(Context *rsc, RsScript vs, int slot, int sig) {
Stephen Hines61c86952013-04-09 17:34:43 -0700152 ScriptKernelID *kid = new ScriptKernelID(rsc, (Script *)vs, slot, sig);
153 kid->incUserRef();
154 return kid;
Jason Samsdbe66d62012-09-17 13:54:41 -0700155}
156
Yang Nieb9aa672015-01-27 14:32:25 -0800157RsScriptInvokeID rsi_ScriptInvokeIDCreate(Context *rsc, RsScript vs, uint32_t slot) {
158 ScriptInvokeID *iid = new ScriptInvokeID(rsc, (Script *)vs, slot);
159 iid->incUserRef();
160 return iid;
161}
162
Jason Samsdbe66d62012-09-17 13:54:41 -0700163RsScriptFieldID rsi_ScriptFieldIDCreate(Context *rsc, RsScript vs, int slot) {
Stephen Hines61c86952013-04-09 17:34:43 -0700164 ScriptFieldID *fid = new ScriptFieldID(rsc, (Script *)vs, slot);
165 fid->incUserRef();
166 return fid;
Jason Samsdbe66d62012-09-17 13:54:41 -0700167}
168
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800169void rsi_ScriptBindAllocation(Context * rsc, RsScript vs, RsAllocation va, uint32_t slot) {
Jason Sams326e0dd2009-05-22 14:03:28 -0700170 Script *s = static_cast<Script *>(vs);
Jason Samsbe36bf32010-05-11 14:03:58 -0700171 Allocation *a = static_cast<Allocation *>(va);
Alex Sakhartchouk700ba382010-10-08 15:00:05 -0700172 s->setSlot(slot, a);
Jason Sams326e0dd2009-05-22 14:03:28 -0700173}
174
Alex Sakhartchoukb81a0eb2011-06-03 10:18:01 -0700175void rsi_ScriptSetTimeZone(Context * rsc, RsScript vs, const char * timeZone, size_t length) {
Stephen Hinesd2432b92011-11-09 18:02:20 -0800176 // We unfortunately need to make a new copy of the string, since it is
Chris Wailes44bef6f2014-08-12 13:51:10 -0700177 // not nullptr-terminated. We then use setenv(), which properly handles
Stephen Hinesd2432b92011-11-09 18:02:20 -0800178 // freeing/duplicating the actual string for the environment.
179 char *tz = (char *) malloc(length + 1);
180 if (!tz) {
Steve Blockaf12ac62012-01-06 19:20:56 +0000181 ALOGE("Couldn't allocate memory for timezone buffer");
Stephen Hinesd2432b92011-11-09 18:02:20 -0800182 return;
183 }
184 strncpy(tz, timeZone, length);
185 tz[length] = '\0';
186 if (setenv("TZ", tz, 1) == 0) {
187 tzset();
188 } else {
Steve Blockaf12ac62012-01-06 19:20:56 +0000189 ALOGE("Error setting timezone");
Stephen Hinesd2432b92011-11-09 18:02:20 -0800190 }
191 free(tz);
Jason Samsd34b7252009-08-04 16:58:20 -0700192}
193
Chris Wailes4b3c34e2014-06-11 12:00:29 -0700194void rsi_ScriptForEachMulti(Context *rsc, RsScript vs, uint32_t slot,
195 RsAllocation *vains, size_t inLen,
196 RsAllocation vaout, const void *params,
197 size_t paramLen, const RsScriptCall *sc,
198 size_t scLen) {
Chris Wailesf3712132014-07-16 15:18:30 -0700199
200 Script *s = static_cast<Script *>(vs);
Chris Wailes4b3c34e2014-06-11 12:00:29 -0700201 Allocation **ains = (Allocation**)(vains);
202
203 s->runForEach(rsc, slot,
204 const_cast<const Allocation **>(ains), inLen,
205 static_cast<Allocation *>(vaout), params, paramLen, sc);
206
207}
208
Chris Wailesf3712132014-07-16 15:18:30 -0700209void rsi_ScriptForEach(Context *rsc, RsScript vs, uint32_t slot,
210 RsAllocation vain, RsAllocation vaout,
211 const void *params, size_t paramLen,
212 const RsScriptCall *sc, size_t scLen) {
213
Chris Wailes44bef6f2014-08-12 13:51:10 -0700214 if (vain == nullptr) {
215 rsi_ScriptForEachMulti(rsc, vs, slot, nullptr, 0, vaout, params, paramLen,
Chris Wailesf3712132014-07-16 15:18:30 -0700216 sc, scLen);
217 } else {
218 RsAllocation ains[1] = {vain};
219
220 rsi_ScriptForEachMulti(rsc, vs, slot, ains,
221 sizeof(ains) / sizeof(RsAllocation), vaout,
222 params, paramLen, sc, scLen);
223 }
224}
225
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800226void rsi_ScriptInvoke(Context *rsc, RsScript vs, uint32_t slot) {
Jason Sams8c6bc692009-09-16 15:04:38 -0700227 Script *s = static_cast<Script *>(vs);
Chris Wailes44bef6f2014-08-12 13:51:10 -0700228 s->Invoke(rsc, slot, nullptr, 0);
Jason Sams8c6bc692009-09-16 15:04:38 -0700229}
230
231
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800232void rsi_ScriptInvokeData(Context *rsc, RsScript vs, uint32_t slot, void *data) {
Jason Samsbe36bf32010-05-11 14:03:58 -0700233 Script *s = static_cast<Script *>(vs);
Chris Wailes44bef6f2014-08-12 13:51:10 -0700234 s->Invoke(rsc, slot, nullptr, 0);
Jason Samsbe36bf32010-05-11 14:03:58 -0700235}
236
Alex Sakhartchoukb81a0eb2011-06-03 10:18:01 -0700237void rsi_ScriptInvokeV(Context *rsc, RsScript vs, uint32_t slot, const void *data, size_t len) {
Jason Samsbe36bf32010-05-11 14:03:58 -0700238 Script *s = static_cast<Script *>(vs);
Jason Sams22fa3712010-05-19 17:22:57 -0700239 s->Invoke(rsc, slot, data, len);
Jason Samsbe36bf32010-05-11 14:03:58 -0700240}
241
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800242void rsi_ScriptSetVarI(Context *rsc, RsScript vs, uint32_t slot, int value) {
Jason Samsbe36bf32010-05-11 14:03:58 -0700243 Script *s = static_cast<Script *>(vs);
244 s->setVar(slot, &value, sizeof(value));
245}
246
Jason Samsa5eb6e12010-11-16 17:37:02 -0800247void rsi_ScriptSetVarObj(Context *rsc, RsScript vs, uint32_t slot, RsObjectBase value) {
248 Script *s = static_cast<Script *>(vs);
249 ObjectBase *o = static_cast<ObjectBase *>(value);
250 s->setVarObj(slot, o);
251}
252
Tim Murray099bc262013-03-20 16:54:03 -0700253void rsi_ScriptSetVarJ(Context *rsc, RsScript vs, uint32_t slot, int64_t value) {
Stephen Hines0977c942010-10-11 10:54:21 -0700254 Script *s = static_cast<Script *>(vs);
255 s->setVar(slot, &value, sizeof(value));
256}
257
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800258void rsi_ScriptSetVarF(Context *rsc, RsScript vs, uint32_t slot, float value) {
Jason Samsbe36bf32010-05-11 14:03:58 -0700259 Script *s = static_cast<Script *>(vs);
260 s->setVar(slot, &value, sizeof(value));
261}
262
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800263void rsi_ScriptSetVarD(Context *rsc, RsScript vs, uint32_t slot, double value) {
Stephen Hines6d0a0742010-09-20 17:20:30 -0700264 Script *s = static_cast<Script *>(vs);
265 s->setVar(slot, &value, sizeof(value));
266}
267
Alex Sakhartchoukb81a0eb2011-06-03 10:18:01 -0700268void rsi_ScriptSetVarV(Context *rsc, RsScript vs, uint32_t slot, const void *data, size_t len) {
Jason Samsbe36bf32010-05-11 14:03:58 -0700269 Script *s = static_cast<Script *>(vs);
270 s->setVar(slot, data, len);
Jason Samsfa517192009-08-13 12:59:04 -0700271}
272
Tim Murray9c642392013-04-11 13:29:59 -0700273void rsi_ScriptGetVarV(Context *rsc, RsScript vs, uint32_t slot, void *data, size_t len) {
274 Script *s = static_cast<Script *>(vs);
275 s->getVar(slot, data, len);
276}
277
Stephen Hines2980f072012-04-09 18:26:29 -0700278void rsi_ScriptSetVarVE(Context *rsc, RsScript vs, uint32_t slot,
279 const void *data, size_t len, RsElement ve,
Stephen Hinesac8d1462014-06-25 00:01:23 -0700280 const uint32_t *dims, size_t dimLen) {
Stephen Hines2980f072012-04-09 18:26:29 -0700281 Script *s = static_cast<Script *>(vs);
282 Element *e = static_cast<Element *>(ve);
283 s->setVar(slot, data, len, e, dims, dimLen);
284}
285
Jason Sams326e0dd2009-05-22 14:03:28 -0700286}
287}