Alex Sakhartchouk | aa7d288 | 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 | |
Alex Sakhartchouk | ed9f210 | 2010-11-09 17:00:54 -0800 | [diff] [blame] | 29 | IStream::IStream(const uint8_t *buf, bool use64) { |
Alex Sakhartchouk | aa7d288 | 2010-05-21 12:53:13 -0700 | [diff] [blame] | 30 | mData = buf; |
| 31 | mPos = 0; |
| 32 | mUse64 = use64; |
| 33 | } |
| 34 | |
Alex Sakhartchouk | ed9f210 | 2010-11-09 17:00:54 -0800 | [diff] [blame] | 35 | void IStream::loadByteArray(void *dest, size_t numBytes) { |
Alex Sakhartchouk | aa7d288 | 2010-05-21 12:53:13 -0700 | [diff] [blame] | 36 | memcpy(dest, mData + mPos, numBytes); |
| 37 | mPos += numBytes; |
| 38 | } |
| 39 | |
Alex Sakhartchouk | ed9f210 | 2010-11-09 17:00:54 -0800 | [diff] [blame] | 40 | uint64_t IStream::loadOffset() { |
Alex Sakhartchouk | aa7d288 | 2010-05-21 12:53:13 -0700 | [diff] [blame] | 41 | uint64_t tmp; |
| 42 | if (mUse64) { |
| 43 | mPos = (mPos + 7) & (~7); |
| 44 | tmp = reinterpret_cast<const uint64_t *>(&mData[mPos])[0]; |
| 45 | mPos += sizeof(uint64_t); |
| 46 | return tmp; |
| 47 | } |
| 48 | return loadU32(); |
| 49 | } |
| 50 | |
Alex Sakhartchouk | ed9f210 | 2010-11-09 17:00:54 -0800 | [diff] [blame] | 51 | void IStream::loadString(String8 *s) { |
Alex Sakhartchouk | aa7d288 | 2010-05-21 12:53:13 -0700 | [diff] [blame] | 52 | uint32_t len = loadU32(); |
Alex Sakhartchouk | aa7d288 | 2010-05-21 12:53:13 -0700 | [diff] [blame] | 53 | s->setTo((const char *)&mData[mPos], len); |
Alex Sakhartchouk | aa7d288 | 2010-05-21 12:53:13 -0700 | [diff] [blame] | 54 | mPos += len; |
| 55 | } |
| 56 | |
Alex Sakhartchouk | aa7d288 | 2010-05-21 12:53:13 -0700 | [diff] [blame] | 57 | // Output stream implementation |
Alex Sakhartchouk | ed9f210 | 2010-11-09 17:00:54 -0800 | [diff] [blame] | 58 | OStream::OStream(uint64_t len, bool use64) { |
Alex Sakhartchouk | aa7d288 | 2010-05-21 12:53:13 -0700 | [diff] [blame] | 59 | mData = (uint8_t*)malloc(len); |
| 60 | mLength = len; |
| 61 | mPos = 0; |
| 62 | mUse64 = use64; |
| 63 | } |
| 64 | |
Alex Sakhartchouk | ed9f210 | 2010-11-09 17:00:54 -0800 | [diff] [blame] | 65 | OStream::~OStream() { |
Alex Sakhartchouk | aa7d288 | 2010-05-21 12:53:13 -0700 | [diff] [blame] | 66 | free(mData); |
| 67 | } |
| 68 | |
Alex Sakhartchouk | ed9f210 | 2010-11-09 17:00:54 -0800 | [diff] [blame] | 69 | void OStream::addByteArray(const void *src, size_t numBytes) { |
Alex Sakhartchouk | aa7d288 | 2010-05-21 12:53:13 -0700 | [diff] [blame] | 70 | // We need to potentially grow more than once if the number of byes we write is substantial |
Alex Sakhartchouk | ed9f210 | 2010-11-09 17:00:54 -0800 | [diff] [blame] | 71 | while (mPos + numBytes >= mLength) { |
Alex Sakhartchouk | aa7d288 | 2010-05-21 12:53:13 -0700 | [diff] [blame] | 72 | growSize(); |
| 73 | } |
| 74 | memcpy(mData + mPos, src, numBytes); |
| 75 | mPos += numBytes; |
| 76 | } |
| 77 | |
Alex Sakhartchouk | ed9f210 | 2010-11-09 17:00:54 -0800 | [diff] [blame] | 78 | void OStream::addOffset(uint64_t v) { |
Alex Sakhartchouk | aa7d288 | 2010-05-21 12:53:13 -0700 | [diff] [blame] | 79 | if (mUse64) { |
| 80 | mPos = (mPos + 7) & (~7); |
Alex Sakhartchouk | ed9f210 | 2010-11-09 17:00:54 -0800 | [diff] [blame] | 81 | if (mPos + sizeof(v) >= mLength) { |
Alex Sakhartchouk | aa7d288 | 2010-05-21 12:53:13 -0700 | [diff] [blame] | 82 | growSize(); |
| 83 | } |
| 84 | mData[mPos++] = (uint8_t)(v & 0xff); |
| 85 | mData[mPos++] = (uint8_t)((v >> 8) & 0xff); |
| 86 | mData[mPos++] = (uint8_t)((v >> 16) & 0xff); |
| 87 | mData[mPos++] = (uint8_t)((v >> 24) & 0xff); |
| 88 | mData[mPos++] = (uint8_t)((v >> 32) & 0xff); |
| 89 | mData[mPos++] = (uint8_t)((v >> 40) & 0xff); |
| 90 | mData[mPos++] = (uint8_t)((v >> 48) & 0xff); |
| 91 | mData[mPos++] = (uint8_t)((v >> 56) & 0xff); |
Alex Sakhartchouk | ed9f210 | 2010-11-09 17:00:54 -0800 | [diff] [blame] | 92 | } else { |
Alex Sakhartchouk | aa7d288 | 2010-05-21 12:53:13 -0700 | [diff] [blame] | 93 | addU32(v); |
| 94 | } |
| 95 | } |
| 96 | |
Alex Sakhartchouk | ed9f210 | 2010-11-09 17:00:54 -0800 | [diff] [blame] | 97 | void OStream::addString(String8 *s) { |
Alex Sakhartchouk | aa7d288 | 2010-05-21 12:53:13 -0700 | [diff] [blame] | 98 | uint32_t len = s->size(); |
| 99 | addU32(len); |
Alex Sakhartchouk | ed9f210 | 2010-11-09 17:00:54 -0800 | [diff] [blame] | 100 | if (mPos + len*sizeof(char) >= mLength) { |
Alex Sakhartchouk | aa7d288 | 2010-05-21 12:53:13 -0700 | [diff] [blame] | 101 | growSize(); |
| 102 | } |
| 103 | char *stringData = reinterpret_cast<char *>(&mData[mPos]); |
Alex Sakhartchouk | ed9f210 | 2010-11-09 17:00:54 -0800 | [diff] [blame] | 104 | for (uint32_t i = 0; i < len; i ++) { |
Alex Sakhartchouk | aa7d288 | 2010-05-21 12:53:13 -0700 | [diff] [blame] | 105 | stringData[i] = s->string()[i]; |
| 106 | } |
| 107 | mPos += len*sizeof(char); |
| 108 | } |
| 109 | |
Alex Sakhartchouk | ed9f210 | 2010-11-09 17:00:54 -0800 | [diff] [blame] | 110 | void OStream::growSize() { |
Alex Sakhartchouk | aa7d288 | 2010-05-21 12:53:13 -0700 | [diff] [blame] | 111 | uint8_t *newData = (uint8_t*)malloc(mLength*2); |
| 112 | memcpy(newData, mData, mLength*sizeof(uint8_t)); |
| 113 | mLength = mLength * 2; |
| 114 | free(mData); |
| 115 | mData = newData; |
| 116 | } |
| 117 | |
| 118 | |