blob: c198b51f8ced4bbe05b82518b0f3ac9c8d098322 [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
Chih-Hung Hsieh11496ac2016-11-15 15:14:05 -080021namespace android {
22namespace renderscript {
Alex Sakhartchoukfb6b6142010-05-21 12:53:13 -070023
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
Jason Sams48ecf6a2013-07-09 15:35:29 -070046const char * IStream::loadString() {
Alex Sakhartchoukfb6b6142010-05-21 12:53:13 -070047 uint32_t len = loadU32();
Jason Sams48ecf6a2013-07-09 15:35:29 -070048 const char *s = rsuCopyString((const char *)&mData[mPos], len);
Alex Sakhartchoukfb6b6142010-05-21 12:53:13 -070049 mPos += len;
Jason Sams48ecf6a2013-07-09 15:35:29 -070050 return s;
Alex Sakhartchoukfb6b6142010-05-21 12:53:13 -070051}
52
Alex Sakhartchoukfb6b6142010-05-21 12:53:13 -070053// Output stream implementation
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -080054OStream::OStream(uint64_t len, bool use64) {
Alex Sakhartchoukfb6b6142010-05-21 12:53:13 -070055 mData = (uint8_t*)malloc(len);
56 mLength = len;
57 mPos = 0;
58 mUse64 = use64;
59}
60
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -080061OStream::~OStream() {
Alex Sakhartchoukfb6b6142010-05-21 12:53:13 -070062 free(mData);
63}
64
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -080065void OStream::addByteArray(const void *src, size_t numBytes) {
Alex Sakhartchoukfb6b6142010-05-21 12:53:13 -070066 // We need to potentially grow more than once if the number of byes we write is substantial
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -080067 while (mPos + numBytes >= mLength) {
Alex Sakhartchoukfb6b6142010-05-21 12:53:13 -070068 growSize();
69 }
70 memcpy(mData + mPos, src, numBytes);
71 mPos += numBytes;
72}
73
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -080074void OStream::addOffset(uint64_t v) {
Alex Sakhartchoukfb6b6142010-05-21 12:53:13 -070075 if (mUse64) {
76 mPos = (mPos + 7) & (~7);
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -080077 if (mPos + sizeof(v) >= mLength) {
Alex Sakhartchoukfb6b6142010-05-21 12:53:13 -070078 growSize();
79 }
80 mData[mPos++] = (uint8_t)(v & 0xff);
81 mData[mPos++] = (uint8_t)((v >> 8) & 0xff);
82 mData[mPos++] = (uint8_t)((v >> 16) & 0xff);
83 mData[mPos++] = (uint8_t)((v >> 24) & 0xff);
84 mData[mPos++] = (uint8_t)((v >> 32) & 0xff);
85 mData[mPos++] = (uint8_t)((v >> 40) & 0xff);
86 mData[mPos++] = (uint8_t)((v >> 48) & 0xff);
87 mData[mPos++] = (uint8_t)((v >> 56) & 0xff);
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -080088 } else {
Alex Sakhartchoukfb6b6142010-05-21 12:53:13 -070089 addU32(v);
90 }
91}
92
Jason Sams48ecf6a2013-07-09 15:35:29 -070093void OStream::addString(const char *s, size_t len) {
Alex Sakhartchoukfb6b6142010-05-21 12:53:13 -070094 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]);
Jason Sams48ecf6a2013-07-09 15:35:29 -070099 memcpy(stringData, s, len);
Alex Sakhartchoukfb6b6142010-05-21 12:53:13 -0700100 mPos += len*sizeof(char);
101}
102
Jason Sams48ecf6a2013-07-09 15:35:29 -0700103void OStream::addString(const char *s) {
104 addString(s, strlen(s));
105}
106
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800107void OStream::growSize() {
Alex Sakhartchoukfb6b6142010-05-21 12:53:13 -0700108 uint8_t *newData = (uint8_t*)malloc(mLength*2);
109 memcpy(newData, mData, mLength*sizeof(uint8_t));
110 mLength = mLength * 2;
111 free(mData);
112 mData = newData;
113}
114
Chih-Hung Hsieh11496ac2016-11-15 15:14:05 -0800115} // namespace renderscript
116} // namespace android