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