blob: 04f1e88e8efb1166a77e9a5c8111d24ad8996b3a [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>
Tim Murray0f98d502014-01-15 14:35:31 -080026#include <unistd.h>
Tim Murraya4230962013-07-17 16:50:10 -070027
Tim Murray0f98d502014-01-15 14:35:31 -080028#if !defined(RS_SERVER) && !defined(RS_COMPATIBILITY_LIB) && defined(HAVE_ANDROID_OS)
Tim Murray4a92d122013-07-22 10:56:18 -070029#include <cutils/properties.h>
Tim Murray0f98d502014-01-15 14:35:31 -080030#else
31#include "rsCompatibilityLib.h"
Tim Murray4a92d122013-07-22 10:56:18 -070032#endif
33
Tim Murray0f98d502014-01-15 14:35:31 -080034
Jason Sams69cccdf2012-04-02 19:11:49 -070035using namespace android;
Tim Murray9eb7f4b2012-11-16 14:02:18 -080036using namespace RSC;
Jason Sams69cccdf2012-04-02 19:11:49 -070037
Tim Murray84bf2b82012-10-31 16:03:16 -070038bool RS::gInitialized = false;
Tim Murray4a92d122013-07-22 10:56:18 -070039bool RS::usingNative = false;
Tim Murray84bf2b82012-10-31 16:03:16 -070040pthread_mutex_t RS::gInitMutex = PTHREAD_MUTEX_INITIALIZER;
Tim Murraya4230962013-07-17 16:50:10 -070041dispatchTable* RS::dispatch = NULL;
Tim Murraya4230962013-07-17 16:50:10 -070042static int gInitError = 0;
Jason Sams221a4b12012-02-22 15:22:41 -080043
Tim Murray84bf2b82012-10-31 16:03:16 -070044RS::RS() {
Jason Sams221a4b12012-02-22 15:22:41 -080045 mDev = NULL;
46 mContext = NULL;
47 mErrorFunc = NULL;
48 mMessageFunc = NULL;
49 mMessageRun = false;
Tim Murraya4230962013-07-17 16:50:10 -070050 mInit = false;
Tim Murray21fa7a02013-08-15 16:25:03 -070051 mCurrentError = RS_SUCCESS;
Jason Sams221a4b12012-02-22 15:22:41 -080052
53 memset(&mElements, 0, sizeof(mElements));
Tim Murray729b6fe2013-07-23 16:20:42 -070054 memset(&mSamplers, 0, sizeof(mSamplers));
Jason Sams221a4b12012-02-22 15:22:41 -080055}
56
Tim Murray84bf2b82012-10-31 16:03:16 -070057RS::~RS() {
Tim Murraya4230962013-07-17 16:50:10 -070058 if (mInit == true) {
59 mMessageRun = false;
Jason Sams221a4b12012-02-22 15:22:41 -080060
Tim Murraya4230962013-07-17 16:50:10 -070061 RS::dispatch->ContextDeinitToClient(mContext);
Jason Sams221a4b12012-02-22 15:22:41 -080062
Tim Murraya4230962013-07-17 16:50:10 -070063 void *res = NULL;
64 int status = pthread_join(mMessageThreadId, &res);
Jason Sams221a4b12012-02-22 15:22:41 -080065
Tim Murraya4230962013-07-17 16:50:10 -070066 RS::dispatch->ContextDestroy(mContext);
67 mContext = NULL;
68 RS::dispatch->DeviceDestroy(mDev);
69 mDev = NULL;
70 }
Jason Sams221a4b12012-02-22 15:22:41 -080071}
72
Tim Murraycaf41262013-12-13 12:54:37 -080073bool RS::init(std::string name, uint32_t flags) {
74 return RS::init(name, RS_VERSION, flags);
Tim Murray84bf2b82012-10-31 16:03:16 -070075}
76
Tim Murray0b8a2be2013-07-23 16:25:41 -070077static bool loadSymbols(void* handle) {
78
79 RS::dispatch->AllocationGetType = (AllocationGetTypeFnPtr)dlsym(handle, "rsaAllocationGetType");
80 if (RS::dispatch->AllocationGetType == NULL) {
Tim Murray87c9d772013-12-03 12:42:00 -080081 ALOGV("Couldn't initialize RS::dispatch->AllocationGetType");
Tim Murray0b8a2be2013-07-23 16:25:41 -070082 return false;
83 }
84 RS::dispatch->TypeGetNativeData = (TypeGetNativeDataFnPtr)dlsym(handle, "rsaTypeGetNativeData");
85 if (RS::dispatch->TypeGetNativeData == NULL) {
Tim Murray87c9d772013-12-03 12:42:00 -080086 ALOGV("Couldn't initialize RS::dispatch->TypeGetNativeData");
Tim Murray0b8a2be2013-07-23 16:25:41 -070087 return false;
88 }
89 RS::dispatch->ElementGetNativeData = (ElementGetNativeDataFnPtr)dlsym(handle, "rsaElementGetNativeData");
90 if (RS::dispatch->ElementGetNativeData == NULL) {
Tim Murray87c9d772013-12-03 12:42:00 -080091 ALOGV("Couldn't initialize RS::dispatch->ElementGetNativeData");
Tim Murray0b8a2be2013-07-23 16:25:41 -070092 return false;
93 }
94 RS::dispatch->ElementGetSubElements = (ElementGetSubElementsFnPtr)dlsym(handle, "rsaElementGetSubElements");
95 if (RS::dispatch->ElementGetSubElements == NULL) {
Tim Murray87c9d772013-12-03 12:42:00 -080096 ALOGV("Couldn't initialize RS::dispatch->ElementGetSubElements");
Tim Murray0b8a2be2013-07-23 16:25:41 -070097 return false;
98 }
99 RS::dispatch->DeviceCreate = (DeviceCreateFnPtr)dlsym(handle, "rsDeviceCreate");
100 if (RS::dispatch->DeviceCreate == NULL) {
Tim Murray87c9d772013-12-03 12:42:00 -0800101 ALOGV("Couldn't initialize RS::dispatch->DeviceCreate");
Tim Murray0b8a2be2013-07-23 16:25:41 -0700102 return false;
103 }
104 RS::dispatch->DeviceDestroy = (DeviceDestroyFnPtr)dlsym(handle, "rsDeviceDestroy");
105 if (RS::dispatch->DeviceDestroy == NULL) {
Tim Murray87c9d772013-12-03 12:42:00 -0800106 ALOGV("Couldn't initialize RS::dispatch->DeviceDestroy");
Tim Murray0b8a2be2013-07-23 16:25:41 -0700107 return false;
108 }
109 RS::dispatch->DeviceSetConfig = (DeviceSetConfigFnPtr)dlsym(handle, "rsDeviceSetConfig");
110 if (RS::dispatch->DeviceSetConfig == NULL) {
Tim Murray87c9d772013-12-03 12:42:00 -0800111 ALOGV("Couldn't initialize RS::dispatch->DeviceSetConfig");
Tim Murray0b8a2be2013-07-23 16:25:41 -0700112 return false;
113 }
114 RS::dispatch->ContextCreate = (ContextCreateFnPtr)dlsym(handle, "rsContextCreate");;
115 if (RS::dispatch->ContextCreate == NULL) {
Tim Murray87c9d772013-12-03 12:42:00 -0800116 ALOGV("Couldn't initialize RS::dispatch->ContextCreate");
Tim Murray0b8a2be2013-07-23 16:25:41 -0700117 return false;
118 }
Tim Murray4a92d122013-07-22 10:56:18 -0700119 RS::dispatch->GetName = (GetNameFnPtr)dlsym(handle, "rsaGetName");;
120 if (RS::dispatch->GetName == NULL) {
Tim Murray87c9d772013-12-03 12:42:00 -0800121 ALOGV("Couldn't initialize RS::dispatch->GetName");
Tim Murray4a92d122013-07-22 10:56:18 -0700122 return false;
123 }
Tim Murray0b8a2be2013-07-23 16:25:41 -0700124 RS::dispatch->ContextDestroy = (ContextDestroyFnPtr)dlsym(handle, "rsContextDestroy");
125 if (RS::dispatch->ContextDestroy == NULL) {
Tim Murray87c9d772013-12-03 12:42:00 -0800126 ALOGV("Couldn't initialize RS::dispatch->ContextDestroy");
Tim Murray0b8a2be2013-07-23 16:25:41 -0700127 return false;
128 }
129 RS::dispatch->ContextGetMessage = (ContextGetMessageFnPtr)dlsym(handle, "rsContextGetMessage");
130 if (RS::dispatch->ContextGetMessage == NULL) {
Tim Murray87c9d772013-12-03 12:42:00 -0800131 ALOGV("Couldn't initialize RS::dispatch->ContextGetMessage");
Tim Murray0b8a2be2013-07-23 16:25:41 -0700132 return false;
133 }
134 RS::dispatch->ContextPeekMessage = (ContextPeekMessageFnPtr)dlsym(handle, "rsContextPeekMessage");
135 if (RS::dispatch->ContextPeekMessage == NULL) {
Tim Murray87c9d772013-12-03 12:42:00 -0800136 ALOGV("Couldn't initialize RS::dispatch->ContextPeekMessage");
Tim Murray0b8a2be2013-07-23 16:25:41 -0700137 return false;
138 }
139 RS::dispatch->ContextSendMessage = (ContextSendMessageFnPtr)dlsym(handle, "rsContextSendMessage");
140 if (RS::dispatch->ContextSendMessage == NULL) {
Tim Murray87c9d772013-12-03 12:42:00 -0800141 ALOGV("Couldn't initialize RS::dispatch->ContextSendMessage");
Tim Murray0b8a2be2013-07-23 16:25:41 -0700142 return false;
143 }
144 RS::dispatch->ContextInitToClient = (ContextInitToClientFnPtr)dlsym(handle, "rsContextInitToClient");
145 if (RS::dispatch->ContextInitToClient == NULL) {
Tim Murray87c9d772013-12-03 12:42:00 -0800146 ALOGV("Couldn't initialize RS::dispatch->ContextInitToClient");
Tim Murray0b8a2be2013-07-23 16:25:41 -0700147 return false;
148 }
149 RS::dispatch->ContextDeinitToClient = (ContextDeinitToClientFnPtr)dlsym(handle, "rsContextDeinitToClient");
150 if (RS::dispatch->ContextDeinitToClient == NULL) {
Tim Murray87c9d772013-12-03 12:42:00 -0800151 ALOGV("Couldn't initialize RS::dispatch->ContextDeinitToClient");
Tim Murray0b8a2be2013-07-23 16:25:41 -0700152 return false;
153 }
154 RS::dispatch->TypeCreate = (TypeCreateFnPtr)dlsym(handle, "rsTypeCreate");
155 if (RS::dispatch->TypeCreate == NULL) {
Tim Murray87c9d772013-12-03 12:42:00 -0800156 ALOGV("Couldn't initialize RS::dispatch->TypeCreate");
Tim Murray0b8a2be2013-07-23 16:25:41 -0700157 return false;
158 }
159 RS::dispatch->AllocationCreateTyped = (AllocationCreateTypedFnPtr)dlsym(handle, "rsAllocationCreateTyped");
160 if (RS::dispatch->AllocationCreateTyped == NULL) {
Tim Murray87c9d772013-12-03 12:42:00 -0800161 ALOGV("Couldn't initialize RS::dispatch->AllocationCreateTyped");
Tim Murray0b8a2be2013-07-23 16:25:41 -0700162 return false;
163 }
164 RS::dispatch->AllocationCreateFromBitmap = (AllocationCreateFromBitmapFnPtr)dlsym(handle, "rsAllocationCreateFromBitmap");
165 if (RS::dispatch->AllocationCreateFromBitmap == NULL) {
Tim Murray87c9d772013-12-03 12:42:00 -0800166 ALOGV("Couldn't initialize RS::dispatch->AllocationCreateFromBitmap");
Tim Murray0b8a2be2013-07-23 16:25:41 -0700167 return false;
168 }
169 RS::dispatch->AllocationCubeCreateFromBitmap = (AllocationCubeCreateFromBitmapFnPtr)dlsym(handle, "rsAllocationCubeCreateFromBitmap");
170 if (RS::dispatch->AllocationCubeCreateFromBitmap == NULL) {
Tim Murray87c9d772013-12-03 12:42:00 -0800171 ALOGV("Couldn't initialize RS::dispatch->AllocationCubeCreateFromBitmap");
Tim Murray0b8a2be2013-07-23 16:25:41 -0700172 return false;
173 }
174 RS::dispatch->AllocationGetSurface = (AllocationGetSurfaceFnPtr)dlsym(handle, "rsAllocationGetSurface");
175 if (RS::dispatch->AllocationGetSurface == NULL) {
Tim Murray87c9d772013-12-03 12:42:00 -0800176 ALOGV("Couldn't initialize RS::dispatch->AllocationGetSurface");
Tim Murray0b8a2be2013-07-23 16:25:41 -0700177 return false;
178 }
179 RS::dispatch->AllocationSetSurface = (AllocationSetSurfaceFnPtr)dlsym(handle, "rsAllocationSetSurface");
180 if (RS::dispatch->AllocationSetSurface == NULL) {
Tim Murray87c9d772013-12-03 12:42:00 -0800181 ALOGV("Couldn't initialize RS::dispatch->AllocationSetSurface");
Tim Murray0b8a2be2013-07-23 16:25:41 -0700182 return false;
183 }
184 RS::dispatch->ContextFinish = (ContextFinishFnPtr)dlsym(handle, "rsContextFinish");
185 if (RS::dispatch->ContextFinish == NULL) {
Tim Murray87c9d772013-12-03 12:42:00 -0800186 ALOGV("Couldn't initialize RS::dispatch->ContextFinish");
Tim Murray0b8a2be2013-07-23 16:25:41 -0700187 return false;
188 }
189 RS::dispatch->ContextDump = (ContextDumpFnPtr)dlsym(handle, "rsContextDump");
190 if (RS::dispatch->ContextDump == NULL) {
Tim Murray87c9d772013-12-03 12:42:00 -0800191 ALOGV("Couldn't initialize RS::dispatch->ContextDump");
Tim Murray0b8a2be2013-07-23 16:25:41 -0700192 return false;
193 }
194 RS::dispatch->ContextSetPriority = (ContextSetPriorityFnPtr)dlsym(handle, "rsContextSetPriority");
195 if (RS::dispatch->ContextSetPriority == NULL) {
Tim Murray87c9d772013-12-03 12:42:00 -0800196 ALOGV("Couldn't initialize RS::dispatch->ContextSetPriority");
Tim Murray0b8a2be2013-07-23 16:25:41 -0700197 return false;
198 }
199 RS::dispatch->AssignName = (AssignNameFnPtr)dlsym(handle, "rsAssignName");
200 if (RS::dispatch->AssignName == NULL) {
Tim Murray87c9d772013-12-03 12:42:00 -0800201 ALOGV("Couldn't initialize RS::dispatch->AssignName");
Tim Murray0b8a2be2013-07-23 16:25:41 -0700202 return false;
203 }
204 RS::dispatch->ObjDestroy = (ObjDestroyFnPtr)dlsym(handle, "rsObjDestroy");
205 if (RS::dispatch->ObjDestroy == NULL) {
Tim Murray87c9d772013-12-03 12:42:00 -0800206 ALOGV("Couldn't initialize RS::dispatch->ObjDestroy");
Tim Murray0b8a2be2013-07-23 16:25:41 -0700207 return false;
208 }
209 RS::dispatch->ElementCreate = (ElementCreateFnPtr)dlsym(handle, "rsElementCreate");
210 if (RS::dispatch->ElementCreate == NULL) {
Tim Murray87c9d772013-12-03 12:42:00 -0800211 ALOGV("Couldn't initialize RS::dispatch->ElementCreate");
Tim Murray0b8a2be2013-07-23 16:25:41 -0700212 return false;
213 }
214 RS::dispatch->ElementCreate2 = (ElementCreate2FnPtr)dlsym(handle, "rsElementCreate2");
215 if (RS::dispatch->ElementCreate2 == NULL) {
Tim Murray87c9d772013-12-03 12:42:00 -0800216 ALOGV("Couldn't initialize RS::dispatch->ElementCreate2");
Tim Murray0b8a2be2013-07-23 16:25:41 -0700217 return false;
218 }
219 RS::dispatch->AllocationCopyToBitmap = (AllocationCopyToBitmapFnPtr)dlsym(handle, "rsAllocationCopyToBitmap");
220 if (RS::dispatch->AllocationCopyToBitmap == NULL) {
Tim Murray87c9d772013-12-03 12:42:00 -0800221 ALOGV("Couldn't initialize RS::dispatch->AllocationCopyToBitmap");
Tim Murray0b8a2be2013-07-23 16:25:41 -0700222 return false;
223 }
224 RS::dispatch->Allocation1DData = (Allocation1DDataFnPtr)dlsym(handle, "rsAllocation1DData");
225 if (RS::dispatch->Allocation1DData == NULL) {
Tim Murray87c9d772013-12-03 12:42:00 -0800226 ALOGV("Couldn't initialize RS::dispatch->Allocation1DData");
Tim Murray0b8a2be2013-07-23 16:25:41 -0700227 return false;
228 }
229 RS::dispatch->Allocation1DElementData = (Allocation1DElementDataFnPtr)dlsym(handle, "rsAllocation1DElementData");
230 if (RS::dispatch->Allocation1DElementData == NULL) {
Tim Murray87c9d772013-12-03 12:42:00 -0800231 ALOGV("Couldn't initialize RS::dispatch->Allocation1DElementData");
Tim Murray0b8a2be2013-07-23 16:25:41 -0700232 return false;
233 }
234 RS::dispatch->Allocation2DData = (Allocation2DDataFnPtr)dlsym(handle, "rsAllocation2DData");
235 if (RS::dispatch->Allocation2DData == NULL) {
Tim Murray87c9d772013-12-03 12:42:00 -0800236 ALOGV("Couldn't initialize RS::dispatch->Allocation2DData");
Tim Murray0b8a2be2013-07-23 16:25:41 -0700237 return false;
238 }
239 RS::dispatch->Allocation3DData = (Allocation3DDataFnPtr)dlsym(handle, "rsAllocation3DData");
240 if (RS::dispatch->Allocation3DData == NULL) {
Tim Murray87c9d772013-12-03 12:42:00 -0800241 ALOGV("Couldn't initialize RS::dispatch->Allocation3DData");
Tim Murray0b8a2be2013-07-23 16:25:41 -0700242 return false;
243 }
244 RS::dispatch->AllocationGenerateMipmaps = (AllocationGenerateMipmapsFnPtr)dlsym(handle, "rsAllocationGenerateMipmaps");
245 if (RS::dispatch->AllocationGenerateMipmaps == NULL) {
Tim Murray87c9d772013-12-03 12:42:00 -0800246 ALOGV("Couldn't initialize RS::dispatch->AllocationGenerateMipmaps");
Tim Murray0b8a2be2013-07-23 16:25:41 -0700247 return false;
248 }
249 RS::dispatch->AllocationRead = (AllocationReadFnPtr)dlsym(handle, "rsAllocationRead");
250 if (RS::dispatch->AllocationRead == NULL) {
Tim Murray87c9d772013-12-03 12:42:00 -0800251 ALOGV("Couldn't initialize RS::dispatch->AllocationRead");
Tim Murray0b8a2be2013-07-23 16:25:41 -0700252 return false;
253 }
254 RS::dispatch->Allocation1DRead = (Allocation1DReadFnPtr)dlsym(handle, "rsAllocation1DRead");
255 if (RS::dispatch->Allocation1DRead == NULL) {
Tim Murray87c9d772013-12-03 12:42:00 -0800256 ALOGV("Couldn't initialize RS::dispatch->Allocation1DRead");
Tim Murray0b8a2be2013-07-23 16:25:41 -0700257 return false;
258 }
259 RS::dispatch->Allocation2DRead = (Allocation2DReadFnPtr)dlsym(handle, "rsAllocation2DRead");
260 if (RS::dispatch->Allocation2DRead == NULL) {
Tim Murray87c9d772013-12-03 12:42:00 -0800261 ALOGV("Couldn't initialize RS::dispatch->Allocation2DRead");
Tim Murray0b8a2be2013-07-23 16:25:41 -0700262 return false;
263 }
264 RS::dispatch->AllocationSyncAll = (AllocationSyncAllFnPtr)dlsym(handle, "rsAllocationSyncAll");
265 if (RS::dispatch->AllocationSyncAll == NULL) {
Tim Murray87c9d772013-12-03 12:42:00 -0800266 ALOGV("Couldn't initialize RS::dispatch->AllocationSyncAll");
Tim Murray0b8a2be2013-07-23 16:25:41 -0700267 return false;
268 }
269 RS::dispatch->AllocationResize1D = (AllocationResize1DFnPtr)dlsym(handle, "rsAllocationResize1D");
270 if (RS::dispatch->AllocationResize1D == NULL) {
Tim Murray87c9d772013-12-03 12:42:00 -0800271 ALOGV("Couldn't initialize RS::dispatch->AllocationResize1D");
Tim Murray0b8a2be2013-07-23 16:25:41 -0700272 return false;
273 }
274 RS::dispatch->AllocationCopy2DRange = (AllocationCopy2DRangeFnPtr)dlsym(handle, "rsAllocationCopy2DRange");
275 if (RS::dispatch->AllocationCopy2DRange == NULL) {
Tim Murray87c9d772013-12-03 12:42:00 -0800276 ALOGV("Couldn't initialize RS::dispatch->AllocationCopy2DRange");
Tim Murray0b8a2be2013-07-23 16:25:41 -0700277 return false;
278 }
279 RS::dispatch->AllocationCopy3DRange = (AllocationCopy3DRangeFnPtr)dlsym(handle, "rsAllocationCopy3DRange");
280 if (RS::dispatch->AllocationCopy3DRange == NULL) {
Tim Murray87c9d772013-12-03 12:42:00 -0800281 ALOGV("Couldn't initialize RS::dispatch->AllocationCopy3DRange");
Tim Murray0b8a2be2013-07-23 16:25:41 -0700282 return false;
283 }
284 RS::dispatch->SamplerCreate = (SamplerCreateFnPtr)dlsym(handle, "rsSamplerCreate");
285 if (RS::dispatch->SamplerCreate == NULL) {
Tim Murray87c9d772013-12-03 12:42:00 -0800286 ALOGV("Couldn't initialize RS::dispatch->SamplerCreate");
Tim Murray0b8a2be2013-07-23 16:25:41 -0700287 return false;
288 }
289 RS::dispatch->ScriptBindAllocation = (ScriptBindAllocationFnPtr)dlsym(handle, "rsScriptBindAllocation");
290 if (RS::dispatch->ScriptBindAllocation == NULL) {
Tim Murray87c9d772013-12-03 12:42:00 -0800291 ALOGV("Couldn't initialize RS::dispatch->ScriptBindAllocation");
Tim Murray0b8a2be2013-07-23 16:25:41 -0700292 return false;
293 }
294 RS::dispatch->ScriptSetTimeZone = (ScriptSetTimeZoneFnPtr)dlsym(handle, "rsScriptSetTimeZone");
295 if (RS::dispatch->ScriptSetTimeZone == NULL) {
Tim Murray87c9d772013-12-03 12:42:00 -0800296 ALOGV("Couldn't initialize RS::dispatch->ScriptSetTimeZone");
Tim Murray0b8a2be2013-07-23 16:25:41 -0700297 return false;
298 }
299 RS::dispatch->ScriptInvoke = (ScriptInvokeFnPtr)dlsym(handle, "rsScriptInvoke");
300 if (RS::dispatch->ScriptInvoke == NULL) {
Tim Murray87c9d772013-12-03 12:42:00 -0800301 ALOGV("Couldn't initialize RS::dispatch->ScriptInvoke");
Tim Murray0b8a2be2013-07-23 16:25:41 -0700302 return false;
303 }
304 RS::dispatch->ScriptInvokeV = (ScriptInvokeVFnPtr)dlsym(handle, "rsScriptInvokeV");
305 if (RS::dispatch->ScriptInvokeV == NULL) {
Tim Murray87c9d772013-12-03 12:42:00 -0800306 ALOGV("Couldn't initialize RS::dispatch->ScriptInvokeV");
Tim Murray0b8a2be2013-07-23 16:25:41 -0700307 return false;
308 }
309 RS::dispatch->ScriptForEach = (ScriptForEachFnPtr)dlsym(handle, "rsScriptForEach");
310 if (RS::dispatch->ScriptForEach == NULL) {
Tim Murray87c9d772013-12-03 12:42:00 -0800311 ALOGV("Couldn't initialize RS::dispatch->ScriptForEach");
Tim Murray0b8a2be2013-07-23 16:25:41 -0700312 return false;
313 }
314 RS::dispatch->ScriptSetVarI = (ScriptSetVarIFnPtr)dlsym(handle, "rsScriptSetVarI");
315 if (RS::dispatch->ScriptSetVarI == NULL) {
Tim Murray87c9d772013-12-03 12:42:00 -0800316 ALOGV("Couldn't initialize RS::dispatch->ScriptSetVarI");
Tim Murray0b8a2be2013-07-23 16:25:41 -0700317 return false;
318 }
319 RS::dispatch->ScriptSetVarObj = (ScriptSetVarObjFnPtr)dlsym(handle, "rsScriptSetVarObj");
320 if (RS::dispatch->ScriptSetVarObj == NULL) {
Tim Murray87c9d772013-12-03 12:42:00 -0800321 ALOGV("Couldn't initialize RS::dispatch->ScriptSetVarObj");
Tim Murray0b8a2be2013-07-23 16:25:41 -0700322 return false;
323 }
324 RS::dispatch->ScriptSetVarJ = (ScriptSetVarJFnPtr)dlsym(handle, "rsScriptSetVarJ");
325 if (RS::dispatch->ScriptSetVarJ == NULL) {
Tim Murray87c9d772013-12-03 12:42:00 -0800326 ALOGV("Couldn't initialize RS::dispatch->ScriptSetVarJ");
Tim Murray0b8a2be2013-07-23 16:25:41 -0700327 return false;
328 }
329 RS::dispatch->ScriptSetVarF = (ScriptSetVarFFnPtr)dlsym(handle, "rsScriptSetVarF");
330 if (RS::dispatch->ScriptSetVarF == NULL) {
Tim Murray87c9d772013-12-03 12:42:00 -0800331 ALOGV("Couldn't initialize RS::dispatch->ScriptSetVarF");
Tim Murray0b8a2be2013-07-23 16:25:41 -0700332 return false;
333 }
334 RS::dispatch->ScriptSetVarD = (ScriptSetVarDFnPtr)dlsym(handle, "rsScriptSetVarD");
335 if (RS::dispatch->ScriptSetVarD == NULL) {
Tim Murray87c9d772013-12-03 12:42:00 -0800336 ALOGV("Couldn't initialize RS::dispatch->ScriptSetVarD");
Tim Murray0b8a2be2013-07-23 16:25:41 -0700337 return false;
338 }
339 RS::dispatch->ScriptSetVarV = (ScriptSetVarVFnPtr)dlsym(handle, "rsScriptSetVarV");
340 if (RS::dispatch->ScriptSetVarV == NULL) {
Tim Murray87c9d772013-12-03 12:42:00 -0800341 ALOGV("Couldn't initialize RS::dispatch->ScriptSetVarV");
Tim Murray0b8a2be2013-07-23 16:25:41 -0700342 return false;
343 }
344 RS::dispatch->ScriptGetVarV = (ScriptGetVarVFnPtr)dlsym(handle, "rsScriptGetVarV");
345 if (RS::dispatch->ScriptGetVarV == NULL) {
Tim Murray87c9d772013-12-03 12:42:00 -0800346 ALOGV("Couldn't initialize RS::dispatch->ScriptGetVarV");
Tim Murray0b8a2be2013-07-23 16:25:41 -0700347 return false;
348 }
349 RS::dispatch->ScriptSetVarVE = (ScriptSetVarVEFnPtr)dlsym(handle, "rsScriptSetVarVE");
350 if (RS::dispatch->ScriptSetVarVE == NULL) {
Tim Murray87c9d772013-12-03 12:42:00 -0800351 ALOGV("Couldn't initialize RS::dispatch->ScriptSetVarVE");
Tim Murray0b8a2be2013-07-23 16:25:41 -0700352 return false;
353 }
354 RS::dispatch->ScriptCCreate = (ScriptCCreateFnPtr)dlsym(handle, "rsScriptCCreate");
355 if (RS::dispatch->ScriptCCreate == NULL) {
Tim Murray87c9d772013-12-03 12:42:00 -0800356 ALOGV("Couldn't initialize RS::dispatch->ScriptCCreate");
Tim Murray0b8a2be2013-07-23 16:25:41 -0700357 return false;
358 }
359 RS::dispatch->ScriptIntrinsicCreate = (ScriptIntrinsicCreateFnPtr)dlsym(handle, "rsScriptIntrinsicCreate");
360 if (RS::dispatch->ScriptIntrinsicCreate == NULL) {
Tim Murray87c9d772013-12-03 12:42:00 -0800361 ALOGV("Couldn't initialize RS::dispatch->ScriptIntrinsicCreate");
Tim Murray0b8a2be2013-07-23 16:25:41 -0700362 return false;
363 }
364 RS::dispatch->ScriptKernelIDCreate = (ScriptKernelIDCreateFnPtr)dlsym(handle, "rsScriptKernelIDCreate");
365 if (RS::dispatch->ScriptKernelIDCreate == NULL) {
Tim Murray87c9d772013-12-03 12:42:00 -0800366 ALOGV("Couldn't initialize RS::dispatch->ScriptKernelIDCreate");
Tim Murray0b8a2be2013-07-23 16:25:41 -0700367 return false;
368 }
369 RS::dispatch->ScriptFieldIDCreate = (ScriptFieldIDCreateFnPtr)dlsym(handle, "rsScriptFieldIDCreate");
370 if (RS::dispatch->ScriptFieldIDCreate == NULL) {
Tim Murray87c9d772013-12-03 12:42:00 -0800371 ALOGV("Couldn't initialize RS::dispatch->ScriptFieldIDCreate");
Tim Murray0b8a2be2013-07-23 16:25:41 -0700372 return false;
373 }
374 RS::dispatch->ScriptGroupCreate = (ScriptGroupCreateFnPtr)dlsym(handle, "rsScriptGroupCreate");
375 if (RS::dispatch->ScriptGroupCreate == NULL) {
Tim Murray87c9d772013-12-03 12:42:00 -0800376 ALOGV("Couldn't initialize RS::dispatch->ScriptGroupCreate");
Tim Murray0b8a2be2013-07-23 16:25:41 -0700377 return false;
378 }
379 RS::dispatch->ScriptGroupSetOutput = (ScriptGroupSetOutputFnPtr)dlsym(handle, "rsScriptGroupSetOutput");
380 if (RS::dispatch->ScriptGroupSetOutput == NULL) {
Tim Murray87c9d772013-12-03 12:42:00 -0800381 ALOGV("Couldn't initialize RS::dispatch->ScriptGroupSetOutput");
Tim Murray0b8a2be2013-07-23 16:25:41 -0700382 return false;
383 }
384 RS::dispatch->ScriptGroupSetInput = (ScriptGroupSetInputFnPtr)dlsym(handle, "rsScriptGroupSetInput");
385 if (RS::dispatch->ScriptGroupSetInput == NULL) {
Tim Murray87c9d772013-12-03 12:42:00 -0800386 ALOGV("Couldn't initialize RS::dispatch->ScriptGroupSetInput");
Tim Murray0b8a2be2013-07-23 16:25:41 -0700387 return false;
388 }
389 RS::dispatch->ScriptGroupExecute = (ScriptGroupExecuteFnPtr)dlsym(handle, "rsScriptGroupExecute");
390 if (RS::dispatch->ScriptGroupExecute == NULL) {
Tim Murray87c9d772013-12-03 12:42:00 -0800391 ALOGV("Couldn't initialize RS::dispatch->ScriptGroupExecute");
Tim Murray0b8a2be2013-07-23 16:25:41 -0700392 return false;
393 }
394 RS::dispatch->AllocationIoSend = (AllocationIoSendFnPtr)dlsym(handle, "rsAllocationIoSend");
395 if (RS::dispatch->AllocationIoSend == NULL) {
Tim Murray87c9d772013-12-03 12:42:00 -0800396 ALOGV("Couldn't initialize RS::dispatch->AllocationIoSend");
Tim Murray0b8a2be2013-07-23 16:25:41 -0700397 return false;
398 }
399 RS::dispatch->AllocationIoReceive = (AllocationIoReceiveFnPtr)dlsym(handle, "rsAllocationIoReceive");
400 if (RS::dispatch->AllocationIoReceive == NULL) {
Tim Murray87c9d772013-12-03 12:42:00 -0800401 ALOGV("Couldn't initialize RS::dispatch->AllocationIoReceive");
Tim Murray0b8a2be2013-07-23 16:25:41 -0700402 return false;
403 }
404
405 return true;
406}
407
Tim Murray75e877d2013-09-11 14:45:20 -0700408// this will only open API 19+ libRS
409// because that's when we changed libRS to extern "C" entry points
Tim Murray4a92d122013-07-22 10:56:18 -0700410static bool loadSO(const char* filename) {
411 void* handle = dlopen(filename, RTLD_LAZY | RTLD_LOCAL);
412 if (handle == NULL) {
Tim Murray87c9d772013-12-03 12:42:00 -0800413 ALOGV("couldn't dlopen %s, %s", filename, dlerror());
Tim Murray4a92d122013-07-22 10:56:18 -0700414 return false;
415 }
Tim Murraya4230962013-07-17 16:50:10 -0700416
Tim Murray4a92d122013-07-22 10:56:18 -0700417 if (loadSymbols(handle) == false) {
Tim Murray87c9d772013-12-03 12:42:00 -0800418 ALOGV("%s init failed!", filename);
Tim Murray4a92d122013-07-22 10:56:18 -0700419 return false;
420 }
Tim Murray84e3dea2013-09-09 16:12:51 -0700421 //ALOGE("Successfully loaded %s", filename);
Tim Murray4a92d122013-07-22 10:56:18 -0700422 return true;
423}
424
425static uint32_t getProp(const char *str) {
426#if !defined(RS_SERVER) && defined(HAVE_ANDROID_OS)
427 char buf[256];
428 property_get(str, buf, "0");
429 return atoi(buf);
430#else
431 return 0;
432#endif
433}
434
435bool RS::initDispatch(int targetApi) {
Tim Murraya4230962013-07-17 16:50:10 -0700436 pthread_mutex_lock(&gInitMutex);
437 if (gInitError) {
438 goto error;
439 } else if (gInitialized) {
Tim Murray47666f52013-07-29 14:32:34 -0700440 pthread_mutex_unlock(&gInitMutex);
Tim Murraya4230962013-07-17 16:50:10 -0700441 return true;
442 }
Tim Murraya4230962013-07-17 16:50:10 -0700443
444 RS::dispatch = new dispatchTable;
Tim Murray4a92d122013-07-22 10:56:18 -0700445
446 // attempt to load libRS, load libRSSupport on failure
447 // if property is set, proceed directly to libRSSupport
448 if (getProp("debug.rs.forcecompat") == 0) {
449 usingNative = loadSO("libRS.so");
450 }
451 if (usingNative == false) {
452 if (loadSO("libRSSupport.so") == false) {
453 ALOGE("Failed to load libRS.so and libRSSupport.so");
454 goto error;
455 }
Tim Murraya4230962013-07-17 16:50:10 -0700456 }
457
458 gInitialized = true;
459
460 pthread_mutex_unlock(&gInitMutex);
461 return true;
462
463 error:
464 gInitError = 1;
465 pthread_mutex_unlock(&gInitMutex);
466 return false;
467}
468
Tim Murraycaf41262013-12-13 12:54:37 -0800469bool RS::init(std::string &name, int targetApi, uint32_t flags) {
470 if (mInit) {
471 return true;
472 }
473
Tim Murraya4230962013-07-17 16:50:10 -0700474 if (initDispatch(targetApi) == false) {
475 ALOGE("Couldn't initialize dispatch table");
476 return false;
477 }
478
Tim Murraycaf41262013-12-13 12:54:37 -0800479 mCacheDir = name;
Tim Murraycaf41262013-12-13 12:54:37 -0800480
Tim Murraya4230962013-07-17 16:50:10 -0700481 mDev = RS::dispatch->DeviceCreate();
Jason Sams221a4b12012-02-22 15:22:41 -0800482 if (mDev == 0) {
483 ALOGE("Device creation failed");
484 return false;
485 }
486
Tim Murray84e3dea2013-09-09 16:12:51 -0700487 if (flags >= RS_CONTEXT_MAX) {
488 ALOGE("Invalid flags passed");
489 return false;
490 }
491
492 mContext = RS::dispatch->ContextCreate(mDev, 0, targetApi, RS_CONTEXT_TYPE_NORMAL, flags);
Jason Sams221a4b12012-02-22 15:22:41 -0800493 if (mContext == 0) {
494 ALOGE("Context creation failed");
495 return false;
496 }
497
Jason Sams221a4b12012-02-22 15:22:41 -0800498 pid_t mNativeMessageThreadId;
499
500 int status = pthread_create(&mMessageThreadId, NULL, threadProc, this);
501 if (status) {
Tim Murray84bf2b82012-10-31 16:03:16 -0700502 ALOGE("Failed to start RS message thread.");
Jason Sams221a4b12012-02-22 15:22:41 -0800503 return false;
504 }
505 // Wait for the message thread to be active.
506 while (!mMessageRun) {
507 usleep(1000);
508 }
509
Tim Murraya4230962013-07-17 16:50:10 -0700510 mInit = true;
511
Jason Sams221a4b12012-02-22 15:22:41 -0800512 return true;
513}
514
Tim Murray21fa7a02013-08-15 16:25:03 -0700515void RS::throwError(RSError error, const char *errMsg) {
516 if (mCurrentError == RS_SUCCESS) {
517 mCurrentError = error;
518 ALOGE("RS CPP error: %s", errMsg);
519 } else {
520 ALOGE("RS CPP error (masked by previous error): %s", errMsg);
521 }
Jason Samsb2e3dc52012-02-23 17:14:39 -0800522}
523
Tim Murray10913a52013-08-20 17:19:47 -0700524RSError RS::getError() {
525 return mCurrentError;
526}
527
Jason Samsb2e3dc52012-02-23 17:14:39 -0800528
Tim Murray84bf2b82012-10-31 16:03:16 -0700529void * RS::threadProc(void *vrsc) {
530 RS *rs = static_cast<RS *>(vrsc);
Jason Sams221a4b12012-02-22 15:22:41 -0800531 size_t rbuf_size = 256;
532 void * rbuf = malloc(rbuf_size);
533
Tim Murraya4230962013-07-17 16:50:10 -0700534 RS::dispatch->ContextInitToClient(rs->mContext);
Jason Sams221a4b12012-02-22 15:22:41 -0800535 rs->mMessageRun = true;
536
537 while (rs->mMessageRun) {
538 size_t receiveLen = 0;
539 uint32_t usrID = 0;
540 uint32_t subID = 0;
Tim Murraya4230962013-07-17 16:50:10 -0700541 RsMessageToClientType r = RS::dispatch->ContextPeekMessage(rs->mContext,
542 &receiveLen, sizeof(receiveLen),
543 &usrID, sizeof(usrID));
Jason Sams221a4b12012-02-22 15:22:41 -0800544
545 if (receiveLen >= rbuf_size) {
546 rbuf_size = receiveLen + 32;
547 rbuf = realloc(rbuf, rbuf_size);
548 }
549 if (!rbuf) {
Tim Murray84bf2b82012-10-31 16:03:16 -0700550 ALOGE("RS::message handler realloc error %zu", rbuf_size);
Jason Sams221a4b12012-02-22 15:22:41 -0800551 // No clean way to recover now?
552 }
Tim Murraya4230962013-07-17 16:50:10 -0700553 RS::dispatch->ContextGetMessage(rs->mContext, rbuf, rbuf_size, &receiveLen, sizeof(receiveLen),
Jason Sams221a4b12012-02-22 15:22:41 -0800554 &subID, sizeof(subID));
555
556 switch(r) {
557 case RS_MESSAGE_TO_CLIENT_ERROR:
558 ALOGE("RS Error %s", (const char *)rbuf);
Tim Murray21fa7a02013-08-15 16:25:03 -0700559 rs->throwError(RS_ERROR_RUNTIME_ERROR, "Error returned from runtime");
Jason Sams221a4b12012-02-22 15:22:41 -0800560 if(rs->mMessageFunc != NULL) {
561 rs->mErrorFunc(usrID, (const char *)rbuf);
562 }
563 break;
Stephen Hines76a1be42012-11-26 16:26:03 -0800564 case RS_MESSAGE_TO_CLIENT_NONE:
Jason Sams221a4b12012-02-22 15:22:41 -0800565 case RS_MESSAGE_TO_CLIENT_EXCEPTION:
Stephen Hines76a1be42012-11-26 16:26:03 -0800566 case RS_MESSAGE_TO_CLIENT_RESIZE:
Jason Sams221a4b12012-02-22 15:22:41 -0800567 // teardown. But we want to avoid starving other threads during
568 // teardown by yielding until the next line in the destructor can
Stephen Hines76a1be42012-11-26 16:26:03 -0800569 // execute to set mRun = false. Note that the FIFO sends an
570 // empty NONE message when it reaches its destructor.
Jason Sams221a4b12012-02-22 15:22:41 -0800571 usleep(1000);
572 break;
573 case RS_MESSAGE_TO_CLIENT_USER:
574 if(rs->mMessageFunc != NULL) {
575 rs->mMessageFunc(usrID, rbuf, receiveLen);
576 } else {
577 ALOGE("Received a message from the script with no message handler installed.");
578 }
579 break;
580
581 default:
Tim Murray84bf2b82012-10-31 16:03:16 -0700582 ALOGE("RS unknown message type %i", r);
Jason Sams221a4b12012-02-22 15:22:41 -0800583 }
584 }
585
586 if (rbuf) {
587 free(rbuf);
588 }
Tim Murray87c9d772013-12-03 12:42:00 -0800589 ALOGV("RS Message thread exiting.");
Jason Sams221a4b12012-02-22 15:22:41 -0800590 return NULL;
591}
592
Tim Murray84bf2b82012-10-31 16:03:16 -0700593void RS::setErrorHandler(ErrorHandlerFunc_t func) {
Jason Sams221a4b12012-02-22 15:22:41 -0800594 mErrorFunc = func;
595}
596
Tim Murray84bf2b82012-10-31 16:03:16 -0700597void RS::setMessageHandler(MessageHandlerFunc_t func) {
Jason Sams221a4b12012-02-22 15:22:41 -0800598 mMessageFunc = func;
599}
Tim Murraybaca6c32012-11-14 16:51:46 -0800600
601void RS::finish() {
Tim Murraya4230962013-07-17 16:50:10 -0700602 RS::dispatch->ContextFinish(mContext);
Tim Murraybaca6c32012-11-14 16:51:46 -0800603}