HostWebGLContext.cpp (6043B)
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/. */ 5 6 #include "HostWebGLContext.h" 7 8 #include "CompositableHost.h" 9 #include "MozFramebuffer.h" 10 #include "TexUnpackBlob.h" 11 #include "WebGL2Context.h" 12 #include "WebGLBuffer.h" 13 #include "WebGLContext.h" 14 #include "WebGLFramebuffer.h" 15 #include "WebGLMemoryTracker.h" 16 #include "WebGLParent.h" 17 #include "WebGLProgram.h" 18 #include "WebGLQuery.h" 19 #include "WebGLRenderbuffer.h" 20 #include "WebGLSampler.h" 21 #include "WebGLShader.h" 22 #include "WebGLSync.h" 23 #include "WebGLTexture.h" 24 #include "WebGLTransformFeedback.h" 25 #include "WebGLVertexArray.h" 26 #include "mozilla/StaticMutex.h" 27 #include "mozilla/layers/LayersSurfaces.h" 28 29 namespace mozilla { 30 31 // - 32 33 static StaticMutex sContextSetLock MOZ_UNANNOTATED; 34 35 static std::unordered_set<HostWebGLContext*>& DeferredStaticContextSet() { 36 static std::unordered_set<HostWebGLContext*> sContextSet; 37 return sContextSet; 38 } 39 40 LockedOutstandingContexts::LockedOutstandingContexts() 41 : contexts(DeferredStaticContextSet()) { 42 sContextSetLock.Lock(); 43 } 44 45 LockedOutstandingContexts::~LockedOutstandingContexts() { 46 sContextSetLock.Unlock(); 47 } 48 49 // - 50 51 /*static*/ 52 std::unique_ptr<HostWebGLContext> HostWebGLContext::Create( 53 const OwnerData& ownerData, const webgl::InitContextDesc& desc, 54 webgl::InitContextResult* const out) { 55 auto host = 56 std::unique_ptr<HostWebGLContext>(new HostWebGLContext(ownerData)); 57 auto webgl = WebGLContext::Create(host.get(), desc, out); 58 if (!webgl) return nullptr; 59 return host; 60 } 61 62 HostWebGLContext::HostWebGLContext(const OwnerData& ownerData) 63 : mOwnerData(ownerData) { 64 StaticMutexAutoLock lock(sContextSetLock); 65 auto& contexts = DeferredStaticContextSet(); 66 (void)contexts.insert(this); 67 } 68 69 HostWebGLContext::~HostWebGLContext() { 70 StaticMutexAutoLock lock(sContextSetLock); 71 auto& contexts = DeferredStaticContextSet(); 72 (void)contexts.erase(this); 73 } 74 75 // - 76 77 void HostWebGLContext::OnContextLoss(const webgl::ContextLossReason reason) { 78 if (mOwnerData.inProcess) { 79 mOwnerData.inProcess->OnContextLoss(reason); 80 } else { 81 (void)mOwnerData.outOfProcess->SendOnContextLoss(reason); 82 } 83 } 84 85 void HostWebGLContext::JsWarning(const std::string& text) const { 86 if (mOwnerData.inProcess) { 87 mOwnerData.inProcess->JsWarning(text); 88 return; 89 } 90 (void)mOwnerData.outOfProcess->SendJsWarning(text); 91 } 92 93 Maybe<layers::SurfaceDescriptor> HostWebGLContext::GetFrontBuffer( 94 const ObjectId xrFb, const bool webvr) const { 95 return mContext->GetFrontBuffer(AutoResolve(xrFb), webvr); 96 } 97 98 ////////////////////////////////////////////// 99 // Creation 100 101 void HostWebGLContext::CreateBuffer(const ObjectId id) { 102 auto& slot = mBufferMap[id]; 103 if (slot) { 104 MOZ_ASSERT(false, "duplicate ID"); 105 return; 106 } 107 slot = mContext->CreateBuffer(); 108 } 109 110 void HostWebGLContext::CreateFramebuffer(const ObjectId id) { 111 auto& slot = mFramebufferMap[id]; 112 if (slot) { 113 MOZ_ASSERT(false, "duplicate ID"); 114 return; 115 } 116 slot = mContext->CreateFramebuffer(); 117 } 118 119 bool HostWebGLContext::CreateOpaqueFramebuffer( 120 const ObjectId id, const webgl::OpaqueFramebufferOptions& options) { 121 auto& slot = mFramebufferMap[id]; 122 if (slot) { 123 MOZ_ASSERT(false, "duplicate ID"); 124 return false; 125 } 126 slot = mContext->CreateOpaqueFramebuffer(options); 127 return slot; 128 } 129 130 void HostWebGLContext::CreateProgram(const ObjectId id) { 131 auto& slot = mProgramMap[id]; 132 if (slot) { 133 MOZ_ASSERT(false, "duplicate ID"); 134 return; 135 } 136 slot = mContext->CreateProgram(); 137 } 138 139 void HostWebGLContext::CreateQuery(const ObjectId id) { 140 auto& slot = mQueryMap[id]; 141 if (slot) { 142 MOZ_ASSERT(false, "duplicate ID"); 143 return; 144 } 145 slot = mContext->CreateQuery(); 146 } 147 148 void HostWebGLContext::CreateRenderbuffer(const ObjectId id) { 149 auto& slot = mRenderbufferMap[id]; 150 if (slot) { 151 MOZ_ASSERT(false, "duplicate ID"); 152 return; 153 } 154 slot = mContext->CreateRenderbuffer(); 155 } 156 157 void HostWebGLContext::CreateSampler(const ObjectId id) { 158 auto& slot = mSamplerMap[id]; 159 if (slot) { 160 MOZ_ASSERT(false, "duplicate ID"); 161 return; 162 } 163 slot = GetWebGL2Context()->CreateSampler(); 164 } 165 166 void HostWebGLContext::CreateShader(const ObjectId id, GLenum type) { 167 auto& slot = mShaderMap[id]; 168 if (slot) { 169 MOZ_ASSERT(false, "duplicate ID"); 170 return; 171 } 172 slot = mContext->CreateShader(type); 173 } 174 175 void HostWebGLContext::CreateSync(const ObjectId id) { 176 auto& slot = mSyncMap[id]; 177 if (slot) { 178 MOZ_ASSERT(false, "duplicate ID"); 179 return; 180 } 181 slot = GetWebGL2Context()->FenceSync(LOCAL_GL_SYNC_GPU_COMMANDS_COMPLETE, 0); 182 183 if (!slot) return; 184 185 slot->OnCompleteTaskAdd([host = WeakPtr{this}, id]() { 186 if (!host) return; 187 if (host->mOwnerData.inProcess) { 188 host->mOwnerData.inProcess->OnSyncComplete(id); 189 } else if (host->mOwnerData.outOfProcess) { 190 (void)host->mOwnerData.outOfProcess->SendOnSyncComplete(id); 191 } 192 }); 193 } 194 195 void HostWebGLContext::CreateTexture(const ObjectId id) { 196 auto& slot = mTextureMap[id]; 197 if (slot) { 198 MOZ_ASSERT(false, "duplicate ID"); 199 return; 200 } 201 slot = mContext->CreateTexture(); 202 } 203 204 void HostWebGLContext::CreateTransformFeedback(const ObjectId id) { 205 auto& slot = mTransformFeedbackMap[id]; 206 if (slot) { 207 MOZ_ASSERT(false, "duplicate ID"); 208 return; 209 } 210 slot = GetWebGL2Context()->CreateTransformFeedback(); 211 } 212 213 void HostWebGLContext::CreateVertexArray(const ObjectId id) { 214 auto& slot = mVertexArrayMap[id]; 215 if (slot) { 216 MOZ_ASSERT(false, "duplicate ID"); 217 return; 218 } 219 slot = mContext->CreateVertexArray(); 220 } 221 222 //////////////////////// 223 224 #define _(X) \ 225 void HostWebGLContext::Delete##X(const ObjectId id) { m##X##Map.erase(id); } 226 227 _(Buffer) 228 _(Framebuffer) 229 _(Program) 230 _(Query) 231 _(Renderbuffer) 232 _(Sampler) 233 _(Shader) 234 _(Sync) 235 _(Texture) 236 _(TransformFeedback) 237 _(VertexArray) 238 239 #undef _ 240 241 } // namespace mozilla