1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
gpu / command_buffer / client / shared_image_pool_unittest.cc [blame]
// Copyright 2024 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "gpu/command_buffer/client/shared_image_pool.h"
#include "base/test/task_environment.h"
#include "base/time/time.h"
#include "components/viz/test/test_context_provider.h"
#include "gpu/command_buffer/client/shared_image_interface.h"
#include "testing/gmock/include/gmock/gmock.h"
#include "testing/gtest/include/gtest/gtest.h"
namespace gpu {
class SharedImagePoolTest : public testing::Test {
public:
SharedImagePoolTest() = default;
~SharedImagePoolTest() override = default;
protected:
void SetUp() override {
test_sii_ = base::MakeRefCounted<gpu::TestSharedImageInterface>();
}
base::test::TaskEnvironment task_environment_{
base::test::TaskEnvironment::TimeSource::MOCK_TIME};
scoped_refptr<gpu::TestSharedImageInterface> test_sii_;
};
// Extending ClientImage for testing.
class TestClientImage : public ClientImage {
public:
explicit TestClientImage(scoped_refptr<ClientSharedImage> shared_image)
: ClientImage(std::move(shared_image)) {
static int i = 0;
id = ++i;
}
int id = 0;
protected:
friend class base::RefCounted<TestClientImage>;
~TestClientImage() override = default;
};
// Class used for showcasing complex usage of SharedImagePool with additional
// metadata in ClientImage.
class ExtendedClientImage : public ClientImage {
public:
int extra_metadata = 0;
explicit ExtendedClientImage(scoped_refptr<ClientSharedImage> shared_image)
: ClientImage(std::move(shared_image)) {}
void SetMetadata(int metadata) { extra_metadata = metadata; }
protected:
friend class base::RefCounted<ExtendedClientImage>;
~ExtendedClientImage() override = default;
};
// Test for verifying if shared image and creation sync token have been created.
TEST_F(SharedImagePoolTest, VerifyImage) {
ImageInfo info = {
gfx::Size(1920, 1080), viz::SinglePlaneFormat::kRGBA_8888, {}};
auto pool = SharedImagePool<ClientImage>::Create(info, test_sii_);
auto image = pool->GetImage();
// Verify shared image is created.
EXPECT_TRUE(image->GetSharedImage() != nullptr);
// Verify the SyncToken has data.
EXPECT_TRUE(image->GetSyncToken().HasData());
}
// Test for verifying releasing and recycling images in the pool.
TEST_F(SharedImagePoolTest, ReleaseAndRecycleImage) {
ImageInfo info = {
gfx::Size(1920, 1080), viz::SinglePlaneFormat::kRGBA_8888, {}};
auto pool = SharedImagePool<TestClientImage>::Create(info, test_sii_);
auto image1 = pool->GetImage();
auto image1_id = image1->id;
pool->ReleaseImage(std::move(image1));
auto recycled_image = pool->GetImage();
auto recycled_image_id = recycled_image->id;
// Check if the recycled image is the same as the one released.
EXPECT_EQ(recycled_image_id, image1_id);
}
// Test the pool's behavior when it reaches maximum capacity.
TEST_F(SharedImagePoolTest, MaxPoolSizeBehavior) {
ImageInfo info = {
gfx::Size(1024, 768), viz::SinglePlaneFormat::kRGBA_8888, {}};
const auto max_pool_size = 1;
auto pool =
SharedImagePool<TestClientImage>::Create(info, test_sii_, max_pool_size);
auto image1 = pool->GetImage();
auto image2 = pool->GetImage();
auto image2_id = image2->id;
pool->ReleaseImage(std::move(image1));
// This image should not be stored as the pool is full.
pool->ReleaseImage(std::move(image2));
auto retrieved_image = pool->GetImage();
auto retrieved_image_id = retrieved_image->id;
// The second image should not be the same as retrieved image.
EXPECT_NE(retrieved_image_id, image2_id);
}
// Test for verifying maximum pool size behavior.
TEST_F(SharedImagePoolTest, MaxPoolSizeEnforcement) {
ImageInfo info = {
gfx::Size(1024, 768), viz::SinglePlaneFormat::kRGBA_8888, {}};
const size_t max_pool_size = 1;
auto pool =
SharedImagePool<ClientImage>::Create(info, test_sii_, max_pool_size);
auto image1 = pool->GetImage();
auto image2 = pool->GetImage();
pool->ReleaseImage(std::move(image1));
// Should trigger destruction of one image.
pool->ReleaseImage(std::move(image2));
// Pool should not exceed max size.
EXPECT_EQ(pool->GetPoolSizeForTesting(), max_pool_size);
}
// Test for verifying release sync token behaviour.
TEST_F(SharedImagePoolTest, TokenConsistencyOnReuse) {
ImageInfo info = {
gfx::Size(1920, 1080), viz::SinglePlaneFormat::kRGBA_8888, {}};
auto pool = SharedImagePool<ClientImage>::Create(info, test_sii_);
auto image = pool->GetImage();
gpu::SyncToken release_token = test_sii_->GenUnverifiedSyncToken();
image->SetReleaseSyncToken(release_token);
pool->ReleaseImage(std::move(image));
auto reused_image = pool->GetImage();
EXPECT_EQ(reused_image->GetSyncToken(), release_token);
}
// Test for verifying release sync token behaviour.
TEST_F(SharedImagePoolTest, ProperTokenHandlingBeforeReuse) {
ImageInfo info = {
gfx::Size(800, 600), viz::SinglePlaneFormat::kRGBA_8888, {}};
const size_t max_pool_size = 1;
auto pool =
SharedImagePool<ClientImage>::Create(info, test_sii_, max_pool_size);
auto image1 = pool->GetImage();
auto image2 = pool->GetImage();
// |image1| will be cached in the pool.
gpu::SyncToken release_token1 = test_sii_->GenUnverifiedSyncToken();
image1->SetReleaseSyncToken(release_token1);
pool->ReleaseImage(std::move(image1));
// |image2| will be destroyed since the cache is full.
gpu::SyncToken release_token2 = test_sii_->GenUnverifiedSyncToken();
image2->SetReleaseSyncToken(release_token2);
pool->ReleaseImage(std::move(image2));
// |image3| will be re-used from the pool.
auto image3 = pool->GetImage();
EXPECT_EQ(image3->GetSyncToken(), release_token1);
EXPECT_TRUE(image3->GetSyncToken().HasData());
}
// Test for showcasing complex client usage via ExtendedClientImage.
TEST_F(SharedImagePoolTest, ComplexClientUsage) {
ImageInfo info = {
gfx::Size(1280, 720), viz::SinglePlaneFormat::kRGBA_8888, {}};
auto pool = SharedImagePool<ExtendedClientImage>::Create(info, test_sii_);
scoped_refptr<ExtendedClientImage> extended_client_image = pool->GetImage();
EXPECT_TRUE(extended_client_image);
// Sets additional metadata.
extended_client_image->SetMetadata(42);
// Verify metadata is set and retrieved correctly
EXPECT_EQ(extended_client_image->extra_metadata, 42);
// Verify that the SyncToken within the image is valid.
EXPECT_TRUE(extended_client_image->GetSyncToken().HasData());
}
// Test to ensure that images from a pool with one configuration are not reused
// in a pool with a different configuration.
TEST_F(SharedImagePoolTest, DiscardImageFromDifferentPool) {
ImageInfo pool1_info = {
gfx::Size(1920, 1080), viz::SinglePlaneFormat::kRGBA_8888, {}};
ImageInfo pool2_info = {
gfx::Size(800, 600), viz::SinglePlaneFormat::kRGBA_8888, {}};
auto pool1 = SharedImagePool<ClientImage>::Create(pool1_info, test_sii_);
auto pool2 =
SharedImagePool<ClientImage>::Create(pool2_info, test_sii_.get());
auto image_from_pool1 = pool1->GetImage();
EXPECT_TRUE(image_from_pool1);
// Attempt to release this image into pool2 which has different ImageInfo.
pool2->ReleaseImage(std::move(image_from_pool1));
// Check that pool2 has not accepted the image from pool1 due to mismatched
// ImageInfo.
EXPECT_EQ(pool2->GetPoolSizeForTesting(), size_t(0));
}
// Test Reconfigure method of SharedImagePool.
TEST_F(SharedImagePoolTest, ReconfigurePool) {
// Initial ImageInfo.
ImageInfo initial_info = {
gfx::Size(1920, 1080), viz::SinglePlaneFormat::kRGBA_8888, {}};
// Create the pool with an initial configuration.
auto pool = SharedImagePool<ClientImage>::Create(
initial_info, test_sii_.get(), /*max_pool_size=*/2);
// Create a different ImageInfo.
ImageInfo new_info = {
gfx::Size(1280, 720), viz::SinglePlaneFormat::kBGRA_8888, {}};
// Reconfigure with a different ImageInfo.
pool->Reconfigure(new_info);
// Verify that the pool was reconfigured.
EXPECT_EQ(pool->GetImageInfo(), new_info);
// The pool should now return images with the new configuration.
auto image = pool->GetImage();
ASSERT_TRUE(image);
EXPECT_EQ(image->GetSharedImage()->size(), new_info.size);
EXPECT_EQ(image->GetSharedImage()->format(), new_info.format);
// Attempt to reconfigure with the same ImageInfo, which should be a no-op.
pool->Reconfigure(new_info);
// Ensure the ImageInfo remains unchanged.
EXPECT_EQ(pool->GetImageInfo(), new_info);
// Confirm that images created after reconfiguration still follow the new
// configuration
auto new_image = pool->GetImage();
ASSERT_TRUE(new_image);
EXPECT_EQ(new_image->GetSharedImage()->size(), new_info.size);
EXPECT_EQ(new_image->GetSharedImage()->format(), new_info.format);
}
// Test for setting the release sync token in ClientImage.
TEST_F(SharedImagePoolTest, SetReleaseSyncToken) {
ImageInfo info = {
gfx::Size(1024, 768), viz::SinglePlaneFormat::kRGBA_8888, {}};
auto pool = SharedImagePool<TestClientImage>::Create(info, test_sii_);
// Create a new ClientImage object from the pool.
auto client_image = pool->GetImage();
// Verify that the client image was successfully created.
EXPECT_TRUE(client_image != nullptr);
// Create a dummy SyncToken to simulate release.
SyncToken release_sync_token(gpu::CommandBufferNamespace::GPU_IO,
gpu::CommandBufferId::FromUnsafeValue(1), 12345);
// Set the release SyncToken.
client_image->SetReleaseSyncToken(release_sync_token);
// Verify that the release SyncToken was set correctly.
EXPECT_EQ(client_image->GetSyncToken(), release_sync_token);
}
// Test that SharedImagePool creates a mappable shared image when buffer_usage
// is set.
TEST_F(SharedImagePoolTest, CreatesMappableSharedImageWhenBufferUsageIsSet) {
test_sii_->UseTestGMBInSharedImageCreationWithBufferUsage();
// Define ImageInfo with buffer_usage set.
ImageInfo info = {gfx::Size(100, 100),
viz::SinglePlaneFormat::kRGBA_8888,
{},
gfx::BufferUsage::GPU_READ};
auto pool = SharedImagePool<ClientImage>::Create(info, test_sii_);
// Expect CreateSharedImage to be called with buffer_usage specified.
EXPECT_CALL(*test_sii_,
DoCreateSharedImage(
gfx::Size(100, 100), viz::SinglePlaneFormat::kRGBA_8888,
gpu::kNullSurfaceHandle, gfx::BufferUsage::GPU_READ));
scoped_refptr<ClientImage> image = pool->GetImage();
EXPECT_NE(image, nullptr);
EXPECT_EQ(image->GetSharedImage()->buffer_usage(),
gfx::BufferUsage::GPU_READ);
}
TEST_F(SharedImagePoolTest, DoesNotReuseSharedImageWithDifferentBufferUsage) {
test_sii_->UseTestGMBInSharedImageCreationWithBufferUsage();
// Define ImageInfo with initial buffer_usage.
ImageInfo info = {gfx::Size(100, 100),
viz::SinglePlaneFormat::kRGBA_8888,
{},
gfx::BufferUsage::GPU_READ};
auto pool = SharedImagePool<ClientImage>::Create(info, test_sii_);
// Expect CreateSharedImage to be called with initial buffer_usage.
EXPECT_CALL(*test_sii_,
DoCreateSharedImage(
gfx::Size(100, 100), viz::SinglePlaneFormat::kRGBA_8888,
gpu::kNullSurfaceHandle, gfx::BufferUsage::GPU_READ))
.Times(1);
// First image created with GPU_READ usage.
scoped_refptr<ClientImage> image1 = pool->GetImage();
EXPECT_NE(image1, nullptr);
EXPECT_EQ(image1->GetSharedImage()->buffer_usage(),
gfx::BufferUsage::GPU_READ);
// Release the first image back to the pool.
pool->ReleaseImage(std::move(image1));
// Change buffer usage to GPU_READ_CPU_READ_WRITE.
info.buffer_usage = gfx::BufferUsage::GPU_READ_CPU_READ_WRITE;
pool->Reconfigure(info);
// Expect CreateSharedImage to be called again with new
// buffer_usage.
EXPECT_CALL(*test_sii_,
DoCreateSharedImage(gfx::Size(100, 100),
viz::SinglePlaneFormat::kRGBA_8888,
gpu::kNullSurfaceHandle,
gfx::BufferUsage::GPU_READ_CPU_READ_WRITE))
.Times(1);
// Second image created with GPU_READ_CPU_READ_WRITE usage. It should be a new
// image.
scoped_refptr<ClientImage> image2 = pool->GetImage();
EXPECT_NE(image2, nullptr);
EXPECT_EQ(image2->GetSharedImage()->buffer_usage(),
gfx::BufferUsage::GPU_READ_CPU_READ_WRITE);
// Ensure the new image is different from the first one due to different
// buffer usage.
EXPECT_NE(image1, image2);
}
// Test for verifying the reclaim timer is started when an image is released.
TEST_F(SharedImagePoolTest, ReclaimTimerStartedOnRelease) {
ImageInfo info = {
gfx::Size(1920, 1080), viz::SinglePlaneFormat::kRGBA_8888, {}};
constexpr auto kExpirationTime = base::Seconds(30);
auto pool = SharedImagePool<ClientImage>::Create(
info, test_sii_,
/*max_pool_size=*/std::nullopt, kExpirationTime);
auto image = pool->GetImage();
pool->ReleaseImage(std::move(image));
EXPECT_TRUE(pool->IsReclaimTimerRunningForTesting());
}
// Test for verifying the reclaim timer is not started when expiration time is
// not set.
TEST_F(SharedImagePoolTest, ReclaimTimerNotStartedWhenExpirationTimeNotSet) {
ImageInfo info = {
gfx::Size(1920, 1080), viz::SinglePlaneFormat::kRGBA_8888, {}};
auto pool = SharedImagePool<ClientImage>::Create(
info, test_sii_, /*max_pool_size=*/std::nullopt,
/*unused_resource_expiration_time=*/std::nullopt);
auto image = pool->GetImage();
pool->ReleaseImage(std::move(image));
EXPECT_FALSE(pool->IsReclaimTimerRunningForTesting());
}
// Test for verifying the reclaim timer is not started when the pool is empty.
TEST_F(SharedImagePoolTest, ReclaimTimerNotStartedWhenPoolIsEmpty) {
ImageInfo info = {
gfx::Size(1920, 1080), viz::SinglePlaneFormat::kRGBA_8888, {}};
constexpr auto kExpirationTime = base::Seconds(30);
auto pool = SharedImagePool<ClientImage>::Create(
info, test_sii_,
/*max_pool_size=*/std::nullopt, kExpirationTime);
EXPECT_FALSE(pool->IsReclaimTimerRunningForTesting());
}
// Test for verifying that unused resources are reclaimed after expiration.
TEST_F(SharedImagePoolTest, UnusedResourcesReclaimedAfterExpiration) {
ImageInfo info = {
gfx::Size(1920, 1080), viz::SinglePlaneFormat::kRGBA_8888, {}};
constexpr auto kExpirationTime = base::Seconds(30);
auto pool = SharedImagePool<ClientImage>::Create(
info, test_sii_,
/*max_pool_size=*/std::nullopt, kExpirationTime);
auto image1 = pool->GetImage();
pool->ReleaseImage(std::move(image1));
EXPECT_EQ(pool->GetPoolSizeForTesting(), 1u);
// Advance time past the expiration time.
task_environment_.FastForwardBy(kExpirationTime + base::Seconds(1));
// Verify that the image is reclaimed.
EXPECT_EQ(pool->GetPoolSizeForTesting(), 0u);
}
// Test for verifying that unused resources are not reclaimed before expiration.
TEST_F(SharedImagePoolTest, UnusedResourcesNotReclaimedBeforeExpiration) {
ImageInfo info = {
gfx::Size(1920, 1080), viz::SinglePlaneFormat::kRGBA_8888, {}};
constexpr auto kExpirationTime = base::Seconds(30);
auto pool = SharedImagePool<ClientImage>::Create(
info, test_sii_,
/*max_pool_size=*/std::nullopt, kExpirationTime);
auto image1 = pool->GetImage();
pool->ReleaseImage(std::move(image1));
EXPECT_EQ(pool->GetPoolSizeForTesting(), 1u);
// Advance time to just before the expiration time.
task_environment_.FastForwardBy(kExpirationTime - base::Seconds(1));
// Verify that the image is not reclaimed.
EXPECT_EQ(pool->GetPoolSizeForTesting(), 1u);
}
// Test to verify that only resources older than the expiration time are
// reclaimed.
TEST_F(SharedImagePoolTest, OnlyResourcesOlderThanExpirationAreReclaimed) {
ImageInfo info = {
gfx::Size(1920, 1080), viz::SinglePlaneFormat::kRGBA_8888, {}};
constexpr auto kExpirationTime = base::Seconds(30);
auto pool = SharedImagePool<ClientImage>::Create(
info, test_sii_,
/*max_pool_size=*/std::nullopt, kExpirationTime);
auto image1 = pool->GetImage();
pool->ReleaseImage(std::move(image1));
// Advance time to half the expiration time.
task_environment_.FastForwardBy(kExpirationTime / 2);
auto image2 = pool->GetImage();
pool->ReleaseImage(std::move(image2));
// Advance time past the expiration time.
task_environment_.FastForwardBy(kExpirationTime / 2 + base::Seconds(1));
// Verify that only the first image is reclaimed.
EXPECT_EQ(pool->GetPoolSizeForTesting(), 1u);
}
// Test to verify that the reclaim timer is restarted after reclaiming
// resources.
TEST_F(SharedImagePoolTest, ReclaimTimerRestartedAfterReclaiming) {
ImageInfo info = {
gfx::Size(1920, 1080), viz::SinglePlaneFormat::kRGBA_8888, {}};
constexpr auto kExpirationTime = base::Seconds(30);
auto pool = SharedImagePool<ClientImage>::Create(
info, test_sii_,
/*max_pool_size=*/std::nullopt, kExpirationTime);
auto image1 = pool->GetImage();
pool->ReleaseImage(std::move(image1));
// Advance time past the expiration time.
task_environment_.FastForwardBy(kExpirationTime + base::Seconds(1));
// Verify that the image is reclaimed.
EXPECT_EQ(pool->GetPoolSizeForTesting(), 0u);
auto image2 = pool->GetImage();
pool->ReleaseImage(std::move(image2));
EXPECT_EQ(pool->GetPoolSizeForTesting(), 1u);
EXPECT_TRUE(pool->IsReclaimTimerRunningForTesting());
}
// Test to verify that Flush() is called on SharedImageInterface after
// reclaiming resources.
TEST_F(SharedImagePoolTest, FlushCalledAfterReclaiming) {
ImageInfo info = {
gfx::Size(1920, 1080), viz::SinglePlaneFormat::kRGBA_8888, {}};
constexpr auto kExpirationTime = base::Seconds(30);
auto pool = SharedImagePool<ClientImage>::Create(
info, test_sii_,
/*max_pool_size=*/std::nullopt, kExpirationTime);
auto image = pool->GetImage();
pool->ReleaseImage(std::move(image));
// Expect that Flush() will be called on the SharedImageInterface.
EXPECT_CALL(*test_sii_, DoFlush()).Times(1);
// Advance time past the expiration time.
task_environment_.FastForwardBy(kExpirationTime + base::Seconds(1));
}
// Test to check that images created by different pools have unique pool IDs.
TEST_F(SharedImagePoolTest, DifferentPoolsHaveDifferentPoolIds) {
ImageInfo info1 = {
gfx::Size(1920, 1080), viz::SinglePlaneFormat::kRGBA_8888, {}};
auto pool1 = SharedImagePool<ClientImage>::Create(info1, test_sii_);
ImageInfo info2 = {
gfx::Size(200, 200), viz::SinglePlaneFormat::kBGRA_8888, {}};
auto pool2 = SharedImagePool<ClientImage>::Create(info2, test_sii_);
auto image_from_first_pool = pool1->GetImage();
auto image_from_second_pool = pool2->GetImage();
ASSERT_NE(image_from_first_pool, nullptr);
ASSERT_NE(image_from_second_pool, nullptr);
// Verify that pool IDs are different for images from different pools.
EXPECT_NE(image_from_first_pool->GetPoolIdForTesting(),
image_from_second_pool->GetPoolIdForTesting());
}
} // namespace gpu