chromium/third_party/blink/renderer/modules/webgpu/dawn_conversions.h

// 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.

#ifndef THIRD_PARTY_BLINK_RENDERER_MODULES_WEBGPU_DAWN_CONVERSIONS_H_
#define THIRD_PARTY_BLINK_RENDERER_MODULES_WEBGPU_DAWN_CONVERSIONS_H_

#include <memory>

#include "base/check.h"
#include "base/containers/heap_array.h"
#include "third_party/blink/renderer/bindings/modules/v8/v8_typedefs.h"
#include "third_party/blink/renderer/modules/webgpu/dawn_enum_conversions.h"
#include "third_party/blink/renderer/modules/webgpu/dawn_object.h"
#include "third_party/blink/renderer/platform/graphics/gpu/webgpu_cpp.h"
#include "third_party/blink/renderer/platform/heap/collection_support/heap_vector.h"
#include "third_party/blink/renderer/platform/heap/member.h"

// This file provides helpers for converting WebGPU objects, descriptors,
// and enums from Blink to Dawn types.

namespace blink {

class GPUImageCopyTexture;
class GPUImageDataLayout;
class V8UnionGPUAutoLayoutModeOrGPUPipelineLayout;

// These conversions are used multiple times and are declared here. Conversions
// used only once, for example for object construction, are defined
// individually.
wgpu::TextureFormat AsDawnType(SkColorType color_type);
wgpu::PipelineLayout AsDawnType(
    V8UnionGPUAutoLayoutModeOrGPUPipelineLayout* webgpu_layout);

// Conversion for convenience types that are dict|sequence<Number> and other
// types that recursively use them. A return value of false means that the
// conversion failed and a TypeError was recorded in the ExceptionState.
bool ConvertToDawn(const V8GPUColor* in, wgpu::Color* out, ExceptionState&);
bool ConvertToDawn(const V8GPUExtent3D* in,
                   wgpu::Extent3D* out,
                   GPUDevice* device,
                   ExceptionState&);
bool ConvertToDawn(const V8GPUOrigin3D* in,
                   wgpu::Origin3D* out,
                   ExceptionState&);
bool ConvertToDawn(const V8GPUOrigin2D* in,
                   wgpu::Origin2D* out,
                   ExceptionState&);
bool ConvertToDawn(const GPUImageCopyTexture* in,
                   wgpu::ImageCopyTexture* out,
                   ExceptionState&);

const char* ValidateTextureDataLayout(const GPUImageDataLayout* webgpu_layout,
                                      wgpu::TextureDataLayout* layout);

// WebGPU objects are converted to Dawn objects by getting the opaque handle
// which can be passed to Dawn.
template <typename Handle>
Handle AsDawnType(const DawnObject<Handle>* object) {}

TypeOfDawnType;

// Helper for converting a list of objects to Dawn structs or handles
template <typename WebGPUType>
std::unique_ptr<TypeOfDawnType<WebGPUType>[]> AsDawnType(
    const HeapVector<Member<WebGPUType>>& webgpu_objects) {}
template <typename WebGPUType, typename DawnType>
bool ConvertToDawn(const HeapVector<Member<WebGPUType>>& in,
                   std::unique_ptr<DawnType[]>* out,
                   ExceptionState& exception_state) {}

template <typename DawnEnum, typename WebGPUEnum>
base::HeapArray<DawnEnum> AsDawnEnum(const Vector<WebGPUEnum>& webgpu_enums) {}

// For sequence of nullable enums, convert null value to undefined
// dawn_enums should be a pre-allocated array with a size of count
template <typename DawnEnum, typename WebGPUEnum>
base::HeapArray<DawnEnum> AsDawnEnum(
    const Vector<std::optional<WebGPUEnum>>& webgpu_enums) {}

}  // namespace blink

#endif  // THIRD_PARTY_BLINK_RENDERER_MODULES_WEBGPU_DAWN_CONVERSIONS_H_