blob: 62bcf941dcbf8c4d8201b21a2795f6be71778a82 [file] [log] [blame]
Alex Sakhartchoukaa7d2882010-05-21 12:53:13 -07001/*
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// ---------------------------------------------------------------------------
24namespace android {
25namespace renderscript {
26
Alex Sakhartchouked9f2102010-11-09 17:00:54 -080027class IStream {
Alex Sakhartchoukaa7d2882010-05-21 12:53:13 -070028public:
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 Sakhartchouked9f2102010-11-09 17:00:54 -080072
Alex Sakhartchoukaa7d2882010-05-21 12:53:13 -070073 const uint8_t * getPtr() const {
74 return mData;
75 }
76protected:
77 const uint8_t * mData;
78 uint64_t mPos;
79 bool mUse64;
80};
81
Alex Sakhartchouked9f2102010-11-09 17:00:54 -080082class OStream {
Alex Sakhartchoukaa7d2882010-05-21 12:53:13 -070083public:
84 OStream(uint64_t length, bool use64);
85 ~OStream();
Alex Sakhartchouked9f2102010-11-09 17:00:54 -080086
Alex Sakhartchoukaae74ad2010-06-04 10:06:50 -070087 void align(uint32_t bytes) {
88 mPos = (mPos + (bytes - 1)) & (~(bytes - 1));
Alex Sakhartchouked9f2102010-11-09 17:00:54 -080089 if (mPos >= mLength) {
Alex Sakhartchoukaae74ad2010-06-04 10:06:50 -070090 growSize();
91 }
92 }
Alex Sakhartchouked9f2102010-11-09 17:00:54 -080093
Alex Sakhartchoukaa7d2882010-05-21 12:53:13 -070094 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 Sakhartchouked9f2102010-11-09 17:00:54 -0800100 if (mPos + sizeof(v) >= mLength) {
Alex Sakhartchoukaa7d2882010-05-21 12:53:13 -0700101 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 Sakhartchouked9f2102010-11-09 17:00:54 -0800110 if (mPos + sizeof(v) >= mLength) {
Alex Sakhartchoukaa7d2882010-05-21 12:53:13 -0700111 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 Sakhartchouked9f2102010-11-09 17:00:54 -0800120 if (mPos + sizeof(v) >= mLength) {
Alex Sakhartchoukaa7d2882010-05-21 12:53:13 -0700121 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 Sakhartchouked9f2102010-11-09 17:00:54 -0800127 if (mPos + 1 >= mLength) {
Alex Sakhartchoukaa7d2882010-05-21 12:53:13 -0700128 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 }
148protected:
149 void growSize();
150 uint8_t * mData;
151 uint64_t mLength;
152 uint64_t mPos;
153 bool mUse64;
154};
Alex Sakhartchouked9f2102010-11-09 17:00:54 -0800155
Alex Sakhartchoukaa7d2882010-05-21 12:53:13 -0700156
157} // renderscript
158} // android
159#endif //ANDROID_RS_STREAM_H
160
161