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 NULL;
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#ifndef RS_COMPATIBILITY_LIB
98    memcpy(local, tmp, sizeof(*tmp));
99#else
100    // WORKAROUND to struct rs_tm != struct tm
101    memcpy(local, tmp, sizeof(int)*9);
102#endif
103    pthread_mutex_unlock(&rsc->gLibMutex);
104    return local;
105}
106
107int64_t rsrUptimeMillis(Context *rsc) {
108#ifndef RS_SERVER
109    return nanoseconds_to_milliseconds(systemTime(SYSTEM_TIME_MONOTONIC));
110#else
111    return 0;
112#endif
113}
114
115int64_t rsrUptimeNanos(Context *rsc) {
116#ifndef RS_SERVER
117    return systemTime(SYSTEM_TIME_MONOTONIC);
118#else
119    return 0;
120#endif
121}
122
123float rsrGetDt(Context *rsc, const Script *sc) {
124#ifndef RS_SERVER
125    int64_t l = sc->mEnviroment.mLastDtTime;
126    sc->mEnviroment.mLastDtTime = systemTime(SYSTEM_TIME_MONOTONIC);
127    return ((float)(sc->mEnviroment.mLastDtTime - l)) / 1.0e9;
128#else
129    return 0.f;
130#endif
131}
132
133//////////////////////////////////////////////////////////////////////////////
134//
135//////////////////////////////////////////////////////////////////////////////
136
137void rsrSetObject(const Context *rsc, ObjectBase **dst, ObjectBase * src) {
138    //ALOGE("rsiSetObject  %p,%p  %p", vdst, *vdst, vsrc);
139    if (src) {
140        CHECK_OBJ(src);
141        src->incSysRef();
142    }
143    if (dst[0]) {
144        CHECK_OBJ(dst[0]);
145        dst[0]->decSysRef();
146    }
147    *dst = src;
148}
149
150void rsrClearObject(const Context *rsc, ObjectBase **dst) {
151    //ALOGE("rsiClearObject  %p,%p", vdst, *vdst);
152    if (dst[0]) {
153        CHECK_OBJ(dst[0]);
154        dst[0]->decSysRef();
155    }
156    *dst = NULL;
157}
158
159bool rsrIsObject(const Context *rsc, const ObjectBase *src) {
160    return src != NULL;
161}
162
163
164uint32_t rsrToClient(Context *rsc, int cmdID, void *data, int len) {
165    //ALOGE("SC_toClient %i %i %i", cmdID, len);
166    return rsc->sendMessageToClient(data, RS_MESSAGE_TO_CLIENT_USER, cmdID, len, false);
167}
168
169uint32_t rsrToClientBlocking(Context *rsc, int cmdID, void *data, int len) {
170    //ALOGE("SC_toClientBlocking %i %i", cmdID, len);
171    return rsc->sendMessageToClient(data, RS_MESSAGE_TO_CLIENT_USER, cmdID, len, true);
172}
173
174void rsrAllocationIoSend(Context *rsc, Allocation *src) {
175    src->ioSend(rsc);
176}
177
178void rsrAllocationIoReceive(Context *rsc, Allocation *src) {
179    src->ioReceive(rsc);
180}
181
182void rsrForEach(Context *rsc,
183                Script *target,
184                Allocation *in, Allocation *out,
185                const void *usr, uint32_t usrBytes,
186                const RsScriptCall *call) {
187    target->runForEach(rsc, /* root slot */ 0, in, out, usr, usrBytes, call);
188}
189
190void rsrAllocationSyncAll(Context *rsc, Allocation *a, RsAllocationUsageType usage) {
191    a->syncAll(rsc, usage);
192}
193
194void rsrAllocationCopy1DRange(Context *rsc, Allocation *dstAlloc,
195                              uint32_t dstOff,
196                              uint32_t dstMip,
197                              uint32_t count,
198                              Allocation *srcAlloc,
199                              uint32_t srcOff, uint32_t srcMip) {
200    rsi_AllocationCopy2DRange(rsc, dstAlloc, dstOff, 0,
201                              dstMip, 0, count, 1,
202                              srcAlloc, srcOff, 0, srcMip, 0);
203}
204
205void rsrAllocationCopy2DRange(Context *rsc, Allocation *dstAlloc,
206                              uint32_t dstXoff, uint32_t dstYoff,
207                              uint32_t dstMip, uint32_t dstFace,
208                              uint32_t width, uint32_t height,
209                              Allocation *srcAlloc,
210                              uint32_t srcXoff, uint32_t srcYoff,
211                              uint32_t srcMip, uint32_t srcFace) {
212    rsi_AllocationCopy2DRange(rsc, dstAlloc, dstXoff, dstYoff,
213                              dstMip, dstFace, width, height,
214                              srcAlloc, srcXoff, srcYoff, srcMip, srcFace);
215}
216
217
218}
219}
220
221