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