• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 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 
17 package androidx.renderscript;
18 
19 import android.util.Log;
20 import java.util.concurrent.locks.ReentrantReadWriteLock;
21 
22 /**
23  * BaseObj is the base class for all RenderScript objects owned by a RS context.
24  * It is responsible for lifetime management and resource tracking. This class
25  * should not be used by a user application.
26  *
27  * @deprecated Renderscript has been deprecated in API level 31. Please refer to the <a
28  * href="https://developer.android.com/guide/topics/renderscript/migration-guide">migration
29  * guide</a> for the proposed alternatives.
30  **/
31 @Deprecated
32 public class BaseObj {
BaseObj(long id, RenderScript rs)33     BaseObj(long id, RenderScript rs) {
34         rs.validate();
35         mRS = rs;
36         mID = id;
37         mDestroyed = false;
38     }
39 
setID(long id)40     void setID(long id) {
41         if (mID != 0) {
42             throw new RSRuntimeException("Internal Error, reset of object ID.");
43         }
44         mID = id;
45     }
46 
47     /**
48      * Lookup the native object ID for this object.  Primarily used by the
49      * generated reflected code.
50      *
51      * @param rs Context to verify against internal context for
52      *           match.
53      *
54      * @return long
55      */
getID(RenderScript rs)56     long getID(RenderScript rs) {
57         mRS.validate();
58         if (mDestroyed) {
59             throw new RSInvalidStateException("using a destroyed object.");
60         }
61         if (mID == 0) {
62             throw new RSRuntimeException("Internal error: Object id 0.");
63         }
64         if ((rs != null) && (rs != mRS)) {
65             throw new RSInvalidStateException("using object with mismatched context.");
66         }
67         return mID;
68     }
69 
getNObj()70     android.renderscript.BaseObj getNObj() {
71         return null;
72     }
73 
checkValid()74     void checkValid() {
75         if ((mID == 0) && (getNObj() == null)) {
76             throw new RSIllegalArgumentException("Invalid object.");
77         }
78     }
79 
80     private long mID;
81     private boolean mDestroyed;
82     RenderScript mRS;
83 
helpDestroy()84     private void helpDestroy() {
85         boolean shouldDestroy = false;
86         synchronized(this) {
87             if (!mDestroyed) {
88                 shouldDestroy = true;
89                 mDestroyed = true;
90             }
91         }
92 
93         if (shouldDestroy) {
94             // must include nObjDestroy in the critical section
95             ReentrantReadWriteLock.ReadLock rlock = mRS.mRWLock.readLock();
96             rlock.lock();
97             if(mRS.isAlive()) {
98                 mRS.nObjDestroy(mID);
99             }
100             rlock.unlock();
101             mRS = null;
102             mID = 0;
103         }
104     }
105 
106 
finalize()107     protected void finalize() throws Throwable {
108         helpDestroy();
109         super.finalize();
110     }
111 
112     /**
113      * Frees any native resources associated with this object.  The
114      * primary use is to force immediate cleanup of resources when it is
115      * believed the GC will not respond quickly enough.
116      */
destroy()117     public void destroy() {
118         if(mDestroyed) {
119             throw new RSInvalidStateException("Object already destroyed.");
120         }
121         helpDestroy();
122     }
123 
124     /**
125      * Calculates the hash code value for a BaseObj.
126      *
127      * @return int
128      */
129     @Override
hashCode()130     public int hashCode() {
131         return (int)((mID & 0xfffffff) ^ (mID >> 32));
132     }
133 
134     /**
135      * Compare the current BaseObj with another BaseObj for equality.
136      *
137      * @param obj The object to check equality with.
138      *
139      * @return boolean
140      */
141     @Override
equals(Object obj)142     public boolean equals(Object obj) {
143         // Early-out check to see if both BaseObjs are actually the same
144         if (this == obj)
145             return true;
146 
147         if (obj == null) {
148             return false;
149         }
150 
151         if (getClass() != obj.getClass()) {
152             return false;
153         }
154 
155         BaseObj b = (BaseObj) obj;
156         return mID == b.mID;
157     }
158 }
159 
160