blob: a8591f50593ca3d7214a3f6beb66b8c40b9afbfa [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 Sams709a0972012-11-15 18:18:04 -080082time_t rsrTime(Context *rsc, time_t *timer) {
Stephen Hinesca3f09c2011-01-07 15:11:30 -080083 return time(timer);
Romain Guy98e10fd2009-07-30 18:45:01 -070084}
85
Jason Sams709a0972012-11-15 18:18:04 -080086tm* rsrLocalTime(Context *rsc, 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);
Jason Sams110f1812013-03-14 16:02:18 -070095#ifndef RS_COMPATIBILITY_LIB
Stephen Hinesca3f09c2011-01-07 15:11:30 -080096 memcpy(local, tmp, sizeof(*tmp));
Jason Sams110f1812013-03-14 16:02:18 -070097#else
98 // WORKAROUND to struct rs_tm != struct tm
99 memcpy(local, tmp, sizeof(int)*9);
100#endif
Stephen Hinesca3f09c2011-01-07 15:11:30 -0800101 pthread_mutex_unlock(&rsc->gLibMutex);
102 return local;
Romain Guy39dbc802009-07-31 11:20:59 -0700103}
104
Jason Sams709a0972012-11-15 18:18:04 -0800105int64_t rsrUptimeMillis(Context *rsc) {
Jason Sams22fa3712010-05-19 17:22:57 -0700106 return nanoseconds_to_milliseconds(systemTime(SYSTEM_TIME_MONOTONIC));
107}
108
Jason Sams709a0972012-11-15 18:18:04 -0800109int64_t rsrUptimeNanos(Context *rsc) {
Jason Sams73495472010-07-29 17:31:14 -0700110 return systemTime(SYSTEM_TIME_MONOTONIC);
Jason Sams22fa3712010-05-19 17:22:57 -0700111}
112
Jason Sams709a0972012-11-15 18:18:04 -0800113float rsrGetDt(Context *rsc, const Script *sc) {
Jason Samsef5867a2010-07-28 11:17:53 -0700114 int64_t l = sc->mEnviroment.mLastDtTime;
115 sc->mEnviroment.mLastDtTime = systemTime(SYSTEM_TIME_MONOTONIC);
116 return ((float)(sc->mEnviroment.mLastDtTime - l)) / 1.0e9;
Jason Samse45ac6e2009-07-20 14:31:06 -0700117}
118
Jason Samse45ac6e2009-07-20 14:31:06 -0700119//////////////////////////////////////////////////////////////////////////////
120//
121//////////////////////////////////////////////////////////////////////////////
122
Jason Sams709a0972012-11-15 18:18:04 -0800123void rsrSetObject(const Context *rsc, ObjectBase **dst, ObjectBase * src) {
Steve Blockaf12ac62012-01-06 19:20:56 +0000124 //ALOGE("rsiSetObject %p,%p %p", vdst, *vdst, vsrc);
Jason Samsbad80742011-03-16 16:29:28 -0700125 if (src) {
126 CHECK_OBJ(src);
127 src->incSysRef();
Jason Samsf24d7d02010-09-17 13:17:17 -0700128 }
Jason Samsbad80742011-03-16 16:29:28 -0700129 if (dst[0]) {
130 CHECK_OBJ(dst[0]);
131 dst[0]->decSysRef();
Jason Samsf24d7d02010-09-17 13:17:17 -0700132 }
Jason Samsbad80742011-03-16 16:29:28 -0700133 *dst = src;
Jason Samsc0936852010-08-16 12:41:48 -0700134}
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800135
Jason Sams709a0972012-11-15 18:18:04 -0800136void rsrClearObject(const Context *rsc, ObjectBase **dst) {
Steve Blockaf12ac62012-01-06 19:20:56 +0000137 //ALOGE("rsiClearObject %p,%p", vdst, *vdst);
Jason Samsbad80742011-03-16 16:29:28 -0700138 if (dst[0]) {
139 CHECK_OBJ(dst[0]);
140 dst[0]->decSysRef();
Jason Samsf24d7d02010-09-17 13:17:17 -0700141 }
Jason Samsbad80742011-03-16 16:29:28 -0700142 *dst = NULL;
Jason Samsc0936852010-08-16 12:41:48 -0700143}
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800144
Jason Sams709a0972012-11-15 18:18:04 -0800145bool rsrIsObject(const Context *rsc, const ObjectBase *src) {
Jason Samsbad80742011-03-16 16:29:28 -0700146 return src != NULL;
Jason Samsc0936852010-08-16 12:41:48 -0700147}
148
Jason Sams7dce6bc2010-08-06 16:22:50 -0700149
Jason Sams709a0972012-11-15 18:18:04 -0800150uint32_t rsrToClient(Context *rsc, int cmdID, void *data, int len) {
Steve Blockaf12ac62012-01-06 19:20:56 +0000151 //ALOGE("SC_toClient %i %i %i", cmdID, len);
Jason Samsaad4bc52010-11-08 17:06:46 -0800152 return rsc->sendMessageToClient(data, RS_MESSAGE_TO_CLIENT_USER, cmdID, len, false);
Jason Sams8c401ef2009-10-06 13:58:47 -0700153}
154
Jason Sams709a0972012-11-15 18:18:04 -0800155uint32_t rsrToClientBlocking(Context *rsc, int cmdID, void *data, int len) {
Steve Blockaf12ac62012-01-06 19:20:56 +0000156 //ALOGE("SC_toClientBlocking %i %i", cmdID, len);
Jason Samsaad4bc52010-11-08 17:06:46 -0800157 return rsc->sendMessageToClient(data, RS_MESSAGE_TO_CLIENT_USER, cmdID, len, true);
Jason Samsef5867a2010-07-28 11:17:53 -0700158}
159
Jason Sams3a27c952009-10-07 18:14:01 -0700160
Jason Sams709a0972012-11-15 18:18:04 -0800161void rsrForEach(Context *rsc,
Jason Sams87fe59a2011-04-20 15:09:01 -0700162 Script *target,
163 Allocation *in, Allocation *out,
164 const void *usr, uint32_t usrBytes,
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800165 const RsScriptCall *call) {
Stephen Hines44199772012-02-21 20:13:12 -0800166 target->runForEach(rsc, /* root slot */ 0, in, out, usr, usrBytes, call);
Jason Samsc61346b2010-05-28 18:23:22 -0700167}
168
Jason Sams709a0972012-11-15 18:18:04 -0800169void rsrAllocationSyncAll(Context *rsc, Allocation *a, RsAllocationUsageType usage) {
Jason Sams87fe59a2011-04-20 15:09:01 -0700170 a->syncAll(rsc, usage);
Jason Sams693080e2011-01-25 21:33:44 -0800171}
172
Alex Sakhartchouk74a82792011-06-14 11:13:19 -0700173void rsrAllocationCopy1DRange(Context *rsc, Allocation *dstAlloc,
174 uint32_t dstOff,
175 uint32_t dstMip,
176 uint32_t count,
177 Allocation *srcAlloc,
178 uint32_t srcOff, uint32_t srcMip) {
179 rsi_AllocationCopy2DRange(rsc, dstAlloc, dstOff, 0,
180 dstMip, 0, count, 1,
181 srcAlloc, srcOff, 0, srcMip, 0);
182}
183
184void rsrAllocationCopy2DRange(Context *rsc, Allocation *dstAlloc,
185 uint32_t dstXoff, uint32_t dstYoff,
186 uint32_t dstMip, uint32_t dstFace,
187 uint32_t width, uint32_t height,
188 Allocation *srcAlloc,
189 uint32_t srcXoff, uint32_t srcYoff,
190 uint32_t srcMip, uint32_t srcFace) {
191 rsi_AllocationCopy2DRange(rsc, dstAlloc, dstXoff, dstYoff,
192 dstMip, dstFace, width, height,
193 srcAlloc, srcXoff, srcYoff, srcMip, srcFace);
194}
195
Jason Sams693080e2011-01-25 21:33:44 -0800196
Jason Sams693080e2011-01-25 21:33:44 -0800197}
Jason Samse45ac6e2009-07-20 14:31:06 -0700198}
Jason Samsbad80742011-03-16 16:29:28 -0700199