/rs/java/android/renderscript/ScriptC.java

https://github.com/aizuzi/platform_frameworks_base · Java · 156 lines · 87 code · 15 blank · 54 comment · 14 complexity · 5cf3bf94a10e10c0ccc2f92d419180be MD5 · raw file

  1. /*
  2. * Copyright (C) 2008 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 android.content.Context;
  18. import android.content.res.Resources;
  19. import android.util.Log;
  20. import java.io.File;
  21. import java.io.IOException;
  22. import java.io.InputStream;
  23. import java.util.Map.Entry;
  24. import java.util.HashMap;
  25. import java.lang.reflect.Field;
  26. import java.lang.reflect.Modifier;
  27. /**
  28. * The superclass for all user-defined scripts. This is only
  29. * intended to be used by the generated derived classes.
  30. **/
  31. public class ScriptC extends Script {
  32. private static final String TAG = "ScriptC";
  33. /**
  34. * Only intended for use by the generated derived classes.
  35. *
  36. * @param id
  37. * @param rs
  38. */
  39. protected ScriptC(int id, RenderScript rs) {
  40. super(id, rs);
  41. }
  42. /**
  43. * Only intended for use by the generated derived classes.
  44. *
  45. * @param id
  46. * @param rs
  47. *
  48. * @hide
  49. */
  50. protected ScriptC(long id, RenderScript rs) {
  51. super(id, rs);
  52. }
  53. /**
  54. * Only intended for use by the generated derived classes.
  55. *
  56. *
  57. * @param rs
  58. * @param resources
  59. * @param resourceID
  60. */
  61. protected ScriptC(RenderScript rs, Resources resources, int resourceID) {
  62. super(0, rs);
  63. long id = internalCreate(rs, resources, resourceID);
  64. if (id == 0) {
  65. throw new RSRuntimeException("Loading of ScriptC script failed.");
  66. }
  67. setID(id);
  68. }
  69. /**
  70. * Only intended for use by the generated derived classes.
  71. *
  72. * @param rs
  73. * @hide
  74. */
  75. protected ScriptC(RenderScript rs, String resName, byte[] bitcode32, byte[] bitcode64) {
  76. super(0, rs);
  77. long id = 0;
  78. if (RenderScript.sPointerSize == 4) {
  79. id = internalStringCreate(rs, resName, bitcode32);
  80. } else {
  81. id = internalStringCreate(rs, resName, bitcode64);
  82. }
  83. if (id == 0) {
  84. throw new RSRuntimeException("Loading of ScriptC script failed.");
  85. }
  86. setID(id);
  87. }
  88. /**
  89. * Name of the file that holds the object cache.
  90. */
  91. private static final String CACHE_PATH = "com.android.renderscript.cache";
  92. static String mCachePath;
  93. private static synchronized long internalCreate(RenderScript rs, Resources resources, int resourceID) {
  94. byte[] pgm;
  95. int pgmLength;
  96. InputStream is = resources.openRawResource(resourceID);
  97. try {
  98. try {
  99. pgm = new byte[1024];
  100. pgmLength = 0;
  101. while(true) {
  102. int bytesLeft = pgm.length - pgmLength;
  103. if (bytesLeft == 0) {
  104. byte[] buf2 = new byte[pgm.length * 2];
  105. System.arraycopy(pgm, 0, buf2, 0, pgm.length);
  106. pgm = buf2;
  107. bytesLeft = pgm.length - pgmLength;
  108. }
  109. int bytesRead = is.read(pgm, pgmLength, bytesLeft);
  110. if (bytesRead <= 0) {
  111. break;
  112. }
  113. pgmLength += bytesRead;
  114. }
  115. } finally {
  116. is.close();
  117. }
  118. } catch(IOException e) {
  119. throw new Resources.NotFoundException();
  120. }
  121. String resName = resources.getResourceEntryName(resourceID);
  122. // Create the RS cache path if we haven't done so already.
  123. if (mCachePath == null) {
  124. File f = new File(rs.mCacheDir, CACHE_PATH);
  125. mCachePath = f.getAbsolutePath();
  126. f.mkdirs();
  127. }
  128. // Log.v(TAG, "Create script for resource = " + resName);
  129. return rs.nScriptCCreate(resName, mCachePath, pgm, pgmLength);
  130. }
  131. private static synchronized long internalStringCreate(RenderScript rs, String resName, byte[] bitcode) {
  132. // Create the RS cache path if we haven't done so already.
  133. if (mCachePath == null) {
  134. File f = new File(rs.mCacheDir, CACHE_PATH);
  135. mCachePath = f.getAbsolutePath();
  136. f.mkdirs();
  137. }
  138. // Log.v(TAG, "Create script for resource = " + resName);
  139. return rs.nScriptCCreate(resName, mCachePath, bitcode, bitcode.length);
  140. }
  141. }