blob: d39fb5e6feaac92de0e10baf2bb5e04cf5f4ba5a [file] [log] [blame]
Jason Sams326e0dd2009-05-22 14:03:28 -07001/*
Stephen Hines44199772012-02-21 20:13:12 -08002 * Copyright (C) 2009-2012 The Android Open Source Project
Jason Sams326e0dd2009-05-22 14:03:28 -07003 *
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 Hinesd2432b92011-11-09 18:02:20 -080018#include <time.h>
Jason Sams326e0dd2009-05-22 14:03:28 -070019
20using namespace android;
21using namespace android::renderscript;
22
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -080023Script::Script(Context *rsc) : ObjectBase(rsc) {
Jason Sams928b7342009-06-08 18:50:13 -070024 memset(&mEnviroment, 0, sizeof(mEnviroment));
Jason Samsbad80742011-03-16 16:29:28 -070025 memset(&mHal, 0, sizeof(mHal));
Alex Sakhartchouk700ba382010-10-08 15:00:05 -070026
27 mSlots = NULL;
28 mTypes = NULL;
Jason Sams77020c52011-11-22 12:49:11 -080029 mInitialized = false;
Jason Sams326e0dd2009-05-22 14:03:28 -070030}
31
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -080032Script::~Script() {
33 if (mSlots) {
Alex Sakhartchouk700ba382010-10-08 15:00:05 -070034 delete [] mSlots;
35 mSlots = NULL;
36 }
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -080037 if (mTypes) {
Alex Sakhartchouk700ba382010-10-08 15:00:05 -070038 delete [] mTypes;
39 mTypes = NULL;
40 }
41}
42
Alex Sakhartchouk700ba382010-10-08 15:00:05 -070043void Script::setSlot(uint32_t slot, Allocation *a) {
Steve Blockaf12ac62012-01-06 19:20:56 +000044 //ALOGE("setSlot %i %p", slot, a);
Jason Samsbad80742011-03-16 16:29:28 -070045 if (slot >= mHal.info.exportedVariableCount) {
Steve Blockaf12ac62012-01-06 19:20:56 +000046 ALOGE("Script::setSlot unable to set allocation, invalid slot index");
Alex Sakhartchouk700ba382010-10-08 15:00:05 -070047 return;
48 }
49
50 mSlots[slot].set(a);
Jason Samsbad80742011-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 Sams326e0dd2009-05-22 14:03:28 -070056}
57
Alex Sakhartchoukb81a0eb2011-06-03 10:18:01 -070058void Script::setVar(uint32_t slot, const void *val, size_t len) {
Steve Blockaf12ac62012-01-06 19:20:56 +000059 //ALOGE("setVar %i %p %i", slot, val, len);
Jason Samsbad80742011-03-16 16:29:28 -070060 if (slot >= mHal.info.exportedVariableCount) {
Steve Blockaf12ac62012-01-06 19:20:56 +000061 ALOGE("Script::setVar unable to set allocation, invalid slot index");
Jason Samsbad80742011-03-16 16:29:28 -070062 return;
Jason Samsbe36bf32010-05-11 14:03:58 -070063 }
Jason Samsbad80742011-03-16 16:29:28 -070064 mRSC->mHal.funcs.script.setGlobalVar(mRSC, this, slot, (void *)val, len);
Jason Samsbe36bf32010-05-11 14:03:58 -070065}
66
Stephen Hines2980f072012-04-09 18:26:29 -070067void Script::setVar(uint32_t slot, const void *val, size_t len, Element *e,
68 const size_t *dims, size_t dimLen) {
69 if (slot >= mHal.info.exportedVariableCount) {
70 ALOGE("Script::setVar unable to set allocation, invalid slot index");
71 return;
72 }
73 mRSC->mHal.funcs.script.setGlobalVarWithElemDims(mRSC, this, slot,
74 (void *)val, len, e, dims, dimLen);
75}
76
Jason Samsa5eb6e12010-11-16 17:37:02 -080077void Script::setVarObj(uint32_t slot, ObjectBase *val) {
Steve Blockaf12ac62012-01-06 19:20:56 +000078 //ALOGE("setVarObj %i %p", slot, val);
Jason Samsbad80742011-03-16 16:29:28 -070079 if (slot >= mHal.info.exportedVariableCount) {
Steve Blockaf12ac62012-01-06 19:20:56 +000080 ALOGE("Script::setVarObj unable to set allocation, invalid slot index");
Jason Samsbad80742011-03-16 16:29:28 -070081 return;
Jason Samsa5eb6e12010-11-16 17:37:02 -080082 }
Steve Blockaf12ac62012-01-06 19:20:56 +000083 //ALOGE("setvarobj %i %p", slot, val);
Jason Samsbad80742011-03-16 16:29:28 -070084 mRSC->mHal.funcs.script.setGlobalObj(mRSC, this, slot, val);
Jason Samsa5eb6e12010-11-16 17:37:02 -080085}
86
Stephen Hines4ee16ff2011-08-31 17:41:39 -070087bool Script::freeChildren() {
88 incSysRef();
89 mRSC->mHal.funcs.script.invokeFreeChildren(mRSC, this);
90 return decSysRef();
91}
92
Jason Sams326e0dd2009-05-22 14:03:28 -070093namespace android {
94namespace renderscript {
95
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -080096void rsi_ScriptBindAllocation(Context * rsc, RsScript vs, RsAllocation va, uint32_t slot) {
Jason Sams326e0dd2009-05-22 14:03:28 -070097 Script *s = static_cast<Script *>(vs);
Jason Samsbe36bf32010-05-11 14:03:58 -070098 Allocation *a = static_cast<Allocation *>(va);
Alex Sakhartchouk700ba382010-10-08 15:00:05 -070099 s->setSlot(slot, a);
Steve Blockaf12ac62012-01-06 19:20:56 +0000100 //ALOGE("rsi_ScriptBindAllocation %i %p %p", slot, a, a->getPtr());
Jason Sams326e0dd2009-05-22 14:03:28 -0700101}
102
Alex Sakhartchoukb81a0eb2011-06-03 10:18:01 -0700103void rsi_ScriptSetTimeZone(Context * rsc, RsScript vs, const char * timeZone, size_t length) {
Stephen Hinesd2432b92011-11-09 18:02:20 -0800104 // We unfortunately need to make a new copy of the string, since it is
105 // not NULL-terminated. We then use setenv(), which properly handles
106 // freeing/duplicating the actual string for the environment.
107 char *tz = (char *) malloc(length + 1);
108 if (!tz) {
Steve Blockaf12ac62012-01-06 19:20:56 +0000109 ALOGE("Couldn't allocate memory for timezone buffer");
Stephen Hinesd2432b92011-11-09 18:02:20 -0800110 return;
111 }
112 strncpy(tz, timeZone, length);
113 tz[length] = '\0';
114 if (setenv("TZ", tz, 1) == 0) {
115 tzset();
116 } else {
Steve Blockaf12ac62012-01-06 19:20:56 +0000117 ALOGE("Error setting timezone");
Stephen Hinesd2432b92011-11-09 18:02:20 -0800118 }
119 free(tz);
Jason Samsd34b7252009-08-04 16:58:20 -0700120}
121
Jason Sams5fb1aeb2011-04-27 15:12:49 -0700122void rsi_ScriptForEach(Context *rsc, RsScript vs, uint32_t slot,
123 RsAllocation vain, RsAllocation vaout,
Alex Sakhartchoukb81a0eb2011-06-03 10:18:01 -0700124 const void *params, size_t paramLen) {
Jason Sams5fb1aeb2011-04-27 15:12:49 -0700125 Script *s = static_cast<Script *>(vs);
Stephen Hines44199772012-02-21 20:13:12 -0800126 s->runForEach(rsc, slot,
Jason Sams5fb1aeb2011-04-27 15:12:49 -0700127 static_cast<const Allocation *>(vain), static_cast<Allocation *>(vaout),
128 params, paramLen);
129
130}
131
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800132void rsi_ScriptInvoke(Context *rsc, RsScript vs, uint32_t slot) {
Jason Sams8c6bc692009-09-16 15:04:38 -0700133 Script *s = static_cast<Script *>(vs);
Jason Sams22fa3712010-05-19 17:22:57 -0700134 s->Invoke(rsc, slot, NULL, 0);
Jason Sams8c6bc692009-09-16 15:04:38 -0700135}
136
137
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800138void rsi_ScriptInvokeData(Context *rsc, RsScript vs, uint32_t slot, void *data) {
Jason Samsbe36bf32010-05-11 14:03:58 -0700139 Script *s = static_cast<Script *>(vs);
Jason Sams22fa3712010-05-19 17:22:57 -0700140 s->Invoke(rsc, slot, NULL, 0);
Jason Samsbe36bf32010-05-11 14:03:58 -0700141}
142
Alex Sakhartchoukb81a0eb2011-06-03 10:18:01 -0700143void rsi_ScriptInvokeV(Context *rsc, RsScript vs, uint32_t slot, const void *data, size_t len) {
Jason Samsbe36bf32010-05-11 14:03:58 -0700144 Script *s = static_cast<Script *>(vs);
Jason Sams22fa3712010-05-19 17:22:57 -0700145 s->Invoke(rsc, slot, data, len);
Jason Samsbe36bf32010-05-11 14:03:58 -0700146}
147
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800148void rsi_ScriptSetVarI(Context *rsc, RsScript vs, uint32_t slot, int value) {
Jason Samsbe36bf32010-05-11 14:03:58 -0700149 Script *s = static_cast<Script *>(vs);
150 s->setVar(slot, &value, sizeof(value));
151}
152
Jason Samsa5eb6e12010-11-16 17:37:02 -0800153void rsi_ScriptSetVarObj(Context *rsc, RsScript vs, uint32_t slot, RsObjectBase value) {
154 Script *s = static_cast<Script *>(vs);
155 ObjectBase *o = static_cast<ObjectBase *>(value);
156 s->setVarObj(slot, o);
157}
158
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800159void rsi_ScriptSetVarJ(Context *rsc, RsScript vs, uint32_t slot, long long value) {
Stephen Hines0977c942010-10-11 10:54:21 -0700160 Script *s = static_cast<Script *>(vs);
161 s->setVar(slot, &value, sizeof(value));
162}
163
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800164void rsi_ScriptSetVarF(Context *rsc, RsScript vs, uint32_t slot, float value) {
Jason Samsbe36bf32010-05-11 14:03:58 -0700165 Script *s = static_cast<Script *>(vs);
166 s->setVar(slot, &value, sizeof(value));
167}
168
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800169void rsi_ScriptSetVarD(Context *rsc, RsScript vs, uint32_t slot, double value) {
Stephen Hines6d0a0742010-09-20 17:20:30 -0700170 Script *s = static_cast<Script *>(vs);
171 s->setVar(slot, &value, sizeof(value));
172}
173
Alex Sakhartchoukb81a0eb2011-06-03 10:18:01 -0700174void rsi_ScriptSetVarV(Context *rsc, RsScript vs, uint32_t slot, const void *data, size_t len) {
Jason Samsbe36bf32010-05-11 14:03:58 -0700175 Script *s = static_cast<Script *>(vs);
176 s->setVar(slot, data, len);
Jason Samsfa517192009-08-13 12:59:04 -0700177}
178
Stephen Hines2980f072012-04-09 18:26:29 -0700179void rsi_ScriptSetVarVE(Context *rsc, RsScript vs, uint32_t slot,
180 const void *data, size_t len, RsElement ve,
181 const size_t *dims, size_t dimLen) {
182 Script *s = static_cast<Script *>(vs);
183 Element *e = static_cast<Element *>(ve);
184 s->setVar(slot, data, len, e, dims, dimLen);
185}
186
Jason Sams326e0dd2009-05-22 14:03:28 -0700187}
188}
189