rsStream.cpp revision 77d9f4bd05b2d2a161f30c12a2248f9c97eaac42
1
2/*
3 * Copyright (C) 2009 The Android Open Source Project
4 *
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
8 *
9 *      http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 */
17
18#include "rsContext.h"
19#include "rsStream.h"
20
21using namespace android;
22using namespace android::renderscript;
23
24IStream::IStream(const uint8_t *buf, bool use64) {
25    mData = buf;
26    mPos = 0;
27    mUse64 = use64;
28}
29
30void IStream::loadByteArray(void *dest, size_t numBytes) {
31    memcpy(dest, mData + mPos, numBytes);
32    mPos += numBytes;
33}
34
35uint64_t IStream::loadOffset() {
36    uint64_t tmp;
37    if (mUse64) {
38        mPos = (mPos + 7) & (~7);
39        tmp = reinterpret_cast<const uint64_t *>(&mData[mPos])[0];
40        mPos += sizeof(uint64_t);
41        return tmp;
42    }
43    return loadU32();
44}
45
46void IStream::loadString(String8 *s) {
47    uint32_t len = loadU32();
48    s->setTo((const char *)&mData[mPos], len);
49    mPos += len;
50}
51
52// Output stream implementation
53OStream::OStream(uint64_t len, bool use64) {
54    mData = (uint8_t*)malloc(len);
55    mLength = len;
56    mPos = 0;
57    mUse64 = use64;
58}
59
60OStream::~OStream() {
61    free(mData);
62}
63
64void OStream::addByteArray(const void *src, size_t numBytes) {
65    // We need to potentially grow more than once if the number of byes we write is substantial
66    while (mPos + numBytes >= mLength) {
67        growSize();
68    }
69    memcpy(mData + mPos, src, numBytes);
70    mPos += numBytes;
71}
72
73void OStream::addOffset(uint64_t v) {
74    if (mUse64) {
75        mPos = (mPos + 7) & (~7);
76        if (mPos + sizeof(v) >= mLength) {
77            growSize();
78        }
79        mData[mPos++] = (uint8_t)(v & 0xff);
80        mData[mPos++] = (uint8_t)((v >> 8) & 0xff);
81        mData[mPos++] = (uint8_t)((v >> 16) & 0xff);
82        mData[mPos++] = (uint8_t)((v >> 24) & 0xff);
83        mData[mPos++] = (uint8_t)((v >> 32) & 0xff);
84        mData[mPos++] = (uint8_t)((v >> 40) & 0xff);
85        mData[mPos++] = (uint8_t)((v >> 48) & 0xff);
86        mData[mPos++] = (uint8_t)((v >> 56) & 0xff);
87    } else {
88        addU32(v);
89    }
90}
91
92void OStream::addString(String8 *s) {
93    uint32_t len = s->size();
94    addU32(len);
95    if (mPos + len*sizeof(char) >= mLength) {
96        growSize();
97    }
98    char *stringData = reinterpret_cast<char *>(&mData[mPos]);
99    for (uint32_t i = 0; i < len; i ++) {
100        stringData[i] = s->string()[i];
101    }
102    mPos += len*sizeof(char);
103}
104
105void OStream::growSize() {
106    uint8_t *newData = (uint8_t*)malloc(mLength*2);
107    memcpy(newData, mData, mLength*sizeof(uint8_t));
108    mLength = mLength * 2;
109    free(mData);
110    mData = newData;
111}
112
113
114