blob: 749495d1a6495d29a7feb34027fa8c3606d45410 [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"
Alex Sakhartchouk4edf0302012-03-09 10:47:27 -080022#include "rsgApiStructs.h"
Jason Samse45ac6e2009-07-20 14:31:06 -070023
Joe Onorato9c4e4ca2009-08-09 11:39:02 -070024#include "utils/Timers.h"
Jason Samse45ac6e2009-07-20 14:31:06 -070025
Romain Guy98e10fd2009-07-30 18:45:01 -070026#include <time.h>
Romain Guy98e10fd2009-07-30 18:45:01 -070027
Jason Samse45ac6e2009-07-20 14:31:06 -070028using namespace android;
29using namespace android::renderscript;
30
Jason Sams87fe59a2011-04-20 15:09:01 -070031
32namespace android {
33namespace renderscript {
Jason Samse45ac6e2009-07-20 14:31:06 -070034
Jason Samsbe36bf32010-05-11 14:03:58 -070035
Jason Samse45ac6e2009-07-20 14:31:06 -070036//////////////////////////////////////////////////////////////////////////////
37// Math routines
38//////////////////////////////////////////////////////////////////////////////
39
Stephen Hines196c1112011-03-01 17:34:59 -080040#if 0
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -080041static float SC_sinf_fast(float x) {
Romain Guy2275d632009-08-18 11:39:17 -070042 const float A = 1.0f / (2.0f * M_PI);
43 const float B = -16.0f;
44 const float C = 8.0f;
Jason Samsa57c0a72009-09-04 14:42:41 -070045
Romain Guy2275d632009-08-18 11:39:17 -070046 // scale angle for easy argument reduction
47 x *= A;
Jason Samsa57c0a72009-09-04 14:42:41 -070048
Romain Guy2275d632009-08-18 11:39:17 -070049 if (fabsf(x) >= 0.5f) {
50 // argument reduction
51 x = x - ceilf(x + 0.5f) + 1.0f;
52 }
Jason Samsa57c0a72009-09-04 14:42:41 -070053
Romain Guy2275d632009-08-18 11:39:17 -070054 const float y = B * x * fabsf(x) + C * x;
55 return 0.2215f * (y * fabsf(y) - y) + y;
56}
57
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -080058static float SC_cosf_fast(float x) {
Romain Guy2275d632009-08-18 11:39:17 -070059 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}
Stephen Hines196c1112011-03-01 17:34:59 -080076#endif
Romain Guy2275d632009-08-18 11:39:17 -070077
Romain Guy98e10fd2009-07-30 18:45:01 -070078//////////////////////////////////////////////////////////////////////////////
79// Time routines
80//////////////////////////////////////////////////////////////////////////////
Jason Samse45ac6e2009-07-20 14:31:06 -070081
Jason Sams87fe59a2011-04-20 15:09:01 -070082time_t rsrTime(Context *rsc, Script *sc, time_t *timer) {
Stephen Hinesca3f09c2011-01-07 15:11:30 -080083 return time(timer);
Romain Guy98e10fd2009-07-30 18:45:01 -070084}
85
Jason Sams87fe59a2011-04-20 15:09:01 -070086tm* rsrLocalTime(Context *rsc, Script *sc, tm *local, time_t *timer) {
Stephen Hinesca3f09c2011-01-07 15:11:30 -080087 if (!local) {
88 return NULL;
89 }
Jason Samse5ffb872009-08-09 17:01:55 -070090
Stephen Hinesca3f09c2011-01-07 15:11:30 -080091 // The native localtime function is not thread-safe, so we
92 // have to apply locking for proper behavior in RenderScript.
93 pthread_mutex_lock(&rsc->gLibMutex);
94 tm *tmp = localtime(timer);
95 memcpy(local, tmp, sizeof(*tmp));
96 pthread_mutex_unlock(&rsc->gLibMutex);
97 return local;
Romain Guy39dbc802009-07-31 11:20:59 -070098}
99
Jason Sams87fe59a2011-04-20 15:09:01 -0700100int64_t rsrUptimeMillis(Context *rsc, Script *sc) {
Jason Sams22fa3712010-05-19 17:22:57 -0700101 return nanoseconds_to_milliseconds(systemTime(SYSTEM_TIME_MONOTONIC));
102}
103
Jason Sams87fe59a2011-04-20 15:09:01 -0700104int64_t rsrUptimeNanos(Context *rsc, Script *sc) {
Jason Sams73495472010-07-29 17:31:14 -0700105 return systemTime(SYSTEM_TIME_MONOTONIC);
Jason Sams22fa3712010-05-19 17:22:57 -0700106}
107
Jason Sams87fe59a2011-04-20 15:09:01 -0700108float rsrGetDt(Context *rsc, Script *sc) {
Jason Samsef5867a2010-07-28 11:17:53 -0700109 int64_t l = sc->mEnviroment.mLastDtTime;
110 sc->mEnviroment.mLastDtTime = systemTime(SYSTEM_TIME_MONOTONIC);
111 return ((float)(sc->mEnviroment.mLastDtTime - l)) / 1.0e9;
Jason Samse45ac6e2009-07-20 14:31:06 -0700112}
113
Jason Samse45ac6e2009-07-20 14:31:06 -0700114//////////////////////////////////////////////////////////////////////////////
115//
116//////////////////////////////////////////////////////////////////////////////
117
Jason Sams87fe59a2011-04-20 15:09:01 -0700118void rsrSetObject(const Context *rsc, const Script *sc, ObjectBase **dst, ObjectBase * src) {
Steve Blockaf12ac62012-01-06 19:20:56 +0000119 //ALOGE("rsiSetObject %p,%p %p", vdst, *vdst, vsrc);
Jason Samsbad80742011-03-16 16:29:28 -0700120 if (src) {
121 CHECK_OBJ(src);
122 src->incSysRef();
Jason Samsf24d7d02010-09-17 13:17:17 -0700123 }
Jason Samsbad80742011-03-16 16:29:28 -0700124 if (dst[0]) {
125 CHECK_OBJ(dst[0]);
126 dst[0]->decSysRef();
Jason Samsf24d7d02010-09-17 13:17:17 -0700127 }
Jason Samsbad80742011-03-16 16:29:28 -0700128 *dst = src;
Jason Samsc0936852010-08-16 12:41:48 -0700129}
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800130
Jason Sams87fe59a2011-04-20 15:09:01 -0700131void rsrClearObject(const Context *rsc, const Script *sc, ObjectBase **dst) {
Steve Blockaf12ac62012-01-06 19:20:56 +0000132 //ALOGE("rsiClearObject %p,%p", vdst, *vdst);
Jason Samsbad80742011-03-16 16:29:28 -0700133 if (dst[0]) {
134 CHECK_OBJ(dst[0]);
135 dst[0]->decSysRef();
Jason Samsf24d7d02010-09-17 13:17:17 -0700136 }
Jason Samsbad80742011-03-16 16:29:28 -0700137 *dst = NULL;
Jason Samsc0936852010-08-16 12:41:48 -0700138}
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800139
Jason Sams87fe59a2011-04-20 15:09:01 -0700140bool rsrIsObject(const Context *rsc, const Script *sc, const ObjectBase *src) {
Jason Samsbad80742011-03-16 16:29:28 -0700141 return src != NULL;
Jason Samsc0936852010-08-16 12:41:48 -0700142}
143
Jason Sams7dce6bc2010-08-06 16:22:50 -0700144
Jason Sams87fe59a2011-04-20 15:09:01 -0700145uint32_t rsrToClient(Context *rsc, Script *sc, int cmdID, void *data, int len) {
Steve Blockaf12ac62012-01-06 19:20:56 +0000146 //ALOGE("SC_toClient %i %i %i", cmdID, len);
Jason Samsaad4bc52010-11-08 17:06:46 -0800147 return rsc->sendMessageToClient(data, RS_MESSAGE_TO_CLIENT_USER, cmdID, len, false);
Jason Sams8c401ef2009-10-06 13:58:47 -0700148}
149
Jason Sams87fe59a2011-04-20 15:09:01 -0700150uint32_t rsrToClientBlocking(Context *rsc, Script *sc, int cmdID, void *data, int len) {
Steve Blockaf12ac62012-01-06 19:20:56 +0000151 //ALOGE("SC_toClientBlocking %i %i", cmdID, len);
Jason Samsaad4bc52010-11-08 17:06:46 -0800152 return rsc->sendMessageToClient(data, RS_MESSAGE_TO_CLIENT_USER, cmdID, len, true);
Jason Samsef5867a2010-07-28 11:17:53 -0700153}
154
Jason Sams3a27c952009-10-07 18:14:01 -0700155
Jason Sams87fe59a2011-04-20 15:09:01 -0700156void rsrForEach(Context *rsc, Script *sc,
157 Script *target,
158 Allocation *in, Allocation *out,
159 const void *usr, uint32_t usrBytes,
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800160 const RsScriptCall *call) {
Stephen Hines44199772012-02-21 20:13:12 -0800161 target->runForEach(rsc, /* root slot */ 0, in, out, usr, usrBytes, call);
Jason Samsc61346b2010-05-28 18:23:22 -0700162}
163
Jason Sams87fe59a2011-04-20 15:09:01 -0700164void rsrAllocationSyncAll(Context *rsc, Script *sc, Allocation *a, RsAllocationUsageType usage) {
165 a->syncAll(rsc, usage);
Jason Sams693080e2011-01-25 21:33:44 -0800166}
167
Alex Sakhartchouk74a82792011-06-14 11:13:19 -0700168void rsrAllocationCopy1DRange(Context *rsc, Allocation *dstAlloc,
169 uint32_t dstOff,
170 uint32_t dstMip,
171 uint32_t count,
172 Allocation *srcAlloc,
173 uint32_t srcOff, uint32_t srcMip) {
174 rsi_AllocationCopy2DRange(rsc, dstAlloc, dstOff, 0,
175 dstMip, 0, count, 1,
176 srcAlloc, srcOff, 0, srcMip, 0);
177}
178
179void rsrAllocationCopy2DRange(Context *rsc, Allocation *dstAlloc,
180 uint32_t dstXoff, uint32_t dstYoff,
181 uint32_t dstMip, uint32_t dstFace,
182 uint32_t width, uint32_t height,
183 Allocation *srcAlloc,
184 uint32_t srcXoff, uint32_t srcYoff,
185 uint32_t srcMip, uint32_t srcFace) {
186 rsi_AllocationCopy2DRange(rsc, dstAlloc, dstXoff, dstYoff,
187 dstMip, dstFace, width, height,
188 srcAlloc, srcXoff, srcYoff, srcMip, srcFace);
189}
190
Jason Sams87fe59a2011-04-20 15:09:01 -0700191const Allocation * rsrGetAllocation(Context *rsc, Script *s, const void *ptr) {
192 ScriptC *sc = (ScriptC *)s;
193 return sc->ptrToAllocation(ptr);
Jason Sams693080e2011-01-25 21:33:44 -0800194}
195
Jason Sams693080e2011-01-25 21:33:44 -0800196}
Jason Samse45ac6e2009-07-20 14:31:06 -0700197}
Jason Samsbad80742011-03-16 16:29:28 -0700198