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 if (a != NULL) {
52 mRSC->mHal.funcs.script.setGlobalBind(mRSC, this, slot, a->getPtr());
53 } else {
54 mRSC->mHal.funcs.script.setGlobalBind(mRSC, this, slot, NULL);
55 }
56 }
57
setVar(uint32_t slot,const void * val,size_t len)58 void Script::setVar(uint32_t slot, const void *val, size_t len) {
59 //ALOGE("setVar %i %p %i", slot, val, len);
60 if (slot >= mHal.info.exportedVariableCount) {
61 ALOGE("Script::setVar unable to set allocation, invalid slot index");
62 return;
63 }
64 mRSC->mHal.funcs.script.setGlobalVar(mRSC, this, slot, (void *)val, len);
65 }
66
setVar(uint32_t slot,const void * val,size_t len,Element * e,const size_t * dims,size_t dimLen)67 void Script::setVar(uint32_t slot, const void *val, size_t len, Element *e,
68 const size_t *dims, size_t dimLen) {
69 if (slot >= mHal.info.exportedVariableCount) {
70 ALOGE("Script::setVar unable to set allocation, invalid slot index");
71 return;
72 }
73 mRSC->mHal.funcs.script.setGlobalVarWithElemDims(mRSC, this, slot,
74 (void *)val, len, e, dims, dimLen);
75 }
76
setVarObj(uint32_t slot,ObjectBase * val)77 void Script::setVarObj(uint32_t slot, ObjectBase *val) {
78 //ALOGE("setVarObj %i %p", slot, val);
79 if (slot >= mHal.info.exportedVariableCount) {
80 ALOGE("Script::setVarObj unable to set allocation, invalid slot index");
81 return;
82 }
83 //ALOGE("setvarobj %i %p", slot, val);
84 mRSC->mHal.funcs.script.setGlobalObj(mRSC, this, slot, val);
85 }
86
freeChildren()87 bool Script::freeChildren() {
88 incSysRef();
89 mRSC->mHal.funcs.script.invokeFreeChildren(mRSC, this);
90 return decSysRef();
91 }
92
93 namespace android {
94 namespace renderscript {
95
rsi_ScriptBindAllocation(Context * rsc,RsScript vs,RsAllocation va,uint32_t slot)96 void rsi_ScriptBindAllocation(Context * rsc, RsScript vs, RsAllocation va, uint32_t slot) {
97 Script *s = static_cast<Script *>(vs);
98 Allocation *a = static_cast<Allocation *>(va);
99 s->setSlot(slot, a);
100 //ALOGE("rsi_ScriptBindAllocation %i %p %p", slot, a, a->getPtr());
101 }
102
rsi_ScriptSetTimeZone(Context * rsc,RsScript vs,const char * timeZone,size_t length)103 void rsi_ScriptSetTimeZone(Context * rsc, RsScript vs, const char * timeZone, size_t length) {
104 // We unfortunately need to make a new copy of the string, since it is
105 // not NULL-terminated. We then use setenv(), which properly handles
106 // freeing/duplicating the actual string for the environment.
107 char *tz = (char *) malloc(length + 1);
108 if (!tz) {
109 ALOGE("Couldn't allocate memory for timezone buffer");
110 return;
111 }
112 strncpy(tz, timeZone, length);
113 tz[length] = '\0';
114 if (setenv("TZ", tz, 1) == 0) {
115 tzset();
116 } else {
117 ALOGE("Error setting timezone");
118 }
119 free(tz);
120 }
121
rsi_ScriptForEach(Context * rsc,RsScript vs,uint32_t slot,RsAllocation vain,RsAllocation vaout,const void * params,size_t paramLen)122 void rsi_ScriptForEach(Context *rsc, RsScript vs, uint32_t slot,
123 RsAllocation vain, RsAllocation vaout,
124 const void *params, size_t paramLen) {
125 Script *s = static_cast<Script *>(vs);
126 s->runForEach(rsc, slot,
127 static_cast<const Allocation *>(vain), static_cast<Allocation *>(vaout),
128 params, paramLen);
129
130 }
131
rsi_ScriptInvoke(Context * rsc,RsScript vs,uint32_t slot)132 void rsi_ScriptInvoke(Context *rsc, RsScript vs, uint32_t slot) {
133 Script *s = static_cast<Script *>(vs);
134 s->Invoke(rsc, slot, NULL, 0);
135 }
136
137
rsi_ScriptInvokeData(Context * rsc,RsScript vs,uint32_t slot,void * data)138 void rsi_ScriptInvokeData(Context *rsc, RsScript vs, uint32_t slot, void *data) {
139 Script *s = static_cast<Script *>(vs);
140 s->Invoke(rsc, slot, NULL, 0);
141 }
142
rsi_ScriptInvokeV(Context * rsc,RsScript vs,uint32_t slot,const void * data,size_t len)143 void rsi_ScriptInvokeV(Context *rsc, RsScript vs, uint32_t slot, const void *data, size_t len) {
144 Script *s = static_cast<Script *>(vs);
145 s->Invoke(rsc, slot, data, len);
146 }
147
rsi_ScriptSetVarI(Context * rsc,RsScript vs,uint32_t slot,int value)148 void rsi_ScriptSetVarI(Context *rsc, RsScript vs, uint32_t slot, int value) {
149 Script *s = static_cast<Script *>(vs);
150 s->setVar(slot, &value, sizeof(value));
151 }
152
rsi_ScriptSetVarObj(Context * rsc,RsScript vs,uint32_t slot,RsObjectBase value)153 void rsi_ScriptSetVarObj(Context *rsc, RsScript vs, uint32_t slot, RsObjectBase value) {
154 Script *s = static_cast<Script *>(vs);
155 ObjectBase *o = static_cast<ObjectBase *>(value);
156 s->setVarObj(slot, o);
157 }
158
rsi_ScriptSetVarJ(Context * rsc,RsScript vs,uint32_t slot,long long value)159 void rsi_ScriptSetVarJ(Context *rsc, RsScript vs, uint32_t slot, long long value) {
160 Script *s = static_cast<Script *>(vs);
161 s->setVar(slot, &value, sizeof(value));
162 }
163
rsi_ScriptSetVarF(Context * rsc,RsScript vs,uint32_t slot,float value)164 void rsi_ScriptSetVarF(Context *rsc, RsScript vs, uint32_t slot, float value) {
165 Script *s = static_cast<Script *>(vs);
166 s->setVar(slot, &value, sizeof(value));
167 }
168
rsi_ScriptSetVarD(Context * rsc,RsScript vs,uint32_t slot,double value)169 void rsi_ScriptSetVarD(Context *rsc, RsScript vs, uint32_t slot, double value) {
170 Script *s = static_cast<Script *>(vs);
171 s->setVar(slot, &value, sizeof(value));
172 }
173
rsi_ScriptSetVarV(Context * rsc,RsScript vs,uint32_t slot,const void * data,size_t len)174 void rsi_ScriptSetVarV(Context *rsc, RsScript vs, uint32_t slot, const void *data, size_t len) {
175 Script *s = static_cast<Script *>(vs);
176 s->setVar(slot, data, len);
177 }
178
rsi_ScriptSetVarVE(Context * rsc,RsScript vs,uint32_t slot,const void * data,size_t len,RsElement ve,const size_t * dims,size_t dimLen)179 void rsi_ScriptSetVarVE(Context *rsc, RsScript vs, uint32_t slot,
180 const void *data, size_t len, RsElement ve,
181 const size_t *dims, size_t dimLen) {
182 Script *s = static_cast<Script *>(vs);
183 Element *e = static_cast<Element *>(ve);
184 s->setVar(slot, data, len, e, dims, dimLen);
185 }
186
187 }
188 }
189
190