rsScriptC_Lib.cpp revision af12ac6a08651464f8d823add667c706f993b587
1/*
2 * Copyright (C) 2009 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
23#include "utils/Timers.h"
24
25#include <time.h>
26
27using namespace android;
28using namespace android::renderscript;
29
30
31namespace android {
32namespace renderscript {
33
34
35//////////////////////////////////////////////////////////////////////////////
36// Math routines
37//////////////////////////////////////////////////////////////////////////////
38
39#if 0
40static float SC_sinf_fast(float x) {
41    const float A =   1.0f / (2.0f * M_PI);
42    const float B = -16.0f;
43    const float C =   8.0f;
44
45    // scale angle for easy argument reduction
46    x *= A;
47
48    if (fabsf(x) >= 0.5f) {
49        // argument reduction
50        x = x - ceilf(x + 0.5f) + 1.0f;
51    }
52
53    const float y = B * x * fabsf(x) + C * x;
54    return 0.2215f * (y * fabsf(y) - y) + y;
55}
56
57static float SC_cosf_fast(float x) {
58    x += float(M_PI / 2);
59
60    const float A =   1.0f / (2.0f * M_PI);
61    const float B = -16.0f;
62    const float C =   8.0f;
63
64    // scale angle for easy argument reduction
65    x *= A;
66
67    if (fabsf(x) >= 0.5f) {
68        // argument reduction
69        x = x - ceilf(x + 0.5f) + 1.0f;
70    }
71
72    const float y = B * x * fabsf(x) + C * x;
73    return 0.2215f * (y * fabsf(y) - y) + y;
74}
75#endif
76
77//////////////////////////////////////////////////////////////////////////////
78// Time routines
79//////////////////////////////////////////////////////////////////////////////
80
81time_t rsrTime(Context *rsc, Script *sc, time_t *timer) {
82    return time(timer);
83}
84
85tm* rsrLocalTime(Context *rsc, Script *sc, tm *local, time_t *timer) {
86    if (!local) {
87      return NULL;
88    }
89
90    // The native localtime function is not thread-safe, so we
91    // have to apply locking for proper behavior in RenderScript.
92    pthread_mutex_lock(&rsc->gLibMutex);
93    tm *tmp = localtime(timer);
94    memcpy(local, tmp, sizeof(*tmp));
95    pthread_mutex_unlock(&rsc->gLibMutex);
96    return local;
97}
98
99int64_t rsrUptimeMillis(Context *rsc, Script *sc) {
100    return nanoseconds_to_milliseconds(systemTime(SYSTEM_TIME_MONOTONIC));
101}
102
103int64_t rsrUptimeNanos(Context *rsc, Script *sc) {
104    return systemTime(SYSTEM_TIME_MONOTONIC);
105}
106
107float rsrGetDt(Context *rsc, Script *sc) {
108    int64_t l = sc->mEnviroment.mLastDtTime;
109    sc->mEnviroment.mLastDtTime = systemTime(SYSTEM_TIME_MONOTONIC);
110    return ((float)(sc->mEnviroment.mLastDtTime - l)) / 1.0e9;
111}
112
113//////////////////////////////////////////////////////////////////////////////
114//
115//////////////////////////////////////////////////////////////////////////////
116
117void rsrSetObject(const Context *rsc, const Script *sc, ObjectBase **dst, ObjectBase * src) {
118    //ALOGE("rsiSetObject  %p,%p  %p", vdst, *vdst, vsrc);
119    if (src) {
120        CHECK_OBJ(src);
121        src->incSysRef();
122    }
123    if (dst[0]) {
124        CHECK_OBJ(dst[0]);
125        dst[0]->decSysRef();
126    }
127    *dst = src;
128}
129
130void rsrClearObject(const Context *rsc, const Script *sc, ObjectBase **dst) {
131    //ALOGE("rsiClearObject  %p,%p", vdst, *vdst);
132    if (dst[0]) {
133        CHECK_OBJ(dst[0]);
134        dst[0]->decSysRef();
135    }
136    *dst = NULL;
137}
138
139bool rsrIsObject(const Context *rsc, const Script *sc, const ObjectBase *src) {
140    return src != NULL;
141}
142
143
144uint32_t rsrToClient(Context *rsc, Script *sc, int cmdID, void *data, int len) {
145    //ALOGE("SC_toClient %i %i %i", cmdID, len);
146    return rsc->sendMessageToClient(data, RS_MESSAGE_TO_CLIENT_USER, cmdID, len, false);
147}
148
149uint32_t rsrToClientBlocking(Context *rsc, Script *sc, int cmdID, void *data, int len) {
150    //ALOGE("SC_toClientBlocking %i %i", cmdID, len);
151    return rsc->sendMessageToClient(data, RS_MESSAGE_TO_CLIENT_USER, cmdID, len, true);
152}
153
154
155void rsrForEach(Context *rsc, Script *sc,
156                Script *target,
157                Allocation *in, Allocation *out,
158                const void *usr, uint32_t usrBytes,
159                const RsScriptCall *call) {
160    target->runForEach(rsc, in, out, usr, usrBytes, call);
161}
162
163void rsrAllocationSyncAll(Context *rsc, Script *sc, Allocation *a, RsAllocationUsageType usage) {
164    a->syncAll(rsc, usage);
165}
166
167void rsrAllocationCopy1DRange(Context *rsc, Allocation *dstAlloc,
168                              uint32_t dstOff,
169                              uint32_t dstMip,
170                              uint32_t count,
171                              Allocation *srcAlloc,
172                              uint32_t srcOff, uint32_t srcMip) {
173    rsi_AllocationCopy2DRange(rsc, dstAlloc, dstOff, 0,
174                              dstMip, 0, count, 1,
175                              srcAlloc, srcOff, 0, srcMip, 0);
176}
177
178void rsrAllocationCopy2DRange(Context *rsc, Allocation *dstAlloc,
179                              uint32_t dstXoff, uint32_t dstYoff,
180                              uint32_t dstMip, uint32_t dstFace,
181                              uint32_t width, uint32_t height,
182                              Allocation *srcAlloc,
183                              uint32_t srcXoff, uint32_t srcYoff,
184                              uint32_t srcMip, uint32_t srcFace) {
185    rsi_AllocationCopy2DRange(rsc, dstAlloc, dstXoff, dstYoff,
186                              dstMip, dstFace, width, height,
187                              srcAlloc, srcXoff, srcYoff, srcMip, srcFace);
188}
189
190const Allocation * rsrGetAllocation(Context *rsc, Script *s, const void *ptr) {
191    ScriptC *sc = (ScriptC *)s;
192    return sc->ptrToAllocation(ptr);
193}
194
195}
196}
197
198