1 /* 2 * Copyright (C) 2009-2012 The Android Open Source Project 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" 19 #include "rsMatrix4x4.h" 20 #include "rsMatrix3x3.h" 21 #include "rsMatrix2x2.h" 22 #include "rsgApiStructs.h" 23 24 #include "utils/Timers.h" 25 26 #include <time.h> 27 28 using namespace android; 29 using namespace android::renderscript; 30 31 32 namespace android { 33 namespace renderscript { 34 35 36 ////////////////////////////////////////////////////////////////////////////// 37 // Math routines 38 ////////////////////////////////////////////////////////////////////////////// 39 40 #if 0 41 static float SC_sinf_fast(float x) { 42 const float A = 1.0f / (2.0f * M_PI); 43 const float B = -16.0f; 44 const float C = 8.0f; 45 46 // scale angle for easy argument reduction 47 x *= A; 48 49 if (fabsf(x) >= 0.5f) { 50 // argument reduction 51 x = x - ceilf(x + 0.5f) + 1.0f; 52 } 53 54 const float y = B * x * fabsf(x) + C * x; 55 return 0.2215f * (y * fabsf(y) - y) + y; 56 } 57 58 static float SC_cosf_fast(float x) { 59 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; 64 65 // scale angle for easy argument reduction 66 x *= A; 67 68 if (fabsf(x) >= 0.5f) { 69 // argument reduction 70 x = x - ceilf(x + 0.5f) + 1.0f; 71 } 72 73 const float y = B * x * fabsf(x) + C * x; 74 return 0.2215f * (y * fabsf(y) - y) + y; 75 } 76 #endif 77 78 ////////////////////////////////////////////////////////////////////////////// 79 // Time routines 80 ////////////////////////////////////////////////////////////////////////////// 81 82 time_t rsrTime(Context *rsc, Script *sc, time_t *timer) { 83 return time(timer); 84 } 85 86 tm* rsrLocalTime(Context *rsc, Script *sc, tm *local, time_t *timer) { 87 if (!local) { 88 return NULL; 89 } 90 91 // 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; 98 } 99 100 int64_t rsrUptimeMillis(Context *rsc, Script *sc) { 101 return nanoseconds_to_milliseconds(systemTime(SYSTEM_TIME_MONOTONIC)); 102 } 103 104 int64_t rsrUptimeNanos(Context *rsc, Script *sc) { 105 return systemTime(SYSTEM_TIME_MONOTONIC); 106 } 107 108 float rsrGetDt(Context *rsc, Script *sc) { 109 int64_t l = sc->mEnviroment.mLastDtTime; 110 sc->mEnviroment.mLastDtTime = systemTime(SYSTEM_TIME_MONOTONIC); 111 return ((float)(sc->mEnviroment.mLastDtTime - l)) / 1.0e9; 112 } 113 114 ////////////////////////////////////////////////////////////////////////////// 115 // 116 ////////////////////////////////////////////////////////////////////////////// 117 118 void rsrSetObject(const Context *rsc, const Script *sc, ObjectBase **dst, ObjectBase * src) { 119 //ALOGE("rsiSetObject %p,%p %p", vdst, *vdst, vsrc); 120 if (src) { 121 CHECK_OBJ(src); 122 src->incSysRef(); 123 } 124 if (dst[0]) { 125 CHECK_OBJ(dst[0]); 126 dst[0]->decSysRef(); 127 } 128 *dst = src; 129 } 130 131 void rsrClearObject(const Context *rsc, const Script *sc, ObjectBase **dst) { 132 //ALOGE("rsiClearObject %p,%p", vdst, *vdst); 133 if (dst[0]) { 134 CHECK_OBJ(dst[0]); 135 dst[0]->decSysRef(); 136 } 137 *dst = NULL; 138 } 139 140 bool rsrIsObject(const Context *rsc, const Script *sc, const ObjectBase *src) { 141 return src != NULL; 142 } 143 144 145 uint32_t rsrToClient(Context *rsc, Script *sc, int cmdID, void *data, int len) { 146 //ALOGE("SC_toClient %i %i %i", cmdID, len); 147 return rsc->sendMessageToClient(data, RS_MESSAGE_TO_CLIENT_USER, cmdID, len, false); 148 } 149 150 uint32_t rsrToClientBlocking(Context *rsc, Script *sc, int cmdID, void *data, int len) { 151 //ALOGE("SC_toClientBlocking %i %i", cmdID, len); 152 return rsc->sendMessageToClient(data, RS_MESSAGE_TO_CLIENT_USER, cmdID, len, true); 153 } 154 155 156 void rsrForEach(Context *rsc, Script *sc, 157 Script *target, 158 Allocation *in, Allocation *out, 159 const void *usr, uint32_t usrBytes, 160 const RsScriptCall *call) { 161 target->runForEach(rsc, /* root slot */ 0, in, out, usr, usrBytes, call); 162 } 163 164 void rsrAllocationSyncAll(Context *rsc, Script *sc, Allocation *a, RsAllocationUsageType usage) { 165 a->syncAll(rsc, usage); 166 } 167 168 void 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 179 void 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 191 const Allocation * rsrGetAllocation(Context *rsc, Script *s, const void *ptr) { 192 ScriptC *sc = (ScriptC *)s; 193 return sc->ptrToAllocation(ptr); 194 } 195 196 } 197 } 198 199