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
:
70 case dom::WebGLPowerPreference::EndGuard_
:
76 inline constexpr bool IsEnumCase(const dom::PredefinedColorSpace raw
) {
78 case dom::PredefinedColorSpace::Srgb
:
79 case dom::PredefinedColorSpace::Display_p3
:
81 case dom::PredefinedColorSpace::EndGuard_
:
87 inline constexpr bool IsEnumCase(const webgl::AttribBaseType raw
) {
89 case webgl::AttribBaseType::Boolean
:
90 case webgl::AttribBaseType::Float
:
91 case webgl::AttribBaseType::Int
:
92 case webgl::AttribBaseType::Uint
:
98 static_assert(IsEnumCase(dom::WebGLPowerPreference(2)));
99 static_assert(!IsEnumCase(dom::WebGLPowerPreference(3)));
100 static_assert(!IsEnumCase(dom::WebGLPowerPreference(5)));
102 #define USE_IS_ENUM_CASE(T) \
104 struct QueueParamTraits<T> : QueueParamTraits_IsEnumCase<T> {};
106 USE_IS_ENUM_CASE(dom::WebGLPowerPreference
)
107 USE_IS_ENUM_CASE(dom::PredefinedColorSpace
)
108 USE_IS_ENUM_CASE(webgl::AttribBaseType
)
109 USE_IS_ENUM_CASE(webgl::ProvokingVertex
)
111 #undef USE_IS_ENUM_CASE
113 // ---------------------------------------------------------------------
114 // Custom QueueParamTraits
116 template <typename T
>
117 struct QueueParamTraits
<RawBuffer
<T
>> {
118 using ParamType
= RawBuffer
<T
>;
120 template <typename U
>
121 static bool Write(ProducerView
<U
>& view
, const ParamType
& in
) {
122 const auto& elemCount
= in
.size();
123 auto status
= view
.WriteParam(elemCount
);
124 if (!status
) return status
;
126 if (!elemCount
) return status
;
127 status
= view
.WriteFromRange(in
.Data());
132 template <typename U
>
133 static bool Read(ConsumerView
<U
>& view
, ParamType
* const out
) {
134 size_t elemCount
= 0;
135 auto status
= view
.ReadParam(&elemCount
);
136 if (!status
) return status
;
143 auto data
= view
.template ReadRange
<T
>(elemCount
);
144 if (!data
) return false;
145 *out
= std::move(RawBuffer
<T
>{*data
});
151 struct QueueParamTraits
<webgl::ContextLossReason
>
152 : public ContiguousEnumSerializerInclusive
<
153 webgl::ContextLossReason
, webgl::ContextLossReason::None
,
154 webgl::ContextLossReason::Guilty
> {};
156 template <typename V
, typename E
>
157 struct QueueParamTraits
<Result
<V
, E
>> {
158 using T
= Result
<V
, E
>;
160 template <typename U
>
161 static bool Write(ProducerView
<U
>& aProducerView
, const T
& aArg
) {
162 const auto ok
= aArg
.isOk();
163 auto status
= aProducerView
.WriteParam(ok
);
164 if (!status
) return status
;
166 status
= aProducerView
.WriteParam(aArg
.unwrap());
168 status
= aProducerView
.WriteParam(aArg
.unwrapErr());
173 template <typename U
>
174 static bool Read(ConsumerView
<U
>& aConsumerView
, T
* aArg
) {
176 auto status
= aConsumerView
.ReadParam(&ok
);
177 if (!status
) return status
;
180 status
= aConsumerView
.ReadParam(&val
);
184 status
= aConsumerView
.ReadParam(&val
);
192 struct QueueParamTraits
<std::string
> {
193 using T
= std::string
;
195 template <typename U
>
196 static bool Write(ProducerView
<U
>& aProducerView
, const T
& aArg
) {
197 const auto size
= aArg
.size();
198 auto status
= aProducerView
.WriteParam(size
);
199 if (!status
) return status
;
200 status
= aProducerView
.WriteFromRange(Range
<const char>{aArg
.data(), size
});
204 template <typename U
>
205 static bool Read(ConsumerView
<U
>& aConsumerView
, T
* aArg
) {
207 auto status
= aConsumerView
.ReadParam(&size
);
208 if (!status
) return status
;
210 const auto view
= aConsumerView
.template ReadRange
<char>(size
);
211 if (!view
) return false;
212 aArg
->assign(view
->begin().get(), size
);
217 template <typename U
>
218 struct QueueParamTraits
<std::vector
<U
>> {
219 using T
= std::vector
<U
>;
221 template <typename V
>
222 static bool Write(ProducerView
<V
>& aProducerView
, const T
& aArg
) {
223 auto status
= aProducerView
.WriteParam(aArg
.size());
224 if (!status
) return status
;
226 for (const auto& cur
: aArg
) {
227 status
= aProducerView
.WriteParam(cur
);
228 if (!status
) return status
;
233 template <typename V
>
234 static bool Read(ConsumerView
<V
>& aConsumerView
, T
* aArg
) {
236 auto status
= aConsumerView
.ReadParam(&size
);
237 if (!status
) return status
;
240 for (auto& cur
: *aArg
) {
241 status
= aConsumerView
.ReadParam(&cur
);
242 if (!status
) return status
;
249 struct QueueParamTraits
<WebGLExtensionID
>
250 : public ContiguousEnumSerializer
<WebGLExtensionID
,
251 WebGLExtensionID::ANGLE_instanced_arrays
,
252 WebGLExtensionID::Max
> {};
255 struct QueueParamTraits
<CompileResult
> {
256 using T
= CompileResult
;
258 template <typename U
>
259 static bool Write(ProducerView
<U
>& aProducerView
, const T
& aArg
) {
260 aProducerView
.WriteParam(aArg
.pending
);
261 aProducerView
.WriteParam(aArg
.log
);
262 aProducerView
.WriteParam(aArg
.translatedSource
);
263 return aProducerView
.WriteParam(aArg
.success
);
266 template <typename U
>
267 static bool Read(ConsumerView
<U
>& aConsumerView
, T
* aArg
) {
268 aConsumerView
.ReadParam(&aArg
->pending
);
269 aConsumerView
.ReadParam(&aArg
->log
);
270 aConsumerView
.ReadParam(&aArg
->translatedSource
);
271 return aConsumerView
.ReadParam(&aArg
->success
);
276 struct QueueParamTraits
<mozilla::layers::TextureType
>
277 : public ContiguousEnumSerializer
<mozilla::layers::TextureType
,
278 mozilla::layers::TextureType::Unknown
,
279 mozilla::layers::TextureType::Last
> {};
282 struct QueueParamTraits
<mozilla::gfx::SurfaceFormat
>
283 : public ContiguousEnumSerializerInclusive
<
284 mozilla::gfx::SurfaceFormat
, mozilla::gfx::SurfaceFormat::B8G8R8A8
,
285 mozilla::gfx::SurfaceFormat::UNKNOWN
> {};
288 struct QueueParamTraits
<gfxAlphaType
>
289 : public ContiguousEnumSerializerInclusive
<
290 gfxAlphaType
, gfxAlphaType::Opaque
, gfxAlphaType::NonPremult
> {};
293 } // namespace mozilla
295 #endif // WEBGLQUEUEPARAMTRAITS_H_