Alex Sakhartchouk | aa7d288 | 2010-05-21 12:53:13 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2009 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | #ifndef ANDROID_RS_STREAM_H |
| 18 | #define ANDROID_RS_STREAM_H |
| 19 | |
| 20 | #include <utils/String8.h> |
| 21 | #include <stdio.h> |
| 22 | |
| 23 | // --------------------------------------------------------------------------- |
| 24 | namespace android { |
| 25 | namespace renderscript { |
| 26 | |
| 27 | class IStream |
| 28 | { |
| 29 | public: |
| 30 | IStream(const uint8_t *, bool use64); |
| 31 | |
| 32 | float loadF() { |
| 33 | mPos = (mPos + 3) & (~3); |
| 34 | float tmp = reinterpret_cast<const float *>(&mData[mPos])[0]; |
| 35 | mPos += sizeof(float); |
| 36 | return tmp; |
| 37 | } |
| 38 | int32_t loadI32() { |
| 39 | mPos = (mPos + 3) & (~3); |
| 40 | int32_t tmp = reinterpret_cast<const int32_t *>(&mData[mPos])[0]; |
| 41 | mPos += sizeof(int32_t); |
| 42 | return tmp; |
| 43 | } |
| 44 | uint32_t loadU32() { |
| 45 | mPos = (mPos + 3) & (~3); |
| 46 | uint32_t tmp = reinterpret_cast<const uint32_t *>(&mData[mPos])[0]; |
| 47 | mPos += sizeof(uint32_t); |
| 48 | return tmp; |
| 49 | } |
| 50 | uint16_t loadU16() { |
| 51 | mPos = (mPos + 1) & (~1); |
| 52 | uint16_t tmp = reinterpret_cast<const uint16_t *>(&mData[mPos])[0]; |
| 53 | mPos += sizeof(uint16_t); |
| 54 | return tmp; |
| 55 | } |
| 56 | inline uint8_t loadU8() { |
| 57 | uint8_t tmp = reinterpret_cast<const uint8_t *>(&mData[mPos])[0]; |
| 58 | mPos += sizeof(uint8_t); |
| 59 | return tmp; |
| 60 | } |
| 61 | void loadByteArray(void *dest, size_t numBytes); |
| 62 | uint64_t loadOffset(); |
| 63 | void loadString(String8 *s); |
| 64 | uint64_t getPos() const { |
| 65 | return mPos; |
| 66 | } |
| 67 | void reset(uint64_t pos) { |
| 68 | mPos = pos; |
| 69 | } |
| 70 | void reset() { |
| 71 | mPos = 0; |
| 72 | } |
| 73 | |
| 74 | const uint8_t * getPtr() const { |
| 75 | return mData; |
| 76 | } |
| 77 | protected: |
| 78 | const uint8_t * mData; |
| 79 | uint64_t mPos; |
| 80 | bool mUse64; |
| 81 | }; |
| 82 | |
| 83 | class OStream |
| 84 | { |
| 85 | public: |
| 86 | OStream(uint64_t length, bool use64); |
| 87 | ~OStream(); |
| 88 | |
Alex Sakhartchouk | aae74ad | 2010-06-04 10:06:50 -0700 | [diff] [blame^] | 89 | void align(uint32_t bytes) { |
| 90 | mPos = (mPos + (bytes - 1)) & (~(bytes - 1)); |
| 91 | if(mPos >= mLength) { |
| 92 | growSize(); |
| 93 | } |
| 94 | } |
| 95 | |
Alex Sakhartchouk | aa7d288 | 2010-05-21 12:53:13 -0700 | [diff] [blame] | 96 | void addF(float v) { |
| 97 | uint32_t uintV = *reinterpret_cast<uint32_t*> (&v); |
| 98 | addU32(uintV); |
| 99 | } |
| 100 | void addI32(int32_t v) { |
| 101 | mPos = (mPos + 3) & (~3); |
| 102 | if(mPos + sizeof(v) >= mLength) { |
| 103 | growSize(); |
| 104 | } |
| 105 | mData[mPos++] = (uint8_t)(v & 0xff); |
| 106 | mData[mPos++] = (uint8_t)((v >> 8) & 0xff); |
| 107 | mData[mPos++] = (uint8_t)((v >> 16) & 0xff); |
| 108 | mData[mPos++] = (uint8_t)((v >> 24) & 0xff); |
| 109 | } |
| 110 | void addU32(uint32_t v) { |
| 111 | mPos = (mPos + 3) & (~3); |
| 112 | if(mPos + sizeof(v) >= mLength) { |
| 113 | growSize(); |
| 114 | } |
| 115 | mData[mPos++] = (uint8_t)(v & 0xff); |
| 116 | mData[mPos++] = (uint8_t)((v >> 8) & 0xff); |
| 117 | mData[mPos++] = (uint8_t)((v >> 16) & 0xff); |
| 118 | mData[mPos++] = (uint8_t)((v >> 24) & 0xff); |
| 119 | } |
| 120 | void addU16(uint16_t v) { |
| 121 | mPos = (mPos + 1) & (~1); |
| 122 | if(mPos + sizeof(v) >= mLength) { |
| 123 | growSize(); |
| 124 | } |
| 125 | mData[mPos++] = (uint8_t)(v & 0xff); |
| 126 | mData[mPos++] = (uint8_t)(v >> 8); |
| 127 | } |
| 128 | inline void addU8(uint8_t v) { |
| 129 | if(mPos + 1 >= mLength) { |
| 130 | growSize(); |
| 131 | } |
| 132 | reinterpret_cast<uint8_t *>(&mData[mPos])[0] = v; |
| 133 | mPos ++; |
| 134 | } |
| 135 | void addByteArray(const void *src, size_t numBytes); |
| 136 | void addOffset(uint64_t v); |
| 137 | void addString(String8 *s); |
| 138 | uint64_t getPos() const { |
| 139 | return mPos; |
| 140 | } |
| 141 | void reset(uint64_t pos) { |
| 142 | mPos = pos; |
| 143 | } |
| 144 | void reset() { |
| 145 | mPos = 0; |
| 146 | } |
| 147 | const uint8_t * getPtr() const { |
| 148 | return mData; |
| 149 | } |
| 150 | protected: |
| 151 | void growSize(); |
| 152 | uint8_t * mData; |
| 153 | uint64_t mLength; |
| 154 | uint64_t mPos; |
| 155 | bool mUse64; |
| 156 | }; |
| 157 | |
| 158 | |
| 159 | } // renderscript |
| 160 | } // android |
| 161 | #endif //ANDROID_RS_STREAM_H |
| 162 | |
| 163 | |