diff options
author | Sybren A. Stüvel <sybren@blender.org> | 2022-06-02 12:20:17 +0300 |
---|---|---|
committer | Sybren A. Stüvel <sybren@blender.org> | 2022-06-02 12:20:17 +0300 |
commit | f4456a4d3c9767da76041bfa48e623cf6afa59ce (patch) | |
tree | 6d489e1244cd6024ac3f9e53d850548e73e51824 /source/blender/windowmanager/intern/wm_jobs.c | |
parent | 40ecf9d606358d773bfe977e6bc85580e5818cc6 (diff) |
Expose background job info to Python
Add `bpy.app.is_job_running(job_type)` as high-level indicator. Job
types currently exposed are `WM_JOB_TYPE_RENDER`,
`WM_JOB_TYPE_RENDER_PREVIEW`, and `WM_JOB_TYPE_OBJECT_BAKE`, as strings
with the `WM_JOB_TYPE_` prefix removed. The functions can be polled by
Python code to determine whether such background work is still ongoing
or not.
Furthermore, new app handles are added for
`object_bake_{pre,complete,canceled}`, which are called respectively
before an object baking job starts, completes sucessfully, and stops due
to a cancellation.
Motivation: There are various cases where Python can trigger the
execution of a background job, without getting notification that that
background job is done. As a result, it's hard to do things like
cleanups, or auto-quitting Blender after the work is done.
The approach in this commit can easily be extended with other job types,
when the need arises. The rendering of asset previews is one that's
likely to be added sooner than later, as there have already been
requests about this.
Reviewed By: campbellbarton
Differential Revision: https://developer.blender.org/D14587
Diffstat (limited to 'source/blender/windowmanager/intern/wm_jobs.c')
-rw-r--r-- | source/blender/windowmanager/intern/wm_jobs.c | 61 |
1 files changed, 54 insertions, 7 deletions
diff --git a/source/blender/windowmanager/intern/wm_jobs.c b/source/blender/windowmanager/intern/wm_jobs.c index b44cf9e48b8..bc80f56ee13 100644 --- a/source/blender/windowmanager/intern/wm_jobs.c +++ b/source/blender/windowmanager/intern/wm_jobs.c @@ -87,6 +87,16 @@ struct wmJob { * Executed in main thread. */ void (*endjob)(void *); + /** + * Called when job is stopped normally, i.e. by simply completing the startjob function. + * Executed in main thread. + */ + void (*completed)(void *); + /** + * Called when job is stopped abnormally, i.e. when stop=true but ready=false. + * Executed in main thread. + */ + void (*canceled)(void *); /** Running jobs each have own timer */ double timestep; @@ -344,10 +354,23 @@ void WM_jobs_callbacks(wmJob *wm_job, void (*update)(void *), void (*endjob)(void *)) { + WM_jobs_callbacks_ex(wm_job, startjob, initjob, update, endjob, NULL, NULL); +} + +void WM_jobs_callbacks_ex(wmJob *wm_job, + wm_jobs_start_callback startjob, + void (*initjob)(void *), + void (*update)(void *), + void (*endjob)(void *), + void (*completed)(void *), + void (*canceled)(void *)) +{ wm_job->startjob = startjob; wm_job->initjob = initjob; wm_job->update = update; wm_job->endjob = endjob; + wm_job->completed = completed; + wm_job->canceled = canceled; } static void *do_job_thread(void *job_v) @@ -465,6 +488,25 @@ void WM_jobs_start(wmWindowManager *wm, wmJob *wm_job) } } +static void wm_job_end(wmJob *wm_job) +{ + BLI_assert_msg(BLI_thread_is_main(), "wm_job_end should only be called from the main thread"); + if (wm_job->endjob) { + wm_job->endjob(wm_job->run_customdata); + } + + /* Do the final callback based on whether the job was run to completion or not. + * Not all jobs have the same way of signalling cancellation (f.e. rendering + * stops when G.is_break=true, but doesn't set any wm_job properties to cancel + * the WM job). */ + const bool was_canceled = wm_job->stop || G.is_break; + void (*final_callback)(void *) = (wm_job->ready && !was_canceled) ? wm_job->completed : + wm_job->canceled; + if (final_callback) { + final_callback(wm_job->run_customdata); + } +} + static void wm_job_free(wmWindowManager *wm, wmJob *wm_job) { BLI_remlink(&wm->jobs, wm_job); @@ -485,10 +527,7 @@ static void wm_jobs_kill_job(wmWindowManager *wm, wmJob *wm_job) WM_job_main_thread_lock_release(wm_job); BLI_threadpool_end(&wm_job->threads); WM_job_main_thread_lock_acquire(wm_job); - - if (wm_job->endjob) { - wm_job->endjob(wm_job->run_customdata); - } + wm_job_end(wm_job); } if (wm_job->wt) { @@ -600,9 +639,7 @@ void wm_jobs_timer(wmWindowManager *wm, wmTimer *wt) } if (wm_job->ready) { - if (wm_job->endjob) { - wm_job->endjob(wm_job->run_customdata); - } + wm_job_end(wm_job); /* free own data */ wm_job->run_free(wm_job->run_customdata); @@ -670,3 +707,13 @@ bool WM_jobs_has_running(const wmWindowManager *wm) return false; } + +bool WM_jobs_has_running_type(const struct wmWindowManager *wm, int job_type) +{ + LISTBASE_FOREACH (wmJob *, wm_job, &wm->jobs) { + if (wm_job->running && wm_job->job_type == job_type) { + return true; + } + } + return false; +} |