blob: 39874a9b25dc533aa66069435004b122198d695f [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
29IStream::IStream(const uint8_t *buf, bool use64)
30{
31 mData = buf;
32 mPos = 0;
33 mUse64 = use64;
34}
35
36void IStream::loadByteArray(void *dest, size_t numBytes)
37{
38 memcpy(dest, mData + mPos, numBytes);
39 mPos += numBytes;
40}
41
42uint64_t IStream::loadOffset()
43{
44 uint64_t tmp;
45 if (mUse64) {
46 mPos = (mPos + 7) & (~7);
47 tmp = reinterpret_cast<const uint64_t *>(&mData[mPos])[0];
48 mPos += sizeof(uint64_t);
49 return tmp;
50 }
51 return loadU32();
52}
53
54void IStream::loadString(String8 *s)
55{
Alex Sakhartchoukfb6b6142010-05-21 12:53:13 -070056 uint32_t len = loadU32();
Alex Sakhartchoukfb6b6142010-05-21 12:53:13 -070057 s->setTo((const char *)&mData[mPos], len);
Alex Sakhartchoukb825f672010-06-04 10:06:50 -070058 LOGE("loadString %s", s->string());
Alex Sakhartchoukfb6b6142010-05-21 12:53:13 -070059 mPos += len;
60}
61
62
63// Output stream implementation
64
65OStream::OStream(uint64_t len, bool use64)
66{
67 mData = (uint8_t*)malloc(len);
68 mLength = len;
69 mPos = 0;
70 mUse64 = use64;
71}
72
73OStream::~OStream()
74{
75 free(mData);
76}
77
78void OStream::addByteArray(const void *src, size_t numBytes)
79{
80 // We need to potentially grow more than once if the number of byes we write is substantial
81 while(mPos + numBytes >= mLength) {
82 growSize();
83 }
84 memcpy(mData + mPos, src, numBytes);
85 mPos += numBytes;
86}
87
88void OStream::addOffset(uint64_t v)
89{
90 if (mUse64) {
91 mPos = (mPos + 7) & (~7);
92 if(mPos + sizeof(v) >= mLength) {
93 growSize();
94 }
95 mData[mPos++] = (uint8_t)(v & 0xff);
96 mData[mPos++] = (uint8_t)((v >> 8) & 0xff);
97 mData[mPos++] = (uint8_t)((v >> 16) & 0xff);
98 mData[mPos++] = (uint8_t)((v >> 24) & 0xff);
99 mData[mPos++] = (uint8_t)((v >> 32) & 0xff);
100 mData[mPos++] = (uint8_t)((v >> 40) & 0xff);
101 mData[mPos++] = (uint8_t)((v >> 48) & 0xff);
102 mData[mPos++] = (uint8_t)((v >> 56) & 0xff);
103 }
104 else {
105 addU32(v);
106 }
107}
108
109void OStream::addString(String8 *s)
110{
111 uint32_t len = s->size();
112 addU32(len);
113 if(mPos + len*sizeof(char) >= mLength) {
114 growSize();
115 }
116 char *stringData = reinterpret_cast<char *>(&mData[mPos]);
117 for(uint32_t i = 0; i < len; i ++) {
118 stringData[i] = s->string()[i];
119 }
120 mPos += len*sizeof(char);
121}
122
123void OStream::growSize()
124{
125 uint8_t *newData = (uint8_t*)malloc(mLength*2);
126 memcpy(newData, mData, mLength*sizeof(uint8_t));
127 mLength = mLength * 2;
128 free(mData);
129 mData = newData;
130}
131
132