blob: 7fc128e8d243046fffefea5a9bf69a4c65c920ef [file] [log] [blame]
Jason Samsd19f10d2009-05-22 14:03:28 -07001/*
2 * Copyright (C) 2009 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"
Stephen Hinesd2f561d2011-11-09 18:02:20 -080018#include <time.h>
Jason Samsd19f10d2009-05-22 14:03:28 -070019
20using namespace android;
21using namespace android::renderscript;
22
Alex Sakhartchouked9f2102010-11-09 17:00:54 -080023Script::Script(Context *rsc) : ObjectBase(rsc) {
Jason Sams928f5cf2009-06-08 18:50:13 -070024 memset(&mEnviroment, 0, sizeof(mEnviroment));
Jason Samse4a06c52011-03-16 16:29:28 -070025 memset(&mHal, 0, sizeof(mHal));
Alex Sakhartchouk6f91cb62010-10-08 15:00:05 -070026
27 mSlots = NULL;
28 mTypes = NULL;
Jason Samsdd39fdf2011-11-22 12:49:11 -080029 mInitialized = false;
Jason Samsd19f10d2009-05-22 14:03:28 -070030}
31
Alex Sakhartchouked9f2102010-11-09 17:00:54 -080032Script::~Script() {
33 if (mSlots) {
Alex Sakhartchouk6f91cb62010-10-08 15:00:05 -070034 delete [] mSlots;
35 mSlots = NULL;
36 }
Alex Sakhartchouked9f2102010-11-09 17:00:54 -080037 if (mTypes) {
Alex Sakhartchouk6f91cb62010-10-08 15:00:05 -070038 delete [] mTypes;
39 mTypes = NULL;
40 }
41}
42
Alex Sakhartchouk6f91cb62010-10-08 15:00:05 -070043void Script::setSlot(uint32_t slot, Allocation *a) {
Jason Samse4a06c52011-03-16 16:29:28 -070044 //LOGE("setSlot %i %p", slot, a);
45 if (slot >= mHal.info.exportedVariableCount) {
Alex Sakhartchouk6f91cb62010-10-08 15:00:05 -070046 LOGE("Script::setSlot unable to set allocation, invalid slot index");
47 return;
48 }
49
50 mSlots[slot].set(a);
Jason Samse4a06c52011-03-16 16:29:28 -070051 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 }
Jason Samsd19f10d2009-05-22 14:03:28 -070056}
57
Alex Sakhartchouk17a8a192011-06-03 10:18:01 -070058void Script::setVar(uint32_t slot, const void *val, size_t len) {
Jason Samse4a06c52011-03-16 16:29:28 -070059 //LOGE("setVar %i %p %i", slot, val, len);
60 if (slot >= mHal.info.exportedVariableCount) {
61 LOGE("Script::setVar unable to set allocation, invalid slot index");
62 return;
Jason Sams4d339932010-05-11 14:03:58 -070063 }
Jason Samse4a06c52011-03-16 16:29:28 -070064 mRSC->mHal.funcs.script.setGlobalVar(mRSC, this, slot, (void *)val, len);
Jason Sams4d339932010-05-11 14:03:58 -070065}
66
Jason Sams6f4cf0b2010-11-16 17:37:02 -080067void Script::setVarObj(uint32_t slot, ObjectBase *val) {
Jason Samse4a06c52011-03-16 16:29:28 -070068 //LOGE("setVarObj %i %p", slot, val);
69 if (slot >= mHal.info.exportedVariableCount) {
70 LOGE("Script::setVarObj unable to set allocation, invalid slot index");
71 return;
Jason Sams6f4cf0b2010-11-16 17:37:02 -080072 }
Jason Samse4a06c52011-03-16 16:29:28 -070073 //LOGE("setvarobj %i %p", slot, val);
74 mRSC->mHal.funcs.script.setGlobalObj(mRSC, this, slot, val);
Jason Sams6f4cf0b2010-11-16 17:37:02 -080075}
76
Stephen Hines514f9792011-08-31 17:41:39 -070077bool Script::freeChildren() {
78 incSysRef();
79 mRSC->mHal.funcs.script.invokeFreeChildren(mRSC, this);
80 return decSysRef();
81}
82
Jason Samsd19f10d2009-05-22 14:03:28 -070083namespace android {
84namespace renderscript {
85
Alex Sakhartchouked9f2102010-11-09 17:00:54 -080086void rsi_ScriptBindAllocation(Context * rsc, RsScript vs, RsAllocation va, uint32_t slot) {
Jason Samsd19f10d2009-05-22 14:03:28 -070087 Script *s = static_cast<Script *>(vs);
Jason Sams4d339932010-05-11 14:03:58 -070088 Allocation *a = static_cast<Allocation *>(va);
Alex Sakhartchouk6f91cb62010-10-08 15:00:05 -070089 s->setSlot(slot, a);
Jason Sams4d339932010-05-11 14:03:58 -070090 //LOGE("rsi_ScriptBindAllocation %i %p %p", slot, a, a->getPtr());
Jason Samsd19f10d2009-05-22 14:03:28 -070091}
92
Alex Sakhartchouk17a8a192011-06-03 10:18:01 -070093void rsi_ScriptSetTimeZone(Context * rsc, RsScript vs, const char * timeZone, size_t length) {
Stephen Hinesd2f561d2011-11-09 18:02:20 -080094 // 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 LOGE("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 LOGE("Error setting timezone");
108 }
109 free(tz);
Jason Sams22534172009-08-04 16:58:20 -0700110}
111
Jason Samsa08526a2011-04-27 15:12:49 -0700112void rsi_ScriptForEach(Context *rsc, RsScript vs, uint32_t slot,
113 RsAllocation vain, RsAllocation vaout,
Alex Sakhartchouk17a8a192011-06-03 10:18:01 -0700114 const void *params, size_t paramLen) {
Jason Samsa08526a2011-04-27 15:12:49 -0700115 Script *s = static_cast<Script *>(vs);
116 s->runForEach(rsc,
117 static_cast<const Allocation *>(vain), static_cast<Allocation *>(vaout),
118 params, paramLen);
119
120}
121
Alex Sakhartchouked9f2102010-11-09 17:00:54 -0800122void rsi_ScriptInvoke(Context *rsc, RsScript vs, uint32_t slot) {
Jason Samsbe2e8412009-09-16 15:04:38 -0700123 Script *s = static_cast<Script *>(vs);
Jason Samsd79b2e92010-05-19 17:22:57 -0700124 s->Invoke(rsc, slot, NULL, 0);
Jason Samsbe2e8412009-09-16 15:04:38 -0700125}
126
127
Alex Sakhartchouked9f2102010-11-09 17:00:54 -0800128void rsi_ScriptInvokeData(Context *rsc, RsScript vs, uint32_t slot, void *data) {
Jason Sams4d339932010-05-11 14:03:58 -0700129 Script *s = static_cast<Script *>(vs);
Jason Samsd79b2e92010-05-19 17:22:57 -0700130 s->Invoke(rsc, slot, NULL, 0);
Jason Sams4d339932010-05-11 14:03:58 -0700131}
132
Alex Sakhartchouk17a8a192011-06-03 10:18:01 -0700133void rsi_ScriptInvokeV(Context *rsc, RsScript vs, uint32_t slot, const void *data, size_t len) {
Jason Sams4d339932010-05-11 14:03:58 -0700134 Script *s = static_cast<Script *>(vs);
Jason Samsd79b2e92010-05-19 17:22:57 -0700135 s->Invoke(rsc, slot, data, len);
Jason Sams4d339932010-05-11 14:03:58 -0700136}
137
Alex Sakhartchouked9f2102010-11-09 17:00:54 -0800138void rsi_ScriptSetVarI(Context *rsc, RsScript vs, uint32_t slot, int value) {
Jason Sams4d339932010-05-11 14:03:58 -0700139 Script *s = static_cast<Script *>(vs);
140 s->setVar(slot, &value, sizeof(value));
141}
142
Jason Sams6f4cf0b2010-11-16 17:37:02 -0800143void 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
Alex Sakhartchouked9f2102010-11-09 17:00:54 -0800149void rsi_ScriptSetVarJ(Context *rsc, RsScript vs, uint32_t slot, long long value) {
Stephen Hines031ec58c2010-10-11 10:54:21 -0700150 Script *s = static_cast<Script *>(vs);
151 s->setVar(slot, &value, sizeof(value));
152}
153
Alex Sakhartchouked9f2102010-11-09 17:00:54 -0800154void rsi_ScriptSetVarF(Context *rsc, RsScript vs, uint32_t slot, float value) {
Jason Sams4d339932010-05-11 14:03:58 -0700155 Script *s = static_cast<Script *>(vs);
156 s->setVar(slot, &value, sizeof(value));
157}
158
Alex Sakhartchouked9f2102010-11-09 17:00:54 -0800159void rsi_ScriptSetVarD(Context *rsc, RsScript vs, uint32_t slot, double value) {
Stephen Hinesca54ec32010-09-20 17:20:30 -0700160 Script *s = static_cast<Script *>(vs);
161 s->setVar(slot, &value, sizeof(value));
162}
163
Alex Sakhartchouk17a8a192011-06-03 10:18:01 -0700164void rsi_ScriptSetVarV(Context *rsc, RsScript vs, uint32_t slot, const void *data, size_t len) {
Jason Sams4d339932010-05-11 14:03:58 -0700165 Script *s = static_cast<Script *>(vs);
166 s->setVar(slot, data, len);
Jason Samsfbf0b9e2009-08-13 12:59:04 -0700167}
168
Jason Samsd19f10d2009-05-22 14:03:28 -0700169}
170}
171