Jason Sams | c97bb88 | 2009-07-20 14:31:06 -0700 | [diff] [blame] | 1 | /* |
Stephen Hines | 473a204 | 2012-02-21 20:13:12 -0800 | [diff] [blame] | 2 | * Copyright (C) 2009-2012 The Android Open Source Project |
Jason Sams | c97bb88 | 2009-07-20 14:31:06 -0700 | [diff] [blame] | 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" |
Jason Sams | fcf7231 | 2011-04-20 15:09:01 -0700 | [diff] [blame] | 19 | #include "rsMatrix4x4.h" |
| 20 | #include "rsMatrix3x3.h" |
| 21 | #include "rsMatrix2x2.h" |
Jason Sams | c97bb88 | 2009-07-20 14:31:06 -0700 | [diff] [blame] | 22 | |
Joe Onorato | 3370ec9 | 2009-08-09 11:39:02 -0700 | [diff] [blame] | 23 | #include "utils/Timers.h" |
Jason Sams | c97bb88 | 2009-07-20 14:31:06 -0700 | [diff] [blame] | 24 | |
Romain Guy | 584a375 | 2009-07-30 18:45:01 -0700 | [diff] [blame] | 25 | #include <time.h> |
Romain Guy | 584a375 | 2009-07-30 18:45:01 -0700 | [diff] [blame] | 26 | |
Jason Sams | c97bb88 | 2009-07-20 14:31:06 -0700 | [diff] [blame] | 27 | using namespace android; |
| 28 | using namespace android::renderscript; |
| 29 | |
Jason Sams | fcf7231 | 2011-04-20 15:09:01 -0700 | [diff] [blame] | 30 | |
| 31 | namespace android { |
| 32 | namespace renderscript { |
Jason Sams | c97bb88 | 2009-07-20 14:31:06 -0700 | [diff] [blame] | 33 | |
Jason Sams | 4d33993 | 2010-05-11 14:03:58 -0700 | [diff] [blame] | 34 | |
Jason Sams | c97bb88 | 2009-07-20 14:31:06 -0700 | [diff] [blame] | 35 | ////////////////////////////////////////////////////////////////////////////// |
| 36 | // Math routines |
| 37 | ////////////////////////////////////////////////////////////////////////////// |
| 38 | |
Stephen Hines | 1bf1f8d | 2011-03-01 17:34:59 -0800 | [diff] [blame] | 39 | #if 0 |
Alex Sakhartchouk | ed9f210 | 2010-11-09 17:00:54 -0800 | [diff] [blame] | 40 | static float SC_sinf_fast(float x) { |
Romain Guy | cac80a6 | 2009-08-18 11:39:17 -0700 | [diff] [blame] | 41 | const float A = 1.0f / (2.0f * M_PI); |
| 42 | const float B = -16.0f; |
| 43 | const float C = 8.0f; |
Jason Sams | ea84a7c | 2009-09-04 14:42:41 -0700 | [diff] [blame] | 44 | |
Romain Guy | cac80a6 | 2009-08-18 11:39:17 -0700 | [diff] [blame] | 45 | // scale angle for easy argument reduction |
| 46 | x *= A; |
Jason Sams | ea84a7c | 2009-09-04 14:42:41 -0700 | [diff] [blame] | 47 | |
Romain Guy | cac80a6 | 2009-08-18 11:39:17 -0700 | [diff] [blame] | 48 | if (fabsf(x) >= 0.5f) { |
| 49 | // argument reduction |
| 50 | x = x - ceilf(x + 0.5f) + 1.0f; |
| 51 | } |
Jason Sams | ea84a7c | 2009-09-04 14:42:41 -0700 | [diff] [blame] | 52 | |
Romain Guy | cac80a6 | 2009-08-18 11:39:17 -0700 | [diff] [blame] | 53 | const float y = B * x * fabsf(x) + C * x; |
| 54 | return 0.2215f * (y * fabsf(y) - y) + y; |
| 55 | } |
| 56 | |
Alex Sakhartchouk | ed9f210 | 2010-11-09 17:00:54 -0800 | [diff] [blame] | 57 | static float SC_cosf_fast(float x) { |
Romain Guy | cac80a6 | 2009-08-18 11:39:17 -0700 | [diff] [blame] | 58 | 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 Sams | ea84a7c | 2009-09-04 14:42:41 -0700 | [diff] [blame] | 63 | |
Romain Guy | cac80a6 | 2009-08-18 11:39:17 -0700 | [diff] [blame] | 64 | // scale angle for easy argument reduction |
| 65 | x *= A; |
Jason Sams | ea84a7c | 2009-09-04 14:42:41 -0700 | [diff] [blame] | 66 | |
Romain Guy | cac80a6 | 2009-08-18 11:39:17 -0700 | [diff] [blame] | 67 | if (fabsf(x) >= 0.5f) { |
| 68 | // argument reduction |
| 69 | x = x - ceilf(x + 0.5f) + 1.0f; |
| 70 | } |
Jason Sams | ea84a7c | 2009-09-04 14:42:41 -0700 | [diff] [blame] | 71 | |
Romain Guy | cac80a6 | 2009-08-18 11:39:17 -0700 | [diff] [blame] | 72 | const float y = B * x * fabsf(x) + C * x; |
| 73 | return 0.2215f * (y * fabsf(y) - y) + y; |
| 74 | } |
Stephen Hines | 1bf1f8d | 2011-03-01 17:34:59 -0800 | [diff] [blame] | 75 | #endif |
Romain Guy | cac80a6 | 2009-08-18 11:39:17 -0700 | [diff] [blame] | 76 | |
Romain Guy | 584a375 | 2009-07-30 18:45:01 -0700 | [diff] [blame] | 77 | ////////////////////////////////////////////////////////////////////////////// |
| 78 | // Time routines |
| 79 | ////////////////////////////////////////////////////////////////////////////// |
Jason Sams | c97bb88 | 2009-07-20 14:31:06 -0700 | [diff] [blame] | 80 | |
Jason Sams | fcf7231 | 2011-04-20 15:09:01 -0700 | [diff] [blame] | 81 | time_t rsrTime(Context *rsc, Script *sc, time_t *timer) { |
Stephen Hines | 1ac9da6 | 2011-01-07 15:11:30 -0800 | [diff] [blame] | 82 | return time(timer); |
Romain Guy | 584a375 | 2009-07-30 18:45:01 -0700 | [diff] [blame] | 83 | } |
| 84 | |
Jason Sams | fcf7231 | 2011-04-20 15:09:01 -0700 | [diff] [blame] | 85 | tm* rsrLocalTime(Context *rsc, Script *sc, tm *local, time_t *timer) { |
Stephen Hines | 1ac9da6 | 2011-01-07 15:11:30 -0800 | [diff] [blame] | 86 | if (!local) { |
| 87 | return NULL; |
| 88 | } |
Jason Sams | 1bada8c | 2009-08-09 17:01:55 -0700 | [diff] [blame] | 89 | |
Stephen Hines | 1ac9da6 | 2011-01-07 15:11:30 -0800 | [diff] [blame] | 90 | // 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 Guy | 8839ca5 | 2009-07-31 11:20:59 -0700 | [diff] [blame] | 97 | } |
| 98 | |
Jason Sams | fcf7231 | 2011-04-20 15:09:01 -0700 | [diff] [blame] | 99 | int64_t rsrUptimeMillis(Context *rsc, Script *sc) { |
Jason Sams | d79b2e9 | 2010-05-19 17:22:57 -0700 | [diff] [blame] | 100 | return nanoseconds_to_milliseconds(systemTime(SYSTEM_TIME_MONOTONIC)); |
| 101 | } |
| 102 | |
Jason Sams | fcf7231 | 2011-04-20 15:09:01 -0700 | [diff] [blame] | 103 | int64_t rsrUptimeNanos(Context *rsc, Script *sc) { |
Jason Sams | f0690c4 | 2010-07-29 17:31:14 -0700 | [diff] [blame] | 104 | return systemTime(SYSTEM_TIME_MONOTONIC); |
Jason Sams | d79b2e9 | 2010-05-19 17:22:57 -0700 | [diff] [blame] | 105 | } |
| 106 | |
Jason Sams | fcf7231 | 2011-04-20 15:09:01 -0700 | [diff] [blame] | 107 | float rsrGetDt(Context *rsc, Script *sc) { |
Jason Sams | 1796651 | 2010-07-28 11:17:53 -0700 | [diff] [blame] | 108 | int64_t l = sc->mEnviroment.mLastDtTime; |
| 109 | sc->mEnviroment.mLastDtTime = systemTime(SYSTEM_TIME_MONOTONIC); |
| 110 | return ((float)(sc->mEnviroment.mLastDtTime - l)) / 1.0e9; |
Jason Sams | c97bb88 | 2009-07-20 14:31:06 -0700 | [diff] [blame] | 111 | } |
| 112 | |
Jason Sams | c97bb88 | 2009-07-20 14:31:06 -0700 | [diff] [blame] | 113 | ////////////////////////////////////////////////////////////////////////////// |
| 114 | // |
| 115 | ////////////////////////////////////////////////////////////////////////////// |
| 116 | |
Jason Sams | fcf7231 | 2011-04-20 15:09:01 -0700 | [diff] [blame] | 117 | void rsrSetObject(const Context *rsc, const Script *sc, ObjectBase **dst, ObjectBase * src) { |
Steve Block | 3762c31 | 2012-01-06 19:20:56 +0000 | [diff] [blame] | 118 | //ALOGE("rsiSetObject %p,%p %p", vdst, *vdst, vsrc); |
Jason Sams | e4a06c5 | 2011-03-16 16:29:28 -0700 | [diff] [blame] | 119 | if (src) { |
| 120 | CHECK_OBJ(src); |
| 121 | src->incSysRef(); |
Jason Sams | 4fd8bb4 | 2010-09-17 13:17:17 -0700 | [diff] [blame] | 122 | } |
Jason Sams | e4a06c5 | 2011-03-16 16:29:28 -0700 | [diff] [blame] | 123 | if (dst[0]) { |
| 124 | CHECK_OBJ(dst[0]); |
| 125 | dst[0]->decSysRef(); |
Jason Sams | 4fd8bb4 | 2010-09-17 13:17:17 -0700 | [diff] [blame] | 126 | } |
Jason Sams | e4a06c5 | 2011-03-16 16:29:28 -0700 | [diff] [blame] | 127 | *dst = src; |
Jason Sams | 02f62aa | 2010-08-16 12:41:48 -0700 | [diff] [blame] | 128 | } |
Alex Sakhartchouk | ed9f210 | 2010-11-09 17:00:54 -0800 | [diff] [blame] | 129 | |
Jason Sams | fcf7231 | 2011-04-20 15:09:01 -0700 | [diff] [blame] | 130 | void rsrClearObject(const Context *rsc, const Script *sc, ObjectBase **dst) { |
Steve Block | 3762c31 | 2012-01-06 19:20:56 +0000 | [diff] [blame] | 131 | //ALOGE("rsiClearObject %p,%p", vdst, *vdst); |
Jason Sams | e4a06c5 | 2011-03-16 16:29:28 -0700 | [diff] [blame] | 132 | if (dst[0]) { |
| 133 | CHECK_OBJ(dst[0]); |
| 134 | dst[0]->decSysRef(); |
Jason Sams | 4fd8bb4 | 2010-09-17 13:17:17 -0700 | [diff] [blame] | 135 | } |
Jason Sams | e4a06c5 | 2011-03-16 16:29:28 -0700 | [diff] [blame] | 136 | *dst = NULL; |
Jason Sams | 02f62aa | 2010-08-16 12:41:48 -0700 | [diff] [blame] | 137 | } |
Alex Sakhartchouk | ed9f210 | 2010-11-09 17:00:54 -0800 | [diff] [blame] | 138 | |
Jason Sams | fcf7231 | 2011-04-20 15:09:01 -0700 | [diff] [blame] | 139 | bool rsrIsObject(const Context *rsc, const Script *sc, const ObjectBase *src) { |
Jason Sams | e4a06c5 | 2011-03-16 16:29:28 -0700 | [diff] [blame] | 140 | return src != NULL; |
Jason Sams | 02f62aa | 2010-08-16 12:41:48 -0700 | [diff] [blame] | 141 | } |
| 142 | |
Jason Sams | d64188a | 2010-08-06 16:22:50 -0700 | [diff] [blame] | 143 | |
Jason Sams | fcf7231 | 2011-04-20 15:09:01 -0700 | [diff] [blame] | 144 | uint32_t rsrToClient(Context *rsc, Script *sc, int cmdID, void *data, int len) { |
Steve Block | 3762c31 | 2012-01-06 19:20:56 +0000 | [diff] [blame] | 145 | //ALOGE("SC_toClient %i %i %i", cmdID, len); |
Jason Sams | 1c41517 | 2010-11-08 17:06:46 -0800 | [diff] [blame] | 146 | return rsc->sendMessageToClient(data, RS_MESSAGE_TO_CLIENT_USER, cmdID, len, false); |
Jason Sams | 516c319 | 2009-10-06 13:58:47 -0700 | [diff] [blame] | 147 | } |
| 148 | |
Jason Sams | fcf7231 | 2011-04-20 15:09:01 -0700 | [diff] [blame] | 149 | uint32_t rsrToClientBlocking(Context *rsc, Script *sc, int cmdID, void *data, int len) { |
Steve Block | 3762c31 | 2012-01-06 19:20:56 +0000 | [diff] [blame] | 150 | //ALOGE("SC_toClientBlocking %i %i", cmdID, len); |
Jason Sams | 1c41517 | 2010-11-08 17:06:46 -0800 | [diff] [blame] | 151 | return rsc->sendMessageToClient(data, RS_MESSAGE_TO_CLIENT_USER, cmdID, len, true); |
Jason Sams | 1796651 | 2010-07-28 11:17:53 -0700 | [diff] [blame] | 152 | } |
| 153 | |
Jason Sams | bd2197f | 2009-10-07 18:14:01 -0700 | [diff] [blame] | 154 | |
Jason Sams | fcf7231 | 2011-04-20 15:09:01 -0700 | [diff] [blame] | 155 | void rsrForEach(Context *rsc, Script *sc, |
| 156 | Script *target, |
| 157 | Allocation *in, Allocation *out, |
| 158 | const void *usr, uint32_t usrBytes, |
Alex Sakhartchouk | ed9f210 | 2010-11-09 17:00:54 -0800 | [diff] [blame] | 159 | const RsScriptCall *call) { |
Stephen Hines | 473a204 | 2012-02-21 20:13:12 -0800 | [diff] [blame] | 160 | target->runForEach(rsc, /* root slot */ 0, in, out, usr, usrBytes, call); |
Jason Sams | f17bccc | 2010-05-28 18:23:22 -0700 | [diff] [blame] | 161 | } |
| 162 | |
Jason Sams | fcf7231 | 2011-04-20 15:09:01 -0700 | [diff] [blame] | 163 | void rsrAllocationSyncAll(Context *rsc, Script *sc, Allocation *a, RsAllocationUsageType usage) { |
| 164 | a->syncAll(rsc, usage); |
Jason Sams | 1afbf54 | 2011-01-25 21:33:44 -0800 | [diff] [blame] | 165 | } |
| 166 | |
Alex Sakhartchouk | 304b1f5 | 2011-06-14 11:13:19 -0700 | [diff] [blame] | 167 | void 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 | |
| 178 | void 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 Sams | fcf7231 | 2011-04-20 15:09:01 -0700 | [diff] [blame] | 190 | const Allocation * rsrGetAllocation(Context *rsc, Script *s, const void *ptr) { |
| 191 | ScriptC *sc = (ScriptC *)s; |
| 192 | return sc->ptrToAllocation(ptr); |
Jason Sams | 1afbf54 | 2011-01-25 21:33:44 -0800 | [diff] [blame] | 193 | } |
| 194 | |
Jason Sams | 1afbf54 | 2011-01-25 21:33:44 -0800 | [diff] [blame] | 195 | } |
Jason Sams | c97bb88 | 2009-07-20 14:31:06 -0700 | [diff] [blame] | 196 | } |
Jason Sams | e4a06c5 | 2011-03-16 16:29:28 -0700 | [diff] [blame] | 197 | |