Miao Wang | da48820 | 2018-10-29 16:26:26 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2018 The Android Open Source Project |
| 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 | |
Miao Wang | 4772b60 | 2017-01-20 10:30:38 -0800 | [diff] [blame] | 17 | #include "Context.h" |
| 18 | #include "Device.h" |
| 19 | |
Brian Carlstrom | 3f2d71f | 2018-01-24 19:22:30 -0800 | [diff] [blame] | 20 | #include <android-base/logging.h> |
Jiyong Park | 0f70905 | 2017-08-16 23:30:42 +0900 | [diff] [blame] | 21 | #include <android/dlext.h> |
| 22 | #include <dlfcn.h> |
| 23 | |
Miao Wang | 4772b60 | 2017-01-20 10:30:38 -0800 | [diff] [blame] | 24 | namespace android { |
| 25 | namespace hardware { |
| 26 | namespace renderscript { |
| 27 | namespace V1_0 { |
| 28 | namespace implementation { |
| 29 | |
| 30 | |
| 31 | static dispatchTable loadHAL(); |
| 32 | dispatchTable Device::mDispatchHal = loadHAL(); |
| 33 | |
| 34 | Device::Device() { |
| 35 | } |
| 36 | |
| 37 | dispatchTable& Device::getHal() { |
| 38 | return mDispatchHal; |
| 39 | } |
| 40 | |
| 41 | |
| 42 | // Methods from ::android::hardware::renderscript::V1_0::IDevice follow. |
| 43 | |
| 44 | Return<sp<IContext>> Device::contextCreate(uint32_t sdkVersion, ContextType ct, int32_t flags) { |
| 45 | return new Context(sdkVersion, ct, flags); |
| 46 | } |
| 47 | |
| 48 | |
| 49 | // Methods from ::android::hidl::base::V1_0::IBase follow. |
| 50 | |
| 51 | IDevice* HIDL_FETCH_IDevice(const char* /* name */) { |
| 52 | return new Device(); |
| 53 | } |
| 54 | |
| 55 | // Helper function |
| 56 | dispatchTable loadHAL() { |
| 57 | |
| 58 | static_assert(sizeof(void*) <= sizeof(uint64_t), "RenderScript HIDL Error: sizeof(void*) > sizeof(uint64_t)"); |
| 59 | static_assert(sizeof(size_t) <= sizeof(uint64_t), "RenderScript HIDL Error: sizeof(size_t) > sizeof(uint64_t)"); |
| 60 | |
| 61 | const char* filename = "libRS_internal.so"; |
Jiyong Park | 0f70905 | 2017-08-16 23:30:42 +0900 | [diff] [blame] | 62 | // Try to load libRS_internal.so from the "rs" namespace directly. |
| 63 | typedef struct android_namespace_t* (*GetExportedNamespaceFnPtr)(const char*); |
Jae Shin | 6a70875 | 2017-10-20 17:28:41 +0900 | [diff] [blame] | 64 | GetExportedNamespaceFnPtr getExportedNamespace = reinterpret_cast<GetExportedNamespaceFnPtr>( |
| 65 | dlsym(RTLD_DEFAULT, "android_get_exported_namespace")); |
Jiyong Park | 0f70905 | 2017-08-16 23:30:42 +0900 | [diff] [blame] | 66 | void* handle = nullptr; |
| 67 | if (getExportedNamespace != nullptr) { |
Jae Shin | 6a70875 | 2017-10-20 17:28:41 +0900 | [diff] [blame] | 68 | android_namespace_t* rsNamespace = getExportedNamespace("rs"); |
| 69 | if (rsNamespace != nullptr) { |
Jiyong Park | 0f70905 | 2017-08-16 23:30:42 +0900 | [diff] [blame] | 70 | const android_dlextinfo dlextinfo = { |
Jae Shin | 6a70875 | 2017-10-20 17:28:41 +0900 | [diff] [blame] | 71 | .flags = ANDROID_DLEXT_USE_NAMESPACE, .library_namespace = rsNamespace, |
Jiyong Park | 0f70905 | 2017-08-16 23:30:42 +0900 | [diff] [blame] | 72 | }; |
| 73 | handle = android_dlopen_ext(filename, RTLD_LAZY | RTLD_LOCAL, &dlextinfo); |
Brian Carlstrom | 3f2d71f | 2018-01-24 19:22:30 -0800 | [diff] [blame] | 74 | if (handle == nullptr) { |
| 75 | LOG(WARNING) << "android_dlopen_ext(" << filename << ") failed: " << dlerror(); |
| 76 | } |
Jiyong Park | 0f70905 | 2017-08-16 23:30:42 +0900 | [diff] [blame] | 77 | } |
| 78 | } |
| 79 | if (handle == nullptr) { |
| 80 | // if there is no "rs" namespace (in case when this HAL impl is loaded |
| 81 | // into a vendor process), then use the plain dlopen. |
| 82 | handle = dlopen(filename, RTLD_LAZY | RTLD_LOCAL); |
Brian Carlstrom | 3f2d71f | 2018-01-24 19:22:30 -0800 | [diff] [blame] | 83 | if (handle == nullptr) { |
| 84 | LOG(FATAL) << "dlopen(" << filename << ") failed: " << dlerror(); |
| 85 | } |
Jiyong Park | 0f70905 | 2017-08-16 23:30:42 +0900 | [diff] [blame] | 86 | } |
Miao Wang | 4772b60 | 2017-01-20 10:30:38 -0800 | [diff] [blame] | 87 | |
| 88 | dispatchTable dispatchHal = { |
Miao Wang | 41d8a44 | 2017-05-16 15:36:54 -0700 | [diff] [blame] | 89 | .SetNativeLibDir = (SetNativeLibDirFnPtr) nullptr, |
Miao Wang | 4772b60 | 2017-01-20 10:30:38 -0800 | [diff] [blame] | 90 | |
Miao Wang | 41d8a44 | 2017-05-16 15:36:54 -0700 | [diff] [blame] | 91 | .Allocation1DData = |
| 92 | (Allocation1DDataFnPtr)dlsym(handle, "rsAllocation1DData"), |
| 93 | .Allocation1DElementData = (Allocation1DElementDataFnPtr) nullptr, |
| 94 | .Allocation1DRead = |
| 95 | (Allocation1DReadFnPtr)dlsym(handle, "rsAllocation1DRead"), |
| 96 | .Allocation2DData = |
| 97 | (Allocation2DDataFnPtr)dlsym(handle, "rsAllocation2DData"), |
| 98 | .Allocation2DRead = |
| 99 | (Allocation2DReadFnPtr)dlsym(handle, "rsAllocation2DRead"), |
| 100 | .Allocation3DData = |
| 101 | (Allocation3DDataFnPtr)dlsym(handle, "rsAllocation3DData"), |
| 102 | .Allocation3DRead = |
| 103 | (Allocation3DReadFnPtr)dlsym(handle, "rsAllocation3DRead"), |
| 104 | .AllocationAdapterCreate = (AllocationAdapterCreateFnPtr)dlsym( |
| 105 | handle, "rsAllocationAdapterCreate"), |
| 106 | .AllocationAdapterOffset = (AllocationAdapterOffsetFnPtr)dlsym( |
| 107 | handle, "rsAllocationAdapterOffset"), |
| 108 | .AllocationCopy2DRange = (AllocationCopy2DRangeFnPtr)dlsym( |
| 109 | handle, "rsAllocationCopy2DRange"), |
| 110 | .AllocationCopy3DRange = (AllocationCopy3DRangeFnPtr)dlsym( |
| 111 | handle, "rsAllocationCopy3DRange"), |
| 112 | .AllocationCopyToBitmap = (AllocationCopyToBitmapFnPtr)dlsym( |
| 113 | handle, "rsAllocationCopyToBitmap"), |
| 114 | .AllocationCreateFromBitmap = (AllocationCreateFromBitmapFnPtr)dlsym( |
| 115 | handle, "rsAllocationCreateFromBitmap"), |
| 116 | .AllocationCreateStrided = (AllocationCreateStridedFnPtr)dlsym( |
| 117 | handle, "rsAllocationCreateStrided"), |
| 118 | .AllocationCreateTyped = (AllocationCreateTypedFnPtr)dlsym( |
| 119 | handle, "rsAllocationCreateTyped"), |
| 120 | .AllocationCubeCreateFromBitmap = |
| 121 | (AllocationCubeCreateFromBitmapFnPtr)dlsym( |
| 122 | handle, "rsAllocationCubeCreateFromBitmap"), |
| 123 | .AllocationElementData = (AllocationElementDataFnPtr)dlsym( |
| 124 | handle, "rsAllocationElementData"), |
| 125 | .AllocationElementRead = (AllocationElementReadFnPtr)dlsym( |
| 126 | handle, "rsAllocationElementRead"), |
| 127 | .AllocationGenerateMipmaps = (AllocationGenerateMipmapsFnPtr)dlsym( |
| 128 | handle, "rsAllocationGenerateMipmaps"), |
| 129 | .AllocationGetPointer = |
| 130 | (AllocationGetPointerFnPtr)dlsym(handle, "rsAllocationGetPointer"), |
| 131 | .AllocationGetSurface = |
| 132 | (AllocationGetSurfaceFnPtr)dlsym(handle, "rsAllocationGetSurface"), |
| 133 | .AllocationGetType = |
| 134 | (AllocationGetTypeFnPtr)dlsym(handle, "rsaAllocationGetType"), |
| 135 | .AllocationIoReceive = |
| 136 | (AllocationIoReceiveFnPtr)dlsym(handle, "rsAllocationIoReceive"), |
| 137 | .AllocationIoSend = |
| 138 | (AllocationIoSendFnPtr)dlsym(handle, "rsAllocationIoSend"), |
| 139 | .AllocationRead = |
| 140 | (AllocationReadFnPtr)dlsym(handle, "rsAllocationRead"), |
| 141 | .AllocationResize1D = |
| 142 | (AllocationResize1DFnPtr)dlsym(handle, "rsAllocationResize1D"), |
| 143 | .AllocationSetSurface = |
| 144 | (AllocationSetSurfaceFnPtr)dlsym(handle, "rsAllocationSetSurface"), |
| 145 | .AllocationSetupBufferQueue = (AllocationSetupBufferQueueFnPtr)dlsym( |
| 146 | handle, "rsAllocationSetupBufferQueue"), |
| 147 | .AllocationShareBufferQueue = (AllocationShareBufferQueueFnPtr)dlsym( |
| 148 | handle, "rsAllocationShareBufferQueue"), |
| 149 | .AllocationSyncAll = |
| 150 | (AllocationSyncAllFnPtr)dlsym(handle, "rsAllocationSyncAll"), |
Miao Wang | 4772b60 | 2017-01-20 10:30:38 -0800 | [diff] [blame] | 151 | .AssignName = (AssignNameFnPtr)dlsym(handle, "rsAssignName"), |
| 152 | .ClosureCreate = (ClosureCreateFnPtr)dlsym(handle, "rsClosureCreate"), |
| 153 | .ClosureSetArg = (ClosureSetArgFnPtr)dlsym(handle, "rsClosureSetArg"), |
Miao Wang | 41d8a44 | 2017-05-16 15:36:54 -0700 | [diff] [blame] | 154 | .ClosureSetGlobal = |
| 155 | (ClosureSetGlobalFnPtr)dlsym(handle, "rsClosureSetGlobal"), |
| 156 | .ContextCreateVendor = |
| 157 | (ContextCreateVendorFnPtr)dlsym(handle, "rsContextCreateVendor"), |
| 158 | .ContextDeinitToClient = (ContextDeinitToClientFnPtr)dlsym( |
| 159 | handle, "rsContextDeinitToClient"), |
| 160 | .ContextDestroy = |
| 161 | (ContextDestroyFnPtr)dlsym(handle, "rsContextDestroy"), |
Miao Wang | 4772b60 | 2017-01-20 10:30:38 -0800 | [diff] [blame] | 162 | .ContextDump = (ContextDumpFnPtr)dlsym(handle, "rsContextDump"), |
| 163 | .ContextFinish = (ContextFinishFnPtr)dlsym(handle, "rsContextFinish"), |
Miao Wang | 41d8a44 | 2017-05-16 15:36:54 -0700 | [diff] [blame] | 164 | .ContextGetMessage = |
| 165 | (ContextGetMessageFnPtr)dlsym(handle, "rsContextGetMessage"), |
| 166 | .ContextInitToClient = |
| 167 | (ContextInitToClientFnPtr)dlsym(handle, "rsContextInitToClient"), |
| 168 | .ContextPeekMessage = |
| 169 | (ContextPeekMessageFnPtr)dlsym(handle, "rsContextPeekMessage"), |
| 170 | .ContextSendMessage = |
| 171 | (ContextSendMessageFnPtr)dlsym(handle, "rsContextSendMessage"), |
| 172 | .ContextSetCacheDir = |
| 173 | (ContextSetCacheDirFnPtr)dlsym(handle, "rsContextSetCacheDir"), |
| 174 | .ContextSetPriority = |
| 175 | (ContextSetPriorityFnPtr)dlsym(handle, "rsContextSetPriority"), |
| 176 | .DeviceCreate = (DeviceCreateFnPtr) nullptr, |
| 177 | .DeviceDestroy = (DeviceDestroyFnPtr) nullptr, |
| 178 | .DeviceSetConfig = (DeviceSetConfigFnPtr) nullptr, |
| 179 | .ElementCreate2 = |
| 180 | (ElementCreate2FnPtr)dlsym(handle, "rsElementCreate2"), |
Miao Wang | 4772b60 | 2017-01-20 10:30:38 -0800 | [diff] [blame] | 181 | .ElementCreate = (ElementCreateFnPtr)dlsym(handle, "rsElementCreate"), |
Miao Wang | 41d8a44 | 2017-05-16 15:36:54 -0700 | [diff] [blame] | 182 | .ElementGetNativeData = |
| 183 | (ElementGetNativeDataFnPtr)dlsym(handle, "rsaElementGetNativeData"), |
| 184 | .ElementGetSubElements = (ElementGetSubElementsFnPtr)dlsym( |
| 185 | handle, "rsaElementGetSubElements"), |
Miao Wang | 4772b60 | 2017-01-20 10:30:38 -0800 | [diff] [blame] | 186 | .GetName = (GetNameFnPtr)dlsym(handle, "rsaGetName"), |
Miao Wang | 41d8a44 | 2017-05-16 15:36:54 -0700 | [diff] [blame] | 187 | .InvokeClosureCreate = |
| 188 | (InvokeClosureCreateFnPtr)dlsym(handle, "rsInvokeClosureCreate"), |
Miao Wang | 4772b60 | 2017-01-20 10:30:38 -0800 | [diff] [blame] | 189 | .ObjDestroy = (ObjDestroyFnPtr)dlsym(handle, "rsObjDestroy"), |
| 190 | .SamplerCreate = (SamplerCreateFnPtr)dlsym(handle, "rsSamplerCreate"), |
Miao Wang | 41d8a44 | 2017-05-16 15:36:54 -0700 | [diff] [blame] | 191 | .ScriptBindAllocation = |
| 192 | (ScriptBindAllocationFnPtr)dlsym(handle, "rsScriptBindAllocation"), |
Miao Wang | 4772b60 | 2017-01-20 10:30:38 -0800 | [diff] [blame] | 193 | .ScriptCCreate = (ScriptCCreateFnPtr)dlsym(handle, "rsScriptCCreate"), |
Miao Wang | 41d8a44 | 2017-05-16 15:36:54 -0700 | [diff] [blame] | 194 | .ScriptFieldIDCreate = |
| 195 | (ScriptFieldIDCreateFnPtr)dlsym(handle, "rsScriptFieldIDCreate"), |
| 196 | .ScriptForEach = (ScriptForEachFnPtr) nullptr, |
| 197 | .ScriptForEachMulti = |
| 198 | (ScriptForEachMultiFnPtr)dlsym(handle, "rsScriptForEachMulti"), |
Miao Wang | 4772b60 | 2017-01-20 10:30:38 -0800 | [diff] [blame] | 199 | .ScriptGetVarV = (ScriptGetVarVFnPtr)dlsym(handle, "rsScriptGetVarV"), |
Miao Wang | 41d8a44 | 2017-05-16 15:36:54 -0700 | [diff] [blame] | 200 | .ScriptGroup2Create = |
| 201 | (ScriptGroup2CreateFnPtr)dlsym(handle, "rsScriptGroup2Create"), |
| 202 | .ScriptGroupCreate = |
| 203 | (ScriptGroupCreateFnPtr)dlsym(handle, "rsScriptGroupCreate"), |
| 204 | .ScriptGroupExecute = |
| 205 | (ScriptGroupExecuteFnPtr)dlsym(handle, "rsScriptGroupExecute"), |
| 206 | .ScriptGroupSetInput = |
| 207 | (ScriptGroupSetInputFnPtr)dlsym(handle, "rsScriptGroupSetInput"), |
| 208 | .ScriptGroupSetOutput = |
| 209 | (ScriptGroupSetOutputFnPtr)dlsym(handle, "rsScriptGroupSetOutput"), |
| 210 | .ScriptIntrinsicCreate = (ScriptIntrinsicCreateFnPtr)dlsym( |
| 211 | handle, "rsScriptIntrinsicCreate"), |
Miao Wang | 4772b60 | 2017-01-20 10:30:38 -0800 | [diff] [blame] | 212 | .ScriptInvoke = (ScriptInvokeFnPtr)dlsym(handle, "rsScriptInvoke"), |
Miao Wang | 41d8a44 | 2017-05-16 15:36:54 -0700 | [diff] [blame] | 213 | .ScriptInvokeIDCreate = |
| 214 | (ScriptInvokeIDCreateFnPtr)dlsym(handle, "rsScriptInvokeIDCreate"), |
Miao Wang | 4772b60 | 2017-01-20 10:30:38 -0800 | [diff] [blame] | 215 | .ScriptInvokeV = (ScriptInvokeVFnPtr)dlsym(handle, "rsScriptInvokeV"), |
Miao Wang | 41d8a44 | 2017-05-16 15:36:54 -0700 | [diff] [blame] | 216 | .ScriptKernelIDCreate = |
| 217 | (ScriptKernelIDCreateFnPtr)dlsym(handle, "rsScriptKernelIDCreate"), |
Miao Wang | 4772b60 | 2017-01-20 10:30:38 -0800 | [diff] [blame] | 218 | .ScriptReduce = (ScriptReduceFnPtr)dlsym(handle, "rsScriptReduce"), |
Miao Wang | 41d8a44 | 2017-05-16 15:36:54 -0700 | [diff] [blame] | 219 | .ScriptSetTimeZone = |
| 220 | (ScriptSetTimeZoneFnPtr)dlsym(handle, "rsScriptSetTimeZone"), |
Miao Wang | 4772b60 | 2017-01-20 10:30:38 -0800 | [diff] [blame] | 221 | .ScriptSetVarD = (ScriptSetVarDFnPtr)dlsym(handle, "rsScriptSetVarD"), |
| 222 | .ScriptSetVarF = (ScriptSetVarFFnPtr)dlsym(handle, "rsScriptSetVarF"), |
| 223 | .ScriptSetVarI = (ScriptSetVarIFnPtr)dlsym(handle, "rsScriptSetVarI"), |
| 224 | .ScriptSetVarJ = (ScriptSetVarJFnPtr)dlsym(handle, "rsScriptSetVarJ"), |
Miao Wang | 41d8a44 | 2017-05-16 15:36:54 -0700 | [diff] [blame] | 225 | .ScriptSetVarObj = |
| 226 | (ScriptSetVarObjFnPtr)dlsym(handle, "rsScriptSetVarObj"), |
| 227 | .ScriptSetVarVE = |
| 228 | (ScriptSetVarVEFnPtr)dlsym(handle, "rsScriptSetVarVE"), |
Miao Wang | 4772b60 | 2017-01-20 10:30:38 -0800 | [diff] [blame] | 229 | .ScriptSetVarV = (ScriptSetVarVFnPtr)dlsym(handle, "rsScriptSetVarV"), |
| 230 | .TypeCreate = (TypeCreateFnPtr)dlsym(handle, "rsTypeCreate"), |
Miao Wang | 41d8a44 | 2017-05-16 15:36:54 -0700 | [diff] [blame] | 231 | .TypeGetNativeData = |
| 232 | (TypeGetNativeDataFnPtr)dlsym(handle, "rsaTypeGetNativeData"), |
Miao Wang | 4772b60 | 2017-01-20 10:30:38 -0800 | [diff] [blame] | 233 | }; |
| 234 | |
| 235 | return dispatchHal; |
| 236 | } |
| 237 | |
| 238 | } // namespace implementation |
| 239 | } // namespace V1_0 |
| 240 | } // namespace renderscript |
| 241 | } // namespace hardware |
| 242 | } // namespace android |