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
;
125 if (!elemCount
) return status
;
127 const auto& begin
= in
.begin();
128 const bool hasData
= static_cast<bool>(begin
);
129 status
= view
.WriteParam(hasData
);
130 if (!status
) return status
;
131 if (!hasData
) return status
;
133 status
= view
.WriteFromRange(in
.Data());
137 template <typename U
>
138 static bool Read(ConsumerView
<U
>& view
, ParamType
* const out
) {
139 size_t elemCount
= 0;
140 auto status
= view
.ReadParam(&elemCount
);
141 if (!status
) return status
;
148 status
= view
.ReadParam(&hasData
);
149 if (!status
) return status
;
151 auto temp
= RawBuffer
<T
>{elemCount
};
152 *out
= std::move(temp
);
156 auto data
= view
.template ReadRange
<T
>(elemCount
);
157 if (!data
) return false;
158 *out
= std::move(RawBuffer
<T
>{*data
});
164 struct QueueParamTraits
<webgl::ContextLossReason
>
165 : public ContiguousEnumSerializerInclusive
<
166 webgl::ContextLossReason
, webgl::ContextLossReason::None
,
167 webgl::ContextLossReason::Guilty
> {};
169 template <typename V
, typename E
>
170 struct QueueParamTraits
<Result
<V
, E
>> {
171 using T
= Result
<V
, E
>;
173 template <typename U
>
174 static bool Write(ProducerView
<U
>& aProducerView
, const T
& aArg
) {
175 const auto ok
= aArg
.isOk();
176 auto status
= aProducerView
.WriteParam(ok
);
177 if (!status
) return status
;
179 status
= aProducerView
.WriteParam(aArg
.unwrap());
181 status
= aProducerView
.WriteParam(aArg
.unwrapErr());
186 template <typename U
>
187 static bool Read(ConsumerView
<U
>& aConsumerView
, T
* aArg
) {
189 auto status
= aConsumerView
.ReadParam(&ok
);
190 if (!status
) return status
;
193 status
= aConsumerView
.ReadParam(&val
);
197 status
= aConsumerView
.ReadParam(&val
);
205 struct QueueParamTraits
<std::string
> {
206 using T
= std::string
;
208 template <typename U
>
209 static bool Write(ProducerView
<U
>& aProducerView
, const T
& aArg
) {
210 const auto size
= aArg
.size();
211 auto status
= aProducerView
.WriteParam(size
);
212 if (!status
) return status
;
213 status
= aProducerView
.WriteFromRange(Range
<const char>{aArg
.data(), size
});
217 template <typename U
>
218 static bool Read(ConsumerView
<U
>& aConsumerView
, T
* aArg
) {
220 auto status
= aConsumerView
.ReadParam(&size
);
221 if (!status
) return status
;
223 const auto view
= aConsumerView
.template ReadRange
<char>(size
);
224 if (!view
) return false;
225 aArg
->assign(view
->begin().get(), size
);
230 template <typename U
>
231 struct QueueParamTraits
<std::vector
<U
>> {
232 using T
= std::vector
<U
>;
234 template <typename V
>
235 static bool Write(ProducerView
<V
>& aProducerView
, const T
& aArg
) {
236 auto status
= aProducerView
.WriteParam(aArg
.size());
237 if (!status
) return status
;
239 for (const auto& cur
: aArg
) {
240 status
= aProducerView
.WriteParam(cur
);
241 if (!status
) return status
;
246 template <typename V
>
247 static bool Read(ConsumerView
<V
>& aConsumerView
, T
* aArg
) {
249 auto status
= aConsumerView
.ReadParam(&size
);
250 if (!status
) return status
;
253 for (auto& cur
: *aArg
) {
254 status
= aConsumerView
.ReadParam(&cur
);
255 if (!status
) return status
;
262 struct QueueParamTraits
<WebGLExtensionID
>
263 : public ContiguousEnumSerializer
<WebGLExtensionID
,
264 WebGLExtensionID::ANGLE_instanced_arrays
,
265 WebGLExtensionID::Max
> {};
268 struct QueueParamTraits
<CompileResult
> {
269 using T
= CompileResult
;
271 template <typename U
>
272 static bool Write(ProducerView
<U
>& aProducerView
, const T
& aArg
) {
273 aProducerView
.WriteParam(aArg
.pending
);
274 aProducerView
.WriteParam(aArg
.log
);
275 aProducerView
.WriteParam(aArg
.translatedSource
);
276 return aProducerView
.WriteParam(aArg
.success
);
279 template <typename U
>
280 static bool Read(ConsumerView
<U
>& aConsumerView
, T
* aArg
) {
281 aConsumerView
.ReadParam(&aArg
->pending
);
282 aConsumerView
.ReadParam(&aArg
->log
);
283 aConsumerView
.ReadParam(&aArg
->translatedSource
);
284 return aConsumerView
.ReadParam(&aArg
->success
);
289 struct QueueParamTraits
<mozilla::layers::TextureType
>
290 : public ContiguousEnumSerializer
<mozilla::layers::TextureType
,
291 mozilla::layers::TextureType::Unknown
,
292 mozilla::layers::TextureType::Last
> {};
295 struct QueueParamTraits
<mozilla::gfx::SurfaceFormat
>
296 : public ContiguousEnumSerializerInclusive
<
297 mozilla::gfx::SurfaceFormat
, mozilla::gfx::SurfaceFormat::B8G8R8A8
,
298 mozilla::gfx::SurfaceFormat::UNKNOWN
> {};
301 struct QueueParamTraits
<gfxAlphaType
>
302 : public ContiguousEnumSerializerInclusive
<
303 gfxAlphaType
, gfxAlphaType::Opaque
, gfxAlphaType::NonPremult
> {};
306 } // namespace mozilla
308 #endif // WEBGLQUEUEPARAMTRAITS_H_