File: Queue.cpp

package info (click to toggle)
firefox 144.0-1
  • links: PTS, VCS
  • area: main
  • in suites:
  • size: 4,637,504 kB
  • sloc: cpp: 7,576,692; javascript: 6,430,831; ansic: 3,748,119; python: 1,398,978; xml: 628,810; asm: 438,679; java: 186,194; sh: 63,212; makefile: 19,159; objc: 13,086; perl: 12,986; yacc: 4,583; cs: 3,846; pascal: 3,448; lex: 1,720; ruby: 1,003; exp: 762; php: 436; lisp: 258; awk: 247; sql: 66; sed: 53; csh: 10
file content (608 lines) | stat: -rw-r--r-- 22,962 bytes parent folder | download | duplicates (3)
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
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */

#include "Queue.h"

#include <algorithm>

#include "CommandBuffer.h"
#include "CommandEncoder.h"
#include "ExternalTexture.h"
#include "Utility.h"
#include "ipc/WebGPUChild.h"
#include "mozilla/Casting.h"
#include "mozilla/ErrorResult.h"
#include "mozilla/dom/BufferSourceBinding.h"
#include "mozilla/dom/HTMLCanvasElement.h"
#include "mozilla/dom/HTMLImageElement.h"
#include "mozilla/dom/ImageBitmap.h"
#include "mozilla/dom/OffscreenCanvas.h"
#include "mozilla/dom/Promise.h"
#include "mozilla/dom/PromiseNativeHandler.h"
#include "mozilla/dom/UnionTypes.h"
#include "mozilla/dom/WebGLTexelConversions.h"
#include "mozilla/dom/WebGLTypes.h"
#include "mozilla/dom/WebGPUBinding.h"
#include "mozilla/ipc/SharedMemoryHandle.h"
#include "mozilla/ipc/SharedMemoryMapping.h"
#include "nsLayoutUtils.h"

namespace mozilla::webgpu {

GPU_IMPL_CYCLE_COLLECTION(Queue, mParent)
GPU_IMPL_JS_WRAP(Queue)

Queue::Queue(Device* const aParent, RawId aId)
    : ObjectBase(aParent->GetChild(), aId, ffi::wgpu_client_drop_queue),
      ChildOf(aParent) {}

Queue::~Queue() = default;

struct ExternalTextureWorkDoneHandler : dom::PromiseNativeHandler {
  NS_DECL_ISUPPORTS

  explicit ExternalTextureWorkDoneHandler(
      nsTArray<RefPtr<ExternalTexture>>&& aExternalTextures,
      uint64_t aSubmissionId)
      : mExternalTextures(std::move(aExternalTextures)),
        mSubmissionId(aSubmissionId) {}

  void ResolvedCallback(JSContext* aCx, JS::Handle<JS::Value> aValue,
                        ErrorResult& aRv) override {
    for (const auto& externalTexture : mExternalTextures) {
      externalTexture->OnSubmittedWorkDone(mSubmissionId);
    }
  }
  void RejectedCallback(JSContext* aCx, JS::Handle<JS::Value> aValue,
                        ErrorResult& aRv) override {
    MOZ_ASSERT_UNREACHABLE("Work done promise should not be rejected");
  }

 private:
  ~ExternalTextureWorkDoneHandler() = default;
  // We must hold a strong reference to the external textures to ensure that
  // they are not released before all work involving them is done.
  const nsTArray<RefPtr<ExternalTexture>> mExternalTextures;
  const uint64_t mSubmissionId;
};

NS_IMPL_ISUPPORTS0(ExternalTextureWorkDoneHandler)

void Queue::Submit(
    const dom::Sequence<OwningNonNull<CommandBuffer>>& aCommandBuffers) {
  nsTArray<RawId> list(aCommandBuffers.Length());
  nsTArray<RefPtr<ExternalTexture>> externalTextures;
  for (uint32_t i = 0; i < aCommandBuffers.Length(); ++i) {
    auto idMaybe = aCommandBuffers[i]->Commit();

    // Generate a validation error if any external texture used by any command
    // buffer is expired. Technically this is a Device timeline step, but since
    // the external texture's expired state is only set on the content timeline
    // it is functionally equivalent to check here and raise any error on the
    // device timeline. A compromised content process could skip this step, but
    // equally it could skip setting the external texture's expired state even
    // if this check were performed on the server side.
    // https://www.w3.org/TR/webgpu/#dom-gpuqueue-submit
    for (const auto& externalTexture :
         aCommandBuffers[i]->GetExternalTextures()) {
      if (externalTexture->IsExpired()) {
        ffi::wgpu_report_validation_error(GetClient(), mParent->GetId(),
                                          "External texture is expired");
        return;
      }
    }
    externalTextures.AppendElements(aCommandBuffers[i]->GetExternalTextures());

    if (idMaybe) {
      list.AppendElement(idMaybe);
    }
  }

  nsTArray<RawId> externalTextureSourceIds;
  for (auto& externalTexture : externalTextures) {
    externalTextureSourceIds.AppendElement(externalTexture->Source()->GetId());
  }

  GetChild()->QueueSubmit(GetId(), mParent->GetId(), list,
                          externalTextureSourceIds);

  if (!externalTextures.IsEmpty()) {
    for (const auto& externalTexture : externalTextures) {
      externalTexture->OnSubmit(mNextExternalTextureSubmissionIndex);
    }
    ErrorResult rv;
    RefPtr<dom::Promise> promise = OnSubmittedWorkDone(rv);
    // Without this promise we have no way of knowing when work involving the
    // external textures is done. This would lead to us holding on to the
    // external texture's resources indefinitely, which we don't want.
    // The alternative of releasing the resources immediately is unacceptable
    // while there is still pending work, so just crash.
    MOZ_RELEASE_ASSERT(promise);
    RefPtr<ExternalTextureWorkDoneHandler> handler =
        new ExternalTextureWorkDoneHandler(std::move(externalTextures),
                                           mNextExternalTextureSubmissionIndex);
    promise->AppendNativeHandler(handler);

    mNextExternalTextureSubmissionIndex++;
  }
}

already_AddRefed<dom::Promise> Queue::OnSubmittedWorkDone(ErrorResult& aRv) {
  RefPtr<dom::Promise> promise = dom::Promise::Create(GetParentObject(), aRv);
  if (NS_WARN_IF(aRv.Failed())) {
    return nullptr;
  }

  ffi::wgpu_client_on_submitted_work_done(GetClient(), GetId());

  GetChild()->mPendingOnSubmittedWorkDonePromises[GetId()].push_back(promise);

  return promise.forget();
}

void Queue::WriteBuffer(
    const Buffer& aBuffer, uint64_t aBufferOffset,
    const dom::MaybeSharedArrayBufferOrMaybeSharedArrayBufferView& aData,
    uint64_t aDataOffset, const dom::Optional<uint64_t>& aSize,
    ErrorResult& aRv) {
  if (!aBuffer.GetId()) {
    // Invalid buffers are unknown to the parent -- don't try to write
    // to them.
    return;
  }

  size_t elementByteSize = 1;
  if (aData.IsArrayBufferView()) {
    auto type = aData.GetAsArrayBufferView().Type();
    if (type != JS::Scalar::MaxTypedArrayViewType) {
      elementByteSize = byteSize(type);
    }
  }
  dom::ProcessTypedArraysFixed(aData, [&, elementByteSize](
                                          const Span<const uint8_t>& aData) {
    uint64_t byteLength = aData.Length();

    auto checkedByteOffset =
        CheckedInt<uint64_t>(aDataOffset) * elementByteSize;
    if (!checkedByteOffset.isValid()) {
      aRv.ThrowOperationError("offset x element size overflows");
      return;
    }
    auto offset = checkedByteOffset.value();

    size_t size;
    if (aSize.WasPassed()) {
      const auto checkedByteSize =
          CheckedInt<size_t>(aSize.Value()) * elementByteSize;
      if (!checkedByteSize.isValid()) {
        aRv.ThrowOperationError("write size x element size overflows");
        return;
      }
      size = checkedByteSize.value();
    } else {
      const auto checkedByteSize = CheckedInt<size_t>(byteLength) - offset;
      if (!checkedByteSize.isValid()) {
        aRv.ThrowOperationError("data byte length - offset underflows");
        return;
      }
      size = checkedByteSize.value();
    }

    auto checkedByteEnd = CheckedInt<uint64_t>(offset) + size;
    if (!checkedByteEnd.isValid() || checkedByteEnd.value() > byteLength) {
      aRv.ThrowOperationError(
          nsPrintfCString("Wrong data size %" PRIuPTR, size));
      return;
    }

    if (size % 4 != 0) {
      aRv.ThrowOperationError("Byte size must be a multiple of 4");
      return;
    }

    if (size < 1024) {
      ipc::ByteBuf bb{};
      bb.Allocate(size);
      memcpy(bb.mData, aData.Elements() + offset, size);
      auto data_buffer_index = GetChild()->QueueDataBuffer(std::move(bb));
      ffi::wgpu_queue_write_buffer_inline(GetClient(), mParent->GetId(),
                                          GetId(), aBuffer.GetId(),
                                          aBufferOffset, data_buffer_index);
      return;
    }

    mozilla::ipc::MutableSharedMemoryHandle handle;
    if (size != 0) {
      handle = mozilla::ipc::shared_memory::Create(size);
      auto mapping = handle.Map();
      if (!handle || !mapping) {
        aRv.Throw(NS_ERROR_OUT_OF_MEMORY);
        return;
      }

      memcpy(mapping.DataAs<uint8_t>(), aData.Elements() + offset, size);
    }
    auto shmem_handle_index = GetChild()->QueueShmemHandle(std::move(handle));
    ffi::wgpu_queue_write_buffer_via_shmem(GetClient(), mParent->GetId(),
                                           GetId(), aBuffer.GetId(),
                                           aBufferOffset, shmem_handle_index);
  });
}

static CheckedInt<size_t> ComputeApproxSize(
    const dom::GPUTexelCopyTextureInfo& aDestination,
    const dom::GPUTexelCopyBufferLayout& aDataLayout,
    const ffi::WGPUExtent3d& extent,
    const ffi::WGPUTextureFormatBlockInfo& info) {
  // The spec's algorithm for [validating linear texture data][vltd] computes
  // an exact size for the transfer. wgpu implements the algorithm and will
  // fully validate the operation as described in the spec.
  //
  // Here, we just want to avoid copying excessive amounts of data in the case
  // where the transfer will use only a small portion of the buffer. So we
  // compute an approximation that will be at least the actual transfer size
  // for any valid request. Then we copy the smaller of the approximated size
  // or the remainder of the buffer.
  //
  // [vltd]:
  // https://www.w3.org/TR/webgpu/#abstract-opdef-validating-linear-texture-data

  // VLTD requires that width/height are multiples of the block size.
  auto widthInBlocks = extent.width / info.width;
  auto heightInBlocks = extent.height / info.height;
  auto bytesInLastRow = CheckedInt<size_t>(widthInBlocks) * info.copy_size;

  // VLTD requires bytesPerRow present if heightInBlocks > 1.
  auto bytesPerRow = CheckedInt<size_t>(aDataLayout.mBytesPerRow.WasPassed()
                                            ? aDataLayout.mBytesPerRow.Value()
                                            : bytesInLastRow);

  if (extent.depth_or_array_layers > 1) {
    // VLTD requires rowsPerImage present if layers > 1
    auto rowsPerImage = aDataLayout.mRowsPerImage.WasPassed()
                            ? aDataLayout.mRowsPerImage.Value()
                            : heightInBlocks;
    return bytesPerRow * rowsPerImage * extent.depth_or_array_layers;
  } else {
    return bytesPerRow * heightInBlocks;
  }
}

void Queue::WriteTexture(
    const dom::GPUTexelCopyTextureInfo& aDestination,
    const dom::MaybeSharedArrayBufferOrMaybeSharedArrayBufferView& aData,
    const dom::GPUTexelCopyBufferLayout& aDataLayout,
    const dom::GPUExtent3D& aSize, ErrorResult& aRv) {
  ffi::WGPUTexelCopyTextureInfo copyView = {};
  CommandEncoder::ConvertTextureCopyViewToFFI(aDestination, &copyView);
  ffi::WGPUTexelCopyBufferLayout dataLayout = {};
  CommandEncoder::ConvertTextureDataLayoutToFFI(aDataLayout, &dataLayout);
  dataLayout.offset = 0;  // our Shmem has the contents starting from 0.
  ffi::WGPUExtent3d extent = {};
  ConvertExtent3DToFFI(aSize, &extent);

  auto format = ConvertTextureFormat(aDestination.mTexture->Format());
  auto aspect = ConvertTextureAspect(aDestination.mAspect);
  ffi::WGPUTextureFormatBlockInfo info = {};
  bool valid = ffi::wgpu_texture_format_get_block_info(format, aspect, &info);
  CheckedInt<size_t> approxSize;
  if (valid) {
    approxSize = ComputeApproxSize(aDestination, aDataLayout, extent, info);
  } else {
    // This happens when the caller does not indicate a single aspect to
    // target in a multi-aspect texture. It needs to be validated on the
    // device timeline, so proceed without an estimated size for now
    approxSize = CheckedInt<size_t>(SIZE_MAX) + 1;
  }

  dom::ProcessTypedArraysFixed(aData, [&](const Span<const uint8_t>& aData) {
    const auto checkedSize =
        CheckedInt<size_t>(aData.Length()) - aDataLayout.mOffset;
    size_t size;
    if (checkedSize.isValid() && approxSize.isValid()) {
      size = std::min(checkedSize.value(), approxSize.value());
    } else if (checkedSize.isValid()) {
      size = checkedSize.value();
    } else {
      // CheckedSize is invalid when the caller-provided offset was past the
      // end of their buffer. Maintain that condition, and fail the operation
      // on the device timeline.
      dataLayout.offset = 1;
      size = 0;
    }

    mozilla::ipc::MutableSharedMemoryHandle handle;
    if (size != 0) {
      handle = mozilla::ipc::shared_memory::Create(size);
      auto mapping = handle.Map();
      if (!handle || !mapping) {
        aRv.Throw(NS_ERROR_OUT_OF_MEMORY);
        return;
      }

      memcpy(mapping.DataAs<uint8_t>(), aData.Elements() + aDataLayout.mOffset,
             size);
    } else {
      handle = mozilla::ipc::MutableSharedMemoryHandle();
    }

    auto shmem_handle_index = GetChild()->QueueShmemHandle(std::move(handle));
    ffi::wgpu_queue_write_texture_via_shmem(GetClient(), mParent->GetId(),
                                            GetId(), copyView, dataLayout,
                                            extent, shmem_handle_index);
  });
}

static WebGLTexelFormat ToWebGLTexelFormat(gfx::SurfaceFormat aFormat) {
  switch (aFormat) {
    case gfx::SurfaceFormat::B8G8R8A8:
    case gfx::SurfaceFormat::B8G8R8X8:
      return WebGLTexelFormat::BGRA8;
    case gfx::SurfaceFormat::R8G8B8A8:
    case gfx::SurfaceFormat::R8G8B8X8:
      return WebGLTexelFormat::RGBA8;
    default:
      return WebGLTexelFormat::FormatNotSupportingAnyConversion;
  }
}

static WebGLTexelFormat ToWebGLTexelFormat(dom::GPUTextureFormat aFormat) {
  // TODO: We need support for Rbg10a2unorm as well.
  switch (aFormat) {
    case dom::GPUTextureFormat::R8unorm:
      return WebGLTexelFormat::R8;
    case dom::GPUTextureFormat::R16float:
      return WebGLTexelFormat::R16F;
    case dom::GPUTextureFormat::R32float:
      return WebGLTexelFormat::R32F;
    case dom::GPUTextureFormat::Rg8unorm:
      return WebGLTexelFormat::RG8;
    case dom::GPUTextureFormat::Rg16float:
      return WebGLTexelFormat::RG16F;
    case dom::GPUTextureFormat::Rg32float:
      return WebGLTexelFormat::RG32F;
    case dom::GPUTextureFormat::Rgba8unorm:
    case dom::GPUTextureFormat::Rgba8unorm_srgb:
      return WebGLTexelFormat::RGBA8;
    case dom::GPUTextureFormat::Bgra8unorm:
    case dom::GPUTextureFormat::Bgra8unorm_srgb:
      return WebGLTexelFormat::BGRA8;
    case dom::GPUTextureFormat::Rgba16float:
      return WebGLTexelFormat::RGBA16F;
    case dom::GPUTextureFormat::Rgba32float:
      return WebGLTexelFormat::RGBA32F;
    default:
      return WebGLTexelFormat::FormatNotSupportingAnyConversion;
  }
}

void Queue::CopyExternalImageToTexture(
    const dom::GPUCopyExternalImageSourceInfo& aSource,
    const dom::GPUCopyExternalImageDestInfo& aDestination,
    const dom::GPUExtent3D& aCopySize, ErrorResult& aRv) {
  if (aSource.mOrigin.IsRangeEnforcedUnsignedLongSequence()) {
    auto seq = aSource.mOrigin.GetAsRangeEnforcedUnsignedLongSequence();
    if (seq.Length() > 2) {
      aRv.ThrowTypeError("`origin` must have a sequence size of 2 or less");
      return;
    }
  }

  const auto dstFormat = ToWebGLTexelFormat(aDestination.mTexture->Format());
  if (dstFormat == WebGLTexelFormat::FormatNotSupportingAnyConversion) {
    aRv.ThrowInvalidStateError("Unsupported destination format");
    return;
  }

  const uint32_t surfaceFlags = nsLayoutUtils::SFE_ALLOW_NON_PREMULT;
  SurfaceFromElementResult sfeResult;
  switch (aSource.mSource.GetType()) {
    case decltype(aSource.mSource)::Type::eImageBitmap: {
      const auto& bitmap = aSource.mSource.GetAsImageBitmap();
      if (bitmap->IsClosed()) {
        aRv.ThrowInvalidStateError("Detached ImageBitmap");
        return;
      }

      sfeResult = nsLayoutUtils::SurfaceFromImageBitmap(bitmap, surfaceFlags);
      break;
    }
    case decltype(aSource.mSource)::Type::eHTMLImageElement: {
      const auto& image = aSource.mSource.GetAsHTMLImageElement();
      if (image->NaturalWidth() == 0 || image->NaturalHeight() == 0) {
        aRv.ThrowInvalidStateError("Zero-sized HTMLImageElement");
        return;
      }

      sfeResult = nsLayoutUtils::SurfaceFromElement(image, surfaceFlags);
      break;
    }
    case decltype(aSource.mSource)::Type::eHTMLCanvasElement: {
      MOZ_ASSERT(NS_IsMainThread());

      const auto& canvas = aSource.mSource.GetAsHTMLCanvasElement();
      if (canvas->Width() == 0 || canvas->Height() == 0) {
        aRv.ThrowInvalidStateError("Zero-sized HTMLCanvasElement");
        return;
      }

      sfeResult = nsLayoutUtils::SurfaceFromElement(canvas, surfaceFlags);
      break;
    }
    case decltype(aSource.mSource)::Type::eOffscreenCanvas: {
      const auto& canvas = aSource.mSource.GetAsOffscreenCanvas();
      if (canvas->Width() == 0 || canvas->Height() == 0) {
        aRv.ThrowInvalidStateError("Zero-sized OffscreenCanvas");
        return;
      }

      sfeResult =
          nsLayoutUtils::SurfaceFromOffscreenCanvas(canvas, surfaceFlags);
      break;
    }
  }

  if (!sfeResult.mCORSUsed) {
    nsIGlobalObject* global = mParent->GetOwnerGlobal();
    nsIPrincipal* dstPrincipal = global ? global->PrincipalOrNull() : nullptr;
    if (!sfeResult.mPrincipal || !dstPrincipal ||
        !dstPrincipal->Subsumes(sfeResult.mPrincipal)) {
      aRv.ThrowSecurityError("Cross-origin elements require CORS!");
      return;
    }
  }

  if (sfeResult.mIsWriteOnly) {
    aRv.ThrowSecurityError("Write only source data not supported!");
    return;
  }

  RefPtr<gfx::SourceSurface> surface = sfeResult.GetSourceSurface();
  if (!surface) {
    aRv.ThrowInvalidStateError("No surface available from source");
    return;
  }

  RefPtr<gfx::DataSourceSurface> dataSurface = surface->GetDataSurface();
  if (!dataSurface) {
    aRv.Throw(NS_ERROR_OUT_OF_MEMORY);
    return;
  }

  bool srcPremultiplied;
  switch (sfeResult.mAlphaType) {
    case gfxAlphaType::Premult:
      srcPremultiplied = true;
      break;
    case gfxAlphaType::NonPremult:
      srcPremultiplied = false;
      break;
    case gfxAlphaType::Opaque:
      // No (un)premultiplication necessary so match the output.
      srcPremultiplied = aDestination.mPremultipliedAlpha;
      break;
  }

  const auto surfaceFormat = dataSurface->GetFormat();
  const auto srcFormat = ToWebGLTexelFormat(surfaceFormat);
  if (srcFormat == WebGLTexelFormat::FormatNotSupportingAnyConversion) {
    gfxCriticalError() << "Unsupported surface format from source "
                       << surfaceFormat;
    MOZ_CRASH();
  }

  gfx::DataSourceSurface::ScopedMap map(dataSurface,
                                        gfx::DataSourceSurface::READ);
  if (!map.IsMapped()) {
    aRv.ThrowInvalidStateError("Cannot map surface from source");
    return;
  }

  ffi::WGPUExtent3d extent = {};
  ConvertExtent3DToFFI(aCopySize, &extent);
  if (extent.depth_or_array_layers > 1) {
    aRv.ThrowOperationError("Depth is greater than 1");
    return;
  }

  uint32_t srcOriginX;
  uint32_t srcOriginY;
  if (aSource.mOrigin.IsRangeEnforcedUnsignedLongSequence()) {
    const auto& seq = aSource.mOrigin.GetAsRangeEnforcedUnsignedLongSequence();
    srcOriginX = seq.Length() > 0 ? seq[0] : 0;
    srcOriginY = seq.Length() > 1 ? seq[1] : 0;
  } else if (aSource.mOrigin.IsGPUOrigin2DDict()) {
    const auto& dict = aSource.mOrigin.GetAsGPUOrigin2DDict();
    srcOriginX = dict.mX;
    srcOriginY = dict.mY;
  } else {
    MOZ_CRASH("Unexpected origin type!");
  }

  const auto checkedMaxWidth = CheckedInt<uint32_t>(srcOriginX) + extent.width;
  const auto checkedMaxHeight =
      CheckedInt<uint32_t>(srcOriginY) + extent.height;
  if (!checkedMaxWidth.isValid() || !checkedMaxHeight.isValid()) {
    aRv.ThrowOperationError("Offset and copy size exceed integer bounds");
    return;
  }

  const gfx::IntSize surfaceSize = dataSurface->GetSize();
  const auto surfaceWidth = AssertedCast<uint32_t>(surfaceSize.width);
  const auto surfaceHeight = AssertedCast<uint32_t>(surfaceSize.height);
  if (surfaceWidth < checkedMaxWidth.value() ||
      surfaceHeight < checkedMaxHeight.value()) {
    aRv.ThrowOperationError("Offset and copy size exceed surface bounds");
    return;
  }

  const auto dstWidth = extent.width;
  const auto dstHeight = extent.height;
  if (dstWidth == 0 || dstHeight == 0) {
    aRv.ThrowOperationError("Destination size is empty");
    return;
  }

  if (!aDestination.mTexture->mBytesPerBlock) {
    // TODO(bug 1781071) This should emmit a GPUValidationError on the device
    // timeline.
    aRv.ThrowInvalidStateError("Invalid destination format");
    return;
  }

  // Note: This assumes bytes per block == bytes per pixel which is the case
  // here because the spec only allows non-compressed texture formats for the
  // destination.
  const auto dstStride = CheckedInt<uint32_t>(extent.width) *
                         aDestination.mTexture->mBytesPerBlock.value();
  const auto dstByteLength = dstStride * extent.height;
  if (!dstStride.isValid() || !dstByteLength.isValid()) {
    aRv.Throw(NS_ERROR_OUT_OF_MEMORY);
    return;
  }

  auto handle = mozilla::ipc::shared_memory::Create(dstByteLength.value());
  auto mapping = handle.Map();
  if (!handle || !mapping) {
    aRv.Throw(NS_ERROR_OUT_OF_MEMORY);
    return;
  }

  const int32_t pixelSize = gfx::BytesPerPixel(surfaceFormat);
  auto* dstBegin = mapping.DataAs<uint8_t>();
  const auto* srcBegin =
      map.GetData() + srcOriginX * pixelSize + srcOriginY * map.GetStride();
  const auto srcOriginPos = gl::OriginPos::TopLeft;
  const auto srcStride = AssertedCast<uint32_t>(map.GetStride());
  const auto dstOriginPos =
      aSource.mFlipY ? gl::OriginPos::BottomLeft : gl::OriginPos::TopLeft;
  bool wasTrivial;

  auto dstStrideVal = dstStride.value();

  if (!ConvertImage(dstWidth, dstHeight, srcBegin, srcStride, srcOriginPos,
                    srcFormat, srcPremultiplied, dstBegin, dstStrideVal,
                    dstOriginPos, dstFormat, aDestination.mPremultipliedAlpha,
                    dom::PredefinedColorSpace::Srgb,
                    dom::PredefinedColorSpace::Srgb, &wasTrivial)) {
    MOZ_ASSERT_UNREACHABLE("ConvertImage failed!");
    aRv.ThrowInvalidStateError(
        nsPrintfCString("Failed to convert source to destination format "
                        "(%i/%i), please file a bug!",
                        (int)srcFormat, (int)dstFormat));
    return;
  }

  ffi::WGPUTexelCopyBufferLayout dataLayout = {0, &dstStrideVal, &dstHeight};
  ffi::WGPUTexelCopyTextureInfo copyView = {};
  CommandEncoder::ConvertTextureCopyViewToFFI(aDestination, &copyView);

  auto shmem_handle_index = GetChild()->QueueShmemHandle(std::move(handle));
  ffi::wgpu_queue_write_texture_via_shmem(GetClient(), mParent->GetId(),
                                          GetId(), copyView, dataLayout, extent,
                                          shmem_handle_index);
}

}  // namespace mozilla::webgpu