blob: ac658c8e53ff846939b642f6e16bfe39af89b518 [file] [log] [blame]
Jason Sams64676f32009-07-08 18:01:53 -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#include "rsContext.h"
Jason Sams64676f32009-07-08 18:01:53 -070019#include "rsFileA3D.h"
20
21#include "rsMesh.h"
Alex Sakhartchoukaa7d2882010-05-21 12:53:13 -070022#include "rsAnimation.h"
Jason Sams64676f32009-07-08 18:01:53 -070023
Alex Sakhartchouk9b949fc2010-06-24 17:15:34 -070024
Jason Sams64676f32009-07-08 18:01:53 -070025using namespace android;
26using namespace android::renderscript;
27
Alex Sakhartchouked9f2102010-11-09 17:00:54 -080028FileA3D::FileA3D(Context *rsc) : ObjectBase(rsc) {
Alex Sakhartchoukaa7d2882010-05-21 12:53:13 -070029 mAlloc = NULL;
30 mData = NULL;
31 mWriteStream = NULL;
32 mReadStream = NULL;
Alex Sakhartchoukb0253ea2011-01-07 11:12:08 -080033 mAsset = NULL;
Alex Sakhartchoukaa7d2882010-05-21 12:53:13 -070034
35 mMajorVersion = 0;
36 mMinorVersion = 1;
37 mDataSize = 0;
Jason Sams64676f32009-07-08 18:01:53 -070038}
39
Alex Sakhartchouked9f2102010-11-09 17:00:54 -080040FileA3D::~FileA3D() {
41 for (size_t i = 0; i < mIndex.size(); i ++) {
Alex Sakhartchoukaa7d2882010-05-21 12:53:13 -070042 delete mIndex[i];
43 }
Alex Sakhartchouked9f2102010-11-09 17:00:54 -080044 for (size_t i = 0; i < mWriteIndex.size(); i ++) {
Alex Sakhartchoukaa7d2882010-05-21 12:53:13 -070045 delete mWriteIndex[i];
46 }
Alex Sakhartchouked9f2102010-11-09 17:00:54 -080047 if (mWriteStream) {
Alex Sakhartchoukaa7d2882010-05-21 12:53:13 -070048 delete mWriteStream;
49 }
Alex Sakhartchouked9f2102010-11-09 17:00:54 -080050 if (mReadStream) {
Alex Sakhartchoukaa7d2882010-05-21 12:53:13 -070051 delete mWriteStream;
52 }
Alex Sakhartchouked9f2102010-11-09 17:00:54 -080053 if (mAlloc) {
Alex Sakhartchoukaa7d2882010-05-21 12:53:13 -070054 free(mAlloc);
55 }
Alex Sakhartchoukb0253ea2011-01-07 11:12:08 -080056 if (mAsset) {
57 delete mAsset;
58 }
Jason Sams64676f32009-07-08 18:01:53 -070059}
60
Alex Sakhartchouked9f2102010-11-09 17:00:54 -080061void FileA3D::parseHeader(IStream *headerStream) {
Alex Sakhartchoukaae74ad2010-06-04 10:06:50 -070062 mMajorVersion = headerStream->loadU32();
63 mMinorVersion = headerStream->loadU32();
64 uint32_t flags = headerStream->loadU32();
65 mUse64BitOffsets = (flags & 1) != 0;
66
Alex Sakhartchoukaae74ad2010-06-04 10:06:50 -070067 uint32_t numIndexEntries = headerStream->loadU32();
Alex Sakhartchouked9f2102010-11-09 17:00:54 -080068 for (uint32_t i = 0; i < numIndexEntries; i ++) {
Alex Sakhartchoukaae74ad2010-06-04 10:06:50 -070069 A3DIndexEntry *entry = new A3DIndexEntry();
70 headerStream->loadString(&entry->mObjectName);
Steve Block71f2cf12011-10-20 11:56:00 +010071 //ALOGV("Header data, entry name = %s", entry->mObjectName.string());
Alex Sakhartchoukaae74ad2010-06-04 10:06:50 -070072 entry->mType = (RsA3DClassID)headerStream->loadU32();
Alex Sakhartchouked9f2102010-11-09 17:00:54 -080073 if (mUse64BitOffsets){
Alex Sakhartchoukaae74ad2010-06-04 10:06:50 -070074 entry->mOffset = headerStream->loadOffset();
75 entry->mLength = headerStream->loadOffset();
Alex Sakhartchouked9f2102010-11-09 17:00:54 -080076 } else {
Alex Sakhartchoukaae74ad2010-06-04 10:06:50 -070077 entry->mOffset = headerStream->loadU32();
78 entry->mLength = headerStream->loadU32();
79 }
80 entry->mRsObj = NULL;
81 mIndex.push(entry);
82 }
83}
84
Alex Sakhartchoukb0253ea2011-01-07 11:12:08 -080085bool FileA3D::load(Asset *asset) {
86 mAsset = asset;
87 return load(asset->getBuffer(false), asset->getLength());
88}
89
Alex Sakhartchouked9f2102010-11-09 17:00:54 -080090bool FileA3D::load(const void *data, size_t length) {
Alex Sakhartchoukaae74ad2010-06-04 10:06:50 -070091 const uint8_t *localData = (const uint8_t *)data;
92
93 size_t lengthRemaining = length;
94 size_t magicStrLen = 12;
95 if ((length < magicStrLen) ||
96 memcmp(data, "Android3D_ff", magicStrLen)) {
97 return false;
98 }
99
100 localData += magicStrLen;
101 lengthRemaining -= magicStrLen;
102
103 // Next we get our header size
104 uint64_t headerSize = 0;
Alex Sakhartchouked9f2102010-11-09 17:00:54 -0800105 if (lengthRemaining < sizeof(headerSize)) {
Alex Sakhartchoukaae74ad2010-06-04 10:06:50 -0700106 return false;
107 }
108
109 memcpy(&headerSize, localData, sizeof(headerSize));
110 localData += sizeof(headerSize);
111 lengthRemaining -= sizeof(headerSize);
112
Alex Sakhartchouked9f2102010-11-09 17:00:54 -0800113 if (lengthRemaining < headerSize) {
Alex Sakhartchoukaae74ad2010-06-04 10:06:50 -0700114 return false;
115 }
116
Alex Sakhartchoukaae74ad2010-06-04 10:06:50 -0700117 // Now open the stream to parse the header
Alex Sakhartchouk581cc642010-10-27 14:10:07 -0700118 IStream headerStream(localData, false);
Alex Sakhartchoukaae74ad2010-06-04 10:06:50 -0700119 parseHeader(&headerStream);
120
Alex Sakhartchoukaae74ad2010-06-04 10:06:50 -0700121 localData += headerSize;
122 lengthRemaining -= headerSize;
123
Alex Sakhartchouked9f2102010-11-09 17:00:54 -0800124 if (lengthRemaining < sizeof(mDataSize)) {
Alex Sakhartchoukaae74ad2010-06-04 10:06:50 -0700125 return false;
126 }
127
128 // Read the size of the data
129 memcpy(&mDataSize, localData, sizeof(mDataSize));
130 localData += sizeof(mDataSize);
131 lengthRemaining -= sizeof(mDataSize);
132
Alex Sakhartchouked9f2102010-11-09 17:00:54 -0800133 if (lengthRemaining < mDataSize) {
Alex Sakhartchoukaae74ad2010-06-04 10:06:50 -0700134 return false;
135 }
136
137 // We should know enough to read the file in at this point.
Alex Sakhartchouk581cc642010-10-27 14:10:07 -0700138 mData = (uint8_t *)localData;
Alex Sakhartchoukaae74ad2010-06-04 10:06:50 -0700139 mReadStream = new IStream(mData, mUse64BitOffsets);
140
141 return true;
142}
143
Alex Sakhartchouked9f2102010-11-09 17:00:54 -0800144bool FileA3D::load(FILE *f) {
Jason Sams64676f32009-07-08 18:01:53 -0700145 char magicString[12];
146 size_t len;
147
Steve Block71f2cf12011-10-20 11:56:00 +0100148 ALOGV("file open 1");
Jason Sams64676f32009-07-08 18:01:53 -0700149 len = fread(magicString, 1, 12, f);
150 if ((len != 12) ||
151 memcmp(magicString, "Android3D_ff", 12)) {
152 return false;
153 }
154
Alex Sakhartchoukaa7d2882010-05-21 12:53:13 -0700155 // Next thing is the size of the header
156 uint64_t headerSize = 0;
157 len = fread(&headerSize, 1, sizeof(headerSize), f);
158 if (len != sizeof(headerSize) || headerSize == 0) {
Jason Sams64676f32009-07-08 18:01:53 -0700159 return false;
160 }
161
Alex Sakhartchoukaa7d2882010-05-21 12:53:13 -0700162 uint8_t *headerData = (uint8_t *)malloc(headerSize);
Alex Sakhartchouked9f2102010-11-09 17:00:54 -0800163 if (!headerData) {
Jason Sams64676f32009-07-08 18:01:53 -0700164 return false;
165 }
166
Alex Sakhartchoukaa7d2882010-05-21 12:53:13 -0700167 len = fread(headerData, 1, headerSize, f);
168 if (len != headerSize) {
Jason Sams64676f32009-07-08 18:01:53 -0700169 return false;
170 }
Alex Sakhartchoukaa7d2882010-05-21 12:53:13 -0700171
172 // Now open the stream to parse the header
173 IStream headerStream(headerData, false);
Alex Sakhartchoukaae74ad2010-06-04 10:06:50 -0700174 parseHeader(&headerStream);
Alex Sakhartchoukaa7d2882010-05-21 12:53:13 -0700175
Alex Sakhartchoukaae74ad2010-06-04 10:06:50 -0700176 free(headerData);
Alex Sakhartchoukaa7d2882010-05-21 12:53:13 -0700177
178 // Next thing is the size of the header
179 len = fread(&mDataSize, 1, sizeof(mDataSize), f);
180 if (len != sizeof(mDataSize) || mDataSize == 0) {
181 return false;
Jason Sams64676f32009-07-08 18:01:53 -0700182 }
183
Steve Block71f2cf12011-10-20 11:56:00 +0100184 ALOGV("file open size = %lli", mDataSize);
Jason Sams64676f32009-07-08 18:01:53 -0700185
186 // We should know enough to read the file in at this point.
Alex Sakhartchoukaa7d2882010-05-21 12:53:13 -0700187 mAlloc = malloc(mDataSize);
Jason Sams64676f32009-07-08 18:01:53 -0700188 if (!mAlloc) {
189 return false;
190 }
191 mData = (uint8_t *)mAlloc;
192 len = fread(mAlloc, 1, mDataSize, f);
193 if (len != mDataSize) {
194 return false;
195 }
196
Alex Sakhartchoukaa7d2882010-05-21 12:53:13 -0700197 mReadStream = new IStream(mData, mUse64BitOffsets);
198
Steve Block71f2cf12011-10-20 11:56:00 +0100199 ALOGV("Header is read an stream initialized");
Alex Sakhartchoukaa7d2882010-05-21 12:53:13 -0700200 return true;
Jason Sams64676f32009-07-08 18:01:53 -0700201}
202
Alex Sakhartchoukaae74ad2010-06-04 10:06:50 -0700203size_t FileA3D::getNumIndexEntries() const {
Alex Sakhartchoukaa7d2882010-05-21 12:53:13 -0700204 return mIndex.size();
205}
Jason Sams64676f32009-07-08 18:01:53 -0700206
Alex Sakhartchoukaae74ad2010-06-04 10:06:50 -0700207const FileA3D::A3DIndexEntry *FileA3D::getIndexEntry(size_t index) const {
Alex Sakhartchouked9f2102010-11-09 17:00:54 -0800208 if (index < mIndex.size()) {
Alex Sakhartchoukaa7d2882010-05-21 12:53:13 -0700209 return mIndex[index];
Jason Sams64676f32009-07-08 18:01:53 -0700210 }
Alex Sakhartchoukaa7d2882010-05-21 12:53:13 -0700211 return NULL;
Jason Sams64676f32009-07-08 18:01:53 -0700212}
213
Alex Sakhartchoukaae74ad2010-06-04 10:06:50 -0700214ObjectBase *FileA3D::initializeFromEntry(size_t index) {
Alex Sakhartchouked9f2102010-11-09 17:00:54 -0800215 if (index >= mIndex.size()) {
Alex Sakhartchoukaae74ad2010-06-04 10:06:50 -0700216 return NULL;
217 }
218
219 FileA3D::A3DIndexEntry *entry = mIndex[index];
Alex Sakhartchouked9f2102010-11-09 17:00:54 -0800220 if (!entry) {
Alex Sakhartchoukaa7d2882010-05-21 12:53:13 -0700221 return NULL;
222 }
223
Alex Sakhartchouked9f2102010-11-09 17:00:54 -0800224 if (entry->mRsObj) {
Alex Sakhartchoukaae74ad2010-06-04 10:06:50 -0700225 entry->mRsObj->incUserRef();
226 return entry->mRsObj;
227 }
228
Alex Sakhartchoukaa7d2882010-05-21 12:53:13 -0700229 // Seek to the beginning of object
230 mReadStream->reset(entry->mOffset);
231 switch (entry->mType) {
Alex Sakhartchoukaae74ad2010-06-04 10:06:50 -0700232 case RS_A3D_CLASS_ID_UNKNOWN:
Alex Sakhartchoukaa7d2882010-05-21 12:53:13 -0700233 return NULL;
Alex Sakhartchoukaae74ad2010-06-04 10:06:50 -0700234 case RS_A3D_CLASS_ID_MESH:
235 entry->mRsObj = Mesh::createFromStream(mRSC, mReadStream);
236 break;
Alex Sakhartchoukaae74ad2010-06-04 10:06:50 -0700237 case RS_A3D_CLASS_ID_TYPE:
238 entry->mRsObj = Type::createFromStream(mRSC, mReadStream);
239 break;
240 case RS_A3D_CLASS_ID_ELEMENT:
241 entry->mRsObj = Element::createFromStream(mRSC, mReadStream);
242 break;
243 case RS_A3D_CLASS_ID_ALLOCATION:
244 entry->mRsObj = Allocation::createFromStream(mRSC, mReadStream);
245 break;
246 case RS_A3D_CLASS_ID_PROGRAM_VERTEX:
Alex Sakhartchouka3b59602011-01-28 09:31:47 -0800247 //entry->mRsObj = ProgramVertex::createFromStream(mRSC, mReadStream);
Alex Sakhartchoukaae74ad2010-06-04 10:06:50 -0700248 break;
249 case RS_A3D_CLASS_ID_PROGRAM_RASTER:
Alex Sakhartchouka3b59602011-01-28 09:31:47 -0800250 //entry->mRsObj = ProgramRaster::createFromStream(mRSC, mReadStream);
Alex Sakhartchoukaae74ad2010-06-04 10:06:50 -0700251 break;
252 case RS_A3D_CLASS_ID_PROGRAM_FRAGMENT:
Alex Sakhartchouka3b59602011-01-28 09:31:47 -0800253 //entry->mRsObj = ProgramFragment::createFromStream(mRSC, mReadStream);
Alex Sakhartchoukaae74ad2010-06-04 10:06:50 -0700254 break;
255 case RS_A3D_CLASS_ID_PROGRAM_STORE:
Alex Sakhartchouka3b59602011-01-28 09:31:47 -0800256 //entry->mRsObj = ProgramStore::createFromStream(mRSC, mReadStream);
Alex Sakhartchoukaae74ad2010-06-04 10:06:50 -0700257 break;
258 case RS_A3D_CLASS_ID_SAMPLER:
Alex Sakhartchouka3b59602011-01-28 09:31:47 -0800259 //entry->mRsObj = Sampler::createFromStream(mRSC, mReadStream);
Alex Sakhartchoukaae74ad2010-06-04 10:06:50 -0700260 break;
261 case RS_A3D_CLASS_ID_ANIMATION:
Alex Sakhartchouka3b59602011-01-28 09:31:47 -0800262 //entry->mRsObj = Animation::createFromStream(mRSC, mReadStream);
Alex Sakhartchoukaae74ad2010-06-04 10:06:50 -0700263 break;
Alex Sakhartchoukaae74ad2010-06-04 10:06:50 -0700264 case RS_A3D_CLASS_ID_ADAPTER_1D:
Alex Sakhartchouka3b59602011-01-28 09:31:47 -0800265 //entry->mRsObj = Adapter1D::createFromStream(mRSC, mReadStream);
Alex Sakhartchoukaae74ad2010-06-04 10:06:50 -0700266 break;
267 case RS_A3D_CLASS_ID_ADAPTER_2D:
Alex Sakhartchouka3b59602011-01-28 09:31:47 -0800268 //entry->mRsObj = Adapter2D::createFromStream(mRSC, mReadStream);
Alex Sakhartchoukaae74ad2010-06-04 10:06:50 -0700269 break;
270 case RS_A3D_CLASS_ID_SCRIPT_C:
Alex Sakhartchouka3b59602011-01-28 09:31:47 -0800271 break;
Alex Sakhartchoukaa7d2882010-05-21 12:53:13 -0700272 }
Alex Sakhartchouked9f2102010-11-09 17:00:54 -0800273 if (entry->mRsObj) {
Alex Sakhartchoukaae74ad2010-06-04 10:06:50 -0700274 entry->mRsObj->incUserRef();
275 }
276 return entry->mRsObj;
Alex Sakhartchoukaa7d2882010-05-21 12:53:13 -0700277}
278
Alex Sakhartchouked9f2102010-11-09 17:00:54 -0800279bool FileA3D::writeFile(const char *filename) {
280 if (!mWriteStream) {
Steve Block3762c312012-01-06 19:20:56 +0000281 ALOGE("No objects to write\n");
Alex Sakhartchoukaa7d2882010-05-21 12:53:13 -0700282 return false;
283 }
Alex Sakhartchouked9f2102010-11-09 17:00:54 -0800284 if (mWriteStream->getPos() == 0) {
Steve Block3762c312012-01-06 19:20:56 +0000285 ALOGE("No objects to write\n");
Alex Sakhartchoukaa7d2882010-05-21 12:53:13 -0700286 return false;
287 }
Jason Sams64676f32009-07-08 18:01:53 -0700288
Alex Sakhartchoukaa7d2882010-05-21 12:53:13 -0700289 FILE *writeHandle = fopen(filename, "wb");
Alex Sakhartchouked9f2102010-11-09 17:00:54 -0800290 if (!writeHandle) {
Steve Block3762c312012-01-06 19:20:56 +0000291 ALOGE("Couldn't open the file for writing\n");
Alex Sakhartchoukaa7d2882010-05-21 12:53:13 -0700292 return false;
293 }
Jason Sams64676f32009-07-08 18:01:53 -0700294
Alex Sakhartchoukaa7d2882010-05-21 12:53:13 -0700295 // Open a new stream to make writing the header easier
296 OStream headerStream(5*1024, false);
297 headerStream.addU32(mMajorVersion);
298 headerStream.addU32(mMinorVersion);
299 uint32_t is64Bit = 0;
300 headerStream.addU32(is64Bit);
Jason Sams64676f32009-07-08 18:01:53 -0700301
Alex Sakhartchoukaa7d2882010-05-21 12:53:13 -0700302 uint32_t writeIndexSize = mWriteIndex.size();
303 headerStream.addU32(writeIndexSize);
Alex Sakhartchouked9f2102010-11-09 17:00:54 -0800304 for (uint32_t i = 0; i < writeIndexSize; i ++) {
Alex Sakhartchoukaae74ad2010-06-04 10:06:50 -0700305 headerStream.addString(&mWriteIndex[i]->mObjectName);
Alex Sakhartchoukaa7d2882010-05-21 12:53:13 -0700306 headerStream.addU32((uint32_t)mWriteIndex[i]->mType);
Alex Sakhartchouked9f2102010-11-09 17:00:54 -0800307 if (mUse64BitOffsets){
Alex Sakhartchoukaa7d2882010-05-21 12:53:13 -0700308 headerStream.addOffset(mWriteIndex[i]->mOffset);
Alex Sakhartchoukaae74ad2010-06-04 10:06:50 -0700309 headerStream.addOffset(mWriteIndex[i]->mLength);
Alex Sakhartchouked9f2102010-11-09 17:00:54 -0800310 } else {
Alex Sakhartchoukaa7d2882010-05-21 12:53:13 -0700311 uint32_t offset = (uint32_t)mWriteIndex[i]->mOffset;
312 headerStream.addU32(offset);
Alex Sakhartchoukaae74ad2010-06-04 10:06:50 -0700313 offset = (uint32_t)mWriteIndex[i]->mLength;
314 headerStream.addU32(offset);
Jason Sams64676f32009-07-08 18:01:53 -0700315 }
316 }
317
Alex Sakhartchoukaa7d2882010-05-21 12:53:13 -0700318 // Write our magic string so we know we are reading the right file
319 String8 magicString(A3D_MAGIC_KEY);
320 fwrite(magicString.string(), sizeof(char), magicString.size(), writeHandle);
Jason Sams64676f32009-07-08 18:01:53 -0700321
Alex Sakhartchoukaa7d2882010-05-21 12:53:13 -0700322 // Store the size of the header to make it easier to parse when we read it
323 uint64_t headerSize = headerStream.getPos();
324 fwrite(&headerSize, sizeof(headerSize), 1, writeHandle);
Jason Sams64676f32009-07-08 18:01:53 -0700325
Alex Sakhartchoukaa7d2882010-05-21 12:53:13 -0700326 // Now write our header
327 fwrite(headerStream.getPtr(), sizeof(uint8_t), headerStream.getPos(), writeHandle);
Jason Sams64676f32009-07-08 18:01:53 -0700328
Alex Sakhartchoukaa7d2882010-05-21 12:53:13 -0700329 // Now write the size of the data part of the file for easier parsing later
330 uint64_t fileDataSize = mWriteStream->getPos();
331 fwrite(&fileDataSize, sizeof(fileDataSize), 1, writeHandle);
332
333 fwrite(mWriteStream->getPtr(), sizeof(uint8_t), mWriteStream->getPos(), writeHandle);
334
335 int status = fclose(writeHandle);
336
Alex Sakhartchouked9f2102010-11-09 17:00:54 -0800337 if (status != 0) {
Steve Block3762c312012-01-06 19:20:56 +0000338 ALOGE("Couldn't close file\n");
Alex Sakhartchoukaa7d2882010-05-21 12:53:13 -0700339 return false;
Jason Sams64676f32009-07-08 18:01:53 -0700340 }
341
Alex Sakhartchoukaa7d2882010-05-21 12:53:13 -0700342 return true;
Jason Sams64676f32009-07-08 18:01:53 -0700343}
344
Alex Sakhartchoukaa7d2882010-05-21 12:53:13 -0700345void FileA3D::appendToFile(ObjectBase *obj) {
Alex Sakhartchouked9f2102010-11-09 17:00:54 -0800346 if (!obj) {
Alex Sakhartchoukaa7d2882010-05-21 12:53:13 -0700347 return;
Jason Sams64676f32009-07-08 18:01:53 -0700348 }
Alex Sakhartchouked9f2102010-11-09 17:00:54 -0800349 if (!mWriteStream) {
Alex Sakhartchoukaa7d2882010-05-21 12:53:13 -0700350 const uint64_t initialStreamSize = 256*1024;
351 mWriteStream = new OStream(initialStreamSize, false);
Jason Sams64676f32009-07-08 18:01:53 -0700352 }
Alex Sakhartchoukaa7d2882010-05-21 12:53:13 -0700353 A3DIndexEntry *indexEntry = new A3DIndexEntry();
Alex Sakhartchoukaae74ad2010-06-04 10:06:50 -0700354 indexEntry->mObjectName.setTo(obj->getName());
Alex Sakhartchoukaa7d2882010-05-21 12:53:13 -0700355 indexEntry->mType = obj->getClassId();
356 indexEntry->mOffset = mWriteStream->getPos();
Alex Sakhartchoukaae74ad2010-06-04 10:06:50 -0700357 indexEntry->mRsObj = obj;
Alex Sakhartchoukaa7d2882010-05-21 12:53:13 -0700358 mWriteIndex.push(indexEntry);
359 obj->serialize(mWriteStream);
Alex Sakhartchoukaae74ad2010-06-04 10:06:50 -0700360 indexEntry->mLength = mWriteStream->getPos() - indexEntry->mOffset;
361 mWriteStream->align(4);
Jason Sams64676f32009-07-08 18:01:53 -0700362}
363
Alex Sakhartchouked9f2102010-11-09 17:00:54 -0800364RsObjectBase rsaFileA3DGetEntryByIndex(RsContext con, uint32_t index, RsFile file) {
Alex Sakhartchouk581cc642010-10-27 14:10:07 -0700365 FileA3D *fa3d = static_cast<FileA3D *>(file);
Alex Sakhartchouked9f2102010-11-09 17:00:54 -0800366 if (!fa3d) {
Steve Block3762c312012-01-06 19:20:56 +0000367 ALOGE("Can't load entry. No valid file");
Alex Sakhartchouk581cc642010-10-27 14:10:07 -0700368 return NULL;
369 }
370
371 ObjectBase *obj = fa3d->initializeFromEntry(index);
Steve Block71f2cf12011-10-20 11:56:00 +0100372 //ALOGV("Returning object with name %s", obj->getName());
Alex Sakhartchouk581cc642010-10-27 14:10:07 -0700373
374 return obj;
375}
376
377
Alex Sakhartchouked9f2102010-11-09 17:00:54 -0800378void rsaFileA3DGetNumIndexEntries(RsContext con, int32_t *numEntries, RsFile file) {
Alex Sakhartchoukaae74ad2010-06-04 10:06:50 -0700379 FileA3D *fa3d = static_cast<FileA3D *>(file);
380
Alex Sakhartchouked9f2102010-11-09 17:00:54 -0800381 if (fa3d) {
Alex Sakhartchoukaae74ad2010-06-04 10:06:50 -0700382 *numEntries = fa3d->getNumIndexEntries();
Alex Sakhartchouked9f2102010-11-09 17:00:54 -0800383 } else {
Alex Sakhartchoukaae74ad2010-06-04 10:06:50 -0700384 *numEntries = 0;
385 }
386}
387
Alex Sakhartchouked9f2102010-11-09 17:00:54 -0800388void rsaFileA3DGetIndexEntries(RsContext con, RsFileIndexEntry *fileEntries, uint32_t numEntries, RsFile file) {
Alex Sakhartchoukaae74ad2010-06-04 10:06:50 -0700389 FileA3D *fa3d = static_cast<FileA3D *>(file);
390
Alex Sakhartchouked9f2102010-11-09 17:00:54 -0800391 if (!fa3d) {
Steve Block3762c312012-01-06 19:20:56 +0000392 ALOGE("Can't load index entries. No valid file");
Alex Sakhartchoukaae74ad2010-06-04 10:06:50 -0700393 return;
394 }
395
396 uint32_t numFileEntries = fa3d->getNumIndexEntries();
Alex Sakhartchouked9f2102010-11-09 17:00:54 -0800397 if (numFileEntries != numEntries || numEntries == 0 || fileEntries == NULL) {
Steve Block3762c312012-01-06 19:20:56 +0000398 ALOGE("Can't load index entries. Invalid number requested");
Alex Sakhartchoukaae74ad2010-06-04 10:06:50 -0700399 return;
400 }
401
Alex Sakhartchouked9f2102010-11-09 17:00:54 -0800402 for (uint32_t i = 0; i < numFileEntries; i ++) {
Alex Sakhartchoukaae74ad2010-06-04 10:06:50 -0700403 const FileA3D::A3DIndexEntry *entry = fa3d->getIndexEntry(i);
404 fileEntries[i].classID = entry->getType();
405 fileEntries[i].objectName = entry->getObjectName().string();
406 }
Alex Sakhartchoukaae74ad2010-06-04 10:06:50 -0700407}
408
Alex Sakhartchoukb0253ea2011-01-07 11:12:08 -0800409RsFile rsaFileA3DCreateFromMemory(RsContext con, const void *data, uint32_t len) {
Alex Sakhartchoukaae74ad2010-06-04 10:06:50 -0700410 if (data == NULL) {
Steve Block3762c312012-01-06 19:20:56 +0000411 ALOGE("File load failed. Asset stream is NULL");
Alex Sakhartchoukaae74ad2010-06-04 10:06:50 -0700412 return NULL;
413 }
414
Alex Sakhartchouk581cc642010-10-27 14:10:07 -0700415 Context *rsc = static_cast<Context *>(con);
Alex Sakhartchoukaae74ad2010-06-04 10:06:50 -0700416 FileA3D *fa3d = new FileA3D(rsc);
Alex Sakhartchoukaae74ad2010-06-04 10:06:50 -0700417 fa3d->incUserRef();
418
Alex Sakhartchouk581cc642010-10-27 14:10:07 -0700419 fa3d->load(data, len);
Alex Sakhartchoukaae74ad2010-06-04 10:06:50 -0700420 return fa3d;
421}
Alex Sakhartchoukb0253ea2011-01-07 11:12:08 -0800422
423RsFile rsaFileA3DCreateFromAsset(RsContext con, void *_asset) {
424 Context *rsc = static_cast<Context *>(con);
425 Asset *asset = static_cast<Asset *>(_asset);
426 FileA3D *fa3d = new FileA3D(rsc);
427 fa3d->incUserRef();
428
429 fa3d->load(asset);
430 return fa3d;
431}
432
433RsFile rsaFileA3DCreateFromFile(RsContext con, const char *path) {
434 if (path == NULL) {
Steve Block3762c312012-01-06 19:20:56 +0000435 ALOGE("File load failed. Path is NULL");
Alex Sakhartchoukb0253ea2011-01-07 11:12:08 -0800436 return NULL;
437 }
438
439 Context *rsc = static_cast<Context *>(con);
440 FileA3D *fa3d = NULL;
441
442 FILE *f = fopen(path, "rb");
443 if (f) {
444 fa3d = new FileA3D(rsc);
445 fa3d->incUserRef();
446 fa3d->load(f);
447 fclose(f);
448 } else {
Steve Block3762c312012-01-06 19:20:56 +0000449 ALOGE("Could not open file %s", path);
Alex Sakhartchoukb0253ea2011-01-07 11:12:08 -0800450 }
451
452 return fa3d;
453}