tor-browser

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

WebGLQueueParamTraits.h (8000B)


      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 WEBGLQUEUEPARAMTRAITS_H_
      7 #define WEBGLQUEUEPARAMTRAITS_H_
      8 
      9 #include "TexUnpackBlob.h"
     10 #include "WebGLTypes.h"
     11 #include "ipc/EnumSerializer.h"
     12 
     13 namespace mozilla {
     14 namespace webgl {
     15 template <typename T>
     16 struct QueueParamTraits;
     17 
     18 // -
     19 
     20 #define USE_TIED_FIELDS(T) \
     21  template <>              \
     22  struct QueueParamTraits<T> : QueueParamTraits_TiedFields<T> {};
     23 
     24 // -
     25 
     26 USE_TIED_FIELDS(layers::RemoteTextureId)
     27 USE_TIED_FIELDS(layers::RemoteTextureOwnerId)
     28 USE_TIED_FIELDS(WebGLContextOptions)
     29 USE_TIED_FIELDS(webgl::PixelUnpackStateWebgl)
     30 USE_TIED_FIELDS(webgl::SwapChainOptions)
     31 USE_TIED_FIELDS(webgl::ReadPixelsDesc)
     32 USE_TIED_FIELDS(webgl::VertAttribPointerDesc)
     33 USE_TIED_FIELDS(webgl::PackingInfo)
     34 USE_TIED_FIELDS(webgl::TypedQuad)
     35 USE_TIED_FIELDS(webgl::PixelPackingState)
     36 USE_TIED_FIELDS(FloatOrInt)
     37 
     38 }  // namespace webgl
     39 template <>
     40 inline auto TiedFields<gfx::IntSize>(gfx::IntSize& a) {
     41  return std::tie(a.width, a.height);
     42 }
     43 namespace webgl {
     44 USE_TIED_FIELDS(gfx::IntSize)
     45 
     46 // -
     47 
     48 #undef USE_TIED_FIELDS
     49 
     50 // -
     51 
     52 template <class T>
     53 struct QueueParamTraits<avec2<T>> : QueueParamTraits_TiedFields<avec2<T>> {};
     54 
     55 template <class T>
     56 struct QueueParamTraits<avec3<T>> : QueueParamTraits_TiedFields<avec3<T>> {};
     57 
     58 // ---------------------------------------------------------------------
     59 // Enums!
     60 
     61 }  // namespace webgl
     62 
     63 inline constexpr bool IsEnumCase(const webgl::AttribBaseType raw) {
     64  switch (raw) {
     65    case webgl::AttribBaseType::Boolean:
     66    case webgl::AttribBaseType::Float:
     67    case webgl::AttribBaseType::Int:
     68    case webgl::AttribBaseType::Uint:
     69      return true;
     70  }
     71  return false;
     72 }
     73 static_assert(IsEnumCase(webgl::AttribBaseType(3)));
     74 static_assert(!IsEnumCase(webgl::AttribBaseType(4)));
     75 static_assert(!IsEnumCase(webgl::AttribBaseType(5)));
     76 
     77 namespace webgl {
     78 
     79 #define USE_IS_ENUM_CASE(T) \
     80  template <>               \
     81  struct QueueParamTraits<T> : QueueParamTraits_IsEnumCase<T> {};
     82 
     83 USE_IS_ENUM_CASE(webgl::AttribBaseType)
     84 USE_IS_ENUM_CASE(webgl::ProvokingVertex)
     85 
     86 #undef USE_IS_ENUM_CASE
     87 
     88 // ---------------------------------------------------------------------
     89 // Custom QueueParamTraits
     90 
     91 template <typename T>
     92 struct QueueParamTraits<Span<T>> {
     93  template <typename U>
     94  static bool Write(ProducerView<U>& view, const Span<T>& in) {
     95    const auto& elemCount = in.size();
     96    auto status = view.WriteParam(elemCount);
     97    if (!status) return status;
     98 
     99    if (!elemCount) return status;
    100    status = view.WriteFromRange(Range<const T>{in});
    101 
    102    return status;
    103  }
    104 
    105  template <typename U>
    106  static bool Read(ConsumerView<U>& view, Span<const T>* const out) {
    107    size_t elemCount = 0;
    108    auto status = view.ReadParam(&elemCount);
    109    if (!status) return status;
    110 
    111    if (!elemCount) {
    112      *out = {};
    113      return true;
    114    }
    115 
    116    auto data = view.template ReadRange<const T>(elemCount);
    117    if (!data) return false;
    118    *out = Span{*data};
    119    return true;
    120  }
    121 };
    122 
    123 template <>
    124 struct QueueParamTraits<webgl::ContextLossReason>
    125    : public ContiguousEnumSerializerInclusive<
    126          webgl::ContextLossReason, webgl::ContextLossReason::None,
    127          webgl::ContextLossReason::Guilty> {};
    128 
    129 template <typename V, typename E>
    130 struct QueueParamTraits<Result<V, E>> {
    131  using T = Result<V, E>;
    132 
    133  template <typename U>
    134  static bool Write(ProducerView<U>& aProducerView, const T& aArg) {
    135    const auto ok = aArg.isOk();
    136    auto status = aProducerView.WriteParam(ok);
    137    if (!status) return status;
    138    if (ok) {
    139      status = aProducerView.WriteParam(aArg.unwrap());
    140    } else {
    141      status = aProducerView.WriteParam(aArg.unwrapErr());
    142    }
    143    return status;
    144  }
    145 
    146  template <typename U>
    147  static bool Read(ConsumerView<U>& aConsumerView, T* aArg) {
    148    bool ok;
    149    auto status = aConsumerView.ReadParam(&ok);
    150    if (!status) return status;
    151    if (ok) {
    152      V val;
    153      status = aConsumerView.ReadParam(&val);
    154      *aArg = val;
    155    } else {
    156      E val;
    157      status = aConsumerView.ReadParam(&val);
    158      *aArg = Err(val);
    159    }
    160    return status;
    161  }
    162 };
    163 
    164 template <>
    165 struct QueueParamTraits<std::string> {
    166  using T = std::string;
    167 
    168  template <typename U>
    169  static bool Write(ProducerView<U>& aProducerView, const T& aArg) {
    170    const auto size = aArg.size();
    171    auto status = aProducerView.WriteParam(size);
    172    if (!status) return status;
    173    status = aProducerView.WriteFromRange(Range<const char>{aArg.data(), size});
    174    return status;
    175  }
    176 
    177  template <typename U>
    178  static bool Read(ConsumerView<U>& aConsumerView, T* aArg) {
    179    size_t size;
    180    auto status = aConsumerView.ReadParam(&size);
    181    if (!status) return status;
    182 
    183    const auto view = aConsumerView.template ReadRange<char>(size);
    184    if (!view) return false;
    185    aArg->assign(view->begin().get(), size);
    186    return status;
    187  }
    188 };
    189 
    190 template <typename U>
    191 struct QueueParamTraits<std::vector<U>> {
    192  using T = std::vector<U>;
    193 
    194  template <typename V>
    195  static bool Write(ProducerView<V>& aProducerView, const T& aArg) {
    196    auto status = aProducerView.WriteParam(aArg.size());
    197    if (!status) return status;
    198 
    199    for (const auto& cur : aArg) {
    200      status = aProducerView.WriteParam(cur);
    201      if (!status) return status;
    202    }
    203    return status;
    204  }
    205 
    206  template <typename V>
    207  static bool Read(ConsumerView<V>& aConsumerView, T* aArg) {
    208    size_t size;
    209    auto status = aConsumerView.ReadParam(&size);
    210    if (!status) return status;
    211    aArg->resize(size);
    212 
    213    for (auto& cur : *aArg) {
    214      status = aConsumerView.ReadParam(&cur);
    215      if (!status) return status;
    216    }
    217    return status;
    218  }
    219 };
    220 
    221 template <>
    222 struct QueueParamTraits<WebGLExtensionID>
    223    : public ContiguousEnumSerializer<WebGLExtensionID,
    224                                      WebGLExtensionID::ANGLE_instanced_arrays,
    225                                      WebGLExtensionID::Max> {};
    226 
    227 template <>
    228 struct QueueParamTraits<CompileResult> {
    229  using T = CompileResult;
    230 
    231  template <typename U>
    232  static bool Write(ProducerView<U>& aProducerView, const T& aArg) {
    233    aProducerView.WriteParam(aArg.pending);
    234    aProducerView.WriteParam(aArg.log);
    235    aProducerView.WriteParam(aArg.translatedSource);
    236    return aProducerView.WriteParam(aArg.success);
    237  }
    238 
    239  template <typename U>
    240  static bool Read(ConsumerView<U>& aConsumerView, T* aArg) {
    241    aConsumerView.ReadParam(&aArg->pending);
    242    aConsumerView.ReadParam(&aArg->log);
    243    aConsumerView.ReadParam(&aArg->translatedSource);
    244    return aConsumerView.ReadParam(&aArg->success);
    245  }
    246 };
    247 
    248 template <>
    249 struct QueueParamTraits<mozilla::layers::TextureType>
    250    : public ContiguousEnumSerializer<mozilla::layers::TextureType,
    251                                      mozilla::layers::TextureType::Unknown,
    252                                      mozilla::layers::TextureType::Last> {};
    253 
    254 template <>
    255 struct QueueParamTraits<mozilla::gfx::SurfaceFormat>
    256    : public ContiguousEnumSerializerInclusive<
    257          mozilla::gfx::SurfaceFormat, mozilla::gfx::SurfaceFormat::B8G8R8A8,
    258          mozilla::gfx::SurfaceFormat::UNKNOWN> {};
    259 
    260 template <>
    261 struct QueueParamTraits<gfxAlphaType>
    262    : public ContiguousEnumSerializerInclusive<
    263          gfxAlphaType, gfxAlphaType::Opaque, gfxAlphaType::NonPremult> {};
    264 
    265 // -
    266 
    267 template <class Enum>
    268 using WebIDLEnumQueueSerializer =
    269    ContiguousEnumSerializerInclusive<Enum, ContiguousEnumValues<Enum>::min,
    270                                      ContiguousEnumValues<Enum>::max>;
    271 
    272 template <>
    273 struct QueueParamTraits<dom::WebGLPowerPreference>
    274    : public WebIDLEnumQueueSerializer<dom::WebGLPowerPreference> {};
    275 template <>
    276 struct QueueParamTraits<dom::PredefinedColorSpace>
    277    : public WebIDLEnumQueueSerializer<dom::PredefinedColorSpace> {};
    278 
    279 }  // namespace webgl
    280 }  // namespace mozilla
    281 
    282 #endif  // WEBGLQUEUEPARAMTRAITS_H_