Jason Sams | 326e0dd | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 1 | /* |
| 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" |
| 18 | |
| 19 | using namespace android; |
| 20 | using namespace android::renderscript; |
| 21 | |
Alex Sakhartchouk | afb743a | 2010-11-09 17:00:54 -0800 | [diff] [blame] | 22 | Script::Script(Context *rsc) : ObjectBase(rsc) { |
Jason Sams | 928b734 | 2009-06-08 18:50:13 -0700 | [diff] [blame] | 23 | memset(&mEnviroment, 0, sizeof(mEnviroment)); |
Alex Sakhartchouk | 700ba38 | 2010-10-08 15:00:05 -0700 | [diff] [blame] | 24 | |
| 25 | mSlots = NULL; |
| 26 | mTypes = NULL; |
Jason Sams | 326e0dd | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 27 | } |
| 28 | |
Alex Sakhartchouk | afb743a | 2010-11-09 17:00:54 -0800 | [diff] [blame] | 29 | Script::~Script() { |
| 30 | if (mSlots) { |
Alex Sakhartchouk | 700ba38 | 2010-10-08 15:00:05 -0700 | [diff] [blame] | 31 | delete [] mSlots; |
| 32 | mSlots = NULL; |
| 33 | } |
Alex Sakhartchouk | afb743a | 2010-11-09 17:00:54 -0800 | [diff] [blame] | 34 | if (mTypes) { |
Alex Sakhartchouk | 700ba38 | 2010-10-08 15:00:05 -0700 | [diff] [blame] | 35 | delete [] mTypes; |
| 36 | mTypes = NULL; |
| 37 | } |
| 38 | } |
| 39 | |
| 40 | void Script::initSlots() { |
Alex Sakhartchouk | afb743a | 2010-11-09 17:00:54 -0800 | [diff] [blame] | 41 | if (mEnviroment.mFieldCount > 0) { |
Alex Sakhartchouk | 700ba38 | 2010-10-08 15:00:05 -0700 | [diff] [blame] | 42 | mSlots = new ObjectBaseRef<Allocation>[mEnviroment.mFieldCount]; |
| 43 | mTypes = new ObjectBaseRef<const Type>[mEnviroment.mFieldCount]; |
| 44 | } |
| 45 | } |
| 46 | |
| 47 | void Script::setSlot(uint32_t slot, Allocation *a) { |
Alex Sakhartchouk | afb743a | 2010-11-09 17:00:54 -0800 | [diff] [blame] | 48 | if (slot >= mEnviroment.mFieldCount) { |
Alex Sakhartchouk | 700ba38 | 2010-10-08 15:00:05 -0700 | [diff] [blame] | 49 | LOGE("Script::setSlot unable to set allocation, invalid slot index"); |
| 50 | return; |
| 51 | } |
| 52 | |
| 53 | mSlots[slot].set(a); |
Jason Sams | 326e0dd | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 54 | } |
| 55 | |
Alex Sakhartchouk | afb743a | 2010-11-09 17:00:54 -0800 | [diff] [blame] | 56 | void Script::setVar(uint32_t slot, const void *val, uint32_t len) { |
Jason Sams | be36bf3 | 2010-05-11 14:03:58 -0700 | [diff] [blame] | 57 | int32_t *destPtr = ((int32_t **)mEnviroment.mFieldAddress)[slot]; |
| 58 | if (destPtr) { |
| 59 | //LOGE("setVar f1 %f", ((const float *)destPtr)[0]); |
| 60 | //LOGE("setVar %p %i", destPtr, len); |
| 61 | memcpy(destPtr, val, len); |
| 62 | //LOGE("setVar f2 %f", ((const float *)destPtr)[0]); |
| 63 | } else { |
Jason Sams | d7e5481 | 2010-10-10 17:58:25 -0700 | [diff] [blame] | 64 | //if (rsc->props.mLogScripts) { |
| 65 | LOGV("Calling setVar on slot = %i which is null", slot); |
| 66 | //} |
Jason Sams | be36bf3 | 2010-05-11 14:03:58 -0700 | [diff] [blame] | 67 | } |
| 68 | } |
| 69 | |
Jason Sams | a5eb6e1 | 2010-11-16 17:37:02 -0800 | [diff] [blame] | 70 | void Script::setVarObj(uint32_t slot, ObjectBase *val) { |
| 71 | ObjectBase **destPtr = ((ObjectBase ***)mEnviroment.mFieldAddress)[slot]; |
| 72 | |
| 73 | if (destPtr) { |
| 74 | if (val != NULL) { |
| 75 | val->incSysRef(); |
| 76 | } |
| 77 | if (*destPtr) { |
| 78 | (*destPtr)->decSysRef(); |
| 79 | } |
| 80 | *destPtr = val; |
| 81 | } else { |
| 82 | LOGV("Calling setVarObj on slot = %i which is null. This is dangerous because the script will not hold a ref count on the object.", slot); |
| 83 | } |
| 84 | } |
| 85 | |
Jason Sams | 326e0dd | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 86 | namespace android { |
| 87 | namespace renderscript { |
| 88 | |
Alex Sakhartchouk | afb743a | 2010-11-09 17:00:54 -0800 | [diff] [blame] | 89 | void rsi_ScriptBindAllocation(Context * rsc, RsScript vs, RsAllocation va, uint32_t slot) { |
Jason Sams | 326e0dd | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 90 | Script *s = static_cast<Script *>(vs); |
Jason Sams | be36bf3 | 2010-05-11 14:03:58 -0700 | [diff] [blame] | 91 | Allocation *a = static_cast<Allocation *>(va); |
Alex Sakhartchouk | 700ba38 | 2010-10-08 15:00:05 -0700 | [diff] [blame] | 92 | s->setSlot(slot, a); |
Jason Sams | be36bf3 | 2010-05-11 14:03:58 -0700 | [diff] [blame] | 93 | //LOGE("rsi_ScriptBindAllocation %i %p %p", slot, a, a->getPtr()); |
Jason Sams | 326e0dd | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 94 | } |
| 95 | |
Alex Sakhartchouk | afb743a | 2010-11-09 17:00:54 -0800 | [diff] [blame] | 96 | void rsi_ScriptSetTimeZone(Context * rsc, RsScript vs, const char * timeZone, uint32_t length) { |
Jason Sams | d34b725 | 2009-08-04 16:58:20 -0700 | [diff] [blame] | 97 | Script *s = static_cast<Script *>(vs); |
| 98 | s->mEnviroment.mTimeZone = timeZone; |
| 99 | } |
| 100 | |
Alex Sakhartchouk | afb743a | 2010-11-09 17:00:54 -0800 | [diff] [blame] | 101 | void rsi_ScriptInvoke(Context *rsc, RsScript vs, uint32_t slot) { |
Jason Sams | 8c6bc69 | 2009-09-16 15:04:38 -0700 | [diff] [blame] | 102 | Script *s = static_cast<Script *>(vs); |
Jason Sams | 22fa371 | 2010-05-19 17:22:57 -0700 | [diff] [blame] | 103 | s->Invoke(rsc, slot, NULL, 0); |
Jason Sams | 8c6bc69 | 2009-09-16 15:04:38 -0700 | [diff] [blame] | 104 | } |
| 105 | |
| 106 | |
Alex Sakhartchouk | afb743a | 2010-11-09 17:00:54 -0800 | [diff] [blame] | 107 | void rsi_ScriptInvokeData(Context *rsc, RsScript vs, uint32_t slot, void *data) { |
Jason Sams | be36bf3 | 2010-05-11 14:03:58 -0700 | [diff] [blame] | 108 | Script *s = static_cast<Script *>(vs); |
Jason Sams | 22fa371 | 2010-05-19 17:22:57 -0700 | [diff] [blame] | 109 | s->Invoke(rsc, slot, NULL, 0); |
Jason Sams | be36bf3 | 2010-05-11 14:03:58 -0700 | [diff] [blame] | 110 | } |
| 111 | |
Alex Sakhartchouk | afb743a | 2010-11-09 17:00:54 -0800 | [diff] [blame] | 112 | void rsi_ScriptInvokeV(Context *rsc, RsScript vs, uint32_t slot, const void *data, uint32_t len) { |
Jason Sams | be36bf3 | 2010-05-11 14:03:58 -0700 | [diff] [blame] | 113 | Script *s = static_cast<Script *>(vs); |
Jason Sams | 22fa371 | 2010-05-19 17:22:57 -0700 | [diff] [blame] | 114 | s->Invoke(rsc, slot, data, len); |
Jason Sams | be36bf3 | 2010-05-11 14:03:58 -0700 | [diff] [blame] | 115 | } |
| 116 | |
Alex Sakhartchouk | afb743a | 2010-11-09 17:00:54 -0800 | [diff] [blame] | 117 | void rsi_ScriptSetVarI(Context *rsc, RsScript vs, uint32_t slot, int value) { |
Jason Sams | be36bf3 | 2010-05-11 14:03:58 -0700 | [diff] [blame] | 118 | Script *s = static_cast<Script *>(vs); |
| 119 | s->setVar(slot, &value, sizeof(value)); |
| 120 | } |
| 121 | |
Jason Sams | a5eb6e1 | 2010-11-16 17:37:02 -0800 | [diff] [blame] | 122 | void rsi_ScriptSetVarObj(Context *rsc, RsScript vs, uint32_t slot, RsObjectBase value) { |
| 123 | Script *s = static_cast<Script *>(vs); |
| 124 | ObjectBase *o = static_cast<ObjectBase *>(value); |
| 125 | s->setVarObj(slot, o); |
| 126 | } |
| 127 | |
Alex Sakhartchouk | afb743a | 2010-11-09 17:00:54 -0800 | [diff] [blame] | 128 | 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] | 129 | Script *s = static_cast<Script *>(vs); |
| 130 | s->setVar(slot, &value, sizeof(value)); |
| 131 | } |
| 132 | |
Alex Sakhartchouk | afb743a | 2010-11-09 17:00:54 -0800 | [diff] [blame] | 133 | void rsi_ScriptSetVarF(Context *rsc, RsScript vs, uint32_t slot, float value) { |
Jason Sams | be36bf3 | 2010-05-11 14:03:58 -0700 | [diff] [blame] | 134 | Script *s = static_cast<Script *>(vs); |
| 135 | s->setVar(slot, &value, sizeof(value)); |
| 136 | } |
| 137 | |
Alex Sakhartchouk | afb743a | 2010-11-09 17:00:54 -0800 | [diff] [blame] | 138 | void rsi_ScriptSetVarD(Context *rsc, RsScript vs, uint32_t slot, double value) { |
Stephen Hines | 6d0a074 | 2010-09-20 17:20:30 -0700 | [diff] [blame] | 139 | Script *s = static_cast<Script *>(vs); |
| 140 | s->setVar(slot, &value, sizeof(value)); |
| 141 | } |
| 142 | |
Alex Sakhartchouk | afb743a | 2010-11-09 17:00:54 -0800 | [diff] [blame] | 143 | void rsi_ScriptSetVarV(Context *rsc, RsScript vs, uint32_t slot, const void *data, uint32_t len) { |
Jason Sams | be36bf3 | 2010-05-11 14:03:58 -0700 | [diff] [blame] | 144 | const float *fp = (const float *)data; |
| 145 | Script *s = static_cast<Script *>(vs); |
| 146 | s->setVar(slot, data, len); |
Jason Sams | fa51719 | 2009-08-13 12:59:04 -0700 | [diff] [blame] | 147 | } |
| 148 | |
Jason Sams | 326e0dd | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 149 | } |
| 150 | } |
| 151 | |