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 = (Allocation1DDataFnPtr)dlsym(handle, "rsAllocation1DData"),
92             .Allocation1DElementData = (Allocation1DElementDataFnPtr) nullptr,
93             .Allocation1DRead = (Allocation1DReadFnPtr)dlsym(handle, "rsAllocation1DRead"),
94             .Allocation2DData = (Allocation2DDataFnPtr)dlsym(handle, "rsAllocation2DData"),
95             .Allocation2DRead = (Allocation2DReadFnPtr)dlsym(handle, "rsAllocation2DRead"),
96             .Allocation3DData = (Allocation3DDataFnPtr)dlsym(handle, "rsAllocation3DData"),
97             .Allocation3DRead = (Allocation3DReadFnPtr)dlsym(handle, "rsAllocation3DRead"),
98             .AllocationAdapterCreate =
99                     (AllocationAdapterCreateFnPtr)dlsym(handle, "rsAllocationAdapterCreate"),
100             .AllocationAdapterOffset =
101                     (AllocationAdapterOffsetFnPtr)dlsym(handle, "rsAllocationAdapterOffset"),
102             .AllocationCopy2DRange =
103                     (AllocationCopy2DRangeFnPtr)dlsym(handle, "rsAllocationCopy2DRange"),
104             .AllocationCopy3DRange =
105                     (AllocationCopy3DRangeFnPtr)dlsym(handle, "rsAllocationCopy3DRange"),
106             .AllocationCopyToBitmap =
107                     (AllocationCopyToBitmapFnPtr)dlsym(handle, "rsAllocationCopyToBitmap"),
108             .AllocationCreateFromBitmap =
109                     (AllocationCreateFromBitmapFnPtr)dlsym(handle, "rsAllocationCreateFromBitmap"),
110             .AllocationCreateStrided =
111                     (AllocationCreateStridedFnPtr)dlsym(handle, "rsAllocationCreateStrided"),
112             .AllocationCreateTyped =
113                     (AllocationCreateTypedFnPtr)dlsym(handle, "rsAllocationCreateTyped"),
114             .AllocationCubeCreateFromBitmap = (AllocationCubeCreateFromBitmapFnPtr)dlsym(
115                     handle, "rsAllocationCubeCreateFromBitmap"),
116             .AllocationElementData =
117                     (AllocationElementDataFnPtr)dlsym(handle, "rsAllocationElementData"),
118             .AllocationElementRead =
119                     (AllocationElementReadFnPtr)dlsym(handle, "rsAllocationElementRead"),
120             .AllocationGenerateMipmaps =
121                     (AllocationGenerateMipmapsFnPtr)dlsym(handle, "rsAllocationGenerateMipmaps"),
122             .AllocationGetPointer =
123                     (AllocationGetPointerFnPtr)dlsym(handle, "rsAllocationGetPointer"),
124             .AllocationGetSurface =
125                     (AllocationGetSurfaceFnPtr)dlsym(handle, "rsAllocationGetSurface"),
126             .AllocationGetType = (AllocationGetTypeFnPtr)dlsym(handle, "rsaAllocationGetType"),
127             .AllocationIoReceive = (AllocationIoReceiveFnPtr)dlsym(handle, "rsAllocationIoReceive"),
128             .AllocationIoSend = (AllocationIoSendFnPtr)dlsym(handle, "rsAllocationIoSend"),
129             .AllocationRead = (AllocationReadFnPtr)dlsym(handle, "rsAllocationRead"),
130             .AllocationResize1D = (AllocationResize1DFnPtr)dlsym(handle, "rsAllocationResize1D"),
131             .AllocationSetSurface =
132                     (AllocationSetSurfaceFnPtr)dlsym(handle, "rsAllocationSetSurface"),
133             .AllocationSyncAll = (AllocationSyncAllFnPtr)dlsym(handle, "rsAllocationSyncAll"),
134             .AllocationSetupBufferQueue =
135                     (AllocationSetupBufferQueueFnPtr)dlsym(handle, "rsAllocationSetupBufferQueue"),
136             .AllocationShareBufferQueue =
137                     (AllocationShareBufferQueueFnPtr)dlsym(handle, "rsAllocationShareBufferQueue"),
138             .AssignName = (AssignNameFnPtr)dlsym(handle, "rsAssignName"),
139             .ClosureCreate = (ClosureCreateFnPtr)dlsym(handle, "rsClosureCreate"),
140             .ClosureSetArg = (ClosureSetArgFnPtr)dlsym(handle, "rsClosureSetArg"),
141             .ClosureSetGlobal = (ClosureSetGlobalFnPtr)dlsym(handle, "rsClosureSetGlobal"),
142             .ContextCreateVendor = (ContextCreateVendorFnPtr)dlsym(handle, "rsContextCreateVendor"),
143             .ContextDeinitToClient =
144                     (ContextDeinitToClientFnPtr)dlsym(handle, "rsContextDeinitToClient"),
145             .ContextDestroy = (ContextDestroyFnPtr)dlsym(handle, "rsContextDestroy"),
146             .ContextDump = (ContextDumpFnPtr)dlsym(handle, "rsContextDump"),
147             .ContextFinish = (ContextFinishFnPtr)dlsym(handle, "rsContextFinish"),
148             .ContextGetMessage = (ContextGetMessageFnPtr)dlsym(handle, "rsContextGetMessage"),
149             .ContextInitToClient = (ContextInitToClientFnPtr)dlsym(handle, "rsContextInitToClient"),
150             .ContextPeekMessage = (ContextPeekMessageFnPtr)dlsym(handle, "rsContextPeekMessage"),
151             .ContextSendMessage = (ContextSendMessageFnPtr)dlsym(handle, "rsContextSendMessage"),
152             .ContextSetPriority = (ContextSetPriorityFnPtr)dlsym(handle, "rsContextSetPriority"),
153             .ContextSetCacheDir = (ContextSetCacheDirFnPtr)dlsym(handle, "rsContextSetCacheDir"),
154             .DeviceCreate = (DeviceCreateFnPtr) nullptr,
155             .DeviceDestroy = (DeviceDestroyFnPtr) nullptr,
156             .DeviceSetConfig = (DeviceSetConfigFnPtr) nullptr,
157             .ElementCreate2 = (ElementCreate2FnPtr)dlsym(handle, "rsElementCreate2"),
158             .ElementCreate = (ElementCreateFnPtr)dlsym(handle, "rsElementCreate"),
159             .ElementGetNativeData =
160                     (ElementGetNativeDataFnPtr)dlsym(handle, "rsaElementGetNativeData"),
161             .ElementGetSubElements =
162                     (ElementGetSubElementsFnPtr)dlsym(handle, "rsaElementGetSubElements"),
163             .GetName = (GetNameFnPtr)dlsym(handle, "rsaGetName"),
164             .InvokeClosureCreate = (InvokeClosureCreateFnPtr)dlsym(handle, "rsInvokeClosureCreate"),
165             .ObjDestroy = (ObjDestroyFnPtr)dlsym(handle, "rsObjDestroy"),
166             .SamplerCreate = (SamplerCreateFnPtr)dlsym(handle, "rsSamplerCreate"),
167             .ScriptBindAllocation =
168                     (ScriptBindAllocationFnPtr)dlsym(handle, "rsScriptBindAllocation"),
169             .ScriptCCreate = (ScriptCCreateFnPtr)dlsym(handle, "rsScriptCCreate"),
170             .ScriptFieldIDCreate = (ScriptFieldIDCreateFnPtr)dlsym(handle, "rsScriptFieldIDCreate"),
171             .ScriptForEach = (ScriptForEachFnPtr) nullptr,
172             .ScriptForEachMulti = (ScriptForEachMultiFnPtr)dlsym(handle, "rsScriptForEachMulti"),
173             .ScriptGetVarV = (ScriptGetVarVFnPtr)dlsym(handle, "rsScriptGetVarV"),
174             .ScriptGroup2Create = (ScriptGroup2CreateFnPtr)dlsym(handle, "rsScriptGroup2Create"),
175             .ScriptGroupCreate = (ScriptGroupCreateFnPtr)dlsym(handle, "rsScriptGroupCreate"),
176             .ScriptGroupExecute = (ScriptGroupExecuteFnPtr)dlsym(handle, "rsScriptGroupExecute"),
177             .ScriptGroupSetInput = (ScriptGroupSetInputFnPtr)dlsym(handle, "rsScriptGroupSetInput"),
178             .ScriptGroupSetOutput =
179                     (ScriptGroupSetOutputFnPtr)dlsym(handle, "rsScriptGroupSetOutput"),
180             .ScriptIntrinsicCreate =
181                     (ScriptIntrinsicCreateFnPtr)dlsym(handle, "rsScriptIntrinsicCreate"),
182             .ScriptInvoke = (ScriptInvokeFnPtr)dlsym(handle, "rsScriptInvoke"),
183             .ScriptInvokeIDCreate =
184                     (ScriptInvokeIDCreateFnPtr)dlsym(handle, "rsScriptInvokeIDCreate"),
185             .ScriptInvokeV = (ScriptInvokeVFnPtr)dlsym(handle, "rsScriptInvokeV"),
186             .ScriptKernelIDCreate =
187                     (ScriptKernelIDCreateFnPtr)dlsym(handle, "rsScriptKernelIDCreate"),
188             .ScriptReduce = (ScriptReduceFnPtr)dlsym(handle, "rsScriptReduce"),
189             .ScriptSetTimeZone = (ScriptSetTimeZoneFnPtr)dlsym(handle, "rsScriptSetTimeZone"),
190             .ScriptSetVarD = (ScriptSetVarDFnPtr)dlsym(handle, "rsScriptSetVarD"),
191             .ScriptSetVarF = (ScriptSetVarFFnPtr)dlsym(handle, "rsScriptSetVarF"),
192             .ScriptSetVarI = (ScriptSetVarIFnPtr)dlsym(handle, "rsScriptSetVarI"),
193             .ScriptSetVarJ = (ScriptSetVarJFnPtr)dlsym(handle, "rsScriptSetVarJ"),
194             .ScriptSetVarObj = (ScriptSetVarObjFnPtr)dlsym(handle, "rsScriptSetVarObj"),
195             .ScriptSetVarVE = (ScriptSetVarVEFnPtr)dlsym(handle, "rsScriptSetVarVE"),
196             .ScriptSetVarV = (ScriptSetVarVFnPtr)dlsym(handle, "rsScriptSetVarV"),
197             .TypeCreate = (TypeCreateFnPtr)dlsym(handle, "rsTypeCreate"),
198             .TypeGetNativeData = (TypeGetNativeDataFnPtr)dlsym(handle, "rsaTypeGetNativeData"),
199     };
200 
201     return dispatchHal;
202 }
203 
204 }  // namespace implementation
205 }  // namespace V1_0
206 }  // namespace renderscript
207 }  // namespace hardware
208 }  // namespace android
209