UI: allocate panel runtime data separately

This results in better separation for what is stored in .blend files and what is not.
Also allows us to potentially use C++ in panel run-time data.

Pull Request: https://projects.blender.org/blender/blender/pulls/113502
This commit is contained in:
Jacques Lucke
2023-10-10 18:17:31 +02:00
parent e235913c0d
commit 663aa3538d
5 changed files with 55 additions and 44 deletions

View File

@@ -527,6 +527,11 @@ ARegion *BKE_area_region_copy(const SpaceType *st, const ARegion *region);
*/ */
void BKE_area_region_free(SpaceType *st, ARegion *region); void BKE_area_region_free(SpaceType *st, ARegion *region);
void BKE_area_region_panels_free(ListBase *panels); void BKE_area_region_panels_free(ListBase *panels);
/**
* Create and free panels.
*/
Panel *BKE_panel_new(PanelType *panel_type);
void BKE_panel_free(Panel *panel);
/** /**
* Doesn't free the area itself. * Doesn't free the area itself.
*/ */

View File

@@ -36,6 +36,7 @@
#include "BLI_math_vector.h" #include "BLI_math_vector.h"
#include "BLI_mempool.h" #include "BLI_mempool.h"
#include "BLI_rect.h" #include "BLI_rect.h"
#include "BLI_string.h"
#include "BLI_utildefines.h" #include "BLI_utildefines.h"
#include "BLT_translation.h" #include "BLT_translation.h"
@@ -486,23 +487,36 @@ void BKE_region_callback_free_gizmomap_set(void (*callback)(wmGizmoMap *))
region_free_gizmomap_callback = callback; region_free_gizmomap_callback = callback;
} }
static void area_region_panels_free_recursive(Panel *panel) Panel *BKE_panel_new(PanelType *panel_type)
{
Panel *panel = MEM_cnew<Panel>(__func__);
panel->runtime = MEM_cnew<Panel_Runtime>(__func__);
panel->type = panel_type;
STRNCPY(panel->panelname, panel_type->idname);
return panel;
}
void BKE_panel_free(Panel *panel)
{ {
MEM_SAFE_FREE(panel->activedata); MEM_SAFE_FREE(panel->activedata);
MEM_SAFE_FREE(panel->drawname); MEM_SAFE_FREE(panel->drawname);
MEM_freeN(panel->runtime);
MEM_freeN(panel);
}
static void area_region_panels_free_recursive(Panel *panel)
{
LISTBASE_FOREACH_MUTABLE (Panel *, child_panel, &panel->children) { LISTBASE_FOREACH_MUTABLE (Panel *, child_panel, &panel->children) {
area_region_panels_free_recursive(child_panel); area_region_panels_free_recursive(child_panel);
} }
BKE_panel_free(panel);
MEM_freeN(panel);
} }
void BKE_area_region_panels_free(ListBase *panels) void BKE_area_region_panels_free(ListBase *panels)
{ {
LISTBASE_FOREACH_MUTABLE (Panel *, panel, panels) { LISTBASE_FOREACH_MUTABLE (Panel *, panel, panels) {
/* Free custom data just for parent panels to avoid a double free. */ /* Free custom data just for parent panels to avoid a double free. */
MEM_SAFE_FREE(panel->runtime.custom_data_ptr); MEM_SAFE_FREE(panel->runtime->custom_data_ptr);
area_region_panels_free_recursive(panel); area_region_panels_free_recursive(panel);
} }
BLI_listbase_clear(panels); BLI_listbase_clear(panels);
@@ -1095,11 +1109,11 @@ static void direct_link_panel_list(BlendDataReader *reader, ListBase *lb)
BLO_read_list(reader, lb); BLO_read_list(reader, lb);
LISTBASE_FOREACH (Panel *, panel, lb) { LISTBASE_FOREACH (Panel *, panel, lb) {
panel->runtime = MEM_cnew<Panel_Runtime>(__func__);
panel->runtime_flag = 0; panel->runtime_flag = 0;
panel->activedata = nullptr; panel->activedata = nullptr;
panel->type = nullptr; panel->type = nullptr;
panel->drawname = nullptr; panel->drawname = nullptr;
panel->runtime.custom_data_ptr = nullptr;
direct_link_panel_list(reader, &panel->children); direct_link_panel_list(reader, &panel->children);
} }
} }

View File

@@ -3179,6 +3179,8 @@ void uiItemPopoverPanel_ptr(
if (ok && (pt->draw_header != nullptr)) { if (ok && (pt->draw_header != nullptr)) {
layout = uiLayoutRow(layout, true); layout = uiLayoutRow(layout, true);
Panel panel{}; Panel panel{};
Panel_Runtime panel_runtime{};
panel.runtime = &panel_runtime;
panel.type = pt; panel.type = pt;
panel.layout = layout; panel.layout = layout;
panel.flag = PNL_POPOVER; panel.flag = PNL_POPOVER;
@@ -5963,8 +5965,7 @@ static bool ui_layout_has_panel_label(const uiLayout *layout, const PanelType *p
static void ui_paneltype_draw_impl(bContext *C, PanelType *pt, uiLayout *layout, bool show_header) static void ui_paneltype_draw_impl(bContext *C, PanelType *pt, uiLayout *layout, bool show_header)
{ {
Panel *panel = MEM_cnew<Panel>(__func__); Panel *panel = BKE_panel_new(pt);
panel->type = pt;
panel->flag = PNL_POPOVER; panel->flag = PNL_POPOVER;
if (pt->listener) { if (pt->listener) {
@@ -5993,9 +5994,9 @@ static void ui_paneltype_draw_impl(bContext *C, PanelType *pt, uiLayout *layout,
panel->layout = layout; panel->layout = layout;
pt->draw(C, panel); pt->draw(C, panel);
panel->layout = nullptr; panel->layout = nullptr;
BLI_assert(panel->runtime.custom_data_ptr == nullptr); BLI_assert(panel->runtime->custom_data_ptr == nullptr);
MEM_freeN(panel); BKE_panel_free(panel);
/* Draw child panels. */ /* Draw child panels. */
LISTBASE_FOREACH (LinkData *, link, &pt->children) { LISTBASE_FOREACH (LinkData *, link, &pt->children) {

View File

@@ -222,11 +222,9 @@ static bool panels_need_realign(const ScrArea *area, ARegion *region, Panel **r_
static Panel *panel_add_instanced(ListBase *panels, PanelType *panel_type, PointerRNA *custom_data) static Panel *panel_add_instanced(ListBase *panels, PanelType *panel_type, PointerRNA *custom_data)
{ {
Panel *panel = MEM_cnew<Panel>(__func__); Panel *panel = BKE_panel_new(panel_type);
panel->type = panel_type;
STRNCPY(panel->panelname, panel_type->idname);
panel->runtime.custom_data_ptr = custom_data; panel->runtime->custom_data_ptr = custom_data;
panel->runtime_flag |= PANEL_NEW_ADDED; panel->runtime_flag |= PANEL_NEW_ADDED;
/* Add the panel's children too. Although they aren't instanced panels, we can still use this /* Add the panel's children too. Although they aren't instanced panels, we can still use this
@@ -301,11 +299,7 @@ static void panel_delete(const bContext *C, ARegion *region, ListBase *panels, P
BLI_freelistN(&panel->children); BLI_freelistN(&panel->children);
BLI_remlink(panels, panel); BLI_remlink(panels, panel);
if (panel->activedata) { BKE_panel_free(panel);
MEM_freeN(panel->activedata);
}
MEM_SAFE_FREE(panel->drawname);
MEM_freeN(panel);
} }
void UI_panels_free_instanced(const bContext *C, ARegion *region) void UI_panels_free_instanced(const bContext *C, ARegion *region)
@@ -319,8 +313,8 @@ void UI_panels_free_instanced(const bContext *C, ARegion *region)
} }
/* Free panel's custom data. */ /* Free panel's custom data. */
if (panel->runtime.custom_data_ptr != nullptr) { if (panel->runtime->custom_data_ptr != nullptr) {
MEM_freeN(panel->runtime.custom_data_ptr); MEM_freeN(panel->runtime->custom_data_ptr);
} }
/* Free the panel and its sub-panels. */ /* Free the panel and its sub-panels. */
@@ -446,7 +440,7 @@ static void reorder_instanced_panel_list(bContext *C, ARegion *region, Panel *dr
/* Set the bit to tell the interface to instanced the list. */ /* Set the bit to tell the interface to instanced the list. */
drag_panel->flag |= PNL_INSTANCED_LIST_ORDER_CHANGED; drag_panel->flag |= PNL_INSTANCED_LIST_ORDER_CHANGED;
CTX_store_set(C, drag_panel->runtime.context); CTX_store_set(C, drag_panel->runtime->context);
/* Finally, move this panel's list item to the new index in its list. */ /* Finally, move this panel's list item to the new index in its list. */
drag_panel->type->reorder(C, drag_panel, move_to_index); drag_panel->type->reorder(C, drag_panel, move_to_index);
@@ -666,13 +660,10 @@ Panel *UI_panel_begin(
{ {
Panel *panel_last; Panel *panel_last;
const char *drawname = CTX_IFACE_(pt->translation_context, pt->label); const char *drawname = CTX_IFACE_(pt->translation_context, pt->label);
const char *idname = pt->idname;
const bool newpanel = (panel == nullptr); const bool newpanel = (panel == nullptr);
if (newpanel) { if (newpanel) {
panel = MEM_cnew<Panel>(__func__); panel = BKE_panel_new(pt);
panel->type = pt;
STRNCPY(panel->panelname, idname);
if (pt->flag & PANEL_TYPE_DEFAULT_CLOSED) { if (pt->flag & PANEL_TYPE_DEFAULT_CLOSED) {
panel->flag |= PNL_CLOSED; panel->flag |= PNL_CLOSED;
@@ -694,7 +685,7 @@ Panel *UI_panel_begin(
panel->type = pt; panel->type = pt;
} }
panel->runtime.block = block; panel->runtime->block = block;
UI_panel_drawname_set(panel, drawname); UI_panel_drawname_set(panel, drawname);
@@ -736,14 +727,14 @@ Panel *UI_panel_begin(
void UI_panel_header_buttons_begin(Panel *panel) void UI_panel_header_buttons_begin(Panel *panel)
{ {
uiBlock *block = panel->runtime.block; uiBlock *block = panel->runtime->block;
ui_block_new_button_group(block, UI_BUTTON_GROUP_LOCK | UI_BUTTON_GROUP_PANEL_HEADER); ui_block_new_button_group(block, UI_BUTTON_GROUP_LOCK | UI_BUTTON_GROUP_PANEL_HEADER);
} }
void UI_panel_header_buttons_end(Panel *panel) void UI_panel_header_buttons_end(Panel *panel)
{ {
uiBlock *block = panel->runtime.block; uiBlock *block = panel->runtime->block;
/* A button group should always be created in #UI_panel_header_buttons_begin. */ /* A button group should always be created in #UI_panel_header_buttons_begin. */
BLI_assert(!block->button_groups.is_empty()); BLI_assert(!block->button_groups.is_empty());
@@ -801,7 +792,7 @@ static void panel_calculate_size_recursive(ARegion *region, Panel *panel)
} }
else { else {
const int old_sizex = panel->sizex, old_sizey = panel->sizey; const int old_sizex = panel->sizex, old_sizey = panel->sizey;
const int old_region_ofsx = panel->runtime.region_ofsx; const int old_region_ofsx = panel->runtime->region_ofsx;
/* Update width/height if non-zero. */ /* Update width/height if non-zero. */
if (width != 0) { if (width != 0) {
@@ -817,8 +808,8 @@ static void panel_calculate_size_recursive(ARegion *region, Panel *panel)
panel->ofsy += old_sizey - panel->sizey; panel->ofsy += old_sizey - panel->sizey;
} }
panel->runtime.region_ofsx = panel_region_offset_x_get(region); panel->runtime->region_ofsx = panel_region_offset_x_get(region);
if (old_region_ofsx != panel->runtime.region_ofsx) { if (old_region_ofsx != panel->runtime->region_ofsx) {
panel->runtime_flag |= PANEL_ANIM_ALIGN; panel->runtime_flag |= PANEL_ANIM_ALIGN;
} }
} }
@@ -922,7 +913,7 @@ static void region_panels_set_expansion_from_search_filter(const bContext *C,
*/ */
static void panel_remove_invisible_layouts_recursive(Panel *panel, const Panel *parent_panel) static void panel_remove_invisible_layouts_recursive(Panel *panel, const Panel *parent_panel)
{ {
uiBlock *block = panel->runtime.block; uiBlock *block = panel->runtime->block;
BLI_assert(block != nullptr); BLI_assert(block != nullptr);
BLI_assert(block->active); BLI_assert(block->active);
if (parent_panel != nullptr && UI_panel_is_closed(parent_panel)) { if (parent_panel != nullptr && UI_panel_is_closed(parent_panel)) {
@@ -948,7 +939,7 @@ static void panel_remove_invisible_layouts_recursive(Panel *panel, const Panel *
LISTBASE_FOREACH (Panel *, child_panel, &panel->children) { LISTBASE_FOREACH (Panel *, child_panel, &panel->children) {
if (child_panel->runtime_flag & PANEL_ACTIVE) { if (child_panel->runtime_flag & PANEL_ACTIVE) {
BLI_assert(child_panel->runtime.block != nullptr); BLI_assert(child_panel->runtime->block != nullptr);
panel_remove_invisible_layouts_recursive(child_panel, panel); panel_remove_invisible_layouts_recursive(child_panel, panel);
} }
} }
@@ -958,7 +949,7 @@ static void region_panels_remove_invisible_layouts(ARegion *region)
{ {
LISTBASE_FOREACH (Panel *, panel, &region->panels) { LISTBASE_FOREACH (Panel *, panel, &region->panels) {
if (panel->runtime_flag & PANEL_ACTIVE) { if (panel->runtime_flag & PANEL_ACTIVE) {
BLI_assert(panel->runtime.block != nullptr); BLI_assert(panel->runtime->block != nullptr);
panel_remove_invisible_layouts_recursive(panel, nullptr); panel_remove_invisible_layouts_recursive(panel, nullptr);
} }
} }
@@ -1058,7 +1049,7 @@ static void panel_draw_highlight_border(const Panel *panel,
} }
const bTheme *btheme = UI_GetTheme(); const bTheme *btheme = UI_GetTheme();
const float aspect = panel->runtime.block->aspect; const float aspect = panel->runtime->block->aspect;
const float radius = (btheme->tui.panel_roundness * U.widget_unit * 0.5f) / aspect; const float radius = (btheme->tui.panel_roundness * U.widget_unit * 0.5f) / aspect;
UI_draw_roundbox_corner_set(UI_CNR_ALL); UI_draw_roundbox_corner_set(UI_CNR_ALL);
@@ -1177,7 +1168,7 @@ static void panel_draw_aligned_backdrop(const Panel *panel,
} }
const bTheme *btheme = UI_GetTheme(); const bTheme *btheme = UI_GetTheme();
const float aspect = panel->runtime.block->aspect; const float aspect = panel->runtime->block->aspect;
const float radius = btheme->tui.panel_roundness * U.widget_unit * 0.5f / aspect; const float radius = btheme->tui.panel_roundness * U.widget_unit * 0.5f / aspect;
immBindBuiltinProgram(GPU_SHADER_3D_UNIFORM_COLOR); immBindBuiltinProgram(GPU_SHADER_3D_UNIFORM_COLOR);
@@ -1669,7 +1660,7 @@ static bool uiAlignPanelStep(ARegion *region, const float factor, const bool dra
for (int i = 0; i < active_panels_len; i++) { for (int i = 0; i < active_panels_len; i++) {
PanelSort *ps = &panel_sort[i]; PanelSort *ps = &panel_sort[i];
const bool show_background = UI_panel_should_show_background(region, ps->panel->type); const bool show_background = UI_panel_should_show_background(region, ps->panel->type);
ps->panel->runtime.region_ofsx = region_offset_x; ps->panel->runtime->region_ofsx = region_offset_x;
ps->new_offset_x = region_offset_x + (show_background ? UI_PANEL_MARGIN_X : 0); ps->new_offset_x = region_offset_x + (show_background ? UI_PANEL_MARGIN_X : 0);
} }
@@ -1828,7 +1819,7 @@ void UI_panels_end(const bContext *C, ARegion *region, int *r_x, int *r_y)
LISTBASE_FOREACH (Panel *, panel, &region->panels) { LISTBASE_FOREACH (Panel *, panel, &region->panels) {
if (panel->runtime_flag & PANEL_ACTIVE) { if (panel->runtime_flag & PANEL_ACTIVE) {
BLI_assert(panel->runtime.block != nullptr); BLI_assert(panel->runtime->block != nullptr);
panel_calculate_size_recursive(region, panel); panel_calculate_size_recursive(region, panel);
} }
} }
@@ -2400,7 +2391,7 @@ int ui_handler_panel_region(bContext *C,
static void ui_panel_custom_data_set_recursive(Panel *panel, PointerRNA *custom_data) static void ui_panel_custom_data_set_recursive(Panel *panel, PointerRNA *custom_data)
{ {
panel->runtime.custom_data_ptr = custom_data; panel->runtime->custom_data_ptr = custom_data;
LISTBASE_FOREACH (Panel *, child_panel, &panel->children) { LISTBASE_FOREACH (Panel *, child_panel, &panel->children) {
ui_panel_custom_data_set_recursive(child_panel, custom_data); ui_panel_custom_data_set_recursive(child_panel, custom_data);
@@ -2410,7 +2401,7 @@ static void ui_panel_custom_data_set_recursive(Panel *panel, PointerRNA *custom_
void UI_panel_context_pointer_set(Panel *panel, const char *name, PointerRNA *ptr) void UI_panel_context_pointer_set(Panel *panel, const char *name, PointerRNA *ptr)
{ {
uiLayoutSetContextPointer(panel->layout, name, ptr); uiLayoutSetContextPointer(panel->layout, name, ptr);
panel->runtime.context = uiLayoutGetContextStore(panel->layout); panel->runtime->context = uiLayoutGetContextStore(panel->layout);
} }
void UI_panel_custom_data_set(Panel *panel, PointerRNA *custom_data) void UI_panel_custom_data_set(Panel *panel, PointerRNA *custom_data)
@@ -2418,8 +2409,8 @@ void UI_panel_custom_data_set(Panel *panel, PointerRNA *custom_data)
BLI_assert(panel->type != nullptr); BLI_assert(panel->type != nullptr);
/* Free the old custom data, which should be shared among all of the panel's sub-panels. */ /* Free the old custom data, which should be shared among all of the panel's sub-panels. */
if (panel->runtime.custom_data_ptr != nullptr) { if (panel->runtime->custom_data_ptr != nullptr) {
MEM_freeN(panel->runtime.custom_data_ptr); MEM_freeN(panel->runtime->custom_data_ptr);
} }
ui_panel_custom_data_set_recursive(panel, custom_data); ui_panel_custom_data_set_recursive(panel, custom_data);
@@ -2427,7 +2418,7 @@ void UI_panel_custom_data_set(Panel *panel, PointerRNA *custom_data)
PointerRNA *UI_panel_custom_data_get(const Panel *panel) PointerRNA *UI_panel_custom_data_get(const Panel *panel)
{ {
return panel->runtime.custom_data_ptr; return panel->runtime->custom_data_ptr;
} }
PointerRNA *UI_region_panel_custom_data_under_cursor(const bContext *C, const wmEvent *event) PointerRNA *UI_region_panel_custom_data_under_cursor(const bContext *C, const wmEvent *event)

View File

@@ -172,7 +172,7 @@ typedef struct Panel {
/** Sub panels. */ /** Sub panels. */
ListBase children; ListBase children;
Panel_Runtime runtime; Panel_Runtime *runtime;
} Panel; } Panel;
/** /**