Skip to content

Commit

Permalink
Add cesium native initialization options to enable / disable caching …
Browse files Browse the repository at this point in the history
…and the cache path
  • Loading branch information
LukasPoque committed Nov 21, 2024
1 parent 6cd548a commit a7b769a
Show file tree
Hide file tree
Showing 6 changed files with 62 additions and 21 deletions.
1 change: 1 addition & 0 deletions lib/cesium_3d_tiles.dart
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,4 @@ library;
export 'src/cesium_3d_tiles/cesium_3d_tiles.dart';
export 'src/cesium_native/cesium_native.dart';
export 'src/cesium_ion/cesium_ion.dart';
export 'src/cesium_native/src/cesium_native_options.dart';
25 changes: 24 additions & 1 deletion lib/src/cesium_native/src/cesium_native.dart
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import 'package:logging/logging.dart';
import 'package:vector_math/vector_math_64.dart';

import 'cesium_tile_selection_state.dart';
import 'cesium_native_options.dart';

class CesiumTileset {
final Pointer<g.CesiumTileset> _ptr;
Expand Down Expand Up @@ -66,10 +67,32 @@ class CesiumNative {
}

CesiumNative._() {
g.CesiumTileset_initialize(16);
_errorMessage = calloc<Char>(256);
}

static bool _initialized = false;

static void initialize([CesiumNativeOptions? options]) {
if (_initialized) return;

final opts = options ?? const CesiumNativeOptions();

final cachePathPtr =
opts.cacheDbPath != null && opts.cacheDbPath!.isNotEmpty
? opts.cacheDbPath!.toNativeUtf8().cast<Char>()
: nullptr;

try {
g.CesiumTileset_initialize(opts.numThreads, cachePathPtr);
_initialized = true;
_errorMessage = calloc<Char>(256);
} finally {
if (cachePathPtr != nullptr) {
calloc.free(cachePathPtr.cast<Utf8>());
}
}
}

///
/// Load a CesiumTileset from a CesiumIonAsset with the specified token.
///
Expand Down
3 changes: 2 additions & 1 deletion lib/src/cesium_native/src/cesium_native.g.dart
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,10 @@ library;

import 'dart:ffi' as ffi;

@ffi.Native<ffi.Void Function(ffi.Uint32)>()
@ffi.Native<ffi.Void Function(ffi.Uint32, ffi.Pointer<ffi.Char>)>()
external void CesiumTileset_initialize(
int numThreads,
ffi.Pointer<ffi.Char> cacheDbPath,
);

@ffi.Native<ffi.Void Function()>()
Expand Down
12 changes: 12 additions & 0 deletions lib/src/cesium_native/src/cesium_native_options.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
class CesiumNativeOptions {
/// The path where the SQLite cache database will be stored
final String? cacheDbPath;

/// Number of threads to use for processing tasks
final int numThreads;

const CesiumNativeOptions({
this.cacheDbPath,
this.numThreads = 16,
});
}
2 changes: 1 addition & 1 deletion native/include/CesiumTilesetCApi.h
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,7 @@ typedef struct SerializedCesiumGltfModel SerializedCesiumGltfModel;
// Initializes all bindings. Must be called before any other CesiumTileset_ function.
// numThreads refers to the number of threads that will be created for the Async system (job queue).
//
API_EXPORT void CesiumTileset_initialize(uint32_t numThreads);
API_EXPORT void CesiumTileset_initialize(uint32_t numThreads, const char* cacheDbPath);

API_EXPORT void CesiumTileset_pumpAsyncQueue();

Expand Down
40 changes: 22 additions & 18 deletions native/src/CesiumTilesetCApi.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ static std::shared_ptr<CesiumAsync::IAssetAccessor> pAssetAccessor;
static std::shared_ptr<CesiumAsync::ICacheDatabase> pCacheDatabase;
static std::shared_ptr<CesiumUtility::CreditSystem> pMockedCreditSystem;
static std::thread *main;
API_EXPORT void CesiumTileset_initialize(uint32_t numThreads) {
API_EXPORT void CesiumTileset_initialize(uint32_t numThreads, const char* cacheDbPath) {
if(pResourcePreparer) {
return;
}
Expand All @@ -151,25 +151,29 @@ API_EXPORT void CesiumTileset_initialize(uint32_t numThreads) {
spdlog::enable_backtrace(32); // Keep a backtrace of 32 messages

pAssetAccessor = std::make_shared<CurlAssetAccessor>();
//TODO: make this a user-configurable path
std::string cacheDbPath = "cesium_tile_cache.db";
pCacheDatabase = std::make_shared<CesiumAsync::SqliteCache>(
spdlog::default_logger(),
cacheDbPath,
4096
);
if (pCacheDatabase) {
spdlog::default_logger()->info("SQLite cache created successfully");

// Only create caching if a valid path is provided
if (cacheDbPath && strlen(cacheDbPath) > 0) {
pCacheDatabase = std::make_shared<CesiumAsync::SqliteCache>(
spdlog::default_logger(),
cacheDbPath,
4096
);
if (pCacheDatabase) {
spdlog::default_logger()->info("SQLite cache created successfully at: {}", cacheDbPath);
pAssetAccessor = std::make_shared<CesiumAsync::CachingAssetAccessor>(
spdlog::default_logger(),
pAssetAccessor,
pCacheDatabase,
10000
);
spdlog::default_logger()->info("CachingAssetAccessor created with database: {}", cacheDbPath);
} else {
spdlog::default_logger()->error("Failed to create SQLite cache at: {}", cacheDbPath);
}
} else {
spdlog::default_logger()->error("Failed to create SQLite cache");
spdlog::default_logger()->info("No cache path provided, running without tile caching");
}
pAssetAccessor = std::make_shared<CesiumAsync::CachingAssetAccessor>(
spdlog::default_logger(),
pAssetAccessor,
pCacheDatabase,
10000
);
spdlog::default_logger()->info("CachingAssetAccessor created with database: {}", cacheDbPath);

asyncSystem = CesiumAsync::AsyncSystem { std::make_shared<SimpleTaskProcessor>(numThreads) };

Expand Down

0 comments on commit a7b769a

Please sign in to comment.