blob: a5a0fae9bf57dbd2a5d4d2d270dacf8242022f5e [file] [log] [blame]
Jason Samse45ac6e2009-07-20 14:31:06 -07001/*
Stephen Hines44199772012-02-21 20:13:12 -08002 * Copyright (C) 2009-2012 The Android Open Source Project
Jason Samse45ac6e2009-07-20 14:31:06 -07003 *
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"
Jason Sams87fe59a2011-04-20 15:09:01 -070019#include "rsMatrix4x4.h"
20#include "rsMatrix3x3.h"
21#include "rsMatrix2x2.h"
Jason Samse45ac6e2009-07-20 14:31:06 -070022
Joe Onorato9c4e4ca2009-08-09 11:39:02 -070023#include "utils/Timers.h"
Jason Samse45ac6e2009-07-20 14:31:06 -070024
Romain Guy98e10fd2009-07-30 18:45:01 -070025#include <time.h>
Romain Guy98e10fd2009-07-30 18:45:01 -070026
Jason Samse45ac6e2009-07-20 14:31:06 -070027using namespace android;
28using namespace android::renderscript;
29
Jason Sams87fe59a2011-04-20 15:09:01 -070030
31namespace android {
32namespace renderscript {
Jason Samse45ac6e2009-07-20 14:31:06 -070033
Jason Samsbe36bf32010-05-11 14:03:58 -070034
Jason Samse45ac6e2009-07-20 14:31:06 -070035//////////////////////////////////////////////////////////////////////////////
36// Math routines
37//////////////////////////////////////////////////////////////////////////////
38
Stephen Hines196c1112011-03-01 17:34:59 -080039#if 0
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -080040static float SC_sinf_fast(float x) {
Romain Guy2275d632009-08-18 11:39:17 -070041 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
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -080057static float SC_cosf_fast(float x) {
Romain Guy2275d632009-08-18 11:39:17 -070058 x += float(M_PI / 2);
59
60 const float A = 1.0f / (2.0f * M_PI);
61 const float B = -16.0f;
62 const float C = 8.0f;
Jason Samsa57c0a72009-09-04 14:42:41 -070063
Romain Guy2275d632009-08-18 11:39:17 -070064 // scale angle for easy argument reduction
65 x *= A;
Jason Samsa57c0a72009-09-04 14:42:41 -070066
Romain Guy2275d632009-08-18 11:39:17 -070067 if (fabsf(x) >= 0.5f) {
68 // argument reduction
69 x = x - ceilf(x + 0.5f) + 1.0f;
70 }
Jason Samsa57c0a72009-09-04 14:42:41 -070071
Romain Guy2275d632009-08-18 11:39:17 -070072 const float y = B * x * fabsf(x) + C * x;
73 return 0.2215f * (y * fabsf(y) - y) + y;
74}
Stephen Hines196c1112011-03-01 17:34:59 -080075#endif
Romain Guy2275d632009-08-18 11:39:17 -070076
Romain Guy98e10fd2009-07-30 18:45:01 -070077//////////////////////////////////////////////////////////////////////////////
78// Time routines
79//////////////////////////////////////////////////////////////////////////////
Jason Samse45ac6e2009-07-20 14:31:06 -070080
Jason Sams87fe59a2011-04-20 15:09:01 -070081time_t rsrTime(Context *rsc, Script *sc, time_t *timer) {
Stephen Hinesca3f09c2011-01-07 15:11:30 -080082 return time(timer);
Romain Guy98e10fd2009-07-30 18:45:01 -070083}
84
Jason Sams87fe59a2011-04-20 15:09:01 -070085tm* rsrLocalTime(Context *rsc, Script *sc, tm *local, time_t *timer) {
Stephen Hinesca3f09c2011-01-07 15:11:30 -080086 if (!local) {
87 return NULL;
88 }
Jason Samse5ffb872009-08-09 17:01:55 -070089
Stephen Hinesca3f09c2011-01-07 15:11:30 -080090 // The native localtime function is not thread-safe, so we
91 // have to apply locking for proper behavior in RenderScript.
92 pthread_mutex_lock(&rsc->gLibMutex);
93 tm *tmp = localtime(timer);
94 memcpy(local, tmp, sizeof(*tmp));
95 pthread_mutex_unlock(&rsc->gLibMutex);
96 return local;
Romain Guy39dbc802009-07-31 11:20:59 -070097}
98
Jason Sams87fe59a2011-04-20 15:09:01 -070099int64_t rsrUptimeMillis(Context *rsc, Script *sc) {
Jason Sams22fa3712010-05-19 17:22:57 -0700100 return nanoseconds_to_milliseconds(systemTime(SYSTEM_TIME_MONOTONIC));
101}
102
Jason Sams87fe59a2011-04-20 15:09:01 -0700103int64_t rsrUptimeNanos(Context *rsc, Script *sc) {
Jason Sams73495472010-07-29 17:31:14 -0700104 return systemTime(SYSTEM_TIME_MONOTONIC);
Jason Sams22fa3712010-05-19 17:22:57 -0700105}
106
Jason Sams87fe59a2011-04-20 15:09:01 -0700107float rsrGetDt(Context *rsc, Script *sc) {
Jason Samsef5867a2010-07-28 11:17:53 -0700108 int64_t l = sc->mEnviroment.mLastDtTime;
109 sc->mEnviroment.mLastDtTime = systemTime(SYSTEM_TIME_MONOTONIC);
110 return ((float)(sc->mEnviroment.mLastDtTime - l)) / 1.0e9;
Jason Samse45ac6e2009-07-20 14:31:06 -0700111}
112
Jason Samse45ac6e2009-07-20 14:31:06 -0700113//////////////////////////////////////////////////////////////////////////////
114//
115//////////////////////////////////////////////////////////////////////////////
116
Jason Sams87fe59a2011-04-20 15:09:01 -0700117void rsrSetObject(const Context *rsc, const Script *sc, ObjectBase **dst, ObjectBase * src) {
Steve Blockaf12ac62012-01-06 19:20:56 +0000118 //ALOGE("rsiSetObject %p,%p %p", vdst, *vdst, vsrc);
Jason Samsbad80742011-03-16 16:29:28 -0700119 if (src) {
120 CHECK_OBJ(src);
121 src->incSysRef();
Jason Samsf24d7d02010-09-17 13:17:17 -0700122 }
Jason Samsbad80742011-03-16 16:29:28 -0700123 if (dst[0]) {
124 CHECK_OBJ(dst[0]);
125 dst[0]->decSysRef();
Jason Samsf24d7d02010-09-17 13:17:17 -0700126 }
Jason Samsbad80742011-03-16 16:29:28 -0700127 *dst = src;
Jason Samsc0936852010-08-16 12:41:48 -0700128}
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800129
Jason Sams87fe59a2011-04-20 15:09:01 -0700130void rsrClearObject(const Context *rsc, const Script *sc, ObjectBase **dst) {
Steve Blockaf12ac62012-01-06 19:20:56 +0000131 //ALOGE("rsiClearObject %p,%p", vdst, *vdst);
Jason Samsbad80742011-03-16 16:29:28 -0700132 if (dst[0]) {
133 CHECK_OBJ(dst[0]);
134 dst[0]->decSysRef();
Jason Samsf24d7d02010-09-17 13:17:17 -0700135 }
Jason Samsbad80742011-03-16 16:29:28 -0700136 *dst = NULL;
Jason Samsc0936852010-08-16 12:41:48 -0700137}
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800138
Jason Sams87fe59a2011-04-20 15:09:01 -0700139bool rsrIsObject(const Context *rsc, const Script *sc, const ObjectBase *src) {
Jason Samsbad80742011-03-16 16:29:28 -0700140 return src != NULL;
Jason Samsc0936852010-08-16 12:41:48 -0700141}
142
Jason Sams7dce6bc2010-08-06 16:22:50 -0700143
Jason Sams87fe59a2011-04-20 15:09:01 -0700144uint32_t rsrToClient(Context *rsc, Script *sc, int cmdID, void *data, int len) {
Steve Blockaf12ac62012-01-06 19:20:56 +0000145 //ALOGE("SC_toClient %i %i %i", cmdID, len);
Jason Samsaad4bc52010-11-08 17:06:46 -0800146 return rsc->sendMessageToClient(data, RS_MESSAGE_TO_CLIENT_USER, cmdID, len, false);
Jason Sams8c401ef2009-10-06 13:58:47 -0700147}
148
Jason Sams87fe59a2011-04-20 15:09:01 -0700149uint32_t rsrToClientBlocking(Context *rsc, Script *sc, int cmdID, void *data, int len) {
Steve Blockaf12ac62012-01-06 19:20:56 +0000150 //ALOGE("SC_toClientBlocking %i %i", cmdID, len);
Jason Samsaad4bc52010-11-08 17:06:46 -0800151 return rsc->sendMessageToClient(data, RS_MESSAGE_TO_CLIENT_USER, cmdID, len, true);
Jason Samsef5867a2010-07-28 11:17:53 -0700152}
153
Jason Sams3a27c952009-10-07 18:14:01 -0700154
Jason Sams87fe59a2011-04-20 15:09:01 -0700155void rsrForEach(Context *rsc, Script *sc,
156 Script *target,
157 Allocation *in, Allocation *out,
158 const void *usr, uint32_t usrBytes,
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800159 const RsScriptCall *call) {
Stephen Hines44199772012-02-21 20:13:12 -0800160 target->runForEach(rsc, /* root slot */ 0, in, out, usr, usrBytes, call);
Jason Samsc61346b2010-05-28 18:23:22 -0700161}
162
Jason Sams87fe59a2011-04-20 15:09:01 -0700163void rsrAllocationSyncAll(Context *rsc, Script *sc, Allocation *a, RsAllocationUsageType usage) {
164 a->syncAll(rsc, usage);
Jason Sams693080e2011-01-25 21:33:44 -0800165}
166
Alex Sakhartchouk74a82792011-06-14 11:13:19 -0700167void rsrAllocationCopy1DRange(Context *rsc, Allocation *dstAlloc,
168 uint32_t dstOff,
169 uint32_t dstMip,
170 uint32_t count,
171 Allocation *srcAlloc,
172 uint32_t srcOff, uint32_t srcMip) {
173 rsi_AllocationCopy2DRange(rsc, dstAlloc, dstOff, 0,
174 dstMip, 0, count, 1,
175 srcAlloc, srcOff, 0, srcMip, 0);
176}
177
178void rsrAllocationCopy2DRange(Context *rsc, Allocation *dstAlloc,
179 uint32_t dstXoff, uint32_t dstYoff,
180 uint32_t dstMip, uint32_t dstFace,
181 uint32_t width, uint32_t height,
182 Allocation *srcAlloc,
183 uint32_t srcXoff, uint32_t srcYoff,
184 uint32_t srcMip, uint32_t srcFace) {
185 rsi_AllocationCopy2DRange(rsc, dstAlloc, dstXoff, dstYoff,
186 dstMip, dstFace, width, height,
187 srcAlloc, srcXoff, srcYoff, srcMip, srcFace);
188}
189
Jason Sams87fe59a2011-04-20 15:09:01 -0700190const Allocation * rsrGetAllocation(Context *rsc, Script *s, const void *ptr) {
191 ScriptC *sc = (ScriptC *)s;
192 return sc->ptrToAllocation(ptr);
Jason Sams693080e2011-01-25 21:33:44 -0800193}
194
Jason Sams693080e2011-01-25 21:33:44 -0800195}
Jason Samse45ac6e2009-07-20 14:31:06 -0700196}
Jason Samsbad80742011-03-16 16:29:28 -0700197