Bug 1842773 - Part 5: Add ArrayBuffer.prototype.{maxByteLength,resizable} getters...
[gecko.git] / dom / canvas / WebGLQueueParamTraits.h
blob3be5a1b980b3284e200bf96fe6fe3503f475145e
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;
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());
134 return status;
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;
142 if (!elemCount) {
143 *out = {};
144 return true;
147 uint8_t hasData = 0;
148 status = view.ReadParam(&hasData);
149 if (!status) return status;
150 if (!hasData) {
151 auto temp = RawBuffer<T>{elemCount};
152 *out = std::move(temp);
153 return true;
156 auto data = view.template ReadRange<T>(elemCount);
157 if (!data) return false;
158 *out = std::move(RawBuffer<T>{*data});
159 return true;
163 template <>
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;
178 if (ok) {
179 status = aProducerView.WriteParam(aArg.unwrap());
180 } else {
181 status = aProducerView.WriteParam(aArg.unwrapErr());
183 return status;
186 template <typename U>
187 static bool Read(ConsumerView<U>& aConsumerView, T* aArg) {
188 bool ok;
189 auto status = aConsumerView.ReadParam(&ok);
190 if (!status) return status;
191 if (ok) {
192 V val;
193 status = aConsumerView.ReadParam(&val);
194 *aArg = val;
195 } else {
196 E val;
197 status = aConsumerView.ReadParam(&val);
198 *aArg = Err(val);
200 return status;
204 template <>
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});
214 return status;
217 template <typename U>
218 static bool Read(ConsumerView<U>& aConsumerView, T* aArg) {
219 size_t size;
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);
226 return status;
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;
243 return status;
246 template <typename V>
247 static bool Read(ConsumerView<V>& aConsumerView, T* aArg) {
248 size_t size;
249 auto status = aConsumerView.ReadParam(&size);
250 if (!status) return status;
251 aArg->resize(size);
253 for (auto& cur : *aArg) {
254 status = aConsumerView.ReadParam(&cur);
255 if (!status) return status;
257 return status;
261 template <>
262 struct QueueParamTraits<WebGLExtensionID>
263 : public ContiguousEnumSerializer<WebGLExtensionID,
264 WebGLExtensionID::ANGLE_instanced_arrays,
265 WebGLExtensionID::Max> {};
267 template <>
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);
288 template <>
289 struct QueueParamTraits<mozilla::layers::TextureType>
290 : public ContiguousEnumSerializer<mozilla::layers::TextureType,
291 mozilla::layers::TextureType::Unknown,
292 mozilla::layers::TextureType::Last> {};
294 template <>
295 struct QueueParamTraits<mozilla::gfx::SurfaceFormat>
296 : public ContiguousEnumSerializerInclusive<
297 mozilla::gfx::SurfaceFormat, mozilla::gfx::SurfaceFormat::B8G8R8A8,
298 mozilla::gfx::SurfaceFormat::UNKNOWN> {};
300 template <>
301 struct QueueParamTraits<gfxAlphaType>
302 : public ContiguousEnumSerializerInclusive<
303 gfxAlphaType, gfxAlphaType::Opaque, gfxAlphaType::NonPremult> {};
305 } // namespace webgl
306 } // namespace mozilla
308 #endif // WEBGLQUEUEPARAMTRAITS_H_