blob: 8fda3ff1e26de812073132c1f77f64d0fb13de82 [file] [log] [blame]
Miao Wang4772b602017-01-20 10:30:38 -08001#include "Context.h"
2#include "Device.h"
3
Brian Carlstrom3f2d71f2018-01-24 19:22:30 -08004#include <android-base/logging.h>
Jiyong Park0f709052017-08-16 23:30:42 +09005#include <android/dlext.h>
6#include <dlfcn.h>
7
Miao Wang4772b602017-01-20 10:30:38 -08008namespace android {
9namespace hardware {
10namespace renderscript {
11namespace V1_0 {
12namespace implementation {
13
14
15static dispatchTable loadHAL();
16dispatchTable Device::mDispatchHal = loadHAL();
17
18Device::Device() {
19}
20
21dispatchTable& Device::getHal() {
22 return mDispatchHal;
23}
24
25
26// Methods from ::android::hardware::renderscript::V1_0::IDevice follow.
27
28Return<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
35IDevice* HIDL_FETCH_IDevice(const char* /* name */) {
36 return new Device();
37}
38
39// Helper function
40dispatchTable 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";
Jiyong Park0f709052017-08-16 23:30:42 +090046 // Try to load libRS_internal.so from the "rs" namespace directly.
47 typedef struct android_namespace_t* (*GetExportedNamespaceFnPtr)(const char*);
Jae Shin6a708752017-10-20 17:28:41 +090048 GetExportedNamespaceFnPtr getExportedNamespace = reinterpret_cast<GetExportedNamespaceFnPtr>(
49 dlsym(RTLD_DEFAULT, "android_get_exported_namespace"));
Jiyong Park0f709052017-08-16 23:30:42 +090050 void* handle = nullptr;
51 if (getExportedNamespace != nullptr) {
Jae Shin6a708752017-10-20 17:28:41 +090052 android_namespace_t* rsNamespace = getExportedNamespace("rs");
53 if (rsNamespace != nullptr) {
Jiyong Park0f709052017-08-16 23:30:42 +090054 const android_dlextinfo dlextinfo = {
Jae Shin6a708752017-10-20 17:28:41 +090055 .flags = ANDROID_DLEXT_USE_NAMESPACE, .library_namespace = rsNamespace,
Jiyong Park0f709052017-08-16 23:30:42 +090056 };
57 handle = android_dlopen_ext(filename, RTLD_LAZY | RTLD_LOCAL, &dlextinfo);
Brian Carlstrom3f2d71f2018-01-24 19:22:30 -080058 if (handle == nullptr) {
59 LOG(WARNING) << "android_dlopen_ext(" << filename << ") failed: " << dlerror();
60 }
Jiyong Park0f709052017-08-16 23:30:42 +090061 }
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);
Brian Carlstrom3f2d71f2018-01-24 19:22:30 -080067 if (handle == nullptr) {
68 LOG(FATAL) << "dlopen(" << filename << ") failed: " << dlerror();
69 }
Jiyong Park0f709052017-08-16 23:30:42 +090070 }
Miao Wang4772b602017-01-20 10:30:38 -080071
72 dispatchTable dispatchHal = {
Miao Wang41d8a442017-05-16 15:36:54 -070073 .SetNativeLibDir = (SetNativeLibDirFnPtr) nullptr,
Miao Wang4772b602017-01-20 10:30:38 -080074
Miao Wang41d8a442017-05-16 15:36:54 -070075 .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"),
Miao Wang4772b602017-01-20 10:30:38 -0800135 .AssignName = (AssignNameFnPtr)dlsym(handle, "rsAssignName"),
136 .ClosureCreate = (ClosureCreateFnPtr)dlsym(handle, "rsClosureCreate"),
137 .ClosureSetArg = (ClosureSetArgFnPtr)dlsym(handle, "rsClosureSetArg"),
Miao Wang41d8a442017-05-16 15:36:54 -0700138 .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"),
Miao Wang4772b602017-01-20 10:30:38 -0800146 .ContextDump = (ContextDumpFnPtr)dlsym(handle, "rsContextDump"),
147 .ContextFinish = (ContextFinishFnPtr)dlsym(handle, "rsContextFinish"),
Miao Wang41d8a442017-05-16 15:36:54 -0700148 .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"),
Miao Wang4772b602017-01-20 10:30:38 -0800165 .ElementCreate = (ElementCreateFnPtr)dlsym(handle, "rsElementCreate"),
Miao Wang41d8a442017-05-16 15:36:54 -0700166 .ElementGetNativeData =
167 (ElementGetNativeDataFnPtr)dlsym(handle, "rsaElementGetNativeData"),
168 .ElementGetSubElements = (ElementGetSubElementsFnPtr)dlsym(
169 handle, "rsaElementGetSubElements"),
Miao Wang4772b602017-01-20 10:30:38 -0800170 .GetName = (GetNameFnPtr)dlsym(handle, "rsaGetName"),
Miao Wang41d8a442017-05-16 15:36:54 -0700171 .InvokeClosureCreate =
172 (InvokeClosureCreateFnPtr)dlsym(handle, "rsInvokeClosureCreate"),
Miao Wang4772b602017-01-20 10:30:38 -0800173 .ObjDestroy = (ObjDestroyFnPtr)dlsym(handle, "rsObjDestroy"),
174 .SamplerCreate = (SamplerCreateFnPtr)dlsym(handle, "rsSamplerCreate"),
Miao Wang41d8a442017-05-16 15:36:54 -0700175 .ScriptBindAllocation =
176 (ScriptBindAllocationFnPtr)dlsym(handle, "rsScriptBindAllocation"),
Miao Wang4772b602017-01-20 10:30:38 -0800177 .ScriptCCreate = (ScriptCCreateFnPtr)dlsym(handle, "rsScriptCCreate"),
Miao Wang41d8a442017-05-16 15:36:54 -0700178 .ScriptFieldIDCreate =
179 (ScriptFieldIDCreateFnPtr)dlsym(handle, "rsScriptFieldIDCreate"),
180 .ScriptForEach = (ScriptForEachFnPtr) nullptr,
181 .ScriptForEachMulti =
182 (ScriptForEachMultiFnPtr)dlsym(handle, "rsScriptForEachMulti"),
Miao Wang4772b602017-01-20 10:30:38 -0800183 .ScriptGetVarV = (ScriptGetVarVFnPtr)dlsym(handle, "rsScriptGetVarV"),
Miao Wang41d8a442017-05-16 15:36:54 -0700184 .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"),
Miao Wang4772b602017-01-20 10:30:38 -0800196 .ScriptInvoke = (ScriptInvokeFnPtr)dlsym(handle, "rsScriptInvoke"),
Miao Wang41d8a442017-05-16 15:36:54 -0700197 .ScriptInvokeIDCreate =
198 (ScriptInvokeIDCreateFnPtr)dlsym(handle, "rsScriptInvokeIDCreate"),
Miao Wang4772b602017-01-20 10:30:38 -0800199 .ScriptInvokeV = (ScriptInvokeVFnPtr)dlsym(handle, "rsScriptInvokeV"),
Miao Wang41d8a442017-05-16 15:36:54 -0700200 .ScriptKernelIDCreate =
201 (ScriptKernelIDCreateFnPtr)dlsym(handle, "rsScriptKernelIDCreate"),
Miao Wang4772b602017-01-20 10:30:38 -0800202 .ScriptReduce = (ScriptReduceFnPtr)dlsym(handle, "rsScriptReduce"),
Miao Wang41d8a442017-05-16 15:36:54 -0700203 .ScriptSetTimeZone =
204 (ScriptSetTimeZoneFnPtr)dlsym(handle, "rsScriptSetTimeZone"),
Miao Wang4772b602017-01-20 10:30:38 -0800205 .ScriptSetVarD = (ScriptSetVarDFnPtr)dlsym(handle, "rsScriptSetVarD"),
206 .ScriptSetVarF = (ScriptSetVarFFnPtr)dlsym(handle, "rsScriptSetVarF"),
207 .ScriptSetVarI = (ScriptSetVarIFnPtr)dlsym(handle, "rsScriptSetVarI"),
208 .ScriptSetVarJ = (ScriptSetVarJFnPtr)dlsym(handle, "rsScriptSetVarJ"),
Miao Wang41d8a442017-05-16 15:36:54 -0700209 .ScriptSetVarObj =
210 (ScriptSetVarObjFnPtr)dlsym(handle, "rsScriptSetVarObj"),
211 .ScriptSetVarVE =
212 (ScriptSetVarVEFnPtr)dlsym(handle, "rsScriptSetVarVE"),
Miao Wang4772b602017-01-20 10:30:38 -0800213 .ScriptSetVarV = (ScriptSetVarVFnPtr)dlsym(handle, "rsScriptSetVarV"),
214 .TypeCreate = (TypeCreateFnPtr)dlsym(handle, "rsTypeCreate"),
Miao Wang41d8a442017-05-16 15:36:54 -0700215 .TypeGetNativeData =
216 (TypeGetNativeDataFnPtr)dlsym(handle, "rsaTypeGetNativeData"),
Miao Wang4772b602017-01-20 10:30:38 -0800217 };
218
219 return dispatchHal;
220}
221
222} // namespace implementation
223} // namespace V1_0
224} // namespace renderscript
225} // namespace hardware
226} // namespace android