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 | 807fdc4 | 2012-07-25 17:55:39 -0700 | [diff] [blame] | 51 | mRSC->mHal.funcs.script.setGlobalBind(mRSC, this, slot, a); |
Jason Sams | 326e0dd | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 52 | } |
| 53 | |
Alex Sakhartchouk | b81a0eb | 2011-06-03 10:18:01 -0700 | [diff] [blame] | 54 | void Script::setVar(uint32_t slot, const void *val, size_t len) { |
Steve Block | af12ac6 | 2012-01-06 19:20:56 +0000 | [diff] [blame] | 55 | //ALOGE("setVar %i %p %i", slot, val, len); |
Jason Sams | bad8074 | 2011-03-16 16:29:28 -0700 | [diff] [blame] | 56 | if (slot >= mHal.info.exportedVariableCount) { |
Steve Block | af12ac6 | 2012-01-06 19:20:56 +0000 | [diff] [blame] | 57 | ALOGE("Script::setVar unable to set allocation, invalid slot index"); |
Jason Sams | bad8074 | 2011-03-16 16:29:28 -0700 | [diff] [blame] | 58 | return; |
Jason Sams | be36bf3 | 2010-05-11 14:03:58 -0700 | [diff] [blame] | 59 | } |
Jason Sams | bad8074 | 2011-03-16 16:29:28 -0700 | [diff] [blame] | 60 | mRSC->mHal.funcs.script.setGlobalVar(mRSC, this, slot, (void *)val, len); |
Jason Sams | be36bf3 | 2010-05-11 14:03:58 -0700 | [diff] [blame] | 61 | } |
| 62 | |
Tim Murray | 9c64239 | 2013-04-11 13:29:59 -0700 | [diff] [blame] | 63 | void Script::getVar(uint32_t slot, const void *val, size_t len) { |
| 64 | //ALOGE("getVar %i %p %i", slot, val, len); |
| 65 | if (slot >= mHal.info.exportedVariableCount) { |
| 66 | ALOGE("Script::getVar unable to set allocation, invalid slot index"); |
| 67 | return; |
| 68 | } |
| 69 | mRSC->mHal.funcs.script.getGlobalVar(mRSC, this, slot, (void *)val, len); |
| 70 | } |
| 71 | |
Stephen Hines | 2980f07 | 2012-04-09 18:26:29 -0700 | [diff] [blame] | 72 | void Script::setVar(uint32_t slot, const void *val, size_t len, Element *e, |
| 73 | const size_t *dims, size_t dimLen) { |
| 74 | if (slot >= mHal.info.exportedVariableCount) { |
| 75 | ALOGE("Script::setVar unable to set allocation, invalid slot index"); |
| 76 | return; |
| 77 | } |
| 78 | mRSC->mHal.funcs.script.setGlobalVarWithElemDims(mRSC, this, slot, |
| 79 | (void *)val, len, e, dims, dimLen); |
| 80 | } |
| 81 | |
Jason Sams | a5eb6e1 | 2010-11-16 17:37:02 -0800 | [diff] [blame] | 82 | void Script::setVarObj(uint32_t slot, ObjectBase *val) { |
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 | if (slot >= mHal.info.exportedVariableCount) { |
Steve Block | af12ac6 | 2012-01-06 19:20:56 +0000 | [diff] [blame] | 85 | ALOGE("Script::setVarObj unable to set allocation, invalid slot index"); |
Jason Sams | bad8074 | 2011-03-16 16:29:28 -0700 | [diff] [blame] | 86 | return; |
Jason Sams | a5eb6e1 | 2010-11-16 17:37:02 -0800 | [diff] [blame] | 87 | } |
Steve Block | af12ac6 | 2012-01-06 19:20:56 +0000 | [diff] [blame] | 88 | //ALOGE("setvarobj %i %p", slot, val); |
Jason Sams | bad8074 | 2011-03-16 16:29:28 -0700 | [diff] [blame] | 89 | mRSC->mHal.funcs.script.setGlobalObj(mRSC, this, slot, val); |
Jason Sams | a5eb6e1 | 2010-11-16 17:37:02 -0800 | [diff] [blame] | 90 | } |
| 91 | |
Stephen Hines | 4ee16ff | 2011-08-31 17:41:39 -0700 | [diff] [blame] | 92 | bool Script::freeChildren() { |
| 93 | incSysRef(); |
| 94 | mRSC->mHal.funcs.script.invokeFreeChildren(mRSC, this); |
| 95 | return decSysRef(); |
| 96 | } |
| 97 | |
Jason Sams | dbe66d6 | 2012-09-17 13:54:41 -0700 | [diff] [blame] | 98 | ScriptKernelID::ScriptKernelID(Context *rsc, Script *s, int slot, int sig) |
| 99 | : ObjectBase(rsc) { |
| 100 | |
| 101 | mScript = s; |
| 102 | mSlot = slot; |
| 103 | mHasKernelInput = (sig & 1) != 0; |
| 104 | mHasKernelOutput = (sig & 2) != 0; |
| 105 | } |
| 106 | |
| 107 | ScriptKernelID::~ScriptKernelID() { |
| 108 | |
| 109 | } |
| 110 | |
| 111 | void ScriptKernelID::serialize(Context *rsc, OStream *stream) const { |
| 112 | |
| 113 | } |
| 114 | |
| 115 | RsA3DClassID ScriptKernelID::getClassId() const { |
| 116 | return RS_A3D_CLASS_ID_SCRIPT_KERNEL_ID; |
| 117 | } |
| 118 | |
| 119 | ScriptFieldID::ScriptFieldID(Context *rsc, Script *s, int slot) : ObjectBase(rsc) { |
| 120 | mScript = s; |
| 121 | mSlot = slot; |
| 122 | } |
| 123 | |
| 124 | ScriptFieldID::~ScriptFieldID() { |
| 125 | |
| 126 | } |
| 127 | |
| 128 | void ScriptFieldID::serialize(Context *rsc, OStream *stream) const { |
| 129 | |
| 130 | } |
| 131 | |
| 132 | RsA3DClassID ScriptFieldID::getClassId() const { |
| 133 | return RS_A3D_CLASS_ID_SCRIPT_FIELD_ID; |
| 134 | } |
| 135 | |
| 136 | |
Jason Sams | 326e0dd | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 137 | namespace android { |
| 138 | namespace renderscript { |
| 139 | |
Jason Sams | dbe66d6 | 2012-09-17 13:54:41 -0700 | [diff] [blame] | 140 | RsScriptKernelID rsi_ScriptKernelIDCreate(Context *rsc, RsScript vs, int slot, int sig) { |
Stephen Hines | 61c8695 | 2013-04-09 17:34:43 -0700 | [diff] [blame] | 141 | ScriptKernelID *kid = new ScriptKernelID(rsc, (Script *)vs, slot, sig); |
| 142 | kid->incUserRef(); |
| 143 | return kid; |
Jason Sams | dbe66d6 | 2012-09-17 13:54:41 -0700 | [diff] [blame] | 144 | } |
| 145 | |
| 146 | RsScriptFieldID rsi_ScriptFieldIDCreate(Context *rsc, RsScript vs, int slot) { |
Stephen Hines | 61c8695 | 2013-04-09 17:34:43 -0700 | [diff] [blame] | 147 | ScriptFieldID *fid = new ScriptFieldID(rsc, (Script *)vs, slot); |
| 148 | fid->incUserRef(); |
| 149 | return fid; |
Jason Sams | dbe66d6 | 2012-09-17 13:54:41 -0700 | [diff] [blame] | 150 | } |
| 151 | |
Alex Sakhartchouk | afb743a | 2010-11-09 17:00:54 -0800 | [diff] [blame] | 152 | void rsi_ScriptBindAllocation(Context * rsc, RsScript vs, RsAllocation va, uint32_t slot) { |
Jason Sams | 326e0dd | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 153 | Script *s = static_cast<Script *>(vs); |
Jason Sams | be36bf3 | 2010-05-11 14:03:58 -0700 | [diff] [blame] | 154 | Allocation *a = static_cast<Allocation *>(va); |
Alex Sakhartchouk | 700ba38 | 2010-10-08 15:00:05 -0700 | [diff] [blame] | 155 | s->setSlot(slot, a); |
Jason Sams | 326e0dd | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 156 | } |
| 157 | |
Alex Sakhartchouk | b81a0eb | 2011-06-03 10:18:01 -0700 | [diff] [blame] | 158 | 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] | 159 | // We unfortunately need to make a new copy of the string, since it is |
| 160 | // not NULL-terminated. We then use setenv(), which properly handles |
| 161 | // freeing/duplicating the actual string for the environment. |
| 162 | char *tz = (char *) malloc(length + 1); |
| 163 | if (!tz) { |
Steve Block | af12ac6 | 2012-01-06 19:20:56 +0000 | [diff] [blame] | 164 | ALOGE("Couldn't allocate memory for timezone buffer"); |
Stephen Hines | d2432b9 | 2011-11-09 18:02:20 -0800 | [diff] [blame] | 165 | return; |
| 166 | } |
| 167 | strncpy(tz, timeZone, length); |
| 168 | tz[length] = '\0'; |
| 169 | if (setenv("TZ", tz, 1) == 0) { |
| 170 | tzset(); |
| 171 | } else { |
Steve Block | af12ac6 | 2012-01-06 19:20:56 +0000 | [diff] [blame] | 172 | ALOGE("Error setting timezone"); |
Stephen Hines | d2432b9 | 2011-11-09 18:02:20 -0800 | [diff] [blame] | 173 | } |
| 174 | free(tz); |
Jason Sams | d34b725 | 2009-08-04 16:58:20 -0700 | [diff] [blame] | 175 | } |
| 176 | |
Jason Sams | 5fb1aeb | 2011-04-27 15:12:49 -0700 | [diff] [blame] | 177 | void rsi_ScriptForEach(Context *rsc, RsScript vs, uint32_t slot, |
| 178 | RsAllocation vain, RsAllocation vaout, |
Tim Murray | d4ecb17 | 2013-02-07 12:17:03 -0800 | [diff] [blame] | 179 | const void *params, size_t paramLen, |
| 180 | const RsScriptCall *sc, size_t scLen) { |
Jason Sams | 5fb1aeb | 2011-04-27 15:12:49 -0700 | [diff] [blame] | 181 | Script *s = static_cast<Script *>(vs); |
Stephen Hines | 5dd9d07 | 2013-04-02 00:08:32 -0700 | [diff] [blame] | 182 | // The rs.spec generated code does not handle the absence of an actual |
| 183 | // input for sc. Instead, it retains an existing pointer value (the prior |
| 184 | // field in the packed data object). This can cause confusion because |
| 185 | // drivers might now inspect bogus sc data. |
| 186 | if (scLen == 0) { |
| 187 | sc = NULL; |
| 188 | } |
Stephen Hines | 4419977 | 2012-02-21 20:13:12 -0800 | [diff] [blame] | 189 | s->runForEach(rsc, slot, |
Jason Sams | 5fb1aeb | 2011-04-27 15:12:49 -0700 | [diff] [blame] | 190 | static_cast<const Allocation *>(vain), static_cast<Allocation *>(vaout), |
Tim Murray | d4ecb17 | 2013-02-07 12:17:03 -0800 | [diff] [blame] | 191 | params, paramLen, sc); |
Jason Sams | 5fb1aeb | 2011-04-27 15:12:49 -0700 | [diff] [blame] | 192 | |
| 193 | } |
| 194 | |
Alex Sakhartchouk | afb743a | 2010-11-09 17:00:54 -0800 | [diff] [blame] | 195 | void rsi_ScriptInvoke(Context *rsc, RsScript vs, uint32_t slot) { |
Jason Sams | 8c6bc69 | 2009-09-16 15:04:38 -0700 | [diff] [blame] | 196 | Script *s = static_cast<Script *>(vs); |
Jason Sams | 22fa371 | 2010-05-19 17:22:57 -0700 | [diff] [blame] | 197 | s->Invoke(rsc, slot, NULL, 0); |
Jason Sams | 8c6bc69 | 2009-09-16 15:04:38 -0700 | [diff] [blame] | 198 | } |
| 199 | |
| 200 | |
Alex Sakhartchouk | afb743a | 2010-11-09 17:00:54 -0800 | [diff] [blame] | 201 | void rsi_ScriptInvokeData(Context *rsc, RsScript vs, uint32_t slot, void *data) { |
Jason Sams | be36bf3 | 2010-05-11 14:03:58 -0700 | [diff] [blame] | 202 | Script *s = static_cast<Script *>(vs); |
Jason Sams | 22fa371 | 2010-05-19 17:22:57 -0700 | [diff] [blame] | 203 | s->Invoke(rsc, slot, NULL, 0); |
Jason Sams | be36bf3 | 2010-05-11 14:03:58 -0700 | [diff] [blame] | 204 | } |
| 205 | |
Alex Sakhartchouk | b81a0eb | 2011-06-03 10:18:01 -0700 | [diff] [blame] | 206 | 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] | 207 | Script *s = static_cast<Script *>(vs); |
Jason Sams | 22fa371 | 2010-05-19 17:22:57 -0700 | [diff] [blame] | 208 | s->Invoke(rsc, slot, data, len); |
Jason Sams | be36bf3 | 2010-05-11 14:03:58 -0700 | [diff] [blame] | 209 | } |
| 210 | |
Alex Sakhartchouk | afb743a | 2010-11-09 17:00:54 -0800 | [diff] [blame] | 211 | void rsi_ScriptSetVarI(Context *rsc, RsScript vs, uint32_t slot, int value) { |
Jason Sams | be36bf3 | 2010-05-11 14:03:58 -0700 | [diff] [blame] | 212 | Script *s = static_cast<Script *>(vs); |
| 213 | s->setVar(slot, &value, sizeof(value)); |
| 214 | } |
| 215 | |
Jason Sams | a5eb6e1 | 2010-11-16 17:37:02 -0800 | [diff] [blame] | 216 | void rsi_ScriptSetVarObj(Context *rsc, RsScript vs, uint32_t slot, RsObjectBase value) { |
| 217 | Script *s = static_cast<Script *>(vs); |
| 218 | ObjectBase *o = static_cast<ObjectBase *>(value); |
| 219 | s->setVarObj(slot, o); |
| 220 | } |
| 221 | |
Tim Murray | 099bc26 | 2013-03-20 16:54:03 -0700 | [diff] [blame] | 222 | void rsi_ScriptSetVarJ(Context *rsc, RsScript vs, uint32_t slot, int64_t value) { |
Stephen Hines | 0977c94 | 2010-10-11 10:54:21 -0700 | [diff] [blame] | 223 | Script *s = static_cast<Script *>(vs); |
| 224 | s->setVar(slot, &value, sizeof(value)); |
| 225 | } |
| 226 | |
Alex Sakhartchouk | afb743a | 2010-11-09 17:00:54 -0800 | [diff] [blame] | 227 | void rsi_ScriptSetVarF(Context *rsc, RsScript vs, uint32_t slot, float value) { |
Jason Sams | be36bf3 | 2010-05-11 14:03:58 -0700 | [diff] [blame] | 228 | Script *s = static_cast<Script *>(vs); |
| 229 | s->setVar(slot, &value, sizeof(value)); |
| 230 | } |
| 231 | |
Alex Sakhartchouk | afb743a | 2010-11-09 17:00:54 -0800 | [diff] [blame] | 232 | void rsi_ScriptSetVarD(Context *rsc, RsScript vs, uint32_t slot, double value) { |
Stephen Hines | 6d0a074 | 2010-09-20 17:20:30 -0700 | [diff] [blame] | 233 | Script *s = static_cast<Script *>(vs); |
| 234 | s->setVar(slot, &value, sizeof(value)); |
| 235 | } |
| 236 | |
Alex Sakhartchouk | b81a0eb | 2011-06-03 10:18:01 -0700 | [diff] [blame] | 237 | 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] | 238 | Script *s = static_cast<Script *>(vs); |
| 239 | s->setVar(slot, data, len); |
Jason Sams | fa51719 | 2009-08-13 12:59:04 -0700 | [diff] [blame] | 240 | } |
| 241 | |
Tim Murray | 9c64239 | 2013-04-11 13:29:59 -0700 | [diff] [blame] | 242 | void rsi_ScriptGetVarV(Context *rsc, RsScript vs, uint32_t slot, void *data, size_t len) { |
| 243 | Script *s = static_cast<Script *>(vs); |
| 244 | s->getVar(slot, data, len); |
| 245 | } |
| 246 | |
Stephen Hines | 2980f07 | 2012-04-09 18:26:29 -0700 | [diff] [blame] | 247 | void rsi_ScriptSetVarVE(Context *rsc, RsScript vs, uint32_t slot, |
| 248 | const void *data, size_t len, RsElement ve, |
| 249 | const size_t *dims, size_t dimLen) { |
| 250 | Script *s = static_cast<Script *>(vs); |
| 251 | Element *e = static_cast<Element *>(ve); |
| 252 | s->setVar(slot, data, len, e, dims, dimLen); |
| 253 | } |
| 254 | |
Jason Sams | 326e0dd | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 255 | } |
| 256 | } |
| 257 | |