1 /* -*- Mode: C++; tab-width: 20; 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 #include "WebGLChild.h"
8 #include "ClientWebGLContext.h"
9 #include "mozilla/StaticPrefs_webgl.h"
10 #include "WebGLMethodDispatcher.h"
12 namespace mozilla::dom
{
14 WebGLChild::WebGLChild(ClientWebGLContext
& context
)
16 mDefaultCmdsShmemSize(StaticPrefs::webgl_out_of_process_shmem_size()) {}
18 WebGLChild::~WebGLChild() { (void)Send__delete__(this); }
20 void WebGLChild::ActorDestroy(ActorDestroyReason why
) {
21 mPendingCmdsShmem
= {};
26 Maybe
<Range
<uint8_t>> WebGLChild::AllocPendingCmdBytes(const size_t size
) {
27 if (!mPendingCmdsShmem
) {
28 size_t capacity
= mDefaultCmdsShmemSize
;
29 if (capacity
< size
) {
33 auto shmem
= webgl::RaiiShmem::Alloc(
35 mozilla::ipc::SharedMemory::SharedMemoryType::TYPE_BASIC
);
37 NS_WARNING("Failed to alloc shmem for AllocPendingCmdBytes.");
40 mPendingCmdsShmem
= std::move(shmem
);
43 const auto range
= mPendingCmdsShmem
.ByteRange();
45 auto itr
= range
.begin() + mPendingCmdsPos
;
46 const auto offset
= AlignmentOffset(kUniversalAlignment
, itr
.get());
47 mPendingCmdsPos
+= offset
;
48 const auto required
= mPendingCmdsPos
+ size
;
49 if (required
> range
.length()) {
51 return AllocPendingCmdBytes(size
);
53 itr
= range
.begin() + mPendingCmdsPos
;
54 const auto remaining
= Range
<uint8_t>{itr
, range
.end()};
55 mPendingCmdsPos
+= size
;
56 return Some(Range
<uint8_t>{remaining
.begin(), remaining
.begin() + size
});
59 void WebGLChild::FlushPendingCmds() {
60 if (!mPendingCmdsShmem
) return;
62 const auto byteSize
= mPendingCmdsPos
;
63 SendDispatchCommands(mPendingCmdsShmem
.Extract(), byteSize
);
65 mFlushedCmdInfo
.flushes
+= 1;
66 mFlushedCmdInfo
.flushedCmdBytes
+= byteSize
;
68 if (gl::GLContext::ShouldSpew()) {
69 printf_stderr("[WebGLChild] Flushed %zu bytes. (%zu over %zu flushes)\n",
70 byteSize
, mFlushedCmdInfo
.flushedCmdBytes
,
71 mFlushedCmdInfo
.flushes
);
77 mozilla::ipc::IPCResult
WebGLChild::RecvJsWarning(
78 const std::string
& text
) const {
79 if (!mContext
) return IPC_OK();
80 mContext
->JsWarning(text
);
84 mozilla::ipc::IPCResult
WebGLChild::RecvOnContextLoss(
85 const webgl::ContextLossReason reason
) const {
86 if (!mContext
) return IPC_OK();
87 mContext
->OnContextLoss(reason
);
91 } // namespace mozilla::dom