Blender V5.3
grease_pencil_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 "BLI_task.hh"
10
11#include "BKE_context.hh"
12#include "BKE_curves.hh"
13#include "BKE_customdata.hh"
14#include "BKE_grease_pencil.hh"
15#include "BKE_lib_id.hh"
16#include "BKE_main.hh"
17#include "BKE_object.hh"
18#include "BKE_undo_system.hh"
19
20#include "CLG_log.h"
21
22#include "DEG_depsgraph.hh"
24
26#include "ED_grease_pencil.hh"
27#include "ED_undo.hh"
28
29#include "MEM_guardedalloc.h"
30
31#include "WM_api.hh"
32#include "WM_types.hh"
33
34namespace blender {
35
36static CLG_LogRef LOG = {"undo.greasepencil"};
37
39
40/* -------------------------------------------------------------------- */
45
54
55/* Store contextual data and status info during undo step encoding or decoding. */
57
65
67 protected:
68 /* Index of this drawing in the original combined array of all drawings in GreasePencil ID. */
69 int index_;
70
71 /* Data from #GreasePencilDrawingBase that needs to be saved in undo steps. */
73
84 const GreasePencilDrawingType drawing_type) const
85 {
86 /* TODO: Maybe that code should rather be part of GreasePencil:: API, together with
87 * `add_empty_drawings` and such? */
88 GreasePencilDrawingBase *drawing = drawings[index_];
89 if (drawing != nullptr) {
90 if (drawing->type == drawing_type) {
91 return;
92 }
93 switch (drawing->type) {
94 case GP_DRAWING:
95 MEM_delete(&reinterpret_cast<GreasePencilDrawing *>(drawing)->wrap());
96 break;
98 MEM_delete(&reinterpret_cast<GreasePencilDrawingReference *>(drawing)->wrap());
99 break;
100 }
101 drawing = nullptr;
102 }
103 if (drawing == nullptr) {
104 switch (drawing_type) {
105 case GP_DRAWING:
106 drawings[index_] = reinterpret_cast<GreasePencilDrawingBase *>(
107 MEM_new<bke::greasepencil::Drawing>(__func__));
108 break;
110 drawings[index_] = reinterpret_cast<GreasePencilDrawingBase *>(
111 MEM_new<bke::greasepencil::DrawingReference>(__func__));
112 break;
113 }
114 }
115 }
116};
117
119 bke::CurvesGeometry geometry_;
120
121 public:
122 void encode(const GreasePencilDrawing &drawing,
123 const int64_t drawing_index,
124 StepEncodeStatus & /*encode_status*/)
125 {
126 BLI_assert(drawing_index >= 0 && drawing_index < INT32_MAX);
127 index_ = int(drawing_index);
128
129 flag_ = drawing.base.flag;
130 geometry_ = drawing.wrap().strokes();
131 }
132
133 void decode(GreasePencil &grease_pencil, StepDecodeStatus & /*decode_status*/) const
134 {
137 BLI_assert(drawings[index_]->type == GP_DRAWING);
138
139 GreasePencilDrawing &drawing = *reinterpret_cast<GreasePencilDrawing *>(drawings[index_]);
140
141 drawing.base.flag = flag_;
142 drawing.wrap().strokes_for_write() = geometry_;
143
144 /* TODO: Check if there is a way to tell if both stored and current geometry are still the
145 * same, to avoid recomputing the caches all the time for all drawings? */
146 drawing.wrap().tag_topology_changed();
147 }
148};
149
151 UndoRefID_GreasePencil grease_pencil_ref_ = {};
152
153 public:
154 void encode(const GreasePencilDrawingReference &drawing_reference,
155 const int64_t drawing_index,
156 StepEncodeStatus & /*encode_status*/)
157 {
158 BLI_assert(drawing_index >= 0 && drawing_index < INT32_MAX);
159 index_ = int(drawing_index);
160
161 flag_ = drawing_reference.base.flag;
162 grease_pencil_ref_.ptr = drawing_reference.id_reference;
163 }
164
166 {
169 BLI_assert(drawings[index_]->type == GP_DRAWING_REFERENCE);
170
171 GreasePencilDrawingReference &drawing_reference =
172 *reinterpret_cast<GreasePencilDrawingReference *>(drawings[index_]);
173 drawing_reference.base.flag = flag_;
174
175 if (drawing_reference.id_reference != grease_pencil_ref_.ptr) {
176 id_us_min(reinterpret_cast<ID *>(drawing_reference.id_reference));
177 drawing_reference.id_reference = grease_pencil_ref_.ptr;
178 id_us_plus(reinterpret_cast<ID *>(drawing_reference.id_reference));
179 decode_status.needs_relationships_update = true;
180 }
181 }
182
183 void foreach_id_ref(UndoTypeForEachIDRefFn foreach_ID_ref_fn, void *user_data)
184 {
185 foreach_ID_ref_fn(user_data, reinterpret_cast<UndoRefID *>(&grease_pencil_ref_));
186 }
187};
188
190 public:
191 UndoRefID_Object obedit_ref = {};
192
193 private:
194 Array<StepDrawingGeometry> drawings_geometry_;
195 Array<StepDrawingReference> drawings_reference_;
196
197 int layers_num_ = 0;
199 std::string active_node_name_;
200 bke::AttributeStorage layer_attributes_;
201
202 void encode_drawings(const GreasePencil &grease_pencil, StepEncodeStatus &encode_status)
203 {
204 const Span<const GreasePencilDrawingBase *> drawings = grease_pencil.drawings();
205
206 int64_t drawings_geometry_num = 0;
207 int64_t drawings_reference_num = 0;
208 for (const int64_t idx : drawings.index_range()) {
209 const GreasePencilDrawingBase &drawing = *drawings[idx];
210 switch (drawing.type) {
211 case GP_DRAWING:
212 drawings_geometry_num++;
213 break;
215 drawings_reference_num++;
216 break;
217 }
218 }
219
220 drawings_geometry_.reinitialize(drawings_geometry_num);
221 drawings_reference_.reinitialize(drawings_reference_num);
222
223 int drawings_geometry_idx = 0;
224 int drawings_reference_idx = 0;
225 for (const int64_t idx : drawings.index_range()) {
226 const GreasePencilDrawingBase &drawing = *drawings[idx];
227 switch (drawing.type) {
228 case GP_DRAWING:
229 drawings_geometry_[drawings_geometry_idx++].encode(
230 reinterpret_cast<const GreasePencilDrawing &>(drawing), idx, encode_status);
231 break;
233 drawings_reference_[drawings_reference_idx++].encode(
234 reinterpret_cast<const GreasePencilDrawingReference &>(drawing), idx, encode_status);
235 break;
236 }
237 }
238 }
239
240 void decode_drawings(GreasePencil &grease_pencil, StepDecodeStatus &decode_status) const
241 {
242 const int drawing_array_num = int(drawings_geometry_.size() + drawings_reference_.size());
243 grease_pencil.resize_drawings(drawing_array_num);
244
245 for (const StepDrawingGeometry &drawing : drawings_geometry_) {
246 drawing.decode(grease_pencil, decode_status);
247 }
248 for (const StepDrawingReference &drawing : drawings_reference_) {
249 drawing.decode(grease_pencil, decode_status);
250 }
251 }
252
253 void encode_layers(const GreasePencil &grease_pencil, StepEncodeStatus & /*encode_status*/)
254 {
255 layers_num_ = int(grease_pencil.layers().size());
256 layer_attributes_ = grease_pencil.attribute_storage.wrap();
257
258 if (grease_pencil.active_node != nullptr) {
259 active_node_name_ = grease_pencil.get_active_node()->name();
260 }
261
262 root_group_ = grease_pencil.root_group();
263 }
264
265 void decode_layers(GreasePencil &grease_pencil, StepDecodeStatus & /*decode_status*/) const
266 {
267 if (grease_pencil.root_group_ptr) {
268 MEM_delete(&grease_pencil.root_group());
269 }
270 grease_pencil.set_active_node(nullptr);
271
272 grease_pencil.root_group_ptr = MEM_new<bke::greasepencil::LayerGroup>(__func__, root_group_);
273 BLI_assert(layers_num_ == grease_pencil.layers().size());
274
275 if (!active_node_name_.empty()) {
276 if (bke::greasepencil::TreeNode *active_node = grease_pencil.root_group().find_node_by_name(
277 active_node_name_))
278 {
279 grease_pencil.set_active_node(active_node);
280 }
281 }
282
283 grease_pencil.attribute_storage.wrap() = layer_attributes_;
284 }
285
286 public:
287 ~StepObject() = default;
288
289 void encode(Object *ob, StepEncodeStatus &encode_status)
290 {
291 const GreasePencil &grease_pencil = *id_cast<GreasePencil *>(ob->data);
292 this->obedit_ref.ptr = ob;
293
294 this->encode_drawings(grease_pencil, encode_status);
295 this->encode_layers(grease_pencil, encode_status);
296 }
297
298 void decode(StepDecodeStatus &decode_status) const
299 {
300 GreasePencil &grease_pencil = *id_cast<GreasePencil *>(this->obedit_ref.ptr->data);
301
302 this->decode_drawings(grease_pencil, decode_status);
303 this->decode_layers(grease_pencil, decode_status);
304
306 }
307
308 void foreach_id_ref(UndoTypeForEachIDRefFn foreach_ID_ref_fn, void *user_data)
309 {
310 foreach_ID_ref_fn(user_data, reinterpret_cast<UndoRefID *>(&this->obedit_ref));
311 for (StepDrawingReference &drawing_ref : drawings_reference_) {
312 drawing_ref.foreach_id_ref(foreach_ID_ref_fn, user_data);
313 }
314 }
315};
316
323
324static bool step_encode(bContext *C, Main *bmain, UndoStep *us_p)
325{
326 GreasePencilUndoStep *us = reinterpret_cast<GreasePencilUndoStep *>(us_p);
327 StepEncodeStatus encode_status;
328
329 Scene *scene = CTX_data_scene(C);
330 ViewLayer *view_layer = CTX_data_view_layer(C);
331 Vector<Object *> objects = ED_undo_editmode_objects_from_view_layer(*bmain, scene, view_layer);
332
333 us->scene_ref.ptr = scene;
334 new (&us->objects) Array<StepObject>(objects.size());
335
336 threading::parallel_for(us->objects.index_range(), 8, [&](const IndexRange range) {
337 for (const int64_t i : range) {
338 Object *ob = objects[i];
339 us->objects[i].encode(ob, encode_status);
340 }
341 });
342
343 bmain->is_memfile_undo_flush_needed = true;
344
345 return true;
346}
347
348static void step_decode(
349 bContext *C, Main *bmain, UndoStep *us_p, const eUndoStepDir /*dir*/, bool /*is_final*/)
350{
351 GreasePencilUndoStep *us = reinterpret_cast<GreasePencilUndoStep *>(us_p);
352 StepDecodeStatus decode_status;
353
354 Scene *scene = CTX_data_scene(C);
355 ViewLayer *view_layer = CTX_data_view_layer(C);
356
358 CTX_wm_manager(C), us->scene_ref.ptr, &scene, &view_layer);
360 view_layer,
361 &us->objects.first().obedit_ref.ptr,
362 uint(us->objects.size()),
363 sizeof(decltype(us->objects)::value_type));
364
365 BLI_assert(BKE_object_is_in_editmode(us->objects.first().obedit_ref.ptr));
366
367 for (const StepObject &step_object : us->objects) {
368 step_object.decode(decode_status);
369 }
370
371 if (decode_status.needs_relationships_update) {
373 }
374
376 *bmain, scene, view_layer, us->objects.first().obedit_ref.ptr, us_p->name, &LOG);
377
378 bmain->is_memfile_undo_flush_needed = true;
379
381}
382
383static void step_free(UndoStep *us_p)
384{
385 GreasePencilUndoStep *us = reinterpret_cast<GreasePencilUndoStep *>(us_p);
386 us->objects.~Array();
387}
388
389static void foreach_ID_ref(UndoStep *us_p,
390 UndoTypeForEachIDRefFn foreach_ID_ref_fn,
391 void *user_data)
392{
393 GreasePencilUndoStep *us = reinterpret_cast<GreasePencilUndoStep *>(us_p);
394
395 foreach_ID_ref_fn(user_data, reinterpret_cast<UndoRefID *>(&us->scene_ref));
396 for (StepObject &object : us->objects) {
397 object.foreach_id_ref(foreach_ID_ref_fn, user_data);
398 }
399}
400
402
403} // namespace ed::greasepencil::undo
404
421
422} // namespace blender
Low-level operations for curves.
CustomData interface, see also DNA_customdata_types.h.
Low-level operations for grease pencil.
General operations, lookup, etc. for blender objects.
#define INT32_MAX
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 BLI_assert(a)
void reinitialize(const int64_t new_size)
Definition BLI_array.hh:430
constexpr IndexRange index_range() const
Definition BLI_span.hh:405
int64_t size() const
void decode_valid_drawingtype_at_index_ensure(MutableSpan< GreasePencilDrawingBase * > drawings, const GreasePencilDrawingType drawing_type) const
void decode(GreasePencil &grease_pencil, StepDecodeStatus &) const
void encode(const GreasePencilDrawing &drawing, const int64_t drawing_index, StepEncodeStatus &)
void foreach_id_ref(UndoTypeForEachIDRefFn foreach_ID_ref_fn, void *user_data)
void encode(const GreasePencilDrawingReference &drawing_reference, const int64_t drawing_index, StepEncodeStatus &)
void decode(GreasePencil &grease_pencil, StepDecodeStatus &decode_status) const
void foreach_id_ref(UndoTypeForEachIDRefFn foreach_ID_ref_fn, void *user_data)
void decode(StepDecodeStatus &decode_status) const
void encode(Object *ob, StepEncodeStatus &encode_status)
static void foreach_ID_ref(UndoStep *us_p, UndoTypeForEachIDRefFn foreach_ID_ref_fn, void *user_data)
static bool step_encode(bContext *C, Main *bmain, UndoStep *us_p)
static void step_free(UndoStep *us_p)
static void step_decode(bContext *C, Main *bmain, UndoStep *us_p, const eUndoStepDir, bool)
bool grease_pencil_edit_poll(bContext *C)
BLI_INLINE Texture * wrap(MTLTexture *texture)
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)
__int64 int64_t
Definition BKE_text.h:90
void ED_undosys_type_grease_pencil(UndoType *ut)
unsigned int uint
Definition BKE_text.h:55
@ UNDOTYPE_FLAG_NEED_CONTEXT_FOR_ENCODE
@ UNDOTYPE_FLAG_ENCODE_PRE_MEMFILE_SUPPORTED
void id_us_plus(ID *id)
Definition lib_id.cc:433
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
Vector< Object * > ED_undo_editmode_objects_from_view_layer(const Main &bmain, const Scene *scene, ViewLayer *view_layer)
Definition ed_undo.cc:1109
ViewLayer * CTX_data_view_layer(const bContext *C)
void DEG_id_tag_update(ID *id, unsigned int flags)
wmWindowManager * CTX_wm_manager(const bContext *C)
void WM_event_add_notifier(const bContext *C, uint type, void *reference)
bool BKE_object_is_in_editmode(const Object *ob)
Definition object.cc:1898
@ 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 DEG_relations_tag_update(Main *bmain)
void(*)(void *user_data, UndoRefID *id_ref) UndoTypeForEachIDRefFn
Scene * CTX_data_scene(const bContext *C)
void id_us_min(ID *id)
Definition lib_id.cc:441
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
bool is_memfile_undo_flush_needed
Definition BKE_main.hh:302
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