Bug 1444373 - Don't crash when trying to draw an empty RectTextureImage. r=mattwoodrow
authorMarkus Stange <mstange@themasta.com>
Mon, 09 Apr 2018 17:07:58 -0400
changeset 779692 74863f508a8ca7f8513d713a5e4ea381efac1173
parent 779691 49a092b664a6361c2898dc0707a642631f87be78
child 779693 6561515c80abd663f37e5b319abb6c611d51b5f6
push id105836
push userbmo:ato@sny.no
push dateTue, 10 Apr 2018 12:07:22 +0000
reviewersmattwoodrow
bugs1444373
milestone61.0a1
Bug 1444373 - Don't crash when trying to draw an empty RectTextureImage. r=mattwoodrow MozReview-Commit-ID: EsnxswIaABQ
widget/cocoa/RectTextureImage.h
widget/cocoa/RectTextureImage.mm
--- a/widget/cocoa/RectTextureImage.h
+++ b/widget/cocoa/RectTextureImage.h
@@ -59,17 +59,17 @@ public:
 
   void Draw(mozilla::layers::GLManager* aManager,
             const LayoutDeviceIntPoint& aLocation,
             const gfx::Matrix4x4& aTransform = gfx::Matrix4x4());
 
 
 protected:
   void DeleteTexture();
-  void BindIOSurfaceToTexture(gl::GLContext* aGL);
+  bool BindIOSurfaceToTexture(gl::GLContext* aGL);
 
   RefPtr<MacIOSurface> mIOSurface;
   gl::GLContext* mGLContext;
   LayoutDeviceIntRegion mUpdateRegion;
   LayoutDeviceIntSize mBufferSize;
   GLuint mTexture;
   bool mInUpdate;
 };
--- a/widget/cocoa/RectTextureImage.mm
+++ b/widget/cocoa/RectTextureImage.mm
@@ -103,17 +103,20 @@ RectTextureImage::UpdateFromCGContext(co
 
 void
 RectTextureImage::Draw(layers::GLManager* aManager,
                        const LayoutDeviceIntPoint& aLocation,
                        const gfx::Matrix4x4& aTransform)
 {
   gl::GLContext* gl = aManager->gl();
 
-  BindIOSurfaceToTexture(gl);
+  bool bound = BindIOSurfaceToTexture(gl);
+  if (!bound) {
+    return;
+  }
 
   layers::ShaderProgramOGL* program =
     aManager->GetProgram(LOCAL_GL_TEXTURE_RECTANGLE_ARB,
                          gfx::SurfaceFormat::R8G8B8A8);
 
   gl->fActiveTexture(LOCAL_GL_TEXTURE0);
   gl::ScopedBindTexture texture(gl, mTexture, LOCAL_GL_TEXTURE_RECTANGLE_ARB);
 
@@ -136,19 +139,25 @@ RectTextureImage::DeleteTexture()
   if (mTexture) {
     MOZ_ASSERT(mGLContext);
     mGLContext->MakeCurrent();
     mGLContext->fDeleteTextures(1, &mTexture);
     mTexture = 0;
   }
 }
 
-void
+bool
 RectTextureImage::BindIOSurfaceToTexture(gl::GLContext* aGL)
 {
+  if (!mIOSurface) {
+    // If our size is zero or MacIOSurface::CreateIOSurface failed for some
+    // other reason, there's nothing we can bind.
+    return false;
+  }
+
   if (!mTexture) {
     MOZ_ASSERT(aGL);
     aGL->fGenTextures(1, &mTexture);
     aGL->fActiveTexture(LOCAL_GL_TEXTURE0);
     gl::ScopedBindTexture texture(aGL, mTexture, LOCAL_GL_TEXTURE_RECTANGLE_ARB);
     aGL->fTexParameteri(LOCAL_GL_TEXTURE_RECTANGLE_ARB,
                         LOCAL_GL_TEXTURE_MIN_FILTER,
                         LOCAL_GL_LINEAR);
@@ -162,12 +171,14 @@ RectTextureImage::BindIOSurfaceToTexture
                         LOCAL_GL_TEXTURE_WRAP_S,
                         LOCAL_GL_CLAMP_TO_EDGE);
 
     mIOSurface->CGLTexImageIOSurface2D(aGL,
                                        gl::GLContextCGL::Cast(aGL)->GetCGLContext(),
                                        0);
     mGLContext = aGL;
   }
+
+  return true;
 }
 
 } // namespace widget
 } // namespace mozilla