mirror of
https://github.com/hyprwm/hyprgraphics.git
synced 2025-12-20 07:30:03 +01:00
resource/AsyncResource: add await() for resources
Some checks failed
Build & Test (Arch) / Arch: Build and Test (gcc) (push) Has been cancelled
Build & Test (Arch) / Arch: Build and Test (clang) (push) Has been cancelled
Build & Test / nix (hyprgraphics) (push) Has been cancelled
Build & Test / nix (hyprgraphics-with-tests) (push) Has been cancelled
Some checks failed
Build & Test (Arch) / Arch: Build and Test (gcc) (push) Has been cancelled
Build & Test (Arch) / Arch: Build and Test (clang) (push) Has been cancelled
Build & Test / nix (hyprgraphics) (push) Has been cancelled
Build & Test / nix (hyprgraphics-with-tests) (push) Has been cancelled
This commit is contained in:
parent
ffc999d980
commit
1fb5bfbd62
5 changed files with 39 additions and 1 deletions
|
|
@ -17,6 +17,9 @@ namespace Hyprgraphics {
|
||||||
|
|
||||||
void enqueue(Hyprutils::Memory::CAtomicSharedPointer<IAsyncResource> resource);
|
void enqueue(Hyprutils::Memory::CAtomicSharedPointer<IAsyncResource> resource);
|
||||||
|
|
||||||
|
// Synchronously await the resource being available
|
||||||
|
void await(Hyprutils::Memory::CAtomicSharedPointer<IAsyncResource> resource);
|
||||||
|
|
||||||
private:
|
private:
|
||||||
std::thread m_gatherThread;
|
std::thread m_gatherThread;
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -7,9 +7,11 @@
|
||||||
#include <atomic>
|
#include <atomic>
|
||||||
|
|
||||||
namespace Hyprgraphics {
|
namespace Hyprgraphics {
|
||||||
|
struct SAsyncResourceImpl;
|
||||||
|
|
||||||
class IAsyncResource {
|
class IAsyncResource {
|
||||||
public:
|
public:
|
||||||
IAsyncResource() = default;
|
IAsyncResource();
|
||||||
virtual ~IAsyncResource() = default;
|
virtual ~IAsyncResource() = default;
|
||||||
|
|
||||||
virtual void render() = 0;
|
virtual void render() = 0;
|
||||||
|
|
@ -29,5 +31,7 @@ namespace Hyprgraphics {
|
||||||
Hyprutils::Memory::CSharedPointer<CCairoSurface> cairoSurface;
|
Hyprutils::Memory::CSharedPointer<CCairoSurface> cairoSurface;
|
||||||
Hyprutils::Math::Vector2D pixelSize;
|
Hyprutils::Math::Vector2D pixelSize;
|
||||||
} m_asset;
|
} m_asset;
|
||||||
|
|
||||||
|
Hyprutils::Memory::CUniquePointer<SAsyncResourceImpl> m_impl;
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -1,4 +1,5 @@
|
||||||
#include <hyprgraphics/resource/AsyncResourceGatherer.hpp>
|
#include <hyprgraphics/resource/AsyncResourceGatherer.hpp>
|
||||||
|
#include "resources/AsyncResource.hpp"
|
||||||
|
|
||||||
using namespace Hyprgraphics;
|
using namespace Hyprgraphics;
|
||||||
|
|
||||||
|
|
@ -28,6 +29,13 @@ void CAsyncResourceGatherer::enqueue(Hyprutils::Memory::CAtomicSharedPointer<IAs
|
||||||
wakeUpMainThread();
|
wakeUpMainThread();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void CAsyncResourceGatherer::await(Hyprutils::Memory::CAtomicSharedPointer<IAsyncResource> resource) {
|
||||||
|
resource->m_impl->awaitingCv = Hyprutils::Memory::makeUnique<std::condition_variable>();
|
||||||
|
std::unique_lock<std::mutex> lk(resource->m_impl->awaitingMtx);
|
||||||
|
resource->m_impl->awaitingCv->wait(lk, [&resource] { return resource->m_impl->awaitingEvent; });
|
||||||
|
resource->m_impl->awaitingCv.reset();
|
||||||
|
}
|
||||||
|
|
||||||
void CAsyncResourceGatherer::asyncAssetSpinLock() {
|
void CAsyncResourceGatherer::asyncAssetSpinLock() {
|
||||||
while (!m_asyncLoopState.exit) {
|
while (!m_asyncLoopState.exit) {
|
||||||
|
|
||||||
|
|
@ -56,6 +64,10 @@ void CAsyncResourceGatherer::asyncAssetSpinLock() {
|
||||||
for (auto& r : requests) {
|
for (auto& r : requests) {
|
||||||
r->render();
|
r->render();
|
||||||
|
|
||||||
|
if (r->m_impl->awaitingCv) {
|
||||||
|
r->m_impl->awaitingEvent = true;
|
||||||
|
r->m_impl->awaitingCv->notify_all();
|
||||||
|
}
|
||||||
r->m_ready = true;
|
r->m_ready = true;
|
||||||
r->m_events.finished.emit();
|
r->m_events.finished.emit();
|
||||||
}
|
}
|
||||||
|
|
|
||||||
8
src/resource/resources/AsyncResource.cpp
Normal file
8
src/resource/resources/AsyncResource.cpp
Normal file
|
|
@ -0,0 +1,8 @@
|
||||||
|
#include "AsyncResource.hpp"
|
||||||
|
|
||||||
|
using namespace Hyprgraphics;
|
||||||
|
using namespace Hyprutils::Memory;
|
||||||
|
|
||||||
|
IAsyncResource::IAsyncResource() : m_impl(makeUnique<SAsyncResourceImpl>()) {
|
||||||
|
;
|
||||||
|
}
|
||||||
11
src/resource/resources/AsyncResource.hpp
Normal file
11
src/resource/resources/AsyncResource.hpp
Normal file
|
|
@ -0,0 +1,11 @@
|
||||||
|
#include <hyprgraphics/resource/resources/AsyncResource.hpp>
|
||||||
|
|
||||||
|
#include <condition_variable>
|
||||||
|
|
||||||
|
namespace Hyprgraphics {
|
||||||
|
struct SAsyncResourceImpl {
|
||||||
|
Hyprutils::Memory::CUniquePointer<std::condition_variable> awaitingCv;
|
||||||
|
std::mutex awaitingMtx;
|
||||||
|
bool awaitingEvent = false;
|
||||||
|
};
|
||||||
|
}
|
||||||
Loading…
Add table
Reference in a new issue