blob: b89c96e123f36a44c133a7e90ef3a6a615928c0b [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
51 mSlots[slot].set(a);
Stephen Hinesc78839b2013-09-10 17:40:41 -070052 mHasObjectSlots = true;
Jason Sams807fdc42012-07-25 17:55:39 -070053 mRSC->mHal.funcs.script.setGlobalBind(mRSC, this, slot, a);
Jason Sams326e0dd2009-05-22 14:03:28 -070054}
55
Alex Sakhartchoukb81a0eb2011-06-03 10:18:01 -070056void Script::setVar(uint32_t slot, const void *val, size_t len) {
Steve Blockaf12ac62012-01-06 19:20:56 +000057 //ALOGE("setVar %i %p %i", slot, val, len);
Jason Samsbad80742011-03-16 16:29:28 -070058 if (slot >= mHal.info.exportedVariableCount) {
Steve Blockaf12ac62012-01-06 19:20:56 +000059 ALOGE("Script::setVar unable to set allocation, invalid slot index");
Jason Samsbad80742011-03-16 16:29:28 -070060 return;
Jason Samsbe36bf32010-05-11 14:03:58 -070061 }
Jason Samsbad80742011-03-16 16:29:28 -070062 mRSC->mHal.funcs.script.setGlobalVar(mRSC, this, slot, (void *)val, len);
Jason Samsbe36bf32010-05-11 14:03:58 -070063}
64
Tim Murray9c642392013-04-11 13:29:59 -070065void Script::getVar(uint32_t slot, const void *val, size_t len) {
66 //ALOGE("getVar %i %p %i", slot, val, len);
67 if (slot >= mHal.info.exportedVariableCount) {
Stephen Hinescadee382013-12-12 13:21:00 -080068 ALOGE("Script::getVar unable to set allocation, invalid slot index: "
Nick Kralevich02759d72013-12-15 21:04:00 -080069 "%u >= %zu", slot, mHal.info.exportedVariableCount);
Tim Murray9c642392013-04-11 13:29:59 -070070 return;
71 }
72 mRSC->mHal.funcs.script.getGlobalVar(mRSC, this, slot, (void *)val, len);
73}
74
Stephen Hines2980f072012-04-09 18:26:29 -070075void Script::setVar(uint32_t slot, const void *val, size_t len, Element *e,
Stephen Hinesf0a50782014-06-25 13:41:10 -070076 const uint32_t *dims, size_t dimLen) {
Stephen Hines2980f072012-04-09 18:26:29 -070077 if (slot >= mHal.info.exportedVariableCount) {
Stephen Hinescadee382013-12-12 13:21:00 -080078 ALOGE("Script::setVar unable to set allocation, invalid slot index: "
Nick Kralevich02759d72013-12-15 21:04:00 -080079 "%u >= %zu", slot, mHal.info.exportedVariableCount);
Stephen Hines2980f072012-04-09 18:26:29 -070080 return;
81 }
82 mRSC->mHal.funcs.script.setGlobalVarWithElemDims(mRSC, this, slot,
83 (void *)val, len, e, dims, dimLen);
84}
85
Jason Samsa5eb6e12010-11-16 17:37:02 -080086void Script::setVarObj(uint32_t slot, ObjectBase *val) {
Steve Blockaf12ac62012-01-06 19:20:56 +000087 //ALOGE("setVarObj %i %p", slot, val);
Jason Samsbad80742011-03-16 16:29:28 -070088 if (slot >= mHal.info.exportedVariableCount) {
Stephen Hinescadee382013-12-12 13:21:00 -080089 ALOGE("Script::setVarObj unable to set allocation, invalid slot index: "
Nick Kralevich02759d72013-12-15 21:04:00 -080090 "%u >= %zu", slot, mHal.info.exportedVariableCount);
Jason Samsbad80742011-03-16 16:29:28 -070091 return;
Jason Samsa5eb6e12010-11-16 17:37:02 -080092 }
Stephen Hinesc78839b2013-09-10 17:40:41 -070093 mHasObjectSlots = true;
Jason Samsbad80742011-03-16 16:29:28 -070094 mRSC->mHal.funcs.script.setGlobalObj(mRSC, this, slot, val);
Jason Samsa5eb6e12010-11-16 17:37:02 -080095}
96
Jason Samsa36c50a2014-06-17 12:06:06 -070097void Script::callUpdateCacheObject(const Context *rsc, void *dstObj) const {
Chris Wailes44bef6f2014-08-12 13:51:10 -070098 if (rsc->mHal.funcs.script.updateCachedObject != nullptr) {
Jason Samsa36c50a2014-06-17 12:06:06 -070099 rsc->mHal.funcs.script.updateCachedObject(rsc, this, (rs_script *)dstObj);
100 } else {
101 *((const void **)dstObj) = this;
102 }
103}
104
Stephen Hines4ee16ff2011-08-31 17:41:39 -0700105bool Script::freeChildren() {
106 incSysRef();
107 mRSC->mHal.funcs.script.invokeFreeChildren(mRSC, this);
108 return decSysRef();
109}
110
Jason Samsdbe66d62012-09-17 13:54:41 -0700111ScriptKernelID::ScriptKernelID(Context *rsc, Script *s, int slot, int sig)
Yang Nieb9aa672015-01-27 14:32:25 -0800112 : IDBase(rsc, s, slot) {
Jason Samsdbe66d62012-09-17 13:54:41 -0700113 mHasKernelInput = (sig & 1) != 0;
114 mHasKernelOutput = (sig & 2) != 0;
115}
116
Jason Samsdbe66d62012-09-17 13:54:41 -0700117RsA3DClassID ScriptKernelID::getClassId() const {
118 return RS_A3D_CLASS_ID_SCRIPT_KERNEL_ID;
119}
120
Yang Nieb9aa672015-01-27 14:32:25 -0800121ScriptInvokeID::ScriptInvokeID(Context *rsc, Script *s, int slot)
122 : IDBase(rsc, s, slot) {
Jason Samsdbe66d62012-09-17 13:54:41 -0700123}
124
Yang Nieb9aa672015-01-27 14:32:25 -0800125RsA3DClassID ScriptInvokeID::getClassId() const {
126 return RS_A3D_CLASS_ID_SCRIPT_INVOKE_ID;
Jason Samsdbe66d62012-09-17 13:54:41 -0700127}
128
Yang Nieb9aa672015-01-27 14:32:25 -0800129ScriptFieldID::ScriptFieldID(Context *rsc, Script *s, int slot) :
130 IDBase(rsc, s, slot) {
Jason Samsdbe66d62012-09-17 13:54:41 -0700131}
132
133RsA3DClassID ScriptFieldID::getClassId() const {
134 return RS_A3D_CLASS_ID_SCRIPT_FIELD_ID;
135}
136
137
Jason Sams326e0dd2009-05-22 14:03:28 -0700138namespace android {
139namespace renderscript {
140
Jason Samsdbe66d62012-09-17 13:54:41 -0700141RsScriptKernelID rsi_ScriptKernelIDCreate(Context *rsc, RsScript vs, int slot, int sig) {
Stephen Hines61c86952013-04-09 17:34:43 -0700142 ScriptKernelID *kid = new ScriptKernelID(rsc, (Script *)vs, slot, sig);
143 kid->incUserRef();
144 return kid;
Jason Samsdbe66d62012-09-17 13:54:41 -0700145}
146
Yang Nieb9aa672015-01-27 14:32:25 -0800147RsScriptInvokeID rsi_ScriptInvokeIDCreate(Context *rsc, RsScript vs, uint32_t slot) {
148 ScriptInvokeID *iid = new ScriptInvokeID(rsc, (Script *)vs, slot);
149 iid->incUserRef();
150 return iid;
151}
152
Jason Samsdbe66d62012-09-17 13:54:41 -0700153RsScriptFieldID rsi_ScriptFieldIDCreate(Context *rsc, RsScript vs, int slot) {
Stephen Hines61c86952013-04-09 17:34:43 -0700154 ScriptFieldID *fid = new ScriptFieldID(rsc, (Script *)vs, slot);
155 fid->incUserRef();
156 return fid;
Jason Samsdbe66d62012-09-17 13:54:41 -0700157}
158
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800159void rsi_ScriptBindAllocation(Context * rsc, RsScript vs, RsAllocation va, uint32_t slot) {
Jason Sams326e0dd2009-05-22 14:03:28 -0700160 Script *s = static_cast<Script *>(vs);
Jason Samsbe36bf32010-05-11 14:03:58 -0700161 Allocation *a = static_cast<Allocation *>(va);
Alex Sakhartchouk700ba382010-10-08 15:00:05 -0700162 s->setSlot(slot, a);
Jason Sams326e0dd2009-05-22 14:03:28 -0700163}
164
Alex Sakhartchoukb81a0eb2011-06-03 10:18:01 -0700165void rsi_ScriptSetTimeZone(Context * rsc, RsScript vs, const char * timeZone, size_t length) {
Stephen Hinesd2432b92011-11-09 18:02:20 -0800166 // We unfortunately need to make a new copy of the string, since it is
Chris Wailes44bef6f2014-08-12 13:51:10 -0700167 // not nullptr-terminated. We then use setenv(), which properly handles
Stephen Hinesd2432b92011-11-09 18:02:20 -0800168 // freeing/duplicating the actual string for the environment.
169 char *tz = (char *) malloc(length + 1);
170 if (!tz) {
Steve Blockaf12ac62012-01-06 19:20:56 +0000171 ALOGE("Couldn't allocate memory for timezone buffer");
Stephen Hinesd2432b92011-11-09 18:02:20 -0800172 return;
173 }
174 strncpy(tz, timeZone, length);
175 tz[length] = '\0';
176 if (setenv("TZ", tz, 1) == 0) {
177 tzset();
178 } else {
Steve Blockaf12ac62012-01-06 19:20:56 +0000179 ALOGE("Error setting timezone");
Stephen Hinesd2432b92011-11-09 18:02:20 -0800180 }
181 free(tz);
Jason Samsd34b7252009-08-04 16:58:20 -0700182}
183
Chris Wailes4b3c34e2014-06-11 12:00:29 -0700184void rsi_ScriptForEachMulti(Context *rsc, RsScript vs, uint32_t slot,
185 RsAllocation *vains, size_t inLen,
186 RsAllocation vaout, const void *params,
187 size_t paramLen, const RsScriptCall *sc,
188 size_t scLen) {
Chris Wailesf3712132014-07-16 15:18:30 -0700189
190 Script *s = static_cast<Script *>(vs);
Chris Wailes4b3c34e2014-06-11 12:00:29 -0700191 Allocation **ains = (Allocation**)(vains);
192
193 s->runForEach(rsc, slot,
194 const_cast<const Allocation **>(ains), inLen,
195 static_cast<Allocation *>(vaout), params, paramLen, sc);
196
197}
198
Chris Wailesf3712132014-07-16 15:18:30 -0700199void rsi_ScriptForEach(Context *rsc, RsScript vs, uint32_t slot,
200 RsAllocation vain, RsAllocation vaout,
201 const void *params, size_t paramLen,
202 const RsScriptCall *sc, size_t scLen) {
203
Chris Wailes44bef6f2014-08-12 13:51:10 -0700204 if (vain == nullptr) {
205 rsi_ScriptForEachMulti(rsc, vs, slot, nullptr, 0, vaout, params, paramLen,
Chris Wailesf3712132014-07-16 15:18:30 -0700206 sc, scLen);
207 } else {
208 RsAllocation ains[1] = {vain};
209
210 rsi_ScriptForEachMulti(rsc, vs, slot, ains,
211 sizeof(ains) / sizeof(RsAllocation), vaout,
212 params, paramLen, sc, scLen);
213 }
214}
215
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800216void rsi_ScriptInvoke(Context *rsc, RsScript vs, uint32_t slot) {
Jason Sams8c6bc692009-09-16 15:04:38 -0700217 Script *s = static_cast<Script *>(vs);
Chris Wailes44bef6f2014-08-12 13:51:10 -0700218 s->Invoke(rsc, slot, nullptr, 0);
Jason Sams8c6bc692009-09-16 15:04:38 -0700219}
220
221
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800222void rsi_ScriptInvokeData(Context *rsc, RsScript vs, uint32_t slot, void *data) {
Jason Samsbe36bf32010-05-11 14:03:58 -0700223 Script *s = static_cast<Script *>(vs);
Chris Wailes44bef6f2014-08-12 13:51:10 -0700224 s->Invoke(rsc, slot, nullptr, 0);
Jason Samsbe36bf32010-05-11 14:03:58 -0700225}
226
Alex Sakhartchoukb81a0eb2011-06-03 10:18:01 -0700227void rsi_ScriptInvokeV(Context *rsc, RsScript vs, uint32_t slot, const void *data, size_t len) {
Jason Samsbe36bf32010-05-11 14:03:58 -0700228 Script *s = static_cast<Script *>(vs);
Jason Sams22fa3712010-05-19 17:22:57 -0700229 s->Invoke(rsc, slot, data, len);
Jason Samsbe36bf32010-05-11 14:03:58 -0700230}
231
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800232void rsi_ScriptSetVarI(Context *rsc, RsScript vs, uint32_t slot, int value) {
Jason Samsbe36bf32010-05-11 14:03:58 -0700233 Script *s = static_cast<Script *>(vs);
234 s->setVar(slot, &value, sizeof(value));
235}
236
Jason Samsa5eb6e12010-11-16 17:37:02 -0800237void rsi_ScriptSetVarObj(Context *rsc, RsScript vs, uint32_t slot, RsObjectBase value) {
238 Script *s = static_cast<Script *>(vs);
239 ObjectBase *o = static_cast<ObjectBase *>(value);
240 s->setVarObj(slot, o);
241}
242
Tim Murray099bc262013-03-20 16:54:03 -0700243void rsi_ScriptSetVarJ(Context *rsc, RsScript vs, uint32_t slot, int64_t value) {
Stephen Hines0977c942010-10-11 10:54:21 -0700244 Script *s = static_cast<Script *>(vs);
245 s->setVar(slot, &value, sizeof(value));
246}
247
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800248void rsi_ScriptSetVarF(Context *rsc, RsScript vs, uint32_t slot, float value) {
Jason Samsbe36bf32010-05-11 14:03:58 -0700249 Script *s = static_cast<Script *>(vs);
250 s->setVar(slot, &value, sizeof(value));
251}
252
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800253void rsi_ScriptSetVarD(Context *rsc, RsScript vs, uint32_t slot, double value) {
Stephen Hines6d0a0742010-09-20 17:20:30 -0700254 Script *s = static_cast<Script *>(vs);
255 s->setVar(slot, &value, sizeof(value));
256}
257
Alex Sakhartchoukb81a0eb2011-06-03 10:18:01 -0700258void rsi_ScriptSetVarV(Context *rsc, RsScript vs, uint32_t slot, const void *data, size_t len) {
Jason Samsbe36bf32010-05-11 14:03:58 -0700259 Script *s = static_cast<Script *>(vs);
260 s->setVar(slot, data, len);
Jason Samsfa517192009-08-13 12:59:04 -0700261}
262
Tim Murray9c642392013-04-11 13:29:59 -0700263void rsi_ScriptGetVarV(Context *rsc, RsScript vs, uint32_t slot, void *data, size_t len) {
264 Script *s = static_cast<Script *>(vs);
265 s->getVar(slot, data, len);
266}
267
Stephen Hines2980f072012-04-09 18:26:29 -0700268void rsi_ScriptSetVarVE(Context *rsc, RsScript vs, uint32_t slot,
269 const void *data, size_t len, RsElement ve,
Stephen Hinesac8d1462014-06-25 00:01:23 -0700270 const uint32_t *dims, size_t dimLen) {
Stephen Hines2980f072012-04-09 18:26:29 -0700271 Script *s = static_cast<Script *>(vs);
272 Element *e = static_cast<Element *>(ve);
273 s->setVar(slot, data, len, e, dims, dimLen);
274}
275
Jason Sams326e0dd2009-05-22 14:03:28 -0700276}
277}