Bug 1700051: part 26) Correct typo in comment of `mozInlineSpellWordUtil::BuildSoftTe...
[gecko.git] / dom / canvas / WebGLQueueParamTraits.h
blobddf383583ca165b7039e42866c41178a606c24c4
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* This Source Code Form is subject to the terms of the Mozilla Public
3 * License, v. 2.0. If a copy of the MPL was not distributed with this
4 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
6 #ifndef WEBGLQUEUEPARAMTRAITS_H_
7 #define WEBGLQUEUEPARAMTRAITS_H_
9 #include <type_traits>
11 #include "ipc/EnumSerializer.h"
12 #include "TexUnpackBlob.h"
13 #include "WebGLContext.h"
14 #include "WebGLTypes.h"
16 namespace mozilla {
18 namespace webgl {
19 template <typename T>
20 struct QueueParamTraits;
22 template <>
23 struct IsTriviallySerializable<FloatOrInt> : std::true_type {};
25 template <>
26 struct IsTriviallySerializable<webgl::ShaderPrecisionFormat> : std::true_type {
29 template <>
30 struct IsTriviallySerializable<WebGLContextOptions> : std::true_type {};
32 template <>
33 struct IsTriviallySerializable<WebGLPixelStore> : std::true_type {};
35 template <>
36 struct IsTriviallySerializable<WebGLTexImageData> : std::true_type {};
38 template <>
39 struct IsTriviallySerializable<WebGLTexPboOffset> : std::true_type {};
41 template <>
42 struct IsTriviallySerializable<webgl::ExtensionBits> : std::true_type {};
43 template <>
44 struct IsTriviallySerializable<webgl::GetUniformData> : std::true_type {};
46 template <>
47 struct IsTriviallySerializable<mozilla::webgl::PackingInfo> : std::true_type {};
49 template <>
50 struct IsTriviallySerializable<ICRData> : std::true_type {};
52 template <>
53 struct IsTriviallySerializable<gfx::IntSize> : std::true_type {};
55 template <typename T>
56 struct IsTriviallySerializable<avec2<T>> : std::true_type {};
57 template <typename T>
58 struct IsTriviallySerializable<avec3<T>> : std::true_type {};
60 template <>
61 struct IsTriviallySerializable<webgl::TexUnpackBlob> : std::true_type {};
63 template <>
64 struct IsTriviallySerializable<webgl::TypedQuad> : std::true_type {};
65 template <>
66 struct IsTriviallySerializable<webgl::VertAttribPointerDesc> : std::true_type {
68 template <>
69 struct IsTriviallySerializable<webgl::ReadPixelsDesc> : std::true_type {};
70 template <>
71 struct IsTriviallySerializable<layers::SurfaceDescriptor> : std::true_type {};
73 template <typename T>
74 struct QueueParamTraits<RawBuffer<T>> {
75 using ParamType = RawBuffer<T>;
77 template <typename U>
78 static QueueStatus Write(ProducerView<U>& view, const ParamType& in) {
79 const auto& elemCount = in.size();
80 auto status = view.WriteParam(elemCount);
81 if (!status) return status;
82 if (!elemCount) return status;
84 const auto& begin = in.begin();
85 const bool hasData = static_cast<bool>(begin);
86 status = view.WriteParam(hasData);
87 if (!status) return status;
88 if (!hasData) return status;
90 status = view.WriteFromRange(in.Data());
91 return status;
94 template <typename U>
95 static QueueStatus Read(ConsumerView<U>& view, ParamType* const out) {
96 size_t elemCount = 0;
97 auto status = view.ReadParam(&elemCount);
98 if (!status) return status;
99 if (!elemCount) {
100 *out = {};
101 return QueueStatus::kSuccess;
104 uint8_t hasData = 0;
105 status = view.ReadParam(&hasData);
106 if (!status) return status;
107 if (!hasData) {
108 auto temp = RawBuffer<T>{elemCount};
109 *out = std::move(temp);
110 return QueueStatus::kSuccess;
113 auto data = view.template ReadRange<T>(elemCount);
114 if (!data) return QueueStatus::kTooSmall;
115 *out = std::move(RawBuffer<T>{*data});
116 return QueueStatus::kSuccess;
120 template <>
121 struct QueueParamTraits<webgl::ContextLossReason>
122 : public ContiguousEnumSerializerInclusive<
123 webgl::ContextLossReason, webgl::ContextLossReason::None,
124 webgl::ContextLossReason::Guilty> {};
126 template <typename V, typename E>
127 struct QueueParamTraits<Result<V, E>> {
128 using T = Result<V, E>;
130 template <typename U>
131 static QueueStatus Write(ProducerView<U>& aProducerView, const T& aArg) {
132 const auto ok = aArg.isOk();
133 auto status = aProducerView.WriteParam(ok);
134 if (!status) return status;
135 if (ok) {
136 status = aProducerView.WriteParam(aArg.unwrap());
137 } else {
138 status = aProducerView.WriteParam(aArg.unwrapErr());
140 return status;
143 template <typename U>
144 static QueueStatus Read(ConsumerView<U>& aConsumerView, T* aArg) {
145 bool ok;
146 auto status = aConsumerView.ReadParam(&ok);
147 if (!status) return status;
148 if (ok) {
149 V val;
150 status = aConsumerView.ReadParam(&val);
151 *aArg = val;
152 } else {
153 E val;
154 status = aConsumerView.ReadParam(&val);
155 *aArg = Err(val);
157 return status;
161 template <>
162 struct QueueParamTraits<std::string> {
163 using T = std::string;
165 template <typename U>
166 static QueueStatus Write(ProducerView<U>& aProducerView, const T& aArg) {
167 const auto size = aArg.size();
168 auto status = aProducerView.WriteParam(size);
169 if (!status) return status;
170 status = aProducerView.WriteFromRange(Range<const char>{aArg.data(), size});
171 return status;
174 template <typename U>
175 static QueueStatus Read(ConsumerView<U>& aConsumerView, T* aArg) {
176 size_t size;
177 auto status = aConsumerView.ReadParam(&size);
178 if (!status) return status;
180 const auto view = aConsumerView.template ReadRange<char>(size);
181 if (!view) return QueueStatus::kFatalError;
182 aArg->assign(view->begin().get(), size);
183 return status;
187 template <typename U>
188 struct QueueParamTraits<std::vector<U>> {
189 using T = std::vector<U>;
191 template <typename V>
192 static QueueStatus Write(ProducerView<V>& aProducerView, const T& aArg) {
193 auto status = aProducerView.WriteParam(aArg.size());
194 if (!status) return status;
196 for (const auto& cur : aArg) {
197 status = aProducerView.WriteParam(cur);
198 if (!status) return status;
200 return status;
203 template <typename V>
204 static QueueStatus Read(ConsumerView<V>& aConsumerView, T* aArg) {
205 size_t size;
206 auto status = aConsumerView.ReadParam(&size);
207 if (!status) return status;
208 aArg->resize(size);
210 for (auto& cur : *aArg) {
211 status = aConsumerView.ReadParam(&cur);
212 if (!status) return status;
214 return status;
218 template <>
219 struct QueueParamTraits<WebGLExtensionID>
220 : public ContiguousEnumSerializer<WebGLExtensionID,
221 WebGLExtensionID::ANGLE_instanced_arrays,
222 WebGLExtensionID::Max> {};
224 template <>
225 struct QueueParamTraits<CompileResult> {
226 using T = CompileResult;
228 template <typename U>
229 static QueueStatus Write(ProducerView<U>& aProducerView, const T& aArg) {
230 aProducerView.WriteParam(aArg.pending);
231 aProducerView.WriteParam(aArg.log);
232 aProducerView.WriteParam(aArg.translatedSource);
233 return aProducerView.WriteParam(aArg.success);
236 template <typename U>
237 static QueueStatus Read(ConsumerView<U>& aConsumerView, T* aArg) {
238 aConsumerView.ReadParam(&aArg->pending);
239 aConsumerView.ReadParam(&aArg->log);
240 aConsumerView.ReadParam(&aArg->translatedSource);
241 return aConsumerView.ReadParam(&aArg->success);
245 template <>
246 struct QueueParamTraits<mozilla::layers::TextureType>
247 : public ContiguousEnumSerializer<mozilla::layers::TextureType,
248 mozilla::layers::TextureType::Unknown,
249 mozilla::layers::TextureType::Last> {};
251 template <>
252 struct QueueParamTraits<mozilla::gfx::SurfaceFormat>
253 : public ContiguousEnumSerializerInclusive<
254 mozilla::gfx::SurfaceFormat, mozilla::gfx::SurfaceFormat::B8G8R8A8,
255 mozilla::gfx::SurfaceFormat::UNKNOWN> {};
257 template <>
258 struct QueueParamTraits<gfxAlphaType>
259 : public ContiguousEnumSerializerInclusive<
260 gfxAlphaType, gfxAlphaType::Opaque, gfxAlphaType::NonPremult> {};
262 } // namespace webgl
263 } // namespace mozilla
265 #endif // WEBGLQUEUEPARAMTRAITS_H_