The gltfio API allows users to destroy ResourceLoader or FilamentAsset even when various asynchronous work (e.g. uploading buffers to the GPU) has not yet been completed. This was achieved in an error-prone manner using manual reference counting and an internal management object called AssetPool. This PR refactors gltfio by wrapping cgltf_data in shared_ptr, which I usually try to avoid. However in this case it provides the precisely the functionality that is needed. I tested this PR for memory leaks and crashes by hacking gltf_viewer and monitoring memory usage in Activity Monitor. This fixes #3383 and makes it easier to implement some missing features, such as animation support for instanced assets.
62 lines
1.7 KiB
C++
62 lines
1.7 KiB
C++
/*
|
|
* Copyright (C) 2020 The Android Open Source Project
|
|
*
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
* you may not use this file except in compliance with the License.
|
|
* You may obtain a copy of the License at
|
|
*
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
*
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
* See the License for the specific language governing permissions and
|
|
* limitations under the License.
|
|
*/
|
|
|
|
#include "FFilamentInstance.h"
|
|
#include "FFilamentAsset.h"
|
|
|
|
#include <gltfio/Animator.h>
|
|
|
|
#include <utils/Log.h>
|
|
|
|
using namespace filament;
|
|
using namespace utils;
|
|
|
|
namespace gltfio {
|
|
|
|
Animator* FFilamentInstance::getAnimator() noexcept {
|
|
if (!animator) {
|
|
if (!owner->mResourcesLoaded) {
|
|
slog.e << "Cannot create animator before resource loading." << io::endl;
|
|
return nullptr;
|
|
}
|
|
if (!owner->mSourceAsset) {
|
|
slog.e << "Cannot create animator from frozen asset." << io::endl;
|
|
return nullptr;
|
|
}
|
|
animator = new Animator(owner, this);
|
|
}
|
|
return animator;
|
|
}
|
|
|
|
size_t FilamentInstance::getEntityCount() const noexcept {
|
|
return upcast(this)->entities.size();
|
|
}
|
|
|
|
const Entity* FilamentInstance::getEntities() const noexcept {
|
|
const auto& entities = upcast(this)->entities;
|
|
return entities.empty() ? nullptr : entities.data();
|
|
}
|
|
|
|
Entity FilamentInstance::getRoot() const noexcept {
|
|
return upcast(this)->root;
|
|
}
|
|
|
|
Animator* FilamentInstance::getAnimator() noexcept {
|
|
return upcast(this)->getAnimator();
|
|
}
|
|
|
|
} // namespace gltfio
|