rsComponent.cpp 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277
  1. /*
  2. * Copyright (C) 2013 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 "rsComponent.h"
  17. namespace android {
  18. namespace renderscript {
  19. Component::Component() {
  20. set(RS_TYPE_NONE, RS_KIND_USER, false, 1);
  21. }
  22. Component::~Component() {
  23. }
  24. void Component::set(RsDataType dt, RsDataKind dk, bool norm, uint32_t vecSize) {
  25. mType = dt;
  26. mKind = dk;
  27. mNormalized = norm;
  28. mVectorSize = vecSize;
  29. rsAssert(vecSize <= 4);
  30. mBits = 0;
  31. mTypeBits = 0;
  32. mIsFloat = false;
  33. mIsSigned = false;
  34. mIsPixel = false;
  35. switch (mKind) {
  36. case RS_KIND_PIXEL_L:
  37. case RS_KIND_PIXEL_A:
  38. mIsPixel = true;
  39. rsAssert(mVectorSize == 1);
  40. rsAssert(mNormalized == true);
  41. break;
  42. case RS_KIND_PIXEL_LA:
  43. mIsPixel = true;
  44. rsAssert(mVectorSize == 2);
  45. rsAssert(mNormalized == true);
  46. break;
  47. case RS_KIND_PIXEL_RGB:
  48. mIsPixel = true;
  49. rsAssert(mVectorSize == 3);
  50. rsAssert(mNormalized == true);
  51. break;
  52. case RS_KIND_PIXEL_RGBA:
  53. mIsPixel = true;
  54. rsAssert(mVectorSize == 4);
  55. rsAssert(mNormalized == true);
  56. break;
  57. case RS_KIND_PIXEL_YUV:
  58. mIsPixel = true;
  59. rsAssert(mVectorSize == 1);
  60. rsAssert(mNormalized == true);
  61. break;
  62. default:
  63. rsAssert(mKind != RS_KIND_INVALID);
  64. break;
  65. }
  66. switch (mType) {
  67. case RS_TYPE_NONE:
  68. return;
  69. case RS_TYPE_UNSIGNED_5_6_5:
  70. mVectorSize = 3;
  71. mBits = 16;
  72. mNormalized = true;
  73. rsAssert(mKind == RS_KIND_PIXEL_RGB);
  74. return;
  75. case RS_TYPE_UNSIGNED_5_5_5_1:
  76. mVectorSize = 4;
  77. mBits = 16;
  78. mNormalized = true;
  79. rsAssert(mKind == RS_KIND_PIXEL_RGBA);
  80. return;
  81. case RS_TYPE_UNSIGNED_4_4_4_4:
  82. mVectorSize = 4;
  83. mBits = 16;
  84. mNormalized = true;
  85. rsAssert(mKind == RS_KIND_PIXEL_RGBA);
  86. return;
  87. case RS_TYPE_MATRIX_4X4:
  88. mTypeBits = 16 * 32;
  89. rsAssert(mVectorSize == 1);
  90. rsAssert(mNormalized == false);
  91. rsAssert(mKind == RS_KIND_USER);
  92. break;
  93. case RS_TYPE_MATRIX_3X3:
  94. mTypeBits = 9 * 32;
  95. rsAssert(mVectorSize == 1);
  96. rsAssert(mNormalized == false);
  97. rsAssert(mKind == RS_KIND_USER);
  98. break;
  99. case RS_TYPE_MATRIX_2X2:
  100. mTypeBits = 4 * 32;
  101. rsAssert(mVectorSize == 1);
  102. rsAssert(mNormalized == false);
  103. rsAssert(mKind == RS_KIND_USER);
  104. break;
  105. case RS_TYPE_ELEMENT:
  106. case RS_TYPE_TYPE:
  107. case RS_TYPE_ALLOCATION:
  108. case RS_TYPE_SAMPLER:
  109. case RS_TYPE_SCRIPT:
  110. case RS_TYPE_MESH:
  111. case RS_TYPE_PROGRAM_FRAGMENT:
  112. case RS_TYPE_PROGRAM_VERTEX:
  113. case RS_TYPE_PROGRAM_RASTER:
  114. case RS_TYPE_PROGRAM_STORE:
  115. case RS_TYPE_FONT:
  116. rsAssert(mVectorSize == 1);
  117. rsAssert(mNormalized == false);
  118. rsAssert(mKind == RS_KIND_USER);
  119. #ifdef __LP64__
  120. // Large object pointers on 64-bit targets.
  121. mBits = 256;
  122. mTypeBits = 256;
  123. #else
  124. // Single pointer objects on 32-bit targets.
  125. mBits = 32;
  126. mTypeBits = 32;
  127. #endif // __LP64__
  128. return;
  129. case RS_TYPE_FLOAT_16:
  130. mTypeBits = 16;
  131. mIsFloat = true;
  132. break;
  133. case RS_TYPE_FLOAT_32:
  134. mTypeBits = 32;
  135. mIsFloat = true;
  136. break;
  137. case RS_TYPE_FLOAT_64:
  138. mTypeBits = 64;
  139. mIsFloat = true;
  140. break;
  141. case RS_TYPE_SIGNED_8:
  142. mTypeBits = 8;
  143. mIsSigned = true;
  144. break;
  145. case RS_TYPE_SIGNED_16:
  146. mTypeBits = 16;
  147. mIsSigned = true;
  148. break;
  149. case RS_TYPE_SIGNED_32:
  150. mTypeBits = 32;
  151. mIsSigned = true;
  152. break;
  153. case RS_TYPE_SIGNED_64:
  154. mTypeBits = 64;
  155. mIsSigned = true;
  156. break;
  157. case RS_TYPE_UNSIGNED_8:
  158. mTypeBits = 8;
  159. break;
  160. case RS_TYPE_UNSIGNED_16:
  161. mTypeBits = 16;
  162. break;
  163. case RS_TYPE_UNSIGNED_32:
  164. mTypeBits = 32;
  165. break;
  166. case RS_TYPE_UNSIGNED_64:
  167. mTypeBits = 64;
  168. break;
  169. case RS_TYPE_BOOLEAN:
  170. mTypeBits = 8;
  171. break;
  172. default:
  173. rsAssert(mType != RS_TYPE_INVALID);
  174. rsAssert(0);
  175. break;
  176. }
  177. mBitsUnpadded = mTypeBits * mVectorSize;
  178. mBits = mTypeBits * rsHigherPow2(mVectorSize);
  179. }
  180. bool Component::isReference() const {
  181. return (mType >= RS_TYPE_ELEMENT);
  182. }
  183. static const char * gTypeBasicStrings[] = {
  184. "NONE",
  185. "F16",
  186. "F32",
  187. "F64",
  188. "S8",
  189. "S16",
  190. "S32",
  191. "S64",
  192. "U8",
  193. "U16",
  194. "U32",
  195. "U64",
  196. "BOOLEAN",
  197. "UP_565",
  198. "UP_5551",
  199. "UP_4444",
  200. "MATRIX_4X4",
  201. "MATRIX_3X3",
  202. "MATRIX_2X2",
  203. };
  204. static const char * gTypeObjStrings[] = {
  205. "ELEMENT",
  206. "TYPE",
  207. "ALLOCATION",
  208. "SAMPLER",
  209. "SCRIPT",
  210. "MESH",
  211. "PROGRAM_FRAGMENT",
  212. "PROGRAM_VERTEX",
  213. "PROGRAM_RASTER",
  214. "PROGRAM_STORE",
  215. };
  216. static const char * gKindStrings[] = {
  217. "USER",
  218. "COLOR",
  219. "POSITION",
  220. "TEXTURE",
  221. "NORMAL",
  222. "INDEX",
  223. "POINT_SIZE",
  224. "PIXEL_L",
  225. "PIXEL_A",
  226. "PIXEL_LA",
  227. "PIXEL_RGB",
  228. "PIXEL_RGBA",
  229. };
  230. void Component::dumpLOGV(const char *prefix) const {
  231. if (mType >= RS_TYPE_ELEMENT) {
  232. ALOGV("%s Component: %s, %s, vectorSize=%i, bits=%i",
  233. prefix, gTypeObjStrings[mType - RS_TYPE_ELEMENT], gKindStrings[mKind], mVectorSize, mBits);
  234. } else {
  235. ALOGV("%s Component: %s, %s, vectorSize=%i, bits=%i",
  236. prefix, gTypeBasicStrings[mType], gKindStrings[mKind], mVectorSize, mBits);
  237. }
  238. }
  239. void Component::serialize(OStream *stream) const {
  240. stream->addU8((uint8_t)mType);
  241. stream->addU8((uint8_t)mKind);
  242. stream->addU8((uint8_t)(mNormalized ? 1 : 0));
  243. stream->addU32(mVectorSize);
  244. }
  245. void Component::loadFromStream(IStream *stream) {
  246. mType = (RsDataType)stream->loadU8();
  247. mKind = (RsDataKind)stream->loadU8();
  248. uint8_t temp = stream->loadU8();
  249. mNormalized = temp != 0;
  250. mVectorSize = stream->loadU32();
  251. set(mType, mKind, mNormalized, mVectorSize);
  252. }
  253. } // namespace renderscript
  254. } // namespace android