Blender V5.3
IMB_partial_update_test.cc
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2020-2026 Blender Authors
2 *
3 * SPDX-License-Identifier: GPL-2.0-or-later */
4
5#include "testing/testing.h"
6
7#include "BLI_rect.hh"
8
9#include "IMB_imbuf.hh"
10#include "IMB_imbuf_types.hh"
11#include "IMB_partial_update.hh"
12
13#include <cstdint>
14
16
17/* Collect the changes to #buffer since #changeset_id, then advance #changeset_id to the
18 * returned point, mirroring how a consumer persists its #changeset_id between collects. */
19static Changes collect(ImBuf *buffer, imbuf::ChangesetID &changeset_id)
20{
23 Changes changes = IMB_partial_update_collect(buffer, changeset_id);
24 changeset_id = new_changeset_id;
25 return changes;
26}
27
28class IMBPartialUpdateTest : public ::testing::Test {
29 protected:
30 ImBuf *image_buffer = nullptr;
31
32 void SetUp() override
33 {
34 /* Only the dimensions matter for change tracking; no pixel data is needed. */
36 }
37
38 void TearDown() override
39 {
41 }
42};
43
44TEST_F(IMBPartialUpdateTest, initial_full_update)
45{
46 /* Check that we get a single full update for a new image buffer. */
47 const imbuf::ChangesetID last_changeset_id = -1;
48 IMB_partial_update_flush(image_buffer);
50 EXPECT_EQ(IMB_partial_update_collect(image_buffer, last_changeset_id).kind, Changes::Kind::Full);
51 EXPECT_EQ(IMB_partial_update_collect(image_buffer, new_changeset_id).kind, Changes::Kind::None);
52}
53
54TEST_F(IMBPartialUpdateTest, mark_full_update)
55{
56 imbuf::ChangesetID changeset_id = -1;
57 /* The first collect should always return a full update. */
58 EXPECT_EQ(collect(image_buffer, changeset_id).kind, Changes::Kind::Full);
59 /* The second collect should detect no changes. */
60 EXPECT_EQ(collect(image_buffer, changeset_id).kind, Changes::Kind::None);
61
62 /* Mark full update */
63 IMB_partial_update_mark_full(image_buffer);
64
65 /* Validate need full update followed by no changes. */
66 EXPECT_EQ(collect(image_buffer, changeset_id).kind, Changes::Kind::Full);
67 EXPECT_EQ(collect(image_buffer, changeset_id).kind, Changes::Kind::None);
68}
69
70TEST_F(IMBPartialUpdateTest, mark_update_global_order)
71{
72 /* Test full and partial update. */
73 imbuf::ChangesetID changeset_id = -1;
74 EXPECT_EQ(collect(image_buffer, changeset_id).kind, Changes::Kind::Full);
75 rcti region;
76 BLI_rcti_init(&region, 10, 20, 40, 50);
77 IMB_partial_update_mark_region(image_buffer, region);
78 EXPECT_EQ(collect(image_buffer, changeset_id).kind, Changes::Kind::Partial);
79
80 /* Test second image buffer has higher changeset ID due to global order. */
81 ImBuf *later = IMB_allocImBuf(1024, 1024, ImBufFlags::Zero);
82 EXPECT_EQ(collect(later, changeset_id).kind, Changes::Kind::Full);
83 IMB_freeImBuf(later);
84}
85
87{
88 imbuf::ChangesetID changeset_id = -1;
89
90 /* Add some history. */
91 EXPECT_EQ(collect(image_buffer, changeset_id).kind, Changes::Kind::Full);
92 rcti region;
93 BLI_rcti_init(&region, 10, 20, 40, 50);
94 IMB_partial_update_mark_region(image_buffer, region);
95 EXPECT_EQ(collect(image_buffer, changeset_id).kind, Changes::Kind::Partial);
96
97 /* A consumer in sync at the old resolution. */
98 imbuf::ChangesetID old_changeset_id = changeset_id;
99
100 /* Change resolution. and detect resize */
101 IMB_scale(image_buffer, 512, 512, IMBScaleFilter::Nearest);
102 EXPECT_EQ(collect(image_buffer, old_changeset_id).kind, Changes::Kind::Resized);
103 EXPECT_EQ(collect(image_buffer, old_changeset_id).kind, Changes::Kind::None);
104
105 /* Check resize is no longer detected. */
106 IMB_partial_update_mark_full(image_buffer);
107 EXPECT_EQ(collect(image_buffer, old_changeset_id).kind, Changes::Kind::Full);
108
109 /* Ensure resize followed by full is resize. */
110 IMB_scale(image_buffer, 256, 256, IMBScaleFilter::Nearest);
111 IMB_partial_update_mark_full(image_buffer);
112 EXPECT_EQ(collect(image_buffer, old_changeset_id).kind, Changes::Kind::Resized);
113}
114
115TEST_F(IMBPartialUpdateTest, mark_single_region)
116{
117 imbuf::ChangesetID changeset_id = -1;
118 /* First should always return a full update. */
119 EXPECT_EQ(collect(image_buffer, changeset_id).kind, Changes::Kind::Full);
120 /* Second should now detect no changes. */
121 EXPECT_EQ(collect(image_buffer, changeset_id).kind, Changes::Kind::None);
122
123 /* Mark region. */
124 rcti region;
125 BLI_rcti_init(&region, 10, 20, 40, 50);
126 IMB_partial_update_mark_region(image_buffer, region);
127
128 /* Partial Update should be available. */
129 const Changes changes = collect(image_buffer, changeset_id);
131
132 /* Check regions. */
133 const Vector<rcti> regions = changes.modified_regions();
134 ASSERT_EQ(regions.size(), 1);
135 EXPECT_TRUE(BLI_rcti_inside_rcti(&regions.first(), &region));
136
137 EXPECT_EQ(collect(image_buffer, changeset_id).kind, Changes::Kind::None);
138}
139
140TEST_F(IMBPartialUpdateTest, mark_unconnected_regions)
141{
142 imbuf::ChangesetID changeset_id = -1;
143 EXPECT_EQ(collect(image_buffer, changeset_id).kind, Changes::Kind::Full);
144
145 /* Two regions in different chunks. */
146 static_assert(CHUNK_SIZE == 256);
147 rcti region_a;
148 BLI_rcti_init(&region_a, 10, 20, 40, 50);
149 IMB_partial_update_mark_region(image_buffer, region_a);
150 rcti region_b;
151 BLI_rcti_init(&region_b, 600, 610, 700, 710);
152 IMB_partial_update_mark_region(image_buffer, region_b);
153
154 const Changes changes = collect(image_buffer, changeset_id);
156 const Vector<rcti> regions = changes.modified_regions();
157 EXPECT_EQ(regions.size(), 2);
158
159 bool found_a = false;
160 bool found_b = false;
161 for (const rcti &changed : regions) {
162 found_a |= BLI_rcti_inside_rcti(&changed, &region_a);
163 found_b |= BLI_rcti_inside_rcti(&changed, &region_b);
164 }
165 EXPECT_TRUE(found_a);
166 EXPECT_TRUE(found_b);
167}
168
169TEST_F(IMBPartialUpdateTest, donot_mark_outside_image)
170{
171 imbuf::ChangesetID changeset_id = -1;
172 /* First tile should always return a full update. */
173 EXPECT_EQ(collect(image_buffer, changeset_id).kind, Changes::Kind::Full);
174 /* Second invoke should now detect no changes. */
175 EXPECT_EQ(collect(image_buffer, changeset_id).kind, Changes::Kind::None);
176
177 /* Axis. */
178 IMB_partial_update_mark_region(image_buffer, {-100, 0, 50, 100});
179 EXPECT_EQ(collect(image_buffer, changeset_id).kind, Changes::Kind::None);
180
181 IMB_partial_update_mark_region(image_buffer, {1024, 1100, 50, 100});
182 EXPECT_EQ(collect(image_buffer, changeset_id).kind, Changes::Kind::None);
183
184 IMB_partial_update_mark_region(image_buffer, {50, 100, -100, 0});
185 EXPECT_EQ(collect(image_buffer, changeset_id).kind, Changes::Kind::None);
186
187 IMB_partial_update_mark_region(image_buffer, {50, 100, 1024, 1100});
188 EXPECT_EQ(collect(image_buffer, changeset_id).kind, Changes::Kind::None);
189
190 /* Diagonals. */
191 IMB_partial_update_mark_region(image_buffer, {-100, 0, -100, 0});
192 EXPECT_EQ(collect(image_buffer, changeset_id).kind, Changes::Kind::None);
193
194 IMB_partial_update_mark_region(image_buffer, {-100, 0, 1024, 1100});
195 EXPECT_EQ(collect(image_buffer, changeset_id).kind, Changes::Kind::None);
196
197 IMB_partial_update_mark_region(image_buffer, {1024, 1100, -100, 0});
198 EXPECT_EQ(collect(image_buffer, changeset_id).kind, Changes::Kind::None);
199
200 IMB_partial_update_mark_region(image_buffer, {1024, 1100, 1024, 1100});
201 EXPECT_EQ(collect(image_buffer, changeset_id).kind, Changes::Kind::None);
202}
203
204TEST_F(IMBPartialUpdateTest, mark_inside_image)
205{
206 imbuf::ChangesetID changeset_id = -1;
207 /* First should always return a full update. */
208 EXPECT_EQ(collect(image_buffer, changeset_id).kind, Changes::Kind::Full);
209 /* Second should now detect no changes. */
210 EXPECT_EQ(collect(image_buffer, changeset_id).kind, Changes::Kind::None);
211
212 /* Regions that touch the image edges are still inside, and detected as partial changes. */
213 rcti region;
214 BLI_rcti_init(&region, 0, 1, 0, 1);
215 IMB_partial_update_mark_region(image_buffer, region);
216 EXPECT_EQ(collect(image_buffer, changeset_id).kind, Changes::Kind::Partial);
217 EXPECT_EQ(collect(image_buffer, changeset_id).kind, Changes::Kind::None);
218
219 BLI_rcti_init(&region, 1023, 1024, 0, 1);
220 IMB_partial_update_mark_region(image_buffer, region);
221 EXPECT_EQ(collect(image_buffer, changeset_id).kind, Changes::Kind::Partial);
222 EXPECT_EQ(collect(image_buffer, changeset_id).kind, Changes::Kind::None);
223
224 BLI_rcti_init(&region, 1023, 1024, 1023, 1024);
225 IMB_partial_update_mark_region(image_buffer, region);
226 EXPECT_EQ(collect(image_buffer, changeset_id).kind, Changes::Kind::Partial);
227}
228
229TEST_F(IMBPartialUpdateTest, sequential_mark_region)
230{
231 imbuf::ChangesetID changeset_id = -1;
232 /* First should always return a full update. */
233 EXPECT_EQ(collect(image_buffer, changeset_id).kind, Changes::Kind::Full);
234 /* Second should now detect no changes. */
235 EXPECT_EQ(collect(image_buffer, changeset_id).kind, Changes::Kind::None);
236
237 {
238 /* Mark region. */
239 rcti region;
240 BLI_rcti_init(&region, 10, 20, 40, 50);
241 IMB_partial_update_mark_region(image_buffer, region);
242
243 /* Partial Update should be available. */
244 const Changes changes = collect(image_buffer, changeset_id);
246
247 /* Check regions. */
248 const Vector<rcti> regions = changes.modified_regions();
249 ASSERT_EQ(regions.size(), 1);
250 EXPECT_TRUE(BLI_rcti_inside_rcti(&regions.first(), &region));
251
252 EXPECT_EQ(collect(image_buffer, changeset_id).kind, Changes::Kind::None);
253 }
254
255 {
256 /* Mark different region. */
257 rcti region;
258 BLI_rcti_init(&region, 710, 720, 740, 750);
259 IMB_partial_update_mark_region(image_buffer, region);
260
261 /* Partial Update should be available. */
262 const Changes changes = collect(image_buffer, changeset_id);
264
265 /* Check regions. */
266 const Vector<rcti> regions = changes.modified_regions();
267 ASSERT_EQ(regions.size(), 1);
268 EXPECT_TRUE(BLI_rcti_inside_rcti(&regions.first(), &region));
269
270 EXPECT_EQ(collect(image_buffer, changeset_id).kind, Changes::Kind::None);
271 }
272}
273
274TEST_F(IMBPartialUpdateTest, mark_multiple_chunks)
275{
276 imbuf::ChangesetID changeset_id = -1;
277 /* First should always return a full update. */
278 EXPECT_EQ(collect(image_buffer, changeset_id).kind, Changes::Kind::Full);
279 /* Second should now detect no changes. */
280 EXPECT_EQ(collect(image_buffer, changeset_id).kind, Changes::Kind::None);
281
282 /* Mark a region spanning multiple chunks. */
283 static_assert(CHUNK_SIZE == 256);
284 rcti region;
285 BLI_rcti_init(&region, 300, 700, 300, 700);
286 IMB_partial_update_mark_region(image_buffer, region);
287
288 /* Partial Update should be available. */
289 const Changes changes = collect(image_buffer, changeset_id);
291
292 /* Contiguous modified chunks in a row are merged. */
293 const Vector<rcti> regions = changes.modified_regions();
294 ASSERT_EQ(regions.size(), 1);
295 EXPECT_TRUE(BLI_rcti_inside_rcti(&regions.first(), &region));
296}
297
298} // namespace blender::imbuf::partial_update::tests
int64_t size() const
const T & first() const
#define EXPECT_TRUE(result)
static Changes collect(ImBuf *buffer, imbuf::ChangesetID &changeset_id)
TEST_F(IMBPartialUpdateTest, initial_full_update)
imbuf::ChangesetID IMB_partial_update_changeset_id_current()
void IMB_freeImBuf(ImBuf *ibuf)
void IMB_partial_update_mark_region(ImBuf *ibuf, const rcti &region)
void IMB_partial_update_mark_full(ImBuf *ibuf)
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
EXPECT_EQ(BLI_expr_pylike_eval(expr, nullptr, 0, &result), EXPR_PYLIKE_INVALID)
imbuf::partial_update::Changes IMB_partial_update_collect(ImBuf *ibuf, imbuf::ChangesetID last_changeset_id)
void IMB_partial_update_flush(ImBuf *ibuf)
bool IMB_scale(ImBuf *ibuf, int2 new_size, IMBScaleFilter filter, bool threaded=true)
Definition scaling.cc:467
bool BLI_rcti_inside_rcti(const rcti *rct_a, const rcti *rct_b)
Definition rct.cc:200