cesium-native/Cesium3DTilesSelection/test/SimplePrepareRendererResour...

125 lines
3.9 KiB
C
Raw Permalink Normal View History

2021-07-01 14:34:08 +08:00
#pragma once
2024-12-21 01:00:09 +08:00
#include <Cesium3DTilesSelection/IPrepareRendererResources.h>
#include <Cesium3DTilesSelection/Tile.h>
2024-12-21 00:56:49 +08:00
#include <CesiumRasterOverlays/RasterOverlayTile.h>
2022-07-30 05:38:14 +08:00
2025-01-16 05:58:03 +08:00
#include <doctest/doctest.h>
2022-07-30 05:37:35 +08:00
#include <atomic>
#include <functional>
2021-07-01 14:34:08 +08:00
namespace Cesium3DTilesSelection {
2021-07-01 14:34:08 +08:00
class SimplePrepareRendererResource
: public Cesium3DTilesSelection::IPrepareRendererResources {
2021-07-01 14:34:08 +08:00
public:
2022-07-30 05:37:35 +08:00
std::atomic<size_t> totalAllocation{};
2021-07-01 14:34:08 +08:00
2022-07-30 05:37:35 +08:00
struct AllocationResult {
2022-07-30 05:38:14 +08:00
AllocationResult(std::atomic<size_t>& allocCount_)
: allocCount{allocCount_} {
2022-07-30 05:37:35 +08:00
++allocCount;
}
~AllocationResult() noexcept { --allocCount; }
2021-07-01 14:34:08 +08:00
2022-07-30 05:37:35 +08:00
std::atomic<size_t>& allocCount;
};
2021-07-01 14:34:08 +08:00
2022-07-30 05:37:35 +08:00
~SimplePrepareRendererResource() noexcept { CHECK(totalAllocation == 0); }
2021-07-01 14:34:08 +08:00
virtual CesiumAsync::Future<TileLoadResultAndRenderResources>
prepareInLoadThread(
const CesiumAsync::AsyncSystem& asyncSystem,
TileLoadResult&& tileLoadResult,
2022-09-03 03:24:42 +08:00
const glm::dmat4& /*transform*/,
const std::any& /*rendererOptions*/) override {
prepareInLoadThreadTestCallback(tileLoadResult);
return asyncSystem.createResolvedFuture(TileLoadResultAndRenderResources{
std::move(tileLoadResult),
new AllocationResult{totalAllocation}});
2021-07-01 14:34:08 +08:00
}
virtual void* prepareInMainThread(
Cesium3DTilesSelection::Tile& /*tile*/,
2022-07-30 05:37:35 +08:00
void* pLoadThreadResult) override {
if (pLoadThreadResult) {
AllocationResult* loadThreadResult =
reinterpret_cast<AllocationResult*>(pLoadThreadResult);
delete loadThreadResult;
}
return new AllocationResult{totalAllocation};
2021-07-01 14:34:08 +08:00
}
virtual void free(
Cesium3DTilesSelection::Tile& /*tile*/,
2021-07-01 14:34:08 +08:00
void* pLoadThreadResult,
void* pMainThreadResult) noexcept override {
if (pMainThreadResult) {
2022-07-30 05:37:35 +08:00
AllocationResult* mainThreadResult =
reinterpret_cast<AllocationResult*>(pMainThreadResult);
2021-07-01 14:34:08 +08:00
delete mainThreadResult;
}
if (pLoadThreadResult) {
2022-07-30 05:37:35 +08:00
AllocationResult* loadThreadResult =
reinterpret_cast<AllocationResult*>(pLoadThreadResult);
2021-07-01 14:34:08 +08:00
delete loadThreadResult;
}
}
virtual void* prepareRasterInLoadThread(
2024-10-10 23:26:58 +08:00
CesiumGltf::ImageAsset& /*image*/,
const std::any& /*rendererOptions*/) override {
2022-07-30 05:37:35 +08:00
return new AllocationResult{totalAllocation};
2021-07-01 14:34:08 +08:00
}
virtual void* prepareRasterInMainThread(
2023-11-14 15:29:47 +08:00
CesiumRasterOverlays::RasterOverlayTile& /*rasterTile*/,
2022-07-30 05:37:35 +08:00
void* pLoadThreadResult) override {
if (pLoadThreadResult) {
AllocationResult* loadThreadResult =
reinterpret_cast<AllocationResult*>(pLoadThreadResult);
delete loadThreadResult;
}
return new AllocationResult{totalAllocation};
2021-07-01 14:34:08 +08:00
}
virtual void freeRaster(
2023-11-14 15:29:47 +08:00
const CesiumRasterOverlays::RasterOverlayTile& /*rasterTile*/,
2021-07-01 14:34:08 +08:00
void* pLoadThreadResult,
void* pMainThreadResult) noexcept override {
if (pMainThreadResult) {
2022-07-30 05:37:35 +08:00
AllocationResult* mainThreadResult =
reinterpret_cast<AllocationResult*>(pMainThreadResult);
2021-07-01 14:34:08 +08:00
delete mainThreadResult;
}
if (pLoadThreadResult) {
2022-07-30 05:37:35 +08:00
AllocationResult* loadThreadResult =
reinterpret_cast<AllocationResult*>(pLoadThreadResult);
2021-07-01 14:34:08 +08:00
delete loadThreadResult;
}
}
virtual void attachRasterInMainThread(
const Cesium3DTilesSelection::Tile& /*tile*/,
int32_t /*overlayTextureCoordinateID*/,
2023-11-14 15:29:47 +08:00
const CesiumRasterOverlays::RasterOverlayTile& /*rasterTile*/,
2021-07-01 14:34:08 +08:00
void* /*pMainThreadRendererResources*/,
const glm::dvec2& /*translation*/,
const glm::dvec2& /*scale*/) override {}
virtual void detachRasterInMainThread(
const Cesium3DTilesSelection::Tile& /*tile*/,
int32_t /*overlayTextureCoordinateID*/,
2023-11-14 15:29:47 +08:00
const CesiumRasterOverlays::RasterOverlayTile& /*rasterTile*/,
2021-08-26 12:47:25 +08:00
void* /*pMainThreadRendererResources*/) noexcept override {}
std::function<void(const TileLoadResult&)> prepareInLoadThreadTestCallback =
[](const TileLoadResult& /*result*/) {};
2021-07-01 14:34:08 +08:00
};
} // namespace Cesium3DTilesSelection