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