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/. */
6 #ifndef WEBGLQUEUEPARAMTRAITS_H_
7 #define WEBGLQUEUEPARAMTRAITS_H_
11 #include "ipc/EnumSerializer.h"
12 #include "TexUnpackBlob.h"
13 #include "WebGLContext.h"
14 #include "WebGLTypes.h"
20 struct QueueParamTraits
;
23 struct IsTriviallySerializable
<FloatOrInt
> : std::true_type
{};
26 struct IsTriviallySerializable
<webgl::ShaderPrecisionFormat
> : std::true_type
{
30 struct IsTriviallySerializable
<WebGLContextOptions
> : std::true_type
{};
33 struct IsTriviallySerializable
<WebGLPixelStore
> : std::true_type
{};
36 struct IsTriviallySerializable
<WebGLTexImageData
> : std::true_type
{};
39 struct IsTriviallySerializable
<WebGLTexPboOffset
> : std::true_type
{};
42 struct IsTriviallySerializable
<webgl::ExtensionBits
> : std::true_type
{};
44 struct IsTriviallySerializable
<webgl::GetUniformData
> : std::true_type
{};
47 struct IsTriviallySerializable
<mozilla::webgl::PackingInfo
> : std::true_type
{};
50 struct IsTriviallySerializable
<ICRData
> : std::true_type
{};
53 struct IsTriviallySerializable
<gfx::IntSize
> : std::true_type
{};
56 struct IsTriviallySerializable
<avec2
<T
>> : std::true_type
{};
58 struct IsTriviallySerializable
<avec3
<T
>> : std::true_type
{};
61 struct IsTriviallySerializable
<webgl::TexUnpackBlob
> : std::true_type
{};
64 struct IsTriviallySerializable
<webgl::TypedQuad
> : std::true_type
{};
66 struct IsTriviallySerializable
<webgl::VertAttribPointerDesc
> : std::true_type
{
69 struct IsTriviallySerializable
<webgl::ReadPixelsDesc
> : std::true_type
{};
71 struct IsTriviallySerializable
<layers::SurfaceDescriptor
> : std::true_type
{};
74 struct QueueParamTraits
<RawBuffer
<T
>> {
75 using ParamType
= RawBuffer
<T
>;
78 static QueueStatus
Write(ProducerView
<U
>& view
, const ParamType
& in
) {
79 const auto& elemCount
= in
.size();
80 auto status
= view
.WriteParam(elemCount
);
81 if (!status
) return status
;
82 if (!elemCount
) return status
;
84 const auto& begin
= in
.begin();
85 const bool hasData
= static_cast<bool>(begin
);
86 status
= view
.WriteParam(hasData
);
87 if (!status
) return status
;
88 if (!hasData
) return status
;
90 status
= view
.WriteFromRange(in
.Data());
95 static QueueStatus
Read(ConsumerView
<U
>& view
, ParamType
* const out
) {
97 auto status
= view
.ReadParam(&elemCount
);
98 if (!status
) return status
;
101 return QueueStatus::kSuccess
;
105 status
= view
.ReadParam(&hasData
);
106 if (!status
) return status
;
108 auto temp
= RawBuffer
<T
>{elemCount
};
109 *out
= std::move(temp
);
110 return QueueStatus::kSuccess
;
113 auto data
= view
.template ReadRange
<T
>(elemCount
);
114 if (!data
) return QueueStatus::kTooSmall
;
115 *out
= std::move(RawBuffer
<T
>{*data
});
116 return QueueStatus::kSuccess
;
121 struct QueueParamTraits
<webgl::ContextLossReason
>
122 : public ContiguousEnumSerializerInclusive
<
123 webgl::ContextLossReason
, webgl::ContextLossReason::None
,
124 webgl::ContextLossReason::Guilty
> {};
126 template <typename V
, typename E
>
127 struct QueueParamTraits
<Result
<V
, E
>> {
128 using T
= Result
<V
, E
>;
130 template <typename U
>
131 static QueueStatus
Write(ProducerView
<U
>& aProducerView
, const T
& aArg
) {
132 const auto ok
= aArg
.isOk();
133 auto status
= aProducerView
.WriteParam(ok
);
134 if (!status
) return status
;
136 status
= aProducerView
.WriteParam(aArg
.unwrap());
138 status
= aProducerView
.WriteParam(aArg
.unwrapErr());
143 template <typename U
>
144 static QueueStatus
Read(ConsumerView
<U
>& aConsumerView
, T
* aArg
) {
146 auto status
= aConsumerView
.ReadParam(&ok
);
147 if (!status
) return status
;
150 status
= aConsumerView
.ReadParam(&val
);
154 status
= aConsumerView
.ReadParam(&val
);
162 struct QueueParamTraits
<std::string
> {
163 using T
= std::string
;
165 template <typename U
>
166 static QueueStatus
Write(ProducerView
<U
>& aProducerView
, const T
& aArg
) {
167 const auto size
= aArg
.size();
168 auto status
= aProducerView
.WriteParam(size
);
169 if (!status
) return status
;
170 status
= aProducerView
.WriteFromRange(Range
<const char>{aArg
.data(), size
});
174 template <typename U
>
175 static QueueStatus
Read(ConsumerView
<U
>& aConsumerView
, T
* aArg
) {
177 auto status
= aConsumerView
.ReadParam(&size
);
178 if (!status
) return status
;
180 const auto view
= aConsumerView
.template ReadRange
<char>(size
);
181 if (!view
) return QueueStatus::kFatalError
;
182 aArg
->assign(view
->begin().get(), size
);
187 template <typename U
>
188 struct QueueParamTraits
<std::vector
<U
>> {
189 using T
= std::vector
<U
>;
191 template <typename V
>
192 static QueueStatus
Write(ProducerView
<V
>& aProducerView
, const T
& aArg
) {
193 auto status
= aProducerView
.WriteParam(aArg
.size());
194 if (!status
) return status
;
196 for (const auto& cur
: aArg
) {
197 status
= aProducerView
.WriteParam(cur
);
198 if (!status
) return status
;
203 template <typename V
>
204 static QueueStatus
Read(ConsumerView
<V
>& aConsumerView
, T
* aArg
) {
206 auto status
= aConsumerView
.ReadParam(&size
);
207 if (!status
) return status
;
210 for (auto& cur
: *aArg
) {
211 status
= aConsumerView
.ReadParam(&cur
);
212 if (!status
) return status
;
219 struct QueueParamTraits
<WebGLExtensionID
>
220 : public ContiguousEnumSerializer
<WebGLExtensionID
,
221 WebGLExtensionID::ANGLE_instanced_arrays
,
222 WebGLExtensionID::Max
> {};
225 struct QueueParamTraits
<CompileResult
> {
226 using T
= CompileResult
;
228 template <typename U
>
229 static QueueStatus
Write(ProducerView
<U
>& aProducerView
, const T
& aArg
) {
230 aProducerView
.WriteParam(aArg
.pending
);
231 aProducerView
.WriteParam(aArg
.log
);
232 aProducerView
.WriteParam(aArg
.translatedSource
);
233 return aProducerView
.WriteParam(aArg
.success
);
236 template <typename U
>
237 static QueueStatus
Read(ConsumerView
<U
>& aConsumerView
, T
* aArg
) {
238 aConsumerView
.ReadParam(&aArg
->pending
);
239 aConsumerView
.ReadParam(&aArg
->log
);
240 aConsumerView
.ReadParam(&aArg
->translatedSource
);
241 return aConsumerView
.ReadParam(&aArg
->success
);
246 struct QueueParamTraits
<mozilla::layers::TextureType
>
247 : public ContiguousEnumSerializer
<mozilla::layers::TextureType
,
248 mozilla::layers::TextureType::Unknown
,
249 mozilla::layers::TextureType::Last
> {};
252 struct QueueParamTraits
<mozilla::gfx::SurfaceFormat
>
253 : public ContiguousEnumSerializerInclusive
<
254 mozilla::gfx::SurfaceFormat
, mozilla::gfx::SurfaceFormat::B8G8R8A8
,
255 mozilla::gfx::SurfaceFormat::UNKNOWN
> {};
258 struct QueueParamTraits
<gfxAlphaType
>
259 : public ContiguousEnumSerializerInclusive
<
260 gfxAlphaType
, gfxAlphaType::Opaque
, gfxAlphaType::NonPremult
> {};
263 } // namespace mozilla
265 #endif // WEBGLQUEUEPARAMTRAITS_H_