2020-01-08 22:19:14 +00:00
|
|
|
/* -*- 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/. */
|
|
|
|
|
2020-04-30 22:21:45 +00:00
|
|
|
#ifndef WEBGLQUEUEPARAMTRAITS_H_
|
|
|
|
#define WEBGLQUEUEPARAMTRAITS_H_
|
2020-01-08 22:19:14 +00:00
|
|
|
|
2020-03-28 13:57:18 +00:00
|
|
|
#include <type_traits>
|
|
|
|
|
2020-01-08 22:19:14 +00:00
|
|
|
#include "mozilla/dom/ProducerConsumerQueue.h"
|
|
|
|
#include "TexUnpackBlob.h"
|
|
|
|
#include "WebGLContext.h"
|
|
|
|
#include "WebGLTypes.h"
|
|
|
|
|
|
|
|
namespace mozilla {
|
|
|
|
|
|
|
|
namespace webgl {
|
|
|
|
template <typename T>
|
2020-04-30 22:21:45 +00:00
|
|
|
struct QueueParamTraits;
|
2020-01-08 22:19:14 +00:00
|
|
|
|
|
|
|
template <>
|
2020-03-28 13:57:20 +00:00
|
|
|
struct IsTriviallySerializable<FloatOrInt> : std::true_type {};
|
2020-01-08 22:19:14 +00:00
|
|
|
|
|
|
|
template <>
|
2020-03-28 13:57:20 +00:00
|
|
|
struct IsTriviallySerializable<webgl::ShaderPrecisionFormat> : std::true_type {
|
|
|
|
};
|
2020-01-08 22:19:14 +00:00
|
|
|
|
|
|
|
template <>
|
2020-03-28 13:57:20 +00:00
|
|
|
struct IsTriviallySerializable<WebGLContextOptions> : std::true_type {};
|
2020-01-08 22:19:14 +00:00
|
|
|
|
|
|
|
template <>
|
2020-03-28 13:57:20 +00:00
|
|
|
struct IsTriviallySerializable<WebGLPixelStore> : std::true_type {};
|
2020-01-08 22:19:14 +00:00
|
|
|
|
|
|
|
template <>
|
2020-03-28 13:57:20 +00:00
|
|
|
struct IsTriviallySerializable<WebGLTexImageData> : std::true_type {};
|
2020-01-08 22:19:14 +00:00
|
|
|
|
|
|
|
template <>
|
2020-03-28 13:57:20 +00:00
|
|
|
struct IsTriviallySerializable<WebGLTexPboOffset> : std::true_type {};
|
2020-01-08 22:19:14 +00:00
|
|
|
|
|
|
|
template <>
|
2020-03-28 13:57:20 +00:00
|
|
|
struct IsTriviallySerializable<webgl::ExtensionBits> : std::true_type {};
|
2020-01-08 22:19:16 +00:00
|
|
|
template <>
|
2020-03-28 13:57:20 +00:00
|
|
|
struct IsTriviallySerializable<webgl::GetUniformData> : std::true_type {};
|
2020-01-08 22:19:14 +00:00
|
|
|
|
|
|
|
template <>
|
2020-03-28 13:57:20 +00:00
|
|
|
struct IsTriviallySerializable<ICRData> : std::true_type {};
|
2020-01-08 22:19:14 +00:00
|
|
|
|
|
|
|
template <>
|
2020-03-28 13:57:20 +00:00
|
|
|
struct IsTriviallySerializable<gfx::IntSize> : std::true_type {};
|
2020-01-08 22:19:14 +00:00
|
|
|
|
2020-01-08 22:19:16 +00:00
|
|
|
template <typename T>
|
2020-03-28 13:57:20 +00:00
|
|
|
struct IsTriviallySerializable<avec2<T>> : std::true_type {};
|
2020-01-08 22:19:16 +00:00
|
|
|
template <typename T>
|
2020-03-28 13:57:20 +00:00
|
|
|
struct IsTriviallySerializable<avec3<T>> : std::true_type {};
|
2020-01-08 22:19:14 +00:00
|
|
|
|
|
|
|
template <>
|
2020-03-28 13:57:20 +00:00
|
|
|
struct IsTriviallySerializable<webgl::TexUnpackBlob> : std::true_type {};
|
2020-04-30 22:22:52 +00:00
|
|
|
|
|
|
|
template <>
|
|
|
|
struct IsTriviallySerializable<webgl::TypedQuad> : std::true_type {};
|
|
|
|
template <>
|
|
|
|
struct IsTriviallySerializable<webgl::VertAttribPointerDesc> : std::true_type {
|
|
|
|
};
|
|
|
|
template <>
|
|
|
|
struct IsTriviallySerializable<webgl::ReadPixelsDesc> : std::true_type {};
|
|
|
|
|
2020-01-08 22:19:14 +00:00
|
|
|
template <typename T>
|
2020-04-30 22:21:45 +00:00
|
|
|
struct QueueParamTraits<RawBuffer<T>> {
|
2020-01-08 22:19:14 +00:00
|
|
|
using ParamType = RawBuffer<T>;
|
|
|
|
|
2020-04-30 22:23:02 +00:00
|
|
|
template <typename U>
|
2020-07-21 22:56:52 +00:00
|
|
|
static QueueStatus Write(ProducerView<U>& view, const ParamType& in) {
|
|
|
|
const auto range = in.Data();
|
|
|
|
|
|
|
|
const auto elemCount = range.length();
|
|
|
|
auto status = view.WriteParam(elemCount);
|
|
|
|
if (!status) return status;
|
|
|
|
if (!elemCount) return status;
|
|
|
|
|
|
|
|
const bool hasData = bool(range.begin().get());
|
|
|
|
status = view.WriteParam(hasData);
|
|
|
|
if (!status) return status;
|
|
|
|
if (!hasData) return status;
|
|
|
|
|
|
|
|
status = view.Write(range.begin().get(), range.end().get());
|
|
|
|
return status;
|
2020-01-08 22:19:14 +00:00
|
|
|
}
|
|
|
|
|
2020-07-21 22:56:52 +00:00
|
|
|
template <typename U>
|
|
|
|
static QueueStatus Read(ConsumerView<U>& view, ParamType* const out) {
|
|
|
|
size_t elemCount = 0;
|
|
|
|
auto status = view.ReadParam(&elemCount);
|
|
|
|
if (!status) return status;
|
|
|
|
if (!elemCount) {
|
|
|
|
*out = {};
|
|
|
|
return QueueStatus::kSuccess;
|
2020-01-08 22:19:14 +00:00
|
|
|
}
|
|
|
|
|
2020-07-21 22:56:52 +00:00
|
|
|
bool hasData = false;
|
|
|
|
status = view.ReadParam(&hasData);
|
|
|
|
if (!status) return status;
|
|
|
|
if (!hasData) {
|
|
|
|
auto temp = RawBuffer<T>{Range<T>{nullptr, elemCount}};
|
|
|
|
*out = std::move(temp);
|
2020-04-30 22:21:45 +00:00
|
|
|
return QueueStatus::kSuccess;
|
2020-01-08 22:19:14 +00:00
|
|
|
}
|
|
|
|
|
2020-07-21 22:56:52 +00:00
|
|
|
auto buffer = UniqueBuffer::Alloc(elemCount * sizeof(T));
|
|
|
|
if (!buffer) return QueueStatus::kOOMError;
|
2020-01-08 22:19:14 +00:00
|
|
|
|
2020-07-21 22:56:52 +00:00
|
|
|
using MutT = std::remove_cv_t<T>;
|
|
|
|
const auto begin = reinterpret_cast<MutT*>(buffer.get());
|
|
|
|
const auto range = Range<MutT>{begin, elemCount};
|
|
|
|
|
|
|
|
auto temp = RawBuffer<T>{range, std::move(buffer)};
|
|
|
|
*out = std::move(temp);
|
|
|
|
return view.Read(range.begin().get(), range.end().get());
|
2020-01-08 22:19:14 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2020-01-08 22:19:16 +00:00
|
|
|
template <>
|
2020-06-08 23:35:46 +00:00
|
|
|
struct QueueParamTraits<webgl::ContextLossReason>
|
|
|
|
: public ContiguousEnumSerializerInclusive<
|
|
|
|
webgl::ContextLossReason, webgl::ContextLossReason::None,
|
|
|
|
webgl::ContextLossReason::Guilty> {};
|
2020-01-08 22:19:16 +00:00
|
|
|
|
|
|
|
template <typename V, typename E>
|
2020-04-30 22:21:45 +00:00
|
|
|
struct QueueParamTraits<Result<V, E>> {
|
2020-01-08 22:19:16 +00:00
|
|
|
using T = Result<V, E>;
|
|
|
|
|
2020-04-30 22:23:02 +00:00
|
|
|
template <typename U>
|
|
|
|
static QueueStatus Write(ProducerView<U>& aProducerView, const T& aArg) {
|
2020-01-08 22:19:16 +00:00
|
|
|
const auto ok = aArg.isOk();
|
|
|
|
auto status = aProducerView.WriteParam(ok);
|
|
|
|
if (!status) return status;
|
|
|
|
if (ok) {
|
|
|
|
status = aProducerView.WriteParam(aArg.unwrap());
|
|
|
|
} else {
|
|
|
|
status = aProducerView.WriteParam(aArg.unwrapErr());
|
|
|
|
}
|
|
|
|
return status;
|
|
|
|
}
|
|
|
|
|
2020-04-30 22:23:02 +00:00
|
|
|
template <typename U>
|
2020-06-08 23:42:37 +00:00
|
|
|
static QueueStatus Read(ConsumerView<U>& aConsumerView, T* aArg) {
|
2020-01-08 22:19:16 +00:00
|
|
|
bool ok;
|
|
|
|
auto status = aConsumerView.ReadParam(&ok);
|
|
|
|
if (!status) return status;
|
|
|
|
if (ok) {
|
|
|
|
V val;
|
|
|
|
status = aConsumerView.ReadParam(&val);
|
|
|
|
*aArg = val;
|
|
|
|
} else {
|
|
|
|
E val;
|
|
|
|
status = aConsumerView.ReadParam(&val);
|
|
|
|
*aArg = Err(val);
|
|
|
|
}
|
|
|
|
return status;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
template <>
|
2020-04-30 22:21:45 +00:00
|
|
|
struct QueueParamTraits<std::string> {
|
2020-01-08 22:19:16 +00:00
|
|
|
using T = std::string;
|
|
|
|
|
2020-04-30 22:23:02 +00:00
|
|
|
template <typename U>
|
|
|
|
static QueueStatus Write(ProducerView<U>& aProducerView, const T& aArg) {
|
2020-01-08 22:19:16 +00:00
|
|
|
auto status = aProducerView.WriteParam(aArg.size());
|
|
|
|
if (!status) return status;
|
2020-07-21 22:56:52 +00:00
|
|
|
status = aProducerView.Write(aArg.data(), aArg.data() + aArg.size());
|
2020-01-08 22:19:16 +00:00
|
|
|
return status;
|
|
|
|
}
|
|
|
|
|
2020-04-30 22:23:02 +00:00
|
|
|
template <typename U>
|
2020-06-08 23:42:37 +00:00
|
|
|
static QueueStatus Read(ConsumerView<U>& aConsumerView, T* aArg) {
|
2020-01-08 22:19:16 +00:00
|
|
|
size_t size;
|
|
|
|
auto status = aConsumerView.ReadParam(&size);
|
|
|
|
if (!status) return status;
|
|
|
|
|
|
|
|
const UniqueBuffer temp = malloc(size);
|
|
|
|
const auto dest = static_cast<char*>(temp.get());
|
2020-04-30 22:21:45 +00:00
|
|
|
if (!dest) return QueueStatus::kFatalError;
|
2020-01-08 22:19:16 +00:00
|
|
|
|
2020-07-21 22:56:52 +00:00
|
|
|
status = aConsumerView.Read(dest, dest + size);
|
2020-06-08 23:42:37 +00:00
|
|
|
aArg->assign(dest, size);
|
2020-01-08 22:19:16 +00:00
|
|
|
return status;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
template <typename U>
|
2020-04-30 22:21:45 +00:00
|
|
|
struct QueueParamTraits<std::vector<U>> {
|
2020-04-30 22:22:52 +00:00
|
|
|
using T = std::vector<U>;
|
2020-01-08 22:19:16 +00:00
|
|
|
|
2020-04-30 22:23:02 +00:00
|
|
|
template <typename V>
|
|
|
|
static QueueStatus Write(ProducerView<V>& aProducerView, const T& aArg) {
|
2020-01-08 22:19:16 +00:00
|
|
|
auto status = aProducerView.WriteParam(aArg.size());
|
|
|
|
if (!status) return status;
|
2020-07-21 22:56:52 +00:00
|
|
|
status = aProducerView.Write(aArg.data(), aArg.data() + aArg.size());
|
2020-01-08 22:19:16 +00:00
|
|
|
return status;
|
|
|
|
}
|
|
|
|
|
2020-04-30 22:23:02 +00:00
|
|
|
template <typename V>
|
2020-06-08 23:42:37 +00:00
|
|
|
static QueueStatus Read(ConsumerView<V>& aConsumerView, T* aArg) {
|
2020-01-08 22:19:16 +00:00
|
|
|
MOZ_CRASH("no way to fallibly resize vectors without exceptions");
|
|
|
|
size_t size;
|
|
|
|
auto status = aConsumerView.ReadParam(&size);
|
|
|
|
if (!status) return status;
|
|
|
|
|
|
|
|
aArg->resize(size);
|
2020-07-21 22:56:52 +00:00
|
|
|
status = aConsumerView.Read(aArg->data(), aArg->data() + size);
|
2020-01-08 22:19:16 +00:00
|
|
|
return status;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
template <>
|
2020-06-08 23:35:46 +00:00
|
|
|
struct QueueParamTraits<WebGLExtensionID>
|
|
|
|
: public ContiguousEnumSerializer<WebGLExtensionID,
|
|
|
|
WebGLExtensionID::ANGLE_instanced_arrays,
|
|
|
|
WebGLExtensionID::Max> {};
|
2020-01-08 22:19:14 +00:00
|
|
|
|
2020-04-30 22:22:52 +00:00
|
|
|
template <>
|
|
|
|
struct QueueParamTraits<CompileResult> {
|
|
|
|
using T = CompileResult;
|
|
|
|
|
2020-04-30 22:23:02 +00:00
|
|
|
template <typename U>
|
|
|
|
static QueueStatus Write(ProducerView<U>& aProducerView, const T& aArg) {
|
2020-04-30 22:22:52 +00:00
|
|
|
aProducerView.WriteParam(aArg.pending);
|
|
|
|
aProducerView.WriteParam(aArg.log);
|
|
|
|
aProducerView.WriteParam(aArg.translatedSource);
|
|
|
|
return aProducerView.WriteParam(aArg.success);
|
|
|
|
}
|
|
|
|
|
2020-04-30 22:23:02 +00:00
|
|
|
template <typename U>
|
2020-06-08 23:42:37 +00:00
|
|
|
static QueueStatus Read(ConsumerView<U>& aConsumerView, T* aArg) {
|
|
|
|
aConsumerView.ReadParam(&aArg->pending);
|
|
|
|
aConsumerView.ReadParam(&aArg->log);
|
|
|
|
aConsumerView.ReadParam(&aArg->translatedSource);
|
|
|
|
return aConsumerView.ReadParam(&aArg->success);
|
2020-04-30 22:22:52 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2020-01-08 22:19:14 +00:00
|
|
|
} // namespace webgl
|
|
|
|
} // namespace mozilla
|
|
|
|
|
2020-04-30 22:21:45 +00:00
|
|
|
#endif // WEBGLQUEUEPARAMTRAITS_H_
|