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
gpu / command_buffer / common / discardable_handle_unittest.cc [blame]
// Copyright 2017 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/common/discardable_handle.h"
#include "gpu/command_buffer/common/buffer.h"
#include "testing/gtest/include/gtest/gtest.h"
namespace gpu {
namespace {
scoped_refptr<Buffer> MakeBufferForTesting(uint32_t num_handles) {
uint32_t size = sizeof(base::subtle::Atomic32) * num_handles;
return MakeMemoryBuffer(size);
}
} // namespace
TEST(DiscardableHandleTest, BasicUsage) {
scoped_refptr<Buffer> buffer = MakeBufferForTesting(1);
uint32_t byte_offset = 0;
int32_t shm_id = 1;
ClientDiscardableHandle client_handle(buffer, byte_offset, shm_id);
EXPECT_EQ(client_handle.shm_id(), shm_id);
EXPECT_TRUE(client_handle.IsLockedForTesting());
ServiceDiscardableHandle service_handle(buffer, byte_offset, shm_id);
EXPECT_EQ(service_handle.shm_id(), shm_id);
EXPECT_TRUE(service_handle.IsLockedForTesting());
EXPECT_FALSE(service_handle.Delete());
EXPECT_FALSE(service_handle.IsDeletedForTesting());
EXPECT_FALSE(client_handle.CanBeReUsed());
service_handle.Unlock();
EXPECT_FALSE(service_handle.IsLockedForTesting());
EXPECT_FALSE(client_handle.IsLockedForTesting());
EXPECT_TRUE(client_handle.Lock());
EXPECT_TRUE(client_handle.IsLockedForTesting());
EXPECT_TRUE(service_handle.IsLockedForTesting());
service_handle.Unlock();
EXPECT_FALSE(service_handle.IsLockedForTesting());
EXPECT_FALSE(client_handle.IsLockedForTesting());
EXPECT_TRUE(service_handle.Delete());
EXPECT_TRUE(service_handle.IsDeletedForTesting());
EXPECT_TRUE(client_handle.CanBeReUsed());
EXPECT_FALSE(service_handle.IsLockedForTesting());
EXPECT_FALSE(client_handle.IsLockedForTesting());
EXPECT_FALSE(client_handle.Lock());
EXPECT_FALSE(service_handle.IsLockedForTesting());
EXPECT_FALSE(client_handle.IsLockedForTesting());
EXPECT_TRUE(service_handle.IsDeletedForTesting());
EXPECT_TRUE(client_handle.IsDeletedForTesting());
}
TEST(DiscardableHandleTest, MultiLock) {
scoped_refptr<Buffer> buffer = MakeBufferForTesting(1);
uint32_t byte_offset = 0;
int32_t shm_id = 1;
ClientDiscardableHandle client_handle(buffer, byte_offset, shm_id);
EXPECT_EQ(client_handle.shm_id(), shm_id);
EXPECT_TRUE(client_handle.IsLockedForTesting());
ServiceDiscardableHandle service_handle(buffer, byte_offset, shm_id);
EXPECT_EQ(service_handle.shm_id(), shm_id);
EXPECT_TRUE(service_handle.IsLockedForTesting());
for (int i = 1; i < 10; ++i) {
EXPECT_TRUE(client_handle.IsLockedForTesting());
EXPECT_TRUE(service_handle.IsLockedForTesting());
EXPECT_TRUE(client_handle.Lock());
}
for (int i = 0; i < 10; ++i) {
EXPECT_TRUE(client_handle.IsLockedForTesting());
EXPECT_TRUE(service_handle.IsLockedForTesting());
service_handle.Unlock();
}
EXPECT_FALSE(client_handle.IsLockedForTesting());
EXPECT_FALSE(service_handle.IsLockedForTesting());
}
TEST(DiscardableHandleTest, Suballocations) {
static const int32_t num_elements = 10;
scoped_refptr<Buffer> buffer = MakeBufferForTesting(num_elements);
std::vector<ClientDiscardableHandle> client_handles;
std::vector<ServiceDiscardableHandle> service_handles;
for (int32_t i = 0; i < num_elements; ++i) {
client_handles.emplace_back(buffer, sizeof(base::subtle::Atomic32) * i,
i + 1);
EXPECT_EQ(client_handles[i].shm_id(), i + 1);
EXPECT_TRUE(client_handles[i].IsLockedForTesting());
service_handles.emplace_back(buffer, sizeof(base::subtle::Atomic32) * i,
i + 1);
EXPECT_EQ(service_handles[i].shm_id(), i + 1);
EXPECT_TRUE(service_handles[i].IsLockedForTesting());
}
for (int32_t i = 0; i < num_elements; i += 2) {
service_handles[i].Unlock();
}
for (int32_t i = 1; i < num_elements; i += 2) {
client_handles[i].Lock();
}
for (int32_t i = 0; i < num_elements; ++i) {
if (i % 2) {
EXPECT_TRUE(client_handles[i].IsLockedForTesting());
EXPECT_TRUE(service_handles[i].IsLockedForTesting());
} else {
EXPECT_FALSE(client_handles[i].IsLockedForTesting());
EXPECT_FALSE(service_handles[i].IsLockedForTesting());
}
}
}
} // namespace gpu