rsStream.cpp revision afb743aca56c18beb7ab924e75cb6e070ef3e55a
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 26using namespace android; 27using namespace android::renderscript; 28 29IStream::IStream(const uint8_t *buf, bool use64) { 30 mData = buf; 31 mPos = 0; 32 mUse64 = use64; 33} 34 35void IStream::loadByteArray(void *dest, size_t numBytes) { 36 memcpy(dest, mData + mPos, numBytes); 37 mPos += numBytes; 38} 39 40uint64_t IStream::loadOffset() { 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 51void IStream::loadString(String8 *s) { 52 uint32_t len = loadU32(); 53 s->setTo((const char *)&mData[mPos], len); 54 mPos += len; 55} 56 57// Output stream implementation 58OStream::OStream(uint64_t len, bool use64) { 59 mData = (uint8_t*)malloc(len); 60 mLength = len; 61 mPos = 0; 62 mUse64 = use64; 63} 64 65OStream::~OStream() { 66 free(mData); 67} 68 69void OStream::addByteArray(const void *src, size_t numBytes) { 70 // We need to potentially grow more than once if the number of byes we write is substantial 71 while (mPos + numBytes >= mLength) { 72 growSize(); 73 } 74 memcpy(mData + mPos, src, numBytes); 75 mPos += numBytes; 76} 77 78void OStream::addOffset(uint64_t v) { 79 if (mUse64) { 80 mPos = (mPos + 7) & (~7); 81 if (mPos + sizeof(v) >= mLength) { 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); 92 } else { 93 addU32(v); 94 } 95} 96 97void OStream::addString(String8 *s) { 98 uint32_t len = s->size(); 99 addU32(len); 100 if (mPos + len*sizeof(char) >= mLength) { 101 growSize(); 102 } 103 char *stringData = reinterpret_cast<char *>(&mData[mPos]); 104 for (uint32_t i = 0; i < len; i ++) { 105 stringData[i] = s->string()[i]; 106 } 107 mPos += len*sizeof(char); 108} 109 110void OStream::growSize() { 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 119