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 // ---------------------------------------------------------------------
66 inline constexpr bool IsEnumCase(const webgl::AttribBaseType raw
) {
68 case webgl::AttribBaseType::Boolean
:
69 case webgl::AttribBaseType::Float
:
70 case webgl::AttribBaseType::Int
:
71 case webgl::AttribBaseType::Uint
:
76 static_assert(IsEnumCase(webgl::AttribBaseType(3)));
77 static_assert(!IsEnumCase(webgl::AttribBaseType(4)));
78 static_assert(!IsEnumCase(webgl::AttribBaseType(5)));
82 #define USE_IS_ENUM_CASE(T) \
84 struct QueueParamTraits<T> : QueueParamTraits_IsEnumCase<T> {};
86 USE_IS_ENUM_CASE(webgl::AttribBaseType
)
87 USE_IS_ENUM_CASE(webgl::ProvokingVertex
)
89 #undef USE_IS_ENUM_CASE
91 // ---------------------------------------------------------------------
92 // Custom QueueParamTraits
95 struct QueueParamTraits
<Span
<T
>> {
97 static bool Write(ProducerView
<U
>& view
, const Span
<T
>& in
) {
98 const auto& elemCount
= in
.size();
99 auto status
= view
.WriteParam(elemCount
);
100 if (!status
) return status
;
102 if (!elemCount
) return status
;
103 status
= view
.WriteFromRange(Range
<const T
>{in
});
108 template <typename U
>
109 static bool Read(ConsumerView
<U
>& view
, Span
<const T
>* const out
) {
110 size_t elemCount
= 0;
111 auto status
= view
.ReadParam(&elemCount
);
112 if (!status
) return status
;
119 auto data
= view
.template ReadRange
<const T
>(elemCount
);
120 if (!data
) return false;
127 struct QueueParamTraits
<webgl::ContextLossReason
>
128 : public ContiguousEnumSerializerInclusive
<
129 webgl::ContextLossReason
, webgl::ContextLossReason::None
,
130 webgl::ContextLossReason::Guilty
> {};
132 template <typename V
, typename E
>
133 struct QueueParamTraits
<Result
<V
, E
>> {
134 using T
= Result
<V
, E
>;
136 template <typename U
>
137 static bool Write(ProducerView
<U
>& aProducerView
, const T
& aArg
) {
138 const auto ok
= aArg
.isOk();
139 auto status
= aProducerView
.WriteParam(ok
);
140 if (!status
) return status
;
142 status
= aProducerView
.WriteParam(aArg
.unwrap());
144 status
= aProducerView
.WriteParam(aArg
.unwrapErr());
149 template <typename U
>
150 static bool Read(ConsumerView
<U
>& aConsumerView
, T
* aArg
) {
152 auto status
= aConsumerView
.ReadParam(&ok
);
153 if (!status
) return status
;
156 status
= aConsumerView
.ReadParam(&val
);
160 status
= aConsumerView
.ReadParam(&val
);
168 struct QueueParamTraits
<std::string
> {
169 using T
= std::string
;
171 template <typename U
>
172 static bool Write(ProducerView
<U
>& aProducerView
, const T
& aArg
) {
173 const auto size
= aArg
.size();
174 auto status
= aProducerView
.WriteParam(size
);
175 if (!status
) return status
;
176 status
= aProducerView
.WriteFromRange(Range
<const char>{aArg
.data(), size
});
180 template <typename U
>
181 static bool Read(ConsumerView
<U
>& aConsumerView
, T
* aArg
) {
183 auto status
= aConsumerView
.ReadParam(&size
);
184 if (!status
) return status
;
186 const auto view
= aConsumerView
.template ReadRange
<char>(size
);
187 if (!view
) return false;
188 aArg
->assign(view
->begin().get(), size
);
193 template <typename U
>
194 struct QueueParamTraits
<std::vector
<U
>> {
195 using T
= std::vector
<U
>;
197 template <typename V
>
198 static bool Write(ProducerView
<V
>& aProducerView
, const T
& aArg
) {
199 auto status
= aProducerView
.WriteParam(aArg
.size());
200 if (!status
) return status
;
202 for (const auto& cur
: aArg
) {
203 status
= aProducerView
.WriteParam(cur
);
204 if (!status
) return status
;
209 template <typename V
>
210 static bool Read(ConsumerView
<V
>& aConsumerView
, T
* aArg
) {
212 auto status
= aConsumerView
.ReadParam(&size
);
213 if (!status
) return status
;
216 for (auto& cur
: *aArg
) {
217 status
= aConsumerView
.ReadParam(&cur
);
218 if (!status
) return status
;
225 struct QueueParamTraits
<WebGLExtensionID
>
226 : public ContiguousEnumSerializer
<WebGLExtensionID
,
227 WebGLExtensionID::ANGLE_instanced_arrays
,
228 WebGLExtensionID::Max
> {};
231 struct QueueParamTraits
<CompileResult
> {
232 using T
= CompileResult
;
234 template <typename U
>
235 static bool Write(ProducerView
<U
>& aProducerView
, const T
& aArg
) {
236 aProducerView
.WriteParam(aArg
.pending
);
237 aProducerView
.WriteParam(aArg
.log
);
238 aProducerView
.WriteParam(aArg
.translatedSource
);
239 return aProducerView
.WriteParam(aArg
.success
);
242 template <typename U
>
243 static bool Read(ConsumerView
<U
>& aConsumerView
, T
* aArg
) {
244 aConsumerView
.ReadParam(&aArg
->pending
);
245 aConsumerView
.ReadParam(&aArg
->log
);
246 aConsumerView
.ReadParam(&aArg
->translatedSource
);
247 return aConsumerView
.ReadParam(&aArg
->success
);
252 struct QueueParamTraits
<mozilla::layers::TextureType
>
253 : public ContiguousEnumSerializer
<mozilla::layers::TextureType
,
254 mozilla::layers::TextureType::Unknown
,
255 mozilla::layers::TextureType::Last
> {};
258 struct QueueParamTraits
<mozilla::gfx::SurfaceFormat
>
259 : public ContiguousEnumSerializerInclusive
<
260 mozilla::gfx::SurfaceFormat
, mozilla::gfx::SurfaceFormat::B8G8R8A8
,
261 mozilla::gfx::SurfaceFormat::UNKNOWN
> {};
264 struct QueueParamTraits
<gfxAlphaType
>
265 : public ContiguousEnumSerializerInclusive
<
266 gfxAlphaType
, gfxAlphaType::Opaque
, gfxAlphaType::NonPremult
> {};
270 template <class Enum
>
271 using WebIDLEnumQueueSerializer
=
272 ContiguousEnumSerializerInclusive
<Enum
, ContiguousEnumValues
<Enum
>::min
,
273 ContiguousEnumValues
<Enum
>::max
>;
276 struct QueueParamTraits
<dom::WebGLPowerPreference
>
277 : public WebIDLEnumQueueSerializer
<dom::WebGLPowerPreference
> {};
279 struct QueueParamTraits
<dom::PredefinedColorSpace
>
280 : public WebIDLEnumQueueSerializer
<dom::PredefinedColorSpace
> {};
283 } // namespace mozilla
285 #endif // WEBGLQUEUEPARAMTRAITS_H_