blob: 4e8ba1ecab3f5a3271f99941cf319d4da9b7bd67 [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;
Jason Sams326e0dd2009-05-22 14:03:28 -070030}
31
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -080032Script::~Script() {
33 if (mSlots) {
Alex Sakhartchouk700ba382010-10-08 15:00:05 -070034 delete [] mSlots;
35 mSlots = NULL;
36 }
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -080037 if (mTypes) {
Alex Sakhartchouk700ba382010-10-08 15:00:05 -070038 delete [] mTypes;
39 mTypes = NULL;
40 }
41}
42
Alex Sakhartchouk700ba382010-10-08 15:00:05 -070043void Script::setSlot(uint32_t slot, Allocation *a) {
Steve Blockaf12ac62012-01-06 19:20:56 +000044 //ALOGE("setSlot %i %p", slot, a);
Jason Samsbad80742011-03-16 16:29:28 -070045 if (slot >= mHal.info.exportedVariableCount) {
Steve Blockaf12ac62012-01-06 19:20:56 +000046 ALOGE("Script::setSlot unable to set allocation, invalid slot index");
Alex Sakhartchouk700ba382010-10-08 15:00:05 -070047 return;
48 }
49
50 mSlots[slot].set(a);
Jason Sams807fdc42012-07-25 17:55:39 -070051 mRSC->mHal.funcs.script.setGlobalBind(mRSC, this, slot, a);
Jason Sams326e0dd2009-05-22 14:03:28 -070052}
53
Alex Sakhartchoukb81a0eb2011-06-03 10:18:01 -070054void Script::setVar(uint32_t slot, const void *val, size_t len) {
Steve Blockaf12ac62012-01-06 19:20:56 +000055 //ALOGE("setVar %i %p %i", slot, val, len);
Jason Samsbad80742011-03-16 16:29:28 -070056 if (slot >= mHal.info.exportedVariableCount) {
Steve Blockaf12ac62012-01-06 19:20:56 +000057 ALOGE("Script::setVar unable to set allocation, invalid slot index");
Jason Samsbad80742011-03-16 16:29:28 -070058 return;
Jason Samsbe36bf32010-05-11 14:03:58 -070059 }
Jason Samsbad80742011-03-16 16:29:28 -070060 mRSC->mHal.funcs.script.setGlobalVar(mRSC, this, slot, (void *)val, len);
Jason Samsbe36bf32010-05-11 14:03:58 -070061}
62
Tim Murray9c642392013-04-11 13:29:59 -070063void Script::getVar(uint32_t slot, const void *val, size_t len) {
64 //ALOGE("getVar %i %p %i", slot, val, len);
65 if (slot >= mHal.info.exportedVariableCount) {
66 ALOGE("Script::getVar unable to set allocation, invalid slot index");
67 return;
68 }
69 mRSC->mHal.funcs.script.getGlobalVar(mRSC, this, slot, (void *)val, len);
70}
71
Stephen Hines2980f072012-04-09 18:26:29 -070072void Script::setVar(uint32_t slot, const void *val, size_t len, Element *e,
73 const size_t *dims, size_t dimLen) {
74 if (slot >= mHal.info.exportedVariableCount) {
75 ALOGE("Script::setVar unable to set allocation, invalid slot index");
76 return;
77 }
78 mRSC->mHal.funcs.script.setGlobalVarWithElemDims(mRSC, this, slot,
79 (void *)val, len, e, dims, dimLen);
80}
81
Jason Samsa5eb6e12010-11-16 17:37:02 -080082void Script::setVarObj(uint32_t slot, ObjectBase *val) {
Steve Blockaf12ac62012-01-06 19:20:56 +000083 //ALOGE("setVarObj %i %p", slot, val);
Jason Samsbad80742011-03-16 16:29:28 -070084 if (slot >= mHal.info.exportedVariableCount) {
Steve Blockaf12ac62012-01-06 19:20:56 +000085 ALOGE("Script::setVarObj unable to set allocation, invalid slot index");
Jason Samsbad80742011-03-16 16:29:28 -070086 return;
Jason Samsa5eb6e12010-11-16 17:37:02 -080087 }
Steve Blockaf12ac62012-01-06 19:20:56 +000088 //ALOGE("setvarobj %i %p", slot, val);
Jason Samsbad80742011-03-16 16:29:28 -070089 mRSC->mHal.funcs.script.setGlobalObj(mRSC, this, slot, val);
Jason Samsa5eb6e12010-11-16 17:37:02 -080090}
91
Stephen Hines4ee16ff2011-08-31 17:41:39 -070092bool Script::freeChildren() {
93 incSysRef();
94 mRSC->mHal.funcs.script.invokeFreeChildren(mRSC, this);
95 return decSysRef();
96}
97
Jason Samsdbe66d62012-09-17 13:54:41 -070098ScriptKernelID::ScriptKernelID(Context *rsc, Script *s, int slot, int sig)
99 : ObjectBase(rsc) {
100
101 mScript = s;
102 mSlot = slot;
103 mHasKernelInput = (sig & 1) != 0;
104 mHasKernelOutput = (sig & 2) != 0;
105}
106
107ScriptKernelID::~ScriptKernelID() {
108
109}
110
111void ScriptKernelID::serialize(Context *rsc, OStream *stream) const {
112
113}
114
115RsA3DClassID ScriptKernelID::getClassId() const {
116 return RS_A3D_CLASS_ID_SCRIPT_KERNEL_ID;
117}
118
119ScriptFieldID::ScriptFieldID(Context *rsc, Script *s, int slot) : ObjectBase(rsc) {
120 mScript = s;
121 mSlot = slot;
122}
123
124ScriptFieldID::~ScriptFieldID() {
125
126}
127
128void ScriptFieldID::serialize(Context *rsc, OStream *stream) const {
129
130}
131
132RsA3DClassID ScriptFieldID::getClassId() const {
133 return RS_A3D_CLASS_ID_SCRIPT_FIELD_ID;
134}
135
136
Jason Sams326e0dd2009-05-22 14:03:28 -0700137namespace android {
138namespace renderscript {
139
Jason Samsdbe66d62012-09-17 13:54:41 -0700140RsScriptKernelID rsi_ScriptKernelIDCreate(Context *rsc, RsScript vs, int slot, int sig) {
Stephen Hines61c86952013-04-09 17:34:43 -0700141 ScriptKernelID *kid = new ScriptKernelID(rsc, (Script *)vs, slot, sig);
142 kid->incUserRef();
143 return kid;
Jason Samsdbe66d62012-09-17 13:54:41 -0700144}
145
146RsScriptFieldID rsi_ScriptFieldIDCreate(Context *rsc, RsScript vs, int slot) {
Stephen Hines61c86952013-04-09 17:34:43 -0700147 ScriptFieldID *fid = new ScriptFieldID(rsc, (Script *)vs, slot);
148 fid->incUserRef();
149 return fid;
Jason Samsdbe66d62012-09-17 13:54:41 -0700150}
151
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800152void rsi_ScriptBindAllocation(Context * rsc, RsScript vs, RsAllocation va, uint32_t slot) {
Jason Sams326e0dd2009-05-22 14:03:28 -0700153 Script *s = static_cast<Script *>(vs);
Jason Samsbe36bf32010-05-11 14:03:58 -0700154 Allocation *a = static_cast<Allocation *>(va);
Alex Sakhartchouk700ba382010-10-08 15:00:05 -0700155 s->setSlot(slot, a);
Jason Sams326e0dd2009-05-22 14:03:28 -0700156}
157
Alex Sakhartchoukb81a0eb2011-06-03 10:18:01 -0700158void rsi_ScriptSetTimeZone(Context * rsc, RsScript vs, const char * timeZone, size_t length) {
Stephen Hinesd2432b92011-11-09 18:02:20 -0800159 // We unfortunately need to make a new copy of the string, since it is
160 // not NULL-terminated. We then use setenv(), which properly handles
161 // freeing/duplicating the actual string for the environment.
162 char *tz = (char *) malloc(length + 1);
163 if (!tz) {
Steve Blockaf12ac62012-01-06 19:20:56 +0000164 ALOGE("Couldn't allocate memory for timezone buffer");
Stephen Hinesd2432b92011-11-09 18:02:20 -0800165 return;
166 }
167 strncpy(tz, timeZone, length);
168 tz[length] = '\0';
169 if (setenv("TZ", tz, 1) == 0) {
170 tzset();
171 } else {
Steve Blockaf12ac62012-01-06 19:20:56 +0000172 ALOGE("Error setting timezone");
Stephen Hinesd2432b92011-11-09 18:02:20 -0800173 }
174 free(tz);
Jason Samsd34b7252009-08-04 16:58:20 -0700175}
176
Jason Sams5fb1aeb2011-04-27 15:12:49 -0700177void rsi_ScriptForEach(Context *rsc, RsScript vs, uint32_t slot,
178 RsAllocation vain, RsAllocation vaout,
Tim Murrayd4ecb172013-02-07 12:17:03 -0800179 const void *params, size_t paramLen,
180 const RsScriptCall *sc, size_t scLen) {
Jason Sams5fb1aeb2011-04-27 15:12:49 -0700181 Script *s = static_cast<Script *>(vs);
Stephen Hines5dd9d072013-04-02 00:08:32 -0700182 // The rs.spec generated code does not handle the absence of an actual
183 // input for sc. Instead, it retains an existing pointer value (the prior
184 // field in the packed data object). This can cause confusion because
185 // drivers might now inspect bogus sc data.
186 if (scLen == 0) {
187 sc = NULL;
188 }
Stephen Hines44199772012-02-21 20:13:12 -0800189 s->runForEach(rsc, slot,
Jason Sams5fb1aeb2011-04-27 15:12:49 -0700190 static_cast<const Allocation *>(vain), static_cast<Allocation *>(vaout),
Tim Murrayd4ecb172013-02-07 12:17:03 -0800191 params, paramLen, sc);
Jason Sams5fb1aeb2011-04-27 15:12:49 -0700192
193}
194
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800195void rsi_ScriptInvoke(Context *rsc, RsScript vs, uint32_t slot) {
Jason Sams8c6bc692009-09-16 15:04:38 -0700196 Script *s = static_cast<Script *>(vs);
Jason Sams22fa3712010-05-19 17:22:57 -0700197 s->Invoke(rsc, slot, NULL, 0);
Jason Sams8c6bc692009-09-16 15:04:38 -0700198}
199
200
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800201void rsi_ScriptInvokeData(Context *rsc, RsScript vs, uint32_t slot, void *data) {
Jason Samsbe36bf32010-05-11 14:03:58 -0700202 Script *s = static_cast<Script *>(vs);
Jason Sams22fa3712010-05-19 17:22:57 -0700203 s->Invoke(rsc, slot, NULL, 0);
Jason Samsbe36bf32010-05-11 14:03:58 -0700204}
205
Alex Sakhartchoukb81a0eb2011-06-03 10:18:01 -0700206void rsi_ScriptInvokeV(Context *rsc, RsScript vs, uint32_t slot, const void *data, size_t len) {
Jason Samsbe36bf32010-05-11 14:03:58 -0700207 Script *s = static_cast<Script *>(vs);
Jason Sams22fa3712010-05-19 17:22:57 -0700208 s->Invoke(rsc, slot, data, len);
Jason Samsbe36bf32010-05-11 14:03:58 -0700209}
210
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800211void rsi_ScriptSetVarI(Context *rsc, RsScript vs, uint32_t slot, int value) {
Jason Samsbe36bf32010-05-11 14:03:58 -0700212 Script *s = static_cast<Script *>(vs);
213 s->setVar(slot, &value, sizeof(value));
214}
215
Jason Samsa5eb6e12010-11-16 17:37:02 -0800216void rsi_ScriptSetVarObj(Context *rsc, RsScript vs, uint32_t slot, RsObjectBase value) {
217 Script *s = static_cast<Script *>(vs);
218 ObjectBase *o = static_cast<ObjectBase *>(value);
219 s->setVarObj(slot, o);
220}
221
Tim Murray099bc262013-03-20 16:54:03 -0700222void rsi_ScriptSetVarJ(Context *rsc, RsScript vs, uint32_t slot, int64_t value) {
Stephen Hines0977c942010-10-11 10:54:21 -0700223 Script *s = static_cast<Script *>(vs);
224 s->setVar(slot, &value, sizeof(value));
225}
226
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800227void rsi_ScriptSetVarF(Context *rsc, RsScript vs, uint32_t slot, float value) {
Jason Samsbe36bf32010-05-11 14:03:58 -0700228 Script *s = static_cast<Script *>(vs);
229 s->setVar(slot, &value, sizeof(value));
230}
231
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800232void rsi_ScriptSetVarD(Context *rsc, RsScript vs, uint32_t slot, double value) {
Stephen Hines6d0a0742010-09-20 17:20:30 -0700233 Script *s = static_cast<Script *>(vs);
234 s->setVar(slot, &value, sizeof(value));
235}
236
Alex Sakhartchoukb81a0eb2011-06-03 10:18:01 -0700237void rsi_ScriptSetVarV(Context *rsc, RsScript vs, uint32_t slot, const void *data, size_t len) {
Jason Samsbe36bf32010-05-11 14:03:58 -0700238 Script *s = static_cast<Script *>(vs);
239 s->setVar(slot, data, len);
Jason Samsfa517192009-08-13 12:59:04 -0700240}
241
Tim Murray9c642392013-04-11 13:29:59 -0700242void rsi_ScriptGetVarV(Context *rsc, RsScript vs, uint32_t slot, void *data, size_t len) {
243 Script *s = static_cast<Script *>(vs);
244 s->getVar(slot, data, len);
245}
246
Stephen Hines2980f072012-04-09 18:26:29 -0700247void rsi_ScriptSetVarVE(Context *rsc, RsScript vs, uint32_t slot,
248 const void *data, size_t len, RsElement ve,
249 const size_t *dims, size_t dimLen) {
250 Script *s = static_cast<Script *>(vs);
251 Element *e = static_cast<Element *>(ve);
252 s->setVar(slot, data, len, e, dims, dimLen);
253}
254
Jason Sams326e0dd2009-05-22 14:03:28 -0700255}
256}
257