Jason Sams | e45ac6e | 2009-07-20 14:31:06 -0700 | [diff] [blame] | 1 | /* |
Stephen Hines | 4419977 | 2012-02-21 20:13:12 -0800 | [diff] [blame] | 2 | * Copyright (C) 2009-2012 The Android Open Source Project |
Jason Sams | e45ac6e | 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 | 87fe59a | 2011-04-20 15:09:01 -0700 | [diff] [blame] | 19 | #include "rsMatrix4x4.h" |
| 20 | #include "rsMatrix3x3.h" |
| 21 | #include "rsMatrix2x2.h" |
Alex Sakhartchouk | 4edf030 | 2012-03-09 10:47:27 -0800 | [diff] [blame] | 22 | #include "rsgApiStructs.h" |
Jason Sams | e45ac6e | 2009-07-20 14:31:06 -0700 | [diff] [blame] | 23 | |
Tim Murray | 0b575de | 2013-03-15 15:56:43 -0700 | [diff] [blame^] | 24 | #ifndef RS_SERVER |
Joe Onorato | 9c4e4ca | 2009-08-09 11:39:02 -0700 | [diff] [blame] | 25 | #include "utils/Timers.h" |
Tim Murray | 0b575de | 2013-03-15 15:56:43 -0700 | [diff] [blame^] | 26 | #endif |
Jason Sams | e45ac6e | 2009-07-20 14:31:06 -0700 | [diff] [blame] | 27 | |
Romain Guy | 98e10fd | 2009-07-30 18:45:01 -0700 | [diff] [blame] | 28 | #include <time.h> |
Romain Guy | 98e10fd | 2009-07-30 18:45:01 -0700 | [diff] [blame] | 29 | |
Jason Sams | e45ac6e | 2009-07-20 14:31:06 -0700 | [diff] [blame] | 30 | using namespace android; |
| 31 | using namespace android::renderscript; |
| 32 | |
Jason Sams | 87fe59a | 2011-04-20 15:09:01 -0700 | [diff] [blame] | 33 | |
| 34 | namespace android { |
| 35 | namespace renderscript { |
Jason Sams | e45ac6e | 2009-07-20 14:31:06 -0700 | [diff] [blame] | 36 | |
Jason Sams | be36bf3 | 2010-05-11 14:03:58 -0700 | [diff] [blame] | 37 | |
Jason Sams | e45ac6e | 2009-07-20 14:31:06 -0700 | [diff] [blame] | 38 | ////////////////////////////////////////////////////////////////////////////// |
| 39 | // Math routines |
| 40 | ////////////////////////////////////////////////////////////////////////////// |
| 41 | |
Stephen Hines | 196c111 | 2011-03-01 17:34:59 -0800 | [diff] [blame] | 42 | #if 0 |
Alex Sakhartchouk | afb743a | 2010-11-09 17:00:54 -0800 | [diff] [blame] | 43 | static float SC_sinf_fast(float x) { |
Romain Guy | 2275d63 | 2009-08-18 11:39:17 -0700 | [diff] [blame] | 44 | const float A = 1.0f / (2.0f * M_PI); |
| 45 | const float B = -16.0f; |
| 46 | const float C = 8.0f; |
Jason Sams | a57c0a7 | 2009-09-04 14:42:41 -0700 | [diff] [blame] | 47 | |
Romain Guy | 2275d63 | 2009-08-18 11:39:17 -0700 | [diff] [blame] | 48 | // scale angle for easy argument reduction |
| 49 | x *= A; |
Jason Sams | a57c0a7 | 2009-09-04 14:42:41 -0700 | [diff] [blame] | 50 | |
Romain Guy | 2275d63 | 2009-08-18 11:39:17 -0700 | [diff] [blame] | 51 | if (fabsf(x) >= 0.5f) { |
| 52 | // argument reduction |
| 53 | x = x - ceilf(x + 0.5f) + 1.0f; |
| 54 | } |
Jason Sams | a57c0a7 | 2009-09-04 14:42:41 -0700 | [diff] [blame] | 55 | |
Romain Guy | 2275d63 | 2009-08-18 11:39:17 -0700 | [diff] [blame] | 56 | const float y = B * x * fabsf(x) + C * x; |
| 57 | return 0.2215f * (y * fabsf(y) - y) + y; |
| 58 | } |
| 59 | |
Alex Sakhartchouk | afb743a | 2010-11-09 17:00:54 -0800 | [diff] [blame] | 60 | static float SC_cosf_fast(float x) { |
Romain Guy | 2275d63 | 2009-08-18 11:39:17 -0700 | [diff] [blame] | 61 | x += float(M_PI / 2); |
| 62 | |
| 63 | const float A = 1.0f / (2.0f * M_PI); |
| 64 | const float B = -16.0f; |
| 65 | const float C = 8.0f; |
Jason Sams | a57c0a7 | 2009-09-04 14:42:41 -0700 | [diff] [blame] | 66 | |
Romain Guy | 2275d63 | 2009-08-18 11:39:17 -0700 | [diff] [blame] | 67 | // scale angle for easy argument reduction |
| 68 | x *= A; |
Jason Sams | a57c0a7 | 2009-09-04 14:42:41 -0700 | [diff] [blame] | 69 | |
Romain Guy | 2275d63 | 2009-08-18 11:39:17 -0700 | [diff] [blame] | 70 | if (fabsf(x) >= 0.5f) { |
| 71 | // argument reduction |
| 72 | x = x - ceilf(x + 0.5f) + 1.0f; |
| 73 | } |
Jason Sams | a57c0a7 | 2009-09-04 14:42:41 -0700 | [diff] [blame] | 74 | |
Romain Guy | 2275d63 | 2009-08-18 11:39:17 -0700 | [diff] [blame] | 75 | const float y = B * x * fabsf(x) + C * x; |
| 76 | return 0.2215f * (y * fabsf(y) - y) + y; |
| 77 | } |
Stephen Hines | 196c111 | 2011-03-01 17:34:59 -0800 | [diff] [blame] | 78 | #endif |
Romain Guy | 2275d63 | 2009-08-18 11:39:17 -0700 | [diff] [blame] | 79 | |
Romain Guy | 98e10fd | 2009-07-30 18:45:01 -0700 | [diff] [blame] | 80 | ////////////////////////////////////////////////////////////////////////////// |
| 81 | // Time routines |
| 82 | ////////////////////////////////////////////////////////////////////////////// |
Jason Sams | e45ac6e | 2009-07-20 14:31:06 -0700 | [diff] [blame] | 83 | |
Jason Sams | 709a097 | 2012-11-15 18:18:04 -0800 | [diff] [blame] | 84 | time_t rsrTime(Context *rsc, time_t *timer) { |
Stephen Hines | ca3f09c | 2011-01-07 15:11:30 -0800 | [diff] [blame] | 85 | return time(timer); |
Romain Guy | 98e10fd | 2009-07-30 18:45:01 -0700 | [diff] [blame] | 86 | } |
| 87 | |
Jason Sams | 709a097 | 2012-11-15 18:18:04 -0800 | [diff] [blame] | 88 | tm* rsrLocalTime(Context *rsc, tm *local, time_t *timer) { |
Stephen Hines | ca3f09c | 2011-01-07 15:11:30 -0800 | [diff] [blame] | 89 | if (!local) { |
| 90 | return NULL; |
| 91 | } |
Jason Sams | e5ffb87 | 2009-08-09 17:01:55 -0700 | [diff] [blame] | 92 | |
Stephen Hines | ca3f09c | 2011-01-07 15:11:30 -0800 | [diff] [blame] | 93 | // The native localtime function is not thread-safe, so we |
| 94 | // have to apply locking for proper behavior in RenderScript. |
| 95 | pthread_mutex_lock(&rsc->gLibMutex); |
| 96 | tm *tmp = localtime(timer); |
Jason Sams | 110f181 | 2013-03-14 16:02:18 -0700 | [diff] [blame] | 97 | #ifndef RS_COMPATIBILITY_LIB |
Stephen Hines | ca3f09c | 2011-01-07 15:11:30 -0800 | [diff] [blame] | 98 | memcpy(local, tmp, sizeof(*tmp)); |
Jason Sams | 110f181 | 2013-03-14 16:02:18 -0700 | [diff] [blame] | 99 | #else |
| 100 | // WORKAROUND to struct rs_tm != struct tm |
| 101 | memcpy(local, tmp, sizeof(int)*9); |
| 102 | #endif |
Stephen Hines | ca3f09c | 2011-01-07 15:11:30 -0800 | [diff] [blame] | 103 | pthread_mutex_unlock(&rsc->gLibMutex); |
| 104 | return local; |
Romain Guy | 39dbc80 | 2009-07-31 11:20:59 -0700 | [diff] [blame] | 105 | } |
| 106 | |
Jason Sams | 709a097 | 2012-11-15 18:18:04 -0800 | [diff] [blame] | 107 | int64_t rsrUptimeMillis(Context *rsc) { |
Tim Murray | 0b575de | 2013-03-15 15:56:43 -0700 | [diff] [blame^] | 108 | #ifndef RS_SERVER |
Jason Sams | 22fa371 | 2010-05-19 17:22:57 -0700 | [diff] [blame] | 109 | return nanoseconds_to_milliseconds(systemTime(SYSTEM_TIME_MONOTONIC)); |
Tim Murray | 0b575de | 2013-03-15 15:56:43 -0700 | [diff] [blame^] | 110 | #else |
| 111 | return 0; |
| 112 | #endif |
Jason Sams | 22fa371 | 2010-05-19 17:22:57 -0700 | [diff] [blame] | 113 | } |
| 114 | |
Jason Sams | 709a097 | 2012-11-15 18:18:04 -0800 | [diff] [blame] | 115 | int64_t rsrUptimeNanos(Context *rsc) { |
Tim Murray | 0b575de | 2013-03-15 15:56:43 -0700 | [diff] [blame^] | 116 | #ifndef RS_SERVER |
Jason Sams | 7349547 | 2010-07-29 17:31:14 -0700 | [diff] [blame] | 117 | return systemTime(SYSTEM_TIME_MONOTONIC); |
Tim Murray | 0b575de | 2013-03-15 15:56:43 -0700 | [diff] [blame^] | 118 | #else |
| 119 | return 0; |
| 120 | #endif |
Jason Sams | 22fa371 | 2010-05-19 17:22:57 -0700 | [diff] [blame] | 121 | } |
| 122 | |
Jason Sams | 709a097 | 2012-11-15 18:18:04 -0800 | [diff] [blame] | 123 | float rsrGetDt(Context *rsc, const Script *sc) { |
Tim Murray | 0b575de | 2013-03-15 15:56:43 -0700 | [diff] [blame^] | 124 | #ifndef RS_SERVER |
Jason Sams | ef5867a | 2010-07-28 11:17:53 -0700 | [diff] [blame] | 125 | int64_t l = sc->mEnviroment.mLastDtTime; |
| 126 | sc->mEnviroment.mLastDtTime = systemTime(SYSTEM_TIME_MONOTONIC); |
| 127 | return ((float)(sc->mEnviroment.mLastDtTime - l)) / 1.0e9; |
Tim Murray | 0b575de | 2013-03-15 15:56:43 -0700 | [diff] [blame^] | 128 | #else |
| 129 | return 0.f; |
| 130 | #endif |
Jason Sams | e45ac6e | 2009-07-20 14:31:06 -0700 | [diff] [blame] | 131 | } |
| 132 | |
Jason Sams | e45ac6e | 2009-07-20 14:31:06 -0700 | [diff] [blame] | 133 | ////////////////////////////////////////////////////////////////////////////// |
| 134 | // |
| 135 | ////////////////////////////////////////////////////////////////////////////// |
| 136 | |
Jason Sams | 709a097 | 2012-11-15 18:18:04 -0800 | [diff] [blame] | 137 | void rsrSetObject(const Context *rsc, ObjectBase **dst, ObjectBase * src) { |
Steve Block | af12ac6 | 2012-01-06 19:20:56 +0000 | [diff] [blame] | 138 | //ALOGE("rsiSetObject %p,%p %p", vdst, *vdst, vsrc); |
Jason Sams | bad8074 | 2011-03-16 16:29:28 -0700 | [diff] [blame] | 139 | if (src) { |
| 140 | CHECK_OBJ(src); |
| 141 | src->incSysRef(); |
Jason Sams | f24d7d0 | 2010-09-17 13:17:17 -0700 | [diff] [blame] | 142 | } |
Jason Sams | bad8074 | 2011-03-16 16:29:28 -0700 | [diff] [blame] | 143 | if (dst[0]) { |
| 144 | CHECK_OBJ(dst[0]); |
| 145 | dst[0]->decSysRef(); |
Jason Sams | f24d7d0 | 2010-09-17 13:17:17 -0700 | [diff] [blame] | 146 | } |
Jason Sams | bad8074 | 2011-03-16 16:29:28 -0700 | [diff] [blame] | 147 | *dst = src; |
Jason Sams | c093685 | 2010-08-16 12:41:48 -0700 | [diff] [blame] | 148 | } |
Alex Sakhartchouk | afb743a | 2010-11-09 17:00:54 -0800 | [diff] [blame] | 149 | |
Jason Sams | 709a097 | 2012-11-15 18:18:04 -0800 | [diff] [blame] | 150 | void rsrClearObject(const Context *rsc, ObjectBase **dst) { |
Steve Block | af12ac6 | 2012-01-06 19:20:56 +0000 | [diff] [blame] | 151 | //ALOGE("rsiClearObject %p,%p", vdst, *vdst); |
Jason Sams | bad8074 | 2011-03-16 16:29:28 -0700 | [diff] [blame] | 152 | if (dst[0]) { |
| 153 | CHECK_OBJ(dst[0]); |
| 154 | dst[0]->decSysRef(); |
Jason Sams | f24d7d0 | 2010-09-17 13:17:17 -0700 | [diff] [blame] | 155 | } |
Jason Sams | bad8074 | 2011-03-16 16:29:28 -0700 | [diff] [blame] | 156 | *dst = NULL; |
Jason Sams | c093685 | 2010-08-16 12:41:48 -0700 | [diff] [blame] | 157 | } |
Alex Sakhartchouk | afb743a | 2010-11-09 17:00:54 -0800 | [diff] [blame] | 158 | |
Jason Sams | 709a097 | 2012-11-15 18:18:04 -0800 | [diff] [blame] | 159 | bool rsrIsObject(const Context *rsc, const ObjectBase *src) { |
Jason Sams | bad8074 | 2011-03-16 16:29:28 -0700 | [diff] [blame] | 160 | return src != NULL; |
Jason Sams | c093685 | 2010-08-16 12:41:48 -0700 | [diff] [blame] | 161 | } |
| 162 | |
Jason Sams | 7dce6bc | 2010-08-06 16:22:50 -0700 | [diff] [blame] | 163 | |
Jason Sams | 709a097 | 2012-11-15 18:18:04 -0800 | [diff] [blame] | 164 | uint32_t rsrToClient(Context *rsc, int cmdID, void *data, int len) { |
Steve Block | af12ac6 | 2012-01-06 19:20:56 +0000 | [diff] [blame] | 165 | //ALOGE("SC_toClient %i %i %i", cmdID, len); |
Jason Sams | aad4bc5 | 2010-11-08 17:06:46 -0800 | [diff] [blame] | 166 | return rsc->sendMessageToClient(data, RS_MESSAGE_TO_CLIENT_USER, cmdID, len, false); |
Jason Sams | 8c401ef | 2009-10-06 13:58:47 -0700 | [diff] [blame] | 167 | } |
| 168 | |
Jason Sams | 709a097 | 2012-11-15 18:18:04 -0800 | [diff] [blame] | 169 | uint32_t rsrToClientBlocking(Context *rsc, int cmdID, void *data, int len) { |
Steve Block | af12ac6 | 2012-01-06 19:20:56 +0000 | [diff] [blame] | 170 | //ALOGE("SC_toClientBlocking %i %i", cmdID, len); |
Jason Sams | aad4bc5 | 2010-11-08 17:06:46 -0800 | [diff] [blame] | 171 | return rsc->sendMessageToClient(data, RS_MESSAGE_TO_CLIENT_USER, cmdID, len, true); |
Jason Sams | ef5867a | 2010-07-28 11:17:53 -0700 | [diff] [blame] | 172 | } |
| 173 | |
Jason Sams | 3a27c95 | 2009-10-07 18:14:01 -0700 | [diff] [blame] | 174 | |
Jason Sams | 709a097 | 2012-11-15 18:18:04 -0800 | [diff] [blame] | 175 | void rsrForEach(Context *rsc, |
Jason Sams | 87fe59a | 2011-04-20 15:09:01 -0700 | [diff] [blame] | 176 | Script *target, |
| 177 | Allocation *in, Allocation *out, |
| 178 | const void *usr, uint32_t usrBytes, |
Alex Sakhartchouk | afb743a | 2010-11-09 17:00:54 -0800 | [diff] [blame] | 179 | const RsScriptCall *call) { |
Stephen Hines | 4419977 | 2012-02-21 20:13:12 -0800 | [diff] [blame] | 180 | target->runForEach(rsc, /* root slot */ 0, in, out, usr, usrBytes, call); |
Jason Sams | c61346b | 2010-05-28 18:23:22 -0700 | [diff] [blame] | 181 | } |
| 182 | |
Jason Sams | 709a097 | 2012-11-15 18:18:04 -0800 | [diff] [blame] | 183 | void rsrAllocationSyncAll(Context *rsc, Allocation *a, RsAllocationUsageType usage) { |
Jason Sams | 87fe59a | 2011-04-20 15:09:01 -0700 | [diff] [blame] | 184 | a->syncAll(rsc, usage); |
Jason Sams | 693080e | 2011-01-25 21:33:44 -0800 | [diff] [blame] | 185 | } |
| 186 | |
Alex Sakhartchouk | 74a8279 | 2011-06-14 11:13:19 -0700 | [diff] [blame] | 187 | void rsrAllocationCopy1DRange(Context *rsc, Allocation *dstAlloc, |
| 188 | uint32_t dstOff, |
| 189 | uint32_t dstMip, |
| 190 | uint32_t count, |
| 191 | Allocation *srcAlloc, |
| 192 | uint32_t srcOff, uint32_t srcMip) { |
| 193 | rsi_AllocationCopy2DRange(rsc, dstAlloc, dstOff, 0, |
| 194 | dstMip, 0, count, 1, |
| 195 | srcAlloc, srcOff, 0, srcMip, 0); |
| 196 | } |
| 197 | |
| 198 | void rsrAllocationCopy2DRange(Context *rsc, Allocation *dstAlloc, |
| 199 | uint32_t dstXoff, uint32_t dstYoff, |
| 200 | uint32_t dstMip, uint32_t dstFace, |
| 201 | uint32_t width, uint32_t height, |
| 202 | Allocation *srcAlloc, |
| 203 | uint32_t srcXoff, uint32_t srcYoff, |
| 204 | uint32_t srcMip, uint32_t srcFace) { |
| 205 | rsi_AllocationCopy2DRange(rsc, dstAlloc, dstXoff, dstYoff, |
| 206 | dstMip, dstFace, width, height, |
| 207 | srcAlloc, srcXoff, srcYoff, srcMip, srcFace); |
| 208 | } |
| 209 | |
Jason Sams | 693080e | 2011-01-25 21:33:44 -0800 | [diff] [blame] | 210 | |
Jason Sams | 693080e | 2011-01-25 21:33:44 -0800 | [diff] [blame] | 211 | } |
Jason Sams | e45ac6e | 2009-07-20 14:31:06 -0700 | [diff] [blame] | 212 | } |
Jason Sams | bad8074 | 2011-03-16 16:29:28 -0700 | [diff] [blame] | 213 | |