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