rsStream.h 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. /*
  2. * Copyright (C) 2009 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. #ifndef ANDROID_RS_STREAM_H
  17. #define ANDROID_RS_STREAM_H
  18. #include <stdio.h>
  19. #include "rsUtils.h"
  20. // ---------------------------------------------------------------------------
  21. namespace android {
  22. namespace renderscript {
  23. class IStream {
  24. public:
  25. IStream(const uint8_t *, bool use64);
  26. float loadF() {
  27. mPos = (mPos + 3) & (~3);
  28. float tmp = reinterpret_cast<const float *>(&mData[mPos])[0];
  29. mPos += sizeof(float);
  30. return tmp;
  31. }
  32. int32_t loadI32() {
  33. mPos = (mPos + 3) & (~3);
  34. int32_t tmp = reinterpret_cast<const int32_t *>(&mData[mPos])[0];
  35. mPos += sizeof(int32_t);
  36. return tmp;
  37. }
  38. uint32_t loadU32() {
  39. mPos = (mPos + 3) & (~3);
  40. uint32_t tmp = reinterpret_cast<const uint32_t *>(&mData[mPos])[0];
  41. mPos += sizeof(uint32_t);
  42. return tmp;
  43. }
  44. uint16_t loadU16() {
  45. mPos = (mPos + 1) & (~1);
  46. uint16_t tmp = reinterpret_cast<const uint16_t *>(&mData[mPos])[0];
  47. mPos += sizeof(uint16_t);
  48. return tmp;
  49. }
  50. inline uint8_t loadU8() {
  51. uint8_t tmp = reinterpret_cast<const uint8_t *>(&mData[mPos])[0];
  52. mPos += sizeof(uint8_t);
  53. return tmp;
  54. }
  55. void loadByteArray(void *dest, size_t numBytes);
  56. uint64_t loadOffset();
  57. const char * loadString();
  58. uint64_t getPos() const {
  59. return mPos;
  60. }
  61. void reset(uint64_t pos) {
  62. mPos = pos;
  63. }
  64. void reset() {
  65. mPos = 0;
  66. }
  67. const uint8_t * getPtr() const {
  68. return mData;
  69. }
  70. protected:
  71. const uint8_t * mData;
  72. uint64_t mPos;
  73. bool mUse64;
  74. };
  75. class OStream {
  76. public:
  77. OStream(uint64_t length, bool use64);
  78. ~OStream();
  79. void align(uint32_t bytes) {
  80. mPos = (mPos + (bytes - 1)) & (~(bytes - 1));
  81. if (mPos >= mLength) {
  82. growSize();
  83. }
  84. }
  85. void addF(float v) {
  86. uint32_t uintV = *reinterpret_cast<uint32_t*> (&v);
  87. addU32(uintV);
  88. }
  89. void addI32(int32_t v) {
  90. mPos = (mPos + 3) & (~3);
  91. if (mPos + sizeof(v) >= mLength) {
  92. growSize();
  93. }
  94. mData[mPos++] = (uint8_t)(v & 0xff);
  95. mData[mPos++] = (uint8_t)((v >> 8) & 0xff);
  96. mData[mPos++] = (uint8_t)((v >> 16) & 0xff);
  97. mData[mPos++] = (uint8_t)((v >> 24) & 0xff);
  98. }
  99. void addU32(uint32_t v) {
  100. mPos = (mPos + 3) & (~3);
  101. if (mPos + sizeof(v) >= mLength) {
  102. growSize();
  103. }
  104. mData[mPos++] = (uint8_t)(v & 0xff);
  105. mData[mPos++] = (uint8_t)((v >> 8) & 0xff);
  106. mData[mPos++] = (uint8_t)((v >> 16) & 0xff);
  107. mData[mPos++] = (uint8_t)((v >> 24) & 0xff);
  108. }
  109. void addU16(uint16_t v) {
  110. mPos = (mPos + 1) & (~1);
  111. if (mPos + sizeof(v) >= mLength) {
  112. growSize();
  113. }
  114. mData[mPos++] = (uint8_t)(v & 0xff);
  115. mData[mPos++] = (uint8_t)(v >> 8);
  116. }
  117. inline void addU8(uint8_t v) {
  118. if (mPos + 1 >= mLength) {
  119. growSize();
  120. }
  121. reinterpret_cast<uint8_t *>(&mData[mPos])[0] = v;
  122. mPos ++;
  123. }
  124. void addByteArray(const void *src, size_t numBytes);
  125. void addOffset(uint64_t v);
  126. void addString(const char *name);
  127. void addString(const char *name, size_t len);
  128. uint64_t getPos() const {
  129. return mPos;
  130. }
  131. void reset(uint64_t pos) {
  132. mPos = pos;
  133. }
  134. void reset() {
  135. mPos = 0;
  136. }
  137. const uint8_t * getPtr() const {
  138. return mData;
  139. }
  140. protected:
  141. void growSize();
  142. uint8_t * mData;
  143. uint64_t mLength;
  144. uint64_t mPos;
  145. bool mUse64;
  146. };
  147. } // namespace renderscript
  148. } // namespace android
  149. #endif //ANDROID_RS_STREAM_H