File: gpu_canvas_context.cc

package info (click to toggle)
chromium 138.0.7204.183-1
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 6,071,908 kB
  • sloc: cpp: 34,937,088; ansic: 7,176,967; javascript: 4,110,704; python: 1,419,953; asm: 946,768; xml: 739,971; pascal: 187,324; sh: 89,623; perl: 88,663; objc: 79,944; sql: 50,304; cs: 41,786; fortran: 24,137; makefile: 21,806; php: 13,980; tcl: 13,166; yacc: 8,925; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (1011 lines) | stat: -rw-r--r-- 38,351 bytes parent folder | download | duplicates (2)
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
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
// Copyright 2019 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "third_party/blink/renderer/modules/webgpu/gpu_canvas_context.h"

#include "components/viz/common/resources/shared_image_format_utils.h"
#include "gpu/command_buffer/client/raster_interface.h"
#include "gpu/config/gpu_finch_features.h"
#include "third_party/blink/renderer/bindings/core/v8/v8_union_htmlcanvaselement_offscreencanvas.h"
#include "third_party/blink/renderer/bindings/modules/v8/v8_gpu_canvas_alpha_mode.h"
#include "third_party/blink/renderer/bindings/modules/v8/v8_gpu_canvas_configuration.h"
#include "third_party/blink/renderer/bindings/modules/v8/v8_gpu_canvas_tone_mapping.h"
#include "third_party/blink/renderer/bindings/modules/v8/v8_gpu_canvas_tone_mapping_mode.h"
#include "third_party/blink/renderer/bindings/modules/v8/v8_union_offscreen_rendering_context.h"
#include "third_party/blink/renderer/bindings/modules/v8/v8_union_rendering_context.h"
#include "third_party/blink/renderer/core/frame/web_feature.h"
#include "third_party/blink/renderer/core/html/canvas/predefined_color_space.h"
#include "third_party/blink/renderer/core/imagebitmap/image_bitmap.h"
#include "third_party/blink/renderer/core/offscreencanvas/offscreen_canvas.h"
#include "third_party/blink/renderer/modules/webgpu/dawn_conversions.h"
#include "third_party/blink/renderer/modules/webgpu/gpu.h"
#include "third_party/blink/renderer/modules/webgpu/gpu_adapter.h"
#include "third_party/blink/renderer/modules/webgpu/gpu_device.h"
#include "third_party/blink/renderer/modules/webgpu/gpu_queue.h"
#include "third_party/blink/renderer/modules/webgpu/gpu_supported_features.h"
#include "third_party/blink/renderer/modules/webgpu/gpu_texture.h"
#include "third_party/blink/renderer/platform/bindings/exception_state.h"
#include "third_party/blink/renderer/platform/graphics/accelerated_static_bitmap_image.h"
#include "third_party/blink/renderer/platform/graphics/canvas_resource_provider.h"
#include "third_party/blink/renderer/platform/graphics/gpu/shared_gpu_context.h"
#include "third_party/blink/renderer/platform/graphics/gpu/webgpu_mailbox_texture.h"
#include "third_party/blink/renderer/platform/graphics/gpu/webgpu_texture_alpha_clearer.h"
#include "third_party/blink/renderer/platform/graphics/skia/skia_utils.h"
#include "third_party/blink/renderer/platform/graphics/unaccelerated_static_bitmap_image.h"
#include "third_party/blink/renderer/platform/scheduler/public/thread_scheduler.h"
#include "third_party/skia/include/core/SkImage.h"

namespace blink {

namespace {

bool IsContextFormatSupported(V8GPUTextureFormat::Enum format) {
  switch (format) {
    case V8GPUTextureFormat::Enum::kBgra8Unorm:
    case V8GPUTextureFormat::Enum::kRgba8Unorm:
    case V8GPUTextureFormat::Enum::kRgba16Float:
      return true;
    default:
      return false;
  }
}

}  // namespace

GPUCanvasContext::Factory::~Factory() = default;

CanvasRenderingContext* GPUCanvasContext::Factory::Create(
    CanvasRenderingContextHost* host,
    const CanvasContextCreationAttributesCore& attrs) {
  CanvasRenderingContext* rendering_context =
      MakeGarbageCollected<GPUCanvasContext>(host, attrs);
  DCHECK(host);
  return rendering_context;
}

CanvasRenderingContext::CanvasRenderingAPI
GPUCanvasContext::Factory::GetRenderingAPI() const {
  return CanvasRenderingContext::CanvasRenderingAPI::kWebgpu;
}

GPUCanvasContext::GPUCanvasContext(
    CanvasRenderingContextHost* host,
    const CanvasContextCreationAttributesCore& attrs)
    : CanvasRenderingContext(host, attrs, CanvasRenderingAPI::kWebgpu) {
  texture_descriptor_ = {};
}

GPUCanvasContext::~GPUCanvasContext() {
  copy_to_swap_texture_required_ = false;

  // Perform destruction that's safe to do inside a GC (as in it doesn't touch
  // other GC objects).
  if (swap_buffers_) {
    swap_buffers_->Neuter();
  }
}

void GPUCanvasContext::Trace(Visitor* visitor) const {
  visitor->Trace(device_);
  visitor->Trace(texture_);
  visitor->Trace(swap_texture_);
  ScriptWrappable::Trace(visitor);
  CanvasRenderingContext::Trace(visitor);
}

// CanvasRenderingContext implementation
V8RenderingContext* GPUCanvasContext::AsV8RenderingContext() {
  return MakeGarbageCollected<V8RenderingContext>(this);
}

V8OffscreenRenderingContext* GPUCanvasContext::AsV8OffscreenRenderingContext() {
  return MakeGarbageCollected<V8OffscreenRenderingContext>(this);
}

SkAlphaType GPUCanvasContext::GetAlphaType() const {
  if (!swap_buffers_) {
    return kPremul_SkAlphaType;
  }
  return alpha_mode_ == V8GPUCanvasAlphaMode::Enum::kOpaque
             ? kOpaque_SkAlphaType
             : kPremul_SkAlphaType;
}

viz::SharedImageFormat GPUCanvasContext::GetSharedImageFormat() const {
  if (!swap_buffers_) {
    return GetN32FormatForCanvas();
    ;
  }
  return swap_buffers_->Format();
}

gfx::ColorSpace GPUCanvasContext::GetColorSpace() const {
  if (!swap_buffers_) {
    return gfx::ColorSpace::CreateSRGB();
  }
  return PredefinedColorSpaceToGfxColorSpace(color_space_);
}

void GPUCanvasContext::Stop() {
  ReplaceDrawingBuffer(/*destroy_swap_buffers*/ true);
  stopped_ = true;
}

cc::Layer* GPUCanvasContext::CcLayer() const {
  if (swap_buffers_) {
    return swap_buffers_->CcLayer();
  }
  return nullptr;
}

void GPUCanvasContext::Reshape(int width, int height) {
  if (stopped_) {
    return;
  }

  // Steps for canvas context resizing:
  // 1. Replace the drawing buffer of context.
  ReplaceDrawingBuffer(/* destroy_swap_buffers */ false);

  // 2. Let configuration be context.[[configuration]]
  // 3. If configuration is not null:
  //   1. Set context.[[textureDescriptor]] to the GPUTextureDescriptor for the
  //      canvas and configuration(canvas, configuration).
  texture_descriptor_.size = {static_cast<uint32_t>(width),
                              static_cast<uint32_t>(height), 1};

  swap_texture_descriptor_.size = texture_descriptor_.size;

  // If we don't notify the host that something has changed it may never check
  // for the new cc::Layer.
  Host()->SetNeedsCompositingUpdate();
}

scoped_refptr<StaticBitmapImage> GPUCanvasContext::GetImage(FlushReason) {
  if (!swap_buffers_) {
    return nullptr;
  }

  if (device_->IsDestroyed()) {
    return MakeFallbackStaticBitmapImage(alpha_mode_);
  }

  // If there is a current texture, create a snapshot from it.
  if (texture_ && !texture_->IsDestroyed()) {
    return SnapshotInternal(texture_->GetHandle());
  } else if (swap_texture_) {
    return SnapshotInternal(swap_texture_->GetHandle());
  }

  // If there is no current texture, return a snapshot of the front buffer if
  // possible.
  auto front_buffer_texture = GetFrontBufferMailboxTexture();
  if (!front_buffer_texture) {
    return nullptr;
  }

  return SnapshotInternal(front_buffer_texture->GetTexture());
}

CanvasResourceProvider* GPUCanvasContext::PaintRenderingResultsToCanvas(
    SourceDrawingBuffer source_buffer) {
  if (!swap_buffers_) {
    return Host()->ResourceProvider();
  }

  if (Host()->ResourceProvider() &&
      Host()->ResourceProvider()->Size() != swap_buffers_->Size()) {
    Host()->DiscardResourceProvider();
  }

  CanvasResourceProvider* resource_provider =
      Host()->GetOrCreateCanvasResourceProvider();
  if (!resource_provider) {
    return nullptr;
  }

  if (device_->IsDestroyed()) {
    SkColor4f color = alpha_mode_ == V8GPUCanvasAlphaMode::Enum::kOpaque
                          ? SkColors::kBlack
                          : SkColors::kTransparent;
    resource_provider->Canvas().clear(color);
    resource_provider->FlushCanvas(FlushReason::kClear);
    return resource_provider;
  }

  wgpu::Texture texture;

  scoped_refptr<WebGPUMailboxTexture> front_buffer_texture;
  if (source_buffer == kFrontBuffer) {
#if BUILDFLAG(IS_LINUX)
    // By returning false here the canvas will show up as black in the scenarios
    // that copy the front buffer, such as printing.
    // TODO(crbug.com/40902474): Support concurrent SharedImage reads via Dawn
    // on Linux backings and enable the below codepath.
    return nullptr;
#else
    // Create a WebGPU texture backed by the front buffer's SharedImage.
    front_buffer_texture = GetFrontBufferMailboxTexture();
    if (!front_buffer_texture) {
      return resource_provider;
    }

    texture = front_buffer_texture->GetTexture();
#endif
  } else {
    if (!texture_) {
      return resource_provider;
    }
    texture = texture_->GetHandle();
  }

  CopyTextureToResourceProvider(texture, resource_provider);
  return resource_provider;
}

bool GPUCanvasContext::CopyRenderingResultsToVideoFrame(
    WebGraphicsContext3DVideoFramePool* frame_pool,
    SourceDrawingBuffer src_buffer,
    const gfx::ColorSpace& dst_color_space,
    VideoFrameCopyCompletedCallback callback) {
  if (!swap_buffers_) {
    return false;
  }

  return swap_buffers_->CopyToVideoFrame(frame_pool, src_buffer,
                                         dst_color_space, std::move(callback));
}

bool GPUCanvasContext::PushFrame() {
  DCHECK(Host());
  DCHECK(Host()->IsOffscreenCanvas());

  if (!swap_buffers_) {
    return false;
  }

  gpu::SyncToken sync_token;
  viz::ReleaseCallback release_callback;
  auto client_si =
      swap_buffers_->ExportCurrentSharedImage(sync_token, &release_callback);
  if (!client_si) {
    return false;
  }

  auto canvas_resource = ExternalCanvasResource::Create(
      std::move(client_si), sync_token,
      viz::TransferableResource::ResourceSource::kWebGPUSwapBuffer,
      swap_buffers_->GetHDRMetadata(), std::move(release_callback),
      GetContextProviderWeakPtr(),
      /*resource_provider=*/nullptr);
  if (!canvas_resource)
    return false;

  const int width = canvas_resource->Size().width();
  const int height = canvas_resource->Size().height();
  return Host()->PushFrame(std::move(canvas_resource),
                           SkIRect::MakeWH(width, height));
}

ImageBitmap* GPUCanvasContext::TransferToImageBitmap(
    ScriptState* script_state,
    ExceptionState& exception_state) {
  // If the canvas configuration is invalid, WebGPU requires that we give a
  // fallback black ImageBitmap if possible.
  if (!swap_buffers_) {
    auto staticBitmapImage =
        MakeFallbackStaticBitmapImage(V8GPUCanvasAlphaMode::Enum::kOpaque);
    return staticBitmapImage
               ? MakeGarbageCollected<ImageBitmap>(staticBitmapImage)
               : nullptr;
  }

  gpu::SyncToken sk_image_sync_token;
  viz::ReleaseCallback release_callback;
  auto client_si = swap_buffers_->ExportCurrentSharedImage(sk_image_sync_token,
                                                           &release_callback);
  if (!client_si) {
    // If we can't get a mailbox, return an transparent black ImageBitmap.
    // The only situation in which this could happen is when two or more calls
    // to transferToImageBitmap are made back-to-back, or when the context gets
    // lost.
    auto staticBitmapImage = MakeFallbackStaticBitmapImage(alpha_mode_);
    return staticBitmapImage
               ? MakeGarbageCollected<ImageBitmap>(staticBitmapImage)
               : nullptr;
  }
  DCHECK(release_callback);

  auto format = client_si->format();
  auto size = client_si->size();

  return MakeGarbageCollected<ImageBitmap>(
      AcceleratedStaticBitmapImage::CreateFromCanvasSharedImage(
          std::move(client_si), sk_image_sync_token,
          /* shared_image_texture_id = */ 0, size, format, kPremul_SkAlphaType,
          gfx::ColorSpace::CreateSRGB(), GetContextProviderWeakPtr(),
          base::PlatformThread::CurrentRef(),
          ThreadScheduler::Current()->CleanupTaskRunner(),
          std::move(release_callback)));
}

V8UnionHTMLCanvasElementOrOffscreenCanvas*
GPUCanvasContext::getHTMLOrOffscreenCanvas() const {
  if (Host()->IsOffscreenCanvas()) {
    return MakeGarbageCollected<V8UnionHTMLCanvasElementOrOffscreenCanvas>(
        static_cast<OffscreenCanvas*>(Host()));
  }
  return MakeGarbageCollected<V8UnionHTMLCanvasElementOrOffscreenCanvas>(
      static_cast<HTMLCanvasElement*>(Host()));
}

void GPUCanvasContext::configure(const GPUCanvasConfiguration* descriptor,
                                 ExceptionState& exception_state) {
  DCHECK(descriptor);

  if (stopped_ || !Host()) {
    // This is probably not possible, or at least would only happen during page
    // shutdown.
    exception_state.ThrowDOMException(DOMExceptionCode::kUnknownError,
                                      "canvas has been destroyed");
    return;
  }

  if (!descriptor->device()->ValidateTextureFormatUsage(descriptor->format(),
                                                        exception_state)) {
    return;
  }

  for (auto view_format : descriptor->viewFormats()) {
    if (!descriptor->device()->ValidateTextureFormatUsage(view_format,
                                                          exception_state)) {
      return;
    }
  }

  if (!IsContextFormatSupported(descriptor->format().AsEnum())) {
    exception_state.ThrowTypeError(
        String::Format("Unsupported canvas context format '%s'.",
                       V8GPUTextureFormat(descriptor->format()).AsCStr()));
    return;
  }

  // As soon as the validation for extensions for usage and formats passes, the
  // canvas is "configured" and calls to getNextTexture() will return GPUTexture
  // objects (valid or invalid) and not throw.
  configured_ = true;

  view_formats_ = AsDawnEnum<wgpu::TextureFormat>(descriptor->viewFormats());
  gfx::Size host_size = Host()->Size();

  // Set the default values of the member corresponding to
  // GPUCanvasContext.[[texture_descriptor]] in the WebGPU spec.
  texture_descriptor_ = {
      // Set the values from the configuration descriptor
      .usage = AsDawnFlags<wgpu::TextureUsage>(descriptor->usage()),
      .dimension = wgpu::TextureDimension::e2D,
      .size = {static_cast<uint32_t>(host_size.width()),
               static_cast<uint32_t>(host_size.height())},
      .format = AsDawnEnum(descriptor->format()),
      .viewFormatCount = descriptor->viewFormats().size(),
      .viewFormats = view_formats_.data(),
      // Set the size of the texture in case there was no Reshape() since the
      // creation of the context.
  };

  // Reconfiguring the context discards previous drawing buffers but we also
  // destroy the swap buffers so that any validation error below will cause
  // swap_buffers_ to be nullptr and getCurrentTexture() to fail.
  ReplaceDrawingBuffer(/*destroy_swap_buffers*/ true);

  // Store the configured device separately, even if the configuration fails, so
  // that errors can be generated in the appropriate error scope.
  device_ = descriptor->device();

  // The WebGPU spec requires that a validation error be produced if the
  // descriptor is invalid. However no call to AssociateMailbox is done in
  // configure() which would produce the error. Directly request that the
  // descriptor be validated instead.
  device_->GetHandle().ValidateTextureDescriptor(&texture_descriptor_);

  copy_to_swap_texture_required_ = false;
#if BUILDFLAG(IS_ANDROID)
  if (texture_descriptor_.format == wgpu::TextureFormat::BGRA8Unorm) {
    // BGRA8Unorm is not natively supported by Android's compositor.
    copy_to_swap_texture_required_ = true;
  }
  if ((texture_descriptor_.usage & wgpu::TextureUsage::StorageBinding) != 0) {
    // Storage images are not supported on some AHB/gralloc versions.
    copy_to_swap_texture_required_ = true;
  }
#endif
#if BUILDFLAG(IS_MAC)
  if (texture_descriptor_.format == wgpu::TextureFormat::RGBA8Unorm) {
    // RGBA8Unorm is not natively supported by MacOS's compositor.
    copy_to_swap_texture_required_ = true;
  }
#endif

  // If the context is configured with STORAGE_BINDING texture usage and
  // "bgra8unorm" is the preferred format but the adapter doesn't support the
  // "bgra8unorm-storage" feature, we can guess that the app is using the
  // non-preferred texture format here because it's the only way to get a
  // storage-compatible canvas texture. As such, we'll suppress the warning
  // about not using the preferred format.
  suppress_preferred_format_warning_ = false;
  if (copy_to_swap_texture_required_ &&
      GPU::GetPreferredCanvasFormat() == wgpu::TextureFormat::BGRA8Unorm &&
      texture_descriptor_.usage & wgpu::TextureUsage::StorageBinding &&
      !device_->adapter()->features()->Has(
          V8GPUFeatureName::Enum::kBgra8UnormStorage)) {
    suppress_preferred_format_warning_ = true;
  }

  alpha_mode_ = descriptor->alphaMode().AsEnum();

  // There are two scenarios that require special configuration here:
  // * In the scenario that the system doesn't support the requested format but
  //   it's one that WebGPU is required to offer, a separate texture will be
  //   returned instead with the desired format and the texture will be copied
  //   to the swap buffer provider's texture with the system-supported format
  //   when we're ready to present.
  // * In the alternative scenario where the texture returned to the user will
  //   be the swap buffer texture, the texture will have various internal
  //   operations done to it depending on the alpha mode.

  // First configure `texture_descriptor_` as necessary in the case where the
  // swap buffer texture will be returned to the user. Note that it is necessary
  // to do this *before* copying `texture_descriptor_` to
  // `swap_texture_descriptor_`: Each can end up being used in operations on the
  // texture depending on whether the operation is on `texture_` or
  // `swap_texture_` (which will of course be the same texture in this case).
  if (!copy_to_swap_texture_required_) {
    // `texture_` will be used as the source of either CopyTextureForBrowser()
    // or CopyTextureToTexture() operations (the former if the alpha mode is
    // opaque, the latter if it is not). In either case, CopySrc is required.
    texture_internal_usage_ = {{
        .internalUsage = wgpu::TextureUsage::CopySrc,
    }};
    if (alpha_mode_ == V8GPUCanvasAlphaMode::Enum::kOpaque) {
      // `texture_` will be used as the source of CopyTextureForBrowser()
      // operations and will have alpha clearing done on it. The former requires
      // the TextureBinding usage (in addition to the already-present CopySrc),
      // while the latter requires RenderAttachment.
      texture_internal_usage_.internalUsage |=
          wgpu::TextureUsage::TextureBinding |
          wgpu::TextureUsage::RenderAttachment;
    }

    texture_descriptor_.nextInChain = &texture_internal_usage_;
  }

  // Copy `texture_descriptor_` to `swap_texture_descriptor_` before making any
  // distinct configuration on each of them that is necessary in the case where
  // they will be distinct textures.
  swap_texture_descriptor_ = texture_descriptor_;
  if (copy_to_swap_texture_required_) {
    // The texture returned to the user will require both the CopySrc and
    // TextureBinding usages in order to be used with CopyTextureForBrowser.
    texture_internal_usage_ = {{
        .internalUsage =
            wgpu::TextureUsage::CopySrc | wgpu::TextureUsage::TextureBinding,
    }};

    texture_descriptor_.nextInChain = &texture_internal_usage_;

    // The swap buffer texture will require both CopyDst and RenderAttachment
    // in order to be used with CopyTextureForBrowser.
    swap_texture_descriptor_.usage =
        wgpu::TextureUsage::CopyDst | wgpu::TextureUsage::RenderAttachment;

    // In cases where a copy is necessary the swap buffers will always use the
    // preferred canvas format.
    swap_texture_descriptor_.format = GPU::GetPreferredCanvasFormat();

    // The swap buffer texture doesn't need any view formats.
    swap_texture_descriptor_.viewFormats = nullptr;
    swap_texture_descriptor_.viewFormatCount = 0;
  }

  if (!ValidateAndConvertColorSpace(descriptor->colorSpace(), color_space_,
                                    exception_state)) {
    return;
  }

  gfx::HDRMetadata hdr_metadata;
  if (descriptor->hasToneMapping() && descriptor->toneMapping()->hasMode()) {
    tone_mapping_mode_ = descriptor->toneMapping()->mode().AsEnum();
    switch (tone_mapping_mode_) {
      case V8GPUCanvasToneMappingMode::Enum::kStandard:
        break;
      case V8GPUCanvasToneMappingMode::Enum::kExtended:
        hdr_metadata.extended_range.emplace(
            /*current_headroom=*/gfx::HdrMetadataExtendedRange::
                kDefaultHdrHeadroom,
            /*desired_headroom=*/gfx::HdrMetadataExtendedRange::
                kDefaultHdrHeadroom);
        break;
    }
  }

  const wgpu::DawnTextureInternalUsageDescriptor* internal_usage_desc = nullptr;
  if (const wgpu::ChainedStruct* next_in_chain =
          swap_texture_descriptor_.nextInChain) {
    // The internal usage descriptor is the only valid struct to chain.
    CHECK_EQ(next_in_chain->sType,
             wgpu::SType::DawnTextureInternalUsageDescriptor);
    internal_usage_desc =
        static_cast<const wgpu::DawnTextureInternalUsageDescriptor*>(
            next_in_chain);
  }
  auto internal_usage = internal_usage_desc ? internal_usage_desc->internalUsage
                                            : wgpu::TextureUsage::None;
  swap_buffers_ = base::AdoptRef(new WebGPUSwapBufferProvider(
      this, device_->GetDawnControlClient(), device_->GetHandle(),
      swap_texture_descriptor_.usage, internal_usage,
      swap_texture_descriptor_.format, color_space_, hdr_metadata));

  // Note: SetContentsOpaque is only an optimization hint. It doesn't
  // actually make the contents opaque.
  switch (alpha_mode_) {
    case V8GPUCanvasAlphaMode::Enum::kOpaque: {
      if (!alpha_clearer_ ||
          !alpha_clearer_->IsCompatible(device_->GetHandle(),
                                        swap_texture_descriptor_.format)) {
        alpha_clearer_ = base::MakeRefCounted<WebGPUTextureAlphaClearer>(
            device_->GetDawnControlClient(), device_->GetHandle(),
            swap_texture_descriptor_.format);
      }
      break;
    }
    case V8GPUCanvasAlphaMode::Enum::kPremultiplied:
      alpha_clearer_ = nullptr;
      break;
  }

  // If we don't notify the host that something has changed it may never check
  // for the new cc::Layer.
  Host()->SetNeedsCompositingUpdate();
}

void GPUCanvasContext::unconfigure() {
  if (stopped_) {
    return;
  }

  ReplaceDrawingBuffer(/*destroy_swap_buffers*/ true);

  // When developers call unconfigure from the page, one of the reasons for
  // doing so is to expressly release the GPUCanvasContext's device reference.
  // In order to fully release it, any TextureAlphaClearer that has been created
  // also needs to be released.
  alpha_clearer_ = nullptr;
  device_ = nullptr;
  configured_ = false;
}

GPUCanvasConfiguration* GPUCanvasContext::getConfiguration() {
  if (!configured_) {
    return nullptr;
  }

  GPUCanvasConfiguration* configuration = GPUCanvasConfiguration::Create();
  configuration->setDevice(device_);
  configuration->setFormat(FromDawnEnum(texture_descriptor_.format));
  configuration->setUsage(static_cast<uint32_t>(texture_descriptor_.usage));

  Vector<V8GPUTextureFormat> view_formats;
  for (size_t i = 0; i < texture_descriptor_.viewFormatCount; ++i) {
    view_formats.push_back(FromDawnEnum(view_formats_[i]));
  }
  configuration->setViewFormats(view_formats);

  configuration->setColorSpace(PredefinedColorSpaceToV8(color_space_));
  configuration->setAlphaMode(alpha_mode_);

  GPUCanvasToneMapping* tone_mapping = GPUCanvasToneMapping::Create();
  tone_mapping->setMode(tone_mapping_mode_);
  configuration->setToneMapping(tone_mapping);

  return configuration;
}

GPUTexture* GPUCanvasContext::getCurrentTexture(
    ScriptState* script_state,
    ExceptionState& exception_state) {
  if (!configured_) {
    exception_state.ThrowDOMException(DOMExceptionCode::kInvalidStateError,
                                      "context is not configured");
    return nullptr;
  }
  DCHECK(device_);

  // Validate the texture descriptor as required by the spec for
  // GPUCanvasContext.getCurrentTexture(). This is required on each call,
  // so it must appear before the cached texture early-out below.
  device_->GetHandle().ValidateTextureDescriptor(&texture_descriptor_);

  // Calling getCurrentTexture returns a texture that is valid until the
  // animation frame it gets presented. If getCurrentTexture is called multiple
  // time, the same texture should be returned. |texture_| is set to
  // null when presented so that we know we should create a new one.
  if (texture_ && !new_texture_required_) {
    return texture_.Get();
  }
  new_texture_required_ = false;

  if (!swap_buffers_) {
    device_->InjectError(wgpu::ErrorType::Validation,
                         "context configuration is invalid.");
    return GPUTexture::CreateError(device_.Get(), &texture_descriptor_);
  }

  ReplaceDrawingBuffer(/* destroy_swap_buffers */ false);

  // Simply requesting a new canvas texture with WebGPU is enough to mark it as
  // "dirty", so always call DidDraw() when a new texture is created.
  DidDraw(CanvasPerformanceMonitor::DrawType::kOther);

  SkAlphaType alpha_type = GetAlphaType();
  scoped_refptr<WebGPUMailboxTexture> mailbox_texture =
      swap_buffers_->GetNewTexture(swap_texture_descriptor_, alpha_type);
  if (!mailbox_texture) {
    // Try to give a helpful message for the most common cause for mailbox
    // texture creation failure.
    if (texture_descriptor_.size.width == 0 ||
        texture_descriptor_.size.height == 0) {
      device_->InjectError(wgpu::ErrorType::Validation,
                           "Could not create a swapchain texture of size 0.");
    } else {
      device_->InjectError(wgpu::ErrorType::Validation,
                           "Could not create the swapchain texture.");
    }
    texture_ = swap_texture_ =
        GPUTexture::CreateError(device_, &texture_descriptor_);
    return texture_.Get();
  }

  mailbox_texture->SetNeedsPresent(true);
  mailbox_texture->SetAlphaClearer(alpha_clearer_);

  swap_texture_ = MakeGarbageCollected<GPUTexture>(
      device_, swap_texture_descriptor_.format,
      static_cast<wgpu::TextureUsage>(swap_texture_descriptor_.usage),
      std::move(mailbox_texture),
      String::FromUTF8(swap_texture_descriptor_.label));

  if (copy_to_swap_texture_required_) {
    texture_ = MakeGarbageCollected<GPUTexture>(
        device_, device_->GetHandle().CreateTexture(&texture_descriptor_),
        String::FromUTF8(texture_descriptor_.label));
    // If the user manually destroys the texture before yielding control back
    // to the browser, do the copy just prior to the texture destruction.
    texture_->SetBeforeDestroyCallback(WTF::BindOnce(
        [](GPUCanvasContext* context, GPUTexture* texture) {
          context->CopyToSwapTexture();
          texture->ClearBeforeDestroyCallback();
        },
        WrapPersistent(this), WrapPersistent(texture_.Get())));
  } else {
    texture_ = swap_texture_;
  }

  ExecutionContext* execution_context = ExecutionContext::From(script_state);
  UseCounter::Count(execution_context,
                    WebFeature::kWebGPUCanvasContextGetCurrentTexture);

  return texture_.Get();
}

scoped_refptr<WebGPUMailboxTexture>
GPUCanvasContext::GetFrontBufferMailboxTexture() {
  auto front_buffer_si = swap_buffers_->GetFrontBufferSharedImage();
  if (!front_buffer_si) {
    return nullptr;
  }
  wgpu::TextureUsage front_buffer_usage =
      wgpu::TextureUsage::CopySrc | wgpu::TextureUsage::TextureBinding;
  wgpu::DawnTextureInternalUsageDescriptor front_buffer_usage_desc = {{
      .internalUsage = front_buffer_usage,
  }};
  wgpu::TextureDescriptor desc = {
      .size = {base::checked_cast<uint32_t>(front_buffer_si->size().width()),
               base::checked_cast<uint32_t>(front_buffer_si->size().height())},
      .format = swap_buffers_->TextureFormat(),
  };
  desc.nextInChain = &front_buffer_usage_desc;

  return WebGPUMailboxTexture::FromExistingSharedImage(
      device_->GetDawnControlClient(), device_->GetHandle(), desc,
      front_buffer_si, swap_buffers_->GetFrontBufferSyncToken());
}

void GPUCanvasContext::ReplaceDrawingBuffer(bool destroy_swap_buffers) {
  if (swap_texture_) {
    DCHECK(swap_buffers_);
    swap_buffers_->DiscardCurrentSwapBuffer();
    swap_texture_ = nullptr;
  }

  if (copy_to_swap_texture_required_ && texture_) {
    texture_->ClearBeforeDestroyCallback();
    texture_->destroy();
  }
  texture_ = nullptr;

  if (swap_buffers_ && destroy_swap_buffers) {
    // Tell any previous swapbuffers that it will no longer be used and can
    // destroy all its resources (and produce errors when used).
    swap_buffers_->Neuter();
    swap_buffers_ = nullptr;
  }
}

void GPUCanvasContext::FinalizeFrame(FlushReason) {
  // In some cases, such as when a canvas is hidden or offscreen, compositing
  // will never happen and thus OnTextureTransferred will never be called. In
  // those cases, getCurrentTexture is still required to return a new texture
  // after the current frame has ended, so we'll mark that a new texture is
  // required here.
  new_texture_required_ = true;
}

// WebGPUSwapBufferProvider::Client implementation
void GPUCanvasContext::OnTextureTransferred() {
  DCHECK(texture_);
  DCHECK(swap_texture_);

  if (copy_to_swap_texture_required_ && texture_ && !texture_->IsDestroyed()) {
    CopyToSwapTexture();
    texture_->ClearBeforeDestroyCallback();
    texture_->destroy();
  }
  texture_ = nullptr;
  swap_texture_ = nullptr;
}

void GPUCanvasContext::InitializeLayer(cc::Layer* layer) {
  if (Host()) {
    Host()->InitializeLayerWithCSSProperties(layer);
  }
}

void GPUCanvasContext::SetNeedsCompositingUpdate() {
  if (Host()) {
    Host()->SetNeedsCompositingUpdate();
  }
}

bool GPUCanvasContext::IsGPUDeviceDestroyed() {
  return device_->IsDestroyed();
}

void GPUCanvasContext::CopyToSwapTexture() {
  DCHECK(copy_to_swap_texture_required_);
  DCHECK(texture_);
  DCHECK(swap_texture_);

  if (!suppress_preferred_format_warning_) {
    // Will only display once per device
    device_->AddSingletonWarning(GPUSingletonWarning::kNonPreferredFormat);
  }

  wgpu::TexelCopyTextureInfo source = {
      .texture = texture_->GetHandle(),
      .aspect = wgpu::TextureAspect::All,
  };
  wgpu::TexelCopyTextureInfo destination = {
      .texture = swap_texture_->GetHandle(),
      .aspect = wgpu::TextureAspect::All,
  };

  gfx::Size size = swap_buffers_->Size();
  wgpu::Extent3D copy_size = {
      .width = static_cast<uint32_t>(size.width()),
      .height = static_cast<uint32_t>(size.height()),
      .depthOrArrayLayers = 1,
  };

  wgpu::AlphaMode copy_alpha_mode =
      alpha_mode_ == V8GPUCanvasAlphaMode::Enum::kOpaque
          ? wgpu::AlphaMode::Opaque
          : wgpu::AlphaMode::Premultiplied;

  wgpu::CopyTextureForBrowserOptions options = {
      .srcAlphaMode = copy_alpha_mode,
      .dstAlphaMode = copy_alpha_mode,
      .internalUsage = true,
  };

  device_->queue()->GetHandle().CopyTextureForBrowser(&source, &destination,
                                                      &copy_size, &options);
}

bool GPUCanvasContext::CopyTextureToResourceProvider(
    const wgpu::Texture& texture,
    CanvasResourceProvider* resource_provider) const {
  DCHECK(resource_provider);

  gfx::Size size(texture.GetWidth(), texture.GetHeight());
  DCHECK_EQ(resource_provider->Size(), size);

  // This method will copy the contents of `texture` to `resource_provider`'s
  // backing SharedImage via the WebGPU interface. Hence, WEBGPU_WRITE usage
  // must be included on that backing SharedImage.
  DCHECK(resource_provider->GetSharedImageUsageFlags().Has(
      gpu::SHARED_IMAGE_USAGE_WEBGPU_WRITE));

  base::WeakPtr<WebGraphicsContext3DProviderWrapper> shared_context_wrapper =
      SharedGpuContext::ContextProviderWrapper();
  if (!shared_context_wrapper) {
    return false;
  }

  gpu::SyncToken sync_token;
  auto dst_client_si =
      resource_provider->GetBackingClientSharedImageForExternalWrite(
          &sync_token, gpu::SharedImageUsageSet());
  if (!dst_client_si) {
    return false;
  }

  if (!GetContextProviderWeakPtr()) {
    return false;
  }
  // todo(crbug/1267244) Use WebGPUMailboxTexture here instead of doing things
  // manually.
  gpu::webgpu::WebGPUInterface* webgpu =
      GetContextProviderWeakPtr()->ContextProvider().WebGPUInterface();
  gpu::webgpu::ReservedTexture reservation =
      webgpu->ReserveTexture(device_->GetHandle().Get());
  DCHECK(reservation.texture);
  wgpu::Texture reserved_texture = wgpu::Texture::Acquire(reservation.texture);

  webgpu->WaitSyncTokenCHROMIUM(sync_token.GetConstData());
  wgpu::TextureUsage usage =
      wgpu::TextureUsage::CopyDst | wgpu::TextureUsage::RenderAttachment;
  webgpu->AssociateMailbox(reservation.deviceId, reservation.deviceGeneration,
                           reservation.id, reservation.generation,
                           static_cast<uint64_t>(usage),
                           dst_client_si->mailbox());
  wgpu::TexelCopyTextureInfo source = {
      .texture = texture,
      .aspect = wgpu::TextureAspect::All,
  };
  wgpu::TexelCopyTextureInfo destination = {
      .texture = reserved_texture,
      .aspect = wgpu::TextureAspect::All,
  };
  wgpu::Extent3D copy_size = {
      .width = static_cast<uint32_t>(size.width()),
      .height = static_cast<uint32_t>(size.height()),
      .depthOrArrayLayers = 1,
  };

  bool isOpaque = alpha_mode_ == V8GPUCanvasAlphaMode::Enum::kOpaque;

  // If either the texture is opaque or the texture format does not match the
  // resource provider's format then CopyTextureForBrowser will be used, which
  // performs a blit and can fix up the texture data during the copy.
  if (isOpaque || copy_to_swap_texture_required_) {
    wgpu::AlphaMode srcAlphaMode =
        isOpaque ? wgpu::AlphaMode::Opaque : wgpu::AlphaMode::Premultiplied;

    // Issue a copyTextureForBrowser call with internal usage turned on.
    // There is a special step for srcAlphaMode == wgpu::AlphaMode::Opaque that
    // clears alpha channel to one.
    wgpu::AlphaMode dstAlphaMode;
    switch (resource_provider->GetAlphaType()) {
      case SkAlphaType::kPremul_SkAlphaType:
        dstAlphaMode = wgpu::AlphaMode::Premultiplied;
        break;
      case SkAlphaType::kUnpremul_SkAlphaType:
        dstAlphaMode = wgpu::AlphaMode::Unpremultiplied;
        break;
      case SkAlphaType::kOpaque_SkAlphaType:
        dstAlphaMode = wgpu::AlphaMode::Opaque;
        break;
      default:
        // Unknown dst alpha type, default to equal to src alpha mode
        dstAlphaMode = srcAlphaMode;
        break;
    }
    wgpu::CopyTextureForBrowserOptions options = {
        .flipY = dst_client_si->surface_origin() == kBottomLeft_GrSurfaceOrigin,
        .srcAlphaMode = srcAlphaMode,
        .dstAlphaMode = dstAlphaMode,
        .internalUsage = true,
    };

    device_->queue()->GetHandle().CopyTextureForBrowser(&source, &destination,
                                                        &copy_size, &options);

  } else {
    // Create a command encoder and call copyTextureToTexture for the image
    // copy.
    wgpu::DawnEncoderInternalUsageDescriptor internal_usage_desc = {{
        .useInternalUsages = true,
    }};

    wgpu::CommandEncoderDescriptor command_encoder_desc = {
        .nextInChain = &internal_usage_desc,
    };
    wgpu::CommandEncoder command_encoder =
        device_->GetHandle().CreateCommandEncoder(&command_encoder_desc);
    command_encoder.CopyTextureToTexture(&source, &destination, &copy_size);

    wgpu::CommandBuffer command_buffer = command_encoder.Finish();
    command_encoder = nullptr;

    device_->queue()->GetHandle().Submit(1u, &command_buffer);
    command_buffer = nullptr;
  }

  webgpu->DissociateMailbox(reservation.id, reservation.generation);
  webgpu->GenUnverifiedSyncTokenCHROMIUM(sync_token.GetData());
  resource_provider->EndExternalWrite(sync_token);

  return true;
}

scoped_refptr<StaticBitmapImage> GPUCanvasContext::SnapshotInternal(
    const wgpu::Texture& texture) const {
  gfx::Size size(texture.GetWidth(), texture.GetHeight());

  // We tag the SharedImage inside the WebGPUImageProvider with display usages
  // since there are uncommon paths which may use this snapshot for compositing.
  // These paths are usually related to either printing or either video and
  // usually related to OffscreenCanvas; in cases where the image created from
  // this Snapshot will be sent eventually to the Display Compositor.
  auto resource_provider = CanvasResourceProvider::CreateWebGPUImageProvider(
      size, GetSharedImageFormat(), GetAlphaType(), GetColorSpace(),
      swap_buffers_->GetSharedImageUsagesForDisplay());
  if (!resource_provider)
    return nullptr;

  if (!CopyTextureToResourceProvider(texture, resource_provider.get())) {
    return nullptr;
  }

  return resource_provider->Snapshot(FlushReason::kNone);
}

base::WeakPtr<WebGraphicsContext3DProviderWrapper>
GPUCanvasContext::GetContextProviderWeakPtr() const {
  return device_->GetDawnControlClient()->GetContextProviderWeakPtr();
}

scoped_refptr<StaticBitmapImage>
GPUCanvasContext::MakeFallbackStaticBitmapImage(
    V8GPUCanvasAlphaMode::Enum alpha_mode) {
  // It is not possible to create an empty image bitmap, return null in that
  // case which will fail ImageBitmap creation with an exception instead.
  gfx::Size size = Host()->Size();
  if (size.IsEmpty()) {
    return nullptr;
  }

  // We intentionally leave the image in legacy color space.
  SkBitmap black_bitmap;
  if (!black_bitmap.tryAllocN32Pixels(size.width(), size.height())) {
    // It is not possible to create such a big image bitmap, return null in
    // that case which will fail ImageBitmap creation with an exception
    // instead.
    return nullptr;
  }

  if (alpha_mode == V8GPUCanvasAlphaMode::Enum::kOpaque) {
    black_bitmap.eraseARGB(255, 0, 0, 0);
  } else {
    black_bitmap.eraseARGB(0, 0, 0, 0);
  }

  // Mark the bitmap as immutable to avoid an unnecessary copy in the
  // following RasterFromBitmap() call.
  black_bitmap.setImmutable();
  return UnacceleratedStaticBitmapImage::Create(
      SkImages::RasterFromBitmap(black_bitmap));
}

}  // namespace blink