Alex Sakhartchouk | fb6b614 | 2010-05-21 12:53:13 -0700 | [diff] [blame] | 1 | |
| 2 | /* |
| 3 | * Copyright (C) 2009 The Android Open Source Project |
| 4 | * |
| 5 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | * you may not use this file except in compliance with the License. |
| 7 | * You may obtain a copy of the License at |
| 8 | * |
| 9 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | * |
| 11 | * Unless required by applicable law or agreed to in writing, software |
| 12 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | * See the License for the specific language governing permissions and |
| 15 | * limitations under the License. |
| 16 | */ |
| 17 | |
| 18 | #ifndef ANDROID_RS_BUILD_FOR_HOST |
| 19 | #include "rsContext.h" |
| 20 | #else |
| 21 | #include "rsContextHostStub.h" |
| 22 | #endif |
| 23 | |
| 24 | #include "rsStream.h" |
| 25 | |
| 26 | using namespace android; |
| 27 | using namespace android::renderscript; |
| 28 | |
| 29 | IStream::IStream(const uint8_t *buf, bool use64) |
| 30 | { |
| 31 | mData = buf; |
| 32 | mPos = 0; |
| 33 | mUse64 = use64; |
| 34 | } |
| 35 | |
| 36 | void IStream::loadByteArray(void *dest, size_t numBytes) |
| 37 | { |
| 38 | memcpy(dest, mData + mPos, numBytes); |
| 39 | mPos += numBytes; |
| 40 | } |
| 41 | |
| 42 | uint64_t IStream::loadOffset() |
| 43 | { |
| 44 | uint64_t tmp; |
| 45 | if (mUse64) { |
| 46 | mPos = (mPos + 7) & (~7); |
| 47 | tmp = reinterpret_cast<const uint64_t *>(&mData[mPos])[0]; |
| 48 | mPos += sizeof(uint64_t); |
| 49 | return tmp; |
| 50 | } |
| 51 | return loadU32(); |
| 52 | } |
| 53 | |
| 54 | void IStream::loadString(String8 *s) |
| 55 | { |
Alex Sakhartchouk | fb6b614 | 2010-05-21 12:53:13 -0700 | [diff] [blame] | 56 | uint32_t len = loadU32(); |
Alex Sakhartchouk | fb6b614 | 2010-05-21 12:53:13 -0700 | [diff] [blame] | 57 | s->setTo((const char *)&mData[mPos], len); |
Alex Sakhartchouk | fb6b614 | 2010-05-21 12:53:13 -0700 | [diff] [blame] | 58 | mPos += len; |
| 59 | } |
| 60 | |
| 61 | |
| 62 | // Output stream implementation |
| 63 | |
| 64 | OStream::OStream(uint64_t len, bool use64) |
| 65 | { |
| 66 | mData = (uint8_t*)malloc(len); |
| 67 | mLength = len; |
| 68 | mPos = 0; |
| 69 | mUse64 = use64; |
| 70 | } |
| 71 | |
| 72 | OStream::~OStream() |
| 73 | { |
| 74 | free(mData); |
| 75 | } |
| 76 | |
| 77 | void OStream::addByteArray(const void *src, size_t numBytes) |
| 78 | { |
| 79 | // We need to potentially grow more than once if the number of byes we write is substantial |
| 80 | while(mPos + numBytes >= mLength) { |
| 81 | growSize(); |
| 82 | } |
| 83 | memcpy(mData + mPos, src, numBytes); |
| 84 | mPos += numBytes; |
| 85 | } |
| 86 | |
| 87 | void OStream::addOffset(uint64_t v) |
| 88 | { |
| 89 | if (mUse64) { |
| 90 | mPos = (mPos + 7) & (~7); |
| 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 | mData[mPos++] = (uint8_t)((v >> 32) & 0xff); |
| 99 | mData[mPos++] = (uint8_t)((v >> 40) & 0xff); |
| 100 | mData[mPos++] = (uint8_t)((v >> 48) & 0xff); |
| 101 | mData[mPos++] = (uint8_t)((v >> 56) & 0xff); |
| 102 | } |
| 103 | else { |
| 104 | addU32(v); |
| 105 | } |
| 106 | } |
| 107 | |
| 108 | void OStream::addString(String8 *s) |
| 109 | { |
| 110 | uint32_t len = s->size(); |
| 111 | addU32(len); |
| 112 | if(mPos + len*sizeof(char) >= mLength) { |
| 113 | growSize(); |
| 114 | } |
| 115 | char *stringData = reinterpret_cast<char *>(&mData[mPos]); |
| 116 | for(uint32_t i = 0; i < len; i ++) { |
| 117 | stringData[i] = s->string()[i]; |
| 118 | } |
| 119 | mPos += len*sizeof(char); |
| 120 | } |
| 121 | |
| 122 | void OStream::growSize() |
| 123 | { |
| 124 | uint8_t *newData = (uint8_t*)malloc(mLength*2); |
| 125 | memcpy(newData, mData, mLength*sizeof(uint8_t)); |
| 126 | mLength = mLength * 2; |
| 127 | free(mData); |
| 128 | mData = newData; |
| 129 | } |
| 130 | |
| 131 | |