rsHidlAdaptation.h 10 KB

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