blob: 0c8692d3f4892a66973f365fbb6236330383c668 [file] [log] [blame]
Jason Samsa5597fc2009-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
Alex Sakhartchoukfb6b6142010-05-21 12:53:13 -070018#ifndef ANDROID_RS_BUILD_FOR_HOST
Jason Samsa5597fc2009-07-08 18:01:53 -070019#include "rsContext.h"
Alex Sakhartchoukfb6b6142010-05-21 12:53:13 -070020#else
21#include "rsContextHostStub.h"
22#endif
Jason Samsa5597fc2009-07-08 18:01:53 -070023
Jason Samsa5597fc2009-07-08 18:01:53 -070024#include "rsFileA3D.h"
25
26#include "rsMesh.h"
Alex Sakhartchoukfb6b6142010-05-21 12:53:13 -070027#include "rsAnimation.h"
Jason Samsa5597fc2009-07-08 18:01:53 -070028
Alex Sakhartchoukd3e0ad42010-06-24 17:15:34 -070029
Jason Samsa5597fc2009-07-08 18:01:53 -070030using namespace android;
31using namespace android::renderscript;
32
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -080033FileA3D::FileA3D(Context *rsc) : ObjectBase(rsc) {
Alex Sakhartchoukfb6b6142010-05-21 12:53:13 -070034 mAlloc = NULL;
35 mData = NULL;
36 mWriteStream = NULL;
37 mReadStream = NULL;
38
39 mMajorVersion = 0;
40 mMinorVersion = 1;
41 mDataSize = 0;
Jason Samsa5597fc2009-07-08 18:01:53 -070042}
43
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -080044FileA3D::~FileA3D() {
45 for (size_t i = 0; i < mIndex.size(); i ++) {
Alex Sakhartchoukfb6b6142010-05-21 12:53:13 -070046 delete mIndex[i];
47 }
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -080048 for (size_t i = 0; i < mWriteIndex.size(); i ++) {
Alex Sakhartchoukfb6b6142010-05-21 12:53:13 -070049 delete mWriteIndex[i];
50 }
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -080051 if (mWriteStream) {
Alex Sakhartchoukfb6b6142010-05-21 12:53:13 -070052 delete mWriteStream;
53 }
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -080054 if (mReadStream) {
Alex Sakhartchoukfb6b6142010-05-21 12:53:13 -070055 delete mWriteStream;
56 }
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -080057 if (mAlloc) {
Alex Sakhartchoukfb6b6142010-05-21 12:53:13 -070058 free(mAlloc);
59 }
Jason Samsa5597fc2009-07-08 18:01:53 -070060}
61
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -080062void FileA3D::parseHeader(IStream *headerStream) {
Alex Sakhartchoukb825f672010-06-04 10:06:50 -070063 mMajorVersion = headerStream->loadU32();
64 mMinorVersion = headerStream->loadU32();
65 uint32_t flags = headerStream->loadU32();
66 mUse64BitOffsets = (flags & 1) != 0;
67
Alex Sakhartchoukb825f672010-06-04 10:06:50 -070068 uint32_t numIndexEntries = headerStream->loadU32();
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -080069 for (uint32_t i = 0; i < numIndexEntries; i ++) {
Alex Sakhartchoukb825f672010-06-04 10:06:50 -070070 A3DIndexEntry *entry = new A3DIndexEntry();
71 headerStream->loadString(&entry->mObjectName);
Alex Sakhartchouk383e5b12010-09-23 16:16:33 -070072 LOGV("Header data, entry name = %s", entry->mObjectName.string());
Alex Sakhartchoukb825f672010-06-04 10:06:50 -070073 entry->mType = (RsA3DClassID)headerStream->loadU32();
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -080074 if (mUse64BitOffsets){
Alex Sakhartchoukb825f672010-06-04 10:06:50 -070075 entry->mOffset = headerStream->loadOffset();
76 entry->mLength = headerStream->loadOffset();
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -080077 } else {
Alex Sakhartchoukb825f672010-06-04 10:06:50 -070078 entry->mOffset = headerStream->loadU32();
79 entry->mLength = headerStream->loadU32();
80 }
81 entry->mRsObj = NULL;
82 mIndex.push(entry);
83 }
84}
85
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -080086bool FileA3D::load(const void *data, size_t length) {
Alex Sakhartchoukb825f672010-06-04 10:06:50 -070087 const uint8_t *localData = (const uint8_t *)data;
88
89 size_t lengthRemaining = length;
90 size_t magicStrLen = 12;
91 if ((length < magicStrLen) ||
92 memcmp(data, "Android3D_ff", magicStrLen)) {
93 return false;
94 }
95
96 localData += magicStrLen;
97 lengthRemaining -= magicStrLen;
98
99 // Next we get our header size
100 uint64_t headerSize = 0;
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800101 if (lengthRemaining < sizeof(headerSize)) {
Alex Sakhartchoukb825f672010-06-04 10:06:50 -0700102 return false;
103 }
104
105 memcpy(&headerSize, localData, sizeof(headerSize));
106 localData += sizeof(headerSize);
107 lengthRemaining -= sizeof(headerSize);
108
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800109 if (lengthRemaining < headerSize) {
Alex Sakhartchoukb825f672010-06-04 10:06:50 -0700110 return false;
111 }
112
Alex Sakhartchoukb825f672010-06-04 10:06:50 -0700113 // Now open the stream to parse the header
Alex Sakhartchoukdc763f32010-10-27 14:10:07 -0700114 IStream headerStream(localData, false);
Alex Sakhartchoukb825f672010-06-04 10:06:50 -0700115 parseHeader(&headerStream);
116
Alex Sakhartchoukb825f672010-06-04 10:06:50 -0700117 localData += headerSize;
118 lengthRemaining -= headerSize;
119
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800120 if (lengthRemaining < sizeof(mDataSize)) {
Alex Sakhartchoukb825f672010-06-04 10:06:50 -0700121 return false;
122 }
123
124 // Read the size of the data
125 memcpy(&mDataSize, localData, sizeof(mDataSize));
126 localData += sizeof(mDataSize);
127 lengthRemaining -= sizeof(mDataSize);
128
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800129 if (lengthRemaining < mDataSize) {
Alex Sakhartchoukb825f672010-06-04 10:06:50 -0700130 return false;
131 }
132
133 // We should know enough to read the file in at this point.
Alex Sakhartchoukdc763f32010-10-27 14:10:07 -0700134 mData = (uint8_t *)localData;
Alex Sakhartchoukb825f672010-06-04 10:06:50 -0700135 mReadStream = new IStream(mData, mUse64BitOffsets);
136
137 return true;
138}
139
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800140bool FileA3D::load(FILE *f) {
Jason Samsa5597fc2009-07-08 18:01:53 -0700141 char magicString[12];
142 size_t len;
143
Alex Sakhartchouk383e5b12010-09-23 16:16:33 -0700144 LOGV("file open 1");
Jason Samsa5597fc2009-07-08 18:01:53 -0700145 len = fread(magicString, 1, 12, f);
146 if ((len != 12) ||
147 memcmp(magicString, "Android3D_ff", 12)) {
148 return false;
149 }
150
Alex Sakhartchoukfb6b6142010-05-21 12:53:13 -0700151 // Next thing is the size of the header
152 uint64_t headerSize = 0;
153 len = fread(&headerSize, 1, sizeof(headerSize), f);
154 if (len != sizeof(headerSize) || headerSize == 0) {
Jason Samsa5597fc2009-07-08 18:01:53 -0700155 return false;
156 }
157
Alex Sakhartchoukfb6b6142010-05-21 12:53:13 -0700158 uint8_t *headerData = (uint8_t *)malloc(headerSize);
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800159 if (!headerData) {
Jason Samsa5597fc2009-07-08 18:01:53 -0700160 return false;
161 }
162
Alex Sakhartchoukfb6b6142010-05-21 12:53:13 -0700163 len = fread(headerData, 1, headerSize, f);
164 if (len != headerSize) {
Jason Samsa5597fc2009-07-08 18:01:53 -0700165 return false;
166 }
Alex Sakhartchoukfb6b6142010-05-21 12:53:13 -0700167
168 // Now open the stream to parse the header
169 IStream headerStream(headerData, false);
Alex Sakhartchoukb825f672010-06-04 10:06:50 -0700170 parseHeader(&headerStream);
Alex Sakhartchoukfb6b6142010-05-21 12:53:13 -0700171
Alex Sakhartchoukb825f672010-06-04 10:06:50 -0700172 free(headerData);
Alex Sakhartchoukfb6b6142010-05-21 12:53:13 -0700173
174 // Next thing is the size of the header
175 len = fread(&mDataSize, 1, sizeof(mDataSize), f);
176 if (len != sizeof(mDataSize) || mDataSize == 0) {
177 return false;
Jason Samsa5597fc2009-07-08 18:01:53 -0700178 }
179
Alex Sakhartchouk383e5b12010-09-23 16:16:33 -0700180 LOGV("file open size = %lli", mDataSize);
Jason Samsa5597fc2009-07-08 18:01:53 -0700181
182 // We should know enough to read the file in at this point.
Alex Sakhartchoukfb6b6142010-05-21 12:53:13 -0700183 mAlloc = malloc(mDataSize);
Jason Samsa5597fc2009-07-08 18:01:53 -0700184 if (!mAlloc) {
185 return false;
186 }
187 mData = (uint8_t *)mAlloc;
188 len = fread(mAlloc, 1, mDataSize, f);
189 if (len != mDataSize) {
190 return false;
191 }
192
Alex Sakhartchoukfb6b6142010-05-21 12:53:13 -0700193 mReadStream = new IStream(mData, mUse64BitOffsets);
194
Alex Sakhartchouk383e5b12010-09-23 16:16:33 -0700195 LOGV("Header is read an stream initialized");
Alex Sakhartchoukfb6b6142010-05-21 12:53:13 -0700196 return true;
Jason Samsa5597fc2009-07-08 18:01:53 -0700197}
198
Alex Sakhartchoukb825f672010-06-04 10:06:50 -0700199size_t FileA3D::getNumIndexEntries() const {
Alex Sakhartchoukfb6b6142010-05-21 12:53:13 -0700200 return mIndex.size();
201}
Jason Samsa5597fc2009-07-08 18:01:53 -0700202
Alex Sakhartchoukb825f672010-06-04 10:06:50 -0700203const FileA3D::A3DIndexEntry *FileA3D::getIndexEntry(size_t index) const {
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800204 if (index < mIndex.size()) {
Alex Sakhartchoukfb6b6142010-05-21 12:53:13 -0700205 return mIndex[index];
Jason Samsa5597fc2009-07-08 18:01:53 -0700206 }
Alex Sakhartchoukfb6b6142010-05-21 12:53:13 -0700207 return NULL;
Jason Samsa5597fc2009-07-08 18:01:53 -0700208}
209
Alex Sakhartchoukb825f672010-06-04 10:06:50 -0700210ObjectBase *FileA3D::initializeFromEntry(size_t index) {
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800211 if (index >= mIndex.size()) {
Alex Sakhartchoukb825f672010-06-04 10:06:50 -0700212 return NULL;
213 }
214
215 FileA3D::A3DIndexEntry *entry = mIndex[index];
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800216 if (!entry) {
Alex Sakhartchoukfb6b6142010-05-21 12:53:13 -0700217 return NULL;
218 }
219
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800220 if (entry->mRsObj) {
Alex Sakhartchoukb825f672010-06-04 10:06:50 -0700221 entry->mRsObj->incUserRef();
222 return entry->mRsObj;
223 }
224
Alex Sakhartchoukfb6b6142010-05-21 12:53:13 -0700225 // Seek to the beginning of object
226 mReadStream->reset(entry->mOffset);
227 switch (entry->mType) {
Alex Sakhartchoukb825f672010-06-04 10:06:50 -0700228 case RS_A3D_CLASS_ID_UNKNOWN:
Alex Sakhartchoukfb6b6142010-05-21 12:53:13 -0700229 return NULL;
Alex Sakhartchoukb825f672010-06-04 10:06:50 -0700230 case RS_A3D_CLASS_ID_MESH:
231 entry->mRsObj = Mesh::createFromStream(mRSC, mReadStream);
232 break;
Alex Sakhartchoukb825f672010-06-04 10:06:50 -0700233 case RS_A3D_CLASS_ID_TYPE:
234 entry->mRsObj = Type::createFromStream(mRSC, mReadStream);
235 break;
236 case RS_A3D_CLASS_ID_ELEMENT:
237 entry->mRsObj = Element::createFromStream(mRSC, mReadStream);
238 break;
239 case RS_A3D_CLASS_ID_ALLOCATION:
240 entry->mRsObj = Allocation::createFromStream(mRSC, mReadStream);
241 break;
242 case RS_A3D_CLASS_ID_PROGRAM_VERTEX:
243 entry->mRsObj = ProgramVertex::createFromStream(mRSC, mReadStream);
244 break;
245 case RS_A3D_CLASS_ID_PROGRAM_RASTER:
246 entry->mRsObj = ProgramRaster::createFromStream(mRSC, mReadStream);
247 break;
248 case RS_A3D_CLASS_ID_PROGRAM_FRAGMENT:
249 entry->mRsObj = ProgramFragment::createFromStream(mRSC, mReadStream);
250 break;
251 case RS_A3D_CLASS_ID_PROGRAM_STORE:
252 entry->mRsObj = ProgramStore::createFromStream(mRSC, mReadStream);
253 break;
254 case RS_A3D_CLASS_ID_SAMPLER:
255 entry->mRsObj = Sampler::createFromStream(mRSC, mReadStream);
256 break;
257 case RS_A3D_CLASS_ID_ANIMATION:
258 entry->mRsObj = Animation::createFromStream(mRSC, mReadStream);
259 break;
Alex Sakhartchoukb825f672010-06-04 10:06:50 -0700260 case RS_A3D_CLASS_ID_ADAPTER_1D:
261 entry->mRsObj = Adapter1D::createFromStream(mRSC, mReadStream);
262 break;
263 case RS_A3D_CLASS_ID_ADAPTER_2D:
264 entry->mRsObj = Adapter2D::createFromStream(mRSC, mReadStream);
265 break;
266 case RS_A3D_CLASS_ID_SCRIPT_C:
Alex Sakhartchoukfb6b6142010-05-21 12:53:13 -0700267 return NULL;
268 }
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800269 if (entry->mRsObj) {
Alex Sakhartchoukb825f672010-06-04 10:06:50 -0700270 entry->mRsObj->incUserRef();
271 }
272 return entry->mRsObj;
Alex Sakhartchoukfb6b6142010-05-21 12:53:13 -0700273}
274
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800275bool FileA3D::writeFile(const char *filename) {
276 if (!mWriteStream) {
Alex Sakhartchoukfb6b6142010-05-21 12:53:13 -0700277 LOGE("No objects to write\n");
278 return false;
279 }
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800280 if (mWriteStream->getPos() == 0) {
Alex Sakhartchoukfb6b6142010-05-21 12:53:13 -0700281 LOGE("No objects to write\n");
282 return false;
283 }
Jason Samsa5597fc2009-07-08 18:01:53 -0700284
Alex Sakhartchoukfb6b6142010-05-21 12:53:13 -0700285 FILE *writeHandle = fopen(filename, "wb");
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800286 if (!writeHandle) {
Alex Sakhartchoukfb6b6142010-05-21 12:53:13 -0700287 LOGE("Couldn't open the file for writing\n");
288 return false;
289 }
Jason Samsa5597fc2009-07-08 18:01:53 -0700290
Alex Sakhartchoukfb6b6142010-05-21 12:53:13 -0700291 // Open a new stream to make writing the header easier
292 OStream headerStream(5*1024, false);
293 headerStream.addU32(mMajorVersion);
294 headerStream.addU32(mMinorVersion);
295 uint32_t is64Bit = 0;
296 headerStream.addU32(is64Bit);
Jason Samsa5597fc2009-07-08 18:01:53 -0700297
Alex Sakhartchoukfb6b6142010-05-21 12:53:13 -0700298 uint32_t writeIndexSize = mWriteIndex.size();
299 headerStream.addU32(writeIndexSize);
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800300 for (uint32_t i = 0; i < writeIndexSize; i ++) {
Alex Sakhartchoukb825f672010-06-04 10:06:50 -0700301 headerStream.addString(&mWriteIndex[i]->mObjectName);
Alex Sakhartchoukfb6b6142010-05-21 12:53:13 -0700302 headerStream.addU32((uint32_t)mWriteIndex[i]->mType);
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800303 if (mUse64BitOffsets){
Alex Sakhartchoukfb6b6142010-05-21 12:53:13 -0700304 headerStream.addOffset(mWriteIndex[i]->mOffset);
Alex Sakhartchoukb825f672010-06-04 10:06:50 -0700305 headerStream.addOffset(mWriteIndex[i]->mLength);
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800306 } else {
Alex Sakhartchoukfb6b6142010-05-21 12:53:13 -0700307 uint32_t offset = (uint32_t)mWriteIndex[i]->mOffset;
308 headerStream.addU32(offset);
Alex Sakhartchoukb825f672010-06-04 10:06:50 -0700309 offset = (uint32_t)mWriteIndex[i]->mLength;
310 headerStream.addU32(offset);
Jason Samsa5597fc2009-07-08 18:01:53 -0700311 }
312 }
313
Alex Sakhartchoukfb6b6142010-05-21 12:53:13 -0700314 // Write our magic string so we know we are reading the right file
315 String8 magicString(A3D_MAGIC_KEY);
316 fwrite(magicString.string(), sizeof(char), magicString.size(), writeHandle);
Jason Samsa5597fc2009-07-08 18:01:53 -0700317
Alex Sakhartchoukfb6b6142010-05-21 12:53:13 -0700318 // Store the size of the header to make it easier to parse when we read it
319 uint64_t headerSize = headerStream.getPos();
320 fwrite(&headerSize, sizeof(headerSize), 1, writeHandle);
Jason Samsa5597fc2009-07-08 18:01:53 -0700321
Alex Sakhartchoukfb6b6142010-05-21 12:53:13 -0700322 // Now write our header
323 fwrite(headerStream.getPtr(), sizeof(uint8_t), headerStream.getPos(), writeHandle);
Jason Samsa5597fc2009-07-08 18:01:53 -0700324
Alex Sakhartchoukfb6b6142010-05-21 12:53:13 -0700325 // Now write the size of the data part of the file for easier parsing later
326 uint64_t fileDataSize = mWriteStream->getPos();
327 fwrite(&fileDataSize, sizeof(fileDataSize), 1, writeHandle);
328
329 fwrite(mWriteStream->getPtr(), sizeof(uint8_t), mWriteStream->getPos(), writeHandle);
330
331 int status = fclose(writeHandle);
332
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800333 if (status != 0) {
Alex Sakhartchoukfb6b6142010-05-21 12:53:13 -0700334 LOGE("Couldn't close file\n");
335 return false;
Jason Samsa5597fc2009-07-08 18:01:53 -0700336 }
337
Alex Sakhartchoukfb6b6142010-05-21 12:53:13 -0700338 return true;
Jason Samsa5597fc2009-07-08 18:01:53 -0700339}
340
Alex Sakhartchoukfb6b6142010-05-21 12:53:13 -0700341void FileA3D::appendToFile(ObjectBase *obj) {
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800342 if (!obj) {
Alex Sakhartchoukfb6b6142010-05-21 12:53:13 -0700343 return;
Jason Samsa5597fc2009-07-08 18:01:53 -0700344 }
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800345 if (!mWriteStream) {
Alex Sakhartchoukfb6b6142010-05-21 12:53:13 -0700346 const uint64_t initialStreamSize = 256*1024;
347 mWriteStream = new OStream(initialStreamSize, false);
Jason Samsa5597fc2009-07-08 18:01:53 -0700348 }
Alex Sakhartchoukfb6b6142010-05-21 12:53:13 -0700349 A3DIndexEntry *indexEntry = new A3DIndexEntry();
Alex Sakhartchoukb825f672010-06-04 10:06:50 -0700350 indexEntry->mObjectName.setTo(obj->getName());
Alex Sakhartchoukfb6b6142010-05-21 12:53:13 -0700351 indexEntry->mType = obj->getClassId();
352 indexEntry->mOffset = mWriteStream->getPos();
Alex Sakhartchoukb825f672010-06-04 10:06:50 -0700353 indexEntry->mRsObj = obj;
Alex Sakhartchoukfb6b6142010-05-21 12:53:13 -0700354 mWriteIndex.push(indexEntry);
355 obj->serialize(mWriteStream);
Alex Sakhartchoukb825f672010-06-04 10:06:50 -0700356 indexEntry->mLength = mWriteStream->getPos() - indexEntry->mOffset;
357 mWriteStream->align(4);
Jason Samsa5597fc2009-07-08 18:01:53 -0700358}
359
360namespace android {
361namespace renderscript {
362
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800363RsFile rsi_FileOpen(Context *rsc, char const *path, unsigned int len) {
Alex Sakhartchoukdc763f32010-10-27 14:10:07 -0700364 FileA3D *fa3d = new FileA3D(rsc);
365
366 FILE *f = fopen("/sdcard/test.a3d", "rb");
367 if (f) {
368 fa3d->load(f);
369 fclose(f);
370 fa3d->incUserRef();
371 return fa3d;
372 }
373 delete fa3d;
374 return NULL;
375}
376
Alex Sakhartchoukdc763f32010-10-27 14:10:07 -0700377}
378}
379
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800380RsObjectBase rsaFileA3DGetEntryByIndex(RsContext con, uint32_t index, RsFile file) {
Alex Sakhartchoukdc763f32010-10-27 14:10:07 -0700381 FileA3D *fa3d = static_cast<FileA3D *>(file);
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800382 if (!fa3d) {
Alex Sakhartchoukdc763f32010-10-27 14:10:07 -0700383 LOGE("Can't load entry. No valid file");
384 return NULL;
385 }
386
387 ObjectBase *obj = fa3d->initializeFromEntry(index);
388 LOGV("Returning object with name %s", obj->getName());
389
390 return obj;
391}
392
393
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800394void rsaFileA3DGetNumIndexEntries(RsContext con, int32_t *numEntries, RsFile file) {
Alex Sakhartchoukb825f672010-06-04 10:06:50 -0700395 FileA3D *fa3d = static_cast<FileA3D *>(file);
396
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800397 if (fa3d) {
Alex Sakhartchoukb825f672010-06-04 10:06:50 -0700398 *numEntries = fa3d->getNumIndexEntries();
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800399 } else {
Alex Sakhartchoukb825f672010-06-04 10:06:50 -0700400 *numEntries = 0;
401 }
402}
403
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800404void rsaFileA3DGetIndexEntries(RsContext con, RsFileIndexEntry *fileEntries, uint32_t numEntries, RsFile file) {
Alex Sakhartchoukb825f672010-06-04 10:06:50 -0700405 FileA3D *fa3d = static_cast<FileA3D *>(file);
406
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800407 if (!fa3d) {
Alex Sakhartchoukb825f672010-06-04 10:06:50 -0700408 LOGE("Can't load index entries. No valid file");
409 return;
410 }
411
412 uint32_t numFileEntries = fa3d->getNumIndexEntries();
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800413 if (numFileEntries != numEntries || numEntries == 0 || fileEntries == NULL) {
Alex Sakhartchoukb825f672010-06-04 10:06:50 -0700414 LOGE("Can't load index entries. Invalid number requested");
415 return;
416 }
417
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800418 for (uint32_t i = 0; i < numFileEntries; i ++) {
Alex Sakhartchoukb825f672010-06-04 10:06:50 -0700419 const FileA3D::A3DIndexEntry *entry = fa3d->getIndexEntry(i);
420 fileEntries[i].classID = entry->getType();
421 fileEntries[i].objectName = entry->getObjectName().string();
422 }
Alex Sakhartchoukb825f672010-06-04 10:06:50 -0700423}
424
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800425RsFile rsaFileA3DCreateFromAssetStream(RsContext con, const void *data, uint32_t len) {
Alex Sakhartchoukb825f672010-06-04 10:06:50 -0700426 if (data == NULL) {
427 LOGE("File load failed. Asset stream is NULL");
428 return NULL;
429 }
430
Alex Sakhartchoukdc763f32010-10-27 14:10:07 -0700431 Context *rsc = static_cast<Context *>(con);
Alex Sakhartchoukb825f672010-06-04 10:06:50 -0700432 FileA3D *fa3d = new FileA3D(rsc);
Alex Sakhartchoukb825f672010-06-04 10:06:50 -0700433 fa3d->incUserRef();
434
Alex Sakhartchoukdc763f32010-10-27 14:10:07 -0700435 fa3d->load(data, len);
Alex Sakhartchoukb825f672010-06-04 10:06:50 -0700436 return fa3d;
437}