async tasks

This commit is contained in:
2025-12-31 16:12:43 +01:00
parent f3a69571a2
commit 3a410775b5
6 changed files with 111 additions and 12 deletions

View File

@@ -17,6 +17,8 @@
#include <memory>
#include <thread>
#include <vector>
#include <future>
#include <functional>
using namespace aidl::com::omixlab::mosis;
using namespace aidl::android::hardware;
@@ -28,6 +30,8 @@ class Renderer : public BnMosisListener
std::unique_ptr<Shader> m_quad_shader;
std::unique_ptr<Quad> m_quad;
AHardwareBuffer* m_hwbuffer = nullptr;
std::vector<std::function<void()>> m_task_queue;
std::mutex m_task_mutex;
std::thread m_render_loop;
bool m_active = false;
void render_loop(ANativeWindow* window)
@@ -39,14 +43,7 @@ class Renderer : public BnMosisListener
m_active = true;
while (m_active)
{
if (m_hwbuffer && !m_texture)
{
m_texture = std::make_unique<ExternalTexture>();
if (!m_texture->create(m_hwbuffer))
{
Logger::Log("Failed to create texture");
}
}
process_tasks();
render_frame();
}
}
@@ -62,9 +59,18 @@ class Renderer : public BnMosisListener
m_quad = std::make_unique<Quad>();
m_quad->create();
}
void process_tasks()
{
std::lock_guard _lock(m_task_mutex);
for (auto& task : m_task_queue)
{
task();
}
m_task_queue.clear();
}
void render_frame()
{
glClearColor(0.f, 0.5f, 0.5f, 1.0f);
glClearColor(0.5f, 0.5f, 0.5f, 1.0f);
glClear(GL_COLOR_BUFFER_BIT);
if (m_texture)
{
@@ -88,9 +94,37 @@ public:
m_active = false;
if (m_render_loop.joinable())
m_render_loop.join();
m_egl_context->destroy();
if (m_egl_context)
m_egl_context->destroy();
m_egl_context.reset();
}
template<typename T>
[[nodiscard]] std::future<void> exec(T&& lambda)
{
std::lock_guard _lock(m_task_mutex);
auto promise = std::make_shared<std::promise<void>>();
auto future = promise->get_future();
m_task_queue.emplace_back(
[l=std::forward<T>(lambda),p=std::move(promise)]
{
l();
p->set_value();
}
);
return future;
}
template<typename T>
void exec_async(T&& lambda)
{
std::lock_guard _lock(m_task_mutex);
m_task_queue.emplace_back(std::forward<T>(lambda));
}
void update_surface(ANativeWindow* window)
{
exec_async([this, window]{
m_egl_context->make_current(window);
});
}
ndk::ScopedAStatus onServiceInitialized(bool in_success) override
{
return ndk::ScopedAStatus::ok();
@@ -104,6 +138,13 @@ public:
{
m_hwbuffer = in_buffer.get();
AHardwareBuffer_acquire(m_hwbuffer);
exec_async([this]{
m_texture = std::make_unique<ExternalTexture>();
if (!m_texture->create(m_hwbuffer))
{
Logger::Log("Failed to create texture");
}
});
return ndk::ScopedAStatus::ok();
}
};
@@ -144,6 +185,10 @@ Java_com_omixlab_mosis_MainActivity_setSurface(JNIEnv *env, jobject thiz,
g_renderer = ndk::SharedRefBase::make<Renderer>();
g_renderer->create(ANativeWindow_fromSurface(env, surface));
}
else
{
g_renderer->update_surface(ANativeWindow_fromSurface(env, surface));
}
}
extern "C"
JNIEXPORT void JNICALL
@@ -153,6 +198,7 @@ Java_com_omixlab_mosis_MainActivity_destroySurface(JNIEnv *env, jobject thiz)
if (g_renderer)
{
g_renderer->destroy();
g_renderer.reset();
}
}