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