Add ability for redraw timer to run for fixed time

Also avoid accessing context vars in timer loop.
This commit is contained in:
Campbell Barton 2015-07-12 22:57:35 +10:00
parent 2fd3b9ad84
commit 8d48f8e0b0

@ -4621,7 +4621,7 @@ static void redraw_timer_window_swap(bContext *C)
CTX_wm_window_set(C, win); /* XXX context manipulation warning! */
}
enum eRedrawTimerItems {
enum {
eRTDrawRegion = 0,
eRTDrawRegionSwap = 1,
eRTDrawWindow = 2,
@ -4642,102 +4642,117 @@ static EnumPropertyItem redraw_timer_type_items[] = {
{0, NULL, 0, NULL, NULL}
};
static int redraw_timer_exec(bContext *C, wmOperator *op)
static void redraw_timer_step(
bContext *C, Main *bmain, Scene *scene, wmWindow *win, ARegion *ar,
const int type, const int cfra)
{
ARegion *ar = CTX_wm_region(C);
double stime = PIL_check_seconds_timer();
int type = RNA_enum_get(op->ptr, "type");
int iter = RNA_int_get(op->ptr, "iterations");
int a;
float time;
const char *infostr = "";
WM_cursor_wait(1);
for (a = 0; a < iter; a++) {
if (type == eRTDrawRegion) {
if (ar) {
ED_region_do_draw(C, ar);
ar->do_draw = false;
}
if (type == eRTDrawRegion) {
if (ar) {
ED_region_do_draw(C, ar);
ar->do_draw = false;
}
else if (type == eRTDrawRegionSwap) {
wmWindow *win = CTX_wm_window(C);
CTX_wm_menu_set(C, NULL);
ED_region_tag_redraw(ar);
wm_draw_update(C);
CTX_wm_window_set(C, win); /* XXX context manipulation warning! */
}
else if (type == eRTDrawWindow) {
wmWindow *win = CTX_wm_window(C);
ScrArea *sa;
ScrArea *sa_back = CTX_wm_area(C);
ARegion *ar_back = CTX_wm_region(C);
}
else if (type == eRTDrawRegionSwap) {
CTX_wm_menu_set(C, NULL);
CTX_wm_menu_set(C, NULL);
for (sa = CTX_wm_screen(C)->areabase.first; sa; sa = sa->next) {
ARegion *ar_iter;
CTX_wm_area_set(C, sa);
ED_region_tag_redraw(ar);
wm_draw_update(C);
for (ar_iter = sa->regionbase.first; ar_iter; ar_iter = ar_iter->next) {
if (ar_iter->swinid) {
CTX_wm_region_set(C, ar_iter);
ED_region_do_draw(C, ar_iter);
ar->do_draw = false;
}
CTX_wm_window_set(C, win); /* XXX context manipulation warning! */
}
else if (type == eRTDrawWindow) {
ScrArea *sa;
ScrArea *sa_back = CTX_wm_area(C);
ARegion *ar_back = CTX_wm_region(C);
CTX_wm_menu_set(C, NULL);
for (sa = CTX_wm_screen(C)->areabase.first; sa; sa = sa->next) {
ARegion *ar_iter;
CTX_wm_area_set(C, sa);
for (ar_iter = sa->regionbase.first; ar_iter; ar_iter = ar_iter->next) {
if (ar_iter->swinid) {
CTX_wm_region_set(C, ar_iter);
ED_region_do_draw(C, ar_iter);
ar->do_draw = false;
}
}
CTX_wm_window_set(C, win); /* XXX context manipulation warning! */
CTX_wm_area_set(C, sa_back);
CTX_wm_region_set(C, ar_back);
}
else if (type == eRTDrawWindowSwap) {
CTX_wm_window_set(C, win); /* XXX context manipulation warning! */
CTX_wm_area_set(C, sa_back);
CTX_wm_region_set(C, ar_back);
}
else if (type == eRTDrawWindowSwap) {
redraw_timer_window_swap(C);
}
else if (type == eRTAnimationStep) {
scene->r.cfra += (cfra == scene->r.cfra) ? 1 : -1;
BKE_scene_update_for_newframe(bmain->eval_ctx, bmain, scene, scene->lay);
}
else if (type == eRTAnimationPlay) {
/* play anim, return on same frame as started with */
int tot = (scene->r.efra - scene->r.sfra) + 1;
while (tot--) {
/* todo, ability to escape! */
scene->r.cfra++;
if (scene->r.cfra > scene->r.efra)
scene->r.cfra = scene->r.sfra;
BKE_scene_update_for_newframe(bmain->eval_ctx, bmain, scene, scene->lay);
redraw_timer_window_swap(C);
}
else if (type == eRTAnimationStep) {
Main *bmain = CTX_data_main(C);
Scene *scene = CTX_data_scene(C);
if (a & 1) scene->r.cfra--;
else scene->r.cfra++;
BKE_scene_update_for_newframe(bmain->eval_ctx, bmain, scene, scene->lay);
}
else if (type == eRTAnimationPlay) {
}
else { /* eRTUndo */
ED_undo_pop(C);
ED_undo_redo(C);
}
}
/* play anim, return on same frame as started with */
Main *bmain = CTX_data_main(C);
Scene *scene = CTX_data_scene(C);
int tot = (scene->r.efra - scene->r.sfra) + 1;
static int redraw_timer_exec(bContext *C, wmOperator *op)
{
Main *bmain = CTX_data_main(C);
Scene *scene = CTX_data_scene(C);
wmWindow *win = CTX_wm_window(C);
ARegion *ar = CTX_wm_region(C);
double time_start, time_delta;
const int type = RNA_enum_get(op->ptr, "type");
const int iter = RNA_int_get(op->ptr, "iterations");
const double time_limit = (double)RNA_float_get(op->ptr, "time_limit");
const int cfra = scene->r.cfra;
int a, iter_steps = 0;
const char *infostr = "";
while (tot--) {
/* todo, ability to escape! */
scene->r.cfra++;
if (scene->r.cfra > scene->r.efra)
scene->r.cfra = scene->r.sfra;
WM_cursor_wait(1);
BKE_scene_update_for_newframe(bmain->eval_ctx, bmain, scene, scene->lay);
redraw_timer_window_swap(C);
time_start = PIL_check_seconds_timer();
for (a = 0; a < iter; a++) {
redraw_timer_step(C, bmain, scene, win, ar, type, cfra);
iter_steps += 1;
if (time_limit != 0.0f) {
if ((PIL_check_seconds_timer() - time_start) > time_limit) {
break;
}
}
else { /* eRTUndo */
ED_undo_pop(C);
ED_undo_redo(C);
a = 0;
}
}
time = (float)((PIL_check_seconds_timer() - stime) * 1000);
time_delta = (float)((PIL_check_seconds_timer() - time_start) * 1000);
RNA_enum_description(redraw_timer_type_items, type, &infostr);
WM_cursor_wait(0);
BKE_reportf(op->reports, RPT_WARNING, "%d x %s: %.2f ms, average: %.4f", iter, infostr, time, time / iter);
BKE_reportf(op->reports, RPT_WARNING,
"%d x %s: %.4f ms, average: %.8f",
iter_steps, infostr, time_delta, time_delta / iter_steps);
return OPERATOR_FINISHED;
}
@ -4754,6 +4769,8 @@ static void WM_OT_redraw_timer(wmOperatorType *ot)
ot->prop = RNA_def_enum(ot->srna, "type", redraw_timer_type_items, eRTDrawRegion, "Type", "");
RNA_def_int(ot->srna, "iterations", 10, 1, INT_MAX, "Iterations", "Number of times to redraw", 1, 1000);
RNA_def_float(ot->srna, "time_limit", 0.0, 0.0, FLT_MAX,
"Time Limit", "Seconds to run the test for (override iterations)", 0.0, 60.0);
}