WebGLIpdl.h (15465B)
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/. */ 5 6 #ifndef WEBGLIPDL_H_ 7 #define WEBGLIPDL_H_ 8 9 #include "WebGLTypes.h" 10 #include "gfxTypes.h" 11 #include "ipc/EnumSerializer.h" 12 #include "ipc/IPCMessageUtils.h" 13 #include "mozilla/GfxMessageUtils.h" 14 #include "mozilla/ParamTraits_IsEnumCase.h" 15 #include "mozilla/ParamTraits_STL.h" 16 #include "mozilla/ParamTraits_TiedFields.h" 17 #include "mozilla/dom/BindingIPCUtils.h" 18 #include "mozilla/ipc/Shmem.h" 19 #include "mozilla/layers/LayersSurfaces.h" 20 21 namespace mozilla { 22 namespace webgl { 23 24 // TODO: This should probably replace Shmem, or at least this should move to 25 // ipc/glue. 26 27 class RaiiShmem final { 28 RefPtr<mozilla::ipc::ActorLifecycleProxy> mWeakRef; 29 mozilla::ipc::Shmem mShmem = {}; 30 31 public: 32 /// Returns zeroed data. 33 static RaiiShmem Alloc(mozilla::ipc::IProtocol* const allocator, 34 const size_t size) { 35 mozilla::ipc::Shmem shmem; 36 if (!allocator->AllocShmem(size, &shmem)) return {}; 37 return {allocator, shmem}; 38 } 39 40 static RaiiShmem AllocUnsafe(mozilla::ipc::IProtocol* const allocator, 41 const size_t size) { 42 mozilla::ipc::Shmem shmem; 43 if (!allocator->AllocUnsafeShmem(size, &shmem)) return {}; 44 return {allocator, shmem}; 45 } 46 47 // - 48 49 RaiiShmem() = default; 50 51 RaiiShmem(mozilla::ipc::IProtocol* const allocator, 52 const mozilla::ipc::Shmem& shmem) { 53 if (!allocator || !allocator->CanSend()) { 54 return; 55 } 56 57 // Shmems are handled by the top-level, so use that or we might leak after 58 // the actor dies. 59 mWeakRef = allocator->ToplevelProtocol()->GetLifecycleProxy(); 60 mShmem = shmem; 61 if (!mWeakRef || !mWeakRef->Get() || !IsShmem()) { 62 reset(); 63 } 64 } 65 66 void reset() { 67 if (IsShmem()) { 68 const auto& allocator = mWeakRef->Get(); 69 if (allocator) { 70 allocator->DeallocShmem(mShmem); 71 } 72 } 73 mWeakRef = nullptr; 74 mShmem = {}; 75 } 76 77 ~RaiiShmem() { reset(); } 78 79 // - 80 81 RaiiShmem(RaiiShmem&& rhs) { *this = std::move(rhs); } 82 RaiiShmem& operator=(RaiiShmem&& rhs) { 83 reset(); 84 mWeakRef = rhs.mWeakRef; 85 mShmem = rhs.Extract(); 86 return *this; 87 } 88 89 // - 90 91 bool IsShmem() const { return mShmem.IsReadable(); } 92 93 explicit operator bool() const { return IsShmem(); } 94 95 // - 96 97 const auto& Shmem() const { 98 MOZ_ASSERT(IsShmem()); 99 return mShmem; 100 } 101 102 Range<uint8_t> ByteRange() const { 103 if (!IsShmem()) { 104 return {}; 105 } 106 return {mShmem.get<uint8_t>(), mShmem.Size<uint8_t>()}; 107 } 108 109 mozilla::ipc::Shmem Extract() { 110 auto ret = mShmem; 111 mShmem = {}; 112 reset(); 113 return ret; 114 } 115 }; 116 117 using Int32Vector = std::vector<int32_t>; 118 119 } // namespace webgl 120 } // namespace mozilla 121 122 namespace IPC { 123 124 template <> 125 struct ParamTraits<mozilla::webgl::FrontBufferSnapshotIpc> final { 126 using T = mozilla::webgl::FrontBufferSnapshotIpc; 127 128 static void Write(IPC::MessageWriter* const writer, T& in) { 129 WriteParam(writer, in.surfSize); 130 WriteParam(writer, in.byteStride); 131 WriteParam(writer, std::move(in.shmem)); 132 } 133 134 static bool Read(IPC::MessageReader* const reader, T* const out) { 135 return ReadParam(reader, &out->surfSize) && 136 ReadParam(reader, &out->byteStride) && 137 ReadParam(reader, &out->shmem); 138 } 139 }; 140 141 // - 142 143 template <> 144 struct ParamTraits<mozilla::webgl::ReadPixelsResultIpc> final { 145 using T = mozilla::webgl::ReadPixelsResultIpc; 146 147 static void Write(MessageWriter* const writer, T& in) { 148 WriteParam(writer, in.subrect); 149 WriteParam(writer, in.byteStride); 150 WriteParam(writer, std::move(in.shmem)); 151 } 152 153 static bool Read(MessageReader* const reader, T* const out) { 154 return ReadParam(reader, &out->subrect) && 155 ReadParam(reader, &out->byteStride) && 156 ReadParam(reader, &out->shmem); 157 } 158 }; 159 160 // - 161 162 template <> 163 struct ParamTraits<mozilla::webgl::TexUnpackBlobDesc> final { 164 using T = mozilla::webgl::TexUnpackBlobDesc; 165 166 static void Write(MessageWriter* const writer, T&& in) { 167 WriteParam(writer, in.imageTarget); 168 WriteParam(writer, in.size); 169 WriteParam(writer, in.srcAlphaType); 170 MOZ_RELEASE_ASSERT(!in.cpuData); 171 MOZ_RELEASE_ASSERT(!in.pboOffset); 172 WriteParam(writer, in.structuredSrcSize); 173 MOZ_RELEASE_ASSERT(!in.image); 174 WriteParam(writer, std::move(in.sd)); 175 MOZ_RELEASE_ASSERT(!in.sourceSurf); 176 WriteParam(writer, in.unpacking); 177 WriteParam(writer, in.applyUnpackTransforms); 178 } 179 180 static bool Read(MessageReader* const reader, T* const out) { 181 return ReadParam(reader, &out->imageTarget) && 182 ReadParam(reader, &out->size) && 183 ReadParam(reader, &out->srcAlphaType) && 184 ReadParam(reader, &out->structuredSrcSize) && 185 ReadParam(reader, &out->sd) && ReadParam(reader, &out->unpacking) && 186 ReadParam(reader, &out->applyUnpackTransforms); 187 } 188 }; 189 190 // - 191 192 template <class U, size_t PaddedSize> 193 struct ParamTraits<mozilla::webgl::Padded<U, PaddedSize>> final { 194 using T = mozilla::webgl::Padded<U, PaddedSize>; 195 196 static void Write(MessageWriter* const writer, const T& in) { 197 WriteParam(writer, *in); 198 } 199 200 static bool Read(MessageReader* const reader, T* const out) { 201 return ReadParam(reader, &**out); 202 } 203 }; 204 205 // - 206 207 template <> 208 struct ParamTraits<mozilla::webgl::AttribBaseType> 209 : public ContiguousEnumSerializerInclusive< 210 mozilla::webgl::AttribBaseType, 211 mozilla::webgl::AttribBaseType::Boolean, 212 mozilla::webgl::AttribBaseType::Uint> {}; 213 214 template <> 215 struct ParamTraits<mozilla::webgl::ContextLossReason> 216 : public ContiguousEnumSerializerInclusive< 217 mozilla::webgl::ContextLossReason, 218 mozilla::webgl::ContextLossReason::None, 219 mozilla::webgl::ContextLossReason::Guilty> {}; 220 221 template <> 222 struct ParamTraits<gfxAlphaType> 223 : public ContiguousEnumSerializerInclusive< 224 gfxAlphaType, gfxAlphaType::Opaque, gfxAlphaType::NonPremult> {}; 225 226 template <> 227 struct ParamTraits<mozilla::dom::WebGLPowerPreference> final 228 : public mozilla::dom::WebIDLEnumSerializer< 229 mozilla::dom::WebGLPowerPreference> {}; 230 231 template <> 232 struct ParamTraits<mozilla::dom::PredefinedColorSpace> final 233 : public mozilla::dom::WebIDLEnumSerializer< 234 mozilla::dom::PredefinedColorSpace> {}; 235 236 // - 237 // ParamTraits_IsEnumCase 238 239 #define USE_IS_ENUM_CASE(T) \ 240 template <> \ 241 struct ParamTraits<T> : public ParamTraits_IsEnumCase<T> {}; 242 243 USE_IS_ENUM_CASE(mozilla::webgl::OptionalRenderableFormatBits) 244 245 #undef USE_IS_ENUM_CASE 246 247 // - 248 // ParamTraits_TiedFields 249 250 template <> 251 struct ParamTraits<mozilla::webgl::InitContextDesc> final 252 : public ParamTraits_TiedFields<mozilla::webgl::InitContextDesc> {}; 253 254 template <> 255 struct ParamTraits<mozilla::WebGLContextOptions> final 256 : public ParamTraits_TiedFields<mozilla::WebGLContextOptions> {}; 257 258 template <> 259 struct ParamTraits<mozilla::webgl::OpaqueFramebufferOptions> final 260 : public ParamTraits_TiedFields<mozilla::webgl::OpaqueFramebufferOptions> { 261 }; 262 263 template <> 264 struct ParamTraits<mozilla::webgl::ShaderPrecisionFormat> final 265 : public ParamTraits_TiedFields<mozilla::webgl::ShaderPrecisionFormat> {}; 266 267 // - 268 269 template <> 270 struct ParamTraits<mozilla::gl::GLVendor> 271 : public ContiguousEnumSerializerInclusive<mozilla::gl::GLVendor, 272 mozilla::gl::GLVendor::Intel, 273 mozilla::gl::kHighestGLVendor> { 274 }; 275 276 template <typename U> 277 struct ParamTraits<mozilla::webgl::EnumMask<U>> final 278 : public ParamTraits_TiedFields<mozilla::webgl::EnumMask<U>> {}; 279 280 template <> 281 struct ParamTraits<mozilla::webgl::InitContextResult> final 282 : public ParamTraits_TiedFields<mozilla::webgl::InitContextResult> {}; 283 284 template <> 285 struct ParamTraits<mozilla::webgl::Limits> final 286 : public ParamTraits_TiedFields<mozilla::webgl::Limits> {}; 287 288 template <> 289 struct ParamTraits<mozilla::webgl::PixelPackingState> final 290 : public ParamTraits_TiedFields<mozilla::webgl::PixelPackingState> {}; 291 template <> 292 struct ParamTraits<mozilla::webgl::PixelUnpackStateWebgl> final 293 : public ParamTraits_TiedFields<mozilla::webgl::PixelUnpackStateWebgl> {}; 294 295 // - 296 297 template <> 298 struct ParamTraits<mozilla::webgl::ReadPixelsDesc> final { 299 using T = mozilla::webgl::ReadPixelsDesc; 300 301 static void Write(MessageWriter* const writer, const T& in) { 302 WriteParam(writer, in.srcOffset); 303 WriteParam(writer, in.size); 304 WriteParam(writer, in.pi); 305 WriteParam(writer, in.packState); 306 } 307 308 static bool Read(MessageReader* const reader, T* const out) { 309 return ReadParam(reader, &out->srcOffset) && 310 ReadParam(reader, &out->size) && ReadParam(reader, &out->pi) && 311 ReadParam(reader, &out->packState); 312 } 313 }; 314 315 // - 316 317 template <> 318 struct ParamTraits<mozilla::webgl::PackingInfo> final { 319 using T = mozilla::webgl::PackingInfo; 320 321 static void Write(MessageWriter* const writer, const T& in) { 322 WriteParam(writer, in.format); 323 WriteParam(writer, in.type); 324 } 325 326 static bool Read(MessageReader* const reader, T* const out) { 327 return ReadParam(reader, &out->format) && ReadParam(reader, &out->type); 328 } 329 }; 330 331 // - 332 333 template <> 334 struct ParamTraits<mozilla::webgl::CompileResult> final { 335 using T = mozilla::webgl::CompileResult; 336 337 static void Write(MessageWriter* const writer, const T& in) { 338 WriteParam(writer, in.pending); 339 WriteParam(writer, in.log); 340 WriteParam(writer, in.translatedSource); 341 WriteParam(writer, in.success); 342 } 343 344 static bool Read(MessageReader* const reader, T* const out) { 345 return ReadParam(reader, &out->pending) && ReadParam(reader, &out->log) && 346 ReadParam(reader, &out->translatedSource) && 347 ReadParam(reader, &out->success); 348 } 349 }; 350 351 // - 352 353 template <> 354 struct ParamTraits<mozilla::webgl::LinkResult> final { 355 using T = mozilla::webgl::LinkResult; 356 357 static void Write(MessageWriter* const writer, const T& in) { 358 WriteParam(writer, in.pending); 359 WriteParam(writer, in.log); 360 WriteParam(writer, in.success); 361 WriteParam(writer, in.active); 362 WriteParam(writer, in.tfBufferMode); 363 } 364 365 static bool Read(MessageReader* const reader, T* const out) { 366 return ReadParam(reader, &out->pending) && ReadParam(reader, &out->log) && 367 ReadParam(reader, &out->success) && 368 ReadParam(reader, &out->active) && 369 ReadParam(reader, &out->tfBufferMode); 370 } 371 }; 372 373 // - 374 375 template <> 376 struct ParamTraits<mozilla::webgl::LinkActiveInfo> final { 377 using T = mozilla::webgl::LinkActiveInfo; 378 379 static void Write(MessageWriter* const writer, const T& in) { 380 WriteParam(writer, in.activeAttribs); 381 WriteParam(writer, in.activeUniforms); 382 WriteParam(writer, in.activeUniformBlocks); 383 WriteParam(writer, in.activeTfVaryings); 384 } 385 386 static bool Read(MessageReader* const reader, T* const out) { 387 return ReadParam(reader, &out->activeAttribs) && 388 ReadParam(reader, &out->activeUniforms) && 389 ReadParam(reader, &out->activeUniformBlocks) && 390 ReadParam(reader, &out->activeTfVaryings); 391 } 392 }; 393 394 // - 395 396 template <> 397 struct ParamTraits<mozilla::webgl::ActiveInfo> final { 398 using T = mozilla::webgl::ActiveInfo; 399 400 static void Write(MessageWriter* const writer, const T& in) { 401 WriteParam(writer, in.elemType); 402 WriteParam(writer, in.elemCount); 403 WriteParam(writer, in.name); 404 } 405 406 static bool Read(MessageReader* const reader, T* const out) { 407 return ReadParam(reader, &out->elemType) && 408 ReadParam(reader, &out->elemCount) && ReadParam(reader, &out->name); 409 } 410 }; 411 412 // - 413 414 template <> 415 struct ParamTraits<mozilla::webgl::ActiveAttribInfo> final { 416 using T = mozilla::webgl::ActiveAttribInfo; 417 418 static void Write(MessageWriter* const writer, const T& in) { 419 WriteParam(writer, static_cast<const mozilla::webgl::ActiveInfo&>(in)); 420 WriteParam(writer, in.location); 421 WriteParam(writer, in.baseType); 422 } 423 424 static bool Read(MessageReader* const reader, T* const out) { 425 return ReadParam(reader, static_cast<mozilla::webgl::ActiveInfo*>(out)) && 426 ReadParam(reader, &out->location) && 427 ReadParam(reader, &out->baseType); 428 } 429 }; 430 431 // - 432 433 template <> 434 struct ParamTraits<mozilla::webgl::ActiveUniformInfo> final { 435 using T = mozilla::webgl::ActiveUniformInfo; 436 437 static void Write(MessageWriter* const writer, const T& in) { 438 WriteParam(writer, static_cast<const mozilla::webgl::ActiveInfo&>(in)); 439 WriteParam(writer, in.locByIndex); 440 WriteParam(writer, in.block_index); 441 WriteParam(writer, in.block_offset); 442 WriteParam(writer, in.block_arrayStride); 443 WriteParam(writer, in.block_matrixStride); 444 WriteParam(writer, in.block_isRowMajor); 445 } 446 447 static bool Read(MessageReader* const reader, T* const out) { 448 return ReadParam(reader, static_cast<mozilla::webgl::ActiveInfo*>(out)) && 449 ReadParam(reader, &out->locByIndex) && 450 ReadParam(reader, &out->block_index) && 451 ReadParam(reader, &out->block_offset) && 452 ReadParam(reader, &out->block_arrayStride) && 453 ReadParam(reader, &out->block_matrixStride) && 454 ReadParam(reader, &out->block_isRowMajor); 455 } 456 }; 457 458 // - 459 460 template <> 461 struct ParamTraits<mozilla::webgl::ActiveUniformBlockInfo> final { 462 using T = mozilla::webgl::ActiveUniformBlockInfo; 463 464 static void Write(MessageWriter* const writer, const T& in) { 465 WriteParam(writer, in.name); 466 WriteParam(writer, in.dataSize); 467 WriteParam(writer, in.activeUniformIndices); 468 WriteParam(writer, in.referencedByVertexShader); 469 WriteParam(writer, in.referencedByFragmentShader); 470 } 471 472 static bool Read(MessageReader* const reader, T* const out) { 473 return ReadParam(reader, &out->name) && ReadParam(reader, &out->dataSize) && 474 ReadParam(reader, &out->activeUniformIndices) && 475 ReadParam(reader, &out->referencedByVertexShader) && 476 ReadParam(reader, &out->referencedByFragmentShader); 477 } 478 }; 479 480 // - 481 482 template <> 483 struct ParamTraits<mozilla::webgl::GetUniformData> final { 484 using T = mozilla::webgl::GetUniformData; 485 486 static void Write(MessageWriter* const writer, const T& in) { 487 ParamTraits<decltype(in.data)>::Write(writer, in.data); 488 WriteParam(writer, in.type); 489 } 490 491 static bool Read(MessageReader* const reader, T* const out) { 492 return ParamTraits<decltype(out->data)>::Read(reader, &out->data) && 493 ReadParam(reader, &out->type); 494 } 495 }; 496 497 // - 498 499 template <typename U> 500 struct ParamTraits<mozilla::avec2<U>> final { 501 using T = mozilla::avec2<U>; 502 503 static void Write(MessageWriter* const writer, const T& in) { 504 WriteParam(writer, in.x); 505 WriteParam(writer, in.y); 506 } 507 508 static bool Read(MessageReader* const reader, T* const out) { 509 return ReadParam(reader, &out->x) && ReadParam(reader, &out->y); 510 } 511 }; 512 513 // - 514 515 template <typename U> 516 struct ParamTraits<mozilla::avec3<U>> final { 517 using T = mozilla::avec3<U>; 518 519 static void Write(MessageWriter* const writer, const T& in) { 520 WriteParam(writer, in.x); 521 WriteParam(writer, in.y); 522 WriteParam(writer, in.z); 523 } 524 525 static bool Read(MessageReader* const reader, T* const out) { 526 return ReadParam(reader, &out->x) && ReadParam(reader, &out->y) && 527 ReadParam(reader, &out->z); 528 } 529 }; 530 531 } // namespace IPC 532 533 #endif