Refactoring: View2DScrollers memory allocation

View2DScrollers used the memory manager to allocate memory. This isn't a
problem but in a upcoming change the scrollers will be drawn more often
than it used to (See {D8066}). To limit the number of allocations and
frees this patch will use the stack for allocation.

Reviewed By: Campbell Barton

Differential Revision: https://developer.blender.org/D8076
This commit is contained in:
Jeroen Bakker 2020-06-22 21:44:18 +02:00 committed by Jeroen Bakker
parent 9335daac2a
commit 2fdca5bd19
Notes: blender-bot 2023-02-14 11:18:07 +01:00
Referenced by commit ed68fee665, Fix T78146: Crash using scroll bars
17 changed files with 63 additions and 112 deletions

@ -1 +1 @@
Subproject commit 72e5040232a544b293dca05dac5707bd4e4bffaf
Subproject commit caf68fed42f55e606b14c7105f5df694957ce036

@ -1 +1 @@
Subproject commit 8397b98b8cb6510869d28a84592b8d7213c3b6c2
Subproject commit bc1262f4d61feeba235bb75046e65e0e8411241f

@ -1 +1 @@
Subproject commit 7c36b48507f79ca62f8c038bad0fb3468c4f48e2
Subproject commit 45aa940dabda64f7877c6d5dd843998a86f0a836

View File

@ -180,9 +180,10 @@ void UI_view2d_draw_scale_x__frames_or_seconds(const struct ARegion *region,
int colorid);
/* scrollbar drawing */
View2DScrollers *UI_view2d_scrollers_calc(struct View2D *v2d, const struct rcti *mask_custom);
void UI_view2d_scrollers_draw(struct View2D *v2d, View2DScrollers *scrollers);
void UI_view2d_scrollers_free(View2DScrollers *scrollers);
void UI_view2d_scrollers_calc(struct View2D *v2d,
const struct rcti *mask_custom,
struct View2DScrollers *r_scrollers);
void UI_view2d_scrollers_draw(struct View2D *v2d, const struct rcti *mask_custom);
/* list view tools */
void UI_view2d_listview_view_to_cell(float columnwidth,

View File

@ -1413,17 +1413,15 @@ struct View2DScrollers {
};
/* Calculate relevant scroller properties */
View2DScrollers *UI_view2d_scrollers_calc(View2D *v2d, const rcti *mask_custom)
void UI_view2d_scrollers_calc(View2D *v2d,
const rcti *mask_custom,
struct View2DScrollers *r_scrollers)
{
View2DScrollers *scrollers;
rcti vert, hor;
float fac1, fac2, totsize, scrollsize;
int scroll = view2d_scroll_mapped(v2d->scroll);
int smaller;
/* scrollers is allocated here... */
scrollers = MEM_callocN(sizeof(View2DScrollers), "View2DScrollers");
/* Always update before drawing (for dynamically sized scrollers). */
view2d_masks(v2d, mask_custom);
@ -1456,8 +1454,8 @@ View2DScrollers *UI_view2d_scrollers_calc(View2D *v2d, const rcti *mask_custom)
CLAMP(hor.xmin, hor.xmin, hor.xmax - V2D_SCROLL_HANDLE_SIZE_HOTSPOT);
/* store in scrollers, used for drawing */
scrollers->vert = vert;
scrollers->hor = hor;
r_scrollers->vert = vert;
r_scrollers->hor = hor;
/* scroller 'buttons':
* - These should always remain within the visible region of the scrollbar
@ -1475,30 +1473,30 @@ View2DScrollers *UI_view2d_scrollers_calc(View2D *v2d, const rcti *mask_custom)
fac1 = (v2d->cur.xmin - v2d->tot.xmin) / totsize;
if (fac1 <= 0.0f) {
scrollers->hor_min = hor.xmin;
r_scrollers->hor_min = hor.xmin;
}
else {
scrollers->hor_min = (int)(hor.xmin + (fac1 * scrollsize));
r_scrollers->hor_min = (int)(hor.xmin + (fac1 * scrollsize));
}
fac2 = (v2d->cur.xmax - v2d->tot.xmin) / totsize;
if (fac2 >= 1.0f) {
scrollers->hor_max = hor.xmax;
r_scrollers->hor_max = hor.xmax;
}
else {
scrollers->hor_max = (int)(hor.xmin + (fac2 * scrollsize));
r_scrollers->hor_max = (int)(hor.xmin + (fac2 * scrollsize));
}
/* prevent inverted sliders */
if (scrollers->hor_min > scrollers->hor_max) {
scrollers->hor_min = scrollers->hor_max;
if (r_scrollers->hor_min > r_scrollers->hor_max) {
r_scrollers->hor_min = r_scrollers->hor_max;
}
/* prevent sliders from being too small to grab */
if ((scrollers->hor_max - scrollers->hor_min) < V2D_SCROLL_THUMB_SIZE_MIN) {
scrollers->hor_max = scrollers->hor_min + V2D_SCROLL_THUMB_SIZE_MIN;
if ((r_scrollers->hor_max - r_scrollers->hor_min) < V2D_SCROLL_THUMB_SIZE_MIN) {
r_scrollers->hor_max = r_scrollers->hor_min + V2D_SCROLL_THUMB_SIZE_MIN;
CLAMP(scrollers->hor_max, hor.xmin + V2D_SCROLL_THUMB_SIZE_MIN, hor.xmax);
CLAMP(scrollers->hor_min, hor.xmin, hor.xmax - V2D_SCROLL_THUMB_SIZE_MIN);
CLAMP(r_scrollers->hor_max, hor.xmin + V2D_SCROLL_THUMB_SIZE_MIN, hor.xmax);
CLAMP(r_scrollers->hor_min, hor.xmin, hor.xmax - V2D_SCROLL_THUMB_SIZE_MIN);
}
}
@ -1513,39 +1511,39 @@ View2DScrollers *UI_view2d_scrollers_calc(View2D *v2d, const rcti *mask_custom)
fac1 = (v2d->cur.ymin - v2d->tot.ymin) / totsize;
if (fac1 <= 0.0f) {
scrollers->vert_min = vert.ymin;
r_scrollers->vert_min = vert.ymin;
}
else {
scrollers->vert_min = (int)(vert.ymin + (fac1 * scrollsize));
r_scrollers->vert_min = (int)(vert.ymin + (fac1 * scrollsize));
}
fac2 = (v2d->cur.ymax - v2d->tot.ymin) / totsize;
if (fac2 >= 1.0f) {
scrollers->vert_max = vert.ymax;
r_scrollers->vert_max = vert.ymax;
}
else {
scrollers->vert_max = (int)(vert.ymin + (fac2 * scrollsize));
r_scrollers->vert_max = (int)(vert.ymin + (fac2 * scrollsize));
}
/* prevent inverted sliders */
if (scrollers->vert_min > scrollers->vert_max) {
scrollers->vert_min = scrollers->vert_max;
if (r_scrollers->vert_min > r_scrollers->vert_max) {
r_scrollers->vert_min = r_scrollers->vert_max;
}
/* prevent sliders from being too small to grab */
if ((scrollers->vert_max - scrollers->vert_min) < V2D_SCROLL_THUMB_SIZE_MIN) {
scrollers->vert_max = scrollers->vert_min + V2D_SCROLL_THUMB_SIZE_MIN;
if ((r_scrollers->vert_max - r_scrollers->vert_min) < V2D_SCROLL_THUMB_SIZE_MIN) {
r_scrollers->vert_max = r_scrollers->vert_min + V2D_SCROLL_THUMB_SIZE_MIN;
CLAMP(scrollers->vert_max, vert.ymin + V2D_SCROLL_THUMB_SIZE_MIN, vert.ymax);
CLAMP(scrollers->vert_min, vert.ymin, vert.ymax - V2D_SCROLL_THUMB_SIZE_MIN);
CLAMP(r_scrollers->vert_max, vert.ymin + V2D_SCROLL_THUMB_SIZE_MIN, vert.ymax);
CLAMP(r_scrollers->vert_min, vert.ymin, vert.ymax - V2D_SCROLL_THUMB_SIZE_MIN);
}
}
return scrollers;
}
/* Draw scrollbars in the given 2d-region */
void UI_view2d_scrollers_draw(View2D *v2d, View2DScrollers *vs)
void UI_view2d_scrollers_draw(View2D *v2d, const rcti *mask_custom)
{
View2DScrollers scrollers;
UI_view2d_scrollers_calc(v2d, mask_custom, &scrollers);
bTheme *btheme = UI_GetTheme();
rcti vert, hor;
const int scroll = view2d_scroll_mapped(v2d->scroll);
@ -1556,8 +1554,8 @@ void UI_view2d_scrollers_draw(View2D *v2d, View2DScrollers *vs)
UI_GetThemeColor4ubv(TH_BACK, scrollers_back_color);
/* make copies of rects for less typing */
vert = vs->vert;
hor = vs->hor;
vert = scrollers.vert;
hor = scrollers.hor;
/* horizontal scrollbar */
if (scroll & V2D_SCROLL_HORIZONTAL) {
@ -1566,8 +1564,8 @@ void UI_view2d_scrollers_draw(View2D *v2d, View2DScrollers *vs)
rcti slider;
int state;
slider.xmin = vs->hor_min;
slider.xmax = vs->hor_max;
slider.xmin = scrollers.hor_min;
slider.xmax = scrollers.hor_max;
slider.ymin = hor.ymin;
slider.ymax = hor.ymax;
@ -1602,8 +1600,8 @@ void UI_view2d_scrollers_draw(View2D *v2d, View2DScrollers *vs)
slider.xmin = vert.xmin;
slider.xmax = vert.xmax;
slider.ymin = vs->vert_min;
slider.ymax = vs->vert_max;
slider.ymin = scrollers.vert_min;
slider.ymax = scrollers.vert_max;
state = (v2d->scroll_ui & V2D_SCROLL_V_ACTIVE) ? UI_SCROLL_PRESSED : 0;
@ -1631,12 +1629,6 @@ void UI_view2d_scrollers_draw(View2D *v2d, View2DScrollers *vs)
btheme->tui.widget_emboss[3] = emboss_alpha;
}
/* free temporary memory used for drawing scrollers */
void UI_view2d_scrollers_free(View2DScrollers *scrollers)
{
MEM_freeN(scrollers);
}
/** \} */
/* -------------------------------------------------------------------- */

View File

@ -2011,7 +2011,7 @@ static void scroller_activate_init(bContext *C,
const char in_scroller)
{
v2dScrollerMove *vsm;
View2DScrollers *scrollers;
View2DScrollers scrollers;
ARegion *region = CTX_wm_region(C);
View2D *v2d = &region->v2d;
rctf tot_cur_union;
@ -2032,7 +2032,7 @@ static void scroller_activate_init(bContext *C,
/* 'zone' depends on where mouse is relative to bubble
* - zooming must be allowed on this axis, otherwise, default to pan
*/
scrollers = UI_view2d_scrollers_calc(v2d, NULL);
UI_view2d_scrollers_calc(v2d, NULL, &scrollers);
/* Use a union of 'cur' & 'tot' in case the current view is far outside 'tot'. In this cases
* moving the scroll bars has far too little effect and the view can get stuck T31476. */
@ -2049,15 +2049,15 @@ static void scroller_activate_init(bContext *C,
/* get 'zone' (i.e. which part of scroller is activated) */
vsm->zone = mouse_in_scroller_handle(
event->mval[0], v2d->hor.xmin, v2d->hor.xmax, scrollers->hor_min, scrollers->hor_max);
event->mval[0], v2d->hor.xmin, v2d->hor.xmax, scrollers.hor_min, scrollers.hor_max);
if ((v2d->keepzoom & V2D_LOCKZOOM_X) && ELEM(vsm->zone, SCROLLHANDLE_MIN, SCROLLHANDLE_MAX)) {
/* default to scroll, as handles not usable */
vsm->zone = SCROLLHANDLE_BAR;
}
vsm->scrollbarwidth = scrollers->hor_max - scrollers->hor_min;
vsm->scrollbar_orig = ((scrollers->hor_max + scrollers->hor_min) / 2) + region->winrct.xmin;
vsm->scrollbarwidth = scrollers.hor_max - scrollers.hor_min;
vsm->scrollbar_orig = ((scrollers.hor_max + scrollers.hor_min) / 2) + region->winrct.xmin;
}
else {
/* vertical scroller - calculate adjustment factor first */
@ -2069,18 +2069,17 @@ static void scroller_activate_init(bContext *C,
/* get 'zone' (i.e. which part of scroller is activated) */
vsm->zone = mouse_in_scroller_handle(
event->mval[1], v2d->vert.ymin, v2d->vert.ymax, scrollers->vert_min, scrollers->vert_max);
event->mval[1], v2d->vert.ymin, v2d->vert.ymax, scrollers.vert_min, scrollers.vert_max);
if ((v2d->keepzoom & V2D_LOCKZOOM_Y) && ELEM(vsm->zone, SCROLLHANDLE_MIN, SCROLLHANDLE_MAX)) {
/* default to scroll, as handles not usable */
vsm->zone = SCROLLHANDLE_BAR;
}
vsm->scrollbarwidth = scrollers->vert_max - scrollers->vert_min;
vsm->scrollbar_orig = ((scrollers->vert_max + scrollers->vert_min) / 2) + region->winrct.ymin;
vsm->scrollbarwidth = scrollers.vert_max - scrollers.vert_min;
vsm->scrollbar_orig = ((scrollers.vert_max + scrollers.vert_min) / 2) + region->winrct.ymin;
}
UI_view2d_scrollers_free(scrollers);
ED_region_tag_redraw_no_rebuild(region);
}

View File

@ -2795,9 +2795,7 @@ void ED_region_panels_draw(const bContext *C, ARegion *region)
mask_buf.xmax -= UI_PANEL_CATEGORY_MARGIN_WIDTH;
mask = &mask_buf;
}
View2DScrollers *scrollers = UI_view2d_scrollers_calc(v2d, mask);
UI_view2d_scrollers_draw(v2d, scrollers);
UI_view2d_scrollers_free(scrollers);
UI_view2d_scrollers_draw(v2d, mask);
}
void ED_region_panels_ex(

View File

@ -181,7 +181,6 @@ static void action_main_region_draw(const bContext *C, ARegion *region)
Object *obact = CTX_data_active_object(C);
bAnimContext ac;
View2D *v2d = &region->v2d;
View2DScrollers *scrollers;
short marker_flag = 0;
short cfra_flag = 0;
@ -240,9 +239,7 @@ static void action_main_region_draw(const bContext *C, ARegion *region)
ED_time_scrub_draw(region, scene, saction->flag & SACTION_DRAWTIME, true);
/* scrollers */
scrollers = UI_view2d_scrollers_calc(v2d, NULL);
UI_view2d_scrollers_draw(v2d, scrollers);
UI_view2d_scrollers_free(scrollers);
UI_view2d_scrollers_draw(v2d, NULL);
}
/* add handlers, stuff you only do once or on area/region changes */

View File

@ -1045,7 +1045,6 @@ static void clip_preview_region_init(wmWindowManager *wm, ARegion *region)
static void graph_region_draw(const bContext *C, ARegion *region)
{
View2D *v2d = &region->v2d;
View2DScrollers *scrollers;
SpaceClip *sc = CTX_wm_space_clip(C);
Scene *scene = CTX_data_scene(C);
short cfra_flag = 0;
@ -1076,9 +1075,7 @@ static void graph_region_draw(const bContext *C, ARegion *region)
ED_time_scrub_draw(region, scene, sc->flag & SC_SHOW_SECONDS, true);
/* scrollers */
scrollers = UI_view2d_scrollers_calc(v2d, NULL);
UI_view2d_scrollers_draw(v2d, scrollers);
UI_view2d_scrollers_free(scrollers);
UI_view2d_scrollers_draw(v2d, NULL);
/* scale indicators */
{
@ -1095,7 +1092,6 @@ static void dopesheet_region_draw(const bContext *C, ARegion *region)
SpaceClip *sc = CTX_wm_space_clip(C);
MovieClip *clip = ED_space_clip_get_clip(sc);
View2D *v2d = &region->v2d;
View2DScrollers *scrollers;
short cfra_flag = 0;
if (clip) {
@ -1127,9 +1123,7 @@ static void dopesheet_region_draw(const bContext *C, ARegion *region)
ED_time_scrub_draw(region, scene, sc->flag & SC_SHOW_SECONDS, true);
/* scrollers */
scrollers = UI_view2d_scrollers_calc(v2d, NULL);
UI_view2d_scrollers_draw(v2d, scrollers);
UI_view2d_scrollers_free(scrollers);
UI_view2d_scrollers_draw(v2d, NULL);
}
static void clip_preview_region_draw(const bContext *C, ARegion *region)

View File

@ -208,7 +208,6 @@ static void console_main_region_draw(const bContext *C, ARegion *region)
/* draw entirely, view changes should be handled here */
SpaceConsole *sc = CTX_wm_space_console(C);
View2D *v2d = &region->v2d;
View2DScrollers *scrollers;
if (BLI_listbase_is_empty(&sc->scrollback)) {
WM_operator_name_call((bContext *)C, "CONSOLE_OT_banner", WM_OP_EXEC_DEFAULT, NULL);
@ -230,9 +229,7 @@ static void console_main_region_draw(const bContext *C, ARegion *region)
UI_view2d_view_restore(C);
/* scrollers */
scrollers = UI_view2d_scrollers_calc(v2d, NULL);
UI_view2d_scrollers_draw(v2d, scrollers);
UI_view2d_scrollers_free(scrollers);
UI_view2d_scrollers_draw(v2d, NULL);
}
static void console_operatortypes(void)

View File

@ -449,7 +449,6 @@ static void file_main_region_draw(const bContext *C, ARegion *region)
FileSelectParams *params = ED_fileselect_get_params(sfile);
View2D *v2d = &region->v2d;
View2DScrollers *scrollers;
float col[3];
/* Needed, because filelist is not initialized on loading */
@ -509,9 +508,7 @@ static void file_main_region_draw(const bContext *C, ARegion *region)
/* scrollers */
rcti view_rect;
ED_fileselect_layout_maskrect(sfile->layout, v2d, &view_rect);
scrollers = UI_view2d_scrollers_calc(v2d, &view_rect);
UI_view2d_scrollers_draw(v2d, scrollers);
UI_view2d_scrollers_free(scrollers);
UI_view2d_scrollers_draw(v2d, &view_rect);
}
static void file_operatortypes(void)

View File

@ -200,7 +200,6 @@ static void graph_main_region_draw(const bContext *C, ARegion *region)
Scene *scene = CTX_data_scene(C);
bAnimContext ac;
View2D *v2d = &region->v2d;
View2DScrollers *scrollers;
float col[3];
short cfra_flag = 0;
@ -319,9 +318,7 @@ static void graph_main_region_draw(const bContext *C, ARegion *region)
/* scrollers */
// FIXME: args for scrollers depend on the type of data being shown...
scrollers = UI_view2d_scrollers_calc(v2d, NULL);
UI_view2d_scrollers_draw(v2d, scrollers);
UI_view2d_scrollers_free(scrollers);
UI_view2d_scrollers_draw(v2d, NULL);
/* scale numbers */
{
@ -358,7 +355,6 @@ static void graph_channel_region_draw(const bContext *C, ARegion *region)
{
bAnimContext ac;
View2D *v2d = &region->v2d;
View2DScrollers *scrollers;
float col[3];
/* clear and setup matrix */
@ -380,9 +376,7 @@ static void graph_channel_region_draw(const bContext *C, ARegion *region)
UI_view2d_view_restore(C);
/* scrollers */
scrollers = UI_view2d_scrollers_calc(v2d, NULL);
UI_view2d_scrollers_draw(v2d, scrollers);
UI_view2d_scrollers_free(scrollers);
UI_view2d_scrollers_draw(v2d, NULL);
}
/* add handlers, stuff you only do once or on area/region changes */

View File

@ -139,7 +139,6 @@ static void info_main_region_draw(const bContext *C, ARegion *region)
/* draw entirely, view changes should be handled here */
SpaceInfo *sinfo = CTX_wm_space_info(C);
View2D *v2d = &region->v2d;
View2DScrollers *scrollers;
/* clear and setup matrix */
UI_ThemeClearColor(TH_BACK);
@ -161,9 +160,7 @@ static void info_main_region_draw(const bContext *C, ARegion *region)
UI_view2d_view_restore(C);
/* scrollers */
scrollers = UI_view2d_scrollers_calc(v2d, NULL);
UI_view2d_scrollers_draw(v2d, scrollers);
UI_view2d_scrollers_free(scrollers);
UI_view2d_scrollers_draw(v2d, NULL);
}
static void info_operatortypes(void)

View File

@ -191,7 +191,6 @@ static void nla_channel_region_draw(const bContext *C, ARegion *region)
{
bAnimContext ac;
View2D *v2d = &region->v2d;
View2DScrollers *scrollers;
/* clear and setup matrix */
UI_ThemeClearColor(TH_BACK);
@ -211,9 +210,7 @@ static void nla_channel_region_draw(const bContext *C, ARegion *region)
UI_view2d_view_restore(C);
/* scrollers */
scrollers = UI_view2d_scrollers_calc(v2d, NULL);
UI_view2d_scrollers_draw(v2d, scrollers);
UI_view2d_scrollers_free(scrollers);
UI_view2d_scrollers_draw(v2d, NULL);
}
/* add handlers, stuff you only do once or on area/region changes */
@ -237,7 +234,6 @@ static void nla_main_region_draw(const bContext *C, ARegion *region)
Scene *scene = CTX_data_scene(C);
bAnimContext ac;
View2D *v2d = &region->v2d;
View2DScrollers *scrollers;
short cfra_flag = 0;
/* clear and setup matrix */
@ -292,9 +288,7 @@ static void nla_main_region_draw(const bContext *C, ARegion *region)
ED_time_scrub_draw(region, scene, snla->flag & SNLA_DRAWTIME, true);
/* scrollers */
scrollers = UI_view2d_scrollers_calc(v2d, NULL);
UI_view2d_scrollers_draw(v2d, scrollers);
UI_view2d_scrollers_free(scrollers);
UI_view2d_scrollers_draw(v2d, NULL);
}
/* add handlers, stuff you only do once or on area/region changes */

View File

@ -1772,7 +1772,6 @@ static void draw_group_overlay(const bContext *C, ARegion *region)
void drawnodespace(const bContext *C, ARegion *region)
{
wmWindow *win = CTX_wm_window(C);
View2DScrollers *scrollers;
SpaceNode *snode = CTX_wm_space_node(C);
View2D *v2d = &region->v2d;
@ -1921,7 +1920,5 @@ void drawnodespace(const bContext *C, ARegion *region)
draw_tree_path(snode);
/* scrollers */
scrollers = UI_view2d_scrollers_calc(v2d, NULL);
UI_view2d_scrollers_draw(v2d, scrollers);
UI_view2d_scrollers_free(scrollers);
UI_view2d_scrollers_draw(v2d, NULL);
}

View File

@ -84,7 +84,6 @@ static void outliner_main_region_init(wmWindowManager *wm, ARegion *region)
static void outliner_main_region_draw(const bContext *C, ARegion *region)
{
View2D *v2d = &region->v2d;
View2DScrollers *scrollers;
/* clear */
UI_ThemeClearColor(TH_BACK);
@ -96,9 +95,7 @@ static void outliner_main_region_draw(const bContext *C, ARegion *region)
UI_view2d_view_restore(C);
/* scrollers */
scrollers = UI_view2d_scrollers_calc(v2d, NULL);
UI_view2d_scrollers_draw(v2d, scrollers);
UI_view2d_scrollers_free(scrollers);
UI_view2d_scrollers_draw(v2d, NULL);
}
static void outliner_main_region_free(ARegion *UNUSED(region))

View File

@ -2257,7 +2257,6 @@ void draw_timeline_seq(const bContext *C, ARegion *region)
Editing *ed = BKE_sequencer_editing_get(scene, false);
SpaceSeq *sseq = CTX_wm_space_seq(C);
View2D *v2d = &region->v2d;
View2DScrollers *scrollers;
short cfra_flag = 0;
float col[3];
@ -2345,9 +2344,7 @@ void draw_timeline_seq(const bContext *C, ARegion *region)
ED_region_draw_cb_draw(C, region, REGION_DRAW_POST_VIEW);
UI_view2d_view_restore(C);
ED_time_scrub_draw(region, scene, !(sseq->flag & SEQ_DRAWFRAMES), true);
scrollers = UI_view2d_scrollers_calc(v2d, NULL);
UI_view2d_scrollers_draw(v2d, scrollers);
UI_view2d_scrollers_free(scrollers);
UI_view2d_scrollers_draw(v2d, NULL);
/* Draw channel numbers. */
{