blob: 628190d3e3646d92d8e4bc3c05686463c096826b [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
Stephen Hinesb0934b62013-07-03 17:27:38 -070024#if !defined(RS_SERVER) && !defined(RS_COMPATIBILITY_LIB)
Joe Onorato9c4e4ca2009-08-09 11:39:02 -070025#include "utils/Timers.h"
Tim Murray0b575de2013-03-15 15:56:43 -070026#endif
Jason Samse45ac6e2009-07-20 14:31:06 -070027
Romain Guy98e10fd2009-07-30 18:45:01 -070028#include <time.h>
Romain Guy98e10fd2009-07-30 18:45:01 -070029
Jason Samse45ac6e2009-07-20 14:31:06 -070030using namespace android;
31using namespace android::renderscript;
32
Jason Sams87fe59a2011-04-20 15:09:01 -070033
34namespace android {
35namespace renderscript {
Jason Samse45ac6e2009-07-20 14:31:06 -070036
Jason Samsbe36bf32010-05-11 14:03:58 -070037
Jason Samse45ac6e2009-07-20 14:31:06 -070038//////////////////////////////////////////////////////////////////////////////
39// Math routines
40//////////////////////////////////////////////////////////////////////////////
41
Stephen Hines196c1112011-03-01 17:34:59 -080042#if 0
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -080043static float SC_sinf_fast(float x) {
Romain Guy2275d632009-08-18 11:39:17 -070044 const float A = 1.0f / (2.0f * M_PI);
45 const float B = -16.0f;
46 const float C = 8.0f;
Jason Samsa57c0a72009-09-04 14:42:41 -070047
Romain Guy2275d632009-08-18 11:39:17 -070048 // scale angle for easy argument reduction
49 x *= A;
Jason Samsa57c0a72009-09-04 14:42:41 -070050
Romain Guy2275d632009-08-18 11:39:17 -070051 if (fabsf(x) >= 0.5f) {
52 // argument reduction
53 x = x - ceilf(x + 0.5f) + 1.0f;
54 }
Jason Samsa57c0a72009-09-04 14:42:41 -070055
Romain Guy2275d632009-08-18 11:39:17 -070056 const float y = B * x * fabsf(x) + C * x;
57 return 0.2215f * (y * fabsf(y) - y) + y;
58}
59
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -080060static float SC_cosf_fast(float x) {
Romain Guy2275d632009-08-18 11:39:17 -070061 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 Samsa57c0a72009-09-04 14:42:41 -070066
Romain Guy2275d632009-08-18 11:39:17 -070067 // scale angle for easy argument reduction
68 x *= A;
Jason Samsa57c0a72009-09-04 14:42:41 -070069
Romain Guy2275d632009-08-18 11:39:17 -070070 if (fabsf(x) >= 0.5f) {
71 // argument reduction
72 x = x - ceilf(x + 0.5f) + 1.0f;
73 }
Jason Samsa57c0a72009-09-04 14:42:41 -070074
Romain Guy2275d632009-08-18 11:39:17 -070075 const float y = B * x * fabsf(x) + C * x;
76 return 0.2215f * (y * fabsf(y) - y) + y;
77}
Stephen Hines196c1112011-03-01 17:34:59 -080078#endif
Romain Guy2275d632009-08-18 11:39:17 -070079
Romain Guy98e10fd2009-07-30 18:45:01 -070080//////////////////////////////////////////////////////////////////////////////
81// Time routines
82//////////////////////////////////////////////////////////////////////////////
Jason Samse45ac6e2009-07-20 14:31:06 -070083
Jason Sams709a0972012-11-15 18:18:04 -080084time_t rsrTime(Context *rsc, time_t *timer) {
Stephen Hinesca3f09c2011-01-07 15:11:30 -080085 return time(timer);
Romain Guy98e10fd2009-07-30 18:45:01 -070086}
87
Jason Sams709a0972012-11-15 18:18:04 -080088tm* rsrLocalTime(Context *rsc, tm *local, time_t *timer) {
Stephen Hinesca3f09c2011-01-07 15:11:30 -080089 if (!local) {
Chris Wailes44bef6f2014-08-12 13:51:10 -070090 return nullptr;
Stephen Hinesca3f09c2011-01-07 15:11:30 -080091 }
Jason Samse5ffb872009-08-09 17:01:55 -070092
Stephen Hinesca3f09c2011-01-07 15:11:30 -080093 // 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 Sams110f1812013-03-14 16:02:18 -070097 memcpy(local, tmp, sizeof(int)*9);
Stephen Hinesca3f09c2011-01-07 15:11:30 -080098 pthread_mutex_unlock(&rsc->gLibMutex);
99 return local;
Romain Guy39dbc802009-07-31 11:20:59 -0700100}
101
Jason Sams709a0972012-11-15 18:18:04 -0800102int64_t rsrUptimeMillis(Context *rsc) {
Tim Murray0b575de2013-03-15 15:56:43 -0700103#ifndef RS_SERVER
Jason Sams22fa3712010-05-19 17:22:57 -0700104 return nanoseconds_to_milliseconds(systemTime(SYSTEM_TIME_MONOTONIC));
Tim Murray0b575de2013-03-15 15:56:43 -0700105#else
106 return 0;
107#endif
Jason Sams22fa3712010-05-19 17:22:57 -0700108}
109
Jason Sams709a0972012-11-15 18:18:04 -0800110int64_t rsrUptimeNanos(Context *rsc) {
Tim Murray0b575de2013-03-15 15:56:43 -0700111#ifndef RS_SERVER
Jason Sams73495472010-07-29 17:31:14 -0700112 return systemTime(SYSTEM_TIME_MONOTONIC);
Tim Murray0b575de2013-03-15 15:56:43 -0700113#else
114 return 0;
115#endif
Jason Sams22fa3712010-05-19 17:22:57 -0700116}
117
Jason Sams709a0972012-11-15 18:18:04 -0800118float rsrGetDt(Context *rsc, const Script *sc) {
Tim Murray0b575de2013-03-15 15:56:43 -0700119#ifndef RS_SERVER
Jason Samsef5867a2010-07-28 11:17:53 -0700120 int64_t l = sc->mEnviroment.mLastDtTime;
121 sc->mEnviroment.mLastDtTime = systemTime(SYSTEM_TIME_MONOTONIC);
122 return ((float)(sc->mEnviroment.mLastDtTime - l)) / 1.0e9;
Tim Murray0b575de2013-03-15 15:56:43 -0700123#else
124 return 0.f;
125#endif
Jason Samse45ac6e2009-07-20 14:31:06 -0700126}
127
Jason Samse45ac6e2009-07-20 14:31:06 -0700128//////////////////////////////////////////////////////////////////////////////
129//
130//////////////////////////////////////////////////////////////////////////////
131
Jason Samsa36c50a2014-06-17 12:06:06 -0700132static void SetObjectRef(const Context *rsc, const ObjectBase *dst, const ObjectBase *src) {
133 //ALOGE("setObjectRef %p,%p %p", rsc, dst, src);
Jason Samsbad80742011-03-16 16:29:28 -0700134 if (src) {
135 CHECK_OBJ(src);
136 src->incSysRef();
Jason Samsf24d7d02010-09-17 13:17:17 -0700137 }
Jason Samsa36c50a2014-06-17 12:06:06 -0700138 if (dst) {
139 CHECK_OBJ(dst);
140 dst->decSysRef();
Jason Samsf24d7d02010-09-17 13:17:17 -0700141 }
Jason Samsc0936852010-08-16 12:41:48 -0700142}
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800143
Jason Sams05ef73f2014-08-05 14:59:22 -0700144// Legacy, remove when drivers are updated
Jason Samsa36c50a2014-06-17 12:06:06 -0700145void rsrSetObject(const Context *rsc, void *dst, ObjectBase *src) {
146 ObjectBase **odst = (ObjectBase **)dst;
147 //ALOGE("rsrSetObject (base) %p,%p %p", dst, *odst, src);
148 SetObjectRef(rsc, odst[0], src);
Chris Wailes44bef6f2014-08-12 13:51:10 -0700149 if (src != nullptr) {
Jason Samsa36c50a2014-06-17 12:06:06 -0700150 src->callUpdateCacheObject(rsc, dst);
Jason Samsf24d7d02010-09-17 13:17:17 -0700151 }
Jason Samsa36c50a2014-06-17 12:06:06 -0700152}
153
Jason Sams05ef73f2014-08-05 14:59:22 -0700154void rsrSetObject(const Context *rsc, rs_object_base *dst, const ObjectBase *src) {
155 ObjectBase **odst = (ObjectBase **)dst;
156 //ALOGE("rsrSetObject (base) %p,%p %p", dst, *odst, src);
157 SetObjectRef(rsc, odst[0], src);
Chris Wailes44bef6f2014-08-12 13:51:10 -0700158 if (src != nullptr) {
Jason Sams05ef73f2014-08-05 14:59:22 -0700159 src->callUpdateCacheObject(rsc, dst);
160 }
161}
Jason Samsa36c50a2014-06-17 12:06:06 -0700162
Jason Sams05ef73f2014-08-05 14:59:22 -0700163// Legacy, remove when drivers are updated
Jason Samsa36c50a2014-06-17 12:06:06 -0700164void rsrClearObject(const Context *rsc, void *dst) {
165 ObjectBase **odst = (ObjectBase **)dst;
166 //ALOGE("rsrClearObject %p,%p", odst, *odst);
167 if (odst[0]) {
168 CHECK_OBJ(odst[0]);
169 odst[0]->decSysRef();
170 }
Chris Wailes44bef6f2014-08-12 13:51:10 -0700171 *odst = nullptr;
Jason Samsc0936852010-08-16 12:41:48 -0700172}
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800173
Jason Sams05ef73f2014-08-05 14:59:22 -0700174void rsrClearObject(const Context *rsc, rs_object_base *dst) {
175 //ALOGE("rsrClearObject %p,%p", odst, *odst);
176 if (dst->p) {
177 CHECK_OBJ(dst->p);
178 dst->p->decSysRef();
179 }
Chris Wailes44bef6f2014-08-12 13:51:10 -0700180 dst->p = nullptr;
Jason Sams05ef73f2014-08-05 14:59:22 -0700181}
182
183// Legacy, remove when drivers are updated
Tim Murray6a45ddb2014-08-06 11:49:02 -0700184bool rsrIsObject(const Context *, ObjectBase* src) {
Jason Sams05ef73f2014-08-05 14:59:22 -0700185 ObjectBase **osrc = (ObjectBase **)src;
Chris Wailes44bef6f2014-08-12 13:51:10 -0700186 return osrc != nullptr;
Jason Sams05ef73f2014-08-05 14:59:22 -0700187}
188
189bool rsrIsObject(const Context *rsc, rs_object_base o) {
Chris Wailes44bef6f2014-08-12 13:51:10 -0700190 return o.p != nullptr;
Jason Samsc0936852010-08-16 12:41:48 -0700191}
192
Jason Sams7dce6bc2010-08-06 16:22:50 -0700193
Tim Murray6a45ddb2014-08-06 11:49:02 -0700194
Stephen Hines276000a2013-12-03 09:33:32 -0800195uint32_t rsrToClient(Context *rsc, int cmdID, const void *data, int len) {
Steve Blockaf12ac62012-01-06 19:20:56 +0000196 //ALOGE("SC_toClient %i %i %i", cmdID, len);
Jason Samsaad4bc52010-11-08 17:06:46 -0800197 return rsc->sendMessageToClient(data, RS_MESSAGE_TO_CLIENT_USER, cmdID, len, false);
Jason Sams8c401ef2009-10-06 13:58:47 -0700198}
199
Stephen Hines276000a2013-12-03 09:33:32 -0800200uint32_t rsrToClientBlocking(Context *rsc, int cmdID, const void *data, int len) {
Steve Blockaf12ac62012-01-06 19:20:56 +0000201 //ALOGE("SC_toClientBlocking %i %i", cmdID, len);
Jason Samsaad4bc52010-11-08 17:06:46 -0800202 return rsc->sendMessageToClient(data, RS_MESSAGE_TO_CLIENT_USER, cmdID, len, true);
Jason Samsef5867a2010-07-28 11:17:53 -0700203}
204
Stephen Hines70537f52013-12-03 16:44:44 -0800205// Keep these two routines (using non-const void pointers) so that we can
206// still use existing GPU drivers.
207uint32_t rsrToClient(Context *rsc, int cmdID, void *data, int len) {
208 return rsrToClient(rsc, cmdID, (const void *)data, len);
209}
210
211uint32_t rsrToClientBlocking(Context *rsc, int cmdID, void *data, int len) {
212 return rsrToClientBlocking(rsc, cmdID, (const void *)data, len);
213}
214
Jason Samsddceab92013-08-07 13:02:32 -0700215void rsrAllocationIoSend(Context *rsc, Allocation *src) {
216 src->ioSend(rsc);
217}
218
219void rsrAllocationIoReceive(Context *rsc, Allocation *src) {
220 src->ioReceive(rsc);
221}
Jason Sams3a27c952009-10-07 18:14:01 -0700222
Jason Sams709a0972012-11-15 18:18:04 -0800223void rsrForEach(Context *rsc,
Jason Sams87fe59a2011-04-20 15:09:01 -0700224 Script *target,
225 Allocation *in, Allocation *out,
226 const void *usr, uint32_t usrBytes,
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800227 const RsScriptCall *call) {
Chris Wailesf3712132014-07-16 15:18:30 -0700228
Chris Wailes44bef6f2014-08-12 13:51:10 -0700229 if (in == nullptr) {
230 target->runForEach(rsc, /* root slot */ 0, nullptr, 0, out, usr,
Chris Wailesf3712132014-07-16 15:18:30 -0700231 usrBytes, call);
232
233 } else {
234 const Allocation *ins[1] = {in};
235 target->runForEach(rsc, /* root slot */ 0, ins,
236 sizeof(ins) / sizeof(RsAllocation), out, usr,
237 usrBytes, call);
238 }
Jason Samsc61346b2010-05-28 18:23:22 -0700239}
240
Jason Sams709a0972012-11-15 18:18:04 -0800241void rsrAllocationSyncAll(Context *rsc, Allocation *a, RsAllocationUsageType usage) {
Jason Sams87fe59a2011-04-20 15:09:01 -0700242 a->syncAll(rsc, usage);
Jason Sams693080e2011-01-25 21:33:44 -0800243}
244
Alex Sakhartchouk74a82792011-06-14 11:13:19 -0700245void rsrAllocationCopy1DRange(Context *rsc, Allocation *dstAlloc,
246 uint32_t dstOff,
247 uint32_t dstMip,
248 uint32_t count,
249 Allocation *srcAlloc,
250 uint32_t srcOff, uint32_t srcMip) {
251 rsi_AllocationCopy2DRange(rsc, dstAlloc, dstOff, 0,
252 dstMip, 0, count, 1,
253 srcAlloc, srcOff, 0, srcMip, 0);
254}
255
256void rsrAllocationCopy2DRange(Context *rsc, Allocation *dstAlloc,
257 uint32_t dstXoff, uint32_t dstYoff,
258 uint32_t dstMip, uint32_t dstFace,
259 uint32_t width, uint32_t height,
260 Allocation *srcAlloc,
261 uint32_t srcXoff, uint32_t srcYoff,
262 uint32_t srcMip, uint32_t srcFace) {
263 rsi_AllocationCopy2DRange(rsc, dstAlloc, dstXoff, dstYoff,
264 dstMip, dstFace, width, height,
265 srcAlloc, srcXoff, srcYoff, srcMip, srcFace);
266}
267
Jason Sams693080e2011-01-25 21:33:44 -0800268
Jason Sams693080e2011-01-25 21:33:44 -0800269}
Jason Samse45ac6e2009-07-20 14:31:06 -0700270}