Bug 1787199 [wpt PR 35620] - Add tests for `VisibilityStateEntry`, a=testonly
[gecko.git] / dom / canvas / HostWebGLContext.cpp
blob36029e33ed55555d446a00168477f1f2b74107ad
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/LayersSurfaces.h"
11 #include "MozFramebuffer.h"
12 #include "TexUnpackBlob.h"
13 #include "WebGL2Context.h"
14 #include "WebGLBuffer.h"
15 #include "WebGLContext.h"
16 #include "WebGLFramebuffer.h"
17 #include "WebGLMemoryTracker.h"
18 #include "WebGLParent.h"
19 #include "WebGLProgram.h"
20 #include "WebGLRenderbuffer.h"
21 #include "WebGLSampler.h"
22 #include "WebGLShader.h"
23 #include "WebGLSync.h"
24 #include "WebGLTexture.h"
25 #include "WebGLTransformFeedback.h"
26 #include "WebGLVertexArray.h"
27 #include "WebGLQuery.h"
29 #include "mozilla/StaticMutex.h"
31 namespace mozilla {
33 // -
35 static StaticMutex sContextSetLock MOZ_UNANNOTATED;
37 static std::unordered_set<HostWebGLContext*>& DeferredStaticContextSet() {
38 static std::unordered_set<HostWebGLContext*> sContextSet;
39 return sContextSet;
42 LockedOutstandingContexts::LockedOutstandingContexts()
43 : contexts(DeferredStaticContextSet()) {
44 sContextSetLock.Lock();
47 LockedOutstandingContexts::~LockedOutstandingContexts() {
48 sContextSetLock.Unlock();
51 // -
53 /*static*/
54 UniquePtr<HostWebGLContext> HostWebGLContext::Create(
55 const OwnerData& ownerData, const webgl::InitContextDesc& desc,
56 webgl::InitContextResult* const out) {
57 auto host = WrapUnique(new HostWebGLContext(ownerData));
58 auto webgl = WebGLContext::Create(*host, desc, out);
59 if (!webgl) return nullptr;
60 return host;
63 HostWebGLContext::HostWebGLContext(const OwnerData& ownerData)
64 : mOwnerData(ownerData) {
65 StaticMutexAutoLock lock(sContextSetLock);
66 auto& contexts = DeferredStaticContextSet();
67 (void)contexts.insert(this);
70 HostWebGLContext::~HostWebGLContext() {
71 StaticMutexAutoLock lock(sContextSetLock);
72 auto& contexts = DeferredStaticContextSet();
73 (void)contexts.erase(this);
76 // -
78 void HostWebGLContext::OnContextLoss(const webgl::ContextLossReason reason) {
79 if (mOwnerData.inProcess) {
80 mOwnerData.inProcess->OnContextLoss(reason);
81 } else {
82 (void)mOwnerData.outOfProcess->SendOnContextLoss(reason);
86 void HostWebGLContext::JsWarning(const std::string& text) const {
87 if (mOwnerData.inProcess) {
88 mOwnerData.inProcess->JsWarning(text);
89 return;
91 (void)mOwnerData.outOfProcess->SendJsWarning(text);
94 Maybe<layers::SurfaceDescriptor> HostWebGLContext::GetFrontBuffer(
95 const ObjectId xrFb, const bool webvr) const {
96 return mContext->GetFrontBuffer(AutoResolve(xrFb), webvr);
99 //////////////////////////////////////////////
100 // Creation
102 void HostWebGLContext::CreateBuffer(const ObjectId id) {
103 auto& slot = mBufferMap[id];
104 if (slot) {
105 MOZ_ASSERT(false, "duplicate ID");
106 return;
108 slot = mContext->CreateBuffer();
111 void HostWebGLContext::CreateFramebuffer(const ObjectId id) {
112 auto& slot = mFramebufferMap[id];
113 if (slot) {
114 MOZ_ASSERT(false, "duplicate ID");
115 return;
117 slot = mContext->CreateFramebuffer();
120 bool HostWebGLContext::CreateOpaqueFramebuffer(
121 const ObjectId id, const webgl::OpaqueFramebufferOptions& options) {
122 auto& slot = mFramebufferMap[id];
123 if (slot) {
124 MOZ_ASSERT(false, "duplicate ID");
125 return false;
127 slot = mContext->CreateOpaqueFramebuffer(options);
128 return slot;
131 void HostWebGLContext::CreateProgram(const ObjectId id) {
132 auto& slot = mProgramMap[id];
133 if (slot) {
134 MOZ_ASSERT(false, "duplicate ID");
135 return;
137 slot = mContext->CreateProgram();
140 void HostWebGLContext::CreateQuery(const ObjectId id) {
141 auto& slot = mQueryMap[id];
142 if (slot) {
143 MOZ_ASSERT(false, "duplicate ID");
144 return;
146 slot = mContext->CreateQuery();
149 void HostWebGLContext::CreateRenderbuffer(const ObjectId id) {
150 auto& slot = mRenderbufferMap[id];
151 if (slot) {
152 MOZ_ASSERT(false, "duplicate ID");
153 return;
155 slot = mContext->CreateRenderbuffer();
158 void HostWebGLContext::CreateSampler(const ObjectId id) {
159 auto& slot = mSamplerMap[id];
160 if (slot) {
161 MOZ_ASSERT(false, "duplicate ID");
162 return;
164 slot = GetWebGL2Context()->CreateSampler();
167 void HostWebGLContext::CreateShader(const ObjectId id, GLenum type) {
168 auto& slot = mShaderMap[id];
169 if (slot) {
170 MOZ_ASSERT(false, "duplicate ID");
171 return;
173 slot = mContext->CreateShader(type);
176 void HostWebGLContext::CreateSync(const ObjectId id) {
177 auto& slot = mSyncMap[id];
178 if (slot) {
179 MOZ_ASSERT(false, "duplicate ID");
180 return;
182 slot = GetWebGL2Context()->FenceSync(LOCAL_GL_SYNC_GPU_COMMANDS_COMPLETE, 0);
185 void HostWebGLContext::CreateTexture(const ObjectId id) {
186 auto& slot = mTextureMap[id];
187 if (slot) {
188 MOZ_ASSERT(false, "duplicate ID");
189 return;
191 slot = mContext->CreateTexture();
194 void HostWebGLContext::CreateTransformFeedback(const ObjectId id) {
195 auto& slot = mTransformFeedbackMap[id];
196 if (slot) {
197 MOZ_ASSERT(false, "duplicate ID");
198 return;
200 slot = GetWebGL2Context()->CreateTransformFeedback();
203 void HostWebGLContext::CreateVertexArray(const ObjectId id) {
204 auto& slot = mVertexArrayMap[id];
205 if (slot) {
206 MOZ_ASSERT(false, "duplicate ID");
207 return;
209 slot = mContext->CreateVertexArray();
212 ////////////////////////
214 #define _(X) \
215 void HostWebGLContext::Delete##X(const ObjectId id) { m##X##Map.erase(id); }
217 _(Buffer)
218 _(Framebuffer)
219 _(Program)
220 _(Query)
221 _(Renderbuffer)
222 _(Sampler)
223 _(Shader)
224 _(Sync)
225 _(Texture)
226 _(TransformFeedback)
227 _(VertexArray)
229 #undef _
231 } // namespace mozilla