blob: 8d9ca9f0b580751c0e43caa3ac494de205ae5a0a [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
21#include "acc/acc.h"
Joe Onorato9c4e4ca2009-08-09 11:39:02 -070022#include "utils/Timers.h"
Jason Samse45ac6e2009-07-20 14:31:06 -070023
Romain Guy98e10fd2009-07-30 18:45:01 -070024#include <time.h>
Romain Guy98e10fd2009-07-30 18:45:01 -070025
Jason Samse45ac6e2009-07-20 14:31:06 -070026using namespace android;
27using namespace android::renderscript;
28
29#define GET_TLS() Context::ScriptTLSStruct * tls = \
30 (Context::ScriptTLSStruct *)pthread_getspecific(Context::gThreadTLSKey); \
31 Context * rsc = tls->mContext; \
32 ScriptC * sc = (ScriptC *) tls->mScript
33
Jason Samsbe36bf32010-05-11 14:03:58 -070034
Jason Samse45ac6e2009-07-20 14:31:06 -070035//////////////////////////////////////////////////////////////////////////////
36// Math routines
37//////////////////////////////////////////////////////////////////////////////
38
Romain Guy2275d632009-08-18 11:39:17 -070039static float SC_sinf_fast(float x)
40{
41 const float A = 1.0f / (2.0f * M_PI);
42 const float B = -16.0f;
43 const float C = 8.0f;
Jason Samsa57c0a72009-09-04 14:42:41 -070044
Romain Guy2275d632009-08-18 11:39:17 -070045 // scale angle for easy argument reduction
46 x *= A;
Jason Samsa57c0a72009-09-04 14:42:41 -070047
Romain Guy2275d632009-08-18 11:39:17 -070048 if (fabsf(x) >= 0.5f) {
49 // argument reduction
50 x = x - ceilf(x + 0.5f) + 1.0f;
51 }
Jason Samsa57c0a72009-09-04 14:42:41 -070052
Romain Guy2275d632009-08-18 11:39:17 -070053 const float y = B * x * fabsf(x) + C * x;
54 return 0.2215f * (y * fabsf(y) - y) + y;
55}
56
57static float SC_cosf_fast(float x)
58{
59 x += float(M_PI / 2);
60
61 const float A = 1.0f / (2.0f * M_PI);
62 const float B = -16.0f;
63 const float C = 8.0f;
Jason Samsa57c0a72009-09-04 14:42:41 -070064
Romain Guy2275d632009-08-18 11:39:17 -070065 // scale angle for easy argument reduction
66 x *= A;
Jason Samsa57c0a72009-09-04 14:42:41 -070067
Romain Guy2275d632009-08-18 11:39:17 -070068 if (fabsf(x) >= 0.5f) {
69 // argument reduction
70 x = x - ceilf(x + 0.5f) + 1.0f;
71 }
Jason Samsa57c0a72009-09-04 14:42:41 -070072
Romain Guy2275d632009-08-18 11:39:17 -070073 const float y = B * x * fabsf(x) + C * x;
74 return 0.2215f * (y * fabsf(y) - y) + y;
75}
76
Jason Sams22fa3712010-05-19 17:22:57 -070077
Jason Samse45ac6e2009-07-20 14:31:06 -070078static float SC_randf(float max)
79{
80 float r = (float)rand();
81 return r / RAND_MAX * max;
82}
83
Romain Guy39dbc802009-07-31 11:20:59 -070084static float SC_randf2(float min, float max)
85{
86 float r = (float)rand();
87 return r / RAND_MAX * (max - min) + min;
88}
89
Jason Sams22fa3712010-05-19 17:22:57 -070090static int SC_randi(int max)
91{
92 return (int)SC_randf(max);
93}
94
95static int SC_randi2(int min, int max)
96{
97 return (int)SC_randf2(min, max);
98}
99
Jason Samsbe36bf32010-05-11 14:03:58 -0700100static float SC_frac(float v)
101{
102 int i = (int)floor(v);
103 return fmin(v - i, 0x1.fffffep-1f);
104}
105
Romain Guy98e10fd2009-07-30 18:45:01 -0700106//////////////////////////////////////////////////////////////////////////////
107// Time routines
108//////////////////////////////////////////////////////////////////////////////
Jason Samse45ac6e2009-07-20 14:31:06 -0700109
Joe Onorato9c4e4ca2009-08-09 11:39:02 -0700110static int32_t SC_second()
Romain Guy98e10fd2009-07-30 18:45:01 -0700111{
112 GET_TLS();
113
114 time_t rawtime;
115 time(&rawtime);
116
Romain Guy519cdc92009-11-11 15:36:06 -0800117 struct tm *timeinfo;
118 timeinfo = localtime(&rawtime);
119 return timeinfo->tm_sec;
Romain Guy98e10fd2009-07-30 18:45:01 -0700120}
121
Joe Onorato9c4e4ca2009-08-09 11:39:02 -0700122static int32_t SC_minute()
Romain Guy98e10fd2009-07-30 18:45:01 -0700123{
124 GET_TLS();
Jason Samse5ffb872009-08-09 17:01:55 -0700125
Romain Guy98e10fd2009-07-30 18:45:01 -0700126 time_t rawtime;
127 time(&rawtime);
Jason Samse5ffb872009-08-09 17:01:55 -0700128
Romain Guy519cdc92009-11-11 15:36:06 -0800129 struct tm *timeinfo;
130 timeinfo = localtime(&rawtime);
131 return timeinfo->tm_min;
Jason Samse5ffb872009-08-09 17:01:55 -0700132}
Romain Guy98e10fd2009-07-30 18:45:01 -0700133
Joe Onorato9c4e4ca2009-08-09 11:39:02 -0700134static int32_t SC_hour()
Romain Guy98e10fd2009-07-30 18:45:01 -0700135{
136 GET_TLS();
Jason Samse5ffb872009-08-09 17:01:55 -0700137
Romain Guy98e10fd2009-07-30 18:45:01 -0700138 time_t rawtime;
139 time(&rawtime);
Jason Samse5ffb872009-08-09 17:01:55 -0700140
Romain Guy519cdc92009-11-11 15:36:06 -0800141 struct tm *timeinfo;
142 timeinfo = localtime(&rawtime);
143 return timeinfo->tm_hour;
Romain Guy39dbc802009-07-31 11:20:59 -0700144}
145
Joe Onorato9c4e4ca2009-08-09 11:39:02 -0700146static int32_t SC_day()
Romain Guy39dbc802009-07-31 11:20:59 -0700147{
148 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_mday;
Jason Samse5ffb872009-08-09 17:01:55 -0700156}
Jason Samse45ac6e2009-07-20 14:31:06 -0700157
Joe Onorato9c4e4ca2009-08-09 11:39:02 -0700158static int32_t SC_month()
Romain Guy39dbc802009-07-31 11:20:59 -0700159{
160 GET_TLS();
Jason Samse5ffb872009-08-09 17:01:55 -0700161
Romain Guy39dbc802009-07-31 11:20:59 -0700162 time_t rawtime;
163 time(&rawtime);
Jason Samse5ffb872009-08-09 17:01:55 -0700164
Romain Guy519cdc92009-11-11 15:36:06 -0800165 struct tm *timeinfo;
166 timeinfo = localtime(&rawtime);
167 return timeinfo->tm_mon;
Jason Samse5ffb872009-08-09 17:01:55 -0700168}
Romain Guy39dbc802009-07-31 11:20:59 -0700169
Joe Onorato9c4e4ca2009-08-09 11:39:02 -0700170static int32_t SC_year()
Romain Guy39dbc802009-07-31 11:20:59 -0700171{
172 GET_TLS();
Jason Samse5ffb872009-08-09 17:01:55 -0700173
Romain Guy39dbc802009-07-31 11:20:59 -0700174 time_t rawtime;
175 time(&rawtime);
Jason Samse5ffb872009-08-09 17:01:55 -0700176
Romain Guy519cdc92009-11-11 15:36:06 -0800177 struct tm *timeinfo;
178 timeinfo = localtime(&rawtime);
179 return timeinfo->tm_year;
Romain Guy39dbc802009-07-31 11:20:59 -0700180}
181
Jason Sams22fa3712010-05-19 17:22:57 -0700182static int64_t SC_uptimeMillis2()
183{
184 return nanoseconds_to_milliseconds(systemTime(SYSTEM_TIME_MONOTONIC));
185}
186
187static int64_t SC_startTimeMillis2()
188{
189 GET_TLS();
190 return sc->mEnviroment.mStartTimeMillis;
191}
192
193static int64_t SC_elapsedTimeMillis2()
194{
195 GET_TLS();
196 return nanoseconds_to_milliseconds(systemTime(SYSTEM_TIME_MONOTONIC))
197 - sc->mEnviroment.mStartTimeMillis;
198}
199
Joe Onorato9c4e4ca2009-08-09 11:39:02 -0700200static int32_t SC_uptimeMillis()
201{
202 return nanoseconds_to_milliseconds(systemTime(SYSTEM_TIME_MONOTONIC));
203}
204
205static int32_t SC_startTimeMillis()
206{
207 GET_TLS();
208 return sc->mEnviroment.mStartTimeMillis;
209}
210
211static int32_t SC_elapsedTimeMillis()
212{
213 GET_TLS();
214 return nanoseconds_to_milliseconds(systemTime(SYSTEM_TIME_MONOTONIC))
215 - sc->mEnviroment.mStartTimeMillis;
216}
217
Jason Samse45ac6e2009-07-20 14:31:06 -0700218//////////////////////////////////////////////////////////////////////////////
219// Matrix routines
220//////////////////////////////////////////////////////////////////////////////
221
222
223static void SC_matrixLoadIdentity(rsc_Matrix *mat)
224{
225 Matrix *m = reinterpret_cast<Matrix *>(mat);
226 m->loadIdentity();
227}
228
229static void SC_matrixLoadFloat(rsc_Matrix *mat, const float *f)
230{
231 Matrix *m = reinterpret_cast<Matrix *>(mat);
232 m->load(f);
233}
234
235static void SC_matrixLoadMat(rsc_Matrix *mat, const rsc_Matrix *newmat)
236{
237 Matrix *m = reinterpret_cast<Matrix *>(mat);
238 m->load(reinterpret_cast<const Matrix *>(newmat));
239}
240
241static void SC_matrixLoadRotate(rsc_Matrix *mat, float rot, float x, float y, float z)
242{
243 Matrix *m = reinterpret_cast<Matrix *>(mat);
244 m->loadRotate(rot, x, y, z);
245}
246
247static void SC_matrixLoadScale(rsc_Matrix *mat, float x, float y, float z)
248{
249 Matrix *m = reinterpret_cast<Matrix *>(mat);
250 m->loadScale(x, y, z);
251}
252
253static void SC_matrixLoadTranslate(rsc_Matrix *mat, float x, float y, float z)
254{
255 Matrix *m = reinterpret_cast<Matrix *>(mat);
256 m->loadTranslate(x, y, z);
257}
258
259static void SC_matrixLoadMultiply(rsc_Matrix *mat, const rsc_Matrix *lhs, const rsc_Matrix *rhs)
260{
261 Matrix *m = reinterpret_cast<Matrix *>(mat);
262 m->loadMultiply(reinterpret_cast<const Matrix *>(lhs),
263 reinterpret_cast<const Matrix *>(rhs));
264}
265
266static void SC_matrixMultiply(rsc_Matrix *mat, const rsc_Matrix *rhs)
267{
268 Matrix *m = reinterpret_cast<Matrix *>(mat);
269 m->multiply(reinterpret_cast<const Matrix *>(rhs));
270}
271
272static void SC_matrixRotate(rsc_Matrix *mat, float rot, float x, float y, float z)
273{
274 Matrix *m = reinterpret_cast<Matrix *>(mat);
275 m->rotate(rot, x, y, z);
276}
277
278static void SC_matrixScale(rsc_Matrix *mat, float x, float y, float z)
279{
280 Matrix *m = reinterpret_cast<Matrix *>(mat);
281 m->scale(x, y, z);
282}
283
284static void SC_matrixTranslate(rsc_Matrix *mat, float x, float y, float z)
285{
286 Matrix *m = reinterpret_cast<Matrix *>(mat);
287 m->translate(x, y, z);
288}
289
290
Jason Samse45ac6e2009-07-20 14:31:06 -0700291//////////////////////////////////////////////////////////////////////////////
292//
293//////////////////////////////////////////////////////////////////////////////
294
Jason Samsbe36bf32010-05-11 14:03:58 -0700295static uint32_t SC_allocGetDimX(RsAllocation va)
296{
297 GET_TLS();
298 const Allocation *a = static_cast<const Allocation *>(va);
299 //LOGE("SC_allocGetDimX a=%p", a);
300 //LOGE(" type=%p", a->getType());
301 return a->getType()->getDimX();
302}
303
304static uint32_t SC_allocGetDimY(RsAllocation va)
305{
306 GET_TLS();
307 const Allocation *a = static_cast<const Allocation *>(va);
308 return a->getType()->getDimY();
309}
310
311static uint32_t SC_allocGetDimZ(RsAllocation va)
312{
313 GET_TLS();
314 const Allocation *a = static_cast<const Allocation *>(va);
315 return a->getType()->getDimZ();
316}
317
318static uint32_t SC_allocGetDimLOD(RsAllocation va)
319{
320 GET_TLS();
321 const Allocation *a = static_cast<const Allocation *>(va);
322 return a->getType()->getDimLOD();
323}
324
325static uint32_t SC_allocGetDimFaces(RsAllocation va)
326{
327 GET_TLS();
328 const Allocation *a = static_cast<const Allocation *>(va);
329 return a->getType()->getDimFaces();
330}
331
332
Jason Samse45ac6e2009-07-20 14:31:06 -0700333
Jason Sams22fa3712010-05-19 17:22:57 -0700334static void SC_debugF(const char *s, float f) {
335 LOGE("%s %f, 0x%08x", s, f, *((int *) (&f)));
Jason Samsc9d43db2009-07-28 12:02:16 -0700336}
Jason Sams22fa3712010-05-19 17:22:57 -0700337static void SC_debugFv2(const char *s, rsvF_2 fv) {
338 float *f = (float *)&fv;
339 LOGE("%s {%f, %f}", s, f[0], f[1]);
Romain Guy370ed152009-08-20 17:08:33 -0700340}
Jason Sams22fa3712010-05-19 17:22:57 -0700341static void SC_debugFv3(const char *s, rsvF_4 fv) {
342 float *f = (float *)&fv;
343 LOGE("%s {%f, %f, %f}", s, f[0], f[1], f[2]);
Jason Samsc9d43db2009-07-28 12:02:16 -0700344}
Jason Sams22fa3712010-05-19 17:22:57 -0700345static void SC_debugFv4(const char *s, rsvF_4 fv) {
346 float *f = (float *)&fv;
347 LOGE("%s {%f, %f, %f, %f}", s, f[0], f[1], f[2], f[3]);
348}
349static void SC_debugI32(const char *s, int32_t i) {
350 LOGE("%s %i 0x%x", s, i, i);
Romain Guy370ed152009-08-20 17:08:33 -0700351}
352
Jason Sams8c401ef2009-10-06 13:58:47 -0700353static uint32_t SC_toClient(void *data, int cmdID, int len, int waitForSpace)
354{
355 GET_TLS();
Jason Samsbe36bf32010-05-11 14:03:58 -0700356 //LOGE("SC_toClient %i %i %i", cmdID, len, waitForSpace);
Jason Sams8c401ef2009-10-06 13:58:47 -0700357 return rsc->sendMessageToClient(data, cmdID, len, waitForSpace != 0);
358}
359
Jason Sams3a27c952009-10-07 18:14:01 -0700360static void SC_scriptCall(int scriptID)
361{
362 GET_TLS();
Jason Samsc61346b2010-05-28 18:23:22 -0700363 rsc->runScript((Script *)scriptID);
Jason Sams3a27c952009-10-07 18:14:01 -0700364}
365
Jason Samsbe36bf32010-05-11 14:03:58 -0700366int SC_divsi3(int a, int b)
367{
368 return a / b;
369}
Jason Sams3a27c952009-10-07 18:14:01 -0700370
Jason Samsce92d4b2010-05-17 14:55:34 -0700371int SC_getAllocation(const void *ptr)
372{
373 GET_TLS();
374 const Allocation *alloc = sc->ptrToAllocation(ptr);
375 return (int)alloc;
376}
377
378
Jason Samsace3e012010-07-15 17:11:13 -0700379void SC_ForEach(RsScript vs,
380 RsAllocation vin,
381 RsAllocation vout,
382 const void *usr)
Jason Samsc61346b2010-05-28 18:23:22 -0700383{
384 GET_TLS();
Jason Samsace3e012010-07-15 17:11:13 -0700385 const Allocation *ain = static_cast<const Allocation *>(vin);
Jason Samsc61346b2010-05-28 18:23:22 -0700386 Allocation *aout = static_cast<Allocation *>(vout);
Jason Samsace3e012010-07-15 17:11:13 -0700387 Script *s = static_cast<Script *>(vs);
388 s->runForEach(rsc, ain, aout, usr);
Jason Samsc61346b2010-05-28 18:23:22 -0700389}
390
Jason Samsace3e012010-07-15 17:11:13 -0700391void SC_ForEach2(RsScript vs,
392 RsAllocation vin,
393 RsAllocation vout,
394 const void *usr,
395 const RsScriptCall *call)
Jason Samsc61346b2010-05-28 18:23:22 -0700396{
397 GET_TLS();
Jason Samsace3e012010-07-15 17:11:13 -0700398 const Allocation *ain = static_cast<const Allocation *>(vin);
Jason Samsc61346b2010-05-28 18:23:22 -0700399 Allocation *aout = static_cast<Allocation *>(vout);
Jason Samsc61346b2010-05-28 18:23:22 -0700400 Script *s = static_cast<Script *>(vs);
Jason Samsace3e012010-07-15 17:11:13 -0700401 s->runForEach(rsc, ain, aout, usr, call);
Jason Samsc61346b2010-05-28 18:23:22 -0700402}
403
Jason Samse45ac6e2009-07-20 14:31:06 -0700404//////////////////////////////////////////////////////////////////////////////
405// Class implementation
406//////////////////////////////////////////////////////////////////////////////
407
Jason Samsbe36bf32010-05-11 14:03:58 -0700408// llvm name mangling ref
409// <builtin-type> ::= v # void
410// ::= b # bool
411// ::= c # char
412// ::= a # signed char
413// ::= h # unsigned char
414// ::= s # short
415// ::= t # unsigned short
416// ::= i # int
417// ::= j # unsigned int
418// ::= l # long
419// ::= m # unsigned long
420// ::= x # long long, __int64
421// ::= y # unsigned long long, __int64
422// ::= f # float
423// ::= d # double
Jason Samse45ac6e2009-07-20 14:31:06 -0700424
Jason Samsaeb094b2010-05-18 13:35:45 -0700425static ScriptCState::SymbolTable_t gSyms[] = {
Jason Samsbe36bf32010-05-11 14:03:58 -0700426 { "__divsi3", (void *)&SC_divsi3 },
427
Jason Sams22fa3712010-05-19 17:22:57 -0700428 // allocation
429 { "rsAllocationGetDimX", (void *)&SC_allocGetDimX },
430 { "rsAllocationGetDimY", (void *)&SC_allocGetDimY },
431 { "rsAllocationGetDimZ", (void *)&SC_allocGetDimZ },
432 { "rsAllocationGetDimLOD", (void *)&SC_allocGetDimLOD },
433 { "rsAllocationGetDimFaces", (void *)&SC_allocGetDimFaces },
434 { "rsGetAllocation", (void *)&SC_getAllocation },
435
Jason Sams22fa3712010-05-19 17:22:57 -0700436 // Debug
437 { "_Z7rsDebugPKcf", (void *)&SC_debugF },
438 { "_Z7rsDebugPKcDv2_f", (void *)&SC_debugFv2 },
439 { "_Z7rsDebugPKcDv3_f", (void *)&SC_debugFv3 },
440 { "_Z7rsDebugPKcDv4_f", (void *)&SC_debugFv4 },
441 { "_Z7rsDebugPKci", (void *)&SC_debugI32 },
442 //extern void __attribute__((overloadable))rsDebug(const char *, const void *);
443
444
445 // RS Math
446 { "_Z6rsRandi", (void *)&SC_randi },
447 { "_Z6rsRandii", (void *)&SC_randi2 },
448 { "_Z6rsRandf", (void *)&SC_randf },
449 { "_Z6rsRandff", (void *)&SC_randf2 },
450 { "_Z6rsFracf", (void *)&SC_frac },
451
452 // time
453 { "rsSecond", (void *)&SC_second },
454 { "rsMinute", (void *)&SC_minute },
455 { "rsHour", (void *)&SC_hour },
456 { "rsDay", (void *)&SC_day },
457 { "rsMonth", (void *)&SC_month },
458 { "rsYear", (void *)&SC_year },
459 { "rsUptimeMillis", (void*)&SC_uptimeMillis2 },
460 { "rsStartTimeMillis", (void*)&SC_startTimeMillis2 },
461 { "rsElapsedTimeMillis", (void*)&SC_elapsedTimeMillis2 },
462
463 { "rsSendToClient", (void *)&SC_toClient },
464
465 // matrix
466 { "rsMatrixLoadIdentity", (void *)&SC_matrixLoadIdentity },
467 { "rsMatrixLoadFloat", (void *)&SC_matrixLoadFloat },
468 { "rsMatrixLoadMat", (void *)&SC_matrixLoadMat },
469 { "rsMatrixLoadRotate", (void *)&SC_matrixLoadRotate },
470 { "rsMatrixLoadScale", (void *)&SC_matrixLoadScale },
471 { "rsMatrixLoadTranslate", (void *)&SC_matrixLoadTranslate },
472 { "rsMatrixLoadMultiply", (void *)&SC_matrixLoadMultiply },
473 { "rsMatrixMultiply", (void *)&SC_matrixMultiply },
474 { "rsMatrixRotate", (void *)&SC_matrixRotate },
475 { "rsMatrixScale", (void *)&SC_matrixScale },
476 { "rsMatrixTranslate", (void *)&SC_matrixTranslate },
477
Jason Samsace3e012010-07-15 17:11:13 -0700478 { "_Z9rsForEach9rs_script13rs_allocationS0_PKv", (void *)&SC_ForEach },
479 //{ "_Z9rsForEach9rs_script13rs_allocationS0_PKv", (void *)&SC_ForEach2 },
Jason Sams22fa3712010-05-19 17:22:57 -0700480
481////////////////////////////////////////////////////////////////////
482
Jason Samsbe36bf32010-05-11 14:03:58 -0700483 //{ "sinf_fast", (void *)&SC_sinf_fast },
484 //{ "cosf_fast", (void *)&SC_cosf_fast },
Jason Samse45ac6e2009-07-20 14:31:06 -0700485
Jason Samsbe36bf32010-05-11 14:03:58 -0700486 { "scriptCall", (void *)&SC_scriptCall },
Jason Samsce92d4b2010-05-17 14:55:34 -0700487
Jason Samsc9d43db2009-07-28 12:02:16 -0700488
Jason Samsbe36bf32010-05-11 14:03:58 -0700489 { NULL, NULL }
Jason Samse45ac6e2009-07-20 14:31:06 -0700490};
491
492const ScriptCState::SymbolTable_t * ScriptCState::lookupSymbol(const char *sym)
493{
494 ScriptCState::SymbolTable_t *syms = gSyms;
495
496 while (syms->mPtr) {
497 if (!strcmp(syms->mName, sym)) {
498 return syms;
499 }
500 syms++;
501 }
502 return NULL;
503}
504