blob: 8dae4190d3e5ed1ebc38ce448fc13292d694279d [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
Chih-Hung Hsieh11496ac2016-11-15 15:14:05 -080020namespace android {
21namespace renderscript {
Jason Sams326e0dd2009-05-22 14:03:28 -070022
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;
Stephen Hines5d95a782015-04-13 20:11:48 -070031 mApiLevel = 0;
Jason Sams326e0dd2009-05-22 14:03:28 -070032}
33
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -080034Script::~Script() {
35 if (mSlots) {
Alex Sakhartchouk700ba382010-10-08 15:00:05 -070036 delete [] mSlots;
Chris Wailes44bef6f2014-08-12 13:51:10 -070037 mSlots = nullptr;
Alex Sakhartchouk700ba382010-10-08 15:00:05 -070038 }
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -080039 if (mTypes) {
Alex Sakhartchouk700ba382010-10-08 15:00:05 -070040 delete [] mTypes;
Chris Wailes44bef6f2014-08-12 13:51:10 -070041 mTypes = nullptr;
Alex Sakhartchouk700ba382010-10-08 15:00:05 -070042 }
43}
44
Alex Sakhartchouk700ba382010-10-08 15:00:05 -070045void Script::setSlot(uint32_t slot, Allocation *a) {
Steve Blockaf12ac62012-01-06 19:20:56 +000046 //ALOGE("setSlot %i %p", slot, a);
Jason Samsbad80742011-03-16 16:29:28 -070047 if (slot >= mHal.info.exportedVariableCount) {
Steve Blockaf12ac62012-01-06 19:20:56 +000048 ALOGE("Script::setSlot unable to set allocation, invalid slot index");
Alex Sakhartchouk700ba382010-10-08 15:00:05 -070049 return;
50 }
51
Jason Sams4efe3d32015-03-18 18:28:06 -070052 if (mRSC->hadFatalError()) return;
53
Alex Sakhartchouk700ba382010-10-08 15:00:05 -070054 mSlots[slot].set(a);
Stephen Hinesc78839b2013-09-10 17:40:41 -070055 mHasObjectSlots = true;
Jason Sams807fdc42012-07-25 17:55:39 -070056 mRSC->mHal.funcs.script.setGlobalBind(mRSC, this, slot, a);
Jason Sams326e0dd2009-05-22 14:03:28 -070057}
58
Alex Sakhartchoukb81a0eb2011-06-03 10:18:01 -070059void Script::setVar(uint32_t slot, const void *val, size_t len) {
Steve Blockaf12ac62012-01-06 19:20:56 +000060 //ALOGE("setVar %i %p %i", slot, val, len);
Jason Samsbad80742011-03-16 16:29:28 -070061 if (slot >= mHal.info.exportedVariableCount) {
Steve Blockaf12ac62012-01-06 19:20:56 +000062 ALOGE("Script::setVar unable to set allocation, invalid slot index");
Jason Samsbad80742011-03-16 16:29:28 -070063 return;
Jason Samsbe36bf32010-05-11 14:03:58 -070064 }
Jason Sams4efe3d32015-03-18 18:28:06 -070065 if (mRSC->hadFatalError()) return;
66
Jason Samsbad80742011-03-16 16:29:28 -070067 mRSC->mHal.funcs.script.setGlobalVar(mRSC, this, slot, (void *)val, len);
Jason Samsbe36bf32010-05-11 14:03:58 -070068}
69
Tim Murray9c642392013-04-11 13:29:59 -070070void Script::getVar(uint32_t slot, const void *val, size_t len) {
71 //ALOGE("getVar %i %p %i", slot, val, len);
72 if (slot >= mHal.info.exportedVariableCount) {
Stephen Hinescadee382013-12-12 13:21:00 -080073 ALOGE("Script::getVar unable to set allocation, invalid slot index: "
Nick Kralevich02759d72013-12-15 21:04:00 -080074 "%u >= %zu", slot, mHal.info.exportedVariableCount);
Tim Murray9c642392013-04-11 13:29:59 -070075 return;
76 }
Jason Sams4efe3d32015-03-18 18:28:06 -070077 if (mRSC->hadFatalError()) return;
78
Tim Murray9c642392013-04-11 13:29:59 -070079 mRSC->mHal.funcs.script.getGlobalVar(mRSC, this, slot, (void *)val, len);
80}
81
Stephen Hines2980f072012-04-09 18:26:29 -070082void Script::setVar(uint32_t slot, const void *val, size_t len, Element *e,
Stephen Hinesf0a50782014-06-25 13:41:10 -070083 const uint32_t *dims, size_t dimLen) {
Stephen Hines2980f072012-04-09 18:26:29 -070084 if (slot >= mHal.info.exportedVariableCount) {
Stephen Hinescadee382013-12-12 13:21:00 -080085 ALOGE("Script::setVar unable to set allocation, invalid slot index: "
Nick Kralevich02759d72013-12-15 21:04:00 -080086 "%u >= %zu", slot, mHal.info.exportedVariableCount);
Stephen Hines2980f072012-04-09 18:26:29 -070087 return;
88 }
Jason Sams4efe3d32015-03-18 18:28:06 -070089 if (mRSC->hadFatalError()) return;
90
Stephen Hines2980f072012-04-09 18:26:29 -070091 mRSC->mHal.funcs.script.setGlobalVarWithElemDims(mRSC, this, slot,
92 (void *)val, len, e, dims, dimLen);
93}
94
Jason Samsa5eb6e12010-11-16 17:37:02 -080095void Script::setVarObj(uint32_t slot, ObjectBase *val) {
Steve Blockaf12ac62012-01-06 19:20:56 +000096 //ALOGE("setVarObj %i %p", slot, val);
Jason Samsbad80742011-03-16 16:29:28 -070097 if (slot >= mHal.info.exportedVariableCount) {
Stephen Hinescadee382013-12-12 13:21:00 -080098 ALOGE("Script::setVarObj unable to set allocation, invalid slot index: "
Nick Kralevich02759d72013-12-15 21:04:00 -080099 "%u >= %zu", slot, mHal.info.exportedVariableCount);
Jason Samsbad80742011-03-16 16:29:28 -0700100 return;
Jason Samsa5eb6e12010-11-16 17:37:02 -0800101 }
Jason Sams4efe3d32015-03-18 18:28:06 -0700102 if (mRSC->hadFatalError()) return;
103
Stephen Hinesc78839b2013-09-10 17:40:41 -0700104 mHasObjectSlots = true;
Jason Samsbad80742011-03-16 16:29:28 -0700105 mRSC->mHal.funcs.script.setGlobalObj(mRSC, this, slot, val);
Jason Samsa5eb6e12010-11-16 17:37:02 -0800106}
107
Jason Samsa36c50a2014-06-17 12:06:06 -0700108void Script::callUpdateCacheObject(const Context *rsc, void *dstObj) const {
Chris Wailes44bef6f2014-08-12 13:51:10 -0700109 if (rsc->mHal.funcs.script.updateCachedObject != nullptr) {
Jason Samsa36c50a2014-06-17 12:06:06 -0700110 rsc->mHal.funcs.script.updateCachedObject(rsc, this, (rs_script *)dstObj);
111 } else {
112 *((const void **)dstObj) = this;
113 }
114}
115
Stephen Hines4ee16ff2011-08-31 17:41:39 -0700116bool Script::freeChildren() {
117 incSysRef();
118 mRSC->mHal.funcs.script.invokeFreeChildren(mRSC, this);
119 return decSysRef();
120}
121
Jason Samsdbe66d62012-09-17 13:54:41 -0700122ScriptKernelID::ScriptKernelID(Context *rsc, Script *s, int slot, int sig)
Yang Nieb9aa672015-01-27 14:32:25 -0800123 : IDBase(rsc, s, slot) {
Jason Samsdbe66d62012-09-17 13:54:41 -0700124 mHasKernelInput = (sig & 1) != 0;
125 mHasKernelOutput = (sig & 2) != 0;
126}
127
Jason Samsdbe66d62012-09-17 13:54:41 -0700128RsA3DClassID ScriptKernelID::getClassId() const {
129 return RS_A3D_CLASS_ID_SCRIPT_KERNEL_ID;
130}
131
Yang Nieb9aa672015-01-27 14:32:25 -0800132ScriptInvokeID::ScriptInvokeID(Context *rsc, Script *s, int slot)
133 : IDBase(rsc, s, slot) {
Jason Samsdbe66d62012-09-17 13:54:41 -0700134}
135
Yang Nieb9aa672015-01-27 14:32:25 -0800136RsA3DClassID ScriptInvokeID::getClassId() const {
137 return RS_A3D_CLASS_ID_SCRIPT_INVOKE_ID;
Jason Samsdbe66d62012-09-17 13:54:41 -0700138}
139
Yang Nieb9aa672015-01-27 14:32:25 -0800140ScriptFieldID::ScriptFieldID(Context *rsc, Script *s, int slot) :
141 IDBase(rsc, s, slot) {
Jason Samsdbe66d62012-09-17 13:54:41 -0700142}
143
144RsA3DClassID ScriptFieldID::getClassId() const {
145 return RS_A3D_CLASS_ID_SCRIPT_FIELD_ID;
146}
147
148
Jason Samsdbe66d62012-09-17 13:54:41 -0700149RsScriptKernelID rsi_ScriptKernelIDCreate(Context *rsc, RsScript vs, int slot, int sig) {
Stephen Hines61c86952013-04-09 17:34:43 -0700150 ScriptKernelID *kid = new ScriptKernelID(rsc, (Script *)vs, slot, sig);
151 kid->incUserRef();
152 return kid;
Jason Samsdbe66d62012-09-17 13:54:41 -0700153}
154
Yang Nieb9aa672015-01-27 14:32:25 -0800155RsScriptInvokeID rsi_ScriptInvokeIDCreate(Context *rsc, RsScript vs, uint32_t slot) {
156 ScriptInvokeID *iid = new ScriptInvokeID(rsc, (Script *)vs, slot);
157 iid->incUserRef();
158 return iid;
159}
160
Jason Samsdbe66d62012-09-17 13:54:41 -0700161RsScriptFieldID rsi_ScriptFieldIDCreate(Context *rsc, RsScript vs, int slot) {
Stephen Hines61c86952013-04-09 17:34:43 -0700162 ScriptFieldID *fid = new ScriptFieldID(rsc, (Script *)vs, slot);
163 fid->incUserRef();
164 return fid;
Jason Samsdbe66d62012-09-17 13:54:41 -0700165}
166
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800167void rsi_ScriptBindAllocation(Context * rsc, RsScript vs, RsAllocation va, uint32_t slot) {
Jason Sams326e0dd2009-05-22 14:03:28 -0700168 Script *s = static_cast<Script *>(vs);
Jason Samsbe36bf32010-05-11 14:03:58 -0700169 Allocation *a = static_cast<Allocation *>(va);
Alex Sakhartchouk700ba382010-10-08 15:00:05 -0700170 s->setSlot(slot, a);
Jason Sams326e0dd2009-05-22 14:03:28 -0700171}
172
Alex Sakhartchoukb81a0eb2011-06-03 10:18:01 -0700173void rsi_ScriptSetTimeZone(Context * rsc, RsScript vs, const char * timeZone, size_t length) {
Stephen Hinesd2432b92011-11-09 18:02:20 -0800174 // We unfortunately need to make a new copy of the string, since it is
Chris Wailes44bef6f2014-08-12 13:51:10 -0700175 // not nullptr-terminated. We then use setenv(), which properly handles
Stephen Hinesd2432b92011-11-09 18:02:20 -0800176 // freeing/duplicating the actual string for the environment.
177 char *tz = (char *) malloc(length + 1);
178 if (!tz) {
Steve Blockaf12ac62012-01-06 19:20:56 +0000179 ALOGE("Couldn't allocate memory for timezone buffer");
Stephen Hinesd2432b92011-11-09 18:02:20 -0800180 return;
181 }
182 strncpy(tz, timeZone, length);
183 tz[length] = '\0';
184 if (setenv("TZ", tz, 1) == 0) {
185 tzset();
186 } else {
Steve Blockaf12ac62012-01-06 19:20:56 +0000187 ALOGE("Error setting timezone");
Stephen Hinesd2432b92011-11-09 18:02:20 -0800188 }
189 free(tz);
Jason Samsd34b7252009-08-04 16:58:20 -0700190}
191
Chris Wailes4b3c34e2014-06-11 12:00:29 -0700192void rsi_ScriptForEachMulti(Context *rsc, RsScript vs, uint32_t slot,
193 RsAllocation *vains, size_t inLen,
194 RsAllocation vaout, const void *params,
195 size_t paramLen, const RsScriptCall *sc,
196 size_t scLen) {
Chris Wailesf3712132014-07-16 15:18:30 -0700197
198 Script *s = static_cast<Script *>(vs);
Chris Wailes4b3c34e2014-06-11 12:00:29 -0700199 Allocation **ains = (Allocation**)(vains);
200
201 s->runForEach(rsc, slot,
202 const_cast<const Allocation **>(ains), inLen,
203 static_cast<Allocation *>(vaout), params, paramLen, sc);
204
205}
206
Chris Wailesf3712132014-07-16 15:18:30 -0700207void rsi_ScriptForEach(Context *rsc, RsScript vs, uint32_t slot,
208 RsAllocation vain, RsAllocation vaout,
209 const void *params, size_t paramLen,
210 const RsScriptCall *sc, size_t scLen) {
211
Chris Wailes44bef6f2014-08-12 13:51:10 -0700212 if (vain == nullptr) {
213 rsi_ScriptForEachMulti(rsc, vs, slot, nullptr, 0, vaout, params, paramLen,
Chris Wailesf3712132014-07-16 15:18:30 -0700214 sc, scLen);
215 } else {
216 RsAllocation ains[1] = {vain};
217
218 rsi_ScriptForEachMulti(rsc, vs, slot, ains,
219 sizeof(ains) / sizeof(RsAllocation), vaout,
220 params, paramLen, sc, scLen);
221 }
222}
223
Matt Wala39a9d9b2015-07-20 15:38:17 -0700224void rsi_ScriptReduce(Context *rsc, RsScript vs, uint32_t slot,
David Grossae2ec3f2016-06-01 14:45:47 -0700225 RsAllocation *vains, size_t inLen,
226 RsAllocation vaout, const RsScriptCall *sc,
227 size_t scLen) {
David Gross6c1876b2016-01-15 11:52:14 -0800228 Script *s = static_cast<Script *>(vs);
229 Allocation **ains = (Allocation**)(vains);
230
David Grossae2ec3f2016-06-01 14:45:47 -0700231 s->runReduce(rsc, slot,
232 const_cast<const Allocation **>(ains), inLen,
233 static_cast<Allocation *>(vaout), sc);
David Gross6c1876b2016-01-15 11:52:14 -0800234}
235
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800236void rsi_ScriptInvoke(Context *rsc, RsScript vs, uint32_t slot) {
Jason Sams8c6bc692009-09-16 15:04:38 -0700237 Script *s = static_cast<Script *>(vs);
Chris Wailes44bef6f2014-08-12 13:51:10 -0700238 s->Invoke(rsc, slot, nullptr, 0);
Jason Sams8c6bc692009-09-16 15:04:38 -0700239}
240
241
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800242void rsi_ScriptInvokeData(Context *rsc, RsScript vs, uint32_t slot, void *data) {
Jason Samsbe36bf32010-05-11 14:03:58 -0700243 Script *s = static_cast<Script *>(vs);
Chris Wailes44bef6f2014-08-12 13:51:10 -0700244 s->Invoke(rsc, slot, nullptr, 0);
Jason Samsbe36bf32010-05-11 14:03:58 -0700245}
246
Alex Sakhartchoukb81a0eb2011-06-03 10:18:01 -0700247void rsi_ScriptInvokeV(Context *rsc, RsScript vs, uint32_t slot, const void *data, size_t len) {
Jason Samsbe36bf32010-05-11 14:03:58 -0700248 Script *s = static_cast<Script *>(vs);
Jason Sams22fa3712010-05-19 17:22:57 -0700249 s->Invoke(rsc, slot, data, len);
Jason Samsbe36bf32010-05-11 14:03:58 -0700250}
251
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800252void rsi_ScriptSetVarI(Context *rsc, RsScript vs, uint32_t slot, int value) {
Jason Samsbe36bf32010-05-11 14:03:58 -0700253 Script *s = static_cast<Script *>(vs);
254 s->setVar(slot, &value, sizeof(value));
255}
256
Jason Samsa5eb6e12010-11-16 17:37:02 -0800257void rsi_ScriptSetVarObj(Context *rsc, RsScript vs, uint32_t slot, RsObjectBase value) {
258 Script *s = static_cast<Script *>(vs);
259 ObjectBase *o = static_cast<ObjectBase *>(value);
260 s->setVarObj(slot, o);
261}
262
Tim Murray099bc262013-03-20 16:54:03 -0700263void rsi_ScriptSetVarJ(Context *rsc, RsScript vs, uint32_t slot, int64_t value) {
Stephen Hines0977c942010-10-11 10:54:21 -0700264 Script *s = static_cast<Script *>(vs);
265 s->setVar(slot, &value, sizeof(value));
266}
267
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800268void rsi_ScriptSetVarF(Context *rsc, RsScript vs, uint32_t slot, float value) {
Jason Samsbe36bf32010-05-11 14:03:58 -0700269 Script *s = static_cast<Script *>(vs);
270 s->setVar(slot, &value, sizeof(value));
271}
272
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800273void rsi_ScriptSetVarD(Context *rsc, RsScript vs, uint32_t slot, double value) {
Stephen Hines6d0a0742010-09-20 17:20:30 -0700274 Script *s = static_cast<Script *>(vs);
275 s->setVar(slot, &value, sizeof(value));
276}
277
Alex Sakhartchoukb81a0eb2011-06-03 10:18:01 -0700278void rsi_ScriptSetVarV(Context *rsc, RsScript vs, uint32_t slot, const void *data, size_t len) {
Jason Samsbe36bf32010-05-11 14:03:58 -0700279 Script *s = static_cast<Script *>(vs);
280 s->setVar(slot, data, len);
Jason Samsfa517192009-08-13 12:59:04 -0700281}
282
Tim Murray9c642392013-04-11 13:29:59 -0700283void rsi_ScriptGetVarV(Context *rsc, RsScript vs, uint32_t slot, void *data, size_t len) {
284 Script *s = static_cast<Script *>(vs);
285 s->getVar(slot, data, len);
286}
287
Stephen Hines2980f072012-04-09 18:26:29 -0700288void rsi_ScriptSetVarVE(Context *rsc, RsScript vs, uint32_t slot,
289 const void *data, size_t len, RsElement ve,
Stephen Hinesac8d1462014-06-25 00:01:23 -0700290 const uint32_t *dims, size_t dimLen) {
Stephen Hines2980f072012-04-09 18:26:29 -0700291 Script *s = static_cast<Script *>(vs);
292 Element *e = static_cast<Element *>(ve);
293 s->setVar(slot, data, len, e, dims, dimLen);
294}
295
Chih-Hung Hsieh11496ac2016-11-15 15:14:05 -0800296} // namespace renderscript
297} // namespace android