Alex Sakhartchouk | fb6b614 | 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 | |
Alex Sakhartchouk | fb6b614 | 2010-05-21 12:53:13 -0700 | [diff] [blame] | 20 | #include <stdio.h> |
Tim Murray | 0b575de | 2013-03-15 15:56:43 -0700 | [diff] [blame] | 21 | #include "rsUtils.h" |
Alex Sakhartchouk | fb6b614 | 2010-05-21 12:53:13 -0700 | [diff] [blame] | 22 | |
| 23 | // --------------------------------------------------------------------------- |
| 24 | namespace android { |
| 25 | namespace renderscript { |
| 26 | |
Alex Sakhartchouk | afb743a | 2010-11-09 17:00:54 -0800 | [diff] [blame] | 27 | class IStream { |
Alex Sakhartchouk | fb6b614 | 2010-05-21 12:53:13 -0700 | [diff] [blame] | 28 | public: |
| 29 | IStream(const uint8_t *, bool use64); |
| 30 | |
| 31 | float loadF() { |
| 32 | mPos = (mPos + 3) & (~3); |
| 33 | float tmp = reinterpret_cast<const float *>(&mData[mPos])[0]; |
| 34 | mPos += sizeof(float); |
| 35 | return tmp; |
| 36 | } |
| 37 | int32_t loadI32() { |
| 38 | mPos = (mPos + 3) & (~3); |
| 39 | int32_t tmp = reinterpret_cast<const int32_t *>(&mData[mPos])[0]; |
| 40 | mPos += sizeof(int32_t); |
| 41 | return tmp; |
| 42 | } |
| 43 | uint32_t loadU32() { |
| 44 | mPos = (mPos + 3) & (~3); |
| 45 | uint32_t tmp = reinterpret_cast<const uint32_t *>(&mData[mPos])[0]; |
| 46 | mPos += sizeof(uint32_t); |
| 47 | return tmp; |
| 48 | } |
| 49 | uint16_t loadU16() { |
| 50 | mPos = (mPos + 1) & (~1); |
| 51 | uint16_t tmp = reinterpret_cast<const uint16_t *>(&mData[mPos])[0]; |
| 52 | mPos += sizeof(uint16_t); |
| 53 | return tmp; |
| 54 | } |
| 55 | inline uint8_t loadU8() { |
| 56 | uint8_t tmp = reinterpret_cast<const uint8_t *>(&mData[mPos])[0]; |
| 57 | mPos += sizeof(uint8_t); |
| 58 | return tmp; |
| 59 | } |
| 60 | void loadByteArray(void *dest, size_t numBytes); |
| 61 | uint64_t loadOffset(); |
| 62 | void loadString(String8 *s); |
| 63 | uint64_t getPos() const { |
| 64 | return mPos; |
| 65 | } |
| 66 | void reset(uint64_t pos) { |
| 67 | mPos = pos; |
| 68 | } |
| 69 | void reset() { |
| 70 | mPos = 0; |
| 71 | } |
Alex Sakhartchouk | afb743a | 2010-11-09 17:00:54 -0800 | [diff] [blame] | 72 | |
Alex Sakhartchouk | fb6b614 | 2010-05-21 12:53:13 -0700 | [diff] [blame] | 73 | const uint8_t * getPtr() const { |
| 74 | return mData; |
| 75 | } |
| 76 | protected: |
| 77 | const uint8_t * mData; |
| 78 | uint64_t mPos; |
| 79 | bool mUse64; |
| 80 | }; |
| 81 | |
Alex Sakhartchouk | afb743a | 2010-11-09 17:00:54 -0800 | [diff] [blame] | 82 | class OStream { |
Alex Sakhartchouk | fb6b614 | 2010-05-21 12:53:13 -0700 | [diff] [blame] | 83 | public: |
| 84 | OStream(uint64_t length, bool use64); |
| 85 | ~OStream(); |
Alex Sakhartchouk | afb743a | 2010-11-09 17:00:54 -0800 | [diff] [blame] | 86 | |
Alex Sakhartchouk | b825f67 | 2010-06-04 10:06:50 -0700 | [diff] [blame] | 87 | void align(uint32_t bytes) { |
| 88 | mPos = (mPos + (bytes - 1)) & (~(bytes - 1)); |
Alex Sakhartchouk | afb743a | 2010-11-09 17:00:54 -0800 | [diff] [blame] | 89 | if (mPos >= mLength) { |
Alex Sakhartchouk | b825f67 | 2010-06-04 10:06:50 -0700 | [diff] [blame] | 90 | growSize(); |
| 91 | } |
| 92 | } |
Alex Sakhartchouk | afb743a | 2010-11-09 17:00:54 -0800 | [diff] [blame] | 93 | |
Alex Sakhartchouk | fb6b614 | 2010-05-21 12:53:13 -0700 | [diff] [blame] | 94 | void addF(float v) { |
| 95 | uint32_t uintV = *reinterpret_cast<uint32_t*> (&v); |
| 96 | addU32(uintV); |
| 97 | } |
| 98 | void addI32(int32_t v) { |
| 99 | mPos = (mPos + 3) & (~3); |
Alex Sakhartchouk | afb743a | 2010-11-09 17:00:54 -0800 | [diff] [blame] | 100 | if (mPos + sizeof(v) >= mLength) { |
Alex Sakhartchouk | fb6b614 | 2010-05-21 12:53:13 -0700 | [diff] [blame] | 101 | growSize(); |
| 102 | } |
| 103 | mData[mPos++] = (uint8_t)(v & 0xff); |
| 104 | mData[mPos++] = (uint8_t)((v >> 8) & 0xff); |
| 105 | mData[mPos++] = (uint8_t)((v >> 16) & 0xff); |
| 106 | mData[mPos++] = (uint8_t)((v >> 24) & 0xff); |
| 107 | } |
| 108 | void addU32(uint32_t v) { |
| 109 | mPos = (mPos + 3) & (~3); |
Alex Sakhartchouk | afb743a | 2010-11-09 17:00:54 -0800 | [diff] [blame] | 110 | if (mPos + sizeof(v) >= mLength) { |
Alex Sakhartchouk | fb6b614 | 2010-05-21 12:53:13 -0700 | [diff] [blame] | 111 | growSize(); |
| 112 | } |
| 113 | mData[mPos++] = (uint8_t)(v & 0xff); |
| 114 | mData[mPos++] = (uint8_t)((v >> 8) & 0xff); |
| 115 | mData[mPos++] = (uint8_t)((v >> 16) & 0xff); |
| 116 | mData[mPos++] = (uint8_t)((v >> 24) & 0xff); |
| 117 | } |
| 118 | void addU16(uint16_t v) { |
| 119 | mPos = (mPos + 1) & (~1); |
Alex Sakhartchouk | afb743a | 2010-11-09 17:00:54 -0800 | [diff] [blame] | 120 | if (mPos + sizeof(v) >= mLength) { |
Alex Sakhartchouk | fb6b614 | 2010-05-21 12:53:13 -0700 | [diff] [blame] | 121 | growSize(); |
| 122 | } |
| 123 | mData[mPos++] = (uint8_t)(v & 0xff); |
| 124 | mData[mPos++] = (uint8_t)(v >> 8); |
| 125 | } |
| 126 | inline void addU8(uint8_t v) { |
Alex Sakhartchouk | afb743a | 2010-11-09 17:00:54 -0800 | [diff] [blame] | 127 | if (mPos + 1 >= mLength) { |
Alex Sakhartchouk | fb6b614 | 2010-05-21 12:53:13 -0700 | [diff] [blame] | 128 | growSize(); |
| 129 | } |
| 130 | reinterpret_cast<uint8_t *>(&mData[mPos])[0] = v; |
| 131 | mPos ++; |
| 132 | } |
| 133 | void addByteArray(const void *src, size_t numBytes); |
| 134 | void addOffset(uint64_t v); |
| 135 | void addString(String8 *s); |
| 136 | uint64_t getPos() const { |
| 137 | return mPos; |
| 138 | } |
| 139 | void reset(uint64_t pos) { |
| 140 | mPos = pos; |
| 141 | } |
| 142 | void reset() { |
| 143 | mPos = 0; |
| 144 | } |
| 145 | const uint8_t * getPtr() const { |
| 146 | return mData; |
| 147 | } |
| 148 | protected: |
| 149 | void growSize(); |
| 150 | uint8_t * mData; |
| 151 | uint64_t mLength; |
| 152 | uint64_t mPos; |
| 153 | bool mUse64; |
| 154 | }; |
Alex Sakhartchouk | afb743a | 2010-11-09 17:00:54 -0800 | [diff] [blame] | 155 | |
Alex Sakhartchouk | fb6b614 | 2010-05-21 12:53:13 -0700 | [diff] [blame] | 156 | |
| 157 | } // renderscript |
| 158 | } // android |
| 159 | #endif //ANDROID_RS_STREAM_H |
| 160 | |
| 161 | |