1 /*
2 * Copyright (C) 2009-2012 The Android Open Source Project
3 *
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"
18 #include <time.h>
19
20 using namespace android;
21 using namespace android::renderscript;
22
Script(Context * rsc)23 Script::Script(Context *rsc) : ObjectBase(rsc) {
24 memset(&mEnviroment, 0, sizeof(mEnviroment));
25 memset(&mHal, 0, sizeof(mHal));
26
27 mSlots = NULL;
28 mTypes = NULL;
29 mInitialized = false;
30 }
31
~Script()32 Script::~Script() {
33 if (mSlots) {
34 delete [] mSlots;
35 mSlots = NULL;
36 }
37 if (mTypes) {
38 delete [] mTypes;
39 mTypes = NULL;
40 }
41 }
42
setSlot(uint32_t slot,Allocation * a)43 void Script::setSlot(uint32_t slot, Allocation *a) {
44 //ALOGE("setSlot %i %p", slot, a);
45 if (slot >= mHal.info.exportedVariableCount) {
46 ALOGE("Script::setSlot unable to set allocation, invalid slot index");
47 return;
48 }
49
50 mSlots[slot].set(a);
51 mRSC->mHal.funcs.script.setGlobalBind(mRSC, this, slot, a);
52 }
53
setVar(uint32_t slot,const void * val,size_t len)54 void Script::setVar(uint32_t slot, const void *val, size_t len) {
55 //ALOGE("setVar %i %p %i", slot, val, len);
56 if (slot >= mHal.info.exportedVariableCount) {
57 ALOGE("Script::setVar unable to set allocation, invalid slot index");
58 return;
59 }
60 mRSC->mHal.funcs.script.setGlobalVar(mRSC, this, slot, (void *)val, len);
61 }
62
setVar(uint32_t slot,const void * val,size_t len,Element * e,const size_t * dims,size_t dimLen)63 void Script::setVar(uint32_t slot, const void *val, size_t len, Element *e,
64 const size_t *dims, size_t dimLen) {
65 if (slot >= mHal.info.exportedVariableCount) {
66 ALOGE("Script::setVar unable to set allocation, invalid slot index");
67 return;
68 }
69 mRSC->mHal.funcs.script.setGlobalVarWithElemDims(mRSC, this, slot,
70 (void *)val, len, e, dims, dimLen);
71 }
72
setVarObj(uint32_t slot,ObjectBase * val)73 void Script::setVarObj(uint32_t slot, ObjectBase *val) {
74 //ALOGE("setVarObj %i %p", slot, val);
75 if (slot >= mHal.info.exportedVariableCount) {
76 ALOGE("Script::setVarObj unable to set allocation, invalid slot index");
77 return;
78 }
79 //ALOGE("setvarobj %i %p", slot, val);
80 mRSC->mHal.funcs.script.setGlobalObj(mRSC, this, slot, val);
81 }
82
freeChildren()83 bool Script::freeChildren() {
84 incSysRef();
85 mRSC->mHal.funcs.script.invokeFreeChildren(mRSC, this);
86 return decSysRef();
87 }
88
ScriptKernelID(Context * rsc,Script * s,int slot,int sig)89 ScriptKernelID::ScriptKernelID(Context *rsc, Script *s, int slot, int sig)
90 : ObjectBase(rsc) {
91
92 mScript = s;
93 mSlot = slot;
94 mHasKernelInput = (sig & 1) != 0;
95 mHasKernelOutput = (sig & 2) != 0;
96 }
97
~ScriptKernelID()98 ScriptKernelID::~ScriptKernelID() {
99
100 }
101
serialize(Context * rsc,OStream * stream) const102 void ScriptKernelID::serialize(Context *rsc, OStream *stream) const {
103
104 }
105
getClassId() const106 RsA3DClassID ScriptKernelID::getClassId() const {
107 return RS_A3D_CLASS_ID_SCRIPT_KERNEL_ID;
108 }
109
ScriptFieldID(Context * rsc,Script * s,int slot)110 ScriptFieldID::ScriptFieldID(Context *rsc, Script *s, int slot) : ObjectBase(rsc) {
111 mScript = s;
112 mSlot = slot;
113 }
114
~ScriptFieldID()115 ScriptFieldID::~ScriptFieldID() {
116
117 }
118
serialize(Context * rsc,OStream * stream) const119 void ScriptFieldID::serialize(Context *rsc, OStream *stream) const {
120
121 }
122
getClassId() const123 RsA3DClassID ScriptFieldID::getClassId() const {
124 return RS_A3D_CLASS_ID_SCRIPT_FIELD_ID;
125 }
126
127
128 namespace android {
129 namespace renderscript {
130
rsi_ScriptKernelIDCreate(Context * rsc,RsScript vs,int slot,int sig)131 RsScriptKernelID rsi_ScriptKernelIDCreate(Context *rsc, RsScript vs, int slot, int sig) {
132 return new ScriptKernelID(rsc, (Script *)vs, slot, sig);
133 }
134
rsi_ScriptFieldIDCreate(Context * rsc,RsScript vs,int slot)135 RsScriptFieldID rsi_ScriptFieldIDCreate(Context *rsc, RsScript vs, int slot) {
136 return new ScriptFieldID(rsc, (Script *)vs, slot);
137 }
138
rsi_ScriptBindAllocation(Context * rsc,RsScript vs,RsAllocation va,uint32_t slot)139 void rsi_ScriptBindAllocation(Context * rsc, RsScript vs, RsAllocation va, uint32_t slot) {
140 Script *s = static_cast<Script *>(vs);
141 Allocation *a = static_cast<Allocation *>(va);
142 s->setSlot(slot, a);
143 }
144
rsi_ScriptSetTimeZone(Context * rsc,RsScript vs,const char * timeZone,size_t length)145 void rsi_ScriptSetTimeZone(Context * rsc, RsScript vs, const char * timeZone, size_t length) {
146 // We unfortunately need to make a new copy of the string, since it is
147 // not NULL-terminated. We then use setenv(), which properly handles
148 // freeing/duplicating the actual string for the environment.
149 char *tz = (char *) malloc(length + 1);
150 if (!tz) {
151 ALOGE("Couldn't allocate memory for timezone buffer");
152 return;
153 }
154 strncpy(tz, timeZone, length);
155 tz[length] = '\0';
156 if (setenv("TZ", tz, 1) == 0) {
157 tzset();
158 } else {
159 ALOGE("Error setting timezone");
160 }
161 free(tz);
162 }
163
rsi_ScriptForEach(Context * rsc,RsScript vs,uint32_t slot,RsAllocation vain,RsAllocation vaout,const void * params,size_t paramLen)164 void rsi_ScriptForEach(Context *rsc, RsScript vs, uint32_t slot,
165 RsAllocation vain, RsAllocation vaout,
166 const void *params, size_t paramLen) {
167 Script *s = static_cast<Script *>(vs);
168 s->runForEach(rsc, slot,
169 static_cast<const Allocation *>(vain), static_cast<Allocation *>(vaout),
170 params, paramLen);
171
172 }
173
rsi_ScriptInvoke(Context * rsc,RsScript vs,uint32_t slot)174 void rsi_ScriptInvoke(Context *rsc, RsScript vs, uint32_t slot) {
175 Script *s = static_cast<Script *>(vs);
176 s->Invoke(rsc, slot, NULL, 0);
177 }
178
179
rsi_ScriptInvokeData(Context * rsc,RsScript vs,uint32_t slot,void * data)180 void rsi_ScriptInvokeData(Context *rsc, RsScript vs, uint32_t slot, void *data) {
181 Script *s = static_cast<Script *>(vs);
182 s->Invoke(rsc, slot, NULL, 0);
183 }
184
rsi_ScriptInvokeV(Context * rsc,RsScript vs,uint32_t slot,const void * data,size_t len)185 void rsi_ScriptInvokeV(Context *rsc, RsScript vs, uint32_t slot, const void *data, size_t len) {
186 Script *s = static_cast<Script *>(vs);
187 s->Invoke(rsc, slot, data, len);
188 }
189
rsi_ScriptSetVarI(Context * rsc,RsScript vs,uint32_t slot,int value)190 void rsi_ScriptSetVarI(Context *rsc, RsScript vs, uint32_t slot, int value) {
191 Script *s = static_cast<Script *>(vs);
192 s->setVar(slot, &value, sizeof(value));
193 }
194
rsi_ScriptSetVarObj(Context * rsc,RsScript vs,uint32_t slot,RsObjectBase value)195 void rsi_ScriptSetVarObj(Context *rsc, RsScript vs, uint32_t slot, RsObjectBase value) {
196 Script *s = static_cast<Script *>(vs);
197 ObjectBase *o = static_cast<ObjectBase *>(value);
198 s->setVarObj(slot, o);
199 }
200
rsi_ScriptSetVarJ(Context * rsc,RsScript vs,uint32_t slot,long long value)201 void rsi_ScriptSetVarJ(Context *rsc, RsScript vs, uint32_t slot, long long value) {
202 Script *s = static_cast<Script *>(vs);
203 s->setVar(slot, &value, sizeof(value));
204 }
205
rsi_ScriptSetVarF(Context * rsc,RsScript vs,uint32_t slot,float value)206 void rsi_ScriptSetVarF(Context *rsc, RsScript vs, uint32_t slot, float value) {
207 Script *s = static_cast<Script *>(vs);
208 s->setVar(slot, &value, sizeof(value));
209 }
210
rsi_ScriptSetVarD(Context * rsc,RsScript vs,uint32_t slot,double value)211 void rsi_ScriptSetVarD(Context *rsc, RsScript vs, uint32_t slot, double value) {
212 Script *s = static_cast<Script *>(vs);
213 s->setVar(slot, &value, sizeof(value));
214 }
215
rsi_ScriptSetVarV(Context * rsc,RsScript vs,uint32_t slot,const void * data,size_t len)216 void rsi_ScriptSetVarV(Context *rsc, RsScript vs, uint32_t slot, const void *data, size_t len) {
217 Script *s = static_cast<Script *>(vs);
218 s->setVar(slot, data, len);
219 }
220
rsi_ScriptSetVarVE(Context * rsc,RsScript vs,uint32_t slot,const void * data,size_t len,RsElement ve,const size_t * dims,size_t dimLen)221 void rsi_ScriptSetVarVE(Context *rsc, RsScript vs, uint32_t slot,
222 const void *data, size_t len, RsElement ve,
223 const size_t *dims, size_t dimLen) {
224 Script *s = static_cast<Script *>(vs);
225 Element *e = static_cast<Element *>(ve);
226 s->setVar(slot, data, len, e, dims, dimLen);
227 }
228
229 }
230 }
231
232