BLEN-365: Improve creation algorithm of PreviewEngine #21

Merged
Bogdan Nagirniak merged 11 commits from BLEN-365 into hydra-render 2023-04-10 12:44:48 +02:00
3 changed files with 46 additions and 45 deletions
Showing only changes of commit 44cce40974 - Show all commits

View File

@ -9,32 +9,47 @@
namespace blender::render::hydra { namespace blender::render::hydra {
std::unique_ptr<PreviewEngine> PreviewEngine::preview_engine; const double LIFETIME = 180.0;
void PreviewEngine::free() std::unique_ptr<PreviewEngine> PreviewEngine::instance;
void PreviewEngine::schedule_free()
{ {
if (preview_engine) { instance->render_delegate->Stop();
BLI_timer_register((uintptr_t)preview_engine.get(),
preview_engine->delete_preview_engine, /* Register timer for schedule free PreviewEngine instance */
&preview_engine, BLI_timer_register((uintptr_t)instance.get(),
nullptr, free_instance,
preview_engine->preview_engine_lifetime, nullptr,
true); nullptr,
} LIFETIME,
true);
} }
std::unique_ptr<PreviewEngine> *PreviewEngine::get(RenderEngine *bl_engine, PreviewEngine *PreviewEngine::get_instance(RenderEngine *bl_engine, const std::string &render_delegate_id)
char *render_delegate_id)
{ {
if (!preview_engine) { if (!instance) {
preview_engine = std::make_unique<PreviewEngine>(bl_engine, render_delegate_id); instance = std::make_unique<PreviewEngine>(bl_engine, render_delegate_id);
} }
if (BLI_timer_is_registered((uintptr_t)preview_engine.get())) { if (BLI_timer_is_registered((uintptr_t)instance.get())) {
BLI_timer_unregister((uintptr_t)preview_engine.get()); /* Unregister timer while PreviewEngine is working */
BLI_timer_unregister((uintptr_t)instance.get());
} }
preview_engine->update_bl_engine(bl_engine); instance->update_bl_engine(bl_engine);
return &preview_engine; return instance.get();
}
double PreviewEngine::free_instance(uintptr_t uuid, void *user_data)
{
if (!instance->render_task_delegate->is_converged()) {
/* Restart timer if render isn't completed */
return LIFETIME;
}
CLOG_INFO(LOG_EN, 2, "");
instance = nullptr;
return -1;
} }
void PreviewEngine::sync(Depsgraph *depsgraph, void PreviewEngine::sync(Depsgraph *depsgraph,
@ -107,18 +122,6 @@ void PreviewEngine::update_bl_engine(RenderEngine *bl_engine)
this->bl_engine = bl_engine; this->bl_engine = bl_engine;
} }
double PreviewEngine::delete_preview_engine(uintptr_t uuid, void *user_data)
{
std::unique_ptr<PreviewEngine> *preview_engine = static_cast<std::unique_ptr<PreviewEngine>*>(user_data);
if (preview_engine && preview_engine->get()->render_task_delegate->is_converged()) {
CLOG_INFO(LOG_EN, uuid, "Delete preview engine");
preview_engine->reset();
return -1;
}
return preview_engine_lifetime;
}
void PreviewEngine::update_render_result(const std::string &layer_name, void PreviewEngine::update_render_result(const std::string &layer_name,
int width, int width,
int height, int height,

View File

@ -8,17 +8,21 @@
namespace blender::render::hydra { namespace blender::render::hydra {
class PreviewEngine : public FinalEngine { class PreviewEngine : public FinalEngine {
private:
/* Singleton class instance */
static std::unique_ptr<PreviewEngine> instance;
static double free_instance(uintptr_t uuid, void *user_data);
public: public:
static PreviewEngine *get_instance(RenderEngine *bl_engine, const std::string &render_delegate_id);
static void schedule_free();
using FinalEngine::FinalEngine; using FinalEngine::FinalEngine;
static void free();
static std::unique_ptr<PreviewEngine> *get(RenderEngine *bl_engine = nullptr,
char *render_delegate_id = nullptr);
void sync(Depsgraph *depsgraph, void sync(Depsgraph *depsgraph,
bContext *context, bContext *context,
pxr::HdRenderSettingsMap &render_settings) override; pxr::HdRenderSettingsMap &render_settings) override;
void render(Depsgraph *depsgraph) override; void render(Depsgraph *depsgraph) override;
void update_bl_engine(RenderEngine *bl_engine); void update_bl_engine(RenderEngine *bl_engine);
static double delete_preview_engine(uintptr_t uuid, void *user_data);
protected: protected:
void update_render_result(const std::string &layer_name, void update_render_result(const std::string &layer_name,
@ -26,12 +30,6 @@ class PreviewEngine : public FinalEngine {
int height, int height,
std::vector<float> &pixels); std::vector<float> &pixels);
public:
static inline double preview_engine_lifetime = 180.0;
protected:
static std::unique_ptr<PreviewEngine> preview_engine;
pxr::HdRenderSettingsMap render_settings;
}; };
} // namespace blender::render::hydra } // namespace blender::render::hydra

View File

@ -114,7 +114,7 @@ static PyObject *engine_create_func(PyObject * /*self*/, PyObject *args)
engine = new ViewportEngine(bl_engine, render_delegate_id); engine = new ViewportEngine(bl_engine, render_delegate_id);
} }
else if (STREQ(engine_type, "PREVIEW")) { else if (STREQ(engine_type, "PREVIEW")) {
engine = PreviewEngine::get(bl_engine, render_delegate_id)->get(); engine = PreviewEngine::get_instance(bl_engine, render_delegate_id);
} }
else { else {
if (bl_engine->type->flag & RE_USE_GPU_CONTEXT) { if (bl_engine->type->flag & RE_USE_GPU_CONTEXT) {
@ -140,11 +140,11 @@ static PyObject *engine_free_func(PyObject * /*self*/, PyObject *args)
Engine *engine = (Engine *)PyLong_AsVoidPtr(pyengine); Engine *engine = (Engine *)PyLong_AsVoidPtr(pyengine);
PreviewEngine *preview_engine = dynamic_cast<PreviewEngine *>(engine); PreviewEngine *preview_engine = dynamic_cast<PreviewEngine *>(engine);
if (preview_engine) { if (preview_engine) {
preview_engine->free(); PreviewEngine::schedule_free();
Py_RETURN_NONE; }
else {
delete engine;
} }
delete engine;
CLOG_INFO(LOG_EN, 2, "Engine %016llx", engine); CLOG_INFO(LOG_EN, 2, "Engine %016llx", engine);
Py_RETURN_NONE; Py_RETURN_NONE;