Jason Sams | 326e0dd | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 1 | /* |
Stephen Hines | 4419977 | 2012-02-21 20:13:12 -0800 | [diff] [blame] | 2 | * Copyright (C) 2009-2012 The Android Open Source Project |
Jason Sams | 326e0dd | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 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 Hines | d2432b9 | 2011-11-09 18:02:20 -0800 | [diff] [blame] | 18 | #include <time.h> |
Jason Sams | 326e0dd | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 19 | |
| 20 | using namespace android; |
| 21 | using namespace android::renderscript; |
| 22 | |
Alex Sakhartchouk | afb743a | 2010-11-09 17:00:54 -0800 | [diff] [blame] | 23 | Script::Script(Context *rsc) : ObjectBase(rsc) { |
Jason Sams | 928b734 | 2009-06-08 18:50:13 -0700 | [diff] [blame] | 24 | memset(&mEnviroment, 0, sizeof(mEnviroment)); |
Jason Sams | bad8074 | 2011-03-16 16:29:28 -0700 | [diff] [blame] | 25 | memset(&mHal, 0, sizeof(mHal)); |
Alex Sakhartchouk | 700ba38 | 2010-10-08 15:00:05 -0700 | [diff] [blame] | 26 | |
| 27 | mSlots = NULL; |
| 28 | mTypes = NULL; |
Jason Sams | 77020c5 | 2011-11-22 12:49:11 -0800 | [diff] [blame] | 29 | mInitialized = false; |
Jason Sams | 326e0dd | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 30 | } |
| 31 | |
Alex Sakhartchouk | afb743a | 2010-11-09 17:00:54 -0800 | [diff] [blame] | 32 | Script::~Script() { |
| 33 | if (mSlots) { |
Alex Sakhartchouk | 700ba38 | 2010-10-08 15:00:05 -0700 | [diff] [blame] | 34 | delete [] mSlots; |
| 35 | mSlots = NULL; |
| 36 | } |
Alex Sakhartchouk | afb743a | 2010-11-09 17:00:54 -0800 | [diff] [blame] | 37 | if (mTypes) { |
Alex Sakhartchouk | 700ba38 | 2010-10-08 15:00:05 -0700 | [diff] [blame] | 38 | delete [] mTypes; |
| 39 | mTypes = NULL; |
| 40 | } |
| 41 | } |
| 42 | |
Alex Sakhartchouk | 700ba38 | 2010-10-08 15:00:05 -0700 | [diff] [blame] | 43 | void Script::setSlot(uint32_t slot, Allocation *a) { |
Steve Block | af12ac6 | 2012-01-06 19:20:56 +0000 | [diff] [blame] | 44 | //ALOGE("setSlot %i %p", slot, a); |
Jason Sams | bad8074 | 2011-03-16 16:29:28 -0700 | [diff] [blame] | 45 | if (slot >= mHal.info.exportedVariableCount) { |
Steve Block | af12ac6 | 2012-01-06 19:20:56 +0000 | [diff] [blame] | 46 | ALOGE("Script::setSlot unable to set allocation, invalid slot index"); |
Alex Sakhartchouk | 700ba38 | 2010-10-08 15:00:05 -0700 | [diff] [blame] | 47 | return; |
| 48 | } |
| 49 | |
| 50 | mSlots[slot].set(a); |
Jason Sams | bad8074 | 2011-03-16 16:29:28 -0700 | [diff] [blame] | 51 | 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 Sams | 326e0dd | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 56 | } |
| 57 | |
Alex Sakhartchouk | b81a0eb | 2011-06-03 10:18:01 -0700 | [diff] [blame] | 58 | void Script::setVar(uint32_t slot, const void *val, size_t len) { |
Steve Block | af12ac6 | 2012-01-06 19:20:56 +0000 | [diff] [blame] | 59 | //ALOGE("setVar %i %p %i", slot, val, len); |
Jason Sams | bad8074 | 2011-03-16 16:29:28 -0700 | [diff] [blame] | 60 | if (slot >= mHal.info.exportedVariableCount) { |
Steve Block | af12ac6 | 2012-01-06 19:20:56 +0000 | [diff] [blame] | 61 | ALOGE("Script::setVar unable to set allocation, invalid slot index"); |
Jason Sams | bad8074 | 2011-03-16 16:29:28 -0700 | [diff] [blame] | 62 | return; |
Jason Sams | be36bf3 | 2010-05-11 14:03:58 -0700 | [diff] [blame] | 63 | } |
Jason Sams | bad8074 | 2011-03-16 16:29:28 -0700 | [diff] [blame] | 64 | mRSC->mHal.funcs.script.setGlobalVar(mRSC, this, slot, (void *)val, len); |
Jason Sams | be36bf3 | 2010-05-11 14:03:58 -0700 | [diff] [blame] | 65 | } |
| 66 | |
Stephen Hines | 2980f07 | 2012-04-09 18:26:29 -0700 | [diff] [blame^] | 67 | void 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 Sams | a5eb6e1 | 2010-11-16 17:37:02 -0800 | [diff] [blame] | 77 | void Script::setVarObj(uint32_t slot, ObjectBase *val) { |
Steve Block | af12ac6 | 2012-01-06 19:20:56 +0000 | [diff] [blame] | 78 | //ALOGE("setVarObj %i %p", slot, val); |
Jason Sams | bad8074 | 2011-03-16 16:29:28 -0700 | [diff] [blame] | 79 | if (slot >= mHal.info.exportedVariableCount) { |
Steve Block | af12ac6 | 2012-01-06 19:20:56 +0000 | [diff] [blame] | 80 | ALOGE("Script::setVarObj unable to set allocation, invalid slot index"); |
Jason Sams | bad8074 | 2011-03-16 16:29:28 -0700 | [diff] [blame] | 81 | return; |
Jason Sams | a5eb6e1 | 2010-11-16 17:37:02 -0800 | [diff] [blame] | 82 | } |
Steve Block | af12ac6 | 2012-01-06 19:20:56 +0000 | [diff] [blame] | 83 | //ALOGE("setvarobj %i %p", slot, val); |
Jason Sams | bad8074 | 2011-03-16 16:29:28 -0700 | [diff] [blame] | 84 | mRSC->mHal.funcs.script.setGlobalObj(mRSC, this, slot, val); |
Jason Sams | a5eb6e1 | 2010-11-16 17:37:02 -0800 | [diff] [blame] | 85 | } |
| 86 | |
Stephen Hines | 4ee16ff | 2011-08-31 17:41:39 -0700 | [diff] [blame] | 87 | bool Script::freeChildren() { |
| 88 | incSysRef(); |
| 89 | mRSC->mHal.funcs.script.invokeFreeChildren(mRSC, this); |
| 90 | return decSysRef(); |
| 91 | } |
| 92 | |
Jason Sams | 326e0dd | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 93 | namespace android { |
| 94 | namespace renderscript { |
| 95 | |
Alex Sakhartchouk | afb743a | 2010-11-09 17:00:54 -0800 | [diff] [blame] | 96 | void rsi_ScriptBindAllocation(Context * rsc, RsScript vs, RsAllocation va, uint32_t slot) { |
Jason Sams | 326e0dd | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 97 | Script *s = static_cast<Script *>(vs); |
Jason Sams | be36bf3 | 2010-05-11 14:03:58 -0700 | [diff] [blame] | 98 | Allocation *a = static_cast<Allocation *>(va); |
Alex Sakhartchouk | 700ba38 | 2010-10-08 15:00:05 -0700 | [diff] [blame] | 99 | s->setSlot(slot, a); |
Steve Block | af12ac6 | 2012-01-06 19:20:56 +0000 | [diff] [blame] | 100 | //ALOGE("rsi_ScriptBindAllocation %i %p %p", slot, a, a->getPtr()); |
Jason Sams | 326e0dd | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 101 | } |
| 102 | |
Alex Sakhartchouk | b81a0eb | 2011-06-03 10:18:01 -0700 | [diff] [blame] | 103 | void rsi_ScriptSetTimeZone(Context * rsc, RsScript vs, const char * timeZone, size_t length) { |
Stephen Hines | d2432b9 | 2011-11-09 18:02:20 -0800 | [diff] [blame] | 104 | // 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 Block | af12ac6 | 2012-01-06 19:20:56 +0000 | [diff] [blame] | 109 | ALOGE("Couldn't allocate memory for timezone buffer"); |
Stephen Hines | d2432b9 | 2011-11-09 18:02:20 -0800 | [diff] [blame] | 110 | return; |
| 111 | } |
| 112 | strncpy(tz, timeZone, length); |
| 113 | tz[length] = '\0'; |
| 114 | if (setenv("TZ", tz, 1) == 0) { |
| 115 | tzset(); |
| 116 | } else { |
Steve Block | af12ac6 | 2012-01-06 19:20:56 +0000 | [diff] [blame] | 117 | ALOGE("Error setting timezone"); |
Stephen Hines | d2432b9 | 2011-11-09 18:02:20 -0800 | [diff] [blame] | 118 | } |
| 119 | free(tz); |
Jason Sams | d34b725 | 2009-08-04 16:58:20 -0700 | [diff] [blame] | 120 | } |
| 121 | |
Jason Sams | 5fb1aeb | 2011-04-27 15:12:49 -0700 | [diff] [blame] | 122 | void rsi_ScriptForEach(Context *rsc, RsScript vs, uint32_t slot, |
| 123 | RsAllocation vain, RsAllocation vaout, |
Alex Sakhartchouk | b81a0eb | 2011-06-03 10:18:01 -0700 | [diff] [blame] | 124 | const void *params, size_t paramLen) { |
Jason Sams | 5fb1aeb | 2011-04-27 15:12:49 -0700 | [diff] [blame] | 125 | Script *s = static_cast<Script *>(vs); |
Stephen Hines | 4419977 | 2012-02-21 20:13:12 -0800 | [diff] [blame] | 126 | s->runForEach(rsc, slot, |
Jason Sams | 5fb1aeb | 2011-04-27 15:12:49 -0700 | [diff] [blame] | 127 | static_cast<const Allocation *>(vain), static_cast<Allocation *>(vaout), |
| 128 | params, paramLen); |
| 129 | |
| 130 | } |
| 131 | |
Alex Sakhartchouk | afb743a | 2010-11-09 17:00:54 -0800 | [diff] [blame] | 132 | void rsi_ScriptInvoke(Context *rsc, RsScript vs, uint32_t slot) { |
Jason Sams | 8c6bc69 | 2009-09-16 15:04:38 -0700 | [diff] [blame] | 133 | Script *s = static_cast<Script *>(vs); |
Jason Sams | 22fa371 | 2010-05-19 17:22:57 -0700 | [diff] [blame] | 134 | s->Invoke(rsc, slot, NULL, 0); |
Jason Sams | 8c6bc69 | 2009-09-16 15:04:38 -0700 | [diff] [blame] | 135 | } |
| 136 | |
| 137 | |
Alex Sakhartchouk | afb743a | 2010-11-09 17:00:54 -0800 | [diff] [blame] | 138 | void rsi_ScriptInvokeData(Context *rsc, RsScript vs, uint32_t slot, void *data) { |
Jason Sams | be36bf3 | 2010-05-11 14:03:58 -0700 | [diff] [blame] | 139 | Script *s = static_cast<Script *>(vs); |
Jason Sams | 22fa371 | 2010-05-19 17:22:57 -0700 | [diff] [blame] | 140 | s->Invoke(rsc, slot, NULL, 0); |
Jason Sams | be36bf3 | 2010-05-11 14:03:58 -0700 | [diff] [blame] | 141 | } |
| 142 | |
Alex Sakhartchouk | b81a0eb | 2011-06-03 10:18:01 -0700 | [diff] [blame] | 143 | void rsi_ScriptInvokeV(Context *rsc, RsScript vs, uint32_t slot, const void *data, size_t len) { |
Jason Sams | be36bf3 | 2010-05-11 14:03:58 -0700 | [diff] [blame] | 144 | Script *s = static_cast<Script *>(vs); |
Jason Sams | 22fa371 | 2010-05-19 17:22:57 -0700 | [diff] [blame] | 145 | s->Invoke(rsc, slot, data, len); |
Jason Sams | be36bf3 | 2010-05-11 14:03:58 -0700 | [diff] [blame] | 146 | } |
| 147 | |
Alex Sakhartchouk | afb743a | 2010-11-09 17:00:54 -0800 | [diff] [blame] | 148 | void rsi_ScriptSetVarI(Context *rsc, RsScript vs, uint32_t slot, int value) { |
Jason Sams | be36bf3 | 2010-05-11 14:03:58 -0700 | [diff] [blame] | 149 | Script *s = static_cast<Script *>(vs); |
| 150 | s->setVar(slot, &value, sizeof(value)); |
| 151 | } |
| 152 | |
Jason Sams | a5eb6e1 | 2010-11-16 17:37:02 -0800 | [diff] [blame] | 153 | void 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 Sakhartchouk | afb743a | 2010-11-09 17:00:54 -0800 | [diff] [blame] | 159 | void rsi_ScriptSetVarJ(Context *rsc, RsScript vs, uint32_t slot, long long value) { |
Stephen Hines | 0977c94 | 2010-10-11 10:54:21 -0700 | [diff] [blame] | 160 | Script *s = static_cast<Script *>(vs); |
| 161 | s->setVar(slot, &value, sizeof(value)); |
| 162 | } |
| 163 | |
Alex Sakhartchouk | afb743a | 2010-11-09 17:00:54 -0800 | [diff] [blame] | 164 | void rsi_ScriptSetVarF(Context *rsc, RsScript vs, uint32_t slot, float value) { |
Jason Sams | be36bf3 | 2010-05-11 14:03:58 -0700 | [diff] [blame] | 165 | Script *s = static_cast<Script *>(vs); |
| 166 | s->setVar(slot, &value, sizeof(value)); |
| 167 | } |
| 168 | |
Alex Sakhartchouk | afb743a | 2010-11-09 17:00:54 -0800 | [diff] [blame] | 169 | void rsi_ScriptSetVarD(Context *rsc, RsScript vs, uint32_t slot, double value) { |
Stephen Hines | 6d0a074 | 2010-09-20 17:20:30 -0700 | [diff] [blame] | 170 | Script *s = static_cast<Script *>(vs); |
| 171 | s->setVar(slot, &value, sizeof(value)); |
| 172 | } |
| 173 | |
Alex Sakhartchouk | b81a0eb | 2011-06-03 10:18:01 -0700 | [diff] [blame] | 174 | void rsi_ScriptSetVarV(Context *rsc, RsScript vs, uint32_t slot, const void *data, size_t len) { |
Jason Sams | be36bf3 | 2010-05-11 14:03:58 -0700 | [diff] [blame] | 175 | Script *s = static_cast<Script *>(vs); |
| 176 | s->setVar(slot, data, len); |
Jason Sams | fa51719 | 2009-08-13 12:59:04 -0700 | [diff] [blame] | 177 | } |
| 178 | |
Stephen Hines | 2980f07 | 2012-04-09 18:26:29 -0700 | [diff] [blame^] | 179 | void 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 Sams | 326e0dd | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 187 | } |
| 188 | } |
| 189 | |