rsElement.cpp 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436
  1. /*
  2. * Copyright (C) 2009 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 "rsContext.h"
  17. namespace android {
  18. namespace renderscript {
  19. Element::Element(Context *rsc) : ObjectBase(rsc) {
  20. mBits = 0;
  21. mBitsUnpadded = 0;
  22. mFields = nullptr;
  23. mFieldCount = 0;
  24. mHasReference = false;
  25. memset(&mHal, 0, sizeof(mHal));
  26. }
  27. Element::~Element() {
  28. clear();
  29. }
  30. void Element::operator delete(void* ptr) {
  31. if (ptr) {
  32. Element *e = (Element*) ptr;
  33. e->getContext()->mHal.funcs.freeRuntimeMem(ptr);
  34. }
  35. }
  36. void Element::preDestroy() const {
  37. auto& elements = mRSC->mStateElement.mElements;
  38. for (uint32_t ct = 0; ct < elements.size(); ct++) {
  39. if (elements[ct] == this) {
  40. elements.erase(elements.begin() + ct);
  41. break;
  42. }
  43. }
  44. }
  45. void Element::clear() {
  46. if (mFields) {
  47. for (size_t i = 0; i < mFieldCount; i++) {
  48. delete[] mFields[i].name;
  49. }
  50. delete [] mFields;
  51. }
  52. mFields = nullptr;
  53. mFieldCount = 0;
  54. mHasReference = false;
  55. delete [] mHal.state.fields;
  56. delete [] mHal.state.fieldArraySizes;
  57. delete [] mHal.state.fieldNames;
  58. delete [] mHal.state.fieldNameLengths;
  59. delete [] mHal.state.fieldOffsetBytes;
  60. }
  61. size_t Element::getSizeBits() const {
  62. if (!mFieldCount) {
  63. return mBits;
  64. }
  65. size_t total = 0;
  66. for (size_t ct=0; ct < mFieldCount; ct++) {
  67. total += mFields[ct].e->mBits * mFields[ct].arraySize;
  68. }
  69. return total;
  70. }
  71. size_t Element::getSizeBitsUnpadded() const {
  72. if (!mFieldCount) {
  73. return mBitsUnpadded;
  74. }
  75. size_t total = 0;
  76. for (size_t ct=0; ct < mFieldCount; ct++) {
  77. total += mFields[ct].e->mBitsUnpadded * mFields[ct].arraySize;
  78. }
  79. return total;
  80. }
  81. void Element::dumpLOGV(const char *prefix) const {
  82. ObjectBase::dumpLOGV(prefix);
  83. ALOGV("%s Element: fieldCount: %zu, size bytes: %zu", prefix, mFieldCount, getSizeBytes());
  84. mComponent.dumpLOGV(prefix);
  85. for (uint32_t ct = 0; ct < mFieldCount; ct++) {
  86. ALOGV("%s Element field index: %u ------------------", prefix, ct);
  87. ALOGV("%s name: %s, offsetBits: %u, arraySize: %u",
  88. prefix, mFields[ct].name, mFields[ct].offsetBits, mFields[ct].arraySize);
  89. mFields[ct].e->dumpLOGV(prefix);
  90. }
  91. }
  92. void Element::serialize(Context *rsc, OStream *stream) const {
  93. // Need to identify ourselves
  94. stream->addU32((uint32_t)getClassId());
  95. stream->addString(getName());
  96. mComponent.serialize(stream);
  97. // Now serialize all the fields
  98. stream->addU32(mFieldCount);
  99. for (uint32_t ct = 0; ct < mFieldCount; ct++) {
  100. stream->addString(mFields[ct].name);
  101. stream->addU32(mFields[ct].arraySize);
  102. mFields[ct].e->serialize(rsc, stream);
  103. }
  104. }
  105. Element *Element::createFromStream(Context *rsc, IStream *stream) {
  106. // First make sure we are reading the correct object
  107. RsA3DClassID classID = (RsA3DClassID)stream->loadU32();
  108. if (classID != RS_A3D_CLASS_ID_ELEMENT) {
  109. ALOGE("element loading skipped due to invalid class id\n");
  110. return nullptr;
  111. }
  112. const char *name = stream->loadString();
  113. Component component;
  114. component.loadFromStream(stream);
  115. uint32_t fieldCount = stream->loadU32();
  116. if (!fieldCount) {
  117. return (Element *)Element::create(rsc,
  118. component.getType(),
  119. component.getKind(),
  120. component.getIsNormalized(),
  121. component.getVectorSize());
  122. }
  123. const Element **subElems = new const Element *[fieldCount];
  124. const char **subElemNames = new const char *[fieldCount];
  125. size_t *subElemNamesLengths = new size_t[fieldCount];
  126. uint32_t *arraySizes = new uint32_t[fieldCount];
  127. for (uint32_t ct = 0; ct < fieldCount; ct ++) {
  128. subElemNames[ct] = stream->loadString();
  129. subElemNamesLengths[ct] = strlen(subElemNames[ct]);
  130. arraySizes[ct] = stream->loadU32();
  131. subElems[ct] = Element::createFromStream(rsc, stream);
  132. }
  133. const Element *elem = Element::create(rsc, fieldCount, subElems, subElemNames,
  134. subElemNamesLengths, arraySizes);
  135. for (uint32_t ct = 0; ct < fieldCount; ct ++) {
  136. delete [] subElemNames[ct];
  137. subElems[ct]->decUserRef();
  138. }
  139. delete[] name;
  140. delete[] subElems;
  141. delete[] subElemNames;
  142. delete[] subElemNamesLengths;
  143. delete[] arraySizes;
  144. return (Element *)elem;
  145. }
  146. void Element::compute() {
  147. mHal.state.dataType = mComponent.getType();
  148. mHal.state.dataKind = mComponent.getKind();
  149. mHal.state.vectorSize = mComponent.getVectorSize();
  150. if (mFieldCount == 0) {
  151. mBits = mComponent.getBits();
  152. mBitsUnpadded = mComponent.getBitsUnpadded();
  153. mHasReference = mComponent.isReference();
  154. mHal.state.elementSizeBytes = getSizeBytes();
  155. return;
  156. }
  157. uint32_t noPaddingFieldCount = 0;
  158. for (uint32_t ct = 0; ct < mFieldCount; ct ++) {
  159. if (mFields[ct].name[0] != '#') {
  160. noPaddingFieldCount ++;
  161. }
  162. }
  163. mHal.state.fields = new const Element*[noPaddingFieldCount];
  164. mHal.state.fieldArraySizes = new uint32_t[noPaddingFieldCount];
  165. mHal.state.fieldNames = new const char*[noPaddingFieldCount];
  166. mHal.state.fieldNameLengths = new uint32_t[noPaddingFieldCount];
  167. mHal.state.fieldOffsetBytes = new uint32_t[noPaddingFieldCount];
  168. mHal.state.fieldsCount = noPaddingFieldCount;
  169. size_t bits = 0;
  170. size_t bitsUnpadded = 0;
  171. for (size_t ct = 0, ctNoPadding = 0; ct < mFieldCount; ct++) {
  172. mFields[ct].offsetBits = bits;
  173. mFields[ct].offsetBitsUnpadded = bitsUnpadded;
  174. bits += mFields[ct].e->getSizeBits() * mFields[ct].arraySize;
  175. bitsUnpadded += mFields[ct].e->getSizeBitsUnpadded() * mFields[ct].arraySize;
  176. if (mFields[ct].e->mHasReference) {
  177. mHasReference = true;
  178. }
  179. if (mFields[ct].name[0] == '#') {
  180. continue;
  181. }
  182. mHal.state.fields[ctNoPadding] = mFields[ct].e.get();
  183. mHal.state.fieldArraySizes[ctNoPadding] = mFields[ct].arraySize;
  184. mHal.state.fieldNames[ctNoPadding] = mFields[ct].name;
  185. mHal.state.fieldNameLengths[ctNoPadding] = strlen(mFields[ct].name) + 1; // to include 0
  186. mHal.state.fieldOffsetBytes[ctNoPadding] = mFields[ct].offsetBits >> 3;
  187. ctNoPadding ++;
  188. }
  189. mBits = bits;
  190. mBitsUnpadded = bitsUnpadded;
  191. mHal.state.elementSizeBytes = getSizeBytes();
  192. }
  193. ObjectBaseRef<const Element> Element::createRef(Context *rsc, RsDataType dt, RsDataKind dk,
  194. bool isNorm, uint32_t vecSize) {
  195. ObjectBaseRef<const Element> returnRef;
  196. // Look for an existing match.
  197. ObjectBase::asyncLock();
  198. for (uint32_t ct=0; ct < rsc->mStateElement.mElements.size(); ct++) {
  199. const Element *ee = rsc->mStateElement.mElements[ct];
  200. if (!ee->getFieldCount() &&
  201. (ee->getComponent().getType() == dt) &&
  202. (ee->getComponent().getKind() == dk) &&
  203. (ee->getComponent().getIsNormalized() == isNorm) &&
  204. (ee->getComponent().getVectorSize() == vecSize)) {
  205. // Match
  206. returnRef.set(ee);
  207. ObjectBase::asyncUnlock();
  208. return ee;
  209. }
  210. }
  211. ObjectBase::asyncUnlock();
  212. // Element objects must use allocator specified by the driver
  213. void* allocMem = rsc->mHal.funcs.allocRuntimeMem(sizeof(Element), 0);
  214. if (!allocMem) {
  215. rsc->setError(RS_ERROR_FATAL_DRIVER, "Couldn't allocate memory for Element");
  216. return nullptr;
  217. }
  218. Element *e = new (allocMem) Element(rsc);
  219. returnRef.set(e);
  220. e->mComponent.set(dt, dk, isNorm, vecSize);
  221. e->compute();
  222. #ifdef RS_FIND_OFFSETS
  223. ALOGE("pointer for element: %p", e);
  224. ALOGE("pointer for element.drv: %p", &e->mHal.drv);
  225. #endif
  226. ObjectBase::asyncLock();
  227. rsc->mStateElement.mElements.push_back(e);
  228. ObjectBase::asyncUnlock();
  229. return returnRef;
  230. }
  231. ObjectBaseRef<const Element> Element::createRef(Context *rsc, size_t count, const Element **ein,
  232. const char **nin, const size_t * lengths, const uint32_t *asin) {
  233. ObjectBaseRef<const Element> returnRef;
  234. // Look for an existing match.
  235. ObjectBase::asyncLock();
  236. for (uint32_t ct=0; ct < rsc->mStateElement.mElements.size(); ct++) {
  237. const Element *ee = rsc->mStateElement.mElements[ct];
  238. if (ee->getFieldCount() == count) {
  239. bool match = true;
  240. for (uint32_t i=0; i < count; i++) {
  241. size_t len;
  242. uint32_t asize = 1;
  243. if (lengths) {
  244. len = lengths[i];
  245. } else {
  246. len = strlen(nin[i]);
  247. }
  248. if (asin) {
  249. asize = asin[i];
  250. }
  251. if ((ee->mFields[i].e.get() != ein[i]) ||
  252. (strlen(ee->mFields[i].name) != len) ||
  253. strcmp(ee->mFields[i].name, nin[i]) ||
  254. (ee->mFields[i].arraySize != asize)) {
  255. match = false;
  256. break;
  257. }
  258. }
  259. if (match) {
  260. returnRef.set(ee);
  261. ObjectBase::asyncUnlock();
  262. return returnRef;
  263. }
  264. }
  265. }
  266. ObjectBase::asyncUnlock();
  267. // Element objects must use allocator specified by the driver
  268. void* allocMem = rsc->mHal.funcs.allocRuntimeMem(sizeof(Element), 0);
  269. if (!allocMem) {
  270. rsc->setError(RS_ERROR_FATAL_DRIVER, "Couldn't allocate memory for Element");
  271. return nullptr;
  272. }
  273. Element *e = new (allocMem) Element(rsc);
  274. returnRef.set(e);
  275. e->mFields = new ElementField_t [count];
  276. e->mFieldCount = count;
  277. for (size_t ct=0; ct < count; ct++) {
  278. size_t len;
  279. uint32_t asize = 1;
  280. if (lengths) {
  281. len = lengths[ct];
  282. } else {
  283. len = strlen(nin[ct]);
  284. }
  285. if (asin) {
  286. asize = asin[ct];
  287. }
  288. e->mFields[ct].e.set(ein[ct]);
  289. e->mFields[ct].name = rsuCopyString(nin[ct], len);
  290. e->mFields[ct].arraySize = asize;
  291. }
  292. e->compute();
  293. ObjectBase::asyncLock();
  294. rsc->mStateElement.mElements.push_back(e);
  295. ObjectBase::asyncUnlock();
  296. return returnRef;
  297. }
  298. void Element::incRefs(const void *ptr) const {
  299. if (!mFieldCount) {
  300. if (mComponent.isReference()) {
  301. ObjectBase *const*obp = static_cast<ObjectBase *const*>(ptr);
  302. ObjectBase *ob = obp[0];
  303. if (ob) ob->incSysRef();
  304. }
  305. return;
  306. }
  307. const uint8_t *p = static_cast<const uint8_t *>(ptr);
  308. for (uint32_t i=0; i < mFieldCount; i++) {
  309. if (mFields[i].e->mHasReference) {
  310. const uint8_t *p2 = &p[mFields[i].offsetBits >> 3];
  311. for (uint32_t ct=0; ct < mFields[i].arraySize; ct++) {
  312. mFields[i].e->incRefs(p2);
  313. p2 += mFields[i].e->getSizeBytes();
  314. }
  315. }
  316. }
  317. }
  318. void Element::decRefs(const void *ptr) const {
  319. if (!mFieldCount) {
  320. if (mComponent.isReference()) {
  321. ObjectBase *const*obp = static_cast<ObjectBase *const*>(ptr);
  322. ObjectBase *ob = obp[0];
  323. if (ob) ob->decSysRef();
  324. }
  325. return;
  326. }
  327. const uint8_t *p = static_cast<const uint8_t *>(ptr);
  328. for (uint32_t i=0; i < mFieldCount; i++) {
  329. if (mFields[i].e->mHasReference) {
  330. const uint8_t *p2 = &p[mFields[i].offsetBits >> 3];
  331. for (uint32_t ct=0; ct < mFields[i].arraySize; ct++) {
  332. mFields[i].e->decRefs(p2);
  333. p2 += mFields[i].e->getSizeBytes();
  334. }
  335. }
  336. }
  337. }
  338. void Element::callUpdateCacheObject(const Context *rsc, void *dstObj) const {
  339. if (rsc->mHal.funcs.element.updateCachedObject != nullptr) {
  340. rsc->mHal.funcs.element.updateCachedObject(rsc, this, (rs_element *)dstObj);
  341. } else {
  342. *((const void **)dstObj) = this;
  343. }
  344. }
  345. ElementState::ElementState() {
  346. }
  347. ElementState::~ElementState() {
  348. rsAssert(!mElements.size());
  349. }
  350. /////////////////////////////////////////
  351. //
  352. RsElement rsi_ElementCreate(Context *rsc,
  353. RsDataType dt,
  354. RsDataKind dk,
  355. bool norm,
  356. uint32_t vecSize) {
  357. return (RsElement)Element::create(rsc, dt, dk, norm, vecSize);
  358. }
  359. RsElement rsi_ElementCreate2(Context *rsc,
  360. const RsElement * ein,
  361. size_t ein_length,
  362. const char ** names,
  363. size_t nameLengths_length,
  364. const size_t * nameLengths,
  365. const uint32_t * arraySizes,
  366. size_t arraySizes_length) {
  367. return (RsElement)Element::create(rsc, ein_length, (const Element **)ein,
  368. names, nameLengths, arraySizes);
  369. }
  370. } // namespace renderscript
  371. } // namespace android