2 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
3 /* This Source Code Form is subject to the terms of the Mozilla Public
4 * License, v. 2.0. If a copy of the MPL was not distributed with this
5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
7 #ifndef WEBGLQUEUEPARAMTRAITS_H_
8 #define WEBGLQUEUEPARAMTRAITS_H_
10 #include <type_traits>
12 #include "ipc/EnumSerializer.h"
13 #include "TexUnpackBlob.h"
14 #include "WebGLTypes.h"
19 struct QueueParamTraits
;
23 #define USE_TIED_FIELDS(T) \
25 struct QueueParamTraits<T> : QueueParamTraits_TiedFields<T> {};
29 USE_TIED_FIELDS(layers::RemoteTextureId
)
30 USE_TIED_FIELDS(layers::RemoteTextureOwnerId
)
31 USE_TIED_FIELDS(WebGLContextOptions
)
32 USE_TIED_FIELDS(webgl::PixelUnpackStateWebgl
)
33 USE_TIED_FIELDS(webgl::SwapChainOptions
)
34 USE_TIED_FIELDS(webgl::ReadPixelsDesc
)
35 USE_TIED_FIELDS(webgl::VertAttribPointerDesc
)
36 USE_TIED_FIELDS(webgl::PackingInfo
)
37 USE_TIED_FIELDS(webgl::TypedQuad
)
38 USE_TIED_FIELDS(webgl::PixelPackingState
)
39 USE_TIED_FIELDS(FloatOrInt
)
43 inline auto TiedFields
<gfx::IntSize
>(gfx::IntSize
& a
) {
44 return std::tie(a
.width
, a
.height
);
47 USE_TIED_FIELDS(gfx::IntSize
)
51 #undef USE_TIED_FIELDS
56 struct QueueParamTraits
<avec2
<T
>> : QueueParamTraits_TiedFields
<avec2
<T
>> {};
59 struct QueueParamTraits
<avec3
<T
>> : QueueParamTraits_TiedFields
<avec3
<T
>> {};
61 // ---------------------------------------------------------------------
64 inline constexpr bool IsEnumCase(const dom::WebGLPowerPreference raw
) {
66 case dom::WebGLPowerPreference::Default
:
67 case dom::WebGLPowerPreference::Low_power
:
68 case dom::WebGLPowerPreference::High_performance
:
74 inline constexpr bool IsEnumCase(const dom::PredefinedColorSpace raw
) {
76 case dom::PredefinedColorSpace::Srgb
:
77 case dom::PredefinedColorSpace::Display_p3
:
83 inline constexpr bool IsEnumCase(const webgl::AttribBaseType raw
) {
85 case webgl::AttribBaseType::Boolean
:
86 case webgl::AttribBaseType::Float
:
87 case webgl::AttribBaseType::Int
:
88 case webgl::AttribBaseType::Uint
:
94 static_assert(IsEnumCase(dom::WebGLPowerPreference(2)));
95 static_assert(!IsEnumCase(dom::WebGLPowerPreference(3)));
96 static_assert(!IsEnumCase(dom::WebGLPowerPreference(5)));
98 #define USE_IS_ENUM_CASE(T) \
100 struct QueueParamTraits<T> : QueueParamTraits_IsEnumCase<T> {};
102 USE_IS_ENUM_CASE(dom::WebGLPowerPreference
)
103 USE_IS_ENUM_CASE(dom::PredefinedColorSpace
)
104 USE_IS_ENUM_CASE(webgl::AttribBaseType
)
105 USE_IS_ENUM_CASE(webgl::ProvokingVertex
)
107 #undef USE_IS_ENUM_CASE
109 // ---------------------------------------------------------------------
110 // Custom QueueParamTraits
112 template <typename T
>
113 struct QueueParamTraits
<Span
<T
>> {
114 template <typename U
>
115 static bool Write(ProducerView
<U
>& view
, const Span
<T
>& in
) {
116 const auto& elemCount
= in
.size();
117 auto status
= view
.WriteParam(elemCount
);
118 if (!status
) return status
;
120 if (!elemCount
) return status
;
121 status
= view
.WriteFromRange(Range
<const T
>{in
});
126 template <typename U
>
127 static bool Read(ConsumerView
<U
>& view
, Span
<const T
>* const out
) {
128 size_t elemCount
= 0;
129 auto status
= view
.ReadParam(&elemCount
);
130 if (!status
) return status
;
137 auto data
= view
.template ReadRange
<const T
>(elemCount
);
138 if (!data
) return false;
145 struct QueueParamTraits
<webgl::ContextLossReason
>
146 : public ContiguousEnumSerializerInclusive
<
147 webgl::ContextLossReason
, webgl::ContextLossReason::None
,
148 webgl::ContextLossReason::Guilty
> {};
150 template <typename V
, typename E
>
151 struct QueueParamTraits
<Result
<V
, E
>> {
152 using T
= Result
<V
, E
>;
154 template <typename U
>
155 static bool Write(ProducerView
<U
>& aProducerView
, const T
& aArg
) {
156 const auto ok
= aArg
.isOk();
157 auto status
= aProducerView
.WriteParam(ok
);
158 if (!status
) return status
;
160 status
= aProducerView
.WriteParam(aArg
.unwrap());
162 status
= aProducerView
.WriteParam(aArg
.unwrapErr());
167 template <typename U
>
168 static bool Read(ConsumerView
<U
>& aConsumerView
, T
* aArg
) {
170 auto status
= aConsumerView
.ReadParam(&ok
);
171 if (!status
) return status
;
174 status
= aConsumerView
.ReadParam(&val
);
178 status
= aConsumerView
.ReadParam(&val
);
186 struct QueueParamTraits
<std::string
> {
187 using T
= std::string
;
189 template <typename U
>
190 static bool Write(ProducerView
<U
>& aProducerView
, const T
& aArg
) {
191 const auto size
= aArg
.size();
192 auto status
= aProducerView
.WriteParam(size
);
193 if (!status
) return status
;
194 status
= aProducerView
.WriteFromRange(Range
<const char>{aArg
.data(), size
});
198 template <typename U
>
199 static bool Read(ConsumerView
<U
>& aConsumerView
, T
* aArg
) {
201 auto status
= aConsumerView
.ReadParam(&size
);
202 if (!status
) return status
;
204 const auto view
= aConsumerView
.template ReadRange
<char>(size
);
205 if (!view
) return false;
206 aArg
->assign(view
->begin().get(), size
);
211 template <typename U
>
212 struct QueueParamTraits
<std::vector
<U
>> {
213 using T
= std::vector
<U
>;
215 template <typename V
>
216 static bool Write(ProducerView
<V
>& aProducerView
, const T
& aArg
) {
217 auto status
= aProducerView
.WriteParam(aArg
.size());
218 if (!status
) return status
;
220 for (const auto& cur
: aArg
) {
221 status
= aProducerView
.WriteParam(cur
);
222 if (!status
) return status
;
227 template <typename V
>
228 static bool Read(ConsumerView
<V
>& aConsumerView
, T
* aArg
) {
230 auto status
= aConsumerView
.ReadParam(&size
);
231 if (!status
) return status
;
234 for (auto& cur
: *aArg
) {
235 status
= aConsumerView
.ReadParam(&cur
);
236 if (!status
) return status
;
243 struct QueueParamTraits
<WebGLExtensionID
>
244 : public ContiguousEnumSerializer
<WebGLExtensionID
,
245 WebGLExtensionID::ANGLE_instanced_arrays
,
246 WebGLExtensionID::Max
> {};
249 struct QueueParamTraits
<CompileResult
> {
250 using T
= CompileResult
;
252 template <typename U
>
253 static bool Write(ProducerView
<U
>& aProducerView
, const T
& aArg
) {
254 aProducerView
.WriteParam(aArg
.pending
);
255 aProducerView
.WriteParam(aArg
.log
);
256 aProducerView
.WriteParam(aArg
.translatedSource
);
257 return aProducerView
.WriteParam(aArg
.success
);
260 template <typename U
>
261 static bool Read(ConsumerView
<U
>& aConsumerView
, T
* aArg
) {
262 aConsumerView
.ReadParam(&aArg
->pending
);
263 aConsumerView
.ReadParam(&aArg
->log
);
264 aConsumerView
.ReadParam(&aArg
->translatedSource
);
265 return aConsumerView
.ReadParam(&aArg
->success
);
270 struct QueueParamTraits
<mozilla::layers::TextureType
>
271 : public ContiguousEnumSerializer
<mozilla::layers::TextureType
,
272 mozilla::layers::TextureType::Unknown
,
273 mozilla::layers::TextureType::Last
> {};
276 struct QueueParamTraits
<mozilla::gfx::SurfaceFormat
>
277 : public ContiguousEnumSerializerInclusive
<
278 mozilla::gfx::SurfaceFormat
, mozilla::gfx::SurfaceFormat::B8G8R8A8
,
279 mozilla::gfx::SurfaceFormat::UNKNOWN
> {};
282 struct QueueParamTraits
<gfxAlphaType
>
283 : public ContiguousEnumSerializerInclusive
<
284 gfxAlphaType
, gfxAlphaType::Opaque
, gfxAlphaType::NonPremult
> {};
287 } // namespace mozilla
289 #endif // WEBGLQUEUEPARAMTRAITS_H_