blob: 5709f79978a9a8b02070deb9dd5bb8a40ed45d81 [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 }
Jason Samsb8a94e22014-02-24 17:52:32 -0800404 RS::dispatch->AllocationGetPointer = (AllocationGetPointerFnPtr)dlsym(handle, "rsAllocationGetPointer");
405 if (RS::dispatch->AllocationGetPointer == NULL) {
406 ALOGV("Couldn't initialize RS::dispatch->AllocationGetPointer");
407 //return false;
408 }
Tim Murray0b8a2be2013-07-23 16:25:41 -0700409
410 return true;
411}
412
Tim Murray75e877d2013-09-11 14:45:20 -0700413// this will only open API 19+ libRS
414// because that's when we changed libRS to extern "C" entry points
Tim Murray4a92d122013-07-22 10:56:18 -0700415static bool loadSO(const char* filename) {
416 void* handle = dlopen(filename, RTLD_LAZY | RTLD_LOCAL);
417 if (handle == NULL) {
Tim Murray87c9d772013-12-03 12:42:00 -0800418 ALOGV("couldn't dlopen %s, %s", filename, dlerror());
Tim Murray4a92d122013-07-22 10:56:18 -0700419 return false;
420 }
Tim Murraya4230962013-07-17 16:50:10 -0700421
Tim Murray4a92d122013-07-22 10:56:18 -0700422 if (loadSymbols(handle) == false) {
Tim Murray87c9d772013-12-03 12:42:00 -0800423 ALOGV("%s init failed!", filename);
Tim Murray4a92d122013-07-22 10:56:18 -0700424 return false;
425 }
Tim Murray84e3dea2013-09-09 16:12:51 -0700426 //ALOGE("Successfully loaded %s", filename);
Tim Murray4a92d122013-07-22 10:56:18 -0700427 return true;
428}
429
430static uint32_t getProp(const char *str) {
431#if !defined(RS_SERVER) && defined(HAVE_ANDROID_OS)
432 char buf[256];
433 property_get(str, buf, "0");
434 return atoi(buf);
435#else
436 return 0;
437#endif
438}
439
440bool RS::initDispatch(int targetApi) {
Tim Murraya4230962013-07-17 16:50:10 -0700441 pthread_mutex_lock(&gInitMutex);
442 if (gInitError) {
443 goto error;
444 } else if (gInitialized) {
Tim Murray47666f52013-07-29 14:32:34 -0700445 pthread_mutex_unlock(&gInitMutex);
Tim Murraya4230962013-07-17 16:50:10 -0700446 return true;
447 }
Tim Murraya4230962013-07-17 16:50:10 -0700448
449 RS::dispatch = new dispatchTable;
Tim Murray4a92d122013-07-22 10:56:18 -0700450
451 // attempt to load libRS, load libRSSupport on failure
452 // if property is set, proceed directly to libRSSupport
453 if (getProp("debug.rs.forcecompat") == 0) {
454 usingNative = loadSO("libRS.so");
455 }
456 if (usingNative == false) {
457 if (loadSO("libRSSupport.so") == false) {
458 ALOGE("Failed to load libRS.so and libRSSupport.so");
459 goto error;
460 }
Tim Murraya4230962013-07-17 16:50:10 -0700461 }
462
463 gInitialized = true;
464
465 pthread_mutex_unlock(&gInitMutex);
466 return true;
467
468 error:
469 gInitError = 1;
470 pthread_mutex_unlock(&gInitMutex);
471 return false;
472}
473
Tim Murraycaf41262013-12-13 12:54:37 -0800474bool RS::init(std::string &name, int targetApi, uint32_t flags) {
475 if (mInit) {
476 return true;
477 }
478
Tim Murraya4230962013-07-17 16:50:10 -0700479 if (initDispatch(targetApi) == false) {
480 ALOGE("Couldn't initialize dispatch table");
481 return false;
482 }
483
Tim Murraycaf41262013-12-13 12:54:37 -0800484 mCacheDir = name;
Tim Murraycaf41262013-12-13 12:54:37 -0800485
Tim Murraya4230962013-07-17 16:50:10 -0700486 mDev = RS::dispatch->DeviceCreate();
Jason Sams221a4b12012-02-22 15:22:41 -0800487 if (mDev == 0) {
488 ALOGE("Device creation failed");
489 return false;
490 }
491
Tim Murray84e3dea2013-09-09 16:12:51 -0700492 if (flags >= RS_CONTEXT_MAX) {
493 ALOGE("Invalid flags passed");
494 return false;
495 }
496
497 mContext = RS::dispatch->ContextCreate(mDev, 0, targetApi, RS_CONTEXT_TYPE_NORMAL, flags);
Jason Sams221a4b12012-02-22 15:22:41 -0800498 if (mContext == 0) {
499 ALOGE("Context creation failed");
500 return false;
501 }
502
Jason Sams221a4b12012-02-22 15:22:41 -0800503 pid_t mNativeMessageThreadId;
504
505 int status = pthread_create(&mMessageThreadId, NULL, threadProc, this);
506 if (status) {
Tim Murray84bf2b82012-10-31 16:03:16 -0700507 ALOGE("Failed to start RS message thread.");
Jason Sams221a4b12012-02-22 15:22:41 -0800508 return false;
509 }
510 // Wait for the message thread to be active.
511 while (!mMessageRun) {
512 usleep(1000);
513 }
514
Tim Murraya4230962013-07-17 16:50:10 -0700515 mInit = true;
516
Jason Sams221a4b12012-02-22 15:22:41 -0800517 return true;
518}
519
Tim Murray21fa7a02013-08-15 16:25:03 -0700520void RS::throwError(RSError error, const char *errMsg) {
521 if (mCurrentError == RS_SUCCESS) {
522 mCurrentError = error;
523 ALOGE("RS CPP error: %s", errMsg);
524 } else {
525 ALOGE("RS CPP error (masked by previous error): %s", errMsg);
526 }
Jason Samsb2e3dc52012-02-23 17:14:39 -0800527}
528
Tim Murray10913a52013-08-20 17:19:47 -0700529RSError RS::getError() {
530 return mCurrentError;
531}
532
Jason Samsb2e3dc52012-02-23 17:14:39 -0800533
Tim Murray84bf2b82012-10-31 16:03:16 -0700534void * RS::threadProc(void *vrsc) {
535 RS *rs = static_cast<RS *>(vrsc);
Jason Sams221a4b12012-02-22 15:22:41 -0800536 size_t rbuf_size = 256;
537 void * rbuf = malloc(rbuf_size);
538
Tim Murraya4230962013-07-17 16:50:10 -0700539 RS::dispatch->ContextInitToClient(rs->mContext);
Jason Sams221a4b12012-02-22 15:22:41 -0800540 rs->mMessageRun = true;
541
542 while (rs->mMessageRun) {
543 size_t receiveLen = 0;
544 uint32_t usrID = 0;
545 uint32_t subID = 0;
Tim Murraya4230962013-07-17 16:50:10 -0700546 RsMessageToClientType r = RS::dispatch->ContextPeekMessage(rs->mContext,
547 &receiveLen, sizeof(receiveLen),
548 &usrID, sizeof(usrID));
Jason Sams221a4b12012-02-22 15:22:41 -0800549
550 if (receiveLen >= rbuf_size) {
551 rbuf_size = receiveLen + 32;
552 rbuf = realloc(rbuf, rbuf_size);
553 }
554 if (!rbuf) {
Tim Murray84bf2b82012-10-31 16:03:16 -0700555 ALOGE("RS::message handler realloc error %zu", rbuf_size);
Jason Sams221a4b12012-02-22 15:22:41 -0800556 // No clean way to recover now?
557 }
Tim Murraya4230962013-07-17 16:50:10 -0700558 RS::dispatch->ContextGetMessage(rs->mContext, rbuf, rbuf_size, &receiveLen, sizeof(receiveLen),
Jason Sams221a4b12012-02-22 15:22:41 -0800559 &subID, sizeof(subID));
560
561 switch(r) {
562 case RS_MESSAGE_TO_CLIENT_ERROR:
563 ALOGE("RS Error %s", (const char *)rbuf);
Tim Murray21fa7a02013-08-15 16:25:03 -0700564 rs->throwError(RS_ERROR_RUNTIME_ERROR, "Error returned from runtime");
Jason Sams221a4b12012-02-22 15:22:41 -0800565 if(rs->mMessageFunc != NULL) {
566 rs->mErrorFunc(usrID, (const char *)rbuf);
567 }
568 break;
Stephen Hines76a1be42012-11-26 16:26:03 -0800569 case RS_MESSAGE_TO_CLIENT_NONE:
Jason Sams221a4b12012-02-22 15:22:41 -0800570 case RS_MESSAGE_TO_CLIENT_EXCEPTION:
Stephen Hines76a1be42012-11-26 16:26:03 -0800571 case RS_MESSAGE_TO_CLIENT_RESIZE:
Jason Sams221a4b12012-02-22 15:22:41 -0800572 // teardown. But we want to avoid starving other threads during
573 // teardown by yielding until the next line in the destructor can
Stephen Hines76a1be42012-11-26 16:26:03 -0800574 // execute to set mRun = false. Note that the FIFO sends an
575 // empty NONE message when it reaches its destructor.
Jason Sams221a4b12012-02-22 15:22:41 -0800576 usleep(1000);
577 break;
578 case RS_MESSAGE_TO_CLIENT_USER:
579 if(rs->mMessageFunc != NULL) {
580 rs->mMessageFunc(usrID, rbuf, receiveLen);
581 } else {
582 ALOGE("Received a message from the script with no message handler installed.");
583 }
584 break;
585
586 default:
Tim Murray84bf2b82012-10-31 16:03:16 -0700587 ALOGE("RS unknown message type %i", r);
Jason Sams221a4b12012-02-22 15:22:41 -0800588 }
589 }
590
591 if (rbuf) {
592 free(rbuf);
593 }
Tim Murray87c9d772013-12-03 12:42:00 -0800594 ALOGV("RS Message thread exiting.");
Jason Sams221a4b12012-02-22 15:22:41 -0800595 return NULL;
596}
597
Tim Murray84bf2b82012-10-31 16:03:16 -0700598void RS::setErrorHandler(ErrorHandlerFunc_t func) {
Jason Sams221a4b12012-02-22 15:22:41 -0800599 mErrorFunc = func;
600}
601
Tim Murray84bf2b82012-10-31 16:03:16 -0700602void RS::setMessageHandler(MessageHandlerFunc_t func) {
Jason Sams221a4b12012-02-22 15:22:41 -0800603 mMessageFunc = func;
604}
Tim Murraybaca6c32012-11-14 16:51:46 -0800605
606void RS::finish() {
Tim Murraya4230962013-07-17 16:50:10 -0700607 RS::dispatch->ContextFinish(mContext);
Tim Murraybaca6c32012-11-14 16:51:46 -0800608}