1/* 2 * Copyright (C) 2009-2012 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#include <time.h> 19 20using namespace android; 21using namespace android::renderscript; 22 23Script::Script(Context *rsc) : ObjectBase(rsc) { 24 memset(&mEnviroment, 0, sizeof(mEnviroment)); 25 memset(&mHal, 0, sizeof(mHal)); 26 27 mSlots = NULL; 28 mTypes = NULL; 29 mInitialized = false; 30 mHasObjectSlots = false; 31} 32 33Script::~Script() { 34 if (mSlots) { 35 delete [] mSlots; 36 mSlots = NULL; 37 } 38 if (mTypes) { 39 delete [] mTypes; 40 mTypes = NULL; 41 } 42} 43 44void Script::setSlot(uint32_t slot, Allocation *a) { 45 //ALOGE("setSlot %i %p", slot, a); 46 if (slot >= mHal.info.exportedVariableCount) { 47 ALOGE("Script::setSlot unable to set allocation, invalid slot index"); 48 return; 49 } 50 51 mSlots[slot].set(a); 52 mHasObjectSlots = true; 53 mRSC->mHal.funcs.script.setGlobalBind(mRSC, this, slot, a); 54} 55 56void Script::setVar(uint32_t slot, const void *val, size_t len) { 57 //ALOGE("setVar %i %p %i", slot, val, len); 58 if (slot >= mHal.info.exportedVariableCount) { 59 ALOGE("Script::setVar unable to set allocation, invalid slot index"); 60 return; 61 } 62 mRSC->mHal.funcs.script.setGlobalVar(mRSC, this, slot, (void *)val, len); 63} 64 65void Script::getVar(uint32_t slot, const void *val, size_t len) { 66 //ALOGE("getVar %i %p %i", slot, val, len); 67 if (slot >= mHal.info.exportedVariableCount) { 68 ALOGE("Script::getVar unable to set allocation, invalid slot index"); 69 return; 70 } 71 mRSC->mHal.funcs.script.getGlobalVar(mRSC, this, slot, (void *)val, len); 72} 73 74void Script::setVar(uint32_t slot, const void *val, size_t len, Element *e, 75 const size_t *dims, size_t dimLen) { 76 if (slot >= mHal.info.exportedVariableCount) { 77 ALOGE("Script::setVar unable to set allocation, invalid slot index"); 78 return; 79 } 80 mRSC->mHal.funcs.script.setGlobalVarWithElemDims(mRSC, this, slot, 81 (void *)val, len, e, dims, dimLen); 82} 83 84void Script::setVarObj(uint32_t slot, ObjectBase *val) { 85 //ALOGE("setVarObj %i %p", slot, val); 86 if (slot >= mHal.info.exportedVariableCount) { 87 ALOGE("Script::setVarObj unable to set allocation, invalid slot index"); 88 return; 89 } 90 mHasObjectSlots = true; 91 //ALOGE("setvarobj %i %p", slot, val); 92 mRSC->mHal.funcs.script.setGlobalObj(mRSC, this, slot, val); 93} 94 95bool Script::freeChildren() { 96 incSysRef(); 97 mRSC->mHal.funcs.script.invokeFreeChildren(mRSC, this); 98 return decSysRef(); 99} 100 101ScriptKernelID::ScriptKernelID(Context *rsc, Script *s, int slot, int sig) 102 : ObjectBase(rsc) { 103 104 mScript = s; 105 mSlot = slot; 106 mHasKernelInput = (sig & 1) != 0; 107 mHasKernelOutput = (sig & 2) != 0; 108} 109 110ScriptKernelID::~ScriptKernelID() { 111 112} 113 114void ScriptKernelID::serialize(Context *rsc, OStream *stream) const { 115 116} 117 118RsA3DClassID ScriptKernelID::getClassId() const { 119 return RS_A3D_CLASS_ID_SCRIPT_KERNEL_ID; 120} 121 122ScriptFieldID::ScriptFieldID(Context *rsc, Script *s, int slot) : ObjectBase(rsc) { 123 mScript = s; 124 mSlot = slot; 125} 126 127ScriptFieldID::~ScriptFieldID() { 128 129} 130 131void ScriptFieldID::serialize(Context *rsc, OStream *stream) const { 132 133} 134 135RsA3DClassID ScriptFieldID::getClassId() const { 136 return RS_A3D_CLASS_ID_SCRIPT_FIELD_ID; 137} 138 139 140namespace android { 141namespace renderscript { 142 143RsScriptKernelID rsi_ScriptKernelIDCreate(Context *rsc, RsScript vs, int slot, int sig) { 144 ScriptKernelID *kid = new ScriptKernelID(rsc, (Script *)vs, slot, sig); 145 kid->incUserRef(); 146 return kid; 147} 148 149RsScriptFieldID rsi_ScriptFieldIDCreate(Context *rsc, RsScript vs, int slot) { 150 ScriptFieldID *fid = new ScriptFieldID(rsc, (Script *)vs, slot); 151 fid->incUserRef(); 152 return fid; 153} 154 155void rsi_ScriptBindAllocation(Context * rsc, RsScript vs, RsAllocation va, uint32_t slot) { 156 Script *s = static_cast<Script *>(vs); 157 Allocation *a = static_cast<Allocation *>(va); 158 s->setSlot(slot, a); 159} 160 161void rsi_ScriptSetTimeZone(Context * rsc, RsScript vs, const char * timeZone, size_t length) { 162 // We unfortunately need to make a new copy of the string, since it is 163 // not NULL-terminated. We then use setenv(), which properly handles 164 // freeing/duplicating the actual string for the environment. 165 char *tz = (char *) malloc(length + 1); 166 if (!tz) { 167 ALOGE("Couldn't allocate memory for timezone buffer"); 168 return; 169 } 170 strncpy(tz, timeZone, length); 171 tz[length] = '\0'; 172 if (setenv("TZ", tz, 1) == 0) { 173 tzset(); 174 } else { 175 ALOGE("Error setting timezone"); 176 } 177 free(tz); 178} 179 180void rsi_ScriptForEach(Context *rsc, RsScript vs, uint32_t slot, 181 RsAllocation vain, RsAllocation vaout, 182 const void *params, size_t paramLen, 183 const RsScriptCall *sc, size_t scLen) { 184 Script *s = static_cast<Script *>(vs); 185 // The rs.spec generated code does not handle the absence of an actual 186 // input for sc. Instead, it retains an existing pointer value (the prior 187 // field in the packed data object). This can cause confusion because 188 // drivers might now inspect bogus sc data. 189 if (scLen == 0) { 190 sc = NULL; 191 } 192 s->runForEach(rsc, slot, 193 static_cast<const Allocation *>(vain), static_cast<Allocation *>(vaout), 194 params, paramLen, sc); 195 196} 197 198void rsi_ScriptInvoke(Context *rsc, RsScript vs, uint32_t slot) { 199 Script *s = static_cast<Script *>(vs); 200 s->Invoke(rsc, slot, NULL, 0); 201} 202 203 204void rsi_ScriptInvokeData(Context *rsc, RsScript vs, uint32_t slot, void *data) { 205 Script *s = static_cast<Script *>(vs); 206 s->Invoke(rsc, slot, NULL, 0); 207} 208 209void rsi_ScriptInvokeV(Context *rsc, RsScript vs, uint32_t slot, const void *data, size_t len) { 210 Script *s = static_cast<Script *>(vs); 211 s->Invoke(rsc, slot, data, len); 212} 213 214void rsi_ScriptSetVarI(Context *rsc, RsScript vs, uint32_t slot, int value) { 215 Script *s = static_cast<Script *>(vs); 216 s->setVar(slot, &value, sizeof(value)); 217} 218 219void rsi_ScriptSetVarObj(Context *rsc, RsScript vs, uint32_t slot, RsObjectBase value) { 220 Script *s = static_cast<Script *>(vs); 221 ObjectBase *o = static_cast<ObjectBase *>(value); 222 s->setVarObj(slot, o); 223} 224 225void rsi_ScriptSetVarJ(Context *rsc, RsScript vs, uint32_t slot, int64_t value) { 226 Script *s = static_cast<Script *>(vs); 227 s->setVar(slot, &value, sizeof(value)); 228} 229 230void rsi_ScriptSetVarF(Context *rsc, RsScript vs, uint32_t slot, float value) { 231 Script *s = static_cast<Script *>(vs); 232 s->setVar(slot, &value, sizeof(value)); 233} 234 235void rsi_ScriptSetVarD(Context *rsc, RsScript vs, uint32_t slot, double value) { 236 Script *s = static_cast<Script *>(vs); 237 s->setVar(slot, &value, sizeof(value)); 238} 239 240void rsi_ScriptSetVarV(Context *rsc, RsScript vs, uint32_t slot, const void *data, size_t len) { 241 Script *s = static_cast<Script *>(vs); 242 s->setVar(slot, data, len); 243} 244 245void rsi_ScriptGetVarV(Context *rsc, RsScript vs, uint32_t slot, void *data, size_t len) { 246 Script *s = static_cast<Script *>(vs); 247 s->getVar(slot, data, len); 248} 249 250void rsi_ScriptSetVarVE(Context *rsc, RsScript vs, uint32_t slot, 251 const void *data, size_t len, RsElement ve, 252 const size_t *dims, size_t dimLen) { 253 Script *s = static_cast<Script *>(vs); 254 Element *e = static_cast<Element *>(ve); 255 s->setVar(slot, data, len, e, dims, dimLen); 256} 257 258} 259} 260 261