• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
30 using namespace android;
31 using namespace android::renderscript;
32 
33 
34 namespace android {
35 namespace renderscript {
36 
37 
38 //////////////////////////////////////////////////////////////////////////////
39 // Math routines
40 //////////////////////////////////////////////////////////////////////////////
41 
42 #if 0
43 static 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 
60 static 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 
rsrTime(Context * rsc,time_t * timer)84 time_t rsrTime(Context *rsc, time_t *timer) {
85     return time(timer);
86 }
87 
rsrLocalTime(Context * rsc,tm * local,time_t * timer)88 tm* 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 
rsrUptimeMillis(Context * rsc)107 int64_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 
rsrUptimeNanos(Context * rsc)115 int64_t rsrUptimeNanos(Context *rsc) {
116 #ifndef RS_SERVER
117     return systemTime(SYSTEM_TIME_MONOTONIC);
118 #else
119     return 0;
120 #endif
121 }
122 
rsrGetDt(Context * rsc,const Script * sc)123 float 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 
SetObjectRef(const Context * rsc,const ObjectBase * dst,const ObjectBase * src)137 static void SetObjectRef(const Context *rsc, const ObjectBase *dst, const ObjectBase *src) {
138     //ALOGE("setObjectRef  %p,%p  %p", rsc, dst, src);
139     if (src) {
140         CHECK_OBJ(src);
141         src->incSysRef();
142     }
143     if (dst) {
144         CHECK_OBJ(dst);
145         dst->decSysRef();
146     }
147 }
148 
149 // Legacy, remove when drivers are updated
rsrSetObject(const Context * rsc,void * dst,ObjectBase * src)150 void rsrSetObject(const Context *rsc, void *dst, ObjectBase *src) {
151     ObjectBase **odst = (ObjectBase **)dst;
152     //ALOGE("rsrSetObject (base) %p,%p  %p", dst, *odst, src);
153     SetObjectRef(rsc, odst[0], src);
154     if (src != NULL) {
155         src->callUpdateCacheObject(rsc, dst);
156     }
157 }
158 
rsrSetObject(const Context * rsc,rs_object_base * dst,const ObjectBase * src)159 void rsrSetObject(const Context *rsc, rs_object_base *dst, const ObjectBase *src) {
160     ObjectBase **odst = (ObjectBase **)dst;
161     //ALOGE("rsrSetObject (base) %p,%p  %p", dst, *odst, src);
162     SetObjectRef(rsc, odst[0], src);
163     if (src != NULL) {
164         src->callUpdateCacheObject(rsc, dst);
165     }
166 }
167 
168 // Legacy, remove when drivers are updated
rsrClearObject(const Context * rsc,void * dst)169 void rsrClearObject(const Context *rsc, void *dst) {
170     ObjectBase **odst = (ObjectBase **)dst;
171     //ALOGE("rsrClearObject  %p,%p", odst, *odst);
172     if (odst[0]) {
173         CHECK_OBJ(odst[0]);
174         odst[0]->decSysRef();
175     }
176     *odst = NULL;
177 }
178 
rsrClearObject(const Context * rsc,rs_object_base * dst)179 void rsrClearObject(const Context *rsc, rs_object_base *dst) {
180     //ALOGE("rsrClearObject  %p,%p", odst, *odst);
181     if (dst->p) {
182         CHECK_OBJ(dst->p);
183         dst->p->decSysRef();
184     }
185     dst->p = NULL;
186 }
187 
188 // Legacy, remove when drivers are updated
rsrIsObject(const Context *,ObjectBase * src)189 bool rsrIsObject(const Context *, ObjectBase* src) {
190     ObjectBase **osrc = (ObjectBase **)src;
191     return osrc != NULL;
192 }
193 
rsrIsObject(const Context * rsc,rs_object_base o)194 bool rsrIsObject(const Context *rsc, rs_object_base o) {
195     return o.p != NULL;
196 }
197 
198 
199 
rsrToClient(Context * rsc,int cmdID,const void * data,int len)200 uint32_t rsrToClient(Context *rsc, int cmdID, const void *data, int len) {
201     //ALOGE("SC_toClient %i %i %i", cmdID, len);
202     return rsc->sendMessageToClient(data, RS_MESSAGE_TO_CLIENT_USER, cmdID, len, false);
203 }
204 
rsrToClientBlocking(Context * rsc,int cmdID,const void * data,int len)205 uint32_t rsrToClientBlocking(Context *rsc, int cmdID, const void *data, int len) {
206     //ALOGE("SC_toClientBlocking %i %i", cmdID, len);
207     return rsc->sendMessageToClient(data, RS_MESSAGE_TO_CLIENT_USER, cmdID, len, true);
208 }
209 
210 // Keep these two routines (using non-const void pointers) so that we can
211 // still use existing GPU drivers.
rsrToClient(Context * rsc,int cmdID,void * data,int len)212 uint32_t rsrToClient(Context *rsc, int cmdID, void *data, int len) {
213     return rsrToClient(rsc, cmdID, (const void *)data, len);
214 }
215 
rsrToClientBlocking(Context * rsc,int cmdID,void * data,int len)216 uint32_t rsrToClientBlocking(Context *rsc, int cmdID, void *data, int len) {
217     return rsrToClientBlocking(rsc, cmdID, (const void *)data, len);
218 }
219 
rsrAllocationIoSend(Context * rsc,Allocation * src)220 void rsrAllocationIoSend(Context *rsc, Allocation *src) {
221     src->ioSend(rsc);
222 }
223 
rsrAllocationIoReceive(Context * rsc,Allocation * src)224 void rsrAllocationIoReceive(Context *rsc, Allocation *src) {
225     src->ioReceive(rsc);
226 }
227 
rsrForEach(Context * rsc,Script * target,Allocation * in,Allocation * out,const void * usr,uint32_t usrBytes,const RsScriptCall * call)228 void rsrForEach(Context *rsc,
229                 Script *target,
230                 Allocation *in, Allocation *out,
231                 const void *usr, uint32_t usrBytes,
232                 const RsScriptCall *call) {
233     target->runForEach(rsc, /* root slot */ 0, in, out, usr, usrBytes, call);
234 }
235 
rsrAllocationSyncAll(Context * rsc,Allocation * a,RsAllocationUsageType usage)236 void rsrAllocationSyncAll(Context *rsc, Allocation *a, RsAllocationUsageType usage) {
237     a->syncAll(rsc, usage);
238 }
239 
rsrAllocationCopy1DRange(Context * rsc,Allocation * dstAlloc,uint32_t dstOff,uint32_t dstMip,uint32_t count,Allocation * srcAlloc,uint32_t srcOff,uint32_t srcMip)240 void rsrAllocationCopy1DRange(Context *rsc, Allocation *dstAlloc,
241                               uint32_t dstOff,
242                               uint32_t dstMip,
243                               uint32_t count,
244                               Allocation *srcAlloc,
245                               uint32_t srcOff, uint32_t srcMip) {
246     rsi_AllocationCopy2DRange(rsc, dstAlloc, dstOff, 0,
247                               dstMip, 0, count, 1,
248                               srcAlloc, srcOff, 0, srcMip, 0);
249 }
250 
rsrAllocationCopy2DRange(Context * rsc,Allocation * dstAlloc,uint32_t dstXoff,uint32_t dstYoff,uint32_t dstMip,uint32_t dstFace,uint32_t width,uint32_t height,Allocation * srcAlloc,uint32_t srcXoff,uint32_t srcYoff,uint32_t srcMip,uint32_t srcFace)251 void rsrAllocationCopy2DRange(Context *rsc, Allocation *dstAlloc,
252                               uint32_t dstXoff, uint32_t dstYoff,
253                               uint32_t dstMip, uint32_t dstFace,
254                               uint32_t width, uint32_t height,
255                               Allocation *srcAlloc,
256                               uint32_t srcXoff, uint32_t srcYoff,
257                               uint32_t srcMip, uint32_t srcFace) {
258     rsi_AllocationCopy2DRange(rsc, dstAlloc, dstXoff, dstYoff,
259                               dstMip, dstFace, width, height,
260                               srcAlloc, srcXoff, srcYoff, srcMip, srcFace);
261 }
262 
263 
264 }
265 }
266