rsScript.cpp revision 0db650c7aff639f24e9c6a651abd4539cb943609
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
89namespace android {
90namespace renderscript {
91
92void rsi_ScriptBindAllocation(Context * rsc, RsScript vs, RsAllocation va, uint32_t slot) {
93    Script *s = static_cast<Script *>(vs);
94    Allocation *a = static_cast<Allocation *>(va);
95    s->setSlot(slot, a);
96}
97
98void rsi_ScriptSetTimeZone(Context * rsc, RsScript vs, const char * timeZone, size_t length) {
99    // We unfortunately need to make a new copy of the string, since it is
100    // not NULL-terminated. We then use setenv(), which properly handles
101    // freeing/duplicating the actual string for the environment.
102    char *tz = (char *) malloc(length + 1);
103    if (!tz) {
104        ALOGE("Couldn't allocate memory for timezone buffer");
105        return;
106    }
107    strncpy(tz, timeZone, length);
108    tz[length] = '\0';
109    if (setenv("TZ", tz, 1) == 0) {
110        tzset();
111    } else {
112        ALOGE("Error setting timezone");
113    }
114    free(tz);
115}
116
117void rsi_ScriptForEach(Context *rsc, RsScript vs, uint32_t slot,
118                       RsAllocation vain, RsAllocation vaout,
119                       const void *params, size_t paramLen) {
120    Script *s = static_cast<Script *>(vs);
121    s->runForEach(rsc, slot,
122                  static_cast<const Allocation *>(vain), static_cast<Allocation *>(vaout),
123                  params, paramLen);
124
125}
126
127void rsi_ScriptInvoke(Context *rsc, RsScript vs, uint32_t slot) {
128    Script *s = static_cast<Script *>(vs);
129    s->Invoke(rsc, slot, NULL, 0);
130}
131
132
133void rsi_ScriptInvokeData(Context *rsc, RsScript vs, uint32_t slot, void *data) {
134    Script *s = static_cast<Script *>(vs);
135    s->Invoke(rsc, slot, NULL, 0);
136}
137
138void rsi_ScriptInvokeV(Context *rsc, RsScript vs, uint32_t slot, const void *data, size_t len) {
139    Script *s = static_cast<Script *>(vs);
140    s->Invoke(rsc, slot, data, len);
141}
142
143void rsi_ScriptSetVarI(Context *rsc, RsScript vs, uint32_t slot, int value) {
144    Script *s = static_cast<Script *>(vs);
145    s->setVar(slot, &value, sizeof(value));
146}
147
148void rsi_ScriptSetVarObj(Context *rsc, RsScript vs, uint32_t slot, RsObjectBase value) {
149    Script *s = static_cast<Script *>(vs);
150    ObjectBase *o = static_cast<ObjectBase *>(value);
151    s->setVarObj(slot, o);
152}
153
154void rsi_ScriptSetVarJ(Context *rsc, RsScript vs, uint32_t slot, long long value) {
155    Script *s = static_cast<Script *>(vs);
156    s->setVar(slot, &value, sizeof(value));
157}
158
159void rsi_ScriptSetVarF(Context *rsc, RsScript vs, uint32_t slot, float value) {
160    Script *s = static_cast<Script *>(vs);
161    s->setVar(slot, &value, sizeof(value));
162}
163
164void rsi_ScriptSetVarD(Context *rsc, RsScript vs, uint32_t slot, double value) {
165    Script *s = static_cast<Script *>(vs);
166    s->setVar(slot, &value, sizeof(value));
167}
168
169void rsi_ScriptSetVarV(Context *rsc, RsScript vs, uint32_t slot, const void *data, size_t len) {
170    Script *s = static_cast<Script *>(vs);
171    s->setVar(slot, data, len);
172}
173
174void rsi_ScriptSetVarVE(Context *rsc, RsScript vs, uint32_t slot,
175                        const void *data, size_t len, RsElement ve,
176                        const size_t *dims, size_t dimLen) {
177    Script *s = static_cast<Script *>(vs);
178    Element *e = static_cast<Element *>(ve);
179    s->setVar(slot, data, len, e, dims, dimLen);
180}
181
182}
183}
184
185