Fix T61272: Undo fails to track multi-edit mode enter/exit
[blender.git] / source / blender / editors / mesh / editmesh_undo.c
1 /*
2  * This program is free software; you can redistribute it and/or
3  * modify it under the terms of the GNU General Public License
4  * as published by the Free Software Foundation; either version 2
5  * of the License, or (at your option) any later version.
6  *
7  * This program is distributed in the hope that it will be useful,
8  * but WITHOUT ANY WARRANTY; without even the implied warranty of
9  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
10  * GNU General Public License for more details.
11  *
12  * You should have received a copy of the GNU General Public License
13  * along with this program; if not, write to the Free Software Foundation,
14  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
15  */
16
17 /** \file \ingroup edmesh
18  */
19
20 #include "MEM_guardedalloc.h"
21
22 #include "CLG_log.h"
23
24 #include "DNA_mesh_types.h"
25 #include "DNA_meshdata_types.h"
26 #include "DNA_object_types.h"
27 #include "DNA_key_types.h"
28 #include "DNA_layer_types.h"
29
30 #include "BLI_listbase.h"
31 #include "BLI_array_utils.h"
32
33 #include "BKE_context.h"
34 #include "BKE_key.h"
35 #include "BKE_layer.h"
36 #include "BKE_mesh.h"
37 #include "BKE_editmesh.h"
38 #include "BKE_undo_system.h"
39
40 #include "DEG_depsgraph.h"
41
42 #include "ED_object.h"
43 #include "ED_mesh.h"
44 #include "ED_util.h"
45 #include "ED_undo.h"
46
47 #include "WM_types.h"
48 #include "WM_api.h"
49
50 #define USE_ARRAY_STORE
51
52 #ifdef USE_ARRAY_STORE
53 // #  define DEBUG_PRINT
54 // #  define DEBUG_TIME
55 #  ifdef DEBUG_TIME
56 #    include "PIL_time_utildefines.h"
57 #  endif
58
59 #  include "BLI_array_store.h"
60 #  include "BLI_array_store_utils.h"
61    /* check on best size later... */
62 #  define ARRAY_CHUNK_SIZE 256
63
64 #  define USE_ARRAY_STORE_THREAD
65 #endif
66
67 #ifdef USE_ARRAY_STORE_THREAD
68 #  include "BLI_task.h"
69 #endif
70
71 /** We only need this locally. */
72 static CLG_LogRef LOG = {"ed.undo.mesh"};
73
74 /* -------------------------------------------------------------------- */
75 /** \name Undo Conversion
76  * \{ */
77
78 #ifdef USE_ARRAY_STORE
79
80 /* Single linked list of layers stored per type */
81 typedef struct BArrayCustomData {
82         struct BArrayCustomData *next;
83         CustomDataType type;
84         int states_len;  /* number of layers for each type */
85         BArrayState *states[0];
86 } BArrayCustomData;
87
88 #endif
89
90 typedef struct UndoMesh {
91         Mesh me;
92         int selectmode;
93
94         /** \note
95          * this isn't a prefect solution, if you edit keys and change shapes this works well (fixing [#32442]),
96          * but editing shape keys, going into object mode, removing or changing their order,
97          * then go back into editmode and undo will give issues - where the old index will be out of sync
98          * with the new object index.
99          *
100          * There are a few ways this could be made to work but for now its a known limitation with mixing
101          * object and editmode operations - Campbell */
102         int shapenr;
103
104 #ifdef USE_ARRAY_STORE
105         /* NULL arrays are considered empty */
106         struct { /* most data is stored as 'custom' data */
107                 BArrayCustomData *vdata, *edata, *ldata, *pdata;
108                 BArrayState **keyblocks;
109                 BArrayState *mselect;
110         } store;
111 #endif  /* USE_ARRAY_STORE */
112
113         size_t undo_size;
114 } UndoMesh;
115
116
117 #ifdef USE_ARRAY_STORE
118
119 /** \name Array Store
120  * \{ */
121
122 static struct {
123         struct BArrayStore_AtSize bs_stride;
124         int users;
125
126         /* We could have the undo API pass in the previous state, for now store a local list */
127         ListBase local_links;
128
129 #ifdef USE_ARRAY_STORE_THREAD
130                 TaskPool *task_pool;
131 #endif
132
133 } um_arraystore = {{NULL}};
134
135 static void um_arraystore_cd_compact(
136         struct CustomData *cdata, const size_t data_len,
137         bool create,
138         const BArrayCustomData *bcd_reference,
139         BArrayCustomData **r_bcd_first)
140 {
141         if (data_len == 0) {
142                 if (create) {
143                         *r_bcd_first = NULL;
144                 }
145         }
146
147         const BArrayCustomData *bcd_reference_current = bcd_reference;
148         BArrayCustomData *bcd = NULL, *bcd_first = NULL, *bcd_prev = NULL;
149         for (int layer_start = 0, layer_end; layer_start < cdata->totlayer; layer_start = layer_end) {
150                 const CustomDataType type = cdata->layers[layer_start].type;
151
152                 layer_end = layer_start + 1;
153                 while ((layer_end < cdata->totlayer) &&
154                        (type == cdata->layers[layer_end].type))
155                 {
156                         layer_end++;
157                 }
158
159                 const int stride = CustomData_sizeof(type);
160                 BArrayStore *bs = create ? BLI_array_store_at_size_ensure(&um_arraystore.bs_stride, stride, ARRAY_CHUNK_SIZE) : NULL;
161                 const int layer_len = layer_end - layer_start;
162
163                 if (create) {
164                         if (bcd_reference_current && (bcd_reference_current->type == type)) {
165                                 /* common case, the reference is aligned */
166                         }
167                         else {
168                                 bcd_reference_current = NULL;
169
170                                 /* do a full lookup when un-alligned */
171                                 if (bcd_reference) {
172                                         const BArrayCustomData *bcd_iter = bcd_reference;
173                                         while (bcd_iter) {
174                                                 if (bcd_iter->type == type) {
175                                                         bcd_reference_current = bcd_iter;
176                                                         break;
177                                                 }
178                                                 bcd_iter = bcd_iter->next;
179                                         }
180                                 }
181                         }
182                 }
183
184                 if (create) {
185                         bcd = MEM_callocN(sizeof(BArrayCustomData) + (layer_len * sizeof(BArrayState *)), __func__);
186                         bcd->next = NULL;
187                         bcd->type = type;
188                         bcd->states_len = layer_end - layer_start;
189
190                         if (bcd_prev) {
191                                 bcd_prev->next = bcd;
192                                 bcd_prev = bcd;
193                         }
194                         else {
195                                 bcd_first = bcd;
196                                 bcd_prev  = bcd;
197                         }
198                 }
199
200                 CustomDataLayer *layer = &cdata->layers[layer_start];
201                 for (int i = 0; i < layer_len; i++, layer++) {
202                         if (create) {
203                                 if (layer->data) {
204                                         BArrayState *state_reference =
205                                                 (bcd_reference_current && i < bcd_reference_current->states_len) ?
206                                                  bcd_reference_current->states[i] : NULL;
207                                         bcd->states[i] = BLI_array_store_state_add(
208                                                 bs, layer->data, (size_t)data_len * stride, state_reference);
209                                 }
210                                 else {
211                                         bcd->states[i] = NULL;
212                                 }
213                         }
214
215                         if (layer->data) {
216                                 MEM_freeN(layer->data);
217                                 layer->data = NULL;
218                         }
219                 }
220
221                 if (create) {
222                         if (bcd_reference_current) {
223                                 bcd_reference_current = bcd_reference_current->next;
224                         }
225                 }
226         }
227
228         if (create) {
229                 *r_bcd_first = bcd_first;
230         }
231 }
232
233 /**
234  * \note There is no room for data going out of sync here.
235  * The layers and the states are stored together so this can be kept working.
236  */
237 static void um_arraystore_cd_expand(
238         const BArrayCustomData *bcd, struct CustomData *cdata, const size_t data_len)
239 {
240         CustomDataLayer *layer = cdata->layers;
241         while (bcd) {
242                 const int stride = CustomData_sizeof(bcd->type);
243                 for (int i = 0; i < bcd->states_len; i++) {
244                         BLI_assert(bcd->type == layer->type);
245                         if (bcd->states[i]) {
246                                 size_t state_len;
247                                 layer->data = BLI_array_store_state_data_get_alloc(bcd->states[i], &state_len);
248                                 BLI_assert(stride * data_len == state_len);
249                                 UNUSED_VARS_NDEBUG(stride, data_len);
250                         }
251                         else {
252                                 layer->data = NULL;
253                         }
254                         layer++;
255                 }
256                 bcd = bcd->next;
257         }
258 }
259
260 static void um_arraystore_cd_free(BArrayCustomData *bcd)
261 {
262         while (bcd) {
263                 BArrayCustomData *bcd_next = bcd->next;
264                 const int stride = CustomData_sizeof(bcd->type);
265                 BArrayStore *bs = BLI_array_store_at_size_get(&um_arraystore.bs_stride, stride);
266                 for (int i = 0; i < bcd->states_len; i++) {
267                         if (bcd->states[i]) {
268                                 BLI_array_store_state_remove(bs, bcd->states[i]);
269                         }
270                 }
271                 MEM_freeN(bcd);
272                 bcd = bcd_next;
273         }
274 }
275
276 /**
277  * \param create: When false, only free the arrays.
278  * This is done since when reading from an undo state, they must be temporarily expanded.
279  * then discarded afterwards, having this argument avoids having 2x code paths.
280  */
281 static void um_arraystore_compact_ex(
282         UndoMesh *um, const UndoMesh *um_ref,
283         bool create)
284 {
285         Mesh *me = &um->me;
286
287         um_arraystore_cd_compact(&me->vdata, me->totvert, create, um_ref ? um_ref->store.vdata : NULL, &um->store.vdata);
288         um_arraystore_cd_compact(&me->edata, me->totedge, create, um_ref ? um_ref->store.edata : NULL, &um->store.edata);
289         um_arraystore_cd_compact(&me->ldata, me->totloop, create, um_ref ? um_ref->store.ldata : NULL, &um->store.ldata);
290         um_arraystore_cd_compact(&me->pdata, me->totpoly, create, um_ref ? um_ref->store.pdata : NULL, &um->store.pdata);
291
292         if (me->key && me->key->totkey) {
293                 const size_t stride = me->key->elemsize;
294                 BArrayStore *bs = create ? BLI_array_store_at_size_ensure(&um_arraystore.bs_stride, stride, ARRAY_CHUNK_SIZE) : NULL;
295                 if (create) {
296                         um->store.keyblocks = MEM_mallocN(me->key->totkey * sizeof(*um->store.keyblocks), __func__);
297                 }
298                 KeyBlock *keyblock = me->key->block.first;
299                 for (int i = 0; i < me->key->totkey; i++, keyblock = keyblock->next) {
300                         if (create) {
301                                 BArrayState *state_reference =
302                                         (um_ref && um_ref->me.key && (i < um_ref->me.key->totkey)) ?
303                                          um_ref->store.keyblocks[i] : NULL;
304                                 um->store.keyblocks[i] = BLI_array_store_state_add(
305                                         bs, keyblock->data, (size_t)keyblock->totelem * stride,
306                                         state_reference);
307                         }
308
309                         if (keyblock->data) {
310                                 MEM_freeN(keyblock->data);
311                                 keyblock->data = NULL;
312                         }
313                 }
314         }
315
316         if (me->mselect && me->totselect) {
317                 BLI_assert(create == (um->store.mselect == NULL));
318                 if (create) {
319                         BArrayState *state_reference = um_ref ? um_ref->store.mselect : NULL;
320                         const size_t stride = sizeof(*me->mselect);
321                         BArrayStore *bs = BLI_array_store_at_size_ensure(&um_arraystore.bs_stride, stride, ARRAY_CHUNK_SIZE);
322                         um->store.mselect = BLI_array_store_state_add(
323                                 bs, me->mselect, (size_t)me->totselect * stride, state_reference);
324                 }
325
326                 /* keep me->totselect for validation */
327                 MEM_freeN(me->mselect);
328                 me->mselect = NULL;
329         }
330
331         if (create) {
332                 um_arraystore.users += 1;
333         }
334
335         BKE_mesh_update_customdata_pointers(me, false);
336 }
337
338 /**
339  * Move data from allocated arrays to de-duplicated states and clear arrays.
340  */
341 static void um_arraystore_compact(UndoMesh *um, const UndoMesh *um_ref)
342 {
343         um_arraystore_compact_ex(um, um_ref, true);
344 }
345
346 static void um_arraystore_compact_with_info(UndoMesh *um, const UndoMesh *um_ref)
347 {
348 #ifdef DEBUG_PRINT
349         size_t size_expanded_prev, size_compacted_prev;
350         BLI_array_store_at_size_calc_memory_usage(&um_arraystore.bs_stride, &size_expanded_prev, &size_compacted_prev);
351 #endif
352
353 #ifdef DEBUG_TIME
354         TIMEIT_START(mesh_undo_compact);
355 #endif
356
357         um_arraystore_compact(um, um_ref);
358
359 #ifdef DEBUG_TIME
360         TIMEIT_END(mesh_undo_compact);
361 #endif
362
363 #ifdef DEBUG_PRINT
364         {
365                 size_t size_expanded, size_compacted;
366                 BLI_array_store_at_size_calc_memory_usage(&um_arraystore.bs_stride, &size_expanded, &size_compacted);
367
368                 const double percent_total = size_expanded ?
369                         (((double)size_compacted / (double)size_expanded) * 100.0) : -1.0;
370
371                 size_t size_expanded_step = size_expanded - size_expanded_prev;
372                 size_t size_compacted_step = size_compacted - size_compacted_prev;
373                 const double percent_step = size_expanded_step ?
374                         (((double)size_compacted_step / (double)size_expanded_step) * 100.0) : -1.0;
375
376                 printf("overall memory use: %.8f%% of expanded size\n", percent_total);
377                 printf("step memory use:    %.8f%% of expanded size\n", percent_step);
378         }
379 #endif
380 }
381
382 #ifdef USE_ARRAY_STORE_THREAD
383
384 struct UMArrayData {
385         UndoMesh *um;
386         const UndoMesh *um_ref;  /* can be NULL */
387 };
388 static void um_arraystore_compact_cb(TaskPool *__restrict UNUSED(pool),
389                                      void *taskdata,
390                                      int UNUSED(threadid))
391 {
392         struct UMArrayData *um_data = taskdata;
393         um_arraystore_compact_with_info(um_data->um, um_data->um_ref);
394 }
395
396 #endif  /* USE_ARRAY_STORE_THREAD */
397
398 /**
399  * Remove data we only expanded for temporary use.
400  */
401 static void um_arraystore_expand_clear(UndoMesh *um)
402 {
403         um_arraystore_compact_ex(um, NULL, false);
404 }
405
406 static void um_arraystore_expand(UndoMesh *um)
407 {
408         Mesh *me = &um->me;
409
410         um_arraystore_cd_expand(um->store.vdata, &me->vdata, me->totvert);
411         um_arraystore_cd_expand(um->store.edata, &me->edata, me->totedge);
412         um_arraystore_cd_expand(um->store.ldata, &me->ldata, me->totloop);
413         um_arraystore_cd_expand(um->store.pdata, &me->pdata, me->totpoly);
414
415         if (um->store.keyblocks) {
416                 const size_t stride = me->key->elemsize;
417                 KeyBlock *keyblock = me->key->block.first;
418                 for (int i = 0; i < me->key->totkey; i++, keyblock = keyblock->next) {
419                         BArrayState *state = um->store.keyblocks[i];
420                         size_t state_len;
421                         keyblock->data = BLI_array_store_state_data_get_alloc(state, &state_len);
422                         BLI_assert(keyblock->totelem == (state_len / stride));
423                         UNUSED_VARS_NDEBUG(stride);
424                 }
425         }
426
427         if (um->store.mselect) {
428                 const size_t stride = sizeof(*me->mselect);
429                 BArrayState *state = um->store.mselect;
430                 size_t state_len;
431                 me->mselect = BLI_array_store_state_data_get_alloc(state, &state_len);
432                 BLI_assert(me->totselect == (state_len / stride));
433                 UNUSED_VARS_NDEBUG(stride);
434         }
435
436         /* not essential, but prevents accidental dangling pointer access */
437         BKE_mesh_update_customdata_pointers(me, false);
438 }
439
440 static void um_arraystore_free(UndoMesh *um)
441 {
442         Mesh *me = &um->me;
443
444         um_arraystore_cd_free(um->store.vdata);
445         um_arraystore_cd_free(um->store.edata);
446         um_arraystore_cd_free(um->store.ldata);
447         um_arraystore_cd_free(um->store.pdata);
448
449         if (um->store.keyblocks) {
450                 const size_t stride = me->key->elemsize;
451                 BArrayStore *bs = BLI_array_store_at_size_get(&um_arraystore.bs_stride, stride);
452                 for (int i = 0; i < me->key->totkey; i++) {
453                         BArrayState *state = um->store.keyblocks[i];
454                         BLI_array_store_state_remove(bs, state);
455                 }
456                 MEM_freeN(um->store.keyblocks);
457                 um->store.keyblocks = NULL;
458         }
459
460         if (um->store.mselect) {
461                 const size_t stride = sizeof(*me->mselect);
462                 BArrayStore *bs = BLI_array_store_at_size_get(&um_arraystore.bs_stride, stride);
463                 BArrayState *state = um->store.mselect;
464                 BLI_array_store_state_remove(bs, state);
465                 um->store.mselect = NULL;
466         }
467
468         um_arraystore.users -= 1;
469
470         BLI_assert(um_arraystore.users >= 0);
471
472         if (um_arraystore.users == 0) {
473 #ifdef DEBUG_PRINT
474                 printf("mesh undo store: freeing all data!\n");
475 #endif
476                 BLI_array_store_at_size_clear(&um_arraystore.bs_stride);
477
478 #ifdef USE_ARRAY_STORE_THREAD
479                 BLI_task_pool_free(um_arraystore.task_pool);
480                 um_arraystore.task_pool = NULL;
481 #endif
482         }
483
484 }
485
486 /** \} */
487
488 #endif  /* USE_ARRAY_STORE */
489
490
491 /* for callbacks */
492 /* undo simply makes copies of a bmesh */
493 static void *undomesh_from_editmesh(UndoMesh *um, BMEditMesh *em, Key *key)
494 {
495         BLI_assert(BLI_array_is_zeroed(um, 1));
496 #ifdef USE_ARRAY_STORE_THREAD
497         /* changes this waits is low, but must have finished */
498         if (um_arraystore.task_pool) {
499                 BLI_task_pool_work_and_wait(um_arraystore.task_pool);
500         }
501 #endif
502         /* make sure shape keys work */
503         um->me.key = key ? BKE_key_copy_nolib(key) : NULL;
504
505         /* BM_mesh_validate(em->bm); */ /* for troubleshooting */
506
507         BM_mesh_bm_to_me(
508                 NULL, em->bm, &um->me, (&(struct BMeshToMeshParams){
509                     /* Undo code should not be manipulating 'G_MAIN->object' hooks/vertex-parent. */
510                     .calc_object_remap = false,
511                     .cd_mask_extra = CD_MASK_SHAPE_KEYINDEX,
512                 }));
513
514         um->selectmode = em->selectmode;
515         um->shapenr = em->bm->shapenr;
516
517 #ifdef USE_ARRAY_STORE
518         {
519                 /* We could be more clever here,
520                  * the previous undo state may be from a separate mesh. */
521                 const UndoMesh *um_ref = um_arraystore.local_links.last ?
522                                          ((LinkData *)um_arraystore.local_links.last)->data : NULL;
523
524                 /* add oursrlves */
525                 BLI_addtail(&um_arraystore.local_links, BLI_genericNodeN(um));
526
527 #ifdef USE_ARRAY_STORE_THREAD
528                 if (um_arraystore.task_pool == NULL) {
529                         TaskScheduler *scheduler = BLI_task_scheduler_get();
530                         um_arraystore.task_pool = BLI_task_pool_create_background(scheduler, NULL);
531                 }
532
533                 struct UMArrayData *um_data = MEM_mallocN(sizeof(*um_data), __func__);
534                 um_data->um = um;
535                 um_data->um_ref = um_ref;
536
537                 BLI_task_pool_push(
538                         um_arraystore.task_pool,
539                         um_arraystore_compact_cb, um_data, true, TASK_PRIORITY_LOW);
540 #else
541                 um_arraystore_compact_with_info(um, um_ref);
542 #endif
543         }
544 #endif
545
546         return um;
547 }
548
549 static void undomesh_to_editmesh(UndoMesh *um, BMEditMesh *em, Mesh *obmesh)
550 {
551         BMEditMesh *em_tmp;
552         Object *ob = em->ob;
553         BMesh *bm;
554         Key *key = obmesh->key;
555
556 #ifdef USE_ARRAY_STORE
557 #ifdef USE_ARRAY_STORE_THREAD
558         /* changes this waits is low, but must have finished */
559         BLI_task_pool_work_and_wait(um_arraystore.task_pool);
560 #endif
561
562 #ifdef DEBUG_TIME
563         TIMEIT_START(mesh_undo_expand);
564 #endif
565
566         um_arraystore_expand(um);
567
568 #ifdef DEBUG_TIME
569         TIMEIT_END(mesh_undo_expand);
570 #endif
571 #endif  /* USE_ARRAY_STORE */
572
573         const BMAllocTemplate allocsize = BMALLOC_TEMPLATE_FROM_ME(&um->me);
574
575         em->bm->shapenr = um->shapenr;
576
577         EDBM_mesh_free(em);
578
579         bm = BM_mesh_create(
580                 &allocsize,
581                 &((struct BMeshCreateParams){.use_toolflags = true,}));
582
583         BM_mesh_bm_from_me(
584                 bm, &um->me, (&(struct BMeshFromMeshParams){
585                     .calc_face_normal = true, .active_shapekey = um->shapenr,
586                 }));
587
588         em_tmp = BKE_editmesh_create(bm, true);
589         *em = *em_tmp;
590
591         em->selectmode = um->selectmode;
592         bm->selectmode = um->selectmode;
593         em->ob = ob;
594
595         bm->spacearr_dirty = BM_SPACEARR_DIRTY_ALL;
596
597         /* T35170: Restore the active key on the RealMesh. Otherwise 'fake' offset propagation happens
598          *         if the active is a basis for any other. */
599         if (key && (key->type == KEY_RELATIVE)) {
600                 /* Since we can't add, remove or reorder keyblocks in editmode, it's safe to assume
601                  * shapenr from restored bmesh and keyblock indices are in sync. */
602                 const int kb_act_idx = ob->shapenr - 1;
603
604                 /* If it is, let's patch the current mesh key block to its restored value.
605                  * Else, the offsets won't be computed and it won't matter. */
606                 if (BKE_keyblock_is_basis(key, kb_act_idx)) {
607                         KeyBlock *kb_act = BLI_findlink(&key->block, kb_act_idx);
608
609                         if (kb_act->totelem != um->me.totvert) {
610                                 /* The current mesh has some extra/missing verts compared to the undo, adjust. */
611                                 MEM_SAFE_FREE(kb_act->data);
612                                 kb_act->data = MEM_mallocN((size_t)(key->elemsize * bm->totvert), __func__);
613                                 kb_act->totelem = um->me.totvert;
614                         }
615
616                         BKE_keyblock_update_from_mesh(&um->me, kb_act);
617                 }
618         }
619
620         ob->shapenr = um->shapenr;
621
622         MEM_freeN(em_tmp);
623
624 #ifdef USE_ARRAY_STORE
625         um_arraystore_expand_clear(um);
626 #endif
627 }
628
629 static void undomesh_free_data(UndoMesh *um)
630 {
631         Mesh *me = &um->me;
632
633 #ifdef USE_ARRAY_STORE
634
635 #ifdef USE_ARRAY_STORE_THREAD
636         /* changes this waits is low, but must have finished */
637         BLI_task_pool_work_and_wait(um_arraystore.task_pool);
638 #endif
639
640         /* we need to expand so any allocations in custom-data are freed with the mesh */
641         um_arraystore_expand(um);
642
643         {
644                 LinkData *link = BLI_findptr(&um_arraystore.local_links, um, offsetof(LinkData, data));
645                 BLI_remlink(&um_arraystore.local_links, link);
646                 MEM_freeN(link);
647         }
648         um_arraystore_free(um);
649 #endif
650
651         if (me->key) {
652                 BKE_key_free(me->key);
653                 MEM_freeN(me->key);
654         }
655
656         BKE_mesh_free(me);
657 }
658
659 static Object *editmesh_object_from_context(bContext *C)
660 {
661         Object *obedit = CTX_data_edit_object(C);
662         if (obedit && obedit->type == OB_MESH) {
663                 Mesh *me = obedit->data;
664                 if (me->edit_btmesh != NULL) {
665                         return obedit;
666                 }
667         }
668         return NULL;
669 }
670
671 /** \} */
672
673 /* -------------------------------------------------------------------- */
674 /** \name Implements ED Undo System
675  *
676  * \note This is similar for all edit-mode types.
677  * \{ */
678
679 typedef struct MeshUndoStep_Elem {
680         struct MeshUndoStep_Elem *next, *prev;
681         UndoRefID_Object obedit_ref;
682         UndoMesh data;
683 } MeshUndoStep_Elem;
684
685 typedef struct MeshUndoStep {
686         UndoStep step;
687         struct UndoIDPtrMap *id_map;
688         MeshUndoStep_Elem *elems;
689         uint               elems_len;
690 } MeshUndoStep;
691
692 static bool mesh_undosys_poll(bContext *C)
693 {
694         return editmesh_object_from_context(C) != NULL;
695 }
696
697 static bool mesh_undosys_step_encode(struct bContext *C, struct Main *UNUSED(bmain), UndoStep *us_p)
698 {
699         MeshUndoStep *us = (MeshUndoStep *)us_p;
700
701         /* Important not to use the 3D view when getting objects because all objects
702          * outside of this list will be moved out of edit-mode when reading back undo steps. */
703         ViewLayer *view_layer = CTX_data_view_layer(C);
704         uint objects_len = 0;
705         Object **objects = BKE_view_layer_array_from_objects_in_edit_mode_unique_data(view_layer, NULL, &objects_len);
706
707         us->elems = MEM_callocN(sizeof(*us->elems) * objects_len, __func__);
708         us->elems_len = objects_len;
709
710         for (uint i = 0; i < objects_len; i++) {
711                 Object *ob = objects[i];
712                 MeshUndoStep_Elem *elem = &us->elems[i];
713
714                 elem->obedit_ref.ptr = ob;
715                 Mesh *me = elem->obedit_ref.ptr->data;
716                 undomesh_from_editmesh(&elem->data, me->edit_btmesh, me->key);
717                 us->step.data_size += elem->data.undo_size;
718         }
719         MEM_freeN(objects);
720         return true;
721 }
722
723 static void mesh_undosys_step_decode(struct bContext *C, struct Main *UNUSED(bmain), UndoStep *us_p, int UNUSED(dir))
724 {
725         MeshUndoStep *us = (MeshUndoStep *)us_p;
726
727         /* Load all our objects  into edit-mode, clear everything else. */
728         ED_undo_object_editmode_restore_helper(C, &us->elems[0].obedit_ref.ptr, us->elems_len, sizeof(*us->elems));
729
730         BLI_assert(mesh_undosys_poll(C));
731
732         for (uint i = 0; i < us->elems_len; i++) {
733                 MeshUndoStep_Elem *elem = &us->elems[i];
734                 Object *obedit = elem->obedit_ref.ptr;
735                 Mesh *me = obedit->data;
736                 if (me->edit_btmesh == NULL) {
737                         /* Should never fail, may not crash but can give odd behavior. */
738                         CLOG_ERROR(&LOG, "name='%s', failed to enter edit-mode for object '%s', undo state invalid",
739                                    us_p->name, obedit->id.name);
740                         continue;
741                 }
742                 BMEditMesh *em = me->edit_btmesh;
743                 undomesh_to_editmesh(&elem->data, em, obedit->data);
744                 DEG_id_tag_update(&obedit->id, ID_RECALC_GEOMETRY);
745         }
746
747         /* The first element is always active */
748         ED_undo_object_set_active_or_warn(CTX_data_view_layer(C), us->elems[0].obedit_ref.ptr, us_p->name, &LOG);
749
750         WM_event_add_notifier(C, NC_GEOM | ND_DATA, NULL);
751 }
752
753 static void mesh_undosys_step_free(UndoStep *us_p)
754 {
755         MeshUndoStep *us = (MeshUndoStep *)us_p;
756
757         for (uint i = 0; i < us->elems_len; i++) {
758                 MeshUndoStep_Elem *elem = &us->elems[i];
759                 undomesh_free_data(&elem->data);
760         }
761         MEM_freeN(us->elems);
762
763         if (us->id_map != NULL) {
764                 BKE_undosys_ID_map_destroy(us->id_map);
765         }
766 }
767
768 static void mesh_undosys_foreach_ID_ref(
769         UndoStep *us_p, UndoTypeForEachIDRefFn foreach_ID_ref_fn, void *user_data)
770 {
771         MeshUndoStep *us = (MeshUndoStep *)us_p;
772
773         for (uint i = 0; i < us->elems_len; i++) {
774                 MeshUndoStep_Elem *elem = &us->elems[i];
775                 foreach_ID_ref_fn(user_data, ((UndoRefID *)&elem->obedit_ref));
776         }
777
778         if (us->id_map != NULL) {
779                 BKE_undosys_ID_map_foreach_ID_ref(us->id_map, foreach_ID_ref_fn, user_data);
780         }
781 }
782
783 /* Export for ED_undo_sys. */
784 void ED_mesh_undosys_type(UndoType *ut)
785 {
786         ut->name = "Edit Mesh";
787         ut->poll = mesh_undosys_poll;
788         ut->step_encode = mesh_undosys_step_encode;
789         ut->step_decode = mesh_undosys_step_decode;
790         ut->step_free = mesh_undosys_step_free;
791
792         ut->step_foreach_ID_ref = mesh_undosys_foreach_ID_ref;
793
794         ut->use_context = true;
795
796         ut->step_size = sizeof(MeshUndoStep);
797 }
798
799 /** \} */