Jason Sams | d19f10d | 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 | |
Jason Sams | a9e7a05 | 2009-09-25 14:51:22 -0700 | [diff] [blame] | 22 | Script::Script(Context *rsc) : ObjectBase(rsc) |
Jason Sams | d19f10d | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 23 | { |
Jason Sams | 61f08d6 | 2009-09-25 16:37:33 -0700 | [diff] [blame] | 24 | mAllocFile = __FILE__; |
| 25 | mAllocLine = __LINE__; |
Jason Sams | 928f5cf | 2009-06-08 18:50:13 -0700 | [diff] [blame] | 26 | memset(&mEnviroment, 0, sizeof(mEnviroment)); |
| 27 | mEnviroment.mClearColor[0] = 0; |
| 28 | mEnviroment.mClearColor[1] = 0; |
| 29 | mEnviroment.mClearColor[2] = 0; |
| 30 | mEnviroment.mClearColor[3] = 1; |
| 31 | mEnviroment.mClearDepth = 1; |
Jason Sams | be2e841 | 2009-09-16 15:04:38 -0700 | [diff] [blame] | 32 | mEnviroment.mClearStencil = 0; |
Jason Sams | d19f10d | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 33 | } |
| 34 | |
| 35 | Script::~Script() |
| 36 | { |
| 37 | } |
| 38 | |
Jason Sams | 4d33993 | 2010-05-11 14:03:58 -0700 | [diff] [blame] | 39 | void Script::setVar(uint32_t slot, const void *val, uint32_t len) |
| 40 | { |
| 41 | int32_t *destPtr = ((int32_t **)mEnviroment.mFieldAddress)[slot]; |
| 42 | if (destPtr) { |
| 43 | //LOGE("setVar f1 %f", ((const float *)destPtr)[0]); |
| 44 | //LOGE("setVar %p %i", destPtr, len); |
| 45 | memcpy(destPtr, val, len); |
| 46 | //LOGE("setVar f2 %f", ((const float *)destPtr)[0]); |
| 47 | } else { |
| 48 | LOGE("Calling setVar on slot = %i which is null", slot); |
| 49 | } |
| 50 | } |
| 51 | |
Jason Sams | d19f10d | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 52 | namespace android { |
| 53 | namespace renderscript { |
| 54 | |
| 55 | |
Jason Sams | d19f10d | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 56 | void rsi_ScriptBindAllocation(Context * rsc, RsScript vs, RsAllocation va, uint32_t slot) |
| 57 | { |
| 58 | Script *s = static_cast<Script *>(vs); |
Jason Sams | 4d33993 | 2010-05-11 14:03:58 -0700 | [diff] [blame] | 59 | Allocation *a = static_cast<Allocation *>(va); |
| 60 | s->mSlots[slot].set(a); |
| 61 | //LOGE("rsi_ScriptBindAllocation %i %p %p", slot, a, a->getPtr()); |
Jason Sams | d19f10d | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 62 | } |
| 63 | |
Jason Sams | 2253417 | 2009-08-04 16:58:20 -0700 | [diff] [blame] | 64 | void rsi_ScriptSetClearColor(Context * rsc, RsScript vs, float r, float g, float b, float a) |
| 65 | { |
| 66 | Script *s = static_cast<Script *>(vs); |
| 67 | s->mEnviroment.mClearColor[0] = r; |
| 68 | s->mEnviroment.mClearColor[1] = g; |
| 69 | s->mEnviroment.mClearColor[2] = b; |
| 70 | s->mEnviroment.mClearColor[3] = a; |
| 71 | } |
| 72 | |
| 73 | void rsi_ScriptSetTimeZone(Context * rsc, RsScript vs, const char * timeZone, uint32_t length) |
| 74 | { |
| 75 | Script *s = static_cast<Script *>(vs); |
| 76 | s->mEnviroment.mTimeZone = timeZone; |
| 77 | } |
| 78 | |
| 79 | void rsi_ScriptSetClearDepth(Context * rsc, RsScript vs, float v) |
| 80 | { |
| 81 | Script *s = static_cast<Script *>(vs); |
| 82 | s->mEnviroment.mClearDepth = v; |
| 83 | } |
| 84 | |
| 85 | void rsi_ScriptSetClearStencil(Context * rsc, RsScript vs, uint32_t v) |
| 86 | { |
| 87 | Script *s = static_cast<Script *>(vs); |
| 88 | s->mEnviroment.mClearStencil = v; |
| 89 | } |
Jason Sams | d19f10d | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 90 | |
Jason Sams | 334ea0c | 2009-08-17 13:56:09 -0700 | [diff] [blame] | 91 | void rsi_ScriptSetType(Context * rsc, RsType vt, uint32_t slot, bool writable, const char *name) |
Jason Sams | fbf0b9e | 2009-08-13 12:59:04 -0700 | [diff] [blame] | 92 | { |
| 93 | ScriptCState *ss = &rsc->mScriptC; |
| 94 | const Type *t = static_cast<const Type *>(vt); |
| 95 | ss->mConstantBufferTypes[slot].set(t); |
Jason Sams | 334ea0c | 2009-08-17 13:56:09 -0700 | [diff] [blame] | 96 | ss->mSlotWritable[slot] = writable; |
Jason Sams | 4d33993 | 2010-05-11 14:03:58 -0700 | [diff] [blame] | 97 | LOGE("rsi_ScriptSetType"); |
Jason Sams | fbf0b9e | 2009-08-13 12:59:04 -0700 | [diff] [blame] | 98 | } |
| 99 | |
Jason Sams | be2e841 | 2009-09-16 15:04:38 -0700 | [diff] [blame] | 100 | void rsi_ScriptInvoke(Context *rsc, RsScript vs, uint32_t slot) |
| 101 | { |
Jason Sams | 4d33993 | 2010-05-11 14:03:58 -0700 | [diff] [blame] | 102 | //LOGE("rsi_ScriptInvoke %i", slot); |
Jason Sams | be2e841 | 2009-09-16 15:04:38 -0700 | [diff] [blame] | 103 | Script *s = static_cast<Script *>(vs); |
Jason Sams | 4d33993 | 2010-05-11 14:03:58 -0700 | [diff] [blame] | 104 | if ((slot >= s->mEnviroment.mInvokeFunctionCount) || |
| 105 | (s->mEnviroment.mInvokeFunctions[slot] == NULL)) { |
Jason Sams | 156cce6 | 2010-03-03 13:03:18 -0800 | [diff] [blame] | 106 | rsc->setError(RS_ERROR_BAD_SCRIPT, "Calling invoke on bad script"); |
| 107 | return; |
| 108 | } |
Jason Sams | e60446b | 2009-09-24 14:55:38 -0700 | [diff] [blame] | 109 | s->setupScript(); |
Jason Sams | 4d33993 | 2010-05-11 14:03:58 -0700 | [diff] [blame] | 110 | //LOGE("invoking %i %p", slot, s->mEnviroment.mInvokeFunctions[slot]); |
| 111 | s->mEnviroment.mInvokeFunctions[slot](); |
| 112 | //LOGE("invoke finished"); |
Jason Sams | be2e841 | 2009-09-16 15:04:38 -0700 | [diff] [blame] | 113 | } |
| 114 | |
| 115 | |
Jason Sams | 4d33993 | 2010-05-11 14:03:58 -0700 | [diff] [blame] | 116 | void rsi_ScriptInvokeData(Context *rsc, RsScript vs, uint32_t slot, void *data) |
| 117 | { |
| 118 | //LOGE("rsi_ScriptInvoke %i", slot); |
| 119 | Script *s = static_cast<Script *>(vs); |
| 120 | if ((slot >= s->mEnviroment.mInvokeFunctionCount) || |
| 121 | (s->mEnviroment.mInvokeFunctions[slot] == NULL)) { |
| 122 | rsc->setError(RS_ERROR_BAD_SCRIPT, "Calling invoke on bad script"); |
| 123 | return; |
| 124 | } |
| 125 | s->setupScript(); |
| 126 | //LOGE("invoking %i %p", slot, s->mEnviroment.mInvokeFunctions[slot]); |
| 127 | s->mEnviroment.mInvokeFunctions[slot](); |
| 128 | //LOGE("invoke finished"); |
| 129 | } |
| 130 | |
| 131 | void rsi_ScriptInvokeV(Context *rsc, RsScript vs, uint32_t slot, const void *data, uint32_t len) |
| 132 | { |
| 133 | //LOGE("rsi_ScriptInvoke %i", slot); |
| 134 | Script *s = static_cast<Script *>(vs); |
| 135 | if ((slot >= s->mEnviroment.mInvokeFunctionCount) || |
| 136 | (s->mEnviroment.mInvokeFunctions[slot] == NULL)) { |
| 137 | rsc->setError(RS_ERROR_BAD_SCRIPT, "Calling invoke on bad script"); |
| 138 | return; |
| 139 | } |
| 140 | s->setupScript(); |
| 141 | |
Jason Sams | f603d21 | 2010-05-14 15:30:29 -0700 | [diff] [blame] | 142 | //LOGE("rsi_ScriptInvokeV, len=%i", len); |
Jason Sams | 4d33993 | 2010-05-11 14:03:58 -0700 | [diff] [blame] | 143 | const uint32_t * dPtr = (const uint32_t *)data; |
| 144 | switch(len) { |
| 145 | case 0: |
| 146 | s->mEnviroment.mInvokeFunctions[slot](); |
| 147 | break; |
| 148 | case 4: |
| 149 | ((void (*)(uint32_t)) |
| 150 | s->mEnviroment.mInvokeFunctions[slot])(dPtr[0]); |
| 151 | break; |
| 152 | case 8: |
| 153 | ((void (*)(uint32_t, uint32_t)) |
| 154 | s->mEnviroment.mInvokeFunctions[slot])(dPtr[0], dPtr[1]); |
| 155 | break; |
| 156 | case 12: |
| 157 | ((void (*)(uint32_t, uint32_t, uint32_t)) |
| 158 | s->mEnviroment.mInvokeFunctions[slot])(dPtr[0], dPtr[1], dPtr[2]); |
| 159 | break; |
| 160 | case 16: |
| 161 | ((void (*)(uint32_t, uint32_t, uint32_t, uint32_t)) |
| 162 | s->mEnviroment.mInvokeFunctions[slot])(dPtr[0], dPtr[1], dPtr[2], dPtr[3]); |
| 163 | break; |
| 164 | case 20: |
| 165 | ((void (*)(uint32_t, uint32_t, uint32_t, uint32_t, uint32_t)) |
| 166 | s->mEnviroment.mInvokeFunctions[slot])(dPtr[0], dPtr[1], dPtr[2], dPtr[3], dPtr[4]); |
| 167 | break; |
| 168 | |
| 169 | |
| 170 | } |
| 171 | |
| 172 | } |
| 173 | |
Jason Sams | 4d33993 | 2010-05-11 14:03:58 -0700 | [diff] [blame] | 174 | void rsi_ScriptSetVarI(Context *rsc, RsScript vs, uint32_t slot, int value) |
| 175 | { |
| 176 | Script *s = static_cast<Script *>(vs); |
| 177 | s->setVar(slot, &value, sizeof(value)); |
| 178 | } |
| 179 | |
| 180 | void rsi_ScriptSetVarF(Context *rsc, RsScript vs, uint32_t slot, float value) |
| 181 | { |
| 182 | Script *s = static_cast<Script *>(vs); |
| 183 | s->setVar(slot, &value, sizeof(value)); |
| 184 | } |
| 185 | |
| 186 | void rsi_ScriptSetVarV(Context *rsc, RsScript vs, uint32_t slot, const void *data, uint32_t len) |
| 187 | { |
| 188 | const float *fp = (const float *)data; |
| 189 | Script *s = static_cast<Script *>(vs); |
| 190 | s->setVar(slot, data, len); |
Jason Sams | fbf0b9e | 2009-08-13 12:59:04 -0700 | [diff] [blame] | 191 | } |
| 192 | |
| 193 | |
Jason Sams | d19f10d | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 194 | } |
| 195 | } |
| 196 | |