rsScript.cpp revision ac8d146a41f18afad5314ac8af440d6aedbe20bf
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 = NULL;
28    mTypes = NULL;
29    mInitialized = false;
30    mHasObjectSlots = false;
31}
32
33Script::~Script() {
34    if (mSlots) {
35        delete [] mSlots;
36        mSlots = NULL;
37    }
38    if (mTypes) {
39        delete [] mTypes;
40        mTypes = NULL;
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 size_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    //ALOGE("setvarobj  %i %p", slot, val);
95    mRSC->mHal.funcs.script.setGlobalObj(mRSC, this, slot, val);
96}
97
98bool Script::freeChildren() {
99    incSysRef();
100    mRSC->mHal.funcs.script.invokeFreeChildren(mRSC, this);
101    return decSysRef();
102}
103
104ScriptKernelID::ScriptKernelID(Context *rsc, Script *s, int slot, int sig)
105        : ObjectBase(rsc) {
106
107    mScript = s;
108    mSlot = slot;
109    mHasKernelInput = (sig & 1) != 0;
110    mHasKernelOutput = (sig & 2) != 0;
111}
112
113ScriptKernelID::~ScriptKernelID() {
114
115}
116
117void ScriptKernelID::serialize(Context *rsc, OStream *stream) const {
118
119}
120
121RsA3DClassID ScriptKernelID::getClassId() const {
122    return RS_A3D_CLASS_ID_SCRIPT_KERNEL_ID;
123}
124
125ScriptFieldID::ScriptFieldID(Context *rsc, Script *s, int slot) : ObjectBase(rsc) {
126    mScript = s;
127    mSlot = slot;
128}
129
130ScriptFieldID::~ScriptFieldID() {
131
132}
133
134void ScriptFieldID::serialize(Context *rsc, OStream *stream) const {
135
136}
137
138RsA3DClassID ScriptFieldID::getClassId() const {
139    return RS_A3D_CLASS_ID_SCRIPT_FIELD_ID;
140}
141
142
143namespace android {
144namespace renderscript {
145
146RsScriptKernelID rsi_ScriptKernelIDCreate(Context *rsc, RsScript vs, int slot, int sig) {
147    ScriptKernelID *kid = new ScriptKernelID(rsc, (Script *)vs, slot, sig);
148    kid->incUserRef();
149    return kid;
150}
151
152RsScriptFieldID rsi_ScriptFieldIDCreate(Context *rsc, RsScript vs, int slot) {
153    ScriptFieldID *fid = new ScriptFieldID(rsc, (Script *)vs, slot);
154    fid->incUserRef();
155    return fid;
156}
157
158void rsi_ScriptBindAllocation(Context * rsc, RsScript vs, RsAllocation va, uint32_t slot) {
159    Script *s = static_cast<Script *>(vs);
160    Allocation *a = static_cast<Allocation *>(va);
161    s->setSlot(slot, a);
162}
163
164void rsi_ScriptSetTimeZone(Context * rsc, RsScript vs, const char * timeZone, size_t length) {
165    // We unfortunately need to make a new copy of the string, since it is
166    // not NULL-terminated. We then use setenv(), which properly handles
167    // freeing/duplicating the actual string for the environment.
168    char *tz = (char *) malloc(length + 1);
169    if (!tz) {
170        ALOGE("Couldn't allocate memory for timezone buffer");
171        return;
172    }
173    strncpy(tz, timeZone, length);
174    tz[length] = '\0';
175    if (setenv("TZ", tz, 1) == 0) {
176        tzset();
177    } else {
178        ALOGE("Error setting timezone");
179    }
180    free(tz);
181}
182
183void rsi_ScriptForEach(Context *rsc, RsScript vs, uint32_t slot,
184                       RsAllocation vain, RsAllocation vaout,
185                       const void *params, size_t paramLen,
186                       const RsScriptCall *sc, size_t scLen) {
187    Script *s = static_cast<Script *>(vs);
188    // The rs.spec generated code does not handle the absence of an actual
189    // input for sc. Instead, it retains an existing pointer value (the prior
190    // field in the packed data object). This can cause confusion because
191    // drivers might now inspect bogus sc data.
192    if (scLen == 0) {
193        sc = NULL;
194    }
195    s->runForEach(rsc, slot,
196                  static_cast<const Allocation *>(vain), static_cast<Allocation *>(vaout),
197                  params, paramLen, sc);
198
199}
200
201void rsi_ScriptInvoke(Context *rsc, RsScript vs, uint32_t slot) {
202    Script *s = static_cast<Script *>(vs);
203    s->Invoke(rsc, slot, NULL, 0);
204}
205
206
207void rsi_ScriptInvokeData(Context *rsc, RsScript vs, uint32_t slot, void *data) {
208    Script *s = static_cast<Script *>(vs);
209    s->Invoke(rsc, slot, NULL, 0);
210}
211
212void rsi_ScriptInvokeV(Context *rsc, RsScript vs, uint32_t slot, const void *data, size_t len) {
213    Script *s = static_cast<Script *>(vs);
214    s->Invoke(rsc, slot, data, len);
215}
216
217void rsi_ScriptSetVarI(Context *rsc, RsScript vs, uint32_t slot, int value) {
218    Script *s = static_cast<Script *>(vs);
219    s->setVar(slot, &value, sizeof(value));
220}
221
222void rsi_ScriptSetVarObj(Context *rsc, RsScript vs, uint32_t slot, RsObjectBase value) {
223    Script *s = static_cast<Script *>(vs);
224    ObjectBase *o = static_cast<ObjectBase *>(value);
225    s->setVarObj(slot, o);
226}
227
228void rsi_ScriptSetVarJ(Context *rsc, RsScript vs, uint32_t slot, int64_t value) {
229    Script *s = static_cast<Script *>(vs);
230    s->setVar(slot, &value, sizeof(value));
231}
232
233void rsi_ScriptSetVarF(Context *rsc, RsScript vs, uint32_t slot, float value) {
234    Script *s = static_cast<Script *>(vs);
235    s->setVar(slot, &value, sizeof(value));
236}
237
238void rsi_ScriptSetVarD(Context *rsc, RsScript vs, uint32_t slot, double value) {
239    Script *s = static_cast<Script *>(vs);
240    s->setVar(slot, &value, sizeof(value));
241}
242
243void rsi_ScriptSetVarV(Context *rsc, RsScript vs, uint32_t slot, const void *data, size_t len) {
244    Script *s = static_cast<Script *>(vs);
245    s->setVar(slot, data, len);
246}
247
248void rsi_ScriptGetVarV(Context *rsc, RsScript vs, uint32_t slot, void *data, size_t len) {
249    Script *s = static_cast<Script *>(vs);
250    s->getVar(slot, data, len);
251}
252
253void rsi_ScriptSetVarVE(Context *rsc, RsScript vs, uint32_t slot,
254                        const void *data, size_t len, RsElement ve,
255                        const uint32_t *dims, size_t dimLen) {
256    Script *s = static_cast<Script *>(vs);
257    Element *e = static_cast<Element *>(ve);
258    s->setVar(slot, data, len, e, dims, dimLen);
259}
260
261}
262}
263
264