blob: 7857667cd0d2d64c4c9148bc8903b730d06df932 [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) {
68 ALOGE("Script::getVar unable to set allocation, invalid slot index");
69 return;
70 }
71 mRSC->mHal.funcs.script.getGlobalVar(mRSC, this, slot, (void *)val, len);
72}
73
Stephen Hines2980f072012-04-09 18:26:29 -070074void Script::setVar(uint32_t slot, const void *val, size_t len, Element *e,
75 const size_t *dims, size_t dimLen) {
76 if (slot >= mHal.info.exportedVariableCount) {
77 ALOGE("Script::setVar unable to set allocation, invalid slot index");
78 return;
79 }
80 mRSC->mHal.funcs.script.setGlobalVarWithElemDims(mRSC, this, slot,
81 (void *)val, len, e, dims, dimLen);
82}
83
Jason Samsa5eb6e12010-11-16 17:37:02 -080084void Script::setVarObj(uint32_t slot, ObjectBase *val) {
Steve Blockaf12ac62012-01-06 19:20:56 +000085 //ALOGE("setVarObj %i %p", slot, val);
Jason Samsbad80742011-03-16 16:29:28 -070086 if (slot >= mHal.info.exportedVariableCount) {
Steve Blockaf12ac62012-01-06 19:20:56 +000087 ALOGE("Script::setVarObj unable to set allocation, invalid slot index");
Jason Samsbad80742011-03-16 16:29:28 -070088 return;
Jason Samsa5eb6e12010-11-16 17:37:02 -080089 }
Stephen Hinesc78839b2013-09-10 17:40:41 -070090 mHasObjectSlots = true;
Steve Blockaf12ac62012-01-06 19:20:56 +000091 //ALOGE("setvarobj %i %p", slot, val);
Jason Samsbad80742011-03-16 16:29:28 -070092 mRSC->mHal.funcs.script.setGlobalObj(mRSC, this, slot, val);
Jason Samsa5eb6e12010-11-16 17:37:02 -080093}
94
Stephen Hines4ee16ff2011-08-31 17:41:39 -070095bool Script::freeChildren() {
96 incSysRef();
97 mRSC->mHal.funcs.script.invokeFreeChildren(mRSC, this);
98 return decSysRef();
99}
100
Jason Samsdbe66d62012-09-17 13:54:41 -0700101ScriptKernelID::ScriptKernelID(Context *rsc, Script *s, int slot, int sig)
102 : ObjectBase(rsc) {
103
104 mScript = s;
105 mSlot = slot;
106 mHasKernelInput = (sig & 1) != 0;
107 mHasKernelOutput = (sig & 2) != 0;
108}
109
110ScriptKernelID::~ScriptKernelID() {
111
112}
113
114void ScriptKernelID::serialize(Context *rsc, OStream *stream) const {
115
116}
117
118RsA3DClassID ScriptKernelID::getClassId() const {
119 return RS_A3D_CLASS_ID_SCRIPT_KERNEL_ID;
120}
121
122ScriptFieldID::ScriptFieldID(Context *rsc, Script *s, int slot) : ObjectBase(rsc) {
123 mScript = s;
124 mSlot = slot;
125}
126
127ScriptFieldID::~ScriptFieldID() {
128
129}
130
131void ScriptFieldID::serialize(Context *rsc, OStream *stream) const {
132
133}
134
135RsA3DClassID ScriptFieldID::getClassId() const {
136 return RS_A3D_CLASS_ID_SCRIPT_FIELD_ID;
137}
138
139
Jason Sams326e0dd2009-05-22 14:03:28 -0700140namespace android {
141namespace renderscript {
142
Jason Samsdbe66d62012-09-17 13:54:41 -0700143RsScriptKernelID rsi_ScriptKernelIDCreate(Context *rsc, RsScript vs, int slot, int sig) {
Stephen Hines61c86952013-04-09 17:34:43 -0700144 ScriptKernelID *kid = new ScriptKernelID(rsc, (Script *)vs, slot, sig);
145 kid->incUserRef();
146 return kid;
Jason Samsdbe66d62012-09-17 13:54:41 -0700147}
148
149RsScriptFieldID rsi_ScriptFieldIDCreate(Context *rsc, RsScript vs, int slot) {
Stephen Hines61c86952013-04-09 17:34:43 -0700150 ScriptFieldID *fid = new ScriptFieldID(rsc, (Script *)vs, slot);
151 fid->incUserRef();
152 return fid;
Jason Samsdbe66d62012-09-17 13:54:41 -0700153}
154
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800155void rsi_ScriptBindAllocation(Context * rsc, RsScript vs, RsAllocation va, uint32_t slot) {
Jason Sams326e0dd2009-05-22 14:03:28 -0700156 Script *s = static_cast<Script *>(vs);
Jason Samsbe36bf32010-05-11 14:03:58 -0700157 Allocation *a = static_cast<Allocation *>(va);
Alex Sakhartchouk700ba382010-10-08 15:00:05 -0700158 s->setSlot(slot, a);
Jason Sams326e0dd2009-05-22 14:03:28 -0700159}
160
Alex Sakhartchoukb81a0eb2011-06-03 10:18:01 -0700161void rsi_ScriptSetTimeZone(Context * rsc, RsScript vs, const char * timeZone, size_t length) {
Stephen Hinesd2432b92011-11-09 18:02:20 -0800162 // We unfortunately need to make a new copy of the string, since it is
163 // not NULL-terminated. We then use setenv(), which properly handles
164 // freeing/duplicating the actual string for the environment.
165 char *tz = (char *) malloc(length + 1);
166 if (!tz) {
Steve Blockaf12ac62012-01-06 19:20:56 +0000167 ALOGE("Couldn't allocate memory for timezone buffer");
Stephen Hinesd2432b92011-11-09 18:02:20 -0800168 return;
169 }
170 strncpy(tz, timeZone, length);
171 tz[length] = '\0';
172 if (setenv("TZ", tz, 1) == 0) {
173 tzset();
174 } else {
Steve Blockaf12ac62012-01-06 19:20:56 +0000175 ALOGE("Error setting timezone");
Stephen Hinesd2432b92011-11-09 18:02:20 -0800176 }
177 free(tz);
Jason Samsd34b7252009-08-04 16:58:20 -0700178}
179
Jason Sams5fb1aeb2011-04-27 15:12:49 -0700180void rsi_ScriptForEach(Context *rsc, RsScript vs, uint32_t slot,
181 RsAllocation vain, RsAllocation vaout,
Tim Murrayd4ecb172013-02-07 12:17:03 -0800182 const void *params, size_t paramLen,
183 const RsScriptCall *sc, size_t scLen) {
Jason Sams5fb1aeb2011-04-27 15:12:49 -0700184 Script *s = static_cast<Script *>(vs);
Stephen Hines5dd9d072013-04-02 00:08:32 -0700185 // The rs.spec generated code does not handle the absence of an actual
186 // input for sc. Instead, it retains an existing pointer value (the prior
187 // field in the packed data object). This can cause confusion because
188 // drivers might now inspect bogus sc data.
189 if (scLen == 0) {
190 sc = NULL;
191 }
Stephen Hines44199772012-02-21 20:13:12 -0800192 s->runForEach(rsc, slot,
Jason Sams5fb1aeb2011-04-27 15:12:49 -0700193 static_cast<const Allocation *>(vain), static_cast<Allocation *>(vaout),
Tim Murrayd4ecb172013-02-07 12:17:03 -0800194 params, paramLen, sc);
Jason Sams5fb1aeb2011-04-27 15:12:49 -0700195
196}
197
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800198void rsi_ScriptInvoke(Context *rsc, RsScript vs, uint32_t slot) {
Jason Sams8c6bc692009-09-16 15:04:38 -0700199 Script *s = static_cast<Script *>(vs);
Jason Sams22fa3712010-05-19 17:22:57 -0700200 s->Invoke(rsc, slot, NULL, 0);
Jason Sams8c6bc692009-09-16 15:04:38 -0700201}
202
203
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800204void rsi_ScriptInvokeData(Context *rsc, RsScript vs, uint32_t slot, void *data) {
Jason Samsbe36bf32010-05-11 14:03:58 -0700205 Script *s = static_cast<Script *>(vs);
Jason Sams22fa3712010-05-19 17:22:57 -0700206 s->Invoke(rsc, slot, NULL, 0);
Jason Samsbe36bf32010-05-11 14:03:58 -0700207}
208
Alex Sakhartchoukb81a0eb2011-06-03 10:18:01 -0700209void rsi_ScriptInvokeV(Context *rsc, RsScript vs, uint32_t slot, const void *data, size_t len) {
Jason Samsbe36bf32010-05-11 14:03:58 -0700210 Script *s = static_cast<Script *>(vs);
Jason Sams22fa3712010-05-19 17:22:57 -0700211 s->Invoke(rsc, slot, data, len);
Jason Samsbe36bf32010-05-11 14:03:58 -0700212}
213
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800214void rsi_ScriptSetVarI(Context *rsc, RsScript vs, uint32_t slot, int value) {
Jason Samsbe36bf32010-05-11 14:03:58 -0700215 Script *s = static_cast<Script *>(vs);
216 s->setVar(slot, &value, sizeof(value));
217}
218
Jason Samsa5eb6e12010-11-16 17:37:02 -0800219void rsi_ScriptSetVarObj(Context *rsc, RsScript vs, uint32_t slot, RsObjectBase value) {
220 Script *s = static_cast<Script *>(vs);
221 ObjectBase *o = static_cast<ObjectBase *>(value);
222 s->setVarObj(slot, o);
223}
224
Tim Murray099bc262013-03-20 16:54:03 -0700225void rsi_ScriptSetVarJ(Context *rsc, RsScript vs, uint32_t slot, int64_t value) {
Stephen Hines0977c942010-10-11 10:54:21 -0700226 Script *s = static_cast<Script *>(vs);
227 s->setVar(slot, &value, sizeof(value));
228}
229
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800230void rsi_ScriptSetVarF(Context *rsc, RsScript vs, uint32_t slot, float value) {
Jason Samsbe36bf32010-05-11 14:03:58 -0700231 Script *s = static_cast<Script *>(vs);
232 s->setVar(slot, &value, sizeof(value));
233}
234
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800235void rsi_ScriptSetVarD(Context *rsc, RsScript vs, uint32_t slot, double value) {
Stephen Hines6d0a0742010-09-20 17:20:30 -0700236 Script *s = static_cast<Script *>(vs);
237 s->setVar(slot, &value, sizeof(value));
238}
239
Alex Sakhartchoukb81a0eb2011-06-03 10:18:01 -0700240void rsi_ScriptSetVarV(Context *rsc, RsScript vs, uint32_t slot, const void *data, size_t len) {
Jason Samsbe36bf32010-05-11 14:03:58 -0700241 Script *s = static_cast<Script *>(vs);
242 s->setVar(slot, data, len);
Jason Samsfa517192009-08-13 12:59:04 -0700243}
244
Tim Murray9c642392013-04-11 13:29:59 -0700245void rsi_ScriptGetVarV(Context *rsc, RsScript vs, uint32_t slot, void *data, size_t len) {
246 Script *s = static_cast<Script *>(vs);
247 s->getVar(slot, data, len);
248}
249
Stephen Hines2980f072012-04-09 18:26:29 -0700250void rsi_ScriptSetVarVE(Context *rsc, RsScript vs, uint32_t slot,
251 const void *data, size_t len, RsElement ve,
252 const size_t *dims, size_t dimLen) {
253 Script *s = static_cast<Script *>(vs);
254 Element *e = static_cast<Element *>(ve);
255 s->setVar(slot, data, len, e, dims, dimLen);
256}
257
Jason Sams326e0dd2009-05-22 14:03:28 -0700258}
259}
260