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