rsovCore.cpp 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339
  1. /*
  2. * Copyright (C) 2016 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 "rsovCore.h"
  17. #include <malloc.h>
  18. #include <sched.h>
  19. #include <string.h>
  20. #include <sys/resource.h>
  21. #include <sys/syscall.h>
  22. #include <sys/types.h>
  23. #include "cpu_ref/rsd_cpu.h"
  24. #include "rsContext.h"
  25. #include "rsovAllocation.h"
  26. #include "rsovContext.h"
  27. #include "rsovElement.h"
  28. #include "rsovSampler.h"
  29. #include "rsovScript.h"
  30. #include "rsovScriptGroup.h"
  31. #include "rsovType.h"
  32. namespace android {
  33. namespace renderscript {
  34. namespace {
  35. void SetPriority(const Context *rsc, int32_t priority) {
  36. RSoVHal *dc = (RSoVHal *)rsc->mHal.drv;
  37. dc->mCpuRef->setPriority(priority);
  38. }
  39. void Shutdown(Context *rsc) {
  40. RSoVHal *dc = (RSoVHal *)rsc->mHal.drv;
  41. delete dc->mCpuRef;
  42. free(dc);
  43. rsc->mHal.drv = nullptr;
  44. }
  45. void *AllocRuntimeMem(size_t size, uint32_t flags) {
  46. void* buffer = calloc(size, sizeof(char));
  47. return buffer;
  48. }
  49. void FreeRuntimeMem(void* ptr) {
  50. free(ptr);
  51. }
  52. const RsdCpuReference::CpuSymbol *lookupRuntimeStubs(
  53. Context *pContext, char const *name) {
  54. return nullptr;
  55. }
  56. } // anonymous namespace
  57. namespace rsov {
  58. namespace {
  59. RsdCpuReference::CpuScript *lookupCpuScript(Context *rsc, const Script *s) {
  60. if (RSoVScript::isScriptCpuBacked(s)) {
  61. return reinterpret_cast<RsdCpuReference::CpuScript *>(s->mHal.drv);
  62. }
  63. RSoVScript *rsovScript = reinterpret_cast<RSoVScript *>(s->mHal.drv);
  64. return rsovScript->getCpuScript();
  65. }
  66. } // anonymous namespace
  67. } // namespace rsov
  68. extern "C" bool rsdHalQueryHal(RsHalInitEnums entry, void **fnPtr) {
  69. switch (entry) {
  70. case RS_HAL_ALLOCATION_INIT:
  71. fnPtr[0] = (void *)rsovAllocationInit;
  72. break;
  73. case RS_HAL_ALLOCATION_INIT_OEM:
  74. fnPtr[0] = (void *)nullptr;
  75. break;
  76. case RS_HAL_ALLOCATION_INIT_ADAPTER:
  77. fnPtr[0] = (void *)rsovAllocationAdapterInit;
  78. break;
  79. case RS_HAL_ALLOCATION_DESTROY:
  80. fnPtr[0] = (void *)rsovAllocationDestroy;
  81. break;
  82. case RS_HAL_ALLOCATION_GET_GRALLOC_BITS:
  83. fnPtr[0] = (void *)rsovAllocationGrallocBits;
  84. break;
  85. case RS_HAL_ALLOCATION_DATA_1D:
  86. fnPtr[0] = (void *)rsovAllocationData1D;
  87. break;
  88. case RS_HAL_ALLOCATION_DATA_2D:
  89. fnPtr[0] = (void *)rsovAllocationData2D;
  90. break;
  91. case RS_HAL_ALLOCATION_DATA_3D:
  92. fnPtr[0] = (void *)rsovAllocationData3D;
  93. break;
  94. case RS_HAL_ALLOCATION_READ_1D:
  95. fnPtr[0] = (void *)rsovAllocationRead1D;
  96. break;
  97. case RS_HAL_ALLOCATION_READ_2D:
  98. fnPtr[0] = (void *)rsovAllocationRead2D;
  99. break;
  100. case RS_HAL_ALLOCATION_READ_3D:
  101. fnPtr[0] = (void *)rsovAllocationRead3D;
  102. break;
  103. case RS_HAL_ALLOCATION_LOCK_1D:
  104. fnPtr[0] = (void *)rsovAllocationLock1D;
  105. break;
  106. case RS_HAL_ALLOCATION_UNLOCK_1D:
  107. fnPtr[0] = (void *)rsovAllocationUnlock1D;
  108. break;
  109. case RS_HAL_ALLOCATION_COPY_1D:
  110. fnPtr[0] = (void *)rsovAllocationData1D_alloc;
  111. break;
  112. case RS_HAL_ALLOCATION_COPY_2D:
  113. fnPtr[0] = (void *)rsovAllocationData2D_alloc;
  114. break;
  115. case RS_HAL_ALLOCATION_COPY_3D:
  116. fnPtr[0] = (void *)rsovAllocationData3D_alloc;
  117. break;
  118. case RS_HAL_ALLOCATION_ADAPTER_OFFSET:
  119. fnPtr[0] = (void *)rsovAllocationAdapterOffset;
  120. break;
  121. case RS_HAL_ALLOCATION_RESIZE:
  122. fnPtr[0] = (void *)rsovAllocationResize;
  123. break;
  124. case RS_HAL_ALLOCATION_SYNC_ALL:
  125. fnPtr[0] = (void *)rsovAllocationSyncAll;
  126. break;
  127. case RS_HAL_ALLOCATION_MARK_DIRTY:
  128. fnPtr[0] = (void *)rsovAllocationMarkDirty;
  129. break;
  130. case RS_HAL_ALLOCATION_GENERATE_MIPMAPS:
  131. fnPtr[0] = (void *)rsovAllocationGenerateMipmaps;
  132. break;
  133. case RS_HAL_ALLOCATION_UPDATE_CACHED_OBJECT:
  134. fnPtr[0] = (void *)rsovAllocationUpdateCachedObject;
  135. break;
  136. case RS_HAL_ALLOCATION_GET_POINTER:
  137. fnPtr[0] = (void *)nullptr;
  138. break;
  139. case RS_HAL_ALLOCATION_SET_SURFACE:
  140. fnPtr[0] = (void *)rsovAllocationSetSurface;
  141. break;
  142. case RS_HAL_ALLOCATION_IO_SEND:
  143. fnPtr[0] = (void *)rsovAllocationIoSend;
  144. break;
  145. case RS_HAL_ALLOCATION_IO_RECEIVE:
  146. fnPtr[0] = (void *)rsovAllocationIoReceive;
  147. break;
  148. case RS_HAL_ALLOCATION_ELEMENT_DATA:
  149. fnPtr[0] = (void *)rsovAllocationElementData;
  150. break;
  151. case RS_HAL_ALLOCATION_ELEMENT_READ:
  152. fnPtr[0] = (void *)rsovAllocationElementRead;
  153. break;
  154. case RS_HAL_CORE_SHUTDOWN:
  155. fnPtr[0] = (void *)Shutdown;
  156. break;
  157. case RS_HAL_CORE_SET_PRIORITY:
  158. fnPtr[0] = (void *)SetPriority;
  159. break;
  160. case RS_HAL_CORE_ALLOC_RUNTIME_MEM:
  161. fnPtr[0] = (void *)AllocRuntimeMem;
  162. break;
  163. case RS_HAL_CORE_FREE_RUNTIME_MEM:
  164. fnPtr[0] = (void *)FreeRuntimeMem;
  165. break;
  166. case RS_HAL_CORE_FINISH:
  167. fnPtr[0] = (void *)nullptr;
  168. break;
  169. case RS_HAL_SCRIPT_INIT:
  170. fnPtr[0] = (void *)rsovScriptInit;
  171. break;
  172. case RS_HAL_SCRIPT_INIT_INTRINSIC:
  173. fnPtr[0] = (void *)rsovInitIntrinsic;
  174. break;
  175. case RS_HAL_SCRIPT_INVOKE_FUNCTION:
  176. fnPtr[0] = (void *)rsovScriptInvokeFunction;
  177. break;
  178. case RS_HAL_SCRIPT_INVOKE_ROOT:
  179. fnPtr[0] = (void *)rsovScriptInvokeRoot;
  180. break;
  181. case RS_HAL_SCRIPT_INVOKE_FOR_EACH:
  182. fnPtr[0] = (void *)rsovScriptInvokeForEach;
  183. break;
  184. case RS_HAL_SCRIPT_INVOKE_INIT:
  185. fnPtr[0] = (void *)rsovScriptInvokeInit;
  186. break;
  187. case RS_HAL_SCRIPT_INVOKE_FREE_CHILDREN:
  188. fnPtr[0] = (void *)rsovScriptInvokeFreeChildren;
  189. break;
  190. case RS_HAL_SCRIPT_DESTROY:
  191. fnPtr[0] = (void *)rsovScriptDestroy;
  192. break;
  193. case RS_HAL_SCRIPT_SET_GLOBAL_VAR:
  194. fnPtr[0] = (void *)rsovScriptSetGlobalVar;
  195. break;
  196. case RS_HAL_SCRIPT_GET_GLOBAL_VAR:
  197. fnPtr[0] = (void *)rsovScriptGetGlobalVar;
  198. break;
  199. case RS_HAL_SCRIPT_SET_GLOBAL_VAR_WITH_ELEMENT_DIM:
  200. fnPtr[0] = (void *)rsovScriptSetGlobalVarWithElemDims;
  201. break;
  202. case RS_HAL_SCRIPT_SET_GLOBAL_BIND:
  203. fnPtr[0] = (void *)rsovScriptSetGlobalBind;
  204. break;
  205. case RS_HAL_SCRIPT_SET_GLOBAL_OBJECT:
  206. fnPtr[0] = (void *)rsovScriptSetGlobalObj;
  207. break;
  208. case RS_HAL_SCRIPT_INVOKE_FOR_EACH_MULTI:
  209. fnPtr[0] = (void *)rsovScriptInvokeForEachMulti;
  210. break;
  211. case RS_HAL_SCRIPT_UPDATE_CACHED_OBJECT:
  212. fnPtr[0] = (void *)rsovScriptUpdateCachedObject;
  213. break;
  214. case RS_HAL_SCRIPT_INVOKE_REDUCE:
  215. fnPtr[0] = (void *)rsovScriptInvokeReduce;
  216. break;
  217. case RS_HAL_SAMPLER_INIT:
  218. fnPtr[0] = (void *)rsovSamplerInit;
  219. break;
  220. case RS_HAL_SAMPLER_DESTROY:
  221. fnPtr[0] = (void *)rsovSamplerDestroy;
  222. break;
  223. case RS_HAL_SAMPLER_UPDATE_CACHED_OBJECT:
  224. fnPtr[0] = (void *)rsovSamplerUpdateCachedObject;
  225. break;
  226. case RS_HAL_TYPE_INIT:
  227. fnPtr[0] = (void *)rsovTypeInit;
  228. break;
  229. case RS_HAL_TYPE_DESTROY:
  230. fnPtr[0] = (void *)rsovTypeDestroy;
  231. break;
  232. case RS_HAL_TYPE_UPDATE_CACHED_OBJECT:
  233. fnPtr[0] = (void *)rsovTypeUpdateCachedObject;
  234. break;
  235. case RS_HAL_ELEMENT_INIT:
  236. fnPtr[0] = (void *)rsovElementInit;
  237. break;
  238. case RS_HAL_ELEMENT_DESTROY:
  239. fnPtr[0] = (void *)rsovElementDestroy;
  240. break;
  241. case RS_HAL_ELEMENT_UPDATE_CACHED_OBJECT:
  242. fnPtr[0] = (void *)rsovElementUpdateCachedObject;
  243. break;
  244. case RS_HAL_SCRIPT_GROUP_INIT:
  245. fnPtr[0] = (void *)rsovScriptGroupInit;
  246. break;
  247. case RS_HAL_SCRIPT_GROUP_DESTROY:
  248. fnPtr[0] = (void *)rsovScriptGroupDestroy;
  249. break;
  250. case RS_HAL_SCRIPT_GROUP_UPDATE_CACHED_OBJECT:
  251. fnPtr[0] = (void *)nullptr;
  252. break;
  253. case RS_HAL_SCRIPT_GROUP_SET_INPUT:
  254. fnPtr[0] = (void *)rsovScriptGroupSetInput;
  255. break;
  256. case RS_HAL_SCRIPT_GROUP_SET_OUTPUT:
  257. fnPtr[0] = (void *)rsovScriptGroupSetOutput;
  258. break;
  259. case RS_HAL_SCRIPT_GROUP_EXECUTE:
  260. fnPtr[0] = (void *)rsovScriptGroupExecute;
  261. break;
  262. // Ignore entries for the legacy graphics api,
  263. default:
  264. ALOGE("ERROR: unknown RenderScript HAL API query, %i", entry);
  265. return false;
  266. }
  267. return true;
  268. }
  269. extern "C" void rsdHalAbort(RsContext) {}
  270. extern "C" bool rsdHalQueryVersion(uint32_t *major, uint32_t *minor) {
  271. *major = RS_HAL_VERSION;
  272. *minor = 0;
  273. return true;
  274. }
  275. extern "C" bool rsdHalInit(RsContext c, uint32_t version_major,
  276. uint32_t version_minor) {
  277. Context *rsc = (Context *)c;
  278. std::unique_ptr<RSoVHal> hal(new RSoVHal());
  279. if (!hal) {
  280. ALOGE("Failed creating RSoV driver hal.");
  281. return false;
  282. }
  283. std::unique_ptr<rsov::RSoVContext> rsov(rsov::RSoVContext::create());
  284. if (!rsov) {
  285. ALOGE("RSoVContext::create for driver hal failed.");
  286. return false;
  287. }
  288. std::unique_ptr<RsdCpuReference> cpuref(RsdCpuReference::create(rsc, version_major, version_minor,
  289. &lookupRuntimeStubs,
  290. &rsov::lookupCpuScript));
  291. if (!cpuref) {
  292. ALOGE("RsdCpuReference::create for driver hal failed.");
  293. return false;
  294. }
  295. hal->mRSoV = rsov.release();
  296. hal->mCpuRef = cpuref.release();
  297. rsc->mHal.drv = hal.release();
  298. return true;
  299. }
  300. } // namespace renderscript
  301. } // namespace android