rsScript.cpp revision 44bef6fba6244292b751387f3d6c31cca96c28ad
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
20using namespace android;
21using namespace android::renderscript;
22
23Script::Script(Context *rsc) : ObjectBase(rsc) {
24    memset(&mEnviroment, 0, sizeof(mEnviroment));
25    memset(&mHal, 0, sizeof(mHal));
26
27    mSlots = nullptr;
28    mTypes = nullptr;
29    mInitialized = false;
30    mHasObjectSlots = false;
31}
32
33Script::~Script() {
34    if (mSlots) {
35        delete [] mSlots;
36        mSlots = nullptr;
37    }
38    if (mTypes) {
39        delete [] mTypes;
40        mTypes = nullptr;
41    }
42}
43
44void Script::setSlot(uint32_t slot, Allocation *a) {
45    //ALOGE("setSlot %i %p", slot, a);
46    if (slot >= mHal.info.exportedVariableCount) {
47        ALOGE("Script::setSlot unable to set allocation, invalid slot index");
48        return;
49    }
50
51    mSlots[slot].set(a);
52    mHasObjectSlots = true;
53    mRSC->mHal.funcs.script.setGlobalBind(mRSC, this, slot, a);
54}
55
56void Script::setVar(uint32_t slot, const void *val, size_t len) {
57    //ALOGE("setVar %i %p %i", slot, val, len);
58    if (slot >= mHal.info.exportedVariableCount) {
59        ALOGE("Script::setVar unable to set allocation, invalid slot index");
60        return;
61    }
62    mRSC->mHal.funcs.script.setGlobalVar(mRSC, this, slot, (void *)val, len);
63}
64
65void 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              "%u >= %zu", slot, mHal.info.exportedVariableCount);
70        return;
71    }
72    mRSC->mHal.funcs.script.getGlobalVar(mRSC, this, slot, (void *)val, len);
73}
74
75void Script::setVar(uint32_t slot, const void *val, size_t len, Element *e,
76                    const uint32_t *dims, size_t dimLen) {
77    if (slot >= mHal.info.exportedVariableCount) {
78        ALOGE("Script::setVar unable to set allocation, invalid slot index: "
79              "%u >= %zu", slot, mHal.info.exportedVariableCount);
80        return;
81    }
82    mRSC->mHal.funcs.script.setGlobalVarWithElemDims(mRSC, this, slot,
83            (void *)val, len, e, dims, dimLen);
84}
85
86void Script::setVarObj(uint32_t slot, ObjectBase *val) {
87    //ALOGE("setVarObj %i %p", slot, val);
88    if (slot >= mHal.info.exportedVariableCount) {
89        ALOGE("Script::setVarObj unable to set allocation, invalid slot index: "
90              "%u >= %zu", slot, mHal.info.exportedVariableCount);
91        return;
92    }
93    mHasObjectSlots = true;
94    mRSC->mHal.funcs.script.setGlobalObj(mRSC, this, slot, val);
95}
96
97void Script::callUpdateCacheObject(const Context *rsc, void *dstObj) const {
98    if (rsc->mHal.funcs.script.updateCachedObject != nullptr) {
99        rsc->mHal.funcs.script.updateCachedObject(rsc, this, (rs_script *)dstObj);
100    } else {
101        *((const void **)dstObj) = this;
102    }
103}
104
105bool Script::freeChildren() {
106    incSysRef();
107    mRSC->mHal.funcs.script.invokeFreeChildren(mRSC, this);
108    return decSysRef();
109}
110
111ScriptKernelID::ScriptKernelID(Context *rsc, Script *s, int slot, int sig)
112        : ObjectBase(rsc) {
113
114    mScript = s;
115    mSlot = slot;
116    mHasKernelInput = (sig & 1) != 0;
117    mHasKernelOutput = (sig & 2) != 0;
118}
119
120ScriptKernelID::~ScriptKernelID() {
121
122}
123
124void ScriptKernelID::serialize(Context *rsc, OStream *stream) const {
125
126}
127
128RsA3DClassID ScriptKernelID::getClassId() const {
129    return RS_A3D_CLASS_ID_SCRIPT_KERNEL_ID;
130}
131
132ScriptFieldID::ScriptFieldID(Context *rsc, Script *s, int slot) : ObjectBase(rsc) {
133    mScript = s;
134    mSlot = slot;
135}
136
137ScriptFieldID::~ScriptFieldID() {
138
139}
140
141void ScriptFieldID::serialize(Context *rsc, OStream *stream) const {
142
143}
144
145RsA3DClassID ScriptFieldID::getClassId() const {
146    return RS_A3D_CLASS_ID_SCRIPT_FIELD_ID;
147}
148
149
150namespace android {
151namespace renderscript {
152
153RsScriptKernelID rsi_ScriptKernelIDCreate(Context *rsc, RsScript vs, int slot, int sig) {
154    ScriptKernelID *kid = new ScriptKernelID(rsc, (Script *)vs, slot, sig);
155    kid->incUserRef();
156    return kid;
157}
158
159RsScriptFieldID rsi_ScriptFieldIDCreate(Context *rsc, RsScript vs, int slot) {
160    ScriptFieldID *fid = new ScriptFieldID(rsc, (Script *)vs, slot);
161    fid->incUserRef();
162    return fid;
163}
164
165void rsi_ScriptBindAllocation(Context * rsc, RsScript vs, RsAllocation va, uint32_t slot) {
166    Script *s = static_cast<Script *>(vs);
167    Allocation *a = static_cast<Allocation *>(va);
168    s->setSlot(slot, a);
169}
170
171void rsi_ScriptSetTimeZone(Context * rsc, RsScript vs, const char * timeZone, size_t length) {
172    // We unfortunately need to make a new copy of the string, since it is
173    // not nullptr-terminated. We then use setenv(), which properly handles
174    // freeing/duplicating the actual string for the environment.
175    char *tz = (char *) malloc(length + 1);
176    if (!tz) {
177        ALOGE("Couldn't allocate memory for timezone buffer");
178        return;
179    }
180    strncpy(tz, timeZone, length);
181    tz[length] = '\0';
182    if (setenv("TZ", tz, 1) == 0) {
183        tzset();
184    } else {
185        ALOGE("Error setting timezone");
186    }
187    free(tz);
188}
189
190void rsi_ScriptForEachMulti(Context *rsc, RsScript vs, uint32_t slot,
191                            RsAllocation *vains, size_t inLen,
192                            RsAllocation vaout, const void *params,
193                            size_t paramLen, const RsScriptCall *sc,
194                            size_t scLen) {
195
196    Script      *s    = static_cast<Script *>(vs);
197    Allocation **ains = (Allocation**)(vains);
198
199    s->runForEach(rsc, slot,
200                  const_cast<const Allocation **>(ains), inLen,
201                  static_cast<Allocation *>(vaout), params, paramLen, sc);
202
203}
204
205void rsi_ScriptForEach(Context *rsc, RsScript vs, uint32_t slot,
206                       RsAllocation vain, RsAllocation vaout,
207                       const void *params, size_t paramLen,
208                       const RsScriptCall *sc, size_t scLen) {
209
210    if (vain == nullptr) {
211        rsi_ScriptForEachMulti(rsc, vs, slot, nullptr, 0, vaout, params, paramLen,
212                               sc, scLen);
213    } else {
214        RsAllocation ains[1] = {vain};
215
216        rsi_ScriptForEachMulti(rsc, vs, slot, ains,
217                               sizeof(ains) / sizeof(RsAllocation), vaout,
218                               params, paramLen, sc, scLen);
219    }
220}
221
222void rsi_ScriptInvoke(Context *rsc, RsScript vs, uint32_t slot) {
223    Script *s = static_cast<Script *>(vs);
224    s->Invoke(rsc, slot, nullptr, 0);
225}
226
227
228void rsi_ScriptInvokeData(Context *rsc, RsScript vs, uint32_t slot, void *data) {
229    Script *s = static_cast<Script *>(vs);
230    s->Invoke(rsc, slot, nullptr, 0);
231}
232
233void rsi_ScriptInvokeV(Context *rsc, RsScript vs, uint32_t slot, const void *data, size_t len) {
234    Script *s = static_cast<Script *>(vs);
235    s->Invoke(rsc, slot, data, len);
236}
237
238void rsi_ScriptSetVarI(Context *rsc, RsScript vs, uint32_t slot, int value) {
239    Script *s = static_cast<Script *>(vs);
240    s->setVar(slot, &value, sizeof(value));
241}
242
243void rsi_ScriptSetVarObj(Context *rsc, RsScript vs, uint32_t slot, RsObjectBase value) {
244    Script *s = static_cast<Script *>(vs);
245    ObjectBase *o = static_cast<ObjectBase *>(value);
246    s->setVarObj(slot, o);
247}
248
249void rsi_ScriptSetVarJ(Context *rsc, RsScript vs, uint32_t slot, int64_t value) {
250    Script *s = static_cast<Script *>(vs);
251    s->setVar(slot, &value, sizeof(value));
252}
253
254void rsi_ScriptSetVarF(Context *rsc, RsScript vs, uint32_t slot, float value) {
255    Script *s = static_cast<Script *>(vs);
256    s->setVar(slot, &value, sizeof(value));
257}
258
259void rsi_ScriptSetVarD(Context *rsc, RsScript vs, uint32_t slot, double value) {
260    Script *s = static_cast<Script *>(vs);
261    s->setVar(slot, &value, sizeof(value));
262}
263
264void rsi_ScriptSetVarV(Context *rsc, RsScript vs, uint32_t slot, const void *data, size_t len) {
265    Script *s = static_cast<Script *>(vs);
266    s->setVar(slot, data, len);
267}
268
269void rsi_ScriptGetVarV(Context *rsc, RsScript vs, uint32_t slot, void *data, size_t len) {
270    Script *s = static_cast<Script *>(vs);
271    s->getVar(slot, data, len);
272}
273
274void rsi_ScriptSetVarVE(Context *rsc, RsScript vs, uint32_t slot,
275                        const void *data, size_t len, RsElement ve,
276                        const uint32_t *dims, size_t dimLen) {
277    Script *s = static_cast<Script *>(vs);
278    Element *e = static_cast<Element *>(ve);
279    s->setVar(slot, data, len, e, dims, dimLen);
280}
281
282}
283}
284