Bug 1936278 - Prevent search mode chiclet from being dismissed when clicking in page...
[gecko.git] / dom / canvas / WebGLQueueParamTraits.h
blob1a348534eda22bc90e05cbfa6909cfcd4b763b52
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 } // namespace webgl
66 inline constexpr bool IsEnumCase(const webgl::AttribBaseType raw) {
67 switch (raw) {
68 case webgl::AttribBaseType::Boolean:
69 case webgl::AttribBaseType::Float:
70 case webgl::AttribBaseType::Int:
71 case webgl::AttribBaseType::Uint:
72 return true;
74 return false;
76 static_assert(IsEnumCase(webgl::AttribBaseType(3)));
77 static_assert(!IsEnumCase(webgl::AttribBaseType(4)));
78 static_assert(!IsEnumCase(webgl::AttribBaseType(5)));
80 namespace webgl {
82 #define USE_IS_ENUM_CASE(T) \
83 template <> \
84 struct QueueParamTraits<T> : QueueParamTraits_IsEnumCase<T> {};
86 USE_IS_ENUM_CASE(webgl::AttribBaseType)
87 USE_IS_ENUM_CASE(webgl::ProvokingVertex)
89 #undef USE_IS_ENUM_CASE
91 // ---------------------------------------------------------------------
92 // Custom QueueParamTraits
94 template <typename T>
95 struct QueueParamTraits<Span<T>> {
96 template <typename U>
97 static bool Write(ProducerView<U>& view, const Span<T>& in) {
98 const auto& elemCount = in.size();
99 auto status = view.WriteParam(elemCount);
100 if (!status) return status;
102 if (!elemCount) return status;
103 status = view.WriteFromRange(Range<const T>{in});
105 return status;
108 template <typename U>
109 static bool Read(ConsumerView<U>& view, Span<const T>* const out) {
110 size_t elemCount = 0;
111 auto status = view.ReadParam(&elemCount);
112 if (!status) return status;
114 if (!elemCount) {
115 *out = {};
116 return true;
119 auto data = view.template ReadRange<const T>(elemCount);
120 if (!data) return false;
121 *out = Span{*data};
122 return true;
126 template <>
127 struct QueueParamTraits<webgl::ContextLossReason>
128 : public ContiguousEnumSerializerInclusive<
129 webgl::ContextLossReason, webgl::ContextLossReason::None,
130 webgl::ContextLossReason::Guilty> {};
132 template <typename V, typename E>
133 struct QueueParamTraits<Result<V, E>> {
134 using T = Result<V, E>;
136 template <typename U>
137 static bool Write(ProducerView<U>& aProducerView, const T& aArg) {
138 const auto ok = aArg.isOk();
139 auto status = aProducerView.WriteParam(ok);
140 if (!status) return status;
141 if (ok) {
142 status = aProducerView.WriteParam(aArg.unwrap());
143 } else {
144 status = aProducerView.WriteParam(aArg.unwrapErr());
146 return status;
149 template <typename U>
150 static bool Read(ConsumerView<U>& aConsumerView, T* aArg) {
151 bool ok;
152 auto status = aConsumerView.ReadParam(&ok);
153 if (!status) return status;
154 if (ok) {
155 V val;
156 status = aConsumerView.ReadParam(&val);
157 *aArg = val;
158 } else {
159 E val;
160 status = aConsumerView.ReadParam(&val);
161 *aArg = Err(val);
163 return status;
167 template <>
168 struct QueueParamTraits<std::string> {
169 using T = std::string;
171 template <typename U>
172 static bool Write(ProducerView<U>& aProducerView, const T& aArg) {
173 const auto size = aArg.size();
174 auto status = aProducerView.WriteParam(size);
175 if (!status) return status;
176 status = aProducerView.WriteFromRange(Range<const char>{aArg.data(), size});
177 return status;
180 template <typename U>
181 static bool Read(ConsumerView<U>& aConsumerView, T* aArg) {
182 size_t size;
183 auto status = aConsumerView.ReadParam(&size);
184 if (!status) return status;
186 const auto view = aConsumerView.template ReadRange<char>(size);
187 if (!view) return false;
188 aArg->assign(view->begin().get(), size);
189 return status;
193 template <typename U>
194 struct QueueParamTraits<std::vector<U>> {
195 using T = std::vector<U>;
197 template <typename V>
198 static bool Write(ProducerView<V>& aProducerView, const T& aArg) {
199 auto status = aProducerView.WriteParam(aArg.size());
200 if (!status) return status;
202 for (const auto& cur : aArg) {
203 status = aProducerView.WriteParam(cur);
204 if (!status) return status;
206 return status;
209 template <typename V>
210 static bool Read(ConsumerView<V>& aConsumerView, T* aArg) {
211 size_t size;
212 auto status = aConsumerView.ReadParam(&size);
213 if (!status) return status;
214 aArg->resize(size);
216 for (auto& cur : *aArg) {
217 status = aConsumerView.ReadParam(&cur);
218 if (!status) return status;
220 return status;
224 template <>
225 struct QueueParamTraits<WebGLExtensionID>
226 : public ContiguousEnumSerializer<WebGLExtensionID,
227 WebGLExtensionID::ANGLE_instanced_arrays,
228 WebGLExtensionID::Max> {};
230 template <>
231 struct QueueParamTraits<CompileResult> {
232 using T = CompileResult;
234 template <typename U>
235 static bool Write(ProducerView<U>& aProducerView, const T& aArg) {
236 aProducerView.WriteParam(aArg.pending);
237 aProducerView.WriteParam(aArg.log);
238 aProducerView.WriteParam(aArg.translatedSource);
239 return aProducerView.WriteParam(aArg.success);
242 template <typename U>
243 static bool Read(ConsumerView<U>& aConsumerView, T* aArg) {
244 aConsumerView.ReadParam(&aArg->pending);
245 aConsumerView.ReadParam(&aArg->log);
246 aConsumerView.ReadParam(&aArg->translatedSource);
247 return aConsumerView.ReadParam(&aArg->success);
251 template <>
252 struct QueueParamTraits<mozilla::layers::TextureType>
253 : public ContiguousEnumSerializer<mozilla::layers::TextureType,
254 mozilla::layers::TextureType::Unknown,
255 mozilla::layers::TextureType::Last> {};
257 template <>
258 struct QueueParamTraits<mozilla::gfx::SurfaceFormat>
259 : public ContiguousEnumSerializerInclusive<
260 mozilla::gfx::SurfaceFormat, mozilla::gfx::SurfaceFormat::B8G8R8A8,
261 mozilla::gfx::SurfaceFormat::UNKNOWN> {};
263 template <>
264 struct QueueParamTraits<gfxAlphaType>
265 : public ContiguousEnumSerializerInclusive<
266 gfxAlphaType, gfxAlphaType::Opaque, gfxAlphaType::NonPremult> {};
268 // -
270 template <class Enum>
271 using WebIDLEnumQueueSerializer =
272 ContiguousEnumSerializerInclusive<Enum, ContiguousEnumValues<Enum>::min,
273 ContiguousEnumValues<Enum>::max>;
275 template <>
276 struct QueueParamTraits<dom::WebGLPowerPreference>
277 : public WebIDLEnumQueueSerializer<dom::WebGLPowerPreference> {};
278 template <>
279 struct QueueParamTraits<dom::PredefinedColorSpace>
280 : public WebIDLEnumQueueSerializer<dom::PredefinedColorSpace> {};
282 } // namespace webgl
283 } // namespace mozilla
285 #endif // WEBGLQUEUEPARAMTRAITS_H_