blob: a5a0fae9bf57dbd2a5d4d2d270dacf8242022f5e [file] [log] [blame]
Jason Samsc97bb882009-07-20 14:31:06 -07001/*
Stephen Hines473a2042012-02-21 20:13:12 -08002 * Copyright (C) 2009-2012 The Android Open Source Project
Jason Samsc97bb882009-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 Samsfcf72312011-04-20 15:09:01 -070019#include "rsMatrix4x4.h"
20#include "rsMatrix3x3.h"
21#include "rsMatrix2x2.h"
Jason Samsc97bb882009-07-20 14:31:06 -070022
Joe Onorato3370ec92009-08-09 11:39:02 -070023#include "utils/Timers.h"
Jason Samsc97bb882009-07-20 14:31:06 -070024
Romain Guy584a3752009-07-30 18:45:01 -070025#include <time.h>
Romain Guy584a3752009-07-30 18:45:01 -070026
Jason Samsc97bb882009-07-20 14:31:06 -070027using namespace android;
28using namespace android::renderscript;
29
Jason Samsfcf72312011-04-20 15:09:01 -070030
31namespace android {
32namespace renderscript {
Jason Samsc97bb882009-07-20 14:31:06 -070033
Jason Sams4d339932010-05-11 14:03:58 -070034
Jason Samsc97bb882009-07-20 14:31:06 -070035//////////////////////////////////////////////////////////////////////////////
36// Math routines
37//////////////////////////////////////////////////////////////////////////////
38
Stephen Hines1bf1f8d2011-03-01 17:34:59 -080039#if 0
Alex Sakhartchouked9f2102010-11-09 17:00:54 -080040static float SC_sinf_fast(float x) {
Romain Guycac80a62009-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 Samsea84a7c2009-09-04 14:42:41 -070044
Romain Guycac80a62009-08-18 11:39:17 -070045 // scale angle for easy argument reduction
46 x *= A;
Jason Samsea84a7c2009-09-04 14:42:41 -070047
Romain Guycac80a62009-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 Samsea84a7c2009-09-04 14:42:41 -070052
Romain Guycac80a62009-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 Sakhartchouked9f2102010-11-09 17:00:54 -080057static float SC_cosf_fast(float x) {
Romain Guycac80a62009-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 Samsea84a7c2009-09-04 14:42:41 -070063
Romain Guycac80a62009-08-18 11:39:17 -070064 // scale angle for easy argument reduction
65 x *= A;
Jason Samsea84a7c2009-09-04 14:42:41 -070066
Romain Guycac80a62009-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 Samsea84a7c2009-09-04 14:42:41 -070071
Romain Guycac80a62009-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 Hines1bf1f8d2011-03-01 17:34:59 -080075#endif
Romain Guycac80a62009-08-18 11:39:17 -070076
Romain Guy584a3752009-07-30 18:45:01 -070077//////////////////////////////////////////////////////////////////////////////
78// Time routines
79//////////////////////////////////////////////////////////////////////////////
Jason Samsc97bb882009-07-20 14:31:06 -070080
Jason Samsfcf72312011-04-20 15:09:01 -070081time_t rsrTime(Context *rsc, Script *sc, time_t *timer) {
Stephen Hines1ac9da62011-01-07 15:11:30 -080082 return time(timer);
Romain Guy584a3752009-07-30 18:45:01 -070083}
84
Jason Samsfcf72312011-04-20 15:09:01 -070085tm* rsrLocalTime(Context *rsc, Script *sc, tm *local, time_t *timer) {
Stephen Hines1ac9da62011-01-07 15:11:30 -080086 if (!local) {
87 return NULL;
88 }
Jason Sams1bada8c2009-08-09 17:01:55 -070089
Stephen Hines1ac9da62011-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 Guy8839ca52009-07-31 11:20:59 -070097}
98
Jason Samsfcf72312011-04-20 15:09:01 -070099int64_t rsrUptimeMillis(Context *rsc, Script *sc) {
Jason Samsd79b2e92010-05-19 17:22:57 -0700100 return nanoseconds_to_milliseconds(systemTime(SYSTEM_TIME_MONOTONIC));
101}
102
Jason Samsfcf72312011-04-20 15:09:01 -0700103int64_t rsrUptimeNanos(Context *rsc, Script *sc) {
Jason Samsf0690c42010-07-29 17:31:14 -0700104 return systemTime(SYSTEM_TIME_MONOTONIC);
Jason Samsd79b2e92010-05-19 17:22:57 -0700105}
106
Jason Samsfcf72312011-04-20 15:09:01 -0700107float rsrGetDt(Context *rsc, Script *sc) {
Jason Sams17966512010-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 Samsc97bb882009-07-20 14:31:06 -0700111}
112
Jason Samsc97bb882009-07-20 14:31:06 -0700113//////////////////////////////////////////////////////////////////////////////
114//
115//////////////////////////////////////////////////////////////////////////////
116
Jason Samsfcf72312011-04-20 15:09:01 -0700117void rsrSetObject(const Context *rsc, const Script *sc, ObjectBase **dst, ObjectBase * src) {
Steve Block3762c312012-01-06 19:20:56 +0000118 //ALOGE("rsiSetObject %p,%p %p", vdst, *vdst, vsrc);
Jason Samse4a06c52011-03-16 16:29:28 -0700119 if (src) {
120 CHECK_OBJ(src);
121 src->incSysRef();
Jason Sams4fd8bb42010-09-17 13:17:17 -0700122 }
Jason Samse4a06c52011-03-16 16:29:28 -0700123 if (dst[0]) {
124 CHECK_OBJ(dst[0]);
125 dst[0]->decSysRef();
Jason Sams4fd8bb42010-09-17 13:17:17 -0700126 }
Jason Samse4a06c52011-03-16 16:29:28 -0700127 *dst = src;
Jason Sams02f62aa2010-08-16 12:41:48 -0700128}
Alex Sakhartchouked9f2102010-11-09 17:00:54 -0800129
Jason Samsfcf72312011-04-20 15:09:01 -0700130void rsrClearObject(const Context *rsc, const Script *sc, ObjectBase **dst) {
Steve Block3762c312012-01-06 19:20:56 +0000131 //ALOGE("rsiClearObject %p,%p", vdst, *vdst);
Jason Samse4a06c52011-03-16 16:29:28 -0700132 if (dst[0]) {
133 CHECK_OBJ(dst[0]);
134 dst[0]->decSysRef();
Jason Sams4fd8bb42010-09-17 13:17:17 -0700135 }
Jason Samse4a06c52011-03-16 16:29:28 -0700136 *dst = NULL;
Jason Sams02f62aa2010-08-16 12:41:48 -0700137}
Alex Sakhartchouked9f2102010-11-09 17:00:54 -0800138
Jason Samsfcf72312011-04-20 15:09:01 -0700139bool rsrIsObject(const Context *rsc, const Script *sc, const ObjectBase *src) {
Jason Samse4a06c52011-03-16 16:29:28 -0700140 return src != NULL;
Jason Sams02f62aa2010-08-16 12:41:48 -0700141}
142
Jason Samsd64188a2010-08-06 16:22:50 -0700143
Jason Samsfcf72312011-04-20 15:09:01 -0700144uint32_t rsrToClient(Context *rsc, Script *sc, int cmdID, void *data, int len) {
Steve Block3762c312012-01-06 19:20:56 +0000145 //ALOGE("SC_toClient %i %i %i", cmdID, len);
Jason Sams1c415172010-11-08 17:06:46 -0800146 return rsc->sendMessageToClient(data, RS_MESSAGE_TO_CLIENT_USER, cmdID, len, false);
Jason Sams516c3192009-10-06 13:58:47 -0700147}
148
Jason Samsfcf72312011-04-20 15:09:01 -0700149uint32_t rsrToClientBlocking(Context *rsc, Script *sc, int cmdID, void *data, int len) {
Steve Block3762c312012-01-06 19:20:56 +0000150 //ALOGE("SC_toClientBlocking %i %i", cmdID, len);
Jason Sams1c415172010-11-08 17:06:46 -0800151 return rsc->sendMessageToClient(data, RS_MESSAGE_TO_CLIENT_USER, cmdID, len, true);
Jason Sams17966512010-07-28 11:17:53 -0700152}
153
Jason Samsbd2197f2009-10-07 18:14:01 -0700154
Jason Samsfcf72312011-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 Sakhartchouked9f2102010-11-09 17:00:54 -0800159 const RsScriptCall *call) {
Stephen Hines473a2042012-02-21 20:13:12 -0800160 target->runForEach(rsc, /* root slot */ 0, in, out, usr, usrBytes, call);
Jason Samsf17bccc2010-05-28 18:23:22 -0700161}
162
Jason Samsfcf72312011-04-20 15:09:01 -0700163void rsrAllocationSyncAll(Context *rsc, Script *sc, Allocation *a, RsAllocationUsageType usage) {
164 a->syncAll(rsc, usage);
Jason Sams1afbf542011-01-25 21:33:44 -0800165}
166
Alex Sakhartchouk304b1f52011-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 Samsfcf72312011-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 Sams1afbf542011-01-25 21:33:44 -0800193}
194
Jason Sams1afbf542011-01-25 21:33:44 -0800195}
Jason Samsc97bb882009-07-20 14:31:06 -0700196}
Jason Samse4a06c52011-03-16 16:29:28 -0700197