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