Jason Sams | a5597fc | 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 | |
Alex Sakhartchouk | fb6b614 | 2010-05-21 12:53:13 -0700 | [diff] [blame] | 18 | #ifndef ANDROID_RS_BUILD_FOR_HOST |
Jason Sams | a5597fc | 2009-07-08 18:01:53 -0700 | [diff] [blame] | 19 | #include "rsContext.h" |
Alex Sakhartchouk | fb6b614 | 2010-05-21 12:53:13 -0700 | [diff] [blame] | 20 | #else |
| 21 | #include "rsContextHostStub.h" |
| 22 | #endif |
Jason Sams | a5597fc | 2009-07-08 18:01:53 -0700 | [diff] [blame] | 23 | |
Jason Sams | a5597fc | 2009-07-08 18:01:53 -0700 | [diff] [blame] | 24 | #include "rsFileA3D.h" |
| 25 | |
| 26 | #include "rsMesh.h" |
Alex Sakhartchouk | fb6b614 | 2010-05-21 12:53:13 -0700 | [diff] [blame] | 27 | #include "rsAnimation.h" |
Jason Sams | a5597fc | 2009-07-08 18:01:53 -0700 | [diff] [blame] | 28 | |
Alex Sakhartchouk | d3e0ad4 | 2010-06-24 17:15:34 -0700 | [diff] [blame] | 29 | |
Jason Sams | a5597fc | 2009-07-08 18:01:53 -0700 | [diff] [blame] | 30 | using namespace android; |
| 31 | using namespace android::renderscript; |
| 32 | |
Alex Sakhartchouk | afb743a | 2010-11-09 17:00:54 -0800 | [diff] [blame] | 33 | FileA3D::FileA3D(Context *rsc) : ObjectBase(rsc) { |
Alex Sakhartchouk | fb6b614 | 2010-05-21 12:53:13 -0700 | [diff] [blame] | 34 | mAlloc = NULL; |
| 35 | mData = NULL; |
| 36 | mWriteStream = NULL; |
| 37 | mReadStream = NULL; |
| 38 | |
| 39 | mMajorVersion = 0; |
| 40 | mMinorVersion = 1; |
| 41 | mDataSize = 0; |
Jason Sams | a5597fc | 2009-07-08 18:01:53 -0700 | [diff] [blame] | 42 | } |
| 43 | |
Alex Sakhartchouk | afb743a | 2010-11-09 17:00:54 -0800 | [diff] [blame] | 44 | FileA3D::~FileA3D() { |
| 45 | for (size_t i = 0; i < mIndex.size(); i ++) { |
Alex Sakhartchouk | fb6b614 | 2010-05-21 12:53:13 -0700 | [diff] [blame] | 46 | delete mIndex[i]; |
| 47 | } |
Alex Sakhartchouk | afb743a | 2010-11-09 17:00:54 -0800 | [diff] [blame] | 48 | for (size_t i = 0; i < mWriteIndex.size(); i ++) { |
Alex Sakhartchouk | fb6b614 | 2010-05-21 12:53:13 -0700 | [diff] [blame] | 49 | delete mWriteIndex[i]; |
| 50 | } |
Alex Sakhartchouk | afb743a | 2010-11-09 17:00:54 -0800 | [diff] [blame] | 51 | if (mWriteStream) { |
Alex Sakhartchouk | fb6b614 | 2010-05-21 12:53:13 -0700 | [diff] [blame] | 52 | delete mWriteStream; |
| 53 | } |
Alex Sakhartchouk | afb743a | 2010-11-09 17:00:54 -0800 | [diff] [blame] | 54 | if (mReadStream) { |
Alex Sakhartchouk | fb6b614 | 2010-05-21 12:53:13 -0700 | [diff] [blame] | 55 | delete mWriteStream; |
| 56 | } |
Alex Sakhartchouk | afb743a | 2010-11-09 17:00:54 -0800 | [diff] [blame] | 57 | if (mAlloc) { |
Alex Sakhartchouk | fb6b614 | 2010-05-21 12:53:13 -0700 | [diff] [blame] | 58 | free(mAlloc); |
| 59 | } |
Jason Sams | a5597fc | 2009-07-08 18:01:53 -0700 | [diff] [blame] | 60 | } |
| 61 | |
Alex Sakhartchouk | afb743a | 2010-11-09 17:00:54 -0800 | [diff] [blame] | 62 | void FileA3D::parseHeader(IStream *headerStream) { |
Alex Sakhartchouk | b825f67 | 2010-06-04 10:06:50 -0700 | [diff] [blame] | 63 | mMajorVersion = headerStream->loadU32(); |
| 64 | mMinorVersion = headerStream->loadU32(); |
| 65 | uint32_t flags = headerStream->loadU32(); |
| 66 | mUse64BitOffsets = (flags & 1) != 0; |
| 67 | |
Alex Sakhartchouk | b825f67 | 2010-06-04 10:06:50 -0700 | [diff] [blame] | 68 | uint32_t numIndexEntries = headerStream->loadU32(); |
Alex Sakhartchouk | afb743a | 2010-11-09 17:00:54 -0800 | [diff] [blame] | 69 | for (uint32_t i = 0; i < numIndexEntries; i ++) { |
Alex Sakhartchouk | b825f67 | 2010-06-04 10:06:50 -0700 | [diff] [blame] | 70 | A3DIndexEntry *entry = new A3DIndexEntry(); |
| 71 | headerStream->loadString(&entry->mObjectName); |
Alex Sakhartchouk | 383e5b1 | 2010-09-23 16:16:33 -0700 | [diff] [blame] | 72 | LOGV("Header data, entry name = %s", entry->mObjectName.string()); |
Alex Sakhartchouk | b825f67 | 2010-06-04 10:06:50 -0700 | [diff] [blame] | 73 | entry->mType = (RsA3DClassID)headerStream->loadU32(); |
Alex Sakhartchouk | afb743a | 2010-11-09 17:00:54 -0800 | [diff] [blame] | 74 | if (mUse64BitOffsets){ |
Alex Sakhartchouk | b825f67 | 2010-06-04 10:06:50 -0700 | [diff] [blame] | 75 | entry->mOffset = headerStream->loadOffset(); |
| 76 | entry->mLength = headerStream->loadOffset(); |
Alex Sakhartchouk | afb743a | 2010-11-09 17:00:54 -0800 | [diff] [blame] | 77 | } else { |
Alex Sakhartchouk | b825f67 | 2010-06-04 10:06:50 -0700 | [diff] [blame] | 78 | entry->mOffset = headerStream->loadU32(); |
| 79 | entry->mLength = headerStream->loadU32(); |
| 80 | } |
| 81 | entry->mRsObj = NULL; |
| 82 | mIndex.push(entry); |
| 83 | } |
| 84 | } |
| 85 | |
Alex Sakhartchouk | afb743a | 2010-11-09 17:00:54 -0800 | [diff] [blame] | 86 | bool FileA3D::load(const void *data, size_t length) { |
Alex Sakhartchouk | b825f67 | 2010-06-04 10:06:50 -0700 | [diff] [blame] | 87 | 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 Sakhartchouk | afb743a | 2010-11-09 17:00:54 -0800 | [diff] [blame] | 101 | if (lengthRemaining < sizeof(headerSize)) { |
Alex Sakhartchouk | b825f67 | 2010-06-04 10:06:50 -0700 | [diff] [blame] | 102 | return false; |
| 103 | } |
| 104 | |
| 105 | memcpy(&headerSize, localData, sizeof(headerSize)); |
| 106 | localData += sizeof(headerSize); |
| 107 | lengthRemaining -= sizeof(headerSize); |
| 108 | |
Alex Sakhartchouk | afb743a | 2010-11-09 17:00:54 -0800 | [diff] [blame] | 109 | if (lengthRemaining < headerSize) { |
Alex Sakhartchouk | b825f67 | 2010-06-04 10:06:50 -0700 | [diff] [blame] | 110 | return false; |
| 111 | } |
| 112 | |
Alex Sakhartchouk | b825f67 | 2010-06-04 10:06:50 -0700 | [diff] [blame] | 113 | // Now open the stream to parse the header |
Alex Sakhartchouk | dc763f3 | 2010-10-27 14:10:07 -0700 | [diff] [blame] | 114 | IStream headerStream(localData, false); |
Alex Sakhartchouk | b825f67 | 2010-06-04 10:06:50 -0700 | [diff] [blame] | 115 | parseHeader(&headerStream); |
| 116 | |
Alex Sakhartchouk | b825f67 | 2010-06-04 10:06:50 -0700 | [diff] [blame] | 117 | localData += headerSize; |
| 118 | lengthRemaining -= headerSize; |
| 119 | |
Alex Sakhartchouk | afb743a | 2010-11-09 17:00:54 -0800 | [diff] [blame] | 120 | if (lengthRemaining < sizeof(mDataSize)) { |
Alex Sakhartchouk | b825f67 | 2010-06-04 10:06:50 -0700 | [diff] [blame] | 121 | 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 Sakhartchouk | afb743a | 2010-11-09 17:00:54 -0800 | [diff] [blame] | 129 | if (lengthRemaining < mDataSize) { |
Alex Sakhartchouk | b825f67 | 2010-06-04 10:06:50 -0700 | [diff] [blame] | 130 | return false; |
| 131 | } |
| 132 | |
| 133 | // We should know enough to read the file in at this point. |
Alex Sakhartchouk | dc763f3 | 2010-10-27 14:10:07 -0700 | [diff] [blame] | 134 | mData = (uint8_t *)localData; |
Alex Sakhartchouk | b825f67 | 2010-06-04 10:06:50 -0700 | [diff] [blame] | 135 | mReadStream = new IStream(mData, mUse64BitOffsets); |
| 136 | |
| 137 | return true; |
| 138 | } |
| 139 | |
Alex Sakhartchouk | afb743a | 2010-11-09 17:00:54 -0800 | [diff] [blame] | 140 | bool FileA3D::load(FILE *f) { |
Jason Sams | a5597fc | 2009-07-08 18:01:53 -0700 | [diff] [blame] | 141 | char magicString[12]; |
| 142 | size_t len; |
| 143 | |
Alex Sakhartchouk | 383e5b1 | 2010-09-23 16:16:33 -0700 | [diff] [blame] | 144 | LOGV("file open 1"); |
Jason Sams | a5597fc | 2009-07-08 18:01:53 -0700 | [diff] [blame] | 145 | len = fread(magicString, 1, 12, f); |
| 146 | if ((len != 12) || |
| 147 | memcmp(magicString, "Android3D_ff", 12)) { |
| 148 | return false; |
| 149 | } |
| 150 | |
Alex Sakhartchouk | fb6b614 | 2010-05-21 12:53:13 -0700 | [diff] [blame] | 151 | // 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 Sams | a5597fc | 2009-07-08 18:01:53 -0700 | [diff] [blame] | 155 | return false; |
| 156 | } |
| 157 | |
Alex Sakhartchouk | fb6b614 | 2010-05-21 12:53:13 -0700 | [diff] [blame] | 158 | uint8_t *headerData = (uint8_t *)malloc(headerSize); |
Alex Sakhartchouk | afb743a | 2010-11-09 17:00:54 -0800 | [diff] [blame] | 159 | if (!headerData) { |
Jason Sams | a5597fc | 2009-07-08 18:01:53 -0700 | [diff] [blame] | 160 | return false; |
| 161 | } |
| 162 | |
Alex Sakhartchouk | fb6b614 | 2010-05-21 12:53:13 -0700 | [diff] [blame] | 163 | len = fread(headerData, 1, headerSize, f); |
| 164 | if (len != headerSize) { |
Jason Sams | a5597fc | 2009-07-08 18:01:53 -0700 | [diff] [blame] | 165 | return false; |
| 166 | } |
Alex Sakhartchouk | fb6b614 | 2010-05-21 12:53:13 -0700 | [diff] [blame] | 167 | |
| 168 | // Now open the stream to parse the header |
| 169 | IStream headerStream(headerData, false); |
Alex Sakhartchouk | b825f67 | 2010-06-04 10:06:50 -0700 | [diff] [blame] | 170 | parseHeader(&headerStream); |
Alex Sakhartchouk | fb6b614 | 2010-05-21 12:53:13 -0700 | [diff] [blame] | 171 | |
Alex Sakhartchouk | b825f67 | 2010-06-04 10:06:50 -0700 | [diff] [blame] | 172 | free(headerData); |
Alex Sakhartchouk | fb6b614 | 2010-05-21 12:53:13 -0700 | [diff] [blame] | 173 | |
| 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 Sams | a5597fc | 2009-07-08 18:01:53 -0700 | [diff] [blame] | 178 | } |
| 179 | |
Alex Sakhartchouk | 383e5b1 | 2010-09-23 16:16:33 -0700 | [diff] [blame] | 180 | LOGV("file open size = %lli", mDataSize); |
Jason Sams | a5597fc | 2009-07-08 18:01:53 -0700 | [diff] [blame] | 181 | |
| 182 | // We should know enough to read the file in at this point. |
Alex Sakhartchouk | fb6b614 | 2010-05-21 12:53:13 -0700 | [diff] [blame] | 183 | mAlloc = malloc(mDataSize); |
Jason Sams | a5597fc | 2009-07-08 18:01:53 -0700 | [diff] [blame] | 184 | 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 Sakhartchouk | fb6b614 | 2010-05-21 12:53:13 -0700 | [diff] [blame] | 193 | mReadStream = new IStream(mData, mUse64BitOffsets); |
| 194 | |
Alex Sakhartchouk | 383e5b1 | 2010-09-23 16:16:33 -0700 | [diff] [blame] | 195 | LOGV("Header is read an stream initialized"); |
Alex Sakhartchouk | fb6b614 | 2010-05-21 12:53:13 -0700 | [diff] [blame] | 196 | return true; |
Jason Sams | a5597fc | 2009-07-08 18:01:53 -0700 | [diff] [blame] | 197 | } |
| 198 | |
Alex Sakhartchouk | b825f67 | 2010-06-04 10:06:50 -0700 | [diff] [blame] | 199 | size_t FileA3D::getNumIndexEntries() const { |
Alex Sakhartchouk | fb6b614 | 2010-05-21 12:53:13 -0700 | [diff] [blame] | 200 | return mIndex.size(); |
| 201 | } |
Jason Sams | a5597fc | 2009-07-08 18:01:53 -0700 | [diff] [blame] | 202 | |
Alex Sakhartchouk | b825f67 | 2010-06-04 10:06:50 -0700 | [diff] [blame] | 203 | const FileA3D::A3DIndexEntry *FileA3D::getIndexEntry(size_t index) const { |
Alex Sakhartchouk | afb743a | 2010-11-09 17:00:54 -0800 | [diff] [blame] | 204 | if (index < mIndex.size()) { |
Alex Sakhartchouk | fb6b614 | 2010-05-21 12:53:13 -0700 | [diff] [blame] | 205 | return mIndex[index]; |
Jason Sams | a5597fc | 2009-07-08 18:01:53 -0700 | [diff] [blame] | 206 | } |
Alex Sakhartchouk | fb6b614 | 2010-05-21 12:53:13 -0700 | [diff] [blame] | 207 | return NULL; |
Jason Sams | a5597fc | 2009-07-08 18:01:53 -0700 | [diff] [blame] | 208 | } |
| 209 | |
Alex Sakhartchouk | b825f67 | 2010-06-04 10:06:50 -0700 | [diff] [blame] | 210 | ObjectBase *FileA3D::initializeFromEntry(size_t index) { |
Alex Sakhartchouk | afb743a | 2010-11-09 17:00:54 -0800 | [diff] [blame] | 211 | if (index >= mIndex.size()) { |
Alex Sakhartchouk | b825f67 | 2010-06-04 10:06:50 -0700 | [diff] [blame] | 212 | return NULL; |
| 213 | } |
| 214 | |
| 215 | FileA3D::A3DIndexEntry *entry = mIndex[index]; |
Alex Sakhartchouk | afb743a | 2010-11-09 17:00:54 -0800 | [diff] [blame] | 216 | if (!entry) { |
Alex Sakhartchouk | fb6b614 | 2010-05-21 12:53:13 -0700 | [diff] [blame] | 217 | return NULL; |
| 218 | } |
| 219 | |
Alex Sakhartchouk | afb743a | 2010-11-09 17:00:54 -0800 | [diff] [blame] | 220 | if (entry->mRsObj) { |
Alex Sakhartchouk | b825f67 | 2010-06-04 10:06:50 -0700 | [diff] [blame] | 221 | entry->mRsObj->incUserRef(); |
| 222 | return entry->mRsObj; |
| 223 | } |
| 224 | |
Alex Sakhartchouk | fb6b614 | 2010-05-21 12:53:13 -0700 | [diff] [blame] | 225 | // Seek to the beginning of object |
| 226 | mReadStream->reset(entry->mOffset); |
| 227 | switch (entry->mType) { |
Alex Sakhartchouk | b825f67 | 2010-06-04 10:06:50 -0700 | [diff] [blame] | 228 | case RS_A3D_CLASS_ID_UNKNOWN: |
Alex Sakhartchouk | fb6b614 | 2010-05-21 12:53:13 -0700 | [diff] [blame] | 229 | return NULL; |
Alex Sakhartchouk | b825f67 | 2010-06-04 10:06:50 -0700 | [diff] [blame] | 230 | case RS_A3D_CLASS_ID_MESH: |
| 231 | entry->mRsObj = Mesh::createFromStream(mRSC, mReadStream); |
| 232 | break; |
Alex Sakhartchouk | b825f67 | 2010-06-04 10:06:50 -0700 | [diff] [blame] | 233 | 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 Sakhartchouk | b825f67 | 2010-06-04 10:06:50 -0700 | [diff] [blame] | 260 | 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 Sakhartchouk | fb6b614 | 2010-05-21 12:53:13 -0700 | [diff] [blame] | 267 | return NULL; |
| 268 | } |
Alex Sakhartchouk | afb743a | 2010-11-09 17:00:54 -0800 | [diff] [blame] | 269 | if (entry->mRsObj) { |
Alex Sakhartchouk | b825f67 | 2010-06-04 10:06:50 -0700 | [diff] [blame] | 270 | entry->mRsObj->incUserRef(); |
| 271 | } |
| 272 | return entry->mRsObj; |
Alex Sakhartchouk | fb6b614 | 2010-05-21 12:53:13 -0700 | [diff] [blame] | 273 | } |
| 274 | |
Alex Sakhartchouk | afb743a | 2010-11-09 17:00:54 -0800 | [diff] [blame] | 275 | bool FileA3D::writeFile(const char *filename) { |
| 276 | if (!mWriteStream) { |
Alex Sakhartchouk | fb6b614 | 2010-05-21 12:53:13 -0700 | [diff] [blame] | 277 | LOGE("No objects to write\n"); |
| 278 | return false; |
| 279 | } |
Alex Sakhartchouk | afb743a | 2010-11-09 17:00:54 -0800 | [diff] [blame] | 280 | if (mWriteStream->getPos() == 0) { |
Alex Sakhartchouk | fb6b614 | 2010-05-21 12:53:13 -0700 | [diff] [blame] | 281 | LOGE("No objects to write\n"); |
| 282 | return false; |
| 283 | } |
Jason Sams | a5597fc | 2009-07-08 18:01:53 -0700 | [diff] [blame] | 284 | |
Alex Sakhartchouk | fb6b614 | 2010-05-21 12:53:13 -0700 | [diff] [blame] | 285 | FILE *writeHandle = fopen(filename, "wb"); |
Alex Sakhartchouk | afb743a | 2010-11-09 17:00:54 -0800 | [diff] [blame] | 286 | if (!writeHandle) { |
Alex Sakhartchouk | fb6b614 | 2010-05-21 12:53:13 -0700 | [diff] [blame] | 287 | LOGE("Couldn't open the file for writing\n"); |
| 288 | return false; |
| 289 | } |
Jason Sams | a5597fc | 2009-07-08 18:01:53 -0700 | [diff] [blame] | 290 | |
Alex Sakhartchouk | fb6b614 | 2010-05-21 12:53:13 -0700 | [diff] [blame] | 291 | // 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 Sams | a5597fc | 2009-07-08 18:01:53 -0700 | [diff] [blame] | 297 | |
Alex Sakhartchouk | fb6b614 | 2010-05-21 12:53:13 -0700 | [diff] [blame] | 298 | uint32_t writeIndexSize = mWriteIndex.size(); |
| 299 | headerStream.addU32(writeIndexSize); |
Alex Sakhartchouk | afb743a | 2010-11-09 17:00:54 -0800 | [diff] [blame] | 300 | for (uint32_t i = 0; i < writeIndexSize; i ++) { |
Alex Sakhartchouk | b825f67 | 2010-06-04 10:06:50 -0700 | [diff] [blame] | 301 | headerStream.addString(&mWriteIndex[i]->mObjectName); |
Alex Sakhartchouk | fb6b614 | 2010-05-21 12:53:13 -0700 | [diff] [blame] | 302 | headerStream.addU32((uint32_t)mWriteIndex[i]->mType); |
Alex Sakhartchouk | afb743a | 2010-11-09 17:00:54 -0800 | [diff] [blame] | 303 | if (mUse64BitOffsets){ |
Alex Sakhartchouk | fb6b614 | 2010-05-21 12:53:13 -0700 | [diff] [blame] | 304 | headerStream.addOffset(mWriteIndex[i]->mOffset); |
Alex Sakhartchouk | b825f67 | 2010-06-04 10:06:50 -0700 | [diff] [blame] | 305 | headerStream.addOffset(mWriteIndex[i]->mLength); |
Alex Sakhartchouk | afb743a | 2010-11-09 17:00:54 -0800 | [diff] [blame] | 306 | } else { |
Alex Sakhartchouk | fb6b614 | 2010-05-21 12:53:13 -0700 | [diff] [blame] | 307 | uint32_t offset = (uint32_t)mWriteIndex[i]->mOffset; |
| 308 | headerStream.addU32(offset); |
Alex Sakhartchouk | b825f67 | 2010-06-04 10:06:50 -0700 | [diff] [blame] | 309 | offset = (uint32_t)mWriteIndex[i]->mLength; |
| 310 | headerStream.addU32(offset); |
Jason Sams | a5597fc | 2009-07-08 18:01:53 -0700 | [diff] [blame] | 311 | } |
| 312 | } |
| 313 | |
Alex Sakhartchouk | fb6b614 | 2010-05-21 12:53:13 -0700 | [diff] [blame] | 314 | // 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 Sams | a5597fc | 2009-07-08 18:01:53 -0700 | [diff] [blame] | 317 | |
Alex Sakhartchouk | fb6b614 | 2010-05-21 12:53:13 -0700 | [diff] [blame] | 318 | // 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 Sams | a5597fc | 2009-07-08 18:01:53 -0700 | [diff] [blame] | 321 | |
Alex Sakhartchouk | fb6b614 | 2010-05-21 12:53:13 -0700 | [diff] [blame] | 322 | // Now write our header |
| 323 | fwrite(headerStream.getPtr(), sizeof(uint8_t), headerStream.getPos(), writeHandle); |
Jason Sams | a5597fc | 2009-07-08 18:01:53 -0700 | [diff] [blame] | 324 | |
Alex Sakhartchouk | fb6b614 | 2010-05-21 12:53:13 -0700 | [diff] [blame] | 325 | // 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 Sakhartchouk | afb743a | 2010-11-09 17:00:54 -0800 | [diff] [blame] | 333 | if (status != 0) { |
Alex Sakhartchouk | fb6b614 | 2010-05-21 12:53:13 -0700 | [diff] [blame] | 334 | LOGE("Couldn't close file\n"); |
| 335 | return false; |
Jason Sams | a5597fc | 2009-07-08 18:01:53 -0700 | [diff] [blame] | 336 | } |
| 337 | |
Alex Sakhartchouk | fb6b614 | 2010-05-21 12:53:13 -0700 | [diff] [blame] | 338 | return true; |
Jason Sams | a5597fc | 2009-07-08 18:01:53 -0700 | [diff] [blame] | 339 | } |
| 340 | |
Alex Sakhartchouk | fb6b614 | 2010-05-21 12:53:13 -0700 | [diff] [blame] | 341 | void FileA3D::appendToFile(ObjectBase *obj) { |
Alex Sakhartchouk | afb743a | 2010-11-09 17:00:54 -0800 | [diff] [blame] | 342 | if (!obj) { |
Alex Sakhartchouk | fb6b614 | 2010-05-21 12:53:13 -0700 | [diff] [blame] | 343 | return; |
Jason Sams | a5597fc | 2009-07-08 18:01:53 -0700 | [diff] [blame] | 344 | } |
Alex Sakhartchouk | afb743a | 2010-11-09 17:00:54 -0800 | [diff] [blame] | 345 | if (!mWriteStream) { |
Alex Sakhartchouk | fb6b614 | 2010-05-21 12:53:13 -0700 | [diff] [blame] | 346 | const uint64_t initialStreamSize = 256*1024; |
| 347 | mWriteStream = new OStream(initialStreamSize, false); |
Jason Sams | a5597fc | 2009-07-08 18:01:53 -0700 | [diff] [blame] | 348 | } |
Alex Sakhartchouk | fb6b614 | 2010-05-21 12:53:13 -0700 | [diff] [blame] | 349 | A3DIndexEntry *indexEntry = new A3DIndexEntry(); |
Alex Sakhartchouk | b825f67 | 2010-06-04 10:06:50 -0700 | [diff] [blame] | 350 | indexEntry->mObjectName.setTo(obj->getName()); |
Alex Sakhartchouk | fb6b614 | 2010-05-21 12:53:13 -0700 | [diff] [blame] | 351 | indexEntry->mType = obj->getClassId(); |
| 352 | indexEntry->mOffset = mWriteStream->getPos(); |
Alex Sakhartchouk | b825f67 | 2010-06-04 10:06:50 -0700 | [diff] [blame] | 353 | indexEntry->mRsObj = obj; |
Alex Sakhartchouk | fb6b614 | 2010-05-21 12:53:13 -0700 | [diff] [blame] | 354 | mWriteIndex.push(indexEntry); |
| 355 | obj->serialize(mWriteStream); |
Alex Sakhartchouk | b825f67 | 2010-06-04 10:06:50 -0700 | [diff] [blame] | 356 | indexEntry->mLength = mWriteStream->getPos() - indexEntry->mOffset; |
| 357 | mWriteStream->align(4); |
Jason Sams | a5597fc | 2009-07-08 18:01:53 -0700 | [diff] [blame] | 358 | } |
| 359 | |
| 360 | namespace android { |
| 361 | namespace renderscript { |
| 362 | |
Alex Sakhartchouk | afb743a | 2010-11-09 17:00:54 -0800 | [diff] [blame] | 363 | RsFile rsi_FileOpen(Context *rsc, char const *path, unsigned int len) { |
Alex Sakhartchouk | dc763f3 | 2010-10-27 14:10:07 -0700 | [diff] [blame] | 364 | 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 Sakhartchouk | dc763f3 | 2010-10-27 14:10:07 -0700 | [diff] [blame] | 377 | } |
| 378 | } |
| 379 | |
Alex Sakhartchouk | afb743a | 2010-11-09 17:00:54 -0800 | [diff] [blame] | 380 | RsObjectBase rsaFileA3DGetEntryByIndex(RsContext con, uint32_t index, RsFile file) { |
Alex Sakhartchouk | dc763f3 | 2010-10-27 14:10:07 -0700 | [diff] [blame] | 381 | FileA3D *fa3d = static_cast<FileA3D *>(file); |
Alex Sakhartchouk | afb743a | 2010-11-09 17:00:54 -0800 | [diff] [blame] | 382 | if (!fa3d) { |
Alex Sakhartchouk | dc763f3 | 2010-10-27 14:10:07 -0700 | [diff] [blame] | 383 | 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 Sakhartchouk | afb743a | 2010-11-09 17:00:54 -0800 | [diff] [blame] | 394 | void rsaFileA3DGetNumIndexEntries(RsContext con, int32_t *numEntries, RsFile file) { |
Alex Sakhartchouk | b825f67 | 2010-06-04 10:06:50 -0700 | [diff] [blame] | 395 | FileA3D *fa3d = static_cast<FileA3D *>(file); |
| 396 | |
Alex Sakhartchouk | afb743a | 2010-11-09 17:00:54 -0800 | [diff] [blame] | 397 | if (fa3d) { |
Alex Sakhartchouk | b825f67 | 2010-06-04 10:06:50 -0700 | [diff] [blame] | 398 | *numEntries = fa3d->getNumIndexEntries(); |
Alex Sakhartchouk | afb743a | 2010-11-09 17:00:54 -0800 | [diff] [blame] | 399 | } else { |
Alex Sakhartchouk | b825f67 | 2010-06-04 10:06:50 -0700 | [diff] [blame] | 400 | *numEntries = 0; |
| 401 | } |
| 402 | } |
| 403 | |
Alex Sakhartchouk | afb743a | 2010-11-09 17:00:54 -0800 | [diff] [blame] | 404 | void rsaFileA3DGetIndexEntries(RsContext con, RsFileIndexEntry *fileEntries, uint32_t numEntries, RsFile file) { |
Alex Sakhartchouk | b825f67 | 2010-06-04 10:06:50 -0700 | [diff] [blame] | 405 | FileA3D *fa3d = static_cast<FileA3D *>(file); |
| 406 | |
Alex Sakhartchouk | afb743a | 2010-11-09 17:00:54 -0800 | [diff] [blame] | 407 | if (!fa3d) { |
Alex Sakhartchouk | b825f67 | 2010-06-04 10:06:50 -0700 | [diff] [blame] | 408 | LOGE("Can't load index entries. No valid file"); |
| 409 | return; |
| 410 | } |
| 411 | |
| 412 | uint32_t numFileEntries = fa3d->getNumIndexEntries(); |
Alex Sakhartchouk | afb743a | 2010-11-09 17:00:54 -0800 | [diff] [blame] | 413 | if (numFileEntries != numEntries || numEntries == 0 || fileEntries == NULL) { |
Alex Sakhartchouk | b825f67 | 2010-06-04 10:06:50 -0700 | [diff] [blame] | 414 | LOGE("Can't load index entries. Invalid number requested"); |
| 415 | return; |
| 416 | } |
| 417 | |
Alex Sakhartchouk | afb743a | 2010-11-09 17:00:54 -0800 | [diff] [blame] | 418 | for (uint32_t i = 0; i < numFileEntries; i ++) { |
Alex Sakhartchouk | b825f67 | 2010-06-04 10:06:50 -0700 | [diff] [blame] | 419 | const FileA3D::A3DIndexEntry *entry = fa3d->getIndexEntry(i); |
| 420 | fileEntries[i].classID = entry->getType(); |
| 421 | fileEntries[i].objectName = entry->getObjectName().string(); |
| 422 | } |
Alex Sakhartchouk | b825f67 | 2010-06-04 10:06:50 -0700 | [diff] [blame] | 423 | } |
| 424 | |
Alex Sakhartchouk | afb743a | 2010-11-09 17:00:54 -0800 | [diff] [blame] | 425 | RsFile rsaFileA3DCreateFromAssetStream(RsContext con, const void *data, uint32_t len) { |
Alex Sakhartchouk | b825f67 | 2010-06-04 10:06:50 -0700 | [diff] [blame] | 426 | if (data == NULL) { |
| 427 | LOGE("File load failed. Asset stream is NULL"); |
| 428 | return NULL; |
| 429 | } |
| 430 | |
Alex Sakhartchouk | dc763f3 | 2010-10-27 14:10:07 -0700 | [diff] [blame] | 431 | Context *rsc = static_cast<Context *>(con); |
Alex Sakhartchouk | b825f67 | 2010-06-04 10:06:50 -0700 | [diff] [blame] | 432 | FileA3D *fa3d = new FileA3D(rsc); |
Alex Sakhartchouk | b825f67 | 2010-06-04 10:06:50 -0700 | [diff] [blame] | 433 | fa3d->incUserRef(); |
| 434 | |
Alex Sakhartchouk | dc763f3 | 2010-10-27 14:10:07 -0700 | [diff] [blame] | 435 | fa3d->load(data, len); |
Alex Sakhartchouk | b825f67 | 2010-06-04 10:06:50 -0700 | [diff] [blame] | 436 | return fa3d; |
| 437 | } |