rsStream.cpp revision fb6b614bcea88a587a7ea4530be45ff0ffa0210e
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{ 31 mData = buf; 32 mPos = 0; 33 mUse64 = use64; 34} 35 36void IStream::loadByteArray(void *dest, size_t numBytes) 37{ 38 memcpy(dest, mData + mPos, numBytes); 39 mPos += numBytes; 40} 41 42uint64_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 54void IStream::loadString(String8 *s) 55{ 56 LOGE("loadString"); 57 uint32_t len = loadU32(); 58 LOGE("loadString len %i", len); 59 s->setTo((const char *)&mData[mPos], len); 60 mPos += len; 61} 62 63 64// Output stream implementation 65 66OStream::OStream(uint64_t len, bool use64) 67{ 68 mData = (uint8_t*)malloc(len); 69 mLength = len; 70 mPos = 0; 71 mUse64 = use64; 72} 73 74OStream::~OStream() 75{ 76 free(mData); 77} 78 79void OStream::addByteArray(const void *src, size_t numBytes) 80{ 81 // We need to potentially grow more than once if the number of byes we write is substantial 82 while(mPos + numBytes >= mLength) { 83 growSize(); 84 } 85 memcpy(mData + mPos, src, numBytes); 86 mPos += numBytes; 87} 88 89void OStream::addOffset(uint64_t v) 90{ 91 if (mUse64) { 92 mPos = (mPos + 7) & (~7); 93 if(mPos + sizeof(v) >= mLength) { 94 growSize(); 95 } 96 mData[mPos++] = (uint8_t)(v & 0xff); 97 mData[mPos++] = (uint8_t)((v >> 8) & 0xff); 98 mData[mPos++] = (uint8_t)((v >> 16) & 0xff); 99 mData[mPos++] = (uint8_t)((v >> 24) & 0xff); 100 mData[mPos++] = (uint8_t)((v >> 32) & 0xff); 101 mData[mPos++] = (uint8_t)((v >> 40) & 0xff); 102 mData[mPos++] = (uint8_t)((v >> 48) & 0xff); 103 mData[mPos++] = (uint8_t)((v >> 56) & 0xff); 104 } 105 else { 106 addU32(v); 107 } 108} 109 110void OStream::addString(String8 *s) 111{ 112 uint32_t len = s->size(); 113 addU32(len); 114 if(mPos + len*sizeof(char) >= mLength) { 115 growSize(); 116 } 117 char *stringData = reinterpret_cast<char *>(&mData[mPos]); 118 for(uint32_t i = 0; i < len; i ++) { 119 stringData[i] = s->string()[i]; 120 } 121 mPos += len*sizeof(char); 122} 123 124void OStream::growSize() 125{ 126 uint8_t *newData = (uint8_t*)malloc(mLength*2); 127 memcpy(newData, mData, mLength*sizeof(uint8_t)); 128 mLength = mLength * 2; 129 free(mData); 130 mData = newData; 131} 132 133 134