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
gpu / command_buffer / service / shared_image / shared_image_manager_unittest.cc [blame]
// Copyright 2018 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/service/shared_image/shared_image_manager.h"
#include "base/task/sequenced_task_runner.h"
#include "base/task/task_traits.h"
#include "base/task/thread_pool.h"
#include "base/task/thread_pool/thread_pool_instance.h"
#include "base/trace_event/memory_allocator_dump.h"
#include "base/trace_event/process_memory_dump.h"
#include "gpu/command_buffer/common/mailbox.h"
#include "gpu/command_buffer/common/shared_image_usage.h"
#include "gpu/command_buffer/service/shared_image/shared_image_backing.h"
#include "gpu/command_buffer/service/shared_image/shared_image_representation.h"
#include "gpu/command_buffer/service/shared_image/test_image_backing.h"
#include "testing/gmock/include/gmock/gmock.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "ui/gfx/color_space.h"
namespace gpu {
namespace {
std::unique_ptr<TestImageBacking> CreateImageBacking(size_t size_in_bytes) {
auto mailbox = Mailbox::Generate();
auto format = viz::SinglePlaneFormat::kRGBA_8888;
gfx::Size size(256, 256);
auto color_space = gfx::ColorSpace::CreateSRGB();
auto surface_origin = kTopLeft_GrSurfaceOrigin;
auto alpha_type = kPremul_SkAlphaType;
gpu::SharedImageUsageSet usage = SHARED_IMAGE_USAGE_GLES2_READ;
return std::make_unique<TestImageBacking>(mailbox, format, size, color_space,
surface_origin, alpha_type, usage,
size_in_bytes);
}
TEST(SharedImageManagerTest, BasicRefCounting) {
const size_t kSizeBytes = 1024;
SharedImageManager manager;
auto tracker = std::make_unique<MemoryTypeTracker>(nullptr);
auto mailbox = Mailbox::Generate();
auto format = viz::SinglePlaneFormat::kRGBA_8888;
gfx::Size size(256, 256);
auto color_space = gfx::ColorSpace::CreateSRGB();
auto surface_origin = kTopLeft_GrSurfaceOrigin;
auto alpha_type = kPremul_SkAlphaType;
gpu::SharedImageUsageSet usage = SHARED_IMAGE_USAGE_GLES2_READ;
auto backing = std::make_unique<TestImageBacking>(
mailbox, format, size, color_space, surface_origin, alpha_type, usage,
kSizeBytes);
auto factory_ref = manager.Register(std::move(backing), tracker.get());
EXPECT_EQ(kSizeBytes, tracker->GetMemRepresented());
// Taking/releasing an additional ref/representation with the same tracker
// should have no impact.
{
auto gl_representation = manager.ProduceGLTexture(mailbox, tracker.get());
EXPECT_EQ(kSizeBytes, tracker->GetMemRepresented());
}
// Taking/releasing an additional ref/representation with a new tracker should
// also have no impact.
{
auto tracker2 = std::make_unique<MemoryTypeTracker>(nullptr);
auto gl_representation = manager.ProduceGLTexture(mailbox, tracker2.get());
EXPECT_EQ(kSizeBytes, tracker->GetMemRepresented());
EXPECT_EQ(0u, tracker2->GetMemRepresented());
}
factory_ref.reset();
EXPECT_EQ(0u, tracker->GetMemRepresented());
}
TEST(SharedImageManagerTest, MemoryDumps) {
constexpr size_t kSizeBytes1 = 1000;
constexpr size_t kSizeBytes2 = 2000;
SharedImageManager manager;
auto tracker = std::make_unique<MemoryTypeTracker>(nullptr);
auto factory_ref1 =
manager.Register(CreateImageBacking(kSizeBytes1), tracker.get());
auto factory_ref2 =
manager.Register(CreateImageBacking(kSizeBytes2), tracker.get());
base::trace_event::MemoryDumpArgs args = {
base::trace_event::MemoryDumpLevelOfDetail::kBackground};
base::trace_event::ProcessMemoryDump pmd(args);
manager.OnMemoryDump(args, &pmd);
auto* dump = pmd.GetAllocatorDump("gpu/shared_images");
ASSERT_NE(nullptr, dump);
ASSERT_EQ(dump->entries().size(), 3u);
for (const auto& entry : dump->entries()) {
if (entry.name == "size") {
EXPECT_EQ(entry.name, base::trace_event::MemoryAllocatorDump::kNameSize);
EXPECT_EQ(entry.units,
base::trace_event::MemoryAllocatorDump::kUnitsBytes);
EXPECT_EQ(entry.entry_type,
base::trace_event::MemoryAllocatorDump::Entry::kUint64);
EXPECT_EQ(entry.value_uint64, kSizeBytes1 + kSizeBytes2);
} else if (entry.name == "purgeable_size") {
EXPECT_EQ(entry.units,
base::trace_event::MemoryAllocatorDump::kUnitsBytes);
EXPECT_EQ(entry.entry_type,
base::trace_event::MemoryAllocatorDump::Entry::kUint64);
// Nothing is purgeable.
EXPECT_EQ(entry.value_uint64, 0u);
} else if (entry.name == "non_exo_size") {
EXPECT_EQ(entry.units,
base::trace_event::MemoryAllocatorDump::kUnitsBytes);
EXPECT_EQ(entry.entry_type,
base::trace_event::MemoryAllocatorDump::Entry::kUint64);
// Nothing is purgeable.
EXPECT_EQ(entry.value_uint64, kSizeBytes1 + kSizeBytes2);
} else {
FAIL() << "Unexpected memory dump entry name: " << entry.name;
}
}
}
TEST(SharedImageManagerTest, TransferRefSameTracker) {
const size_t kSizeBytes = 1024;
SharedImageManager manager;
auto tracker = std::make_unique<MemoryTypeTracker>(nullptr);
auto mailbox = Mailbox::Generate();
auto format = viz::SinglePlaneFormat::kRGBA_8888;
gfx::Size size(256, 256);
auto color_space = gfx::ColorSpace::CreateSRGB();
auto surface_origin = kTopLeft_GrSurfaceOrigin;
auto alpha_type = kPremul_SkAlphaType;
gpu::SharedImageUsageSet usage = SHARED_IMAGE_USAGE_GLES2_READ;
auto backing = std::make_unique<TestImageBacking>(
mailbox, format, size, color_space, surface_origin, alpha_type, usage,
kSizeBytes);
auto factory_ref = manager.Register(std::move(backing), tracker.get());
EXPECT_EQ(kSizeBytes, tracker->GetMemRepresented());
// Take an additional ref/representation.
auto gl_representation = manager.ProduceGLTexture(mailbox, tracker.get());
// Releasing the original ref should have no impact.
factory_ref.reset();
EXPECT_EQ(kSizeBytes, tracker->GetMemRepresented());
gl_representation.reset();
EXPECT_EQ(0u, tracker->GetMemRepresented());
}
TEST(SharedImageManagerTest, TransferRefNewTracker) {
const size_t kSizeBytes = 1024;
SharedImageManager manager;
auto tracker = std::make_unique<MemoryTypeTracker>(nullptr);
auto tracker2 = std::make_unique<MemoryTypeTracker>(nullptr);
auto mailbox = Mailbox::Generate();
auto format = viz::SinglePlaneFormat::kRGBA_8888;
gfx::Size size(256, 256);
auto color_space = gfx::ColorSpace::CreateSRGB();
auto surface_origin = kTopLeft_GrSurfaceOrigin;
auto alpha_type = kPremul_SkAlphaType;
gpu::SharedImageUsageSet usage = SHARED_IMAGE_USAGE_GLES2_READ;
auto backing = std::make_unique<TestImageBacking>(
mailbox, format, size, color_space, surface_origin, alpha_type, usage,
kSizeBytes);
auto factory_ref = manager.Register(std::move(backing), tracker.get());
EXPECT_EQ(kSizeBytes, tracker->GetMemRepresented());
// Take an additional ref/representation with a new tracker. Memory should
// stay accounted to the original tracker.
auto gl_representation = manager.ProduceGLTexture(mailbox, tracker2.get());
EXPECT_EQ(kSizeBytes, tracker->GetMemRepresented());
EXPECT_EQ(0u, tracker2->GetMemRepresented());
// Releasing the original should transfer memory to the new tracker.
factory_ref.reset();
EXPECT_EQ(0u, tracker->GetMemRepresented());
EXPECT_EQ(kSizeBytes, tracker2->GetMemRepresented());
// We can now safely destroy the original tracker.
tracker.reset();
gl_representation.reset();
EXPECT_EQ(0u, tracker2->GetMemRepresented());
}
class SequenceValidatingMemoryTracker : public MemoryTracker {
public:
SequenceValidatingMemoryTracker()
: task_runner_(base::ThreadPool::CreateSequencedTaskRunner({})) {}
~SequenceValidatingMemoryTracker() override { EXPECT_EQ(size_, 0u); }
scoped_refptr<base::SequencedTaskRunner> task_runner() const {
return task_runner_;
}
void TrackMemoryAllocatedChange(int64_t delta) override {
EXPECT_TRUE(task_runner_->RunsTasksInCurrentSequence());
size_ += delta;
}
uint64_t GetSize() const override { return size_; }
int ClientId() const override { return 0; }
uint64_t ClientTracingId() const override { return 0; }
uint64_t ContextGroupTracingId() const override { return 0; }
private:
scoped_refptr<base::SequencedTaskRunner> task_runner_;
int64_t size_ = 0;
};
TEST(SharedImageManagerTest, TransferRefCrossThread) {
const size_t kSizeBytes = 1024;
SharedImageManager manager;
SequenceValidatingMemoryTracker memory_tracker1;
SequenceValidatingMemoryTracker memory_tracker2;
auto memory_type_tracker1 = std::make_unique<MemoryTypeTracker>(
&memory_tracker1, memory_tracker1.task_runner());
auto memory_type_tracker2 = std::make_unique<MemoryTypeTracker>(
&memory_tracker2, memory_tracker2.task_runner());
auto mailbox = Mailbox::Generate();
auto format = viz::SinglePlaneFormat::kRGBA_8888;
gfx::Size size(256, 256);
auto color_space = gfx::ColorSpace::CreateSRGB();
auto surface_origin = kTopLeft_GrSurfaceOrigin;
auto alpha_type = kPremul_SkAlphaType;
gpu::SharedImageUsageSet usage = SHARED_IMAGE_USAGE_GLES2_READ;
auto backing = std::make_unique<TestImageBacking>(
mailbox, format, size, color_space, surface_origin, alpha_type, usage,
kSizeBytes);
auto factory_ref =
manager.Register(std::move(backing), memory_type_tracker1.get());
EXPECT_EQ(kSizeBytes, memory_type_tracker1->GetMemRepresented());
base::ThreadPoolInstance::Get()->FlushForTesting();
EXPECT_EQ(kSizeBytes, memory_tracker1.GetSize());
// Take an additional ref/representation with a new tracker. Memory should
// stay accounted to the original tracker.
auto gl_representation =
manager.ProduceGLTexture(mailbox, memory_type_tracker2.get());
EXPECT_EQ(kSizeBytes, memory_type_tracker1->GetMemRepresented());
EXPECT_EQ(0u, memory_type_tracker2->GetMemRepresented());
base::ThreadPoolInstance::Get()->FlushForTesting();
EXPECT_EQ(kSizeBytes, memory_tracker1.GetSize());
EXPECT_EQ(0u, memory_tracker2.GetSize());
// Releasing the original should transfer memory to the new tracker.
factory_ref.reset();
EXPECT_EQ(0u, memory_type_tracker1->GetMemRepresented());
EXPECT_EQ(kSizeBytes, memory_type_tracker2->GetMemRepresented());
base::ThreadPoolInstance::Get()->FlushForTesting();
EXPECT_EQ(0u, memory_tracker1.GetSize());
EXPECT_EQ(kSizeBytes, memory_tracker2.GetSize());
// We can now safely destroy the original tracker.
memory_type_tracker1.reset();
gl_representation.reset();
EXPECT_EQ(0u, memory_type_tracker2->GetMemRepresented());
base::ThreadPoolInstance::Get()->FlushForTesting();
EXPECT_EQ(0u, memory_tracker2.GetSize());
}
TEST(SharedImageManagerTest, GetUsageForMailbox) {
const size_t kSizeBytes = 1024;
auto backing = CreateImageBacking(kSizeBytes);
const gpu::Mailbox mailbox = backing->mailbox();
const gpu::SharedImageUsageSet usage = backing->usage();
SharedImageManager manager;
EXPECT_EQ(std::nullopt, manager.GetUsageForMailbox(mailbox));
auto tracker = std::make_unique<MemoryTypeTracker>(nullptr);
auto factory_ref = manager.Register(std::move(backing), tracker.get());
EXPECT_EQ(std::make_optional(usage), manager.GetUsageForMailbox(mailbox));
factory_ref.reset();
EXPECT_EQ(std::nullopt, manager.GetUsageForMailbox(mailbox));
}
} // anonymous namespace
} // namespace gpu