blob: 3059833f6075fa4fddec42e7becaa41647b6acc9 [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)
112 : ObjectBase(rsc) {
113
114 mScript = s;
115 mSlot = slot;
116 mHasKernelInput = (sig & 1) != 0;
117 mHasKernelOutput = (sig & 2) != 0;
118}
119
120ScriptKernelID::~ScriptKernelID() {
121
122}
123
124void ScriptKernelID::serialize(Context *rsc, OStream *stream) const {
125
126}
127
128RsA3DClassID ScriptKernelID::getClassId() const {
129 return RS_A3D_CLASS_ID_SCRIPT_KERNEL_ID;
130}
131
132ScriptFieldID::ScriptFieldID(Context *rsc, Script *s, int slot) : ObjectBase(rsc) {
133 mScript = s;
134 mSlot = slot;
135}
136
137ScriptFieldID::~ScriptFieldID() {
138
139}
140
141void ScriptFieldID::serialize(Context *rsc, OStream *stream) const {
142
143}
144
145RsA3DClassID ScriptFieldID::getClassId() const {
146 return RS_A3D_CLASS_ID_SCRIPT_FIELD_ID;
147}
148
149
Jason Sams326e0dd2009-05-22 14:03:28 -0700150namespace android {
151namespace renderscript {
152
Jason Samsdbe66d62012-09-17 13:54:41 -0700153RsScriptKernelID rsi_ScriptKernelIDCreate(Context *rsc, RsScript vs, int slot, int sig) {
Stephen Hines61c86952013-04-09 17:34:43 -0700154 ScriptKernelID *kid = new ScriptKernelID(rsc, (Script *)vs, slot, sig);
155 kid->incUserRef();
156 return kid;
Jason Samsdbe66d62012-09-17 13:54:41 -0700157}
158
159RsScriptFieldID rsi_ScriptFieldIDCreate(Context *rsc, RsScript vs, int slot) {
Stephen Hines61c86952013-04-09 17:34:43 -0700160 ScriptFieldID *fid = new ScriptFieldID(rsc, (Script *)vs, slot);
161 fid->incUserRef();
162 return fid;
Jason Samsdbe66d62012-09-17 13:54:41 -0700163}
164
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800165void rsi_ScriptBindAllocation(Context * rsc, RsScript vs, RsAllocation va, uint32_t slot) {
Jason Sams326e0dd2009-05-22 14:03:28 -0700166 Script *s = static_cast<Script *>(vs);
Jason Samsbe36bf32010-05-11 14:03:58 -0700167 Allocation *a = static_cast<Allocation *>(va);
Alex Sakhartchouk700ba382010-10-08 15:00:05 -0700168 s->setSlot(slot, a);
Jason Sams326e0dd2009-05-22 14:03:28 -0700169}
170
Alex Sakhartchoukb81a0eb2011-06-03 10:18:01 -0700171void rsi_ScriptSetTimeZone(Context * rsc, RsScript vs, const char * timeZone, size_t length) {
Stephen Hinesd2432b92011-11-09 18:02:20 -0800172 // We unfortunately need to make a new copy of the string, since it is
Chris Wailes44bef6f2014-08-12 13:51:10 -0700173 // not nullptr-terminated. We then use setenv(), which properly handles
Stephen Hinesd2432b92011-11-09 18:02:20 -0800174 // freeing/duplicating the actual string for the environment.
175 char *tz = (char *) malloc(length + 1);
176 if (!tz) {
Steve Blockaf12ac62012-01-06 19:20:56 +0000177 ALOGE("Couldn't allocate memory for timezone buffer");
Stephen Hinesd2432b92011-11-09 18:02:20 -0800178 return;
179 }
180 strncpy(tz, timeZone, length);
181 tz[length] = '\0';
182 if (setenv("TZ", tz, 1) == 0) {
183 tzset();
184 } else {
Steve Blockaf12ac62012-01-06 19:20:56 +0000185 ALOGE("Error setting timezone");
Stephen Hinesd2432b92011-11-09 18:02:20 -0800186 }
187 free(tz);
Jason Samsd34b7252009-08-04 16:58:20 -0700188}
189
Chris Wailes4b3c34e2014-06-11 12:00:29 -0700190void rsi_ScriptForEachMulti(Context *rsc, RsScript vs, uint32_t slot,
191 RsAllocation *vains, size_t inLen,
192 RsAllocation vaout, const void *params,
193 size_t paramLen, const RsScriptCall *sc,
194 size_t scLen) {
Chris Wailesf3712132014-07-16 15:18:30 -0700195
196 Script *s = static_cast<Script *>(vs);
Chris Wailes4b3c34e2014-06-11 12:00:29 -0700197 Allocation **ains = (Allocation**)(vains);
198
199 s->runForEach(rsc, slot,
200 const_cast<const Allocation **>(ains), inLen,
201 static_cast<Allocation *>(vaout), params, paramLen, sc);
202
203}
204
Chris Wailesf3712132014-07-16 15:18:30 -0700205void rsi_ScriptForEach(Context *rsc, RsScript vs, uint32_t slot,
206 RsAllocation vain, RsAllocation vaout,
207 const void *params, size_t paramLen,
208 const RsScriptCall *sc, size_t scLen) {
209
Chris Wailes44bef6f2014-08-12 13:51:10 -0700210 if (vain == nullptr) {
211 rsi_ScriptForEachMulti(rsc, vs, slot, nullptr, 0, vaout, params, paramLen,
Chris Wailesf3712132014-07-16 15:18:30 -0700212 sc, scLen);
213 } else {
214 RsAllocation ains[1] = {vain};
215
216 rsi_ScriptForEachMulti(rsc, vs, slot, ains,
217 sizeof(ains) / sizeof(RsAllocation), vaout,
218 params, paramLen, sc, scLen);
219 }
220}
221
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800222void rsi_ScriptInvoke(Context *rsc, RsScript vs, uint32_t slot) {
Jason Sams8c6bc692009-09-16 15:04:38 -0700223 Script *s = static_cast<Script *>(vs);
Chris Wailes44bef6f2014-08-12 13:51:10 -0700224 s->Invoke(rsc, slot, nullptr, 0);
Jason Sams8c6bc692009-09-16 15:04:38 -0700225}
226
227
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800228void rsi_ScriptInvokeData(Context *rsc, RsScript vs, uint32_t slot, void *data) {
Jason Samsbe36bf32010-05-11 14:03:58 -0700229 Script *s = static_cast<Script *>(vs);
Chris Wailes44bef6f2014-08-12 13:51:10 -0700230 s->Invoke(rsc, slot, nullptr, 0);
Jason Samsbe36bf32010-05-11 14:03:58 -0700231}
232
Alex Sakhartchoukb81a0eb2011-06-03 10:18:01 -0700233void rsi_ScriptInvokeV(Context *rsc, RsScript vs, uint32_t slot, const void *data, size_t len) {
Jason Samsbe36bf32010-05-11 14:03:58 -0700234 Script *s = static_cast<Script *>(vs);
Jason Sams22fa3712010-05-19 17:22:57 -0700235 s->Invoke(rsc, slot, data, len);
Jason Samsbe36bf32010-05-11 14:03:58 -0700236}
237
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800238void rsi_ScriptSetVarI(Context *rsc, RsScript vs, uint32_t slot, int value) {
Jason Samsbe36bf32010-05-11 14:03:58 -0700239 Script *s = static_cast<Script *>(vs);
240 s->setVar(slot, &value, sizeof(value));
241}
242
Jason Samsa5eb6e12010-11-16 17:37:02 -0800243void rsi_ScriptSetVarObj(Context *rsc, RsScript vs, uint32_t slot, RsObjectBase value) {
244 Script *s = static_cast<Script *>(vs);
245 ObjectBase *o = static_cast<ObjectBase *>(value);
246 s->setVarObj(slot, o);
247}
248
Tim Murray099bc262013-03-20 16:54:03 -0700249void rsi_ScriptSetVarJ(Context *rsc, RsScript vs, uint32_t slot, int64_t value) {
Stephen Hines0977c942010-10-11 10:54:21 -0700250 Script *s = static_cast<Script *>(vs);
251 s->setVar(slot, &value, sizeof(value));
252}
253
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800254void rsi_ScriptSetVarF(Context *rsc, RsScript vs, uint32_t slot, float value) {
Jason Samsbe36bf32010-05-11 14:03:58 -0700255 Script *s = static_cast<Script *>(vs);
256 s->setVar(slot, &value, sizeof(value));
257}
258
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800259void rsi_ScriptSetVarD(Context *rsc, RsScript vs, uint32_t slot, double value) {
Stephen Hines6d0a0742010-09-20 17:20:30 -0700260 Script *s = static_cast<Script *>(vs);
261 s->setVar(slot, &value, sizeof(value));
262}
263
Alex Sakhartchoukb81a0eb2011-06-03 10:18:01 -0700264void rsi_ScriptSetVarV(Context *rsc, RsScript vs, uint32_t slot, const void *data, size_t len) {
Jason Samsbe36bf32010-05-11 14:03:58 -0700265 Script *s = static_cast<Script *>(vs);
266 s->setVar(slot, data, len);
Jason Samsfa517192009-08-13 12:59:04 -0700267}
268
Tim Murray9c642392013-04-11 13:29:59 -0700269void rsi_ScriptGetVarV(Context *rsc, RsScript vs, uint32_t slot, void *data, size_t len) {
270 Script *s = static_cast<Script *>(vs);
271 s->getVar(slot, data, len);
272}
273
Stephen Hines2980f072012-04-09 18:26:29 -0700274void rsi_ScriptSetVarVE(Context *rsc, RsScript vs, uint32_t slot,
275 const void *data, size_t len, RsElement ve,
Stephen Hinesac8d1462014-06-25 00:01:23 -0700276 const uint32_t *dims, size_t dimLen) {
Stephen Hines2980f072012-04-09 18:26:29 -0700277 Script *s = static_cast<Script *>(vs);
278 Element *e = static_cast<Element *>(ve);
279 s->setVar(slot, data, len, e, dims, dimLen);
280}
281
Jason Sams326e0dd2009-05-22 14:03:28 -0700282}
283}