/libs/rs/rsStream.cpp

http://github.com/CyanogenMod/android_frameworks_base · C++ · 113 lines · 81 code · 15 blank · 17 comment · 7 complexity · 425a670d64ed3a927bc523066e7cd906 MD5 · raw file

  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. #include "rsContext.h"
  17. #include "rsStream.h"
  18. using namespace android;
  19. using namespace android::renderscript;
  20. IStream::IStream(const uint8_t *buf, bool use64) {
  21. mData = buf;
  22. mPos = 0;
  23. mUse64 = use64;
  24. }
  25. void IStream::loadByteArray(void *dest, size_t numBytes) {
  26. memcpy(dest, mData + mPos, numBytes);
  27. mPos += numBytes;
  28. }
  29. uint64_t IStream::loadOffset() {
  30. uint64_t tmp;
  31. if (mUse64) {
  32. mPos = (mPos + 7) & (~7);
  33. tmp = reinterpret_cast<const uint64_t *>(&mData[mPos])[0];
  34. mPos += sizeof(uint64_t);
  35. return tmp;
  36. }
  37. return loadU32();
  38. }
  39. void IStream::loadString(String8 *s) {
  40. uint32_t len = loadU32();
  41. s->setTo((const char *)&mData[mPos], len);
  42. mPos += len;
  43. }
  44. // Output stream implementation
  45. OStream::OStream(uint64_t len, bool use64) {
  46. mData = (uint8_t*)malloc(len);
  47. mLength = len;
  48. mPos = 0;
  49. mUse64 = use64;
  50. }
  51. OStream::~OStream() {
  52. free(mData);
  53. }
  54. void OStream::addByteArray(const void *src, size_t numBytes) {
  55. // We need to potentially grow more than once if the number of byes we write is substantial
  56. while (mPos + numBytes >= mLength) {
  57. growSize();
  58. }
  59. memcpy(mData + mPos, src, numBytes);
  60. mPos += numBytes;
  61. }
  62. void OStream::addOffset(uint64_t v) {
  63. if (mUse64) {
  64. mPos = (mPos + 7) & (~7);
  65. if (mPos + sizeof(v) >= mLength) {
  66. growSize();
  67. }
  68. mData[mPos++] = (uint8_t)(v & 0xff);
  69. mData[mPos++] = (uint8_t)((v >> 8) & 0xff);
  70. mData[mPos++] = (uint8_t)((v >> 16) & 0xff);
  71. mData[mPos++] = (uint8_t)((v >> 24) & 0xff);
  72. mData[mPos++] = (uint8_t)((v >> 32) & 0xff);
  73. mData[mPos++] = (uint8_t)((v >> 40) & 0xff);
  74. mData[mPos++] = (uint8_t)((v >> 48) & 0xff);
  75. mData[mPos++] = (uint8_t)((v >> 56) & 0xff);
  76. } else {
  77. addU32(v);
  78. }
  79. }
  80. void OStream::addString(String8 *s) {
  81. uint32_t len = s->size();
  82. addU32(len);
  83. if (mPos + len*sizeof(char) >= mLength) {
  84. growSize();
  85. }
  86. char *stringData = reinterpret_cast<char *>(&mData[mPos]);
  87. for (uint32_t i = 0; i < len; i ++) {
  88. stringData[i] = s->string()[i];
  89. }
  90. mPos += len*sizeof(char);
  91. }
  92. void OStream::growSize() {
  93. uint8_t *newData = (uint8_t*)malloc(mLength*2);
  94. memcpy(newData, mData, mLength*sizeof(uint8_t));
  95. mLength = mLength * 2;
  96. free(mData);
  97. mData = newData;
  98. }