blob: b9df0ccfac6ae822190965ee0bd499a871abe915 [file] [log] [blame]
Alex Sakhartchoukfb6b6142010-05-21 12:53:13 -07001
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
Alex Sakhartchoukfb6b6142010-05-21 12:53:13 -070018#include "rsContext.h"
Alex Sakhartchoukfb6b6142010-05-21 12:53:13 -070019#include "rsStream.h"
20
21using namespace android;
22using namespace android::renderscript;
23
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -080024IStream::IStream(const uint8_t *buf, bool use64) {
Alex Sakhartchoukfb6b6142010-05-21 12:53:13 -070025 mData = buf;
26 mPos = 0;
27 mUse64 = use64;
28}
29
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -080030void IStream::loadByteArray(void *dest, size_t numBytes) {
Alex Sakhartchoukfb6b6142010-05-21 12:53:13 -070031 memcpy(dest, mData + mPos, numBytes);
32 mPos += numBytes;
33}
34
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -080035uint64_t IStream::loadOffset() {
Alex Sakhartchoukfb6b6142010-05-21 12:53:13 -070036 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
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -080046void IStream::loadString(String8 *s) {
Alex Sakhartchoukfb6b6142010-05-21 12:53:13 -070047 uint32_t len = loadU32();
Alex Sakhartchoukfb6b6142010-05-21 12:53:13 -070048 s->setTo((const char *)&mData[mPos], len);
Alex Sakhartchoukfb6b6142010-05-21 12:53:13 -070049 mPos += len;
50}
51
Alex Sakhartchoukfb6b6142010-05-21 12:53:13 -070052// Output stream implementation
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -080053OStream::OStream(uint64_t len, bool use64) {
Alex Sakhartchoukfb6b6142010-05-21 12:53:13 -070054 mData = (uint8_t*)malloc(len);
55 mLength = len;
56 mPos = 0;
57 mUse64 = use64;
58}
59
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -080060OStream::~OStream() {
Alex Sakhartchoukfb6b6142010-05-21 12:53:13 -070061 free(mData);
62}
63
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -080064void OStream::addByteArray(const void *src, size_t numBytes) {
Alex Sakhartchoukfb6b6142010-05-21 12:53:13 -070065 // We need to potentially grow more than once if the number of byes we write is substantial
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -080066 while (mPos + numBytes >= mLength) {
Alex Sakhartchoukfb6b6142010-05-21 12:53:13 -070067 growSize();
68 }
69 memcpy(mData + mPos, src, numBytes);
70 mPos += numBytes;
71}
72
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -080073void OStream::addOffset(uint64_t v) {
Alex Sakhartchoukfb6b6142010-05-21 12:53:13 -070074 if (mUse64) {
75 mPos = (mPos + 7) & (~7);
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -080076 if (mPos + sizeof(v) >= mLength) {
Alex Sakhartchoukfb6b6142010-05-21 12:53:13 -070077 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);
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -080087 } else {
Alex Sakhartchoukfb6b6142010-05-21 12:53:13 -070088 addU32(v);
89 }
90}
91
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -080092void OStream::addString(String8 *s) {
Alex Sakhartchoukfb6b6142010-05-21 12:53:13 -070093 uint32_t len = s->size();
94 addU32(len);
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -080095 if (mPos + len*sizeof(char) >= mLength) {
Alex Sakhartchoukfb6b6142010-05-21 12:53:13 -070096 growSize();
97 }
98 char *stringData = reinterpret_cast<char *>(&mData[mPos]);
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -080099 for (uint32_t i = 0; i < len; i ++) {
Alex Sakhartchoukfb6b6142010-05-21 12:53:13 -0700100 stringData[i] = s->string()[i];
101 }
102 mPos += len*sizeof(char);
103}
104
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800105void OStream::growSize() {
Alex Sakhartchoukfb6b6142010-05-21 12:53:13 -0700106 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