Blender V5.3
image_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
27
28#include "CLG_log.h"
29
30#include "MEM_guardedalloc.h"
31
32#include "BLI_index_range.hh"
33#include "BLI_listbase.hh"
34#include "BLI_map.hh"
35#include "BLI_math_base_c.hh"
36#include "BLI_math_vector.hh"
37#include "BLI_mutex.hh"
38#include "BLI_rect.hh"
39#include "BLI_string.hh"
40#include "BLI_task.hh"
41#include "BLI_threads.hh"
42#include "BLI_utildefines.hh"
43
44#include "DNA_image_types.h"
45#include "DNA_object_types.h"
46#include "DNA_screen_types.h"
47#include "DNA_space_types.h"
48#include "DNA_userdef_types.h"
50
51#include "IMB_imbuf.hh"
52#include "IMB_imbuf_types.hh"
53#include "IMB_partial_update.hh"
54
55#include "BKE_context.hh"
56#include "BKE_image.hh"
57#include "BKE_image_gpu.hh"
58#include "BKE_paint.hh"
59#include "BKE_paint_types.hh"
60#include "BKE_undo_system.hh"
61
62#include "DEG_depsgraph.hh"
63
64#include "ED_object.hh"
65#include "ED_paint.hh"
66#include "ED_undo.hh"
67#include "ED_util.hh"
68
69#include "WM_api.hh"
70
71namespace blender {
72
73static CLG_LogRef LOG = {"undo.image"};
74
75/* -------------------------------------------------------------------- */
84
85static void calc_tile_rect(
86 const ImBuf &ibuf, const int x_tile, const int y_tile, int2 &r_tile_pos, int2 &r_tile_size)
87{
88 r_tile_pos = int2(x_tile * ED_IMAGE_UNDO_TILE_SIZE, y_tile * ED_IMAGE_UNDO_TILE_SIZE);
89 r_tile_size = math::min(int2(ibuf.x, ibuf.y), r_tile_pos + int2(ED_IMAGE_UNDO_TILE_SIZE)) -
90 r_tile_pos;
91}
92
93static rcti calc_tile_region(const ImBuf &ibuf, const int x_tile, const int y_tile)
94{
95 int2 tile_pos;
96 int2 tile_size;
97 calc_tile_rect(ibuf, x_tile, y_tile, tile_pos, tile_size);
98
99 rcti region;
101 &region, tile_pos.x, tile_pos.x + tile_size.x, tile_pos.y, tile_pos.y + tile_size.y);
102 return region;
103}
104
109 /* Copied from iuser.tile in PaintTile. */
111
113 {
115 }
116 bool operator==(const PaintTileKey &other) const
117 {
118 return x_tile == other.x_tile && y_tile == other.y_tile && image == other.image &&
119 ibuf == other.ibuf && iuser_tile == other.iuser_tile;
120 }
121};
122
123struct PaintTile {
124 Image *image = nullptr;
125 ImBuf *ibuf = nullptr;
126 /* For 2D image painting the ImageUser uses most of the values.
127 * Even though views and passes are stored they are currently not supported for painting.
128 * For 3D projection painting this only uses a tile & frame number.
129 * The scene pointer must be cleared (or temporarily set it as needed, but leave cleared). */
131 ImBuf *ptile_ibuf = nullptr;
132 uint16_t *mask = nullptr;
133 bool valid = false;
134 bool use_float = false;
135 int x_tile = 0, y_tile = 0;
139};
140
141static void ptile_free(PaintTile *ptile)
142{
144 if (ptile->mask) {
145 MEM_delete(ptile->mask);
146 }
147 MEM_delete(ptile);
148}
149
153
155 {
156 for (PaintTile *ptile : map.values()) {
157 ptile_free(ptile);
158 }
159 }
160};
161
162static void ptile_invalidate_map(PaintTileMap *paint_tile_map)
163{
164 for (PaintTile *ptile : paint_tile_map->map.values()) {
165 ptile->valid = false;
166 }
167}
168
170 Image *image,
171 ImBuf *ibuf,
172 ImageUser *iuser,
173 int x_tile,
174 int y_tile,
175 ushort **r_mask,
176 bool validate)
177{
178 PaintTileKey key;
179 key.ibuf = ibuf;
180 key.image = image;
181 key.iuser_tile = iuser->tile;
182 key.x_tile = x_tile;
183 key.y_tile = y_tile;
184 PaintTile **pptile = paint_tile_map->map.lookup_ptr(key);
185 if (pptile == nullptr) {
186 return nullptr;
187 }
188 PaintTile *ptile = *pptile;
189 if (r_mask) {
190 /* allocate mask if requested. */
191 if (!ptile->mask) {
193 "UndoImageTile.mask");
194 }
195 *r_mask = ptile->mask;
196 }
197 if (validate) {
198 ptile->valid = true;
199 }
200 return ptile->ptile_ibuf;
201}
202
204 Image *image,
205 ImBuf *ibuf,
206 ImageUser *iuser,
207 int x_tile,
208 int y_tile,
209 ushort **r_mask,
210 bool **r_valid)
211{
212 const bool has_float = (ibuf->float_data() != nullptr);
213
214 PaintTileKey key = {};
215 key.ibuf = ibuf;
216 key.image = image;
217 key.iuser_tile = iuser->tile;
218 key.x_tile = x_tile;
219 key.y_tile = y_tile;
220
221 /* Allocate the tile if it does not exist yet. */
222 PaintTile *ptile = nullptr;
223 bool created = false;
224 {
225 std::scoped_lock map_lock(paint_tile_map->mutex);
226 paint_tile_map->map.add_or_modify(
227 key,
228 [&](PaintTile **pptile) {
229 ptile = MEM_new<PaintTile>("PaintTile");
230 ptile->image = image;
231 ptile->ibuf = ibuf;
232 ptile->iuser = *iuser;
233 ptile->iuser.scene = nullptr;
234 ptile->x_tile = x_tile;
235 ptile->y_tile = y_tile;
236 ptile->use_float = has_float;
238 ptile->mutex.lock();
239 *pptile = ptile;
240 created = true;
241 },
242 [&](PaintTile **pptile) { ptile = *pptile; });
243 }
244
245 if (created) {
246 /* This thread created the tile, allocate it and copy pixels. */
247 int2 tile_pos;
248 int2 tile_copy_size;
249 calc_tile_rect(*ibuf, x_tile, y_tile, tile_pos, tile_copy_size);
250
251 ImBuf *ptile_ibuf;
252 if (ibuf->float_data()) {
258 ibuf->float_data(),
259 int2(ibuf->x, ibuf->y),
260 ibuf->channels,
261 tile_pos,
262 int2(0, 0),
263 tile_copy_size);
264 }
265 else {
271 ibuf->byte_data(),
272 int2(ibuf->x, ibuf->y),
273 tile_pos,
274 int2(0, 0),
275 tile_copy_size);
276 }
277
278 ptile->ptile_ibuf = ptile_ibuf;
279 }
280 else {
281 /* Other threads wait until the creator is finished and unlocks the mutex. */
282 ptile->mutex.lock();
283 }
284
285 /* Set up the mask on demand. */
286 ptile->valid = true;
287 if (r_mask) {
288 if (!ptile->mask) {
290 "PaintTile.mask");
291 }
292 *r_mask = ptile->mask;
293 }
294 if (r_valid) {
295 *r_valid = &ptile->valid;
296 }
297
298 ptile->mutex.unlock();
299 return ptile->ptile_ibuf;
300}
301
302static void ptile_restore_runtime_map(PaintTileMap *paint_tile_map)
303{
304 for (PaintTile *ptile : paint_tile_map->map.values()) {
305 Image *image = ptile->image;
306 ImBuf *ibuf = BKE_image_acquire_ibuf(image, &ptile->iuser, nullptr);
307
308 int2 tile_pos;
309 int2 tile_copy_size;
310 calc_tile_rect(*ibuf, ptile->x_tile, ptile->y_tile, tile_pos, tile_copy_size);
311
312 if (ibuf->float_data()) {
314 int2(ibuf->x, ibuf->y),
315 ptile->ptile_ibuf->float_data(),
317 ibuf->channels,
318 int2(0, 0),
319 tile_pos,
320 tile_copy_size);
321 }
322 else {
324 int2(ibuf->x, ibuf->y),
325 ptile->ptile_ibuf->byte_data(),
327 int2(0, 0),
328 tile_pos,
329 tile_copy_size);
330 }
331
332 IMB_partial_update_mark_region(ibuf, calc_tile_region(*ibuf, ptile->x_tile, ptile->y_tile));
333
334 BKE_image_release_ibuf(image, ibuf, nullptr);
335 }
336}
337
339
340/* -------------------------------------------------------------------- */
343
344static uint32_t index_from_xy(uint32_t tile_x, uint32_t tile_y, const uint32_t tiles_dims[2])
345{
346 BLI_assert(tile_x < tiles_dims[0] && tile_y < tiles_dims[1]);
347 return (tile_y * tiles_dims[0]) + tile_x;
348}
349
352 int users;
353};
354
355static UndoImageTile *utile_alloc(bool has_float)
356{
357 UndoImageTile *utile = MEM_new_zeroed<UndoImageTile>("ImageUndoTile");
361 return utile;
362}
363
365 const int x_tile,
366 const int y_tile,
367 const ImBuf *ibuf)
368{
369 int2 tile_pos;
370 int2 tile_copy_size;
371 calc_tile_rect(*ibuf, x_tile, y_tile, tile_pos, tile_copy_size);
372 if (ibuf->float_data()) {
375 ibuf->float_data(),
376 int2(ibuf->x, ibuf->y),
377 ibuf->channels,
378 tile_pos,
379 int2(0, 0),
380 tile_copy_size);
381 }
382 else {
385 ibuf->byte_data(),
386 int2(ibuf->x, ibuf->y),
387 tile_pos,
388 int2(0, 0),
389 tile_copy_size);
390 }
391}
392
393static void utile_restore(const UndoImageTile *utile,
394 const int x_tile,
395 const int y_tile,
396 ImBuf *ibuf)
397{
398 int2 tile_pos;
399 int2 tile_copy_size;
400 calc_tile_rect(*ibuf, x_tile, y_tile, tile_pos, tile_copy_size);
401 if (ibuf->float_data()) {
403 int2(ibuf->x, ibuf->y),
404 utile->ibuf->float_data(),
406 ibuf->channels,
407 int2(0, 0),
408 tile_pos,
409 tile_copy_size);
410 }
411 else {
413 int2(ibuf->x, ibuf->y),
414 utile->ibuf->byte_data(),
416 int2(0, 0),
417 tile_pos,
418 tile_copy_size);
419 }
420}
421
422static void utile_decref(UndoImageTile *utile)
423{
424 utile->users -= 1;
425 BLI_assert(utile->users >= 0);
426 if (utile->users == 0) {
427 IMB_freeImBuf(utile->ibuf);
428 MEM_delete(utile);
429 }
430}
431
433
434/* -------------------------------------------------------------------- */
437
439 UndoImageBuf *next = nullptr, *prev = nullptr;
440
444 UndoImageBuf *post = nullptr;
445
446 std::string ibuf_filepath;
450
456 UndoImageTile **tiles = nullptr;
457
461
463
465 struct {
466 short source;
467 bool use_float;
469};
470
472{
473 UndoImageBuf *ubuf = MEM_new<UndoImageBuf>(__func__);
474
475 ubuf->image_dims[0] = ibuf->x;
476 ubuf->image_dims[1] = ibuf->y;
477
480
481 ubuf->tiles_len = ubuf->tiles_dims[0] * ubuf->tiles_dims[1];
483
484 ubuf->ibuf_filepath = ibuf->filepath;
485 ubuf->ibuf_fileframe = ibuf->fileframe;
487 ubuf->image_state.source = image->source;
488 ubuf->image_state.use_float = ibuf->float_data() != nullptr;
489
490 return ubuf;
491}
492
493static void ubuf_from_image_all_tiles(UndoImageBuf *ubuf, const ImBuf *ibuf)
494{
495 const bool has_float = ibuf->float_data();
496 int i = 0;
497 for (uint y_tile = 0; y_tile < ubuf->tiles_dims[1]; y_tile += 1) {
498 for (uint x_tile = 0; x_tile < ubuf->tiles_dims[0]; x_tile += 1) {
499
500 BLI_assert(ubuf->tiles[i] == nullptr);
501 UndoImageTile *utile = utile_alloc(has_float);
502 utile->users = 1;
503 utile_init_from_imbuf(utile, x_tile, y_tile, ibuf);
504 ubuf->tiles[i] = utile;
505
506 i += 1;
507 }
508 }
509
510 BLI_assert(i == ubuf->tiles_len);
511}
512
517[[nodiscard]] static bool ubuf_ensure_compat_ibuf(const UndoImageBuf *ubuf, ImBuf *ibuf)
518{
519 bool replaced = false;
520
521 /* We could have both float and rect buffers,
522 * in this case free the float buffer if it's unused. */
523 if ((ibuf->float_data() != nullptr) && (ubuf->image_state.use_float == false)) {
525 replaced = true;
526 }
527
528 if (ibuf->x == ubuf->image_dims[0] && ibuf->y == ubuf->image_dims[1] &&
529 (ubuf->image_state.use_float ? static_cast<void *>(ibuf->float_data_for_write()) :
530 static_cast<void *>(ibuf->byte_data_for_write())))
531 {
532 return replaced;
533 }
534
535 IMB_free_all_data(ibuf);
536 IMB_rect_size_set(ibuf, ubuf->image_dims);
537
538 if (ubuf->image_state.use_float) {
539 IMB_alloc_float_pixels(ibuf, 4);
540 }
541 else {
543 }
544
545 return true;
546}
547
548static void ubuf_free(UndoImageBuf *ubuf)
549{
550 UndoImageBuf *ubuf_post = ubuf->post;
551 for (uint i = 0; i < ubuf->tiles_len; i++) {
552 UndoImageTile *utile = ubuf->tiles[i];
553 utile_decref(utile);
554 }
555 MEM_delete(ubuf->tiles);
556 MEM_delete(ubuf);
557 if (ubuf_post) {
558 ubuf_free(ubuf_post);
559 }
560}
561
563
564/* -------------------------------------------------------------------- */
567
570
572 UndoRefID_Image image_ref;
573
579
583 ListBaseT<UndoImageBuf> buffers;
584};
585
594
600static void uhandle_restore_list(ListBaseT<UndoImageHandle> *undo_handles,
601 const bool use_init,
603{
604 for (UndoImageHandle &uh : *undo_handles) {
605 /* Tiles only added to second set of tiles. */
606 Image *image = uh.image_ref.ptr;
607
608 ImBuf *ibuf = BKE_image_acquire_ibuf(image, &uh.iuser, nullptr);
609 if (ibuf == nullptr) [[unlikely]] {
610 CLOG_ERROR(&LOG, "Unable to get buffer for image '%s'", image->id.name + 2);
611 continue;
612 }
613 bool changed = false;
614 for (UndoImageBuf &ubuf_iter : uh.buffers) {
615 UndoImageBuf *ubuf = use_init ? &ubuf_iter : ubuf_iter.post;
616 const UndoImageBuf *ubuf_other = use_init ? ubuf_iter.post : &ubuf_iter;
617
618 /* Skip image buffers without a full update, restore all tiles of others. */
619 if (mode == RestoreMode::FullUpdateChanged &&
621 {
622 continue;
623 }
624
625 /* Check if we need to restore all because of modified image dimensions or image
626 * buffer, or if we can do a partial restore and update. */
627 const bool ibuf_reallocated = ubuf_ensure_compat_ibuf(ubuf, ibuf);
628 const bool restore_all = ibuf_reallocated || mode != RestoreMode::Changed ||
629 ubuf_other == nullptr ||
630 ubuf_other->tiles_dims[0] != ubuf->tiles_dims[0] ||
631 ubuf_other->tiles_dims[1] != ubuf->tiles_dims[1] ||
634
635 int i = 0;
636 for (uint y_tile = 0; y_tile < ubuf->tiles_dims[1]; y_tile += 1) {
637 for (uint x_tile = 0; x_tile < ubuf->tiles_dims[0]; x_tile += 1) {
638 if (restore_all || ubuf->tiles[i] != ubuf_other->tiles[i]) {
639 utile_restore(ubuf->tiles[i], x_tile, y_tile, ibuf);
640 IMB_partial_update_mark_region(ibuf, calc_tile_region(*ibuf, x_tile, y_tile));
641 changed = true;
642 }
643 i += 1;
644 }
645 }
646
647 /* Copy undo step changeset ID back to the image buffer. */
649 }
650
651 if (changed) {
652 IMB_mark_dirty(ibuf);
653
654 DEG_id_tag_update(&image->id, 0);
655 }
656 BKE_image_release_ibuf(image, ibuf, nullptr);
657 }
658}
659
660static void uhandle_free_list(ListBaseT<UndoImageHandle> *undo_handles)
661{
662 for (UndoImageHandle &uh : undo_handles->items_mutable()) {
663 for (UndoImageBuf &ubuf : uh.buffers.items_mutable()) {
664 ubuf_free(&ubuf);
665 }
666 MEM_delete(&uh);
667 }
668 undo_handles->clear_no_delete();
669}
670
672
673/* -------------------------------------------------------------------- */
676
678
680 const Image * /*image*/,
681 const StringRef ibuf_filepath,
682 const int ibuf_fileframe)
683{
684 for (UndoImageBuf &ubuf : uh->buffers) {
685 if (ubuf.ibuf_filepath == ibuf_filepath && ubuf.ibuf_fileframe == ibuf_fileframe) {
686 return &ubuf;
687 }
688 }
689 return nullptr;
690}
691
693{
694 BLI_assert(uhandle_lookup_ubuf(uh, image, ibuf->filepath, ibuf->fileframe) == nullptr);
696 BLI_addtail(&uh->buffers, ubuf);
697
698 ubuf->post = nullptr;
699
700 return ubuf;
701}
702
704{
705 UndoImageBuf *ubuf = uhandle_lookup_ubuf(uh, image, ibuf->filepath, ibuf->fileframe);
706 if (ubuf == nullptr) {
707 ubuf = uhandle_add_ubuf(uh, image, ibuf);
708 }
709 return ubuf;
710}
711
712static UndoImageHandle *uhandle_lookup_by_name(ListBaseT<UndoImageHandle> *undo_handles,
713 const Image *image,
714 int tile_number)
715{
716 for (UndoImageHandle &uh : *undo_handles) {
717 if (STREQ(image->id.name + 2, uh.image_ref.name + 2) && uh.iuser.tile == tile_number) {
718 return &uh;
719 }
720 }
721 return nullptr;
722}
723
724static UndoImageHandle *uhandle_lookup(ListBaseT<UndoImageHandle> *undo_handles,
725 const Image *image,
726 int tile_number)
727{
728 for (UndoImageHandle &uh : *undo_handles) {
729 if (image == uh.image_ref.ptr && uh.iuser.tile == tile_number) {
730 return &uh;
731 }
732 }
733 return nullptr;
734}
735
736static UndoImageHandle *uhandle_add(ListBaseT<UndoImageHandle> *undo_handles,
737 Image *image,
738 ImageUser *iuser)
739{
740 BLI_assert(uhandle_lookup(undo_handles, image, iuser->tile) == nullptr);
741 UndoImageHandle *uh = MEM_new<UndoImageHandle>(__func__);
742 uh->image_ref.ptr = image;
743 uh->iuser = *iuser;
744 uh->iuser.scene = nullptr;
745 BLI_addtail(undo_handles, uh);
746 return uh;
747}
748
749static UndoImageHandle *uhandle_ensure(ListBaseT<UndoImageHandle> *undo_handles,
750 Image *image,
751 ImageUser *iuser)
752{
753 UndoImageHandle *uh = uhandle_lookup(undo_handles, image, iuser->tile);
754 if (uh == nullptr) {
755 uh = uhandle_add(undo_handles, image, iuser);
756 }
757 return uh;
758}
759
761
762/* -------------------------------------------------------------------- */
765
780
786 const Image *image,
787 int tile_number,
788 const UndoImageBuf *ubuf)
789{
790 /* Use name lookup because the pointer is cleared for previous steps. */
791 UndoImageHandle *uh_prev = uhandle_lookup_by_name(&us_prev->handles, image, tile_number);
792 if (uh_prev != nullptr) {
793 UndoImageBuf *ubuf_reference = uhandle_lookup_ubuf(
794 uh_prev, image, ubuf->ibuf_filepath, ubuf->ibuf_fileframe);
795 if (ubuf_reference) {
796 ubuf_reference = ubuf_reference->post;
797 if ((ubuf_reference->image_dims[0] == ubuf->image_dims[0]) &&
798 (ubuf_reference->image_dims[1] == ubuf->image_dims[1]) &&
800 {
801 return ubuf_reference;
802 }
803 }
804 }
805 return nullptr;
806}
807
809{
810 ImageUndoStep *us = reinterpret_cast<ImageUndoStep *>(us_p);
811 /* dummy, memory is cleared anyway. */
812 us->is_encode_init = true;
813 us->handles.clear_no_delete();
814 us->paint_tile_map = MEM_new<PaintTileMap>(__func__);
815}
816
817static bool image_undosys_step_encode(bContext *C, Main * /*bmain*/, UndoStep *us_p)
818{
819 /* Encoding is done along the way by adding tiles
820 * to the current 'ImageUndoStep' added by encode_init.
821 *
822 * This function ensures there are previous and current states of the image in the undo buffer.
823 */
824 ImageUndoStep *us = reinterpret_cast<ImageUndoStep *>(us_p);
825
826 BLI_assert(us->step.data_size == 0);
827
828 if (us->is_encode_init) {
829
830 ImageUndoStep *us_reference = reinterpret_cast<ImageUndoStep *>(
832 while (us_reference && us_reference->step.type != BKE_UNDOSYS_TYPE_IMAGE) {
833 us_reference = reinterpret_cast<ImageUndoStep *>(us_reference->step.prev);
834 }
835
836 /* Initialize undo tiles from paint-tiles (if they exist). */
837 for (PaintTile *ptile : us->paint_tile_map->map.values()) {
838 if (ptile->valid) {
839 UndoImageHandle *uh = uhandle_ensure(&us->handles, ptile->image, &ptile->iuser);
840 UndoImageBuf *ubuf_pre = uhandle_ensure_ubuf(uh, ptile->image, ptile->ibuf);
841
842 /* Use the state before the stroke, which may itself have done a full update. */
844 ptile->ibuf_full_update_changeset_id);
845
846 UndoImageTile *utile = MEM_new_zeroed<UndoImageTile>("UndoImageTile");
847 utile->users = 1;
848 utile->ibuf = ptile->ptile_ibuf;
849 ptile->ptile_ibuf = nullptr;
850 const uint tile_index = index_from_xy(ptile->x_tile, ptile->y_tile, ubuf_pre->tiles_dims);
851
852 BLI_assert(ubuf_pre->tiles[tile_index] == nullptr);
853 ubuf_pre->tiles[tile_index] = utile;
854 }
855 ptile_free(ptile);
856 }
857 us->paint_tile_map->map.clear();
858
859 for (UndoImageHandle &uh : us->handles) {
860 for (UndoImageBuf &ubuf_pre : uh.buffers) {
861
862 ImBuf *ibuf = BKE_image_acquire_ibuf(uh.image_ref.ptr, &uh.iuser, nullptr);
863
864 const bool has_float = ibuf->float_data();
865
866 BLI_assert(ubuf_pre.post == nullptr);
867 ubuf_pre.post = ubuf_from_image_no_tiles(uh.image_ref.ptr, ibuf);
868 UndoImageBuf *ubuf_post = ubuf_pre.post;
869
870 if (ubuf_pre.image_dims[0] != ubuf_post->image_dims[0] ||
871 ubuf_pre.image_dims[1] != ubuf_post->image_dims[1])
872 {
873 ubuf_from_image_all_tiles(ubuf_post, ibuf);
874 }
875 else {
876 /* Search for the previous buffer, matching the state before this step. */
877 UndoImageBuf *ubuf_reference =
878 (us_reference ? ubuf_lookup_from_reference(
879 us_reference, uh.image_ref.ptr, uh.iuser.tile, &ubuf_pre) :
880 nullptr);
881
882 const uint tiles_dims_x = ubuf_pre.tiles_dims[0];
884 IndexRange(ubuf_pre.tiles_len), 1024, [&](const IndexRange range) {
885 for (const int64_t i : range) {
886 const uint x_tile = uint(i) % tiles_dims_x;
887 const uint y_tile = uint(i) / tiles_dims_x;
888
889 if ((ubuf_reference != nullptr) &&
890 ((ubuf_pre.tiles[i] == nullptr) ||
891 /* In this case the paint stroke as has added a tile
892 * which we have a duplicate reference available. */
893 (ubuf_pre.tiles[i]->users == 1)))
894 {
895 if (ubuf_pre.tiles[i] != nullptr) {
896 /* If we have a reference, re-use this single use tile for the post state. */
897 BLI_assert(ubuf_pre.tiles[i]->users == 1);
898 ubuf_post->tiles[i] = ubuf_pre.tiles[i];
899 ubuf_pre.tiles[i] = nullptr;
900 utile_init_from_imbuf(ubuf_post->tiles[i], x_tile, y_tile, ibuf);
901 }
902 else {
903 BLI_assert(ubuf_post->tiles[i] == nullptr);
904 ubuf_post->tiles[i] = ubuf_reference->tiles[i];
905 ubuf_post->tiles[i]->users += 1;
906 }
907 BLI_assert(ubuf_pre.tiles[i] == nullptr);
908 ubuf_pre.tiles[i] = ubuf_reference->tiles[i];
909 ubuf_pre.tiles[i]->users += 1;
910
911 BLI_assert(ubuf_pre.tiles[i] != nullptr);
912 BLI_assert(ubuf_post->tiles[i] != nullptr);
913 }
914 else {
915 UndoImageTile *utile = utile_alloc(has_float);
916 utile_init_from_imbuf(utile, x_tile, y_tile, ibuf);
917
918 if (ubuf_pre.tiles[i] != nullptr) {
919 ubuf_post->tiles[i] = utile;
920 utile->users = 1;
921 }
922 else {
923 ubuf_pre.tiles[i] = utile;
924 ubuf_post->tiles[i] = utile;
925 utile->users = 2;
926 }
927 }
928 BLI_assert(ubuf_pre.tiles[i] != nullptr);
929 BLI_assert(ubuf_post->tiles[i] != nullptr);
930 }
931 });
932 }
933 BKE_image_release_ibuf(uh.image_ref.ptr, ibuf, nullptr);
934 }
935 }
936
937 /* Useful to debug tiles are stored correctly. */
938 if (false) {
940 }
941 }
942 else {
943 BLI_assert(C != nullptr);
944 /* Happens when switching modes. */
947 us->paint_mode = paint_mode;
948 }
949
950 us_p->is_applied = true;
951
952 return true;
953}
954
956{
957 BLI_assert(us->step.is_applied == true);
958 uhandle_restore_list(&us->handles, true);
959 us->step.is_applied = false;
960}
961
963{
964 BLI_assert(us->step.is_applied == false);
965 uhandle_restore_list(&us->handles, false);
966 us->step.is_applied = true;
967}
968
969static void image_undosys_step_decode_undo(ImageUndoStep *us, bool is_final)
970{
971 /* Walk forward over any applied steps of same type,
972 * then walk back in the next loop, un-applying them. */
973 ImageUndoStep *us_iter = us;
974 while (us_iter->step.next && (us_iter->step.next->type == us_iter->step.type)) {
975 if (us_iter->step.next->is_applied == false) {
976 break;
977 }
978 us_iter = reinterpret_cast<ImageUndoStep *>(us_iter->step.next);
979 }
980 while (us_iter != us || (!is_final && us_iter == us)) {
981 BLI_assert(us_iter->step.type == us->step.type); /* Previous loop ensures this. */
983 if (us_iter == us) {
984 break;
985 }
986 us_iter = reinterpret_cast<ImageUndoStep *>(us_iter->step.prev);
987 }
988}
989
991{
992 ImageUndoStep *us_iter = us;
993 while (us_iter->step.prev && (us_iter->step.prev->type == us_iter->step.type)) {
994 if (us_iter->step.prev->is_applied == true) {
995 break;
996 }
997 us_iter = reinterpret_cast<ImageUndoStep *>(us_iter->step.prev);
998 }
999 while (us_iter && (us_iter->step.is_applied == false)) {
1001 if (us_iter == us) {
1002 break;
1003 }
1004 us_iter = reinterpret_cast<ImageUndoStep *>(us_iter->step.next);
1005 }
1006}
1007
1009 bContext *C, Main *bmain, UndoStep *us_p, const eUndoStepDir dir, bool is_final)
1010{
1011 /* NOTE: behavior for undo/redo closely matches sculpt undo. */
1012 BLI_assert(dir != STEP_INVALID);
1013
1014 ImageUndoStep *us = reinterpret_cast<ImageUndoStep *>(us_p);
1015 if (dir == STEP_UNDO) {
1016 image_undosys_step_decode_undo(us, is_final);
1017 }
1018 else if (dir == STEP_REDO) {
1020 }
1021
1022 /* A memfile undo step decoded before this step may have regenerated the image buffer,
1023 * e.g. when undoing image pack. Already applied steps must be restored again then. */
1024 if (us->step.is_applied) {
1026 }
1027
1028 if (us->paint_mode == PaintMode::Texture3D) {
1030 }
1031
1032 /* Ideally, we shouldn't have to tag the object as needing to be recalculated if using this paint
1033 * mode, however, because the image isn't connected as part of the shader nodes, the draw code
1034 * is unaware of the corresponding image tag. See #150957 for more details. */
1035 const Scene *scene = CTX_data_scene(C);
1036 Object *object = CTX_data_active_object(C);
1037 if (object && object->type == OB_MESH && scene &&
1039 {
1041 }
1042
1043 /* Refresh texture slots. */
1045}
1046
1048{
1049 ImageUndoStep *us = reinterpret_cast<ImageUndoStep *>(us_p);
1051
1052 /* Typically this map will have been cleared. */
1053 MEM_delete(us->paint_tile_map);
1054 us->paint_tile_map = nullptr;
1055}
1056
1058 UndoTypeForEachIDRefFn foreach_ID_ref_fn,
1059 void *user_data)
1060{
1061 ImageUndoStep *us = reinterpret_cast<ImageUndoStep *>(us_p);
1062 for (UndoImageHandle &uh : us->handles) {
1063 foreach_ID_ref_fn(user_data, (reinterpret_cast<UndoRefID *>(&uh.image_ref)));
1064 }
1065}
1066
1068{
1069 ut->identifier = "IMAGE";
1070 /* Note, we do not need the `poll` method overridden because of the `step_encode_init` callback
1071 * and exposed #ED_image_undo_push_begin/end calls. */
1076
1078
1079 /* NOTE: this is actually a confusing case, since it expects a valid context, but only in a
1080 * specific case, see `image_undosys_step_encode` code. We cannot specify
1081 * `UNDOTYPE_FLAG_NEED_CONTEXT_FOR_ENCODE` though, as it can be called with a null context by
1082 * current code. */
1084
1085 ut->step_size = sizeof(ImageUndoStep);
1086}
1087
1089
1090/* -------------------------------------------------------------------- */
1101
1103{
1104 UndoStack *ustack = ED_undo_stack_get();
1105 UndoStep *us_prev = ustack->step_init;
1107 ImageUndoStep *us = reinterpret_cast<ImageUndoStep *>(us_p);
1108 /* We should always have an undo push started when accessing tiles,
1109 * not doing this means we won't have paint_mode correctly set. */
1110 BLI_assert(us_p == us_prev);
1111 if (us_p != us_prev) {
1112 /* Fallback value until we can be sure this never happens. */
1114 }
1115 return us->paint_tile_map;
1116}
1117
1119{
1120 PaintTileMap *paint_tile_map = reinterpret_cast<ImageUndoStep *>(us)->paint_tile_map;
1121 ptile_restore_runtime_map(paint_tile_map);
1122 ptile_invalidate_map(paint_tile_map);
1123}
1124
1125static ImageUndoStep *image_undo_push_begin(const char *name, PaintMode paint_mode)
1126{
1127 UndoStack *ustack = ED_undo_stack_get();
1128 bContext *C = nullptr; /* special case, we never read from this. */
1130 ImageUndoStep *us = reinterpret_cast<ImageUndoStep *>(us_p);
1132 us->paint_mode = paint_mode;
1133 return us;
1134}
1135
1136void ED_image_undo_push_begin(const char *name, PaintMode paint_mode)
1137{
1138 image_undo_push_begin(name, paint_mode);
1139}
1140
1142 Image *image,
1143 ImBuf *ibuf,
1144 ImageUser *iuser)
1145{
1147
1148 ED_image_undo_push(image, ibuf, iuser, us);
1149}
1150
1152 Image *image,
1153 ImageUser *iuser)
1154{
1156
1157 for (ImageTile &current_tile : image->tiles) {
1158 iuser->tile = current_tile.tile_number;
1159 ImBuf *ibuf = BKE_image_acquire_ibuf(image, iuser, nullptr);
1160
1161 ED_image_undo_push(image, ibuf, iuser, us);
1162
1163 /* Release the image buffer to avoid leaking memory. */
1164 BKE_image_release_ibuf(image, ibuf, nullptr);
1165 }
1166}
1167
1169{
1171 UndoImageHandle *uh = uhandle_ensure(&us->handles, image, iuser);
1172 UndoImageBuf *ubuf_pre = uhandle_ensure_ubuf(uh, image, ibuf);
1173 BLI_assert(ubuf_pre->post == nullptr);
1174
1175 ImageUndoStep *us_reference = reinterpret_cast<ImageUndoStep *>(
1177 while (us_reference && us_reference->step.type != BKE_UNDOSYS_TYPE_IMAGE) {
1178 us_reference = reinterpret_cast<ImageUndoStep *>(us_reference->step.prev);
1179 }
1180 UndoImageBuf *ubuf_reference = (us_reference ? ubuf_lookup_from_reference(
1181 us_reference, image, iuser->tile, ubuf_pre) :
1182 nullptr);
1183
1184 if (ubuf_reference) {
1185 memcpy(ubuf_pre->tiles, ubuf_reference->tiles, sizeof(*ubuf_pre->tiles) * ubuf_pre->tiles_len);
1186 for (uint32_t i = 0; i < ubuf_pre->tiles_len; i++) {
1187 UndoImageTile *utile = ubuf_pre->tiles[i];
1188 utile->users += 1;
1189 }
1190 }
1191 else {
1192 ubuf_from_image_all_tiles(ubuf_pre, ibuf);
1193 }
1194}
1195
1203
1205
1206} // namespace blender
#define BKE_undosys_stack_limit_steps_and_memory_defaults(ustack)
#define CLOG_ERROR(clg_ref,...)
Definition CLG_log.h:188
Object is a sort of wrapper for general info.
#define IMAGEPAINT_MODE_IMAGE
#define ED_IMAGE_UNDO_TILE_NUMBER(size)
Definition ED_paint.hh:117
#define ED_IMAGE_UNDO_TILE_SIZE
Definition ED_paint.hh:116
Read Blender memory allocation and freeing.
#define C
Definition RandGen.cpp:29
#define BLI_assert(a)
#define ELEM(...)
#define STREQ(a, b)
constexpr IndexRange()=default
ValueIterator values() const &
Definition BLI_map.hh:884
const int tile_index
bool mode_set_ex(bContext *C, eObjectMode mode, bool use_undo, ReportList *reports)
T min(const T &a, const T &b)
void parallel_for(const IndexRange range, const int64_t grain_size, const Function &function, const TaskSizeHints &size_hints=detail::TaskSizeHints_Static(1))
Definition BLI_task.hh:69
BMesh Iterators.
BLI_assert(slot->slot_subtype.map==BMO_OP_SLOT_SUBTYPE_MAP_FLT)
void ED_image_undo_restore(UndoStep *us)
unsigned __int64 uint64_t
Definition BKE_text.h:91
static UndoImageHandle * uhandle_ensure(ListBaseT< UndoImageHandle > *undo_handles, Image *image, ImageUser *iuser)
static UndoImageHandle * uhandle_lookup_by_name(ListBaseT< UndoImageHandle > *undo_handles, const Image *image, int tile_number)
static void utile_restore(const UndoImageTile *utile, const int x_tile, const int y_tile, ImBuf *ibuf)
static UndoImageBuf * ubuf_from_image_no_tiles(Image *image, const ImBuf *ibuf)
void ED_image_undosys_type(UndoType *ut)
unsigned int uint
Definition BKE_text.h:55
UndoStack * ED_undo_stack_get()
Definition ed_undo.cc:690
@ UNDOTYPE_FLAG_DECODE_ACTIVE_STEP
MINLINE int square_i(int a)
void ED_image_undo_push_end()
static void image_undosys_step_free(UndoStep *us_p)
static UndoImageTile * utile_alloc(bool has_float)
static UndoImageBuf * ubuf_lookup_from_reference(ImageUndoStep *us_prev, const Image *image, int tile_number, const UndoImageBuf *ubuf)
void ED_image_undo_push_begin(const char *name, PaintMode paint_mode)
void ED_image_undo_push(Image *image, ImBuf *ibuf, ImageUser *iuser, ImageUndoStep *us)
static void ptile_invalidate_map(PaintTileMap *paint_tile_map)
bool IMB_alloc_byte_pixels(ImBuf *ibuf, bool initialize_pixels=true)
void IMB_rect_size_set(ImBuf *ibuf, const uint size[2])
Definition rectop.cc:489
void IMB_mark_dirty(ImBuf *ibuf)
unsigned short ushort
Definition BKE_text.h:56
static void ptile_restore_runtime_map(PaintTileMap *paint_tile_map)
ImageTile * BKE_image_get_tile(Image *ima, int tile_number)
static void calc_tile_rect(const ImBuf &ibuf, const int x_tile, const int y_tile, int2 &r_tile_pos, int2 &r_tile_size)
Definition image_undo.cc:85
static void ubuf_from_image_all_tiles(UndoImageBuf *ubuf, const ImBuf *ibuf)
static void image_undosys_step_decode(bContext *C, Main *bmain, UndoStep *us_p, const eUndoStepDir dir, bool is_final)
PaintTileMap * ED_image_paint_tile_map_get()
unsigned short uint16_t
Definition BKE_text.h:80
unsigned int uint32_t
Definition BKE_text.h:81
void IMB_freeImBuf(ImBuf *ibuf)
VecBase< int32_t, 2 > int2
void * MEM_new_zeroed(size_t len, const char *str) ATTR_WARN_UNUSED_RESULT ATTR_ALLOC_SIZE(1) ATTR_NONNULL(2)
void ED_editors_init_for_undo(Main *bmain)
Definition ed_util.cc:64
void DEG_id_tag_update(ID *id, unsigned int flags)
@ OB_MODE_TEXTURE_PAINT
UndoStep * BKE_undosys_stack_init_or_active_with_type(UndoStack *ustack, const UndoType *ut)
static void utile_decref(UndoImageTile *utile)
void ED_image_undo_push_begin_with_image_all_udims(const char *name, Image *image, ImageUser *iuser)
void IMB_partial_update_mark_region(ImBuf *ibuf, const rcti &region)
const ImBuf * ED_image_paint_tile_find(PaintTileMap *paint_tile_map, Image *image, ImBuf *ibuf, ImageUser *iuser, int x_tile, int y_tile, unsigned short **r_mask, bool validate)
static bool ubuf_ensure_compat_ibuf(const UndoImageBuf *ubuf, ImBuf *ibuf)
static uint32_t index_from_xy(uint32_t tile_x, uint32_t tile_y, const uint32_t tiles_dims[2])
ImBuf * IMB_allocImBuf(unsigned int x, unsigned int y, ImBufFlags flags)
void BLI_rcti_init(struct rcti *rect, int xmin, int xmax, int ymin, int ymax)
Definition rct.cc:428
ImBuf * BKE_image_acquire_ibuf(Image *ima, ImageUser *iuser, void **r_lock)
static void utile_init_from_imbuf(UndoImageTile *utile, const int x_tile, const int y_tile, const ImBuf *ibuf)
static UndoImageBuf * uhandle_add_ubuf(UndoImageHandle *uh, Image *image, ImBuf *ibuf)
static bool image_undosys_step_encode(bContext *C, Main *, UndoStep *us_p)
static UndoImageBuf * uhandle_ensure_ubuf(UndoImageHandle *uh, Image *image, ImBuf *ibuf)
void WM_file_tag_modified()
Definition wm_files.cc:188
void BLI_addtail(ListBase *listbase, void *vlink) ATTR_NONNULL(1)
Definition listbase.cc:118
const ImBuf * ED_image_paint_tile_push(PaintTileMap *paint_tile_map, Image *image, ImBuf *ibuf, ImageUser *iuser, int x_tile, int y_tile, unsigned short **r_mask, bool **r_valid)
static rcti calc_tile_region(const ImBuf &ibuf, const int x_tile, const int y_tile)
Definition image_undo.cc:93
static void ubuf_free(UndoImageBuf *ubuf)
static UndoImageBuf * uhandle_lookup_ubuf(UndoImageHandle *uh, const Image *, const StringRef ibuf_filepath, const int ibuf_fileframe)
static void uhandle_free_list(ListBaseT< UndoImageHandle > *undo_handles)
std::mutex Mutex
Definition BLI_mutex.hh:51
static void image_undosys_step_decode_undo(ImageUndoStep *us, bool is_final)
@ ID_RECALC_SHADING
Definition DNA_ID.h:1094
void IMB_free_all_data(ImBuf *ibuf)
static ImageUndoStep * image_undo_push_begin(const char *name, PaintMode paint_mode)
static UndoImageHandle * uhandle_lookup(ListBaseT< UndoImageHandle > *undo_handles, const Image *image, int tile_number)
static void image_undosys_step_decode_redo(ImageUndoStep *us)
static void image_undosys_step_decode_undo_impl(ImageUndoStep *us)
static CLG_LogRef LOG
Definition logging.cpp:12
void(*)(void *user_data, UndoRefID *id_ref) UndoTypeForEachIDRefFn
Scene * CTX_data_scene(const bContext *C)
void ED_image_undo_push_begin_with_image(const char *name, Image *image, ImBuf *ibuf, ImageUser *iuser)
static void ptile_free(PaintTile *ptile)
bool IMB_alloc_float_pixels(ImBuf *ibuf, unsigned int channels, bool initialize_pixels=true)
static void uhandle_restore_list(ListBaseT< UndoImageHandle > *undo_handles, const bool use_init, const RestoreMode mode=RestoreMode::Changed)
const UndoType * BKE_UNDOSYS_TYPE_IMAGE
Object * CTX_data_active_object(const bContext *C)
constexpr uint64_t get_default_hash(const T &v, const Args &...args)
Definition BLI_hash.hh:252
static UndoImageHandle * uhandle_add(ListBaseT< UndoImageHandle > *undo_handles, Image *image, ImageUser *iuser)
PaintMode BKE_paintmode_get_active_from_context(const bContext *C)
void IMB_free_float_pixels(ImBuf *ibuf)
eUndoPushReturn BKE_undosys_step_push(UndoStack *ustack, bContext *C, const char *name, UndoEncodeHints hints)
static void image_undosys_step_decode_redo_impl(ImageUndoStep *us)
static void image_undosys_step_encode_init(bContext *, UndoStep *us_p)
void * MEM_new_array_zeroed(size_t len, size_t size, const char *str) ATTR_WARN_UNUSED_RESULT ATTR_ALLOC_SIZE(1
void IMB_copy_rect(float *dst, const int2 &dst_size, const float *src, const int2 &src_size, int channels, const int2 &src_rect_pos, const int2 &dst_rect_pos, const int2 &rect_size)
Definition rectop.cc:414
void BKE_image_release_ibuf(Image *ima, ImBuf *ibuf, void *lock)
static void image_undosys_foreach_ID_ref(UndoStep *us_p, UndoTypeForEachIDRefFn foreach_ID_ref_fn, void *user_data)
UndoStep * BKE_undosys_step_push_init_with_type(UndoStack *ustack, bContext *C, const char *name, const UndoType *ut)
const uint8_t * byte_data() const
uint8_t * byte_data_for_write()
Definition allocimbuf.cc:60
std::atomic< imbuf::ChangesetID > full_update_changeset_id
const float * float_data() const
std::string filepath
float * float_data_for_write()
Definition allocimbuf.cc:79
ListBaseT< UndoImageHandle > handles
PaintTileMap * paint_tile_map
struct Scene * scene
uint64_t hash() const
bool operator==(const PaintTileKey &other) const
Map< PaintTileKey, PaintTile * > map
blender::Mutex mutex
imbuf::ChangesetID ibuf_full_update_changeset_id
blender::Mutex mutex
struct ToolSettings * toolsettings
struct ImagePaintSettings imapaint
struct blender::UndoImageBuf::@056213222331336054052020000054316262262052143262 image_state
UndoImageBuf * next
std::string ibuf_filepath
UndoImageBuf * prev
UndoImageTile ** tiles
UndoImageBuf * post
imbuf::ChangesetID ibuf_full_update_changeset_id
UndoImageHandle * prev
UndoImageHandle * next
ListBaseT< UndoImageBuf > buffers
UndoRefID_Image image_ref
const UndoType * type
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
void(*) step_encode_init(bContext *C, UndoStep *us)