Skip to content

Commit

Permalink
Pass factory to asset depot constructor
Browse files Browse the repository at this point in the history
  • Loading branch information
azrogers committed Oct 11, 2024
1 parent 3e5eb71 commit 38b4811
Show file tree
Hide file tree
Showing 7 changed files with 62 additions and 41 deletions.
12 changes: 9 additions & 3 deletions Cesium3DTilesSelection/src/TilesetContentManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -666,7 +666,9 @@ TilesetContentManager::TilesetContentManager(
_tileLoadsInProgress{0},
_loadedTilesCount{0},
_tilesDataUsed{0},
_pSharedAssets(CesiumGltfReader::GltfSharedAssetSystem::getDefault()),
_pSharedAssets(CesiumGltfReader::GltfSharedAssetSystem::getDefault(
CesiumGltfReader::AssetSystemOptions{
tilesetOptions.contentOptions.ktx2TranscodeTargets})),
_destructionCompletePromise{externals.asyncSystem.createPromise<void>()},
_destructionCompleteFuture{
this->_destructionCompletePromise.getFuture().share()},
Expand Down Expand Up @@ -696,7 +698,9 @@ TilesetContentManager::TilesetContentManager(
_tileLoadsInProgress{0},
_loadedTilesCount{0},
_tilesDataUsed{0},
_pSharedAssets(CesiumGltfReader::GltfSharedAssetSystem::getDefault()),
_pSharedAssets(CesiumGltfReader::GltfSharedAssetSystem::getDefault(
CesiumGltfReader::AssetSystemOptions{
tilesetOptions.contentOptions.ktx2TranscodeTargets})),
_destructionCompletePromise{externals.asyncSystem.createPromise<void>()},
_destructionCompleteFuture{
this->_destructionCompletePromise.getFuture().share()},
Expand Down Expand Up @@ -848,7 +852,9 @@ TilesetContentManager::TilesetContentManager(
_tileLoadsInProgress{0},
_loadedTilesCount{0},
_tilesDataUsed{0},
_pSharedAssets(CesiumGltfReader::GltfSharedAssetSystem::getDefault()),
_pSharedAssets(CesiumGltfReader::GltfSharedAssetSystem::getDefault(
CesiumGltfReader::AssetSystemOptions{
tilesetOptions.contentOptions.ktx2TranscodeTargets})),
_destructionCompletePromise{externals.asyncSystem.createPromise<void>()},
_destructionCompleteFuture{
this->_destructionCompletePromise.getFuture().share()},
Expand Down
19 changes: 13 additions & 6 deletions CesiumAsync/include/CesiumAsync/SharedAssetDepot.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,12 @@ namespace CesiumAsync {

template <typename T> class SharedAsset;

template <typename AssetType> class AssetFactory {
public:
virtual CesiumUtility::IntrusivePointer<AssetType>
createFrom(const gsl::span<const gsl::byte>& data) const = 0;
};

/**
* A depot for {@link SharedAsset} instances, which are potentially shared between multiple objects.
* @tparam AssetType The type of asset stored in this depot. This should usually
Expand All @@ -44,7 +50,8 @@ class CESIUMASYNC_API SharedAssetDepot
*/
int64_t staleAssetSizeLimit = 16 * 1024 * 1024;

SharedAssetDepot() = default;
SharedAssetDepot(std::unique_ptr<AssetFactory<AssetType>> _factory)
: factory(std::move(_factory)) {}

~SharedAssetDepot() {
// It's possible the assets will outlive the depot, if they're still in use.
Expand Down Expand Up @@ -102,12 +109,10 @@ class CESIUMASYNC_API SharedAssetDepot
* If the asset has already started loading in this depot but hasn't finished,
* its future will be returned.
*/
template <typename Factory>
CesiumAsync::SharedFuture<CesiumUtility::IntrusivePointer<AssetType>>
getOrFetch(
const CesiumAsync::AsyncSystem& asyncSystem,
const std::shared_ptr<CesiumAsync::IAssetAccessor>& pAssetAccessor,
const Factory& factory,
const std::string& uri,
const std::vector<CesiumAsync::IAssetAccessor::THeader>& headers) {
// We need to avoid:
Expand Down Expand Up @@ -138,16 +143,15 @@ class CESIUMASYNC_API SharedAssetDepot
CesiumAsync::Future<CesiumUtility::IntrusivePointer<AssetType>> future =
pAssetAccessor->get(asyncSystem, uri, headers)
.thenInWorkerThread(
[factory = std::move(factory)](
std::shared_ptr<CesiumAsync::IAssetRequest>&& pRequest)
[this](std::shared_ptr<CesiumAsync::IAssetRequest>&& pRequest)
-> CesiumUtility::IntrusivePointer<AssetType> {
const CesiumAsync::IAssetResponse* pResponse =
pRequest->response();
if (!pResponse) {
return nullptr;
}

return factory.createFrom(pResponse->data());
return this->factory->createFrom(pResponse->data());
})
// Do this in main thread since we're messing with the collections.
.thenInMainThread(
Expand Down Expand Up @@ -271,6 +275,9 @@ class CESIUMASYNC_API SharedAssetDepot
// Mutex for modifying the deletionCandidates list.
mutable std::mutex deletionCandidatesMutex;

// The factory used to create new AssetType instances.
std::unique_ptr<AssetFactory<AssetType>> factory;

friend class SharedAsset<AssetType>;
};

Expand Down
2 changes: 1 addition & 1 deletion CesiumGltfReader/include/CesiumGltfReader/GltfReader.h
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ struct CESIUMGLTFREADER_API GltfReaderOptions {
* that might appear in this glTF.
*/
CesiumUtility::IntrusivePointer<GltfSharedAssetSystem> pSharedAssets =
GltfSharedAssetSystem::getDefault();
nullptr;
};

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,28 @@

#include <CesiumAsync/SharedAssetDepot.h>
#include <CesiumAsync/SharedFuture.h>
#include <CesiumGltf/Ktx2TranscodeTargets.h>

namespace CesiumGltf {
struct ImageAsset;
}

namespace CesiumGltfReader {

class AssetSystemOptions {
public:
CesiumGltf::Ktx2TranscodeTargets ktx2TranscodeTargets;
};

/**
* @brief Contains assets that are potentially shared across multiple glTF
* models.
*/
class GltfSharedAssetSystem
: public CesiumUtility::ReferenceCountedThreadSafe<GltfSharedAssetSystem> {
public:
static CesiumUtility::IntrusivePointer<GltfSharedAssetSystem> getDefault();
static CesiumUtility::IntrusivePointer<GltfSharedAssetSystem>
getDefault(const AssetSystemOptions& options);

CesiumUtility::IntrusivePointer<
CesiumAsync::SharedAssetDepot<CesiumGltf::ImageAsset>>
Expand Down
21 changes: 21 additions & 0 deletions CesiumGltfReader/include/CesiumGltfReader/ImageDecoder.h
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#include "CesiumGltf/ImageAsset.h"
#include "CesiumGltfReader/Library.h"

#include <CesiumAsync/SharedAssetDepot.h>
#include <CesiumUtility/IntrusivePointer.h>

#include <gsl/span>
Expand Down Expand Up @@ -66,4 +67,24 @@ class ImageDecoder {
generateMipMaps(CesiumGltf::ImageAsset& image);
};

/**
* Used to construct an ImageAsset.
*/
struct ImageAssetFactory : CesiumAsync::AssetFactory<CesiumGltf::ImageAsset> {
ImageAssetFactory(
const CesiumGltf::Ktx2TranscodeTargets& ktx2TranscodeTargets_)
: ktx2TranscodeTargets(ktx2TranscodeTargets_) {}

CesiumUtility::IntrusivePointer<CesiumGltf::ImageAsset>
createFrom(const gsl::span<const gsl::byte>& data) const override {
CesiumGltfReader::ImageReaderResult imageResult =
ImageDecoder::readImage(data, this->ktx2TranscodeTargets);
// TODO: report warnings and errors!
return imageResult.pImage;
}

private:
const CesiumGltf::Ktx2TranscodeTargets ktx2TranscodeTargets;
};

} // namespace CesiumGltfReader
28 changes: 2 additions & 26 deletions CesiumGltfReader/src/GltfReader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -41,26 +41,6 @@ using namespace CesiumJsonReader;
using namespace CesiumUtility;

namespace {

/**
* Used to construct an ImageAsset.
*/
struct ImageAssetFactory {
ImageAssetFactory(const Ktx2TranscodeTargets& ktx2TranscodeTargets_)
: ktx2TranscodeTargets(ktx2TranscodeTargets_) {}

CesiumUtility::IntrusivePointer<ImageAsset>
createFrom(const gsl::span<const gsl::byte>& data) const {
ImageReaderResult imageResult =
ImageDecoder::readImage(data, this->ktx2TranscodeTargets);
// TODO: report warnings and errors!
return imageResult.pImage;
}

private:
const Ktx2TranscodeTargets ktx2TranscodeTargets;
};

#pragma pack(push, 1)
struct GlbHeader {
uint32_t magic;
Expand Down Expand Up @@ -581,12 +561,8 @@ void CesiumGltfReader::GltfReader::postprocessGltf(
.share();
} else {
// We have a depot, this is easy!
return options.pSharedAssets->pImage->getOrFetch(
asyncSystem,
pAssetAccessor,
ImageAssetFactory(options.ktx2TranscodeTargets),
uri,
headers);
return options.pSharedAssets->pImage
->getOrFetch(asyncSystem, pAssetAccessor, uri, headers);
}
};

Expand Down
12 changes: 8 additions & 4 deletions CesiumGltfReader/src/GltfSharedAssetSystem.cpp
Original file line number Diff line number Diff line change
@@ -1,25 +1,29 @@
#include <CesiumGltf/ImageAsset.h>
#include <CesiumGltfReader/GltfSharedAssetSystem.h>
#include <CesiumGltfReader/ImageDecoder.h>
#include <CesiumGltfReader/ImageReader.h>

namespace CesiumGltfReader {

namespace {

CesiumUtility::IntrusivePointer<GltfSharedAssetSystem> createDefault() {
CesiumUtility::IntrusivePointer<GltfSharedAssetSystem>
createDefault(const AssetSystemOptions& options) {
CesiumUtility::IntrusivePointer<GltfSharedAssetSystem> p =
new GltfSharedAssetSystem();

p->pImage.emplace();
p->pImage.emplace(
std::make_unique<ImageAssetFactory>(options.ktx2TranscodeTargets));

return p;
}

} // namespace

/*static*/ CesiumUtility::IntrusivePointer<GltfSharedAssetSystem>
GltfSharedAssetSystem::getDefault() {
GltfSharedAssetSystem::getDefault(const AssetSystemOptions& options) {
static CesiumUtility::IntrusivePointer<GltfSharedAssetSystem> pDefault =
createDefault();
createDefault(options);
return pDefault;
}

Expand Down

0 comments on commit 38b4811

Please sign in to comment.