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 <stdio.h>
21 #include "rsUtils.h"
22 
23 // ---------------------------------------------------------------------------
24 namespace android {
25 namespace renderscript {
26 
27 class IStream {
28 public:
29     IStream(const uint8_t *, bool use64);
30 
loadF()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     }
loadI32()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     }
loadU32()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     }
loadU16()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     }
loadU8()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     const char * loadString();
getPos()63     uint64_t getPos() const {
64         return mPos;
65     }
reset(uint64_t pos)66     void reset(uint64_t pos) {
67         mPos = pos;
68     }
reset()69     void reset() {
70         mPos = 0;
71     }
72 
getPtr()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 
82 class OStream {
83 public:
84     OStream(uint64_t length, bool use64);
85     ~OStream();
86 
align(uint32_t bytes)87     void align(uint32_t bytes) {
88         mPos = (mPos + (bytes - 1)) & (~(bytes - 1));
89         if (mPos >= mLength) {
90             growSize();
91         }
92     }
93 
addF(float v)94     void addF(float v) {
95         uint32_t uintV = *reinterpret_cast<uint32_t*> (&v);
96         addU32(uintV);
97     }
addI32(int32_t v)98     void addI32(int32_t v) {
99         mPos = (mPos + 3) & (~3);
100         if (mPos + sizeof(v) >= mLength) {
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     }
addU32(uint32_t v)108     void addU32(uint32_t v) {
109         mPos = (mPos + 3) & (~3);
110         if (mPos + sizeof(v) >= mLength) {
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     }
addU16(uint16_t v)118     void addU16(uint16_t v) {
119         mPos = (mPos + 1) & (~1);
120         if (mPos + sizeof(v) >= mLength) {
121             growSize();
122         }
123         mData[mPos++] = (uint8_t)(v & 0xff);
124         mData[mPos++] = (uint8_t)(v >> 8);
125     }
addU8(uint8_t v)126     inline void addU8(uint8_t v) {
127         if (mPos + 1 >= mLength) {
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(const char *name);
136     void addString(const char *name, size_t len);
getPos()137     uint64_t getPos() const {
138         return mPos;
139     }
reset(uint64_t pos)140     void reset(uint64_t pos) {
141         mPos = pos;
142     }
reset()143     void reset() {
144         mPos = 0;
145     }
getPtr()146     const uint8_t * getPtr() const {
147         return mData;
148     }
149 protected:
150     void growSize();
151     uint8_t * mData;
152     uint64_t mLength;
153     uint64_t mPos;
154     bool mUse64;
155 };
156 
157 
158 } // renderscript
159 } // android
160 #endif //ANDROID_RS_STREAM_H
161 
162 
163