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    return new ScriptKernelID(rsc, (Script *)vs, slot, sig);
133}
134
135RsScriptFieldID rsi_ScriptFieldIDCreate(Context *rsc, RsScript vs, int slot) {
136    return new ScriptFieldID(rsc, (Script *)vs, slot);
137}
138
139void 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
145void 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
164void 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
174void 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
180void 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
185void 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
190void 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
195void 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
201void 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
206void 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
211void 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
216void 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
221void 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