blob: 6a3bd4bf7a08f06c9a89ba221a4b9b99823a68cd [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
Jason Samsa5eb6e12010-11-16 17:37:02 -080067void Script::setVarObj(uint32_t slot, ObjectBase *val) {
Steve Blockaf12ac62012-01-06 19:20:56 +000068 //ALOGE("setVarObj %i %p", slot, val);
Jason Samsbad80742011-03-16 16:29:28 -070069 if (slot >= mHal.info.exportedVariableCount) {
Steve Blockaf12ac62012-01-06 19:20:56 +000070 ALOGE("Script::setVarObj unable to set allocation, invalid slot index");
Jason Samsbad80742011-03-16 16:29:28 -070071 return;
Jason Samsa5eb6e12010-11-16 17:37:02 -080072 }
Steve Blockaf12ac62012-01-06 19:20:56 +000073 //ALOGE("setvarobj %i %p", slot, val);
Jason Samsbad80742011-03-16 16:29:28 -070074 mRSC->mHal.funcs.script.setGlobalObj(mRSC, this, slot, val);
Jason Samsa5eb6e12010-11-16 17:37:02 -080075}
76
Stephen Hines4ee16ff2011-08-31 17:41:39 -070077bool Script::freeChildren() {
78 incSysRef();
79 mRSC->mHal.funcs.script.invokeFreeChildren(mRSC, this);
80 return decSysRef();
81}
82
Jason Sams326e0dd2009-05-22 14:03:28 -070083namespace android {
84namespace renderscript {
85
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -080086void rsi_ScriptBindAllocation(Context * rsc, RsScript vs, RsAllocation va, uint32_t slot) {
Jason Sams326e0dd2009-05-22 14:03:28 -070087 Script *s = static_cast<Script *>(vs);
Jason Samsbe36bf32010-05-11 14:03:58 -070088 Allocation *a = static_cast<Allocation *>(va);
Alex Sakhartchouk700ba382010-10-08 15:00:05 -070089 s->setSlot(slot, a);
Steve Blockaf12ac62012-01-06 19:20:56 +000090 //ALOGE("rsi_ScriptBindAllocation %i %p %p", slot, a, a->getPtr());
Jason Sams326e0dd2009-05-22 14:03:28 -070091}
92
Alex Sakhartchoukb81a0eb2011-06-03 10:18:01 -070093void rsi_ScriptSetTimeZone(Context * rsc, RsScript vs, const char * timeZone, size_t length) {
Stephen Hinesd2432b92011-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) {
Steve Blockaf12ac62012-01-06 19:20:56 +000099 ALOGE("Couldn't allocate memory for timezone buffer");
Stephen Hinesd2432b92011-11-09 18:02:20 -0800100 return;
101 }
102 strncpy(tz, timeZone, length);
103 tz[length] = '\0';
104 if (setenv("TZ", tz, 1) == 0) {
105 tzset();
106 } else {
Steve Blockaf12ac62012-01-06 19:20:56 +0000107 ALOGE("Error setting timezone");
Stephen Hinesd2432b92011-11-09 18:02:20 -0800108 }
109 free(tz);
Jason Samsd34b7252009-08-04 16:58:20 -0700110}
111
Jason Sams5fb1aeb2011-04-27 15:12:49 -0700112void rsi_ScriptForEach(Context *rsc, RsScript vs, uint32_t slot,
113 RsAllocation vain, RsAllocation vaout,
Alex Sakhartchoukb81a0eb2011-06-03 10:18:01 -0700114 const void *params, size_t paramLen) {
Jason Sams5fb1aeb2011-04-27 15:12:49 -0700115 Script *s = static_cast<Script *>(vs);
Stephen Hines44199772012-02-21 20:13:12 -0800116 s->runForEach(rsc, slot,
Jason Sams5fb1aeb2011-04-27 15:12:49 -0700117 static_cast<const Allocation *>(vain), static_cast<Allocation *>(vaout),
118 params, paramLen);
119
120}
121
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800122void rsi_ScriptInvoke(Context *rsc, RsScript vs, uint32_t slot) {
Jason Sams8c6bc692009-09-16 15:04:38 -0700123 Script *s = static_cast<Script *>(vs);
Jason Sams22fa3712010-05-19 17:22:57 -0700124 s->Invoke(rsc, slot, NULL, 0);
Jason Sams8c6bc692009-09-16 15:04:38 -0700125}
126
127
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800128void rsi_ScriptInvokeData(Context *rsc, RsScript vs, uint32_t slot, void *data) {
Jason Samsbe36bf32010-05-11 14:03:58 -0700129 Script *s = static_cast<Script *>(vs);
Jason Sams22fa3712010-05-19 17:22:57 -0700130 s->Invoke(rsc, slot, NULL, 0);
Jason Samsbe36bf32010-05-11 14:03:58 -0700131}
132
Alex Sakhartchoukb81a0eb2011-06-03 10:18:01 -0700133void rsi_ScriptInvokeV(Context *rsc, RsScript vs, uint32_t slot, const void *data, size_t len) {
Jason Samsbe36bf32010-05-11 14:03:58 -0700134 Script *s = static_cast<Script *>(vs);
Jason Sams22fa3712010-05-19 17:22:57 -0700135 s->Invoke(rsc, slot, data, len);
Jason Samsbe36bf32010-05-11 14:03:58 -0700136}
137
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800138void rsi_ScriptSetVarI(Context *rsc, RsScript vs, uint32_t slot, int value) {
Jason Samsbe36bf32010-05-11 14:03:58 -0700139 Script *s = static_cast<Script *>(vs);
140 s->setVar(slot, &value, sizeof(value));
141}
142
Jason Samsa5eb6e12010-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 Sakhartchoukafb743a2010-11-09 17:00:54 -0800149void rsi_ScriptSetVarJ(Context *rsc, RsScript vs, uint32_t slot, long long value) {
Stephen Hines0977c942010-10-11 10:54:21 -0700150 Script *s = static_cast<Script *>(vs);
151 s->setVar(slot, &value, sizeof(value));
152}
153
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800154void rsi_ScriptSetVarF(Context *rsc, RsScript vs, uint32_t slot, float value) {
Jason Samsbe36bf32010-05-11 14:03:58 -0700155 Script *s = static_cast<Script *>(vs);
156 s->setVar(slot, &value, sizeof(value));
157}
158
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800159void rsi_ScriptSetVarD(Context *rsc, RsScript vs, uint32_t slot, double value) {
Stephen Hines6d0a0742010-09-20 17:20:30 -0700160 Script *s = static_cast<Script *>(vs);
161 s->setVar(slot, &value, sizeof(value));
162}
163
Alex Sakhartchoukb81a0eb2011-06-03 10:18:01 -0700164void rsi_ScriptSetVarV(Context *rsc, RsScript vs, uint32_t slot, const void *data, size_t len) {
Jason Samsbe36bf32010-05-11 14:03:58 -0700165 Script *s = static_cast<Script *>(vs);
166 s->setVar(slot, data, len);
Jason Samsfa517192009-08-13 12:59:04 -0700167}
168
Jason Sams326e0dd2009-05-22 14:03:28 -0700169}
170}
171