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 {
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()
{
DagerD marked this conversation as resolved Outdated

Move this to clear()

Move this to clear()
if (preview_engine) {
BLI_timer_register((uintptr_t)preview_engine.get(),
preview_engine->delete_preview_engine,
&preview_engine,
nullptr,
preview_engine->preview_engine_lifetime,
true);
}
instance->render_delegate->Stop();
/* Register timer for schedule free PreviewEngine instance */
BLI_timer_register((uintptr_t)instance.get(),
free_instance,
nullptr,
nullptr,
LIFETIME,
true);
}
std::unique_ptr<PreviewEngine> *PreviewEngine::get(RenderEngine *bl_engine,
char *render_delegate_id)
PreviewEngine *PreviewEngine::get_instance(RenderEngine *bl_engine, const std::string &render_delegate_id)
{
if (!preview_engine) {
preview_engine = std::make_unique<PreviewEngine>(bl_engine, render_delegate_id);
if (!instance) {
instance = std::make_unique<PreviewEngine>(bl_engine, render_delegate_id);
}
if (BLI_timer_is_registered((uintptr_t)preview_engine.get())) {
BLI_timer_unregister((uintptr_t)preview_engine.get());
if (BLI_timer_is_registered((uintptr_t)instance.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,
@ -107,18 +122,6 @@ void PreviewEngine::update_bl_engine(RenderEngine *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,
int width,
int height,

View File

@ -8,17 +8,21 @@
namespace blender::render::hydra {
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:
static PreviewEngine *get_instance(RenderEngine *bl_engine, const std::string &render_delegate_id);
static void schedule_free();
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,
bContext *context,
pxr::HdRenderSettingsMap &render_settings) override;
void render(Depsgraph *depsgraph) override;
void update_bl_engine(RenderEngine *bl_engine);
static double delete_preview_engine(uintptr_t uuid, void *user_data);
protected:
void update_render_result(const std::string &layer_name,
@ -26,12 +30,6 @@ class PreviewEngine : public FinalEngine {
int height,
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

View File

@ -114,7 +114,7 @@ static PyObject *engine_create_func(PyObject * /*self*/, PyObject *args)
engine = new ViewportEngine(bl_engine, render_delegate_id);
}
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 {
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);
PreviewEngine *preview_engine = dynamic_cast<PreviewEngine *>(engine);
if (preview_engine) {
preview_engine->free();
Py_RETURN_NONE;
PreviewEngine::schedule_free();
}
else {
delete engine;
}
delete engine;
CLOG_INFO(LOG_EN, 2, "Engine %016llx", engine);
Py_RETURN_NONE;