Bug 1671598 [wpt PR 26128] - [AspectRatio] Fix divide by zero with a small float...
[gecko.git] / dom / canvas / HostWebGLContext.cpp
blobe0fdc0a74a9c8ccc8a4d5ddd635895a220493739
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 "HostWebGLContext.h"
8 #include "CompositableHost.h"
9 #include "mozilla/layers/LayerTransactionChild.h"
10 #include "mozilla/layers/LayersSurfaces.h"
12 #include "MozFramebuffer.h"
13 #include "TexUnpackBlob.h"
14 #include "WebGL2Context.h"
15 #include "WebGLBuffer.h"
16 #include "WebGLContext.h"
17 #include "WebGLCrossProcessCommandQueue.h"
18 #include "WebGLFramebuffer.h"
19 #include "WebGLMemoryTracker.h"
20 #include "WebGLParent.h"
21 #include "WebGLProgram.h"
22 #include "WebGLRenderbuffer.h"
23 #include "WebGLSampler.h"
24 #include "WebGLShader.h"
25 #include "WebGLSync.h"
26 #include "WebGLTexture.h"
27 #include "WebGLTransformFeedback.h"
28 #include "WebGLVertexArray.h"
29 #include "WebGLQuery.h"
31 #include "mozilla/StaticMutex.h"
33 namespace mozilla {
35 LazyLogModule gWebGLBridgeLog("webglbridge");
37 // -
39 static StaticMutex sContextSetLock;
41 static std::unordered_set<HostWebGLContext*>& DeferredStaticContextSet() {
42 static std::unordered_set<HostWebGLContext*> sContextSet;
43 return sContextSet;
46 LockedOutstandingContexts::LockedOutstandingContexts()
47 : contexts(DeferredStaticContextSet()) {
48 sContextSetLock.Lock();
51 LockedOutstandingContexts::~LockedOutstandingContexts() {
52 sContextSetLock.Unlock();
55 // -
57 /*static*/
58 UniquePtr<HostWebGLContext> HostWebGLContext::Create(
59 OwnerData&& ownerData, const webgl::InitContextDesc& desc,
60 webgl::InitContextResult* const out) {
61 auto host = WrapUnique(new HostWebGLContext(std::move(ownerData)));
62 auto webgl = WebGLContext::Create(*host, desc, out);
63 if (!webgl) return nullptr;
64 return host;
67 HostWebGLContext::HostWebGLContext(OwnerData&& ownerData)
68 : mOwnerData(std::move(ownerData)) {
69 if (mOwnerData.outOfProcess) {
70 if (mOwnerData.outOfProcess->mCommandSinkP) {
71 mOwnerData.outOfProcess->mCommandSinkP->mHostContext = this;
73 if (mOwnerData.outOfProcess->mCommandSinkI) {
74 mOwnerData.outOfProcess->mCommandSinkI->mHostContext = this;
79 StaticMutexAutoLock lock(sContextSetLock);
80 auto& contexts = DeferredStaticContextSet();
81 (void)contexts.insert(this);
85 HostWebGLContext::~HostWebGLContext() {
86 StaticMutexAutoLock lock(sContextSetLock);
87 auto& contexts = DeferredStaticContextSet();
88 (void)contexts.erase(this);
91 // -
93 void HostWebGLContext::OnContextLoss(const webgl::ContextLossReason reason) {
94 if (mOwnerData.inProcess) {
95 (*mOwnerData.inProcess)->OnContextLoss(reason);
96 } else {
97 (void)mOwnerData.outOfProcess->mParent.SendOnContextLoss(reason);
101 void HostWebGLContext::JsWarning(const std::string& text) const {
102 if (mOwnerData.inProcess) {
103 (*mOwnerData.inProcess)->JsWarning(text);
104 return;
106 (void)mOwnerData.outOfProcess->mParent.SendJsWarning(text);
109 Maybe<layers::SurfaceDescriptor> HostWebGLContext::GetFrontBuffer(
110 const ObjectId xrFb, const bool webvr) const {
111 return mContext->GetFrontBuffer(AutoResolve(xrFb), webvr);
114 //////////////////////////////////////////////
115 // Creation
117 void HostWebGLContext::CreateBuffer(const ObjectId id) {
118 auto& slot = mBufferMap[id];
119 if (slot) {
120 MOZ_ASSERT(false, "duplicate ID");
121 return;
123 slot = mContext->CreateBuffer();
126 void HostWebGLContext::CreateFramebuffer(const ObjectId id) {
127 auto& slot = mFramebufferMap[id];
128 if (slot) {
129 MOZ_ASSERT(false, "duplicate ID");
130 return;
132 slot = mContext->CreateFramebuffer();
135 bool HostWebGLContext::CreateOpaqueFramebuffer(
136 const ObjectId id, const webgl::OpaqueFramebufferOptions& options) {
137 auto& slot = mFramebufferMap[id];
138 if (slot) {
139 MOZ_ASSERT(false, "duplicate ID");
140 return false;
142 slot = mContext->CreateOpaqueFramebuffer(options);
143 return slot;
146 void HostWebGLContext::CreateProgram(const ObjectId id) {
147 auto& slot = mProgramMap[id];
148 if (slot) {
149 MOZ_ASSERT(false, "duplicate ID");
150 return;
152 slot = mContext->CreateProgram();
155 void HostWebGLContext::CreateQuery(const ObjectId id) {
156 auto& slot = mQueryMap[id];
157 if (slot) {
158 MOZ_ASSERT(false, "duplicate ID");
159 return;
161 slot = mContext->CreateQuery();
164 void HostWebGLContext::CreateRenderbuffer(const ObjectId id) {
165 auto& slot = mRenderbufferMap[id];
166 if (slot) {
167 MOZ_ASSERT(false, "duplicate ID");
168 return;
170 slot = mContext->CreateRenderbuffer();
173 void HostWebGLContext::CreateSampler(const ObjectId id) {
174 auto& slot = mSamplerMap[id];
175 if (slot) {
176 MOZ_ASSERT(false, "duplicate ID");
177 return;
179 slot = GetWebGL2Context()->CreateSampler();
182 void HostWebGLContext::CreateShader(const ObjectId id, GLenum type) {
183 auto& slot = mShaderMap[id];
184 if (slot) {
185 MOZ_ASSERT(false, "duplicate ID");
186 return;
188 slot = mContext->CreateShader(type);
191 void HostWebGLContext::CreateSync(const ObjectId id) {
192 auto& slot = mSyncMap[id];
193 if (slot) {
194 MOZ_ASSERT(false, "duplicate ID");
195 return;
197 slot = GetWebGL2Context()->FenceSync(LOCAL_GL_SYNC_GPU_COMMANDS_COMPLETE, 0);
200 void HostWebGLContext::CreateTexture(const ObjectId id) {
201 auto& slot = mTextureMap[id];
202 if (slot) {
203 MOZ_ASSERT(false, "duplicate ID");
204 return;
206 slot = mContext->CreateTexture();
209 void HostWebGLContext::CreateTransformFeedback(const ObjectId id) {
210 auto& slot = mTransformFeedbackMap[id];
211 if (slot) {
212 MOZ_ASSERT(false, "duplicate ID");
213 return;
215 slot = GetWebGL2Context()->CreateTransformFeedback();
218 void HostWebGLContext::CreateVertexArray(const ObjectId id) {
219 auto& slot = mVertexArrayMap[id];
220 if (slot) {
221 MOZ_ASSERT(false, "duplicate ID");
222 return;
224 slot = mContext->CreateVertexArray();
227 ////////////////////////
229 #define _(X) \
230 void HostWebGLContext::Delete##X(const ObjectId id) { m##X##Map.erase(id); }
232 _(Buffer)
233 _(Framebuffer)
234 _(Program)
235 _(Query)
236 _(Renderbuffer)
237 _(Sampler)
238 _(Shader)
239 _(Sync)
240 _(Texture)
241 _(TransformFeedback)
242 _(VertexArray)
244 #undef _
246 } // namespace mozilla