blob: efdc626071f837d96eb25f3db840f177f9699e53 [file] [log] [blame]
Jason Sams326e0dd2009-05-22 14:03:28 -07001/*
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
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -080022Script::Script(Context *rsc) : ObjectBase(rsc) {
Jason Sams928b7342009-06-08 18:50:13 -070023 memset(&mEnviroment, 0, sizeof(mEnviroment));
Alex Sakhartchouk700ba382010-10-08 15:00:05 -070024
25 mSlots = NULL;
26 mTypes = NULL;
Jason Sams326e0dd2009-05-22 14:03:28 -070027}
28
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -080029Script::~Script() {
30 if (mSlots) {
Alex Sakhartchouk700ba382010-10-08 15:00:05 -070031 delete [] mSlots;
32 mSlots = NULL;
33 }
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -080034 if (mTypes) {
Alex Sakhartchouk700ba382010-10-08 15:00:05 -070035 delete [] mTypes;
36 mTypes = NULL;
37 }
38}
39
40void Script::initSlots() {
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -080041 if (mEnviroment.mFieldCount > 0) {
Alex Sakhartchouk700ba382010-10-08 15:00:05 -070042 mSlots = new ObjectBaseRef<Allocation>[mEnviroment.mFieldCount];
43 mTypes = new ObjectBaseRef<const Type>[mEnviroment.mFieldCount];
44 }
45}
46
47void Script::setSlot(uint32_t slot, Allocation *a) {
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -080048 if (slot >= mEnviroment.mFieldCount) {
Alex Sakhartchouk700ba382010-10-08 15:00:05 -070049 LOGE("Script::setSlot unable to set allocation, invalid slot index");
50 return;
51 }
52
53 mSlots[slot].set(a);
Jason Sams326e0dd2009-05-22 14:03:28 -070054}
55
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -080056void Script::setVar(uint32_t slot, const void *val, uint32_t len) {
Jason Samsbe36bf32010-05-11 14:03:58 -070057 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 Samsd7e54812010-10-10 17:58:25 -070064 //if (rsc->props.mLogScripts) {
65 LOGV("Calling setVar on slot = %i which is null", slot);
66 //}
Jason Samsbe36bf32010-05-11 14:03:58 -070067 }
68}
69
Jason Samsa5eb6e12010-11-16 17:37:02 -080070void 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 Sams326e0dd2009-05-22 14:03:28 -070086namespace android {
87namespace renderscript {
88
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -080089void rsi_ScriptBindAllocation(Context * rsc, RsScript vs, RsAllocation va, uint32_t slot) {
Jason Sams326e0dd2009-05-22 14:03:28 -070090 Script *s = static_cast<Script *>(vs);
Jason Samsbe36bf32010-05-11 14:03:58 -070091 Allocation *a = static_cast<Allocation *>(va);
Alex Sakhartchouk700ba382010-10-08 15:00:05 -070092 s->setSlot(slot, a);
Jason Samsbe36bf32010-05-11 14:03:58 -070093 //LOGE("rsi_ScriptBindAllocation %i %p %p", slot, a, a->getPtr());
Jason Sams326e0dd2009-05-22 14:03:28 -070094}
95
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -080096void rsi_ScriptSetTimeZone(Context * rsc, RsScript vs, const char * timeZone, uint32_t length) {
Jason Samsd34b7252009-08-04 16:58:20 -070097 Script *s = static_cast<Script *>(vs);
98 s->mEnviroment.mTimeZone = timeZone;
99}
100
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800101void rsi_ScriptInvoke(Context *rsc, RsScript vs, uint32_t slot) {
Jason Sams8c6bc692009-09-16 15:04:38 -0700102 Script *s = static_cast<Script *>(vs);
Jason Sams22fa3712010-05-19 17:22:57 -0700103 s->Invoke(rsc, slot, NULL, 0);
Jason Sams8c6bc692009-09-16 15:04:38 -0700104}
105
106
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800107void rsi_ScriptInvokeData(Context *rsc, RsScript vs, uint32_t slot, void *data) {
Jason Samsbe36bf32010-05-11 14:03:58 -0700108 Script *s = static_cast<Script *>(vs);
Jason Sams22fa3712010-05-19 17:22:57 -0700109 s->Invoke(rsc, slot, NULL, 0);
Jason Samsbe36bf32010-05-11 14:03:58 -0700110}
111
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800112void rsi_ScriptInvokeV(Context *rsc, RsScript vs, uint32_t slot, const void *data, uint32_t len) {
Jason Samsbe36bf32010-05-11 14:03:58 -0700113 Script *s = static_cast<Script *>(vs);
Jason Sams22fa3712010-05-19 17:22:57 -0700114 s->Invoke(rsc, slot, data, len);
Jason Samsbe36bf32010-05-11 14:03:58 -0700115}
116
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800117void rsi_ScriptSetVarI(Context *rsc, RsScript vs, uint32_t slot, int value) {
Jason Samsbe36bf32010-05-11 14:03:58 -0700118 Script *s = static_cast<Script *>(vs);
119 s->setVar(slot, &value, sizeof(value));
120}
121
Jason Samsa5eb6e12010-11-16 17:37:02 -0800122void 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 Sakhartchoukafb743a2010-11-09 17:00:54 -0800128void rsi_ScriptSetVarJ(Context *rsc, RsScript vs, uint32_t slot, long long value) {
Stephen Hines0977c942010-10-11 10:54:21 -0700129 Script *s = static_cast<Script *>(vs);
130 s->setVar(slot, &value, sizeof(value));
131}
132
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800133void rsi_ScriptSetVarF(Context *rsc, RsScript vs, uint32_t slot, float value) {
Jason Samsbe36bf32010-05-11 14:03:58 -0700134 Script *s = static_cast<Script *>(vs);
135 s->setVar(slot, &value, sizeof(value));
136}
137
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800138void rsi_ScriptSetVarD(Context *rsc, RsScript vs, uint32_t slot, double value) {
Stephen Hines6d0a0742010-09-20 17:20:30 -0700139 Script *s = static_cast<Script *>(vs);
140 s->setVar(slot, &value, sizeof(value));
141}
142
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800143void rsi_ScriptSetVarV(Context *rsc, RsScript vs, uint32_t slot, const void *data, uint32_t len) {
Jason Samsbe36bf32010-05-11 14:03:58 -0700144 const float *fp = (const float *)data;
145 Script *s = static_cast<Script *>(vs);
146 s->setVar(slot, data, len);
Jason Samsfa517192009-08-13 12:59:04 -0700147}
148
Jason Sams326e0dd2009-05-22 14:03:28 -0700149}
150}
151