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#if !defined(RS_SERVER) && !defined(RS_COMPATIBILITY_LIB) 25#include "utils/Timers.h" 26#endif 27 28#include <time.h> 29 30using namespace android; 31using namespace android::renderscript; 32 33 34namespace android { 35namespace renderscript { 36 37 38////////////////////////////////////////////////////////////////////////////// 39// Math routines 40////////////////////////////////////////////////////////////////////////////// 41 42#if 0 43static float SC_sinf_fast(float x) { 44 const float A = 1.0f / (2.0f * M_PI); 45 const float B = -16.0f; 46 const float C = 8.0f; 47 48 // scale angle for easy argument reduction 49 x *= A; 50 51 if (fabsf(x) >= 0.5f) { 52 // argument reduction 53 x = x - ceilf(x + 0.5f) + 1.0f; 54 } 55 56 const float y = B * x * fabsf(x) + C * x; 57 return 0.2215f * (y * fabsf(y) - y) + y; 58} 59 60static float SC_cosf_fast(float x) { 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; 66 67 // scale angle for easy argument reduction 68 x *= A; 69 70 if (fabsf(x) >= 0.5f) { 71 // argument reduction 72 x = x - ceilf(x + 0.5f) + 1.0f; 73 } 74 75 const float y = B * x * fabsf(x) + C * x; 76 return 0.2215f * (y * fabsf(y) - y) + y; 77} 78#endif 79 80////////////////////////////////////////////////////////////////////////////// 81// Time routines 82////////////////////////////////////////////////////////////////////////////// 83 84time_t rsrTime(Context *rsc, time_t *timer) { 85 return time(timer); 86} 87 88tm* rsrLocalTime(Context *rsc, tm *local, time_t *timer) { 89 if (!local) { 90 return nullptr; 91 } 92 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); 97 memcpy(local, tmp, sizeof(int)*9); 98 pthread_mutex_unlock(&rsc->gLibMutex); 99 return local; 100} 101 102int64_t rsrUptimeMillis(Context *rsc) { 103#ifndef RS_SERVER 104 return nanoseconds_to_milliseconds(systemTime(SYSTEM_TIME_MONOTONIC)); 105#else 106 return 0; 107#endif 108} 109 110int64_t rsrUptimeNanos(Context *rsc) { 111#ifndef RS_SERVER 112 return systemTime(SYSTEM_TIME_MONOTONIC); 113#else 114 return 0; 115#endif 116} 117 118float rsrGetDt(Context *rsc, const Script *sc) { 119#ifndef RS_SERVER 120 int64_t l = sc->mEnviroment.mLastDtTime; 121 sc->mEnviroment.mLastDtTime = systemTime(SYSTEM_TIME_MONOTONIC); 122 return ((float)(sc->mEnviroment.mLastDtTime - l)) / 1.0e9; 123#else 124 return 0.f; 125#endif 126} 127 128////////////////////////////////////////////////////////////////////////////// 129// 130////////////////////////////////////////////////////////////////////////////// 131 132static void SetObjectRef(const Context *rsc, const ObjectBase *dst, const ObjectBase *src) { 133 //ALOGE("setObjectRef %p,%p %p", rsc, dst, src); 134 if (src) { 135 CHECK_OBJ(src); 136 src->incSysRef(); 137 } 138 if (dst) { 139 CHECK_OBJ(dst); 140 dst->decSysRef(); 141 } 142} 143 144// Legacy, remove when drivers are updated 145void rsrClearObject(const Context *rsc, void *dst) { 146 ObjectBase **odst = (ObjectBase **)dst; 147 if (ObjectBase::gDebugReferences) { 148 ALOGE("rsrClearObject %p,%p", odst, *odst); 149 } 150 if (odst[0]) { 151 CHECK_OBJ(odst[0]); 152 odst[0]->decSysRef(); 153 } 154 *odst = nullptr; 155} 156 157void rsrClearObject(const Context *rsc, rs_object_base *dst) { 158 if (ObjectBase::gDebugReferences) { 159 ALOGE("rsrClearObject %p,%p", dst, dst->p); 160 } 161 if (dst->p) { 162 CHECK_OBJ(dst->p); 163 dst->p->decSysRef(); 164 } 165 dst->p = nullptr; 166} 167 168// Legacy, remove when drivers are updated 169void rsrSetObject(const Context *rsc, void *dst, ObjectBase *src) { 170 if (src == nullptr) { 171 rsrClearObject(rsc, dst); 172 return; 173 } 174 175 ObjectBase **odst = (ObjectBase **)dst; 176 if (ObjectBase::gDebugReferences) { 177 ALOGE("rsrSetObject (base) %p,%p %p", dst, *odst, src); 178 } 179 SetObjectRef(rsc, odst[0], src); 180 src->callUpdateCacheObject(rsc, dst); 181} 182 183void rsrSetObject(const Context *rsc, rs_object_base *dst, const ObjectBase *src) { 184 if (src == nullptr) { 185 rsrClearObject(rsc, dst); 186 return; 187 } 188 189 ObjectBase **odst = (ObjectBase **)dst; 190 if (ObjectBase::gDebugReferences) { 191 ALOGE("rsrSetObject (base) %p,%p %p", dst, *odst, src); 192 } 193 SetObjectRef(rsc, odst[0], src); 194 src->callUpdateCacheObject(rsc, dst); 195} 196 197// Legacy, remove when drivers are updated 198bool rsrIsObject(const Context *, ObjectBase* src) { 199 ObjectBase **osrc = (ObjectBase **)src; 200 return osrc != nullptr; 201} 202 203bool rsrIsObject(const Context *rsc, rs_object_base o) { 204 return o.p != nullptr; 205} 206 207 208 209uint32_t rsrToClient(Context *rsc, int cmdID, const void *data, int len) { 210 //ALOGE("SC_toClient %i %i %i", cmdID, len); 211 return rsc->sendMessageToClient(data, RS_MESSAGE_TO_CLIENT_USER, cmdID, len, false); 212} 213 214uint32_t rsrToClientBlocking(Context *rsc, int cmdID, const void *data, int len) { 215 //ALOGE("SC_toClientBlocking %i %i", cmdID, len); 216 return rsc->sendMessageToClient(data, RS_MESSAGE_TO_CLIENT_USER, cmdID, len, true); 217} 218 219// Keep these two routines (using non-const void pointers) so that we can 220// still use existing GPU drivers. 221uint32_t rsrToClient(Context *rsc, int cmdID, void *data, int len) { 222 return rsrToClient(rsc, cmdID, (const void *)data, len); 223} 224 225uint32_t rsrToClientBlocking(Context *rsc, int cmdID, void *data, int len) { 226 return rsrToClientBlocking(rsc, cmdID, (const void *)data, len); 227} 228 229void rsrAllocationIoSend(Context *rsc, Allocation *src) { 230 src->ioSend(rsc); 231} 232 233void rsrAllocationIoReceive(Context *rsc, Allocation *src) { 234 src->ioReceive(rsc); 235} 236 237void rsrForEach(Context *rsc, 238 Script *target, 239 Allocation *in, Allocation *out, 240 const void *usr, uint32_t usrBytes, 241 const RsScriptCall *call) { 242 243 if (in == nullptr) { 244 target->runForEach(rsc, /* root slot */ 0, nullptr, 0, out, usr, 245 usrBytes, call); 246 247 } else { 248 const Allocation *ins[1] = {in}; 249 target->runForEach(rsc, /* root slot */ 0, ins, 250 sizeof(ins) / sizeof(RsAllocation), out, usr, 251 usrBytes, call); 252 } 253} 254 255void rsrAllocationSyncAll(Context *rsc, Allocation *a, RsAllocationUsageType usage) { 256 a->syncAll(rsc, usage); 257} 258 259void rsrAllocationCopy1DRange(Context *rsc, Allocation *dstAlloc, 260 uint32_t dstOff, 261 uint32_t dstMip, 262 uint32_t count, 263 Allocation *srcAlloc, 264 uint32_t srcOff, uint32_t srcMip) { 265 rsi_AllocationCopy2DRange(rsc, dstAlloc, dstOff, 0, 266 dstMip, 0, count, 1, 267 srcAlloc, srcOff, 0, srcMip, 0); 268} 269 270void rsrAllocationCopy2DRange(Context *rsc, Allocation *dstAlloc, 271 uint32_t dstXoff, uint32_t dstYoff, 272 uint32_t dstMip, uint32_t dstFace, 273 uint32_t width, uint32_t height, 274 Allocation *srcAlloc, 275 uint32_t srcXoff, uint32_t srcYoff, 276 uint32_t srcMip, uint32_t srcFace) { 277 rsi_AllocationCopy2DRange(rsc, dstAlloc, dstXoff, dstYoff, 278 dstMip, dstFace, width, height, 279 srcAlloc, srcXoff, srcYoff, srcMip, srcFace); 280} 281 282 283} 284} 285