hyprlock/src/renderer/AsyncResourceGatherer.hpp

91 lines
2.4 KiB
C++
Raw Normal View History

2024-02-19 00:08:03 +01:00
#pragma once
#include "Shader.hpp"
#include "../helpers/Box.hpp"
#include "../helpers/Color.hpp"
2024-02-21 22:38:37 +01:00
#include "DMAFrame.hpp"
2024-02-19 00:08:03 +01:00
#include "Texture.hpp"
#include <thread>
#include <atomic>
#include <vector>
#include <unordered_map>
2024-02-19 17:26:08 +01:00
#include <condition_variable>
#include <any>
2024-02-21 22:38:37 +01:00
#include "Shared.hpp"
2024-02-19 00:08:03 +01:00
class CAsyncResourceGatherer {
public:
CAsyncResourceGatherer();
std::atomic<bool> ready = false;
std::atomic<bool> applied = false;
std::atomic<float> progress = 0;
2024-02-19 17:26:08 +01:00
/* only call from ogl thread */
SPreloadedAsset* getAssetByID(const std::string& id);
2024-02-19 00:08:03 +01:00
2024-02-19 17:26:08 +01:00
void apply();
2024-02-19 00:08:03 +01:00
enum eTargetType {
TARGET_IMAGE = 0,
2024-02-19 17:26:08 +01:00
TARGET_TEXT
2024-02-19 00:08:03 +01:00
};
2024-02-19 17:26:08 +01:00
struct SPreloadRequest {
eTargetType type;
std::string asset;
std::string id;
std::unordered_map<std::string, std::any> props;
// optional. Callbacks will be dispatched from the main thread,
// so wayland/gl calls are OK.
// will fire once the resource is fully loaded and ready.
void (*callback)(void*) = nullptr;
void* callbackData = nullptr;
2024-02-19 17:26:08 +01:00
};
void requestAsyncAssetPreload(const SPreloadRequest& request);
2024-02-20 01:11:19 +01:00
void unloadAsset(SPreloadedAsset* asset);
2024-02-20 02:30:47 +01:00
void notify();
2024-02-21 22:38:37 +01:00
void await();
void recheckDMAFramesFor(COutput* output);
2024-02-19 17:26:08 +01:00
private:
std::thread asyncLoopThread;
void asyncAssetSpinLock();
void renderText(const SPreloadRequest& rq);
struct {
std::condition_variable loopGuard;
std::mutex loopMutex;
std::mutex requestMutex;
2024-02-20 01:11:19 +01:00
std::mutex assetsMutex;
2024-02-19 17:26:08 +01:00
std::vector<SPreloadRequest> requests;
bool pending = false;
bool busy = false;
} asyncLoopState;
2024-02-19 00:08:03 +01:00
struct SPreloadTarget {
eTargetType type = TARGET_IMAGE;
std::string id = "";
void* data;
void* cairo;
void* cairosurface;
Vector2D size;
};
2024-02-21 22:38:37 +01:00
std::vector<std::unique_ptr<CDMAFrame>> dmas;
2024-02-19 00:08:03 +01:00
std::vector<SPreloadTarget> preloadTargets;
std::unordered_map<std::string, SPreloadedAsset> assets;
void gather();
};