thread pool update + refactor
This commit is contained in:
+56
-17
@@ -6,6 +6,8 @@
|
||||
#include <iostream>
|
||||
#include <memory>
|
||||
|
||||
#include "../util/ThreadPool.h"
|
||||
|
||||
#include "../constants.h"
|
||||
#include "../data/dynamic.h"
|
||||
#include "../debug/Logger.h"
|
||||
@@ -21,37 +23,42 @@ static debug::Logger logger("assets-loader");
|
||||
AssetsLoader::AssetsLoader(Assets* assets, const ResPaths* paths)
|
||||
: assets(assets), paths(paths)
|
||||
{
|
||||
addLoader(AssetType::shader, assetload::shader);
|
||||
addLoader(AssetType::texture, assetload::texture);
|
||||
addLoader(AssetType::font, assetload::font);
|
||||
addLoader(AssetType::atlas, assetload::atlas);
|
||||
addLoader(AssetType::shader, assetload::shader);
|
||||
addLoader(AssetType::texture, assetload::texture);
|
||||
addLoader(AssetType::font, assetload::font);
|
||||
addLoader(AssetType::atlas, assetload::atlas);
|
||||
addLoader(AssetType::layout, assetload::layout);
|
||||
addLoader(AssetType::sound, assetload::sound);
|
||||
}
|
||||
|
||||
void AssetsLoader::addLoader(AssetType tag, aloader_func func) {
|
||||
loaders[tag] = func;
|
||||
loaders[tag] = func;
|
||||
}
|
||||
|
||||
void AssetsLoader::add(AssetType tag, const std::string filename, const std::string alias, std::shared_ptr<AssetCfg> settings) {
|
||||
entries.push(aloader_entry{tag, filename, alias, settings});
|
||||
entries.push(aloader_entry{tag, filename, alias, settings});
|
||||
}
|
||||
|
||||
bool AssetsLoader::hasNext() const {
|
||||
return !entries.empty();
|
||||
return !entries.empty();
|
||||
}
|
||||
|
||||
aloader_func AssetsLoader::getLoader(AssetType tag) {
|
||||
auto found = loaders.find(tag);
|
||||
if (found == loaders.end()) {
|
||||
throw std::runtime_error(
|
||||
"unknown asset tag "+std::to_string(static_cast<int>(tag))
|
||||
);
|
||||
}
|
||||
return found->second;
|
||||
}
|
||||
|
||||
bool AssetsLoader::loadNext() {
|
||||
const aloader_entry& entry = entries.front();
|
||||
logger.info() << "loading " << entry.filename << " as " << entry.alias;
|
||||
auto found = loaders.find(entry.tag);
|
||||
if (found == loaders.end()) {
|
||||
logger.error() << "unknown asset tag " << static_cast<int>(entry.tag);
|
||||
return false;
|
||||
}
|
||||
aloader_func loader = found->second;
|
||||
const aloader_entry& entry = entries.front();
|
||||
logger.info() << "loading " << entry.filename << " as " << entry.alias;
|
||||
try {
|
||||
auto postfunc = loader(*this, assets, paths, entry.filename, entry.alias, entry.config);
|
||||
aloader_func loader = getLoader(entry.tag);
|
||||
auto postfunc = loader(this, paths, entry.filename, entry.alias, entry.config);
|
||||
postfunc(assets);
|
||||
entries.pop();
|
||||
return true;
|
||||
@@ -198,5 +205,37 @@ void AssetsLoader::addDefaults(AssetsLoader& loader, const Content* content) {
|
||||
}
|
||||
|
||||
const ResPaths* AssetsLoader::getPaths() const {
|
||||
return paths;
|
||||
return paths;
|
||||
}
|
||||
|
||||
class LoaderWorker : public util::Worker<std::shared_ptr<aloader_entry>, assetload::postfunc> {
|
||||
AssetsLoader* loader;
|
||||
public:
|
||||
LoaderWorker(AssetsLoader* loader) : loader(loader) {
|
||||
}
|
||||
|
||||
assetload::postfunc operator()(const std::shared_ptr<aloader_entry>& entry) override {
|
||||
aloader_func loadfunc = loader->getLoader(entry->tag);
|
||||
return loadfunc(loader, loader->getPaths(), entry->filename, entry->alias, entry->config);
|
||||
}
|
||||
};
|
||||
|
||||
std::shared_ptr<Task> AssetsLoader::startTask(runnable onDone) {
|
||||
auto pool = std::make_shared<
|
||||
util::ThreadPool<std::shared_ptr<aloader_entry>, assetload::postfunc>
|
||||
>(
|
||||
"assets-loader-pool",
|
||||
[=](){return std::make_shared<LoaderWorker>(this);},
|
||||
[=](assetload::postfunc& func) {
|
||||
func(assets);
|
||||
}
|
||||
);
|
||||
pool->setOnComplete(onDone);
|
||||
while (!entries.empty()) {
|
||||
const aloader_entry& entry = entries.front();
|
||||
auto ptr = std::make_shared<aloader_entry>(entry);
|
||||
pool->enqueueJob(ptr);
|
||||
entries.pop();
|
||||
}
|
||||
return pool;
|
||||
}
|
||||
|
||||
@@ -2,6 +2,8 @@
|
||||
#define ASSETS_ASSETS_LOADER_H
|
||||
|
||||
#include "Assets.h"
|
||||
#include "../interfaces/Task.h"
|
||||
#include "../delegates.h"
|
||||
|
||||
#include <string>
|
||||
#include <memory>
|
||||
@@ -45,8 +47,7 @@ struct SoundCfg : AssetCfg {
|
||||
};
|
||||
|
||||
using aloader_func = std::function<assetload::postfunc(
|
||||
AssetsLoader&,
|
||||
Assets*,
|
||||
AssetsLoader*,
|
||||
const ResPaths*,
|
||||
const std::string&,
|
||||
const std::string&,
|
||||
@@ -96,7 +97,10 @@ public:
|
||||
/// @param content engine content
|
||||
static void addDefaults(AssetsLoader& loader, const Content* content);
|
||||
|
||||
std::shared_ptr<Task> startTask(runnable onDone);
|
||||
|
||||
const ResPaths* getPaths() const;
|
||||
aloader_func getLoader(AssetType tag);
|
||||
};
|
||||
|
||||
#endif // ASSETS_ASSETS_LOADER_H
|
||||
|
||||
@@ -8,7 +8,7 @@
|
||||
#include "../audio/audio.h"
|
||||
#include "../files/files.h"
|
||||
#include "../files/engine_paths.h"
|
||||
#include "../coders/png.h"
|
||||
#include "../coders/imageio.h"
|
||||
#include "../coders/json.h"
|
||||
#include "../coders/GLSLExtension.h"
|
||||
#include "../graphics/core/Shader.h"
|
||||
@@ -30,15 +30,14 @@ static bool animation(
|
||||
);
|
||||
|
||||
assetload::postfunc assetload::texture(
|
||||
AssetsLoader&,
|
||||
Assets* assets,
|
||||
AssetsLoader*,
|
||||
const ResPaths* paths,
|
||||
const std::string filename,
|
||||
const std::string name,
|
||||
std::shared_ptr<AssetCfg>
|
||||
) {
|
||||
std::shared_ptr<ImageData> image (
|
||||
png::load_image(paths->find(filename+".png").u8string())
|
||||
imageio::read(paths->find(filename+".png").u8string()).release()
|
||||
);
|
||||
return [name, image](auto assets) {
|
||||
assets->store(Texture::from(image.get()), name);
|
||||
@@ -46,8 +45,7 @@ assetload::postfunc assetload::texture(
|
||||
}
|
||||
|
||||
assetload::postfunc assetload::shader(
|
||||
AssetsLoader&,
|
||||
Assets* assets,
|
||||
AssetsLoader*,
|
||||
const ResPaths* paths,
|
||||
const std::string filename,
|
||||
const std::string name,
|
||||
@@ -72,15 +70,12 @@ assetload::postfunc assetload::shader(
|
||||
}
|
||||
|
||||
static bool appendAtlas(AtlasBuilder& atlas, const fs::path& file) {
|
||||
// png is only supported format
|
||||
if (file.extension() != ".png")
|
||||
return false;
|
||||
std::string name = file.stem().string();
|
||||
// skip duplicates
|
||||
if (atlas.has(name)) {
|
||||
return false;
|
||||
}
|
||||
std::unique_ptr<ImageData> image(png::load_image(file.string()));
|
||||
auto image = imageio::read(file.string());
|
||||
image->fixAlphaColor();
|
||||
atlas.add(name, image.release());
|
||||
|
||||
@@ -88,8 +83,7 @@ static bool appendAtlas(AtlasBuilder& atlas, const fs::path& file) {
|
||||
}
|
||||
|
||||
assetload::postfunc assetload::atlas(
|
||||
AssetsLoader&,
|
||||
Assets* assets,
|
||||
AssetsLoader*,
|
||||
const ResPaths* paths,
|
||||
const std::string directory,
|
||||
const std::string name,
|
||||
@@ -114,8 +108,7 @@ assetload::postfunc assetload::atlas(
|
||||
}
|
||||
|
||||
assetload::postfunc assetload::font(
|
||||
AssetsLoader&,
|
||||
Assets* assets,
|
||||
AssetsLoader*,
|
||||
const ResPaths* paths,
|
||||
const std::string filename,
|
||||
const std::string name,
|
||||
@@ -125,8 +118,7 @@ assetload::postfunc assetload::font(
|
||||
for (size_t i = 0; i <= 4; i++) {
|
||||
std::string name = filename + "_" + std::to_string(i) + ".png";
|
||||
name = paths->find(name).string();
|
||||
std::unique_ptr<ImageData> image (png::load_image(name));
|
||||
pages->push_back(std::move(image));
|
||||
pages->push_back(std::move(imageio::read(name)));
|
||||
}
|
||||
return [=](auto assets) {
|
||||
int res = pages->at(0)->getHeight() / 16;
|
||||
@@ -139,8 +131,7 @@ assetload::postfunc assetload::font(
|
||||
}
|
||||
|
||||
assetload::postfunc assetload::layout(
|
||||
AssetsLoader& loader,
|
||||
Assets* assets,
|
||||
AssetsLoader*,
|
||||
const ResPaths* paths,
|
||||
const std::string file,
|
||||
const std::string name,
|
||||
@@ -159,8 +150,7 @@ assetload::postfunc assetload::layout(
|
||||
};
|
||||
}
|
||||
assetload::postfunc assetload::sound(
|
||||
AssetsLoader& loader,
|
||||
Assets* assets,
|
||||
AssetsLoader*,
|
||||
const ResPaths* paths,
|
||||
const std::string file,
|
||||
const std::string name,
|
||||
|
||||
@@ -15,40 +15,35 @@ struct AssetCfg;
|
||||
/// @brief see AssetsLoader.h: aloader_func
|
||||
namespace assetload {
|
||||
postfunc texture(
|
||||
AssetsLoader&,
|
||||
Assets*,
|
||||
AssetsLoader*,
|
||||
const ResPaths* paths,
|
||||
const std::string filename,
|
||||
const std::string name,
|
||||
std::shared_ptr<AssetCfg> settings
|
||||
);
|
||||
postfunc shader(
|
||||
AssetsLoader&,
|
||||
Assets*,
|
||||
AssetsLoader*,
|
||||
const ResPaths* paths,
|
||||
const std::string filename,
|
||||
const std::string name,
|
||||
std::shared_ptr<AssetCfg> settings
|
||||
);
|
||||
postfunc atlas(
|
||||
AssetsLoader&,
|
||||
Assets*,
|
||||
AssetsLoader*,
|
||||
const ResPaths* paths,
|
||||
const std::string directory,
|
||||
const std::string name,
|
||||
std::shared_ptr<AssetCfg> settings
|
||||
);
|
||||
postfunc font(
|
||||
AssetsLoader&,
|
||||
Assets*,
|
||||
AssetsLoader*,
|
||||
const ResPaths* paths,
|
||||
const std::string filename,
|
||||
const std::string name,
|
||||
std::shared_ptr<AssetCfg> settings
|
||||
);
|
||||
postfunc layout(
|
||||
AssetsLoader&,
|
||||
Assets*,
|
||||
AssetsLoader*,
|
||||
const ResPaths* paths,
|
||||
const std::string file,
|
||||
const std::string name,
|
||||
@@ -56,8 +51,7 @@ namespace assetload {
|
||||
);
|
||||
|
||||
postfunc sound(
|
||||
AssetsLoader&,
|
||||
Assets*,
|
||||
AssetsLoader*,
|
||||
const ResPaths* paths,
|
||||
const std::string file,
|
||||
const std::string name,
|
||||
|
||||
Reference in New Issue
Block a user