rsStream.h revision fb6b614bcea88a587a7ea4530be45ff0ffa0210e
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// ---------------------------------------------------------------------------
24namespace android {
25namespace renderscript {
26
27class IStream
28{
29public:
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    }
77protected:
78    const uint8_t * mData;
79    uint64_t mPos;
80    bool mUse64;
81};
82
83class OStream
84{
85public:
86    OStream(uint64_t length, bool use64);
87    ~OStream();
88
89    void addF(float v) {
90        uint32_t uintV = *reinterpret_cast<uint32_t*> (&v);
91        addU32(uintV);
92    }
93    void addI32(int32_t v) {
94        mPos = (mPos + 3) & (~3);
95        if(mPos + sizeof(v) >= mLength) {
96            growSize();
97        }
98        mData[mPos++] = (uint8_t)(v & 0xff);
99        mData[mPos++] = (uint8_t)((v >> 8) & 0xff);
100        mData[mPos++] = (uint8_t)((v >> 16) & 0xff);
101        mData[mPos++] = (uint8_t)((v >> 24) & 0xff);
102    }
103    void addU32(uint32_t v) {
104        mPos = (mPos + 3) & (~3);
105        if(mPos + sizeof(v) >= mLength) {
106            growSize();
107        }
108        mData[mPos++] = (uint8_t)(v & 0xff);
109        mData[mPos++] = (uint8_t)((v >> 8) & 0xff);
110        mData[mPos++] = (uint8_t)((v >> 16) & 0xff);
111        mData[mPos++] = (uint8_t)((v >> 24) & 0xff);
112    }
113    void addU16(uint16_t v) {
114        mPos = (mPos + 1) & (~1);
115        if(mPos + sizeof(v) >= mLength) {
116            growSize();
117        }
118        mData[mPos++] = (uint8_t)(v & 0xff);
119        mData[mPos++] = (uint8_t)(v >> 8);
120    }
121    inline void addU8(uint8_t v) {
122        if(mPos + 1 >= mLength) {
123            growSize();
124        }
125        reinterpret_cast<uint8_t *>(&mData[mPos])[0] = v;
126        mPos ++;
127    }
128    void addByteArray(const void *src, size_t numBytes);
129    void addOffset(uint64_t v);
130    void addString(String8 *s);
131    uint64_t getPos() const {
132        return mPos;
133    }
134    void reset(uint64_t pos) {
135        mPos = pos;
136    }
137    void reset() {
138        mPos = 0;
139    }
140    const uint8_t * getPtr() const {
141        return mData;
142    }
143protected:
144    void growSize();
145    uint8_t * mData;
146    uint64_t mLength;
147    uint64_t mPos;
148    bool mUse64;
149};
150
151
152} // renderscript
153} // android
154#endif //ANDROID_RS_STREAM_H
155
156
157