rsDriverLoader.cpp 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272
  1. /*
  2. * Copyright (C) 2015 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. #include "rs.h"
  17. #include "rsDevice.h"
  18. #include "rsContext.h"
  19. #include "rsThreadIO.h"
  20. #include "rsgApiStructs.h"
  21. #if !defined(RS_VENDOR_LIB) && !defined(RS_COMPATIBILITY_LIB)
  22. #include "rsMesh.h"
  23. #endif
  24. #include <sys/types.h>
  25. #include <sys/resource.h>
  26. #include <sched.h>
  27. #include <sys/syscall.h>
  28. #include <string.h>
  29. #include <dlfcn.h>
  30. #include <inttypes.h>
  31. #include <unistd.h>
  32. #ifdef RS_COMPATIBILITY_LIB
  33. #include "rsCompatibilityLib.h"
  34. #endif
  35. namespace android {
  36. namespace renderscript {
  37. typedef bool (*HalQueryVersion)(uint32_t *version_major, uint32_t *version_minor);
  38. typedef bool (*HalQueryHal)(android::renderscript::RsHalInitEnums entry, void **fnPtr);
  39. typedef bool (*HalInit)(RsContext, uint32_t version_major, uint32_t version_minor);
  40. typedef void (*HalAbort)(RsContext);
  41. static bool LoadHalTable(Context *rsc, HalQueryHal fn, bool loadGraphics) {
  42. bool ret = true;
  43. ret &= fn(RS_HAL_CORE_SHUTDOWN, (void **)&rsc->mHal.funcs.shutdownDriver);
  44. ret &= fn(RS_HAL_CORE_SET_PRIORITY, (void **)&rsc->mHal.funcs.setPriority);
  45. ret &= fn(RS_HAL_CORE_ALLOC_RUNTIME_MEM, (void **)&rsc->mHal.funcs.allocRuntimeMem);
  46. ret &= fn(RS_HAL_CORE_FREE_RUNTIME_MEM, (void **)&rsc->mHal.funcs.freeRuntimeMem);
  47. ret &= fn(RS_HAL_CORE_FINISH, (void **)&rsc->mHal.funcs.finish);
  48. ret &= fn(RS_HAL_SCRIPT_INIT, (void **)&rsc->mHal.funcs.script.init);
  49. ret &= fn(RS_HAL_SCRIPT_INIT_INTRINSIC, (void **)&rsc->mHal.funcs.script.initIntrinsic);
  50. ret &= fn(RS_HAL_SCRIPT_INVOKE_FUNCTION, (void **)&rsc->mHal.funcs.script.invokeFunction);
  51. ret &= fn(RS_HAL_SCRIPT_INVOKE_ROOT, (void **)&rsc->mHal.funcs.script.invokeRoot);
  52. ret &= fn(RS_HAL_SCRIPT_INVOKE_FOR_EACH, (void **)&rsc->mHal.funcs.script.invokeForEach);
  53. ret &= fn(RS_HAL_SCRIPT_INVOKE_REDUCE, (void **)&rsc->mHal.funcs.script.invokeReduce);
  54. ret &= fn(RS_HAL_SCRIPT_INVOKE_INIT, (void **)&rsc->mHal.funcs.script.invokeInit);
  55. ret &= fn(RS_HAL_SCRIPT_INVOKE_FREE_CHILDREN, (void **)&rsc->mHal.funcs.script.invokeFreeChildren);
  56. ret &= fn(RS_HAL_SCRIPT_SET_GLOBAL_VAR, (void **)&rsc->mHal.funcs.script.setGlobalVar);
  57. ret &= fn(RS_HAL_SCRIPT_GET_GLOBAL_VAR, (void **)&rsc->mHal.funcs.script.getGlobalVar);
  58. ret &= fn(RS_HAL_SCRIPT_SET_GLOBAL_VAR_WITH_ELEMENT_DIM, (void **)&rsc->mHal.funcs.script.setGlobalVarWithElemDims);
  59. ret &= fn(RS_HAL_SCRIPT_SET_GLOBAL_BIND, (void **)&rsc->mHal.funcs.script.setGlobalBind);
  60. ret &= fn(RS_HAL_SCRIPT_SET_GLOBAL_OBJECT, (void **)&rsc->mHal.funcs.script.setGlobalObj);
  61. ret &= fn(RS_HAL_SCRIPT_DESTROY, (void **)&rsc->mHal.funcs.script.destroy);
  62. ret &= fn(RS_HAL_SCRIPT_INVOKE_FOR_EACH_MULTI, (void **)&rsc->mHal.funcs.script.invokeForEachMulti);
  63. ret &= fn(RS_HAL_SCRIPT_UPDATE_CACHED_OBJECT, (void **)&rsc->mHal.funcs.script.updateCachedObject);
  64. ret &= fn(RS_HAL_ALLOCATION_INIT, (void **)&rsc->mHal.funcs.allocation.init);
  65. ret &= fn(RS_HAL_ALLOCATION_INIT_OEM, (void **)&rsc->mHal.funcs.allocation.initOem);
  66. ret &= fn(RS_HAL_ALLOCATION_INIT_ADAPTER, (void **)&rsc->mHal.funcs.allocation.initAdapter);
  67. ret &= fn(RS_HAL_ALLOCATION_DESTROY, (void **)&rsc->mHal.funcs.allocation.destroy);
  68. ret &= fn(RS_HAL_ALLOCATION_GET_GRALLOC_BITS, (void **)&rsc->mHal.funcs.allocation.grallocBits);
  69. ret &= fn(RS_HAL_ALLOCATION_RESIZE, (void **)&rsc->mHal.funcs.allocation.resize);
  70. ret &= fn(RS_HAL_ALLOCATION_SYNC_ALL, (void **)&rsc->mHal.funcs.allocation.syncAll);
  71. ret &= fn(RS_HAL_ALLOCATION_MARK_DIRTY, (void **)&rsc->mHal.funcs.allocation.markDirty);
  72. ret &= fn(RS_HAL_ALLOCATION_SET_SURFACE, (void **)&rsc->mHal.funcs.allocation.setSurface);
  73. ret &= fn(RS_HAL_ALLOCATION_IO_SEND, (void **)&rsc->mHal.funcs.allocation.ioSend);
  74. ret &= fn(RS_HAL_ALLOCATION_IO_RECEIVE, (void **)&rsc->mHal.funcs.allocation.ioReceive);
  75. ret &= fn(RS_HAL_ALLOCATION_DATA_1D, (void **)&rsc->mHal.funcs.allocation.data1D);
  76. ret &= fn(RS_HAL_ALLOCATION_DATA_2D, (void **)&rsc->mHal.funcs.allocation.data2D);
  77. ret &= fn(RS_HAL_ALLOCATION_DATA_3D, (void **)&rsc->mHal.funcs.allocation.data3D);
  78. ret &= fn(RS_HAL_ALLOCATION_READ_1D, (void **)&rsc->mHal.funcs.allocation.read1D);
  79. ret &= fn(RS_HAL_ALLOCATION_READ_2D, (void **)&rsc->mHal.funcs.allocation.read2D);
  80. ret &= fn(RS_HAL_ALLOCATION_READ_3D, (void **)&rsc->mHal.funcs.allocation.read3D);
  81. ret &= fn(RS_HAL_ALLOCATION_LOCK_1D, (void **)&rsc->mHal.funcs.allocation.lock1D);
  82. ret &= fn(RS_HAL_ALLOCATION_UNLOCK_1D, (void **)&rsc->mHal.funcs.allocation.unlock1D);
  83. ret &= fn(RS_HAL_ALLOCATION_COPY_1D, (void **)&rsc->mHal.funcs.allocation.allocData1D);
  84. ret &= fn(RS_HAL_ALLOCATION_COPY_2D, (void **)&rsc->mHal.funcs.allocation.allocData2D);
  85. ret &= fn(RS_HAL_ALLOCATION_COPY_3D, (void **)&rsc->mHal.funcs.allocation.allocData3D);
  86. ret &= fn(RS_HAL_ALLOCATION_ELEMENT_DATA, (void **)&rsc->mHal.funcs.allocation.elementData);
  87. ret &= fn(RS_HAL_ALLOCATION_ELEMENT_READ, (void **)&rsc->mHal.funcs.allocation.elementRead);
  88. ret &= fn(RS_HAL_ALLOCATION_GENERATE_MIPMAPS, (void **)&rsc->mHal.funcs.allocation.generateMipmaps);
  89. ret &= fn(RS_HAL_ALLOCATION_UPDATE_CACHED_OBJECT, (void **)&rsc->mHal.funcs.allocation.updateCachedObject);
  90. ret &= fn(RS_HAL_ALLOCATION_ADAPTER_OFFSET, (void **)&rsc->mHal.funcs.allocation.adapterOffset);
  91. ret &= fn(RS_HAL_ALLOCATION_GET_POINTER, (void **)&rsc->mHal.funcs.allocation.getPointer);
  92. #ifdef RS_COMPATIBILITY_LIB
  93. ret &= fn(RS_HAL_ALLOCATION_INIT_STRIDED, (void **)&rsc->mHal.funcs.allocation.initStrided);
  94. #endif
  95. ret &= fn(RS_HAL_SAMPLER_INIT, (void **)&rsc->mHal.funcs.sampler.init);
  96. ret &= fn(RS_HAL_SAMPLER_DESTROY, (void **)&rsc->mHal.funcs.sampler.destroy);
  97. ret &= fn(RS_HAL_SAMPLER_UPDATE_CACHED_OBJECT, (void **)&rsc->mHal.funcs.sampler.updateCachedObject);
  98. ret &= fn(RS_HAL_TYPE_INIT, (void **)&rsc->mHal.funcs.type.init);
  99. ret &= fn(RS_HAL_TYPE_DESTROY, (void **)&rsc->mHal.funcs.type.destroy);
  100. ret &= fn(RS_HAL_TYPE_UPDATE_CACHED_OBJECT, (void **)&rsc->mHal.funcs.type.updateCachedObject);
  101. ret &= fn(RS_HAL_ELEMENT_INIT, (void **)&rsc->mHal.funcs.element.init);
  102. ret &= fn(RS_HAL_ELEMENT_DESTROY, (void **)&rsc->mHal.funcs.element.destroy);
  103. ret &= fn(RS_HAL_ELEMENT_UPDATE_CACHED_OBJECT, (void **)&rsc->mHal.funcs.element.updateCachedObject);
  104. ret &= fn(RS_HAL_SCRIPT_GROUP_INIT, (void **)&rsc->mHal.funcs.scriptgroup.init);
  105. ret &= fn(RS_HAL_SCRIPT_GROUP_DESTROY, (void **)&rsc->mHal.funcs.scriptgroup.destroy);
  106. ret &= fn(RS_HAL_SCRIPT_GROUP_UPDATE_CACHED_OBJECT, (void **)&rsc->mHal.funcs.scriptgroup.updateCachedObject);
  107. ret &= fn(RS_HAL_SCRIPT_GROUP_SET_INPUT, (void **)&rsc->mHal.funcs.scriptgroup.setInput);
  108. ret &= fn(RS_HAL_SCRIPT_GROUP_SET_OUTPUT, (void **)&rsc->mHal.funcs.scriptgroup.setOutput);
  109. ret &= fn(RS_HAL_SCRIPT_GROUP_EXECUTE, (void **)&rsc->mHal.funcs.scriptgroup.execute);
  110. if (loadGraphics) {
  111. ret &= fn(RS_HAL_GRAPHICS_INIT, (void **)&rsc->mHal.funcs.initGraphics);
  112. ret &= fn(RS_HAL_GRAPHICS_SHUTDOWN, (void **)&rsc->mHal.funcs.shutdownGraphics);
  113. ret &= fn(RS_HAL_GRAPHICS_SWAP, (void **)&rsc->mHal.funcs.swap);
  114. ret &= fn(RS_HAL_GRAPHICS_SET_SURFACE, (void **)&rsc->mHal.funcs.setSurface);
  115. ret &= fn(RS_HAL_GRAPHICS_RASTER_INIT, (void **)&rsc->mHal.funcs.raster.init);
  116. ret &= fn(RS_HAL_GRAPHICS_RASTER_SET_ACTIVE, (void **)&rsc->mHal.funcs.raster.setActive);
  117. ret &= fn(RS_HAL_GRAPHICS_RASTER_DESTROY, (void **)&rsc->mHal.funcs.raster.destroy);
  118. ret &= fn(RS_HAL_GRAPHICS_VERTEX_INIT, (void **)&rsc->mHal.funcs.vertex.init);
  119. ret &= fn(RS_HAL_GRAPHICS_VERTEX_SET_ACTIVE, (void **)&rsc->mHal.funcs.vertex.setActive);
  120. ret &= fn(RS_HAL_GRAPHICS_VERTEX_DESTROY, (void **)&rsc->mHal.funcs.vertex.destroy);
  121. ret &= fn(RS_HAL_GRAPHICS_FRAGMENT_INIT, (void **)&rsc->mHal.funcs.fragment.init);
  122. ret &= fn(RS_HAL_GRAPHICS_FRAGMENT_SET_ACTIVE, (void **)&rsc->mHal.funcs.fragment.setActive);
  123. ret &= fn(RS_HAL_GRAPHICS_FRAGMENT_DESTROY, (void **)&rsc->mHal.funcs.fragment.destroy);
  124. ret &= fn(RS_HAL_GRAPHICS_MESH_INIT, (void **)&rsc->mHal.funcs.mesh.init);
  125. ret &= fn(RS_HAL_GRAPHICS_MESH_DRAW, (void **)&rsc->mHal.funcs.mesh.draw);
  126. ret &= fn(RS_HAL_GRAPHICS_MESH_DESTROY, (void **)&rsc->mHal.funcs.mesh.destroy);
  127. ret &= fn(RS_HAL_GRAPHICS_FB_INIT, (void **)&rsc->mHal.funcs.framebuffer.init);
  128. ret &= fn(RS_HAL_GRAPHICS_FB_SET_ACTIVE, (void **)&rsc->mHal.funcs.framebuffer.setActive);
  129. ret &= fn(RS_HAL_GRAPHICS_FB_DESTROY, (void **)&rsc->mHal.funcs.framebuffer.destroy);
  130. ret &= fn(RS_HAL_GRAPHICS_STORE_INIT, (void **)&rsc->mHal.funcs.store.init);
  131. ret &= fn(RS_HAL_GRAPHICS_STORE_SET_ACTIVE, (void **)&rsc->mHal.funcs.store.setActive);
  132. ret &= fn(RS_HAL_GRAPHICS_STORE_DESTROY, (void **)&rsc->mHal.funcs.store.destroy);
  133. }
  134. return ret;
  135. }
  136. bool Context::loadRuntime(const char* filename) {
  137. HalQueryVersion fnQueryVersion = nullptr;
  138. HalQueryHal fnQueryHal = nullptr;
  139. HalInit fnInit = nullptr;
  140. HalAbort fnAbort = nullptr;
  141. // TODO: store the driverSO somewhere so we can dlclose later
  142. void *driverSO = nullptr;
  143. driverSO = dlopen(filename, RTLD_LAZY);
  144. if (driverSO == nullptr) {
  145. ALOGE("Failed loading RS driver: %s", dlerror());
  146. return false;
  147. }
  148. // Need to call dlerror() to clear buffer before using it for dlsym().
  149. (void) dlerror();
  150. fnQueryVersion = (HalQueryVersion) dlsym(driverSO, "rsdHalQueryVersion");
  151. fnQueryHal = (HalQueryHal) dlsym(driverSO, "rsdHalQueryHal");
  152. fnInit = (HalInit) dlsym(driverSO, "rsdHalInit");
  153. fnAbort = (HalAbort) dlsym(driverSO, "rsdHalAbort");
  154. uint32_t version_major = 0;
  155. uint32_t version_minor = 0;
  156. if ((fnQueryVersion == nullptr) || (fnQueryHal == nullptr) ||
  157. (fnInit == nullptr) || (fnAbort == nullptr)) {
  158. ALOGE("Failed to find hal setup entry points: %s", dlerror());
  159. goto error;
  160. }
  161. if (!fnQueryVersion(&version_major, &version_minor)) {
  162. ALOGE("Error checking RS driver version, %s", filename);
  163. goto error;
  164. }
  165. if (version_major != RS_HAL_VERSION) {
  166. ALOGE("Mismatched RS HAL versions: %s is version %u but version %u is expected",
  167. filename, version_major, RS_HAL_VERSION);
  168. goto error;
  169. }
  170. if (!LoadHalTable(this, fnQueryHal, mIsGraphicsContext)) {
  171. ALOGE("Error loading RS HAL table, %s", filename);
  172. goto error;
  173. }
  174. if (!(*fnInit)(this, 0, 0)) {
  175. ALOGE("Hal init failed, %s", filename);
  176. goto error;
  177. }
  178. // Only map in the actual driver name if we successfully load the runtime.
  179. setDriverName(filename);
  180. return true;
  181. error:
  182. if (fnAbort != nullptr) {
  183. fnAbort(this);
  184. }
  185. dlclose(driverSO);
  186. return false;
  187. }
  188. bool Context::loadDriver(bool forceDefault, bool forceRSoV) {
  189. bool loadDefault = true;
  190. // Provide a mechanism for dropping in a different RS driver.
  191. #ifndef RS_COMPATIBILITY_LIB
  192. if (forceRSoV) {
  193. // If the property is set to use the RSoV driver, load it and fall back
  194. // to the vendor driver or the CPU reference driver if it does not load.
  195. if (loadRuntime("libRSDriver_RSoV.so")) {
  196. ALOGV("Successfully loaded the RSoV driver!");
  197. return true;
  198. }
  199. ALOGE("Failed to load the RSoV driver!");
  200. }
  201. if (!forceDefault && mVendorDriverName != nullptr) {
  202. if (loadRuntime(mVendorDriverName)) {
  203. ALOGV("Successfully loaded runtime: %s", mVendorDriverName);
  204. loadDefault = false;
  205. } else {
  206. ALOGE("Failed to load runtime %s, loading default", mVendorDriverName);
  207. }
  208. }
  209. if (loadDefault) {
  210. if (!loadRuntime("libRSDriver.so")) {
  211. ALOGE("Failed to load default runtime!");
  212. return false;
  213. }
  214. }
  215. #else // RS_COMPATIBILITY_LIB
  216. if (!LoadHalTable(this, rsdHalQueryHal, false)) {
  217. ALOGE("Error loading RS HAL table");
  218. return false;
  219. }
  220. if (rsdHalInit(this, 0, 0) != true) {
  221. return false;
  222. }
  223. #endif
  224. return true;
  225. }
  226. } // namespace renderscript
  227. } // namespace android