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