Blender V5.3
editfont_undo.cc
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2023 Blender Authors
2 *
3 * SPDX-License-Identifier: GPL-2.0-or-later */
4
8
9#include <cstdlib>
10#include <cstring>
11
12#include "MEM_guardedalloc.h"
13
14#include "CLG_log.h"
15
16#include "BLI_array_utils_c.hh" /* For #BLI_array_is_zeroed. */
17#include "BLI_utildefines.hh"
18
19#include "DNA_curve_types.h"
20#include "DNA_object_types.h"
21
22#include "BKE_context.hh"
23#include "BKE_layer.hh"
24#include "BKE_main.hh"
25#include "BKE_undo_system.hh"
26#include "BKE_vfont.hh"
27
28#include "DEG_depsgraph.hh"
29
30#include "ED_curve.hh"
31#include "ED_undo.hh"
32
33#include "WM_api.hh"
34#include "WM_types.hh"
35
36#define USE_ARRAY_STORE
37
38#ifdef USE_ARRAY_STORE
39// # define DEBUG_PRINT
40# include "BLI_array_store.hh"
42# include "BLI_listbase.hh"
43# define ARRAY_CHUNK_SIZE 32
44#endif
45
46namespace blender {
47
49static CLG_LogRef LOG = {"undo.font"};
50
51/* -------------------------------------------------------------------- */
54
55struct UndoFont {
56 char32_t *textbuf;
58
60
61#ifdef USE_ARRAY_STORE
66#endif
67
68 size_t undo_size;
69};
70
72
73#ifdef USE_ARRAY_STORE
74
75/* -------------------------------------------------------------------- */
78
79static struct {
81 int users;
82
83 /* We could have the undo API pass in the previous state, for now store a local list */
84 ListBaseT<LinkData> local_links;
85
86} uf_arraystore = {{nullptr}};
87
93static void uf_arraystore_compact_ex(UndoFont *uf, const UndoFont *uf_ref, bool create)
94{
95# define STATE_COMPACT(uf, id, len) \
96 if ((uf)->id) { \
97 BLI_assert(create == ((uf)->store.id == nullptr)); \
98 if (create) { \
99 const BArrayState *state_reference = uf_ref ? uf_ref->store.id : nullptr; \
100 const size_t stride = sizeof(*(uf)->id); \
101 BArrayStore *bs = BLI_array_store_at_size_ensure( \
102 &uf_arraystore.bs_stride, stride, ARRAY_CHUNK_SIZE); \
103 (uf)->store.id = BLI_array_store_state_add( \
104 bs, (uf)->id, size_t(len) * stride, state_reference); \
105 } \
106 /* keep uf->len for validation */ \
107 MEM_delete((uf)->id); \
108 (uf)->id = nullptr; \
109 } \
110 ((void)0)
111
112 STATE_COMPACT(uf, textbuf, uf->len + 1);
113 STATE_COMPACT(uf, textbufinfo, uf->len + 1);
114
115# undef STATE_COMPACT
116
117 if (create) {
118 uf_arraystore.users += 1;
119 }
120}
121
125static void uf_arraystore_compact(UndoFont *um, const UndoFont *uf_ref)
126{
127 uf_arraystore_compact_ex(um, uf_ref, true);
128}
129
130static void uf_arraystore_compact_with_info(UndoFont *um, const UndoFont *uf_ref)
131{
132# ifdef DEBUG_PRINT
133 size_t size_expanded_prev, size_compacted_prev;
135 &uf_arraystore.bs_stride, &size_expanded_prev, &size_compacted_prev);
136# endif
137
138 uf_arraystore_compact(um, uf_ref);
139
140# ifdef DEBUG_PRINT
141 {
142 size_t size_expanded, size_compacted;
144 &uf_arraystore.bs_stride, &size_expanded, &size_compacted);
145
146 const double percent_total = size_expanded ?
147 ((double(size_compacted) / double(size_expanded)) * 100.0) :
148 -1.0;
149
150 size_t size_expanded_step = size_expanded - size_expanded_prev;
151 size_t size_compacted_step = size_compacted - size_compacted_prev;
152 const double percent_step = size_expanded_step ?
153 ((double(size_compacted_step) / double(size_expanded_step)) *
154 100.0) :
155 -1.0;
156
157 printf("overall memory use: %.8f%% of expanded size\n", percent_total);
158 printf("step memory use: %.8f%% of expanded size\n", percent_step);
159 }
160# endif
161}
162
167{
168 uf_arraystore_compact_ex(um, nullptr, false);
169}
170
172{
173# define STATE_EXPAND(uf, id, len) \
174 if ((uf)->store.id) { \
175 const size_t stride = sizeof(*(uf)->id); \
176 const BArrayState *state = (uf)->store.id; \
177 size_t state_len; \
178 *(void **)&(uf)->id = BLI_array_store_state_data_get_alloc(state, &state_len); \
179 BLI_assert((len) == (state_len / stride)); \
180 UNUSED_VARS_NDEBUG(stride); \
181 } \
182 ((void)0)
183
184 STATE_EXPAND(uf, textbuf, uf->len + 1);
185 STATE_EXPAND(uf, textbufinfo, uf->len + 1);
186
187# undef STATE_EXPAND
188}
189
191{
192# define STATE_FREE(uf, id) \
193 if ((uf)->store.id) { \
194 const size_t stride = sizeof(*(uf)->id); \
195 BArrayStore *bs = BLI_array_store_at_size_get(&uf_arraystore.bs_stride, stride); \
196 BArrayState *state = (uf)->store.id; \
197 BLI_array_store_state_remove(bs, state); \
198 (uf)->store.id = nullptr; \
199 } \
200 ((void)0)
201
202 STATE_FREE(uf, textbuf);
203 STATE_FREE(uf, textbufinfo);
204
205# undef STATE_FREE
206
207 uf_arraystore.users -= 1;
208
209 BLI_assert(uf_arraystore.users >= 0);
210
211 if (uf_arraystore.users == 0) {
212# ifdef DEBUG_PRINT
213 printf("editfont undo store: freeing all data!\n");
214# endif
215
217 }
218}
219
221
222#endif /* USE_ARRAY_STORE */
223
224/* -------------------------------------------------------------------- */
227
229{
230 EditFont *ef = cu->editfont;
231
232 size_t final_size;
233
234#ifdef USE_ARRAY_STORE
236#endif
237
238 final_size = sizeof(*ef->textbuf) * (uf->len + 1);
239 memcpy(ef->textbuf, uf->textbuf, final_size);
240
241 final_size = sizeof(CharInfo) * (uf->len + 1);
242 memcpy(ef->textbufinfo, uf->textbufinfo, final_size);
243
244 ef->pos = uf->pos;
245 ef->selstart = uf->selstart;
246 ef->selend = uf->selend;
247 ef->len = uf->len;
248
249#ifdef USE_ARRAY_STORE
251#endif
252}
253
255{
257
258 const EditFont *ef = cu->editfont;
259
260 size_t mem_used_prev = MEM_get_memory_in_use();
261
262 size_t alloc_len = ef->len + 1;
263
264 BLI_assert(sizeof(*uf->textbuf) == sizeof(*ef->textbuf));
265 uf->textbuf = MEM_new_array_uninitialized<char32_t>(alloc_len, __func__);
266 memcpy(uf->textbuf, ef->textbuf, sizeof(char32_t) * alloc_len);
267
268 uf->textbufinfo = MEM_new_array<CharInfo>(alloc_len, __func__);
269 memcpy(uf->textbufinfo, ef->textbufinfo, sizeof(CharInfo) * alloc_len);
270
271 uf->pos = ef->pos;
272 uf->selstart = ef->selstart;
273 uf->selend = ef->selend;
274 uf->len = ef->len;
275
276#ifdef USE_ARRAY_STORE
277 {
278 const UndoFont *uf_ref = static_cast<const UndoFont *>(
279 uf_arraystore.local_links.last() ? (uf_arraystore.local_links.last())->data : nullptr);
280
281 /* Add ourselves. */
282 BLI_addtail(&uf_arraystore.local_links, BLI_genericNodeN(uf));
283
285 }
286#endif
287
288 size_t mem_used_curr = MEM_get_memory_in_use();
289
290 uf->undo_size = mem_used_prev < mem_used_curr ? mem_used_curr - mem_used_prev : sizeof(UndoFont);
291
292 return uf;
293}
294
296{
297#ifdef USE_ARRAY_STORE
298 {
299 LinkData *link = static_cast<LinkData *>(
300 BLI_findptr(&uf_arraystore.local_links, uf, offsetof(LinkData, data)));
301 BLI_remlink(&uf_arraystore.local_links, link);
302 MEM_delete(link);
303 }
305#endif
306
307 if (uf->textbuf) {
308 MEM_delete(uf->textbuf);
309 }
310 if (uf->textbufinfo) {
311 MEM_delete(uf->textbufinfo);
312 }
313}
314
316{
317 const Main *bmain = CTX_data_main(C);
318 Scene *scene = CTX_data_scene(C);
319 ViewLayer *view_layer = CTX_data_view_layer(C);
320 BKE_view_layer_synced_ensure(*bmain, scene, view_layer);
321 Object *obedit = BKE_view_layer_edit_object_get(view_layer);
322 if (obedit && obedit->type == OB_FONT) {
323 const Curve *cu = id_cast<Curve *>(obedit->data);
324 const EditFont *ef = cu->editfont;
325 if (ef != nullptr) {
326 return obedit;
327 }
328 }
329 return nullptr;
330}
331
333
334/* -------------------------------------------------------------------- */
337
341 UndoRefID_Scene scene_ref;
342 /* NOTE: will split out into list for multi-object-editmode. */
343 UndoRefID_Object obedit_ref;
345};
346
348{
349 return editfont_object_from_context(C) != nullptr;
350}
351
352static bool font_undosys_step_encode(bContext *C, Main *bmain, UndoStep *us_p)
353{
354 FontUndoStep *us = reinterpret_cast<FontUndoStep *>(us_p);
355 us->scene_ref.ptr = CTX_data_scene(C);
357 Curve *cu = id_cast<Curve *>(us->obedit_ref.ptr->data);
359 us->step.data_size = us->data.undo_size;
361 bmain->is_memfile_undo_flush_needed = true;
362
363 return true;
364}
365
367 bContext *C, Main *bmain, UndoStep *us_p, const eUndoStepDir /*dir*/, bool /*is_final*/)
368{
369
370 FontUndoStep *us = reinterpret_cast<FontUndoStep *>(us_p);
371 Object *obedit = us->obedit_ref.ptr;
372 Scene *scene = CTX_data_scene(C);
373 ViewLayer *view_layer = CTX_data_view_layer(C);
374
375 /* Pass in an array of 1 (typically used for multi-object edit-mode). */
377 CTX_wm_manager(C), us->scene_ref.ptr, &scene, &view_layer);
378 ED_undo_object_editmode_restore_helper(scene, view_layer, &obedit, 1, sizeof(Object *));
379
380 Curve *cu = id_cast<Curve *>(obedit->data);
381 undofont_to_editfont(&us->data, cu);
383
384 ED_undo_object_set_active_or_warn(*bmain, scene, view_layer, obedit, us_p->name, &LOG);
385
386 /* Check after setting active (unless undoing into another scene). */
388
390 bmain->is_memfile_undo_flush_needed = true;
392}
393
395{
396 FontUndoStep *us = reinterpret_cast<FontUndoStep *>(us_p);
398}
399
401 UndoTypeForEachIDRefFn foreach_ID_ref_fn,
402 void *user_data)
403{
404 FontUndoStep *us = reinterpret_cast<FontUndoStep *>(us_p);
405 foreach_ID_ref_fn(user_data, (reinterpret_cast<UndoRefID *>(&us->scene_ref)));
406 foreach_ID_ref_fn(user_data, (reinterpret_cast<UndoRefID *>(&us->obedit_ref)));
407}
408
423
425
426} // namespace blender
Efficient in-memory storage of multiple similar arrays.
Generic array manipulation API.
#define BLI_array_is_zeroed(arr, arr_len)
Object is a sort of wrapper for general info.
Read Blender memory allocation and freeing.
#define C
Definition RandGen.cpp:29
#define NC_GEOM
Definition WM_types.hh:406
#define ND_DATA
Definition WM_types.hh:552
#define offsetof(t, d)
#define STATE_COMPACT(uf, id, len)
#define STATE_FREE(uf, id)
#define STATE_EXPAND(uf, id, len)
BMesh Iterators.
void * BLI_findptr(const ListBase *listbase, const void *ptr, int offset) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL(1)
Definition listbase.cc:710
BLI_assert(slot->slot_subtype.map==BMO_OP_SLOT_SUBTYPE_MAP_FLT)
void ED_font_undosys_type(UndoType *ut)
static void uf_arraystore_compact_ex(UndoFont *uf, const UndoFont *uf_ref, bool create)
void * MEM_new_array_uninitialized(size_t len, size_t size, const char *str) ATTR_WARN_UNUSED_RESULT ATTR_ALLOC_SIZE(1
@ UNDOTYPE_FLAG_NEED_CONTEXT_FOR_ENCODE
@ UNDOTYPE_FLAG_ENCODE_PRE_MEMFILE_SUPPORTED
static void uf_arraystore_expand_clear(UndoFont *um)
void ED_undo_object_set_active_or_warn(const Main &bmain, Scene *scene, ViewLayer *view_layer, Object *ob, const char *info, CLG_LogRef *log)
Definition ed_undo.cc:1024
static bool font_undosys_step_encode(bContext *C, Main *bmain, UndoStep *us_p)
static void uf_arraystore_expand(UndoFont *uf)
static void font_undosys_foreach_ID_ref(UndoStep *us_p, UndoTypeForEachIDRefFn foreach_ID_ref_fn, void *user_data)
ViewLayer * CTX_data_view_layer(const bContext *C)
size_t(*) MEM_get_memory_in_use()
void BLI_array_store_at_size_calc_memory_usage(const struct BArrayStore_AtSize *bs_stride, size_t *r_size_expanded, size_t *r_size_compacted)
static void uf_arraystore_compact(UndoFont *um, const UndoFont *uf_ref)
static void undofont_to_editfont(UndoFont *uf, Curve *cu)
void BLI_remlink(ListBase *listbase, void *vlink) ATTR_NONNULL(1)
Definition listbase.cc:138
Object * BKE_view_layer_edit_object_get(const ViewLayer *view_layer)
void DEG_id_tag_update(ID *id, unsigned int flags)
BMesh const char void * data
wmWindowManager * CTX_wm_manager(const bContext *C)
static void undofont_free_data(UndoFont *uf)
bool BKE_view_layer_synced_ensure(const Main &bmain, const Scene *scene, ViewLayer *view_layer)
LinkData * BLI_genericNodeN(void *data)
Definition listbase.cc:936
static Object * editfont_object_from_context(bContext *C)
void WM_event_add_notifier(const bContext *C, uint type, void *reference)
static void uf_arraystore_free(UndoFont *uf)
static void uf_arraystore_compact_with_info(UndoFont *um, const UndoFont *uf_ref)
static void * undofont_from_editfont(UndoFont *uf, Curve *cu)
static void font_undosys_step_free(UndoStep *us_p)
void BLI_array_store_at_size_clear(struct BArrayStore_AtSize *bs_stride)
void BLI_addtail(ListBase *listbase, void *vlink) ATTR_NONNULL(1)
Definition listbase.cc:118
Main * CTX_data_main(const bContext *C)
@ ID_RECALC_GEOMETRY
Definition DNA_ID.h:1074
static CLG_LogRef LOG
Definition logging.cpp:12
void ED_undo_object_editmode_restore_helper(Scene *scene, ViewLayer *view_layer, Object **object_array, uint object_array_len, uint object_array_stride)
Definition ed_undo.cc:1064
void(*)(void *user_data, UndoRefID *id_ref) UndoTypeForEachIDRefFn
Scene * CTX_data_scene(const bContext *C)
static struct blender::@242310203126017303145171221064117234336261275223 uf_arraystore
static void font_undosys_step_decode(bContext *C, Main *bmain, UndoStep *us_p, const eUndoStepDir, bool)
static bool font_undosys_poll(bContext *C)
void ED_undo_object_editmode_validate_scene_from_windows(wmWindowManager *wm, const Scene *scene_ref, Scene **scene_p, ViewLayer **view_layer_p)
Definition ed_undo.cc:1047
struct EditFont * editfont
CharInfo * textbufinfo
Definition BKE_vfont.hh:48
char32_t * textbuf
Definition BKE_vfont.hh:46
UndoRefID_Object obedit_ref
UndoRefID_Scene scene_ref
bool is_memfile_undo_flush_needed
Definition BKE_main.hh:302
struct blender::UndoFont::@117034065220130327136050171372377364323132157076 store
CharInfo * textbufinfo
bool(*) poll(bContext *C)
void(*) step_free(UndoStep *us)
void(*) step_decode(bContext *C, Main *bmain, UndoStep *us, eUndoStepDir dir, bool is_final)
void(*) step_foreach_ID_ref(UndoStep *us, UndoTypeForEachIDRefFn foreach_ID_ref_fn, void *user_data)
bool(*) step_encode(bContext *C, Main *bmain, UndoStep *us)
const char * identifier
BArrayStore_AtSize bs_stride
ListBaseT< LinkData > local_links