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