Jason Sams | 221a4b1 | 2012-02-22 15:22:41 -0800 | [diff] [blame] | 1 | /* |
Tim Murray | a423096 | 2013-07-17 16:50:10 -0700 | [diff] [blame] | 2 | * Copyright (C) 2013 The Android Open Source Project |
Jason Sams | 221a4b1 | 2012-02-22 15:22:41 -0800 | [diff] [blame] | 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
Jason Sams | 221a4b1 | 2012-02-22 15:22:41 -0800 | [diff] [blame] | 17 | #include <malloc.h> |
| 18 | #include <string.h> |
Tim Murray | 84bf2b8 | 2012-10-31 16:03:16 -0700 | [diff] [blame] | 19 | #include <pthread.h> |
Jason Sams | 221a4b1 | 2012-02-22 15:22:41 -0800 | [diff] [blame] | 20 | |
| 21 | #include "RenderScript.h" |
Tim Murray | 89daad6 | 2013-07-29 14:30:02 -0700 | [diff] [blame^] | 22 | #include "rsCppStructs.h" |
Jason Sams | 221a4b1 | 2012-02-22 15:22:41 -0800 | [diff] [blame] | 23 | |
Tim Murray | a423096 | 2013-07-17 16:50:10 -0700 | [diff] [blame] | 24 | #include <dlfcn.h> |
| 25 | |
Tim Murray | 4a92d12 | 2013-07-22 10:56:18 -0700 | [diff] [blame] | 26 | #if !defined(RS_SERVER) && defined(HAVE_ANDROID_OS) |
| 27 | #include <cutils/properties.h> |
| 28 | #endif |
| 29 | |
Tim Murray | a423096 | 2013-07-17 16:50:10 -0700 | [diff] [blame] | 30 | #define LOG_NDEBUG 0 |
| 31 | #define LOG_TAG "rsC++" |
| 32 | |
Jason Sams | 69cccdf | 2012-04-02 19:11:49 -0700 | [diff] [blame] | 33 | using namespace android; |
Tim Murray | 9eb7f4b | 2012-11-16 14:02:18 -0800 | [diff] [blame] | 34 | using namespace RSC; |
Jason Sams | 69cccdf | 2012-04-02 19:11:49 -0700 | [diff] [blame] | 35 | |
Tim Murray | 84bf2b8 | 2012-10-31 16:03:16 -0700 | [diff] [blame] | 36 | bool RS::gInitialized = false; |
Tim Murray | 4a92d12 | 2013-07-22 10:56:18 -0700 | [diff] [blame] | 37 | bool RS::usingNative = false; |
Tim Murray | 84bf2b8 | 2012-10-31 16:03:16 -0700 | [diff] [blame] | 38 | pthread_mutex_t RS::gInitMutex = PTHREAD_MUTEX_INITIALIZER; |
Tim Murray | a423096 | 2013-07-17 16:50:10 -0700 | [diff] [blame] | 39 | dispatchTable* RS::dispatch = NULL; |
Tim Murray | a423096 | 2013-07-17 16:50:10 -0700 | [diff] [blame] | 40 | static int gInitError = 0; |
Jason Sams | 221a4b1 | 2012-02-22 15:22:41 -0800 | [diff] [blame] | 41 | |
Tim Murray | 84bf2b8 | 2012-10-31 16:03:16 -0700 | [diff] [blame] | 42 | RS::RS() { |
Jason Sams | 221a4b1 | 2012-02-22 15:22:41 -0800 | [diff] [blame] | 43 | mDev = NULL; |
| 44 | mContext = NULL; |
| 45 | mErrorFunc = NULL; |
| 46 | mMessageFunc = NULL; |
| 47 | mMessageRun = false; |
Tim Murray | a423096 | 2013-07-17 16:50:10 -0700 | [diff] [blame] | 48 | mInit = false; |
Jason Sams | 221a4b1 | 2012-02-22 15:22:41 -0800 | [diff] [blame] | 49 | |
| 50 | memset(&mElements, 0, sizeof(mElements)); |
Tim Murray | 729b6fe | 2013-07-23 16:20:42 -0700 | [diff] [blame] | 51 | memset(&mSamplers, 0, sizeof(mSamplers)); |
Jason Sams | 221a4b1 | 2012-02-22 15:22:41 -0800 | [diff] [blame] | 52 | } |
| 53 | |
Tim Murray | 84bf2b8 | 2012-10-31 16:03:16 -0700 | [diff] [blame] | 54 | RS::~RS() { |
Tim Murray | a423096 | 2013-07-17 16:50:10 -0700 | [diff] [blame] | 55 | if (mInit == true) { |
| 56 | mMessageRun = false; |
Jason Sams | 221a4b1 | 2012-02-22 15:22:41 -0800 | [diff] [blame] | 57 | |
Tim Murray | a423096 | 2013-07-17 16:50:10 -0700 | [diff] [blame] | 58 | RS::dispatch->ContextDeinitToClient(mContext); |
Jason Sams | 221a4b1 | 2012-02-22 15:22:41 -0800 | [diff] [blame] | 59 | |
Tim Murray | a423096 | 2013-07-17 16:50:10 -0700 | [diff] [blame] | 60 | void *res = NULL; |
| 61 | int status = pthread_join(mMessageThreadId, &res); |
Jason Sams | 221a4b1 | 2012-02-22 15:22:41 -0800 | [diff] [blame] | 62 | |
Tim Murray | a423096 | 2013-07-17 16:50:10 -0700 | [diff] [blame] | 63 | RS::dispatch->ContextDestroy(mContext); |
| 64 | mContext = NULL; |
| 65 | RS::dispatch->DeviceDestroy(mDev); |
| 66 | mDev = NULL; |
| 67 | } |
Jason Sams | 221a4b1 | 2012-02-22 15:22:41 -0800 | [diff] [blame] | 68 | } |
| 69 | |
Tim Murray | 4d252d6 | 2012-11-29 14:37:59 -0800 | [diff] [blame] | 70 | bool RS::init(bool forceCpu, bool synchronous) { |
| 71 | return RS::init(RS_VERSION, forceCpu, synchronous); |
Tim Murray | 84bf2b8 | 2012-10-31 16:03:16 -0700 | [diff] [blame] | 72 | } |
| 73 | |
Tim Murray | 0b8a2be | 2013-07-23 16:25:41 -0700 | [diff] [blame] | 74 | static bool loadSymbols(void* handle) { |
| 75 | |
| 76 | RS::dispatch->AllocationGetType = (AllocationGetTypeFnPtr)dlsym(handle, "rsaAllocationGetType"); |
| 77 | if (RS::dispatch->AllocationGetType == NULL) { |
| 78 | ALOGE("Couldn't initialize RS::dispatch->AllocationGetType"); |
| 79 | return false; |
| 80 | } |
| 81 | RS::dispatch->TypeGetNativeData = (TypeGetNativeDataFnPtr)dlsym(handle, "rsaTypeGetNativeData"); |
| 82 | if (RS::dispatch->TypeGetNativeData == NULL) { |
| 83 | ALOGE("Couldn't initialize RS::dispatch->TypeGetNativeData"); |
| 84 | return false; |
| 85 | } |
| 86 | RS::dispatch->ElementGetNativeData = (ElementGetNativeDataFnPtr)dlsym(handle, "rsaElementGetNativeData"); |
| 87 | if (RS::dispatch->ElementGetNativeData == NULL) { |
| 88 | ALOGE("Couldn't initialize RS::dispatch->ElementGetNativeData"); |
| 89 | return false; |
| 90 | } |
| 91 | RS::dispatch->ElementGetSubElements = (ElementGetSubElementsFnPtr)dlsym(handle, "rsaElementGetSubElements"); |
| 92 | if (RS::dispatch->ElementGetSubElements == NULL) { |
| 93 | ALOGE("Couldn't initialize RS::dispatch->ElementGetSubElements"); |
| 94 | return false; |
| 95 | } |
| 96 | RS::dispatch->DeviceCreate = (DeviceCreateFnPtr)dlsym(handle, "rsDeviceCreate"); |
| 97 | if (RS::dispatch->DeviceCreate == NULL) { |
| 98 | ALOGE("Couldn't initialize RS::dispatch->DeviceCreate"); |
| 99 | return false; |
| 100 | } |
| 101 | RS::dispatch->DeviceDestroy = (DeviceDestroyFnPtr)dlsym(handle, "rsDeviceDestroy"); |
| 102 | if (RS::dispatch->DeviceDestroy == NULL) { |
| 103 | ALOGE("Couldn't initialize RS::dispatch->DeviceDestroy"); |
| 104 | return false; |
| 105 | } |
| 106 | RS::dispatch->DeviceSetConfig = (DeviceSetConfigFnPtr)dlsym(handle, "rsDeviceSetConfig"); |
| 107 | if (RS::dispatch->DeviceSetConfig == NULL) { |
| 108 | ALOGE("Couldn't initialize RS::dispatch->DeviceSetConfig"); |
| 109 | return false; |
| 110 | } |
| 111 | RS::dispatch->ContextCreate = (ContextCreateFnPtr)dlsym(handle, "rsContextCreate");; |
| 112 | if (RS::dispatch->ContextCreate == NULL) { |
| 113 | ALOGE("Couldn't initialize RS::dispatch->ContextCreate"); |
| 114 | return false; |
| 115 | } |
Tim Murray | 4a92d12 | 2013-07-22 10:56:18 -0700 | [diff] [blame] | 116 | RS::dispatch->GetName = (GetNameFnPtr)dlsym(handle, "rsaGetName");; |
| 117 | if (RS::dispatch->GetName == NULL) { |
| 118 | ALOGE("Couldn't initialize RS::dispatch->GetName"); |
| 119 | return false; |
| 120 | } |
Tim Murray | 0b8a2be | 2013-07-23 16:25:41 -0700 | [diff] [blame] | 121 | RS::dispatch->ContextDestroy = (ContextDestroyFnPtr)dlsym(handle, "rsContextDestroy"); |
| 122 | if (RS::dispatch->ContextDestroy == NULL) { |
| 123 | ALOGE("Couldn't initialize RS::dispatch->ContextDestroy"); |
| 124 | return false; |
| 125 | } |
| 126 | RS::dispatch->ContextGetMessage = (ContextGetMessageFnPtr)dlsym(handle, "rsContextGetMessage"); |
| 127 | if (RS::dispatch->ContextGetMessage == NULL) { |
| 128 | ALOGE("Couldn't initialize RS::dispatch->ContextGetMessage"); |
| 129 | return false; |
| 130 | } |
| 131 | RS::dispatch->ContextPeekMessage = (ContextPeekMessageFnPtr)dlsym(handle, "rsContextPeekMessage"); |
| 132 | if (RS::dispatch->ContextPeekMessage == NULL) { |
| 133 | ALOGE("Couldn't initialize RS::dispatch->ContextPeekMessage"); |
| 134 | return false; |
| 135 | } |
| 136 | RS::dispatch->ContextSendMessage = (ContextSendMessageFnPtr)dlsym(handle, "rsContextSendMessage"); |
| 137 | if (RS::dispatch->ContextSendMessage == NULL) { |
| 138 | ALOGE("Couldn't initialize RS::dispatch->ContextSendMessage"); |
| 139 | return false; |
| 140 | } |
| 141 | RS::dispatch->ContextInitToClient = (ContextInitToClientFnPtr)dlsym(handle, "rsContextInitToClient"); |
| 142 | if (RS::dispatch->ContextInitToClient == NULL) { |
| 143 | ALOGE("Couldn't initialize RS::dispatch->ContextInitToClient"); |
| 144 | return false; |
| 145 | } |
| 146 | RS::dispatch->ContextDeinitToClient = (ContextDeinitToClientFnPtr)dlsym(handle, "rsContextDeinitToClient"); |
| 147 | if (RS::dispatch->ContextDeinitToClient == NULL) { |
| 148 | ALOGE("Couldn't initialize RS::dispatch->ContextDeinitToClient"); |
| 149 | return false; |
| 150 | } |
| 151 | RS::dispatch->TypeCreate = (TypeCreateFnPtr)dlsym(handle, "rsTypeCreate"); |
| 152 | if (RS::dispatch->TypeCreate == NULL) { |
| 153 | ALOGE("Couldn't initialize RS::dispatch->TypeCreate"); |
| 154 | return false; |
| 155 | } |
| 156 | RS::dispatch->AllocationCreateTyped = (AllocationCreateTypedFnPtr)dlsym(handle, "rsAllocationCreateTyped"); |
| 157 | if (RS::dispatch->AllocationCreateTyped == NULL) { |
| 158 | ALOGE("Couldn't initialize RS::dispatch->AllocationCreateTyped"); |
| 159 | return false; |
| 160 | } |
| 161 | RS::dispatch->AllocationCreateFromBitmap = (AllocationCreateFromBitmapFnPtr)dlsym(handle, "rsAllocationCreateFromBitmap"); |
| 162 | if (RS::dispatch->AllocationCreateFromBitmap == NULL) { |
| 163 | ALOGE("Couldn't initialize RS::dispatch->AllocationCreateFromBitmap"); |
| 164 | return false; |
| 165 | } |
| 166 | RS::dispatch->AllocationCubeCreateFromBitmap = (AllocationCubeCreateFromBitmapFnPtr)dlsym(handle, "rsAllocationCubeCreateFromBitmap"); |
| 167 | if (RS::dispatch->AllocationCubeCreateFromBitmap == NULL) { |
| 168 | ALOGE("Couldn't initialize RS::dispatch->AllocationCubeCreateFromBitmap"); |
| 169 | return false; |
| 170 | } |
| 171 | RS::dispatch->AllocationGetSurface = (AllocationGetSurfaceFnPtr)dlsym(handle, "rsAllocationGetSurface"); |
| 172 | if (RS::dispatch->AllocationGetSurface == NULL) { |
| 173 | ALOGE("Couldn't initialize RS::dispatch->AllocationGetSurface"); |
| 174 | return false; |
| 175 | } |
| 176 | RS::dispatch->AllocationSetSurface = (AllocationSetSurfaceFnPtr)dlsym(handle, "rsAllocationSetSurface"); |
| 177 | if (RS::dispatch->AllocationSetSurface == NULL) { |
| 178 | ALOGE("Couldn't initialize RS::dispatch->AllocationSetSurface"); |
| 179 | return false; |
| 180 | } |
| 181 | RS::dispatch->ContextFinish = (ContextFinishFnPtr)dlsym(handle, "rsContextFinish"); |
| 182 | if (RS::dispatch->ContextFinish == NULL) { |
| 183 | ALOGE("Couldn't initialize RS::dispatch->ContextFinish"); |
| 184 | return false; |
| 185 | } |
| 186 | RS::dispatch->ContextDump = (ContextDumpFnPtr)dlsym(handle, "rsContextDump"); |
| 187 | if (RS::dispatch->ContextDump == NULL) { |
| 188 | ALOGE("Couldn't initialize RS::dispatch->ContextDump"); |
| 189 | return false; |
| 190 | } |
| 191 | RS::dispatch->ContextSetPriority = (ContextSetPriorityFnPtr)dlsym(handle, "rsContextSetPriority"); |
| 192 | if (RS::dispatch->ContextSetPriority == NULL) { |
| 193 | ALOGE("Couldn't initialize RS::dispatch->ContextSetPriority"); |
| 194 | return false; |
| 195 | } |
| 196 | RS::dispatch->AssignName = (AssignNameFnPtr)dlsym(handle, "rsAssignName"); |
| 197 | if (RS::dispatch->AssignName == NULL) { |
| 198 | ALOGE("Couldn't initialize RS::dispatch->AssignName"); |
| 199 | return false; |
| 200 | } |
| 201 | RS::dispatch->ObjDestroy = (ObjDestroyFnPtr)dlsym(handle, "rsObjDestroy"); |
| 202 | if (RS::dispatch->ObjDestroy == NULL) { |
| 203 | ALOGE("Couldn't initialize RS::dispatch->ObjDestroy"); |
| 204 | return false; |
| 205 | } |
| 206 | RS::dispatch->ElementCreate = (ElementCreateFnPtr)dlsym(handle, "rsElementCreate"); |
| 207 | if (RS::dispatch->ElementCreate == NULL) { |
| 208 | ALOGE("Couldn't initialize RS::dispatch->ElementCreate"); |
| 209 | return false; |
| 210 | } |
| 211 | RS::dispatch->ElementCreate2 = (ElementCreate2FnPtr)dlsym(handle, "rsElementCreate2"); |
| 212 | if (RS::dispatch->ElementCreate2 == NULL) { |
| 213 | ALOGE("Couldn't initialize RS::dispatch->ElementCreate2"); |
| 214 | return false; |
| 215 | } |
| 216 | RS::dispatch->AllocationCopyToBitmap = (AllocationCopyToBitmapFnPtr)dlsym(handle, "rsAllocationCopyToBitmap"); |
| 217 | if (RS::dispatch->AllocationCopyToBitmap == NULL) { |
| 218 | ALOGE("Couldn't initialize RS::dispatch->AllocationCopyToBitmap"); |
| 219 | return false; |
| 220 | } |
| 221 | RS::dispatch->Allocation1DData = (Allocation1DDataFnPtr)dlsym(handle, "rsAllocation1DData"); |
| 222 | if (RS::dispatch->Allocation1DData == NULL) { |
| 223 | ALOGE("Couldn't initialize RS::dispatch->Allocation1DData"); |
| 224 | return false; |
| 225 | } |
| 226 | RS::dispatch->Allocation1DElementData = (Allocation1DElementDataFnPtr)dlsym(handle, "rsAllocation1DElementData"); |
| 227 | if (RS::dispatch->Allocation1DElementData == NULL) { |
| 228 | ALOGE("Couldn't initialize RS::dispatch->Allocation1DElementData"); |
| 229 | return false; |
| 230 | } |
| 231 | RS::dispatch->Allocation2DData = (Allocation2DDataFnPtr)dlsym(handle, "rsAllocation2DData"); |
| 232 | if (RS::dispatch->Allocation2DData == NULL) { |
| 233 | ALOGE("Couldn't initialize RS::dispatch->Allocation2DData"); |
| 234 | return false; |
| 235 | } |
| 236 | RS::dispatch->Allocation3DData = (Allocation3DDataFnPtr)dlsym(handle, "rsAllocation3DData"); |
| 237 | if (RS::dispatch->Allocation3DData == NULL) { |
| 238 | ALOGE("Couldn't initialize RS::dispatch->Allocation3DData"); |
| 239 | return false; |
| 240 | } |
| 241 | RS::dispatch->AllocationGenerateMipmaps = (AllocationGenerateMipmapsFnPtr)dlsym(handle, "rsAllocationGenerateMipmaps"); |
| 242 | if (RS::dispatch->AllocationGenerateMipmaps == NULL) { |
| 243 | ALOGE("Couldn't initialize RS::dispatch->AllocationGenerateMipmaps"); |
| 244 | return false; |
| 245 | } |
| 246 | RS::dispatch->AllocationRead = (AllocationReadFnPtr)dlsym(handle, "rsAllocationRead"); |
| 247 | if (RS::dispatch->AllocationRead == NULL) { |
| 248 | ALOGE("Couldn't initialize RS::dispatch->AllocationRead"); |
| 249 | return false; |
| 250 | } |
| 251 | RS::dispatch->Allocation1DRead = (Allocation1DReadFnPtr)dlsym(handle, "rsAllocation1DRead"); |
| 252 | if (RS::dispatch->Allocation1DRead == NULL) { |
| 253 | ALOGE("Couldn't initialize RS::dispatch->Allocation1DRead"); |
| 254 | return false; |
| 255 | } |
| 256 | RS::dispatch->Allocation2DRead = (Allocation2DReadFnPtr)dlsym(handle, "rsAllocation2DRead"); |
| 257 | if (RS::dispatch->Allocation2DRead == NULL) { |
| 258 | ALOGE("Couldn't initialize RS::dispatch->Allocation2DRead"); |
| 259 | return false; |
| 260 | } |
| 261 | RS::dispatch->AllocationSyncAll = (AllocationSyncAllFnPtr)dlsym(handle, "rsAllocationSyncAll"); |
| 262 | if (RS::dispatch->AllocationSyncAll == NULL) { |
| 263 | ALOGE("Couldn't initialize RS::dispatch->AllocationSyncAll"); |
| 264 | return false; |
| 265 | } |
| 266 | RS::dispatch->AllocationResize1D = (AllocationResize1DFnPtr)dlsym(handle, "rsAllocationResize1D"); |
| 267 | if (RS::dispatch->AllocationResize1D == NULL) { |
| 268 | ALOGE("Couldn't initialize RS::dispatch->AllocationResize1D"); |
| 269 | return false; |
| 270 | } |
| 271 | RS::dispatch->AllocationCopy2DRange = (AllocationCopy2DRangeFnPtr)dlsym(handle, "rsAllocationCopy2DRange"); |
| 272 | if (RS::dispatch->AllocationCopy2DRange == NULL) { |
| 273 | ALOGE("Couldn't initialize RS::dispatch->AllocationCopy2DRange"); |
| 274 | return false; |
| 275 | } |
| 276 | RS::dispatch->AllocationCopy3DRange = (AllocationCopy3DRangeFnPtr)dlsym(handle, "rsAllocationCopy3DRange"); |
| 277 | if (RS::dispatch->AllocationCopy3DRange == NULL) { |
| 278 | ALOGE("Couldn't initialize RS::dispatch->AllocationCopy3DRange"); |
| 279 | return false; |
| 280 | } |
| 281 | RS::dispatch->SamplerCreate = (SamplerCreateFnPtr)dlsym(handle, "rsSamplerCreate"); |
| 282 | if (RS::dispatch->SamplerCreate == NULL) { |
| 283 | ALOGE("Couldn't initialize RS::dispatch->SamplerCreate"); |
| 284 | return false; |
| 285 | } |
| 286 | RS::dispatch->ScriptBindAllocation = (ScriptBindAllocationFnPtr)dlsym(handle, "rsScriptBindAllocation"); |
| 287 | if (RS::dispatch->ScriptBindAllocation == NULL) { |
| 288 | ALOGE("Couldn't initialize RS::dispatch->ScriptBindAllocation"); |
| 289 | return false; |
| 290 | } |
| 291 | RS::dispatch->ScriptSetTimeZone = (ScriptSetTimeZoneFnPtr)dlsym(handle, "rsScriptSetTimeZone"); |
| 292 | if (RS::dispatch->ScriptSetTimeZone == NULL) { |
| 293 | ALOGE("Couldn't initialize RS::dispatch->ScriptSetTimeZone"); |
| 294 | return false; |
| 295 | } |
| 296 | RS::dispatch->ScriptInvoke = (ScriptInvokeFnPtr)dlsym(handle, "rsScriptInvoke"); |
| 297 | if (RS::dispatch->ScriptInvoke == NULL) { |
| 298 | ALOGE("Couldn't initialize RS::dispatch->ScriptInvoke"); |
| 299 | return false; |
| 300 | } |
| 301 | RS::dispatch->ScriptInvokeV = (ScriptInvokeVFnPtr)dlsym(handle, "rsScriptInvokeV"); |
| 302 | if (RS::dispatch->ScriptInvokeV == NULL) { |
| 303 | ALOGE("Couldn't initialize RS::dispatch->ScriptInvokeV"); |
| 304 | return false; |
| 305 | } |
| 306 | RS::dispatch->ScriptForEach = (ScriptForEachFnPtr)dlsym(handle, "rsScriptForEach"); |
| 307 | if (RS::dispatch->ScriptForEach == NULL) { |
| 308 | ALOGE("Couldn't initialize RS::dispatch->ScriptForEach"); |
| 309 | return false; |
| 310 | } |
| 311 | RS::dispatch->ScriptSetVarI = (ScriptSetVarIFnPtr)dlsym(handle, "rsScriptSetVarI"); |
| 312 | if (RS::dispatch->ScriptSetVarI == NULL) { |
| 313 | ALOGE("Couldn't initialize RS::dispatch->ScriptSetVarI"); |
| 314 | return false; |
| 315 | } |
| 316 | RS::dispatch->ScriptSetVarObj = (ScriptSetVarObjFnPtr)dlsym(handle, "rsScriptSetVarObj"); |
| 317 | if (RS::dispatch->ScriptSetVarObj == NULL) { |
| 318 | ALOGE("Couldn't initialize RS::dispatch->ScriptSetVarObj"); |
| 319 | return false; |
| 320 | } |
| 321 | RS::dispatch->ScriptSetVarJ = (ScriptSetVarJFnPtr)dlsym(handle, "rsScriptSetVarJ"); |
| 322 | if (RS::dispatch->ScriptSetVarJ == NULL) { |
| 323 | ALOGE("Couldn't initialize RS::dispatch->ScriptSetVarJ"); |
| 324 | return false; |
| 325 | } |
| 326 | RS::dispatch->ScriptSetVarF = (ScriptSetVarFFnPtr)dlsym(handle, "rsScriptSetVarF"); |
| 327 | if (RS::dispatch->ScriptSetVarF == NULL) { |
| 328 | ALOGE("Couldn't initialize RS::dispatch->ScriptSetVarF"); |
| 329 | return false; |
| 330 | } |
| 331 | RS::dispatch->ScriptSetVarD = (ScriptSetVarDFnPtr)dlsym(handle, "rsScriptSetVarD"); |
| 332 | if (RS::dispatch->ScriptSetVarD == NULL) { |
| 333 | ALOGE("Couldn't initialize RS::dispatch->ScriptSetVarD"); |
| 334 | return false; |
| 335 | } |
| 336 | RS::dispatch->ScriptSetVarV = (ScriptSetVarVFnPtr)dlsym(handle, "rsScriptSetVarV"); |
| 337 | if (RS::dispatch->ScriptSetVarV == NULL) { |
| 338 | ALOGE("Couldn't initialize RS::dispatch->ScriptSetVarV"); |
| 339 | return false; |
| 340 | } |
| 341 | RS::dispatch->ScriptGetVarV = (ScriptGetVarVFnPtr)dlsym(handle, "rsScriptGetVarV"); |
| 342 | if (RS::dispatch->ScriptGetVarV == NULL) { |
| 343 | ALOGE("Couldn't initialize RS::dispatch->ScriptGetVarV"); |
| 344 | return false; |
| 345 | } |
| 346 | RS::dispatch->ScriptSetVarVE = (ScriptSetVarVEFnPtr)dlsym(handle, "rsScriptSetVarVE"); |
| 347 | if (RS::dispatch->ScriptSetVarVE == NULL) { |
| 348 | ALOGE("Couldn't initialize RS::dispatch->ScriptSetVarVE"); |
| 349 | return false; |
| 350 | } |
| 351 | RS::dispatch->ScriptCCreate = (ScriptCCreateFnPtr)dlsym(handle, "rsScriptCCreate"); |
| 352 | if (RS::dispatch->ScriptCCreate == NULL) { |
| 353 | ALOGE("Couldn't initialize RS::dispatch->ScriptCCreate"); |
| 354 | return false; |
| 355 | } |
| 356 | RS::dispatch->ScriptIntrinsicCreate = (ScriptIntrinsicCreateFnPtr)dlsym(handle, "rsScriptIntrinsicCreate"); |
| 357 | if (RS::dispatch->ScriptIntrinsicCreate == NULL) { |
| 358 | ALOGE("Couldn't initialize RS::dispatch->ScriptIntrinsicCreate"); |
| 359 | return false; |
| 360 | } |
| 361 | RS::dispatch->ScriptKernelIDCreate = (ScriptKernelIDCreateFnPtr)dlsym(handle, "rsScriptKernelIDCreate"); |
| 362 | if (RS::dispatch->ScriptKernelIDCreate == NULL) { |
| 363 | ALOGE("Couldn't initialize RS::dispatch->ScriptKernelIDCreate"); |
| 364 | return false; |
| 365 | } |
| 366 | RS::dispatch->ScriptFieldIDCreate = (ScriptFieldIDCreateFnPtr)dlsym(handle, "rsScriptFieldIDCreate"); |
| 367 | if (RS::dispatch->ScriptFieldIDCreate == NULL) { |
| 368 | ALOGE("Couldn't initialize RS::dispatch->ScriptFieldIDCreate"); |
| 369 | return false; |
| 370 | } |
| 371 | RS::dispatch->ScriptGroupCreate = (ScriptGroupCreateFnPtr)dlsym(handle, "rsScriptGroupCreate"); |
| 372 | if (RS::dispatch->ScriptGroupCreate == NULL) { |
| 373 | ALOGE("Couldn't initialize RS::dispatch->ScriptGroupCreate"); |
| 374 | return false; |
| 375 | } |
| 376 | RS::dispatch->ScriptGroupSetOutput = (ScriptGroupSetOutputFnPtr)dlsym(handle, "rsScriptGroupSetOutput"); |
| 377 | if (RS::dispatch->ScriptGroupSetOutput == NULL) { |
| 378 | ALOGE("Couldn't initialize RS::dispatch->ScriptGroupSetOutput"); |
| 379 | return false; |
| 380 | } |
| 381 | RS::dispatch->ScriptGroupSetInput = (ScriptGroupSetInputFnPtr)dlsym(handle, "rsScriptGroupSetInput"); |
| 382 | if (RS::dispatch->ScriptGroupSetInput == NULL) { |
| 383 | ALOGE("Couldn't initialize RS::dispatch->ScriptGroupSetInput"); |
| 384 | return false; |
| 385 | } |
| 386 | RS::dispatch->ScriptGroupExecute = (ScriptGroupExecuteFnPtr)dlsym(handle, "rsScriptGroupExecute"); |
| 387 | if (RS::dispatch->ScriptGroupExecute == NULL) { |
| 388 | ALOGE("Couldn't initialize RS::dispatch->ScriptGroupExecute"); |
| 389 | return false; |
| 390 | } |
| 391 | RS::dispatch->AllocationIoSend = (AllocationIoSendFnPtr)dlsym(handle, "rsAllocationIoSend"); |
| 392 | if (RS::dispatch->AllocationIoSend == NULL) { |
| 393 | ALOGE("Couldn't initialize RS::dispatch->AllocationIoSend"); |
| 394 | return false; |
| 395 | } |
| 396 | RS::dispatch->AllocationIoReceive = (AllocationIoReceiveFnPtr)dlsym(handle, "rsAllocationIoReceive"); |
| 397 | if (RS::dispatch->AllocationIoReceive == NULL) { |
| 398 | ALOGE("Couldn't initialize RS::dispatch->AllocationIoReceive"); |
| 399 | return false; |
| 400 | } |
| 401 | |
| 402 | return true; |
| 403 | } |
| 404 | |
Tim Murray | 4a92d12 | 2013-07-22 10:56:18 -0700 | [diff] [blame] | 405 | static bool loadSO(const char* filename) { |
| 406 | void* handle = dlopen(filename, RTLD_LAZY | RTLD_LOCAL); |
| 407 | if (handle == NULL) { |
| 408 | ALOGE("couldn't dlopen %s, %s", filename, dlerror()); |
| 409 | return false; |
| 410 | } |
Tim Murray | a423096 | 2013-07-17 16:50:10 -0700 | [diff] [blame] | 411 | |
Tim Murray | 4a92d12 | 2013-07-22 10:56:18 -0700 | [diff] [blame] | 412 | if (loadSymbols(handle) == false) { |
| 413 | ALOGE("%s init failed!", filename); |
| 414 | return false; |
| 415 | } |
| 416 | ALOGE("Successfully loaded %s", filename); |
| 417 | return true; |
| 418 | } |
| 419 | |
| 420 | static uint32_t getProp(const char *str) { |
| 421 | #if !defined(RS_SERVER) && defined(HAVE_ANDROID_OS) |
| 422 | char buf[256]; |
| 423 | property_get(str, buf, "0"); |
| 424 | return atoi(buf); |
| 425 | #else |
| 426 | return 0; |
| 427 | #endif |
| 428 | } |
| 429 | |
| 430 | bool RS::initDispatch(int targetApi) { |
Tim Murray | a423096 | 2013-07-17 16:50:10 -0700 | [diff] [blame] | 431 | pthread_mutex_lock(&gInitMutex); |
| 432 | if (gInitError) { |
| 433 | goto error; |
| 434 | } else if (gInitialized) { |
Tim Murray | 47666f5 | 2013-07-29 14:32:34 -0700 | [diff] [blame] | 435 | pthread_mutex_unlock(&gInitMutex); |
Tim Murray | a423096 | 2013-07-17 16:50:10 -0700 | [diff] [blame] | 436 | return true; |
| 437 | } |
Tim Murray | a423096 | 2013-07-17 16:50:10 -0700 | [diff] [blame] | 438 | |
| 439 | RS::dispatch = new dispatchTable; |
Tim Murray | 4a92d12 | 2013-07-22 10:56:18 -0700 | [diff] [blame] | 440 | |
| 441 | // attempt to load libRS, load libRSSupport on failure |
| 442 | // if property is set, proceed directly to libRSSupport |
| 443 | if (getProp("debug.rs.forcecompat") == 0) { |
| 444 | usingNative = loadSO("libRS.so"); |
| 445 | } |
| 446 | if (usingNative == false) { |
| 447 | if (loadSO("libRSSupport.so") == false) { |
| 448 | ALOGE("Failed to load libRS.so and libRSSupport.so"); |
| 449 | goto error; |
| 450 | } |
Tim Murray | a423096 | 2013-07-17 16:50:10 -0700 | [diff] [blame] | 451 | } |
| 452 | |
| 453 | gInitialized = true; |
| 454 | |
| 455 | pthread_mutex_unlock(&gInitMutex); |
| 456 | return true; |
| 457 | |
| 458 | error: |
| 459 | gInitError = 1; |
| 460 | pthread_mutex_unlock(&gInitMutex); |
| 461 | return false; |
| 462 | } |
| 463 | |
Tim Murray | 4d252d6 | 2012-11-29 14:37:59 -0800 | [diff] [blame] | 464 | bool RS::init(int targetApi, bool forceCpu, bool synchronous) { |
Tim Murray | a423096 | 2013-07-17 16:50:10 -0700 | [diff] [blame] | 465 | if (initDispatch(targetApi) == false) { |
| 466 | ALOGE("Couldn't initialize dispatch table"); |
| 467 | return false; |
| 468 | } |
| 469 | |
| 470 | mDev = RS::dispatch->DeviceCreate(); |
Jason Sams | 221a4b1 | 2012-02-22 15:22:41 -0800 | [diff] [blame] | 471 | if (mDev == 0) { |
| 472 | ALOGE("Device creation failed"); |
| 473 | return false; |
| 474 | } |
| 475 | |
Tim Murray | a423096 | 2013-07-17 16:50:10 -0700 | [diff] [blame] | 476 | mContext = RS::dispatch->ContextCreate(mDev, 0, targetApi, RS_CONTEXT_TYPE_NORMAL, forceCpu, synchronous); |
Jason Sams | 221a4b1 | 2012-02-22 15:22:41 -0800 | [diff] [blame] | 477 | if (mContext == 0) { |
| 478 | ALOGE("Context creation failed"); |
| 479 | return false; |
| 480 | } |
| 481 | |
Jason Sams | 221a4b1 | 2012-02-22 15:22:41 -0800 | [diff] [blame] | 482 | pid_t mNativeMessageThreadId; |
| 483 | |
| 484 | int status = pthread_create(&mMessageThreadId, NULL, threadProc, this); |
| 485 | if (status) { |
Tim Murray | 84bf2b8 | 2012-10-31 16:03:16 -0700 | [diff] [blame] | 486 | ALOGE("Failed to start RS message thread."); |
Jason Sams | 221a4b1 | 2012-02-22 15:22:41 -0800 | [diff] [blame] | 487 | return false; |
| 488 | } |
| 489 | // Wait for the message thread to be active. |
| 490 | while (!mMessageRun) { |
| 491 | usleep(1000); |
| 492 | } |
| 493 | |
Tim Murray | a423096 | 2013-07-17 16:50:10 -0700 | [diff] [blame] | 494 | mInit = true; |
| 495 | |
Jason Sams | 221a4b1 | 2012-02-22 15:22:41 -0800 | [diff] [blame] | 496 | return true; |
| 497 | } |
| 498 | |
Tim Murray | 84bf2b8 | 2012-10-31 16:03:16 -0700 | [diff] [blame] | 499 | void RS::throwError(const char *err) const { |
Jason Sams | b2e3dc5 | 2012-02-23 17:14:39 -0800 | [diff] [blame] | 500 | ALOGE("RS CPP error: %s", err); |
| 501 | int * v = NULL; |
| 502 | v[0] = 0; |
| 503 | } |
| 504 | |
| 505 | |
Tim Murray | 84bf2b8 | 2012-10-31 16:03:16 -0700 | [diff] [blame] | 506 | void * RS::threadProc(void *vrsc) { |
| 507 | RS *rs = static_cast<RS *>(vrsc); |
Jason Sams | 221a4b1 | 2012-02-22 15:22:41 -0800 | [diff] [blame] | 508 | size_t rbuf_size = 256; |
| 509 | void * rbuf = malloc(rbuf_size); |
| 510 | |
Tim Murray | a423096 | 2013-07-17 16:50:10 -0700 | [diff] [blame] | 511 | RS::dispatch->ContextInitToClient(rs->mContext); |
Jason Sams | 221a4b1 | 2012-02-22 15:22:41 -0800 | [diff] [blame] | 512 | rs->mMessageRun = true; |
| 513 | |
| 514 | while (rs->mMessageRun) { |
| 515 | size_t receiveLen = 0; |
| 516 | uint32_t usrID = 0; |
| 517 | uint32_t subID = 0; |
Tim Murray | a423096 | 2013-07-17 16:50:10 -0700 | [diff] [blame] | 518 | RsMessageToClientType r = RS::dispatch->ContextPeekMessage(rs->mContext, |
| 519 | &receiveLen, sizeof(receiveLen), |
| 520 | &usrID, sizeof(usrID)); |
Jason Sams | 221a4b1 | 2012-02-22 15:22:41 -0800 | [diff] [blame] | 521 | |
| 522 | if (receiveLen >= rbuf_size) { |
| 523 | rbuf_size = receiveLen + 32; |
| 524 | rbuf = realloc(rbuf, rbuf_size); |
| 525 | } |
| 526 | if (!rbuf) { |
Tim Murray | 84bf2b8 | 2012-10-31 16:03:16 -0700 | [diff] [blame] | 527 | ALOGE("RS::message handler realloc error %zu", rbuf_size); |
Jason Sams | 221a4b1 | 2012-02-22 15:22:41 -0800 | [diff] [blame] | 528 | // No clean way to recover now? |
| 529 | } |
Tim Murray | a423096 | 2013-07-17 16:50:10 -0700 | [diff] [blame] | 530 | RS::dispatch->ContextGetMessage(rs->mContext, rbuf, rbuf_size, &receiveLen, sizeof(receiveLen), |
Jason Sams | 221a4b1 | 2012-02-22 15:22:41 -0800 | [diff] [blame] | 531 | &subID, sizeof(subID)); |
| 532 | |
| 533 | switch(r) { |
| 534 | case RS_MESSAGE_TO_CLIENT_ERROR: |
| 535 | ALOGE("RS Error %s", (const char *)rbuf); |
| 536 | |
| 537 | if(rs->mMessageFunc != NULL) { |
| 538 | rs->mErrorFunc(usrID, (const char *)rbuf); |
| 539 | } |
| 540 | break; |
Stephen Hines | 76a1be4 | 2012-11-26 16:26:03 -0800 | [diff] [blame] | 541 | case RS_MESSAGE_TO_CLIENT_NONE: |
Jason Sams | 221a4b1 | 2012-02-22 15:22:41 -0800 | [diff] [blame] | 542 | case RS_MESSAGE_TO_CLIENT_EXCEPTION: |
Stephen Hines | 76a1be4 | 2012-11-26 16:26:03 -0800 | [diff] [blame] | 543 | case RS_MESSAGE_TO_CLIENT_RESIZE: |
Jason Sams | 221a4b1 | 2012-02-22 15:22:41 -0800 | [diff] [blame] | 544 | // teardown. But we want to avoid starving other threads during |
| 545 | // teardown by yielding until the next line in the destructor can |
Stephen Hines | 76a1be4 | 2012-11-26 16:26:03 -0800 | [diff] [blame] | 546 | // execute to set mRun = false. Note that the FIFO sends an |
| 547 | // empty NONE message when it reaches its destructor. |
Jason Sams | 221a4b1 | 2012-02-22 15:22:41 -0800 | [diff] [blame] | 548 | usleep(1000); |
| 549 | break; |
| 550 | case RS_MESSAGE_TO_CLIENT_USER: |
| 551 | if(rs->mMessageFunc != NULL) { |
| 552 | rs->mMessageFunc(usrID, rbuf, receiveLen); |
| 553 | } else { |
| 554 | ALOGE("Received a message from the script with no message handler installed."); |
| 555 | } |
| 556 | break; |
| 557 | |
| 558 | default: |
Tim Murray | 84bf2b8 | 2012-10-31 16:03:16 -0700 | [diff] [blame] | 559 | ALOGE("RS unknown message type %i", r); |
Jason Sams | 221a4b1 | 2012-02-22 15:22:41 -0800 | [diff] [blame] | 560 | } |
| 561 | } |
| 562 | |
| 563 | if (rbuf) { |
| 564 | free(rbuf); |
| 565 | } |
Tim Murray | 84bf2b8 | 2012-10-31 16:03:16 -0700 | [diff] [blame] | 566 | ALOGE("RS Message thread exiting."); |
Jason Sams | 221a4b1 | 2012-02-22 15:22:41 -0800 | [diff] [blame] | 567 | return NULL; |
| 568 | } |
| 569 | |
Tim Murray | 84bf2b8 | 2012-10-31 16:03:16 -0700 | [diff] [blame] | 570 | void RS::setErrorHandler(ErrorHandlerFunc_t func) { |
Jason Sams | 221a4b1 | 2012-02-22 15:22:41 -0800 | [diff] [blame] | 571 | mErrorFunc = func; |
| 572 | } |
| 573 | |
Tim Murray | 84bf2b8 | 2012-10-31 16:03:16 -0700 | [diff] [blame] | 574 | void RS::setMessageHandler(MessageHandlerFunc_t func) { |
Jason Sams | 221a4b1 | 2012-02-22 15:22:41 -0800 | [diff] [blame] | 575 | mMessageFunc = func; |
| 576 | } |
Tim Murray | baca6c3 | 2012-11-14 16:51:46 -0800 | [diff] [blame] | 577 | |
| 578 | void RS::finish() { |
Tim Murray | a423096 | 2013-07-17 16:50:10 -0700 | [diff] [blame] | 579 | RS::dispatch->ContextFinish(mContext); |
Tim Murray | baca6c3 | 2012-11-14 16:51:46 -0800 | [diff] [blame] | 580 | } |