Blender
V5.3
source
blender
imbuf
tests
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
15
namespace
blender::imbuf::partial_update::tests
{
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. */
19
static
Changes
collect
(
ImBuf
*buffer,
imbuf::ChangesetID
&changeset_id)
20
{
21
IMB_partial_update_flush
(buffer);
22
const
imbuf::ChangesetID
new_changeset_id =
IMB_partial_update_changeset_id_current
();
23
Changes
changes =
IMB_partial_update_collect
(buffer, changeset_id);
24
changeset_id = new_changeset_id;
25
return
changes;
26
}
27
28
class
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. */
35
image_buffer
=
IMB_allocImBuf
(1024, 1024,
ImBufFlags::Zero
);
36
}
37
38
void
TearDown
()
override
39
{
40
IMB_freeImBuf
(
image_buffer
);
41
}
42
};
43
44
TEST_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);
49
const
imbuf::ChangesetID
new_changeset_id =
IMB_partial_update_changeset_id_current
();
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
54
TEST_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
70
TEST_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
(®ion, 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
86
TEST_F
(
IMBPartialUpdateTest
, resize)
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
(®ion, 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
115
TEST_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
(®ion, 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);
130
EXPECT_EQ
(changes.
kind
,
Changes::Kind::Partial
);
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
(®ions.
first
(), ®ion));
136
137
EXPECT_EQ
(
collect
(image_buffer, changeset_id).kind,
Changes::Kind::None
);
138
}
139
140
TEST_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
(®ion_a, 10, 20, 40, 50);
149
IMB_partial_update_mark_region
(image_buffer, region_a);
150
rcti
region_b;
151
BLI_rcti_init
(®ion_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);
155
EXPECT_EQ
(changes.
kind
,
Changes::Kind::Partial
);
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, ®ion_a);
163
found_b |=
BLI_rcti_inside_rcti
(&changed, ®ion_b);
164
}
165
EXPECT_TRUE
(found_a);
166
EXPECT_TRUE
(found_b);
167
}
168
169
TEST_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
204
TEST_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
(®ion, 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
(®ion, 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
(®ion, 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
229
TEST_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
(®ion, 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);
245
EXPECT_EQ
(changes.
kind
,
Changes::Kind::Partial
);
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
(®ions.
first
(), ®ion));
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
(®ion, 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);
263
EXPECT_EQ
(changes.
kind
,
Changes::Kind::Partial
);
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
(®ions.
first
(), ®ion));
269
270
EXPECT_EQ
(
collect
(image_buffer, changeset_id).kind,
Changes::Kind::None
);
271
}
272
}
273
274
TEST_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
(®ion, 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);
290
EXPECT_EQ
(changes.
kind
,
Changes::Kind::Partial
);
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
(®ions.
first
(), ®ion));
296
}
297
298
}
// namespace blender::imbuf::partial_update::tests
BLI_rect.hh
IMB_imbuf.hh
IMB_imbuf_types.hh
IMB_partial_update.hh
blender::Vector
Definition
BLI_vector.hh:76
blender::Vector::size
int64_t size() const
Definition
BLI_vector.hh:780
blender::Vector::first
const T & first() const
Definition
BLI_vector.hh:766
blender::imbuf::partial_update::tests::IMBPartialUpdateTest
Definition
IMB_partial_update_test.cc:28
blender::imbuf::partial_update::tests::IMBPartialUpdateTest::image_buffer
ImBuf * image_buffer
Definition
IMB_partial_update_test.cc:30
blender::imbuf::partial_update::tests::IMBPartialUpdateTest::TearDown
void TearDown() override
Definition
IMB_partial_update_test.cc:38
blender::imbuf::partial_update::tests::IMBPartialUpdateTest::SetUp
void SetUp() override
Definition
IMB_partial_update_test.cc:32
EXPECT_TRUE
#define EXPECT_TRUE(result)
Definition
gpu_shader_test_lib.bsl.hh:198
blender::imbuf::partial_update::tests
Definition
IMB_partial_update_test.cc:15
blender::imbuf::partial_update::tests::collect
static Changes collect(ImBuf *buffer, imbuf::ChangesetID &changeset_id)
Definition
IMB_partial_update_test.cc:19
blender::imbuf::partial_update::tests::TEST_F
TEST_F(IMBPartialUpdateTest, initial_full_update)
Definition
IMB_partial_update_test.cc:44
blender::imbuf::partial_update::CHUNK_SIZE
constexpr int CHUNK_SIZE
Definition
IMB_partial_update.hh:61
blender::imbuf::ChangesetID
int64_t ChangesetID
Definition
IMB_imbuf_types.hh:38
blender::IMB_partial_update_changeset_id_current
imbuf::ChangesetID IMB_partial_update_changeset_id_current()
Definition
partial_update.cc:394
blender::IMB_freeImBuf
void IMB_freeImBuf(ImBuf *ibuf)
Definition
allocimbuf.cc:144
blender::ImBufFlags::Zero
@ Zero
Definition
IMB_imbuf_enums.h:18
blender::IMB_partial_update_mark_region
void IMB_partial_update_mark_region(ImBuf *ibuf, const rcti ®ion)
Definition
partial_update.cc:399
blender::IMB_partial_update_mark_full
void IMB_partial_update_mark_full(ImBuf *ibuf)
Definition
partial_update.cc:410
blender::IMB_allocImBuf
ImBuf * IMB_allocImBuf(unsigned int x, unsigned int y, ImBufFlags flags)
Definition
allocimbuf.cc:367
blender::BLI_rcti_init
void BLI_rcti_init(struct rcti *rect, int xmin, int xmax, int ymin, int ymax)
Definition
rct.cc:428
blender::EXPECT_EQ
EXPECT_EQ(BLI_expr_pylike_eval(expr, nullptr, 0, &result), EXPR_PYLIKE_INVALID)
blender::IMB_partial_update_collect
imbuf::partial_update::Changes IMB_partial_update_collect(ImBuf *ibuf, imbuf::ChangesetID last_changeset_id)
Definition
partial_update.cc:435
blender::IMB_partial_update_flush
void IMB_partial_update_flush(ImBuf *ibuf)
Definition
partial_update.cc:427
blender::IMB_scale
bool IMB_scale(ImBuf *ibuf, int2 new_size, IMBScaleFilter filter, bool threaded=true)
Definition
scaling.cc:467
blender::BLI_rcti_inside_rcti
bool BLI_rcti_inside_rcti(const rcti *rct_a, const rcti *rct_b)
Definition
rct.cc:200
blender::IMBScaleFilter::Nearest
@ Nearest
Definition
IMB_imbuf.hh:364
blender::ImBuf
Definition
IMB_imbuf_types.hh:171
blender::imbuf::partial_update::Changes
Definition
IMB_partial_update.hh:64
blender::imbuf::partial_update::Changes::Kind::Resized
@ Resized
Definition
IMB_partial_update.hh:73
blender::imbuf::partial_update::Changes::Kind::Partial
@ Partial
Definition
IMB_partial_update.hh:69
blender::imbuf::partial_update::Changes::Kind::None
@ None
Definition
IMB_partial_update.hh:67
blender::imbuf::partial_update::Changes::Kind::Full
@ Full
Definition
IMB_partial_update.hh:71
blender::imbuf::partial_update::Changes::kind
Kind kind
Definition
IMB_partial_update.hh:76
blender::imbuf::partial_update::Changes::modified_regions
Vector< rcti > modified_regions() const
Definition
partial_update.cc:302
blender::rcti
Definition
DNA_vec_types.h:70
Generated on
for Blender by
doxygen
1.18.0