Bug 1685822 [wpt PR 27117] - [Import Maps] Add tests for rejecting multiple import...
[gecko.git] / dom / canvas / WebGLParent.cpp
blobd559c72e2814e34e90f70a729bb9460380e39c75
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 "WebGLParent.h"
8 #include "WebGLChild.h"
9 #include "mozilla/dom/WebGLCrossProcessCommandQueue.h"
10 #include "mozilla/layers/LayerTransactionParent.h"
11 #include "mozilla/layers/TextureClientSharedSurface.h"
12 #include "ImageContainer.h"
13 #include "HostWebGLContext.h"
14 #include "WebGLMethodDispatcher.h"
16 namespace mozilla::dom {
18 mozilla::ipc::IPCResult WebGLParent::RecvInitialize(
19 const webgl::InitContextDesc& desc,
20 UniquePtr<HostWebGLCommandSinkP>&& aSinkP,
21 UniquePtr<HostWebGLCommandSinkI>&& aSinkI,
22 webgl::InitContextResult* const out) {
23 auto remotingData = Some(HostWebGLContext::RemotingData{
24 *this, {}, // std::move(commandSink),
25 });
27 mHost = HostWebGLContext::Create(
29 {},
30 std::move(remotingData),
32 desc, out);
34 if (!mHost && !out->error.size()) {
35 return IPC_FAIL(this, "Abnormally failed to create HostWebGLContext.");
38 return IPC_OK();
41 WebGLParent::WebGLParent() = default;
42 WebGLParent::~WebGLParent() = default;
44 // -
46 using IPCResult = mozilla::ipc::IPCResult;
48 IPCResult WebGLParent::RecvDispatchCommands(Shmem&& rawShmem,
49 const uint64_t cmdsByteSize) {
50 auto shmem = webgl::RaiiShmem(this, std::move(rawShmem));
52 const auto& gl = mHost->mContext->GL();
53 const gl::GLContext::TlsScope tlsIsCurrent(gl);
55 MOZ_ASSERT(cmdsByteSize);
56 const auto shmemBytes = shmem.ByteRange();
57 const auto byteSize = std::min<uint64_t>(shmemBytes.length(), cmdsByteSize);
58 const auto cmdsBytes =
59 Range<const uint8_t>{shmemBytes.begin(), shmemBytes.begin() + byteSize};
60 auto view = webgl::RangeConsumerView{cmdsBytes};
62 while (true) {
63 view.AlignTo(kUniversalAlignment);
64 size_t id = 0;
65 const auto status = view.ReadParam(&id);
66 if (status != QueueStatus::kSuccess) break;
68 WebGLMethodDispatcher<0>::DispatchCommand(*mHost, id, view);
71 return IPC_OK();
74 // -
76 mozilla::ipc::IPCResult WebGLParent::Recv__delete__() {
77 mHost = nullptr;
78 return IPC_OK();
81 void WebGLParent::ActorDestroy(ActorDestroyReason aWhy) { mHost = nullptr; }
83 // -
85 IPCResult WebGLParent::RecvGetFrontBufferSnapshot(
86 webgl::FrontBufferSnapshotIpc* const ret) {
87 *ret = {};
89 const auto surfSize = mHost->GetFrontBufferSize();
90 const auto byteSize = 4 * surfSize.x * surfSize.y;
92 auto shmem = webgl::RaiiShmem::Alloc(
93 this, byteSize, mozilla::ipc::SharedMemory::SharedMemoryType::TYPE_BASIC);
94 if (!shmem) {
95 NS_WARNING("Failed to alloc shmem for RecvGetFrontBufferSnapshot.");
96 return IPC_FAIL(this, "Failed to allocate shmem for result");
99 const auto range = shmem.ByteRange();
100 auto retSize = surfSize;
101 if (!mHost->FrontBufferSnapshotInto(range)) {
102 retSize = {0, 0}; // Zero means failure.
104 *ret = {retSize, shmem.Extract()};
105 return IPC_OK();
108 IPCResult WebGLParent::RecvGetBufferSubData(const GLenum target,
109 const uint64_t srcByteOffset,
110 const uint64_t byteSize,
111 Shmem* const ret) {
112 const auto allocSize = 1 + byteSize;
113 auto shmem = webgl::RaiiShmem::Alloc(
114 this, allocSize,
115 mozilla::ipc::SharedMemory::SharedMemoryType::TYPE_BASIC);
116 if (!shmem) {
117 NS_WARNING("Failed to alloc shmem for RecvGetBufferSubData.");
118 return IPC_FAIL(this, "Failed to allocate shmem for result");
121 const auto shmemRange = shmem.ByteRange();
122 const auto dataRange =
123 Range<uint8_t>{shmemRange.begin() + 1, shmemRange.end()};
125 // We need to always send the shmem:
126 // https://bugzilla.mozilla.org/show_bug.cgi?id=1463831#c2
127 const auto ok = mHost->GetBufferSubData(target, srcByteOffset, dataRange);
128 *(shmemRange.begin().get()) = ok;
129 *ret = shmem.Extract();
130 return IPC_OK();
133 IPCResult WebGLParent::RecvReadPixels(const webgl::ReadPixelsDesc& desc,
134 const uint64_t byteSize,
135 webgl::ReadPixelsResultIpc* const ret) {
136 *ret = {};
138 const auto allocSize = std::max<uint64_t>(1, byteSize);
139 auto shmem = webgl::RaiiShmem::Alloc(
140 this, allocSize,
141 mozilla::ipc::SharedMemory::SharedMemoryType::TYPE_BASIC);
142 if (!shmem) {
143 NS_WARNING("Failed to alloc shmem for RecvReadPixels.");
144 return IPC_FAIL(this, "Failed to allocate shmem for result");
147 const auto range = shmem.ByteRange();
149 const auto res = mHost->ReadPixelsInto(desc, range);
150 *ret = {res, shmem.Extract()};
151 return IPC_OK();
154 // -
156 IPCResult WebGLParent::RecvCheckFramebufferStatus(GLenum target,
157 GLenum* const ret) {
158 *ret = mHost->CheckFramebufferStatus(target);
159 return IPC_OK();
162 IPCResult WebGLParent::RecvClientWaitSync(ObjectId id, GLbitfield flags,
163 GLuint64 timeout, GLenum* const ret) {
164 *ret = mHost->ClientWaitSync(id, flags, timeout);
165 return IPC_OK();
168 IPCResult WebGLParent::RecvCreateOpaqueFramebuffer(
169 const ObjectId id, const OpaqueFramebufferOptions& options,
170 bool* const ret) {
171 *ret = mHost->CreateOpaqueFramebuffer(id, options);
172 return IPC_OK();
175 IPCResult WebGLParent::RecvDrawingBufferSize(uvec2* const ret) {
176 *ret = mHost->DrawingBufferSize();
177 return IPC_OK();
180 IPCResult WebGLParent::RecvFinish() {
181 mHost->Finish();
182 return IPC_OK();
185 IPCResult WebGLParent::RecvGetBufferParameter(GLenum target, GLenum pname,
186 Maybe<double>* const ret) {
187 *ret = mHost->GetBufferParameter(target, pname);
188 return IPC_OK();
191 IPCResult WebGLParent::RecvGetCompileResult(ObjectId id,
192 webgl::CompileResult* const ret) {
193 *ret = mHost->GetCompileResult(id);
194 return IPC_OK();
197 IPCResult WebGLParent::RecvGetError(GLenum* const ret) {
198 *ret = mHost->GetError();
199 return IPC_OK();
202 IPCResult WebGLParent::RecvGetFragDataLocation(ObjectId id,
203 const std::string& name,
204 GLint* const ret) {
205 *ret = mHost->GetFragDataLocation(id, name);
206 return IPC_OK();
209 IPCResult WebGLParent::RecvGetFramebufferAttachmentParameter(
210 ObjectId id, GLenum attachment, GLenum pname, Maybe<double>* const ret) {
211 *ret = mHost->GetFramebufferAttachmentParameter(id, attachment, pname);
212 return IPC_OK();
215 IPCResult WebGLParent::RecvGetFrontBuffer(
216 ObjectId fb, const bool vr, Maybe<layers::SurfaceDescriptor>* const ret) {
217 *ret = mHost->GetFrontBuffer(fb, vr);
218 return IPC_OK();
221 IPCResult WebGLParent::RecvGetIndexedParameter(GLenum target, GLuint index,
222 Maybe<double>* const ret) {
223 *ret = mHost->GetIndexedParameter(target, index);
224 return IPC_OK();
227 IPCResult WebGLParent::RecvGetInternalformatParameter(
228 const GLenum target, const GLuint format, const GLuint pname,
229 Maybe<std::vector<int32_t>>* const ret) {
230 *ret = mHost->GetInternalformatParameter(target, format, pname);
231 return IPC_OK();
234 IPCResult WebGLParent::RecvGetLinkResult(ObjectId id,
235 webgl::LinkResult* const ret) {
236 *ret = mHost->GetLinkResult(id);
237 return IPC_OK();
240 IPCResult WebGLParent::RecvGetNumber(GLenum pname, Maybe<double>* const ret) {
241 *ret = mHost->GetNumber(pname);
242 return IPC_OK();
245 IPCResult WebGLParent::RecvGetQueryParameter(ObjectId id, GLenum pname,
246 Maybe<double>* const ret) {
247 *ret = mHost->GetQueryParameter(id, pname);
248 return IPC_OK();
251 IPCResult WebGLParent::RecvGetRenderbufferParameter(ObjectId id, GLenum pname,
252 Maybe<double>* const ret) {
253 *ret = mHost->GetRenderbufferParameter(id, pname);
254 return IPC_OK();
257 IPCResult WebGLParent::RecvGetSamplerParameter(ObjectId id, GLenum pname,
258 Maybe<double>* const ret) {
259 *ret = mHost->GetSamplerParameter(id, pname);
260 return IPC_OK();
263 IPCResult WebGLParent::RecvGetShaderPrecisionFormat(
264 GLenum shaderType, GLenum precisionType,
265 Maybe<webgl::ShaderPrecisionFormat>* const ret) {
266 *ret = mHost->GetShaderPrecisionFormat(shaderType, precisionType);
267 return IPC_OK();
270 IPCResult WebGLParent::RecvGetString(GLenum pname,
271 Maybe<std::string>* const ret) {
272 *ret = mHost->GetString(pname);
273 return IPC_OK();
276 IPCResult WebGLParent::RecvGetTexParameter(ObjectId id, GLenum pname,
277 Maybe<double>* const ret) {
278 *ret = mHost->GetTexParameter(id, pname);
279 return IPC_OK();
282 IPCResult WebGLParent::RecvGetUniform(ObjectId id, uint32_t loc,
283 webgl::GetUniformData* const ret) {
284 *ret = mHost->GetUniform(id, loc);
285 return IPC_OK();
288 IPCResult WebGLParent::RecvGetVertexAttrib(GLuint index, GLenum pname,
289 Maybe<double>* const ret) {
290 *ret = mHost->GetVertexAttrib(index, pname);
291 return IPC_OK();
294 IPCResult WebGLParent::RecvIsEnabled(GLenum cap, bool* const ret) {
295 *ret = mHost->IsEnabled(cap);
296 return IPC_OK();
299 IPCResult WebGLParent::RecvOnMemoryPressure() {
300 mHost->OnMemoryPressure();
301 return IPC_OK();
304 IPCResult WebGLParent::RecvValidateProgram(ObjectId id, bool* const ret) {
305 *ret = mHost->ValidateProgram(id);
306 return IPC_OK();
309 } // namespace mozilla::dom