blob: e8741ac0ef230de035033007c8a638f2346fc694 [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
Romain Guy98e10fd2009-07-30 18:45:01 -070024#include <time.h>
I-Jui (Ray) Sung0ab0f902016-10-19 15:53:20 -070025#include <sstream>
Romain Guy98e10fd2009-07-30 18:45:01 -070026
Jason Sams87fe59a2011-04-20 15:09:01 -070027
28namespace android {
29namespace renderscript {
Jason Samse45ac6e2009-07-20 14:31:06 -070030
Jason Samsbe36bf32010-05-11 14:03:58 -070031
Jason Samse45ac6e2009-07-20 14:31:06 -070032//////////////////////////////////////////////////////////////////////////////
33// Math routines
34//////////////////////////////////////////////////////////////////////////////
35
Stephen Hines196c1112011-03-01 17:34:59 -080036#if 0
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -080037static float SC_sinf_fast(float x) {
Romain Guy2275d632009-08-18 11:39:17 -070038 const float A = 1.0f / (2.0f * M_PI);
39 const float B = -16.0f;
40 const float C = 8.0f;
Jason Samsa57c0a72009-09-04 14:42:41 -070041
Romain Guy2275d632009-08-18 11:39:17 -070042 // scale angle for easy argument reduction
43 x *= A;
Jason Samsa57c0a72009-09-04 14:42:41 -070044
Romain Guy2275d632009-08-18 11:39:17 -070045 if (fabsf(x) >= 0.5f) {
46 // argument reduction
47 x = x - ceilf(x + 0.5f) + 1.0f;
48 }
Jason Samsa57c0a72009-09-04 14:42:41 -070049
Romain Guy2275d632009-08-18 11:39:17 -070050 const float y = B * x * fabsf(x) + C * x;
51 return 0.2215f * (y * fabsf(y) - y) + y;
52}
53
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -080054static float SC_cosf_fast(float x) {
Romain Guy2275d632009-08-18 11:39:17 -070055 x += float(M_PI / 2);
56
57 const float A = 1.0f / (2.0f * M_PI);
58 const float B = -16.0f;
59 const float C = 8.0f;
Jason Samsa57c0a72009-09-04 14:42:41 -070060
Romain Guy2275d632009-08-18 11:39:17 -070061 // scale angle for easy argument reduction
62 x *= A;
Jason Samsa57c0a72009-09-04 14:42:41 -070063
Romain Guy2275d632009-08-18 11:39:17 -070064 if (fabsf(x) >= 0.5f) {
65 // argument reduction
66 x = x - ceilf(x + 0.5f) + 1.0f;
67 }
Jason Samsa57c0a72009-09-04 14:42:41 -070068
Romain Guy2275d632009-08-18 11:39:17 -070069 const float y = B * x * fabsf(x) + C * x;
70 return 0.2215f * (y * fabsf(y) - y) + y;
71}
Stephen Hines196c1112011-03-01 17:34:59 -080072#endif
Romain Guy2275d632009-08-18 11:39:17 -070073
Romain Guy98e10fd2009-07-30 18:45:01 -070074//////////////////////////////////////////////////////////////////////////////
75// Time routines
76//////////////////////////////////////////////////////////////////////////////
Jason Samse45ac6e2009-07-20 14:31:06 -070077
Jason Sams709a0972012-11-15 18:18:04 -080078time_t rsrTime(Context *rsc, time_t *timer) {
Stephen Hinesca3f09c2011-01-07 15:11:30 -080079 return time(timer);
Romain Guy98e10fd2009-07-30 18:45:01 -070080}
81
Jason Sams709a0972012-11-15 18:18:04 -080082tm* rsrLocalTime(Context *rsc, tm *local, time_t *timer) {
Stephen Hinesca3f09c2011-01-07 15:11:30 -080083 if (!local) {
Chris Wailes44bef6f2014-08-12 13:51:10 -070084 return nullptr;
Stephen Hinesca3f09c2011-01-07 15:11:30 -080085 }
Jason Samse5ffb872009-08-09 17:01:55 -070086
Stephen Hinesca3f09c2011-01-07 15:11:30 -080087 // The native localtime function is not thread-safe, so we
88 // have to apply locking for proper behavior in RenderScript.
89 pthread_mutex_lock(&rsc->gLibMutex);
90 tm *tmp = localtime(timer);
Jason Sams110f1812013-03-14 16:02:18 -070091 memcpy(local, tmp, sizeof(int)*9);
Stephen Hinesca3f09c2011-01-07 15:11:30 -080092 pthread_mutex_unlock(&rsc->gLibMutex);
93 return local;
Romain Guy39dbc802009-07-31 11:20:59 -070094}
95
Jason Sams709a0972012-11-15 18:18:04 -080096int64_t rsrUptimeMillis(Context *rsc) {
Jason Sams22fa3712010-05-19 17:22:57 -070097 return nanoseconds_to_milliseconds(systemTime(SYSTEM_TIME_MONOTONIC));
98}
99
Jason Sams709a0972012-11-15 18:18:04 -0800100int64_t rsrUptimeNanos(Context *rsc) {
Jason Sams73495472010-07-29 17:31:14 -0700101 return systemTime(SYSTEM_TIME_MONOTONIC);
Jason Sams22fa3712010-05-19 17:22:57 -0700102}
103
Jason Sams709a0972012-11-15 18:18:04 -0800104float rsrGetDt(Context *rsc, const Script *sc) {
Jason Samsef5867a2010-07-28 11:17:53 -0700105 int64_t l = sc->mEnviroment.mLastDtTime;
106 sc->mEnviroment.mLastDtTime = systemTime(SYSTEM_TIME_MONOTONIC);
107 return ((float)(sc->mEnviroment.mLastDtTime - l)) / 1.0e9;
Jason Samse45ac6e2009-07-20 14:31:06 -0700108}
109
Jason Samse45ac6e2009-07-20 14:31:06 -0700110//////////////////////////////////////////////////////////////////////////////
111//
112//////////////////////////////////////////////////////////////////////////////
113
Jason Samsa36c50a2014-06-17 12:06:06 -0700114static void SetObjectRef(const Context *rsc, const ObjectBase *dst, const ObjectBase *src) {
115 //ALOGE("setObjectRef %p,%p %p", rsc, dst, src);
Jason Samsbad80742011-03-16 16:29:28 -0700116 if (src) {
117 CHECK_OBJ(src);
118 src->incSysRef();
Jason Samsf24d7d02010-09-17 13:17:17 -0700119 }
Jason Samsa36c50a2014-06-17 12:06:06 -0700120 if (dst) {
121 CHECK_OBJ(dst);
122 dst->decSysRef();
Jason Samsf24d7d02010-09-17 13:17:17 -0700123 }
Jason Samsc0936852010-08-16 12:41:48 -0700124}
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800125
Jason Sams05ef73f2014-08-05 14:59:22 -0700126// Legacy, remove when drivers are updated
Jason Samsa36c50a2014-06-17 12:06:06 -0700127void rsrClearObject(const Context *rsc, void *dst) {
128 ObjectBase **odst = (ObjectBase **)dst;
Jason Sams0c772022015-04-14 17:27:39 -0700129 if (ObjectBase::gDebugReferences) {
130 ALOGE("rsrClearObject %p,%p", odst, *odst);
131 }
Jason Samsa36c50a2014-06-17 12:06:06 -0700132 if (odst[0]) {
133 CHECK_OBJ(odst[0]);
134 odst[0]->decSysRef();
135 }
Chris Wailes44bef6f2014-08-12 13:51:10 -0700136 *odst = nullptr;
Jason Samsc0936852010-08-16 12:41:48 -0700137}
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800138
Yang Ni5e480022016-04-06 09:34:34 -0700139void rsrClearObject(rs_object_base *dst) {
Jason Sams0c772022015-04-14 17:27:39 -0700140 if (ObjectBase::gDebugReferences) {
141 ALOGE("rsrClearObject %p,%p", dst, dst->p);
142 }
Jason Sams05ef73f2014-08-05 14:59:22 -0700143 if (dst->p) {
144 CHECK_OBJ(dst->p);
145 dst->p->decSysRef();
146 }
Chris Wailes44bef6f2014-08-12 13:51:10 -0700147 dst->p = nullptr;
Jason Sams05ef73f2014-08-05 14:59:22 -0700148}
149
150// Legacy, remove when drivers are updated
Yang Ni3a174692016-04-11 15:29:20 -0700151void rsrClearObject(const Context *rsc, rs_object_base *dst) {
152 rsrClearObject(dst);
153}
154
155// Legacy, remove when drivers are updated
Jason Sams0c772022015-04-14 17:27:39 -0700156void rsrSetObject(const Context *rsc, void *dst, ObjectBase *src) {
157 if (src == nullptr) {
158 rsrClearObject(rsc, dst);
159 return;
160 }
161
162 ObjectBase **odst = (ObjectBase **)dst;
163 if (ObjectBase::gDebugReferences) {
164 ALOGE("rsrSetObject (base) %p,%p %p", dst, *odst, src);
165 }
166 SetObjectRef(rsc, odst[0], src);
167 src->callUpdateCacheObject(rsc, dst);
168}
169
170void rsrSetObject(const Context *rsc, rs_object_base *dst, const ObjectBase *src) {
171 if (src == nullptr) {
172 rsrClearObject(rsc, dst);
173 return;
174 }
175
176 ObjectBase **odst = (ObjectBase **)dst;
177 if (ObjectBase::gDebugReferences) {
178 ALOGE("rsrSetObject (base) %p,%p %p", dst, *odst, src);
179 }
180 SetObjectRef(rsc, odst[0], src);
181 src->callUpdateCacheObject(rsc, dst);
182}
183
184// Legacy, remove when drivers are updated
Tim Murray6a45ddb2014-08-06 11:49:02 -0700185bool rsrIsObject(const Context *, ObjectBase* src) {
Jason Sams05ef73f2014-08-05 14:59:22 -0700186 ObjectBase **osrc = (ObjectBase **)src;
Chris Wailes44bef6f2014-08-12 13:51:10 -0700187 return osrc != nullptr;
Jason Sams05ef73f2014-08-05 14:59:22 -0700188}
189
190bool rsrIsObject(const Context *rsc, rs_object_base o) {
Chris Wailes44bef6f2014-08-12 13:51:10 -0700191 return o.p != nullptr;
Jason Samsc0936852010-08-16 12:41:48 -0700192}
193
Jason Sams7dce6bc2010-08-06 16:22:50 -0700194
Tim Murray6a45ddb2014-08-06 11:49:02 -0700195
Stephen Hines276000a2013-12-03 09:33:32 -0800196uint32_t rsrToClient(Context *rsc, int cmdID, const void *data, int len) {
Steve Blockaf12ac62012-01-06 19:20:56 +0000197 //ALOGE("SC_toClient %i %i %i", cmdID, len);
Jason Samsaad4bc52010-11-08 17:06:46 -0800198 return rsc->sendMessageToClient(data, RS_MESSAGE_TO_CLIENT_USER, cmdID, len, false);
Jason Sams8c401ef2009-10-06 13:58:47 -0700199}
200
Stephen Hines276000a2013-12-03 09:33:32 -0800201uint32_t rsrToClientBlocking(Context *rsc, int cmdID, const void *data, int len) {
Steve Blockaf12ac62012-01-06 19:20:56 +0000202 //ALOGE("SC_toClientBlocking %i %i", cmdID, len);
Jason Samsaad4bc52010-11-08 17:06:46 -0800203 return rsc->sendMessageToClient(data, RS_MESSAGE_TO_CLIENT_USER, cmdID, len, true);
Jason Samsef5867a2010-07-28 11:17:53 -0700204}
205
Stephen Hines70537f52013-12-03 16:44:44 -0800206// Keep these two routines (using non-const void pointers) so that we can
207// still use existing GPU drivers.
208uint32_t rsrToClient(Context *rsc, int cmdID, void *data, int len) {
209 return rsrToClient(rsc, cmdID, (const void *)data, len);
210}
211
212uint32_t rsrToClientBlocking(Context *rsc, int cmdID, void *data, int len) {
213 return rsrToClientBlocking(rsc, cmdID, (const void *)data, len);
214}
215
Jason Samsddceab92013-08-07 13:02:32 -0700216void rsrAllocationIoSend(Context *rsc, Allocation *src) {
217 src->ioSend(rsc);
218}
219
220void rsrAllocationIoReceive(Context *rsc, Allocation *src) {
221 src->ioReceive(rsc);
222}
Jason Sams3a27c952009-10-07 18:14:01 -0700223
Jason Sams709a0972012-11-15 18:18:04 -0800224void rsrForEach(Context *rsc,
Jason Sams87fe59a2011-04-20 15:09:01 -0700225 Script *target,
Yang Ni12398d82015-09-18 14:57:07 -0700226 uint32_t slot,
Yang Nidda5cb52015-10-27 15:23:17 -0700227 uint32_t numInputs,
228 Allocation **in, Allocation *out,
Jason Sams87fe59a2011-04-20 15:09:01 -0700229 const void *usr, uint32_t usrBytes,
Pirama Arumuga Nainar9479e5b2015-04-28 14:40:45 -0700230 const RsScriptCall *call) {
Yang Nidda5cb52015-10-27 15:23:17 -0700231 target->runForEach(rsc, slot, (const Allocation**)in, numInputs, out, usr, usrBytes, call);
Jason Samsc61346b2010-05-28 18:23:22 -0700232}
233
Jason Sams709a0972012-11-15 18:18:04 -0800234void rsrAllocationSyncAll(Context *rsc, Allocation *a, RsAllocationUsageType usage) {
Jason Sams87fe59a2011-04-20 15:09:01 -0700235 a->syncAll(rsc, usage);
Jason Sams693080e2011-01-25 21:33:44 -0800236}
237
I-Jui (Ray) Sung0ab0f902016-10-19 15:53:20 -0700238// Helper for validateCopyArgs() - initialize the error message; only called on
239// infrequently executed paths
240static void initializeErrorMsg(std::stringstream &ss, int expectDim, bool isSrc) {
241 ss << (expectDim == 1 ? "rsAllocationCopy1DRange" : "rsAllocationCopy2DRange") << ": ";
242 ss << (isSrc? "source" : "destination") << " ";
243}
244
245// We are doing the check even in a non-debug context, which is permissible because in that case
246// a failed bound check results in unspecified behavior.
247static bool validateCopyArgs(Context *rsc, bool isSrc, uint32_t expectDim,
248 const Allocation *alloc, uint32_t xoff, uint32_t yoff,
249 uint32_t lod, uint32_t w, uint32_t h) {
250 std::stringstream ss;
251
252 if (lod >= alloc->mHal.drvState.lodCount) {
253 initializeErrorMsg(ss, expectDim, isSrc);
254 ss << "Mip level out of range: ";
255 ss << lod << " >= " << alloc->mHal.drvState.lodCount;
256 rsc->setError(RS_ERROR_FATAL_DEBUG, ss.str().c_str());
257 return false;
258 }
259
260 const uint32_t allocDimX = alloc->mHal.drvState.lod[lod].dimX;
261
262 // Check both in case xoff + w overflows
263 if (xoff >= allocDimX || (xoff + w) > allocDimX) {
264 initializeErrorMsg(ss, expectDim, isSrc);
265 ss << "X range: ";
266 ss << "[" << xoff << ", " << xoff + w << ") outside ";
267 ss << "[0, " << allocDimX << ")";
268 rsc->setError(RS_ERROR_FATAL_DEBUG, ss.str().c_str());
269 return false;
270 }
271
272 const uint32_t allocDimY = alloc->mHal.drvState.lod[lod].dimY;
273
274 if (expectDim > 1) {
275 if (allocDimY == 0) { // Copy2D was given an allocation of 1D
276 initializeErrorMsg(ss, expectDim, isSrc);
277 ss << "dimensionality invalid: expected 2D; given 1D rs_allocation";
278 rsc->setError(RS_ERROR_FATAL_DEBUG, ss.str().c_str());
279 return false;
280 }
281 // Check both in case yoff + h overflows
282 if (yoff >= allocDimY || (yoff + h) > allocDimY) {
283 initializeErrorMsg(ss, expectDim, isSrc);
284 ss << "Y range: ";
285 ss << "[" << yoff << ", " << yoff + h << ") outside ";
286 ss << "[0, " << allocDimY << ")";
287 rsc->setError(RS_ERROR_FATAL_DEBUG, ss.str().c_str());
288 return false;
289 }
290 } else {
291 if (allocDimY != 0) { // Copy1D was given an allocation of 2D
292 initializeErrorMsg(ss, expectDim, isSrc);
293 ss << "dimensionality invalid: expected 1D; given 2D rs_allocation";
294 rsc->setError(RS_ERROR_FATAL_DEBUG, ss.str().c_str());
295 return false;
296 }
297 }
298
299 return true;
300}
301
Alex Sakhartchouk74a82792011-06-14 11:13:19 -0700302void rsrAllocationCopy1DRange(Context *rsc, Allocation *dstAlloc,
303 uint32_t dstOff,
304 uint32_t dstMip,
305 uint32_t count,
306 Allocation *srcAlloc,
307 uint32_t srcOff, uint32_t srcMip) {
I-Jui (Ray) Sung0ab0f902016-10-19 15:53:20 -0700308 if (!validateCopyArgs(rsc, false, 1, dstAlloc, dstOff, 0, dstMip, count, 1) ||
309 !validateCopyArgs(rsc, true, 1, srcAlloc, srcOff, 0, srcMip, count, 1)) {
310 return;
311 }
Alex Sakhartchouk74a82792011-06-14 11:13:19 -0700312 rsi_AllocationCopy2DRange(rsc, dstAlloc, dstOff, 0,
313 dstMip, 0, count, 1,
314 srcAlloc, srcOff, 0, srcMip, 0);
315}
316
317void rsrAllocationCopy2DRange(Context *rsc, Allocation *dstAlloc,
318 uint32_t dstXoff, uint32_t dstYoff,
319 uint32_t dstMip, uint32_t dstFace,
320 uint32_t width, uint32_t height,
321 Allocation *srcAlloc,
322 uint32_t srcXoff, uint32_t srcYoff,
323 uint32_t srcMip, uint32_t srcFace) {
I-Jui (Ray) Sung0ab0f902016-10-19 15:53:20 -0700324 if (!validateCopyArgs(rsc, false, 2, dstAlloc, dstXoff, dstYoff, dstMip, width, height) ||
325 !validateCopyArgs(rsc, true, 2, srcAlloc, srcXoff, srcYoff, srcMip, width, height)) {
326 return;
327 }
328
Alex Sakhartchouk74a82792011-06-14 11:13:19 -0700329 rsi_AllocationCopy2DRange(rsc, dstAlloc, dstXoff, dstYoff,
330 dstMip, dstFace, width, height,
331 srcAlloc, srcXoff, srcYoff, srcMip, srcFace);
332}
333
Pirama Arumuga Nainar6b387c12015-10-12 14:06:57 -0700334RsElement rsrElementCreate(Context *rsc, RsDataType dt, RsDataKind dk,
335 bool norm, uint32_t vecSize) {
Yang Nicc1b09f2016-03-08 20:59:21 +0000336 return rsi_ElementCreate(rsc, dt, dk, norm, vecSize);
Pirama Arumuga Nainar6b387c12015-10-12 14:06:57 -0700337}
338
339RsType rsrTypeCreate(Context *rsc, const RsElement element, uint32_t dimX,
340 uint32_t dimY, uint32_t dimZ, bool mipmaps, bool faces,
341 uint32_t yuv) {
Yang Nicc1b09f2016-03-08 20:59:21 +0000342 return rsi_TypeCreate(rsc, element, dimX, dimY, dimZ, mipmaps, faces, yuv);
Pirama Arumuga Nainar6b387c12015-10-12 14:06:57 -0700343}
344
345RsAllocation rsrAllocationCreateTyped(Context *rsc, const RsType type,
346 RsAllocationMipmapControl mipmaps,
347 uint32_t usages, uintptr_t ptr) {
Yang Nicc1b09f2016-03-08 20:59:21 +0000348 return rsi_AllocationCreateTyped(rsc, type, mipmaps, usages, ptr);
Pirama Arumuga Nainar6b387c12015-10-12 14:06:57 -0700349}
Jason Sams693080e2011-01-25 21:33:44 -0800350
Rahul Chaudhry7974fc02017-02-09 12:33:28 -0800351} // namespace renderscript
352} // namespace android