Element.cpp 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502
  1. /*
  2. * Copyright (C) 2012 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 <malloc.h>
  17. #include <string.h>
  18. #include "RenderScript.h"
  19. #include "rsCppInternal.h"
  20. using android::RSC::Element;
  21. android::RSC::sp<const Element> Element::getSubElement(uint32_t index) {
  22. if (!mVisibleElementMapSize) {
  23. mRS->throwError(RS_ERROR_INVALID_PARAMETER, "Element contains no sub-elements");
  24. return nullptr;
  25. }
  26. if (index >= mVisibleElementMapSize) {
  27. mRS->throwError(RS_ERROR_INVALID_PARAMETER, "Illegal sub-element index");
  28. return nullptr;
  29. }
  30. return mElements[mVisibleElementMap[index]];
  31. }
  32. const char * Element::getSubElementName(uint32_t index) {
  33. if (!mVisibleElementMapSize) {
  34. mRS->throwError(RS_ERROR_INVALID_PARAMETER, "Element contains no sub-elements");
  35. return nullptr;
  36. }
  37. if (index >= mVisibleElementMapSize) {
  38. mRS->throwError(RS_ERROR_INVALID_PARAMETER, "Illegal sub-element index");
  39. return nullptr;
  40. }
  41. return mElementNames[mVisibleElementMap[index]];
  42. }
  43. size_t Element::getSubElementArraySize(uint32_t index) {
  44. if (!mVisibleElementMapSize) {
  45. mRS->throwError(RS_ERROR_INVALID_PARAMETER, "Element contains no sub-elements");
  46. return 0;
  47. }
  48. if (index >= mVisibleElementMapSize) {
  49. mRS->throwError(RS_ERROR_INVALID_PARAMETER, "Illegal sub-element index");
  50. return 0;
  51. }
  52. return mArraySizes[mVisibleElementMap[index]];
  53. }
  54. uint32_t Element::getSubElementOffsetBytes(uint32_t index) {
  55. if (!mVisibleElementMapSize) {
  56. mRS->throwError(RS_ERROR_INVALID_PARAMETER, "Element contains no sub-elements");
  57. return 0;
  58. }
  59. if (index >= mVisibleElementMapSize) {
  60. mRS->throwError(RS_ERROR_INVALID_PARAMETER, "Illegal sub-element index");
  61. return 0;
  62. }
  63. return mOffsetInBytes[mVisibleElementMap[index]];
  64. }
  65. #define CREATE_USER(N, T) android::RSC::sp<const Element> Element::N(const android::RSC::sp<RS>& rs) { \
  66. if (rs->mElements.N == nullptr) { \
  67. rs->mElements.N = (createUser(rs, RS_TYPE_##T)); \
  68. } \
  69. return rs->mElements.N; \
  70. }
  71. CREATE_USER(BOOLEAN, BOOLEAN);
  72. CREATE_USER(U8, UNSIGNED_8);
  73. CREATE_USER(I8, SIGNED_8);
  74. CREATE_USER(U16, UNSIGNED_16);
  75. CREATE_USER(I16, SIGNED_16);
  76. CREATE_USER(U32, UNSIGNED_32);
  77. CREATE_USER(I32, SIGNED_32);
  78. CREATE_USER(U64, UNSIGNED_64);
  79. CREATE_USER(I64, SIGNED_64);
  80. CREATE_USER(F16, FLOAT_16);
  81. CREATE_USER(F32, FLOAT_32);
  82. CREATE_USER(F64, FLOAT_64);
  83. CREATE_USER(ELEMENT, ELEMENT);
  84. CREATE_USER(TYPE, TYPE);
  85. CREATE_USER(ALLOCATION, ALLOCATION);
  86. CREATE_USER(SAMPLER, SAMPLER);
  87. CREATE_USER(SCRIPT, SCRIPT);
  88. CREATE_USER(MATRIX_4X4, MATRIX_4X4);
  89. CREATE_USER(MATRIX_3X3, MATRIX_3X3);
  90. CREATE_USER(MATRIX_2X2, MATRIX_2X2);
  91. #define CREATE_PIXEL(N, T, K) android::RSC::sp<const Element> Element::N(const android::RSC::sp<RS> &rs) { \
  92. if (rs->mElements.N == nullptr) { \
  93. rs->mElements.N = createPixel(rs, RS_TYPE_##T, RS_KIND_##K); \
  94. } \
  95. return rs->mElements.N; \
  96. }
  97. CREATE_PIXEL(A_8, UNSIGNED_8, PIXEL_A);
  98. CREATE_PIXEL(RGB_565, UNSIGNED_5_6_5, PIXEL_RGB);
  99. CREATE_PIXEL(RGB_888, UNSIGNED_8, PIXEL_RGB);
  100. CREATE_PIXEL(RGBA_4444, UNSIGNED_4_4_4_4, PIXEL_RGBA);
  101. CREATE_PIXEL(RGBA_8888, UNSIGNED_8, PIXEL_RGBA);
  102. CREATE_PIXEL(YUV, UNSIGNED_8, PIXEL_YUV);
  103. CREATE_PIXEL(RGBA_5551, UNSIGNED_5_5_5_1, PIXEL_RGBA);
  104. #define CREATE_VECTOR(N, T) android::RSC::sp<const Element> Element::N##_2(const android::RSC::sp<RS> &rs) { \
  105. if (rs->mElements.N##_2 == nullptr) { \
  106. rs->mElements.N##_2 = createVector(rs, RS_TYPE_##T, 2); \
  107. } \
  108. return rs->mElements.N##_2; \
  109. } \
  110. android::RSC::sp<const Element> Element::N##_3(const android::RSC::sp<RS> &rs) { \
  111. if (rs->mElements.N##_3 == nullptr) { \
  112. rs->mElements.N##_3 = createVector(rs, RS_TYPE_##T, 3); \
  113. } \
  114. return rs->mElements.N##_3; \
  115. } \
  116. android::RSC::sp<const Element> Element::N##_4(const android::RSC::sp<RS> &rs) { \
  117. if (rs->mElements.N##_4 == nullptr) { \
  118. rs->mElements.N##_4 = createVector(rs, RS_TYPE_##T, 4); \
  119. } \
  120. return rs->mElements.N##_4; \
  121. }
  122. CREATE_VECTOR(U8, UNSIGNED_8);
  123. CREATE_VECTOR(I8, SIGNED_8);
  124. CREATE_VECTOR(U16, UNSIGNED_16);
  125. CREATE_VECTOR(I16, SIGNED_16);
  126. CREATE_VECTOR(U32, UNSIGNED_32);
  127. CREATE_VECTOR(I32, SIGNED_32);
  128. CREATE_VECTOR(U64, UNSIGNED_64);
  129. CREATE_VECTOR(I64, SIGNED_64);
  130. CREATE_VECTOR(F16, FLOAT_16);
  131. CREATE_VECTOR(F32, FLOAT_32);
  132. CREATE_VECTOR(F64, FLOAT_64);
  133. void Element::updateVisibleSubElements() {
  134. if (!mElementsCount) {
  135. return;
  136. }
  137. if (mVisibleElementMapSize) {
  138. free(mVisibleElementMap);
  139. mVisibleElementMapSize = 0;
  140. }
  141. mVisibleElementMap = (uint32_t*)calloc(mElementsCount, sizeof(uint32_t));
  142. int noPaddingFieldCount = 0;
  143. size_t fieldCount = mElementsCount;
  144. // Find out how many elements are not padding.
  145. for (size_t ct = 0; ct < fieldCount; ct ++) {
  146. if (mElementNames[ct][0] != '#') {
  147. noPaddingFieldCount ++;
  148. }
  149. }
  150. // Make a map that points us at non-padding elements.
  151. size_t i = 0;
  152. for (size_t ct = 0; ct < fieldCount; ct ++) {
  153. if (mElementNames[ct][0] != '#') {
  154. mVisibleElementMap[i++] = (uint32_t)ct;
  155. }
  156. }
  157. mVisibleElementMapSize = i;
  158. }
  159. Element::Element(void *id, android::RSC::sp<RS> rs,
  160. android::RSC::sp<const Element> * elements,
  161. size_t elementCount,
  162. const char ** elementNames,
  163. size_t * elementNameLengths,
  164. uint32_t * arraySizes) : BaseObj(id, rs) {
  165. mSizeBytes = 0;
  166. mVectorSize = 1;
  167. mElementsCount = elementCount;
  168. mVisibleElementMap = nullptr;
  169. mVisibleElementMapSize = 0;
  170. mElements = (android::RSC::sp<const Element> *)calloc(mElementsCount, sizeof(android::RSC::sp<const Element>));
  171. mElementNames = (char **)calloc(mElementsCount, sizeof(char *));
  172. mElementNameLengths = (size_t*)calloc(mElementsCount, sizeof(size_t));
  173. mArraySizes = (uint32_t*)calloc(mElementsCount, sizeof(uint32_t));
  174. mOffsetInBytes = (uint32_t*)calloc(mElementsCount, sizeof(uint32_t));
  175. memcpy(mElements, elements, mElementsCount * sizeof(android::RSC::sp<Element>));
  176. memcpy(mArraySizes, arraySizes, mElementsCount * sizeof(uint32_t));
  177. // Copy strings (char array).
  178. memcpy(mElementNameLengths, elementNameLengths, mElementsCount * sizeof(size_t));
  179. for (size_t ct = 0; ct < mElementsCount; ct++ ) {
  180. size_t elemNameLen = mElementNameLengths[ct];
  181. mElementNames[ct] = (char *)calloc(elemNameLen, sizeof(char));
  182. memcpy(mElementNames[ct], elementNames[ct], elemNameLen);
  183. }
  184. mType = RS_TYPE_NONE;
  185. mKind = RS_KIND_USER;
  186. for (size_t ct = 0; ct < mElementsCount; ct++ ) {
  187. mOffsetInBytes[ct] = mSizeBytes;
  188. mSizeBytes += mElements[ct]->mSizeBytes * mArraySizes[ct];
  189. }
  190. updateVisibleSubElements();
  191. }
  192. Element::Element(void *id, android::RSC::sp<RS> rs) :
  193. BaseObj(id, rs) {
  194. }
  195. static uint32_t GetSizeInBytesForType(RsDataType dt) {
  196. switch(dt) {
  197. case RS_TYPE_NONE:
  198. return 0;
  199. case RS_TYPE_SIGNED_8:
  200. case RS_TYPE_UNSIGNED_8:
  201. case RS_TYPE_BOOLEAN:
  202. return 1;
  203. case RS_TYPE_FLOAT_16:
  204. case RS_TYPE_SIGNED_16:
  205. case RS_TYPE_UNSIGNED_16:
  206. case RS_TYPE_UNSIGNED_5_6_5:
  207. case RS_TYPE_UNSIGNED_5_5_5_1:
  208. case RS_TYPE_UNSIGNED_4_4_4_4:
  209. return 2;
  210. case RS_TYPE_FLOAT_32:
  211. case RS_TYPE_SIGNED_32:
  212. case RS_TYPE_UNSIGNED_32:
  213. return 4;
  214. case RS_TYPE_FLOAT_64:
  215. case RS_TYPE_SIGNED_64:
  216. case RS_TYPE_UNSIGNED_64:
  217. return 8;
  218. case RS_TYPE_MATRIX_4X4:
  219. return 16 * 4;
  220. case RS_TYPE_MATRIX_3X3:
  221. return 9 * 4;
  222. case RS_TYPE_MATRIX_2X2:
  223. return 4 * 4;
  224. case RS_TYPE_TYPE:
  225. case RS_TYPE_ALLOCATION:
  226. case RS_TYPE_SAMPLER:
  227. case RS_TYPE_SCRIPT:
  228. case RS_TYPE_MESH:
  229. case RS_TYPE_PROGRAM_FRAGMENT:
  230. case RS_TYPE_PROGRAM_VERTEX:
  231. case RS_TYPE_PROGRAM_RASTER:
  232. case RS_TYPE_PROGRAM_STORE:
  233. return 4;
  234. default:
  235. break;
  236. }
  237. ALOGE("Missing type %i", dt);
  238. return 0;
  239. }
  240. Element::Element(void *id, android::RSC::sp<RS> rs,
  241. RsDataType dt, RsDataKind dk, bool norm, uint32_t size) :
  242. BaseObj(id, rs)
  243. {
  244. uint32_t tsize = GetSizeInBytesForType(dt);
  245. if ((dt != RS_TYPE_UNSIGNED_5_6_5) &&
  246. (dt != RS_TYPE_UNSIGNED_4_4_4_4) &&
  247. (dt != RS_TYPE_UNSIGNED_5_5_5_1)) {
  248. if (size == 3) {
  249. mSizeBytes = tsize * 4;
  250. } else {
  251. mSizeBytes = tsize * size;
  252. }
  253. } else {
  254. mSizeBytes = tsize;
  255. }
  256. mType = dt;
  257. mKind = dk;
  258. mNormalized = norm;
  259. mVectorSize = size;
  260. mElementsCount = 0;
  261. mVisibleElementMap = 0;
  262. }
  263. Element::~Element() {
  264. if (mElementsCount) {
  265. free(mElements);
  266. for (size_t ct = 0; ct < mElementsCount; ct++ ) {
  267. free(mElementNames[ct]);
  268. }
  269. free(mElementNames);
  270. free(mElementNameLengths);
  271. free(mArraySizes);
  272. free(mOffsetInBytes);
  273. }
  274. if (mVisibleElementMapSize) {
  275. free(mVisibleElementMap);
  276. }
  277. }
  278. void Element::updateFromNative() {
  279. BaseObj::updateFromNative();
  280. updateVisibleSubElements();
  281. }
  282. android::RSC::sp<const Element> Element::createUser(const android::RSC::sp<RS>& rs, RsDataType dt) {
  283. void * id = RS::dispatch->ElementCreate(rs->getContext(), dt, RS_KIND_USER, false, 1);
  284. return new Element(id, rs, dt, RS_KIND_USER, false, 1);
  285. }
  286. android::RSC::sp<const Element> Element::createVector(const android::RSC::sp<RS>& rs, RsDataType dt, uint32_t size) {
  287. if (size < 2 || size > 4) {
  288. rs->throwError(RS_ERROR_INVALID_PARAMETER, "Vector size out of range 2-4.");
  289. return nullptr;
  290. }
  291. void *id = RS::dispatch->ElementCreate(rs->getContext(), dt, RS_KIND_USER, false, size);
  292. return new Element(id, rs, dt, RS_KIND_USER, false, size);
  293. }
  294. android::RSC::sp<const Element> Element::createPixel(const android::RSC::sp<RS>& rs, RsDataType dt, RsDataKind dk) {
  295. if (!(dk == RS_KIND_PIXEL_L ||
  296. dk == RS_KIND_PIXEL_A ||
  297. dk == RS_KIND_PIXEL_LA ||
  298. dk == RS_KIND_PIXEL_RGB ||
  299. dk == RS_KIND_PIXEL_RGBA ||
  300. dk == RS_KIND_PIXEL_DEPTH ||
  301. dk == RS_KIND_PIXEL_YUV)) {
  302. rs->throwError(RS_ERROR_INVALID_PARAMETER, "Unsupported DataKind");
  303. return nullptr;
  304. }
  305. if (!(dt == RS_TYPE_UNSIGNED_8 ||
  306. dt == RS_TYPE_UNSIGNED_16 ||
  307. dt == RS_TYPE_UNSIGNED_5_6_5 ||
  308. dt == RS_TYPE_UNSIGNED_4_4_4_4 ||
  309. dt == RS_TYPE_UNSIGNED_5_5_5_1)) {
  310. rs->throwError(RS_ERROR_INVALID_PARAMETER, "Unsupported DataType");
  311. return nullptr;
  312. }
  313. if (dt == RS_TYPE_UNSIGNED_5_6_5 && dk != RS_KIND_PIXEL_RGB) {
  314. rs->throwError(RS_ERROR_INVALID_PARAMETER, "Bad kind and type combo");
  315. return nullptr;
  316. }
  317. if (dt == RS_TYPE_UNSIGNED_5_5_5_1 && dk != RS_KIND_PIXEL_RGBA) {
  318. rs->throwError(RS_ERROR_INVALID_PARAMETER, "Bad kind and type combo");
  319. return nullptr;
  320. }
  321. if (dt == RS_TYPE_UNSIGNED_4_4_4_4 && dk != RS_KIND_PIXEL_RGBA) {
  322. rs->throwError(RS_ERROR_INVALID_PARAMETER, "Bad kind and type combo");
  323. return nullptr;
  324. }
  325. if (dt == RS_TYPE_UNSIGNED_16 && dk != RS_KIND_PIXEL_DEPTH) {
  326. rs->throwError(RS_ERROR_INVALID_PARAMETER, "Bad kind and type combo");
  327. return nullptr;
  328. }
  329. int size = 1;
  330. switch (dk) {
  331. case RS_KIND_PIXEL_LA:
  332. size = 2;
  333. break;
  334. case RS_KIND_PIXEL_RGB:
  335. size = 3;
  336. break;
  337. case RS_KIND_PIXEL_RGBA:
  338. size = 4;
  339. break;
  340. case RS_KIND_PIXEL_DEPTH:
  341. size = 2;
  342. break;
  343. default:
  344. break;
  345. }
  346. void * id = RS::dispatch->ElementCreate(rs->getContext(), dt, dk, true, size);
  347. return new Element(id, rs, dt, dk, true, size);
  348. }
  349. bool Element::isCompatible(const android::RSC::sp<const Element>&e) const {
  350. // Try strict BaseObj equality to start with.
  351. if (this == e.get()) {
  352. return true;
  353. }
  354. /*
  355. * Ignore mKind because it is allowed to be different (user vs. pixel).
  356. * We also ignore mNormalized because it can be different. The mType
  357. * field must be non-null since we require name equivalence for
  358. * user-created Elements.
  359. */
  360. return ((mSizeBytes == e->mSizeBytes) &&
  361. (mType != RS_TYPE_NONE) &&
  362. (mType == e->mType) &&
  363. (mVectorSize == e->mVectorSize));
  364. }
  365. Element::Builder::Builder(android::RSC::sp<RS> rs) {
  366. mRS = rs.get();
  367. mSkipPadding = false;
  368. mElementsVecSize = 8;
  369. mElementsCount = 0;
  370. // Initialize space.
  371. mElements = (android::RSC::sp<const Element> *)calloc(mElementsVecSize, sizeof(android::RSC::sp<const Element>));
  372. mElementNames = (char **)calloc(mElementsVecSize, sizeof(char *));
  373. mElementNameLengths = (size_t*)calloc(mElementsVecSize, sizeof(size_t));
  374. mArraySizes = (uint32_t*)calloc(mElementsVecSize, sizeof(uint32_t));
  375. }
  376. Element::Builder::~Builder() {
  377. // Free allocated space.
  378. free(mElements);
  379. for (size_t ct = 0; ct < mElementsCount; ct++ ) {
  380. free(mElementNames[ct]);
  381. }
  382. free(mElementNameLengths);
  383. free(mElementNames);
  384. free(mArraySizes);
  385. }
  386. void Element::Builder::add(const android::RSC::sp<const Element>&e, const char * name, uint32_t arraySize) {
  387. // Skip padding fields after a vector 3 type.
  388. if (mSkipPadding) {
  389. const char *s1 = "#padding_";
  390. const char *s2 = name;
  391. size_t len = strlen(s1);
  392. if (strlen(s2) >= len) {
  393. if (!memcmp(s1, s2, len)) {
  394. mSkipPadding = false;
  395. return;
  396. }
  397. }
  398. }
  399. if (e->mVectorSize == 3) {
  400. mSkipPadding = true;
  401. } else {
  402. mSkipPadding = false;
  403. }
  404. if (mElementsCount >= mElementsVecSize) {
  405. // If pre-allocated space is full, allocate a larger one.
  406. mElementsVecSize += 8;
  407. android::RSC::sp<const Element> * newElements = (android::RSC::sp<const Element> *)calloc(mElementsVecSize, sizeof(android::RSC::sp<const Element>));
  408. char ** newElementNames = (char **)calloc(mElementsVecSize, sizeof(char *));
  409. size_t * newElementNameLengths = (size_t*)calloc(mElementsVecSize, sizeof(size_t));
  410. uint32_t * newArraySizes = (uint32_t*)calloc(mElementsVecSize, sizeof(uint32_t));
  411. memcpy(newElements, mElements, mElementsCount * sizeof(android::RSC::sp<Element>));
  412. memcpy(newElementNames, mElementNames, mElementsCount * sizeof(char *));
  413. memcpy(newElementNameLengths, mElementNameLengths, mElementsCount * sizeof(size_t));
  414. memcpy(newArraySizes, mArraySizes, mElementsCount * sizeof(uint32_t));
  415. // Free the old arrays.
  416. free(mElements);
  417. free(mElementNames);
  418. free(mArraySizes);
  419. free(mElementNameLengths);
  420. mElements = newElements;
  421. mElementNames = newElementNames;
  422. mArraySizes = newArraySizes;
  423. mElementNameLengths = newElementNameLengths;
  424. }
  425. mElements[mElementsCount] = e;
  426. mArraySizes[mElementsCount] = arraySize;
  427. size_t nameLen = strlen(name);
  428. mElementNameLengths[mElementsCount] = nameLen + 1;
  429. mElementNames[mElementsCount] = (char *)calloc(nameLen + 1, sizeof(char));
  430. memcpy(mElementNames[mElementsCount], name, nameLen);
  431. mElementNames[mElementsCount][nameLen] = 0;
  432. mElementsCount++;
  433. }
  434. android::RSC::sp<const Element> Element::Builder::create() {
  435. size_t fieldCount = mElementsCount;
  436. void ** elementArray = (void **)calloc(fieldCount, sizeof(void *));
  437. for (size_t ct = 0; ct < fieldCount; ct++) {
  438. elementArray[ct] = mElements[ct]->getID();
  439. }
  440. void *id = RS::dispatch->ElementCreate2(mRS->getContext(),
  441. (RsElement *)elementArray, fieldCount,
  442. (const char **)mElementNames, fieldCount, mElementNameLengths,
  443. mArraySizes, fieldCount);
  444. free(elementArray);
  445. return new Element(id, mRS, mElements, mElementsCount, (const char **)mElementNames, mElementNameLengths, mArraySizes);
  446. }