1a5597fcd411badad50c1345b3eca26453da45bbbJason Sams
2a5597fcd411badad50c1345b3eca26453da45bbbJason Sams/*
3a5597fcd411badad50c1345b3eca26453da45bbbJason Sams * Copyright (C) 2009 The Android Open Source Project
4a5597fcd411badad50c1345b3eca26453da45bbbJason Sams *
5a5597fcd411badad50c1345b3eca26453da45bbbJason Sams * Licensed under the Apache License, Version 2.0 (the "License");
6a5597fcd411badad50c1345b3eca26453da45bbbJason Sams * you may not use this file except in compliance with the License.
7a5597fcd411badad50c1345b3eca26453da45bbbJason Sams * You may obtain a copy of the License at
8a5597fcd411badad50c1345b3eca26453da45bbbJason Sams *
9a5597fcd411badad50c1345b3eca26453da45bbbJason Sams *      http://www.apache.org/licenses/LICENSE-2.0
10a5597fcd411badad50c1345b3eca26453da45bbbJason Sams *
11a5597fcd411badad50c1345b3eca26453da45bbbJason Sams * Unless required by applicable law or agreed to in writing, software
12a5597fcd411badad50c1345b3eca26453da45bbbJason Sams * distributed under the License is distributed on an "AS IS" BASIS,
13a5597fcd411badad50c1345b3eca26453da45bbbJason Sams * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14a5597fcd411badad50c1345b3eca26453da45bbbJason Sams * See the License for the specific language governing permissions and
15a5597fcd411badad50c1345b3eca26453da45bbbJason Sams * limitations under the License.
16a5597fcd411badad50c1345b3eca26453da45bbbJason Sams */
17a5597fcd411badad50c1345b3eca26453da45bbbJason Sams
18a5597fcd411badad50c1345b3eca26453da45bbbJason Sams#include "rsContext.h"
19a5597fcd411badad50c1345b3eca26453da45bbbJason Sams#include "rsFileA3D.h"
20a5597fcd411badad50c1345b3eca26453da45bbbJason Sams
21a5597fcd411badad50c1345b3eca26453da45bbbJason Sams#include "rsMesh.h"
22fb6b614bcea88a587a7ea4530be45ff0ffa0210eAlex Sakhartchouk#include "rsAnimation.h"
23e23d239828a229eb7d4d33c9630070f0a87833e1Alex Sakhartchouk#include "rs.h"
24d3e0ad43dc758c409fc23d1893dab67b18520c24Alex Sakhartchouk
25a6ab26a035017143a24efa11859c9d8815a27f41Jason Sams#if !defined(__RS_PDK__)
26a6ab26a035017143a24efa11859c9d8815a27f41Jason Sams    #include <androidfw/Asset.h>
27a6ab26a035017143a24efa11859c9d8815a27f41Jason Sams#endif
28a6ab26a035017143a24efa11859c9d8815a27f41Jason Sams
29f8852d0494a260c583795a96a2a06c49b86a9b10Ian Rogers#include <inttypes.h>
30f8852d0494a260c583795a96a2a06c49b86a9b10Ian Rogers
31a5597fcd411badad50c1345b3eca26453da45bbbJason Samsusing namespace android;
32a5597fcd411badad50c1345b3eca26453da45bbbJason Samsusing namespace android::renderscript;
33a5597fcd411badad50c1345b3eca26453da45bbbJason Sams
34afb743aca56c18beb7ab924e75cb6e070ef3e55aAlex SakhartchoukFileA3D::FileA3D(Context *rsc) : ObjectBase(rsc) {
3544bef6fba6244292b751387f3d6c31cca96c28adChris Wailes    mAlloc = nullptr;
3644bef6fba6244292b751387f3d6c31cca96c28adChris Wailes    mData = nullptr;
3744bef6fba6244292b751387f3d6c31cca96c28adChris Wailes    mWriteStream = nullptr;
3844bef6fba6244292b751387f3d6c31cca96c28adChris Wailes    mReadStream = nullptr;
3944bef6fba6244292b751387f3d6c31cca96c28adChris Wailes    mAsset = nullptr;
40fb6b614bcea88a587a7ea4530be45ff0ffa0210eAlex Sakhartchouk
41fb6b614bcea88a587a7ea4530be45ff0ffa0210eAlex Sakhartchouk    mMajorVersion = 0;
42fb6b614bcea88a587a7ea4530be45ff0ffa0210eAlex Sakhartchouk    mMinorVersion = 1;
43fb6b614bcea88a587a7ea4530be45ff0ffa0210eAlex Sakhartchouk    mDataSize = 0;
44a5597fcd411badad50c1345b3eca26453da45bbbJason Sams}
45a5597fcd411badad50c1345b3eca26453da45bbbJason Sams
46afb743aca56c18beb7ab924e75cb6e070ef3e55aAlex SakhartchoukFileA3D::~FileA3D() {
47afb743aca56c18beb7ab924e75cb6e070ef3e55aAlex Sakhartchouk    for (size_t i = 0; i < mIndex.size(); i ++) {
48fb6b614bcea88a587a7ea4530be45ff0ffa0210eAlex Sakhartchouk        delete mIndex[i];
49fb6b614bcea88a587a7ea4530be45ff0ffa0210eAlex Sakhartchouk    }
50afb743aca56c18beb7ab924e75cb6e070ef3e55aAlex Sakhartchouk    for (size_t i = 0; i < mWriteIndex.size(); i ++) {
51fb6b614bcea88a587a7ea4530be45ff0ffa0210eAlex Sakhartchouk        delete mWriteIndex[i];
52fb6b614bcea88a587a7ea4530be45ff0ffa0210eAlex Sakhartchouk    }
53afb743aca56c18beb7ab924e75cb6e070ef3e55aAlex Sakhartchouk    if (mWriteStream) {
54fb6b614bcea88a587a7ea4530be45ff0ffa0210eAlex Sakhartchouk        delete mWriteStream;
55fb6b614bcea88a587a7ea4530be45ff0ffa0210eAlex Sakhartchouk    }
56afb743aca56c18beb7ab924e75cb6e070ef3e55aAlex Sakhartchouk    if (mReadStream) {
575bb6dc1933e11cb57e5f4205d13686ad113b205eMiao Wang        delete mReadStream;
58fb6b614bcea88a587a7ea4530be45ff0ffa0210eAlex Sakhartchouk    }
59afb743aca56c18beb7ab924e75cb6e070ef3e55aAlex Sakhartchouk    if (mAlloc) {
60fb6b614bcea88a587a7ea4530be45ff0ffa0210eAlex Sakhartchouk        free(mAlloc);
61fb6b614bcea88a587a7ea4530be45ff0ffa0210eAlex Sakhartchouk    }
625224a27798f89093b13722b41143551a057ce550Alex Sakhartchouk    if (mAsset) {
63a6ab26a035017143a24efa11859c9d8815a27f41Jason Sams#if !defined(__RS_PDK__)
645224a27798f89093b13722b41143551a057ce550Alex Sakhartchouk        delete mAsset;
65a6ab26a035017143a24efa11859c9d8815a27f41Jason Sams#endif
665224a27798f89093b13722b41143551a057ce550Alex Sakhartchouk    }
67a5597fcd411badad50c1345b3eca26453da45bbbJason Sams}
68a5597fcd411badad50c1345b3eca26453da45bbbJason Sams
69afb743aca56c18beb7ab924e75cb6e070ef3e55aAlex Sakhartchoukvoid FileA3D::parseHeader(IStream *headerStream) {
70b825f67adb5d1e1751fe108e6dbf9c6f2555c283Alex Sakhartchouk    mMajorVersion = headerStream->loadU32();
71b825f67adb5d1e1751fe108e6dbf9c6f2555c283Alex Sakhartchouk    mMinorVersion = headerStream->loadU32();
72b825f67adb5d1e1751fe108e6dbf9c6f2555c283Alex Sakhartchouk    uint32_t flags = headerStream->loadU32();
73b825f67adb5d1e1751fe108e6dbf9c6f2555c283Alex Sakhartchouk    mUse64BitOffsets = (flags & 1) != 0;
74b825f67adb5d1e1751fe108e6dbf9c6f2555c283Alex Sakhartchouk
75b825f67adb5d1e1751fe108e6dbf9c6f2555c283Alex Sakhartchouk    uint32_t numIndexEntries = headerStream->loadU32();
76afb743aca56c18beb7ab924e75cb6e070ef3e55aAlex Sakhartchouk    for (uint32_t i = 0; i < numIndexEntries; i ++) {
77b825f67adb5d1e1751fe108e6dbf9c6f2555c283Alex Sakhartchouk        A3DIndexEntry *entry = new A3DIndexEntry();
7848ecf6a5e85a9a832f41393ed2802385bb8b5db8Jason Sams        entry->mObjectName = headerStream->loadString();
7948ecf6a5e85a9a832f41393ed2802385bb8b5db8Jason Sams
806598201f1c4f409defac9a5af789fb53a7cc00f8Steve Block        //ALOGV("Header data, entry name = %s", entry->mObjectName.string());
81b825f67adb5d1e1751fe108e6dbf9c6f2555c283Alex Sakhartchouk        entry->mType = (RsA3DClassID)headerStream->loadU32();
82afb743aca56c18beb7ab924e75cb6e070ef3e55aAlex Sakhartchouk        if (mUse64BitOffsets){
83b825f67adb5d1e1751fe108e6dbf9c6f2555c283Alex Sakhartchouk            entry->mOffset = headerStream->loadOffset();
84b825f67adb5d1e1751fe108e6dbf9c6f2555c283Alex Sakhartchouk            entry->mLength = headerStream->loadOffset();
85afb743aca56c18beb7ab924e75cb6e070ef3e55aAlex Sakhartchouk        } else {
86b825f67adb5d1e1751fe108e6dbf9c6f2555c283Alex Sakhartchouk            entry->mOffset = headerStream->loadU32();
87b825f67adb5d1e1751fe108e6dbf9c6f2555c283Alex Sakhartchouk            entry->mLength = headerStream->loadU32();
88b825f67adb5d1e1751fe108e6dbf9c6f2555c283Alex Sakhartchouk        }
8944bef6fba6244292b751387f3d6c31cca96c28adChris Wailes        entry->mRsObj = nullptr;
90b8353c5943f4038fd7f08db3d958390ce9418798Yang Ni        mIndex.push(entry);
91b825f67adb5d1e1751fe108e6dbf9c6f2555c283Alex Sakhartchouk    }
92b825f67adb5d1e1751fe108e6dbf9c6f2555c283Alex Sakhartchouk}
93b825f67adb5d1e1751fe108e6dbf9c6f2555c283Alex Sakhartchouk
945224a27798f89093b13722b41143551a057ce550Alex Sakhartchoukbool FileA3D::load(Asset *asset) {
95a6ab26a035017143a24efa11859c9d8815a27f41Jason Sams#if !defined(__RS_PDK__)
965224a27798f89093b13722b41143551a057ce550Alex Sakhartchouk    mAsset = asset;
975224a27798f89093b13722b41143551a057ce550Alex Sakhartchouk    return load(asset->getBuffer(false), asset->getLength());
98a6ab26a035017143a24efa11859c9d8815a27f41Jason Sams#else
99a6ab26a035017143a24efa11859c9d8815a27f41Jason Sams    return false;
100a6ab26a035017143a24efa11859c9d8815a27f41Jason Sams#endif
1015224a27798f89093b13722b41143551a057ce550Alex Sakhartchouk}
1025224a27798f89093b13722b41143551a057ce550Alex Sakhartchouk
103afb743aca56c18beb7ab924e75cb6e070ef3e55aAlex Sakhartchoukbool FileA3D::load(const void *data, size_t length) {
104b825f67adb5d1e1751fe108e6dbf9c6f2555c283Alex Sakhartchouk    const uint8_t *localData = (const uint8_t *)data;
105b825f67adb5d1e1751fe108e6dbf9c6f2555c283Alex Sakhartchouk
106b825f67adb5d1e1751fe108e6dbf9c6f2555c283Alex Sakhartchouk    size_t lengthRemaining = length;
107b825f67adb5d1e1751fe108e6dbf9c6f2555c283Alex Sakhartchouk    size_t magicStrLen = 12;
108b825f67adb5d1e1751fe108e6dbf9c6f2555c283Alex Sakhartchouk    if ((length < magicStrLen) ||
109b825f67adb5d1e1751fe108e6dbf9c6f2555c283Alex Sakhartchouk        memcmp(data, "Android3D_ff", magicStrLen)) {
110b825f67adb5d1e1751fe108e6dbf9c6f2555c283Alex Sakhartchouk        return false;
111b825f67adb5d1e1751fe108e6dbf9c6f2555c283Alex Sakhartchouk    }
112b825f67adb5d1e1751fe108e6dbf9c6f2555c283Alex Sakhartchouk
113b825f67adb5d1e1751fe108e6dbf9c6f2555c283Alex Sakhartchouk    localData += magicStrLen;
114b825f67adb5d1e1751fe108e6dbf9c6f2555c283Alex Sakhartchouk    lengthRemaining -= magicStrLen;
115b825f67adb5d1e1751fe108e6dbf9c6f2555c283Alex Sakhartchouk
116b825f67adb5d1e1751fe108e6dbf9c6f2555c283Alex Sakhartchouk    // Next we get our header size
117b825f67adb5d1e1751fe108e6dbf9c6f2555c283Alex Sakhartchouk    uint64_t headerSize = 0;
118afb743aca56c18beb7ab924e75cb6e070ef3e55aAlex Sakhartchouk    if (lengthRemaining < sizeof(headerSize)) {
119b825f67adb5d1e1751fe108e6dbf9c6f2555c283Alex Sakhartchouk        return false;
120b825f67adb5d1e1751fe108e6dbf9c6f2555c283Alex Sakhartchouk    }
121b825f67adb5d1e1751fe108e6dbf9c6f2555c283Alex Sakhartchouk
122b825f67adb5d1e1751fe108e6dbf9c6f2555c283Alex Sakhartchouk    memcpy(&headerSize, localData, sizeof(headerSize));
123b825f67adb5d1e1751fe108e6dbf9c6f2555c283Alex Sakhartchouk    localData += sizeof(headerSize);
124b825f67adb5d1e1751fe108e6dbf9c6f2555c283Alex Sakhartchouk    lengthRemaining -= sizeof(headerSize);
125b825f67adb5d1e1751fe108e6dbf9c6f2555c283Alex Sakhartchouk
126afb743aca56c18beb7ab924e75cb6e070ef3e55aAlex Sakhartchouk    if (lengthRemaining < headerSize) {
127b825f67adb5d1e1751fe108e6dbf9c6f2555c283Alex Sakhartchouk        return false;
128b825f67adb5d1e1751fe108e6dbf9c6f2555c283Alex Sakhartchouk    }
129b825f67adb5d1e1751fe108e6dbf9c6f2555c283Alex Sakhartchouk
130b825f67adb5d1e1751fe108e6dbf9c6f2555c283Alex Sakhartchouk    // Now open the stream to parse the header
131dc763f345db3e796efc28dc4b4d8edffda5a803eAlex Sakhartchouk    IStream headerStream(localData, false);
132b825f67adb5d1e1751fe108e6dbf9c6f2555c283Alex Sakhartchouk    parseHeader(&headerStream);
133b825f67adb5d1e1751fe108e6dbf9c6f2555c283Alex Sakhartchouk
134b825f67adb5d1e1751fe108e6dbf9c6f2555c283Alex Sakhartchouk    localData += headerSize;
135b825f67adb5d1e1751fe108e6dbf9c6f2555c283Alex Sakhartchouk    lengthRemaining -= headerSize;
136b825f67adb5d1e1751fe108e6dbf9c6f2555c283Alex Sakhartchouk
137afb743aca56c18beb7ab924e75cb6e070ef3e55aAlex Sakhartchouk    if (lengthRemaining < sizeof(mDataSize)) {
138b825f67adb5d1e1751fe108e6dbf9c6f2555c283Alex Sakhartchouk        return false;
139b825f67adb5d1e1751fe108e6dbf9c6f2555c283Alex Sakhartchouk    }
140b825f67adb5d1e1751fe108e6dbf9c6f2555c283Alex Sakhartchouk
141b825f67adb5d1e1751fe108e6dbf9c6f2555c283Alex Sakhartchouk    // Read the size of the data
142b825f67adb5d1e1751fe108e6dbf9c6f2555c283Alex Sakhartchouk    memcpy(&mDataSize, localData, sizeof(mDataSize));
143b825f67adb5d1e1751fe108e6dbf9c6f2555c283Alex Sakhartchouk    localData += sizeof(mDataSize);
144b825f67adb5d1e1751fe108e6dbf9c6f2555c283Alex Sakhartchouk    lengthRemaining -= sizeof(mDataSize);
145b825f67adb5d1e1751fe108e6dbf9c6f2555c283Alex Sakhartchouk
146afb743aca56c18beb7ab924e75cb6e070ef3e55aAlex Sakhartchouk    if (lengthRemaining < mDataSize) {
147b825f67adb5d1e1751fe108e6dbf9c6f2555c283Alex Sakhartchouk        return false;
148b825f67adb5d1e1751fe108e6dbf9c6f2555c283Alex Sakhartchouk    }
149b825f67adb5d1e1751fe108e6dbf9c6f2555c283Alex Sakhartchouk
150b825f67adb5d1e1751fe108e6dbf9c6f2555c283Alex Sakhartchouk    // We should know enough to read the file in at this point.
151dc763f345db3e796efc28dc4b4d8edffda5a803eAlex Sakhartchouk    mData = (uint8_t *)localData;
152b825f67adb5d1e1751fe108e6dbf9c6f2555c283Alex Sakhartchouk    mReadStream = new IStream(mData, mUse64BitOffsets);
153b825f67adb5d1e1751fe108e6dbf9c6f2555c283Alex Sakhartchouk
154b825f67adb5d1e1751fe108e6dbf9c6f2555c283Alex Sakhartchouk    return true;
155b825f67adb5d1e1751fe108e6dbf9c6f2555c283Alex Sakhartchouk}
156b825f67adb5d1e1751fe108e6dbf9c6f2555c283Alex Sakhartchouk
157afb743aca56c18beb7ab924e75cb6e070ef3e55aAlex Sakhartchoukbool FileA3D::load(FILE *f) {
158a5597fcd411badad50c1345b3eca26453da45bbbJason Sams    char magicString[12];
159a5597fcd411badad50c1345b3eca26453da45bbbJason Sams    size_t len;
160a5597fcd411badad50c1345b3eca26453da45bbbJason Sams
1616598201f1c4f409defac9a5af789fb53a7cc00f8Steve Block    ALOGV("file open 1");
162a5597fcd411badad50c1345b3eca26453da45bbbJason Sams    len = fread(magicString, 1, 12, f);
163a5597fcd411badad50c1345b3eca26453da45bbbJason Sams    if ((len != 12) ||
164a5597fcd411badad50c1345b3eca26453da45bbbJason Sams        memcmp(magicString, "Android3D_ff", 12)) {
165a5597fcd411badad50c1345b3eca26453da45bbbJason Sams        return false;
166a5597fcd411badad50c1345b3eca26453da45bbbJason Sams    }
167a5597fcd411badad50c1345b3eca26453da45bbbJason Sams
168fb6b614bcea88a587a7ea4530be45ff0ffa0210eAlex Sakhartchouk    // Next thing is the size of the header
169fb6b614bcea88a587a7ea4530be45ff0ffa0210eAlex Sakhartchouk    uint64_t headerSize = 0;
170fb6b614bcea88a587a7ea4530be45ff0ffa0210eAlex Sakhartchouk    len = fread(&headerSize, 1, sizeof(headerSize), f);
171fb6b614bcea88a587a7ea4530be45ff0ffa0210eAlex Sakhartchouk    if (len != sizeof(headerSize) || headerSize == 0) {
172a5597fcd411badad50c1345b3eca26453da45bbbJason Sams        return false;
173a5597fcd411badad50c1345b3eca26453da45bbbJason Sams    }
174a5597fcd411badad50c1345b3eca26453da45bbbJason Sams
175fb6b614bcea88a587a7ea4530be45ff0ffa0210eAlex Sakhartchouk    uint8_t *headerData = (uint8_t *)malloc(headerSize);
176afb743aca56c18beb7ab924e75cb6e070ef3e55aAlex Sakhartchouk    if (!headerData) {
177a5597fcd411badad50c1345b3eca26453da45bbbJason Sams        return false;
178a5597fcd411badad50c1345b3eca26453da45bbbJason Sams    }
179a5597fcd411badad50c1345b3eca26453da45bbbJason Sams
180fb6b614bcea88a587a7ea4530be45ff0ffa0210eAlex Sakhartchouk    len = fread(headerData, 1, headerSize, f);
181fb6b614bcea88a587a7ea4530be45ff0ffa0210eAlex Sakhartchouk    if (len != headerSize) {
182a5597fcd411badad50c1345b3eca26453da45bbbJason Sams        return false;
183a5597fcd411badad50c1345b3eca26453da45bbbJason Sams    }
184fb6b614bcea88a587a7ea4530be45ff0ffa0210eAlex Sakhartchouk
185fb6b614bcea88a587a7ea4530be45ff0ffa0210eAlex Sakhartchouk    // Now open the stream to parse the header
186fb6b614bcea88a587a7ea4530be45ff0ffa0210eAlex Sakhartchouk    IStream headerStream(headerData, false);
187b825f67adb5d1e1751fe108e6dbf9c6f2555c283Alex Sakhartchouk    parseHeader(&headerStream);
188fb6b614bcea88a587a7ea4530be45ff0ffa0210eAlex Sakhartchouk
189b825f67adb5d1e1751fe108e6dbf9c6f2555c283Alex Sakhartchouk    free(headerData);
190fb6b614bcea88a587a7ea4530be45ff0ffa0210eAlex Sakhartchouk
191fb6b614bcea88a587a7ea4530be45ff0ffa0210eAlex Sakhartchouk    // Next thing is the size of the header
192fb6b614bcea88a587a7ea4530be45ff0ffa0210eAlex Sakhartchouk    len = fread(&mDataSize, 1, sizeof(mDataSize), f);
193fb6b614bcea88a587a7ea4530be45ff0ffa0210eAlex Sakhartchouk    if (len != sizeof(mDataSize) || mDataSize == 0) {
194fb6b614bcea88a587a7ea4530be45ff0ffa0210eAlex Sakhartchouk        return false;
195a5597fcd411badad50c1345b3eca26453da45bbbJason Sams    }
196a5597fcd411badad50c1345b3eca26453da45bbbJason Sams
197f8852d0494a260c583795a96a2a06c49b86a9b10Ian Rogers    ALOGV("file open size = %" PRIi64, mDataSize);
198a5597fcd411badad50c1345b3eca26453da45bbbJason Sams
199a5597fcd411badad50c1345b3eca26453da45bbbJason Sams    // We should know enough to read the file in at this point.
200fb6b614bcea88a587a7ea4530be45ff0ffa0210eAlex Sakhartchouk    mAlloc = malloc(mDataSize);
201a5597fcd411badad50c1345b3eca26453da45bbbJason Sams    if (!mAlloc) {
202a5597fcd411badad50c1345b3eca26453da45bbbJason Sams        return false;
203a5597fcd411badad50c1345b3eca26453da45bbbJason Sams    }
204a5597fcd411badad50c1345b3eca26453da45bbbJason Sams    mData = (uint8_t *)mAlloc;
205a5597fcd411badad50c1345b3eca26453da45bbbJason Sams    len = fread(mAlloc, 1, mDataSize, f);
206a5597fcd411badad50c1345b3eca26453da45bbbJason Sams    if (len != mDataSize) {
207a5597fcd411badad50c1345b3eca26453da45bbbJason Sams        return false;
208a5597fcd411badad50c1345b3eca26453da45bbbJason Sams    }
209a5597fcd411badad50c1345b3eca26453da45bbbJason Sams
210fb6b614bcea88a587a7ea4530be45ff0ffa0210eAlex Sakhartchouk    mReadStream = new IStream(mData, mUse64BitOffsets);
211fb6b614bcea88a587a7ea4530be45ff0ffa0210eAlex Sakhartchouk
2126598201f1c4f409defac9a5af789fb53a7cc00f8Steve Block    ALOGV("Header is read an stream initialized");
213fb6b614bcea88a587a7ea4530be45ff0ffa0210eAlex Sakhartchouk    return true;
214a5597fcd411badad50c1345b3eca26453da45bbbJason Sams}
215a5597fcd411badad50c1345b3eca26453da45bbbJason Sams
216b825f67adb5d1e1751fe108e6dbf9c6f2555c283Alex Sakhartchouksize_t FileA3D::getNumIndexEntries() const {
217fb6b614bcea88a587a7ea4530be45ff0ffa0210eAlex Sakhartchouk    return mIndex.size();
218a5597fcd411badad50c1345b3eca26453da45bbbJason Sams}
219a5597fcd411badad50c1345b3eca26453da45bbbJason Sams
22048ecf6a5e85a9a832f41393ed2802385bb8b5db8Jason SamsFileA3D::A3DIndexEntry::~A3DIndexEntry() {
22148ecf6a5e85a9a832f41393ed2802385bb8b5db8Jason Sams    delete[] mObjectName;
22248ecf6a5e85a9a832f41393ed2802385bb8b5db8Jason Sams}
22348ecf6a5e85a9a832f41393ed2802385bb8b5db8Jason Sams
224b825f67adb5d1e1751fe108e6dbf9c6f2555c283Alex Sakhartchoukconst FileA3D::A3DIndexEntry *FileA3D::getIndexEntry(size_t index) const {
225afb743aca56c18beb7ab924e75cb6e070ef3e55aAlex Sakhartchouk    if (index < mIndex.size()) {
226fb6b614bcea88a587a7ea4530be45ff0ffa0210eAlex Sakhartchouk        return mIndex[index];
227a5597fcd411badad50c1345b3eca26453da45bbbJason Sams    }
22844bef6fba6244292b751387f3d6c31cca96c28adChris Wailes    return nullptr;
229fb6b614bcea88a587a7ea4530be45ff0ffa0210eAlex Sakhartchouk}
230a5597fcd411badad50c1345b3eca26453da45bbbJason Sams
231b825f67adb5d1e1751fe108e6dbf9c6f2555c283Alex SakhartchoukObjectBase *FileA3D::initializeFromEntry(size_t index) {
232afb743aca56c18beb7ab924e75cb6e070ef3e55aAlex Sakhartchouk    if (index >= mIndex.size()) {
23344bef6fba6244292b751387f3d6c31cca96c28adChris Wailes        return nullptr;
234b825f67adb5d1e1751fe108e6dbf9c6f2555c283Alex Sakhartchouk    }
235b825f67adb5d1e1751fe108e6dbf9c6f2555c283Alex Sakhartchouk
236b825f67adb5d1e1751fe108e6dbf9c6f2555c283Alex Sakhartchouk    FileA3D::A3DIndexEntry *entry = mIndex[index];
237afb743aca56c18beb7ab924e75cb6e070ef3e55aAlex Sakhartchouk    if (!entry) {
23844bef6fba6244292b751387f3d6c31cca96c28adChris Wailes        return nullptr;
239a5597fcd411badad50c1345b3eca26453da45bbbJason Sams    }
240a5597fcd411badad50c1345b3eca26453da45bbbJason Sams
241afb743aca56c18beb7ab924e75cb6e070ef3e55aAlex Sakhartchouk    if (entry->mRsObj) {
242b825f67adb5d1e1751fe108e6dbf9c6f2555c283Alex Sakhartchouk        entry->mRsObj->incUserRef();
243b825f67adb5d1e1751fe108e6dbf9c6f2555c283Alex Sakhartchouk        return entry->mRsObj;
244b825f67adb5d1e1751fe108e6dbf9c6f2555c283Alex Sakhartchouk    }
245b825f67adb5d1e1751fe108e6dbf9c6f2555c283Alex Sakhartchouk
246fb6b614bcea88a587a7ea4530be45ff0ffa0210eAlex Sakhartchouk    // Seek to the beginning of object
247fb6b614bcea88a587a7ea4530be45ff0ffa0210eAlex Sakhartchouk    mReadStream->reset(entry->mOffset);
248fb6b614bcea88a587a7ea4530be45ff0ffa0210eAlex Sakhartchouk    switch (entry->mType) {
249b825f67adb5d1e1751fe108e6dbf9c6f2555c283Alex Sakhartchouk        case RS_A3D_CLASS_ID_UNKNOWN:
25044bef6fba6244292b751387f3d6c31cca96c28adChris Wailes            return nullptr;
251b825f67adb5d1e1751fe108e6dbf9c6f2555c283Alex Sakhartchouk        case RS_A3D_CLASS_ID_MESH:
252b825f67adb5d1e1751fe108e6dbf9c6f2555c283Alex Sakhartchouk            entry->mRsObj = Mesh::createFromStream(mRSC, mReadStream);
253b825f67adb5d1e1751fe108e6dbf9c6f2555c283Alex Sakhartchouk            break;
254b825f67adb5d1e1751fe108e6dbf9c6f2555c283Alex Sakhartchouk        case RS_A3D_CLASS_ID_TYPE:
255b825f67adb5d1e1751fe108e6dbf9c6f2555c283Alex Sakhartchouk            entry->mRsObj = Type::createFromStream(mRSC, mReadStream);
256b825f67adb5d1e1751fe108e6dbf9c6f2555c283Alex Sakhartchouk            break;
257b825f67adb5d1e1751fe108e6dbf9c6f2555c283Alex Sakhartchouk        case RS_A3D_CLASS_ID_ELEMENT:
258b825f67adb5d1e1751fe108e6dbf9c6f2555c283Alex Sakhartchouk            entry->mRsObj = Element::createFromStream(mRSC, mReadStream);
259b825f67adb5d1e1751fe108e6dbf9c6f2555c283Alex Sakhartchouk            break;
260b825f67adb5d1e1751fe108e6dbf9c6f2555c283Alex Sakhartchouk        case RS_A3D_CLASS_ID_ALLOCATION:
261b825f67adb5d1e1751fe108e6dbf9c6f2555c283Alex Sakhartchouk            entry->mRsObj = Allocation::createFromStream(mRSC, mReadStream);
262b825f67adb5d1e1751fe108e6dbf9c6f2555c283Alex Sakhartchouk            break;
263b825f67adb5d1e1751fe108e6dbf9c6f2555c283Alex Sakhartchouk        case RS_A3D_CLASS_ID_PROGRAM_VERTEX:
264099d7d33e55afeb3399f6e8cf8d665223ca94939Alex Sakhartchouk            //entry->mRsObj = ProgramVertex::createFromStream(mRSC, mReadStream);
265b825f67adb5d1e1751fe108e6dbf9c6f2555c283Alex Sakhartchouk            break;
266b825f67adb5d1e1751fe108e6dbf9c6f2555c283Alex Sakhartchouk        case RS_A3D_CLASS_ID_PROGRAM_RASTER:
267099d7d33e55afeb3399f6e8cf8d665223ca94939Alex Sakhartchouk            //entry->mRsObj = ProgramRaster::createFromStream(mRSC, mReadStream);
268b825f67adb5d1e1751fe108e6dbf9c6f2555c283Alex Sakhartchouk            break;
269b825f67adb5d1e1751fe108e6dbf9c6f2555c283Alex Sakhartchouk        case RS_A3D_CLASS_ID_PROGRAM_FRAGMENT:
270099d7d33e55afeb3399f6e8cf8d665223ca94939Alex Sakhartchouk            //entry->mRsObj = ProgramFragment::createFromStream(mRSC, mReadStream);
271b825f67adb5d1e1751fe108e6dbf9c6f2555c283Alex Sakhartchouk            break;
272b825f67adb5d1e1751fe108e6dbf9c6f2555c283Alex Sakhartchouk        case RS_A3D_CLASS_ID_PROGRAM_STORE:
273099d7d33e55afeb3399f6e8cf8d665223ca94939Alex Sakhartchouk            //entry->mRsObj = ProgramStore::createFromStream(mRSC, mReadStream);
274b825f67adb5d1e1751fe108e6dbf9c6f2555c283Alex Sakhartchouk            break;
275b825f67adb5d1e1751fe108e6dbf9c6f2555c283Alex Sakhartchouk        case RS_A3D_CLASS_ID_SAMPLER:
276099d7d33e55afeb3399f6e8cf8d665223ca94939Alex Sakhartchouk            //entry->mRsObj = Sampler::createFromStream(mRSC, mReadStream);
277b825f67adb5d1e1751fe108e6dbf9c6f2555c283Alex Sakhartchouk            break;
278b825f67adb5d1e1751fe108e6dbf9c6f2555c283Alex Sakhartchouk        case RS_A3D_CLASS_ID_ANIMATION:
279099d7d33e55afeb3399f6e8cf8d665223ca94939Alex Sakhartchouk            //entry->mRsObj = Animation::createFromStream(mRSC, mReadStream);
280b825f67adb5d1e1751fe108e6dbf9c6f2555c283Alex Sakhartchouk            break;
281b825f67adb5d1e1751fe108e6dbf9c6f2555c283Alex Sakhartchouk        case RS_A3D_CLASS_ID_ADAPTER_1D:
282099d7d33e55afeb3399f6e8cf8d665223ca94939Alex Sakhartchouk            //entry->mRsObj = Adapter1D::createFromStream(mRSC, mReadStream);
283b825f67adb5d1e1751fe108e6dbf9c6f2555c283Alex Sakhartchouk            break;
284b825f67adb5d1e1751fe108e6dbf9c6f2555c283Alex Sakhartchouk        case RS_A3D_CLASS_ID_ADAPTER_2D:
285099d7d33e55afeb3399f6e8cf8d665223ca94939Alex Sakhartchouk            //entry->mRsObj = Adapter2D::createFromStream(mRSC, mReadStream);
286b825f67adb5d1e1751fe108e6dbf9c6f2555c283Alex Sakhartchouk            break;
287b825f67adb5d1e1751fe108e6dbf9c6f2555c283Alex Sakhartchouk        case RS_A3D_CLASS_ID_SCRIPT_C:
288099d7d33e55afeb3399f6e8cf8d665223ca94939Alex Sakhartchouk            break;
289dbe66d6783c1e53cd1572de0ef6ef6fdf6f76f48Jason Sams        case RS_A3D_CLASS_ID_SCRIPT_KERNEL_ID:
290dbe66d6783c1e53cd1572de0ef6ef6fdf6f76f48Jason Sams            break;
291eb9aa675754c49f613c6ad71d41472b30f38b007Yang Ni        case RS_A3D_CLASS_ID_SCRIPT_INVOKE_ID:
292eb9aa675754c49f613c6ad71d41472b30f38b007Yang Ni            break;
293dbe66d6783c1e53cd1572de0ef6ef6fdf6f76f48Jason Sams        case RS_A3D_CLASS_ID_SCRIPT_FIELD_ID:
294dbe66d6783c1e53cd1572de0ef6ef6fdf6f76f48Jason Sams            break;
295dbe66d6783c1e53cd1572de0ef6ef6fdf6f76f48Jason Sams        case RS_A3D_CLASS_ID_SCRIPT_METHOD_ID:
296dbe66d6783c1e53cd1572de0ef6ef6fdf6f76f48Jason Sams            break;
297dbe66d6783c1e53cd1572de0ef6ef6fdf6f76f48Jason Sams        case RS_A3D_CLASS_ID_SCRIPT_GROUP:
298dbe66d6783c1e53cd1572de0ef6ef6fdf6f76f48Jason Sams            break;
2991ffd86b448d78366190c540f98f8b6d641cdb6cfYang Ni        case RS_A3D_CLASS_ID_CLOSURE:
3001ffd86b448d78366190c540f98f8b6d641cdb6cfYang Ni            break;
3011ffd86b448d78366190c540f98f8b6d641cdb6cfYang Ni        case RS_A3D_CLASS_ID_SCRIPT_GROUP2:
3021ffd86b448d78366190c540f98f8b6d641cdb6cfYang Ni            break;
303a5597fcd411badad50c1345b3eca26453da45bbbJason Sams    }
304afb743aca56c18beb7ab924e75cb6e070ef3e55aAlex Sakhartchouk    if (entry->mRsObj) {
305b825f67adb5d1e1751fe108e6dbf9c6f2555c283Alex Sakhartchouk        entry->mRsObj->incUserRef();
306b825f67adb5d1e1751fe108e6dbf9c6f2555c283Alex Sakhartchouk    }
307b825f67adb5d1e1751fe108e6dbf9c6f2555c283Alex Sakhartchouk    return entry->mRsObj;
308a5597fcd411badad50c1345b3eca26453da45bbbJason Sams}
309a5597fcd411badad50c1345b3eca26453da45bbbJason Sams
310afb743aca56c18beb7ab924e75cb6e070ef3e55aAlex Sakhartchoukbool FileA3D::writeFile(const char *filename) {
311afb743aca56c18beb7ab924e75cb6e070ef3e55aAlex Sakhartchouk    if (!mWriteStream) {
312af12ac6a08651464f8d823add667c706f993b587Steve Block        ALOGE("No objects to write\n");
313fb6b614bcea88a587a7ea4530be45ff0ffa0210eAlex Sakhartchouk        return false;
314a5597fcd411badad50c1345b3eca26453da45bbbJason Sams    }
315afb743aca56c18beb7ab924e75cb6e070ef3e55aAlex Sakhartchouk    if (mWriteStream->getPos() == 0) {
316af12ac6a08651464f8d823add667c706f993b587Steve Block        ALOGE("No objects to write\n");
317fb6b614bcea88a587a7ea4530be45ff0ffa0210eAlex Sakhartchouk        return false;
318a5597fcd411badad50c1345b3eca26453da45bbbJason Sams    }
319a5597fcd411badad50c1345b3eca26453da45bbbJason Sams
320fb6b614bcea88a587a7ea4530be45ff0ffa0210eAlex Sakhartchouk    FILE *writeHandle = fopen(filename, "wb");
321afb743aca56c18beb7ab924e75cb6e070ef3e55aAlex Sakhartchouk    if (!writeHandle) {
322af12ac6a08651464f8d823add667c706f993b587Steve Block        ALOGE("Couldn't open the file for writing\n");
323fb6b614bcea88a587a7ea4530be45ff0ffa0210eAlex Sakhartchouk        return false;
324b80dfa72dfdd146a9ddee948ad76635dbea3c0cdJason Sams    }
325b80dfa72dfdd146a9ddee948ad76635dbea3c0cdJason Sams
326fb6b614bcea88a587a7ea4530be45ff0ffa0210eAlex Sakhartchouk    // Open a new stream to make writing the header easier
327fb6b614bcea88a587a7ea4530be45ff0ffa0210eAlex Sakhartchouk    OStream headerStream(5*1024, false);
328fb6b614bcea88a587a7ea4530be45ff0ffa0210eAlex Sakhartchouk    headerStream.addU32(mMajorVersion);
329fb6b614bcea88a587a7ea4530be45ff0ffa0210eAlex Sakhartchouk    headerStream.addU32(mMinorVersion);
330fb6b614bcea88a587a7ea4530be45ff0ffa0210eAlex Sakhartchouk    uint32_t is64Bit = 0;
331fb6b614bcea88a587a7ea4530be45ff0ffa0210eAlex Sakhartchouk    headerStream.addU32(is64Bit);
332fb6b614bcea88a587a7ea4530be45ff0ffa0210eAlex Sakhartchouk
333fb6b614bcea88a587a7ea4530be45ff0ffa0210eAlex Sakhartchouk    uint32_t writeIndexSize = mWriteIndex.size();
334fb6b614bcea88a587a7ea4530be45ff0ffa0210eAlex Sakhartchouk    headerStream.addU32(writeIndexSize);
335afb743aca56c18beb7ab924e75cb6e070ef3e55aAlex Sakhartchouk    for (uint32_t i = 0; i < writeIndexSize; i ++) {
33648ecf6a5e85a9a832f41393ed2802385bb8b5db8Jason Sams        headerStream.addString(mWriteIndex[i]->mObjectName);
337fb6b614bcea88a587a7ea4530be45ff0ffa0210eAlex Sakhartchouk        headerStream.addU32((uint32_t)mWriteIndex[i]->mType);
338afb743aca56c18beb7ab924e75cb6e070ef3e55aAlex Sakhartchouk        if (mUse64BitOffsets){
339fb6b614bcea88a587a7ea4530be45ff0ffa0210eAlex Sakhartchouk            headerStream.addOffset(mWriteIndex[i]->mOffset);
340b825f67adb5d1e1751fe108e6dbf9c6f2555c283Alex Sakhartchouk            headerStream.addOffset(mWriteIndex[i]->mLength);
341afb743aca56c18beb7ab924e75cb6e070ef3e55aAlex Sakhartchouk        } else {
342fb6b614bcea88a587a7ea4530be45ff0ffa0210eAlex Sakhartchouk            uint32_t offset = (uint32_t)mWriteIndex[i]->mOffset;
343fb6b614bcea88a587a7ea4530be45ff0ffa0210eAlex Sakhartchouk            headerStream.addU32(offset);
344b825f67adb5d1e1751fe108e6dbf9c6f2555c283Alex Sakhartchouk            offset = (uint32_t)mWriteIndex[i]->mLength;
345b825f67adb5d1e1751fe108e6dbf9c6f2555c283Alex Sakhartchouk            headerStream.addU32(offset);
346b80dfa72dfdd146a9ddee948ad76635dbea3c0cdJason Sams        }
347a5597fcd411badad50c1345b3eca26453da45bbbJason Sams    }
348a5597fcd411badad50c1345b3eca26453da45bbbJason Sams
349fb6b614bcea88a587a7ea4530be45ff0ffa0210eAlex Sakhartchouk    // Write our magic string so we know we are reading the right file
35048ecf6a5e85a9a832f41393ed2802385bb8b5db8Jason Sams    fwrite(A3D_MAGIC_KEY, sizeof(char), strlen(A3D_MAGIC_KEY), writeHandle);
351a5597fcd411badad50c1345b3eca26453da45bbbJason Sams
352fb6b614bcea88a587a7ea4530be45ff0ffa0210eAlex Sakhartchouk    // Store the size of the header to make it easier to parse when we read it
353fb6b614bcea88a587a7ea4530be45ff0ffa0210eAlex Sakhartchouk    uint64_t headerSize = headerStream.getPos();
354fb6b614bcea88a587a7ea4530be45ff0ffa0210eAlex Sakhartchouk    fwrite(&headerSize, sizeof(headerSize), 1, writeHandle);
355a5597fcd411badad50c1345b3eca26453da45bbbJason Sams
356fb6b614bcea88a587a7ea4530be45ff0ffa0210eAlex Sakhartchouk    // Now write our header
357fb6b614bcea88a587a7ea4530be45ff0ffa0210eAlex Sakhartchouk    fwrite(headerStream.getPtr(), sizeof(uint8_t), headerStream.getPos(), writeHandle);
358a5597fcd411badad50c1345b3eca26453da45bbbJason Sams
359fb6b614bcea88a587a7ea4530be45ff0ffa0210eAlex Sakhartchouk    // Now write the size of the data part of the file for easier parsing later
360fb6b614bcea88a587a7ea4530be45ff0ffa0210eAlex Sakhartchouk    uint64_t fileDataSize = mWriteStream->getPos();
361fb6b614bcea88a587a7ea4530be45ff0ffa0210eAlex Sakhartchouk    fwrite(&fileDataSize, sizeof(fileDataSize), 1, writeHandle);
362a5597fcd411badad50c1345b3eca26453da45bbbJason Sams
363fb6b614bcea88a587a7ea4530be45ff0ffa0210eAlex Sakhartchouk    fwrite(mWriteStream->getPtr(), sizeof(uint8_t), mWriteStream->getPos(), writeHandle);
364b80dfa72dfdd146a9ddee948ad76635dbea3c0cdJason Sams
365fb6b614bcea88a587a7ea4530be45ff0ffa0210eAlex Sakhartchouk    int status = fclose(writeHandle);
366a5597fcd411badad50c1345b3eca26453da45bbbJason Sams
367afb743aca56c18beb7ab924e75cb6e070ef3e55aAlex Sakhartchouk    if (status != 0) {
368af12ac6a08651464f8d823add667c706f993b587Steve Block        ALOGE("Couldn't close file\n");
369fb6b614bcea88a587a7ea4530be45ff0ffa0210eAlex Sakhartchouk        return false;
370fb6b614bcea88a587a7ea4530be45ff0ffa0210eAlex Sakhartchouk    }
371a5597fcd411badad50c1345b3eca26453da45bbbJason Sams
372fb6b614bcea88a587a7ea4530be45ff0ffa0210eAlex Sakhartchouk    return true;
373fb6b614bcea88a587a7ea4530be45ff0ffa0210eAlex Sakhartchouk}
374fb6b614bcea88a587a7ea4530be45ff0ffa0210eAlex Sakhartchouk
375e3150cfb3edb028407669e4a65e087eae77e718cJason Samsvoid FileA3D::appendToFile(Context *con, ObjectBase *obj) {
376afb743aca56c18beb7ab924e75cb6e070ef3e55aAlex Sakhartchouk    if (!obj) {
377fb6b614bcea88a587a7ea4530be45ff0ffa0210eAlex Sakhartchouk        return;
378fb6b614bcea88a587a7ea4530be45ff0ffa0210eAlex Sakhartchouk    }
379afb743aca56c18beb7ab924e75cb6e070ef3e55aAlex Sakhartchouk    if (!mWriteStream) {
380fb6b614bcea88a587a7ea4530be45ff0ffa0210eAlex Sakhartchouk        const uint64_t initialStreamSize = 256*1024;
381fb6b614bcea88a587a7ea4530be45ff0ffa0210eAlex Sakhartchouk        mWriteStream = new OStream(initialStreamSize, false);
382a5597fcd411badad50c1345b3eca26453da45bbbJason Sams    }
383fb6b614bcea88a587a7ea4530be45ff0ffa0210eAlex Sakhartchouk    A3DIndexEntry *indexEntry = new A3DIndexEntry();
38448ecf6a5e85a9a832f41393ed2802385bb8b5db8Jason Sams    indexEntry->mObjectName = rsuCopyString(obj->getName());
385fb6b614bcea88a587a7ea4530be45ff0ffa0210eAlex Sakhartchouk    indexEntry->mType = obj->getClassId();
386fb6b614bcea88a587a7ea4530be45ff0ffa0210eAlex Sakhartchouk    indexEntry->mOffset = mWriteStream->getPos();
387b825f67adb5d1e1751fe108e6dbf9c6f2555c283Alex Sakhartchouk    indexEntry->mRsObj = obj;
388b8353c5943f4038fd7f08db3d958390ce9418798Yang Ni    mWriteIndex.push(indexEntry);
389e3150cfb3edb028407669e4a65e087eae77e718cJason Sams    obj->serialize(con, mWriteStream);
390b825f67adb5d1e1751fe108e6dbf9c6f2555c283Alex Sakhartchouk    indexEntry->mLength = mWriteStream->getPos() - indexEntry->mOffset;
391b825f67adb5d1e1751fe108e6dbf9c6f2555c283Alex Sakhartchouk    mWriteStream->align(4);
392a5597fcd411badad50c1345b3eca26453da45bbbJason Sams}
393a5597fcd411badad50c1345b3eca26453da45bbbJason Sams
394