blob: 835ded1a05691cc36a49260ee22665b8a59ac50f [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
27 mSlots = NULL;
28 mTypes = NULL;
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;
36 mSlots = NULL;
37 }
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -080038 if (mTypes) {
Alex Sakhartchouk700ba382010-10-08 15:00:05 -070039 delete [] mTypes;
40 mTypes = NULL;
41 }
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,
76 const size_t *dims, size_t dimLen) {
77 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;
Steve Blockaf12ac62012-01-06 19:20:56 +000094 //ALOGE("setvarobj %i %p", slot, val);
Jason Samsbad80742011-03-16 16:29:28 -070095 mRSC->mHal.funcs.script.setGlobalObj(mRSC, this, slot, val);
Jason Samsa5eb6e12010-11-16 17:37:02 -080096}
97
Stephen Hines4ee16ff2011-08-31 17:41:39 -070098bool Script::freeChildren() {
99 incSysRef();
100 mRSC->mHal.funcs.script.invokeFreeChildren(mRSC, this);
101 return decSysRef();
102}
103
Jason Samsdbe66d62012-09-17 13:54:41 -0700104ScriptKernelID::ScriptKernelID(Context *rsc, Script *s, int slot, int sig)
105 : ObjectBase(rsc) {
106
107 mScript = s;
108 mSlot = slot;
109 mHasKernelInput = (sig & 1) != 0;
110 mHasKernelOutput = (sig & 2) != 0;
111}
112
113ScriptKernelID::~ScriptKernelID() {
114
115}
116
117void ScriptKernelID::serialize(Context *rsc, OStream *stream) const {
118
119}
120
121RsA3DClassID ScriptKernelID::getClassId() const {
122 return RS_A3D_CLASS_ID_SCRIPT_KERNEL_ID;
123}
124
125ScriptFieldID::ScriptFieldID(Context *rsc, Script *s, int slot) : ObjectBase(rsc) {
126 mScript = s;
127 mSlot = slot;
128}
129
130ScriptFieldID::~ScriptFieldID() {
131
132}
133
134void ScriptFieldID::serialize(Context *rsc, OStream *stream) const {
135
136}
137
138RsA3DClassID ScriptFieldID::getClassId() const {
139 return RS_A3D_CLASS_ID_SCRIPT_FIELD_ID;
140}
141
142
Jason Sams326e0dd2009-05-22 14:03:28 -0700143namespace android {
144namespace renderscript {
145
Jason Samsdbe66d62012-09-17 13:54:41 -0700146RsScriptKernelID rsi_ScriptKernelIDCreate(Context *rsc, RsScript vs, int slot, int sig) {
Stephen Hines61c86952013-04-09 17:34:43 -0700147 ScriptKernelID *kid = new ScriptKernelID(rsc, (Script *)vs, slot, sig);
148 kid->incUserRef();
149 return kid;
Jason Samsdbe66d62012-09-17 13:54:41 -0700150}
151
152RsScriptFieldID rsi_ScriptFieldIDCreate(Context *rsc, RsScript vs, int slot) {
Stephen Hines61c86952013-04-09 17:34:43 -0700153 ScriptFieldID *fid = new ScriptFieldID(rsc, (Script *)vs, slot);
154 fid->incUserRef();
155 return fid;
Jason Samsdbe66d62012-09-17 13:54:41 -0700156}
157
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800158void rsi_ScriptBindAllocation(Context * rsc, RsScript vs, RsAllocation va, uint32_t slot) {
Jason Sams326e0dd2009-05-22 14:03:28 -0700159 Script *s = static_cast<Script *>(vs);
Jason Samsbe36bf32010-05-11 14:03:58 -0700160 Allocation *a = static_cast<Allocation *>(va);
Alex Sakhartchouk700ba382010-10-08 15:00:05 -0700161 s->setSlot(slot, a);
Jason Sams326e0dd2009-05-22 14:03:28 -0700162}
163
Alex Sakhartchoukb81a0eb2011-06-03 10:18:01 -0700164void rsi_ScriptSetTimeZone(Context * rsc, RsScript vs, const char * timeZone, size_t length) {
Stephen Hinesd2432b92011-11-09 18:02:20 -0800165 // We unfortunately need to make a new copy of the string, since it is
166 // not NULL-terminated. We then use setenv(), which properly handles
167 // freeing/duplicating the actual string for the environment.
168 char *tz = (char *) malloc(length + 1);
169 if (!tz) {
Steve Blockaf12ac62012-01-06 19:20:56 +0000170 ALOGE("Couldn't allocate memory for timezone buffer");
Stephen Hinesd2432b92011-11-09 18:02:20 -0800171 return;
172 }
173 strncpy(tz, timeZone, length);
174 tz[length] = '\0';
175 if (setenv("TZ", tz, 1) == 0) {
176 tzset();
177 } else {
Steve Blockaf12ac62012-01-06 19:20:56 +0000178 ALOGE("Error setting timezone");
Stephen Hinesd2432b92011-11-09 18:02:20 -0800179 }
180 free(tz);
Jason Samsd34b7252009-08-04 16:58:20 -0700181}
182
Jason Sams5fb1aeb2011-04-27 15:12:49 -0700183void rsi_ScriptForEach(Context *rsc, RsScript vs, uint32_t slot,
184 RsAllocation vain, RsAllocation vaout,
Tim Murrayd4ecb172013-02-07 12:17:03 -0800185 const void *params, size_t paramLen,
186 const RsScriptCall *sc, size_t scLen) {
Jason Sams5fb1aeb2011-04-27 15:12:49 -0700187 Script *s = static_cast<Script *>(vs);
Stephen Hines5dd9d072013-04-02 00:08:32 -0700188 // The rs.spec generated code does not handle the absence of an actual
189 // input for sc. Instead, it retains an existing pointer value (the prior
190 // field in the packed data object). This can cause confusion because
191 // drivers might now inspect bogus sc data.
192 if (scLen == 0) {
193 sc = NULL;
194 }
Stephen Hines44199772012-02-21 20:13:12 -0800195 s->runForEach(rsc, slot,
Jason Sams5fb1aeb2011-04-27 15:12:49 -0700196 static_cast<const Allocation *>(vain), static_cast<Allocation *>(vaout),
Tim Murrayd4ecb172013-02-07 12:17:03 -0800197 params, paramLen, sc);
Jason Sams5fb1aeb2011-04-27 15:12:49 -0700198
199}
200
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800201void rsi_ScriptInvoke(Context *rsc, RsScript vs, uint32_t slot) {
Jason Sams8c6bc692009-09-16 15:04:38 -0700202 Script *s = static_cast<Script *>(vs);
Jason Sams22fa3712010-05-19 17:22:57 -0700203 s->Invoke(rsc, slot, NULL, 0);
Jason Sams8c6bc692009-09-16 15:04:38 -0700204}
205
206
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800207void rsi_ScriptInvokeData(Context *rsc, RsScript vs, uint32_t slot, void *data) {
Jason Samsbe36bf32010-05-11 14:03:58 -0700208 Script *s = static_cast<Script *>(vs);
Jason Sams22fa3712010-05-19 17:22:57 -0700209 s->Invoke(rsc, slot, NULL, 0);
Jason Samsbe36bf32010-05-11 14:03:58 -0700210}
211
Alex Sakhartchoukb81a0eb2011-06-03 10:18:01 -0700212void rsi_ScriptInvokeV(Context *rsc, RsScript vs, uint32_t slot, const void *data, size_t len) {
Jason Samsbe36bf32010-05-11 14:03:58 -0700213 Script *s = static_cast<Script *>(vs);
Jason Sams22fa3712010-05-19 17:22:57 -0700214 s->Invoke(rsc, slot, data, len);
Jason Samsbe36bf32010-05-11 14:03:58 -0700215}
216
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800217void rsi_ScriptSetVarI(Context *rsc, RsScript vs, uint32_t slot, int value) {
Jason Samsbe36bf32010-05-11 14:03:58 -0700218 Script *s = static_cast<Script *>(vs);
219 s->setVar(slot, &value, sizeof(value));
220}
221
Jason Samsa5eb6e12010-11-16 17:37:02 -0800222void rsi_ScriptSetVarObj(Context *rsc, RsScript vs, uint32_t slot, RsObjectBase value) {
223 Script *s = static_cast<Script *>(vs);
224 ObjectBase *o = static_cast<ObjectBase *>(value);
225 s->setVarObj(slot, o);
226}
227
Tim Murray099bc262013-03-20 16:54:03 -0700228void rsi_ScriptSetVarJ(Context *rsc, RsScript vs, uint32_t slot, int64_t value) {
Stephen Hines0977c942010-10-11 10:54:21 -0700229 Script *s = static_cast<Script *>(vs);
230 s->setVar(slot, &value, sizeof(value));
231}
232
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800233void rsi_ScriptSetVarF(Context *rsc, RsScript vs, uint32_t slot, float value) {
Jason Samsbe36bf32010-05-11 14:03:58 -0700234 Script *s = static_cast<Script *>(vs);
235 s->setVar(slot, &value, sizeof(value));
236}
237
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800238void rsi_ScriptSetVarD(Context *rsc, RsScript vs, uint32_t slot, double value) {
Stephen Hines6d0a0742010-09-20 17:20:30 -0700239 Script *s = static_cast<Script *>(vs);
240 s->setVar(slot, &value, sizeof(value));
241}
242
Alex Sakhartchoukb81a0eb2011-06-03 10:18:01 -0700243void rsi_ScriptSetVarV(Context *rsc, RsScript vs, uint32_t slot, const void *data, size_t len) {
Jason Samsbe36bf32010-05-11 14:03:58 -0700244 Script *s = static_cast<Script *>(vs);
245 s->setVar(slot, data, len);
Jason Samsfa517192009-08-13 12:59:04 -0700246}
247
Tim Murray9c642392013-04-11 13:29:59 -0700248void rsi_ScriptGetVarV(Context *rsc, RsScript vs, uint32_t slot, void *data, size_t len) {
249 Script *s = static_cast<Script *>(vs);
250 s->getVar(slot, data, len);
251}
252
Stephen Hines2980f072012-04-09 18:26:29 -0700253void rsi_ScriptSetVarVE(Context *rsc, RsScript vs, uint32_t slot,
254 const void *data, size_t len, RsElement ve,
255 const size_t *dims, size_t dimLen) {
256 Script *s = static_cast<Script *>(vs);
257 Element *e = static_cast<Element *>(ve);
258 s->setVar(slot, data, len, e, dims, dimLen);
259}
260
Jason Sams326e0dd2009-05-22 14:03:28 -0700261}
262}
263