/rs/java/android/renderscript/BaseObj.java

https://github.com/aizuzi/platform_frameworks_base · Java · 196 lines · 112 code · 21 blank · 63 comment · 29 complexity · 9d50401cd2416ec44cf091d0d589d60f 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 java.util.concurrent.locks.ReentrantReadWriteLock;
  18. /**
  19. * BaseObj is the base class for all RenderScript objects owned by a RS context.
  20. * It is responsible for lifetime management and resource tracking. This class
  21. * should not be used by a user application.
  22. *
  23. **/
  24. public class BaseObj {
  25. BaseObj(long id, RenderScript rs) {
  26. rs.validate();
  27. mRS = rs;
  28. mID = id;
  29. mDestroyed = false;
  30. }
  31. void setID(long id) {
  32. if (mID != 0) {
  33. throw new RSRuntimeException("Internal Error, reset of object ID.");
  34. }
  35. mID = id;
  36. }
  37. /**
  38. * Lookup the native object ID for this object. Primarily used by the
  39. * generated reflected code.
  40. *
  41. * @param rs Context to verify against internal context for
  42. * match.
  43. *
  44. * @return long
  45. */
  46. long getID(RenderScript rs) {
  47. mRS.validate();
  48. if (mDestroyed) {
  49. throw new RSInvalidStateException("using a destroyed object.");
  50. }
  51. if (mID == 0) {
  52. throw new RSRuntimeException("Internal error: Object id 0.");
  53. }
  54. if ((rs != null) && (rs != mRS)) {
  55. throw new RSInvalidStateException("using object with mismatched context.");
  56. }
  57. return mID;
  58. }
  59. void checkValid() {
  60. if (mID == 0) {
  61. throw new RSIllegalArgumentException("Invalid object.");
  62. }
  63. }
  64. private long mID;
  65. private boolean mDestroyed;
  66. private String mName;
  67. RenderScript mRS;
  68. /**
  69. * setName assigns a name to an object. This object can later be looked up
  70. * by this name.
  71. *
  72. * @param name The name to assign to the object.
  73. */
  74. public void setName(String name) {
  75. if (name == null) {
  76. throw new RSIllegalArgumentException(
  77. "setName requires a string of non-zero length.");
  78. }
  79. if(name.length() < 1) {
  80. throw new RSIllegalArgumentException(
  81. "setName does not accept a zero length string.");
  82. }
  83. if(mName != null) {
  84. throw new RSIllegalArgumentException(
  85. "setName object already has a name.");
  86. }
  87. try {
  88. byte[] bytes = name.getBytes("UTF-8");
  89. mRS.nAssignName(mID, bytes);
  90. mName = name;
  91. } catch (java.io.UnsupportedEncodingException e) {
  92. throw new RuntimeException(e);
  93. }
  94. }
  95. /**
  96. * @return name of the renderscript object
  97. */
  98. public String getName() {
  99. return mName;
  100. }
  101. private void helpDestroy() {
  102. boolean shouldDestroy = false;
  103. synchronized(this) {
  104. if (!mDestroyed) {
  105. shouldDestroy = true;
  106. mDestroyed = true;
  107. }
  108. }
  109. if (shouldDestroy) {
  110. // must include nObjDestroy in the critical section
  111. ReentrantReadWriteLock.ReadLock rlock = mRS.mRWLock.readLock();
  112. rlock.lock();
  113. // AllocationAdapters are BaseObjs with an ID of 0 but should not be passed to nObjDestroy
  114. if(mRS.isAlive() && mID != 0) {
  115. mRS.nObjDestroy(mID);
  116. }
  117. rlock.unlock();
  118. mRS = null;
  119. mID = 0;
  120. }
  121. }
  122. protected void finalize() throws Throwable {
  123. helpDestroy();
  124. super.finalize();
  125. }
  126. /**
  127. * Frees any native resources associated with this object. The
  128. * primary use is to force immediate cleanup of resources when it is
  129. * believed the GC will not respond quickly enough.
  130. */
  131. public void destroy() {
  132. if(mDestroyed) {
  133. throw new RSInvalidStateException("Object already destroyed.");
  134. }
  135. helpDestroy();
  136. }
  137. /**
  138. * If an object came from an a3d file, java fields need to be
  139. * created with objects from the native layer
  140. */
  141. void updateFromNative() {
  142. mRS.validate();
  143. mName = mRS.nGetName(getID(mRS));
  144. }
  145. /**
  146. * Calculates the hash code value for a BaseObj.
  147. *
  148. * @return int
  149. */
  150. @Override
  151. public int hashCode() {
  152. return (int)((mID & 0xfffffff) ^ (mID >> 32));
  153. }
  154. /**
  155. * Compare the current BaseObj with another BaseObj for equality.
  156. *
  157. * @param obj The object to check equality with.
  158. *
  159. * @return boolean
  160. */
  161. @Override
  162. public boolean equals(Object obj) {
  163. // Early-out check to see if both BaseObjs are actually the same
  164. if (this == obj)
  165. return true;
  166. if (obj == null) {
  167. return false;
  168. }
  169. if (getClass() != obj.getClass()) {
  170. return false;
  171. }
  172. BaseObj b = (BaseObj) obj;
  173. return mID == b.mID;
  174. }
  175. }