/rs/java/android/renderscript/FieldPacker.java

https://github.com/aizuzi/platform_frameworks_base · Java · 630 lines · 532 code · 64 blank · 34 comment · 40 complexity · 873e76aef4ed2a4bf66903e16d7cb210 MD5 · raw file

  1. /*
  2. * Copyright (C) 2013 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.util.Log;
  18. import java.util.BitSet;
  19. /**
  20. * Utility class for packing arguments and structures from Android system objects to
  21. * RenderScript objects.
  22. *
  23. * This class is only intended to be used to support the
  24. * reflected code generated by the RS tool chain. It should not
  25. * be called directly.
  26. *
  27. **/
  28. public class FieldPacker {
  29. public FieldPacker(int len) {
  30. mPos = 0;
  31. mLen = len;
  32. mData = new byte[len];
  33. mAlignment = new BitSet();
  34. }
  35. public FieldPacker(byte[] data) {
  36. // Advance mPos to the end of the buffer, since we are copying in the
  37. // full data input.
  38. mPos = data.length;
  39. mLen = data.length;
  40. mData = data;
  41. mAlignment = new BitSet();
  42. // TODO: We should either have an actual FieldPacker copy constructor
  43. // or drop support for computing alignment like this. As it stands,
  44. // subAlign() can never work correctly for copied FieldPacker objects.
  45. }
  46. public void align(int v) {
  47. if ((v <= 0) || ((v & (v - 1)) != 0)) {
  48. throw new RSIllegalArgumentException("argument must be a non-negative non-zero power of 2: " + v);
  49. }
  50. while ((mPos & (v - 1)) != 0) {
  51. mAlignment.flip(mPos);
  52. mData[mPos++] = 0;
  53. }
  54. }
  55. public void subalign(int v) {
  56. if ((v & (v - 1)) != 0) {
  57. throw new RSIllegalArgumentException("argument must be a non-negative non-zero power of 2: " + v);
  58. }
  59. while ((mPos & (v - 1)) != 0) {
  60. mPos--;
  61. }
  62. if (mPos > 0) {
  63. while (mAlignment.get(mPos - 1) == true) {
  64. mPos--;
  65. mAlignment.flip(mPos);
  66. }
  67. }
  68. }
  69. public void reset() {
  70. mPos = 0;
  71. }
  72. public void reset(int i) {
  73. if ((i < 0) || (i > mLen)) {
  74. throw new RSIllegalArgumentException("out of range argument: " + i);
  75. }
  76. mPos = i;
  77. }
  78. public void skip(int i) {
  79. int res = mPos + i;
  80. if ((res < 0) || (res > mLen)) {
  81. throw new RSIllegalArgumentException("out of range argument: " + i);
  82. }
  83. mPos = res;
  84. }
  85. public void addI8(byte v) {
  86. mData[mPos++] = v;
  87. }
  88. public byte subI8() {
  89. subalign(1);
  90. return mData[--mPos];
  91. }
  92. public void addI16(short v) {
  93. align(2);
  94. mData[mPos++] = (byte)(v & 0xff);
  95. mData[mPos++] = (byte)(v >> 8);
  96. }
  97. public short subI16() {
  98. subalign(2);
  99. short v = 0;
  100. v = (short)((mData[--mPos] & 0xff) << 8);
  101. v = (short)(v | (short)(mData[--mPos] & 0xff));
  102. return v;
  103. }
  104. public void addI32(int v) {
  105. align(4);
  106. mData[mPos++] = (byte)(v & 0xff);
  107. mData[mPos++] = (byte)((v >> 8) & 0xff);
  108. mData[mPos++] = (byte)((v >> 16) & 0xff);
  109. mData[mPos++] = (byte)((v >> 24) & 0xff);
  110. }
  111. public int subI32() {
  112. subalign(4);
  113. int v = 0;
  114. v = ((mData[--mPos] & 0xff) << 24);
  115. v = v | ((mData[--mPos] & 0xff) << 16);
  116. v = v | ((mData[--mPos] & 0xff) << 8);
  117. v = v | ((mData[--mPos] & 0xff));
  118. return v;
  119. }
  120. public void addI64(long v) {
  121. align(8);
  122. mData[mPos++] = (byte)(v & 0xff);
  123. mData[mPos++] = (byte)((v >> 8) & 0xff);
  124. mData[mPos++] = (byte)((v >> 16) & 0xff);
  125. mData[mPos++] = (byte)((v >> 24) & 0xff);
  126. mData[mPos++] = (byte)((v >> 32) & 0xff);
  127. mData[mPos++] = (byte)((v >> 40) & 0xff);
  128. mData[mPos++] = (byte)((v >> 48) & 0xff);
  129. mData[mPos++] = (byte)((v >> 56) & 0xff);
  130. }
  131. public long subI64() {
  132. subalign(8);
  133. long v = 0;
  134. byte x = 0;
  135. x = ((mData[--mPos]));
  136. v = (long)(v | (((long)x) & 0xff) << 56l);
  137. x = ((mData[--mPos]));
  138. v = (long)(v | (((long)x) & 0xff) << 48l);
  139. x = ((mData[--mPos]));
  140. v = (long)(v | (((long)x) & 0xff) << 40l);
  141. x = ((mData[--mPos]));
  142. v = (long)(v | (((long)x) & 0xff) << 32l);
  143. x = ((mData[--mPos]));
  144. v = (long)(v | (((long)x) & 0xff) << 24l);
  145. x = ((mData[--mPos]));
  146. v = (long)(v | (((long)x) & 0xff) << 16l);
  147. x = ((mData[--mPos]));
  148. v = (long)(v | (((long)x) & 0xff) << 8l);
  149. x = ((mData[--mPos]));
  150. v = (long)(v | (((long)x) & 0xff));
  151. return v;
  152. }
  153. public void addU8(short v) {
  154. if ((v < 0) || (v > 0xff)) {
  155. android.util.Log.e("rs", "FieldPacker.addU8( " + v + " )");
  156. throw new IllegalArgumentException("Saving value out of range for type");
  157. }
  158. mData[mPos++] = (byte)v;
  159. }
  160. public void addU16(int v) {
  161. if ((v < 0) || (v > 0xffff)) {
  162. android.util.Log.e("rs", "FieldPacker.addU16( " + v + " )");
  163. throw new IllegalArgumentException("Saving value out of range for type");
  164. }
  165. align(2);
  166. mData[mPos++] = (byte)(v & 0xff);
  167. mData[mPos++] = (byte)(v >> 8);
  168. }
  169. public void addU32(long v) {
  170. if ((v < 0) || (v > 0xffffffffL)) {
  171. android.util.Log.e("rs", "FieldPacker.addU32( " + v + " )");
  172. throw new IllegalArgumentException("Saving value out of range for type");
  173. }
  174. align(4);
  175. mData[mPos++] = (byte)(v & 0xff);
  176. mData[mPos++] = (byte)((v >> 8) & 0xff);
  177. mData[mPos++] = (byte)((v >> 16) & 0xff);
  178. mData[mPos++] = (byte)((v >> 24) & 0xff);
  179. }
  180. public void addU64(long v) {
  181. if (v < 0) {
  182. android.util.Log.e("rs", "FieldPacker.addU64( " + v + " )");
  183. throw new IllegalArgumentException("Saving value out of range for type");
  184. }
  185. align(8);
  186. mData[mPos++] = (byte)(v & 0xff);
  187. mData[mPos++] = (byte)((v >> 8) & 0xff);
  188. mData[mPos++] = (byte)((v >> 16) & 0xff);
  189. mData[mPos++] = (byte)((v >> 24) & 0xff);
  190. mData[mPos++] = (byte)((v >> 32) & 0xff);
  191. mData[mPos++] = (byte)((v >> 40) & 0xff);
  192. mData[mPos++] = (byte)((v >> 48) & 0xff);
  193. mData[mPos++] = (byte)((v >> 56) & 0xff);
  194. }
  195. public void addF32(float v) {
  196. addI32(Float.floatToRawIntBits(v));
  197. }
  198. public float subF32() {
  199. return Float.intBitsToFloat(subI32());
  200. }
  201. public void addF64(double v) {
  202. addI64(Double.doubleToRawLongBits(v));
  203. }
  204. public double subF64() {
  205. return Double.longBitsToDouble(subI64());
  206. }
  207. public void addObj(BaseObj obj) {
  208. if (obj != null) {
  209. if (RenderScript.sPointerSize == 8) {
  210. addI64(obj.getID(null));
  211. addI64(0);
  212. addI64(0);
  213. addI64(0);
  214. }
  215. else {
  216. addI32((int)obj.getID(null));
  217. }
  218. } else {
  219. if (RenderScript.sPointerSize == 8) {
  220. addI64(0);
  221. addI64(0);
  222. addI64(0);
  223. addI64(0);
  224. } else {
  225. addI32(0);
  226. }
  227. }
  228. }
  229. public void addF32(Float2 v) {
  230. addF32(v.x);
  231. addF32(v.y);
  232. }
  233. public void addF32(Float3 v) {
  234. addF32(v.x);
  235. addF32(v.y);
  236. addF32(v.z);
  237. }
  238. public void addF32(Float4 v) {
  239. addF32(v.x);
  240. addF32(v.y);
  241. addF32(v.z);
  242. addF32(v.w);
  243. }
  244. public void addF64(Double2 v) {
  245. addF64(v.x);
  246. addF64(v.y);
  247. }
  248. public void addF64(Double3 v) {
  249. addF64(v.x);
  250. addF64(v.y);
  251. addF64(v.z);
  252. }
  253. public void addF64(Double4 v) {
  254. addF64(v.x);
  255. addF64(v.y);
  256. addF64(v.z);
  257. addF64(v.w);
  258. }
  259. public void addI8(Byte2 v) {
  260. addI8(v.x);
  261. addI8(v.y);
  262. }
  263. public void addI8(Byte3 v) {
  264. addI8(v.x);
  265. addI8(v.y);
  266. addI8(v.z);
  267. }
  268. public void addI8(Byte4 v) {
  269. addI8(v.x);
  270. addI8(v.y);
  271. addI8(v.z);
  272. addI8(v.w);
  273. }
  274. public void addU8(Short2 v) {
  275. addU8(v.x);
  276. addU8(v.y);
  277. }
  278. public void addU8(Short3 v) {
  279. addU8(v.x);
  280. addU8(v.y);
  281. addU8(v.z);
  282. }
  283. public void addU8(Short4 v) {
  284. addU8(v.x);
  285. addU8(v.y);
  286. addU8(v.z);
  287. addU8(v.w);
  288. }
  289. public void addI16(Short2 v) {
  290. addI16(v.x);
  291. addI16(v.y);
  292. }
  293. public void addI16(Short3 v) {
  294. addI16(v.x);
  295. addI16(v.y);
  296. addI16(v.z);
  297. }
  298. public void addI16(Short4 v) {
  299. addI16(v.x);
  300. addI16(v.y);
  301. addI16(v.z);
  302. addI16(v.w);
  303. }
  304. public void addU16(Int2 v) {
  305. addU16(v.x);
  306. addU16(v.y);
  307. }
  308. public void addU16(Int3 v) {
  309. addU16(v.x);
  310. addU16(v.y);
  311. addU16(v.z);
  312. }
  313. public void addU16(Int4 v) {
  314. addU16(v.x);
  315. addU16(v.y);
  316. addU16(v.z);
  317. addU16(v.w);
  318. }
  319. public void addI32(Int2 v) {
  320. addI32(v.x);
  321. addI32(v.y);
  322. }
  323. public void addI32(Int3 v) {
  324. addI32(v.x);
  325. addI32(v.y);
  326. addI32(v.z);
  327. }
  328. public void addI32(Int4 v) {
  329. addI32(v.x);
  330. addI32(v.y);
  331. addI32(v.z);
  332. addI32(v.w);
  333. }
  334. public void addU32(Long2 v) {
  335. addU32(v.x);
  336. addU32(v.y);
  337. }
  338. public void addU32(Long3 v) {
  339. addU32(v.x);
  340. addU32(v.y);
  341. addU32(v.z);
  342. }
  343. public void addU32(Long4 v) {
  344. addU32(v.x);
  345. addU32(v.y);
  346. addU32(v.z);
  347. addU32(v.w);
  348. }
  349. public void addI64(Long2 v) {
  350. addI64(v.x);
  351. addI64(v.y);
  352. }
  353. public void addI64(Long3 v) {
  354. addI64(v.x);
  355. addI64(v.y);
  356. addI64(v.z);
  357. }
  358. public void addI64(Long4 v) {
  359. addI64(v.x);
  360. addI64(v.y);
  361. addI64(v.z);
  362. addI64(v.w);
  363. }
  364. public void addU64(Long2 v) {
  365. addU64(v.x);
  366. addU64(v.y);
  367. }
  368. public void addU64(Long3 v) {
  369. addU64(v.x);
  370. addU64(v.y);
  371. addU64(v.z);
  372. }
  373. public void addU64(Long4 v) {
  374. addU64(v.x);
  375. addU64(v.y);
  376. addU64(v.z);
  377. addU64(v.w);
  378. }
  379. public Float2 subFloat2() {
  380. Float2 v = new Float2();
  381. v.y = subF32();
  382. v.x = subF32();
  383. return v;
  384. }
  385. public Float3 subFloat3() {
  386. Float3 v = new Float3();
  387. v.z = subF32();
  388. v.y = subF32();
  389. v.x = subF32();
  390. return v;
  391. }
  392. public Float4 subFloat4() {
  393. Float4 v = new Float4();
  394. v.w = subF32();
  395. v.z = subF32();
  396. v.y = subF32();
  397. v.x = subF32();
  398. return v;
  399. }
  400. public Double2 subDouble2() {
  401. Double2 v = new Double2();
  402. v.y = subF64();
  403. v.x = subF64();
  404. return v;
  405. }
  406. public Double3 subDouble3() {
  407. Double3 v = new Double3();
  408. v.z = subF64();
  409. v.y = subF64();
  410. v.x = subF64();
  411. return v;
  412. }
  413. public Double4 subDouble4() {
  414. Double4 v = new Double4();
  415. v.w = subF64();
  416. v.z = subF64();
  417. v.y = subF64();
  418. v.x = subF64();
  419. return v;
  420. }
  421. public Byte2 subByte2() {
  422. Byte2 v = new Byte2();
  423. v.y = subI8();
  424. v.x = subI8();
  425. return v;
  426. }
  427. public Byte3 subByte3() {
  428. Byte3 v = new Byte3();
  429. v.z = subI8();
  430. v.y = subI8();
  431. v.x = subI8();
  432. return v;
  433. }
  434. public Byte4 subByte4() {
  435. Byte4 v = new Byte4();
  436. v.w = subI8();
  437. v.z = subI8();
  438. v.y = subI8();
  439. v.x = subI8();
  440. return v;
  441. }
  442. public Short2 subShort2() {
  443. Short2 v = new Short2();
  444. v.y = subI16();
  445. v.x = subI16();
  446. return v;
  447. }
  448. public Short3 subShort3() {
  449. Short3 v = new Short3();
  450. v.z = subI16();
  451. v.y = subI16();
  452. v.x = subI16();
  453. return v;
  454. }
  455. public Short4 subShort4() {
  456. Short4 v = new Short4();
  457. v.w = subI16();
  458. v.z = subI16();
  459. v.y = subI16();
  460. v.x = subI16();
  461. return v;
  462. }
  463. public Int2 subInt2() {
  464. Int2 v = new Int2();
  465. v.y = subI32();
  466. v.x = subI32();
  467. return v;
  468. }
  469. public Int3 subInt3() {
  470. Int3 v = new Int3();
  471. v.z = subI32();
  472. v.y = subI32();
  473. v.x = subI32();
  474. return v;
  475. }
  476. public Int4 subInt4() {
  477. Int4 v = new Int4();
  478. v.w = subI32();
  479. v.z = subI32();
  480. v.y = subI32();
  481. v.x = subI32();
  482. return v;
  483. }
  484. public Long2 subLong2() {
  485. Long2 v = new Long2();
  486. v.y = subI64();
  487. v.x = subI64();
  488. return v;
  489. }
  490. public Long3 subLong3() {
  491. Long3 v = new Long3();
  492. v.z = subI64();
  493. v.y = subI64();
  494. v.x = subI64();
  495. return v;
  496. }
  497. public Long4 subLong4() {
  498. Long4 v = new Long4();
  499. v.w = subI64();
  500. v.z = subI64();
  501. v.y = subI64();
  502. v.x = subI64();
  503. return v;
  504. }
  505. public void addMatrix(Matrix4f v) {
  506. for (int i=0; i < v.mMat.length; i++) {
  507. addF32(v.mMat[i]);
  508. }
  509. }
  510. public Matrix4f subMatrix4f() {
  511. Matrix4f v = new Matrix4f();
  512. for (int i = v.mMat.length - 1; i >= 0; i--) {
  513. v.mMat[i] = subF32();
  514. }
  515. return v;
  516. }
  517. public void addMatrix(Matrix3f v) {
  518. for (int i=0; i < v.mMat.length; i++) {
  519. addF32(v.mMat[i]);
  520. }
  521. }
  522. public Matrix3f subMatrix3f() {
  523. Matrix3f v = new Matrix3f();
  524. for (int i = v.mMat.length - 1; i >= 0; i--) {
  525. v.mMat[i] = subF32();
  526. }
  527. return v;
  528. }
  529. public void addMatrix(Matrix2f v) {
  530. for (int i=0; i < v.mMat.length; i++) {
  531. addF32(v.mMat[i]);
  532. }
  533. }
  534. public Matrix2f subMatrix2f() {
  535. Matrix2f v = new Matrix2f();
  536. for (int i = v.mMat.length - 1; i >= 0; i--) {
  537. v.mMat[i] = subF32();
  538. }
  539. return v;
  540. }
  541. public void addBoolean(boolean v) {
  542. addI8((byte)(v ? 1 : 0));
  543. }
  544. public boolean subBoolean() {
  545. byte v = subI8();
  546. if (v == 1) {
  547. return true;
  548. }
  549. return false;
  550. }
  551. public final byte[] getData() {
  552. return mData;
  553. }
  554. /**
  555. * Get the actual length used for the FieldPacker.
  556. *
  557. * @hide
  558. */
  559. public int getPos() {
  560. return mPos;
  561. }
  562. private final byte mData[];
  563. private int mPos;
  564. private int mLen;
  565. private BitSet mAlignment;
  566. }