rsApiStubs.cpp 36 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911
  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. #include "rsApiStubs.h"
  17. #include "rsHidlAdaptation.h"
  18. #include "rsFallbackAdaptation.h"
  19. #include "cpp/rsDispatch.h"
  20. #include <log/log.h>
  21. #include <dlfcn.h>
  22. #include <mutex>
  23. #include <map>
  24. #undef LOG_TAG
  25. #define LOG_TAG "RenderScript"
  26. // TODO: Figure out how to use different declared types for the two interfaces
  27. // to avoid the confusion. Currently, RsContext is used as the API type for
  28. // both the client interface and the dispatch table interface, but at the
  29. // client interface it's really RsContextWrapper* instead.
  30. // TODO: Figure out how to better design class hierarchy for all these Contexts.
  31. // RsContextWrapper wraps the RsContext and corresponding dispatchTable pointer.
  32. // The wrapper object is created during ContextCreate, and the address of the
  33. // object is returned to Java / C++, instead of the RsContext handle.
  34. // The wrapper object is destroyed during ContextDestroy to release the memory.
  35. struct RsContextWrapper {
  36. RsContext context;
  37. const dispatchTable* dispatch;
  38. };
  39. #define RS_DISPATCH(opaqueWrapper, func, ...) \
  40. [&]() { \
  41. const RsContextWrapper *wrapper = reinterpret_cast<RsContextWrapper *>(opaqueWrapper); \
  42. RsContext context = wrapper->context; \
  43. return wrapper->dispatch->func(context, ##__VA_ARGS__); \
  44. }()
  45. // contextMap maps RsContext to the corresponding RsContextWrapper pointer.
  46. static std::map<RsContext, RsContextWrapper* > contextMap;
  47. // contextMapMutex is used to protect concurrent access of the contextMap.
  48. // std::mutex is safe for pthreads on Android. Since other threading model
  49. // supported on Android are built on top of pthread, std::mutex is safe for them.
  50. static std::mutex contextMapMutex;
  51. // globalObjAlive is a global flag indicating whether the global objects,
  52. // contextMap & contextMapMutex, are still alive.
  53. // For the protected functions during application teardown, this
  54. // flag will be checked before accessing the global objects.
  55. static bool globalObjAlive;
  56. // GlobalObjGuard manipulates the globalObjAlive flag during construction and
  57. // destruction. If the guard object is destroyed, globalObjAlive will be set
  58. // to false, which will make the protected functions NO-OP.
  59. // https://goto.google.com/rs-static-destructor
  60. class GlobalObjGuard {
  61. public:
  62. GlobalObjGuard() {
  63. globalObjAlive = true;
  64. }
  65. ~GlobalObjGuard() {
  66. globalObjAlive = false;
  67. }
  68. };
  69. static GlobalObjGuard guard;
  70. // API to find high-level context (RsContextWrapper) given a low level context.
  71. // This API is only intended to be used by RenderScript debugger.
  72. extern "C" RsContext rsDebugGetHighLevelContext(RsContext context) {
  73. std::unique_lock<std::mutex> lock(contextMapMutex);
  74. return contextMap.at(context);
  75. }
  76. // Device
  77. // These API stubs are kept here to maintain backward compatibility,
  78. // but they are not actually doing anything.
  79. extern "C" RsDevice rsDeviceCreate()
  80. {
  81. return (void *) 1;
  82. }
  83. extern "C" void rsDeviceDestroy(RsDevice dev)
  84. {
  85. }
  86. extern "C" void rsDeviceSetConfig(RsDevice dev, RsDeviceParam p, int32_t value)
  87. {
  88. }
  89. /*
  90. * This global will be found by the debugger and will have its value flipped.
  91. * It's independent of the Context class to allow the debugger to do the above
  92. * without knowing the type makeup. This allows the debugger to be attached at
  93. * an earlier stage.
  94. */
  95. extern "C" int gDebuggerPresent = 0;
  96. namespace{
  97. // Check if the calling process is a vendor process or not.
  98. static bool isVendorProcess() {
  99. char path[PATH_MAX];
  100. ssize_t path_len = readlink("/proc/self/exe", path, sizeof(path));
  101. if (path_len == -1) {
  102. return false;
  103. }
  104. // Vendor process will return "/vendor/*"
  105. static const char vendor_path[] = "/vendor/";
  106. return !strncmp(path, vendor_path, sizeof(vendor_path)-1);
  107. }
  108. typedef const char* (*QueryCacheDirFnPtr)();
  109. // Dynamically load the queryCacheDir function pointer, so that for vendor
  110. // processes, libandroid_runtime.so will not be loaded.
  111. static QueryCacheDirFnPtr loadQueryCacheFnPtr() {
  112. QueryCacheDirFnPtr queryCacheDir = nullptr;
  113. void* handle = dlopen("libRSCacheDir.so", RTLD_LAZY | RTLD_LOCAL);
  114. if (!handle ||
  115. !(queryCacheDir = (QueryCacheDirFnPtr)dlsym(handle, "rsQueryCacheDir"))) {
  116. ALOGW("Not able to query cache dir: %s", dlerror());
  117. }
  118. return queryCacheDir;
  119. }
  120. } // anonymous namespace
  121. // Context
  122. extern "C" RsContext rsContextCreate(RsDevice vdev, uint32_t version, uint32_t sdkVersion,
  123. RsContextType ct, uint32_t flags)
  124. {
  125. if (!globalObjAlive) {
  126. ALOGE("rsContextCreate is not allowed during process teardown.");
  127. return nullptr;
  128. }
  129. RsContext context;
  130. RsContextWrapper *ctxWrapper;
  131. if (flags & RS_CONTEXT_LOW_LATENCY) {
  132. // Use CPU path for LOW_LATENCY context.
  133. RsFallbackAdaptation& instance = RsFallbackAdaptation::GetInstance();
  134. context = instance.GetEntryFuncs()->ContextCreate(vdev, version, sdkVersion, ct, flags);
  135. ctxWrapper = new RsContextWrapper{context, instance.GetEntryFuncs()};
  136. } else {
  137. RsHidlAdaptation& instance = RsHidlAdaptation::GetInstance();
  138. context = instance.GetEntryFuncs()->ContextCreate(vdev, version, sdkVersion, ct, flags);
  139. ctxWrapper = new RsContextWrapper{context, instance.GetEntryFuncs()};
  140. // Use function local static variables to ensure thread safety.
  141. static QueryCacheDirFnPtr queryCacheDir = isVendorProcess() ? nullptr : loadQueryCacheFnPtr();
  142. if (queryCacheDir) {
  143. static std::string defaultCacheDir = std::string(queryCacheDir());
  144. if (defaultCacheDir.size() > 0) {
  145. ALOGD("Setting cache dir: %s", defaultCacheDir.c_str());
  146. rsContextSetCacheDir(ctxWrapper,
  147. defaultCacheDir.c_str(),
  148. defaultCacheDir.size());
  149. }
  150. }
  151. }
  152. // Wait for debugger to attach if RS_CONTEXT_WAIT_FOR_ATTACH flag set.
  153. if (flags & RS_CONTEXT_WAIT_FOR_ATTACH) {
  154. while (!gDebuggerPresent) {
  155. sleep(0);
  156. }
  157. }
  158. // Lock contextMap when adding new entries.
  159. std::unique_lock<std::mutex> lock(contextMapMutex);
  160. contextMap.insert(std::make_pair(context, ctxWrapper));
  161. return (RsContext) ctxWrapper;
  162. }
  163. extern "C" void rsContextDestroy (RsContext ctxWrapper)
  164. {
  165. if (!globalObjAlive) {
  166. return;
  167. }
  168. RS_DISPATCH(ctxWrapper, ContextDestroy);
  169. // Lock contextMap when deleting an existing entry.
  170. std::unique_lock<std::mutex> lock(contextMapMutex);
  171. contextMap.erase(reinterpret_cast< RsContextWrapper* >(ctxWrapper)->context);
  172. delete (RsContextWrapper *)ctxWrapper;
  173. }
  174. extern "C" void rsContextFinish (RsContext ctxWrapper)
  175. {
  176. RS_DISPATCH(ctxWrapper, ContextFinish);
  177. }
  178. extern "C" void rsContextDump (RsContext ctxWrapper, int32_t bits)
  179. {
  180. RS_DISPATCH(ctxWrapper, ContextDump, bits);
  181. }
  182. extern "C" void rsContextSetPriority (RsContext ctxWrapper, int32_t priority)
  183. {
  184. RS_DISPATCH(ctxWrapper, ContextSetPriority, priority);
  185. }
  186. extern "C" void rsContextDestroyWorker (RsContext ctxWrapper)
  187. {
  188. }
  189. extern "C" RsMessageToClientType rsContextGetMessage (RsContext ctxWrapper, void * data, size_t data_length,
  190. size_t * receiveLen, size_t receiveLen_length,
  191. uint32_t * usrID, size_t usrID_length)
  192. {
  193. return RS_DISPATCH(ctxWrapper, ContextGetMessage, data, data_length,
  194. receiveLen, receiveLen_length,
  195. usrID, usrID_length);
  196. }
  197. extern "C" RsMessageToClientType rsContextPeekMessage (RsContext ctxWrapper,
  198. size_t * receiveLen, size_t receiveLen_length,
  199. uint32_t * usrID, size_t usrID_length)
  200. {
  201. return RS_DISPATCH(ctxWrapper, ContextPeekMessage,
  202. receiveLen, receiveLen_length,
  203. usrID, usrID_length);
  204. }
  205. extern "C" void rsContextSendMessage (RsContext ctxWrapper, uint32_t id, const uint8_t * data, size_t data_length)
  206. {
  207. RS_DISPATCH(ctxWrapper, ContextSendMessage, id, data, data_length);
  208. }
  209. extern "C" void rsContextInitToClient (RsContext ctxWrapper)
  210. {
  211. RS_DISPATCH(ctxWrapper, ContextInitToClient);
  212. }
  213. extern "C" void rsContextDeinitToClient (RsContext ctxWrapper)
  214. {
  215. RS_DISPATCH(ctxWrapper, ContextDeinitToClient);
  216. }
  217. extern "C" void rsContextSetCacheDir (RsContext ctxWrapper, const char * cacheDir, size_t cacheDir_length)
  218. {
  219. RS_DISPATCH(ctxWrapper, ContextSetCacheDir, cacheDir, cacheDir_length);
  220. }
  221. extern "C" void rsaContextSetNativeLibDir(RsContext ctxWrapper, char *libDir, size_t length)
  222. {
  223. }
  224. // BaseObject
  225. extern "C" void rsAssignName (RsContext ctxWrapper, RsObjectBase obj, const char * name, size_t name_length)
  226. {
  227. RS_DISPATCH(ctxWrapper, AssignName, obj, name, name_length);
  228. }
  229. extern "C" void rsaGetName(RsContext ctxWrapper, void * obj, const char **name)
  230. {
  231. RS_DISPATCH(ctxWrapper, GetName, obj, name);
  232. }
  233. extern "C" void rsObjDestroy (RsContext ctxWrapper, RsAsyncVoidPtr objPtr)
  234. {
  235. RS_DISPATCH(ctxWrapper, ObjDestroy, objPtr);
  236. }
  237. // Element
  238. extern "C" RsElement rsElementCreate (RsContext ctxWrapper, RsDataType mType, RsDataKind mKind,
  239. bool mNormalized, uint32_t mVectorSize)
  240. {
  241. return RS_DISPATCH(ctxWrapper, ElementCreate, mType, mKind, mNormalized, mVectorSize);
  242. }
  243. extern "C" RsElement rsElementCreate2 (RsContext ctxWrapper, const RsElement * elements, size_t elements_length,
  244. const char ** names, size_t names_length_length, const size_t * names_length,
  245. const uint32_t * arraySize, size_t arraySize_length)
  246. {
  247. return RS_DISPATCH(ctxWrapper, ElementCreate2,
  248. elements, elements_length,
  249. names, names_length_length, names_length,
  250. arraySize, arraySize_length);
  251. }
  252. extern "C" void rsaElementGetNativeData(RsContext ctxWrapper, RsElement elem, uint32_t *elemData, uint32_t elemDataSize)
  253. {
  254. RS_DISPATCH(ctxWrapper, ElementGetNativeData, elem, elemData, elemDataSize);
  255. }
  256. extern "C" void rsaElementGetSubElements(RsContext ctxWrapper, RsElement elem, uintptr_t *ids, const char **names,
  257. size_t *arraySizes, uint32_t dataSize)
  258. {
  259. RS_DISPATCH(ctxWrapper, ElementGetSubElements, elem, ids, names, arraySizes, dataSize);
  260. }
  261. // Type
  262. extern "C" RsType rsTypeCreate (RsContext ctxWrapper, RsElement e, uint32_t dimX, uint32_t dimY, uint32_t dimZ,
  263. bool mipmaps, bool faces, uint32_t yuv)
  264. {
  265. return RS_DISPATCH(ctxWrapper, TypeCreate, e, dimX, dimY, dimZ, mipmaps, faces, yuv);
  266. }
  267. extern "C" RsType rsTypeCreate2 (RsContext ctxWrapper, const RsTypeCreateParams * dat, size_t dat_length)
  268. {
  269. return nullptr;
  270. }
  271. extern "C" void rsaTypeGetNativeData(RsContext ctxWrapper, RsType type, uintptr_t *typeData, uint32_t typeDataSize)
  272. {
  273. RS_DISPATCH(ctxWrapper, TypeGetNativeData, type, typeData, typeDataSize);
  274. }
  275. // Allocation
  276. extern "C" RsAllocation rsAllocationCreateTyped (RsContext ctxWrapper, RsType vtype, RsAllocationMipmapControl mipmaps,
  277. uint32_t usages, uintptr_t ptr)
  278. {
  279. return RS_DISPATCH(ctxWrapper, AllocationCreateTyped, vtype, mipmaps, usages, ptr);
  280. }
  281. extern "C" RsAllocation rsAllocationCreateFromBitmap (RsContext ctxWrapper, RsType vtype, RsAllocationMipmapControl mipmaps,
  282. const void * data, size_t data_length, uint32_t usages)
  283. {
  284. return RS_DISPATCH(ctxWrapper, AllocationCreateFromBitmap, vtype, mipmaps, data, data_length, usages);
  285. }
  286. extern "C" RsAllocation rsAllocationCubeCreateFromBitmap (RsContext ctxWrapper, RsType vtype, RsAllocationMipmapControl mipmaps,
  287. const void * data, size_t data_length, uint32_t usages)
  288. {
  289. return RS_DISPATCH(ctxWrapper, AllocationCubeCreateFromBitmap, vtype, mipmaps, data, data_length, usages);
  290. }
  291. extern "C" RsAllocation rsAllocationAdapterCreate (RsContext ctxWrapper, RsType vtype, RsAllocation baseAlloc)
  292. {
  293. return RS_DISPATCH(ctxWrapper, AllocationAdapterCreate, vtype, baseAlloc);
  294. }
  295. extern "C" const void * rsaAllocationGetType(RsContext ctxWrapper, RsAllocation va)
  296. {
  297. return RS_DISPATCH(ctxWrapper, AllocationGetType, va);
  298. }
  299. extern "C" RsNativeWindow rsAllocationGetSurface (RsContext ctxWrapper, RsAllocation alloc)
  300. {
  301. return RS_DISPATCH(ctxWrapper, AllocationGetSurface, alloc);
  302. }
  303. extern "C" void rsAllocationSetupBufferQueue (RsContext ctxWrapper, RsAllocation alloc, uint32_t numAlloc)
  304. {
  305. RS_DISPATCH(ctxWrapper, AllocationSetupBufferQueue, alloc, numAlloc);
  306. }
  307. extern "C" void rsAllocationShareBufferQueue (RsContext ctxWrapper, RsAllocation alloc1, RsAllocation alloc2)
  308. {
  309. RS_DISPATCH(ctxWrapper, AllocationShareBufferQueue, alloc1, alloc2);
  310. }
  311. extern "C" void rsAllocationSetSurface (RsContext ctxWrapper, RsAllocation alloc, RsNativeWindow sur)
  312. {
  313. RS_DISPATCH(ctxWrapper, AllocationSetSurface, alloc, sur);
  314. }
  315. extern "C" void rsAllocationAdapterOffset (RsContext ctxWrapper, RsAllocation alloc,
  316. const uint32_t * offsets, size_t offsets_length)
  317. {
  318. RS_DISPATCH(ctxWrapper, AllocationAdapterOffset, alloc, offsets, offsets_length);
  319. }
  320. extern "C" void rsAllocationCopyToBitmap (RsContext ctxWrapper, RsAllocation alloc, void * data, size_t data_length)
  321. {
  322. RS_DISPATCH(ctxWrapper, AllocationCopyToBitmap, alloc, data, data_length);
  323. }
  324. extern "C" void * rsAllocationGetPointer (RsContext ctxWrapper, RsAllocation va, uint32_t lod, RsAllocationCubemapFace face,
  325. uint32_t z, uint32_t array, size_t * stride, size_t stride_length)
  326. {
  327. return RS_DISPATCH(ctxWrapper, AllocationGetPointer, va, lod, face, z, array, stride, stride_length);
  328. }
  329. extern "C" void rsAllocation1DData (RsContext ctxWrapper, RsAllocation va, uint32_t xoff, uint32_t lod, uint32_t count,
  330. const void * data, size_t data_length)
  331. {
  332. RS_DISPATCH(ctxWrapper, Allocation1DData, va, xoff, lod, count, data, data_length);
  333. }
  334. extern "C" void rsAllocation1DElementData (RsContext ctxWrapper, RsAllocation va, uint32_t x, uint32_t lod,
  335. const void * data, size_t data_length, size_t comp_offset)
  336. {
  337. RS_DISPATCH(ctxWrapper, Allocation1DElementData, va, x, lod, data, data_length, comp_offset);
  338. }
  339. extern "C" void rsAllocationElementData (RsContext ctxWrapper, RsAllocation va, uint32_t x, uint32_t y, uint32_t z,
  340. uint32_t lod, const void * data, size_t data_length, size_t comp_offset)
  341. {
  342. RS_DISPATCH(ctxWrapper, AllocationElementData, va, x, y, z, lod, data, data_length, comp_offset);
  343. }
  344. extern "C" void rsAllocation2DData (RsContext ctxWrapper, RsAllocation va, uint32_t xoff, uint32_t yoff, uint32_t lod,
  345. RsAllocationCubemapFace face, uint32_t w, uint32_t h,
  346. const void * data, size_t data_length, size_t stride)
  347. {
  348. RS_DISPATCH(ctxWrapper, Allocation2DData, va, xoff, yoff, lod, face, w, h, data, data_length, stride);
  349. }
  350. extern "C" void rsAllocation3DData (RsContext ctxWrapper, RsAllocation va, uint32_t xoff, uint32_t yoff, uint32_t zoff,
  351. uint32_t lod, uint32_t w, uint32_t h, uint32_t d,
  352. const void * data, size_t data_length, size_t stride)
  353. {
  354. RS_DISPATCH(ctxWrapper, Allocation3DData, va, xoff, yoff, zoff, lod, w, h, d, data, data_length, stride);
  355. }
  356. extern "C" void rsAllocationGenerateMipmaps (RsContext ctxWrapper, RsAllocation va)
  357. {
  358. RS_DISPATCH(ctxWrapper, AllocationGenerateMipmaps, va);
  359. }
  360. extern "C" void rsAllocationRead (RsContext ctxWrapper, RsAllocation va, void * data, size_t data_length)
  361. {
  362. RS_DISPATCH(ctxWrapper, AllocationRead, va, data, data_length);
  363. }
  364. extern "C" void rsAllocation1DRead (RsContext ctxWrapper, RsAllocation va, uint32_t xoff, uint32_t lod, uint32_t count,
  365. void * data, size_t data_length)
  366. {
  367. RS_DISPATCH(ctxWrapper, Allocation1DRead, va, xoff, lod, count, data, data_length);
  368. }
  369. extern "C" void rsAllocationElementRead (RsContext ctxWrapper, RsAllocation va, uint32_t x, uint32_t y, uint32_t z,
  370. uint32_t lod, void * data, size_t data_length, size_t comp_offset)
  371. {
  372. RS_DISPATCH(ctxWrapper, AllocationElementRead, va, x, y, z, lod, data, data_length, comp_offset);
  373. }
  374. extern "C" void rsAllocation2DRead (RsContext ctxWrapper, RsAllocation va, uint32_t xoff, uint32_t yoff, uint32_t lod,
  375. RsAllocationCubemapFace face, uint32_t w, uint32_t h,
  376. void * data, size_t data_length, size_t stride)
  377. {
  378. RS_DISPATCH(ctxWrapper, Allocation2DRead, va, xoff, yoff, lod, face, w, h, data, data_length, stride);
  379. }
  380. extern "C" void rsAllocation3DRead (RsContext ctxWrapper, RsAllocation va, uint32_t xoff, uint32_t yoff, uint32_t zoff,
  381. uint32_t lod, uint32_t w, uint32_t h, uint32_t d,
  382. void * data, size_t data_length, size_t stride)
  383. {
  384. RS_DISPATCH(ctxWrapper, Allocation3DRead, va, xoff, yoff, zoff, lod, w, h, d, data, data_length, stride);
  385. }
  386. extern "C" void rsAllocationSyncAll (RsContext ctxWrapper, RsAllocation va, RsAllocationUsageType src)
  387. {
  388. RS_DISPATCH(ctxWrapper, AllocationSyncAll, va, src);
  389. }
  390. extern "C" void rsAllocationResize1D (RsContext ctxWrapper, RsAllocation va, uint32_t dimX)
  391. {
  392. RS_DISPATCH(ctxWrapper, AllocationResize1D, va, dimX);
  393. }
  394. extern "C" void rsAllocationCopy2DRange (RsContext ctxWrapper,
  395. RsAllocation dest,
  396. uint32_t destXoff, uint32_t destYoff,
  397. uint32_t destMip, uint32_t destFace,
  398. uint32_t width, uint32_t height,
  399. RsAllocation src,
  400. uint32_t srcXoff, uint32_t srcYoff,
  401. uint32_t srcMip, uint32_t srcFace)
  402. {
  403. RS_DISPATCH(ctxWrapper, AllocationCopy2DRange, dest, destXoff, destYoff, destMip, destFace,
  404. width, height, src, srcXoff, srcYoff, srcMip, srcFace);
  405. }
  406. extern "C" void rsAllocationCopy3DRange (RsContext ctxWrapper,
  407. RsAllocation dest,
  408. uint32_t destXoff, uint32_t destYoff, uint32_t destZoff,
  409. uint32_t destMip,
  410. uint32_t width, uint32_t height, uint32_t depth,
  411. RsAllocation src,
  412. uint32_t srcXoff, uint32_t srcYoff, uint32_t srcZoff,
  413. uint32_t srcMip)
  414. {
  415. RS_DISPATCH(ctxWrapper, AllocationCopy3DRange,
  416. dest, destXoff, destYoff, destZoff, destMip,
  417. width, height, depth, src, srcXoff, srcYoff, srcZoff, srcMip);
  418. }
  419. extern "C" void rsAllocationIoSend (RsContext ctxWrapper, RsAllocation alloc)
  420. {
  421. RS_DISPATCH(ctxWrapper, AllocationIoSend, alloc);
  422. }
  423. extern "C" int64_t rsAllocationIoReceive (RsContext ctxWrapper, RsAllocation alloc)
  424. {
  425. return RS_DISPATCH(ctxWrapper, AllocationIoReceive, alloc);
  426. }
  427. // ScriptGroup
  428. extern "C" void rsScriptGroupExecute (RsContext ctxWrapper, RsScriptGroup group)
  429. {
  430. RS_DISPATCH(ctxWrapper, ScriptGroupExecute, group);
  431. }
  432. extern "C" RsScriptGroup2 rsScriptGroup2Create (RsContext ctxWrapper, const char * name, size_t name_length,
  433. const char * cacheDir, size_t cacheDir_length,
  434. RsClosure * closures, size_t closures_length)
  435. {
  436. return RS_DISPATCH(ctxWrapper, ScriptGroup2Create,
  437. name, name_length,
  438. cacheDir, cacheDir_length,
  439. closures, closures_length);
  440. }
  441. extern "C" RsClosure rsClosureCreate (RsContext ctxWrapper, RsScriptKernelID kernelID, RsAllocation returnValue,
  442. RsScriptFieldID * fieldIDs, size_t fieldIDs_length,
  443. const int64_t * values, size_t values_length,
  444. const int * sizes, size_t sizes_length,
  445. RsClosure * depClosures, size_t depClosures_length,
  446. RsScriptFieldID * depFieldIDs, size_t depFieldIDs_length)
  447. {
  448. return RS_DISPATCH(ctxWrapper, ClosureCreate, kernelID, returnValue, fieldIDs, fieldIDs_length,
  449. const_cast<int64_t *>(values), values_length,
  450. const_cast<int *>(sizes), sizes_length,
  451. depClosures, depClosures_length, depFieldIDs, depFieldIDs_length);
  452. }
  453. extern "C" RsClosure rsInvokeClosureCreate (RsContext ctxWrapper, RsScriptInvokeID invokeID,
  454. const void * params, size_t params_length,
  455. const RsScriptFieldID * fieldIDs, size_t fieldIDs_length,
  456. const int64_t * values, size_t values_length,
  457. const int * sizes, size_t sizes_length)
  458. {
  459. return RS_DISPATCH(ctxWrapper, InvokeClosureCreate, invokeID,
  460. params, params_length,
  461. fieldIDs, fieldIDs_length,
  462. values, values_length,
  463. sizes, sizes_length);
  464. }
  465. extern "C" void rsClosureSetArg (RsContext ctxWrapper, RsClosure closureID, uint32_t index,
  466. uintptr_t value, int valueSize)
  467. {
  468. RS_DISPATCH(ctxWrapper, ClosureSetArg, closureID, index, value, valueSize);
  469. }
  470. extern "C" void rsClosureSetGlobal (RsContext ctxWrapper, RsClosure closureID, RsScriptFieldID fieldID,
  471. int64_t value, int valueSize)
  472. {
  473. RS_DISPATCH(ctxWrapper, ClosureSetGlobal, closureID, fieldID, value, valueSize);
  474. }
  475. extern "C" RsScriptKernelID rsScriptKernelIDCreate (RsContext ctxWrapper, RsScript sid, int slot, int sig)
  476. {
  477. return RS_DISPATCH(ctxWrapper, ScriptKernelIDCreate, sid, slot, sig);
  478. }
  479. extern "C" RsScriptFieldID rsScriptFieldIDCreate (RsContext ctxWrapper, RsScript sid, int slot)
  480. {
  481. return RS_DISPATCH(ctxWrapper, ScriptFieldIDCreate, sid, slot);
  482. }
  483. extern "C" RsScriptGroup rsScriptGroupCreate (RsContext ctxWrapper, RsScriptKernelID * kernels, size_t kernels_length,
  484. RsScriptKernelID * src, size_t src_length,
  485. RsScriptKernelID * dstK, size_t dstK_length,
  486. RsScriptFieldID * dstF, size_t dstF_length,
  487. const RsType * type, size_t type_length)
  488. {
  489. return RS_DISPATCH(ctxWrapper, ScriptGroupCreate,
  490. kernels, kernels_length,
  491. src, src_length, dstK, dstK_length,
  492. dstF, dstF_length, type, type_length);
  493. }
  494. extern "C" void rsScriptGroupSetOutput (RsContext ctxWrapper, RsScriptGroup group,
  495. RsScriptKernelID kernel, RsAllocation alloc)
  496. {
  497. RS_DISPATCH(ctxWrapper, ScriptGroupSetOutput, group, kernel, alloc);
  498. }
  499. extern "C" void rsScriptGroupSetInput (RsContext ctxWrapper, RsScriptGroup group,
  500. RsScriptKernelID kernel, RsAllocation alloc)
  501. {
  502. RS_DISPATCH(ctxWrapper, ScriptGroupSetInput, group, kernel, alloc);
  503. }
  504. // Sampler
  505. extern "C" RsSampler rsSamplerCreate (RsContext ctxWrapper, RsSamplerValue magFilter, RsSamplerValue minFilter,
  506. RsSamplerValue wrapS, RsSamplerValue wrapT, RsSamplerValue wrapR,
  507. float mAniso)
  508. {
  509. return RS_DISPATCH(ctxWrapper, SamplerCreate, magFilter, minFilter, wrapS, wrapT, wrapR, mAniso);
  510. }
  511. // Script
  512. extern "C" RsScript rsScriptCCreate (RsContext ctxWrapper, const char * resName, size_t resName_length,
  513. const char * cacheDir, size_t cacheDir_length,
  514. const char * text, size_t text_length)
  515. {
  516. return RS_DISPATCH(ctxWrapper, ScriptCCreate, resName, resName_length, cacheDir, cacheDir_length, text, text_length);
  517. }
  518. extern "C" RsScript rsScriptIntrinsicCreate (RsContext ctxWrapper, uint32_t id, RsElement eid)
  519. {
  520. return RS_DISPATCH(ctxWrapper, ScriptIntrinsicCreate, id, eid);
  521. }
  522. extern "C" void rsScriptBindAllocation (RsContext ctxWrapper, RsScript vtm, RsAllocation va, uint32_t slot)
  523. {
  524. RS_DISPATCH(ctxWrapper, ScriptBindAllocation, vtm, va, slot);
  525. }
  526. extern "C" void rsScriptSetTimeZone (RsContext ctxWrapper, RsScript s, const char * timeZone, size_t timeZone_length)
  527. {
  528. RS_DISPATCH(ctxWrapper, ScriptSetTimeZone, s, timeZone, timeZone_length);
  529. }
  530. extern "C" RsScriptInvokeID rsScriptInvokeIDCreate (RsContext ctxWrapper, RsScript s, uint32_t slot)
  531. {
  532. return RS_DISPATCH(ctxWrapper, ScriptInvokeIDCreate, s, slot);
  533. }
  534. extern "C" void rsScriptInvoke (RsContext ctxWrapper, RsScript s, uint32_t slot)
  535. {
  536. RS_DISPATCH(ctxWrapper, ScriptInvoke, s, slot);
  537. }
  538. extern "C" void rsScriptInvokeV (RsContext ctxWrapper, RsScript s, uint32_t slot, const void * data, size_t data_length)
  539. {
  540. RS_DISPATCH(ctxWrapper, ScriptInvokeV, s, slot, data, data_length);
  541. }
  542. extern "C" void rsScriptForEach (RsContext ctxWrapper, RsScript s, uint32_t slot,
  543. RsAllocation ain, RsAllocation aout,
  544. const void * usr, size_t usr_length,
  545. const RsScriptCall * sc, size_t sc_length)
  546. {
  547. RS_DISPATCH(ctxWrapper, ScriptForEach, s, slot, ain, aout, usr, usr_length, sc, sc_length);
  548. }
  549. extern "C" void rsScriptForEachMulti (RsContext ctxWrapper, RsScript s, uint32_t slot,
  550. RsAllocation * ains, size_t ains_length, RsAllocation aout,
  551. const void * usr, size_t usr_length,
  552. const RsScriptCall * sc, size_t sc_length)
  553. {
  554. RS_DISPATCH(ctxWrapper, ScriptForEachMulti, s, slot, ains, ains_length, aout, usr, usr_length, sc, sc_length);
  555. }
  556. extern "C" void rsScriptReduce (RsContext ctxWrapper, RsScript s, uint32_t slot,
  557. RsAllocation * ains, size_t ains_length, RsAllocation aout,
  558. const RsScriptCall * sc, size_t sc_length)
  559. {
  560. RS_DISPATCH(ctxWrapper, ScriptReduce, s, slot, ains, ains_length, aout, sc, sc_length);
  561. }
  562. extern "C" void rsScriptSetVarI (RsContext ctxWrapper, RsScript s, uint32_t slot, int value)
  563. {
  564. RS_DISPATCH(ctxWrapper, ScriptSetVarI, s, slot, value);
  565. }
  566. extern "C" void rsScriptSetVarObj (RsContext ctxWrapper, RsScript s, uint32_t slot, RsObjectBase value)
  567. {
  568. RS_DISPATCH(ctxWrapper, ScriptSetVarObj, s, slot, value);
  569. }
  570. extern "C" void rsScriptSetVarJ (RsContext ctxWrapper, RsScript s, uint32_t slot, int64_t value)
  571. {
  572. RS_DISPATCH(ctxWrapper, ScriptSetVarJ, s, slot, value);
  573. }
  574. extern "C" void rsScriptSetVarF (RsContext ctxWrapper, RsScript s, uint32_t slot, float value)
  575. {
  576. RS_DISPATCH(ctxWrapper, ScriptSetVarF, s, slot, value);
  577. }
  578. extern "C" void rsScriptSetVarD (RsContext ctxWrapper, RsScript s, uint32_t slot, double value)
  579. {
  580. RS_DISPATCH(ctxWrapper, ScriptSetVarD, s, slot, value);
  581. }
  582. extern "C" void rsScriptSetVarV (RsContext ctxWrapper, RsScript s, uint32_t slot,
  583. const void * data, size_t data_length)
  584. {
  585. RS_DISPATCH(ctxWrapper, ScriptSetVarV, s, slot, data, data_length);
  586. }
  587. extern "C" void rsScriptGetVarV (RsContext ctxWrapper, RsScript s, uint32_t slot,
  588. void * data, size_t data_length)
  589. {
  590. RS_DISPATCH(ctxWrapper, ScriptGetVarV, s, slot, data, data_length);
  591. }
  592. extern "C" void rsScriptSetVarVE (RsContext ctxWrapper, RsScript s, uint32_t slot,
  593. const void * data, size_t data_length,
  594. RsElement e, const uint32_t * dims, size_t dims_length)
  595. {
  596. RS_DISPATCH(ctxWrapper, ScriptSetVarVE, s, slot, data, data_length, e, dims, dims_length);
  597. }
  598. // Graphics
  599. /* The following API are deprecated. */
  600. extern "C" RsContext rsContextCreateGL(RsDevice vdev, uint32_t version, uint32_t sdkVersion,
  601. RsSurfaceConfig sc, uint32_t dpi)
  602. {
  603. if (!globalObjAlive) {
  604. ALOGE("rsContextCreateGL is not allowed during process teardown.");
  605. return nullptr;
  606. }
  607. RsFallbackAdaptation& instance = RsFallbackAdaptation::GetInstance();
  608. RsContext context = instance.GetEntryFuncs()->ContextCreateGL(vdev, version, sdkVersion, sc, dpi);
  609. RsContextWrapper *ctxWrapper = new RsContextWrapper{context, instance.GetEntryFuncs()};
  610. // Lock contextMap when adding new entries.
  611. std::unique_lock<std::mutex> lock(contextMapMutex);
  612. contextMap.insert(std::make_pair(context, ctxWrapper));
  613. return (RsContext) ctxWrapper;
  614. }
  615. extern "C" void rsContextBindProgramStore (RsContext ctxWrapper, RsProgramStore pgm)
  616. {
  617. RS_DISPATCH(ctxWrapper, ContextBindProgramStore, pgm);
  618. }
  619. extern "C" void rsContextBindProgramFragment (RsContext ctxWrapper, RsProgramFragment pgm)
  620. {
  621. RS_DISPATCH(ctxWrapper, ContextBindProgramFragment, pgm);
  622. }
  623. extern "C" void rsContextBindProgramVertex (RsContext ctxWrapper, RsProgramVertex pgm)
  624. {
  625. RS_DISPATCH(ctxWrapper, ContextBindProgramVertex, pgm);
  626. }
  627. extern "C" void rsContextBindProgramRaster (RsContext ctxWrapper, RsProgramRaster pgm)
  628. {
  629. RS_DISPATCH(ctxWrapper, ContextBindProgramRaster, pgm);
  630. }
  631. extern "C" void rsContextBindFont (RsContext ctxWrapper, RsFont pgm)
  632. {
  633. RS_DISPATCH(ctxWrapper, ContextBindFont, pgm);
  634. }
  635. extern "C" void rsContextSetSurface (RsContext ctxWrapper, uint32_t width, uint32_t height,
  636. RsNativeWindow sur)
  637. {
  638. RS_DISPATCH(ctxWrapper, ContextSetSurface, width, height, sur);
  639. }
  640. extern "C" void rsContextBindRootScript (RsContext ctxWrapper, RsScript sampler)
  641. {
  642. RS_DISPATCH(ctxWrapper, ContextBindRootScript, sampler);
  643. }
  644. extern "C" void rsContextPause (RsContext ctxWrapper)
  645. {
  646. RS_DISPATCH(ctxWrapper, ContextPause);
  647. }
  648. extern "C" void rsContextResume (RsContext ctxWrapper)
  649. {
  650. RS_DISPATCH(ctxWrapper, ContextResume);
  651. }
  652. extern "C" RsProgramStore rsProgramStoreCreate (RsContext ctxWrapper,
  653. bool colorMaskR, bool colorMaskG,
  654. bool colorMaskB, bool colorMaskA,
  655. bool depthMask, bool ditherEnable,
  656. RsBlendSrcFunc srcFunc,
  657. RsBlendDstFunc destFunc,
  658. RsDepthFunc depthFunc)
  659. {
  660. return RS_DISPATCH(ctxWrapper, ProgramStoreCreate,
  661. colorMaskR, colorMaskG, colorMaskB, colorMaskA,
  662. depthMask, ditherEnable, srcFunc, destFunc, depthFunc);
  663. }
  664. extern "C" RsProgramRaster rsProgramRasterCreate (RsContext ctxWrapper, bool pointSprite, RsCullMode cull)
  665. {
  666. return RS_DISPATCH(ctxWrapper, ProgramRasterCreate, pointSprite, cull);
  667. }
  668. extern "C" RsProgramFragment rsProgramFragmentCreate (RsContext ctxWrapper,
  669. const char * shaderText, size_t shaderText_length,
  670. const char ** textureNames, size_t textureNames_length_length,
  671. const size_t * textureNames_length,
  672. const uintptr_t * params, size_t params_length)
  673. {
  674. return RS_DISPATCH(ctxWrapper, ProgramFragmentCreate,
  675. shaderText, shaderText_length,
  676. textureNames, textureNames_length_length, textureNames_length,
  677. params, params_length);
  678. }
  679. extern "C" RsProgramVertex rsProgramVertexCreate (RsContext ctxWrapper,
  680. const char * shaderText, size_t shaderText_length,
  681. const char ** textureNames, size_t textureNames_length_length,
  682. const size_t * textureNames_length,
  683. const uintptr_t * params, size_t params_length)
  684. {
  685. return RS_DISPATCH(ctxWrapper, ProgramVertexCreate,
  686. shaderText, shaderText_length,
  687. textureNames, textureNames_length_length, textureNames_length,
  688. params, params_length);
  689. }
  690. extern "C" RsFont rsFontCreateFromFile (RsContext ctxWrapper, const char * name, size_t name_length,
  691. float fontSize, uint32_t dpi)
  692. {
  693. return RS_DISPATCH(ctxWrapper, FontCreateFromFile, name, name_length, fontSize, dpi);
  694. }
  695. extern "C" RsFont rsFontCreateFromMemory (RsContext ctxWrapper, const char * name, size_t name_length,
  696. float fontSize, uint32_t dpi,
  697. const void * data, size_t data_length)
  698. {
  699. return RS_DISPATCH(ctxWrapper, FontCreateFromMemory, name, name_length, fontSize, dpi, data, data_length);
  700. }
  701. extern "C" RsMesh rsMeshCreate (RsContext ctxWrapper, RsAllocation * vtx, size_t vtx_length,
  702. RsAllocation * idx, size_t idx_length,
  703. uint32_t * primType, size_t primType_length)
  704. {
  705. return RS_DISPATCH(ctxWrapper, MeshCreate, vtx, vtx_length, idx, idx_length, primType, primType_length);
  706. }
  707. extern "C" void rsProgramBindConstants (RsContext ctxWrapper, RsProgram vp, uint32_t slot, RsAllocation constants)
  708. {
  709. RS_DISPATCH(ctxWrapper, ProgramBindConstants, vp, slot, constants);
  710. }
  711. extern "C" void rsProgramBindTexture (RsContext ctxWrapper, RsProgramFragment pf, uint32_t slot, RsAllocation a)
  712. {
  713. RS_DISPATCH(ctxWrapper, ProgramBindTexture, pf, slot,a);
  714. }
  715. extern "C" void rsProgramBindSampler (RsContext ctxWrapper, RsProgramFragment pf, uint32_t slot, RsSampler s)
  716. {
  717. RS_DISPATCH(ctxWrapper, ProgramBindSampler, pf, slot, s);
  718. }
  719. extern "C" RsObjectBase rsaFileA3DGetEntryByIndex(RsContext ctxWrapper, uint32_t index, RsFile file)
  720. {
  721. return RS_DISPATCH(ctxWrapper, FileA3DGetEntryByIndex, index, file);
  722. }
  723. extern "C" RsFile rsaFileA3DCreateFromMemory(RsContext ctxWrapper, const void *data, uint32_t len)
  724. {
  725. return RS_DISPATCH(ctxWrapper, FileA3DCreateFromMemory, data, len);
  726. }
  727. extern "C" RsFile rsaFileA3DCreateFromAsset(RsContext ctxWrapper, void *_asset)
  728. {
  729. return RS_DISPATCH(ctxWrapper, FileA3DCreateFromAsset, _asset);
  730. }
  731. extern "C" RsFile rsaFileA3DCreateFromFile(RsContext ctxWrapper, const char *path)
  732. {
  733. return RS_DISPATCH(ctxWrapper, FileA3DCreateFromFile, path);
  734. }
  735. extern "C" void rsaFileA3DGetNumIndexEntries(RsContext ctxWrapper, int32_t *numEntries, RsFile file)
  736. {
  737. RS_DISPATCH(ctxWrapper, FileA3DGetNumIndexEntries, numEntries, file);
  738. }
  739. extern "C" void rsaFileA3DGetIndexEntries(RsContext ctxWrapper, RsFileIndexEntry *fileEntries, uint32_t numEntries, RsFile file)
  740. {
  741. RS_DISPATCH(ctxWrapper, FileA3DGetIndexEntries, fileEntries, numEntries, file);
  742. }
  743. extern "C" void rsaMeshGetVertexBufferCount(RsContext ctxWrapper, RsMesh mv, int32_t *numVtx)
  744. {
  745. RS_DISPATCH(ctxWrapper, MeshGetVertexBufferCount, mv, numVtx);
  746. }
  747. extern "C" void rsaMeshGetIndexCount(RsContext ctxWrapper, RsMesh mv, int32_t *numIdx)
  748. {
  749. RS_DISPATCH(ctxWrapper, MeshGetIndexCount, mv, numIdx);
  750. }
  751. extern "C" void rsaMeshGetVertices(RsContext ctxWrapper, RsMesh mv, RsAllocation *vtxData, uint32_t vtxDataCount)
  752. {
  753. RS_DISPATCH(ctxWrapper, MeshGetVertices, mv, vtxData, vtxDataCount);
  754. }
  755. extern "C" void rsaMeshGetIndices(RsContext ctxWrapper, RsMesh mv, RsAllocation *va, uint32_t *primType, uint32_t idxDataCount)
  756. {
  757. RS_DISPATCH(ctxWrapper, MeshGetIndices, mv, va, primType, idxDataCount);
  758. }