Bug 1245813 - Make TextureHost bullet-proof against changing its compositor. r=dvander
authorNicolas Silva <nsilva@mozilla.com>
Tue, 22 Mar 2016 15:28:27 +0100
changeset 290044 2e636d5fcda8ec844e2fc917cccd4126b80f4137
parent 290043 bc476c66d225412b0aaace0de9e06e3d1eaef15b
child 290045 8535142add0fce0b838b944dfdab767e143a82ac
push id30114
push usercbook@mozilla.com
push dateThu, 24 Mar 2016 15:15:54 +0000
treeherdermozilla-central@24c5fbde4488 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersdvander
bugs1245813
milestone48.0a1
first release with
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
last release without
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
Bug 1245813 - Make TextureHost bullet-proof against changing its compositor. r=dvander
gfx/layers/composite/ImageHost.cpp
gfx/layers/composite/TextureHost.cpp
gfx/layers/d3d11/TextureD3D11.cpp
gfx/layers/d3d9/TextureD3D9.cpp
gfx/layers/opengl/MacIOSurfaceTextureHostOGL.cpp
gfx/layers/opengl/TextureHostOGL.cpp
gfx/layers/opengl/TextureHostOGL.h
--- a/gfx/layers/composite/ImageHost.cpp
+++ b/gfx/layers/composite/ImageHost.cpp
@@ -325,19 +325,19 @@ ImageHost::Composite(LayerComposite* aLa
     return;
   }
 
   if (uint32_t(imageIndex) + 1 < mImages.Length()) {
     GetCompositor()->CompositeUntil(mImages[imageIndex + 1].mTimeStamp + TimeDuration::FromMilliseconds(BIAS_TIME_MS));
   }
 
   TimedImage* img = &mImages[imageIndex];
+  img->mTextureHost->SetCompositor(GetCompositor());
   SetCurrentTextureHost(img->mTextureHost);
   // Make sure the front buffer has a compositor
-  mCurrentTextureHost->SetCompositor(GetCompositor());
   if (mCurrentTextureSource) {
     mCurrentTextureSource->SetCompositor(GetCompositor());
   }
 
   {
     AutoLockCompositableHost autoLock(this);
     if (autoLock.Failed()) {
       NS_WARNING("failed to lock front buffer");
--- a/gfx/layers/composite/TextureHost.cpp
+++ b/gfx/layers/composite/TextureHost.cpp
@@ -437,20 +437,23 @@ BufferTextureHost::UpdatedInternal(const
 
 void
 BufferTextureHost::SetCompositor(Compositor* aCompositor)
 {
   MOZ_ASSERT(aCompositor);
   if (mCompositor == aCompositor) {
     return;
   }
-  RefPtr<TextureSource> it = mFirstSource;
-  while (it) {
-    it->SetCompositor(aCompositor);
-    it = it->GetNextSibling();
+  if (aCompositor && mCompositor &&
+      aCompositor->GetBackendType() == mCompositor->GetBackendType()) {
+    RefPtr<TextureSource> it = mFirstSource;
+    while (it) {
+      it->SetCompositor(aCompositor);
+      it = it->GetNextSibling();
+    }
   }
   if (mFirstSource && mFirstSource->IsOwnedBy(this)) {
     mFirstSource->SetOwner(nullptr);
   }
   mFirstSource = nullptr;
   mCompositor = aCompositor;
 }
 
--- a/gfx/layers/d3d11/TextureD3D11.cpp
+++ b/gfx/layers/d3d11/TextureD3D11.cpp
@@ -629,20 +629,31 @@ DXGITextureHostD3D11::OpenSharedHandle()
 RefPtr<ID3D11Device>
 DXGITextureHostD3D11::GetDevice()
 {
   RefPtr<ID3D11Device> device;
   gfxWindowsPlatform::GetPlatform()->GetD3D11Device(&device);
   return device;
 }
 
+static bool AssertD3D11Compositor(Compositor* aCompositor)
+{
+  bool ok = aCompositor && aCompositor->GetBackendType() == LayersBackend::LAYERS_D3D11;
+  MOZ_ASSERT(ok);
+  return ok;
+}
+
 void
 DXGITextureHostD3D11::SetCompositor(Compositor* aCompositor)
 {
-  MOZ_ASSERT(aCompositor);
+  if (!AssertD3D11Compositor(aCompositor)) {
+    mCompositor = nullptr;
+    mTextureSource = nullptr;
+    return;
+  }
   mCompositor = static_cast<CompositorD3D11*>(aCompositor);
   if (mTextureSource) {
     mTextureSource->SetCompositor(aCompositor);
   }
 }
 
 bool
 DXGITextureHostD3D11::Lock()
@@ -739,17 +750,23 @@ DXGIYCbCrTextureHostD3D11::GetDevice()
   RefPtr<ID3D11Device> device;
   gfxWindowsPlatform::GetPlatform()->GetD3D11Device(&device);
   return device;
 }
 
 void
 DXGIYCbCrTextureHostD3D11::SetCompositor(Compositor* aCompositor)
 {
-  MOZ_ASSERT(aCompositor);
+  if (!AssertD3D11Compositor(aCompositor)) {
+    mCompositor = nullptr;
+    mTextureSources[0] = nullptr;
+    mTextureSources[1] = nullptr;
+    mTextureSources[2] = nullptr;
+    return;
+  }
   mCompositor = static_cast<CompositorD3D11*>(aCompositor);
   if (mTextureSources[0]) {
     mTextureSources[0]->SetCompositor(aCompositor);
   }
 }
 
 bool
 DXGIYCbCrTextureHostD3D11::Lock()
@@ -966,17 +983,19 @@ DataTextureSourceD3D11::GetTileRect()
 {
   IntRect rect = GetTileRect(mCurrentTile);
   return IntRect(rect.x, rect.y, rect.width, rect.height);
 }
 
 void
 DataTextureSourceD3D11::SetCompositor(Compositor* aCompositor)
 {
-  MOZ_ASSERT(aCompositor);
+  if (!AssertD3D11Compositor(aCompositor)) {
+    return;
+  }
   mCompositor = static_cast<CompositorD3D11*>(aCompositor);
   if (mNextSibling) {
     mNextSibling->SetCompositor(aCompositor);
   }
 }
 
 CompositingRenderTargetD3D11::CompositingRenderTargetD3D11(ID3D11Texture2D* aTexture,
                                                            const gfx::IntPoint& aOrigin,
--- a/gfx/layers/d3d9/TextureD3D9.cpp
+++ b/gfx/layers/d3d9/TextureD3D9.cpp
@@ -488,20 +488,30 @@ DataTextureSourceD3D9::Update(gfx::DataS
         return false;
       }
     }
   }
 
   return true;
 }
 
+static bool AssertD3D9Compositor(Compositor* aCompositor)
+{
+  bool ok = aCompositor && aCompositor->GetBackendType() == LayersBackend::LAYERS_D3D9;
+  MOZ_ASSERT(ok);
+  return ok;
+}
+
 void
 DataTextureSourceD3D9::SetCompositor(Compositor* aCompositor)
 {
-  MOZ_ASSERT(aCompositor);
+  if (!AssertD3D9Compositor(aCompositor)) {
+    Reset();
+    return;
+  }
   CompositorD3D9* d3dCompositor = static_cast<CompositorD3D9*>(aCompositor);
   if (mCompositor && mCompositor != d3dCompositor) {
     Reset();
   }
   mCompositor = d3dCompositor;
 }
 
 void
@@ -902,16 +912,21 @@ IDirect3DDevice9*
 TextureHostD3D9::GetDevice()
 {
   return mCompositor ? mCompositor->device() : nullptr;
 }
 
 void
 TextureHostD3D9::SetCompositor(Compositor* aCompositor)
 {
+  if (!AssertD3D9Compositor(aCompositor)) {
+    mCompositor = nullptr;
+    mTextureSource = nullptr;
+    return;
+  }
   mCompositor = static_cast<CompositorD3D9*>(aCompositor);
   if (mTextureSource) {
     mTextureSource->SetCompositor(aCompositor);
   }
 }
 
 bool
 TextureHostD3D9::BindTextureSource(CompositableTextureSourceRef& aTexture)
@@ -1020,17 +1035,21 @@ DXGITextureHostD3D9::Unlock()
 {
   MOZ_ASSERT(mIsLocked);
   mIsLocked = false;
 }
 
 void
 DXGITextureHostD3D9::SetCompositor(Compositor* aCompositor)
 {
-  MOZ_ASSERT(aCompositor);
+  if (!AssertD3D9Compositor(aCompositor)) {
+    mCompositor = nullptr;
+    mTextureSource = nullptr;
+    return;
+  }
   mCompositor = static_cast<CompositorD3D9*>(aCompositor);
 }
 
 void
 DXGITextureHostD3D9::DeallocateDeviceData()
 {
   mTextureSource = nullptr;
 }
@@ -1052,17 +1071,23 @@ IDirect3DDevice9*
 DXGIYCbCrTextureHostD3D9::GetDevice()
 {
   return mCompositor ? mCompositor->device() : nullptr;
 }
 
 void
 DXGIYCbCrTextureHostD3D9::SetCompositor(Compositor* aCompositor)
 {
-  MOZ_ASSERT(aCompositor);
+  if (!AssertD3D9Compositor(aCompositor)) {
+    mCompositor = nullptr;
+    mTextureSources[0] = nullptr;
+    mTextureSources[1] = nullptr;
+    mTextureSources[2] = nullptr;
+    return;
+  }
   mCompositor = static_cast<CompositorD3D9*>(aCompositor);
 }
 
 bool
 DXGIYCbCrTextureHostD3D9::Lock()
 {
   if (!GetDevice()) {
     NS_WARNING("trying to lock a TextureHost without a D3D device");
--- a/gfx/layers/opengl/MacIOSurfaceTextureHostOGL.cpp
+++ b/gfx/layers/opengl/MacIOSurfaceTextureHostOGL.cpp
@@ -43,17 +43,17 @@ MacIOSurfaceTextureHostOGL::CreateTextur
                              // XXX: This isn't really correct (but isn't used), we should be using the
                              // format of the individual plane, not of the whole buffer.
                              mSurface->GetFormat());
 }
 
 bool
 MacIOSurfaceTextureHostOGL::Lock()
 {
-  if (!mCompositor || !mSurface) {
+  if (!gl() || !gl()->MakeCurrent() || !mSurface) {
     return false;
   }
 
   if (!mTextureSource) {
     mTextureSource = CreateTextureSourceForPlane(0);
 
     RefPtr<TextureSource> prev = mTextureSource;
     for (size_t i = 1; i < mSurface->GetPlaneCount(); i++) {
@@ -63,16 +63,21 @@ MacIOSurfaceTextureHostOGL::Lock()
     }
   }
   return true;
 }
 
 void
 MacIOSurfaceTextureHostOGL::SetCompositor(Compositor* aCompositor)
 {
+  if (!AssertGLCompositor(aCompositor)) {
+    mTextureSource = nullptr;
+    mCompositor = nullptr;
+    return;
+  }
   CompositorOGL* glCompositor = static_cast<CompositorOGL*>(aCompositor);
   mCompositor = glCompositor;
   if (mTextureSource) {
     mTextureSource->SetCompositor(glCompositor);
   }
 }
 
 gfx::SurfaceFormat
@@ -89,22 +94,29 @@ gfx::IntSize
 MacIOSurfaceTextureHostOGL::GetSize() const {
   if (!mSurface) {
     return gfx::IntSize();
   }
   return gfx::IntSize(mSurface->GetDevicePixelWidth(),
                       mSurface->GetDevicePixelHeight());
 }
 
+gl::GLContext*
+MacIOSurfaceTextureHostOGL::gl() const
+{
+  return mCompositor ? mCompositor->gl() : nullptr;
+}
+
 MacIOSurfaceTextureSourceOGL::MacIOSurfaceTextureSourceOGL(
                                 CompositorOGL* aCompositor,
                                 MacIOSurface* aSurface)
   : mCompositor(aCompositor)
   , mSurface(aSurface)
 {
+  MOZ_ASSERT(aCompositor);
   MOZ_COUNT_CTOR(MacIOSurfaceTextureSourceOGL);
 }
 
 MacIOSurfaceTextureSourceOGL::~MacIOSurfaceTextureSourceOGL()
 {
   MOZ_COUNT_DTOR(MacIOSurfaceTextureSourceOGL);
 }
 
@@ -120,31 +132,37 @@ MacIOSurfaceTextureSourceOGL::GetFormat(
 {
   return mSurface->HasAlpha() ? gfx::SurfaceFormat::R8G8B8A8
                               : gfx::SurfaceFormat::R8G8B8X8;
 }
 
 void
 MacIOSurfaceTextureSourceOGL::BindTexture(GLenum aTextureUnit, gfx::Filter aFilter)
 {
-  if (!gl()) {
-    NS_WARNING("Trying to bind a texture without a GLContext");
+  gl::GLContext* gl = this->gl();
+  if (!gl || !gl->MakeCurrent()) {
+    NS_WARNING("Trying to bind a texture without a working GLContext");
     return;
   }
   GLuint tex = mCompositor->GetTemporaryTexture(GetTextureTarget(), aTextureUnit);
 
-  gl()->fActiveTexture(aTextureUnit);
-  gl()->fBindTexture(LOCAL_GL_TEXTURE_RECTANGLE_ARB, tex);
-  mSurface->CGLTexImageIOSurface2D(gl::GLContextCGL::Cast(gl())->GetCGLContext());
-  ApplyFilterToBoundTexture(gl(), aFilter, LOCAL_GL_TEXTURE_RECTANGLE_ARB);
+  gl->fActiveTexture(aTextureUnit);
+  gl->fBindTexture(LOCAL_GL_TEXTURE_RECTANGLE_ARB, tex);
+  mSurface->CGLTexImageIOSurface2D(gl::GLContextCGL::Cast(gl)->GetCGLContext());
+  ApplyFilterToBoundTexture(gl, aFilter, LOCAL_GL_TEXTURE_RECTANGLE_ARB);
 }
 
 void
 MacIOSurfaceTextureSourceOGL::SetCompositor(Compositor* aCompositor)
 {
+  if (!AssertGLCompositor(aCompositor)) {
+    mCompositor = nullptr;
+    return;
+  }
+
   mCompositor = static_cast<CompositorOGL*>(aCompositor);
   if (mNextSibling) {
     mNextSibling->SetCompositor(aCompositor);
   }
 }
 
 gl::GLContext*
 MacIOSurfaceTextureSourceOGL::gl() const
--- a/gfx/layers/opengl/TextureHostOGL.cpp
+++ b/gfx/layers/opengl/TextureHostOGL.cpp
@@ -134,17 +134,17 @@ FlagsToGLFlags(TextureFlags aFlags)
 
 bool
 TextureImageTextureSourceOGL::Update(gfx::DataSourceSurface* aSurface,
                                      nsIntRegion* aDestRegion,
                                      gfx::IntPoint* aSrcOffset)
 {
   GLContext *gl = mCompositor->gl();
   MOZ_ASSERT(gl);
-  if (!gl) {
+  if (!gl || !gl->MakeCurrent()) {
     NS_WARNING("trying to update TextureImageTextureSourceOGL without a GLContext");
     return false;
   }
   if (!aSurface) {
     gfxCriticalError() << "Invalid surface for OGL update";
     return false;
   }
   MOZ_ASSERT(aSurface);
@@ -226,23 +226,32 @@ TextureImageTextureSourceOGL::CopyTo(con
     aDest->AsSourceOGL()->AsTextureImageTextureSource();
   MOZ_ASSERT(dest, "Incompatible destination type!");
 
   mCompositor->BlitTextureImageHelper()->BlitTextureImage(mTexImage, aSourceRect,
                                                   dest->mTexImage, aDestRect);
   dest->mTexImage->MarkValid();
 }
 
+bool AssertGLCompositor(Compositor* aCompositor)
+{
+  bool ok = aCompositor && aCompositor->GetBackendType() == LayersBackend::LAYERS_OPENGL;
+  MOZ_ASSERT(ok);
+  return ok;
+}
+
 void
 TextureImageTextureSourceOGL::SetCompositor(Compositor* aCompositor)
 {
-  MOZ_ASSERT(aCompositor);
+  if (!AssertGLCompositor(aCompositor)) {
+    DeallocateDeviceData();
+    return;
+  }
   CompositorOGL* glCompositor = static_cast<CompositorOGL*>(aCompositor);
-
-  if (!glCompositor || (mCompositor != glCompositor)) {
+  if (mCompositor != glCompositor) {
     DeallocateDeviceData();
     mCompositor = glCompositor;
   }
 }
 
 gfx::IntSize
 TextureImageTextureSourceOGL::GetSize() const
 {
@@ -313,39 +322,42 @@ GLTextureSource::DeallocateDeviceData()
   if (!mExternallyOwned) {
     DeleteTextureHandle();
   }
 }
 
 void
 GLTextureSource::DeleteTextureHandle()
 {
-  if (mTextureHandle != 0 && gl() && gl()->MakeCurrent()) {
-    gl()->fDeleteTextures(1, &mTextureHandle);
+  GLContext* gl = this->gl();
+  if (mTextureHandle != 0 && gl && gl->MakeCurrent()) {
+    gl->fDeleteTextures(1, &mTextureHandle);
   }
   mTextureHandle = 0;
 }
 
 void
 GLTextureSource::BindTexture(GLenum aTextureUnit, gfx::Filter aFilter)
 {
-  MOZ_ASSERT(gl());
   MOZ_ASSERT(mTextureHandle != 0);
-  if (!gl()) {
+  GLContext* gl = this->gl();
+  if (gl || !gl->MakeCurrent()) {
     return;
   }
-  gl()->fActiveTexture(aTextureUnit);
-  gl()->fBindTexture(mTextureTarget, mTextureHandle);
-  ApplyFilterToBoundTexture(gl(), aFilter, mTextureTarget);
+  gl->fActiveTexture(aTextureUnit);
+  gl->fBindTexture(mTextureTarget, mTextureHandle);
+  ApplyFilterToBoundTexture(gl, aFilter, mTextureTarget);
 }
 
 void
 GLTextureSource::SetCompositor(Compositor* aCompositor)
 {
-  MOZ_ASSERT(aCompositor);
+  if (!AssertGLCompositor(aCompositor)) {
+    return;
+  }
   mCompositor = static_cast<CompositorOGL*>(aCompositor);
   if (mNextSibling) {
     mNextSibling->SetCompositor(aCompositor);
   }
 }
 
 bool
 GLTextureSource::IsValid() const
@@ -379,36 +391,40 @@ SurfaceTextureSource::SurfaceTextureSour
   , mSize(aSize)
 {
 }
 
 void
 SurfaceTextureSource::BindTexture(GLenum aTextureUnit, gfx::Filter aFilter)
 {
   MOZ_ASSERT(mSurfTex);
-  if (!gl()) {
+  GLContext* gl = this->gl();
+  if (!gl || !gl->MakeCurrent()) {
     NS_WARNING("Trying to bind a texture without a GLContext");
     return;
   }
 
-  gl()->fActiveTexture(aTextureUnit);
+  gl->fActiveTexture(aTextureUnit);
 
   // SurfaceTexture spams us if there are any existing GL errors, so
   // we'll clear them here in order to avoid that.
-  gl()->FlushErrors();
+  gl->FlushErrors();
 
   mSurfTex->UpdateTexImage();
 
-  ApplyFilterToBoundTexture(gl(), aFilter, mTextureTarget);
+  ApplyFilterToBoundTexture(gl, aFilter, mTextureTarget);
 }
 
 void
 SurfaceTextureSource::SetCompositor(Compositor* aCompositor)
 {
-  MOZ_ASSERT(aCompositor);
+  if (!AssertGLCompositor(aCompositor)) {
+    DeallocateDeviceData();
+    return;
+  }
   if (mCompositor != aCompositor) {
     DeallocateDeviceData();
   }
 
   mCompositor = static_cast<CompositorOGL*>(aCompositor);
 }
 
 bool
@@ -461,58 +477,62 @@ SurfaceTextureHost::gl() const
 {
   return mCompositor ? mCompositor->gl() : nullptr;
 }
 
 bool
 SurfaceTextureHost::Lock()
 {
   MOZ_ASSERT(mSurfTex);
-  if (!mCompositor) {
+  GLContext* gl = this->gl();
+  if (!gl || !gl->MakeCurrent()) {
     return false;
   }
 
   if (!mTextureSource) {
     gfx::SurfaceFormat format = gfx::SurfaceFormat::R8G8B8A8;
     GLenum target = LOCAL_GL_TEXTURE_EXTERNAL;
     GLenum wrapMode = LOCAL_GL_CLAMP_TO_EDGE;
     mTextureSource = new SurfaceTextureSource(mCompositor,
                                               mSurfTex,
                                               format,
                                               target,
                                               wrapMode,
                                               mSize);
   }
 
-  return NS_SUCCEEDED(mSurfTex->Attach(gl()));
+  return NS_SUCCEEDED(mSurfTex->Attach(gl);
 }
 
 void
 SurfaceTextureHost::Unlock()
 {
   MOZ_ASSERT(mSurfTex);
   mSurfTex->Detach();
 }
 
 void
 SurfaceTextureHost::SetCompositor(Compositor* aCompositor)
 {
-  MOZ_ASSERT(aCompositor);
+  if (!AssertGLCompositor(aCompositor)) {
+    DeallocateDeviceData();
+    return;
+  }
   CompositorOGL* glCompositor = static_cast<CompositorOGL*>(aCompositor);
   mCompositor = glCompositor;
   if (mTextureSource) {
     mTextureSource->SetCompositor(glCompositor);
   }
 }
 
 gfx::SurfaceFormat
 SurfaceTextureHost::GetFormat() const
 {
   MOZ_ASSERT(mTextureSource);
-  return mTextureSource->GetFormat();
+  return mTextureSource ? mTextureSource->GetFormat() : gfx::SurfaceFormat::UNKNOWN;
 }
 
 void
 SurfaceTextureHost::DeallocateDeviceData()
 {
   if (mTextureSource) {
     mTextureSource->DeallocateDeviceData();
   }
@@ -540,38 +560,42 @@ EGLImageTextureSource::EGLImageTextureSo
 {
   MOZ_ASSERT(mTextureTarget == LOCAL_GL_TEXTURE_2D ||
              mTextureTarget == LOCAL_GL_TEXTURE_EXTERNAL);
 }
 
 void
 EGLImageTextureSource::BindTexture(GLenum aTextureUnit, gfx::Filter aFilter)
 {
-  if (!gl()) {
+  GLContext* gl = this->gl();
+  if (!gl || !gl->MakeCurrent()) {
     NS_WARNING("Trying to bind a texture without a GLContext");
     return;
   }
 
-  MOZ_ASSERT(DoesEGLContextSupportSharingWithEGLImage(gl()),
+  MOZ_ASSERT(DoesEGLContextSupportSharingWithEGLImage(gl),
              "EGLImage not supported or disabled in runtime");
 
   GLuint tex = mCompositor->GetTemporaryTexture(mTextureTarget, aTextureUnit);
 
-  gl()->fActiveTexture(aTextureUnit);
-  gl()->fBindTexture(mTextureTarget, tex);
+  gl->fActiveTexture(aTextureUnit);
+  gl->fBindTexture(mTextureTarget, tex);
 
-  gl()->fEGLImageTargetTexture2D(mTextureTarget, mImage);
+  gl->fEGLImageTargetTexture2D(mTextureTarget, mImage);
 
-  ApplyFilterToBoundTexture(gl(), aFilter, mTextureTarget);
+  ApplyFilterToBoundTexture(gl, aFilter, mTextureTarget);
 }
 
 void
 EGLImageTextureSource::SetCompositor(Compositor* aCompositor)
 {
-  MOZ_ASSERT(aCompositor);
+  if (!AssertGLCompositor(aCompositor)) {
+    mCompositor = nullptr;
+    return;
+  }
   mCompositor = static_cast<CompositorOGL*>(aCompositor);
 }
 
 bool
 EGLImageTextureSource::IsValid() const
 {
   return !!gl();
 }
@@ -611,17 +635,18 @@ gl::GLContext*
 EGLImageTextureHost::gl() const
 {
   return mCompositor ? mCompositor->gl() : nullptr;
 }
 
 bool
 EGLImageTextureHost::Lock()
 {
-  if (!mCompositor) {
+  GLContext* gl = this->gl();
+  if (!gl || !gl->MakeCurrent()) {
     return false;
   }
 
   EGLint status = LOCAL_EGL_CONDITION_SATISFIED;
 
   if (mSync) {
     MOZ_ASSERT(sEGLLibrary.IsExtensionSupported(GLLibraryEGL::KHR_fence_sync));
     status = sEGLLibrary.fClientWaitSync(EGL_DISPLAY(), mSync, 0, LOCAL_EGL_FOREVER);
@@ -652,29 +677,33 @@ EGLImageTextureHost::Lock()
 void
 EGLImageTextureHost::Unlock()
 {
 }
 
 void
 EGLImageTextureHost::SetCompositor(Compositor* aCompositor)
 {
-  MOZ_ASSERT(aCompositor);
+  if (!AssertGLCompositor(aCompositor)) {
+    mCompositor = nullptr;
+    mTextureSource = nullptr;
+    return;
+  }
   CompositorOGL* glCompositor = static_cast<CompositorOGL*>(aCompositor);
   mCompositor = glCompositor;
   if (mTextureSource) {
     mTextureSource->SetCompositor(glCompositor);
   }
 }
 
 gfx::SurfaceFormat
 EGLImageTextureHost::GetFormat() const
 {
   MOZ_ASSERT(mTextureSource);
-  return mTextureSource->GetFormat();
+  return mTextureSource ? mTextureSource->GetFormat() : gfx::SurfaceFormat::UNKNOWN;
 }
 
 //
 
 GLTextureHost::GLTextureHost(TextureFlags aFlags,
                              GLuint aTextureHandle,
                              GLenum aTarget,
                              GLsync aSync,
@@ -696,24 +725,27 @@ gl::GLContext*
 GLTextureHost::gl() const
 {
   return mCompositor ? mCompositor->gl() : nullptr;
 }
 
 bool
 GLTextureHost::Lock()
 {
-  if (!mCompositor) {
+  GLContext* gl = this->gl();
+  if (!gl || !gl->MakeCurrent()) {
     return false;
   }
 
   if (mSync) {
-    gl()->MakeCurrent();
-    gl()->fWaitSync(mSync, 0, LOCAL_GL_TIMEOUT_IGNORED);
-    gl()->fDeleteSync(mSync);
+    if (!gl->MakeCurrent()) {
+      return false;
+    }
+    gl->fWaitSync(mSync, 0, LOCAL_GL_TIMEOUT_IGNORED);
+    gl->fDeleteSync(mSync);
     mSync = 0;
   }
 
   if (!mTextureSource) {
     gfx::SurfaceFormat format = mHasAlpha ? gfx::SurfaceFormat::R8G8B8A8
                                           : gfx::SurfaceFormat::R8G8B8X8;
     mTextureSource = new GLTextureSource(mCompositor,
                                          mTexture,
@@ -723,25 +755,29 @@ GLTextureHost::Lock()
                                          false /* owned by the client */);
   }
 
   return true;
 }
 void
 GLTextureHost::SetCompositor(Compositor* aCompositor)
 {
-  MOZ_ASSERT(aCompositor);
+  if (!AssertGLCompositor(aCompositor)) {
+    mCompositor = nullptr;
+    mTextureSource = nullptr;
+    return;
+  }
   CompositorOGL* glCompositor = static_cast<CompositorOGL*>(aCompositor);
   mCompositor = glCompositor;
   if (mTextureSource) {
     mTextureSource->SetCompositor(glCompositor);
   }
 }
 
 gfx::SurfaceFormat
 GLTextureHost::GetFormat() const
 {
   MOZ_ASSERT(mTextureSource);
-  return mTextureSource->GetFormat();
+  return mTextureSource ? mTextureSource->GetFormat() : gfx::SurfaceFormat::UNKNOWN;
 }
 
 } // namespace layers
 } // namespace mozilla
--- a/gfx/layers/opengl/TextureHostOGL.h
+++ b/gfx/layers/opengl/TextureHostOGL.h
@@ -516,12 +516,14 @@ protected:
   const EGLImage mImage;
   const EGLSync mSync;
   const gfx::IntSize mSize;
   const bool mHasAlpha;
   RefPtr<CompositorOGL> mCompositor;
   RefPtr<EGLImageTextureSource> mTextureSource;
 };
 
+bool AssertGLCompositor(Compositor* aCompositor);
+
 } // namespace layers
 } // namespace mozilla
 
 #endif /* MOZILLA_GFX_TEXTUREOGL_H */