File: shared_image_interface_in_process_base.cc

package info (click to toggle)
chromium 144.0.7559.109-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 5,915,868 kB
  • sloc: cpp: 35,866,215; ansic: 7,599,035; javascript: 3,623,761; python: 1,639,407; xml: 833,084; asm: 716,173; pascal: 185,323; sh: 88,763; perl: 88,699; objc: 79,984; sql: 58,217; cs: 42,430; fortran: 24,101; makefile: 20,747; tcl: 15,277; php: 14,022; yacc: 9,059; ruby: 7,553; awk: 3,720; lisp: 3,233; lex: 1,330; ada: 727; jsp: 228; sed: 36
file content (552 lines) | stat: -rw-r--r-- 20,337 bytes parent folder | download | duplicates (4)
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
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
// Copyright 2025 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_interface_in_process_base.h"

#include <cstdint>
#include <memory>
#include <optional>
#include <string_view>
#include <vector>

#include "base/check.h"
#include "base/containers/span.h"
#include "base/functional/bind.h"
#include "base/functional/callback_helpers.h"
#include "base/memory/scoped_refptr.h"
#include "base/memory/shared_memory_mapping.h"
#include "base/notreached.h"
#include "base/sequence_checker.h"
#include "base/synchronization/lock.h"
#include "base/synchronization/waitable_event.h"
#include "build/buildflag.h"
#include "components/viz/common/resources/shared_image_format.h"
#include "gpu/command_buffer/client/client_shared_image.h"
#include "gpu/command_buffer/client/shared_image_interface.h"
#include "gpu/command_buffer/common/command_buffer_id.h"
#include "gpu/command_buffer/common/constants.h"
#include "gpu/command_buffer/common/mailbox.h"
#include "gpu/command_buffer/common/shared_image_capabilities.h"
#include "gpu/command_buffer/common/shared_image_pool_id.h"
#include "gpu/command_buffer/common/shared_image_usage.h"
#include "gpu/command_buffer/common/sync_token.h"
#include "gpu/command_buffer/service/shared_image/shared_image_factory.h"
#include "gpu/ipc/common/gpu_memory_buffer_handle_info.h"
#include "gpu/ipc/common/surface_handle.h"
#include "third_party/abseil-cpp/absl/cleanup/cleanup.h"
#include "third_party/skia/include/core/SkAlphaType.h"
#include "third_party/skia/include/gpu/ganesh/GrTypes.h"
#include "ui/gfx/buffer_types.h"
#include "ui/gfx/color_space.h"
#include "ui/gfx/gpu_fence.h"
#include "ui/gfx/gpu_memory_buffer_handle.h"

namespace gpu {

SharedImageInterfaceInProcessBase::SharedImageInterfaceInProcessBase(
    CommandBufferNamespace namespace_id,
    CommandBufferId command_buffer_id,
    bool verify_creation_sync_token,
    SharedImageCapabilities shared_image_capabilities)
    : namespace_id_(namespace_id),
      command_buffer_id_(command_buffer_id),
      verify_creation_sync_token_(verify_creation_sync_token),
      shared_image_capabilities_(std::move(shared_image_capabilities)),
      shared_image_capabilities_ready_(
          base::WaitableEvent::ResetPolicy::MANUAL,
          base::WaitableEvent::InitialState::SIGNALED) {
  DETACH_FROM_SEQUENCE(gpu_sequence_checker_);
}

SharedImageInterfaceInProcessBase::SharedImageInterfaceInProcessBase(
    CommandBufferNamespace namespace_id,
    CommandBufferId command_buffer_id,
    bool verify_creation_sync_token)
    : namespace_id_(namespace_id),
      command_buffer_id_(command_buffer_id),
      verify_creation_sync_token_(verify_creation_sync_token),
      shared_image_capabilities_ready_(
          base::WaitableEvent::ResetPolicy::MANUAL,
          base::WaitableEvent::InitialState::NOT_SIGNALED) {
  DETACH_FROM_SEQUENCE(gpu_sequence_checker_);
}

SharedImageInterfaceInProcessBase::~SharedImageInterfaceInProcessBase() =
    default;

scoped_refptr<ClientSharedImage>
SharedImageInterfaceInProcessBase::CreateSharedImage(
    const SharedImageInfo& si_info,
    gpu::SurfaceHandle surface_handle,
    std::optional<SharedImagePoolId> pool_id) {
  DCHECK(gpu::IsValidClientUsage(si_info.meta.usage));
  auto mailbox = Mailbox::Generate();
  {
    base::AutoLock lock(lock_);
    // Note: we enqueue the task under the lock to guarantee monotonicity of
    // the release ids as seen by the service.
    ScheduleGpuTask(
        base::BindOnce(
            &SharedImageInterfaceInProcessBase::CreateSharedImageOnGpuThread,
            this, mailbox, si_info, surface_handle),
        /*sync_token_fences=*/{}, GenNextSyncTokenLocked());
  }
  return base::MakeRefCounted<ClientSharedImage>(
      mailbox, si_info, GenCreationSyncToken(), holder_, gfx::EMPTY_BUFFER);
}

void SharedImageInterfaceInProcessBase::CreateSharedImageOnGpuThread(
    const Mailbox& mailbox,
    SharedImageInfo si_info,
    gpu::SurfaceHandle surface_handle) {
  DCHECK_CALLED_ON_VALID_SEQUENCE(gpu_sequence_checker_);
  SharedImageFactory* shared_image_factory = GetSharedImageFactoryOnGpuThread();
  if (!shared_image_factory) {
    return;
  }

  if (!MakeContextCurrentOnGpuThread()) {
    return;
  }

  if (!shared_image_factory->CreateSharedImage(
          mailbox, si_info.meta.format, si_info.meta.size,
          si_info.meta.color_space, si_info.meta.surface_origin,
          si_info.meta.alpha_type, surface_handle, si_info.meta.usage,
          std::string(si_info.debug_label))) {
    MarkContextLostOnGpuThread();
  }
}

scoped_refptr<ClientSharedImage>
SharedImageInterfaceInProcessBase::CreateSharedImage(
    const SharedImageInfo& si_info,
    base::span<const uint8_t> pixel_data) {
  DCHECK(gpu::IsValidClientUsage(si_info.meta.usage));
  auto mailbox = Mailbox::Generate();
  std::vector<uint8_t> pixel_data_copy(pixel_data.begin(), pixel_data.end());
  {
    base::AutoLock lock(lock_);
    // Note: we enqueue the task under the lock to guarantee monotonicity of
    // the release ids as seen by the service.
    ScheduleGpuTask(
        base::BindOnce(&SharedImageInterfaceInProcessBase::
                           CreateSharedImageWithDataOnGpuThread,
                       this, mailbox, si_info, std::move(pixel_data_copy)),
        /*sync_token_fences=*/{}, GenNextSyncTokenLocked());
  }
  return base::MakeRefCounted<ClientSharedImage>(
      mailbox, si_info, GenCreationSyncToken(), holder_, gfx::EMPTY_BUFFER);
}

void SharedImageInterfaceInProcessBase::CreateSharedImageWithDataOnGpuThread(
    const Mailbox& mailbox,
    SharedImageInfo si_info,
    std::vector<uint8_t> pixel_data) {
  DCHECK_CALLED_ON_VALID_SEQUENCE(gpu_sequence_checker_);
  SharedImageFactory* shared_image_factory = GetSharedImageFactoryOnGpuThread();
  if (!shared_image_factory) {
    return;
  }

  if (!MakeContextCurrentOnGpuThread()) {
    return;
  }

  if (!shared_image_factory->CreateSharedImage(
          mailbox, si_info.meta.format, si_info.meta.size,
          si_info.meta.color_space, si_info.meta.surface_origin,
          si_info.meta.alpha_type, si_info.meta.usage,
          std::move(si_info.debug_label), pixel_data)) {
    MarkContextLostOnGpuThread();
  }
}

scoped_refptr<ClientSharedImage>
SharedImageInterfaceInProcessBase::CreateSharedImage(
    const SharedImageInfo& si_info,
    SurfaceHandle surface_handle,
    gfx::BufferUsage buffer_usage,
    std::optional<SharedImagePoolId> pool_id) {
  DCHECK(gpu::IsValidClientUsage(si_info.meta.usage));
  auto mailbox = Mailbox::Generate();
  // Copy which can be modified.
  SharedImageInfo si_info_copy = si_info;
  // Set CPU read/write usage based on buffer usage.
  si_info_copy.meta.usage |= GetCpuSIUsage(buffer_usage);
  {
    base::AutoLock lock(lock_);
    // Note: we enqueue the task under the lock to guarantee monotonicity of
    // the release ids as seen by the service.
    ScheduleGpuTask(
        base::BindOnce(&SharedImageInterfaceInProcessBase::
                           CreateSharedImageWithBufferUsageOnGpuThread,
                       this, mailbox, si_info_copy, surface_handle,
                       buffer_usage),
        /*sync_token_fences=*/{}, GenNextSyncTokenLocked());
  }

  auto handle_info = GetGpuMemoryBufferHandleInfo(mailbox);
  // Clear the external sampler prefs for shared memory case if it is set.
  // https://issues.chromium.org/339546249.
  if (si_info_copy.meta.format.PrefersExternalSampler() &&
      (handle_info.handle.type ==
       gfx::GpuMemoryBufferType::SHARED_MEMORY_BUFFER)) {
    si_info_copy.meta.format.ClearPrefersExternalSampler();
  }

  return base::MakeRefCounted<ClientSharedImage>(
      mailbox, si_info_copy, GenCreationSyncToken(), std::move(handle_info),
      holder_);
}

void SharedImageInterfaceInProcessBase::
    CreateSharedImageWithBufferUsageOnGpuThread(const Mailbox& mailbox,
                                                SharedImageInfo si_info,
                                                SurfaceHandle surface_handle,
                                                gfx::BufferUsage buffer_usage) {
  DCHECK_CALLED_ON_VALID_SEQUENCE(gpu_sequence_checker_);
  SharedImageFactory* shared_image_factory = GetSharedImageFactoryOnGpuThread();
  if (!shared_image_factory) {
    return;
  }

  if (!MakeContextCurrentOnGpuThread()) {
    return;
  }

  // Note that SharedImageInterfaceInProcess implementation here uses
  // SharedImageFactory::CreateSharedImage() to create a shared image backed by
  // native buffer/shared memory in GPU process. This is different
  // implementation and code path compared to ClientSharedImage implementation
  // which creates native buffer/shared memory on IO thread and then creates a
  // mailbox from it on GPU thread.
  if (!shared_image_factory->CreateSharedImage(
          mailbox, si_info.meta.format, si_info.meta.size,
          si_info.meta.color_space, si_info.meta.surface_origin,
          si_info.meta.alpha_type, surface_handle, si_info.meta.usage,
          std::move(si_info.debug_label), buffer_usage)) {
    MarkContextLostOnGpuThread();
  }
}

GpuMemoryBufferHandleInfo
SharedImageInterfaceInProcessBase::GetGpuMemoryBufferHandleInfo(
    const Mailbox& mailbox) {
  base::WaitableEvent completion(
      base::WaitableEvent::ResetPolicy::MANUAL,
      base::WaitableEvent::InitialState::NOT_SIGNALED);

  gfx::GpuMemoryBufferHandle handle;
  gfx::BufferUsage buffer_usage;

  ScheduleGpuTask(
      base::BindOnce(&SharedImageInterfaceInProcessBase::
                         GetGpuMemoryBufferHandleInfoOnGpuThread,
                     this, mailbox, &handle, &buffer_usage, &completion),
      /*sync_token_fences=*/{}, SyncToken());
  completion.Wait();
  return GpuMemoryBufferHandleInfo(std::move(handle), buffer_usage);
}

void SharedImageInterfaceInProcessBase::GetGpuMemoryBufferHandleInfoOnGpuThread(
    const Mailbox& mailbox,
    gfx::GpuMemoryBufferHandle* handle,
    gfx::BufferUsage* buffer_usage,
    base::WaitableEvent* completion) {
  DCHECK_CALLED_ON_VALID_SEQUENCE(gpu_sequence_checker_);
  absl::Cleanup completion_runner = [completion] { completion->Signal(); };

  SharedImageFactory* shared_image_factory = GetSharedImageFactoryOnGpuThread();
  DCHECK(shared_image_factory);

  // Note that we are not calling `MakeContextCurrent()` here as of now since
  // it is not needed to get the handle from the backings. Make context current
  // if we find that it is required.
  if (!shared_image_factory->GetGpuMemoryBufferHandleInfo(mailbox, *handle,
                                                          *buffer_usage)) {
    LOG(ERROR) << "SharedImageInterfaceInProcessBase: Unable to get "
                  "GpuMemoryBufferHandle";
  }
}

scoped_refptr<ClientSharedImage>
SharedImageInterfaceInProcessBase::CreateSharedImage(
    const SharedImageInfo& si_info,
    gpu::SurfaceHandle surface_handle,
    gfx::BufferUsage buffer_usage,
    gfx::GpuMemoryBufferHandle buffer_handle) {
  DCHECK(gpu::IsValidClientUsage(si_info.meta.usage));

#if BUILDFLAG(IS_APPLE) || BUILDFLAG(IS_WIN)
  CHECK(!si_info.meta.format.PrefersExternalSampler());
#endif

  auto client_buffer_handle = buffer_handle.Clone();
  auto mailbox = Mailbox::Generate();
  // Copy which can be modified.
  SharedImageInfo si_info_copy = si_info;
  // Set CPU read/write usage based on buffer usage.
  si_info_copy.meta.usage |= GetCpuSIUsage(buffer_usage);
  {
    base::AutoLock lock(lock_);
    // Note: we enqueue the task under the lock to guarantee monotonicity of
    // the release ids as seen by the service.
    ScheduleGpuTask(
        base::BindOnce(&SharedImageInterfaceInProcessBase::
                           CreateSharedImageWithBufferOnGpuThread,
                       this, mailbox, si_info_copy, std::move(buffer_handle)),
        /*sync_token_fences=*/{}, GenNextSyncTokenLocked());
  }

  return base::MakeRefCounted<ClientSharedImage>(
      mailbox, si_info_copy, GenCreationSyncToken(),
      GpuMemoryBufferHandleInfo(std::move(client_buffer_handle), buffer_usage),
      holder_);
}

scoped_refptr<ClientSharedImage>
SharedImageInterfaceInProcessBase::CreateSharedImage(
    const SharedImageInfo& si_info,
    gfx::GpuMemoryBufferHandle buffer_handle) {
  DCHECK(gpu::IsValidClientUsage(si_info.meta.usage));

#if BUILDFLAG(IS_APPLE) || BUILDFLAG(IS_WIN)
  CHECK(!si_info.meta.format.PrefersExternalSampler());
#endif

  auto mailbox = Mailbox::Generate();
  auto gmb_type = buffer_handle.type;
  {
    base::AutoLock lock(lock_);
    // Note: we enqueue the task under the lock to guarantee monotonicity of
    // the release ids as seen by the service.
    ScheduleGpuTask(
        base::BindOnce(&SharedImageInterfaceInProcessBase::
                           CreateSharedImageWithBufferOnGpuThread,
                       this, mailbox, si_info, std::move(buffer_handle)),
        /*sync_token_fences=*/{}, GenNextSyncTokenLocked());
  }

  return base::MakeRefCounted<ClientSharedImage>(
      mailbox, si_info, GenCreationSyncToken(), holder_, gmb_type);
}

scoped_refptr<ClientSharedImage>
SharedImageInterfaceInProcessBase::CreateSharedImageForMLTensor(
    std::string debug_label,
    viz::SharedImageFormat format,
    const gfx::Size& size,
    gpu::SharedImageUsageSet usage) {
  NOTREACHED();
}

scoped_refptr<ClientSharedImage>
SharedImageInterfaceInProcessBase::CreateSharedImageForSoftwareCompositor(
    const SharedImageInfo& si_info) {
  base::WritableSharedMemoryMapping mapping;
  gfx::GpuMemoryBufferHandle handle;
  CreateSharedMemoryRegionFromSIInfo(si_info, mapping, handle);

  auto mailbox = Mailbox::Generate();
  {
    base::AutoLock lock(lock_);
    // Note: we enqueue the task under the lock to guarantee monotonicity of
    // the release ids as seen by the service.
    ScheduleGpuTask(base::BindOnce(&SharedImageInterfaceInProcessBase::
                                       CreateSharedImageWithBufferOnGpuThread,
                                   this, mailbox, si_info, std::move(handle)),
                    /*sync_token_fences=*/{}, GenNextSyncTokenLocked());
  }
  return base::MakeRefCounted<ClientSharedImage>(
      mailbox, si_info, GenCreationSyncToken(), holder_, std::move(mapping));
}

void SharedImageInterfaceInProcessBase::CreateSharedImageWithBufferOnGpuThread(
    const Mailbox& mailbox,
    SharedImageInfo si_info,
    gfx::GpuMemoryBufferHandle buffer_handle) {
  DCHECK_CALLED_ON_VALID_SEQUENCE(gpu_sequence_checker_);
  SharedImageFactory* shared_image_factory = GetSharedImageFactoryOnGpuThread();
  if (!shared_image_factory) {
    return;
  }

  if (!MakeContextCurrentOnGpuThread()) {
    return;
  }

  if (!shared_image_factory->CreateSharedImage(
          mailbox, si_info.meta.format, si_info.meta.size,
          si_info.meta.color_space, si_info.meta.surface_origin,
          si_info.meta.alpha_type, si_info.meta.usage,
          std::move(si_info.debug_label), std::move(buffer_handle))) {
    MarkContextLostOnGpuThread();
  }
}

#if BUILDFLAG(IS_FUCHSIA)
void SharedImageInterfaceInProcessBase::RegisterSysmemBufferCollection(
    zx::eventpair service_handle,
    zx::channel sysmem_token,
    const viz::SharedImageFormat& format,
    gfx::BufferUsage usage,
    bool register_with_image_pipe) {
  NOTREACHED();
}
#endif  // BUILDFLAG(IS_FUCHSIA)

void SharedImageInterfaceInProcessBase::UpdateSharedImage(
    const SyncToken& sync_token,
    const Mailbox& mailbox) {
  UpdateSharedImage(sync_token, nullptr, mailbox);
}

void SharedImageInterfaceInProcessBase::UpdateSharedImage(
    const SyncToken& sync_token,
    std::unique_ptr<gfx::GpuFence> acquire_fence,
    const Mailbox& mailbox) {
  DCHECK(!acquire_fence);
  base::AutoLock lock(lock_);
  // Note: we enqueue the task under the lock to guarantee monotonicity of the
  // release ids as seen by the service.
  ScheduleGpuTask(
      base::BindOnce(
          &SharedImageInterfaceInProcessBase::UpdateSharedImageOnGpuThread,
          this, mailbox),
      /*sync_token_fences=*/{sync_token}, GenNextSyncTokenLocked());
}

void SharedImageInterfaceInProcessBase::UpdateSharedImageOnGpuThread(
    const Mailbox& mailbox) {
  DCHECK_CALLED_ON_VALID_SEQUENCE(gpu_sequence_checker_);

  if (!MakeContextCurrentOnGpuThread()) {
    return;
  }

  SharedImageFactory* shared_image_factory = GetSharedImageFactoryOnGpuThread();
  if (!shared_image_factory ||
      !shared_image_factory->UpdateSharedImage(mailbox)) {
    MarkContextLostOnGpuThread();
  }
}

void SharedImageInterfaceInProcessBase::DestroySharedImage(
    const SyncToken& sync_token,
    const Mailbox& mailbox) {
  // Use sync token dependency to ensure that the destroy task does not run
  // before sync token is released.
  ScheduleGpuTask(
      base::BindOnce(
          &SharedImageInterfaceInProcessBase::DestroySharedImageOnGpuThread,
          this, mailbox),
      /*sync_token_fences=*/{sync_token}, SyncToken());
}

void SharedImageInterfaceInProcessBase::DestroySharedImage(
    const SyncToken& sync_token,
    scoped_refptr<ClientSharedImage> client_shared_image) {
  CHECK(client_shared_image->HasOneRef());
  client_shared_image->UpdateDestructionSyncToken(sync_token);
}

void SharedImageInterfaceInProcessBase::DestroySharedImageOnGpuThread(
    const Mailbox& mailbox) {
  DCHECK_CALLED_ON_VALID_SEQUENCE(gpu_sequence_checker_);

  if (!MakeContextCurrentOnGpuThread()) {
    return;
  }

  SharedImageFactory* shared_image_factory = GetSharedImageFactoryOnGpuThread();
  if (!shared_image_factory ||
      !shared_image_factory->DestroySharedImage(mailbox)) {
    MarkContextLostOnGpuThread();
  }
}

SyncToken SharedImageInterfaceInProcessBase::GenNextSyncToken() {
  base::AutoLock lock(lock_);
  return GenNextSyncTokenLocked();
}

SyncToken SharedImageInterfaceInProcessBase::GenNextSyncTokenLocked() {
  return MakeSyncToken(next_fence_sync_release_++);
}

SyncToken SharedImageInterfaceInProcessBase::GenUnverifiedSyncToken() {
  base::AutoLock lock(lock_);
  return MakeSyncToken(next_fence_sync_release_ - 1);
}

SyncToken SharedImageInterfaceInProcessBase::GenVerifiedSyncToken() {
  base::AutoLock lock(lock_);
  SyncToken sync_token = MakeSyncToken(next_fence_sync_release_ - 1);
  VerifySyncToken(sync_token);
  return sync_token;
}

void SharedImageInterfaceInProcessBase::VerifySyncToken(SyncToken& sync_token) {
  sync_token.SetVerifyFlush();
}

bool SharedImageInterfaceInProcessBase::CanVerifySyncToken(
    const gpu::SyncToken& sync_token) {
  return sync_token.namespace_id() == namespace_id_;
}

void SharedImageInterfaceInProcessBase::VerifyFlush() {
  // No flush required as we are only within a single process
}

void SharedImageInterfaceInProcessBase::WaitSyncToken(
    const SyncToken& sync_token) {
  base::AutoLock lock(lock_);

  ScheduleGpuTask(base::DoNothing(),
                  /*sync_token_fences=*/{sync_token}, GenNextSyncTokenLocked());
}

scoped_refptr<ClientSharedImage>
SharedImageInterfaceInProcessBase::ImportSharedImage(
    ExportedSharedImage exported_shared_image) {
  // Secondary references are required only by client processes, so it shouldn't
  // be reachable here.
  NOTREACHED();
}

const SharedImageCapabilities&
SharedImageInterfaceInProcessBase::GetCapabilities() {
  // Return fast on already-initialized common case.
  if (shared_image_capabilities_ready_.IsSignaled()) {
    return shared_image_capabilities_;
  }

  ScheduleGpuTask(
      base::BindOnce(
          &SharedImageInterfaceInProcessBase::GetCapabilitiesOnGpuThread, this),
      /*sync_token_fences=*/{}, SyncToken());

  shared_image_capabilities_ready_.Wait();
  return shared_image_capabilities_;
}

void SharedImageInterfaceInProcessBase::GetCapabilitiesOnGpuThread() {
  // `GetCapabilitiesOnGpuThread()` may be scheduled by multiple threads,
  // so quick return if it's already been run.
  if (shared_image_capabilities_ready_.IsSignaled()) {
    return;
  }

  SharedImageFactory* shared_image_factory = GetSharedImageFactoryOnGpuThread();
  if (shared_image_factory) {
    shared_image_capabilities_ = shared_image_factory->MakeCapabilities();
  }
  // Fallback to default-initialized version if no factory.

  shared_image_capabilities_ready_.Signal();
}

}  // namespace gpu