1 /*
2  * Copyright(C) 2017 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 #ifndef ANDROID_RS_HIDL_ADAPTATION_H
18 #define ANDROID_RS_HIDL_ADAPTATION_H
19 
20 #include <android/hardware/renderscript/1.0/IDevice.h>
21 #include <set>
22 #include <mutex>
23 
24 struct dispatchTable;
25 
26 using ::android::hardware::renderscript::V1_0::IContext;
27 
28 /*
29  * RsHidlAdaptation is a singleton class.
30  *
31  * RsHidlAdaptation is used for the following purposes:
32  * 1. Load the dispatch table with HIDL implementation or CPU fallback.
33  * 2. Convert input paramters to HIDL types, when using HIDL path.
34  * 3. Convert output parameters from HIDL types to RS types, when using HIDL path.
35  *
36  * Access of static member objects is protected by global teardown flag.
37  * https://goto.google.com/rs-static-destructor
38  *
39  */
40 class RsHidlAdaptation
41 {
42 public:
43     static RsHidlAdaptation& GetInstance();
44     static const dispatchTable* GetEntryFuncs();
45 
46 private:
47     RsHidlAdaptation();
48     void InitializeHalDeviceContext();
49     void LoadDispatchForHidl();
50     static IContext *GetIContextHandle(RsContext con);
51 
52     // HAL entries
53     static RsContext ContextCreate(RsDevice vdev, uint32_t version, uint32_t sdkVersion, RsContextType ct, uint32_t flags);
54     static void ContextDestroy(RsContext);
55     static void ContextFinish(RsContext);
56     static void ContextDump(RsContext, int32_t);
57     static void ContextSetCacheDir(RsContext rsc, const char *cacheDir, size_t cacheDir_length);
58     static void ContextSetPriority(RsContext, int32_t);
59     static RsMessageToClientType ContextGetMessage(RsContext, void*, size_t, size_t*, size_t, uint32_t*, size_t);
60     static RsMessageToClientType ContextPeekMessage(RsContext, size_t*, size_t, uint32_t*, size_t);
61     static void ContextSendMessage(RsContext, uint32_t, const uint8_t*, size_t);
62     static void ContextInitToClient(RsContext);
63     static void ContextDeinitToClient(RsContext);
64 
65     static void AssignName(RsContext, RsObjectBase, const char*, size_t);
66     static void GetName(RsContext, void * obj, const char **name);
67     static void ObjDestroy(RsContext, RsAsyncVoidPtr);
68 
69     static RsElement ElementCreate(RsContext, RsDataType, RsDataKind, bool, uint32_t);
70     static RsElement ElementCreate2(RsContext, const RsElement*, size_t, const char**, size_t, const size_t*, const uint32_t*, size_t);
71     static void ElementGetNativeData(RsContext, RsElement, uint32_t *elemData, uint32_t elemDataSize);
72     static void ElementGetSubElements(RsContext, RsElement, uintptr_t *ids, const char **names, size_t *arraySizes, uint32_t dataSize);
73 
74     static RsType TypeCreate(RsContext, RsElement, uint32_t, uint32_t, uint32_t, bool, bool, uint32_t);
75     static void TypeGetNativeData(RsContext, RsType, uintptr_t *typeData, uint32_t typeDataSize);
76 
77     static RsAllocation AllocationCreateTyped(RsContext, RsType, RsAllocationMipmapControl, uint32_t, uintptr_t);
78     static RsAllocation AllocationCreateFromBitmap(RsContext, RsType, RsAllocationMipmapControl, const void*, size_t, uint32_t);
79     static RsAllocation AllocationCubeCreateFromBitmap(RsContext, RsType, RsAllocationMipmapControl, const void*, size_t, uint32_t);
80     static const void* AllocationGetType(RsContext con, RsAllocation va);
81     static RsNativeWindow AllocationGetSurface(RsContext, RsAllocation);
82     static void AllocationSetSurface(RsContext, RsAllocation, RsNativeWindow);
83     static void AllocationCopyToBitmap(RsContext, RsAllocation, void*, size_t);
84     static void Allocation1DData(RsContext, RsAllocation, uint32_t, uint32_t, uint32_t, const void*, size_t);
85     static void Allocation1DElementData(RsContext, RsAllocation, uint32_t, uint32_t, const void*, size_t, size_t);
86     static void AllocationElementData(RsContext, RsAllocation, uint32_t, uint32_t, uint32_t, uint32_t, const void*, size_t, size_t);
87     static void Allocation2DData(RsContext, RsAllocation, uint32_t, uint32_t, uint32_t, RsAllocationCubemapFace, uint32_t, uint32_t, const void*, size_t, size_t);
88     static void Allocation3DData(RsContext, RsAllocation, uint32_t, uint32_t, uint32_t, uint32_t, uint32_t, uint32_t, uint32_t, const void*, size_t, size_t);
89     static void AllocationGenerateMipmaps(RsContext, RsAllocation);
90     static void AllocationRead(RsContext, RsAllocation, void*, size_t);
91     static void Allocation1DRead(RsContext, RsAllocation, uint32_t, uint32_t, uint32_t, void*, size_t);
92     static void AllocationElementRead(RsContext, RsAllocation, uint32_t, uint32_t, uint32_t, uint32_t, void*, size_t, size_t);
93     static void Allocation2DRead(RsContext, RsAllocation, uint32_t, uint32_t, uint32_t, RsAllocationCubemapFace, uint32_t, uint32_t, void*, size_t, size_t);
94     static void Allocation3DRead(RsContext, RsAllocation, uint32_t, uint32_t, uint32_t, uint32_t, uint32_t, uint32_t, uint32_t, void*, size_t, size_t);
95     static void AllocationSyncAll(RsContext, RsAllocation, RsAllocationUsageType);
96     static void AllocationResize1D(RsContext, RsAllocation, uint32_t);
97     static void AllocationCopy2DRange(RsContext, RsAllocation, uint32_t, uint32_t, uint32_t, uint32_t, uint32_t, uint32_t, RsAllocation, uint32_t, uint32_t, uint32_t, uint32_t);
98     static void AllocationCopy3DRange(RsContext, RsAllocation, uint32_t, uint32_t, uint32_t, uint32_t, uint32_t, uint32_t, uint32_t, RsAllocation, uint32_t, uint32_t, uint32_t, uint32_t);
99     static void AllocationIoSend(RsContext, RsAllocation);
100     static int64_t AllocationIoReceive(RsContext, RsAllocation);
101     static void *AllocationGetPointer(RsContext, RsAllocation, uint32_t lod, RsAllocationCubemapFace face, uint32_t z, uint32_t array, size_t *stride, size_t stride_len);
102     static void AllocationSetupBufferQueue(RsContext context, RsAllocation valloc, uint32_t numAlloc);
103     static void AllocationShareBufferQueue(RsContext context, RsAllocation valloc1, RsAllocation valloc2);
104     static RsAllocation AllocationAdapterCreate(RsContext rsc, RsType vtype, RsAllocation baseAlloc);
105     static void AllocationAdapterOffset(RsContext rsc, RsAllocation alloc, const uint32_t * offsets, size_t offsets_length);
106 
107     static RsScript ScriptCCreate(RsContext, const char*, size_t, const char*, size_t, const char*, size_t);
108     static RsScript ScriptIntrinsicCreate(RsContext, uint32_t id, RsElement);
109     static void ScriptBindAllocation(RsContext, RsScript, RsAllocation, uint32_t);
110     static void ScriptSetTimeZone(RsContext, RsScript, const char*, size_t);
111     static void ScriptInvoke(RsContext, RsScript, uint32_t);
112     static void ScriptInvokeV(RsContext, RsScript, uint32_t, const void*, size_t);
113     static void ScriptForEach(RsContext, RsScript, uint32_t, RsAllocation, RsAllocation, const void*, size_t, const RsScriptCall*, size_t);
114     static void ScriptForEachMulti(RsContext, RsScript, uint32_t, RsAllocation*, size_t, RsAllocation, const void*, size_t, const RsScriptCall*, size_t);
115     static void ScriptReduce(RsContext, RsScript, uint32_t, RsAllocation*, size_t, RsAllocation, const RsScriptCall*, size_t);
116     static void ScriptSetVarI(RsContext, RsScript, uint32_t, int);
117     static void ScriptSetVarObj(RsContext, RsScript, uint32_t, RsObjectBase);
118     static void ScriptSetVarJ(RsContext, RsScript, uint32_t, int64_t);
119     static void ScriptSetVarF(RsContext, RsScript, uint32_t, float);
120     static void ScriptSetVarD(RsContext, RsScript, uint32_t, double);
121     static void ScriptSetVarV(RsContext, RsScript, uint32_t, const void*, size_t);
122     static void ScriptGetVarV(RsContext, RsScript, uint32_t, void*, size_t);
123     static void ScriptSetVarVE(RsContext, RsScript, uint32_t, const void*, size_t, RsElement, const uint32_t*, size_t);
124 
125     static RsScriptGroup ScriptGroupCreate(RsContext, RsScriptKernelID*, size_t, RsScriptKernelID*, size_t, RsScriptKernelID*, size_t, RsScriptFieldID*, size_t, const RsType*, size_t);
126     static RsScriptGroup2 ScriptGroup2Create(RsContext, const char*, size_t, const char*, size_t, RsClosure*, size_t);
127     static RsScriptKernelID ScriptKernelIDCreate(RsContext, RsScript, int, int);
128     static RsScriptInvokeID ScriptInvokeIDCreate(RsContext, RsScript, int);
129     static RsScriptFieldID ScriptFieldIDCreate(RsContext, RsScript, int);
130     static void ScriptGroupSetOutput(RsContext, RsScriptGroup, RsScriptKernelID, RsAllocation);
131     static void ScriptGroupSetInput(RsContext, RsScriptGroup, RsScriptKernelID, RsAllocation);
132     static void ScriptGroupExecute(RsContext, RsScriptGroup);
133     static RsClosure ClosureCreate(RsContext context, RsScriptKernelID kernelID, RsAllocation returnValue, RsScriptFieldID* fieldIDs, size_t fieldIDs_length, int64_t* values, size_t values_length, int* sizes, size_t sizes_length, RsClosure* depClosures, size_t depClosures_length, RsScriptFieldID* depFieldIDs, size_t depFieldIDs_length);
134     static RsClosure InvokeClosureCreate(RsContext, RsScriptInvokeID, const void*, const size_t, const RsScriptFieldID*, const size_t, const int64_t*, const size_t, const int*, const size_t);
135     static void ClosureSetArg(RsContext, RsClosure, uint32_t, uintptr_t, int);
136     static void ClosureSetGlobal(RsContext, RsClosure, RsScriptFieldID, int64_t, int);
137 
138     static RsSampler SamplerCreate(RsContext, RsSamplerValue, RsSamplerValue, RsSamplerValue, RsSamplerValue, RsSamplerValue, float);
139 
140     // Dispatch table entries for HIDL. The dispatch table is initialized by the
141     // constructor of this singleton class, to avoid concurrency issues.
142     static dispatchTable mEntryFuncs;
143 
144     // HIDL instance. This is a singleton HIDL instance, providing a handle for
145     // RenderScript HIDL implementation (driver). The handle is created by the
146     // constructor of this singleton class.
147     static android::sp<android::hardware::renderscript::V1_0::IDevice> mHidl;
148 
149     // mContexts keeps Treble returned sp<IContext>s alive.
150     static std::set<android::sp<IContext> > mContexts;
151 
152     // mMutex is used to protect concurrent modification to the mContexts set.
153     // std::mutex is safe for pthreads on Android. Since other threading model
154     // supported on Android are built on top of pthread, std::mutex is safe for them.
155     static std::mutex mContextsMutex;
156 };
157 
158 #endif // ANDROID_RS_HIDL_ADAPTATION_H
159