1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
|
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
#ifndef WEBGLIPDL_H_
#define WEBGLIPDL_H_
#include "gfxTypes.h"
#include "ipc/EnumSerializer.h"
#include "ipc/IPCMessageUtils.h"
#include "mozilla/GfxMessageUtils.h"
#include "mozilla/dom/BindingIPCUtils.h"
#include "mozilla/ipc/IPDLParamTraits.h"
#include "mozilla/ipc/Shmem.h"
#include "mozilla/layers/LayersSurfaces.h"
#include "TiedFields.h"
#include "TupleUtils.h"
#include "WebGLTypes.h"
namespace mozilla {
namespace webgl {
// TODO: This should probably replace Shmem, or at least this should move to
// ipc/glue.
class RaiiShmem final {
RefPtr<mozilla::ipc::ActorLifecycleProxy> mWeakRef;
mozilla::ipc::Shmem mShmem = {};
public:
/// Returns zeroed data.
static RaiiShmem Alloc(mozilla::ipc::IProtocol* const allocator,
const size_t size) {
mozilla::ipc::Shmem shmem;
if (!allocator->AllocShmem(size, &shmem)) return {};
return {allocator, shmem};
}
static RaiiShmem AllocUnsafe(mozilla::ipc::IProtocol* const allocator,
const size_t size) {
mozilla::ipc::Shmem shmem;
if (!allocator->AllocUnsafeShmem(size, &shmem)) return {};
return {allocator, shmem};
}
// -
RaiiShmem() = default;
RaiiShmem(mozilla::ipc::IProtocol* const allocator,
const mozilla::ipc::Shmem& shmem) {
if (!allocator || !allocator->CanSend()) {
return;
}
// Shmems are handled by the top-level, so use that or we might leak after
// the actor dies.
mWeakRef = allocator->ToplevelProtocol()->GetLifecycleProxy();
mShmem = shmem;
if (!mWeakRef || !mWeakRef->Get() || !IsShmem()) {
reset();
}
}
void reset() {
if (IsShmem()) {
const auto& allocator = mWeakRef->Get();
if (allocator) {
allocator->DeallocShmem(mShmem);
}
}
mWeakRef = nullptr;
mShmem = {};
}
~RaiiShmem() { reset(); }
// -
RaiiShmem(RaiiShmem&& rhs) { *this = std::move(rhs); }
RaiiShmem& operator=(RaiiShmem&& rhs) {
reset();
mWeakRef = rhs.mWeakRef;
mShmem = rhs.Extract();
return *this;
}
// -
bool IsShmem() const { return mShmem.IsReadable(); }
explicit operator bool() const { return IsShmem(); }
// -
const auto& Shmem() const {
MOZ_ASSERT(IsShmem());
return mShmem;
}
Range<uint8_t> ByteRange() const {
if (!IsShmem()) {
return {};
}
return {mShmem.get<uint8_t>(), mShmem.Size<uint8_t>()};
}
mozilla::ipc::Shmem Extract() {
auto ret = mShmem;
mShmem = {};
reset();
return ret;
}
};
using Int32Vector = std::vector<int32_t>;
} // namespace webgl
namespace ipc {
template <>
struct IPDLParamTraits<mozilla::webgl::FrontBufferSnapshotIpc> final {
using T = mozilla::webgl::FrontBufferSnapshotIpc;
static void Write(IPC::MessageWriter* const writer, IProtocol* actor, T& in) {
WriteParam(writer, in.surfSize);
WriteIPDLParam(writer, actor, std::move(in.shmem));
}
static bool Read(IPC::MessageReader* const reader, IProtocol* actor,
T* const out) {
return ReadParam(reader, &out->surfSize) &&
ReadIPDLParam(reader, actor, &out->shmem);
}
};
// -
template <>
struct IPDLParamTraits<mozilla::webgl::ReadPixelsResultIpc> final {
using T = mozilla::webgl::ReadPixelsResultIpc;
static void Write(IPC::MessageWriter* const writer, IProtocol* actor, T& in) {
WriteParam(writer, in.subrect);
WriteParam(writer, in.byteStride);
WriteIPDLParam(writer, actor, std::move(in.shmem));
}
static bool Read(IPC::MessageReader* const reader, IProtocol* actor,
T* const out) {
return ReadParam(reader, &out->subrect) &&
ReadParam(reader, &out->byteStride) &&
ReadIPDLParam(reader, actor, &out->shmem);
}
};
// -
template <>
struct IPDLParamTraits<mozilla::webgl::TexUnpackBlobDesc> final {
using T = mozilla::webgl::TexUnpackBlobDesc;
static void Write(IPC::MessageWriter* const writer, IProtocol* actor,
T&& in) {
WriteParam(writer, in.imageTarget);
WriteParam(writer, in.size);
WriteParam(writer, in.srcAlphaType);
MOZ_RELEASE_ASSERT(!in.cpuData);
MOZ_RELEASE_ASSERT(!in.pboOffset);
WriteParam(writer, in.structuredSrcSize);
MOZ_RELEASE_ASSERT(!in.image);
WriteIPDLParam(writer, actor, std::move(in.sd));
MOZ_RELEASE_ASSERT(!in.dataSurf);
WriteParam(writer, in.unpacking);
WriteParam(writer, in.applyUnpackTransforms);
}
static bool Read(IPC::MessageReader* const reader, IProtocol* actor,
T* const out) {
return ReadParam(reader, &out->imageTarget) &&
ReadParam(reader, &out->size) &&
ReadParam(reader, &out->srcAlphaType) &&
ReadParam(reader, &out->structuredSrcSize) &&
ReadIPDLParam(reader, actor, &out->sd) &&
ReadParam(reader, &out->unpacking) &&
ReadParam(reader, &out->applyUnpackTransforms);
}
};
} // namespace ipc
namespace webgl {
using Int32Vector = std::vector<int32_t>;
} // namespace webgl
} // namespace mozilla
namespace IPC {
// -
template <class U, size_t PaddedSize>
struct ParamTraits<mozilla::webgl::Padded<U, PaddedSize>> final {
using T = mozilla::webgl::Padded<U, PaddedSize>;
static void Write(MessageWriter* const writer, const T& in) {
WriteParam(writer, *in);
}
static bool Read(MessageReader* const reader, T* const out) {
return ReadParam(reader, &**out);
}
};
// -
template <>
struct ParamTraits<mozilla::webgl::AttribBaseType>
: public ContiguousEnumSerializerInclusive<
mozilla::webgl::AttribBaseType,
mozilla::webgl::AttribBaseType::Boolean,
mozilla::webgl::AttribBaseType::Uint> {};
template <>
struct ParamTraits<mozilla::webgl::ContextLossReason>
: public ContiguousEnumSerializerInclusive<
mozilla::webgl::ContextLossReason,
mozilla::webgl::ContextLossReason::None,
mozilla::webgl::ContextLossReason::Guilty> {};
template <>
struct ParamTraits<gfxAlphaType>
: public ContiguousEnumSerializerInclusive<
gfxAlphaType, gfxAlphaType::Opaque, gfxAlphaType::NonPremult> {};
template <>
struct ParamTraits<mozilla::dom::WebGLPowerPreference> final
: public mozilla::dom::WebIDLEnumSerializer<
mozilla::dom::WebGLPowerPreference> {};
template <>
struct ParamTraits<mozilla::dom::PredefinedColorSpace> final
: public mozilla::dom::WebIDLEnumSerializer<
mozilla::dom::PredefinedColorSpace> {};
// -
// ParamTraits_TiedFields
template <class T>
struct ParamTraits_TiedFields {
static_assert(mozilla::AssertTiedFieldsAreExhaustive<T>());
static void Write(MessageWriter* const writer, const T& in) {
const auto& fields = mozilla::TiedFields(in);
mozilla::MapTuple(fields, [&](const auto& field) {
WriteParam(writer, field);
return true; // ignored
});
}
static bool Read(MessageReader* const reader, T* const out) {
const auto& fields = mozilla::TiedFields(*out);
bool ok = true;
mozilla::MapTuple(fields, [&](auto& field) {
if (ok) {
ok &= ReadParam(reader, &field);
}
return true; // ignored
});
return ok;
}
};
// -
template <>
struct ParamTraits<mozilla::webgl::InitContextDesc> final
: public ParamTraits_TiedFields<mozilla::webgl::InitContextDesc> {};
template <>
struct ParamTraits<mozilla::WebGLContextOptions> final
: public ParamTraits_TiedFields<mozilla::WebGLContextOptions> {};
template <>
struct ParamTraits<mozilla::webgl::OpaqueFramebufferOptions> final
: public ParamTraits_TiedFields<mozilla::webgl::OpaqueFramebufferOptions> {
};
// -
template <>
struct ParamTraits<mozilla::gl::GLVendor>
: public ContiguousEnumSerializerInclusive<mozilla::gl::GLVendor,
mozilla::gl::GLVendor::Intel,
mozilla::gl::kHighestGLVendor> {
};
template <typename U>
struct ParamTraits<mozilla::webgl::EnumMask<U>> final
: public ParamTraits_TiedFields<mozilla::webgl::EnumMask<U>> {};
template <>
struct ParamTraits<mozilla::webgl::InitContextResult> final
: public ParamTraits_TiedFields<mozilla::webgl::InitContextResult> {};
template <>
struct ParamTraits<mozilla::webgl::Limits> final
: public ParamTraits_TiedFields<mozilla::webgl::Limits> {};
template <>
struct ParamTraits<mozilla::webgl::PixelPackingState> final
: public ParamTraits_TiedFields<mozilla::webgl::PixelPackingState> {};
template <>
struct ParamTraits<mozilla::webgl::PixelUnpackStateWebgl> final
: public ParamTraits_TiedFields<mozilla::webgl::PixelUnpackStateWebgl> {};
// -
template <>
struct ParamTraits<mozilla::webgl::ReadPixelsDesc> final {
using T = mozilla::webgl::ReadPixelsDesc;
static void Write(MessageWriter* const writer, const T& in) {
WriteParam(writer, in.srcOffset);
WriteParam(writer, in.size);
WriteParam(writer, in.pi);
WriteParam(writer, in.packState);
}
static bool Read(MessageReader* const reader, T* const out) {
return ReadParam(reader, &out->srcOffset) &&
ReadParam(reader, &out->size) && ReadParam(reader, &out->pi) &&
ReadParam(reader, &out->packState);
}
};
// -
template <>
struct ParamTraits<mozilla::webgl::PackingInfo> final {
using T = mozilla::webgl::PackingInfo;
static void Write(MessageWriter* const writer, const T& in) {
WriteParam(writer, in.format);
WriteParam(writer, in.type);
}
static bool Read(MessageReader* const reader, T* const out) {
return ReadParam(reader, &out->format) && ReadParam(reader, &out->type);
}
};
// -
template <>
struct ParamTraits<mozilla::webgl::CompileResult> final {
using T = mozilla::webgl::CompileResult;
static void Write(MessageWriter* const writer, const T& in) {
WriteParam(writer, in.pending);
WriteParam(writer, in.log);
WriteParam(writer, in.translatedSource);
WriteParam(writer, in.success);
}
static bool Read(MessageReader* const reader, T* const out) {
return ReadParam(reader, &out->pending) && ReadParam(reader, &out->log) &&
ReadParam(reader, &out->translatedSource) &&
ReadParam(reader, &out->success);
}
};
// -
template <>
struct ParamTraits<mozilla::webgl::LinkResult> final {
using T = mozilla::webgl::LinkResult;
static void Write(MessageWriter* const writer, const T& in) {
WriteParam(writer, in.pending);
WriteParam(writer, in.log);
WriteParam(writer, in.success);
WriteParam(writer, in.active);
WriteParam(writer, in.tfBufferMode);
}
static bool Read(MessageReader* const reader, T* const out) {
return ReadParam(reader, &out->pending) && ReadParam(reader, &out->log) &&
ReadParam(reader, &out->success) &&
ReadParam(reader, &out->active) &&
ReadParam(reader, &out->tfBufferMode);
}
};
// -
template <>
struct ParamTraits<mozilla::webgl::LinkActiveInfo> final {
using T = mozilla::webgl::LinkActiveInfo;
static void Write(MessageWriter* const writer, const T& in) {
WriteParam(writer, in.activeAttribs);
WriteParam(writer, in.activeUniforms);
WriteParam(writer, in.activeUniformBlocks);
WriteParam(writer, in.activeTfVaryings);
}
static bool Read(MessageReader* const reader, T* const out) {
return ReadParam(reader, &out->activeAttribs) &&
ReadParam(reader, &out->activeUniforms) &&
ReadParam(reader, &out->activeUniformBlocks) &&
ReadParam(reader, &out->activeTfVaryings);
}
};
// -
template <>
struct ParamTraits<mozilla::webgl::ActiveInfo> final {
using T = mozilla::webgl::ActiveInfo;
static void Write(MessageWriter* const writer, const T& in) {
WriteParam(writer, in.elemType);
WriteParam(writer, in.elemCount);
WriteParam(writer, in.name);
}
static bool Read(MessageReader* const reader, T* const out) {
return ReadParam(reader, &out->elemType) &&
ReadParam(reader, &out->elemCount) && ReadParam(reader, &out->name);
}
};
// -
template <>
struct ParamTraits<mozilla::webgl::ActiveAttribInfo> final {
using T = mozilla::webgl::ActiveAttribInfo;
static void Write(MessageWriter* const writer, const T& in) {
WriteParam(writer, static_cast<const mozilla::webgl::ActiveInfo&>(in));
WriteParam(writer, in.location);
WriteParam(writer, in.baseType);
}
static bool Read(MessageReader* const reader, T* const out) {
return ReadParam(reader, static_cast<mozilla::webgl::ActiveInfo*>(out)) &&
ReadParam(reader, &out->location) &&
ReadParam(reader, &out->baseType);
}
};
// -
template <>
struct ParamTraits<mozilla::webgl::ActiveUniformInfo> final {
using T = mozilla::webgl::ActiveUniformInfo;
static void Write(MessageWriter* const writer, const T& in) {
WriteParam(writer, static_cast<const mozilla::webgl::ActiveInfo&>(in));
WriteParam(writer, in.locByIndex);
WriteParam(writer, in.block_index);
WriteParam(writer, in.block_offset);
WriteParam(writer, in.block_arrayStride);
WriteParam(writer, in.block_matrixStride);
WriteParam(writer, in.block_isRowMajor);
}
static bool Read(MessageReader* const reader, T* const out) {
return ReadParam(reader, static_cast<mozilla::webgl::ActiveInfo*>(out)) &&
ReadParam(reader, &out->locByIndex) &&
ReadParam(reader, &out->block_index) &&
ReadParam(reader, &out->block_offset) &&
ReadParam(reader, &out->block_arrayStride) &&
ReadParam(reader, &out->block_matrixStride) &&
ReadParam(reader, &out->block_isRowMajor);
}
};
// -
template <>
struct ParamTraits<mozilla::webgl::ActiveUniformBlockInfo> final {
using T = mozilla::webgl::ActiveUniformBlockInfo;
static void Write(MessageWriter* const writer, const T& in) {
WriteParam(writer, in.name);
WriteParam(writer, in.dataSize);
WriteParam(writer, in.activeUniformIndices);
WriteParam(writer, in.referencedByVertexShader);
WriteParam(writer, in.referencedByFragmentShader);
}
static bool Read(MessageReader* const reader, T* const out) {
return ReadParam(reader, &out->name) && ReadParam(reader, &out->dataSize) &&
ReadParam(reader, &out->activeUniformIndices) &&
ReadParam(reader, &out->referencedByVertexShader) &&
ReadParam(reader, &out->referencedByFragmentShader);
}
};
// -
template <>
struct ParamTraits<mozilla::webgl::ShaderPrecisionFormat> final {
using T = mozilla::webgl::ShaderPrecisionFormat;
static void Write(MessageWriter* const writer, const T& in) {
WriteParam(writer, in.rangeMin);
WriteParam(writer, in.rangeMax);
WriteParam(writer, in.precision);
}
static bool Read(MessageReader* const reader, T* const out) {
return ReadParam(reader, &out->rangeMin) &&
ReadParam(reader, &out->rangeMax) &&
ReadParam(reader, &out->precision);
}
};
// -
template <typename U, size_t N>
struct ParamTraits<std::array<U, N>> final {
using T = std::array<U, N>;
static void Write(MessageWriter* const writer, const T& in) {
for (const auto& v : in) {
WriteParam(writer, v);
}
}
static bool Read(MessageReader* const reader, T* const out) {
for (auto& v : *out) {
if (!ReadParam(reader, &v)) return false;
}
return true;
}
};
template <typename U, size_t N>
struct ParamTraits<U[N]> final {
using T = U[N];
static constexpr size_t kByteSize = sizeof(U) * N;
static_assert(std::is_trivial<U>::value);
static void Write(MessageWriter* const writer, const T& in) {
writer->WriteBytes(in, kByteSize);
}
static bool Read(MessageReader* const reader, T* const out) {
if (!reader->HasBytesAvailable(kByteSize)) {
return false;
}
return reader->ReadBytesInto(*out, kByteSize);
}
};
// -
template <>
struct ParamTraits<mozilla::webgl::GetUniformData> final {
using T = mozilla::webgl::GetUniformData;
static void Write(MessageWriter* const writer, const T& in) {
ParamTraits<decltype(in.data)>::Write(writer, in.data);
WriteParam(writer, in.type);
}
static bool Read(MessageReader* const reader, T* const out) {
return ParamTraits<decltype(out->data)>::Read(reader, &out->data) &&
ReadParam(reader, &out->type);
}
};
// -
template <typename U>
struct ParamTraits<mozilla::avec2<U>> final {
using T = mozilla::avec2<U>;
static void Write(MessageWriter* const writer, const T& in) {
WriteParam(writer, in.x);
WriteParam(writer, in.y);
}
static bool Read(MessageReader* const reader, T* const out) {
return ReadParam(reader, &out->x) && ReadParam(reader, &out->y);
}
};
// -
template <typename U>
struct ParamTraits<mozilla::avec3<U>> final {
using T = mozilla::avec3<U>;
static void Write(MessageWriter* const writer, const T& in) {
WriteParam(writer, in.x);
WriteParam(writer, in.y);
WriteParam(writer, in.z);
}
static bool Read(MessageReader* const reader, T* const out) {
return ReadParam(reader, &out->x) && ReadParam(reader, &out->y) &&
ReadParam(reader, &out->z);
}
};
// -
template <class TT>
struct ParamTraits_IsEnumCase {
using T = TT;
static void Write(IPC::MessageWriter* const writer, const T& in) {
MOZ_RELEASE_ASSERT(IsEnumCase(in));
WriteParam(writer, mozilla::UnderlyingValue(in));
}
static bool Read(IPC::MessageReader* const reader, T* const out) {
std::underlying_type_t<T> rawVal;
if (!ReadParam(reader, &rawVal)) return false;
*out = static_cast<T>(rawVal);
return IsEnumCase(*out);
}
};
// -
#define USE_IS_ENUM_CASE(T) \
template <> \
struct ParamTraits<T> : public ParamTraits_IsEnumCase<T> {};
USE_IS_ENUM_CASE(mozilla::webgl::OptionalRenderableFormatBits)
#undef USE_IS_ENUM_CASE
} // namespace IPC
#endif
|