rsScript.cpp revision 225afd317e101a7be5fe02c0a86361146ea89f05
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
19using namespace android;
20using namespace android::renderscript;
21
22Script::Script(Context *rsc) : ObjectBase(rsc)
23{
24    memset(&mEnviroment, 0, sizeof(mEnviroment));
25
26    mSlots = NULL;
27    mTypes = NULL;
28}
29
30Script::~Script()
31{
32    if(mSlots) {
33        delete [] mSlots;
34        mSlots = NULL;
35    }
36    if(mTypes) {
37        delete [] mTypes;
38        mTypes = NULL;
39    }
40}
41
42void Script::initSlots() {
43    if(mEnviroment.mFieldCount > 0) {
44        mSlots = new ObjectBaseRef<Allocation>[mEnviroment.mFieldCount];
45        mTypes = new ObjectBaseRef<const Type>[mEnviroment.mFieldCount];
46    }
47}
48
49void Script::setSlot(uint32_t slot, Allocation *a) {
50    if(slot >= mEnviroment.mFieldCount) {
51        LOGE("Script::setSlot unable to set allocation, invalid slot index");
52        return;
53    }
54
55    mSlots[slot].set(a);
56}
57
58void Script::setVar(uint32_t slot, const void *val, uint32_t len)
59{
60    int32_t *destPtr = ((int32_t **)mEnviroment.mFieldAddress)[slot];
61    if (destPtr) {
62        //LOGE("setVar f1  %f", ((const float *)destPtr)[0]);
63        //LOGE("setVar %p %i", destPtr, len);
64        memcpy(destPtr, val, len);
65        //LOGE("setVar f2  %f", ((const float *)destPtr)[0]);
66    } else {
67        //if (rsc->props.mLogScripts) {
68            LOGV("Calling setVar on slot = %i which is null", slot);
69        //}
70    }
71}
72
73namespace android {
74namespace renderscript {
75
76
77void rsi_ScriptBindAllocation(Context * rsc, RsScript vs, RsAllocation va, uint32_t slot)
78{
79    Script *s = static_cast<Script *>(vs);
80    Allocation *a = static_cast<Allocation *>(va);
81    s->setSlot(slot, a);
82    //LOGE("rsi_ScriptBindAllocation %i  %p  %p", slot, a, a->getPtr());
83}
84
85void rsi_ScriptSetTimeZone(Context * rsc, RsScript vs, const char * timeZone, uint32_t length)
86{
87    Script *s = static_cast<Script *>(vs);
88    s->mEnviroment.mTimeZone = timeZone;
89}
90
91void rsi_ScriptInvoke(Context *rsc, RsScript vs, uint32_t slot)
92{
93    Script *s = static_cast<Script *>(vs);
94    s->Invoke(rsc, slot, NULL, 0);
95}
96
97
98void rsi_ScriptInvokeData(Context *rsc, RsScript vs, uint32_t slot, void *data)
99{
100    Script *s = static_cast<Script *>(vs);
101    s->Invoke(rsc, slot, NULL, 0);
102}
103
104void rsi_ScriptInvokeV(Context *rsc, RsScript vs, uint32_t slot, const void *data, uint32_t len)
105{
106    Script *s = static_cast<Script *>(vs);
107    s->Invoke(rsc, slot, data, len);
108}
109
110void rsi_ScriptSetVarI(Context *rsc, RsScript vs, uint32_t slot, int value)
111{
112    Script *s = static_cast<Script *>(vs);
113    s->setVar(slot, &value, sizeof(value));
114}
115
116void rsi_ScriptSetVarJ(Context *rsc, RsScript vs, uint32_t slot, long long value)
117{
118    Script *s = static_cast<Script *>(vs);
119    s->setVar(slot, &value, sizeof(value));
120}
121
122void rsi_ScriptSetVarF(Context *rsc, RsScript vs, uint32_t slot, float value)
123{
124    Script *s = static_cast<Script *>(vs);
125    s->setVar(slot, &value, sizeof(value));
126}
127
128void rsi_ScriptSetVarD(Context *rsc, RsScript vs, uint32_t slot, double value)
129{
130    Script *s = static_cast<Script *>(vs);
131    s->setVar(slot, &value, sizeof(value));
132}
133
134void rsi_ScriptSetVarV(Context *rsc, RsScript vs, uint32_t slot, const void *data, uint32_t len)
135{
136    const float *fp = (const float *)data;
137    Script *s = static_cast<Script *>(vs);
138    s->setVar(slot, data, len);
139}
140
141
142}
143}
144
145