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 | |
| 18 | #include "rsContext.h" |
Jason Sams | a5597fc | 2009-07-08 18:01:53 -0700 | [diff] [blame] | 19 | #include "rsFileA3D.h" |
| 20 | |
| 21 | #include "rsMesh.h" |
Alex Sakhartchouk | fb6b614 | 2010-05-21 12:53:13 -0700 | [diff] [blame] | 22 | #include "rsAnimation.h" |
Alex Sakhartchouk | e23d239 | 2012-03-09 09:24:39 -0800 | [diff] [blame] | 23 | #include "rs.h" |
Alex Sakhartchouk | d3e0ad4 | 2010-06-24 17:15:34 -0700 | [diff] [blame] | 24 | |
Jason Sams | a6ab26a | 2012-03-28 17:44:21 -0700 | [diff] [blame] | 25 | #if !defined(__RS_PDK__) |
| 26 | #include <androidfw/Asset.h> |
| 27 | #endif |
| 28 | |
Jason Sams | a5597fc | 2009-07-08 18:01:53 -0700 | [diff] [blame] | 29 | using namespace android; |
| 30 | using namespace android::renderscript; |
| 31 | |
Alex Sakhartchouk | afb743a | 2010-11-09 17:00:54 -0800 | [diff] [blame] | 32 | FileA3D::FileA3D(Context *rsc) : ObjectBase(rsc) { |
Alex Sakhartchouk | fb6b614 | 2010-05-21 12:53:13 -0700 | [diff] [blame] | 33 | mAlloc = NULL; |
| 34 | mData = NULL; |
| 35 | mWriteStream = NULL; |
| 36 | mReadStream = NULL; |
Alex Sakhartchouk | 5224a27 | 2011-01-07 11:12:08 -0800 | [diff] [blame] | 37 | mAsset = NULL; |
Alex Sakhartchouk | fb6b614 | 2010-05-21 12:53:13 -0700 | [diff] [blame] | 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 | } |
Alex Sakhartchouk | 5224a27 | 2011-01-07 11:12:08 -0800 | [diff] [blame] | 60 | if (mAsset) { |
Jason Sams | a6ab26a | 2012-03-28 17:44:21 -0700 | [diff] [blame] | 61 | #if !defined(__RS_PDK__) |
Alex Sakhartchouk | 5224a27 | 2011-01-07 11:12:08 -0800 | [diff] [blame] | 62 | delete mAsset; |
Jason Sams | a6ab26a | 2012-03-28 17:44:21 -0700 | [diff] [blame] | 63 | #endif |
Alex Sakhartchouk | 5224a27 | 2011-01-07 11:12:08 -0800 | [diff] [blame] | 64 | } |
Jason Sams | a5597fc | 2009-07-08 18:01:53 -0700 | [diff] [blame] | 65 | } |
| 66 | |
Alex Sakhartchouk | afb743a | 2010-11-09 17:00:54 -0800 | [diff] [blame] | 67 | void FileA3D::parseHeader(IStream *headerStream) { |
Alex Sakhartchouk | b825f67 | 2010-06-04 10:06:50 -0700 | [diff] [blame] | 68 | mMajorVersion = headerStream->loadU32(); |
| 69 | mMinorVersion = headerStream->loadU32(); |
| 70 | uint32_t flags = headerStream->loadU32(); |
| 71 | mUse64BitOffsets = (flags & 1) != 0; |
| 72 | |
Alex Sakhartchouk | b825f67 | 2010-06-04 10:06:50 -0700 | [diff] [blame] | 73 | uint32_t numIndexEntries = headerStream->loadU32(); |
Alex Sakhartchouk | afb743a | 2010-11-09 17:00:54 -0800 | [diff] [blame] | 74 | for (uint32_t i = 0; i < numIndexEntries; i ++) { |
Alex Sakhartchouk | b825f67 | 2010-06-04 10:06:50 -0700 | [diff] [blame] | 75 | A3DIndexEntry *entry = new A3DIndexEntry(); |
| 76 | headerStream->loadString(&entry->mObjectName); |
Steve Block | 6598201 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 77 | //ALOGV("Header data, entry name = %s", entry->mObjectName.string()); |
Alex Sakhartchouk | b825f67 | 2010-06-04 10:06:50 -0700 | [diff] [blame] | 78 | entry->mType = (RsA3DClassID)headerStream->loadU32(); |
Alex Sakhartchouk | afb743a | 2010-11-09 17:00:54 -0800 | [diff] [blame] | 79 | if (mUse64BitOffsets){ |
Alex Sakhartchouk | b825f67 | 2010-06-04 10:06:50 -0700 | [diff] [blame] | 80 | entry->mOffset = headerStream->loadOffset(); |
| 81 | entry->mLength = headerStream->loadOffset(); |
Alex Sakhartchouk | afb743a | 2010-11-09 17:00:54 -0800 | [diff] [blame] | 82 | } else { |
Alex Sakhartchouk | b825f67 | 2010-06-04 10:06:50 -0700 | [diff] [blame] | 83 | entry->mOffset = headerStream->loadU32(); |
| 84 | entry->mLength = headerStream->loadU32(); |
| 85 | } |
| 86 | entry->mRsObj = NULL; |
| 87 | mIndex.push(entry); |
| 88 | } |
| 89 | } |
| 90 | |
Alex Sakhartchouk | 5224a27 | 2011-01-07 11:12:08 -0800 | [diff] [blame] | 91 | bool FileA3D::load(Asset *asset) { |
Jason Sams | a6ab26a | 2012-03-28 17:44:21 -0700 | [diff] [blame] | 92 | #if !defined(__RS_PDK__) |
Alex Sakhartchouk | 5224a27 | 2011-01-07 11:12:08 -0800 | [diff] [blame] | 93 | mAsset = asset; |
| 94 | return load(asset->getBuffer(false), asset->getLength()); |
Jason Sams | a6ab26a | 2012-03-28 17:44:21 -0700 | [diff] [blame] | 95 | #else |
| 96 | return false; |
| 97 | #endif |
Alex Sakhartchouk | 5224a27 | 2011-01-07 11:12:08 -0800 | [diff] [blame] | 98 | } |
| 99 | |
Alex Sakhartchouk | afb743a | 2010-11-09 17:00:54 -0800 | [diff] [blame] | 100 | bool FileA3D::load(const void *data, size_t length) { |
Alex Sakhartchouk | b825f67 | 2010-06-04 10:06:50 -0700 | [diff] [blame] | 101 | const uint8_t *localData = (const uint8_t *)data; |
| 102 | |
| 103 | size_t lengthRemaining = length; |
| 104 | size_t magicStrLen = 12; |
| 105 | if ((length < magicStrLen) || |
| 106 | memcmp(data, "Android3D_ff", magicStrLen)) { |
| 107 | return false; |
| 108 | } |
| 109 | |
| 110 | localData += magicStrLen; |
| 111 | lengthRemaining -= magicStrLen; |
| 112 | |
| 113 | // Next we get our header size |
| 114 | uint64_t headerSize = 0; |
Alex Sakhartchouk | afb743a | 2010-11-09 17:00:54 -0800 | [diff] [blame] | 115 | if (lengthRemaining < sizeof(headerSize)) { |
Alex Sakhartchouk | b825f67 | 2010-06-04 10:06:50 -0700 | [diff] [blame] | 116 | return false; |
| 117 | } |
| 118 | |
| 119 | memcpy(&headerSize, localData, sizeof(headerSize)); |
| 120 | localData += sizeof(headerSize); |
| 121 | lengthRemaining -= sizeof(headerSize); |
| 122 | |
Alex Sakhartchouk | afb743a | 2010-11-09 17:00:54 -0800 | [diff] [blame] | 123 | if (lengthRemaining < headerSize) { |
Alex Sakhartchouk | b825f67 | 2010-06-04 10:06:50 -0700 | [diff] [blame] | 124 | return false; |
| 125 | } |
| 126 | |
Alex Sakhartchouk | b825f67 | 2010-06-04 10:06:50 -0700 | [diff] [blame] | 127 | // Now open the stream to parse the header |
Alex Sakhartchouk | dc763f3 | 2010-10-27 14:10:07 -0700 | [diff] [blame] | 128 | IStream headerStream(localData, false); |
Alex Sakhartchouk | b825f67 | 2010-06-04 10:06:50 -0700 | [diff] [blame] | 129 | parseHeader(&headerStream); |
| 130 | |
Alex Sakhartchouk | b825f67 | 2010-06-04 10:06:50 -0700 | [diff] [blame] | 131 | localData += headerSize; |
| 132 | lengthRemaining -= headerSize; |
| 133 | |
Alex Sakhartchouk | afb743a | 2010-11-09 17:00:54 -0800 | [diff] [blame] | 134 | if (lengthRemaining < sizeof(mDataSize)) { |
Alex Sakhartchouk | b825f67 | 2010-06-04 10:06:50 -0700 | [diff] [blame] | 135 | return false; |
| 136 | } |
| 137 | |
| 138 | // Read the size of the data |
| 139 | memcpy(&mDataSize, localData, sizeof(mDataSize)); |
| 140 | localData += sizeof(mDataSize); |
| 141 | lengthRemaining -= sizeof(mDataSize); |
| 142 | |
Alex Sakhartchouk | afb743a | 2010-11-09 17:00:54 -0800 | [diff] [blame] | 143 | if (lengthRemaining < mDataSize) { |
Alex Sakhartchouk | b825f67 | 2010-06-04 10:06:50 -0700 | [diff] [blame] | 144 | return false; |
| 145 | } |
| 146 | |
| 147 | // We should know enough to read the file in at this point. |
Alex Sakhartchouk | dc763f3 | 2010-10-27 14:10:07 -0700 | [diff] [blame] | 148 | mData = (uint8_t *)localData; |
Alex Sakhartchouk | b825f67 | 2010-06-04 10:06:50 -0700 | [diff] [blame] | 149 | mReadStream = new IStream(mData, mUse64BitOffsets); |
| 150 | |
| 151 | return true; |
| 152 | } |
| 153 | |
Alex Sakhartchouk | afb743a | 2010-11-09 17:00:54 -0800 | [diff] [blame] | 154 | bool FileA3D::load(FILE *f) { |
Jason Sams | a5597fc | 2009-07-08 18:01:53 -0700 | [diff] [blame] | 155 | char magicString[12]; |
| 156 | size_t len; |
| 157 | |
Steve Block | 6598201 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 158 | ALOGV("file open 1"); |
Jason Sams | a5597fc | 2009-07-08 18:01:53 -0700 | [diff] [blame] | 159 | len = fread(magicString, 1, 12, f); |
| 160 | if ((len != 12) || |
| 161 | memcmp(magicString, "Android3D_ff", 12)) { |
| 162 | return false; |
| 163 | } |
| 164 | |
Alex Sakhartchouk | fb6b614 | 2010-05-21 12:53:13 -0700 | [diff] [blame] | 165 | // Next thing is the size of the header |
| 166 | uint64_t headerSize = 0; |
| 167 | len = fread(&headerSize, 1, sizeof(headerSize), f); |
| 168 | if (len != sizeof(headerSize) || headerSize == 0) { |
Jason Sams | a5597fc | 2009-07-08 18:01:53 -0700 | [diff] [blame] | 169 | return false; |
| 170 | } |
| 171 | |
Alex Sakhartchouk | fb6b614 | 2010-05-21 12:53:13 -0700 | [diff] [blame] | 172 | uint8_t *headerData = (uint8_t *)malloc(headerSize); |
Alex Sakhartchouk | afb743a | 2010-11-09 17:00:54 -0800 | [diff] [blame] | 173 | if (!headerData) { |
Jason Sams | a5597fc | 2009-07-08 18:01:53 -0700 | [diff] [blame] | 174 | return false; |
| 175 | } |
| 176 | |
Alex Sakhartchouk | fb6b614 | 2010-05-21 12:53:13 -0700 | [diff] [blame] | 177 | len = fread(headerData, 1, headerSize, f); |
| 178 | if (len != headerSize) { |
Jason Sams | a5597fc | 2009-07-08 18:01:53 -0700 | [diff] [blame] | 179 | return false; |
| 180 | } |
Alex Sakhartchouk | fb6b614 | 2010-05-21 12:53:13 -0700 | [diff] [blame] | 181 | |
| 182 | // Now open the stream to parse the header |
| 183 | IStream headerStream(headerData, false); |
Alex Sakhartchouk | b825f67 | 2010-06-04 10:06:50 -0700 | [diff] [blame] | 184 | parseHeader(&headerStream); |
Alex Sakhartchouk | fb6b614 | 2010-05-21 12:53:13 -0700 | [diff] [blame] | 185 | |
Alex Sakhartchouk | b825f67 | 2010-06-04 10:06:50 -0700 | [diff] [blame] | 186 | free(headerData); |
Alex Sakhartchouk | fb6b614 | 2010-05-21 12:53:13 -0700 | [diff] [blame] | 187 | |
| 188 | // Next thing is the size of the header |
| 189 | len = fread(&mDataSize, 1, sizeof(mDataSize), f); |
| 190 | if (len != sizeof(mDataSize) || mDataSize == 0) { |
| 191 | return false; |
Jason Sams | a5597fc | 2009-07-08 18:01:53 -0700 | [diff] [blame] | 192 | } |
| 193 | |
Steve Block | 6598201 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 194 | ALOGV("file open size = %lli", mDataSize); |
Jason Sams | a5597fc | 2009-07-08 18:01:53 -0700 | [diff] [blame] | 195 | |
| 196 | // We should know enough to read the file in at this point. |
Alex Sakhartchouk | fb6b614 | 2010-05-21 12:53:13 -0700 | [diff] [blame] | 197 | mAlloc = malloc(mDataSize); |
Jason Sams | a5597fc | 2009-07-08 18:01:53 -0700 | [diff] [blame] | 198 | if (!mAlloc) { |
| 199 | return false; |
| 200 | } |
| 201 | mData = (uint8_t *)mAlloc; |
| 202 | len = fread(mAlloc, 1, mDataSize, f); |
| 203 | if (len != mDataSize) { |
| 204 | return false; |
| 205 | } |
| 206 | |
Alex Sakhartchouk | fb6b614 | 2010-05-21 12:53:13 -0700 | [diff] [blame] | 207 | mReadStream = new IStream(mData, mUse64BitOffsets); |
| 208 | |
Steve Block | 6598201 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 209 | ALOGV("Header is read an stream initialized"); |
Alex Sakhartchouk | fb6b614 | 2010-05-21 12:53:13 -0700 | [diff] [blame] | 210 | return true; |
Jason Sams | a5597fc | 2009-07-08 18:01:53 -0700 | [diff] [blame] | 211 | } |
| 212 | |
Alex Sakhartchouk | b825f67 | 2010-06-04 10:06:50 -0700 | [diff] [blame] | 213 | size_t FileA3D::getNumIndexEntries() const { |
Alex Sakhartchouk | fb6b614 | 2010-05-21 12:53:13 -0700 | [diff] [blame] | 214 | return mIndex.size(); |
| 215 | } |
Jason Sams | a5597fc | 2009-07-08 18:01:53 -0700 | [diff] [blame] | 216 | |
Alex Sakhartchouk | b825f67 | 2010-06-04 10:06:50 -0700 | [diff] [blame] | 217 | const FileA3D::A3DIndexEntry *FileA3D::getIndexEntry(size_t index) const { |
Alex Sakhartchouk | afb743a | 2010-11-09 17:00:54 -0800 | [diff] [blame] | 218 | if (index < mIndex.size()) { |
Alex Sakhartchouk | fb6b614 | 2010-05-21 12:53:13 -0700 | [diff] [blame] | 219 | return mIndex[index]; |
Jason Sams | a5597fc | 2009-07-08 18:01:53 -0700 | [diff] [blame] | 220 | } |
Alex Sakhartchouk | fb6b614 | 2010-05-21 12:53:13 -0700 | [diff] [blame] | 221 | return NULL; |
Jason Sams | a5597fc | 2009-07-08 18:01:53 -0700 | [diff] [blame] | 222 | } |
| 223 | |
Alex Sakhartchouk | b825f67 | 2010-06-04 10:06:50 -0700 | [diff] [blame] | 224 | ObjectBase *FileA3D::initializeFromEntry(size_t index) { |
Alex Sakhartchouk | afb743a | 2010-11-09 17:00:54 -0800 | [diff] [blame] | 225 | if (index >= mIndex.size()) { |
Alex Sakhartchouk | b825f67 | 2010-06-04 10:06:50 -0700 | [diff] [blame] | 226 | return NULL; |
| 227 | } |
| 228 | |
| 229 | FileA3D::A3DIndexEntry *entry = mIndex[index]; |
Alex Sakhartchouk | afb743a | 2010-11-09 17:00:54 -0800 | [diff] [blame] | 230 | if (!entry) { |
Alex Sakhartchouk | fb6b614 | 2010-05-21 12:53:13 -0700 | [diff] [blame] | 231 | return NULL; |
| 232 | } |
| 233 | |
Alex Sakhartchouk | afb743a | 2010-11-09 17:00:54 -0800 | [diff] [blame] | 234 | if (entry->mRsObj) { |
Alex Sakhartchouk | b825f67 | 2010-06-04 10:06:50 -0700 | [diff] [blame] | 235 | entry->mRsObj->incUserRef(); |
| 236 | return entry->mRsObj; |
| 237 | } |
| 238 | |
Alex Sakhartchouk | fb6b614 | 2010-05-21 12:53:13 -0700 | [diff] [blame] | 239 | // Seek to the beginning of object |
| 240 | mReadStream->reset(entry->mOffset); |
| 241 | switch (entry->mType) { |
Alex Sakhartchouk | b825f67 | 2010-06-04 10:06:50 -0700 | [diff] [blame] | 242 | case RS_A3D_CLASS_ID_UNKNOWN: |
Alex Sakhartchouk | fb6b614 | 2010-05-21 12:53:13 -0700 | [diff] [blame] | 243 | return NULL; |
Alex Sakhartchouk | b825f67 | 2010-06-04 10:06:50 -0700 | [diff] [blame] | 244 | case RS_A3D_CLASS_ID_MESH: |
| 245 | entry->mRsObj = Mesh::createFromStream(mRSC, mReadStream); |
| 246 | break; |
Alex Sakhartchouk | b825f67 | 2010-06-04 10:06:50 -0700 | [diff] [blame] | 247 | case RS_A3D_CLASS_ID_TYPE: |
| 248 | entry->mRsObj = Type::createFromStream(mRSC, mReadStream); |
| 249 | break; |
| 250 | case RS_A3D_CLASS_ID_ELEMENT: |
| 251 | entry->mRsObj = Element::createFromStream(mRSC, mReadStream); |
| 252 | break; |
| 253 | case RS_A3D_CLASS_ID_ALLOCATION: |
| 254 | entry->mRsObj = Allocation::createFromStream(mRSC, mReadStream); |
| 255 | break; |
| 256 | case RS_A3D_CLASS_ID_PROGRAM_VERTEX: |
Alex Sakhartchouk | 099d7d3 | 2011-01-28 09:31:47 -0800 | [diff] [blame] | 257 | //entry->mRsObj = ProgramVertex::createFromStream(mRSC, mReadStream); |
Alex Sakhartchouk | b825f67 | 2010-06-04 10:06:50 -0700 | [diff] [blame] | 258 | break; |
| 259 | case RS_A3D_CLASS_ID_PROGRAM_RASTER: |
Alex Sakhartchouk | 099d7d3 | 2011-01-28 09:31:47 -0800 | [diff] [blame] | 260 | //entry->mRsObj = ProgramRaster::createFromStream(mRSC, mReadStream); |
Alex Sakhartchouk | b825f67 | 2010-06-04 10:06:50 -0700 | [diff] [blame] | 261 | break; |
| 262 | case RS_A3D_CLASS_ID_PROGRAM_FRAGMENT: |
Alex Sakhartchouk | 099d7d3 | 2011-01-28 09:31:47 -0800 | [diff] [blame] | 263 | //entry->mRsObj = ProgramFragment::createFromStream(mRSC, mReadStream); |
Alex Sakhartchouk | b825f67 | 2010-06-04 10:06:50 -0700 | [diff] [blame] | 264 | break; |
| 265 | case RS_A3D_CLASS_ID_PROGRAM_STORE: |
Alex Sakhartchouk | 099d7d3 | 2011-01-28 09:31:47 -0800 | [diff] [blame] | 266 | //entry->mRsObj = ProgramStore::createFromStream(mRSC, mReadStream); |
Alex Sakhartchouk | b825f67 | 2010-06-04 10:06:50 -0700 | [diff] [blame] | 267 | break; |
| 268 | case RS_A3D_CLASS_ID_SAMPLER: |
Alex Sakhartchouk | 099d7d3 | 2011-01-28 09:31:47 -0800 | [diff] [blame] | 269 | //entry->mRsObj = Sampler::createFromStream(mRSC, mReadStream); |
Alex Sakhartchouk | b825f67 | 2010-06-04 10:06:50 -0700 | [diff] [blame] | 270 | break; |
| 271 | case RS_A3D_CLASS_ID_ANIMATION: |
Alex Sakhartchouk | 099d7d3 | 2011-01-28 09:31:47 -0800 | [diff] [blame] | 272 | //entry->mRsObj = Animation::createFromStream(mRSC, mReadStream); |
Alex Sakhartchouk | b825f67 | 2010-06-04 10:06:50 -0700 | [diff] [blame] | 273 | break; |
Alex Sakhartchouk | b825f67 | 2010-06-04 10:06:50 -0700 | [diff] [blame] | 274 | case RS_A3D_CLASS_ID_ADAPTER_1D: |
Alex Sakhartchouk | 099d7d3 | 2011-01-28 09:31:47 -0800 | [diff] [blame] | 275 | //entry->mRsObj = Adapter1D::createFromStream(mRSC, mReadStream); |
Alex Sakhartchouk | b825f67 | 2010-06-04 10:06:50 -0700 | [diff] [blame] | 276 | break; |
| 277 | case RS_A3D_CLASS_ID_ADAPTER_2D: |
Alex Sakhartchouk | 099d7d3 | 2011-01-28 09:31:47 -0800 | [diff] [blame] | 278 | //entry->mRsObj = Adapter2D::createFromStream(mRSC, mReadStream); |
Alex Sakhartchouk | b825f67 | 2010-06-04 10:06:50 -0700 | [diff] [blame] | 279 | break; |
| 280 | case RS_A3D_CLASS_ID_SCRIPT_C: |
Alex Sakhartchouk | 099d7d3 | 2011-01-28 09:31:47 -0800 | [diff] [blame] | 281 | break; |
Alex Sakhartchouk | fb6b614 | 2010-05-21 12:53:13 -0700 | [diff] [blame] | 282 | } |
Alex Sakhartchouk | afb743a | 2010-11-09 17:00:54 -0800 | [diff] [blame] | 283 | if (entry->mRsObj) { |
Alex Sakhartchouk | b825f67 | 2010-06-04 10:06:50 -0700 | [diff] [blame] | 284 | entry->mRsObj->incUserRef(); |
| 285 | } |
| 286 | return entry->mRsObj; |
Alex Sakhartchouk | fb6b614 | 2010-05-21 12:53:13 -0700 | [diff] [blame] | 287 | } |
| 288 | |
Alex Sakhartchouk | afb743a | 2010-11-09 17:00:54 -0800 | [diff] [blame] | 289 | bool FileA3D::writeFile(const char *filename) { |
| 290 | if (!mWriteStream) { |
Steve Block | af12ac6 | 2012-01-06 19:20:56 +0000 | [diff] [blame] | 291 | ALOGE("No objects to write\n"); |
Alex Sakhartchouk | fb6b614 | 2010-05-21 12:53:13 -0700 | [diff] [blame] | 292 | return false; |
| 293 | } |
Alex Sakhartchouk | afb743a | 2010-11-09 17:00:54 -0800 | [diff] [blame] | 294 | if (mWriteStream->getPos() == 0) { |
Steve Block | af12ac6 | 2012-01-06 19:20:56 +0000 | [diff] [blame] | 295 | ALOGE("No objects to write\n"); |
Alex Sakhartchouk | fb6b614 | 2010-05-21 12:53:13 -0700 | [diff] [blame] | 296 | return false; |
| 297 | } |
Jason Sams | a5597fc | 2009-07-08 18:01:53 -0700 | [diff] [blame] | 298 | |
Alex Sakhartchouk | fb6b614 | 2010-05-21 12:53:13 -0700 | [diff] [blame] | 299 | FILE *writeHandle = fopen(filename, "wb"); |
Alex Sakhartchouk | afb743a | 2010-11-09 17:00:54 -0800 | [diff] [blame] | 300 | if (!writeHandle) { |
Steve Block | af12ac6 | 2012-01-06 19:20:56 +0000 | [diff] [blame] | 301 | ALOGE("Couldn't open the file for writing\n"); |
Alex Sakhartchouk | fb6b614 | 2010-05-21 12:53:13 -0700 | [diff] [blame] | 302 | return false; |
| 303 | } |
Jason Sams | a5597fc | 2009-07-08 18:01:53 -0700 | [diff] [blame] | 304 | |
Alex Sakhartchouk | fb6b614 | 2010-05-21 12:53:13 -0700 | [diff] [blame] | 305 | // Open a new stream to make writing the header easier |
| 306 | OStream headerStream(5*1024, false); |
| 307 | headerStream.addU32(mMajorVersion); |
| 308 | headerStream.addU32(mMinorVersion); |
| 309 | uint32_t is64Bit = 0; |
| 310 | headerStream.addU32(is64Bit); |
Jason Sams | a5597fc | 2009-07-08 18:01:53 -0700 | [diff] [blame] | 311 | |
Alex Sakhartchouk | fb6b614 | 2010-05-21 12:53:13 -0700 | [diff] [blame] | 312 | uint32_t writeIndexSize = mWriteIndex.size(); |
| 313 | headerStream.addU32(writeIndexSize); |
Alex Sakhartchouk | afb743a | 2010-11-09 17:00:54 -0800 | [diff] [blame] | 314 | for (uint32_t i = 0; i < writeIndexSize; i ++) { |
Alex Sakhartchouk | b825f67 | 2010-06-04 10:06:50 -0700 | [diff] [blame] | 315 | headerStream.addString(&mWriteIndex[i]->mObjectName); |
Alex Sakhartchouk | fb6b614 | 2010-05-21 12:53:13 -0700 | [diff] [blame] | 316 | headerStream.addU32((uint32_t)mWriteIndex[i]->mType); |
Alex Sakhartchouk | afb743a | 2010-11-09 17:00:54 -0800 | [diff] [blame] | 317 | if (mUse64BitOffsets){ |
Alex Sakhartchouk | fb6b614 | 2010-05-21 12:53:13 -0700 | [diff] [blame] | 318 | headerStream.addOffset(mWriteIndex[i]->mOffset); |
Alex Sakhartchouk | b825f67 | 2010-06-04 10:06:50 -0700 | [diff] [blame] | 319 | headerStream.addOffset(mWriteIndex[i]->mLength); |
Alex Sakhartchouk | afb743a | 2010-11-09 17:00:54 -0800 | [diff] [blame] | 320 | } else { |
Alex Sakhartchouk | fb6b614 | 2010-05-21 12:53:13 -0700 | [diff] [blame] | 321 | uint32_t offset = (uint32_t)mWriteIndex[i]->mOffset; |
| 322 | headerStream.addU32(offset); |
Alex Sakhartchouk | b825f67 | 2010-06-04 10:06:50 -0700 | [diff] [blame] | 323 | offset = (uint32_t)mWriteIndex[i]->mLength; |
| 324 | headerStream.addU32(offset); |
Jason Sams | a5597fc | 2009-07-08 18:01:53 -0700 | [diff] [blame] | 325 | } |
| 326 | } |
| 327 | |
Alex Sakhartchouk | fb6b614 | 2010-05-21 12:53:13 -0700 | [diff] [blame] | 328 | // Write our magic string so we know we are reading the right file |
| 329 | String8 magicString(A3D_MAGIC_KEY); |
| 330 | fwrite(magicString.string(), sizeof(char), magicString.size(), writeHandle); |
Jason Sams | a5597fc | 2009-07-08 18:01:53 -0700 | [diff] [blame] | 331 | |
Alex Sakhartchouk | fb6b614 | 2010-05-21 12:53:13 -0700 | [diff] [blame] | 332 | // Store the size of the header to make it easier to parse when we read it |
| 333 | uint64_t headerSize = headerStream.getPos(); |
| 334 | fwrite(&headerSize, sizeof(headerSize), 1, writeHandle); |
Jason Sams | a5597fc | 2009-07-08 18:01:53 -0700 | [diff] [blame] | 335 | |
Alex Sakhartchouk | fb6b614 | 2010-05-21 12:53:13 -0700 | [diff] [blame] | 336 | // Now write our header |
| 337 | fwrite(headerStream.getPtr(), sizeof(uint8_t), headerStream.getPos(), writeHandle); |
Jason Sams | a5597fc | 2009-07-08 18:01:53 -0700 | [diff] [blame] | 338 | |
Alex Sakhartchouk | fb6b614 | 2010-05-21 12:53:13 -0700 | [diff] [blame] | 339 | // Now write the size of the data part of the file for easier parsing later |
| 340 | uint64_t fileDataSize = mWriteStream->getPos(); |
| 341 | fwrite(&fileDataSize, sizeof(fileDataSize), 1, writeHandle); |
| 342 | |
| 343 | fwrite(mWriteStream->getPtr(), sizeof(uint8_t), mWriteStream->getPos(), writeHandle); |
| 344 | |
| 345 | int status = fclose(writeHandle); |
| 346 | |
Alex Sakhartchouk | afb743a | 2010-11-09 17:00:54 -0800 | [diff] [blame] | 347 | if (status != 0) { |
Steve Block | af12ac6 | 2012-01-06 19:20:56 +0000 | [diff] [blame] | 348 | ALOGE("Couldn't close file\n"); |
Alex Sakhartchouk | fb6b614 | 2010-05-21 12:53:13 -0700 | [diff] [blame] | 349 | return false; |
Jason Sams | a5597fc | 2009-07-08 18:01:53 -0700 | [diff] [blame] | 350 | } |
| 351 | |
Alex Sakhartchouk | fb6b614 | 2010-05-21 12:53:13 -0700 | [diff] [blame] | 352 | return true; |
Jason Sams | a5597fc | 2009-07-08 18:01:53 -0700 | [diff] [blame] | 353 | } |
| 354 | |
Alex Sakhartchouk | fb6b614 | 2010-05-21 12:53:13 -0700 | [diff] [blame] | 355 | void FileA3D::appendToFile(ObjectBase *obj) { |
Alex Sakhartchouk | afb743a | 2010-11-09 17:00:54 -0800 | [diff] [blame] | 356 | if (!obj) { |
Alex Sakhartchouk | fb6b614 | 2010-05-21 12:53:13 -0700 | [diff] [blame] | 357 | return; |
Jason Sams | a5597fc | 2009-07-08 18:01:53 -0700 | [diff] [blame] | 358 | } |
Alex Sakhartchouk | afb743a | 2010-11-09 17:00:54 -0800 | [diff] [blame] | 359 | if (!mWriteStream) { |
Alex Sakhartchouk | fb6b614 | 2010-05-21 12:53:13 -0700 | [diff] [blame] | 360 | const uint64_t initialStreamSize = 256*1024; |
| 361 | mWriteStream = new OStream(initialStreamSize, false); |
Jason Sams | a5597fc | 2009-07-08 18:01:53 -0700 | [diff] [blame] | 362 | } |
Alex Sakhartchouk | fb6b614 | 2010-05-21 12:53:13 -0700 | [diff] [blame] | 363 | A3DIndexEntry *indexEntry = new A3DIndexEntry(); |
Alex Sakhartchouk | b825f67 | 2010-06-04 10:06:50 -0700 | [diff] [blame] | 364 | indexEntry->mObjectName.setTo(obj->getName()); |
Alex Sakhartchouk | fb6b614 | 2010-05-21 12:53:13 -0700 | [diff] [blame] | 365 | indexEntry->mType = obj->getClassId(); |
| 366 | indexEntry->mOffset = mWriteStream->getPos(); |
Alex Sakhartchouk | b825f67 | 2010-06-04 10:06:50 -0700 | [diff] [blame] | 367 | indexEntry->mRsObj = obj; |
Alex Sakhartchouk | fb6b614 | 2010-05-21 12:53:13 -0700 | [diff] [blame] | 368 | mWriteIndex.push(indexEntry); |
| 369 | obj->serialize(mWriteStream); |
Alex Sakhartchouk | b825f67 | 2010-06-04 10:06:50 -0700 | [diff] [blame] | 370 | indexEntry->mLength = mWriteStream->getPos() - indexEntry->mOffset; |
| 371 | mWriteStream->align(4); |
Jason Sams | a5597fc | 2009-07-08 18:01:53 -0700 | [diff] [blame] | 372 | } |
| 373 | |
Alex Sakhartchouk | afb743a | 2010-11-09 17:00:54 -0800 | [diff] [blame] | 374 | RsObjectBase rsaFileA3DGetEntryByIndex(RsContext con, uint32_t index, RsFile file) { |
Alex Sakhartchouk | dc763f3 | 2010-10-27 14:10:07 -0700 | [diff] [blame] | 375 | FileA3D *fa3d = static_cast<FileA3D *>(file); |
Alex Sakhartchouk | afb743a | 2010-11-09 17:00:54 -0800 | [diff] [blame] | 376 | if (!fa3d) { |
Steve Block | af12ac6 | 2012-01-06 19:20:56 +0000 | [diff] [blame] | 377 | ALOGE("Can't load entry. No valid file"); |
Alex Sakhartchouk | dc763f3 | 2010-10-27 14:10:07 -0700 | [diff] [blame] | 378 | return NULL; |
| 379 | } |
| 380 | |
| 381 | ObjectBase *obj = fa3d->initializeFromEntry(index); |
Steve Block | 6598201 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 382 | //ALOGV("Returning object with name %s", obj->getName()); |
Alex Sakhartchouk | dc763f3 | 2010-10-27 14:10:07 -0700 | [diff] [blame] | 383 | |
| 384 | return obj; |
| 385 | } |
| 386 | |
| 387 | |
Alex Sakhartchouk | afb743a | 2010-11-09 17:00:54 -0800 | [diff] [blame] | 388 | void rsaFileA3DGetNumIndexEntries(RsContext con, int32_t *numEntries, RsFile file) { |
Alex Sakhartchouk | b825f67 | 2010-06-04 10:06:50 -0700 | [diff] [blame] | 389 | FileA3D *fa3d = static_cast<FileA3D *>(file); |
| 390 | |
Alex Sakhartchouk | afb743a | 2010-11-09 17:00:54 -0800 | [diff] [blame] | 391 | if (fa3d) { |
Alex Sakhartchouk | b825f67 | 2010-06-04 10:06:50 -0700 | [diff] [blame] | 392 | *numEntries = fa3d->getNumIndexEntries(); |
Alex Sakhartchouk | afb743a | 2010-11-09 17:00:54 -0800 | [diff] [blame] | 393 | } else { |
Alex Sakhartchouk | b825f67 | 2010-06-04 10:06:50 -0700 | [diff] [blame] | 394 | *numEntries = 0; |
| 395 | } |
| 396 | } |
| 397 | |
Alex Sakhartchouk | afb743a | 2010-11-09 17:00:54 -0800 | [diff] [blame] | 398 | void rsaFileA3DGetIndexEntries(RsContext con, RsFileIndexEntry *fileEntries, uint32_t numEntries, RsFile file) { |
Alex Sakhartchouk | b825f67 | 2010-06-04 10:06:50 -0700 | [diff] [blame] | 399 | FileA3D *fa3d = static_cast<FileA3D *>(file); |
| 400 | |
Alex Sakhartchouk | afb743a | 2010-11-09 17:00:54 -0800 | [diff] [blame] | 401 | if (!fa3d) { |
Steve Block | af12ac6 | 2012-01-06 19:20:56 +0000 | [diff] [blame] | 402 | ALOGE("Can't load index entries. No valid file"); |
Alex Sakhartchouk | b825f67 | 2010-06-04 10:06:50 -0700 | [diff] [blame] | 403 | return; |
| 404 | } |
| 405 | |
| 406 | uint32_t numFileEntries = fa3d->getNumIndexEntries(); |
Alex Sakhartchouk | afb743a | 2010-11-09 17:00:54 -0800 | [diff] [blame] | 407 | if (numFileEntries != numEntries || numEntries == 0 || fileEntries == NULL) { |
Steve Block | af12ac6 | 2012-01-06 19:20:56 +0000 | [diff] [blame] | 408 | ALOGE("Can't load index entries. Invalid number requested"); |
Alex Sakhartchouk | b825f67 | 2010-06-04 10:06:50 -0700 | [diff] [blame] | 409 | return; |
| 410 | } |
| 411 | |
Alex Sakhartchouk | afb743a | 2010-11-09 17:00:54 -0800 | [diff] [blame] | 412 | for (uint32_t i = 0; i < numFileEntries; i ++) { |
Alex Sakhartchouk | b825f67 | 2010-06-04 10:06:50 -0700 | [diff] [blame] | 413 | const FileA3D::A3DIndexEntry *entry = fa3d->getIndexEntry(i); |
| 414 | fileEntries[i].classID = entry->getType(); |
| 415 | fileEntries[i].objectName = entry->getObjectName().string(); |
| 416 | } |
Alex Sakhartchouk | b825f67 | 2010-06-04 10:06:50 -0700 | [diff] [blame] | 417 | } |
| 418 | |
Alex Sakhartchouk | 5224a27 | 2011-01-07 11:12:08 -0800 | [diff] [blame] | 419 | RsFile rsaFileA3DCreateFromMemory(RsContext con, const void *data, uint32_t len) { |
Alex Sakhartchouk | b825f67 | 2010-06-04 10:06:50 -0700 | [diff] [blame] | 420 | if (data == NULL) { |
Steve Block | af12ac6 | 2012-01-06 19:20:56 +0000 | [diff] [blame] | 421 | ALOGE("File load failed. Asset stream is NULL"); |
Alex Sakhartchouk | b825f67 | 2010-06-04 10:06:50 -0700 | [diff] [blame] | 422 | return NULL; |
| 423 | } |
| 424 | |
Alex Sakhartchouk | dc763f3 | 2010-10-27 14:10:07 -0700 | [diff] [blame] | 425 | Context *rsc = static_cast<Context *>(con); |
Alex Sakhartchouk | b825f67 | 2010-06-04 10:06:50 -0700 | [diff] [blame] | 426 | FileA3D *fa3d = new FileA3D(rsc); |
Alex Sakhartchouk | b825f67 | 2010-06-04 10:06:50 -0700 | [diff] [blame] | 427 | fa3d->incUserRef(); |
| 428 | |
Alex Sakhartchouk | dc763f3 | 2010-10-27 14:10:07 -0700 | [diff] [blame] | 429 | fa3d->load(data, len); |
Alex Sakhartchouk | b825f67 | 2010-06-04 10:06:50 -0700 | [diff] [blame] | 430 | return fa3d; |
| 431 | } |
Alex Sakhartchouk | 5224a27 | 2011-01-07 11:12:08 -0800 | [diff] [blame] | 432 | |
| 433 | RsFile rsaFileA3DCreateFromAsset(RsContext con, void *_asset) { |
Jason Sams | a6ab26a | 2012-03-28 17:44:21 -0700 | [diff] [blame] | 434 | #if !defined(__RS_PDK__) |
Alex Sakhartchouk | 5224a27 | 2011-01-07 11:12:08 -0800 | [diff] [blame] | 435 | Context *rsc = static_cast<Context *>(con); |
| 436 | Asset *asset = static_cast<Asset *>(_asset); |
| 437 | FileA3D *fa3d = new FileA3D(rsc); |
| 438 | fa3d->incUserRef(); |
| 439 | |
| 440 | fa3d->load(asset); |
| 441 | return fa3d; |
Jason Sams | a6ab26a | 2012-03-28 17:44:21 -0700 | [diff] [blame] | 442 | #else |
| 443 | return NULL; |
| 444 | #endif |
Alex Sakhartchouk | 5224a27 | 2011-01-07 11:12:08 -0800 | [diff] [blame] | 445 | } |
| 446 | |
| 447 | RsFile rsaFileA3DCreateFromFile(RsContext con, const char *path) { |
| 448 | if (path == NULL) { |
Steve Block | af12ac6 | 2012-01-06 19:20:56 +0000 | [diff] [blame] | 449 | ALOGE("File load failed. Path is NULL"); |
Alex Sakhartchouk | 5224a27 | 2011-01-07 11:12:08 -0800 | [diff] [blame] | 450 | return NULL; |
| 451 | } |
| 452 | |
| 453 | Context *rsc = static_cast<Context *>(con); |
| 454 | FileA3D *fa3d = NULL; |
| 455 | |
| 456 | FILE *f = fopen(path, "rb"); |
| 457 | if (f) { |
| 458 | fa3d = new FileA3D(rsc); |
| 459 | fa3d->incUserRef(); |
| 460 | fa3d->load(f); |
| 461 | fclose(f); |
| 462 | } else { |
Steve Block | af12ac6 | 2012-01-06 19:20:56 +0000 | [diff] [blame] | 463 | ALOGE("Could not open file %s", path); |
Alex Sakhartchouk | 5224a27 | 2011-01-07 11:12:08 -0800 | [diff] [blame] | 464 | } |
| 465 | |
| 466 | return fa3d; |
| 467 | } |