| 1 |
/* |
| 2 |
* Copyright (C) 2019 The Android Open Source Project |
| 3 |
* |
| 4 |
* Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 |
* you may not use this file except in compliance with the License. |
| 6 |
* You may obtain a copy of the License at |
| 7 |
* |
| 8 |
* http://www.apache.org/licenses/LICENSE-2.0 |
| 9 |
* |
| 10 |
* Unless required by applicable law or agreed to in writing, software |
| 11 |
* distributed under the License is distributed on an "AS IS" BASIS, |
| 12 |
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 |
* See the License for the specific language governing permissions and |
| 14 |
* limitations under the License. |
| 15 |
*/ |
| 16 |
|
| 17 |
#include "FFilamentAsset.h" |
| 18 |
|
| 19 |
#include <gltfio/Animator.h> |
| 20 |
|
| 21 |
#include <utils/EntityManager.h> |
| 22 |
#include <utils/Log.h> |
| 23 |
#include <utils/NameComponentManager.h> |
| 24 |
|
| 25 |
#include "Wireframe.h" |
| 26 |
|
| 27 |
using namespace filament; |
| 28 |
using namespace utils; |
| 29 |
|
| 30 |
namespace gltfio { |
| 31 |
|
| 32 |
FFilamentAsset::~FFilamentAsset() { |
| 33 |
releaseSourceData(); |
| 34 |
|
| 35 |
// The only things we need to free in the instances are their animators. |
| 36 |
// The union of all instance entities will be destroyed below. |
| 37 |
for (FFilamentInstance* instance : mInstances) { |
| 38 |
delete instance->animator; |
| 39 |
delete instance; |
| 40 |
} |
| 41 |
|
| 42 |
delete mAnimator; |
| 43 |
delete mWireframe; |
| 44 |
|
| 45 |
mEngine->destroy(mRoot); |
| 46 |
mEntityManager->destroy(mRoot); |
| 47 |
|
| 48 |
for (auto entity : mEntities) { |
| 49 |
// Destroy the entity's renderable, light, transform, and camera components. |
| 50 |
mEngine->destroy(entity); |
| 51 |
// Destroy the name component. |
| 52 |
if (mNameManager) { |
| 53 |
mNameManager->removeComponent(entity); |
| 54 |
} |
| 55 |
// Destroy the actual entity. |
| 56 |
mEntityManager->destroy(entity); |
| 57 |
} |
| 58 |
for (auto mi : mMaterialInstances) { |
| 59 |
mEngine->destroy(mi); |
| 60 |
} |
| 61 |
for (auto vb : mVertexBuffers) { |
| 62 |
mEngine->destroy(vb); |
| 63 |
} |
| 64 |
for (auto bo : mBufferObjects) { |
| 65 |
mEngine->destroy(bo); |
| 66 |
} |
| 67 |
for (auto ib : mIndexBuffers) { |
| 68 |
mEngine->destroy(ib); |
| 69 |
} |
| 70 |
for (auto tx : mTextures) { |
| 71 |
mEngine->destroy(tx); |
| 72 |
} |
| 73 |
} |
| 74 |
|
| 75 |
Animator* FFilamentAsset::getAnimator() noexcept { |
| 76 |
if (!mAnimator) { |
| 77 |
if (!mResourcesLoaded) { |
| 78 |
slog.e << "Cannot create animator before resource loading." << io::endl; |
| 79 |
return nullptr; |
| 80 |
} |
| 81 |
if (!mSourceAsset) { |
| 82 |
slog.e << "Cannot create animator from frozen asset." << io::endl; |
| 83 |
return nullptr; |
| 84 |
} |
| 85 |
mAnimator = new Animator(this, nullptr); |
| 86 |
} |
| 87 |
return mAnimator; |
| 88 |
} |
| 89 |
|
| 90 |
Entity FFilamentAsset::getWireframe() noexcept { |
| 91 |
if (!mWireframe) { |
| 92 |
mWireframe = new Wireframe(this); |
| 93 |
} |
| 94 |
return mWireframe->mEntity; |
| 95 |
} |
| 96 |
|
| 97 |
void FFilamentAsset::releaseSourceData() noexcept { |
| 98 |
// To ensure that all possible memory is freed, we reassign to new containers rather than |
| 99 |
// calling clear(). With many container types (such as robin_map), clearing is a fast |
| 100 |
// operation that merely frees the storage for the items. |
| 101 |
mMatInstanceCache = {}; |
| 102 |
mMeshCache = {}; |
| 103 |
mResourceUris = {}; |
| 104 |
mNodeMap = {}; |
| 105 |
mPrimitives = {}; |
| 106 |
mBufferSlots = {}; |
| 107 |
mTextureSlots = {}; |
| 108 |
mSourceAsset.reset(); |
| 109 |
for (FFilamentInstance* instance : mInstances) { |
| 110 |
instance->nodeMap = {}; |
| 111 |
} |
| 112 |
} |
| 113 |
|
| 114 |
const char* FFilamentAsset::getName(utils::Entity entity) const noexcept { |
| 115 |
if (mNameManager == nullptr) { |
| 116 |
return nullptr; |
| 117 |
} |
| 118 |
auto nameInstance = mNameManager->getInstance(entity); |
| 119 |
return nameInstance ? mNameManager->getName(nameInstance) : nullptr; |
| 120 |
} |
| 121 |
|
| 122 |
Entity FFilamentAsset::getFirstEntityByName(const char* name) noexcept { |
| 123 |
const auto iter = mNameToEntity.find(name); |
| 124 |
if (iter == mNameToEntity.end()) { |
| 125 |
return {}; |
| 126 |
} |
| 127 |
return iter->front(); |
| 128 |
} |
| 129 |
|
| 130 |
size_t FFilamentAsset::getEntitiesByName(const char* name, Entity* entities, |
| 131 |
size_t maxCount) const noexcept { |
| 132 |
const auto iter = mNameToEntity.find(name); |
| 133 |
if (iter == mNameToEntity.end()) { |
| 134 |
return 0; |
| 135 |
} |
| 136 |
const auto& source = *iter; |
| 137 |
if (entities == nullptr) { |
| 138 |
return source.size(); |
| 139 |
} |
| 140 |
maxCount = std::min(maxCount, source.size()); |
| 141 |
if (maxCount == 0) { |
| 142 |
return 0; |
| 143 |
} |
| 144 |
size_t count = 0; |
| 145 |
for (Entity entity : source) { |
| 146 |
entities[count] = entity; |
| 147 |
if (++count >= maxCount) { |
| 148 |
return count; |
| 149 |
} |
| 150 |
} |
| 151 |
return count; |
| 152 |
} |
| 153 |
|
| 154 |
size_t FFilamentAsset::getEntitiesByPrefix(const char* prefix, Entity* entities, |
| 155 |
size_t maxCount) const noexcept { |
| 156 |
const auto range = mNameToEntity.equal_prefix_range(prefix); |
| 157 |
size_t count = 0; |
| 158 |
for (auto iter = range.first; iter != range.second; ++iter) { |
| 159 |
count += iter->size(); |
| 160 |
} |
| 161 |
if (entities == nullptr) { |
| 162 |
return count; |
| 163 |
} |
| 164 |
maxCount = std::min(maxCount, count); |
| 165 |
if (maxCount == 0) { |
| 166 |
return 0; |
| 167 |
} |
| 168 |
count = 0; |
| 169 |
for (auto iter = range.first; iter != range.second; ++iter) { |
| 170 |
const auto& source = *iter; |
| 171 |
for (Entity entity : source) { |
| 172 |
entities[count] = entity; |
| 173 |
if (++count >= maxCount) { |
| 174 |
return count; |
| 175 |
} |
| 176 |
} |
| 177 |
} |
| 178 |
return count; |
| 179 |
} |
| 180 |
|
| 181 |
size_t FilamentAsset::getEntityCount() const noexcept { |
| 182 |
return upcast(this)->getEntityCount(); |
| 183 |
} |
| 184 |
|
| 185 |
const Entity* FilamentAsset::getEntities() const noexcept { |
| 186 |
return upcast(this)->getEntities(); |
| 187 |
} |
| 188 |
|
| 189 |
const Entity* FilamentAsset::getLightEntities() const noexcept { |
| 190 |
return upcast(this)->getLightEntities(); |
| 191 |
} |
| 192 |
|
| 193 |
size_t FilamentAsset::getLightEntityCount() const noexcept { |
| 194 |
return upcast(this)->getLightEntityCount(); |
| 195 |
} |
| 196 |
|
| 197 |
const utils::Entity* FilamentAsset::getCameraEntities() const noexcept { |
| 198 |
return upcast(this)->getCameraEntities(); |
| 199 |
} |
| 200 |
|
| 201 |
size_t FilamentAsset::getCameraEntityCount() const noexcept { |
| 202 |
return upcast(this)->getCameraEntityCount(); |
| 203 |
} |
| 204 |
|
| 205 |
Entity FilamentAsset::getRoot() const noexcept { |
| 206 |
return upcast(this)->getRoot(); |
| 207 |
} |
| 208 |
|
| 209 |
Entity FilamentAsset::popRenderable() noexcept { |
| 210 |
Entity result[1]; |
| 211 |
const bool empty = !popRenderables(result, 1); |
| 212 |
return empty ? Entity() : result[0]; |
| 213 |
} |
| 214 |
|
| 215 |
size_t FilamentAsset::popRenderables(Entity* result, size_t count) noexcept { |
| 216 |
return upcast(this)->popRenderables(result, count); |
| 217 |
} |
| 218 |
|
| 219 |
size_t FilamentAsset::getMaterialInstanceCount() const noexcept { |
| 220 |
return upcast(this)->getMaterialInstanceCount(); |
| 221 |
} |
| 222 |
|
| 223 |
const MaterialInstance* const* FilamentAsset::getMaterialInstances() const noexcept { |
| 224 |
return upcast(this)->getMaterialInstances(); |
| 225 |
} |
| 226 |
|
| 227 |
MaterialInstance* const* FilamentAsset::getMaterialInstances() noexcept { |
| 228 |
return upcast(this)->getMaterialInstances(); |
| 229 |
} |
| 230 |
|
| 231 |
size_t FilamentAsset::getResourceUriCount() const noexcept { |
| 232 |
return upcast(this)->getResourceUriCount(); |
| 233 |
} |
| 234 |
|
| 235 |
const char* const* FilamentAsset::getResourceUris() const noexcept { |
| 236 |
return upcast(this)->getResourceUris(); |
| 237 |
} |
| 238 |
|
| 239 |
filament::Aabb FilamentAsset::getBoundingBox() const noexcept { |
| 240 |
return upcast(this)->getBoundingBox(); |
| 241 |
} |
| 242 |
|
| 243 |
const char* FilamentAsset::getName(Entity entity) const noexcept { |
| 244 |
return upcast(this)->getName(entity); |
| 245 |
} |
| 246 |
|
| 247 |
Entity FilamentAsset::getFirstEntityByName(const char* name) noexcept { |
| 248 |
return upcast(this)->getFirstEntityByName(name); |
| 249 |
} |
| 250 |
|
| 251 |
size_t FilamentAsset::getEntitiesByName(const char* name, Entity* entities, |
| 252 |
size_t maxCount) const noexcept { |
| 253 |
return upcast(this)->getEntitiesByName(name, entities, maxCount); |
| 254 |
} |
| 255 |
|
| 256 |
size_t FilamentAsset::getEntitiesByPrefix(const char* prefix, Entity* entities, |
| 257 |
size_t maxCount) const noexcept { |
| 258 |
return upcast(this)->getEntitiesByPrefix(prefix, entities, maxCount); |
| 259 |
} |
| 260 |
|
| 261 |
Animator* FilamentAsset::getAnimator() noexcept { |
| 262 |
return upcast(this)->getAnimator(); |
| 263 |
} |
| 264 |
|
| 265 |
Entity FilamentAsset::getWireframe() noexcept { |
| 266 |
return upcast(this)->getWireframe(); |
| 267 |
} |
| 268 |
|
| 269 |
Engine* FilamentAsset::getEngine() const noexcept { |
| 270 |
return upcast(this)->getEngine(); |
| 271 |
} |
| 272 |
|
| 273 |
void FilamentAsset::releaseSourceData() noexcept { |
| 274 |
return upcast(this)->releaseSourceData(); |
| 275 |
} |
| 276 |
|
| 277 |
const void* FilamentAsset::getSourceAsset() noexcept { |
| 278 |
return upcast(this)->getSourceAsset(); |
| 279 |
} |
| 280 |
|
| 281 |
FilamentInstance** FilamentAsset::getAssetInstances() noexcept { |
| 282 |
return upcast(this)->getAssetInstances(); |
| 283 |
} |
| 284 |
|
| 285 |
size_t FilamentAsset::getAssetInstanceCount() const noexcept { |
| 286 |
return upcast(this)->getAssetInstanceCount(); |
| 287 |
} |
| 288 |
|
| 289 |
} // namespace gltfio |