blob: f61b983b6ce2cf518bbc797cc5ee633c3db4043f [file] [log] [blame]
Jason Samse45ac6e2009-07-20 14:31:06 -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#include "rsScriptC.h"
19#include "rsMatrix.h"
20
Joe Onorato9c4e4ca2009-08-09 11:39:02 -070021#include "utils/Timers.h"
Jason Samse45ac6e2009-07-20 14:31:06 -070022
Romain Guy98e10fd2009-07-30 18:45:01 -070023#include <time.h>
Romain Guy98e10fd2009-07-30 18:45:01 -070024
Jason Samse45ac6e2009-07-20 14:31:06 -070025using namespace android;
26using namespace android::renderscript;
27
28#define GET_TLS() Context::ScriptTLSStruct * tls = \
29 (Context::ScriptTLSStruct *)pthread_getspecific(Context::gThreadTLSKey); \
30 Context * rsc = tls->mContext; \
31 ScriptC * sc = (ScriptC *) tls->mScript
32
Jason Samsbe36bf32010-05-11 14:03:58 -070033
Jason Samse45ac6e2009-07-20 14:31:06 -070034//////////////////////////////////////////////////////////////////////////////
35// Math routines
36//////////////////////////////////////////////////////////////////////////////
37
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -080038static float SC_sinf_fast(float x) {
Romain Guy2275d632009-08-18 11:39:17 -070039 const float A = 1.0f / (2.0f * M_PI);
40 const float B = -16.0f;
41 const float C = 8.0f;
Jason Samsa57c0a72009-09-04 14:42:41 -070042
Romain Guy2275d632009-08-18 11:39:17 -070043 // scale angle for easy argument reduction
44 x *= A;
Jason Samsa57c0a72009-09-04 14:42:41 -070045
Romain Guy2275d632009-08-18 11:39:17 -070046 if (fabsf(x) >= 0.5f) {
47 // argument reduction
48 x = x - ceilf(x + 0.5f) + 1.0f;
49 }
Jason Samsa57c0a72009-09-04 14:42:41 -070050
Romain Guy2275d632009-08-18 11:39:17 -070051 const float y = B * x * fabsf(x) + C * x;
52 return 0.2215f * (y * fabsf(y) - y) + y;
53}
54
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -080055static float SC_cosf_fast(float x) {
Romain Guy2275d632009-08-18 11:39:17 -070056 x += float(M_PI / 2);
57
58 const float A = 1.0f / (2.0f * M_PI);
59 const float B = -16.0f;
60 const float C = 8.0f;
Jason Samsa57c0a72009-09-04 14:42:41 -070061
Romain Guy2275d632009-08-18 11:39:17 -070062 // scale angle for easy argument reduction
63 x *= A;
Jason Samsa57c0a72009-09-04 14:42:41 -070064
Romain Guy2275d632009-08-18 11:39:17 -070065 if (fabsf(x) >= 0.5f) {
66 // argument reduction
67 x = x - ceilf(x + 0.5f) + 1.0f;
68 }
Jason Samsa57c0a72009-09-04 14:42:41 -070069
Romain Guy2275d632009-08-18 11:39:17 -070070 const float y = B * x * fabsf(x) + C * x;
71 return 0.2215f * (y * fabsf(y) - y) + y;
72}
73
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -080074static float SC_randf(float max) {
Jason Samse45ac6e2009-07-20 14:31:06 -070075 float r = (float)rand();
Jason Sams366c9c82010-12-08 16:14:36 -080076 r *= max;
77 return r / RAND_MAX;
Jason Samse45ac6e2009-07-20 14:31:06 -070078}
79
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -080080static float SC_randf2(float min, float max) {
Romain Guy39dbc802009-07-31 11:20:59 -070081 float r = (float)rand();
Jason Sams366c9c82010-12-08 16:14:36 -080082 r = r * (max - min) + min;
83 return r / RAND_MAX;
Romain Guy39dbc802009-07-31 11:20:59 -070084}
85
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -080086static int SC_randi(int max) {
Jason Sams22fa3712010-05-19 17:22:57 -070087 return (int)SC_randf(max);
88}
89
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -080090static int SC_randi2(int min, int max) {
Jason Sams22fa3712010-05-19 17:22:57 -070091 return (int)SC_randf2(min, max);
92}
93
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -080094static float SC_frac(float v) {
Jason Samsbe36bf32010-05-11 14:03:58 -070095 int i = (int)floor(v);
96 return fmin(v - i, 0x1.fffffep-1f);
97}
98
Romain Guy98e10fd2009-07-30 18:45:01 -070099//////////////////////////////////////////////////////////////////////////////
100// Time routines
101//////////////////////////////////////////////////////////////////////////////
Jason Samse45ac6e2009-07-20 14:31:06 -0700102
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800103static int32_t SC_second() {
Romain Guy98e10fd2009-07-30 18:45:01 -0700104 GET_TLS();
105
106 time_t rawtime;
107 time(&rawtime);
108
Romain Guy519cdc92009-11-11 15:36:06 -0800109 struct tm *timeinfo;
110 timeinfo = localtime(&rawtime);
111 return timeinfo->tm_sec;
Romain Guy98e10fd2009-07-30 18:45:01 -0700112}
113
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800114static int32_t SC_minute() {
Romain Guy98e10fd2009-07-30 18:45:01 -0700115 GET_TLS();
Jason Samse5ffb872009-08-09 17:01:55 -0700116
Romain Guy98e10fd2009-07-30 18:45:01 -0700117 time_t rawtime;
118 time(&rawtime);
Jason Samse5ffb872009-08-09 17:01:55 -0700119
Romain Guy519cdc92009-11-11 15:36:06 -0800120 struct tm *timeinfo;
121 timeinfo = localtime(&rawtime);
122 return timeinfo->tm_min;
Jason Samse5ffb872009-08-09 17:01:55 -0700123}
Romain Guy98e10fd2009-07-30 18:45:01 -0700124
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800125static int32_t SC_hour() {
Romain Guy98e10fd2009-07-30 18:45:01 -0700126 GET_TLS();
Jason Samse5ffb872009-08-09 17:01:55 -0700127
Romain Guy98e10fd2009-07-30 18:45:01 -0700128 time_t rawtime;
129 time(&rawtime);
Jason Samse5ffb872009-08-09 17:01:55 -0700130
Romain Guy519cdc92009-11-11 15:36:06 -0800131 struct tm *timeinfo;
132 timeinfo = localtime(&rawtime);
133 return timeinfo->tm_hour;
Romain Guy39dbc802009-07-31 11:20:59 -0700134}
135
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800136static int32_t SC_day() {
Romain Guy39dbc802009-07-31 11:20:59 -0700137 GET_TLS();
Jason Samse5ffb872009-08-09 17:01:55 -0700138
Romain Guy39dbc802009-07-31 11:20:59 -0700139 time_t rawtime;
140 time(&rawtime);
Jason Samse5ffb872009-08-09 17:01:55 -0700141
Romain Guy519cdc92009-11-11 15:36:06 -0800142 struct tm *timeinfo;
143 timeinfo = localtime(&rawtime);
144 return timeinfo->tm_mday;
Jason Samse5ffb872009-08-09 17:01:55 -0700145}
Jason Samse45ac6e2009-07-20 14:31:06 -0700146
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800147static int32_t SC_month() {
Romain Guy39dbc802009-07-31 11:20:59 -0700148 GET_TLS();
Jason Samse5ffb872009-08-09 17:01:55 -0700149
Romain Guy39dbc802009-07-31 11:20:59 -0700150 time_t rawtime;
151 time(&rawtime);
Jason Samse5ffb872009-08-09 17:01:55 -0700152
Romain Guy519cdc92009-11-11 15:36:06 -0800153 struct tm *timeinfo;
154 timeinfo = localtime(&rawtime);
155 return timeinfo->tm_mon;
Jason Samse5ffb872009-08-09 17:01:55 -0700156}
Romain Guy39dbc802009-07-31 11:20:59 -0700157
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800158static int32_t SC_year() {
Romain Guy39dbc802009-07-31 11:20:59 -0700159 GET_TLS();
Jason Samse5ffb872009-08-09 17:01:55 -0700160
Romain Guy39dbc802009-07-31 11:20:59 -0700161 time_t rawtime;
162 time(&rawtime);
Jason Samse5ffb872009-08-09 17:01:55 -0700163
Romain Guy519cdc92009-11-11 15:36:06 -0800164 struct tm *timeinfo;
165 timeinfo = localtime(&rawtime);
166 return timeinfo->tm_year;
Romain Guy39dbc802009-07-31 11:20:59 -0700167}
168
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800169static int64_t SC_uptimeMillis() {
Jason Sams22fa3712010-05-19 17:22:57 -0700170 return nanoseconds_to_milliseconds(systemTime(SYSTEM_TIME_MONOTONIC));
171}
172
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800173static int64_t SC_uptimeNanos() {
Jason Sams73495472010-07-29 17:31:14 -0700174 return systemTime(SYSTEM_TIME_MONOTONIC);
Jason Sams22fa3712010-05-19 17:22:57 -0700175}
176
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800177static float SC_getDt() {
Joe Onorato9c4e4ca2009-08-09 11:39:02 -0700178 GET_TLS();
Jason Samsef5867a2010-07-28 11:17:53 -0700179 int64_t l = sc->mEnviroment.mLastDtTime;
180 sc->mEnviroment.mLastDtTime = systemTime(SYSTEM_TIME_MONOTONIC);
181 return ((float)(sc->mEnviroment.mLastDtTime - l)) / 1.0e9;
Jason Samse45ac6e2009-07-20 14:31:06 -0700182}
183
Jason Samse45ac6e2009-07-20 14:31:06 -0700184//////////////////////////////////////////////////////////////////////////////
185//
186//////////////////////////////////////////////////////////////////////////////
187
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800188static uint32_t SC_allocGetDimX(RsAllocation va) {
Jason Samsbe36bf32010-05-11 14:03:58 -0700189 const Allocation *a = static_cast<const Allocation *>(va);
Jason Sams605048a2010-09-30 18:15:52 -0700190 CHECK_OBJ(a);
191 //LOGE("SC_allocGetDimX a=%p type=%p", a, a->getType());
Jason Samsbe36bf32010-05-11 14:03:58 -0700192 return a->getType()->getDimX();
193}
194
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800195static uint32_t SC_allocGetDimY(RsAllocation va) {
Jason Samsbe36bf32010-05-11 14:03:58 -0700196 const Allocation *a = static_cast<const Allocation *>(va);
Jason Sams605048a2010-09-30 18:15:52 -0700197 CHECK_OBJ(a);
Jason Samsbe36bf32010-05-11 14:03:58 -0700198 return a->getType()->getDimY();
199}
200
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800201static uint32_t SC_allocGetDimZ(RsAllocation va) {
Jason Samsbe36bf32010-05-11 14:03:58 -0700202 const Allocation *a = static_cast<const Allocation *>(va);
Jason Sams605048a2010-09-30 18:15:52 -0700203 CHECK_OBJ(a);
Jason Samsbe36bf32010-05-11 14:03:58 -0700204 return a->getType()->getDimZ();
205}
206
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800207static uint32_t SC_allocGetDimLOD(RsAllocation va) {
Jason Samsbe36bf32010-05-11 14:03:58 -0700208 const Allocation *a = static_cast<const Allocation *>(va);
Jason Sams605048a2010-09-30 18:15:52 -0700209 CHECK_OBJ(a);
Jason Samsbe36bf32010-05-11 14:03:58 -0700210 return a->getType()->getDimLOD();
211}
212
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800213static uint32_t SC_allocGetDimFaces(RsAllocation va) {
Jason Samsbe36bf32010-05-11 14:03:58 -0700214 const Allocation *a = static_cast<const Allocation *>(va);
Jason Sams605048a2010-09-30 18:15:52 -0700215 CHECK_OBJ(a);
Jason Samsbe36bf32010-05-11 14:03:58 -0700216 return a->getType()->getDimFaces();
217}
218
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800219static const void * SC_getElementAtX(RsAllocation va, uint32_t x) {
Jason Sams7bf29dd2010-07-19 15:38:19 -0700220 const Allocation *a = static_cast<const Allocation *>(va);
Jason Sams605048a2010-09-30 18:15:52 -0700221 CHECK_OBJ(a);
Jason Sams7bf29dd2010-07-19 15:38:19 -0700222 const Type *t = a->getType();
Jason Sams605048a2010-09-30 18:15:52 -0700223 CHECK_OBJ(t);
Jason Sams7bf29dd2010-07-19 15:38:19 -0700224 const uint8_t *p = (const uint8_t *)a->getPtr();
225 return &p[t->getElementSizeBytes() * x];
226}
227
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800228static const void * SC_getElementAtXY(RsAllocation va, uint32_t x, uint32_t y) {
Jason Sams7bf29dd2010-07-19 15:38:19 -0700229 const Allocation *a = static_cast<const Allocation *>(va);
Jason Sams605048a2010-09-30 18:15:52 -0700230 CHECK_OBJ(a);
Jason Sams7bf29dd2010-07-19 15:38:19 -0700231 const Type *t = a->getType();
Jason Sams605048a2010-09-30 18:15:52 -0700232 CHECK_OBJ(t);
Jason Sams7bf29dd2010-07-19 15:38:19 -0700233 const uint8_t *p = (const uint8_t *)a->getPtr();
234 return &p[t->getElementSizeBytes() * (x + y*t->getDimX())];
235}
236
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800237static const void * SC_getElementAtXYZ(RsAllocation va, uint32_t x, uint32_t y, uint32_t z) {
Jason Sams7bf29dd2010-07-19 15:38:19 -0700238 const Allocation *a = static_cast<const Allocation *>(va);
Jason Sams605048a2010-09-30 18:15:52 -0700239 CHECK_OBJ(a);
Jason Sams7bf29dd2010-07-19 15:38:19 -0700240 const Type *t = a->getType();
Jason Sams605048a2010-09-30 18:15:52 -0700241 CHECK_OBJ(t);
Jason Sams7bf29dd2010-07-19 15:38:19 -0700242 const uint8_t *p = (const uint8_t *)a->getPtr();
243 return &p[t->getElementSizeBytes() * (x + y*t->getDimX())];
244}
Jason Samsbe36bf32010-05-11 14:03:58 -0700245
Jason Samsc0936852010-08-16 12:41:48 -0700246static void SC_setObject(void **vdst, void * vsrc) {
Jason Samsf24d7d02010-09-17 13:17:17 -0700247 //LOGE("SC_setObject %p,%p %p", vdst, *vdst, vsrc);
248 if (vsrc) {
Jason Sams605048a2010-09-30 18:15:52 -0700249 CHECK_OBJ(vsrc);
Jason Samsf24d7d02010-09-17 13:17:17 -0700250 static_cast<ObjectBase *>(vsrc)->incSysRef();
251 }
252 if (vdst[0]) {
Jason Sams605048a2010-09-30 18:15:52 -0700253 CHECK_OBJ(vdst[0]);
Jason Samsf24d7d02010-09-17 13:17:17 -0700254 static_cast<ObjectBase *>(vdst[0])->decSysRef();
255 }
Jason Samsc0936852010-08-16 12:41:48 -0700256 *vdst = vsrc;
Jason Samsf24d7d02010-09-17 13:17:17 -0700257 //LOGE("SC_setObject *");
Jason Samsc0936852010-08-16 12:41:48 -0700258}
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800259
Jason Samsc0936852010-08-16 12:41:48 -0700260static void SC_clearObject(void **vdst) {
Jason Samsf24d7d02010-09-17 13:17:17 -0700261 //LOGE("SC_clearObject %p,%p", vdst, *vdst);
262 if (vdst[0]) {
Jason Sams605048a2010-09-30 18:15:52 -0700263 CHECK_OBJ(vdst[0]);
Jason Samsf24d7d02010-09-17 13:17:17 -0700264 static_cast<ObjectBase *>(vdst[0])->decSysRef();
265 }
Jason Samsc0936852010-08-16 12:41:48 -0700266 *vdst = NULL;
Jason Samsf24d7d02010-09-17 13:17:17 -0700267 //LOGE("SC_clearObject *");
Jason Samsc0936852010-08-16 12:41:48 -0700268}
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800269
Jason Samsc0936852010-08-16 12:41:48 -0700270static bool SC_isObject(RsAllocation vsrc) {
271 return vsrc != NULL;
272}
273
Jason Sams22fa3712010-05-19 17:22:57 -0700274static void SC_debugF(const char *s, float f) {
275 LOGE("%s %f, 0x%08x", s, f, *((int *) (&f)));
Jason Samsc9d43db2009-07-28 12:02:16 -0700276}
Jason Sams7dce6bc2010-08-06 16:22:50 -0700277static void SC_debugFv2(const char *s, float f1, float f2) {
278 LOGE("%s {%f, %f}", s, f1, f2);
Romain Guy370ed152009-08-20 17:08:33 -0700279}
Jason Sams7dce6bc2010-08-06 16:22:50 -0700280static void SC_debugFv3(const char *s, float f1, float f2, float f3) {
281 LOGE("%s {%f, %f, %f}", s, f1, f2, f3);
Jason Samsc9d43db2009-07-28 12:02:16 -0700282}
Jason Sams7dce6bc2010-08-06 16:22:50 -0700283static void SC_debugFv4(const char *s, float f1, float f2, float f3, float f4) {
284 LOGE("%s {%f, %f, %f, %f}", s, f1, f2, f3, f4);
Jason Sams22fa3712010-05-19 17:22:57 -0700285}
Stephen Hinesdf097192010-10-15 12:47:49 -0700286static void SC_debugD(const char *s, double d) {
287 LOGE("%s %f, 0x%08llx", s, d, *((long long *) (&d)));
288}
Jason Sams7dce6bc2010-08-06 16:22:50 -0700289static void SC_debugFM4v4(const char *s, const float *f) {
290 LOGE("%s {%f, %f, %f, %f", s, f[0], f[4], f[8], f[12]);
291 LOGE("%s %f, %f, %f, %f", s, f[1], f[5], f[9], f[13]);
292 LOGE("%s %f, %f, %f, %f", s, f[2], f[6], f[10], f[14]);
293 LOGE("%s %f, %f, %f, %f}", s, f[3], f[7], f[11], f[15]);
294}
295static void SC_debugFM3v3(const char *s, const float *f) {
296 LOGE("%s {%f, %f, %f", s, f[0], f[3], f[6]);
297 LOGE("%s %f, %f, %f", s, f[1], f[4], f[7]);
298 LOGE("%s %f, %f, %f}",s, f[2], f[5], f[8]);
299}
300static void SC_debugFM2v2(const char *s, const float *f) {
301 LOGE("%s {%f, %f", s, f[0], f[2]);
302 LOGE("%s %f, %f}",s, f[1], f[3]);
303}
304
Jason Sams22fa3712010-05-19 17:22:57 -0700305static void SC_debugI32(const char *s, int32_t i) {
306 LOGE("%s %i 0x%x", s, i, i);
Romain Guy370ed152009-08-20 17:08:33 -0700307}
Jason Samsef5867a2010-07-28 11:17:53 -0700308static void SC_debugU32(const char *s, uint32_t i) {
Stephen Hinesdf097192010-10-15 12:47:49 -0700309 LOGE("%s %u 0x%x", s, i, i);
310}
311static void SC_debugLL64(const char *s, long long ll) {
312 LOGE("%s %lld 0x%llx", s, ll, ll);
313}
314static void SC_debugULL64(const char *s, unsigned long long ll) {
315 LOGE("%s %llu 0x%llx", s, ll, ll);
Jason Samsef5867a2010-07-28 11:17:53 -0700316}
Romain Guy370ed152009-08-20 17:08:33 -0700317
Jason Sams7bf29dd2010-07-19 15:38:19 -0700318static void SC_debugP(const char *s, const void *p) {
319 LOGE("%s %p", s, p);
320}
321
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800322static uint32_t SC_toClient2(int cmdID, void *data, int len) {
Jason Sams8c401ef2009-10-06 13:58:47 -0700323 GET_TLS();
Jason Samsef5867a2010-07-28 11:17:53 -0700324 //LOGE("SC_toClient %i %i %i", cmdID, len);
Jason Samsaad4bc52010-11-08 17:06:46 -0800325 return rsc->sendMessageToClient(data, RS_MESSAGE_TO_CLIENT_USER, cmdID, len, false);
Jason Sams8c401ef2009-10-06 13:58:47 -0700326}
327
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800328static uint32_t SC_toClient(int cmdID) {
Jason Sams3a27c952009-10-07 18:14:01 -0700329 GET_TLS();
Jason Samsef5867a2010-07-28 11:17:53 -0700330 //LOGE("SC_toClient %i", cmdID);
Jason Samsaad4bc52010-11-08 17:06:46 -0800331 return rsc->sendMessageToClient(NULL, RS_MESSAGE_TO_CLIENT_USER, cmdID, 0, false);
Jason Samsef5867a2010-07-28 11:17:53 -0700332}
333
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800334static uint32_t SC_toClientBlocking2(int cmdID, void *data, int len) {
Jason Samsef5867a2010-07-28 11:17:53 -0700335 GET_TLS();
336 //LOGE("SC_toClientBlocking %i %i", cmdID, len);
Jason Samsaad4bc52010-11-08 17:06:46 -0800337 return rsc->sendMessageToClient(data, RS_MESSAGE_TO_CLIENT_USER, cmdID, len, true);
Jason Samsef5867a2010-07-28 11:17:53 -0700338}
339
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800340static uint32_t SC_toClientBlocking(int cmdID) {
Jason Samsef5867a2010-07-28 11:17:53 -0700341 GET_TLS();
342 //LOGE("SC_toClientBlocking %i", cmdID);
Jason Samsaad4bc52010-11-08 17:06:46 -0800343 return rsc->sendMessageToClient(NULL, RS_MESSAGE_TO_CLIENT_USER, cmdID, 0, true);
Jason Sams3a27c952009-10-07 18:14:01 -0700344}
345
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800346int SC_divsi3(int a, int b) {
Jason Samsbe36bf32010-05-11 14:03:58 -0700347 return a / b;
348}
Jason Sams3a27c952009-10-07 18:14:01 -0700349
Bryan Mawhinneycb082a32010-11-11 14:33:12 +0000350int SC_modsi3(int a, int b) {
351 return a % b;
352}
353
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800354int SC_getAllocation(const void *ptr) {
Jason Samsce92d4b2010-05-17 14:55:34 -0700355 GET_TLS();
356 const Allocation *alloc = sc->ptrToAllocation(ptr);
357 return (int)alloc;
358}
359
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800360void SC_allocationMarkDirty(RsAllocation a) {
Alex Sakhartchouk1e5168d2010-09-01 16:34:48 -0700361 Allocation *alloc = static_cast<Allocation *>(a);
362 alloc->sendDirty();
363}
Jason Samsce92d4b2010-05-17 14:55:34 -0700364
Jason Samsace3e012010-07-15 17:11:13 -0700365void SC_ForEach(RsScript vs,
366 RsAllocation vin,
367 RsAllocation vout,
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800368 const void *usr) {
Jason Samsc61346b2010-05-28 18:23:22 -0700369 GET_TLS();
Jason Samsace3e012010-07-15 17:11:13 -0700370 const Allocation *ain = static_cast<const Allocation *>(vin);
Jason Samsc61346b2010-05-28 18:23:22 -0700371 Allocation *aout = static_cast<Allocation *>(vout);
Jason Samsace3e012010-07-15 17:11:13 -0700372 Script *s = static_cast<Script *>(vs);
373 s->runForEach(rsc, ain, aout, usr);
Jason Samsc61346b2010-05-28 18:23:22 -0700374}
375
Jason Samsace3e012010-07-15 17:11:13 -0700376void SC_ForEach2(RsScript vs,
377 RsAllocation vin,
378 RsAllocation vout,
379 const void *usr,
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800380 const RsScriptCall *call) {
Jason Samsc61346b2010-05-28 18:23:22 -0700381 GET_TLS();
Jason Samsace3e012010-07-15 17:11:13 -0700382 const Allocation *ain = static_cast<const Allocation *>(vin);
Jason Samsc61346b2010-05-28 18:23:22 -0700383 Allocation *aout = static_cast<Allocation *>(vout);
Jason Samsc61346b2010-05-28 18:23:22 -0700384 Script *s = static_cast<Script *>(vs);
Jason Samsace3e012010-07-15 17:11:13 -0700385 s->runForEach(rsc, ain, aout, usr, call);
Jason Samsc61346b2010-05-28 18:23:22 -0700386}
387
Jason Samse45ac6e2009-07-20 14:31:06 -0700388//////////////////////////////////////////////////////////////////////////////
389// Class implementation
390//////////////////////////////////////////////////////////////////////////////
391
Jason Samsbe36bf32010-05-11 14:03:58 -0700392// llvm name mangling ref
393// <builtin-type> ::= v # void
394// ::= b # bool
395// ::= c # char
396// ::= a # signed char
397// ::= h # unsigned char
398// ::= s # short
399// ::= t # unsigned short
400// ::= i # int
401// ::= j # unsigned int
402// ::= l # long
403// ::= m # unsigned long
404// ::= x # long long, __int64
405// ::= y # unsigned long long, __int64
406// ::= f # float
407// ::= d # double
Jason Samse45ac6e2009-07-20 14:31:06 -0700408
Jason Samsaeb094b2010-05-18 13:35:45 -0700409static ScriptCState::SymbolTable_t gSyms[] = {
Jason Sams6bfc1b92010-11-01 14:26:30 -0700410 { "__divsi3", (void *)&SC_divsi3, true },
Bryan Mawhinneycb082a32010-11-11 14:33:12 +0000411 { "__modsi3", (void *)&SC_modsi3, true },
Jason Samsbe36bf32010-05-11 14:03:58 -0700412
Jason Sams22fa3712010-05-19 17:22:57 -0700413 // allocation
Jason Sams6bfc1b92010-11-01 14:26:30 -0700414 { "_Z19rsAllocationGetDimX13rs_allocation", (void *)&SC_allocGetDimX, true },
415 { "_Z19rsAllocationGetDimY13rs_allocation", (void *)&SC_allocGetDimY, true },
416 { "_Z19rsAllocationGetDimZ13rs_allocation", (void *)&SC_allocGetDimZ, true },
417 { "_Z21rsAllocationGetDimLOD13rs_allocation", (void *)&SC_allocGetDimLOD, true },
418 { "_Z23rsAllocationGetDimFaces13rs_allocation", (void *)&SC_allocGetDimFaces, true },
419 { "_Z15rsGetAllocationPKv", (void *)&SC_getAllocation, true },
Jason Sams22fa3712010-05-19 17:22:57 -0700420
Jason Sams6bfc1b92010-11-01 14:26:30 -0700421 { "_Z14rsGetElementAt13rs_allocationj", (void *)&SC_getElementAtX, true },
422 { "_Z14rsGetElementAt13rs_allocationjj", (void *)&SC_getElementAtXY, true },
423 { "_Z14rsGetElementAt13rs_allocationjjj", (void *)&SC_getElementAtXYZ, true },
Jason Sams7bf29dd2010-07-19 15:38:19 -0700424
Jason Sams6bfc1b92010-11-01 14:26:30 -0700425 { "_Z11rsSetObjectP10rs_elementS_", (void *)&SC_setObject, true },
426 { "_Z13rsClearObjectP10rs_element", (void *)&SC_clearObject, true },
427 { "_Z10rsIsObject10rs_element", (void *)&SC_isObject, true },
Jason Samsf24d7d02010-09-17 13:17:17 -0700428
Jason Sams6bfc1b92010-11-01 14:26:30 -0700429 { "_Z11rsSetObjectP7rs_typeS_", (void *)&SC_setObject, true },
430 { "_Z13rsClearObjectP7rs_type", (void *)&SC_clearObject, true },
431 { "_Z10rsIsObject7rs_type", (void *)&SC_isObject, true },
Jason Samsf24d7d02010-09-17 13:17:17 -0700432
Jason Sams6bfc1b92010-11-01 14:26:30 -0700433 { "_Z11rsSetObjectP13rs_allocationS_", (void *)&SC_setObject, true },
434 { "_Z13rsClearObjectP13rs_allocation", (void *)&SC_clearObject, true },
435 { "_Z10rsIsObject13rs_allocation", (void *)&SC_isObject, true },
Jason Samsc0936852010-08-16 12:41:48 -0700436
Jason Sams6bfc1b92010-11-01 14:26:30 -0700437 { "_Z11rsSetObjectP10rs_samplerS_", (void *)&SC_setObject, true },
438 { "_Z13rsClearObjectP10rs_sampler", (void *)&SC_clearObject, true },
439 { "_Z10rsIsObject10rs_sampler", (void *)&SC_isObject, true },
Jason Samsf24d7d02010-09-17 13:17:17 -0700440
Jason Sams6bfc1b92010-11-01 14:26:30 -0700441 { "_Z11rsSetObjectP9rs_scriptS_", (void *)&SC_setObject, true },
442 { "_Z13rsClearObjectP9rs_script", (void *)&SC_clearObject, true },
443 { "_Z10rsIsObject9rs_script", (void *)&SC_isObject, true },
Jason Samsf24d7d02010-09-17 13:17:17 -0700444
Jason Sams6bfc1b92010-11-01 14:26:30 -0700445 { "_Z11rsSetObjectP7rs_meshS_", (void *)&SC_setObject, true },
446 { "_Z13rsClearObjectP7rs_mesh", (void *)&SC_clearObject, true },
447 { "_Z10rsIsObject7rs_mesh", (void *)&SC_isObject, true },
Jason Samsf24d7d02010-09-17 13:17:17 -0700448
Jason Sams6bfc1b92010-11-01 14:26:30 -0700449 { "_Z11rsSetObjectP19rs_program_fragmentS_", (void *)&SC_setObject, true },
450 { "_Z13rsClearObjectP19rs_program_fragment", (void *)&SC_clearObject, true },
451 { "_Z10rsIsObject19rs_program_fragment", (void *)&SC_isObject, true },
Jason Samsf24d7d02010-09-17 13:17:17 -0700452
Jason Sams6bfc1b92010-11-01 14:26:30 -0700453 { "_Z11rsSetObjectP17rs_program_vertexS_", (void *)&SC_setObject, true },
454 { "_Z13rsClearObjectP17rs_program_vertex", (void *)&SC_clearObject, true },
455 { "_Z10rsIsObject17rs_program_vertex", (void *)&SC_isObject, true },
Jason Samsf24d7d02010-09-17 13:17:17 -0700456
Jason Sams6bfc1b92010-11-01 14:26:30 -0700457 { "_Z11rsSetObjectP17rs_program_rasterS_", (void *)&SC_setObject, true },
458 { "_Z13rsClearObjectP17rs_program_raster", (void *)&SC_clearObject, true },
459 { "_Z10rsIsObject17rs_program_raster", (void *)&SC_isObject, true },
Jason Samsf24d7d02010-09-17 13:17:17 -0700460
Jason Sams6bfc1b92010-11-01 14:26:30 -0700461 { "_Z11rsSetObjectP16rs_program_storeS_", (void *)&SC_setObject, true },
462 { "_Z13rsClearObjectP16rs_program_store", (void *)&SC_clearObject, true },
463 { "_Z10rsIsObject16rs_program_store", (void *)&SC_isObject, true },
Jason Samsf24d7d02010-09-17 13:17:17 -0700464
Jason Sams6bfc1b92010-11-01 14:26:30 -0700465 { "_Z11rsSetObjectP7rs_fontS_", (void *)&SC_setObject, true },
466 { "_Z13rsClearObjectP7rs_font", (void *)&SC_clearObject, true },
467 { "_Z10rsIsObject7rs_font", (void *)&SC_isObject, true },
Jason Samsf24d7d02010-09-17 13:17:17 -0700468
469
Jason Sams6bfc1b92010-11-01 14:26:30 -0700470 { "_Z21rsAllocationMarkDirty13rs_allocation", (void *)&SC_allocationMarkDirty, true },
Alex Sakhartchouk1e5168d2010-09-01 16:34:48 -0700471
Jason Samsc0936852010-08-16 12:41:48 -0700472
Jason Sams22fa3712010-05-19 17:22:57 -0700473 // Debug
Jason Sams6bfc1b92010-11-01 14:26:30 -0700474 { "_Z7rsDebugPKcf", (void *)&SC_debugF, true },
475 { "_Z7rsDebugPKcff", (void *)&SC_debugFv2, true },
476 { "_Z7rsDebugPKcfff", (void *)&SC_debugFv3, true },
477 { "_Z7rsDebugPKcffff", (void *)&SC_debugFv4, true },
478 { "_Z7rsDebugPKcd", (void *)&SC_debugD, true },
479 { "_Z7rsDebugPKcPK12rs_matrix4x4", (void *)&SC_debugFM4v4, true },
480 { "_Z7rsDebugPKcPK12rs_matrix3x3", (void *)&SC_debugFM3v3, true },
481 { "_Z7rsDebugPKcPK12rs_matrix2x2", (void *)&SC_debugFM2v2, true },
482 { "_Z7rsDebugPKci", (void *)&SC_debugI32, true },
483 { "_Z7rsDebugPKcj", (void *)&SC_debugU32, true },
Stephen Hinesdf097192010-10-15 12:47:49 -0700484 // Both "long" and "unsigned long" need to be redirected to their
485 // 64-bit counterparts, since we have hacked Slang to use 64-bit
486 // for "long" on Arm (to be similar to Java).
Jason Sams6bfc1b92010-11-01 14:26:30 -0700487 { "_Z7rsDebugPKcl", (void *)&SC_debugLL64, true },
488 { "_Z7rsDebugPKcm", (void *)&SC_debugULL64, true },
489 { "_Z7rsDebugPKcx", (void *)&SC_debugLL64, true },
490 { "_Z7rsDebugPKcy", (void *)&SC_debugULL64, true },
491 { "_Z7rsDebugPKcPKv", (void *)&SC_debugP, true },
Jason Sams22fa3712010-05-19 17:22:57 -0700492
493 // RS Math
Jason Sams6bfc1b92010-11-01 14:26:30 -0700494 { "_Z6rsRandi", (void *)&SC_randi, true },
495 { "_Z6rsRandii", (void *)&SC_randi2, true },
496 { "_Z6rsRandf", (void *)&SC_randf, true },
497 { "_Z6rsRandff", (void *)&SC_randf2, true },
498 { "_Z6rsFracf", (void *)&SC_frac, true },
Jason Sams22fa3712010-05-19 17:22:57 -0700499
500 // time
Jason Sams6bfc1b92010-11-01 14:26:30 -0700501 { "_Z8rsSecondv", (void *)&SC_second, true },
502 { "_Z8rsMinutev", (void *)&SC_minute, true },
503 { "_Z6rsHourv", (void *)&SC_hour, true },
504 { "_Z5rsDayv", (void *)&SC_day, true },
505 { "_Z7rsMonthv", (void *)&SC_month, true },
506 { "_Z6rsYearv", (void *)&SC_year, true },
507 { "_Z14rsUptimeMillisv", (void*)&SC_uptimeMillis, true },
508 { "_Z13rsUptimeNanosv", (void*)&SC_uptimeNanos, true },
509 { "_Z7rsGetDtv", (void*)&SC_getDt, false },
Jason Sams22fa3712010-05-19 17:22:57 -0700510
Jason Sams6bfc1b92010-11-01 14:26:30 -0700511 { "_Z14rsSendToClienti", (void *)&SC_toClient, false },
512 { "_Z14rsSendToClientiPKvj", (void *)&SC_toClient2, false },
513 { "_Z22rsSendToClientBlockingi", (void *)&SC_toClientBlocking, false },
514 { "_Z22rsSendToClientBlockingiPKvj", (void *)&SC_toClientBlocking2, false },
Jason Sams22fa3712010-05-19 17:22:57 -0700515
Jason Sams6bfc1b92010-11-01 14:26:30 -0700516 { "_Z9rsForEach9rs_script13rs_allocationS0_PKv", (void *)&SC_ForEach, false },
517 //{ "_Z9rsForEach9rs_script13rs_allocationS0_PKv", (void *)&SC_ForEach2, true },
Jason Sams22fa3712010-05-19 17:22:57 -0700518
519////////////////////////////////////////////////////////////////////
520
Jason Sams6bfc1b92010-11-01 14:26:30 -0700521 //{ "sinf_fast", (void *)&SC_sinf_fast, true },
522 //{ "cosf_fast", (void *)&SC_cosf_fast, true },
Jason Samse45ac6e2009-07-20 14:31:06 -0700523
Jason Sams6bfc1b92010-11-01 14:26:30 -0700524 { NULL, NULL, false }
Jason Samse45ac6e2009-07-20 14:31:06 -0700525};
526
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800527const ScriptCState::SymbolTable_t * ScriptCState::lookupSymbol(const char *sym) {
Jason Samse45ac6e2009-07-20 14:31:06 -0700528 ScriptCState::SymbolTable_t *syms = gSyms;
529
530 while (syms->mPtr) {
531 if (!strcmp(syms->mName, sym)) {
532 return syms;
533 }
534 syms++;
535 }
536 return NULL;
537}