tor-browser

The Tor Browser
git clone https://git.dasho.dev/tor-browser.git
Log | Files | Refs | README | LICENSE

WebGPUTypes.h (3429B)


      1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
      2 /* This Source Code Form is subject to the terms of the Mozilla Public
      3 * License, v. 2.0. If a copy of the MPL was not distributed with this
      4 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
      5 
      6 #ifndef WEBGPU_TYPES_H_
      7 #define WEBGPU_TYPES_H_
      8 
      9 #include <cstdint>
     10 
     11 #include "mozilla/GfxMessageUtils.h"
     12 #include "mozilla/Maybe.h"
     13 #include "mozilla/ParamTraits_STL.h"
     14 #include "mozilla/dom/BindingDeclarations.h"
     15 #include "mozilla/layers/LayersSurfaces.h"
     16 #include "nsString.h"
     17 
     18 namespace mozilla::dom {
     19 enum class GPUErrorFilter : uint8_t;
     20 }  // namespace mozilla::dom
     21 
     22 namespace mozilla::webgpu {
     23 
     24 using RawId = uint64_t;
     25 using BufferAddress = uint64_t;
     26 
     27 struct ErrorScope {
     28  dom::GPUErrorFilter filter;
     29  Maybe<nsCString> firstMessage;
     30 };
     31 
     32 enum class PopErrorScopeResultType : uint8_t {
     33  NoError,
     34  ThrowOperationError,
     35  ValidationError,
     36  OutOfMemory,
     37  InternalError,
     38  DeviceLost,
     39  _LAST = DeviceLost,
     40 };
     41 
     42 struct PopErrorScopeResult {
     43  PopErrorScopeResultType resultType;
     44  nsCString message;
     45 };
     46 
     47 enum class WebGPUCompilationMessageType { Error, Warning, Info };
     48 
     49 // TODO: Better name? CompilationMessage alread taken by the dom object.
     50 /// The serializable counterpart of the dom object CompilationMessage.
     51 struct WebGPUCompilationMessage {
     52  nsString message;
     53  uint64_t lineNum = 0;
     54  uint64_t linePos = 0;
     55  // In utf16 code units.
     56  uint64_t offset = 0;
     57  // In utf16 code units.
     58  uint64_t length = 0;
     59  WebGPUCompilationMessageType messageType =
     60      WebGPUCompilationMessageType::Error;
     61 };
     62 
     63 /// A helper to reduce the boiler plate of turning the many Optional<nsAString>
     64 /// we get from the dom to the nullable nsACString* we pass to the wgpu ffi.
     65 class StringHelper {
     66 public:
     67  explicit StringHelper(const nsString& aWide) {
     68    if (!aWide.IsEmpty()) {
     69      mNarrow = Some(NS_ConvertUTF16toUTF8(aWide));
     70    }
     71  }
     72 
     73  const nsACString* Get() const {
     74    if (mNarrow.isSome()) {
     75      return mNarrow.ptr();
     76    }
     77    return nullptr;
     78  }
     79 
     80 private:
     81  Maybe<NS_ConvertUTF16toUTF8> mNarrow;
     82 };
     83 
     84 // Used to create an ExternalTextureSourceHost.
     85 struct ExternalTextureSourceDescriptor {
     86  std::array<RawId, 3> mTextureIds;
     87  std::array<RawId, 3> mViewIds;
     88  layers::SurfaceDescriptor mSurfaceDescriptor;
     89  gfx::IntSize mSize;
     90  std::array<float, 6> mSampleTransform;
     91  std::array<float, 6> mLoadTransform;
     92 };
     93 
     94 }  // namespace mozilla::webgpu
     95 
     96 namespace IPC {
     97 template <>
     98 struct ParamTraits<mozilla::webgpu::ExternalTextureSourceDescriptor> {
     99  using ParamType = mozilla::webgpu::ExternalTextureSourceDescriptor;
    100 
    101  static void Write(MessageWriter* aWriter, const ParamType& aParam) {
    102    WriteParam(aWriter, aParam.mTextureIds);
    103    WriteParam(aWriter, aParam.mViewIds);
    104    WriteParam(aWriter, aParam.mSurfaceDescriptor);
    105    WriteParam(aWriter, aParam.mSize);
    106    WriteParam(aWriter, aParam.mSampleTransform);
    107    WriteParam(aWriter, aParam.mLoadTransform);
    108  }
    109 
    110  static bool Read(MessageReader* aReader, ParamType* aResult) {
    111    return ReadParam(aReader, &aResult->mTextureIds) &&
    112           ReadParam(aReader, &aResult->mViewIds) &&
    113           ReadParam(aReader, &aResult->mSurfaceDescriptor) &&
    114           ReadParam(aReader, &aResult->mSize) &&
    115           ReadParam(aReader, &aResult->mSampleTransform) &&
    116           ReadParam(aReader, &aResult->mLoadTransform);
    117  }
    118 };
    119 
    120 }  // namespace IPC
    121 
    122 #endif  // WEBGPU_TYPES_H_