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
17 #include "Context.h"
18 #include "Device.h"
19
20 #include <android-base/logging.h>
21 #include <android/dlext.h>
22 #include <dlfcn.h>
23
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
Device()34 Device::Device() {
35 }
36
getHal()37 dispatchTable& Device::getHal() {
38 return mDispatchHal;
39 }
40
41
42 // Methods from ::android::hardware::renderscript::V1_0::IDevice follow.
43
contextCreate(uint32_t sdkVersion,ContextType ct,int32_t flags)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
HIDL_FETCH_IDevice(const char *)51 IDevice* HIDL_FETCH_IDevice(const char* /* name */) {
52 return new Device();
53 }
54
55 // Helper function
loadHAL()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";
62 // Try to load libRS_internal.so from the "rs" namespace directly.
63 typedef struct android_namespace_t* (*GetExportedNamespaceFnPtr)(const char*);
64 GetExportedNamespaceFnPtr getExportedNamespace = reinterpret_cast<GetExportedNamespaceFnPtr>(
65 dlsym(RTLD_DEFAULT, "android_get_exported_namespace"));
66 void* handle = nullptr;
67 if (getExportedNamespace != nullptr) {
68 android_namespace_t* rsNamespace = getExportedNamespace("rs");
69 if (rsNamespace != nullptr) {
70 const android_dlextinfo dlextinfo = {
71 .flags = ANDROID_DLEXT_USE_NAMESPACE, .library_namespace = rsNamespace,
72 };
73 handle = android_dlopen_ext(filename, RTLD_LAZY | RTLD_LOCAL, &dlextinfo);
74 if (handle == nullptr) {
75 LOG(WARNING) << "android_dlopen_ext(" << filename << ") failed: " << dlerror();
76 }
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);
83 if (handle == nullptr) {
84 LOG(FATAL) << "dlopen(" << filename << ") failed: " << dlerror();
85 }
86 }
87
88 dispatchTable dispatchHal = {
89 .SetNativeLibDir = (SetNativeLibDirFnPtr) nullptr,
90
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"),
151 .AssignName = (AssignNameFnPtr)dlsym(handle, "rsAssignName"),
152 .ClosureCreate = (ClosureCreateFnPtr)dlsym(handle, "rsClosureCreate"),
153 .ClosureSetArg = (ClosureSetArgFnPtr)dlsym(handle, "rsClosureSetArg"),
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"),
162 .ContextDump = (ContextDumpFnPtr)dlsym(handle, "rsContextDump"),
163 .ContextFinish = (ContextFinishFnPtr)dlsym(handle, "rsContextFinish"),
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"),
181 .ElementCreate = (ElementCreateFnPtr)dlsym(handle, "rsElementCreate"),
182 .ElementGetNativeData =
183 (ElementGetNativeDataFnPtr)dlsym(handle, "rsaElementGetNativeData"),
184 .ElementGetSubElements = (ElementGetSubElementsFnPtr)dlsym(
185 handle, "rsaElementGetSubElements"),
186 .GetName = (GetNameFnPtr)dlsym(handle, "rsaGetName"),
187 .InvokeClosureCreate =
188 (InvokeClosureCreateFnPtr)dlsym(handle, "rsInvokeClosureCreate"),
189 .ObjDestroy = (ObjDestroyFnPtr)dlsym(handle, "rsObjDestroy"),
190 .SamplerCreate = (SamplerCreateFnPtr)dlsym(handle, "rsSamplerCreate"),
191 .ScriptBindAllocation =
192 (ScriptBindAllocationFnPtr)dlsym(handle, "rsScriptBindAllocation"),
193 .ScriptCCreate = (ScriptCCreateFnPtr)dlsym(handle, "rsScriptCCreate"),
194 .ScriptFieldIDCreate =
195 (ScriptFieldIDCreateFnPtr)dlsym(handle, "rsScriptFieldIDCreate"),
196 .ScriptForEach = (ScriptForEachFnPtr) nullptr,
197 .ScriptForEachMulti =
198 (ScriptForEachMultiFnPtr)dlsym(handle, "rsScriptForEachMulti"),
199 .ScriptGetVarV = (ScriptGetVarVFnPtr)dlsym(handle, "rsScriptGetVarV"),
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"),
212 .ScriptInvoke = (ScriptInvokeFnPtr)dlsym(handle, "rsScriptInvoke"),
213 .ScriptInvokeIDCreate =
214 (ScriptInvokeIDCreateFnPtr)dlsym(handle, "rsScriptInvokeIDCreate"),
215 .ScriptInvokeV = (ScriptInvokeVFnPtr)dlsym(handle, "rsScriptInvokeV"),
216 .ScriptKernelIDCreate =
217 (ScriptKernelIDCreateFnPtr)dlsym(handle, "rsScriptKernelIDCreate"),
218 .ScriptReduce = (ScriptReduceFnPtr)dlsym(handle, "rsScriptReduce"),
219 .ScriptSetTimeZone =
220 (ScriptSetTimeZoneFnPtr)dlsym(handle, "rsScriptSetTimeZone"),
221 .ScriptSetVarD = (ScriptSetVarDFnPtr)dlsym(handle, "rsScriptSetVarD"),
222 .ScriptSetVarF = (ScriptSetVarFFnPtr)dlsym(handle, "rsScriptSetVarF"),
223 .ScriptSetVarI = (ScriptSetVarIFnPtr)dlsym(handle, "rsScriptSetVarI"),
224 .ScriptSetVarJ = (ScriptSetVarJFnPtr)dlsym(handle, "rsScriptSetVarJ"),
225 .ScriptSetVarObj =
226 (ScriptSetVarObjFnPtr)dlsym(handle, "rsScriptSetVarObj"),
227 .ScriptSetVarVE =
228 (ScriptSetVarVEFnPtr)dlsym(handle, "rsScriptSetVarVE"),
229 .ScriptSetVarV = (ScriptSetVarVFnPtr)dlsym(handle, "rsScriptSetVarV"),
230 .TypeCreate = (TypeCreateFnPtr)dlsym(handle, "rsTypeCreate"),
231 .TypeGetNativeData =
232 (TypeGetNativeDataFnPtr)dlsym(handle, "rsaTypeGetNativeData"),
233 };
234
235 return dispatchHal;
236 }
237
238 } // namespace implementation
239 } // namespace V1_0
240 } // namespace renderscript
241 } // namespace hardware
242 } // namespace android
243