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
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
gpu / command_buffer / service / shared_image / compound_image_backing.cc [blame]
// Copyright 2022 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/compound_image_backing.h"
#include "base/functional/bind.h"
#include "base/logging.h"
#include "base/memory/ptr_util.h"
#include "base/memory/scoped_refptr.h"
#include "base/strings/stringprintf.h"
#include "base/trace_event/memory_allocator_dump_guid.h"
#include "base/trace_event/process_memory_dump.h"
#include "components/viz/common/resources/shared_image_format.h"
#include "components/viz/common/resources/shared_image_format_utils.h"
#include "gpu/command_buffer/common/gpu_memory_buffer_support.h"
#include "gpu/command_buffer/common/mailbox.h"
#include "gpu/command_buffer/common/shared_image_usage.h"
#include "gpu/command_buffer/service/memory_tracking.h"
#include "gpu/command_buffer/service/shared_context_state.h"
#include "gpu/command_buffer/service/shared_image/shared_image_backing_factory.h"
#include "gpu/command_buffer/service/shared_image/shared_image_format_service_utils.h"
#include "gpu/command_buffer/service/shared_image/shared_image_manager.h"
#include "gpu/command_buffer/service/shared_image/shared_image_representation.h"
#include "gpu/command_buffer/service/shared_image/shared_memory_image_backing.h"
#include "gpu/command_buffer/service/shared_memory_region_wrapper.h"
#include "gpu/config/gpu_finch_features.h"
#include "gpu/ipc/common/gpu_memory_buffer_impl_shared_memory.h"
#include "third_party/skia/include/core/SkAlphaType.h"
#include "third_party/skia/include/core/SkPixmap.h"
#include "third_party/skia/include/core/SkSurface.h"
#include "third_party/skia/include/core/SkSurfaceProps.h"
#include "third_party/skia/include/gpu/ganesh/GrDirectContext.h"
#include "third_party/skia/include/gpu/ganesh/GrTypes.h"
#include "third_party/skia/include/gpu/graphite/BackendTexture.h"
#include "third_party/skia/include/private/chromium/GrPromiseImageTexture.h"
#include "ui/gfx/buffer_format_util.h"
#include "ui/gfx/buffer_types.h"
#include "ui/gfx/color_space.h"
#include "ui/gfx/geometry/size.h"
#include "ui/gfx/gpu_memory_buffer.h"
namespace gpu {
namespace {
constexpr AccessStreamSet kMemoryStreamSet = {SharedImageAccessStream::kMemory};
// Unique GUIDs for child backings.
base::trace_event::MemoryAllocatorDumpGuid GetSubBackingGUIDForTracing(
const Mailbox& mailbox,
int backing_index) {
return base::trace_event::MemoryAllocatorDumpGuid(
base::StringPrintf("gpu-shared-image/%s/sub-backing/%d",
mailbox.ToDebugString().c_str(), backing_index));
}
#if BUILDFLAG(IS_WIN)
// Only allow shmem overlays for NV12 on Windows.
// This moves the SCANOUT flag from the GPU backing to the shmem backing in the
// CompoundImageBacking.
constexpr bool kAllowShmOverlays = true;
#else
constexpr bool kAllowShmOverlays = false;
#endif
gpu::SharedImageUsageSet GetShmSharedImageUsage(SharedImageUsageSet usage) {
if (kAllowShmOverlays) {
return usage.Has(gpu::SHARED_IMAGE_USAGE_SCANOUT)
? SHARED_IMAGE_USAGE_CPU_WRITE_ONLY | SHARED_IMAGE_USAGE_SCANOUT
: SHARED_IMAGE_USAGE_CPU_WRITE_ONLY;
}
return SHARED_IMAGE_USAGE_CPU_WRITE_ONLY;
}
} // namespace
// Wrapped representation types are not in the anonymous namespace because they
// need to be friend classes to real representations to access protected
// virtual functions.
class WrappedGLTextureCompoundImageRepresentation
: public GLTextureImageRepresentation {
public:
WrappedGLTextureCompoundImageRepresentation(
SharedImageManager* manager,
SharedImageBacking* backing,
MemoryTypeTracker* tracker,
std::unique_ptr<GLTextureImageRepresentation> wrapped)
: GLTextureImageRepresentation(manager, backing, tracker),
wrapped_(std::move(wrapped)) {
DCHECK(wrapped_);
}
CompoundImageBacking* compound_backing() {
return static_cast<CompoundImageBacking*>(backing());
}
// GLTextureImageRepresentation implementation.
bool BeginAccess(GLenum mode) final {
AccessMode access_mode =
mode == kReadAccessMode ? AccessMode::kRead : AccessMode::kWrite;
compound_backing()->NotifyBeginAccess(SharedImageAccessStream::kGL,
access_mode);
return wrapped_->BeginAccess(mode);
}
void EndAccess() final { wrapped_->EndAccess(); }
gpu::TextureBase* GetTextureBase(int plane_index) final {
return wrapped_->GetTextureBase(plane_index);
}
bool SupportsMultipleConcurrentReadAccess() final {
return wrapped_->SupportsMultipleConcurrentReadAccess();
}
gles2::Texture* GetTexture(int plane_index) final {
return wrapped_->GetTexture(plane_index);
}
private:
std::unique_ptr<GLTextureImageRepresentation> wrapped_;
};
class WrappedGLTexturePassthroughCompoundImageRepresentation
: public GLTexturePassthroughImageRepresentation {
public:
WrappedGLTexturePassthroughCompoundImageRepresentation(
SharedImageManager* manager,
SharedImageBacking* backing,
MemoryTypeTracker* tracker,
std::unique_ptr<GLTexturePassthroughImageRepresentation> wrapped)
: GLTexturePassthroughImageRepresentation(manager, backing, tracker),
wrapped_(std::move(wrapped)) {
DCHECK(wrapped_);
}
CompoundImageBacking* compound_backing() {
return static_cast<CompoundImageBacking*>(backing());
}
// GLTexturePassthroughImageRepresentation implementation.
bool BeginAccess(GLenum mode) final {
AccessMode access_mode =
mode == kReadAccessMode ? AccessMode::kRead : AccessMode::kWrite;
compound_backing()->NotifyBeginAccess(SharedImageAccessStream::kGL,
access_mode);
return wrapped_->BeginAccess(mode);
}
void EndAccess() final { wrapped_->EndAccess(); }
gpu::TextureBase* GetTextureBase(int plane_index) final {
return wrapped_->GetTextureBase(plane_index);
}
bool SupportsMultipleConcurrentReadAccess() final {
return wrapped_->SupportsMultipleConcurrentReadAccess();
}
const scoped_refptr<gles2::TexturePassthrough>& GetTexturePassthrough(
int plane_index) final {
return wrapped_->GetTexturePassthrough(plane_index);
}
private:
std::unique_ptr<GLTexturePassthroughImageRepresentation> wrapped_;
};
class WrappedSkiaGaneshCompoundImageRepresentation
: public SkiaGaneshImageRepresentation {
public:
WrappedSkiaGaneshCompoundImageRepresentation(
GrDirectContext* gr_context,
SharedImageManager* manager,
SharedImageBacking* backing,
MemoryTypeTracker* tracker,
std::unique_ptr<SkiaGaneshImageRepresentation> wrapped)
: SkiaGaneshImageRepresentation(gr_context, manager, backing, tracker),
wrapped_(std::move(wrapped)) {
DCHECK(wrapped_);
}
CompoundImageBacking* compound_backing() {
return static_cast<CompoundImageBacking*>(backing());
}
// SkiaImageRepresentation implementation.
bool SupportsMultipleConcurrentReadAccess() final {
return wrapped_->SupportsMultipleConcurrentReadAccess();
}
std::vector<sk_sp<SkSurface>> BeginWriteAccess(
int final_msaa_count,
const SkSurfaceProps& surface_props,
const gfx::Rect& update_rect,
std::vector<GrBackendSemaphore>* begin_semaphores,
std::vector<GrBackendSemaphore>* end_semaphores,
std::unique_ptr<skgpu::MutableTextureState>* end_state) final {
compound_backing()->NotifyBeginAccess(SharedImageAccessStream::kSkia,
AccessMode::kWrite);
return wrapped_->BeginWriteAccess(final_msaa_count, surface_props,
update_rect, begin_semaphores,
end_semaphores, end_state);
}
std::vector<sk_sp<GrPromiseImageTexture>> BeginWriteAccess(
std::vector<GrBackendSemaphore>* begin_semaphores,
std::vector<GrBackendSemaphore>* end_semaphores,
std::unique_ptr<skgpu::MutableTextureState>* end_state) final {
compound_backing()->NotifyBeginAccess(SharedImageAccessStream::kSkia,
AccessMode::kWrite);
return wrapped_->BeginWriteAccess(begin_semaphores, end_semaphores,
end_state);
}
void EndWriteAccess() final { wrapped_->EndWriteAccess(); }
std::vector<sk_sp<GrPromiseImageTexture>> BeginReadAccess(
std::vector<GrBackendSemaphore>* begin_semaphores,
std::vector<GrBackendSemaphore>* end_semaphores,
std::unique_ptr<skgpu::MutableTextureState>* end_state) final {
compound_backing()->NotifyBeginAccess(SharedImageAccessStream::kSkia,
AccessMode::kRead);
return wrapped_->BeginReadAccess(begin_semaphores, end_semaphores,
end_state);
}
void EndReadAccess() final { wrapped_->EndReadAccess(); }
private:
std::unique_ptr<SkiaGaneshImageRepresentation> wrapped_;
};
class WrappedSkiaGraphiteCompoundImageRepresentation
: public SkiaGraphiteImageRepresentation {
public:
WrappedSkiaGraphiteCompoundImageRepresentation(
SharedImageManager* manager,
SharedImageBacking* backing,
MemoryTypeTracker* tracker,
std::unique_ptr<SkiaGraphiteImageRepresentation> wrapped)
: SkiaGraphiteImageRepresentation(manager, backing, tracker),
wrapped_(std::move(wrapped)) {
CHECK(wrapped_);
}
CompoundImageBacking* compound_backing() {
return static_cast<CompoundImageBacking*>(backing());
}
// SkiaGraphiteImageRepresentation implementation.
bool SupportsMultipleConcurrentReadAccess() final {
return wrapped_->SupportsMultipleConcurrentReadAccess();
}
std::vector<sk_sp<SkSurface>> BeginWriteAccess(
const SkSurfaceProps& surface_props,
const gfx::Rect& update_rect) final {
compound_backing()->NotifyBeginAccess(SharedImageAccessStream::kSkia,
AccessMode::kWrite);
return wrapped_->BeginWriteAccess(surface_props, update_rect);
}
std::vector<skgpu::graphite::BackendTexture> BeginWriteAccess() final {
compound_backing()->NotifyBeginAccess(SharedImageAccessStream::kSkia,
AccessMode::kWrite);
return wrapped_->BeginWriteAccess();
}
void EndWriteAccess() final { wrapped_->EndWriteAccess(); }
std::vector<skgpu::graphite::BackendTexture> BeginReadAccess() final {
compound_backing()->NotifyBeginAccess(SharedImageAccessStream::kSkia,
AccessMode::kRead);
return wrapped_->BeginReadAccess();
}
void EndReadAccess() final { wrapped_->EndReadAccess(); }
private:
std::unique_ptr<SkiaGraphiteImageRepresentation> wrapped_;
};
class WrappedDawnCompoundImageRepresentation : public DawnImageRepresentation {
public:
WrappedDawnCompoundImageRepresentation(
SharedImageManager* manager,
SharedImageBacking* backing,
MemoryTypeTracker* tracker,
std::unique_ptr<DawnImageRepresentation> wrapped)
: DawnImageRepresentation(manager, backing, tracker),
wrapped_(std::move(wrapped)) {
DCHECK(wrapped_);
}
CompoundImageBacking* compound_backing() {
return static_cast<CompoundImageBacking*>(backing());
}
// DawnImageRepresentation implementation.
wgpu::Texture BeginAccess(wgpu::TextureUsage webgpu_usage,
wgpu::TextureUsage internal_usage) final {
AccessMode access_mode =
webgpu_usage & kWriteUsage ? AccessMode::kWrite : AccessMode::kRead;
if (internal_usage & kWriteUsage) {
access_mode = AccessMode::kWrite;
}
compound_backing()->NotifyBeginAccess(SharedImageAccessStream::kDawn,
access_mode);
return wrapped_->BeginAccess(webgpu_usage, internal_usage);
}
void EndAccess() final { wrapped_->EndAccess(); }
private:
std::unique_ptr<DawnImageRepresentation> wrapped_;
};
class WrappedOverlayCompoundImageRepresentation
: public OverlayImageRepresentation {
public:
WrappedOverlayCompoundImageRepresentation(
SharedImageManager* manager,
SharedImageBacking* backing,
MemoryTypeTracker* tracker,
std::unique_ptr<OverlayImageRepresentation> wrapped)
: OverlayImageRepresentation(manager, backing, tracker),
wrapped_(std::move(wrapped)) {
DCHECK(wrapped_);
}
CompoundImageBacking* compound_backing() {
return static_cast<CompoundImageBacking*>(backing());
}
// OverlayImageRepresentation implementation.
bool BeginReadAccess(gfx::GpuFenceHandle& acquire_fence) final {
compound_backing()->NotifyBeginAccess(SharedImageAccessStream::kOverlay,
AccessMode::kRead);
return wrapped_->BeginReadAccess(acquire_fence);
}
void EndReadAccess(gfx::GpuFenceHandle release_fence) final {
return wrapped_->EndReadAccess(std::move(release_fence));
}
#if BUILDFLAG(IS_WIN)
std::optional<gl::DCLayerOverlayImage> GetDCLayerOverlayImage() final {
return wrapped_->GetDCLayerOverlayImage();
}
#endif
private:
std::unique_ptr<OverlayImageRepresentation> wrapped_;
};
// static
bool CompoundImageBacking::IsValidSharedMemoryBufferFormat(
const gfx::Size& size,
viz::SharedImageFormat format) {
if (format.PrefersExternalSampler() ||
!viz::HasEquivalentBufferFormat(format)) {
DVLOG(1) << "Not a valid format: " << format.ToString();
return false;
}
if (!gpu::IsImageSizeValidForGpuMemoryBufferFormat(size,
ToBufferFormat(format))) {
DVLOG(1) << "Invalid image size: " << size.ToString()
<< " for format: " << format.ToString();
return false;
}
return true;
}
// static
SharedImageUsageSet CompoundImageBacking::GetGpuSharedImageUsage(
SharedImageUsageSet usage) {
// Add allow copying from the shmem backing to the gpu backing.
usage |= SHARED_IMAGE_USAGE_CPU_UPLOAD;
if (kAllowShmOverlays) {
// Remove SCANOUT usage since it was previously moved to the shmem backing.
// See: |GetShmSharedImageUsage|
usage.RemoveAll(SharedImageUsageSet(gpu::SHARED_IMAGE_USAGE_SCANOUT));
return usage;
}
return usage;
}
// static
std::unique_ptr<SharedImageBacking> CompoundImageBacking::CreateSharedMemory(
SharedImageBackingFactory* gpu_backing_factory,
const Mailbox& mailbox,
gfx::GpuMemoryBufferHandle handle,
viz::SharedImageFormat format,
const gfx::Size& size,
const gfx::ColorSpace& color_space,
GrSurfaceOrigin surface_origin,
SkAlphaType alpha_type,
SharedImageUsageSet usage,
std::string debug_label) {
DCHECK(IsValidSharedMemoryBufferFormat(size, format));
SharedMemoryRegionWrapper shm_wrapper;
if (!shm_wrapper.Initialize(handle, size, ToBufferFormat(format))) {
DLOG(ERROR) << "Failed to create SharedMemoryRegionWrapper";
return nullptr;
}
auto shm_backing = std::make_unique<SharedMemoryImageBacking>(
mailbox, format, size, color_space, surface_origin, alpha_type,
GetShmSharedImageUsage(usage), debug_label, std::move(shm_wrapper));
shm_backing->SetNotRefCounted();
return base::WrapUnique(new CompoundImageBacking(
mailbox, format, size, color_space, surface_origin, alpha_type, usage,
std::move(debug_label), std::move(shm_backing),
gpu_backing_factory->GetWeakPtr()));
}
// static
std::unique_ptr<SharedImageBacking> CompoundImageBacking::CreateSharedMemory(
SharedImageBackingFactory* gpu_backing_factory,
const Mailbox& mailbox,
viz::SharedImageFormat format,
const gfx::Size& size,
const gfx::ColorSpace& color_space,
GrSurfaceOrigin surface_origin,
SkAlphaType alpha_type,
SharedImageUsageSet usage,
std::string debug_label,
gfx::BufferUsage buffer_usage) {
DCHECK(IsValidSharedMemoryBufferFormat(size, format));
auto buffer_format = ToBufferFormat(format);
auto handle = GpuMemoryBufferImplSharedMemory::CreateGpuMemoryBuffer(
gfx::GpuMemoryBufferId(0), size, buffer_format, buffer_usage);
SharedMemoryRegionWrapper shm_wrapper;
if (!shm_wrapper.Initialize(handle, size, buffer_format)) {
DLOG(ERROR) << "Failed to create SharedMemoryRegionWrapper";
return nullptr;
}
auto shm_backing = std::make_unique<SharedMemoryImageBacking>(
mailbox, format, size, color_space, surface_origin, alpha_type,
GetShmSharedImageUsage(usage), debug_label, std::move(shm_wrapper),
std::move(handle));
shm_backing->SetNotRefCounted();
return base::WrapUnique(new CompoundImageBacking(
mailbox, format, size, color_space, surface_origin, alpha_type, usage,
std::move(debug_label), std::move(shm_backing),
gpu_backing_factory->GetWeakPtr(), std::move(buffer_usage)));
}
CompoundImageBacking::CompoundImageBacking(
const Mailbox& mailbox,
viz::SharedImageFormat format,
const gfx::Size& size,
const gfx::ColorSpace& color_space,
GrSurfaceOrigin surface_origin,
SkAlphaType alpha_type,
SharedImageUsageSet usage,
std::string debug_label,
std::unique_ptr<SharedMemoryImageBacking> shm_backing,
base::WeakPtr<SharedImageBackingFactory> gpu_backing_factory,
std::optional<gfx::BufferUsage> buffer_usage)
: SharedImageBacking(mailbox,
format,
size,
color_space,
surface_origin,
alpha_type,
usage,
debug_label,
shm_backing->GetEstimatedSize(),
/*is_thread_safe=*/false,
std::move(buffer_usage)) {
DCHECK(shm_backing);
DCHECK_EQ(size, shm_backing->size());
elements_[0].backing = std::move(shm_backing);
elements_[0].access_streams = kMemoryStreamSet;
if (kAllowShmOverlays && usage.Has(gpu::SHARED_IMAGE_USAGE_SCANOUT)) {
elements_[0].access_streams.Put(SharedImageAccessStream::kOverlay);
}
elements_[0].content_id_ = latest_content_id_;
elements_[1].create_callback = base::BindOnce(
&CompoundImageBacking::LazyCreateBacking, base::Unretained(this),
std::move(gpu_backing_factory), std::move(debug_label));
elements_[1].access_streams =
base::Difference(AccessStreamSet::All(), kMemoryStreamSet);
}
CompoundImageBacking::~CompoundImageBacking() {
if (pending_copy_to_gmb_callback_) {
std::move(pending_copy_to_gmb_callback_).Run(/*success=*/false);
}
}
void CompoundImageBacking::NotifyBeginAccess(SharedImageAccessStream stream,
RepresentationAccessMode mode) {
// Compound backings don't support VAAPI yet.
DCHECK_NE(stream, SharedImageAccessStream::kVaapi);
// TODO(kylechar): Keep track of access to the compound backing as we
// only want to update a backing if it's not currently being accessed.
auto& access_element = GetElement(stream);
if (access_element.access_streams.Has(SharedImageAccessStream::kMemory)) {
DCHECK_EQ(mode, RepresentationAccessMode::kRead);
return;
}
auto& shm_element = GetElement(SharedImageAccessStream::kMemory);
DCHECK_NE(&shm_element, &access_element);
bool updated_backing = false;
if (!HasLatestContent(access_element)) {
DCHECK(HasLatestContent(shm_element));
auto* gpu_backing = access_element.GetBacking();
if (gpu_backing &&
gpu_backing->UploadFromMemory(GetSharedMemoryPixmaps())) {
updated_backing = true;
} else {
DLOG(ERROR) << "Failed to upload from shared memory to GPU backing";
}
}
// If a backing was updated or this is write access update what has the latest
// content.
bool is_write_access = mode == RepresentationAccessMode::kWrite;
if (updated_backing || is_write_access)
SetLatestContent(stream, is_write_access);
}
SharedImageBackingType CompoundImageBacking::GetType() const {
return SharedImageBackingType::kCompound;
}
void CompoundImageBacking::Update(std::unique_ptr<gfx::GpuFence> in_fence) {
DCHECK(!in_fence);
SetLatestContent(SharedImageAccessStream::kMemory,
/*write_access=*/true);
}
bool CompoundImageBacking::CopyToGpuMemoryBuffer() {
auto& shm_element = GetElement(SharedImageAccessStream::kMemory);
if (HasLatestContent(shm_element)) {
return true;
}
auto* gpu_backing = elements_[1].GetBacking();
const std::vector<SkPixmap>& pixmaps = GetSharedMemoryPixmaps();
if (!gpu_backing || !gpu_backing->ReadbackToMemory(pixmaps)) {
DLOG(ERROR) << "Failed to copy from GPU backing to shared memory";
return false;
}
SetLatestContent(SharedImageAccessStream::kMemory, /*write_access=*/false);
return true;
}
void CompoundImageBacking::CopyToGpuMemoryBufferAsync(
base::OnceCallback<void(bool)> callback) {
auto& shm_element = GetElement(SharedImageAccessStream::kMemory);
if (HasLatestContent(shm_element)) {
std::move(callback).Run(true);
return;
}
if (pending_copy_to_gmb_callback_) {
DLOG(ERROR) << "Existing CopyToGpuMemoryBuffer operation pending";
std::move(callback).Run(false);
return;
}
auto* gpu_backing = elements_[1].GetBacking();
if (!gpu_backing) {
DLOG(ERROR) << "Failed to copy from GPU backing to shared memory";
std::move(callback).Run(false);
return;
}
pending_copy_to_gmb_callback_ = std::move(callback);
gpu_backing->ReadbackToMemoryAsync(
GetSharedMemoryPixmaps(),
base::BindOnce(&CompoundImageBacking::OnCopyToGpuMemoryBufferComplete,
base::Unretained(this)));
}
void CompoundImageBacking::OnCopyToGpuMemoryBufferComplete(bool success) {
if (success) {
SetLatestContent(SharedImageAccessStream::kMemory, /*write_access=*/false);
}
std::move(pending_copy_to_gmb_callback_).Run(success);
}
gfx::Rect CompoundImageBacking::ClearedRect() const {
// Copy on access will always ensure backing is cleared by first access.
return gfx::Rect(size());
}
void CompoundImageBacking::SetClearedRect(const gfx::Rect& cleared_rect) {}
gfx::GpuMemoryBufferHandle CompoundImageBacking::GetGpuMemoryBufferHandle() {
auto& element = GetElement(SharedImageAccessStream::kMemory);
CHECK(element.backing);
return element.backing->GetGpuMemoryBufferHandle();
}
std::unique_ptr<DawnImageRepresentation> CompoundImageBacking::ProduceDawn(
SharedImageManager* manager,
MemoryTypeTracker* tracker,
const wgpu::Device& device,
wgpu::BackendType backend_type,
std::vector<wgpu::TextureFormat> view_formats,
scoped_refptr<SharedContextState> context_state) {
auto* backing = GetBacking(SharedImageAccessStream::kDawn);
if (!backing)
return nullptr;
auto real_rep = backing->ProduceDawn(manager, tracker, device, backend_type,
std::move(view_formats), context_state);
if (!real_rep)
return nullptr;
return std::make_unique<WrappedDawnCompoundImageRepresentation>(
manager, this, tracker, std::move(real_rep));
}
std::unique_ptr<GLTextureImageRepresentation>
CompoundImageBacking::ProduceGLTexture(SharedImageManager* manager,
MemoryTypeTracker* tracker) {
auto* backing = GetBacking(SharedImageAccessStream::kGL);
if (!backing)
return nullptr;
auto real_rep = backing->ProduceGLTexture(manager, tracker);
if (!real_rep)
return nullptr;
return std::make_unique<WrappedGLTextureCompoundImageRepresentation>(
manager, this, tracker, std::move(real_rep));
}
std::unique_ptr<GLTexturePassthroughImageRepresentation>
CompoundImageBacking::ProduceGLTexturePassthrough(SharedImageManager* manager,
MemoryTypeTracker* tracker) {
auto* backing = GetBacking(SharedImageAccessStream::kGL);
if (!backing)
return nullptr;
auto real_rep = backing->ProduceGLTexturePassthrough(manager, tracker);
if (!real_rep)
return nullptr;
return std::make_unique<
WrappedGLTexturePassthroughCompoundImageRepresentation>(
manager, this, tracker, std::move(real_rep));
}
std::unique_ptr<SkiaGaneshImageRepresentation>
CompoundImageBacking::ProduceSkiaGanesh(
SharedImageManager* manager,
MemoryTypeTracker* tracker,
scoped_refptr<SharedContextState> context_state) {
auto* backing = GetBacking(SharedImageAccessStream::kSkia);
if (!backing)
return nullptr;
auto real_rep = backing->ProduceSkiaGanesh(manager, tracker, context_state);
if (!real_rep)
return nullptr;
auto* gr_context = context_state ? context_state->gr_context() : nullptr;
return std::make_unique<WrappedSkiaGaneshCompoundImageRepresentation>(
gr_context, manager, this, tracker, std::move(real_rep));
}
std::unique_ptr<SkiaGraphiteImageRepresentation>
CompoundImageBacking::ProduceSkiaGraphite(
SharedImageManager* manager,
MemoryTypeTracker* tracker,
scoped_refptr<SharedContextState> context_state) {
auto* backing = GetBacking(SharedImageAccessStream::kSkia);
if (!backing) {
return nullptr;
}
auto real_rep = backing->ProduceSkiaGraphite(manager, tracker, context_state);
if (!real_rep) {
return nullptr;
}
return std::make_unique<WrappedSkiaGraphiteCompoundImageRepresentation>(
manager, this, tracker, std::move(real_rep));
}
std::unique_ptr<OverlayImageRepresentation>
CompoundImageBacking::ProduceOverlay(SharedImageManager* manager,
MemoryTypeTracker* tracker) {
auto* backing = GetBacking(SharedImageAccessStream::kOverlay);
if (!backing)
return nullptr;
auto real_rep = backing->ProduceOverlay(manager, tracker);
if (!real_rep)
return nullptr;
return std::make_unique<WrappedOverlayCompoundImageRepresentation>(
manager, this, tracker, std::move(real_rep));
}
base::trace_event::MemoryAllocatorDump* CompoundImageBacking::OnMemoryDump(
const std::string& dump_name,
base::trace_event::MemoryAllocatorDumpGuid client_guid,
base::trace_event::ProcessMemoryDump* pmd,
uint64_t client_tracing_id) {
// Create dump but don't add scalar size. The size will be inferred from the
// sizes of the sub-backings.
base::trace_event::MemoryAllocatorDump* dump =
pmd->CreateAllocatorDump(dump_name);
dump->AddString("type", "", GetName());
dump->AddString("dimensions", "", size().ToString());
dump->AddString("format", "", format().ToString());
dump->AddString("usage", "", CreateLabelForSharedImageUsage(usage()));
// Add ownership edge to `client_guid` which expresses shared ownership with
// the client process for the top level dump.
pmd->CreateSharedGlobalAllocatorDump(client_guid);
pmd->AddOwnershipEdge(dump->guid(), client_guid,
static_cast<int>(TracingImportance::kNotOwner));
// Add dumps nested under `dump_name` for child backings owned by compound
// image. These get different shared GUIDs to add ownership edges with GPU
// texture or shared memory.
for (int i = 0; i < static_cast<int>(elements_.size()); ++i) {
auto* backing = elements_[i].backing.get();
if (!backing)
continue;
auto element_client_guid = GetSubBackingGUIDForTracing(mailbox(), i + 1);
std::string element_dump_name =
base::StringPrintf("%s/element_%d", dump_name.c_str(), i);
backing->OnMemoryDump(element_dump_name, element_client_guid, pmd,
client_tracing_id);
}
return dump;
}
const std::vector<SkPixmap>& CompoundImageBacking::GetSharedMemoryPixmaps() {
auto* shm_backing = GetElement(SharedImageAccessStream::kMemory).GetBacking();
DCHECK(shm_backing);
return static_cast<SharedMemoryImageBacking*>(shm_backing)->pixmaps();
}
CompoundImageBacking::ElementHolder& CompoundImageBacking::GetElement(
SharedImageAccessStream stream) {
for (auto& element : elements_) {
// For each access stream there should be exactly one element where this
// returns true.
if (element.access_streams.Has(stream))
return element;
}
NOTREACHED();
}
SharedImageBacking* CompoundImageBacking::GetBacking(
SharedImageAccessStream stream) {
return GetElement(stream).GetBacking();
}
void CompoundImageBacking::LazyCreateBacking(
base::WeakPtr<SharedImageBackingFactory> factory,
std::string debug_label,
std::unique_ptr<SharedImageBacking>& backing) {
if (!factory) {
DLOG(ERROR) << "Can't allocate backing after image has been destroyed";
return;
}
backing = factory->CreateSharedImage(
mailbox(), format(), kNullSurfaceHandle, size(), color_space(),
surface_origin(), alpha_type(),
GetGpuSharedImageUsage(SharedImageUsageSet(usage())),
std::move(debug_label), /*is_thread_safe=*/false);
if (!backing) {
DLOG(ERROR) << "Failed to allocate GPU backing";
return;
}
// Since the owned GPU backing is never registered with SharedImageManager
// it's not recorded in UMA histogram there.
UMA_HISTOGRAM_ENUMERATION("GPU.SharedImage.BackingType", backing->GetType());
backing->SetNotRefCounted();
backing->SetCleared();
// Update peak GPU memory tracking with the new estimated size.
size_t estimated_size = 0;
for (auto& element : elements_) {
if (element.backing)
estimated_size += element.backing->GetEstimatedSize();
}
AutoLock auto_lock(this);
UpdateEstimatedSize(estimated_size);
}
bool CompoundImageBacking::HasLatestContent(ElementHolder& element) {
return element.content_id_ == latest_content_id_;
}
void CompoundImageBacking::SetLatestContent(SharedImageAccessStream stream,
bool write_access) {
if (write_access)
++latest_content_id_;
auto& element = GetElement(stream);
DCHECK(element.backing);
element.content_id_ = latest_content_id_;
}
void CompoundImageBacking::OnAddSecondaryReference() {
// When client adds a reference from another processes it expects this
// SharedImage can outlive original factory ref and so potentially
// SharedimageFactory. We should create all backings now as we might not have
// access to corresponding SharedImageBackingFactories later.
for (auto& element : elements_) {
element.CreateBackingIfNecessary();
}
}
CompoundImageBacking::ElementHolder::ElementHolder() = default;
CompoundImageBacking::ElementHolder::~ElementHolder() = default;
void CompoundImageBacking::ElementHolder::CreateBackingIfNecessary() {
if (create_callback) {
std::move(create_callback).Run(backing);
}
}
SharedImageBacking* CompoundImageBacking::ElementHolder::GetBacking() {
CreateBackingIfNecessary();
return backing.get();
}
} // namespace gpu