| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
package android.renderscript; |
| 18 |
|
| 19 |
import android.compat.annotation.UnsupportedAppUsage; |
| 20 |
|
| 21 |
import dalvik.system.CloseGuard; |
| 22 |
|
| 23 |
import java.util.concurrent.locks.ReentrantReadWriteLock; |
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
@Deprecated |
| 35 |
public class BaseObj { |
| 36 |
BaseObj(long id, RenderScript rs) { |
| 37 |
rs.validate(); |
| 38 |
mRS = rs; |
| 39 |
mID = id; |
| 40 |
mDestroyed = false; |
| 41 |
} |
| 42 |
|
| 43 |
void setID(long id) { |
| 44 |
if (mID != 0) { |
| 45 |
throw new RSRuntimeException("Internal Error, reset of object ID."); |
| 46 |
} |
| 47 |
mID = id; |
| 48 |
} |
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
long getID(RenderScript rs) { |
| 60 |
mRS.validate(); |
| 61 |
if (mDestroyed) { |
| 62 |
throw new RSInvalidStateException("using a destroyed object."); |
| 63 |
} |
| 64 |
if (mID == 0) { |
| 65 |
throw new RSRuntimeException("Internal error: Object id 0."); |
| 66 |
} |
| 67 |
if ((rs != null) && (rs != mRS)) { |
| 68 |
throw new RSInvalidStateException("using object with mismatched context."); |
| 69 |
} |
| 70 |
return mID; |
| 71 |
} |
| 72 |
|
| 73 |
void checkValid() { |
| 74 |
if (mID == 0) { |
| 75 |
throw new RSIllegalArgumentException("Invalid object."); |
| 76 |
} |
| 77 |
} |
| 78 |
|
| 79 |
private long mID; |
| 80 |
final CloseGuard guard = CloseGuard.get(); |
| 81 |
private boolean mDestroyed; |
| 82 |
private String mName; |
| 83 |
@UnsupportedAppUsage |
| 84 |
RenderScript mRS; |
| 85 |
|
| 86 |
|
| 87 |
|
| 88 |
|
| 89 |
|
| 90 |
|
| 91 |
|
| 92 |
public void setName(String name) { |
| 93 |
if (name == null) { |
| 94 |
throw new RSIllegalArgumentException( |
| 95 |
"setName requires a string of non-zero length."); |
| 96 |
} |
| 97 |
if(name.length() < 1) { |
| 98 |
throw new RSIllegalArgumentException( |
| 99 |
"setName does not accept a zero length string."); |
| 100 |
} |
| 101 |
if(mName != null) { |
| 102 |
throw new RSIllegalArgumentException( |
| 103 |
"setName object already has a name."); |
| 104 |
} |
| 105 |
|
| 106 |
try { |
| 107 |
byte[] bytes = name.getBytes("UTF-8"); |
| 108 |
mRS.nAssignName(mID, bytes); |
| 109 |
mName = name; |
| 110 |
} catch (java.io.UnsupportedEncodingException e) { |
| 111 |
throw new RuntimeException(e); |
| 112 |
} |
| 113 |
} |
| 114 |
|
| 115 |
|
| 116 |
|
| 117 |
|
| 118 |
public String getName() { |
| 119 |
return mName; |
| 120 |
} |
| 121 |
|
| 122 |
private void helpDestroy() { |
| 123 |
boolean shouldDestroy = false; |
| 124 |
synchronized(this) { |
| 125 |
if (!mDestroyed) { |
| 126 |
shouldDestroy = true; |
| 127 |
mDestroyed = true; |
| 128 |
} |
| 129 |
} |
| 130 |
|
| 131 |
if (shouldDestroy) { |
| 132 |
guard.close(); |
| 133 |
|
| 134 |
ReentrantReadWriteLock.ReadLock rlock = mRS.mRWLock.readLock(); |
| 135 |
rlock.lock(); |
| 136 |
|
| 137 |
if(mRS.isAlive() && mID != 0) { |
| 138 |
mRS.nObjDestroy(mID); |
| 139 |
} |
| 140 |
rlock.unlock(); |
| 141 |
mRS = null; |
| 142 |
mID = 0; |
| 143 |
} |
| 144 |
} |
| 145 |
|
| 146 |
protected void finalize() throws Throwable { |
| 147 |
try { |
| 148 |
if (guard != null) { |
| 149 |
guard.warnIfOpen(); |
| 150 |
} |
| 151 |
helpDestroy(); |
| 152 |
} finally { |
| 153 |
super.finalize(); |
| 154 |
} |
| 155 |
} |
| 156 |
|
| 157 |
|
| 158 |
|
| 159 |
|
| 160 |
|
| 161 |
|
| 162 |
public void destroy() { |
| 163 |
if(mDestroyed) { |
| 164 |
throw new RSInvalidStateException("Object already destroyed."); |
| 165 |
} |
| 166 |
helpDestroy(); |
| 167 |
} |
| 168 |
|
| 169 |
|
| 170 |
|
| 171 |
|
| 172 |
|
| 173 |
void updateFromNative() { |
| 174 |
mRS.validate(); |
| 175 |
mName = mRS.nGetName(getID(mRS)); |
| 176 |
} |
| 177 |
|
| 178 |
|
| 179 |
|
| 180 |
|
| 181 |
|
| 182 |
|
| 183 |
@Override |
| 184 |
public int hashCode() { |
| 185 |
return (int)((mID & 0xfffffff) ^ (mID >> 32)); |
| 186 |
} |
| 187 |
|
| 188 |
|
| 189 |
|
| 190 |
|
| 191 |
|
| 192 |
|
| 193 |
|
| 194 |
|
| 195 |
@Override |
| 196 |
public boolean equals(Object obj) { |
| 197 |
|
| 198 |
if (this == obj) |
| 199 |
return true; |
| 200 |
|
| 201 |
if (obj == null) { |
| 202 |
return false; |
| 203 |
} |
| 204 |
|
| 205 |
if (getClass() != obj.getClass()) { |
| 206 |
return false; |
| 207 |
} |
| 208 |
|
| 209 |
BaseObj b = (BaseObj) obj; |
| 210 |
return mID == b.mID; |
| 211 |
} |
| 212 |
} |
| 213 |
|