blob: 3a75ffe0a5bcb7e7bd4e4029a64bdd7d9aa0d49c [file] [log] [blame]
Jason Sams221a4b12012-02-22 15:22:41 -08001/*
Tim Murraya4230962013-07-17 16:50:10 -07002 * Copyright (C) 2013 The Android Open Source Project
Jason Sams221a4b12012-02-22 15:22:41 -08003 *
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
Jason Sams221a4b12012-02-22 15:22:41 -080017#include <malloc.h>
18#include <string.h>
Tim Murray84bf2b82012-10-31 16:03:16 -070019#include <pthread.h>
Jason Sams221a4b12012-02-22 15:22:41 -080020
21#include "RenderScript.h"
Tim Murray89daad62013-07-29 14:30:02 -070022#include "rsCppStructs.h"
Tim Murrayeeaf7142013-09-09 15:03:50 -070023#include "rsCppInternal.h"
Jason Sams221a4b12012-02-22 15:22:41 -080024
Tim Murraya4230962013-07-17 16:50:10 -070025#include <dlfcn.h>
26
Tim Murray4a92d122013-07-22 10:56:18 -070027#if !defined(RS_SERVER) && defined(HAVE_ANDROID_OS)
28#include <cutils/properties.h>
29#endif
30
Jason Sams69cccdf2012-04-02 19:11:49 -070031using namespace android;
Tim Murray9eb7f4b2012-11-16 14:02:18 -080032using namespace RSC;
Jason Sams69cccdf2012-04-02 19:11:49 -070033
Tim Murray84bf2b82012-10-31 16:03:16 -070034bool RS::gInitialized = false;
Tim Murray4a92d122013-07-22 10:56:18 -070035bool RS::usingNative = false;
Tim Murray84bf2b82012-10-31 16:03:16 -070036pthread_mutex_t RS::gInitMutex = PTHREAD_MUTEX_INITIALIZER;
Tim Murraya4230962013-07-17 16:50:10 -070037dispatchTable* RS::dispatch = NULL;
Tim Murraya4230962013-07-17 16:50:10 -070038static int gInitError = 0;
Jason Sams221a4b12012-02-22 15:22:41 -080039
Tim Murray84bf2b82012-10-31 16:03:16 -070040RS::RS() {
Jason Sams221a4b12012-02-22 15:22:41 -080041 mDev = NULL;
42 mContext = NULL;
43 mErrorFunc = NULL;
44 mMessageFunc = NULL;
45 mMessageRun = false;
Tim Murraya4230962013-07-17 16:50:10 -070046 mInit = false;
Tim Murray21fa7a02013-08-15 16:25:03 -070047 mCurrentError = RS_SUCCESS;
Jason Sams221a4b12012-02-22 15:22:41 -080048
49 memset(&mElements, 0, sizeof(mElements));
Tim Murray729b6fe2013-07-23 16:20:42 -070050 memset(&mSamplers, 0, sizeof(mSamplers));
Jason Sams221a4b12012-02-22 15:22:41 -080051}
52
Tim Murray84bf2b82012-10-31 16:03:16 -070053RS::~RS() {
Tim Murraya4230962013-07-17 16:50:10 -070054 if (mInit == true) {
55 mMessageRun = false;
Jason Sams221a4b12012-02-22 15:22:41 -080056
Tim Murraya4230962013-07-17 16:50:10 -070057 RS::dispatch->ContextDeinitToClient(mContext);
Jason Sams221a4b12012-02-22 15:22:41 -080058
Tim Murraya4230962013-07-17 16:50:10 -070059 void *res = NULL;
60 int status = pthread_join(mMessageThreadId, &res);
Jason Sams221a4b12012-02-22 15:22:41 -080061
Tim Murraya4230962013-07-17 16:50:10 -070062 RS::dispatch->ContextDestroy(mContext);
63 mContext = NULL;
64 RS::dispatch->DeviceDestroy(mDev);
65 mDev = NULL;
66 }
Jason Sams221a4b12012-02-22 15:22:41 -080067}
68
Tim Murraycaf41262013-12-13 12:54:37 -080069bool RS::init(std::string name, uint32_t flags) {
70 return RS::init(name, RS_VERSION, flags);
Tim Murray84bf2b82012-10-31 16:03:16 -070071}
72
Tim Murray0b8a2be2013-07-23 16:25:41 -070073static bool loadSymbols(void* handle) {
74
75 RS::dispatch->AllocationGetType = (AllocationGetTypeFnPtr)dlsym(handle, "rsaAllocationGetType");
76 if (RS::dispatch->AllocationGetType == NULL) {
Tim Murray87c9d772013-12-03 12:42:00 -080077 ALOGV("Couldn't initialize RS::dispatch->AllocationGetType");
Tim Murray0b8a2be2013-07-23 16:25:41 -070078 return false;
79 }
80 RS::dispatch->TypeGetNativeData = (TypeGetNativeDataFnPtr)dlsym(handle, "rsaTypeGetNativeData");
81 if (RS::dispatch->TypeGetNativeData == NULL) {
Tim Murray87c9d772013-12-03 12:42:00 -080082 ALOGV("Couldn't initialize RS::dispatch->TypeGetNativeData");
Tim Murray0b8a2be2013-07-23 16:25:41 -070083 return false;
84 }
85 RS::dispatch->ElementGetNativeData = (ElementGetNativeDataFnPtr)dlsym(handle, "rsaElementGetNativeData");
86 if (RS::dispatch->ElementGetNativeData == NULL) {
Tim Murray87c9d772013-12-03 12:42:00 -080087 ALOGV("Couldn't initialize RS::dispatch->ElementGetNativeData");
Tim Murray0b8a2be2013-07-23 16:25:41 -070088 return false;
89 }
90 RS::dispatch->ElementGetSubElements = (ElementGetSubElementsFnPtr)dlsym(handle, "rsaElementGetSubElements");
91 if (RS::dispatch->ElementGetSubElements == NULL) {
Tim Murray87c9d772013-12-03 12:42:00 -080092 ALOGV("Couldn't initialize RS::dispatch->ElementGetSubElements");
Tim Murray0b8a2be2013-07-23 16:25:41 -070093 return false;
94 }
95 RS::dispatch->DeviceCreate = (DeviceCreateFnPtr)dlsym(handle, "rsDeviceCreate");
96 if (RS::dispatch->DeviceCreate == NULL) {
Tim Murray87c9d772013-12-03 12:42:00 -080097 ALOGV("Couldn't initialize RS::dispatch->DeviceCreate");
Tim Murray0b8a2be2013-07-23 16:25:41 -070098 return false;
99 }
100 RS::dispatch->DeviceDestroy = (DeviceDestroyFnPtr)dlsym(handle, "rsDeviceDestroy");
101 if (RS::dispatch->DeviceDestroy == NULL) {
Tim Murray87c9d772013-12-03 12:42:00 -0800102 ALOGV("Couldn't initialize RS::dispatch->DeviceDestroy");
Tim Murray0b8a2be2013-07-23 16:25:41 -0700103 return false;
104 }
105 RS::dispatch->DeviceSetConfig = (DeviceSetConfigFnPtr)dlsym(handle, "rsDeviceSetConfig");
106 if (RS::dispatch->DeviceSetConfig == NULL) {
Tim Murray87c9d772013-12-03 12:42:00 -0800107 ALOGV("Couldn't initialize RS::dispatch->DeviceSetConfig");
Tim Murray0b8a2be2013-07-23 16:25:41 -0700108 return false;
109 }
110 RS::dispatch->ContextCreate = (ContextCreateFnPtr)dlsym(handle, "rsContextCreate");;
111 if (RS::dispatch->ContextCreate == NULL) {
Tim Murray87c9d772013-12-03 12:42:00 -0800112 ALOGV("Couldn't initialize RS::dispatch->ContextCreate");
Tim Murray0b8a2be2013-07-23 16:25:41 -0700113 return false;
114 }
Tim Murray4a92d122013-07-22 10:56:18 -0700115 RS::dispatch->GetName = (GetNameFnPtr)dlsym(handle, "rsaGetName");;
116 if (RS::dispatch->GetName == NULL) {
Tim Murray87c9d772013-12-03 12:42:00 -0800117 ALOGV("Couldn't initialize RS::dispatch->GetName");
Tim Murray4a92d122013-07-22 10:56:18 -0700118 return false;
119 }
Tim Murray0b8a2be2013-07-23 16:25:41 -0700120 RS::dispatch->ContextDestroy = (ContextDestroyFnPtr)dlsym(handle, "rsContextDestroy");
121 if (RS::dispatch->ContextDestroy == NULL) {
Tim Murray87c9d772013-12-03 12:42:00 -0800122 ALOGV("Couldn't initialize RS::dispatch->ContextDestroy");
Tim Murray0b8a2be2013-07-23 16:25:41 -0700123 return false;
124 }
125 RS::dispatch->ContextGetMessage = (ContextGetMessageFnPtr)dlsym(handle, "rsContextGetMessage");
126 if (RS::dispatch->ContextGetMessage == NULL) {
Tim Murray87c9d772013-12-03 12:42:00 -0800127 ALOGV("Couldn't initialize RS::dispatch->ContextGetMessage");
Tim Murray0b8a2be2013-07-23 16:25:41 -0700128 return false;
129 }
130 RS::dispatch->ContextPeekMessage = (ContextPeekMessageFnPtr)dlsym(handle, "rsContextPeekMessage");
131 if (RS::dispatch->ContextPeekMessage == NULL) {
Tim Murray87c9d772013-12-03 12:42:00 -0800132 ALOGV("Couldn't initialize RS::dispatch->ContextPeekMessage");
Tim Murray0b8a2be2013-07-23 16:25:41 -0700133 return false;
134 }
135 RS::dispatch->ContextSendMessage = (ContextSendMessageFnPtr)dlsym(handle, "rsContextSendMessage");
136 if (RS::dispatch->ContextSendMessage == NULL) {
Tim Murray87c9d772013-12-03 12:42:00 -0800137 ALOGV("Couldn't initialize RS::dispatch->ContextSendMessage");
Tim Murray0b8a2be2013-07-23 16:25:41 -0700138 return false;
139 }
140 RS::dispatch->ContextInitToClient = (ContextInitToClientFnPtr)dlsym(handle, "rsContextInitToClient");
141 if (RS::dispatch->ContextInitToClient == NULL) {
Tim Murray87c9d772013-12-03 12:42:00 -0800142 ALOGV("Couldn't initialize RS::dispatch->ContextInitToClient");
Tim Murray0b8a2be2013-07-23 16:25:41 -0700143 return false;
144 }
145 RS::dispatch->ContextDeinitToClient = (ContextDeinitToClientFnPtr)dlsym(handle, "rsContextDeinitToClient");
146 if (RS::dispatch->ContextDeinitToClient == NULL) {
Tim Murray87c9d772013-12-03 12:42:00 -0800147 ALOGV("Couldn't initialize RS::dispatch->ContextDeinitToClient");
Tim Murray0b8a2be2013-07-23 16:25:41 -0700148 return false;
149 }
150 RS::dispatch->TypeCreate = (TypeCreateFnPtr)dlsym(handle, "rsTypeCreate");
151 if (RS::dispatch->TypeCreate == NULL) {
Tim Murray87c9d772013-12-03 12:42:00 -0800152 ALOGV("Couldn't initialize RS::dispatch->TypeCreate");
Tim Murray0b8a2be2013-07-23 16:25:41 -0700153 return false;
154 }
155 RS::dispatch->AllocationCreateTyped = (AllocationCreateTypedFnPtr)dlsym(handle, "rsAllocationCreateTyped");
156 if (RS::dispatch->AllocationCreateTyped == NULL) {
Tim Murray87c9d772013-12-03 12:42:00 -0800157 ALOGV("Couldn't initialize RS::dispatch->AllocationCreateTyped");
Tim Murray0b8a2be2013-07-23 16:25:41 -0700158 return false;
159 }
160 RS::dispatch->AllocationCreateFromBitmap = (AllocationCreateFromBitmapFnPtr)dlsym(handle, "rsAllocationCreateFromBitmap");
161 if (RS::dispatch->AllocationCreateFromBitmap == NULL) {
Tim Murray87c9d772013-12-03 12:42:00 -0800162 ALOGV("Couldn't initialize RS::dispatch->AllocationCreateFromBitmap");
Tim Murray0b8a2be2013-07-23 16:25:41 -0700163 return false;
164 }
165 RS::dispatch->AllocationCubeCreateFromBitmap = (AllocationCubeCreateFromBitmapFnPtr)dlsym(handle, "rsAllocationCubeCreateFromBitmap");
166 if (RS::dispatch->AllocationCubeCreateFromBitmap == NULL) {
Tim Murray87c9d772013-12-03 12:42:00 -0800167 ALOGV("Couldn't initialize RS::dispatch->AllocationCubeCreateFromBitmap");
Tim Murray0b8a2be2013-07-23 16:25:41 -0700168 return false;
169 }
170 RS::dispatch->AllocationGetSurface = (AllocationGetSurfaceFnPtr)dlsym(handle, "rsAllocationGetSurface");
171 if (RS::dispatch->AllocationGetSurface == NULL) {
Tim Murray87c9d772013-12-03 12:42:00 -0800172 ALOGV("Couldn't initialize RS::dispatch->AllocationGetSurface");
Tim Murray0b8a2be2013-07-23 16:25:41 -0700173 return false;
174 }
175 RS::dispatch->AllocationSetSurface = (AllocationSetSurfaceFnPtr)dlsym(handle, "rsAllocationSetSurface");
176 if (RS::dispatch->AllocationSetSurface == NULL) {
Tim Murray87c9d772013-12-03 12:42:00 -0800177 ALOGV("Couldn't initialize RS::dispatch->AllocationSetSurface");
Tim Murray0b8a2be2013-07-23 16:25:41 -0700178 return false;
179 }
180 RS::dispatch->ContextFinish = (ContextFinishFnPtr)dlsym(handle, "rsContextFinish");
181 if (RS::dispatch->ContextFinish == NULL) {
Tim Murray87c9d772013-12-03 12:42:00 -0800182 ALOGV("Couldn't initialize RS::dispatch->ContextFinish");
Tim Murray0b8a2be2013-07-23 16:25:41 -0700183 return false;
184 }
185 RS::dispatch->ContextDump = (ContextDumpFnPtr)dlsym(handle, "rsContextDump");
186 if (RS::dispatch->ContextDump == NULL) {
Tim Murray87c9d772013-12-03 12:42:00 -0800187 ALOGV("Couldn't initialize RS::dispatch->ContextDump");
Tim Murray0b8a2be2013-07-23 16:25:41 -0700188 return false;
189 }
190 RS::dispatch->ContextSetPriority = (ContextSetPriorityFnPtr)dlsym(handle, "rsContextSetPriority");
191 if (RS::dispatch->ContextSetPriority == NULL) {
Tim Murray87c9d772013-12-03 12:42:00 -0800192 ALOGV("Couldn't initialize RS::dispatch->ContextSetPriority");
Tim Murray0b8a2be2013-07-23 16:25:41 -0700193 return false;
194 }
195 RS::dispatch->AssignName = (AssignNameFnPtr)dlsym(handle, "rsAssignName");
196 if (RS::dispatch->AssignName == NULL) {
Tim Murray87c9d772013-12-03 12:42:00 -0800197 ALOGV("Couldn't initialize RS::dispatch->AssignName");
Tim Murray0b8a2be2013-07-23 16:25:41 -0700198 return false;
199 }
200 RS::dispatch->ObjDestroy = (ObjDestroyFnPtr)dlsym(handle, "rsObjDestroy");
201 if (RS::dispatch->ObjDestroy == NULL) {
Tim Murray87c9d772013-12-03 12:42:00 -0800202 ALOGV("Couldn't initialize RS::dispatch->ObjDestroy");
Tim Murray0b8a2be2013-07-23 16:25:41 -0700203 return false;
204 }
205 RS::dispatch->ElementCreate = (ElementCreateFnPtr)dlsym(handle, "rsElementCreate");
206 if (RS::dispatch->ElementCreate == NULL) {
Tim Murray87c9d772013-12-03 12:42:00 -0800207 ALOGV("Couldn't initialize RS::dispatch->ElementCreate");
Tim Murray0b8a2be2013-07-23 16:25:41 -0700208 return false;
209 }
210 RS::dispatch->ElementCreate2 = (ElementCreate2FnPtr)dlsym(handle, "rsElementCreate2");
211 if (RS::dispatch->ElementCreate2 == NULL) {
Tim Murray87c9d772013-12-03 12:42:00 -0800212 ALOGV("Couldn't initialize RS::dispatch->ElementCreate2");
Tim Murray0b8a2be2013-07-23 16:25:41 -0700213 return false;
214 }
215 RS::dispatch->AllocationCopyToBitmap = (AllocationCopyToBitmapFnPtr)dlsym(handle, "rsAllocationCopyToBitmap");
216 if (RS::dispatch->AllocationCopyToBitmap == NULL) {
Tim Murray87c9d772013-12-03 12:42:00 -0800217 ALOGV("Couldn't initialize RS::dispatch->AllocationCopyToBitmap");
Tim Murray0b8a2be2013-07-23 16:25:41 -0700218 return false;
219 }
220 RS::dispatch->Allocation1DData = (Allocation1DDataFnPtr)dlsym(handle, "rsAllocation1DData");
221 if (RS::dispatch->Allocation1DData == NULL) {
Tim Murray87c9d772013-12-03 12:42:00 -0800222 ALOGV("Couldn't initialize RS::dispatch->Allocation1DData");
Tim Murray0b8a2be2013-07-23 16:25:41 -0700223 return false;
224 }
225 RS::dispatch->Allocation1DElementData = (Allocation1DElementDataFnPtr)dlsym(handle, "rsAllocation1DElementData");
226 if (RS::dispatch->Allocation1DElementData == NULL) {
Tim Murray87c9d772013-12-03 12:42:00 -0800227 ALOGV("Couldn't initialize RS::dispatch->Allocation1DElementData");
Tim Murray0b8a2be2013-07-23 16:25:41 -0700228 return false;
229 }
230 RS::dispatch->Allocation2DData = (Allocation2DDataFnPtr)dlsym(handle, "rsAllocation2DData");
231 if (RS::dispatch->Allocation2DData == NULL) {
Tim Murray87c9d772013-12-03 12:42:00 -0800232 ALOGV("Couldn't initialize RS::dispatch->Allocation2DData");
Tim Murray0b8a2be2013-07-23 16:25:41 -0700233 return false;
234 }
235 RS::dispatch->Allocation3DData = (Allocation3DDataFnPtr)dlsym(handle, "rsAllocation3DData");
236 if (RS::dispatch->Allocation3DData == NULL) {
Tim Murray87c9d772013-12-03 12:42:00 -0800237 ALOGV("Couldn't initialize RS::dispatch->Allocation3DData");
Tim Murray0b8a2be2013-07-23 16:25:41 -0700238 return false;
239 }
240 RS::dispatch->AllocationGenerateMipmaps = (AllocationGenerateMipmapsFnPtr)dlsym(handle, "rsAllocationGenerateMipmaps");
241 if (RS::dispatch->AllocationGenerateMipmaps == NULL) {
Tim Murray87c9d772013-12-03 12:42:00 -0800242 ALOGV("Couldn't initialize RS::dispatch->AllocationGenerateMipmaps");
Tim Murray0b8a2be2013-07-23 16:25:41 -0700243 return false;
244 }
245 RS::dispatch->AllocationRead = (AllocationReadFnPtr)dlsym(handle, "rsAllocationRead");
246 if (RS::dispatch->AllocationRead == NULL) {
Tim Murray87c9d772013-12-03 12:42:00 -0800247 ALOGV("Couldn't initialize RS::dispatch->AllocationRead");
Tim Murray0b8a2be2013-07-23 16:25:41 -0700248 return false;
249 }
250 RS::dispatch->Allocation1DRead = (Allocation1DReadFnPtr)dlsym(handle, "rsAllocation1DRead");
251 if (RS::dispatch->Allocation1DRead == NULL) {
Tim Murray87c9d772013-12-03 12:42:00 -0800252 ALOGV("Couldn't initialize RS::dispatch->Allocation1DRead");
Tim Murray0b8a2be2013-07-23 16:25:41 -0700253 return false;
254 }
255 RS::dispatch->Allocation2DRead = (Allocation2DReadFnPtr)dlsym(handle, "rsAllocation2DRead");
256 if (RS::dispatch->Allocation2DRead == NULL) {
Tim Murray87c9d772013-12-03 12:42:00 -0800257 ALOGV("Couldn't initialize RS::dispatch->Allocation2DRead");
Tim Murray0b8a2be2013-07-23 16:25:41 -0700258 return false;
259 }
260 RS::dispatch->AllocationSyncAll = (AllocationSyncAllFnPtr)dlsym(handle, "rsAllocationSyncAll");
261 if (RS::dispatch->AllocationSyncAll == NULL) {
Tim Murray87c9d772013-12-03 12:42:00 -0800262 ALOGV("Couldn't initialize RS::dispatch->AllocationSyncAll");
Tim Murray0b8a2be2013-07-23 16:25:41 -0700263 return false;
264 }
265 RS::dispatch->AllocationResize1D = (AllocationResize1DFnPtr)dlsym(handle, "rsAllocationResize1D");
266 if (RS::dispatch->AllocationResize1D == NULL) {
Tim Murray87c9d772013-12-03 12:42:00 -0800267 ALOGV("Couldn't initialize RS::dispatch->AllocationResize1D");
Tim Murray0b8a2be2013-07-23 16:25:41 -0700268 return false;
269 }
270 RS::dispatch->AllocationCopy2DRange = (AllocationCopy2DRangeFnPtr)dlsym(handle, "rsAllocationCopy2DRange");
271 if (RS::dispatch->AllocationCopy2DRange == NULL) {
Tim Murray87c9d772013-12-03 12:42:00 -0800272 ALOGV("Couldn't initialize RS::dispatch->AllocationCopy2DRange");
Tim Murray0b8a2be2013-07-23 16:25:41 -0700273 return false;
274 }
275 RS::dispatch->AllocationCopy3DRange = (AllocationCopy3DRangeFnPtr)dlsym(handle, "rsAllocationCopy3DRange");
276 if (RS::dispatch->AllocationCopy3DRange == NULL) {
Tim Murray87c9d772013-12-03 12:42:00 -0800277 ALOGV("Couldn't initialize RS::dispatch->AllocationCopy3DRange");
Tim Murray0b8a2be2013-07-23 16:25:41 -0700278 return false;
279 }
280 RS::dispatch->SamplerCreate = (SamplerCreateFnPtr)dlsym(handle, "rsSamplerCreate");
281 if (RS::dispatch->SamplerCreate == NULL) {
Tim Murray87c9d772013-12-03 12:42:00 -0800282 ALOGV("Couldn't initialize RS::dispatch->SamplerCreate");
Tim Murray0b8a2be2013-07-23 16:25:41 -0700283 return false;
284 }
285 RS::dispatch->ScriptBindAllocation = (ScriptBindAllocationFnPtr)dlsym(handle, "rsScriptBindAllocation");
286 if (RS::dispatch->ScriptBindAllocation == NULL) {
Tim Murray87c9d772013-12-03 12:42:00 -0800287 ALOGV("Couldn't initialize RS::dispatch->ScriptBindAllocation");
Tim Murray0b8a2be2013-07-23 16:25:41 -0700288 return false;
289 }
290 RS::dispatch->ScriptSetTimeZone = (ScriptSetTimeZoneFnPtr)dlsym(handle, "rsScriptSetTimeZone");
291 if (RS::dispatch->ScriptSetTimeZone == NULL) {
Tim Murray87c9d772013-12-03 12:42:00 -0800292 ALOGV("Couldn't initialize RS::dispatch->ScriptSetTimeZone");
Tim Murray0b8a2be2013-07-23 16:25:41 -0700293 return false;
294 }
295 RS::dispatch->ScriptInvoke = (ScriptInvokeFnPtr)dlsym(handle, "rsScriptInvoke");
296 if (RS::dispatch->ScriptInvoke == NULL) {
Tim Murray87c9d772013-12-03 12:42:00 -0800297 ALOGV("Couldn't initialize RS::dispatch->ScriptInvoke");
Tim Murray0b8a2be2013-07-23 16:25:41 -0700298 return false;
299 }
300 RS::dispatch->ScriptInvokeV = (ScriptInvokeVFnPtr)dlsym(handle, "rsScriptInvokeV");
301 if (RS::dispatch->ScriptInvokeV == NULL) {
Tim Murray87c9d772013-12-03 12:42:00 -0800302 ALOGV("Couldn't initialize RS::dispatch->ScriptInvokeV");
Tim Murray0b8a2be2013-07-23 16:25:41 -0700303 return false;
304 }
305 RS::dispatch->ScriptForEach = (ScriptForEachFnPtr)dlsym(handle, "rsScriptForEach");
306 if (RS::dispatch->ScriptForEach == NULL) {
Tim Murray87c9d772013-12-03 12:42:00 -0800307 ALOGV("Couldn't initialize RS::dispatch->ScriptForEach");
Tim Murray0b8a2be2013-07-23 16:25:41 -0700308 return false;
309 }
310 RS::dispatch->ScriptSetVarI = (ScriptSetVarIFnPtr)dlsym(handle, "rsScriptSetVarI");
311 if (RS::dispatch->ScriptSetVarI == NULL) {
Tim Murray87c9d772013-12-03 12:42:00 -0800312 ALOGV("Couldn't initialize RS::dispatch->ScriptSetVarI");
Tim Murray0b8a2be2013-07-23 16:25:41 -0700313 return false;
314 }
315 RS::dispatch->ScriptSetVarObj = (ScriptSetVarObjFnPtr)dlsym(handle, "rsScriptSetVarObj");
316 if (RS::dispatch->ScriptSetVarObj == NULL) {
Tim Murray87c9d772013-12-03 12:42:00 -0800317 ALOGV("Couldn't initialize RS::dispatch->ScriptSetVarObj");
Tim Murray0b8a2be2013-07-23 16:25:41 -0700318 return false;
319 }
320 RS::dispatch->ScriptSetVarJ = (ScriptSetVarJFnPtr)dlsym(handle, "rsScriptSetVarJ");
321 if (RS::dispatch->ScriptSetVarJ == NULL) {
Tim Murray87c9d772013-12-03 12:42:00 -0800322 ALOGV("Couldn't initialize RS::dispatch->ScriptSetVarJ");
Tim Murray0b8a2be2013-07-23 16:25:41 -0700323 return false;
324 }
325 RS::dispatch->ScriptSetVarF = (ScriptSetVarFFnPtr)dlsym(handle, "rsScriptSetVarF");
326 if (RS::dispatch->ScriptSetVarF == NULL) {
Tim Murray87c9d772013-12-03 12:42:00 -0800327 ALOGV("Couldn't initialize RS::dispatch->ScriptSetVarF");
Tim Murray0b8a2be2013-07-23 16:25:41 -0700328 return false;
329 }
330 RS::dispatch->ScriptSetVarD = (ScriptSetVarDFnPtr)dlsym(handle, "rsScriptSetVarD");
331 if (RS::dispatch->ScriptSetVarD == NULL) {
Tim Murray87c9d772013-12-03 12:42:00 -0800332 ALOGV("Couldn't initialize RS::dispatch->ScriptSetVarD");
Tim Murray0b8a2be2013-07-23 16:25:41 -0700333 return false;
334 }
335 RS::dispatch->ScriptSetVarV = (ScriptSetVarVFnPtr)dlsym(handle, "rsScriptSetVarV");
336 if (RS::dispatch->ScriptSetVarV == NULL) {
Tim Murray87c9d772013-12-03 12:42:00 -0800337 ALOGV("Couldn't initialize RS::dispatch->ScriptSetVarV");
Tim Murray0b8a2be2013-07-23 16:25:41 -0700338 return false;
339 }
340 RS::dispatch->ScriptGetVarV = (ScriptGetVarVFnPtr)dlsym(handle, "rsScriptGetVarV");
341 if (RS::dispatch->ScriptGetVarV == NULL) {
Tim Murray87c9d772013-12-03 12:42:00 -0800342 ALOGV("Couldn't initialize RS::dispatch->ScriptGetVarV");
Tim Murray0b8a2be2013-07-23 16:25:41 -0700343 return false;
344 }
345 RS::dispatch->ScriptSetVarVE = (ScriptSetVarVEFnPtr)dlsym(handle, "rsScriptSetVarVE");
346 if (RS::dispatch->ScriptSetVarVE == NULL) {
Tim Murray87c9d772013-12-03 12:42:00 -0800347 ALOGV("Couldn't initialize RS::dispatch->ScriptSetVarVE");
Tim Murray0b8a2be2013-07-23 16:25:41 -0700348 return false;
349 }
350 RS::dispatch->ScriptCCreate = (ScriptCCreateFnPtr)dlsym(handle, "rsScriptCCreate");
351 if (RS::dispatch->ScriptCCreate == NULL) {
Tim Murray87c9d772013-12-03 12:42:00 -0800352 ALOGV("Couldn't initialize RS::dispatch->ScriptCCreate");
Tim Murray0b8a2be2013-07-23 16:25:41 -0700353 return false;
354 }
355 RS::dispatch->ScriptIntrinsicCreate = (ScriptIntrinsicCreateFnPtr)dlsym(handle, "rsScriptIntrinsicCreate");
356 if (RS::dispatch->ScriptIntrinsicCreate == NULL) {
Tim Murray87c9d772013-12-03 12:42:00 -0800357 ALOGV("Couldn't initialize RS::dispatch->ScriptIntrinsicCreate");
Tim Murray0b8a2be2013-07-23 16:25:41 -0700358 return false;
359 }
360 RS::dispatch->ScriptKernelIDCreate = (ScriptKernelIDCreateFnPtr)dlsym(handle, "rsScriptKernelIDCreate");
361 if (RS::dispatch->ScriptKernelIDCreate == NULL) {
Tim Murray87c9d772013-12-03 12:42:00 -0800362 ALOGV("Couldn't initialize RS::dispatch->ScriptKernelIDCreate");
Tim Murray0b8a2be2013-07-23 16:25:41 -0700363 return false;
364 }
365 RS::dispatch->ScriptFieldIDCreate = (ScriptFieldIDCreateFnPtr)dlsym(handle, "rsScriptFieldIDCreate");
366 if (RS::dispatch->ScriptFieldIDCreate == NULL) {
Tim Murray87c9d772013-12-03 12:42:00 -0800367 ALOGV("Couldn't initialize RS::dispatch->ScriptFieldIDCreate");
Tim Murray0b8a2be2013-07-23 16:25:41 -0700368 return false;
369 }
370 RS::dispatch->ScriptGroupCreate = (ScriptGroupCreateFnPtr)dlsym(handle, "rsScriptGroupCreate");
371 if (RS::dispatch->ScriptGroupCreate == NULL) {
Tim Murray87c9d772013-12-03 12:42:00 -0800372 ALOGV("Couldn't initialize RS::dispatch->ScriptGroupCreate");
Tim Murray0b8a2be2013-07-23 16:25:41 -0700373 return false;
374 }
375 RS::dispatch->ScriptGroupSetOutput = (ScriptGroupSetOutputFnPtr)dlsym(handle, "rsScriptGroupSetOutput");
376 if (RS::dispatch->ScriptGroupSetOutput == NULL) {
Tim Murray87c9d772013-12-03 12:42:00 -0800377 ALOGV("Couldn't initialize RS::dispatch->ScriptGroupSetOutput");
Tim Murray0b8a2be2013-07-23 16:25:41 -0700378 return false;
379 }
380 RS::dispatch->ScriptGroupSetInput = (ScriptGroupSetInputFnPtr)dlsym(handle, "rsScriptGroupSetInput");
381 if (RS::dispatch->ScriptGroupSetInput == NULL) {
Tim Murray87c9d772013-12-03 12:42:00 -0800382 ALOGV("Couldn't initialize RS::dispatch->ScriptGroupSetInput");
Tim Murray0b8a2be2013-07-23 16:25:41 -0700383 return false;
384 }
385 RS::dispatch->ScriptGroupExecute = (ScriptGroupExecuteFnPtr)dlsym(handle, "rsScriptGroupExecute");
386 if (RS::dispatch->ScriptGroupExecute == NULL) {
Tim Murray87c9d772013-12-03 12:42:00 -0800387 ALOGV("Couldn't initialize RS::dispatch->ScriptGroupExecute");
Tim Murray0b8a2be2013-07-23 16:25:41 -0700388 return false;
389 }
390 RS::dispatch->AllocationIoSend = (AllocationIoSendFnPtr)dlsym(handle, "rsAllocationIoSend");
391 if (RS::dispatch->AllocationIoSend == NULL) {
Tim Murray87c9d772013-12-03 12:42:00 -0800392 ALOGV("Couldn't initialize RS::dispatch->AllocationIoSend");
Tim Murray0b8a2be2013-07-23 16:25:41 -0700393 return false;
394 }
395 RS::dispatch->AllocationIoReceive = (AllocationIoReceiveFnPtr)dlsym(handle, "rsAllocationIoReceive");
396 if (RS::dispatch->AllocationIoReceive == NULL) {
Tim Murray87c9d772013-12-03 12:42:00 -0800397 ALOGV("Couldn't initialize RS::dispatch->AllocationIoReceive");
Tim Murray0b8a2be2013-07-23 16:25:41 -0700398 return false;
399 }
400
401 return true;
402}
403
Tim Murray75e877d2013-09-11 14:45:20 -0700404// this will only open API 19+ libRS
405// because that's when we changed libRS to extern "C" entry points
Tim Murray4a92d122013-07-22 10:56:18 -0700406static bool loadSO(const char* filename) {
407 void* handle = dlopen(filename, RTLD_LAZY | RTLD_LOCAL);
408 if (handle == NULL) {
Tim Murray87c9d772013-12-03 12:42:00 -0800409 ALOGV("couldn't dlopen %s, %s", filename, dlerror());
Tim Murray4a92d122013-07-22 10:56:18 -0700410 return false;
411 }
Tim Murraya4230962013-07-17 16:50:10 -0700412
Tim Murray4a92d122013-07-22 10:56:18 -0700413 if (loadSymbols(handle) == false) {
Tim Murray87c9d772013-12-03 12:42:00 -0800414 ALOGV("%s init failed!", filename);
Tim Murray4a92d122013-07-22 10:56:18 -0700415 return false;
416 }
Tim Murray84e3dea2013-09-09 16:12:51 -0700417 //ALOGE("Successfully loaded %s", filename);
Tim Murray4a92d122013-07-22 10:56:18 -0700418 return true;
419}
420
421static uint32_t getProp(const char *str) {
422#if !defined(RS_SERVER) && defined(HAVE_ANDROID_OS)
423 char buf[256];
424 property_get(str, buf, "0");
425 return atoi(buf);
426#else
427 return 0;
428#endif
429}
430
431bool RS::initDispatch(int targetApi) {
Tim Murraya4230962013-07-17 16:50:10 -0700432 pthread_mutex_lock(&gInitMutex);
433 if (gInitError) {
434 goto error;
435 } else if (gInitialized) {
Tim Murray47666f52013-07-29 14:32:34 -0700436 pthread_mutex_unlock(&gInitMutex);
Tim Murraya4230962013-07-17 16:50:10 -0700437 return true;
438 }
Tim Murraya4230962013-07-17 16:50:10 -0700439
440 RS::dispatch = new dispatchTable;
Tim Murray4a92d122013-07-22 10:56:18 -0700441
442 // attempt to load libRS, load libRSSupport on failure
443 // if property is set, proceed directly to libRSSupport
444 if (getProp("debug.rs.forcecompat") == 0) {
445 usingNative = loadSO("libRS.so");
446 }
447 if (usingNative == false) {
448 if (loadSO("libRSSupport.so") == false) {
449 ALOGE("Failed to load libRS.so and libRSSupport.so");
450 goto error;
451 }
Tim Murraya4230962013-07-17 16:50:10 -0700452 }
453
454 gInitialized = true;
455
456 pthread_mutex_unlock(&gInitMutex);
457 return true;
458
459 error:
460 gInitError = 1;
461 pthread_mutex_unlock(&gInitMutex);
462 return false;
463}
464
Tim Murraycaf41262013-12-13 12:54:37 -0800465bool RS::init(std::string &name, int targetApi, uint32_t flags) {
466 if (mInit) {
467 return true;
468 }
469
Tim Murraya4230962013-07-17 16:50:10 -0700470 if (initDispatch(targetApi) == false) {
471 ALOGE("Couldn't initialize dispatch table");
472 return false;
473 }
474
Tim Murraycaf41262013-12-13 12:54:37 -0800475 mCacheDir = name;
476 mCacheDir += "/com.android.renderscript.cache/";
477
Tim Murraya4230962013-07-17 16:50:10 -0700478 mDev = RS::dispatch->DeviceCreate();
Jason Sams221a4b12012-02-22 15:22:41 -0800479 if (mDev == 0) {
480 ALOGE("Device creation failed");
481 return false;
482 }
483
Tim Murray84e3dea2013-09-09 16:12:51 -0700484 if (flags >= RS_CONTEXT_MAX) {
485 ALOGE("Invalid flags passed");
486 return false;
487 }
488
489 mContext = RS::dispatch->ContextCreate(mDev, 0, targetApi, RS_CONTEXT_TYPE_NORMAL, flags);
Jason Sams221a4b12012-02-22 15:22:41 -0800490 if (mContext == 0) {
491 ALOGE("Context creation failed");
492 return false;
493 }
494
Jason Sams221a4b12012-02-22 15:22:41 -0800495 pid_t mNativeMessageThreadId;
496
497 int status = pthread_create(&mMessageThreadId, NULL, threadProc, this);
498 if (status) {
Tim Murray84bf2b82012-10-31 16:03:16 -0700499 ALOGE("Failed to start RS message thread.");
Jason Sams221a4b12012-02-22 15:22:41 -0800500 return false;
501 }
502 // Wait for the message thread to be active.
503 while (!mMessageRun) {
504 usleep(1000);
505 }
506
Tim Murraya4230962013-07-17 16:50:10 -0700507 mInit = true;
508
Jason Sams221a4b12012-02-22 15:22:41 -0800509 return true;
510}
511
Tim Murray21fa7a02013-08-15 16:25:03 -0700512void RS::throwError(RSError error, const char *errMsg) {
513 if (mCurrentError == RS_SUCCESS) {
514 mCurrentError = error;
515 ALOGE("RS CPP error: %s", errMsg);
516 } else {
517 ALOGE("RS CPP error (masked by previous error): %s", errMsg);
518 }
Jason Samsb2e3dc52012-02-23 17:14:39 -0800519}
520
Tim Murray10913a52013-08-20 17:19:47 -0700521RSError RS::getError() {
522 return mCurrentError;
523}
524
Jason Samsb2e3dc52012-02-23 17:14:39 -0800525
Tim Murray84bf2b82012-10-31 16:03:16 -0700526void * RS::threadProc(void *vrsc) {
527 RS *rs = static_cast<RS *>(vrsc);
Jason Sams221a4b12012-02-22 15:22:41 -0800528 size_t rbuf_size = 256;
529 void * rbuf = malloc(rbuf_size);
530
Tim Murraya4230962013-07-17 16:50:10 -0700531 RS::dispatch->ContextInitToClient(rs->mContext);
Jason Sams221a4b12012-02-22 15:22:41 -0800532 rs->mMessageRun = true;
533
534 while (rs->mMessageRun) {
535 size_t receiveLen = 0;
536 uint32_t usrID = 0;
537 uint32_t subID = 0;
Tim Murraya4230962013-07-17 16:50:10 -0700538 RsMessageToClientType r = RS::dispatch->ContextPeekMessage(rs->mContext,
539 &receiveLen, sizeof(receiveLen),
540 &usrID, sizeof(usrID));
Jason Sams221a4b12012-02-22 15:22:41 -0800541
542 if (receiveLen >= rbuf_size) {
543 rbuf_size = receiveLen + 32;
544 rbuf = realloc(rbuf, rbuf_size);
545 }
546 if (!rbuf) {
Tim Murray84bf2b82012-10-31 16:03:16 -0700547 ALOGE("RS::message handler realloc error %zu", rbuf_size);
Jason Sams221a4b12012-02-22 15:22:41 -0800548 // No clean way to recover now?
549 }
Tim Murraya4230962013-07-17 16:50:10 -0700550 RS::dispatch->ContextGetMessage(rs->mContext, rbuf, rbuf_size, &receiveLen, sizeof(receiveLen),
Jason Sams221a4b12012-02-22 15:22:41 -0800551 &subID, sizeof(subID));
552
553 switch(r) {
554 case RS_MESSAGE_TO_CLIENT_ERROR:
555 ALOGE("RS Error %s", (const char *)rbuf);
Tim Murray21fa7a02013-08-15 16:25:03 -0700556 rs->throwError(RS_ERROR_RUNTIME_ERROR, "Error returned from runtime");
Jason Sams221a4b12012-02-22 15:22:41 -0800557 if(rs->mMessageFunc != NULL) {
558 rs->mErrorFunc(usrID, (const char *)rbuf);
559 }
560 break;
Stephen Hines76a1be42012-11-26 16:26:03 -0800561 case RS_MESSAGE_TO_CLIENT_NONE:
Jason Sams221a4b12012-02-22 15:22:41 -0800562 case RS_MESSAGE_TO_CLIENT_EXCEPTION:
Stephen Hines76a1be42012-11-26 16:26:03 -0800563 case RS_MESSAGE_TO_CLIENT_RESIZE:
Jason Sams221a4b12012-02-22 15:22:41 -0800564 // teardown. But we want to avoid starving other threads during
565 // teardown by yielding until the next line in the destructor can
Stephen Hines76a1be42012-11-26 16:26:03 -0800566 // execute to set mRun = false. Note that the FIFO sends an
567 // empty NONE message when it reaches its destructor.
Jason Sams221a4b12012-02-22 15:22:41 -0800568 usleep(1000);
569 break;
570 case RS_MESSAGE_TO_CLIENT_USER:
571 if(rs->mMessageFunc != NULL) {
572 rs->mMessageFunc(usrID, rbuf, receiveLen);
573 } else {
574 ALOGE("Received a message from the script with no message handler installed.");
575 }
576 break;
577
578 default:
Tim Murray84bf2b82012-10-31 16:03:16 -0700579 ALOGE("RS unknown message type %i", r);
Jason Sams221a4b12012-02-22 15:22:41 -0800580 }
581 }
582
583 if (rbuf) {
584 free(rbuf);
585 }
Tim Murray87c9d772013-12-03 12:42:00 -0800586 ALOGV("RS Message thread exiting.");
Jason Sams221a4b12012-02-22 15:22:41 -0800587 return NULL;
588}
589
Tim Murray84bf2b82012-10-31 16:03:16 -0700590void RS::setErrorHandler(ErrorHandlerFunc_t func) {
Jason Sams221a4b12012-02-22 15:22:41 -0800591 mErrorFunc = func;
592}
593
Tim Murray84bf2b82012-10-31 16:03:16 -0700594void RS::setMessageHandler(MessageHandlerFunc_t func) {
Jason Sams221a4b12012-02-22 15:22:41 -0800595 mMessageFunc = func;
596}
Tim Murraybaca6c32012-11-14 16:51:46 -0800597
598void RS::finish() {
Tim Murraya4230962013-07-17 16:50:10 -0700599 RS::dispatch->ContextFinish(mContext);
Tim Murraybaca6c32012-11-14 16:51:46 -0800600}