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"
35 static StaticMutex sContextSetLock MOZ_UNANNOTATED
;
37 static std::unordered_set
<HostWebGLContext
*>& DeferredStaticContextSet() {
38 static std::unordered_set
<HostWebGLContext
*> sContextSet
;
42 LockedOutstandingContexts::LockedOutstandingContexts()
43 : contexts(DeferredStaticContextSet()) {
44 sContextSetLock
.Lock();
47 LockedOutstandingContexts::~LockedOutstandingContexts() {
48 sContextSetLock
.Unlock();
54 std::unique_ptr
<HostWebGLContext
> HostWebGLContext::Create(
55 const OwnerData
& ownerData
, const webgl::InitContextDesc
& desc
,
56 webgl::InitContextResult
* const out
) {
58 std::unique_ptr
<HostWebGLContext
>(new HostWebGLContext(ownerData
));
59 auto webgl
= WebGLContext::Create(host
.get(), desc
, out
);
60 if (!webgl
) return nullptr;
64 HostWebGLContext::HostWebGLContext(const OwnerData
& ownerData
)
65 : mOwnerData(ownerData
) {
66 StaticMutexAutoLock
lock(sContextSetLock
);
67 auto& contexts
= DeferredStaticContextSet();
68 (void)contexts
.insert(this);
71 HostWebGLContext::~HostWebGLContext() {
72 StaticMutexAutoLock
lock(sContextSetLock
);
73 auto& contexts
= DeferredStaticContextSet();
74 (void)contexts
.erase(this);
79 void HostWebGLContext::OnContextLoss(const webgl::ContextLossReason reason
) {
80 if (mOwnerData
.inProcess
) {
81 mOwnerData
.inProcess
->OnContextLoss(reason
);
83 (void)mOwnerData
.outOfProcess
->SendOnContextLoss(reason
);
87 void HostWebGLContext::JsWarning(const std::string
& text
) const {
88 if (mOwnerData
.inProcess
) {
89 mOwnerData
.inProcess
->JsWarning(text
);
92 (void)mOwnerData
.outOfProcess
->SendJsWarning(text
);
95 Maybe
<layers::SurfaceDescriptor
> HostWebGLContext::GetFrontBuffer(
96 const ObjectId xrFb
, const bool webvr
) const {
97 return mContext
->GetFrontBuffer(AutoResolve(xrFb
), webvr
);
100 //////////////////////////////////////////////
103 void HostWebGLContext::CreateBuffer(const ObjectId id
) {
104 auto& slot
= mBufferMap
[id
];
106 MOZ_ASSERT(false, "duplicate ID");
109 slot
= mContext
->CreateBuffer();
112 void HostWebGLContext::CreateFramebuffer(const ObjectId id
) {
113 auto& slot
= mFramebufferMap
[id
];
115 MOZ_ASSERT(false, "duplicate ID");
118 slot
= mContext
->CreateFramebuffer();
121 bool HostWebGLContext::CreateOpaqueFramebuffer(
122 const ObjectId id
, const webgl::OpaqueFramebufferOptions
& options
) {
123 auto& slot
= mFramebufferMap
[id
];
125 MOZ_ASSERT(false, "duplicate ID");
128 slot
= mContext
->CreateOpaqueFramebuffer(options
);
132 void HostWebGLContext::CreateProgram(const ObjectId id
) {
133 auto& slot
= mProgramMap
[id
];
135 MOZ_ASSERT(false, "duplicate ID");
138 slot
= mContext
->CreateProgram();
141 void HostWebGLContext::CreateQuery(const ObjectId id
) {
142 auto& slot
= mQueryMap
[id
];
144 MOZ_ASSERT(false, "duplicate ID");
147 slot
= mContext
->CreateQuery();
150 void HostWebGLContext::CreateRenderbuffer(const ObjectId id
) {
151 auto& slot
= mRenderbufferMap
[id
];
153 MOZ_ASSERT(false, "duplicate ID");
156 slot
= mContext
->CreateRenderbuffer();
159 void HostWebGLContext::CreateSampler(const ObjectId id
) {
160 auto& slot
= mSamplerMap
[id
];
162 MOZ_ASSERT(false, "duplicate ID");
165 slot
= GetWebGL2Context()->CreateSampler();
168 void HostWebGLContext::CreateShader(const ObjectId id
, GLenum type
) {
169 auto& slot
= mShaderMap
[id
];
171 MOZ_ASSERT(false, "duplicate ID");
174 slot
= mContext
->CreateShader(type
);
177 void HostWebGLContext::CreateSync(const ObjectId id
) {
178 auto& slot
= mSyncMap
[id
];
180 MOZ_ASSERT(false, "duplicate ID");
183 slot
= GetWebGL2Context()->FenceSync(LOCAL_GL_SYNC_GPU_COMMANDS_COMPLETE
, 0);
187 slot
->OnCompleteTaskAdd([host
= WeakPtr
{this}, id
]() {
189 if (host
->mOwnerData
.inProcess
) {
190 host
->mOwnerData
.inProcess
->OnSyncComplete(id
);
191 } else if (host
->mOwnerData
.outOfProcess
) {
192 (void)host
->mOwnerData
.outOfProcess
->SendOnSyncComplete(id
);
197 void HostWebGLContext::CreateTexture(const ObjectId id
) {
198 auto& slot
= mTextureMap
[id
];
200 MOZ_ASSERT(false, "duplicate ID");
203 slot
= mContext
->CreateTexture();
206 void HostWebGLContext::CreateTransformFeedback(const ObjectId id
) {
207 auto& slot
= mTransformFeedbackMap
[id
];
209 MOZ_ASSERT(false, "duplicate ID");
212 slot
= GetWebGL2Context()->CreateTransformFeedback();
215 void HostWebGLContext::CreateVertexArray(const ObjectId id
) {
216 auto& slot
= mVertexArrayMap
[id
];
218 MOZ_ASSERT(false, "duplicate ID");
221 slot
= mContext
->CreateVertexArray();
224 ////////////////////////
227 void HostWebGLContext::Delete##X(const ObjectId id) { m##X##Map.erase(id); }
243 } // namespace mozilla