/rs/java/android/renderscript/Type.java

https://github.com/aizuzi/platform_frameworks_base · Java · 433 lines · 238 code · 47 blank · 148 comment · 42 complexity · 6dbd6f2ba9de4aaafb25277a4c9909ef MD5 · raw file

  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. package android.renderscript;
  17. import java.lang.reflect.Field;
  18. import android.graphics.ImageFormat;
  19. import android.util.Log;
  20. /**
  21. * <p>A Type describes the {@link android.renderscript.Element} and dimensions used for an {@link
  22. * android.renderscript.Allocation} or a parallel operation. Types are created through {@link
  23. * android.renderscript.Type.Builder}.</p>
  24. *
  25. * <p>A Type always includes an {@link android.renderscript.Element} and an X
  26. * dimension. A Type may be multidimensional, up to three dimensions. A nonzero
  27. * value in the Y or Z dimensions indicates that the dimension is present. Note
  28. * that a Type with only a given X dimension and a Type with the same X
  29. * dimension but Y = 1 are not equivalent.</p>
  30. *
  31. * <p>A Type also supports inclusion of level of detail (LOD) or cube map
  32. * faces. LOD and cube map faces are booleans to indicate present or not
  33. * present. </p>
  34. *
  35. * <p>A Type also supports YUV format information to support an
  36. * {@link android.renderscript.Allocation} in a YUV format. The YUV formats
  37. * supported are {@link android.graphics.ImageFormat#YV12},
  38. * {@link android.graphics.ImageFormat#NV21}, and
  39. * {@link android.graphics.ImageFormat#YUV_420_888}</p>
  40. *
  41. * <div class="special reference">
  42. * <h3>Developer Guides</h3>
  43. * <p>For more information about creating an application that uses RenderScript, read the
  44. * <a href="{@docRoot}guide/topics/renderscript/index.html">RenderScript</a> developer guide.</p>
  45. * </div>
  46. **/
  47. public class Type extends BaseObj {
  48. int mDimX;
  49. int mDimY;
  50. int mDimZ;
  51. boolean mDimMipmaps;
  52. boolean mDimFaces;
  53. int mDimYuv;
  54. int mElementCount;
  55. Element mElement;
  56. public enum CubemapFace {
  57. POSITIVE_X (0),
  58. NEGATIVE_X (1),
  59. POSITIVE_Y (2),
  60. NEGATIVE_Y (3),
  61. POSITIVE_Z (4),
  62. NEGATIVE_Z (5),
  63. @Deprecated
  64. POSITVE_X (0),
  65. @Deprecated
  66. POSITVE_Y (2),
  67. @Deprecated
  68. POSITVE_Z (4);
  69. int mID;
  70. CubemapFace(int id) {
  71. mID = id;
  72. }
  73. }
  74. /**
  75. * Return the element associated with this Type.
  76. *
  77. * @return Element
  78. */
  79. public Element getElement() {
  80. return mElement;
  81. }
  82. /**
  83. * Return the value of the X dimension.
  84. *
  85. * @return int
  86. */
  87. public int getX() {
  88. return mDimX;
  89. }
  90. /**
  91. * Return the value of the Y dimension or 0 for a 1D allocation.
  92. *
  93. * @return int
  94. */
  95. public int getY() {
  96. return mDimY;
  97. }
  98. /**
  99. * Return the value of the Z dimension or 0 for a 1D or 2D allocation.
  100. *
  101. * @return int
  102. */
  103. public int getZ() {
  104. return mDimZ;
  105. }
  106. /**
  107. * Get the YUV format
  108. *
  109. *
  110. * @return int
  111. */
  112. public int getYuv() {
  113. return mDimYuv;
  114. }
  115. /**
  116. * Return if the Type has a mipmap chain.
  117. *
  118. * @return boolean
  119. */
  120. public boolean hasMipmaps() {
  121. return mDimMipmaps;
  122. }
  123. /**
  124. * Return if the Type is a cube map.
  125. *
  126. * @return boolean
  127. */
  128. public boolean hasFaces() {
  129. return mDimFaces;
  130. }
  131. /**
  132. * Return the total number of accessable cells in the Type.
  133. *
  134. * @return int
  135. */
  136. public int getCount() {
  137. return mElementCount;
  138. }
  139. void calcElementCount() {
  140. boolean hasLod = hasMipmaps();
  141. int x = getX();
  142. int y = getY();
  143. int z = getZ();
  144. int faces = 1;
  145. if (hasFaces()) {
  146. faces = 6;
  147. }
  148. if (x == 0) {
  149. x = 1;
  150. }
  151. if (y == 0) {
  152. y = 1;
  153. }
  154. if (z == 0) {
  155. z = 1;
  156. }
  157. int count = x * y * z * faces;
  158. while (hasLod && ((x > 1) || (y > 1) || (z > 1))) {
  159. if(x > 1) {
  160. x >>= 1;
  161. }
  162. if(y > 1) {
  163. y >>= 1;
  164. }
  165. if(z > 1) {
  166. z >>= 1;
  167. }
  168. count += x * y * z * faces;
  169. }
  170. mElementCount = count;
  171. }
  172. Type(long id, RenderScript rs) {
  173. super(id, rs);
  174. }
  175. @Override
  176. void updateFromNative() {
  177. // We have 6 integer/long to obtain mDimX; mDimY; mDimZ;
  178. // mDimLOD; mDimFaces; mElement;
  179. long[] dataBuffer = new long[6];
  180. mRS.nTypeGetNativeData(getID(mRS), dataBuffer);
  181. mDimX = (int)dataBuffer[0];
  182. mDimY = (int)dataBuffer[1];
  183. mDimZ = (int)dataBuffer[2];
  184. mDimMipmaps = dataBuffer[3] == 1 ? true : false;
  185. mDimFaces = dataBuffer[4] == 1 ? true : false;
  186. long elementID = dataBuffer[5];
  187. if(elementID != 0) {
  188. mElement = new Element(elementID, mRS);
  189. mElement.updateFromNative();
  190. }
  191. calcElementCount();
  192. }
  193. /**
  194. * @hide
  195. * Utility function for creating basic 1D types. The type is
  196. * created without mipmaps enabled.
  197. *
  198. * @param rs The RenderScript context
  199. * @param e The Element for the Type
  200. * @param dimX The X dimension, must be > 0
  201. *
  202. * @return Type
  203. */
  204. static public Type createX(RenderScript rs, Element e, int dimX) {
  205. if (dimX < 1) {
  206. throw new RSInvalidStateException("Dimension must be >= 1.");
  207. }
  208. long id = rs.nTypeCreate(e.getID(rs), dimX, 0, 0, false, false, 0);
  209. Type t = new Type(id, rs);
  210. t.mElement = e;
  211. t.mDimX = dimX;
  212. t.calcElementCount();
  213. return t;
  214. }
  215. /**
  216. * @hide
  217. * Utility function for creating basic 2D types. The type is
  218. * created without mipmaps or cubemaps.
  219. *
  220. * @param rs The RenderScript context
  221. * @param e The Element for the Type
  222. * @param dimX The X dimension, must be > 0
  223. * @param dimY The Y dimension, must be > 0
  224. *
  225. * @return Type
  226. */
  227. static public Type createXY(RenderScript rs, Element e, int dimX, int dimY) {
  228. if ((dimX < 1) || (dimY < 1)) {
  229. throw new RSInvalidStateException("Dimension must be >= 1.");
  230. }
  231. long id = rs.nTypeCreate(e.getID(rs), dimX, dimY, 0, false, false, 0);
  232. Type t = new Type(id, rs);
  233. t.mElement = e;
  234. t.mDimX = dimX;
  235. t.mDimY = dimY;
  236. t.calcElementCount();
  237. return t;
  238. }
  239. /**
  240. * @hide
  241. * Utility function for creating basic 3D types. The type is
  242. * created without mipmaps.
  243. *
  244. * @param rs The RenderScript context
  245. * @param e The Element for the Type
  246. * @param dimX The X dimension, must be > 0
  247. * @param dimY The Y dimension, must be > 0
  248. * @param dimZ The Z dimension, must be > 0
  249. *
  250. * @return Type
  251. */
  252. static public Type createXYZ(RenderScript rs, Element e, int dimX, int dimY, int dimZ) {
  253. if ((dimX < 1) || (dimY < 1) || (dimZ < 1)) {
  254. throw new RSInvalidStateException("Dimension must be >= 1.");
  255. }
  256. long id = rs.nTypeCreate(e.getID(rs), dimX, dimY, dimZ, false, false, 0);
  257. Type t = new Type(id, rs);
  258. t.mElement = e;
  259. t.mDimX = dimX;
  260. t.mDimY = dimY;
  261. t.mDimZ = dimZ;
  262. t.calcElementCount();
  263. return t;
  264. }
  265. /**
  266. * Builder class for Type.
  267. *
  268. */
  269. public static class Builder {
  270. RenderScript mRS;
  271. int mDimX = 1;
  272. int mDimY;
  273. int mDimZ;
  274. boolean mDimMipmaps;
  275. boolean mDimFaces;
  276. int mYuv;
  277. Element mElement;
  278. /**
  279. * Create a new builder object.
  280. *
  281. * @param rs
  282. * @param e The element for the type to be created.
  283. */
  284. public Builder(RenderScript rs, Element e) {
  285. e.checkValid();
  286. mRS = rs;
  287. mElement = e;
  288. }
  289. /**
  290. * Add a dimension to the Type.
  291. *
  292. *
  293. * @param value
  294. */
  295. public Builder setX(int value) {
  296. if(value < 1) {
  297. throw new RSIllegalArgumentException("Values of less than 1 for Dimension X are not valid.");
  298. }
  299. mDimX = value;
  300. return this;
  301. }
  302. public Builder setY(int value) {
  303. if(value < 1) {
  304. throw new RSIllegalArgumentException("Values of less than 1 for Dimension Y are not valid.");
  305. }
  306. mDimY = value;
  307. return this;
  308. }
  309. public Builder setZ(int value) {
  310. if(value < 1) {
  311. throw new RSIllegalArgumentException("Values of less than 1 for Dimension Z are not valid.");
  312. }
  313. mDimZ = value;
  314. return this;
  315. }
  316. public Builder setMipmaps(boolean value) {
  317. mDimMipmaps = value;
  318. return this;
  319. }
  320. public Builder setFaces(boolean value) {
  321. mDimFaces = value;
  322. return this;
  323. }
  324. /**
  325. * Set the YUV layout for a Type.
  326. *
  327. * @param yuvFormat {@link android.graphics.ImageFormat#YV12}, {@link android.graphics.ImageFormat#NV21}, or
  328. * {@link android.graphics.ImageFormat#YUV_420_888}.
  329. */
  330. public Builder setYuvFormat(int yuvFormat) {
  331. switch (yuvFormat) {
  332. case android.graphics.ImageFormat.NV21:
  333. case android.graphics.ImageFormat.YV12:
  334. case android.graphics.ImageFormat.YUV_420_888:
  335. break;
  336. default:
  337. throw new RSIllegalArgumentException(
  338. "Only ImageFormat.NV21, .YV12, and .YUV_420_888 are supported..");
  339. }
  340. mYuv = yuvFormat;
  341. return this;
  342. }
  343. /**
  344. * Validate structure and create a new Type.
  345. *
  346. * @return Type
  347. */
  348. public Type create() {
  349. if (mDimZ > 0) {
  350. if ((mDimX < 1) || (mDimY < 1)) {
  351. throw new RSInvalidStateException("Both X and Y dimension required when Z is present.");
  352. }
  353. if (mDimFaces) {
  354. throw new RSInvalidStateException("Cube maps not supported with 3D types.");
  355. }
  356. }
  357. if (mDimY > 0) {
  358. if (mDimX < 1) {
  359. throw new RSInvalidStateException("X dimension required when Y is present.");
  360. }
  361. }
  362. if (mDimFaces) {
  363. if (mDimY < 1) {
  364. throw new RSInvalidStateException("Cube maps require 2D Types.");
  365. }
  366. }
  367. if (mYuv != 0) {
  368. if ((mDimZ != 0) || mDimFaces || mDimMipmaps) {
  369. throw new RSInvalidStateException("YUV only supports basic 2D.");
  370. }
  371. }
  372. long id = mRS.nTypeCreate(mElement.getID(mRS),
  373. mDimX, mDimY, mDimZ, mDimMipmaps, mDimFaces, mYuv);
  374. Type t = new Type(id, mRS);
  375. t.mElement = mElement;
  376. t.mDimX = mDimX;
  377. t.mDimY = mDimY;
  378. t.mDimZ = mDimZ;
  379. t.mDimMipmaps = mDimMipmaps;
  380. t.mDimFaces = mDimFaces;
  381. t.mDimYuv = mYuv;
  382. t.calcElementCount();
  383. return t;
  384. }
  385. }
  386. }