blob: 16446ddd5c6c41e64d8aaff0f79603a9841d3748 [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 Samsd19f10d2009-05-22 14:03:28 -070029}
30
Alex Sakhartchouked9f2102010-11-09 17:00:54 -080031Script::~Script() {
32 if (mSlots) {
Alex Sakhartchouk6f91cb62010-10-08 15:00:05 -070033 delete [] mSlots;
34 mSlots = NULL;
35 }
Alex Sakhartchouked9f2102010-11-09 17:00:54 -080036 if (mTypes) {
Alex Sakhartchouk6f91cb62010-10-08 15:00:05 -070037 delete [] mTypes;
38 mTypes = NULL;
39 }
40}
41
Alex Sakhartchouk6f91cb62010-10-08 15:00:05 -070042void Script::setSlot(uint32_t slot, Allocation *a) {
Jason Samse4a06c52011-03-16 16:29:28 -070043 //LOGE("setSlot %i %p", slot, a);
44 if (slot >= mHal.info.exportedVariableCount) {
Alex Sakhartchouk6f91cb62010-10-08 15:00:05 -070045 LOGE("Script::setSlot unable to set allocation, invalid slot index");
46 return;
47 }
48
49 mSlots[slot].set(a);
Jason Samse4a06c52011-03-16 16:29:28 -070050 if (a != NULL) {
51 mRSC->mHal.funcs.script.setGlobalBind(mRSC, this, slot, a->getPtr());
52 } else {
53 mRSC->mHal.funcs.script.setGlobalBind(mRSC, this, slot, NULL);
54 }
Jason Samsd19f10d2009-05-22 14:03:28 -070055}
56
Alex Sakhartchouk17a8a192011-06-03 10:18:01 -070057void Script::setVar(uint32_t slot, const void *val, size_t len) {
Jason Samse4a06c52011-03-16 16:29:28 -070058 //LOGE("setVar %i %p %i", slot, val, len);
59 if (slot >= mHal.info.exportedVariableCount) {
60 LOGE("Script::setVar unable to set allocation, invalid slot index");
61 return;
Jason Sams4d339932010-05-11 14:03:58 -070062 }
Jason Samse4a06c52011-03-16 16:29:28 -070063 mRSC->mHal.funcs.script.setGlobalVar(mRSC, this, slot, (void *)val, len);
Jason Sams4d339932010-05-11 14:03:58 -070064}
65
Jason Sams6f4cf0b2010-11-16 17:37:02 -080066void Script::setVarObj(uint32_t slot, ObjectBase *val) {
Jason Samse4a06c52011-03-16 16:29:28 -070067 //LOGE("setVarObj %i %p", slot, val);
68 if (slot >= mHal.info.exportedVariableCount) {
69 LOGE("Script::setVarObj unable to set allocation, invalid slot index");
70 return;
Jason Sams6f4cf0b2010-11-16 17:37:02 -080071 }
Jason Samse4a06c52011-03-16 16:29:28 -070072 //LOGE("setvarobj %i %p", slot, val);
73 mRSC->mHal.funcs.script.setGlobalObj(mRSC, this, slot, val);
Jason Sams6f4cf0b2010-11-16 17:37:02 -080074}
75
Stephen Hines514f9792011-08-31 17:41:39 -070076bool Script::freeChildren() {
77 incSysRef();
78 mRSC->mHal.funcs.script.invokeFreeChildren(mRSC, this);
79 return decSysRef();
80}
81
Jason Samsd19f10d2009-05-22 14:03:28 -070082namespace android {
83namespace renderscript {
84
Alex Sakhartchouked9f2102010-11-09 17:00:54 -080085void rsi_ScriptBindAllocation(Context * rsc, RsScript vs, RsAllocation va, uint32_t slot) {
Jason Samsd19f10d2009-05-22 14:03:28 -070086 Script *s = static_cast<Script *>(vs);
Jason Sams4d339932010-05-11 14:03:58 -070087 Allocation *a = static_cast<Allocation *>(va);
Alex Sakhartchouk6f91cb62010-10-08 15:00:05 -070088 s->setSlot(slot, a);
Jason Sams4d339932010-05-11 14:03:58 -070089 //LOGE("rsi_ScriptBindAllocation %i %p %p", slot, a, a->getPtr());
Jason Samsd19f10d2009-05-22 14:03:28 -070090}
91
Alex Sakhartchouk17a8a192011-06-03 10:18:01 -070092void rsi_ScriptSetTimeZone(Context * rsc, RsScript vs, const char * timeZone, size_t length) {
Stephen Hinesd2f561d2011-11-09 18:02:20 -080093 // We unfortunately need to make a new copy of the string, since it is
94 // not NULL-terminated. We then use setenv(), which properly handles
95 // freeing/duplicating the actual string for the environment.
96 char *tz = (char *) malloc(length + 1);
97 if (!tz) {
98 LOGE("Couldn't allocate memory for timezone buffer");
99 return;
100 }
101 strncpy(tz, timeZone, length);
102 tz[length] = '\0';
103 if (setenv("TZ", tz, 1) == 0) {
104 tzset();
105 } else {
106 LOGE("Error setting timezone");
107 }
108 free(tz);
Jason Sams22534172009-08-04 16:58:20 -0700109}
110
Jason Samsa08526a2011-04-27 15:12:49 -0700111void rsi_ScriptForEach(Context *rsc, RsScript vs, uint32_t slot,
112 RsAllocation vain, RsAllocation vaout,
Alex Sakhartchouk17a8a192011-06-03 10:18:01 -0700113 const void *params, size_t paramLen) {
Jason Samsa08526a2011-04-27 15:12:49 -0700114 Script *s = static_cast<Script *>(vs);
115 s->runForEach(rsc,
116 static_cast<const Allocation *>(vain), static_cast<Allocation *>(vaout),
117 params, paramLen);
118
119}
120
Alex Sakhartchouked9f2102010-11-09 17:00:54 -0800121void rsi_ScriptInvoke(Context *rsc, RsScript vs, uint32_t slot) {
Jason Samsbe2e8412009-09-16 15:04:38 -0700122 Script *s = static_cast<Script *>(vs);
Jason Samsd79b2e92010-05-19 17:22:57 -0700123 s->Invoke(rsc, slot, NULL, 0);
Jason Samsbe2e8412009-09-16 15:04:38 -0700124}
125
126
Alex Sakhartchouked9f2102010-11-09 17:00:54 -0800127void rsi_ScriptInvokeData(Context *rsc, RsScript vs, uint32_t slot, void *data) {
Jason Sams4d339932010-05-11 14:03:58 -0700128 Script *s = static_cast<Script *>(vs);
Jason Samsd79b2e92010-05-19 17:22:57 -0700129 s->Invoke(rsc, slot, NULL, 0);
Jason Sams4d339932010-05-11 14:03:58 -0700130}
131
Alex Sakhartchouk17a8a192011-06-03 10:18:01 -0700132void rsi_ScriptInvokeV(Context *rsc, RsScript vs, uint32_t slot, const void *data, size_t len) {
Jason Sams4d339932010-05-11 14:03:58 -0700133 Script *s = static_cast<Script *>(vs);
Jason Samsd79b2e92010-05-19 17:22:57 -0700134 s->Invoke(rsc, slot, data, len);
Jason Sams4d339932010-05-11 14:03:58 -0700135}
136
Alex Sakhartchouked9f2102010-11-09 17:00:54 -0800137void rsi_ScriptSetVarI(Context *rsc, RsScript vs, uint32_t slot, int value) {
Jason Sams4d339932010-05-11 14:03:58 -0700138 Script *s = static_cast<Script *>(vs);
139 s->setVar(slot, &value, sizeof(value));
140}
141
Jason Sams6f4cf0b2010-11-16 17:37:02 -0800142void rsi_ScriptSetVarObj(Context *rsc, RsScript vs, uint32_t slot, RsObjectBase value) {
143 Script *s = static_cast<Script *>(vs);
144 ObjectBase *o = static_cast<ObjectBase *>(value);
145 s->setVarObj(slot, o);
146}
147
Alex Sakhartchouked9f2102010-11-09 17:00:54 -0800148void rsi_ScriptSetVarJ(Context *rsc, RsScript vs, uint32_t slot, long long value) {
Stephen Hines031ec58c2010-10-11 10:54:21 -0700149 Script *s = static_cast<Script *>(vs);
150 s->setVar(slot, &value, sizeof(value));
151}
152
Alex Sakhartchouked9f2102010-11-09 17:00:54 -0800153void rsi_ScriptSetVarF(Context *rsc, RsScript vs, uint32_t slot, float value) {
Jason Sams4d339932010-05-11 14:03:58 -0700154 Script *s = static_cast<Script *>(vs);
155 s->setVar(slot, &value, sizeof(value));
156}
157
Alex Sakhartchouked9f2102010-11-09 17:00:54 -0800158void rsi_ScriptSetVarD(Context *rsc, RsScript vs, uint32_t slot, double value) {
Stephen Hinesca54ec32010-09-20 17:20:30 -0700159 Script *s = static_cast<Script *>(vs);
160 s->setVar(slot, &value, sizeof(value));
161}
162
Alex Sakhartchouk17a8a192011-06-03 10:18:01 -0700163void rsi_ScriptSetVarV(Context *rsc, RsScript vs, uint32_t slot, const void *data, size_t len) {
Jason Sams4d339932010-05-11 14:03:58 -0700164 Script *s = static_cast<Script *>(vs);
165 s->setVar(slot, data, len);
Jason Samsfbf0b9e2009-08-13 12:59:04 -0700166}
167
Jason Samsd19f10d2009-05-22 14:03:28 -0700168}
169}
170