Jason Sams | 64676f3 | 2009-07-08 18:01:53 -0700 | [diff] [blame] | 1 | |
| 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 Sams | 64676f3 | 2009-07-08 18:01:53 -0700 | [diff] [blame] | 19 | #include "rsFileA3D.h" |
| 20 | |
| 21 | #include "rsMesh.h" |
Alex Sakhartchouk | aa7d288 | 2010-05-21 12:53:13 -0700 | [diff] [blame] | 22 | #include "rsAnimation.h" |
Jason Sams | 64676f3 | 2009-07-08 18:01:53 -0700 | [diff] [blame] | 23 | |
Alex Sakhartchouk | 9b949fc | 2010-06-24 17:15:34 -0700 | [diff] [blame] | 24 | |
Jason Sams | 64676f3 | 2009-07-08 18:01:53 -0700 | [diff] [blame] | 25 | using namespace android; |
| 26 | using namespace android::renderscript; |
| 27 | |
Alex Sakhartchouk | ed9f210 | 2010-11-09 17:00:54 -0800 | [diff] [blame] | 28 | FileA3D::FileA3D(Context *rsc) : ObjectBase(rsc) { |
Alex Sakhartchouk | aa7d288 | 2010-05-21 12:53:13 -0700 | [diff] [blame] | 29 | mAlloc = NULL; |
| 30 | mData = NULL; |
| 31 | mWriteStream = NULL; |
| 32 | mReadStream = NULL; |
Alex Sakhartchouk | b0253ea | 2011-01-07 11:12:08 -0800 | [diff] [blame] | 33 | mAsset = NULL; |
Alex Sakhartchouk | aa7d288 | 2010-05-21 12:53:13 -0700 | [diff] [blame] | 34 | |
| 35 | mMajorVersion = 0; |
| 36 | mMinorVersion = 1; |
| 37 | mDataSize = 0; |
Jason Sams | 64676f3 | 2009-07-08 18:01:53 -0700 | [diff] [blame] | 38 | } |
| 39 | |
Alex Sakhartchouk | ed9f210 | 2010-11-09 17:00:54 -0800 | [diff] [blame] | 40 | FileA3D::~FileA3D() { |
| 41 | for (size_t i = 0; i < mIndex.size(); i ++) { |
Alex Sakhartchouk | aa7d288 | 2010-05-21 12:53:13 -0700 | [diff] [blame] | 42 | delete mIndex[i]; |
| 43 | } |
Alex Sakhartchouk | ed9f210 | 2010-11-09 17:00:54 -0800 | [diff] [blame] | 44 | for (size_t i = 0; i < mWriteIndex.size(); i ++) { |
Alex Sakhartchouk | aa7d288 | 2010-05-21 12:53:13 -0700 | [diff] [blame] | 45 | delete mWriteIndex[i]; |
| 46 | } |
Alex Sakhartchouk | ed9f210 | 2010-11-09 17:00:54 -0800 | [diff] [blame] | 47 | if (mWriteStream) { |
Alex Sakhartchouk | aa7d288 | 2010-05-21 12:53:13 -0700 | [diff] [blame] | 48 | delete mWriteStream; |
| 49 | } |
Alex Sakhartchouk | ed9f210 | 2010-11-09 17:00:54 -0800 | [diff] [blame] | 50 | if (mReadStream) { |
Alex Sakhartchouk | aa7d288 | 2010-05-21 12:53:13 -0700 | [diff] [blame] | 51 | delete mWriteStream; |
| 52 | } |
Alex Sakhartchouk | ed9f210 | 2010-11-09 17:00:54 -0800 | [diff] [blame] | 53 | if (mAlloc) { |
Alex Sakhartchouk | aa7d288 | 2010-05-21 12:53:13 -0700 | [diff] [blame] | 54 | free(mAlloc); |
| 55 | } |
Alex Sakhartchouk | b0253ea | 2011-01-07 11:12:08 -0800 | [diff] [blame] | 56 | if (mAsset) { |
| 57 | delete mAsset; |
| 58 | } |
Jason Sams | 64676f3 | 2009-07-08 18:01:53 -0700 | [diff] [blame] | 59 | } |
| 60 | |
Alex Sakhartchouk | ed9f210 | 2010-11-09 17:00:54 -0800 | [diff] [blame] | 61 | void FileA3D::parseHeader(IStream *headerStream) { |
Alex Sakhartchouk | aae74ad | 2010-06-04 10:06:50 -0700 | [diff] [blame] | 62 | mMajorVersion = headerStream->loadU32(); |
| 63 | mMinorVersion = headerStream->loadU32(); |
| 64 | uint32_t flags = headerStream->loadU32(); |
| 65 | mUse64BitOffsets = (flags & 1) != 0; |
| 66 | |
Alex Sakhartchouk | aae74ad | 2010-06-04 10:06:50 -0700 | [diff] [blame] | 67 | uint32_t numIndexEntries = headerStream->loadU32(); |
Alex Sakhartchouk | ed9f210 | 2010-11-09 17:00:54 -0800 | [diff] [blame] | 68 | for (uint32_t i = 0; i < numIndexEntries; i ++) { |
Alex Sakhartchouk | aae74ad | 2010-06-04 10:06:50 -0700 | [diff] [blame] | 69 | A3DIndexEntry *entry = new A3DIndexEntry(); |
| 70 | headerStream->loadString(&entry->mObjectName); |
Steve Block | 71f2cf1 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 71 | //ALOGV("Header data, entry name = %s", entry->mObjectName.string()); |
Alex Sakhartchouk | aae74ad | 2010-06-04 10:06:50 -0700 | [diff] [blame] | 72 | entry->mType = (RsA3DClassID)headerStream->loadU32(); |
Alex Sakhartchouk | ed9f210 | 2010-11-09 17:00:54 -0800 | [diff] [blame] | 73 | if (mUse64BitOffsets){ |
Alex Sakhartchouk | aae74ad | 2010-06-04 10:06:50 -0700 | [diff] [blame] | 74 | entry->mOffset = headerStream->loadOffset(); |
| 75 | entry->mLength = headerStream->loadOffset(); |
Alex Sakhartchouk | ed9f210 | 2010-11-09 17:00:54 -0800 | [diff] [blame] | 76 | } else { |
Alex Sakhartchouk | aae74ad | 2010-06-04 10:06:50 -0700 | [diff] [blame] | 77 | entry->mOffset = headerStream->loadU32(); |
| 78 | entry->mLength = headerStream->loadU32(); |
| 79 | } |
| 80 | entry->mRsObj = NULL; |
| 81 | mIndex.push(entry); |
| 82 | } |
| 83 | } |
| 84 | |
Alex Sakhartchouk | b0253ea | 2011-01-07 11:12:08 -0800 | [diff] [blame] | 85 | bool FileA3D::load(Asset *asset) { |
| 86 | mAsset = asset; |
| 87 | return load(asset->getBuffer(false), asset->getLength()); |
| 88 | } |
| 89 | |
Alex Sakhartchouk | ed9f210 | 2010-11-09 17:00:54 -0800 | [diff] [blame] | 90 | bool FileA3D::load(const void *data, size_t length) { |
Alex Sakhartchouk | aae74ad | 2010-06-04 10:06:50 -0700 | [diff] [blame] | 91 | 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 Sakhartchouk | ed9f210 | 2010-11-09 17:00:54 -0800 | [diff] [blame] | 105 | if (lengthRemaining < sizeof(headerSize)) { |
Alex Sakhartchouk | aae74ad | 2010-06-04 10:06:50 -0700 | [diff] [blame] | 106 | return false; |
| 107 | } |
| 108 | |
| 109 | memcpy(&headerSize, localData, sizeof(headerSize)); |
| 110 | localData += sizeof(headerSize); |
| 111 | lengthRemaining -= sizeof(headerSize); |
| 112 | |
Alex Sakhartchouk | ed9f210 | 2010-11-09 17:00:54 -0800 | [diff] [blame] | 113 | if (lengthRemaining < headerSize) { |
Alex Sakhartchouk | aae74ad | 2010-06-04 10:06:50 -0700 | [diff] [blame] | 114 | return false; |
| 115 | } |
| 116 | |
Alex Sakhartchouk | aae74ad | 2010-06-04 10:06:50 -0700 | [diff] [blame] | 117 | // Now open the stream to parse the header |
Alex Sakhartchouk | 581cc64 | 2010-10-27 14:10:07 -0700 | [diff] [blame] | 118 | IStream headerStream(localData, false); |
Alex Sakhartchouk | aae74ad | 2010-06-04 10:06:50 -0700 | [diff] [blame] | 119 | parseHeader(&headerStream); |
| 120 | |
Alex Sakhartchouk | aae74ad | 2010-06-04 10:06:50 -0700 | [diff] [blame] | 121 | localData += headerSize; |
| 122 | lengthRemaining -= headerSize; |
| 123 | |
Alex Sakhartchouk | ed9f210 | 2010-11-09 17:00:54 -0800 | [diff] [blame] | 124 | if (lengthRemaining < sizeof(mDataSize)) { |
Alex Sakhartchouk | aae74ad | 2010-06-04 10:06:50 -0700 | [diff] [blame] | 125 | 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 Sakhartchouk | ed9f210 | 2010-11-09 17:00:54 -0800 | [diff] [blame] | 133 | if (lengthRemaining < mDataSize) { |
Alex Sakhartchouk | aae74ad | 2010-06-04 10:06:50 -0700 | [diff] [blame] | 134 | return false; |
| 135 | } |
| 136 | |
| 137 | // We should know enough to read the file in at this point. |
Alex Sakhartchouk | 581cc64 | 2010-10-27 14:10:07 -0700 | [diff] [blame] | 138 | mData = (uint8_t *)localData; |
Alex Sakhartchouk | aae74ad | 2010-06-04 10:06:50 -0700 | [diff] [blame] | 139 | mReadStream = new IStream(mData, mUse64BitOffsets); |
| 140 | |
| 141 | return true; |
| 142 | } |
| 143 | |
Alex Sakhartchouk | ed9f210 | 2010-11-09 17:00:54 -0800 | [diff] [blame] | 144 | bool FileA3D::load(FILE *f) { |
Jason Sams | 64676f3 | 2009-07-08 18:01:53 -0700 | [diff] [blame] | 145 | char magicString[12]; |
| 146 | size_t len; |
| 147 | |
Steve Block | 71f2cf1 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 148 | ALOGV("file open 1"); |
Jason Sams | 64676f3 | 2009-07-08 18:01:53 -0700 | [diff] [blame] | 149 | len = fread(magicString, 1, 12, f); |
| 150 | if ((len != 12) || |
| 151 | memcmp(magicString, "Android3D_ff", 12)) { |
| 152 | return false; |
| 153 | } |
| 154 | |
Alex Sakhartchouk | aa7d288 | 2010-05-21 12:53:13 -0700 | [diff] [blame] | 155 | // 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 Sams | 64676f3 | 2009-07-08 18:01:53 -0700 | [diff] [blame] | 159 | return false; |
| 160 | } |
| 161 | |
Alex Sakhartchouk | aa7d288 | 2010-05-21 12:53:13 -0700 | [diff] [blame] | 162 | uint8_t *headerData = (uint8_t *)malloc(headerSize); |
Alex Sakhartchouk | ed9f210 | 2010-11-09 17:00:54 -0800 | [diff] [blame] | 163 | if (!headerData) { |
Jason Sams | 64676f3 | 2009-07-08 18:01:53 -0700 | [diff] [blame] | 164 | return false; |
| 165 | } |
| 166 | |
Alex Sakhartchouk | aa7d288 | 2010-05-21 12:53:13 -0700 | [diff] [blame] | 167 | len = fread(headerData, 1, headerSize, f); |
| 168 | if (len != headerSize) { |
Jason Sams | 64676f3 | 2009-07-08 18:01:53 -0700 | [diff] [blame] | 169 | return false; |
| 170 | } |
Alex Sakhartchouk | aa7d288 | 2010-05-21 12:53:13 -0700 | [diff] [blame] | 171 | |
| 172 | // Now open the stream to parse the header |
| 173 | IStream headerStream(headerData, false); |
Alex Sakhartchouk | aae74ad | 2010-06-04 10:06:50 -0700 | [diff] [blame] | 174 | parseHeader(&headerStream); |
Alex Sakhartchouk | aa7d288 | 2010-05-21 12:53:13 -0700 | [diff] [blame] | 175 | |
Alex Sakhartchouk | aae74ad | 2010-06-04 10:06:50 -0700 | [diff] [blame] | 176 | free(headerData); |
Alex Sakhartchouk | aa7d288 | 2010-05-21 12:53:13 -0700 | [diff] [blame] | 177 | |
| 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 Sams | 64676f3 | 2009-07-08 18:01:53 -0700 | [diff] [blame] | 182 | } |
| 183 | |
Steve Block | 71f2cf1 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 184 | ALOGV("file open size = %lli", mDataSize); |
Jason Sams | 64676f3 | 2009-07-08 18:01:53 -0700 | [diff] [blame] | 185 | |
| 186 | // We should know enough to read the file in at this point. |
Alex Sakhartchouk | aa7d288 | 2010-05-21 12:53:13 -0700 | [diff] [blame] | 187 | mAlloc = malloc(mDataSize); |
Jason Sams | 64676f3 | 2009-07-08 18:01:53 -0700 | [diff] [blame] | 188 | 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 Sakhartchouk | aa7d288 | 2010-05-21 12:53:13 -0700 | [diff] [blame] | 197 | mReadStream = new IStream(mData, mUse64BitOffsets); |
| 198 | |
Steve Block | 71f2cf1 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 199 | ALOGV("Header is read an stream initialized"); |
Alex Sakhartchouk | aa7d288 | 2010-05-21 12:53:13 -0700 | [diff] [blame] | 200 | return true; |
Jason Sams | 64676f3 | 2009-07-08 18:01:53 -0700 | [diff] [blame] | 201 | } |
| 202 | |
Alex Sakhartchouk | aae74ad | 2010-06-04 10:06:50 -0700 | [diff] [blame] | 203 | size_t FileA3D::getNumIndexEntries() const { |
Alex Sakhartchouk | aa7d288 | 2010-05-21 12:53:13 -0700 | [diff] [blame] | 204 | return mIndex.size(); |
| 205 | } |
Jason Sams | 64676f3 | 2009-07-08 18:01:53 -0700 | [diff] [blame] | 206 | |
Alex Sakhartchouk | aae74ad | 2010-06-04 10:06:50 -0700 | [diff] [blame] | 207 | const FileA3D::A3DIndexEntry *FileA3D::getIndexEntry(size_t index) const { |
Alex Sakhartchouk | ed9f210 | 2010-11-09 17:00:54 -0800 | [diff] [blame] | 208 | if (index < mIndex.size()) { |
Alex Sakhartchouk | aa7d288 | 2010-05-21 12:53:13 -0700 | [diff] [blame] | 209 | return mIndex[index]; |
Jason Sams | 64676f3 | 2009-07-08 18:01:53 -0700 | [diff] [blame] | 210 | } |
Alex Sakhartchouk | aa7d288 | 2010-05-21 12:53:13 -0700 | [diff] [blame] | 211 | return NULL; |
Jason Sams | 64676f3 | 2009-07-08 18:01:53 -0700 | [diff] [blame] | 212 | } |
| 213 | |
Alex Sakhartchouk | aae74ad | 2010-06-04 10:06:50 -0700 | [diff] [blame] | 214 | ObjectBase *FileA3D::initializeFromEntry(size_t index) { |
Alex Sakhartchouk | ed9f210 | 2010-11-09 17:00:54 -0800 | [diff] [blame] | 215 | if (index >= mIndex.size()) { |
Alex Sakhartchouk | aae74ad | 2010-06-04 10:06:50 -0700 | [diff] [blame] | 216 | return NULL; |
| 217 | } |
| 218 | |
| 219 | FileA3D::A3DIndexEntry *entry = mIndex[index]; |
Alex Sakhartchouk | ed9f210 | 2010-11-09 17:00:54 -0800 | [diff] [blame] | 220 | if (!entry) { |
Alex Sakhartchouk | aa7d288 | 2010-05-21 12:53:13 -0700 | [diff] [blame] | 221 | return NULL; |
| 222 | } |
| 223 | |
Alex Sakhartchouk | ed9f210 | 2010-11-09 17:00:54 -0800 | [diff] [blame] | 224 | if (entry->mRsObj) { |
Alex Sakhartchouk | aae74ad | 2010-06-04 10:06:50 -0700 | [diff] [blame] | 225 | entry->mRsObj->incUserRef(); |
| 226 | return entry->mRsObj; |
| 227 | } |
| 228 | |
Alex Sakhartchouk | aa7d288 | 2010-05-21 12:53:13 -0700 | [diff] [blame] | 229 | // Seek to the beginning of object |
| 230 | mReadStream->reset(entry->mOffset); |
| 231 | switch (entry->mType) { |
Alex Sakhartchouk | aae74ad | 2010-06-04 10:06:50 -0700 | [diff] [blame] | 232 | case RS_A3D_CLASS_ID_UNKNOWN: |
Alex Sakhartchouk | aa7d288 | 2010-05-21 12:53:13 -0700 | [diff] [blame] | 233 | return NULL; |
Alex Sakhartchouk | aae74ad | 2010-06-04 10:06:50 -0700 | [diff] [blame] | 234 | case RS_A3D_CLASS_ID_MESH: |
| 235 | entry->mRsObj = Mesh::createFromStream(mRSC, mReadStream); |
| 236 | break; |
Alex Sakhartchouk | aae74ad | 2010-06-04 10:06:50 -0700 | [diff] [blame] | 237 | 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 Sakhartchouk | a3b5960 | 2011-01-28 09:31:47 -0800 | [diff] [blame] | 247 | //entry->mRsObj = ProgramVertex::createFromStream(mRSC, mReadStream); |
Alex Sakhartchouk | aae74ad | 2010-06-04 10:06:50 -0700 | [diff] [blame] | 248 | break; |
| 249 | case RS_A3D_CLASS_ID_PROGRAM_RASTER: |
Alex Sakhartchouk | a3b5960 | 2011-01-28 09:31:47 -0800 | [diff] [blame] | 250 | //entry->mRsObj = ProgramRaster::createFromStream(mRSC, mReadStream); |
Alex Sakhartchouk | aae74ad | 2010-06-04 10:06:50 -0700 | [diff] [blame] | 251 | break; |
| 252 | case RS_A3D_CLASS_ID_PROGRAM_FRAGMENT: |
Alex Sakhartchouk | a3b5960 | 2011-01-28 09:31:47 -0800 | [diff] [blame] | 253 | //entry->mRsObj = ProgramFragment::createFromStream(mRSC, mReadStream); |
Alex Sakhartchouk | aae74ad | 2010-06-04 10:06:50 -0700 | [diff] [blame] | 254 | break; |
| 255 | case RS_A3D_CLASS_ID_PROGRAM_STORE: |
Alex Sakhartchouk | a3b5960 | 2011-01-28 09:31:47 -0800 | [diff] [blame] | 256 | //entry->mRsObj = ProgramStore::createFromStream(mRSC, mReadStream); |
Alex Sakhartchouk | aae74ad | 2010-06-04 10:06:50 -0700 | [diff] [blame] | 257 | break; |
| 258 | case RS_A3D_CLASS_ID_SAMPLER: |
Alex Sakhartchouk | a3b5960 | 2011-01-28 09:31:47 -0800 | [diff] [blame] | 259 | //entry->mRsObj = Sampler::createFromStream(mRSC, mReadStream); |
Alex Sakhartchouk | aae74ad | 2010-06-04 10:06:50 -0700 | [diff] [blame] | 260 | break; |
| 261 | case RS_A3D_CLASS_ID_ANIMATION: |
Alex Sakhartchouk | a3b5960 | 2011-01-28 09:31:47 -0800 | [diff] [blame] | 262 | //entry->mRsObj = Animation::createFromStream(mRSC, mReadStream); |
Alex Sakhartchouk | aae74ad | 2010-06-04 10:06:50 -0700 | [diff] [blame] | 263 | break; |
Alex Sakhartchouk | aae74ad | 2010-06-04 10:06:50 -0700 | [diff] [blame] | 264 | case RS_A3D_CLASS_ID_ADAPTER_1D: |
Alex Sakhartchouk | a3b5960 | 2011-01-28 09:31:47 -0800 | [diff] [blame] | 265 | //entry->mRsObj = Adapter1D::createFromStream(mRSC, mReadStream); |
Alex Sakhartchouk | aae74ad | 2010-06-04 10:06:50 -0700 | [diff] [blame] | 266 | break; |
| 267 | case RS_A3D_CLASS_ID_ADAPTER_2D: |
Alex Sakhartchouk | a3b5960 | 2011-01-28 09:31:47 -0800 | [diff] [blame] | 268 | //entry->mRsObj = Adapter2D::createFromStream(mRSC, mReadStream); |
Alex Sakhartchouk | aae74ad | 2010-06-04 10:06:50 -0700 | [diff] [blame] | 269 | break; |
| 270 | case RS_A3D_CLASS_ID_SCRIPT_C: |
Alex Sakhartchouk | a3b5960 | 2011-01-28 09:31:47 -0800 | [diff] [blame] | 271 | break; |
Alex Sakhartchouk | aa7d288 | 2010-05-21 12:53:13 -0700 | [diff] [blame] | 272 | } |
Alex Sakhartchouk | ed9f210 | 2010-11-09 17:00:54 -0800 | [diff] [blame] | 273 | if (entry->mRsObj) { |
Alex Sakhartchouk | aae74ad | 2010-06-04 10:06:50 -0700 | [diff] [blame] | 274 | entry->mRsObj->incUserRef(); |
| 275 | } |
| 276 | return entry->mRsObj; |
Alex Sakhartchouk | aa7d288 | 2010-05-21 12:53:13 -0700 | [diff] [blame] | 277 | } |
| 278 | |
Alex Sakhartchouk | ed9f210 | 2010-11-09 17:00:54 -0800 | [diff] [blame] | 279 | bool FileA3D::writeFile(const char *filename) { |
| 280 | if (!mWriteStream) { |
Steve Block | 3762c31 | 2012-01-06 19:20:56 +0000 | [diff] [blame] | 281 | ALOGE("No objects to write\n"); |
Alex Sakhartchouk | aa7d288 | 2010-05-21 12:53:13 -0700 | [diff] [blame] | 282 | return false; |
| 283 | } |
Alex Sakhartchouk | ed9f210 | 2010-11-09 17:00:54 -0800 | [diff] [blame] | 284 | if (mWriteStream->getPos() == 0) { |
Steve Block | 3762c31 | 2012-01-06 19:20:56 +0000 | [diff] [blame] | 285 | ALOGE("No objects to write\n"); |
Alex Sakhartchouk | aa7d288 | 2010-05-21 12:53:13 -0700 | [diff] [blame] | 286 | return false; |
| 287 | } |
Jason Sams | 64676f3 | 2009-07-08 18:01:53 -0700 | [diff] [blame] | 288 | |
Alex Sakhartchouk | aa7d288 | 2010-05-21 12:53:13 -0700 | [diff] [blame] | 289 | FILE *writeHandle = fopen(filename, "wb"); |
Alex Sakhartchouk | ed9f210 | 2010-11-09 17:00:54 -0800 | [diff] [blame] | 290 | if (!writeHandle) { |
Steve Block | 3762c31 | 2012-01-06 19:20:56 +0000 | [diff] [blame] | 291 | ALOGE("Couldn't open the file for writing\n"); |
Alex Sakhartchouk | aa7d288 | 2010-05-21 12:53:13 -0700 | [diff] [blame] | 292 | return false; |
| 293 | } |
Jason Sams | 64676f3 | 2009-07-08 18:01:53 -0700 | [diff] [blame] | 294 | |
Alex Sakhartchouk | aa7d288 | 2010-05-21 12:53:13 -0700 | [diff] [blame] | 295 | // 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 Sams | 64676f3 | 2009-07-08 18:01:53 -0700 | [diff] [blame] | 301 | |
Alex Sakhartchouk | aa7d288 | 2010-05-21 12:53:13 -0700 | [diff] [blame] | 302 | uint32_t writeIndexSize = mWriteIndex.size(); |
| 303 | headerStream.addU32(writeIndexSize); |
Alex Sakhartchouk | ed9f210 | 2010-11-09 17:00:54 -0800 | [diff] [blame] | 304 | for (uint32_t i = 0; i < writeIndexSize; i ++) { |
Alex Sakhartchouk | aae74ad | 2010-06-04 10:06:50 -0700 | [diff] [blame] | 305 | headerStream.addString(&mWriteIndex[i]->mObjectName); |
Alex Sakhartchouk | aa7d288 | 2010-05-21 12:53:13 -0700 | [diff] [blame] | 306 | headerStream.addU32((uint32_t)mWriteIndex[i]->mType); |
Alex Sakhartchouk | ed9f210 | 2010-11-09 17:00:54 -0800 | [diff] [blame] | 307 | if (mUse64BitOffsets){ |
Alex Sakhartchouk | aa7d288 | 2010-05-21 12:53:13 -0700 | [diff] [blame] | 308 | headerStream.addOffset(mWriteIndex[i]->mOffset); |
Alex Sakhartchouk | aae74ad | 2010-06-04 10:06:50 -0700 | [diff] [blame] | 309 | headerStream.addOffset(mWriteIndex[i]->mLength); |
Alex Sakhartchouk | ed9f210 | 2010-11-09 17:00:54 -0800 | [diff] [blame] | 310 | } else { |
Alex Sakhartchouk | aa7d288 | 2010-05-21 12:53:13 -0700 | [diff] [blame] | 311 | uint32_t offset = (uint32_t)mWriteIndex[i]->mOffset; |
| 312 | headerStream.addU32(offset); |
Alex Sakhartchouk | aae74ad | 2010-06-04 10:06:50 -0700 | [diff] [blame] | 313 | offset = (uint32_t)mWriteIndex[i]->mLength; |
| 314 | headerStream.addU32(offset); |
Jason Sams | 64676f3 | 2009-07-08 18:01:53 -0700 | [diff] [blame] | 315 | } |
| 316 | } |
| 317 | |
Alex Sakhartchouk | aa7d288 | 2010-05-21 12:53:13 -0700 | [diff] [blame] | 318 | // 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 Sams | 64676f3 | 2009-07-08 18:01:53 -0700 | [diff] [blame] | 321 | |
Alex Sakhartchouk | aa7d288 | 2010-05-21 12:53:13 -0700 | [diff] [blame] | 322 | // 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 Sams | 64676f3 | 2009-07-08 18:01:53 -0700 | [diff] [blame] | 325 | |
Alex Sakhartchouk | aa7d288 | 2010-05-21 12:53:13 -0700 | [diff] [blame] | 326 | // Now write our header |
| 327 | fwrite(headerStream.getPtr(), sizeof(uint8_t), headerStream.getPos(), writeHandle); |
Jason Sams | 64676f3 | 2009-07-08 18:01:53 -0700 | [diff] [blame] | 328 | |
Alex Sakhartchouk | aa7d288 | 2010-05-21 12:53:13 -0700 | [diff] [blame] | 329 | // 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 Sakhartchouk | ed9f210 | 2010-11-09 17:00:54 -0800 | [diff] [blame] | 337 | if (status != 0) { |
Steve Block | 3762c31 | 2012-01-06 19:20:56 +0000 | [diff] [blame] | 338 | ALOGE("Couldn't close file\n"); |
Alex Sakhartchouk | aa7d288 | 2010-05-21 12:53:13 -0700 | [diff] [blame] | 339 | return false; |
Jason Sams | 64676f3 | 2009-07-08 18:01:53 -0700 | [diff] [blame] | 340 | } |
| 341 | |
Alex Sakhartchouk | aa7d288 | 2010-05-21 12:53:13 -0700 | [diff] [blame] | 342 | return true; |
Jason Sams | 64676f3 | 2009-07-08 18:01:53 -0700 | [diff] [blame] | 343 | } |
| 344 | |
Alex Sakhartchouk | aa7d288 | 2010-05-21 12:53:13 -0700 | [diff] [blame] | 345 | void FileA3D::appendToFile(ObjectBase *obj) { |
Alex Sakhartchouk | ed9f210 | 2010-11-09 17:00:54 -0800 | [diff] [blame] | 346 | if (!obj) { |
Alex Sakhartchouk | aa7d288 | 2010-05-21 12:53:13 -0700 | [diff] [blame] | 347 | return; |
Jason Sams | 64676f3 | 2009-07-08 18:01:53 -0700 | [diff] [blame] | 348 | } |
Alex Sakhartchouk | ed9f210 | 2010-11-09 17:00:54 -0800 | [diff] [blame] | 349 | if (!mWriteStream) { |
Alex Sakhartchouk | aa7d288 | 2010-05-21 12:53:13 -0700 | [diff] [blame] | 350 | const uint64_t initialStreamSize = 256*1024; |
| 351 | mWriteStream = new OStream(initialStreamSize, false); |
Jason Sams | 64676f3 | 2009-07-08 18:01:53 -0700 | [diff] [blame] | 352 | } |
Alex Sakhartchouk | aa7d288 | 2010-05-21 12:53:13 -0700 | [diff] [blame] | 353 | A3DIndexEntry *indexEntry = new A3DIndexEntry(); |
Alex Sakhartchouk | aae74ad | 2010-06-04 10:06:50 -0700 | [diff] [blame] | 354 | indexEntry->mObjectName.setTo(obj->getName()); |
Alex Sakhartchouk | aa7d288 | 2010-05-21 12:53:13 -0700 | [diff] [blame] | 355 | indexEntry->mType = obj->getClassId(); |
| 356 | indexEntry->mOffset = mWriteStream->getPos(); |
Alex Sakhartchouk | aae74ad | 2010-06-04 10:06:50 -0700 | [diff] [blame] | 357 | indexEntry->mRsObj = obj; |
Alex Sakhartchouk | aa7d288 | 2010-05-21 12:53:13 -0700 | [diff] [blame] | 358 | mWriteIndex.push(indexEntry); |
| 359 | obj->serialize(mWriteStream); |
Alex Sakhartchouk | aae74ad | 2010-06-04 10:06:50 -0700 | [diff] [blame] | 360 | indexEntry->mLength = mWriteStream->getPos() - indexEntry->mOffset; |
| 361 | mWriteStream->align(4); |
Jason Sams | 64676f3 | 2009-07-08 18:01:53 -0700 | [diff] [blame] | 362 | } |
| 363 | |
Alex Sakhartchouk | ed9f210 | 2010-11-09 17:00:54 -0800 | [diff] [blame] | 364 | RsObjectBase rsaFileA3DGetEntryByIndex(RsContext con, uint32_t index, RsFile file) { |
Alex Sakhartchouk | 581cc64 | 2010-10-27 14:10:07 -0700 | [diff] [blame] | 365 | FileA3D *fa3d = static_cast<FileA3D *>(file); |
Alex Sakhartchouk | ed9f210 | 2010-11-09 17:00:54 -0800 | [diff] [blame] | 366 | if (!fa3d) { |
Steve Block | 3762c31 | 2012-01-06 19:20:56 +0000 | [diff] [blame] | 367 | ALOGE("Can't load entry. No valid file"); |
Alex Sakhartchouk | 581cc64 | 2010-10-27 14:10:07 -0700 | [diff] [blame] | 368 | return NULL; |
| 369 | } |
| 370 | |
| 371 | ObjectBase *obj = fa3d->initializeFromEntry(index); |
Steve Block | 71f2cf1 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 372 | //ALOGV("Returning object with name %s", obj->getName()); |
Alex Sakhartchouk | 581cc64 | 2010-10-27 14:10:07 -0700 | [diff] [blame] | 373 | |
| 374 | return obj; |
| 375 | } |
| 376 | |
| 377 | |
Alex Sakhartchouk | ed9f210 | 2010-11-09 17:00:54 -0800 | [diff] [blame] | 378 | void rsaFileA3DGetNumIndexEntries(RsContext con, int32_t *numEntries, RsFile file) { |
Alex Sakhartchouk | aae74ad | 2010-06-04 10:06:50 -0700 | [diff] [blame] | 379 | FileA3D *fa3d = static_cast<FileA3D *>(file); |
| 380 | |
Alex Sakhartchouk | ed9f210 | 2010-11-09 17:00:54 -0800 | [diff] [blame] | 381 | if (fa3d) { |
Alex Sakhartchouk | aae74ad | 2010-06-04 10:06:50 -0700 | [diff] [blame] | 382 | *numEntries = fa3d->getNumIndexEntries(); |
Alex Sakhartchouk | ed9f210 | 2010-11-09 17:00:54 -0800 | [diff] [blame] | 383 | } else { |
Alex Sakhartchouk | aae74ad | 2010-06-04 10:06:50 -0700 | [diff] [blame] | 384 | *numEntries = 0; |
| 385 | } |
| 386 | } |
| 387 | |
Alex Sakhartchouk | ed9f210 | 2010-11-09 17:00:54 -0800 | [diff] [blame] | 388 | void rsaFileA3DGetIndexEntries(RsContext con, RsFileIndexEntry *fileEntries, uint32_t numEntries, RsFile file) { |
Alex Sakhartchouk | aae74ad | 2010-06-04 10:06:50 -0700 | [diff] [blame] | 389 | FileA3D *fa3d = static_cast<FileA3D *>(file); |
| 390 | |
Alex Sakhartchouk | ed9f210 | 2010-11-09 17:00:54 -0800 | [diff] [blame] | 391 | if (!fa3d) { |
Steve Block | 3762c31 | 2012-01-06 19:20:56 +0000 | [diff] [blame] | 392 | ALOGE("Can't load index entries. No valid file"); |
Alex Sakhartchouk | aae74ad | 2010-06-04 10:06:50 -0700 | [diff] [blame] | 393 | return; |
| 394 | } |
| 395 | |
| 396 | uint32_t numFileEntries = fa3d->getNumIndexEntries(); |
Alex Sakhartchouk | ed9f210 | 2010-11-09 17:00:54 -0800 | [diff] [blame] | 397 | if (numFileEntries != numEntries || numEntries == 0 || fileEntries == NULL) { |
Steve Block | 3762c31 | 2012-01-06 19:20:56 +0000 | [diff] [blame] | 398 | ALOGE("Can't load index entries. Invalid number requested"); |
Alex Sakhartchouk | aae74ad | 2010-06-04 10:06:50 -0700 | [diff] [blame] | 399 | return; |
| 400 | } |
| 401 | |
Alex Sakhartchouk | ed9f210 | 2010-11-09 17:00:54 -0800 | [diff] [blame] | 402 | for (uint32_t i = 0; i < numFileEntries; i ++) { |
Alex Sakhartchouk | aae74ad | 2010-06-04 10:06:50 -0700 | [diff] [blame] | 403 | const FileA3D::A3DIndexEntry *entry = fa3d->getIndexEntry(i); |
| 404 | fileEntries[i].classID = entry->getType(); |
| 405 | fileEntries[i].objectName = entry->getObjectName().string(); |
| 406 | } |
Alex Sakhartchouk | aae74ad | 2010-06-04 10:06:50 -0700 | [diff] [blame] | 407 | } |
| 408 | |
Alex Sakhartchouk | b0253ea | 2011-01-07 11:12:08 -0800 | [diff] [blame] | 409 | RsFile rsaFileA3DCreateFromMemory(RsContext con, const void *data, uint32_t len) { |
Alex Sakhartchouk | aae74ad | 2010-06-04 10:06:50 -0700 | [diff] [blame] | 410 | if (data == NULL) { |
Steve Block | 3762c31 | 2012-01-06 19:20:56 +0000 | [diff] [blame] | 411 | ALOGE("File load failed. Asset stream is NULL"); |
Alex Sakhartchouk | aae74ad | 2010-06-04 10:06:50 -0700 | [diff] [blame] | 412 | return NULL; |
| 413 | } |
| 414 | |
Alex Sakhartchouk | 581cc64 | 2010-10-27 14:10:07 -0700 | [diff] [blame] | 415 | Context *rsc = static_cast<Context *>(con); |
Alex Sakhartchouk | aae74ad | 2010-06-04 10:06:50 -0700 | [diff] [blame] | 416 | FileA3D *fa3d = new FileA3D(rsc); |
Alex Sakhartchouk | aae74ad | 2010-06-04 10:06:50 -0700 | [diff] [blame] | 417 | fa3d->incUserRef(); |
| 418 | |
Alex Sakhartchouk | 581cc64 | 2010-10-27 14:10:07 -0700 | [diff] [blame] | 419 | fa3d->load(data, len); |
Alex Sakhartchouk | aae74ad | 2010-06-04 10:06:50 -0700 | [diff] [blame] | 420 | return fa3d; |
| 421 | } |
Alex Sakhartchouk | b0253ea | 2011-01-07 11:12:08 -0800 | [diff] [blame] | 422 | |
| 423 | RsFile 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 | |
| 433 | RsFile rsaFileA3DCreateFromFile(RsContext con, const char *path) { |
| 434 | if (path == NULL) { |
Steve Block | 3762c31 | 2012-01-06 19:20:56 +0000 | [diff] [blame] | 435 | ALOGE("File load failed. Path is NULL"); |
Alex Sakhartchouk | b0253ea | 2011-01-07 11:12:08 -0800 | [diff] [blame] | 436 | 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 Block | 3762c31 | 2012-01-06 19:20:56 +0000 | [diff] [blame] | 449 | ALOGE("Could not open file %s", path); |
Alex Sakhartchouk | b0253ea | 2011-01-07 11:12:08 -0800 | [diff] [blame] | 450 | } |
| 451 | |
| 452 | return fa3d; |
| 453 | } |