Home | History | Annotate | Download | only in rs
      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 
     20 using namespace android;
     21 using namespace android::renderscript;
     22 
     23 Script::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 }
     31 
     32 Script::~Script() {
     33     if (mSlots) {
     34         delete [] mSlots;
     35         mSlots = NULL;
     36     }
     37     if (mTypes) {
     38         delete [] mTypes;
     39         mTypes = NULL;
     40     }
     41 }
     42 
     43 void Script::setSlot(uint32_t slot, Allocation *a) {
     44     //ALOGE("setSlot %i %p", slot, a);
     45     if (slot >= mHal.info.exportedVariableCount) {
     46         ALOGE("Script::setSlot unable to set allocation, invalid slot index");
     47         return;
     48     }
     49 
     50     mSlots[slot].set(a);
     51     mRSC->mHal.funcs.script.setGlobalBind(mRSC, this, slot, a);
     52 }
     53 
     54 void Script::setVar(uint32_t slot, const void *val, size_t len) {
     55     //ALOGE("setVar %i %p %i", slot, val, len);
     56     if (slot >= mHal.info.exportedVariableCount) {
     57         ALOGE("Script::setVar unable to set allocation, invalid slot index");
     58         return;
     59     }
     60     mRSC->mHal.funcs.script.setGlobalVar(mRSC, this, slot, (void *)val, len);
     61 }
     62 
     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 
     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 
     82 void Script::setVarObj(uint32_t slot, ObjectBase *val) {
     83     //ALOGE("setVarObj %i %p", slot, val);
     84     if (slot >= mHal.info.exportedVariableCount) {
     85         ALOGE("Script::setVarObj unable to set allocation, invalid slot index");
     86         return;
     87     }
     88     //ALOGE("setvarobj  %i %p", slot, val);
     89     mRSC->mHal.funcs.script.setGlobalObj(mRSC, this, slot, val);
     90 }
     91 
     92 bool Script::freeChildren() {
     93     incSysRef();
     94     mRSC->mHal.funcs.script.invokeFreeChildren(mRSC, this);
     95     return decSysRef();
     96 }
     97 
     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 
    137 namespace android {
    138 namespace renderscript {
    139 
    140 RsScriptKernelID rsi_ScriptKernelIDCreate(Context *rsc, RsScript vs, int slot, int sig) {
    141     ScriptKernelID *kid = new ScriptKernelID(rsc, (Script *)vs, slot, sig);
    142     kid->incUserRef();
    143     return kid;
    144 }
    145 
    146 RsScriptFieldID rsi_ScriptFieldIDCreate(Context *rsc, RsScript vs, int slot) {
    147     ScriptFieldID *fid = new ScriptFieldID(rsc, (Script *)vs, slot);
    148     fid->incUserRef();
    149     return fid;
    150 }
    151 
    152 void rsi_ScriptBindAllocation(Context * rsc, RsScript vs, RsAllocation va, uint32_t slot) {
    153     Script *s = static_cast<Script *>(vs);
    154     Allocation *a = static_cast<Allocation *>(va);
    155     s->setSlot(slot, a);
    156 }
    157 
    158 void rsi_ScriptSetTimeZone(Context * rsc, RsScript vs, const char * timeZone, size_t length) {
    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) {
    164         ALOGE("Couldn't allocate memory for timezone buffer");
    165         return;
    166     }
    167     strncpy(tz, timeZone, length);
    168     tz[length] = '\0';
    169     if (setenv("TZ", tz, 1) == 0) {
    170         tzset();
    171     } else {
    172         ALOGE("Error setting timezone");
    173     }
    174     free(tz);
    175 }
    176 
    177 void rsi_ScriptForEach(Context *rsc, RsScript vs, uint32_t slot,
    178                        RsAllocation vain, RsAllocation vaout,
    179                        const void *params, size_t paramLen,
    180                        const RsScriptCall *sc, size_t scLen) {
    181     Script *s = static_cast<Script *>(vs);
    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     }
    189     s->runForEach(rsc, slot,
    190                   static_cast<const Allocation *>(vain), static_cast<Allocation *>(vaout),
    191                   params, paramLen, sc);
    192 
    193 }
    194 
    195 void rsi_ScriptInvoke(Context *rsc, RsScript vs, uint32_t slot) {
    196     Script *s = static_cast<Script *>(vs);
    197     s->Invoke(rsc, slot, NULL, 0);
    198 }
    199 
    200 
    201 void rsi_ScriptInvokeData(Context *rsc, RsScript vs, uint32_t slot, void *data) {
    202     Script *s = static_cast<Script *>(vs);
    203     s->Invoke(rsc, slot, NULL, 0);
    204 }
    205 
    206 void rsi_ScriptInvokeV(Context *rsc, RsScript vs, uint32_t slot, const void *data, size_t len) {
    207     Script *s = static_cast<Script *>(vs);
    208     s->Invoke(rsc, slot, data, len);
    209 }
    210 
    211 void rsi_ScriptSetVarI(Context *rsc, RsScript vs, uint32_t slot, int value) {
    212     Script *s = static_cast<Script *>(vs);
    213     s->setVar(slot, &value, sizeof(value));
    214 }
    215 
    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 
    222 void rsi_ScriptSetVarJ(Context *rsc, RsScript vs, uint32_t slot, int64_t value) {
    223     Script *s = static_cast<Script *>(vs);
    224     s->setVar(slot, &value, sizeof(value));
    225 }
    226 
    227 void rsi_ScriptSetVarF(Context *rsc, RsScript vs, uint32_t slot, float value) {
    228     Script *s = static_cast<Script *>(vs);
    229     s->setVar(slot, &value, sizeof(value));
    230 }
    231 
    232 void rsi_ScriptSetVarD(Context *rsc, RsScript vs, uint32_t slot, double value) {
    233     Script *s = static_cast<Script *>(vs);
    234     s->setVar(slot, &value, sizeof(value));
    235 }
    236 
    237 void rsi_ScriptSetVarV(Context *rsc, RsScript vs, uint32_t slot, const void *data, size_t len) {
    238     Script *s = static_cast<Script *>(vs);
    239     s->setVar(slot, data, len);
    240 }
    241 
    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 
    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 
    255 }
    256 }
    257 
    258