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
gpu / command_buffer / client / test_shared_image_interface.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/test_shared_image_interface.h"
#include <GLES2/gl2.h>
#include <GLES2/gl2extchromium.h>
#include <utility>
#if BUILDFLAG(IS_FUCHSIA)
#include <fuchsia/sysmem2/cpp/fidl.h>
#include <lib/sys/cpp/component_context.h>
#endif
#include "base/check.h"
#include "base/notreached.h"
#include "build/build_config.h"
#include "components/viz/common/resources/shared_image_format_utils.h"
#include "gpu/command_buffer/client/client_shared_image.h"
#include "gpu/command_buffer/common/shared_image_capabilities.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "ui/gfx/buffer_format_util.h"
#include "ui/gfx/gpu_fence.h"
#include "ui/gfx/gpu_memory_buffer.h"
#if BUILDFLAG(IS_FUCHSIA)
#include "base/fuchsia/fuchsia_logging.h"
#include "base/fuchsia/koid.h"
#include "base/fuchsia/process_context.h"
#endif
namespace gpu {
namespace {
gfx::GpuMemoryBufferType GetNativeBufferType() {
#if BUILDFLAG(IS_APPLE)
return gfx::IO_SURFACE_BUFFER;
#elif BUILDFLAG(IS_ANDROID)
return gfx::ANDROID_HARDWARE_BUFFER;
#elif BUILDFLAG(IS_WIN)
return gfx::DXGI_SHARED_HANDLE;
#else
// Ozone
return gfx::NATIVE_PIXMAP;
#endif
}
// Creates a shared memory region and returns a handle to it.
gfx::GpuMemoryBufferHandle CreateGMBHandle(
const gfx::BufferFormat& buffer_format,
const gfx::Size& size,
gfx::BufferUsage buffer_usage) {
static int last_handle_id = 0;
size_t buffer_size = 0u;
CHECK(
gfx::BufferSizeForBufferFormatChecked(size, buffer_format, &buffer_size));
auto shared_memory_region =
base::UnsafeSharedMemoryRegion::Create(buffer_size);
CHECK(shared_memory_region.IsValid());
gfx::GpuMemoryBufferHandle handle;
handle.type = gfx::SHARED_MEMORY_BUFFER;
handle.id = gfx::GpuMemoryBufferId(last_handle_id++);
handle.offset = 0;
handle.stride = static_cast<uint32_t>(
gfx::RowSizeForBufferFormat(size.width(), buffer_format, 0));
handle.region = std::move(shared_memory_region);
return handle;
}
} // namespace
#if BUILDFLAG(IS_FUCHSIA)
class TestBufferCollection {
public:
TestBufferCollection(zx::eventpair handle, zx::channel collection_token)
: handle_(std::move(handle)) {
sysmem_allocator_ = base::ComponentContextForProcess()
->svc()
->Connect<fuchsia::sysmem2::Allocator>();
sysmem_allocator_.set_error_handler([](zx_status_t status) {
ZX_LOG(FATAL, status)
<< "The fuchsia.sysmem.Allocator channel was terminated.";
});
fuchsia::sysmem2::AllocatorSetDebugClientInfoRequest set_debug_request;
set_debug_request.set_name("CrTestBufferCollection");
set_debug_request.set_id(base::GetCurrentProcId());
sysmem_allocator_->SetDebugClientInfo(std::move(set_debug_request));
fuchsia::sysmem2::AllocatorBindSharedCollectionRequest bind_shared_request;
bind_shared_request.set_token(fidl::InterfaceHandle<fuchsia::sysmem2::BufferCollectionToken>(
std::move(collection_token)));
bind_shared_request.set_buffer_collection_request(buffers_collection_.NewRequest());
sysmem_allocator_->BindSharedCollection(std::move(bind_shared_request));
fuchsia::sysmem2::BufferCollectionSetConstraintsRequest set_constraints_request;
auto& buffer_constraints = *set_constraints_request.mutable_constraints();
buffer_constraints.mutable_usage()->set_cpu(fuchsia::sysmem2::CPU_USAGE_READ);
zx_status_t status = buffers_collection_->SetConstraints(std::move(set_constraints_request));
ZX_CHECK(status == ZX_OK, status) << "BufferCollection::SetConstraints()";
}
TestBufferCollection(const TestBufferCollection&) = delete;
TestBufferCollection& operator=(const TestBufferCollection&) = delete;
~TestBufferCollection() { buffers_collection_->Release(); }
size_t GetNumBuffers() {
if (!buffer_collection_info_) {
fuchsia::sysmem2::BufferCollection_WaitForAllBuffersAllocated_Result wait_result;
zx_status_t status =
buffers_collection_->WaitForAllBuffersAllocated(&wait_result);
if (status != ZX_OK) {
ZX_LOG(FATAL, status) <<
"BufferCollection::WaitForAllBuffersAllocated() (status)";
} else if (wait_result.is_framework_err()) {
LOG(FATAL) <<
"BufferCollection::WaitForAllBuffersAllocated (framework_err): " <<
fidl::ToUnderlying(wait_result.framework_err());
} else if (!wait_result.is_response()) {
LOG(FATAL) << "BufferCollection::WaitForAllBuffersAllocated (err)" <<
static_cast<uint32_t>(wait_result.err());
}
auto info = std::move(*wait_result.response().mutable_buffer_collection_info());
buffer_collection_info_ = std::move(info);
}
return buffer_collection_info_->buffers().size();
}
private:
zx::eventpair handle_;
fuchsia::sysmem2::AllocatorPtr sysmem_allocator_;
fuchsia::sysmem2::BufferCollectionSyncPtr buffers_collection_;
std::optional<fuchsia::sysmem2::BufferCollectionInfo>
buffer_collection_info_;
};
#endif
TestSharedImageInterface::TestSharedImageInterface() {
InitializeSharedImageCapabilities();
}
TestSharedImageInterface::~TestSharedImageInterface() = default;
scoped_refptr<ClientSharedImage> TestSharedImageInterface::CreateSharedImage(
const SharedImageInfo& si_info,
SurfaceHandle surface_handle,
std::optional<SharedImagePoolId> pool_id) {
SyncToken sync_token = GenUnverifiedSyncToken();
base::AutoLock locked(lock_);
auto mailbox = Mailbox::Generate();
shared_images_.insert(mailbox);
most_recent_size_ = si_info.meta.size;
auto gmb_handle_type = emulate_client_provided_native_buffer_
? GetNativeBufferType()
: gfx::EMPTY_BUFFER;
return base::MakeRefCounted<ClientSharedImage>(
mailbox, si_info.meta, sync_token, holder_, gmb_handle_type);
}
scoped_refptr<ClientSharedImage>
TestSharedImageInterface::CreateSharedImage(
const SharedImageInfo& si_info,
base::span<const uint8_t> pixel_data) {
SyncToken sync_token = GenUnverifiedSyncToken();
base::AutoLock locked(lock_);
auto mailbox = Mailbox::Generate();
shared_images_.insert(mailbox);
return base::MakeRefCounted<ClientSharedImage>(
mailbox, si_info.meta, sync_token, holder_, gfx::EMPTY_BUFFER);
}
scoped_refptr<ClientSharedImage> TestSharedImageInterface::CreateSharedImage(
const SharedImageInfo& si_info,
SurfaceHandle surface_handle,
gfx::BufferUsage buffer_usage,
std::optional<SharedImagePoolId> pool_id) {
DoCreateSharedImage(si_info.meta.size, si_info.meta.format, surface_handle,
buffer_usage);
if (fail_shared_image_creation_with_buffer_usage_) {
return nullptr;
}
SyncToken sync_token = GenUnverifiedSyncToken();
// Create a ClientSharedImage with a GMB.
base::AutoLock locked(lock_);
auto mailbox = Mailbox::Generate();
shared_images_.insert(mailbox);
most_recent_size_ = si_info.meta.size;
auto buffer_format =
viz::SharedImageFormatToBufferFormatRestrictedUtils::ToBufferFormat(
si_info.meta.format);
if (test_gmb_manager_) {
auto gpu_memory_buffer = test_gmb_manager_->CreateGpuMemoryBuffer(
si_info.meta.size, buffer_format, buffer_usage, surface_handle,
nullptr);
// Since the |gpu_memory_buffer| here is always a shared memory, clear the
// external sampler prefs if it is already set by client.
// https://issues.chromium.org/339546249.
SharedImageInfo si_info_copy = si_info;
if (si_info_copy.meta.format.PrefersExternalSampler()) {
si_info_copy.meta.format.ClearPrefersExternalSampler();
}
return ClientSharedImage::CreateForTesting(
mailbox, si_info_copy.meta, sync_token, std::move(gpu_memory_buffer),
buffer_usage, holder_);
}
auto gmb_handle =
CreateGMBHandle(buffer_format, si_info.meta.size, buffer_usage);
return base::MakeRefCounted<ClientSharedImage>(
mailbox, si_info.meta, sync_token,
GpuMemoryBufferHandleInfo(std::move(gmb_handle), si_info.meta.format,
si_info.meta.size, buffer_usage),
holder_);
}
scoped_refptr<ClientSharedImage>
TestSharedImageInterface::CreateSharedImage(
const SharedImageInfo& si_info,
SurfaceHandle surface_handle,
gfx::BufferUsage buffer_usage,
gfx::GpuMemoryBufferHandle buffer_handle) {
SyncToken sync_token = GenUnverifiedSyncToken();
base::AutoLock locked(lock_);
auto mailbox = Mailbox::Generate();
shared_images_.insert(mailbox);
most_recent_size_ = si_info.meta.size;
auto buffer_format =
viz::SharedImageFormatToBufferFormatRestrictedUtils::ToBufferFormat(
si_info.meta.format);
if (test_gmb_manager_) {
auto gpu_memory_buffer = test_gmb_manager_->CreateGpuMemoryBuffer(
si_info.meta.size, buffer_format, buffer_usage, surface_handle,
nullptr);
// Since the |gpu_memory_buffer| here is always a shared memory, clear the
// external sampler prefs if it is already set by client.
// https://issues.chromium.org/339546249.
SharedImageInfo si_info_copy = si_info;
if (si_info_copy.meta.format.PrefersExternalSampler()) {
si_info_copy.meta.format.ClearPrefersExternalSampler();
}
return ClientSharedImage::CreateForTesting(
mailbox, si_info_copy.meta, sync_token, std::move(gpu_memory_buffer),
buffer_usage, holder_);
}
return base::MakeRefCounted<ClientSharedImage>(
mailbox, si_info.meta, sync_token,
GpuMemoryBufferHandleInfo(std::move(buffer_handle),
si_info.meta.format, si_info.meta.size,
buffer_usage),
holder_);
}
scoped_refptr<ClientSharedImage>
TestSharedImageInterface::CreateSharedImage(
const SharedImageInfo& si_info,
gfx::GpuMemoryBufferHandle buffer_handle) {
SyncToken sync_token = GenUnverifiedSyncToken();
base::AutoLock locked(lock_);
#if BUILDFLAG(IS_FUCHSIA)
if (buffer_handle.type == gfx::GpuMemoryBufferType::NATIVE_PIXMAP) {
zx_koid_t id =
base::GetRelatedKoid(
buffer_handle.native_pixmap_handle.buffer_collection_handle)
.value();
auto collection_it = sysmem_buffer_collections_.find(id);
// NOTE: Not all unittests invoke RegisterSysmemBufferCollection(), but
// the below CHECK should hold for those that do.
if (collection_it != sysmem_buffer_collections_.end()) {
CHECK_LT(buffer_handle.native_pixmap_handle.buffer_index,
collection_it->second->GetNumBuffers());
}
}
#endif
auto mailbox = Mailbox::Generate();
shared_images_.insert(mailbox);
most_recent_size_ = si_info.meta.size;
return base::MakeRefCounted<ClientSharedImage>(
mailbox, si_info.meta, sync_token, holder_, buffer_handle.type);
}
SharedImageInterface::SharedImageMapping
TestSharedImageInterface::CreateSharedImage(
const SharedImageInfo& si_info) {
base::WritableSharedMemoryMapping mapping;
gfx::GpuMemoryBufferHandle handle;
CreateSharedMemoryRegionFromSIInfo(si_info, mapping, handle);
auto mailbox = Mailbox::Generate();
shared_images_.insert(mailbox);
most_recent_size_ = si_info.meta.size;
SharedImageInterface::SharedImageMapping shared_image_mapping;
shared_image_mapping.mapping = std::move(mapping);
shared_image_mapping.shared_image = base::MakeRefCounted<ClientSharedImage>(
mailbox, si_info.meta, GenUnverifiedSyncToken(), holder_, handle.type);
return shared_image_mapping;
}
scoped_refptr<ClientSharedImage>
TestSharedImageInterface::CreateSharedImageForSoftwareCompositor(
const SharedImageInfo& si_info) {
base::WritableSharedMemoryMapping mapping;
gfx::GpuMemoryBufferHandle handle;
CreateSharedMemoryRegionFromSIInfo(si_info, mapping, handle);
auto mailbox = Mailbox::Generate();
shared_images_.insert(mailbox);
most_recent_size_ = si_info.meta.size;
return base::MakeRefCounted<ClientSharedImage>(mailbox, si_info.meta,
GenUnverifiedSyncToken(),
holder_, std::move(mapping));
}
void TestSharedImageInterface::UpdateSharedImage(
const SyncToken& sync_token,
const Mailbox& mailbox) {
base::AutoLock locked(lock_);
DCHECK(shared_images_.find(mailbox) != shared_images_.end());
}
void TestSharedImageInterface::UpdateSharedImage(
const SyncToken& sync_token,
std::unique_ptr<gfx::GpuFence> acquire_fence,
const Mailbox& mailbox) {
base::AutoLock locked(lock_);
DCHECK(shared_images_.find(mailbox) != shared_images_.end());
}
scoped_refptr<ClientSharedImage>
TestSharedImageInterface::ImportSharedImage(
const ExportedSharedImage& exported_shared_image) {
shared_images_.insert(exported_shared_image.mailbox_);
return base::WrapRefCounted<ClientSharedImage>(
new ClientSharedImage(
exported_shared_image.mailbox_, exported_shared_image.metadata_,
exported_shared_image.creation_sync_token_, holder_,
exported_shared_image.texture_target_));
}
void TestSharedImageInterface::DestroySharedImage(
const SyncToken& sync_token,
const Mailbox& mailbox) {
base::AutoLock locked(lock_);
shared_images_.erase(mailbox);
most_recent_destroy_token_ = sync_token;
}
void TestSharedImageInterface::DestroySharedImage(
const SyncToken& sync_token,
scoped_refptr<ClientSharedImage> client_shared_image) {
CHECK(client_shared_image->HasOneRef());
client_shared_image->UpdateDestructionSyncToken(sync_token);
}
SharedImageInterface::SwapChainSharedImages
TestSharedImageInterface::CreateSwapChain(viz::SharedImageFormat format,
const gfx::Size& size,
const gfx::ColorSpace& color_space,
GrSurfaceOrigin surface_origin,
SkAlphaType alpha_type,
gpu::SharedImageUsageSet usage) {
auto front_buffer = Mailbox::Generate();
auto back_buffer = Mailbox::Generate();
SyncToken sync_token = GenUnverifiedSyncToken();
shared_images_.insert(front_buffer);
shared_images_.insert(back_buffer);
return {base::MakeRefCounted<ClientSharedImage>(
front_buffer,
SharedImageMetadata(format, size, color_space,
surface_origin, alpha_type, usage),
sync_token, holder_, gfx::EMPTY_BUFFER),
base::MakeRefCounted<ClientSharedImage>(
back_buffer,
SharedImageMetadata(format, size, color_space,
surface_origin, alpha_type, usage),
sync_token, holder_, gfx::EMPTY_BUFFER)};
}
void TestSharedImageInterface::PresentSwapChain(
const SyncToken& sync_token,
const Mailbox& mailbox) {}
#if BUILDFLAG(IS_FUCHSIA)
void TestSharedImageInterface::RegisterSysmemBufferCollection(
zx::eventpair service_handle,
zx::channel sysmem_token,
const viz::SharedImageFormat& format,
gfx::BufferUsage usage,
bool register_with_image_pipe) {
EXPECT_EQ(format, viz::MultiPlaneFormat::kNV12);
EXPECT_EQ(usage, gfx::BufferUsage::GPU_READ);
zx_koid_t id = base::GetKoid(service_handle).value();
std::unique_ptr<TestBufferCollection>& collection =
sysmem_buffer_collections_[id];
EXPECT_FALSE(collection);
collection = std::make_unique<TestBufferCollection>(std::move(service_handle),
std::move(sysmem_token));
}
#endif // BUILDFLAG(IS_FUCHSIA)
SyncToken TestSharedImageInterface::GenVerifiedSyncToken() {
base::AutoLock locked(lock_);
most_recent_generated_token_ =
SyncToken(CommandBufferNamespace::GPU_IO,
CommandBufferId(), ++release_id_);
VerifySyncToken(most_recent_generated_token_);
return most_recent_generated_token_;
}
SyncToken TestSharedImageInterface::GenUnverifiedSyncToken() {
base::AutoLock locked(lock_);
most_recent_generated_token_ =
SyncToken(CommandBufferNamespace::GPU_IO,
CommandBufferId(), ++release_id_);
return most_recent_generated_token_;
}
void TestSharedImageInterface::VerifySyncToken(SyncToken& sync_token) {
sync_token.SetVerifyFlush();
}
void TestSharedImageInterface::WaitSyncToken(const SyncToken& sync_token) {
NOTREACHED();
}
void TestSharedImageInterface::Flush() {
// No need to flush in this implementation. DoFlush() is for mock.
DoFlush();
}
scoped_refptr<gfx::NativePixmap> TestSharedImageInterface::GetNativePixmap(
const Mailbox& mailbox) {
return nullptr;
}
bool TestSharedImageInterface::CheckSharedImageExists(
const Mailbox& mailbox) const {
base::AutoLock locked(lock_);
return shared_images_.contains(mailbox);
}
const SharedImageCapabilities&
TestSharedImageInterface::GetCapabilities() {
return shared_image_capabilities_;
}
void TestSharedImageInterface::SetCapabilities(
const SharedImageCapabilities& caps) {
shared_image_capabilities_ = caps;
InitializeSharedImageCapabilities();
}
void TestSharedImageInterface::InitializeSharedImageCapabilities() {
#if BUILDFLAG(IS_MAC)
// Initialize `texture_target_for_io_surfaces` to a value that is valid for
// ClientSharedImage to use, as unittests broadly create and use
// SharedImageCapabilities instances without initializing this field. The
// specific value is chosen to match the historical default value that was
// used when this state was accessed via a global variable.
if (!shared_image_capabilities_.texture_target_for_io_surfaces) {
shared_image_capabilities_.texture_target_for_io_surfaces =
GL_TEXTURE_RECTANGLE_ARB;
}
#endif
}
} // namespace gpu