no bug - Bumping Firefox l10n changesets r=release a=l10n-bump DONTBUILD CLOSED TREE
[gecko.git] / dom / canvas / WebGLQueueParamTraits.h
blobdb3ef0a44b07781cdce03e09edab9042b91278a8
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"
16 namespace mozilla {
17 namespace webgl {
18 template <typename T>
19 struct QueueParamTraits;
21 // -
23 #define USE_TIED_FIELDS(T) \
24 template <> \
25 struct QueueParamTraits<T> : QueueParamTraits_TiedFields<T> {};
27 // -
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)
41 } // namespace webgl
42 template <>
43 inline auto TiedFields<gfx::IntSize>(gfx::IntSize& a) {
44 return std::tie(a.width, a.height);
46 namespace webgl {
47 USE_TIED_FIELDS(gfx::IntSize)
49 // -
51 #undef USE_TIED_FIELDS
53 // -
55 template <class T>
56 struct QueueParamTraits<avec2<T>> : QueueParamTraits_TiedFields<avec2<T>> {};
58 template <class T>
59 struct QueueParamTraits<avec3<T>> : QueueParamTraits_TiedFields<avec3<T>> {};
61 // ---------------------------------------------------------------------
62 // Enums!
64 inline constexpr bool IsEnumCase(const dom::WebGLPowerPreference raw) {
65 switch (raw) {
66 case dom::WebGLPowerPreference::Default:
67 case dom::WebGLPowerPreference::Low_power:
68 case dom::WebGLPowerPreference::High_performance:
69 return true;
70 case dom::WebGLPowerPreference::EndGuard_:
71 break;
73 return false;
76 inline constexpr bool IsEnumCase(const dom::PredefinedColorSpace raw) {
77 switch (raw) {
78 case dom::PredefinedColorSpace::Srgb:
79 case dom::PredefinedColorSpace::Display_p3:
80 return true;
81 case dom::PredefinedColorSpace::EndGuard_:
82 break;
84 return false;
87 inline constexpr bool IsEnumCase(const webgl::AttribBaseType raw) {
88 switch (raw) {
89 case webgl::AttribBaseType::Boolean:
90 case webgl::AttribBaseType::Float:
91 case webgl::AttribBaseType::Int:
92 case webgl::AttribBaseType::Uint:
93 return true;
95 return false;
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) \
103 template <> \
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());
129 return status;
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;
138 if (!elemCount) {
139 *out = {};
140 return true;
143 auto data = view.template ReadRange<T>(elemCount);
144 if (!data) return false;
145 *out = std::move(RawBuffer<T>{*data});
146 return true;
150 template <>
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;
165 if (ok) {
166 status = aProducerView.WriteParam(aArg.unwrap());
167 } else {
168 status = aProducerView.WriteParam(aArg.unwrapErr());
170 return status;
173 template <typename U>
174 static bool Read(ConsumerView<U>& aConsumerView, T* aArg) {
175 bool ok;
176 auto status = aConsumerView.ReadParam(&ok);
177 if (!status) return status;
178 if (ok) {
179 V val;
180 status = aConsumerView.ReadParam(&val);
181 *aArg = val;
182 } else {
183 E val;
184 status = aConsumerView.ReadParam(&val);
185 *aArg = Err(val);
187 return status;
191 template <>
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});
201 return status;
204 template <typename U>
205 static bool Read(ConsumerView<U>& aConsumerView, T* aArg) {
206 size_t size;
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);
213 return status;
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;
230 return status;
233 template <typename V>
234 static bool Read(ConsumerView<V>& aConsumerView, T* aArg) {
235 size_t size;
236 auto status = aConsumerView.ReadParam(&size);
237 if (!status) return status;
238 aArg->resize(size);
240 for (auto& cur : *aArg) {
241 status = aConsumerView.ReadParam(&cur);
242 if (!status) return status;
244 return status;
248 template <>
249 struct QueueParamTraits<WebGLExtensionID>
250 : public ContiguousEnumSerializer<WebGLExtensionID,
251 WebGLExtensionID::ANGLE_instanced_arrays,
252 WebGLExtensionID::Max> {};
254 template <>
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);
275 template <>
276 struct QueueParamTraits<mozilla::layers::TextureType>
277 : public ContiguousEnumSerializer<mozilla::layers::TextureType,
278 mozilla::layers::TextureType::Unknown,
279 mozilla::layers::TextureType::Last> {};
281 template <>
282 struct QueueParamTraits<mozilla::gfx::SurfaceFormat>
283 : public ContiguousEnumSerializerInclusive<
284 mozilla::gfx::SurfaceFormat, mozilla::gfx::SurfaceFormat::B8G8R8A8,
285 mozilla::gfx::SurfaceFormat::UNKNOWN> {};
287 template <>
288 struct QueueParamTraits<gfxAlphaType>
289 : public ContiguousEnumSerializerInclusive<
290 gfxAlphaType, gfxAlphaType::Opaque, gfxAlphaType::NonPremult> {};
292 } // namespace webgl
293 } // namespace mozilla
295 #endif // WEBGLQUEUEPARAMTRAITS_H_