blob: 49ed567dcecab80ade40e8fc166a9045a7872a67 [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
18#ifndef ANDROID_RS_BUILD_FOR_HOST
19#include "rsContext.h"
20#else
21#include "rsContextHostStub.h"
22#endif
23
24#include "rsStream.h"
25
26using namespace android;
27using namespace android::renderscript;
28
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -080029IStream::IStream(const uint8_t *buf, bool use64) {
Alex Sakhartchoukfb6b6142010-05-21 12:53:13 -070030 mData = buf;
31 mPos = 0;
32 mUse64 = use64;
33}
34
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -080035void IStream::loadByteArray(void *dest, size_t numBytes) {
Alex Sakhartchoukfb6b6142010-05-21 12:53:13 -070036 memcpy(dest, mData + mPos, numBytes);
37 mPos += numBytes;
38}
39
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -080040uint64_t IStream::loadOffset() {
Alex Sakhartchoukfb6b6142010-05-21 12:53:13 -070041 uint64_t tmp;
42 if (mUse64) {
43 mPos = (mPos + 7) & (~7);
44 tmp = reinterpret_cast<const uint64_t *>(&mData[mPos])[0];
45 mPos += sizeof(uint64_t);
46 return tmp;
47 }
48 return loadU32();
49}
50
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -080051void IStream::loadString(String8 *s) {
Alex Sakhartchoukfb6b6142010-05-21 12:53:13 -070052 uint32_t len = loadU32();
Alex Sakhartchoukfb6b6142010-05-21 12:53:13 -070053 s->setTo((const char *)&mData[mPos], len);
Alex Sakhartchoukfb6b6142010-05-21 12:53:13 -070054 mPos += len;
55}
56
Alex Sakhartchoukfb6b6142010-05-21 12:53:13 -070057// Output stream implementation
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -080058OStream::OStream(uint64_t len, bool use64) {
Alex Sakhartchoukfb6b6142010-05-21 12:53:13 -070059 mData = (uint8_t*)malloc(len);
60 mLength = len;
61 mPos = 0;
62 mUse64 = use64;
63}
64
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -080065OStream::~OStream() {
Alex Sakhartchoukfb6b6142010-05-21 12:53:13 -070066 free(mData);
67}
68
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -080069void OStream::addByteArray(const void *src, size_t numBytes) {
Alex Sakhartchoukfb6b6142010-05-21 12:53:13 -070070 // We need to potentially grow more than once if the number of byes we write is substantial
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -080071 while (mPos + numBytes >= mLength) {
Alex Sakhartchoukfb6b6142010-05-21 12:53:13 -070072 growSize();
73 }
74 memcpy(mData + mPos, src, numBytes);
75 mPos += numBytes;
76}
77
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -080078void OStream::addOffset(uint64_t v) {
Alex Sakhartchoukfb6b6142010-05-21 12:53:13 -070079 if (mUse64) {
80 mPos = (mPos + 7) & (~7);
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -080081 if (mPos + sizeof(v) >= mLength) {
Alex Sakhartchoukfb6b6142010-05-21 12:53:13 -070082 growSize();
83 }
84 mData[mPos++] = (uint8_t)(v & 0xff);
85 mData[mPos++] = (uint8_t)((v >> 8) & 0xff);
86 mData[mPos++] = (uint8_t)((v >> 16) & 0xff);
87 mData[mPos++] = (uint8_t)((v >> 24) & 0xff);
88 mData[mPos++] = (uint8_t)((v >> 32) & 0xff);
89 mData[mPos++] = (uint8_t)((v >> 40) & 0xff);
90 mData[mPos++] = (uint8_t)((v >> 48) & 0xff);
91 mData[mPos++] = (uint8_t)((v >> 56) & 0xff);
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -080092 } else {
Alex Sakhartchoukfb6b6142010-05-21 12:53:13 -070093 addU32(v);
94 }
95}
96
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -080097void OStream::addString(String8 *s) {
Alex Sakhartchoukfb6b6142010-05-21 12:53:13 -070098 uint32_t len = s->size();
99 addU32(len);
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800100 if (mPos + len*sizeof(char) >= mLength) {
Alex Sakhartchoukfb6b6142010-05-21 12:53:13 -0700101 growSize();
102 }
103 char *stringData = reinterpret_cast<char *>(&mData[mPos]);
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800104 for (uint32_t i = 0; i < len; i ++) {
Alex Sakhartchoukfb6b6142010-05-21 12:53:13 -0700105 stringData[i] = s->string()[i];
106 }
107 mPos += len*sizeof(char);
108}
109
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800110void OStream::growSize() {
Alex Sakhartchoukfb6b6142010-05-21 12:53:13 -0700111 uint8_t *newData = (uint8_t*)malloc(mLength*2);
112 memcpy(newData, mData, mLength*sizeof(uint8_t));
113 mLength = mLength * 2;
114 free(mData);
115 mData = newData;
116}
117
118