10838b98ea2d72bd2262bf9bff5aca093699d64ffMiao Wang/*
20838b98ea2d72bd2262bf9bff5aca093699d64ffMiao Wang * Copyright(C) 2017 The Android Open Source Project
30838b98ea2d72bd2262bf9bff5aca093699d64ffMiao Wang *
40838b98ea2d72bd2262bf9bff5aca093699d64ffMiao Wang * Licensed under the Apache License, Version 2.0 (the "License");
50838b98ea2d72bd2262bf9bff5aca093699d64ffMiao Wang * you may not use this file except in compliance with the License.
60838b98ea2d72bd2262bf9bff5aca093699d64ffMiao Wang * You may obtain a copy of the License at
70838b98ea2d72bd2262bf9bff5aca093699d64ffMiao Wang *
80838b98ea2d72bd2262bf9bff5aca093699d64ffMiao Wang *      http://www.apache.org/licenses/LICENSE-2.0
90838b98ea2d72bd2262bf9bff5aca093699d64ffMiao Wang *
100838b98ea2d72bd2262bf9bff5aca093699d64ffMiao Wang * Unless required by applicable law or agreed to in writing, software
110838b98ea2d72bd2262bf9bff5aca093699d64ffMiao Wang * distributed under the License is distributed on an "AS IS" BASIS,
120838b98ea2d72bd2262bf9bff5aca093699d64ffMiao Wang * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
130838b98ea2d72bd2262bf9bff5aca093699d64ffMiao Wang * See the License for the specific language governing permissions and
140838b98ea2d72bd2262bf9bff5aca093699d64ffMiao Wang * limitations under the License.
150838b98ea2d72bd2262bf9bff5aca093699d64ffMiao Wang */
160838b98ea2d72bd2262bf9bff5aca093699d64ffMiao Wang
170838b98ea2d72bd2262bf9bff5aca093699d64ffMiao Wang#ifndef ANDROID_RS_HIDL_ADAPTATION_H
180838b98ea2d72bd2262bf9bff5aca093699d64ffMiao Wang#define ANDROID_RS_HIDL_ADAPTATION_H
190838b98ea2d72bd2262bf9bff5aca093699d64ffMiao Wang
200838b98ea2d72bd2262bf9bff5aca093699d64ffMiao Wang#include <android/hardware/renderscript/1.0/IDevice.h>
210838b98ea2d72bd2262bf9bff5aca093699d64ffMiao Wang#include <set>
220838b98ea2d72bd2262bf9bff5aca093699d64ffMiao Wang#include <mutex>
230838b98ea2d72bd2262bf9bff5aca093699d64ffMiao Wang
240838b98ea2d72bd2262bf9bff5aca093699d64ffMiao Wangstruct dispatchTable;
250838b98ea2d72bd2262bf9bff5aca093699d64ffMiao Wang
260838b98ea2d72bd2262bf9bff5aca093699d64ffMiao Wangusing ::android::hardware::renderscript::V1_0::IContext;
270838b98ea2d72bd2262bf9bff5aca093699d64ffMiao Wang
280838b98ea2d72bd2262bf9bff5aca093699d64ffMiao Wang/*
290838b98ea2d72bd2262bf9bff5aca093699d64ffMiao Wang * RsHidlAdaptation is a singleton class.
300838b98ea2d72bd2262bf9bff5aca093699d64ffMiao Wang *
310838b98ea2d72bd2262bf9bff5aca093699d64ffMiao Wang * RsHidlAdaptation is used for the following purposes:
320838b98ea2d72bd2262bf9bff5aca093699d64ffMiao Wang * 1. Load the dispatch table with HIDL implementation or CPU fallback.
330838b98ea2d72bd2262bf9bff5aca093699d64ffMiao Wang * 2. Convert input paramters to HIDL types, when using HIDL path.
340838b98ea2d72bd2262bf9bff5aca093699d64ffMiao Wang * 3. Convert output parameters from HIDL types to RS types, when using HIDL path.
3593ad4e01925d2ff1b3b6b3cdf375d874f6a0a60dMiao Wang *
3693ad4e01925d2ff1b3b6b3cdf375d874f6a0a60dMiao Wang * Access of static member objects is protected by global teardown flag.
3793ad4e01925d2ff1b3b6b3cdf375d874f6a0a60dMiao Wang * https://goto.google.com/rs-static-destructor
3893ad4e01925d2ff1b3b6b3cdf375d874f6a0a60dMiao Wang *
390838b98ea2d72bd2262bf9bff5aca093699d64ffMiao Wang */
400838b98ea2d72bd2262bf9bff5aca093699d64ffMiao Wangclass RsHidlAdaptation
410838b98ea2d72bd2262bf9bff5aca093699d64ffMiao Wang{
420838b98ea2d72bd2262bf9bff5aca093699d64ffMiao Wangpublic:
430838b98ea2d72bd2262bf9bff5aca093699d64ffMiao Wang    static RsHidlAdaptation& GetInstance();
440838b98ea2d72bd2262bf9bff5aca093699d64ffMiao Wang    static const dispatchTable* GetEntryFuncs();
450838b98ea2d72bd2262bf9bff5aca093699d64ffMiao Wang
460838b98ea2d72bd2262bf9bff5aca093699d64ffMiao Wangprivate:
470838b98ea2d72bd2262bf9bff5aca093699d64ffMiao Wang    RsHidlAdaptation();
480838b98ea2d72bd2262bf9bff5aca093699d64ffMiao Wang    void InitializeHalDeviceContext();
490838b98ea2d72bd2262bf9bff5aca093699d64ffMiao Wang    void LoadDispatchForHidl();
500838b98ea2d72bd2262bf9bff5aca093699d64ffMiao Wang    static IContext *GetIContextHandle(RsContext con);
510838b98ea2d72bd2262bf9bff5aca093699d64ffMiao Wang
520838b98ea2d72bd2262bf9bff5aca093699d64ffMiao Wang    // HAL entries
530838b98ea2d72bd2262bf9bff5aca093699d64ffMiao Wang    static RsContext ContextCreate(RsDevice vdev, uint32_t version, uint32_t sdkVersion, RsContextType ct, uint32_t flags);
540838b98ea2d72bd2262bf9bff5aca093699d64ffMiao Wang    static void ContextDestroy(RsContext);
550838b98ea2d72bd2262bf9bff5aca093699d64ffMiao Wang    static void ContextFinish(RsContext);
560838b98ea2d72bd2262bf9bff5aca093699d64ffMiao Wang    static void ContextDump(RsContext, int32_t);
570838b98ea2d72bd2262bf9bff5aca093699d64ffMiao Wang    static void ContextSetCacheDir(RsContext rsc, const char *cacheDir, size_t cacheDir_length);
580838b98ea2d72bd2262bf9bff5aca093699d64ffMiao Wang    static void ContextSetPriority(RsContext, int32_t);
590838b98ea2d72bd2262bf9bff5aca093699d64ffMiao Wang    static RsMessageToClientType ContextGetMessage(RsContext, void*, size_t, size_t*, size_t, uint32_t*, size_t);
600838b98ea2d72bd2262bf9bff5aca093699d64ffMiao Wang    static RsMessageToClientType ContextPeekMessage(RsContext, size_t*, size_t, uint32_t*, size_t);
610838b98ea2d72bd2262bf9bff5aca093699d64ffMiao Wang    static void ContextSendMessage(RsContext, uint32_t, const uint8_t*, size_t);
620838b98ea2d72bd2262bf9bff5aca093699d64ffMiao Wang    static void ContextInitToClient(RsContext);
630838b98ea2d72bd2262bf9bff5aca093699d64ffMiao Wang    static void ContextDeinitToClient(RsContext);
640838b98ea2d72bd2262bf9bff5aca093699d64ffMiao Wang
650838b98ea2d72bd2262bf9bff5aca093699d64ffMiao Wang    static void AssignName(RsContext, RsObjectBase, const char*, size_t);
660838b98ea2d72bd2262bf9bff5aca093699d64ffMiao Wang    static void GetName(RsContext, void * obj, const char **name);
670838b98ea2d72bd2262bf9bff5aca093699d64ffMiao Wang    static void ObjDestroy(RsContext, RsAsyncVoidPtr);
680838b98ea2d72bd2262bf9bff5aca093699d64ffMiao Wang
690838b98ea2d72bd2262bf9bff5aca093699d64ffMiao Wang    static RsElement ElementCreate(RsContext, RsDataType, RsDataKind, bool, uint32_t);
700838b98ea2d72bd2262bf9bff5aca093699d64ffMiao Wang    static RsElement ElementCreate2(RsContext, const RsElement*, size_t, const char**, size_t, const size_t*, const uint32_t*, size_t);
710838b98ea2d72bd2262bf9bff5aca093699d64ffMiao Wang    static void ElementGetNativeData(RsContext, RsElement, uint32_t *elemData, uint32_t elemDataSize);
720838b98ea2d72bd2262bf9bff5aca093699d64ffMiao Wang    static void ElementGetSubElements(RsContext, RsElement, uintptr_t *ids, const char **names, size_t *arraySizes, uint32_t dataSize);
730838b98ea2d72bd2262bf9bff5aca093699d64ffMiao Wang
740838b98ea2d72bd2262bf9bff5aca093699d64ffMiao Wang    static RsType TypeCreate(RsContext, RsElement, uint32_t, uint32_t, uint32_t, bool, bool, uint32_t);
750838b98ea2d72bd2262bf9bff5aca093699d64ffMiao Wang    static void TypeGetNativeData(RsContext, RsType, uintptr_t *typeData, uint32_t typeDataSize);
760838b98ea2d72bd2262bf9bff5aca093699d64ffMiao Wang
770838b98ea2d72bd2262bf9bff5aca093699d64ffMiao Wang    static RsAllocation AllocationCreateTyped(RsContext, RsType, RsAllocationMipmapControl, uint32_t, uintptr_t);
780838b98ea2d72bd2262bf9bff5aca093699d64ffMiao Wang    static RsAllocation AllocationCreateFromBitmap(RsContext, RsType, RsAllocationMipmapControl, const void*, size_t, uint32_t);
790838b98ea2d72bd2262bf9bff5aca093699d64ffMiao Wang    static RsAllocation AllocationCubeCreateFromBitmap(RsContext, RsType, RsAllocationMipmapControl, const void*, size_t, uint32_t);
800838b98ea2d72bd2262bf9bff5aca093699d64ffMiao Wang    static const void* AllocationGetType(RsContext con, RsAllocation va);
810838b98ea2d72bd2262bf9bff5aca093699d64ffMiao Wang    static RsNativeWindow AllocationGetSurface(RsContext, RsAllocation);
820838b98ea2d72bd2262bf9bff5aca093699d64ffMiao Wang    static void AllocationSetSurface(RsContext, RsAllocation, RsNativeWindow);
830838b98ea2d72bd2262bf9bff5aca093699d64ffMiao Wang    static void AllocationCopyToBitmap(RsContext, RsAllocation, void*, size_t);
840838b98ea2d72bd2262bf9bff5aca093699d64ffMiao Wang    static void Allocation1DData(RsContext, RsAllocation, uint32_t, uint32_t, uint32_t, const void*, size_t);
850838b98ea2d72bd2262bf9bff5aca093699d64ffMiao Wang    static void Allocation1DElementData(RsContext, RsAllocation, uint32_t, uint32_t, const void*, size_t, size_t);
860838b98ea2d72bd2262bf9bff5aca093699d64ffMiao Wang    static void AllocationElementData(RsContext, RsAllocation, uint32_t, uint32_t, uint32_t, uint32_t, const void*, size_t, size_t);
870838b98ea2d72bd2262bf9bff5aca093699d64ffMiao Wang    static void Allocation2DData(RsContext, RsAllocation, uint32_t, uint32_t, uint32_t, RsAllocationCubemapFace, uint32_t, uint32_t, const void*, size_t, size_t);
880838b98ea2d72bd2262bf9bff5aca093699d64ffMiao Wang    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);
890838b98ea2d72bd2262bf9bff5aca093699d64ffMiao Wang    static void AllocationGenerateMipmaps(RsContext, RsAllocation);
900838b98ea2d72bd2262bf9bff5aca093699d64ffMiao Wang    static void AllocationRead(RsContext, RsAllocation, void*, size_t);
910838b98ea2d72bd2262bf9bff5aca093699d64ffMiao Wang    static void Allocation1DRead(RsContext, RsAllocation, uint32_t, uint32_t, uint32_t, void*, size_t);
920838b98ea2d72bd2262bf9bff5aca093699d64ffMiao Wang    static void AllocationElementRead(RsContext, RsAllocation, uint32_t, uint32_t, uint32_t, uint32_t, void*, size_t, size_t);
930838b98ea2d72bd2262bf9bff5aca093699d64ffMiao Wang    static void Allocation2DRead(RsContext, RsAllocation, uint32_t, uint32_t, uint32_t, RsAllocationCubemapFace, uint32_t, uint32_t, void*, size_t, size_t);
940838b98ea2d72bd2262bf9bff5aca093699d64ffMiao Wang    static void Allocation3DRead(RsContext, RsAllocation, uint32_t, uint32_t, uint32_t, uint32_t, uint32_t, uint32_t, uint32_t, void*, size_t, size_t);
950838b98ea2d72bd2262bf9bff5aca093699d64ffMiao Wang    static void AllocationSyncAll(RsContext, RsAllocation, RsAllocationUsageType);
960838b98ea2d72bd2262bf9bff5aca093699d64ffMiao Wang    static void AllocationResize1D(RsContext, RsAllocation, uint32_t);
970838b98ea2d72bd2262bf9bff5aca093699d64ffMiao Wang    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);
980838b98ea2d72bd2262bf9bff5aca093699d64ffMiao Wang    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);
990838b98ea2d72bd2262bf9bff5aca093699d64ffMiao Wang    static void AllocationIoSend(RsContext, RsAllocation);
1000838b98ea2d72bd2262bf9bff5aca093699d64ffMiao Wang    static int64_t AllocationIoReceive(RsContext, RsAllocation);
1010838b98ea2d72bd2262bf9bff5aca093699d64ffMiao Wang    static void *AllocationGetPointer(RsContext, RsAllocation, uint32_t lod, RsAllocationCubemapFace face, uint32_t z, uint32_t array, size_t *stride, size_t stride_len);
1020838b98ea2d72bd2262bf9bff5aca093699d64ffMiao Wang    static void AllocationSetupBufferQueue(RsContext context, RsAllocation valloc, uint32_t numAlloc);
1030838b98ea2d72bd2262bf9bff5aca093699d64ffMiao Wang    static void AllocationShareBufferQueue(RsContext context, RsAllocation valloc1, RsAllocation valloc2);
1040838b98ea2d72bd2262bf9bff5aca093699d64ffMiao Wang    static RsAllocation AllocationAdapterCreate(RsContext rsc, RsType vtype, RsAllocation baseAlloc);
1050838b98ea2d72bd2262bf9bff5aca093699d64ffMiao Wang    static void AllocationAdapterOffset(RsContext rsc, RsAllocation alloc, const uint32_t * offsets, size_t offsets_length);
1060838b98ea2d72bd2262bf9bff5aca093699d64ffMiao Wang
1070838b98ea2d72bd2262bf9bff5aca093699d64ffMiao Wang    static RsScript ScriptCCreate(RsContext, const char*, size_t, const char*, size_t, const char*, size_t);
1080838b98ea2d72bd2262bf9bff5aca093699d64ffMiao Wang    static RsScript ScriptIntrinsicCreate(RsContext, uint32_t id, RsElement);
1090838b98ea2d72bd2262bf9bff5aca093699d64ffMiao Wang    static void ScriptBindAllocation(RsContext, RsScript, RsAllocation, uint32_t);
1100838b98ea2d72bd2262bf9bff5aca093699d64ffMiao Wang    static void ScriptSetTimeZone(RsContext, RsScript, const char*, size_t);
1110838b98ea2d72bd2262bf9bff5aca093699d64ffMiao Wang    static void ScriptInvoke(RsContext, RsScript, uint32_t);
1120838b98ea2d72bd2262bf9bff5aca093699d64ffMiao Wang    static void ScriptInvokeV(RsContext, RsScript, uint32_t, const void*, size_t);
1130838b98ea2d72bd2262bf9bff5aca093699d64ffMiao Wang    static void ScriptForEach(RsContext, RsScript, uint32_t, RsAllocation, RsAllocation, const void*, size_t, const RsScriptCall*, size_t);
1140838b98ea2d72bd2262bf9bff5aca093699d64ffMiao Wang    static void ScriptForEachMulti(RsContext, RsScript, uint32_t, RsAllocation*, size_t, RsAllocation, const void*, size_t, const RsScriptCall*, size_t);
1150838b98ea2d72bd2262bf9bff5aca093699d64ffMiao Wang    static void ScriptReduce(RsContext, RsScript, uint32_t, RsAllocation*, size_t, RsAllocation, const RsScriptCall*, size_t);
1160838b98ea2d72bd2262bf9bff5aca093699d64ffMiao Wang    static void ScriptSetVarI(RsContext, RsScript, uint32_t, int);
1170838b98ea2d72bd2262bf9bff5aca093699d64ffMiao Wang    static void ScriptSetVarObj(RsContext, RsScript, uint32_t, RsObjectBase);
1180838b98ea2d72bd2262bf9bff5aca093699d64ffMiao Wang    static void ScriptSetVarJ(RsContext, RsScript, uint32_t, int64_t);
1190838b98ea2d72bd2262bf9bff5aca093699d64ffMiao Wang    static void ScriptSetVarF(RsContext, RsScript, uint32_t, float);
1200838b98ea2d72bd2262bf9bff5aca093699d64ffMiao Wang    static void ScriptSetVarD(RsContext, RsScript, uint32_t, double);
1210838b98ea2d72bd2262bf9bff5aca093699d64ffMiao Wang    static void ScriptSetVarV(RsContext, RsScript, uint32_t, const void*, size_t);
1220838b98ea2d72bd2262bf9bff5aca093699d64ffMiao Wang    static void ScriptGetVarV(RsContext, RsScript, uint32_t, void*, size_t);
1230838b98ea2d72bd2262bf9bff5aca093699d64ffMiao Wang    static void ScriptSetVarVE(RsContext, RsScript, uint32_t, const void*, size_t, RsElement, const uint32_t*, size_t);
1240838b98ea2d72bd2262bf9bff5aca093699d64ffMiao Wang
1250838b98ea2d72bd2262bf9bff5aca093699d64ffMiao Wang    static RsScriptGroup ScriptGroupCreate(RsContext, RsScriptKernelID*, size_t, RsScriptKernelID*, size_t, RsScriptKernelID*, size_t, RsScriptFieldID*, size_t, const RsType*, size_t);
1260838b98ea2d72bd2262bf9bff5aca093699d64ffMiao Wang    static RsScriptGroup2 ScriptGroup2Create(RsContext, const char*, size_t, const char*, size_t, RsClosure*, size_t);
1270838b98ea2d72bd2262bf9bff5aca093699d64ffMiao Wang    static RsScriptKernelID ScriptKernelIDCreate(RsContext, RsScript, int, int);
1280838b98ea2d72bd2262bf9bff5aca093699d64ffMiao Wang    static RsScriptInvokeID ScriptInvokeIDCreate(RsContext, RsScript, int);
1290838b98ea2d72bd2262bf9bff5aca093699d64ffMiao Wang    static RsScriptFieldID ScriptFieldIDCreate(RsContext, RsScript, int);
1300838b98ea2d72bd2262bf9bff5aca093699d64ffMiao Wang    static void ScriptGroupSetOutput(RsContext, RsScriptGroup, RsScriptKernelID, RsAllocation);
1310838b98ea2d72bd2262bf9bff5aca093699d64ffMiao Wang    static void ScriptGroupSetInput(RsContext, RsScriptGroup, RsScriptKernelID, RsAllocation);
1320838b98ea2d72bd2262bf9bff5aca093699d64ffMiao Wang    static void ScriptGroupExecute(RsContext, RsScriptGroup);
1330838b98ea2d72bd2262bf9bff5aca093699d64ffMiao Wang    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);
1340838b98ea2d72bd2262bf9bff5aca093699d64ffMiao Wang    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);
1350838b98ea2d72bd2262bf9bff5aca093699d64ffMiao Wang    static void ClosureSetArg(RsContext, RsClosure, uint32_t, uintptr_t, int);
1360838b98ea2d72bd2262bf9bff5aca093699d64ffMiao Wang    static void ClosureSetGlobal(RsContext, RsClosure, RsScriptFieldID, int64_t, int);
1370838b98ea2d72bd2262bf9bff5aca093699d64ffMiao Wang
1380838b98ea2d72bd2262bf9bff5aca093699d64ffMiao Wang    static RsSampler SamplerCreate(RsContext, RsSamplerValue, RsSamplerValue, RsSamplerValue, RsSamplerValue, RsSamplerValue, float);
1390838b98ea2d72bd2262bf9bff5aca093699d64ffMiao Wang
1400838b98ea2d72bd2262bf9bff5aca093699d64ffMiao Wang    // Dispatch table entries for HIDL. The dispatch table is initialized by the
1410838b98ea2d72bd2262bf9bff5aca093699d64ffMiao Wang    // constructor of this singleton class, to avoid concurrency issues.
1420838b98ea2d72bd2262bf9bff5aca093699d64ffMiao Wang    static dispatchTable mEntryFuncs;
1430838b98ea2d72bd2262bf9bff5aca093699d64ffMiao Wang
1440838b98ea2d72bd2262bf9bff5aca093699d64ffMiao Wang    // HIDL instance. This is a singleton HIDL instance, providing a handle for
1450838b98ea2d72bd2262bf9bff5aca093699d64ffMiao Wang    // RenderScript HIDL implementation (driver). The handle is created by the
1460838b98ea2d72bd2262bf9bff5aca093699d64ffMiao Wang    // constructor of this singleton class.
1470838b98ea2d72bd2262bf9bff5aca093699d64ffMiao Wang    static android::sp<android::hardware::renderscript::V1_0::IDevice> mHidl;
1480838b98ea2d72bd2262bf9bff5aca093699d64ffMiao Wang
1490838b98ea2d72bd2262bf9bff5aca093699d64ffMiao Wang    // mContexts keeps Treble returned sp<IContext>s alive.
1500838b98ea2d72bd2262bf9bff5aca093699d64ffMiao Wang    static std::set<android::sp<IContext> > mContexts;
1510838b98ea2d72bd2262bf9bff5aca093699d64ffMiao Wang
1520838b98ea2d72bd2262bf9bff5aca093699d64ffMiao Wang    // mMutex is used to protect concurrent modification to the mContexts set.
1530838b98ea2d72bd2262bf9bff5aca093699d64ffMiao Wang    // std::mutex is safe for pthreads on Android. Since other threading model
1540838b98ea2d72bd2262bf9bff5aca093699d64ffMiao Wang    // supported on Android are built on top of pthread, std::mutex is safe for them.
1550838b98ea2d72bd2262bf9bff5aca093699d64ffMiao Wang    static std::mutex mContextsMutex;
1560838b98ea2d72bd2262bf9bff5aca093699d64ffMiao Wang};
1570838b98ea2d72bd2262bf9bff5aca093699d64ffMiao Wang
1580838b98ea2d72bd2262bf9bff5aca093699d64ffMiao Wang#endif // ANDROID_RS_HIDL_ADAPTATION_H
159