Bug 1886946: Remove incorrect assertion that buffer is not-pinned. r=sfink
[gecko.git] / dom / canvas / WebGLQueueParamTraits.h
blob3c74f0875041dbc9dc47af8cbd9195d385f6c79b
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;
71 return false;
74 inline constexpr bool IsEnumCase(const dom::PredefinedColorSpace raw) {
75 switch (raw) {
76 case dom::PredefinedColorSpace::Srgb:
77 case dom::PredefinedColorSpace::Display_p3:
78 return true;
80 return false;
83 inline constexpr bool IsEnumCase(const webgl::AttribBaseType raw) {
84 switch (raw) {
85 case webgl::AttribBaseType::Boolean:
86 case webgl::AttribBaseType::Float:
87 case webgl::AttribBaseType::Int:
88 case webgl::AttribBaseType::Uint:
89 return true;
91 return false;
94 static_assert(IsEnumCase(dom::WebGLPowerPreference(2)));
95 static_assert(!IsEnumCase(dom::WebGLPowerPreference(3)));
96 static_assert(!IsEnumCase(dom::WebGLPowerPreference(5)));
98 #define USE_IS_ENUM_CASE(T) \
99 template <> \
100 struct QueueParamTraits<T> : QueueParamTraits_IsEnumCase<T> {};
102 USE_IS_ENUM_CASE(dom::WebGLPowerPreference)
103 USE_IS_ENUM_CASE(dom::PredefinedColorSpace)
104 USE_IS_ENUM_CASE(webgl::AttribBaseType)
105 USE_IS_ENUM_CASE(webgl::ProvokingVertex)
107 #undef USE_IS_ENUM_CASE
109 // ---------------------------------------------------------------------
110 // Custom QueueParamTraits
112 template <typename T>
113 struct QueueParamTraits<Span<T>> {
114 template <typename U>
115 static bool Write(ProducerView<U>& view, const Span<T>& in) {
116 const auto& elemCount = in.size();
117 auto status = view.WriteParam(elemCount);
118 if (!status) return status;
120 if (!elemCount) return status;
121 status = view.WriteFromRange(Range<const T>{in});
123 return status;
126 template <typename U>
127 static bool Read(ConsumerView<U>& view, Span<const T>* const out) {
128 size_t elemCount = 0;
129 auto status = view.ReadParam(&elemCount);
130 if (!status) return status;
132 if (!elemCount) {
133 *out = {};
134 return true;
137 auto data = view.template ReadRange<const T>(elemCount);
138 if (!data) return false;
139 *out = Span{*data};
140 return true;
144 template <>
145 struct QueueParamTraits<webgl::ContextLossReason>
146 : public ContiguousEnumSerializerInclusive<
147 webgl::ContextLossReason, webgl::ContextLossReason::None,
148 webgl::ContextLossReason::Guilty> {};
150 template <typename V, typename E>
151 struct QueueParamTraits<Result<V, E>> {
152 using T = Result<V, E>;
154 template <typename U>
155 static bool Write(ProducerView<U>& aProducerView, const T& aArg) {
156 const auto ok = aArg.isOk();
157 auto status = aProducerView.WriteParam(ok);
158 if (!status) return status;
159 if (ok) {
160 status = aProducerView.WriteParam(aArg.unwrap());
161 } else {
162 status = aProducerView.WriteParam(aArg.unwrapErr());
164 return status;
167 template <typename U>
168 static bool Read(ConsumerView<U>& aConsumerView, T* aArg) {
169 bool ok;
170 auto status = aConsumerView.ReadParam(&ok);
171 if (!status) return status;
172 if (ok) {
173 V val;
174 status = aConsumerView.ReadParam(&val);
175 *aArg = val;
176 } else {
177 E val;
178 status = aConsumerView.ReadParam(&val);
179 *aArg = Err(val);
181 return status;
185 template <>
186 struct QueueParamTraits<std::string> {
187 using T = std::string;
189 template <typename U>
190 static bool Write(ProducerView<U>& aProducerView, const T& aArg) {
191 const auto size = aArg.size();
192 auto status = aProducerView.WriteParam(size);
193 if (!status) return status;
194 status = aProducerView.WriteFromRange(Range<const char>{aArg.data(), size});
195 return status;
198 template <typename U>
199 static bool Read(ConsumerView<U>& aConsumerView, T* aArg) {
200 size_t size;
201 auto status = aConsumerView.ReadParam(&size);
202 if (!status) return status;
204 const auto view = aConsumerView.template ReadRange<char>(size);
205 if (!view) return false;
206 aArg->assign(view->begin().get(), size);
207 return status;
211 template <typename U>
212 struct QueueParamTraits<std::vector<U>> {
213 using T = std::vector<U>;
215 template <typename V>
216 static bool Write(ProducerView<V>& aProducerView, const T& aArg) {
217 auto status = aProducerView.WriteParam(aArg.size());
218 if (!status) return status;
220 for (const auto& cur : aArg) {
221 status = aProducerView.WriteParam(cur);
222 if (!status) return status;
224 return status;
227 template <typename V>
228 static bool Read(ConsumerView<V>& aConsumerView, T* aArg) {
229 size_t size;
230 auto status = aConsumerView.ReadParam(&size);
231 if (!status) return status;
232 aArg->resize(size);
234 for (auto& cur : *aArg) {
235 status = aConsumerView.ReadParam(&cur);
236 if (!status) return status;
238 return status;
242 template <>
243 struct QueueParamTraits<WebGLExtensionID>
244 : public ContiguousEnumSerializer<WebGLExtensionID,
245 WebGLExtensionID::ANGLE_instanced_arrays,
246 WebGLExtensionID::Max> {};
248 template <>
249 struct QueueParamTraits<CompileResult> {
250 using T = CompileResult;
252 template <typename U>
253 static bool Write(ProducerView<U>& aProducerView, const T& aArg) {
254 aProducerView.WriteParam(aArg.pending);
255 aProducerView.WriteParam(aArg.log);
256 aProducerView.WriteParam(aArg.translatedSource);
257 return aProducerView.WriteParam(aArg.success);
260 template <typename U>
261 static bool Read(ConsumerView<U>& aConsumerView, T* aArg) {
262 aConsumerView.ReadParam(&aArg->pending);
263 aConsumerView.ReadParam(&aArg->log);
264 aConsumerView.ReadParam(&aArg->translatedSource);
265 return aConsumerView.ReadParam(&aArg->success);
269 template <>
270 struct QueueParamTraits<mozilla::layers::TextureType>
271 : public ContiguousEnumSerializer<mozilla::layers::TextureType,
272 mozilla::layers::TextureType::Unknown,
273 mozilla::layers::TextureType::Last> {};
275 template <>
276 struct QueueParamTraits<mozilla::gfx::SurfaceFormat>
277 : public ContiguousEnumSerializerInclusive<
278 mozilla::gfx::SurfaceFormat, mozilla::gfx::SurfaceFormat::B8G8R8A8,
279 mozilla::gfx::SurfaceFormat::UNKNOWN> {};
281 template <>
282 struct QueueParamTraits<gfxAlphaType>
283 : public ContiguousEnumSerializerInclusive<
284 gfxAlphaType, gfxAlphaType::Opaque, gfxAlphaType::NonPremult> {};
286 } // namespace webgl
287 } // namespace mozilla
289 #endif // WEBGLQUEUEPARAMTRAITS_H_