/rs/java/android/renderscript/FileA3D.java

https://github.com/aizuzi/platform_frameworks_base · Java · 314 lines · 147 code · 37 blank · 130 comment · 23 complexity · f133f922415b5c4baa8237ebbde4a3f2 MD5 · raw file

  1. /*
  2. * Copyright (C) 2008-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. package android.renderscript;
  17. import java.io.File;
  18. import java.io.IOException;
  19. import java.io.InputStream;
  20. import android.content.res.AssetManager;
  21. import android.content.res.Resources;
  22. import android.graphics.Bitmap;
  23. import android.graphics.BitmapFactory;
  24. import android.util.Log;
  25. import android.util.TypedValue;
  26. /**
  27. * @hide
  28. * @deprecated in API 16
  29. * FileA3D allows users to load RenderScript objects from files
  30. * or resources stored on disk. It could be used to load items
  31. * such as 3D geometry data converted to a RenderScript format from
  32. * content creation tools. Currently only meshes are supported
  33. * in FileA3D.
  34. *
  35. * When successfully loaded, FileA3D will contain a list of
  36. * index entries for all the objects stored inside it.
  37. *
  38. **/
  39. public class FileA3D extends BaseObj {
  40. /**
  41. * @deprecated in API 16
  42. * Specifies what renderscript object type is contained within
  43. * the FileA3D IndexEntry
  44. **/
  45. public enum EntryType {
  46. /**
  47. * @deprecated in API 16
  48. * Unknown or or invalid object, nothing will be loaded
  49. **/
  50. UNKNOWN (0),
  51. /**
  52. * @deprecated in API 16
  53. * RenderScript Mesh object
  54. **/
  55. MESH (1);
  56. int mID;
  57. EntryType(int id) {
  58. mID = id;
  59. }
  60. static EntryType toEntryType(int intID) {
  61. return EntryType.values()[intID];
  62. }
  63. }
  64. /**
  65. * @deprecated in API 16
  66. * IndexEntry contains information about one of the RenderScript
  67. * objects inside the file's index. It could be used to query the
  68. * object's type and also name and load the object itself if
  69. * necessary.
  70. */
  71. public static class IndexEntry {
  72. RenderScript mRS;
  73. int mIndex;
  74. long mID;
  75. String mName;
  76. EntryType mEntryType;
  77. BaseObj mLoadedObj;
  78. /**
  79. * @deprecated in API 16
  80. * Returns the name of a renderscript object the index entry
  81. * describes
  82. *
  83. * @return name of a renderscript object the index entry
  84. * describes
  85. *
  86. */
  87. public String getName() {
  88. return mName;
  89. }
  90. /**
  91. * @deprecated in API 16
  92. * Returns the type of a renderscript object the index entry
  93. * describes
  94. * @return type of a renderscript object the index entry
  95. * describes
  96. */
  97. public EntryType getEntryType() {
  98. return mEntryType;
  99. }
  100. /**
  101. * @deprecated in API 16
  102. * Used to load the object described by the index entry
  103. * @return base renderscript object described by the entry
  104. */
  105. public BaseObj getObject() {
  106. mRS.validate();
  107. BaseObj obj = internalCreate(mRS, this);
  108. return obj;
  109. }
  110. /**
  111. * @deprecated in API 16
  112. * Used to load the mesh described by the index entry, object
  113. * described by the index entry must be a renderscript mesh
  114. *
  115. * @return renderscript mesh object described by the entry
  116. */
  117. public Mesh getMesh() {
  118. return (Mesh)getObject();
  119. }
  120. static synchronized BaseObj internalCreate(RenderScript rs, IndexEntry entry) {
  121. if(entry.mLoadedObj != null) {
  122. return entry.mLoadedObj;
  123. }
  124. // to be purged on cleanup
  125. if(entry.mEntryType == EntryType.UNKNOWN) {
  126. return null;
  127. }
  128. long objectID = rs.nFileA3DGetEntryByIndex(entry.mID, entry.mIndex);
  129. if(objectID == 0) {
  130. return null;
  131. }
  132. switch (entry.mEntryType) {
  133. case MESH:
  134. entry.mLoadedObj = new Mesh(objectID, rs);
  135. break;
  136. }
  137. entry.mLoadedObj.updateFromNative();
  138. return entry.mLoadedObj;
  139. }
  140. IndexEntry(RenderScript rs, int index, long id, String name, EntryType type) {
  141. mRS = rs;
  142. mIndex = index;
  143. mID = id;
  144. mName = name;
  145. mEntryType = type;
  146. mLoadedObj = null;
  147. }
  148. }
  149. IndexEntry[] mFileEntries;
  150. InputStream mInputStream;
  151. FileA3D(long id, RenderScript rs, InputStream stream) {
  152. super(id, rs);
  153. mInputStream = stream;
  154. }
  155. private void initEntries() {
  156. int numFileEntries = mRS.nFileA3DGetNumIndexEntries(getID(mRS));
  157. if(numFileEntries <= 0) {
  158. return;
  159. }
  160. mFileEntries = new IndexEntry[numFileEntries];
  161. int[] ids = new int[numFileEntries];
  162. String[] names = new String[numFileEntries];
  163. mRS.nFileA3DGetIndexEntries(getID(mRS), numFileEntries, ids, names);
  164. for(int i = 0; i < numFileEntries; i ++) {
  165. mFileEntries[i] = new IndexEntry(mRS, i, getID(mRS), names[i], EntryType.toEntryType(ids[i]));
  166. }
  167. }
  168. /**
  169. * @deprecated in API 16
  170. * Returns the number of objects stored inside the a3d file
  171. *
  172. * @return the number of objects stored inside the a3d file
  173. */
  174. public int getIndexEntryCount() {
  175. if(mFileEntries == null) {
  176. return 0;
  177. }
  178. return mFileEntries.length;
  179. }
  180. /**
  181. * @deprecated in API 16
  182. * Returns an index entry from the list of all objects inside
  183. * FileA3D
  184. *
  185. * @param index number of the entry from the list to return
  186. *
  187. * @return entry in the a3d file described by the index
  188. */
  189. public IndexEntry getIndexEntry(int index) {
  190. if(getIndexEntryCount() == 0 || index < 0 || index >= mFileEntries.length) {
  191. return null;
  192. }
  193. return mFileEntries[index];
  194. }
  195. /**
  196. * @deprecated in API 16
  197. * Creates a FileA3D object from an asset stored on disk
  198. *
  199. * @param rs Context to which the object will belong.
  200. * @param mgr asset manager used to load asset
  201. * @param path location of the file to load
  202. *
  203. * @return a3d file containing renderscript objects
  204. */
  205. static public FileA3D createFromAsset(RenderScript rs, AssetManager mgr, String path) {
  206. rs.validate();
  207. long fileId = rs.nFileA3DCreateFromAsset(mgr, path);
  208. if(fileId == 0) {
  209. throw new RSRuntimeException("Unable to create a3d file from asset " + path);
  210. }
  211. FileA3D fa3d = new FileA3D(fileId, rs, null);
  212. fa3d.initEntries();
  213. return fa3d;
  214. }
  215. /**
  216. * @deprecated in API 16
  217. * Creates a FileA3D object from a file stored on disk
  218. *
  219. * @param rs Context to which the object will belong.
  220. * @param path location of the file to load
  221. *
  222. * @return a3d file containing renderscript objects
  223. */
  224. static public FileA3D createFromFile(RenderScript rs, String path) {
  225. long fileId = rs.nFileA3DCreateFromFile(path);
  226. if(fileId == 0) {
  227. throw new RSRuntimeException("Unable to create a3d file from " + path);
  228. }
  229. FileA3D fa3d = new FileA3D(fileId, rs, null);
  230. fa3d.initEntries();
  231. return fa3d;
  232. }
  233. /**
  234. * @deprecated in API 16
  235. * Creates a FileA3D object from a file stored on disk
  236. *
  237. * @param rs Context to which the object will belong.
  238. * @param path location of the file to load
  239. *
  240. * @return a3d file containing renderscript objects
  241. */
  242. static public FileA3D createFromFile(RenderScript rs, File path) {
  243. return createFromFile(rs, path.getAbsolutePath());
  244. }
  245. /**
  246. * @deprecated in API 16
  247. * Creates a FileA3D object from an application resource
  248. *
  249. * @param rs Context to which the object will belong.
  250. * @param res resource manager used for loading
  251. * @param id resource to create FileA3D from
  252. *
  253. * @return a3d file containing renderscript objects
  254. */
  255. static public FileA3D createFromResource(RenderScript rs, Resources res, int id) {
  256. rs.validate();
  257. InputStream is = null;
  258. try {
  259. is = res.openRawResource(id);
  260. } catch (Exception e) {
  261. throw new RSRuntimeException("Unable to open resource " + id);
  262. }
  263. long fileId = 0;
  264. if (is instanceof AssetManager.AssetInputStream) {
  265. long asset = ((AssetManager.AssetInputStream) is).getNativeAsset();
  266. fileId = rs.nFileA3DCreateFromAssetStream(asset);
  267. } else {
  268. throw new RSRuntimeException("Unsupported asset stream");
  269. }
  270. if(fileId == 0) {
  271. throw new RSRuntimeException("Unable to create a3d file from resource " + id);
  272. }
  273. FileA3D fa3d = new FileA3D(fileId, rs, is);
  274. fa3d.initEntries();
  275. return fa3d;
  276. }
  277. }